Paranoid/Input.pm

filter: Statementstmt 98.4% 123 / 125 Branchbran 83.8% 57 / 68 Conditioncond 50.0% 9 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 86.5% 206 / 238 CC 47 SCAR 28.3
CC
47
Cov
90%
detaint
26.3
pchomp
14.8
stringMatch
9.8
CRAP 17.0
« Paranoid/Log.pm ↑ Summary Paranoid/Data/AVLTree.pm »
1 # Paranoid::Input -- Paranoid Input functions
2 #
3 # $Id: lib/Paranoid/Input.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Input;
33  
34 64 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 64 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 64 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 64 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 64 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 64 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 64 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
45  
46 64 @EXPORT = qw(detaint stringMatch pchomp);
47 64 @EXPORT_OK = ( @EXPORT, qw(NEWLINE_REGEX) );
48 64 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
49  
50 64 ▶use constant NEWLINE_REGEX => qr#(?:\15\12|\15|\12)#so;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 #####################################################################
53 #
54 # Module code follows
55 #
56 #####################################################################
57  
58 51 ▶sub pchomp (;\[$@%]) {
Subroutine pchomp: called CC=14 SCAR=26.9
Pod documented
59  
60 # Purpose: Platform neutral chomping
61 # Returns: same as chomp
62 # Usage: $n = pchomp($string);
63  
64 51 my ($ref) = @_;
65 51 my $rv = 0;
66 51 my $nl = NEWLINE_REGEX;
67 51 my $e;
68  
69 # If no args were passed work on $_
70 51 ▶ $ref = \$_ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 50 51
71  
72 # slurp-mode bypass
73 51 ▶ return $rv unless defined $/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $/ 1 50 51
74  
75 50 ▶ if ( ref $ref eq 'SCALAR' and defined $$ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'SCALAR' and defined $$ref) { } 8 42 50
elsif (ref $ref eq 'ARRAY') { } 41 1 42
elsif (ref $ref eq 'HASH') { } 1 0 1
Condition66%2/3
ref $ref eq "SCALAR" and defined $$ref
!ll&&!rl&&r
4208
MC/DC50%1/2
ref $ref eq "SCALAR" and defined $$ref
ref $ref eq "SCALAR" defined $$ref
Truth table66%2/3
A and B
A= ref $ref eq "SCALAR"B= defined $$ref
ABresult
0X0
100
111
76 8 ▶ if ( $/ =~ /^$nl$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($/ =~ /^$nl$/so) { } 5 3 8
77 5 $e = length $$ref;
78 5 $$ref =~ s/$nl$//so;
79 5 $rv = $e - length $$ref;
80 } else {
81 3 $rv = chomp $$ref;
82 }
83 } elsif ( ref $ref eq 'ARRAY' ) {
84 41 ▶ if ( $/ =~ /^$nl$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($/ =~ /^$nl$/so) { } 41 0 41
85 41 foreach (@$ref) {
86 1676 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 1676 1676
87 1676 $e = length $_;
88 1676 $_ =~ s/$nl$//so;
89 1676 $rv += $e - length $_;
90 }
91 } else {
92 0 $rv = chomp @$ref;
93 }
94 } elsif ( ref $ref eq 'HASH' ) {
95 1 ▶ if ( $/ =~ /^$nl$/so ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($/ =~ /^$nl$/so) { } 1 0 1
96 1 foreach ( keys %$ref ) {
97 4 ▶ next unless defined $$ref{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref->{$_} 0 4 4
98 4 $e = length $$ref{$_};
99 4 $$ref{$_} =~ s/$nl$//so;
100 4 $rv += $e - length $$ref{$_};
101 }
102 } else {
103 0 $rv = chomp %$ref;
104 }
105 }
106  
107 50 return $rv;
108 }
109  
110 64 our %regexes = (
111 alphabetic => qr/[a-z]+/si,
112 alphanumeric => qr/[a-z0-9]+/si,
113 alphawhite => qr/[a-z\s]+/si,
114 alnumwhite => qr/[a-z0-9\s]+/si,
115 email => qr/[a-z][\w\.\-]*\@(?:[a-z0-9][a-z0-9\-]*\.)*[a-z0-9]+/si,
116 filename => qr#[/ \w\-\.:,@\+]+\[?#s,
117 fileglob => qr#[/ \w\-\.:,@\+\*\?\{\}\[\]]+\[?#s,
118 hostname => qr#(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?#s,
119 ipv4addr =>
120 qr/(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])/s,
121 ipv4netaddr =>
122 qr#(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])/(?:(?:\d|[12]\d|3[0-2])|(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5]))#s,
123 ipv6addr => qr/
124 :(?::[abcdef\d]{1,4}){1,7} |
125 [abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} |
126 (?:[abcdef\d]{1,4}:){1,7}:
127 /six,
128 ipv6netaddr => qr#(?::(?::[abcdef\d]{1,4}){1,7}|
129 [abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} |
130 (?:[abcdef\d]{1,4}:){1,7}:)/(?:\d\d?|1(?:[01]\d|2[0-8]))#six,
131 login => qr/[a-z][\w\.\-]*/si,
132 nometa => qr/[^\%\`\$\!\@]+/s,
133 number => qr/[+\-]?[0-9]+(?:\.[0-9]+)?/s,
134 'int' => qr/[-+]?\d+/s,
135 uint => qr/\d+/s,
136 float => qr/[-+]?\d+(?:\.\d+)/s,
137 ufloat => qr/\d+(?:\.\d+)/s,
138 bin => qr/[01]+/s,
139 octal => qr/[0-7]+/s,
140 'hex' => qr/[a-z0-9]+/si,
141 );
142  
143 938 ▶sub detaint (\[$@%]$;\[$@%]) {
Subroutine detaint: called CC=26 SCAR=32.7
Pod documented
144  
145 # Purpose: Detaints and validates input in one call
146 # Returns: True (1) if detainting was successful,
147 # False (0) if there are any errors
148 # Usage: $rv = detaint($input, $dataType, $detainted);
149 # Usage: $rv = detaint(@input, $dataType, @detainted);
150 # Usage: $rv = detaint(%input, $dataType, %detainted);
151  
152 938 my $iref = shift;
153 938 my $type = shift;
154 938 my $oref = shift;
155 938 my $po = defined $oref;
156 938 my $rv = 0;
157 938 my ( $regex, $tmp );
158  
159 938 subPreamble( PDLEVEL1, '\[$@%]$;\[$@%]', $iref, $type, $oref );
160  
161 # Make sure input and output data types match
162 938 ▶ croak "$iref and $oref aren't compatible data types"
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $oref or ref $iref eq ref $oref 0 938 938
Condition66%2/3
not defined $oref or ref $iref eq ref $oref
l!l&&r!l&&!r
5833550
MC/DC0%0/2
not defined $oref or ref $iref eq ref $oref
not defined $oref ref $iref eq ref $oref
Truth table66%2/3
not(A) or B
A= not defined $orefB= ref $iref eq ref $oref
ABresult
1X1
011
000
163 unless !defined $oref
164 or ref $iref eq ref $oref;
165  
166 # Warn on unknown regexes
167 938 ▶ if ( ref $type eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'Regexp') { } 1 937 938
168 1 $regex = $type;
169 1 $type = 'custom';
170 } else {
171 937 ▶ if ( defined $type and exists $regexes{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and exists $Paranoid::Input::regexes{$type}) { } 936 1 937
Condition66%2/3
defined $type and exists $Paranoid::Input::regexes{$type}
!ll&&!rl&&r
01936
MC/DC50%1/2
defined $type and exists $Paranoid::Input::regexes{$type}
defined $type exists $Paranoid::Input::regexes{$type}
Truth table66%2/3
A and B
A= defined $typeB= exists $Paranoid::Input::regexes{$type}
ABresult
0X0
100
111
172 936 $regex = $regexes{$type};
173 } else {
174 1 pdebug( 'unknown regex type requested: %s', PDLEVEL1, $type );
175 }
176 }
177  
178 # Create a reference structure under $oref if none was passed
179 938 ▶ unless ( defined $oref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $oref) 583 355 938
180 583 ▶ $oref =
Branch: 2 branches
BranchTrueFalseTotal
ref $iref eq 'ARRAY' ? : 2 581 583
ref $iref eq 'HASH' ? : 2 579 581
181 ref $iref eq 'ARRAY' ? []
182 : ref $iref eq 'HASH' ? {}
183 : \$tmp;
184 }
185  
186 # Make sure $oref is empty
187 938 ▶ if ( ref $oref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $oref eq 'SCALAR') { } 930 8 938
elsif (ref $oref eq 'ARRAY') { } 4 4 8
188 930 $$oref = undef;
189 } elsif ( ref $oref eq 'ARRAY' ) {
190 4 @$oref = ();
191 } else {
192 4 %$oref = ();
193 }
194  
195 # Start working
196 938 ▶ if ( defined $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex) 937 1 938
197 937 ▶ if ( ref $iref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $iref eq 'SCALAR') { } 929 8 937
elsif (ref $iref eq 'ARRAY') { } 4 4 8
198 929 pdebug( 'evaluating (%s)', PDLEVEL2, $$iref );
199 929 ▶ ($$oref) = ( $$iref =~ /^($regex)$/s )
Branch: 1 branch
BranchTrueFalseTotal
if defined $$iref 928 1 929
200 if defined $$iref;
201 929 $rv = defined $$oref;
202 } elsif ( ref $iref eq 'ARRAY' ) {
203 4 ▶ if ( scalar @$iref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$iref) 4 0 4
204 4 $rv = 1;
205 4 foreach (@$iref) {
206 11 pdebug( 'evaluating (%s)', PDLEVEL2, $_ );
207 11 ▶ ( $$oref[ $#{$oref} + 1 ] ) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 11 0 11
208 defined $_ ? m/^($regex)$/s : (undef);
209 11 ▶ $rv = 0 unless defined $$oref[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oref->[-1] 3 8 11
210 11 pdebug( 'got (%s)', PDLEVEL2, $$oref[-1] );
211 }
212 }
213 4 $rv = !scalar grep { !defined } @$oref;
214 } else {
215 4 ▶ if ( scalar keys %$iref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$iref) 4 0 4
216 4 $rv = 1;
217 4 foreach ( keys %$iref ) {
218 11 pdebug( 'evaluating (%s)', PDLEVEL2, $$iref{$_} );
219 ( $$oref{$_} ) =
220 defined $$iref{$_}
221 11 ▶ ? ( $$iref{$_} =~ m/^($regex)$/s )
Branch: 1 branch
BranchTrueFalseTotal
defined $iref->{$_} ? : 11 0 11
222 : undef;
223 11 ▶ $rv = 0 unless defined $$oref{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oref->{$_} 3 8 11
224 }
225 }
226 }
227 }
228  
229 # Copy everything back to $iref if needed
230 938 ▶ unless ($po) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($po) 583 355 938
231 583 ▶ if ( ref $iref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $iref eq 'SCALAR') { } 579 4 583
elsif (ref $iref eq 'ARRAY') { } 2 2 4
232 579 $$iref = $$oref;
233 } elsif ( ref $iref eq 'ARRAY' ) {
234 2 @$iref = @$oref;
235 } else {
236 2 %$iref = %$oref;
237 }
238 }
239  
240 938 subPostamble( PDLEVEL1, '$', $rv );
241  
242 938 return $rv;
243 }
244  
245 2 ▶sub stringMatch ($@) {
Subroutine stringMatch: called CC=9 SCAR=22.8
Pod documented
246  
247 # Purpose: Looks for occurrences of strings and/or regexes in the passed
248 # input
249 # Returns: True (1) any of the strings/regexes match,
250 # False (0), otherwise
251 # Usage: $rv = stringMatch($input, @words);
252  
253 2 my $input = shift;
254 2 my @match = splice @_;
255 2 my $rv = 0;
256 2 my @regex;
257  
258 2 subPreamble( PDLEVEL1, '$@', $input, @match );
259  
260 2 ▶ if ( defined $input and @match ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and @match) 2 0 2
Condition33%1/3
defined $input and @match
!ll&&!rl&&r
002
MC/DC0%0/2
defined $input and @match
defined $input @match
Truth table33%1/3
A and B
A= defined $inputB= @match
ABresult
0X0
100
111
261  
262 # Populate @regex w/regexes
263 2 ▶ @regex = grep { defined $_ && ref $_ eq 'Regexp' } @match;
Condition33%1/3
defined $_ && ref $_ eq 'Regexp'
!ll&&!rl&&r
060
MC/DC0%0/2
defined $_ && ref $_ eq 'Regexp'
defined $_ ref $_ eq 'Regexp'
Truth table33%1/3
A && B
A= defined $_B= ref $_ eq 'Regexp'
ABresult
0X0
100
111
264  
265 # Convert remaining strings to regexes
266 2 ▶ foreach ( grep { defined $_ && ref $_ ne 'Regexp' } @match ) {
Condition33%1/3
defined $_ && ref $_ ne 'Regexp'
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_ && ref $_ ne 'Regexp'
defined $_ ref $_ ne 'Regexp'
Truth table33%1/3
A && B
A= defined $_B= ref $_ ne 'Regexp'
ABresult
0X0
100
111
267 6 ▶ push @regex, m#^/(.+)/$#s ? qr#$1#si : qr#\Q$_\E#si;
Branch: 1 branch
BranchTrueFalseTotal
m[^/(.+)/$]s ? : 4 2 6
268 }
269  
270 # Start comparisons
271 2 study $input;
272 2 foreach my $r (@regex) {
273 3 ▶ if ( $input =~ /$r/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /$r/is) 1 2 3
274 1 $rv = 1;
275 1 last;
276 }
277 }
278 }
279  
280 2 subPostamble( PDLEVEL1, '$', $rv );
281  
282 2 return $rv;
283 }
284  
285 64 1;
286  
287 __END__
« Paranoid/Log.pm ↑ Summary Paranoid/Data/AVLTree.pm »