Paranoid/Data.pm

filter: Statementstmt 96.1% 148 / 154 Branchbran 84.2% 59 / 70 Conditioncond 76.1% 16 / 21 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 90.4% 255 / 282 CC 45 SCAR 22.0
CC
45
Cov
91%
__ANON__
14.0
_cmpArray
13.0
deepCmp
12.3
CRAP 9.1
« Paranoid/Network/IPv6.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/BATHeader.pm »
1 # Paranoid::Data -- Misc. Data Manipulation Functions
2 #
3 # $Id: lib/Paranoid/Data.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::Data;
33  
34 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 15 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 15 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 15 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 15 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 15 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
45  
46 15 @EXPORT = qw(deepCopy deepCmp has64bInt quad2Longs longs2Quad);
47 15 @EXPORT_OK = @EXPORT;
48 15 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
49  
50 15 ▶use constant MAX32VAL => 0b11111111_11111111_11111111_11111111;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 15 ▶use constant TEST32INT => 1 << 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #####################################################################
54 #
55 # Module code follows
56 #
57 #####################################################################
58  
59 9 ▶sub deepCopy (\[$@%]\[$@%]) {
Subroutine deepCopy: called CC=8 SCAR=20.8
Pod documented
60  
61 # Purpose: Attempts to safely copy an arbitrarily deep data
62 # structure from the source to the target
63 # Returns: True or False
64 # Usage: $rv = deepCopy($source, $target);
65 # Usage: $rv = deepCopy(@source, @target);
66 # Usage: $rv = deepCopy(%source, %target);
67  
68 9 my $source = shift;
69 9 my $target = shift;
70 9 my $rv = 1;
71 9 my $counter = 0;
72 9 ▶ my $sref = defined $source ? ref $source : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $source ? : 9 0 9
73 9 ▶ my $tref = defined $target ? ref $target : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $target ? : 9 0 9
74 9 my ( @refs, $recurseSub );
75  
76 9 subPreamble( PDLEVEL1, '$$', $source, $target );
77  
78 9 ▶ croak 'source and target must be identical data types'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sref eq ref $tref 0 9 9
79 unless ref $sref eq ref $tref;
80  
81 25 ▶ $recurseSub = sub {
Subroutine __ANON__: called CC=14 SCAR=26.4
82 25 my $s = shift;
83 25 my $t = shift;
84 25 my $type = ref $s;
85 25 my $irv = 1;
86 25 my ( $key, $value );
87  
88 # We'll grep the @refs list to make sure there's no
89 # circular references going on
90 25 ▶ if ( grep { $_ eq $s } @refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $s;} @refs) 1 24 25
91 1 Paranoid::ERROR = pdebug(
92 'Found a circular reference in data structure: ' . '(%s) %s',
93 PDLEVEL1, $s, @refs
94 );
95 1 return 0;
96 }
97  
98 # Push the reference onto the list
99 24 push @refs, $s;
100  
101 # Copy data over
102 24 ▶ if ( $type eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 12 12 24
elsif ($type eq 'HASH') { } 12 0 12
103  
104 # Copy over array elements
105 12 foreach my $element (@$s) {
106  
107 42 $type = ref $element;
108 42 $counter++;
109 42 ▶ if ( $type eq 'ARRAY' or $type eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY' or $type eq 'HASH') { } 7 35 42
Condition100%3/3
$type eq "ARRAY" or $type eq "HASH"
l!l&&r!l&&!r
5235
MC/DC100%2/2
$type eq "ARRAY" or $type eq "HASH"
$type eq "ARRAY" $type eq "HASH"
Truth table100%3/3
A or B
A= $type eq "ARRAY"B= $type eq "HASH"
ABresult
1X1
011
000
110  
111 # Copy over sub arrays or hashes
112 7 ▶ push @$t, $type eq 'ARRAY' ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 5 2 7
113 7 ▶ return 0 unless &$recurseSub( $element, $$t[-1] );
Branch: 1 branch
BranchTrueFalseTotal
unless &$recurseSub($element, $t->[-1]) 3 4 7
114  
115 } else {
116  
117 # Copy over everything else as-is
118 35 push @$t, $element;
119 }
120 }
121  
122 } elsif ( $type eq 'HASH' ) {
123 12 ▶ while ( ( $key, $value ) = each %$s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$s) 33 11 44
124 33 $type = ref $value;
125 33 $counter++;
126 33 ▶ if ( $type eq 'ARRAY' or $type eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY' or $type eq 'HASH') { } 10 23 33
Condition100%3/3
$type eq "ARRAY" or $type eq "HASH"
l!l&&r!l&&!r
4623
MC/DC100%2/2
$type eq "ARRAY" or $type eq "HASH"
$type eq "ARRAY" $type eq "HASH"
Truth table100%3/3
A or B
A= $type eq "ARRAY"B= $type eq "HASH"
ABresult
1X1
011
000
127  
128 # Copy over sub arrays or hashes
129 10 ▶ $$t{$key} = $type eq 'ARRAY' ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 4 6 10
130 10 ▶ return 0 unless &$recurseSub( $value, $$t{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless &$recurseSub($value, $t->{$key}) 1 9 10
131  
132 } else {
133  
134 # Copy over everything else as-is
135 23 $$t{$key} = $value;
136 }
137 }
138 }
139  
140 # We're done, so let's remove the reference we were working on
141 20 pop @refs;
142  
143 20 return 1;
144 9 };
145  
146 # Start the copy
147 9 ▶ if ( $sref eq 'ARRAY' or $sref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq 'ARRAY' or $sref eq 'HASH') { } 8 1 9
Condition100%3/3
$sref eq "ARRAY" or $sref eq "HASH"
l!l&&r!l&&!r
441
MC/DC100%2/2
$sref eq "ARRAY" or $sref eq "HASH"
$sref eq "ARRAY" $sref eq "HASH"
Truth table100%3/3
A or B
A= $sref eq "ARRAY"B= $sref eq "HASH"
ABresult
1X1
011
000
148  
149 # Copy over arrays & hashes
150 8 ▶ if ( $sref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq 'ARRAY') { } 4 4 8
151 4 @$target = ();
152 } else {
153 4 %$target = ();
154 }
155 8 $rv = &$recurseSub( $source, $target );
156  
157 } else {
158  
159 # Copy over everything else directly
160 1 $$target = $$source;
161 1 $counter++;
162 }
163  
164 9 ▶ $rv = $counter if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 8 1 9
165  
166 9 subPostamble( PDLEVEL1, '$', $rv );
167  
168 9 return $rv;
169 }
170  
171 41 ▶sub _cmpArray (\@\@) {
Subroutine _cmpArray: called CC=13 SCAR=25.6
172  
173 # Purpose: Compares arrays, returns true if identical
174 # Returns: Boolean
175 # Usage: $rv = _cmpArray(@array1, @array2);
176  
177 41 my $ref1 = shift;
178 41 my $ref2 = shift;
179 41 my $rv = 1;
180 41 my $i = 0;
181 41 my ( $n, $d1, $d2, $t1, $t2 );
182  
183 41 subPreamble( PDLEVEL2, '$$', $ref1, $ref2 );
184  
185 41 $rv = scalar @$ref1 == scalar @$ref2;
186 41 $n = scalar @$ref1;
187  
188 # Compare contents if there is any
189 41 ▶ if ( $rv and $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $n) 39 2 41
Condition100%3/3
$rv and $n
!ll&&!rl&&r
1139
MC/DC100%2/2
$rv and $n
$rv $n
Truth table100%3/3
A and B
A= $rvB= $n
ABresult
0X0
100
111
190 39 ▶ while ( $i <= $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 172 28 200
191  
192 # Collect some meta data
193 172 $d1 = defined $$ref1[$i];
194 172 $d2 = defined $$ref2[$i];
195 172 ▶ $t1 = $d1 ? ref $$ref1[$i] : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
$d1 ? : 130 42 172
196 172 ▶ $t2 = $d2 ? ref $$ref2[$i] : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
$d2 ? : 130 42 172
197  
198 172 ▶ if ( $d1 == $d2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d1 == $d2) { } 170 2 172
199  
200 # Both are undefined, so move to the next item
201 170 ▶ unless ($d1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d1) 41 129 170
202 41 $i++;
203 41 next;
204 }
205  
206 # Both are defined, so check for type
207 129 $rv = $t1 eq $t2;
208  
209 129 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 127 2 129
210  
211 # The types are the same, so do some comparisons
212 127 ▶ if ( $t1 eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t1 eq 'ARRAY') { } 6 121 127
elsif ($t1 eq 'HASH') { } 9 112 121
213 6 $rv = deepCmp( $$ref1[$i], $$ref2[$i] );
214 } elsif ( $t1 eq 'HASH' ) {
215 9 $rv = deepCmp( $$ref1[$i], $$ref2[$i] );
216 } else {
217  
218 # Compare scalar value of all other types
219 112 $rv = $$ref1[$i] eq $$ref2[$i];
220 }
221 }
222  
223 } else {
224  
225 # One of the two are undefined, so quick exit
226 2 $rv = 0;
227 }
228  
229 # Early exit if we've found a difference already
230 131 ▶ last unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 11 120 131
231  
232 # Otherwise, on to the next element
233 120 $i++;
234 }
235 }
236  
237 # A little explicit sanitizing of input for false returns
238 41 ▶ $rv = 0 unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 12 29 41
239  
240 41 subPostamble( PDLEVEL2, '$', $rv );
241  
242 41 return $rv;
243 }
244  
245 13 ▶sub _cmpHash (\%\%) {
Subroutine _cmpHash: called CC=3 SCAR=11.0
246  
247 # Purpose: Compares hashes, returns true if identical
248 # Returns: Boolean
249 # Usage: $rv = _cmpHash(%hash1, %hash2);
250  
251 13 my $ref1 = shift;
252 13 my $ref2 = shift;
253 13 my $rv = 1;
254 13 my ( @k1, @k2, @v1, @v2 );
255  
256 13 subPreamble( PDLEVEL2, '$$', $ref1, $ref2 );
257  
258 13 @k1 = sort keys %$ref1;
259 13 @k2 = sort keys %$ref2;
260  
261 # Compare first by key list
262 13 $rv = _cmpArray( @k1, @k2 );
263  
264 13 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 12 1 13
265  
266 # Compare by value list
267 12 foreach (@k1) {
268 40 push @v1, $$ref1{$_};
269 40 push @v2, $$ref2{$_};
270 }
271 12 $rv = _cmpArray( @v1, @v2 );
272 }
273  
274 13 subPostamble( PDLEVEL2, '$', $rv );
275  
276 13 return $rv;
277 }
278  
279 29 ▶sub deepCmp (\[$@%]\[$@%]) {
Subroutine deepCmp: called CC=8 SCAR=25.1
Pod documented
280  
281 # Purpose: Compares data structures, returns true if identical
282 # Returns: Boolean
283 # Usage: $rv = deepCmp(%hash1, %hash2);
284 # Usage: $rv = deepCmp(@array1, @arrays2);
285  
286 29 my $ref1 = shift;
287 29 my $ref2 = shift;
288 29 my $rv = 1;
289  
290 29 subPreamble( PDLEVEL1, '$$', $ref1, $ref2 );
291  
292 29 ▶ unless ( ref $ref1 eq ref $ref1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref1 eq ref $ref1) 0 29 29
293 0 $rv = 0;
294 0 Paranoid::ERROR =
295 pdebug( 'data structures are not the same type', PDLEVEL1 );
296 }
297  
298 29 ▶ if ( $rv and ref $ref1 eq 'SCALAR' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rv and ref $ref1 eq 'SCALAR') { } 0 29 29
elsif ($rv and ref $ref1 eq 'ARRAY') { } 16 13 29
elsif ($rv and ref $ref1 eq 'HASH') { } 13 0 13
Condition33%1/3
$rv and ref $ref1 eq "SCALAR"
!ll&&!rl&&r
0290
MC/DC0%0/2
$rv and ref $ref1 eq "SCALAR"
$rv ref $ref1 eq "SCALAR"
Truth table33%1/3
A and B
A= $rvB= ref $ref1 eq "SCALAR"
ABresult
0X0
100
111
299 0 $rv = $ref1 eq $ref2;
300 ▶ } elsif ( $rv and ref $ref1 eq 'ARRAY' ) {
Condition66%2/3
$rv and ref $ref1 eq "ARRAY"
!ll&&!rl&&r
01316
MC/DC50%1/2
$rv and ref $ref1 eq "ARRAY"
$rv ref $ref1 eq "ARRAY"
Truth table66%2/3
A and B
A= $rvB= ref $ref1 eq "ARRAY"
ABresult
0X0
100
111
301 16 $rv = _cmpArray( @$ref1, @$ref2 );
302 ▶ } elsif ( $rv and ref $ref1 eq 'HASH' ) {
Condition33%1/3
$rv and ref $ref1 eq "HASH"
!ll&&!rl&&r
0013
MC/DC0%0/2
$rv and ref $ref1 eq "HASH"
$rv ref $ref1 eq "HASH"
Truth table33%1/3
A and B
A= $rvB= ref $ref1 eq "HASH"
ABresult
0X0
100
111
303 13 $rv = _cmpHash( %$ref1, %$ref2 );
304 } else {
305 0 $rv = 0;
306 0 Paranoid::ERROR =
307 pdebug( 'called with non-simple data types', PDLEVEL1 );
308 }
309  
310 29 subPostamble( PDLEVEL1, '$', $rv );
311  
312 29 return $rv;
313 }
314  
315 12966 ▶sub has64bInt {
Subroutine has64bInt: called CC=1 SCAR=0.0
Pod documented
316  
317 # Purpose: Returns whether the current platform supports 64b integers
318 # Returns: Boolean
319 # Usage: $rv = has64bInt();
320  
321 12966 return TEST32INT == 1 ? 0 : 1;
322 }
323  
324 1321 ▶sub quad2Longs {
Subroutine quad2Longs: called CC=2 SCAR=7.0
Pod documented
325  
326 # Purpose: Splits a quad into long integers
327 # Returns: Array of Longs (low bytes, high bytes)
328 # Usage: ($low, $high) = quad2Longs($quad);
329  
330 1321 my $quad = shift;
331 1321 my ( $upper, $lower );
332  
333 # Extract lower 32 bits
334 1321 $lower = $quad & MAX32VAL;
335  
336 # Extract upper 32 bits
337 1321 ▶ $upper = has64bInt() ? ( $quad & ~MAX32VAL ) >> 32 : 0;
Branch: 1 branch
BranchTrueFalseTotal
has64bInt() ? : 1321 0 1321
338  
339 1321 return ( $lower, $upper );
340 }
341  
342 11645 ▶sub longs2Quad {
Subroutine longs2Quad: called CC=3 SCAR=12.3
Pod documented
343  
344 # Purpose: Joins two longs into a quad (if supported)
345 # Returns: Quad Integer/undef
346 # Usage: $quad = longs2Quad($low, $high);
347  
348 11645 my $low = shift;
349 11645 my $high = shift;
350 11645 my $quad;
351  
352 11645 ▶ if ( has64bInt() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (has64bInt()) { } 11645 0 11645
353 11645 $quad = $low | ( $high << 32 );
354 } else {
355 0 ▶ $quad = $low if $high == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $high == 0 0 0 0
356 }
357  
358 11645 return $quad;
359 }
360  
361 15 1;
362  
363 __END__
« Paranoid/Network/IPv6.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/BATHeader.pm »