KDBX/Loader/XML.pm

filter: Statementstmt 90.1% 264 / 293 Branchbran 63.7% 93 / 146 Conditioncond 57.6% 15 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 94.5% 52 / 55 Podpod n/a totaltotal 80.1% 429 / 535 CC 86 SCAR 18.1
CC
86
Cov
80%
_read_xml_element
17.2
_resolve_binary_refs
8.7
_read_inner_body
8.6
CRAP 6.1
« KDBX/Object.pm ↑ Summary KDBX/Error.pm »
1 package File::KDBX::Loader::XML;
2 # ABSTRACT: Load unencrypted XML KeePass files
3  
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Crypt::Misc 0.029 qw(decode_b64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::KDBX::Constants qw(:version :time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::KDBX::Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::KDBX::Util qw(:class :int :text gunzip erase_scoped);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Time::Piece 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 extends 'File::KDBX::Loader';
20 2686 ▶ 
Subroutine _reader: called CC=3 SCAR=11.8
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 2686 2686
21 39 ▶our $VERSION = '0.905'; # VERSION
Subroutine _safe: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 39 39
22 2686 ▶ 
Condition50%1/2
$_[0]{"_reader"} //= undef
l!l
26860
MC/DC0%0/1
$_[0]{"_reader"} //= undef
$_[0]{"_reader"}
Truth table50%1/2
A
A= $_[0]{"_reader"}
Aresult
11
00
23 39 ▶has '_reader', is => 'ro';
Condition100%2/2
$_[0]{"_safe"} //= scalar &$default($_[0])
l!l
2712
MC/DC100%1/1
$_[0]{"_safe"} //= scalar &$default($_[0])
$_[0]{"_safe"}
Truth table100%2/2
A
A= $_[0]{"_safe"}
Aresult
11
00
24 12 ▶has '_safe', is => 'ro', default => sub { File::KDBX::Safe->new(cipher => $_[0]->kdbx->random_stream) };
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 sub _read {
27 0 ▶ my $self = shift;
Subroutine _read: not called CC=1 SCAR=6.9
28 0 my $fh = shift;
29  
30 0 $self->_read_inner_body($fh);
31 }
32  
33 sub _read_inner_body {
34 21 ▶ my $self = shift;
Subroutine _read_inner_body: called CC=8 SCAR=21.5
35 21 my $fh = shift;
36  
37 21 my $reader = $self->{_reader} = XML::LibXML::Reader->new(IO => $fh);
38  
39 21 delete $self->{_safe};
40 21 my $root_done;
41  
42 21 my $pattern = XML::LibXML::Pattern->new('/KeePassFile/Meta|/KeePassFile/Root');
43 21 ▶ while ($reader->nextPatternMatch($pattern) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->nextPatternMatch($pattern) == 1) 42 21 63
44 42 ▶ next if $reader->nodeType != XML_READER_TYPE_ELEMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType != 1 0 42 42
45 42 my $name = $reader->localName;
46 42 ▶ if ($name eq 'Meta') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'Meta') { } 21 21 42
elsif ($name eq 'Root') { } 21 0 21
47 21 $self->_read_xml_meta;
48 }
49 elsif ($name eq 'Root') {
50 21 ▶ if ($root_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root_done) 0 21 21
51 0 alert 'Ignoring extra Root element in KeePass XML file', line => $reader->lineNumber;
52 0 next;
53 }
54 21 $self->_read_xml_root;
55 21 $root_done = 1;
56 }
57 }
58  
59 21 ▶ if ($reader->readState == XML_READER_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->readState == 6) 0 21 21
60 0 throw 'Failed to parse KeePass XML';
61 }
62  
63 21 ▶ $self->kdbx->_safe($self->_safe) if $self->{_safe};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_safe"} 12 9 21
64  
65 21 $self->_resolve_binary_refs;
66 }
67  
68 sub _read_xml_meta {
69 21 ▶ my $self = shift;
Subroutine _read_xml_meta: called CC=1 SCAR=0.0
70  
71 21 $self->_read_xml_element($self->kdbx->meta,
72 Generator => 'text',
73 HeaderHash => 'binary',
74 DatabaseName => 'text',
75 DatabaseNameChanged => 'datetime',
76 DatabaseDescription => 'text',
77 DatabaseDescriptionChanged => 'datetime',
78 DefaultUserName => 'text',
79 DefaultUserNameChanged => 'datetime',
80 MaintenanceHistoryDays => 'number',
81 Color => 'text',
82 MasterKeyChanged => 'datetime',
83 MasterKeyChangeRec => 'number',
84 MasterKeyChangeForce => 'number',
85 MemoryProtection => \&_read_xml_memory_protection,
86 CustomIcons => \&_read_xml_custom_icons,
87 RecycleBinEnabled => 'bool',
88 RecycleBinUUID => 'uuid',
89 RecycleBinChanged => 'datetime',
90 EntryTemplatesGroup => 'uuid',
91 EntryTemplatesGroupChanged => 'datetime',
92 LastSelectedGroup => 'uuid',
93 LastTopVisibleGroup => 'uuid',
94 HistoryMaxItems => 'number',
95 HistoryMaxSize => 'number',
96 SettingsChanged => 'datetime',
97 Binaries => \&_read_xml_binaries,
98 CustomData => \&_read_xml_custom_data,
99 );
100 }
101  
102 sub _read_xml_memory_protection {
103 21 ▶ my $self = shift;
Subroutine _read_xml_memory_protection: called CC=2 SCAR=7.6
104 21 ▶ my $meta = shift // $self->kdbx->meta;
Condition33%1/3
shift() // $self->kdbx->meta
l!l&&r!l&&!r
2100
MC/DC0%0/2
shift() // $self->kdbx->meta
shift() $self->kdbx->meta
Truth table33%1/3
A // B
A= shift()B= $self->kdbx->meta
ABresult
1X1
011
000
105  
106 21 return $self->_read_xml_element(
107 ProtectTitle => 'bool',
108 ProtectUserName => 'bool',
109 ProtectPassword => 'bool',
110 ProtectURL => 'bool',
111 ProtectNotes => 'bool',
112 AutoEnableVisualHiding => 'bool',
113 );
114 }
115  
116 sub _read_xml_binaries {
117 12 ▶ my $self = shift;
Subroutine _read_xml_binaries: called CC=1 SCAR=0.2
118 12 my $kdbx = $self->kdbx;
119  
120 my $binaries = $self->_read_xml_element(
121 Binary => sub {
122 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=18.7
123 2 my $id = $self->_read_xml_attribute('ID');
124 2 my $compressed = $self->_read_xml_attribute('Compressed', 'bool', false);
125 2 my $protected = $self->_read_xml_attribute('Protected', 'bool', false);
126 2 my $data = $self->_read_xml_content('binary');
127  
128 2 ▶ my $binary = {
Branch: 1 branch
BranchTrueFalseTotal
$protected ? : 0 2 2
129 value => $data,
130 $protected ? (protect => true) : (),
131 };
132  
133 2 ▶ if ($protected) {
Branch: 2 branches
BranchTrueFalseTotal
if ($protected) { } 0 2 2
elsif ($compressed) { } 0 2 2
134 # if compressed, decompress later when the safe is unlocked
135 0 ▶ $self->_safe->add_protected($compressed ? \&gunzip : (), $binary);
Branch: 1 branch
BranchTrueFalseTotal
$compressed ? : 0 0 0
136 }
137 elsif ($compressed) {
138 0 $binary->{value} = gunzip($data);
139 }
140  
141 2 $id => $binary;
142 },
143 12 );
144  
145 12 $kdbx->binaries({%{$kdbx->binaries}, %$binaries});
146 12 return (); # do not add to meta
147 }
148  
149 sub _read_xml_custom_data {
150 32 ▶ my $self = shift;
Subroutine _read_xml_custom_data: called CC=1 SCAR=0.0
151  
152 return $self->_read_xml_element(
153 Item => sub {
154 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
155 10 my $item = $self->_read_xml_element(
156 Key => 'text',
157 Value => 'text',
158 LastModificationTime => 'datetime', # KDBX4.1
159 );
160 10 $item->{key} => $item;
161 },
162 32 );
163 }
164  
165 sub _read_xml_custom_icons {
166 14 ▶ my $self = shift;
Subroutine _read_xml_custom_icons: called CC=1 SCAR=1.2
167  
168 return $self->_read_xml_element([],
169 Icon => sub {
170 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 $self->_read_xml_element(
172 UUID => 'uuid',
173 Data => 'binary',
174 Name => 'text', # KDBX4.1
175 LastModificationTime => 'datetime', # KDBX4.1
176 );
177 },
178 14 );
179 }
180  
181 sub _read_xml_root {
182 21 ▶ my $self = shift;
Subroutine _read_xml_root: called CC=2 SCAR=7.0
183 21 my $kdbx = $self->kdbx;
184  
185 21 my $root = $self->_read_xml_element(
186 Group => \&_read_xml_group,
187 DeletedObjects => \&_read_xml_deleted_objects,
188 );
189  
190 21 $kdbx->deleted_objects($root->{deleted_objects});
191 21 ▶ $kdbx->root($root->{group}) if $root->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $root->{"group"} 21 0 21
192 }
193  
194 sub _read_xml_group {
195 53 ▶ my $self = shift;
Subroutine _read_xml_group: called CC=1 SCAR=0.0
196  
197 53 return $self->_read_xml_element({entries => [], groups => []},
198 UUID => 'uuid',
199 Name => 'text',
200 Notes => 'text',
201 Tags => 'text', # KDBX4.1
202 IconID => 'number',
203 CustomIconUUID => 'uuid',
204 Times => \&_read_xml_times,
205 IsExpanded => 'bool',
206 DefaultAutoTypeSequence => 'text',
207 EnableAutoType => 'tristate',
208 EnableSearching => 'tristate',
209 LastTopVisibleEntry => 'uuid',
210 CustomData => \&_read_xml_custom_data, # KDBX4
211 PreviousParentGroup => 'uuid', # KDBX4.1
212 Entry => [entries => \&_read_xml_entry],
213 Group => [groups => \&_read_xml_group],
214 );
215 }
216  
217 sub _read_xml_entry {
218 18 ▶ my $self = shift;
Subroutine _read_xml_entry: called CC=5 SCAR=16.2
219  
220 my $entry = $self->_read_xml_element({strings => [], binaries => []},
221 UUID => 'uuid',
222 IconID => 'number',
223 CustomIconUUID => 'uuid',
224 ForegroundColor => 'text',
225 BackgroundColor => 'text',
226 OverrideURL => 'text',
227 Tags => 'text',
228 Times => \&_read_xml_times,
229 AutoType => \&_read_xml_entry_auto_type,
230 PreviousParentGroup => 'uuid', # KDBX4.1
231 QualityCheck => 'bool', # KDBX4.1
232 String => [strings => \&_read_xml_entry_string],
233 Binary => [binaries => \&_read_xml_entry_binary],
234 CustomData => \&_read_xml_custom_data, # KDBX4
235 History => sub {
236 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
237 8 return $self->_read_xml_element([],
238 Entry => \&_read_xml_entry,
239 );
240 },
241 18 );
242  
243 18 my %strings;
244 18 ▶ for my $string (@{$entry->{strings} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$entry->{"strings"} or [] 0 18 18
245 95 $strings{$string->{key}} = $string->{value};
246 }
247 18 $entry->{strings} = \%strings;
248  
249 18 my %binaries;
250 18 ▶ for my $binary (@{$entry->{binaries} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$entry->{"binaries"} or [] 0 18 18
251 7 $binaries{$binary->{key}} = $binary->{value};
252 }
253 18 $entry->{binaries} = \%binaries;
254  
255 18 return $entry;
256 }
257  
258 sub _read_xml_times {
259 71 ▶ my $self = shift;
Subroutine _read_xml_times: called CC=1 SCAR=0.0
260  
261 71 return $self->_read_xml_element(
262 LastModificationTime => 'datetime',
263 CreationTime => 'datetime',
264 LastAccessTime => 'datetime',
265 ExpiryTime => 'datetime',
266 Expires => 'bool',
267 UsageCount => 'number',
268 LocationChanged => 'datetime',
269 );
270 }
271  
272 sub _read_xml_entry_string {
273 95 ▶ my $self = shift;
Subroutine _read_xml_entry_string: called CC=1 SCAR=0.0
274  
275 return $self->_read_xml_element(
276 Key => 'text',
277 Value => sub {
278 95 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
279  
280 95 my $protected = $self->_read_xml_attribute('Protected', 'bool', false);
281 95 my $protect_in_memory = $self->_read_xml_attribute('ProtectInMemory', 'bool', false);
282 95 ▶ my $protect = $protected || $protect_in_memory;
Condition66%2/3
$protected || $protect_in_memory
l!l&&r!l&&!r
27068
MC/DC50%1/2
$protected || $protect_in_memory
$protected $protect_in_memory
Truth table66%2/3
A || B
A= $protectedB= $protect_in_memory
ABresult
1X1
011
000
283  
284 95 ▶ my $val = $self->_read_xml_content($protected ? 'binary' : 'text');
Branch: 1 branch
BranchTrueFalseTotal
$protected ? : 27 68 95
285  
286 95 ▶ my $string = {
Branch: 1 branch
BranchTrueFalseTotal
$protect ? : 27 68 95
287 value => $val,
288 $protect ? (protect => true) : (),
289 };
290  
291 95 ▶ $self->_safe->add_protected(sub { decode('UTF-8', $_[0]) }, $string) if $protected;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $protected 27 68 95
292  
293 95 $string;
294 },
295 95 );
296 }
297  
298 sub _read_xml_entry_binary {
299 7 ▶ my $self = shift;
Subroutine _read_xml_entry_binary: called CC=1 SCAR=0.2
300  
301 return $self->_read_xml_element(
302 Key => 'text',
303 Value => sub {
304 7 ▶ my $self = shift;
Subroutine __ANON__: called CC=6 SCAR=19.5
305  
306 7 my $ref = $self->_read_xml_attribute('Ref');
307 7 my $compressed = $self->_read_xml_attribute('Compressed', 'bool', false);
308 7 my $protected = $self->_read_xml_attribute('Protected', 'bool', false);
309 7 my $binary = {};
310  
311 7 ▶ if (defined $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) { } 4 3 7
312 4 $binary->{ref} = $ref;
313 }
314 else {
315 3 $binary->{value} = $self->_read_xml_content('binary');
316 3 ▶ $binary->{protect} = true if $protected;
Branch: 1 branch
BranchTrueFalseTotal
if $protected 0 3 3
317  
318 3 ▶ if ($protected) {
Branch: 2 branches
BranchTrueFalseTotal
if ($protected) { } 0 3 3
elsif ($compressed) { } 0 3 3
319 # if compressed, decompress later when the safe is unlocked
320 0 ▶ $self->_safe->add_protected($compressed ? \&gunzip : (), $binary);
Branch: 1 branch
BranchTrueFalseTotal
$compressed ? : 0 0 0
321 }
322 elsif ($compressed) {
323 0 $binary->{value} = gunzip($binary->{value});
324 }
325 }
326  
327 7 $binary;
328 },
329 7 );
330 }
331  
332 sub _read_xml_entry_auto_type {
333 18 ▶ my $self = shift;
Subroutine _read_xml_entry_auto_type: called CC=1 SCAR=0.0
334  
335 return $self->_read_xml_element({associations => []},
336 Enabled => 'bool',
337 DataTransferObfuscation => 'number',
338 DefaultSequence => 'text',
339 Association => [associations => sub {
340 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
341 11 return $self->_read_xml_element(
342 Window => 'text',
343 KeystrokeSequence => 'text',
344 );
345 18 }],
346 );
347 }
348  
349 sub _read_xml_deleted_objects {
350 21 ▶ my $self = shift;
Subroutine _read_xml_deleted_objects: called CC=1 SCAR=0.0
351  
352 return $self->_read_xml_element(
353 DeletedObject => sub {
354 14 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
355 14 my $object = $self->_read_xml_element(
356 UUID => 'uuid',
357 DeletionTime => 'datetime',
358 );
359 14 $object->{uuid} => $object;
360 }
361 21 );
362 }
363  
364 ##############################################################################
365  
366 sub _resolve_binary_refs {
367 21 ▶ my $self = shift;
Subroutine _resolve_binary_refs: called CC=8 SCAR=21.7
368 21 my $kdbx = $self->kdbx;
369  
370 21 my $pool = $kdbx->binaries;
371  
372 21 my $entries = $kdbx->entries(history => 1);
373 21 ▶ while (my $entry = $entries->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $entries->next) 18 21 39
374 18 ▶ while (my ($key, $binary) = each %{$entry->binaries}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$entry->binaries;}) 7 18 25
375 7 ▶ my $ref = $binary->{ref} // next;
Condition100%2/2
$binary->{"ref"} // (next)
l!l
43
MC/DC100%1/1
$binary->{"ref"} // (next)
$binary->{"ref"}
Truth table100%2/2
A
A= $binary->{"ref"}
Aresult
11
00
376 4 ▶ next if defined $binary->{value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $binary->{"value"} 0 4 4
377  
378 4 my $data = $pool->{$ref};
379 4 ▶ if (!defined $data || !defined $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data and defined $data->{"value"}) 0 4 4
Condition33%1/3
defined $data and defined $data->{"value"}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $data and defined $data->{"value"}
defined $data defined $data->{"value"}
Truth table33%1/3
A and B
A= defined $dataB= defined $data->{"value"}
ABresult
0X0
100
111
380 0 alert "Found a reference to a missing binary: $key", key => $key, ref => $ref;
381 0 next;
382 }
383 4 $binary->{value} = $data->{value};
384 4 ▶ $binary->{protect} = true if $data->{protect};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"protect"} 0 4 4
385 4 delete $binary->{ref};
386 }
387 }
388 }
389  
390 ##############################################################################
391  
392 sub _read_xml_element {
393 447 ▶ my $self = shift;
Subroutine _read_xml_element: called CC=16 SCAR=28.4
394 447 ▶ my $args = @_ % 2 == 1 ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 132 315 447
395 447 my %spec = @_;
396  
397 447 my $reader = $self->_reader;
398 447 my $path = $reader->nodePath;
399 447 $path =~ s!\Q/text()\E$!!;
400  
401 447 ▶ return $args if $reader->isEmptyElement;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->isEmptyElement 72 375 447
402  
403 my $store = ref $args eq 'CODE' ? $args
404 : ref $args eq 'HASH' ? sub {
405 2218 ▶ my ($key, $val) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.3
406 2218 ▶ if (ref $args->{$key} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{$key} eq 'HASH') { } 0 2218 2218
elsif (ref $args->{$key} eq 'ARRAY') { } 146 2072 2218
407 0 $args->{$key}{$key} = $val;
408 }
409 elsif (ref $args->{$key} eq 'ARRAY') {
410 146 push @{$args->{$key}}, $val;
411 }
412 else {
413 2072 ▶ exists $args->{$key}
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$key} and alert("Overwriting value", "node", $reader->nodePath, "line", $reader->lineNumber) 0 2072 2072
414 and alert 'Overwriting value', node => $reader->nodePath, line => $reader->lineNumber;
415 2072 $args->{$key} = $val;
416 }
417 } : ref $args eq 'ARRAY' ? sub {
418 5 ▶ my ($key, $val) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
419 5 push @$args, $val;
420 375 ▶ } : sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 3 branches
BranchTrueFalseTotal
ref $args eq 'CODE' ? : 0 375 375
ref $args eq 'HASH' ? : 371 4 375
ref $args eq 'ARRAY' ? : 4 0 4
421  
422 375 my $pattern = XML::LibXML::Pattern->new("${path}|${path}/*");
423 375 ▶ while ($reader->nextPatternMatch($pattern) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->nextPatternMatch($pattern) == 1) 4397 0 4397
424 4397 ▶ last if $reader->nodePath eq $path && $reader->nodeType == XML_READER_TYPE_END_ELEMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodePath eq $path and $reader->nodeType == 15 375 4022 4397
Condition66%2/3
$reader->nodePath eq $path and $reader->nodeType == 15
!ll&&!rl&&r
40220375
MC/DC50%1/2
$reader->nodePath eq $path and $reader->nodeType == 15
$reader->nodePath eq $path $reader->nodeType == 15
Truth table66%2/3
A and B
A= $reader->nodePath eq $pathB= $reader->nodeType == 15
ABresult
0X0
100
111
425 4022 ▶ next if $reader->nodeType != XML_READER_TYPE_ELEMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType != 1 1669 2353 4022
426  
427 2353 my $name = $reader->localName;
428 2353 my $key = snakify($name);
429 2353 my $type = $spec{$name};
430 2353 ▶ ($key, $type) = @$type if ref $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $type eq "ARRAY" 157 2196 2353
431  
432 2353 ▶ if (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 2353 2353
433 0 ▶ exists $spec{$name} or alert "Ignoring unknown element: $name",
Branch: 1 branch
BranchTrueFalseTotal
exists $spec{$name} or alert("Ignoring unknown element: $name", "node", $reader->nodePath, "line", $reader->lineNumber) 0 0 0
434 node => $reader->nodePath,
435 line => $reader->lineNumber;
436 0 next;
437 }
438  
439 2353 ▶ if (ref $type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'CODE') { } 509 1844 2353
440 509 my @result = $self->$type($args, $reader->nodePath);
441 509 ▶ if (@result == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@result == 2) { } 26 483 509
elsif (@result == 1) { } 471 12 483
442 26 $store->(@result);
443 }
444 elsif (@result == 1) {
445 471 $store->($key, @result);
446 }
447 }
448 else {
449 1844 $store->($key, $self->_read_xml_content($type));
450 }
451 }
452  
453 375 return $args;
454 }
455  
456 sub _read_xml_attribute {
457 217 ▶ my $self = shift;
Subroutine _read_xml_attribute: called CC=8 SCAR=21.1
458 217 my $name = shift;
459 217 ▶ my $type = shift // 'text';
Condition100%2/2
shift() // "text"
l!l
2089
MC/DC100%1/1
shift() // "text"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
460 217 my $default = shift;
461 217 my $reader = $self->_reader;
462  
463 217 ▶ return $default if !$reader->hasAttributes;
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->hasAttributes 145 72 217
464  
465 72 my $value = trim($reader->getAttribute($name));
466 72 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 39 33 72
467 # try again after reading in all the attributes
468 39 $reader->moveToFirstAttribute;
469 39 ▶ while ($self->_reader->readAttributeValue == 1) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_reader->readAttributeValue == 1) 39 39 78
470 39 $reader->moveToElement;
471  
472 39 $value = trim($reader->getAttribute($name));
473 }
474  
475 72 ▶ return $default if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 39 33 72
476  
477 33 my $decoded = eval { _decode_primitive($value, $type) };
478 33 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 33 33
479 0 ▶ ref $err and $err->details(attribute => $name, node => $reader->nodePath, line => $reader->lineNumber);
Branch: 1 branch
BranchTrueFalseTotal
ref $err and $err->details("attribute", $name, "node", $reader->nodePath, "line", $reader->lineNumber) 0 0 0
480 0 throw $err
481 }
482  
483 33 return $decoded;
484 }
485  
486 sub _read_xml_content {
487 1944 ▶ my $self = shift;
Subroutine _read_xml_content: called CC=5 SCAR=16.8
488 1944 my $type = shift;
489 1944 my $reader = $self->_reader;
490  
491 1944 ▶ $reader->read if !$reader->isEmptyElement; # step into element
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->isEmptyElement 1669 275 1944
492 1944 ▶ return '' if !$reader->hasValue;
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->hasValue 275 1669 1944
493  
494 1669 my $content = trim($reader->value);
495  
496 1669 my $decoded = eval { _decode_primitive($content, $type) };
497 1669 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1669 1669
498 0 ▶ ref $err and $err->details(node => $reader->nodePath, line => $reader->lineNumber);
Branch: 1 branch
BranchTrueFalseTotal
ref $err and $err->details("node", $reader->nodePath, "line", $reader->lineNumber) 0 0 0
499 0 throw $err;
500 }
501  
502 1669 return $decoded;
503 }
504  
505 ##############################################################################
506  
507 1702 ▶sub _decode_primitive { goto &{__PACKAGE__."::_decode_$_[1]"} }
Subroutine _decode_primitive: called CC=1 SCAR=0.0
508  
509 sub _decode_binary {
510 246 ▶ local $_ = shift;
Subroutine _decode_binary: called CC=6 SCAR=20.2
511 246 ▶ return '' if !defined || (ref && !defined $$_);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or ref $_ and not defined $$_ 0 246 246
Condition33%2/6
not defined $_ or ref $_ and not defined $$_
l!l&&r!l&&!r
00246
ref $_ and not defined $$_
!ll&&!rl&&r
24600
MC/DC0%0/3
not defined $_ or ref $_ and not defined $$_
not defined $_ ref $_ not defined $$_
Truth table0%0/4
not(A) or B and not(C)
A= not defined $_B= ref $_C= not defined $$_
ABCresult
1XX1
0111
00X0
0100
512 246 ▶ $_ = eval { decode_b64(ref $_ ? $$_ : $_) };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 246 246
513 246 my $err = $@;
514 246 my $cleanup = erase_scoped $_;
515 246 ▶ $err and throw 'Failed to parse binary', error => $err;
Branch: 1 branch
BranchTrueFalseTotal
$err and throw("Failed to parse binary", "error", $err) 0 246 246
516 246 return $_;
517 }
518  
519 sub _decode_bool {
520 296 ▶ local $_ = shift;
Subroutine _decode_bool: called CC=4 SCAR=17.1
521 296 ▶ return true if /^True$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^True$/i 139 157 296
522 157 ▶ return false if /^False$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^False$/i 157 0 157
523 0 ▶ return false if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
524 0 throw 'Expected boolean', text => $_;
525 }
526  
527 sub _decode_datetime {
528 501 ▶ local $_ = shift;
Subroutine _decode_datetime: called CC=6 SCAR=18.6
529  
530 501 ▶ if (/^[A-Za-z0-9\+\/\=]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^[A-Za-z0-9\+/\=]+$]) 131 370 501
531 131 my $binary = eval { decode_b64($_) };
532 131 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 131 131
533 0 throw 'Failed to parse binary datetime', text => $_, error => $err;
534 }
535 131 ▶ throw $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 131 131
536 131 ▶ $binary .= \0 x (8 - length($binary)) if length($binary) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $binary < 8 0 131 131
537 131 my ($seconds_since_ad1) = unpack_Ql($binary);
538 131 my $epoch = $seconds_since_ad1 - TIME_SECONDS_AD1_TO_UNIX_EPOCH;
539 131 return gmtime($epoch);
540 }
541  
542 370 my $dt = eval { Time::Piece->strptime($_, '%Y-%m-%dT%H:%M:%SZ') };
543 370 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 370 370
544 0 throw 'Failed to parse datetime', text => $_, error => $err;
545 }
546 370 return $dt;
547 }
548  
549 sub _decode_tristate {
550 106 ▶ local $_ = shift;
Subroutine _decode_tristate: called CC=3 SCAR=18.1
551 106 ▶ return undef if /^null$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^null$/i 106 0 106
552 0 my $tristate = eval { _decode_bool($_) };
553 0 ▶ $@ and throw 'Expected tristate', text => $_, error => $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and throw("Expected tristate", "text", $_, "error", $@) 0 0 0
554 0 return $tristate;
555 }
556  
557 sub _decode_number {
558 257 ▶ local $_ = shift;
Subroutine _decode_number: called CC=2 SCAR=7.0
559 257 $_ = _decode_text($_);
560 257 ▶ looks_like_number($_) or throw 'Expected number', text => $_;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) or throw("Expected number", "text", $_) 0 257 257
561 257 return $_+0;
562 }
563  
564 sub _decode_text {
565 553 ▶ local $_ = shift;
Subroutine _decode_text: called CC=2 SCAR=7.1
566 553 ▶ return '' if !defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 553 553
567 553 return $_;
568 }
569  
570 sub _decode_uuid {
571 207 ▶ local $_ = shift;
Subroutine _decode_uuid: called CC=3 SCAR=11.2
572 207 my $uuid = eval { _decode_binary($_) };
573 207 ▶ $@ and throw 'Expected UUID', text => $_, error => $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and throw("Expected UUID", "text", $_, "error", $@) 0 207 207
574 207 ▶ length($uuid) == 16 or throw 'Invalid UUID size', size => length($uuid);
Branch: 1 branch
BranchTrueFalseTotal
length $uuid == 16 or throw("Invalid UUID size", "size", length $uuid) 0 207 207
575 207 return $uuid;
576 }
577  
578 6 1;
579  
580 __END__
« KDBX/Object.pm ↑ Summary KDBX/Error.pm »