lib/Parse/Win32Registry/WinNT/Value.pm

filter: Statementstmt 73.9% 139 / 188 Branchbran 68.8% 62 / 90 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 69.8% 222 / 318 CC 52 SCAR 34.8
CC
52
Cov
71%
_extract_data
102.3
new
14.2
get_data
14.0
CRAP 32.4
« script/wxregcompare.pl ↑ Summary script/regtree.pl »
1 package Parse::Win32Registry::WinNT::Value;
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(Parse::Win32Registry::Value);
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 Parse::Win32Registry::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use constant VK_HEADER_LENGTH => 0x18;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use constant OFFSET_TO_FIRST_HBIN => 0x1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6124 ▶sub new {
Subroutine new: called CC=14 SCAR=26.6
Pod undocumented
16 6124 my $class = shift;
17 6124 my $regfile = shift;
18 6124 my $offset = shift; # offset to vk record relative to first hbin
19  
20 6124 ▶ croak 'Missing registry file' if !defined $regfile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regfile 0 6124 6124
21 6124 ▶ croak 'Missing offset' if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 6124 6124
22  
23 6124 my $fh = $regfile->get_filehandle;
24  
25 # 0x00 dword = value length (negative = allocated)
26 # 0x04 word = 'vk' signature
27 # 0x06 word = value name length
28 # 0x08 dword = value data length (bit 31 set => data stored inline)
29 # 0x0c dword = offset to data/inline data
30 # 0x10 dword = value type
31 # 0x14 word = flags (bit 1 set => compressed name)
32 # 0x16 word
33 # 0x18 = value name [for value name length bytes]
34  
35 # Extracted offsets are always relative to first hbin
36  
37 6124 sysseek($fh, $offset, 0);
38 6124 my $bytes_read = sysread($fh, my $vk_header, VK_HEADER_LENGTH);
39 6124 ▶ if ($bytes_read != VK_HEADER_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 24) 1 6123 6124
40 1 warnf('Could not read value at 0x%x', $offset);
41 1 return;
42 }
43  
44 6123 my ($length,
45 $sig,
46 $name_length,
47 $data_length,
48 $offset_to_data,
49 $type,
50 $flags,
51 ) = unpack('Va2vVVVv', $vk_header);
52  
53 6123 my $allocated = 0;
54 6123 ▶ if ($length > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 2147483647) 6123 0 6123
55 6123 $allocated = 1;
56 6123 $length = (0xffffffff - $length) + 1;
57 }
58 # allocated should be true
59  
60 6123 ▶ if ($length < VK_HEADER_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 24) 0 6123 6123
61 0 warnf('Invalid value entry length at 0x%x', $offset);
62 0 return;
63 }
64  
65 6123 ▶ if ($sig ne 'vk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig ne "vk") 1 6122 6123
66 1 warnf('Invalid signature for value at 0x%x', $offset);
67 1 return;
68 }
69  
70 6122 $bytes_read = sysread($fh, my $name, $name_length);
71 6122 ▶ if ($bytes_read != $name_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != $name_length) 1 6121 6122
72 1 warnf('Could not read name for value at 0x%x', $offset);
73 1 return;
74 }
75  
76 6121 ▶ if ($flags & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1) { } 6015 106 6121
77 6015 $name = decode($Parse::Win32Registry::Base::CODEPAGE, $name);
78 }
79 else {
80 106 $name = decode('UCS-2LE', $name);
81 };
82  
83 # If the top bit of the data_length is set, then
84 # the value is inline and stored in the offset to data field (at 0xc).
85 6121 my $data;
86 6121 my $data_inline = $data_length >> 31;
87 6121 ▶ if ($data_inline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_inline) { } 3194 2927 6121
88 # REG_DWORDs are always inline, but I've also seen
89 # REG_SZ, REG_BINARY, REG_EXPAND_SZ, and REG_NONE inline
90 3194 $data_length &= 0x7fffffff;
91 3194 ▶ if ($data_length > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_length > 4) { } 425 2769 3194
92 425 warnf("Invalid inline data length for value '%s' at 0x%x",
93 $name, $offset);
94 425 $data = undef;
95 }
96 else {
97 # unpack inline data from header
98 2769 $data = substr($vk_header, 0xc, $data_length);
99 }
100 }
101 else {
102 2927 ▶ if ($offset_to_data != 0 && $offset_to_data != 0xffffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_data != 0 and $offset_to_data != 4294967295) 2927 0 2927
Condition33%1/3
$offset_to_data != 0 and $offset_to_data != 4294967295
!ll&&!rl&&r
002927
MC/DC0%0/2
$offset_to_data != 0 and $offset_to_data != 4294967295
$offset_to_data != 0 $offset_to_data != 4294967295
Truth table33%1/3
A and B
A= $offset_to_data != 0B= $offset_to_data != 4294967295
ABresult
0X0
100
111
103 2927 $offset_to_data += OFFSET_TO_FIRST_HBIN;
104 2927 ▶ if ($offset_to_data < ($regfile->get_length - $data_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_data < $regfile->get_length - $data_length) { } 2926 1 2927
105 2926 $data = _extract_data($fh, $offset_to_data, $data_length);
106 }
107 else {
108 1 warnf("Invalid offset to data for value '%s' at 0x%x",
109 $name, $offset);
110 }
111 }
112 }
113  
114 6121 my $self = {};
115 6121 $self->{_regfile} = $regfile;
116 6121 $self->{_offset} = $offset;
117 6121 $self->{_length} = $length;
118 6121 $self->{_allocated} = $allocated;
119 6121 $self->{_tag} = $sig;
120 6121 $self->{_name} = $name;
121 6121 $self->{_name_length} = $name_length;
122 6121 $self->{_type} = $type;
123 6121 $self->{_data} = $data;
124 6121 $self->{_data_length} = $data_length;
125 6121 $self->{_data_inline} = $data_inline;
126 6121 $self->{_offset_to_data} = $offset_to_data;
127 6121 $self->{_flags} = $flags;
128 6121 bless $self, $class;
129  
130 6121 return $self;
131 }
132  
133 2926 ▶sub _extract_data {
Subroutine _extract_data: called CC=15 SCAR=46.3
134 2926 my $fh = shift;
135 2926 my $offset_to_data = shift;
136 2926 my $data_length = shift;
137  
138 2926 ▶ if ($offset_to_data == 0 || $offset_to_data == 0xffffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_data == 0 or $offset_to_data == 4294967295) 0 2926 2926
Condition33%1/3
$offset_to_data == 0 or $offset_to_data == 4294967295
l!l&&r!l&&!r
002926
MC/DC0%0/2
$offset_to_data == 0 or $offset_to_data == 4294967295
$offset_to_data == 0 $offset_to_data == 4294967295
Truth table33%1/3
A or B
A= $offset_to_data == 0B= $offset_to_data == 4294967295
ABresult
1X1
011
000
139 0 return undef;
140 }
141  
142 2926 sysseek($fh, $offset_to_data, 0);
143 2926 my $bytes_read = sysread($fh, my $data_header, 4);
144 2926 ▶ if ($bytes_read != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 4) 0 2926 2926
145 0 warnf('Could not read data at 0x%x', $offset_to_data);
146 0 return undef;
147 }
148  
149 2926 my ($max_data_length) = unpack('V', $data_header);
150  
151 2926 my $data_allocated = 0;
152 2926 ▶ if ($max_data_length > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_data_length > 2147483647) 2926 0 2926
153 2926 $data_allocated = 1;
154 2926 $max_data_length = (0xffffffff - $max_data_length) + 1;
155 }
156 # data_allocated should be true
157  
158 2926 my $data;
159  
160 2926 ▶ if ($data_length > $max_data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_length > $max_data_length) { } 0 2926 2926
161 0 $bytes_read = sysread($fh, my $db_entry, 8);
162 0 ▶ if ($bytes_read != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 8) 0 0 0
163 0 warnf('Could not read data at 0x%x', $offset_to_data);
164 0 return undef;
165 }
166  
167 0 my ($sig, $num_data_blocks, $offset_to_data_block_list)
168 = unpack('a2vV', $db_entry);
169 0 ▶ if ($sig ne 'db') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig ne "db") 0 0 0
170 0 warnf('Invalid signature for big data at 0x%x', $offset_to_data);
171 0 return undef;
172 }
173 0 $offset_to_data_block_list += OFFSET_TO_FIRST_HBIN;
174  
175 0 sysseek($fh, $offset_to_data_block_list + 4, 0);
176 0 $bytes_read = sysread($fh, my $data_block_list, $num_data_blocks * 4);
177 0 ▶ if ($bytes_read != $num_data_blocks * 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != $num_data_blocks * 4) 0 0 0
178 0 warnf('Could not read data block list at 0x%x',
179 $offset_to_data_block_list);
180 0 return undef;
181 }
182  
183 0 $data = "";
184 0 my @offsets = map { OFFSET_TO_FIRST_HBIN + $_ }
185 unpack("V$num_data_blocks", $data_block_list);
186 0 foreach my $offset (@offsets) {
187 0 sysseek($fh, $offset, 0);
188 0 $bytes_read = sysread($fh, my $block_header, 4);
189 0 ▶ if ($bytes_read != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 4) 0 0 0
190 0 warnf('Could not read data block at 0x%x', $offset);
191 0 return undef;
192 }
193 0 my ($block_length) = unpack('V', $block_header);
194 0 ▶ if ($block_length > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length > 2147483647) 0 0 0
195 0 $block_length = (0xffffffff - $block_length) + 1;
196 }
197 0 $bytes_read = sysread($fh, my $block_data, $block_length - 8);
198 0 ▶ if ($bytes_read != $block_length - 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != $block_length - 8) 0 0 0
199 0 warnf('Could not read data block at 0x%x', $offset);
200 0 return undef;
201 }
202 0 $data .= $block_data;
203 }
204 0 ▶ if (length($data) < $data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $data_length) 0 0 0
205 0 warnf("Insufficient data blocks for data at 0x%x", $offset_to_data);
206 0 return undef;
207 }
208 0 $data = substr($data, 0, $data_length);
209 0 return $data;
210 }
211 else {
212 2926 $bytes_read = sysread($fh, $data, $data_length);
213 2926 ▶ if ($bytes_read != $data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != $data_length) 0 2926 2926
214 0 warnf("Could not read data at 0x%x", $offset_to_data);
215 0 return undef;
216 }
217 }
218 2926 return $data;
219 }
220  
221 292 ▶sub get_data {
Subroutine get_data: called CC=14 SCAR=26.4
Pod undocumented
222 292 my $self = shift;
223  
224 292 my $type = $self->get_type;
225  
226 292 my $data = $self->{_data};
227 292 ▶ return if !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 11 281 292
228  
229 # apply decoding to appropriate data types
230 281 ▶ if ($type == REG_DWORD) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 4) { } 89 192 281
elsif ($type == 5) { } 44 148 192
elsif ($type == 1 or $type == 2) { } 42 106 148
elsif ($type == 7) { } 82 24 106
231 89 ▶ if (length($data) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data == 4) { } 64 25 89
232 64 $data = unpack('V', $data);
233 }
234 else {
235 # incorrect length for dword data
236 25 $data = undef;
237 }
238 }
239 elsif ($type == REG_DWORD_BIG_ENDIAN) {
240 44 ▶ if (length($data) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data == 4) { } 24 20 44
241 24 $data = unpack('N', $data);
242 }
243 else {
244 # incorrect length for dword data
245 20 $data = undef;
246 }
247 }
248 ▶ elsif ($type == REG_SZ || $type == REG_EXPAND_SZ) {
Condition66%2/3
$type == 1 or $type == 2
l!l&&r!l&&!r
420106
MC/DC50%1/2
$type == 1 or $type == 2
$type == 1 $type == 2
Truth table66%2/3
A or B
A= $type == 1B= $type == 2
ABresult
1X1
011
000
249 42 $data = decode('UCS-2LE', $data);
250 # snip off any terminating null
251 42 ▶ chop $data if substr($data, -1, 1) eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if substr($data, -1, 1) eq "\0" 22 20 42
252 }
253 elsif ($type == REG_MULTI_SZ) {
254 82 $data = decode('UCS-2LE', $data);
255 # snip off any terminating nulls
256 82 ▶ chop $data if substr($data, -1, 1) eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if substr($data, -1, 1) eq "\0" 56 26 82
257 82 ▶ chop $data if substr($data, -1, 1) eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if substr($data, -1, 1) eq "\0" 30 52 82
258 82 my @multi_sz = split("\0", $data, -1);
259 # make sure there is at least one empty string
260 82 ▶ @multi_sz = ('') if @multi_sz == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @multi_sz == 0 15 67 82
261 82 ▶ return wantarray ? @multi_sz : join($", @multi_sz);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 32 50 82
262 }
263  
264 199 return $data;
265 }
266  
267 53 ▶sub as_regedit_export {
Subroutine as_regedit_export: called CC=11 SCAR=24.1
Pod undocumented
268 53 my $self = shift;
269 53 ▶ my $version = shift || 5;
Condition50%1/2
shift() || 5
l!l
053
MC/DC0%0/1
shift() || 5
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
270  
271 53 my $name = $self->get_name;
272 53 ▶ my $export = $name eq '' ? '@=' : '"' . $name . '"=';
Branch: 1 branch
BranchTrueFalseTotal
$name eq '' ? : 1 52 53
273  
274 53 my $type = $self->get_type;
275  
276 # XXX
277 # if (!defined $self->{_data}) {
278 # $name = $name eq '' ? '@' : qq{"$name"};
279 # return qq{; $name=(invalid data)\n};
280 # }
281  
282 53 ▶ if ($type == REG_SZ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 1) { } 8 45 53
elsif ($type == 3) { } 4 41 45
elsif ($type == 4) { } 16 25 41
elsif ($type == 2 or $type == 7) { } 12 13 25
283 8 my $data = $self->get_data;
284 8 ▶ $data = '' if !defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 8 8
285 8 $export .= '"' . $data . '"';
286 8 $export .= "\n";
287 }
288 elsif ($type == REG_BINARY) {
289 4 $export .= "hex:";
290 4 $export .= format_octets($self->{_data}, length($export));
291 }
292 elsif ($type == REG_DWORD) {
293 16 my $data = $self->get_data;
294 16 ▶ $export .= defined($data)
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 10 6 16
295 ? sprintf("dword:%08x", $data)
296 : "dword:";
297 16 $export .= "\n";
298 }
299 ▶ elsif ($type == REG_EXPAND_SZ || $type == REG_MULTI_SZ) {
Condition66%2/3
$type == 2 or $type == 7
l!l&&r!l&&!r
01213
MC/DC50%1/2
$type == 2 or $type == 7
$type == 2 $type == 7
Truth table66%2/3
A or B
A= $type == 2B= $type == 7
ABresult
1X1
011
000
300 my $data = $version == 4
301 ? encode("ascii", $self->{_data}) # unicode->ascii
302 12 ▶ : $self->{_data}; # raw data
Branch: 1 branch
BranchTrueFalseTotal
$version == 4 ? : 0 12 12
303 12 $export .= sprintf("hex(%x):", $type);
304 12 $export .= format_octets($data, length($export));
305 }
306 else {
307 13 $export .= sprintf("hex(%x):", $type);
308 13 $export .= format_octets($self->{_data}, length($export));
309 }
310 53 return $export;
311 }
312  
313 0 ▶sub parse_info {
Subroutine parse_info: not called CC=2 SCAR=17.9
Pod undocumented
314 0 my $self = shift;
315  
316 my $info = sprintf '0x%x vk len=0x%x alloc=%d "%s" type=%d',
317 $self->{_offset},
318 $self->{_length},
319 $self->{_allocated},
320 $self->{_name},
321 0 $self->{_type};
322 0 ▶ if ($self->{_data_inline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_data_inline'}) { } 0 0 0
323 $info .= sprintf ' data=inline,len=0x%x',
324 0 $self->{_data_length};
325 }
326 else {
327 $info .= sprintf ' data=0x%x,len=0x%x',
328 $self->{_offset_to_data},
329 0 $self->{_data_length};
330 }
331 0 return $info;
332 }
333  
334 13 1;
« script/wxregcompare.pl ↑ Summary script/regtree.pl »