lib/Parse/Win32Registry/Base.pm

filter: Statementstmt 86.2% 434 / 503 Branchbran 80.8% 181 / 224 Conditioncond 65.1% 71 / 109 MC/DCmcdc 44.7% 30 / 67 Subroutinesub 90.7% 69 / 76 Podpod 0.0% 0 / 18 totaltotal 78.7% 785 / 997 CC 161 SCAR 24.4
CC
161
Cov
82%
as_stanza
90.0
_compare_keys
22.7
unpack_series
20.0
CRAP 11.4
« lib/Parse/Win32Registry/Key.pm ↑ Summary lib/Parse/Win32Registry/WinNT/Key.pm »
1 package Parse::Win32Registry::Base;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 our @EXPORT_OK = qw(
13 warnf
14 iso8601
15 hexdump
16 format_octets
17 unpack_windows_time
18 unpack_string
19 unpack_unicode_string
20 unpack_guid
21 unpack_sid
22 unpack_ace
23 unpack_acl
24 unpack_security_descriptor
25 unpack_series
26 make_multiple_subkey_iterator
27 make_multiple_value_iterator
28 make_multiple_subtree_iterator
29 compare_multiple_keys
30 compare_multiple_values
31 REG_NONE
32 REG_SZ
33 REG_EXPAND_SZ
34 REG_BINARY
35 REG_DWORD
36 REG_DWORD_BIG_ENDIAN
37 REG_LINK
38 REG_MULTI_SZ
39 REG_RESOURCE_LIST
40 REG_FULL_RESOURCE_DESCRIPTOR
41 REG_RESOURCE_REQUIREMENTS_LIST
42 REG_QWORD
43 );
44  
45 13 our %EXPORT_TAGS = (
46 all => [@EXPORT_OK],
47 );
48  
49 13 ▶use constant REG_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 13 ▶use constant REG_SZ => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 13 ▶use constant REG_EXPAND_SZ => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 13 ▶use constant REG_BINARY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 13 ▶use constant REG_DWORD => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 13 ▶use constant REG_DWORD_BIG_ENDIAN => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 13 ▶use constant REG_LINK => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 13 ▶use constant REG_MULTI_SZ => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 13 ▶use constant REG_RESOURCE_LIST => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 13 ▶use constant REG_FULL_RESOURCE_DESCRIPTOR => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 13 ▶use constant REG_RESOURCE_REQUIREMENTS_LIST => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 13 ▶use constant REG_QWORD => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 13 our $WARNINGS = 0;
63  
64 13 our $CODEPAGE = 'cp1252';
65  
66 455 ▶sub warnf {
Subroutine warnf: called CC=2 SCAR=6.9
Pod undocumented
67 455 my $message = shift;
68 455 ▶ warn sprintf "$message\n", @_ if $WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
if $Parse::Win32Registry::Base::WARNINGS 31 424 455
69 }
70  
71 40 ▶sub hexdump {
Subroutine hexdump: called CC=7 SCAR=19.5
Pod undocumented
72 40 my $data = shift; # packed binary data
73 40 ▶ my $start = shift || 0; # starting value for displayed offset
Condition100%2/2
shift() || 0
l!l
346
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
74  
75 40 ▶ return '' if !defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 39 40
76  
77 39 my $output = '';
78  
79 39 my $fake_start = $start & ~0xf;
80 39 my $end = length($data);
81  
82 39 my $pos = 0;
83 39 ▶ if ($fake_start < $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fake_start < $start) 30 9 39
84 30 $output .= sprintf '%8x ', $fake_start;
85 30 my $indent = $start - $fake_start;
86 30 $output .= ' ' x $indent;
87 30 my $row = substr($data, $pos, 16 - $indent);
88 30 my $len = length($row);
89 30 $output .= join(' ', unpack('H2' x $len, $row));
90 30 ▶ if ($indent + $len < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent + $len < 16) 11 19 30
91 11 my $padding = 16 - $len - $indent;
92 11 $output .= ' ' x $padding;
93 }
94 30 $output .= ' ';
95 30 $output .= ' ' x $indent;
96 30 $row =~ tr/\x20-\x7e/./c;
97 30 $output .= $row;
98 30 $output .= "\n";
99 30 $pos += $len;
100 }
101 39 ▶ while ($pos < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $end) 68 39 107
102 68 $output .= sprintf '%8x ', $start + $pos;
103 68 my $row = substr($data, $pos, 16);
104 68 my $len = length($row);
105 68 $output .= join(' ', unpack('H2' x $len, $row));
106 68 ▶ if ($len < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 16) 24 44 68
107 24 my $padding = 16 - $len;
108 24 $output .= ' ' x $padding;
109 }
110 68 $output .= ' ';
111 68 $row =~ tr/\x20-\x7e/./c;
112 68 $output .= $row;
113 68 $output .= "\n";
114 68 $pos += 16;
115 }
116  
117 39 return $output;
118 }
119  
120 70 ▶sub format_octets {
Subroutine format_octets: called CC=8 SCAR=20.8
Pod undocumented
121 70 my $data = shift; # packed binary data
122 70 ▶ my $col = shift || 0; # starting column, e.g. length of initial string
Condition100%2/2
shift() || 0
l!l
655
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
123  
124 70 ▶ return "\n" if !defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 68 70
125  
126 68 my $output = '';
127  
128 68 ▶ $col = 76 if $col > 76;
Branch: 1 branch
BranchTrueFalseTotal
if $col > 76 6 62 68
129 68 my $max_octets = int((76 - $col) / 3) + 1;
130  
131 68 my $end = length($data);
132 68 my $pos = 0;
133 68 my $num_octets = $end - $pos;
134 68 ▶ $num_octets = $max_octets if $num_octets > $max_octets;
Branch: 1 branch
BranchTrueFalseTotal
if $num_octets > $max_octets 27 41 68
135 68 ▶ while ($pos < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $end) 105 68 173
136 105 $output .= join(',', unpack("x$pos(H2)$num_octets", $data));
137 105 $pos += $num_octets;
138 105 $num_octets = $end - $pos;
139 105 ▶ $num_octets = 25 if $num_octets > 25;
Branch: 1 branch
BranchTrueFalseTotal
if $num_octets > 25 19 86 105
140 105 ▶ if ($num_octets > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_octets > 0) 46 59 105
141 46 $output .= ",\\\n ";
142 }
143 }
144 68 $output .= "\n";
145 68 return $output;
146 }
147  
148 535 ▶sub unpack_windows_time {
Subroutine unpack_windows_time: called CC=6 SCAR=18.0
Pod undocumented
149 535 my $data = shift;
150  
151 535 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 535 535
152 0 return;
153 }
154  
155 535 ▶ if (length($data) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 8) 2 533 535
156 2 return;
157 }
158  
159 # The conversion uses real numbers
160 # as 32-bit perl does not provide 64-bit integers.
161 # The equation can be found in several places on the Net.
162 # My thanks go to Dan Sully for Audio::WMA's _fileTimeToUnixTime
163 # which shows a perl implementation of it.
164 533 my ($low, $high) = unpack("VV", $data);
165 533 my $filetime = $high * 2 ** 32 + $low;
166 533 my $epoch_time = int(($filetime - 116444736000000000) / 10000000);
167  
168 # adjust the UNIX epoch time to the local OS's epoch time
169 # (see perlport's Time and Date section)
170 533 my $epoch_offset = timegm(0, 0, 0, 1, 0, 1970);
171 533 $epoch_time += $epoch_offset;
172  
173 533 ▶ if ($epoch_time < 0 || $epoch_time > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($epoch_time < 0 or $epoch_time > 2147483647) 8 525 533
Condition100%3/3
$epoch_time < 0 or $epoch_time > 2147483647
l!l&&r!l&&!r
44525
MC/DC100%2/2
$epoch_time < 0 or $epoch_time > 2147483647
$epoch_time < 0 $epoch_time > 2147483647
Truth table100%3/3
A or B
A= $epoch_time < 0B= $epoch_time > 2147483647
ABresult
1X1
011
000
174 8 $epoch_time = undef;
175 }
176  
177 533 ▶ return wantarray ? ($epoch_time, 8) : $epoch_time;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 23 510 533
178 }
179  
180 138 ▶sub iso8601 {
Subroutine iso8601: called CC=6 SCAR=19.7
Pod undocumented
181 138 my $time = shift;
182 138 my $tz = shift;
183  
184 138 ▶ if (!defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 40 98 138
185 40 return '(undefined)';
186 }
187  
188 98 ▶ if (!defined $tz || $tz ne 'Z') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tz or $tz ne "Z") 98 0 98
Condition33%1/3
not defined $tz or $tz ne "Z"
l!l&&r!l&&!r
9800
MC/DC0%0/2
not defined $tz or $tz ne "Z"
not defined $tz $tz ne "Z"
Truth table33%1/3
not(A) or B
A= not defined $tzB= $tz ne "Z"
ABresult
1X1
011
000
189 98 $tz = 'Z'
190 }
191  
192 # On Windows, gmtime will return undef if $time < 0 or > 0x7fffffff
193 98 ▶ if ($time < 0 || $time > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time < 0 or $time > 2147483647) 0 98 98
Condition33%1/3
$time < 0 or $time > 2147483647
l!l&&r!l&&!r
0098
MC/DC0%0/2
$time < 0 or $time > 2147483647
$time < 0 $time > 2147483647
Truth table33%1/3
A or B
A= $time < 0B= $time > 2147483647
ABresult
1X1
011
000
194 0 return '(undefined)';
195 }
196 98 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime $time;
197  
198 # The final 'Z' indicates UTC ("zero meridian")
199 98 return sprintf '%04d-%02d-%02dT%02d:%02d:%02d%s',
200 1900+$year, 1+$mon, $mday, $hour, $min, $sec, $tz;
201 }
202  
203 12 ▶sub unpack_string {
Subroutine unpack_string: called CC=4 SCAR=13.9
Pod undocumented
204 12 my $data = shift;
205  
206 12 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 12 12
207 0 return;
208 }
209  
210 12 my $str;
211 my $str_len;
212 12 ▶ if ((my $end = index($data, "\0")) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $end = index($data, "\000")) != -1) { } 8 4 12
213 8 $str = substr($data, 0, $end);
214 8 $str_len = $end + 1; # include the final null in the length
215 }
216 else {
217 4 $str = $data;
218 4 $str_len = length($data);
219 }
220  
221 12 ▶ return wantarray ? ($str, $str_len) : $str;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 6 12
222 }
223  
224 22 ▶sub unpack_unicode_string {
Subroutine unpack_unicode_string: called CC=7 SCAR=19.5
Pod undocumented
225 22 my $data = shift;
226  
227 22 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 22 22
228 0 return;
229 }
230  
231 22 my $str_len = 0;
232 22 foreach my $v (unpack('v*', $data)) {
233 70 $str_len += 2;
234 70 ▶ last if $v == 0; # include the final null in the length
Branch: 1 branch
BranchTrueFalseTotal
if $v == 0 12 58 70
235 }
236 22 my $str = decode('UCS-2LE', substr($data, 0, $str_len));
237  
238 # The decode function from Encode may create invalid unicode characters
239 # which cause subsequent warnings (e.g. during regex matching).
240 # For example, characters in the 0xd800 to 0xdfff range of the
241 # basic multilingual plane (0x0000 to 0xffff) are 'surrogate pairs'
242 # and are expected to appear as a 'high surrogate' (0xd800 to 0xdbff)
243 # followed by a 'low surrogate' (0xdc00 to 0xdfff).
244  
245 # remove any final null
246 22 ▶ if (length($str) > 0 && substr($str, -1, 1) eq "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0 and substr($str, -1, 1) eq "\0") 12 10 22
Condition100%3/3
length $str > 0 and substr($str, -1, 1) eq "\0"
!ll&&!rl&&r
4612
MC/DC100%2/2
length $str > 0 and substr($str, -1, 1) eq "\0"
length $str > 0 substr($str, -1, 1) eq "\0"
Truth table100%3/3
A and B
A= length $str > 0B= substr($str, -1, 1) eq "\0"
ABresult
0X0
100
111
247 12 chop $str;
248 }
249  
250 22 ▶ return wantarray ? ($str, $str_len) : $str;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 11 22
251 }
252  
253 8 ▶sub unpack_guid {
Subroutine unpack_guid: called CC=3 SCAR=11.0
Pod undocumented
254 8 my $guid = Parse::Win32Registry::GUID->new($_[0]);
255 8 ▶ return if !defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 2 6 8
256 6 ▶ return wantarray ? ($guid, $guid->get_length) : $guid;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
257 }
258  
259 14 ▶sub unpack_sid {
Subroutine unpack_sid: called CC=3 SCAR=11.0
Pod undocumented
260 14 my $sid = Parse::Win32Registry::SID->new($_[0]);
261 14 ▶ return if !defined $sid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sid 6 8 14
262 8 ▶ return wantarray ? ($sid, $sid->get_length) : $sid;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
263 }
264  
265 22 ▶sub unpack_ace {
Subroutine unpack_ace: called CC=3 SCAR=11.0
Pod undocumented
266 22 my $ace = Parse::Win32Registry::ACE->new($_[0]);
267 22 ▶ return if !defined $ace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ace 12 10 22
268 10 ▶ return wantarray ? ($ace, $ace->get_length) : $ace;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 5 10
269 }
270  
271 22 ▶sub unpack_acl {
Subroutine unpack_acl: called CC=3 SCAR=11.0
Pod undocumented
272 22 my $acl = Parse::Win32Registry::ACL->new($_[0]);
273 22 ▶ return if !defined $acl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $acl 12 10 22
274 10 ▶ return wantarray ? ($acl, $acl->get_length) : $acl;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 5 10
275 }
276  
277 37 ▶sub unpack_security_descriptor {
Subroutine unpack_security_descriptor: called CC=3 SCAR=11.0
Pod undocumented
278 37 my $sd = Parse::Win32Registry::SecurityDescriptor->new($_[0]);
279 37 ▶ return if !defined $sd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sd 11 26 37
280 26 ▶ return wantarray ? ($sd, $sd->get_length) : $sd;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 17 26
281 }
282  
283 0 ▶sub unpack_series {
Subroutine unpack_series: not called CC=4 SCAR=30.0
Pod undocumented
284 0 my $function = shift;
285 0 my $data = shift;
286  
287 0 ▶ if (!defined $function || !defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $function and defined $data) 0 0 0
Condition0%0/3
defined $function and defined $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $function and defined $data
defined $function defined $data
Truth table0%0/3
A and B
A= defined $functionB= defined $data
ABresult
0X0
100
111
288 0 croak "Usage: unpack_series(\\\&unpack_function, \$data)";
289 }
290  
291 0 my $pos = 0;
292 0 my @items = ();
293 0 ▶ while (my ($item, $item_len) = $function->(substr($data, $pos))) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$function(substr($data, $pos))) 0 0 0
294 0 push @items, $item;
295 0 $pos += $item_len;
296 }
297 0 return @items;
298 }
299  
300 41 ▶sub make_multiple_subkey_iterator {
Subroutine make_multiple_subkey_iterator: called CC=9 SCAR=22.0
Pod undocumented
301 41 my @keys = @_;
302  
303 # check @keys contains keys
304 41 ▶ if (@keys == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys) 1 40 41
Condition66%2/3
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
l!l&&r!l&&!r
1040
MC/DC50%1/2
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
@keys == 0 grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
Truth table66%2/3
A or B
A= @keys == 0B= grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
ABresult
1X1
011
000
305 64 ▶ grep { defined && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key') }
Condition66%2/3
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
!ll&&!rl&&r
8560
MC/DC0%0/2
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
defined $_ !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
Truth table66%2/3
A && not(B)
A= defined $_B= !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
ABresult
0X0
100
111
306 @keys) {
307 1 croak 'Usage: make_multiple_subkey_iterator($key1, $key2, ...)';
308 }
309  
310 40 my %subkeys_seen = ();
311 40 my @subkeys_queue;
312 40 ▶ for (my $i = 0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 64 40 104
313 64 my $key = $keys[$i];
314 64 ▶ next if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 8 56 64
315 56 foreach my $subkey ($key->get_list_of_subkeys) {
316 46 my $name = $subkey->get_name;
317 46 $subkeys_seen{$name}[$i] = $subkey;
318 }
319 }
320 40 foreach my $name (sort keys %subkeys_seen) {
321 # make sure number of subkeys matches number of keys
322 34 ▶ if (@{$subkeys_seen{$name}} != @keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$subkeys_seen{$name};} != @keys) 2 32 34
323 2 @{$subkeys_seen{$name}}[@keys - 1] = undef;
324 }
325 34 push @subkeys_queue, $subkeys_seen{$name};
326 }
327  
328 68 ▶ return Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
329 68 my $subkeys = shift @subkeys_queue;
330 68 ▶ if (defined $subkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subkeys) { } 34 34 68
331 34 return $subkeys;
332 }
333 else {
334 34 return;
335 }
336 40 });
337 }
338  
339 41 ▶sub make_multiple_value_iterator {
Subroutine make_multiple_value_iterator: called CC=9 SCAR=22.0
Pod undocumented
340 41 my @keys = @_;
341  
342 # check @keys contains keys
343 41 ▶ if (@keys == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys) 1 40 41
Condition66%2/3
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
l!l&&r!l&&!r
1040
MC/DC50%1/2
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
@keys == 0 grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
Truth table66%2/3
A or B
A= @keys == 0B= grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
ABresult
1X1
011
000
344 64 ▶ grep { defined && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key') }
Condition66%2/3
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
!ll&&!rl&&r
8560
MC/DC0%0/2
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
defined $_ !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
Truth table66%2/3
A && not(B)
A= defined $_B= !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
ABresult
0X0
100
111
345 @keys) {
346 1 croak 'Usage: make_multiple_value_iterator($key1, $key2, ...)';
347 }
348  
349 40 my %values_seen = ();
350 40 my @values_queue;
351 40 ▶ for (my $i = 0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 64 40 104
352 64 my $key = $keys[$i];
353 64 ▶ next if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 8 56 64
354 56 foreach my $value ($key->get_list_of_values) {
355 74 my $name = $value->get_name;
356 74 $values_seen{$name}[$i] = $value;
357 }
358 }
359 40 foreach my $name (sort keys %values_seen) {
360 # make sure number of values matches number of keys
361 62 ▶ if (@{$values_seen{$name}} != @keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$values_seen{$name};} != @keys) 2 60 62
362 2 @{$values_seen{$name}}[@keys - 1] = undef;
363 }
364 62 push @values_queue, $values_seen{$name};
365 }
366  
367 60 ▶ return Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
368 60 my $values = shift @values_queue;
369 60 ▶ if (defined $values) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values) { } 36 24 60
370 36 return $values;
371 }
372 else {
373 24 return;
374 }
375 40 });
376 }
377  
378 7 ▶sub make_multiple_subtree_iterator {
Subroutine make_multiple_subtree_iterator: called CC=4 SCAR=13.9
Pod undocumented
379 7 my @keys = @_;
380  
381 # check @keys contains keys
382 7 ▶ if (@keys == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys) 1 6 7
Condition66%2/3
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
l!l&&r!l&&!r
106
MC/DC50%1/2
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
@keys == 0 grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
Truth table66%2/3
A or B
A= @keys == 0B= grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
ABresult
1X1
011
000
383 10 ▶ grep { defined && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key') }
Condition33%1/3
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
defined $_ !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
Truth table33%1/3
A && not(B)
A= defined $_B= !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
ABresult
0X0
100
111
384 @keys) {
385 1 croak 'Usage: make_multiple_subtree_iterator($key1, $key2, ...)';
386 }
387  
388 6 my @start_keys = (\@keys);
389 10 ▶ push my (@subkey_iters), Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
390 10 return shift @start_keys;
391 6 });
392 6 my $value_iter;
393 my $subkeys; # used to remember subkeys while iterating values
394  
395 80 ▶ return Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
396 80 ▶ if (defined $value_iter && wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_iter and wantarray) 60 20 80
Condition100%3/3
defined $value_iter and wantarray
!ll&&!rl&&r
61460
MC/DC100%2/2
defined $value_iter and wantarray
defined $value_iter wantarray
Truth table100%3/3
A and B
A= defined $value_iterB= wantarray
ABresult
0X0
100
111
397 60 my $values = $value_iter->();
398 60 ▶ if (defined $values) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values) 36 24 60
399 36 return ($subkeys, $values);
400 }
401 }
402 44 ▶ while (@subkey_iters > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subkey_iters > 0) 78 4 82
403 78 $subkeys = $subkey_iters[-1]->(); # depth-first
404 78 ▶ if (defined $subkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subkeys) 40 38 78
405 40 push @subkey_iters, make_multiple_subkey_iterator(@$subkeys);
406 40 $value_iter = make_multiple_value_iterator(@$subkeys);
407 40 return $subkeys;
408 }
409 38 pop @subkey_iters; # iter finished, so remove it
410 }
411 4 return;
412 6 });
413 }
414  
415 13 ▶sub compare_multiple_keys {
Subroutine compare_multiple_keys: called CC=6 SCAR=18.0
Pod undocumented
416 13 my @keys = @_;
417  
418 # check @keys contains keys
419 13 ▶ if (@keys == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys) 1 12 13
Condition66%2/3
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
l!l&&r!l&&!r
1012
MC/DC50%1/2
@keys == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
@keys == 0 grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
Truth table66%2/3
A or B
A= @keys == 0B= grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Key");} @keys
ABresult
1X1
011
000
420 36 ▶ grep { defined && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key') }
Condition66%2/3
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
!ll&&!rl&&r
8280
MC/DC0%0/2
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
defined $_ !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
Truth table66%2/3
A && not(B)
A= defined $_B= !UNIVERSAL::isa($_, 'Parse::Win32Registry::Key')
ABresult
0X0
100
111
421 @keys) {
422 1 croak 'Usage: compare_multiple_keys($key1, $key2, ...)';
423 }
424  
425 12 my @changes = ();
426  
427 12 my $benchmark_key;
428 12 foreach my $key (@keys) {
429 36 my $diff = '';
430 # Skip comparison for the first value
431 36 ▶ if (@changes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changes > 0) 24 12 36
432 24 $diff = _compare_keys($benchmark_key, $key);
433 }
434 36 $benchmark_key = $key;
435 36 push @changes, $diff;
436 }
437 12 return @changes;
438 }
439  
440 11 ▶sub compare_multiple_values {
Subroutine compare_multiple_values: called CC=6 SCAR=18.0
Pod undocumented
441 11 my @values = @_;
442  
443 # check @values contains values
444 11 ▶ if (@values == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@values == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Value");} @values) 1 10 11
Condition66%2/3
@values == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Value");} @values
l!l&&r!l&&!r
1010
MC/DC50%1/2
@values == 0 or grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Value");} @values
@values == 0 grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Value");} @values
Truth table66%2/3
A or B
A= @values == 0B= grep {defined $_ and not UNIVERSAL::isa($_, "Parse::Win32Registry::Value");} @values
ABresult
1X1
011
000
445 30 ▶ grep { defined && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Value') }
Condition66%2/3
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Value')
!ll&&!rl&&r
8220
MC/DC0%0/2
defined $_ && !UNIVERSAL::isa($_, 'Parse::Win32Registry::Value')
defined $_ !UNIVERSAL::isa($_, 'Parse::Win32Registry::Value')
Truth table66%2/3
A && not(B)
A= defined $_B= !UNIVERSAL::isa($_, 'Parse::Win32Registry::Value')
ABresult
0X0
100
111
446 @values) {
447 1 croak 'Usage: compare_multiple_values($value1, $value2, ...)';
448 }
449  
450 10 my @changes = ();
451  
452 10 my $benchmark_value;
453 10 foreach my $value (@values) {
454 30 my $diff = '';
455 # Skip comparison for the first value
456 30 ▶ if (@changes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changes > 0) 20 10 30
457 20 $diff = _compare_values($benchmark_value, $value);
458 }
459 30 $benchmark_value = $value;
460 30 push @changes, $diff;
461 }
462 10 return @changes;
463 }
464  
465 24 ▶sub _compare_keys {
Subroutine _compare_keys: called CC=14 SCAR=31.2
466 24 my ($key1, $key2) = @_;
467  
468 24 ▶ if (!defined $key1 && !defined $key2) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key1 || defined $key2) { } 4 20 24
elsif (defined $key1 and not defined $key2) { } 2 18 20
elsif (not defined $key1 and defined $key2) { } 2 32 34
Condition100%3/3
defined $key1 || defined $key2
l!l&&r!l&&!r
1824
MC/DC100%2/2
defined $key1 || defined $key2
defined $key1 defined $key2
Truth table100%3/3
A || B
A= defined $key1B= defined $key2
ABresult
1X1
011
000
469 4 return ''; # 'MISSING'
470 }
471 ▶ elsif (defined $key1 && !defined $key2) {
Condition100%3/3
defined $key1 and not defined $key2
!ll&&!rl&&r
2162
MC/DC100%2/2
defined $key1 and not defined $key2
defined $key1 not defined $key2
Truth table100%3/3
A and not(B)
A= defined $key1B= not defined $key2
ABresult
0X0
100
111
472 2 return 'DELETED';
473 }
474 ▶ elsif (!defined $key1 && defined $key2) {
Condition66%2/3
not defined $key1 and defined $key2
!ll&&!rl&&r
1602
MC/DC50%1/2
not defined $key1 and defined $key2
not defined $key1 defined $key2
Truth table66%2/3
not(A) and B
A= not defined $key1B= defined $key2
ABresult
0X0
100
111
475 2 return 'ADDED';
476 }
477  
478 16 my $timestamp1 = $key1->get_timestamp;
479 16 my $timestamp2 = $key2->get_timestamp;
480 16 ▶ if ($key1->get_name ne $key2->get_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key1->get_name ne $key2->get_name) { } 0 16 16
elsif (defined $timestamp1 and defined $timestamp2) { } 8 8 16
481 0 return 'CHANGED';
482 }
483 ▶ elsif (defined $timestamp1 && defined $timestamp2) {
Condition66%2/3
defined $timestamp1 and defined $timestamp2
!ll&&!rl&&r
808
MC/DC50%1/2
defined $timestamp1 and defined $timestamp2
defined $timestamp1 defined $timestamp2
Truth table66%2/3
A and B
A= defined $timestamp1B= defined $timestamp2
ABresult
0X0
100
111
484 8 ▶ if ($timestamp1 < $timestamp2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($timestamp1 < $timestamp2) { } 0 8 8
elsif ($timestamp1 > $timestamp2) { } 0 8 8
485 0 return 'NEWER';
486 }
487 elsif ($timestamp1 > $timestamp2) {
488 0 return 'OLDER';
489 }
490 }
491 else {
492 8 return ''; # comment out to check values...
493 0 my $value_iter = make_multiple_value_iterator($key1, $key2);
494 0 ▶ while (my ($val1, $val2) = $value_iter->get_next) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $value_iter->get_next) 0 0 0
495 0 ▶ if (_compare_values($val1, $val2) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (_compare_values($val1, $val2) ne "") 0 0 0
496 0 return 'VALUES';
497 }
498 }
499 0 return '';
500 }
501 }
502  
503 20 ▶sub _compare_values {
Subroutine _compare_values: called CC=13 SCAR=28.9
504 20 my ($val1, $val2) = @_;
505  
506 20 ▶ if (!defined $val1 && !defined $val2) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $val1 || defined $val2) { } 4 16 20
elsif (defined $val1 and not defined $val2) { } 2 14 16
elsif (not defined $val1 and defined $val2) { } 2 24 26
Condition100%3/3
defined $val1 || defined $val2
l!l&&r!l&&!r
1424
MC/DC100%2/2
defined $val1 || defined $val2
defined $val1 defined $val2
Truth table100%3/3
A || B
A= defined $val1B= defined $val2
ABresult
1X1
011
000
507 4 return ''; # 'MISSING'
508 }
509 ▶ elsif (defined $val1 && !defined $val2) {
Condition100%3/3
defined $val1 and not defined $val2
!ll&&!rl&&r
2122
MC/DC100%2/2
defined $val1 and not defined $val2
defined $val1 not defined $val2
Truth table100%3/3
A and not(B)
A= defined $val1B= not defined $val2
ABresult
0X0
100
111
510 2 return 'DELETED';
511 }
512 ▶ elsif (!defined $val1 && defined $val2) {
Condition66%2/3
not defined $val1 and defined $val2
!ll&&!rl&&r
1202
MC/DC50%1/2
not defined $val1 and defined $val2
not defined $val1 defined $val2
Truth table66%2/3
not(A) and B
A= not defined $val1B= defined $val2
ABresult
0X0
100
111
513 2 return 'ADDED';
514 }
515  
516 12 my $data1 = $val1->get_data;
517 12 my $data2 = $val2->get_data;
518 12 ▶ if ($val1->get_name ne $val2->get_name ||
Branch: 1 branch
BranchTrueFalseTotal
if ($val1->get_name ne $val2->get_name or $val1->get_type != $val2->get_type or defined $data1 ne defined $data2 or defined $data1 and defined $data2 and $data1 ne $data2) { } 0 12 12
Condition33%5/15
$val1->get_name ne $val2->get_name or $val1->get_type != $val2->get_type or defined $data1 ne defined $data2 or defined $data1 and defined $data2 and $data1 ne $data2
l!l&&r!l&&!r
0012
$val1->get_name ne $val2->get_name or $val1->get_type != $val2->get_type or defined $data1 ne defined $data2
l!l&&r!l&&!r
0012
$val1->get_name ne $val2->get_name or $val1->get_type != $val2->get_type
l!l&&r!l&&!r
0012
defined $data1 and defined $data2 and $data1 ne $data2
!ll&&!rl&&r
0120
defined $data1 and defined $data2
!ll&&!rl&&r
0012
MC/DC0%0/6
$val1->get_name ne $val2->get_name or $val1->get_type != $val2->get_type or defined $data1 ne defined $data2 or defined $data1 and defined $data2 and $data1 ne $data2
$val1->get_name ne $val2->get_name $val1->get_type != $val2->get_type defined $data1 ne defined $data2 defined $data1 defined $data2 $data1 ne $data2
Truth table14%1/7
A or B or C or D and E and F
A= $val1->get_name ne $val2->get_nameB= $val1->get_type != $val2->get_typeC= defined $data1 ne defined $data2D= defined $data1E= defined $data2F= $data1 ne $data2
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001111
0000XX0
00010X0
0001100
519 $val1->get_type != $val2->get_type ||
520 defined $data1 ne defined $data2 ||
521 (defined $data1 && defined $data2 && $data1 ne $data2)) {
522 0 return 'CHANGED';
523 }
524 else {
525 12 return '';
526 }
527 }
528  
529  
530 package Parse::Win32Registry::Iterator;
531  
532 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
533  
534 744 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
535 744 my $class = shift;
536 744 my $self = shift;
537  
538 744 my $type = ref $self;
539 744 ▶ croak 'Missing iterator subroutine' if $type ne 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "CODE" and $type ne __PACKAGE__ 0 744 744
Condition33%1/3
$type ne "CODE" and $type ne __PACKAGE__
!ll&&!rl&&r
74400
MC/DC0%0/2
$type ne "CODE" and $type ne __PACKAGE__
$type ne "CODE" $type ne __PACKAGE__
Truth table33%1/3
A and B
A= $type ne "CODE"B= $type ne __PACKAGE__
ABresult
0X0
100
111
540 && $type ne __PACKAGE__;
541  
542 744 bless $self, $class;
543 744 return $self;
544 }
545  
546 138 ▶sub get_next {
Subroutine get_next: called CC=1 SCAR=0.0
547 138 $_[0]->();
548 }
549  
550  
551 package Parse::Win32Registry::GUID;
552  
553 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
554 8 my $class = shift;
555 8 my $data = shift;
556  
557 8 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 8 8
558 0 return;
559 }
560  
561 8 ▶ if (length($data) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 16) 2 6 8
562 2 return;
563 }
564  
565 6 my $guid = sprintf '{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}',
566 unpack('VvvC2C6', $data);
567  
568 6 my $self = {
569 _guid => $guid,
570 _length => 16,
571 };
572 6 bless $self, $class;
573  
574 6 return $self;
575 }
576  
577 6 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
578 6 my $self = shift;
579  
580 6 return $self->{_guid};
581 }
582  
583 3 ▶sub get_length {
Subroutine get_length: called CC=1 SCAR=0.0
584 3 my $self = shift;
585  
586 3 return $self->{_length};
587 }
588  
589  
590 package Parse::Win32Registry::SID;
591  
592 170 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
593 170 my $class = shift;
594 170 my $data = shift;
595  
596 170 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 170 170
597 0 return;
598 }
599  
600 # 0x00 byte = revision
601 # 0x01 byte = number of sub authorities
602 # 0x07 byte = identifier authority
603 # 0x08 dword = 1st sub authority
604 # 0x0c dword = 2nd sub authority
605 # ...
606  
607 170 ▶ if (length($data) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 8) 4 166 170
608 4 return;
609 }
610  
611 166 my ($rev, $num_sub_auths, $id_auth) = unpack('CCx5C', $data);
612  
613 166 ▶ if ($num_sub_auths == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_sub_auths == 0) 2 164 166
614 2 return;
615 }
616  
617 164 my $sid_len = 8 + 4 * $num_sub_auths;
618  
619 164 ▶ if (length($data) < $sid_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $sid_len) 8 156 164
620 8 return;
621 }
622  
623 156 my @sub_auths = unpack("x8V$num_sub_auths", $data);
624 156 my $sid = "S-$rev-$id_auth-" . join('-', @sub_auths);
625  
626 156 my $self = {
627 _sid => $sid,
628 _length => $sid_len,
629 };
630 156 bless $self, $class;
631  
632 156 return $self;
633 }
634  
635 # See KB243330 for a list of well known sids
636 13 our %WellKnownSids = (
637 'S-1-0-0' => 'Nobody',
638 'S-1-1-0' => 'Everyone',
639 'S-1-3-0' => 'Creator Owner',
640 'S-1-3-1' => 'Creator Group',
641 'S-1-3-2' => 'Creator Owner Server',
642 'S-1-3-3' => 'Creator Group Server',
643 'S-1-5-1' => 'Dialup',
644 'S-1-5-2' => 'Network',
645 'S-1-5-3' => 'Batch',
646 'S-1-5-4' => 'Interactive',
647 'S-1-5-5-\\d+-\\d+' => 'Logon Session',
648 'S-1-5-6' => 'Service',
649 'S-1-5-7' => 'Anonymous',
650 'S-1-5-8' => 'Proxy',
651 'S-1-5-9' => 'Enterprise Domain Controllers',
652 'S-1-5-10' => 'Principal Self',
653 'S-1-5-11' => 'Authenticated Users',
654 'S-1-5-12' => 'Restricted Code',
655 'S-1-5-13' => 'Terminal Server Users',
656 'S-1-5-18' => 'Local System',
657 'S-1-5-19' => 'Local Service',
658 'S-1-5-20' => 'Network Service',
659 'S-1-5-\\d+-\\d+-\\d+-\\d+-500' => 'Administrator',
660 'S-1-5-\\d+-\\d+-\\d+-\\d+-501' => 'Guest',
661 'S-1-5-\\d+-\\d+-\\d+-\\d+-502' => 'KRBTGT',
662 'S-1-5-\\d+-\\d+-\\d+-\\d+-512' => 'Domain Admins',
663 'S-1-5-\\d+-\\d+-\\d+-\\d+-513' => 'Domain Users',
664 'S-1-5-\\d+-\\d+-\\d+-\\d+-514' => 'Domain Guests',
665 'S-1-5-\\d+-\\d+-\\d+-\\d+-515' => 'Domain Computers',
666 'S-1-5-\\d+-\\d+-\\d+-\\d+-516' => 'Domain Controllers',
667 'S-1-5-\\d+-\\d+-\\d+-\\d+-517' => 'Cert Publishers',
668 'S-1-5-\\d+-\\d+-\\d+-\\d+-518' => 'Schema Admins',
669 'S-1-5-\\d+-\\d+-\\d+-\\d+-519' => 'Enterprise Admins',
670 'S-1-5-\\d+-\\d+-\\d+-\\d+-520' => 'Group Policy Creator Owners',
671 'S-1-5-\\d+-\\d+-\\d+-\\d+-533' => 'RAS and IAS Servers',
672 'S-1-5-32-544' => 'Administrators',
673 'S-1-5-32-545' => 'Users',
674 'S-1-5-32-546' => 'Guest',
675 'S-1-5-32-547' => 'Power Users',
676 'S-1-5-32-548' => 'Account Operators',
677 'S-1-5-32-549' => 'Server Operators',
678 'S-1-5-32-550' => 'Print Operators',
679 'S-1-5-32-551' => 'Backup Operators',
680 'S-1-5-32-552' => 'Replicators',
681 'S-1-16-4096' => 'Low Integrity Level',
682 'S-1-16-8192' => 'Medium Integrity Level',
683 'S-1-16-12288' => 'High Integrity Level',
684 'S-1-16-16384' => 'System Integrity Level',
685 );
686  
687 0 ▶sub get_name {
Subroutine get_name: not called CC=3 SCAR=24.8
688 0 my $self = shift;
689  
690 0 my $sid = $self->{_sid};
691  
692 0 foreach my $regexp (keys %WellKnownSids) {
693 0 ▶ if ($sid =~ m/^$regexp$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sid =~ /^$regexp$/) 0 0 0
694 0 return $WellKnownSids{$regexp};
695 }
696 }
697 0 return;
698 }
699  
700 131 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
701 131 my $self = shift;
702  
703 131 return $self->{_sid};
704 }
705  
706 192 ▶sub get_length {
Subroutine get_length: called CC=1 SCAR=0.0
707 192 my $self = shift;
708  
709 192 return $self->{_length};
710 }
711  
712  
713 package Parse::Win32Registry::ACE;
714  
715 129 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
716 129 my $class = shift;
717 129 my $data = shift;
718  
719 129 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 129 129
720 0 return;
721 }
722  
723 # 0x00 byte = type
724 # 0x01 byte = flags
725 # 0x02 word = length
726  
727 # Types:
728 # ACCESS_ALLOWED_ACE_TYPE = 0
729 # ACCESS_DENIED_ACE_TYPE = 1
730 # SYSTEM_AUDIT_ACE_TYPE = 2
731 # SYSTEM_MANDATORY_LABEL_ACE_TYPE = x011
732  
733 # Flags:
734 # OBJECT_INHERIT_ACE = 0x01
735 # CONTAINER_INHERIT_ACE = 0x02
736 # NO_PROPAGATE_INHERIT_ACE = 0x04
737 # INHERIT_ONLY_ACE = 0x08
738 # INHERITED_ACE = 0x10
739 # SUCCESSFUL_ACCESS_ACE_FLAG = 0x40 (Audit Success)
740 # FAILED_ACCESS_ACE_FLAG = 0x80 (Audit Failure)
741  
742 129 ▶ if (length($data) < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 4) 6 123 129
743 6 return;
744 }
745  
746 123 my ($type, $flags, $ace_len) = unpack('CCv', $data);
747  
748 123 ▶ if (length($data) < $ace_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $ace_len) 6 117 123
749 6 return;
750 }
751  
752 # The data following the header varies depending on the type.
753 # For ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, SYSTEM_AUDIT_ACE
754 # the header is followed by an access mask and a sid.
755 # 0x04 dword = access mask
756 # 0x08 = SID
757  
758 # Only the following types are currently unpacked:
759 # 0 (ACCESS_ALLOWED_ACE), 1 (ACCESS_DENIED_ACE), 2 (SYSTEM_AUDIT_ACE)
760 117 ▶ if ($type >= 0 && $type <= 2 || $type == 0x11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type >= 0 and $type <= 2 or $type == 17) { } 112 5 117
Condition83%5/6
$type >= 0 and $type <= 2 or $type == 17
l!l&&r!l&&!r
11025
$type >= 0 and $type <= 2
!ll&&!rl&&r
07110
MC/DC66%2/3
$type >= 0 and $type <= 2 or $type == 17
$type >= 0 $type <= 2 $type == 17
Truth table60%3/5
A and B or C
A= $type >= 0B= $type <= 2C= $type == 17
ABCresult
11X1
0X11
1011
0X00
1000
761 112 my $access_mask = unpack('x4V', $data);
762 112 my $sid = Parse::Win32Registry::SID->new(substr($data, 8,
763 $ace_len - 8));
764  
765 # Abandon ace if sid is invalid
766 112 ▶ if (!defined $sid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sid) 4 108 112
767 4 return;
768 }
769  
770 # Abandon ace if not the expected length
771 108 ▶ if (($sid->get_length + 8) != $ace_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sid->get_length + 8 != $ace_len) 2 106 108
772 2 return;
773 }
774  
775 106 my $self = {
776 _type => $type,
777 _flags => $flags,
778 _mask => $access_mask,
779 _trustee => $sid,
780 _length => $ace_len,
781 };
782 106 bless $self, $class;
783  
784 106 return $self;
785 }
786 else {
787 5 return;
788 }
789 }
790  
791 13 our @Types = qw(
792 ACCESS_ALLOWED
793 ACCESS_DENIED
794 SYSTEM_AUDIT
795 SYSTEM_ALARM
796 ALLOWED_COMPOUND
797 ACCESS_ALLOWED_OBJECT
798 ACCESS_DENIED_OBJECT
799 SYSTEM_AUDIT_OBJECT
800 SYSTEM_ALARM_OBJECT
801 ACCESS_ALLOWED_CALLBACK
802 ACCESS_DENIED_CALLBACK
803 ACCESS_ALLOWED_CALLBACK_OBJECT
804 ACCESS_DENIED_CALLBACK_OBJECT
805 SYSTEM_AUDIT_CALLBACK
806 SYSTEM_ALARM_CALLBACK
807 SYSTEM_AUDIT_CALLBACK_OBJECT
808 SYSTEM_ALARM_CALLBACK_OBJECT
809 SYSTEM_MANDATORY_LABEL
810 );
811  
812 97 ▶sub _look_up_ace_type {
Subroutine _look_up_ace_type: called CC=2 SCAR=7.6
813 97 my $type = shift;
814  
815 97 ▶ if (exists $Types[$type]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Parse::Win32Registry::ACE::Types[$type]) { } 97 0 97
816 97 return $Types[$type];
817 }
818 else {
819 0 return '';
820 }
821 }
822  
823 97 ▶sub get_type {
Subroutine get_type: called CC=1 SCAR=0.0
824 97 return $_[0]->{_type};
825 }
826  
827 97 ▶sub get_type_as_string {
Subroutine get_type_as_string: called CC=1 SCAR=0.0
828 97 return _look_up_ace_type($_[0]->{_type});
829 }
830  
831 97 ▶sub get_flags {
Subroutine get_flags: called CC=1 SCAR=0.0
832 97 return $_[0]->{_flags};
833 }
834  
835 97 ▶sub get_access_mask {
Subroutine get_access_mask: called CC=1 SCAR=0.0
836 97 return $_[0]->{_mask};
837 }
838  
839 97 ▶sub get_trustee {
Subroutine get_trustee: called CC=1 SCAR=0.0
840 97 return $_[0]->{_trustee};
841 }
842  
843 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
844 0 my $self = shift;
845  
846 0 my $sid = $self->{_trustee};
847 my $string = sprintf '%s 0x%02x 0x%08x %s',
848 _look_up_ace_type($self->{_type}),
849 $self->{_flags},
850 $self->{_mask},
851 0 $sid->as_string;
852 0 my $name = $sid->get_name;
853 0 ▶ $string .= " [$name]" if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
854 0 return $string;
855 }
856  
857 101 ▶sub get_length {
Subroutine get_length: called CC=1 SCAR=0.0
858 101 my $self = shift;
859  
860 101 return $self->{_length};
861 }
862  
863  
864 package Parse::Win32Registry::ACL;
865  
866 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
867  
868 51 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
869 51 my $class = shift;
870 51 my $data = shift;
871  
872 51 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 51 51
873 0 return;
874 }
875  
876 # 0x00 byte = revision
877 # 0x01
878 # 0x02 word = length
879 # 0x04 word = number of aces
880 # 0x06
881 # 0x08 = first ace (variable length)
882 # ... = second ace (variable length)
883 # ...
884  
885 51 ▶ if (length($data) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 8) 2 49 51
886 2 return;
887 }
888  
889 49 my ($rev, $acl_len, $num_aces) = unpack('Cxvv', $data);
890  
891 49 ▶ if (length($data) < $acl_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $acl_len) 4 45 49
892 4 return;
893 }
894  
895 45 my $pos = 8;
896 45 my @acl = ();
897 45 ▶ foreach (my $num = 0; $num < $num_aces; $num++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < $num_aces) 107 34 141
898 107 my $ace = Parse::Win32Registry::ACE->new(substr($data, $pos,
899 $acl_len - $pos));
900 # Abandon acl if any single ace is undefined
901 107 ▶ return if !defined $ace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ace 11 96 107
902 96 push @acl, $ace;
903 96 $pos += $ace->get_length;
904 }
905  
906 # Abandon acl if not expected length, but don't use
907 # $pos != $acl_len as some acls contain unused space.
908 34 ▶ if ($pos > $acl_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $acl_len) 0 34 34
909 0 return;
910 }
911  
912 34 my $self = {
913 _acl => \@acl,
914 _length => $acl_len,
915 };
916 34 bless $self, $class;
917  
918 34 return $self;
919 }
920  
921 31 ▶sub get_list_of_aces {
Subroutine get_list_of_aces: called CC=1 SCAR=0.0
922 31 my $self = shift;
923  
924 31 return @{$self->{_acl}};
925 }
926  
927 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
928 0 croak 'Usage: ACLs do not have an as_string method; use as_stanza instead';
929 }
930  
931 0 ▶sub as_stanza {
Subroutine as_stanza: not called CC=2 SCAR=17.9
932 0 my $self = shift;
933  
934 0 my $stanza = '';
935 0 foreach my $ace (@{$self->{_acl}}) {
936 0 $stanza .= 'ACE: '. $ace->as_string . "\n";
937 }
938 0 return $stanza;
939 }
940  
941 35 ▶sub get_length {
Subroutine get_length: called CC=1 SCAR=0.0
942 35 my $self = shift;
943  
944 35 return $self->{_length};
945 }
946  
947  
948 package Parse::Win32Registry::SecurityDescriptor;
949  
950 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
951  
952 37 ▶sub new {
Subroutine new: called CC=19 SCAR=29.7
953 37 my $class = shift;
954 37 my $data = shift;
955  
956 37 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 37 37
957 0 return;
958 }
959  
960 # Unpacks "self-relative" security descriptors
961  
962 # 0x00 word = revision
963 # 0x02 word = control flags
964 # 0x04 dword = offset to owner sid
965 # 0x08 dword = offset to group sid
966 # 0x0c dword = offset to sacl
967 # 0x10 dword = offset to dacl
968  
969 # Offsets are relative to the start of the security descriptor
970  
971 # Control Flags:
972 # SE_OWNER_DEFAULTED 0x0001
973 # SE_GROUP_DEFAULTED 0x0002
974 # SE_DACL_PRESENT 0x0004
975 # SE_DACL_DEFAULTED 0x0008
976 # SE_SACL_PRESENT 0x0010
977 # SE_SACL_DEFAULTED 0x0020
978 # SE_DACL_AUTO_INHERIT_REQ 0x0100
979 # SE_SACL_AUTO_INHERIT_REQ 0x0200
980 # SE_DACL_AUTO_INHERITED 0x0400
981 # SE_SACL_AUTO_INHERITED 0x0800
982 # SE_DACL_PROTECTED 0x1000
983 # SE_SACL_PROTECTED 0x2000
984 # SE_RM_CONTROL_VALID 0x4000
985 # SE_SELF_RELATIVE 0x8000
986  
987 37 ▶ if (length($data) < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 20) 2 35 37
988 2 return;
989 }
990  
991 35 my ($rev,
992 $flags,
993 $offset_to_owner,
994 $offset_to_group,
995 $offset_to_sacl,
996 $offset_to_dacl) = unpack('vvVVVV', $data);
997  
998 35 my %sd = ();
999 35 my $sd_len = 20;
1000  
1001 35 my $self = {};
1002 35 ▶ if ($offset_to_owner > 0 && $offset_to_owner < length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_owner > 0 and $offset_to_owner < length $data) 23 12 35
Condition66%2/3
$offset_to_owner > 0 and $offset_to_owner < length $data
!ll&&!rl&&r
12023
MC/DC50%1/2
$offset_to_owner > 0 and $offset_to_owner < length $data
$offset_to_owner > 0 $offset_to_owner < length $data
Truth table66%2/3
A and B
A= $offset_to_owner > 0B= $offset_to_owner < length $data
ABresult
0X0
100
111
1003 23 my $owner = Parse::Win32Registry::SID->new(substr($data,
1004 $offset_to_owner));
1005 23 ▶ return if !defined $owner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owner 2 21 23
1006 21 $self->{_owner} = $owner;
1007 21 ▶ if ($offset_to_owner + $owner->get_length > $sd_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_owner + $owner->get_length > $sd_len) 21 0 21
1008 21 $sd_len = $offset_to_owner + $owner->get_length;
1009 }
1010 }
1011 33 ▶ if ($offset_to_group > 0 && $offset_to_group < length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_group > 0 and $offset_to_group < length $data) 21 12 33
Condition66%2/3
$offset_to_group > 0 and $offset_to_group < length $data
!ll&&!rl&&r
12021
MC/DC50%1/2
$offset_to_group > 0 and $offset_to_group < length $data
$offset_to_group > 0 $offset_to_group < length $data
Truth table66%2/3
A and B
A= $offset_to_group > 0B= $offset_to_group < length $data
ABresult
0X0
100
111
1012 21 my $group = Parse::Win32Registry::SID->new(substr($data,
1013 $offset_to_group));
1014 21 ▶ return if !defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 2 19 21
1015 19 $self->{_group} = $group;
1016 19 ▶ if ($offset_to_group + $group->get_length > $sd_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_group + $group->get_length > $sd_len) 19 0 19
1017 19 $sd_len = $offset_to_group + $group->get_length;
1018 }
1019 }
1020 31 ▶ if ($offset_to_sacl > 0 && $offset_to_sacl < length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_sacl > 0 and $offset_to_sacl < length $data) 12 19 31
Condition66%2/3
$offset_to_sacl > 0 and $offset_to_sacl < length $data
!ll&&!rl&&r
19012
MC/DC50%1/2
$offset_to_sacl > 0 and $offset_to_sacl < length $data
$offset_to_sacl > 0 $offset_to_sacl < length $data
Truth table66%2/3
A and B
A= $offset_to_sacl > 0B= $offset_to_sacl < length $data
ABresult
0X0
100
111
1021 12 my $sacl = Parse::Win32Registry::ACL->new(substr($data,
1022 $offset_to_sacl));
1023 12 ▶ return if !defined $sacl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sacl 2 10 12
1024 10 $self->{_sacl} = $sacl;
1025 10 ▶ if ($offset_to_sacl + $sacl->get_length > $sd_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_sacl + $sacl->get_length > $sd_len) 3 7 10
1026 3 $sd_len = $offset_to_sacl + $sacl->get_length;
1027 }
1028 }
1029 29 ▶ if ($offset_to_dacl > 0 && $offset_to_dacl < length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_dacl > 0 and $offset_to_dacl < length $data) 17 12 29
Condition66%2/3
$offset_to_dacl > 0 and $offset_to_dacl < length $data
!ll&&!rl&&r
12017
MC/DC50%1/2
$offset_to_dacl > 0 and $offset_to_dacl < length $data
$offset_to_dacl > 0 $offset_to_dacl < length $data
Truth table66%2/3
A and B
A= $offset_to_dacl > 0B= $offset_to_dacl < length $data
ABresult
0X0
100
111
1030 17 my $dacl = Parse::Win32Registry::ACL->new(substr($data,
1031 $offset_to_dacl));
1032 17 ▶ return if !defined $dacl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dacl 3 14 17
1033 14 $self->{_dacl} = $dacl;
1034 14 ▶ if ($offset_to_dacl + $dacl->get_length > $sd_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_dacl + $dacl->get_length > $sd_len) 3 11 14
1035 3 $sd_len = $offset_to_dacl + $dacl->get_length;
1036 }
1037 }
1038 26 $self->{_length} = $sd_len;
1039 26 bless $self, $class;
1040  
1041 26 return $self;
1042 }
1043  
1044 25 ▶sub get_owner {
Subroutine get_owner: called CC=1 SCAR=0.0
1045 25 my $self = shift;
1046  
1047 25 return $self->{_owner};
1048 }
1049  
1050 25 ▶sub get_group {
Subroutine get_group: called CC=1 SCAR=0.0
1051 25 my $self = shift;
1052  
1053 25 return $self->{_group};
1054 }
1055  
1056 25 ▶sub get_sacl {
Subroutine get_sacl: called CC=1 SCAR=0.0
1057 25 my $self = shift;
1058  
1059 25 return $self->{_sacl};
1060 }
1061  
1062 25 ▶sub get_dacl {
Subroutine get_dacl: called CC=1 SCAR=0.0
1063 25 my $self = shift;
1064  
1065 25 return $self->{_dacl};
1066 }
1067  
1068 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
1069 0 croak 'Usage: Security Descriptors do not have an as_string method; use as_stanza instead';
1070 }
1071  
1072 0 ▶sub as_stanza {
Subroutine as_stanza: not called CC=9 SCAR=45.0
1073 0 my $self = shift;
1074  
1075 0 my $stanza = '';
1076 0 ▶ if (defined(my $owner = $self->{_owner})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $owner = $self->{"_owner"})) 0 0 0
1077 0 $stanza .= 'Owner SID: ' . $owner->as_string;
1078 0 my $name = $owner->get_name;
1079 0 ▶ $stanza .= " [$name]" if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
1080 0 $stanza .= "\n";
1081 }
1082 0 ▶ if (defined(my $group = $self->{_group})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $group = $self->{"_group"})) 0 0 0
1083 0 $stanza .= 'Group SID: ' . $group->as_string;
1084 0 my $name = $group->get_name;
1085 0 ▶ $stanza .= " [$name]" if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
1086 0 $stanza .= "\n";
1087 }
1088 0 ▶ if (defined(my $sacl = $self->{_sacl})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sacl = $self->{"_sacl"})) 0 0 0
1089 0 foreach my $ace ($sacl->get_list_of_aces) {
1090 0 $stanza .= 'SACL ACE: ' . $ace->as_string . "\n";
1091 }
1092 }
1093 0 ▶ if (defined(my $dacl = $self->{_dacl})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dacl = $self->{"_dacl"})) 0 0 0
1094 0 foreach my $ace ($dacl->get_list_of_aces) {
1095 0 $stanza .= 'DACL ACE: ' . $ace->as_string . "\n";
1096 }
1097 }
1098 0 return $stanza;
1099 }
1100  
1101 9 ▶sub get_length {
Subroutine get_length: called CC=1 SCAR=0.0
1102 9 my $self = shift;
1103  
1104 9 return $self->{_length};
1105 }
1106  
1107 13 1;
« lib/Parse/Win32Registry/Key.pm ↑ Summary lib/Parse/Win32Registry/WinNT/Key.pm »