KDBX/Dumper/XML.pm

filter: Statementstmt 89.6% 268 / 299 Branchbran 61.6% 69 / 112 Conditioncond 57.1% 36 / 63 MC/DCmcdc 23.6% 9 / 38 Subroutinesub 95.7% 45 / 47 Podpod 100% 5 / 5 totaltotal 76.6% 432 / 564 CC 91 SCAR 20.1
CC
91
Cov
79%
_write_xml_entry
23.0
_write_xml_custom_icons
14.1
_write_xml_compressed_content
10.7
CRAP 7.4
« KDBX.pm ↑ Summary KDBX/Safe.pm »
1 package File::KDBX::Dumper::XML;
2 # ABSTRACT: Dump 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::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Crypt::Misc 0.029 qw(encode_b64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::KDBX::Constants qw(:version :time);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::KDBX::Util qw(:class :int erase_scoped gzip snakify);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Scalar::Util qw(blessed isdual looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Time::Piece 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 extends 'File::KDBX::Dumper';
21  
22 12 ▶our $VERSION = '0.905'; # VERSION
Subroutine allow_protection: called CC=4 SCAR=14.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 12 12
23 12 ▶ 
Subroutine binaries: called CC=4 SCAR=15.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 12 12
unless builtin::blessed($_[0]) 0 7 7
24 12 ▶ 
Subroutine compress_binaries: called CC=4 SCAR=14.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 7 7
unless builtin::blessed($_[0]) 0 2 2
Condition66%2/3
$_[0]{"allow_protection"} //= $default
l!l&&r!l&&!r
660
MC/DC0%0/2
$_[0]{"allow_protection"} //= $default
$_[0]{"allow_protection"} $default
Truth table66%2/3
A //= B
A= $_[0]{"allow_protection"}B= $default
ABresult
1X1
011
000
25 7 ▶has allow_protection => 1;
Subroutine compress_datetimes: called CC=4 SCAR=14.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 2 2
unless builtin::blessed($_[0]) 0 256 256
Condition50%1/2
$_[0]{"binaries"} //= scalar &$default($_[0])
l!l
07
MC/DC0%0/1
$_[0]{"binaries"} //= scalar &$default($_[0])
$_[0]{"binaries"}
Truth table50%1/2
A
A= $_[0]{"binaries"}
Aresult
11
00
26 2 ▶has binaries => sub { $_[0]->kdbx->version < KDBX_VERSION_4_0 };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $#_ 0 256 256
Condition50%1/2
$_[0]{"compress_binaries"} //= undef
l!l
02
MC/DC0%0/1
$_[0]{"compress_binaries"} //= undef
$_[0]{"compress_binaries"}
Truth table50%1/2
A
A= $_[0]{"compress_binaries"}
Aresult
11
00
27 256 ▶has 'compress_binaries';
Condition100%2/2
$_[0]{"compress_datetimes"} //= undef
l!l
104152
MC/DC100%1/1
$_[0]{"compress_datetimes"} //= undef
$_[0]{"compress_datetimes"}
Truth table100%2/2
A
A= $_[0]{"compress_datetimes"}
Aresult
11
00
28 6 has 'compress_datetimes';
29  
30 0 ▶sub header_hash { $_[0]->{header_hash} }
Subroutine header_hash: not called CC=1 SCAR=6.9
Pod documented
31  
32 34 ▶sub _binaries_written { $_[0]->{_binaries_written} //= {} }
Subroutine _binaries_written: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"_binaries_written"} //= {}
l!l
268
MC/DC100%1/1
$_[0]{"_binaries_written"} //= {}
$_[0]{"_binaries_written"}
Truth table100%2/2
A
A= $_[0]{"_binaries_written"}
Aresult
11
00
33  
34 12 ▶sub _random_stream { $_[0]->{random_stream} //= $_[0]->kdbx->random_stream }
Subroutine _random_stream: called CC=2 SCAR=7.2
Condition66%2/3
$_[0]{"random_stream"} //= $_[0]->kdbx->random_stream
l!l&&r!l&&!r
660
MC/DC0%0/2
$_[0]{"random_stream"} //= $_[0]->kdbx->random_stream
$_[0]{"random_stream"} $_[0]->kdbx->random_stream
Truth table66%2/3
A //= B
A= $_[0]{"random_stream"}B= $_[0]->kdbx->random_stream
ABresult
1X1
011
000
35  
36 sub _dump {
37 0 ▶ my $self = shift;
Subroutine _dump: not called CC=1 SCAR=6.9
38 0 my $fh = shift;
39  
40 0 $self->_write_inner_body($fh, $self->header_hash);
41 }
42  
43 sub _write_inner_body {
44 15 ▶ my $self = shift;
Subroutine _write_inner_body: called CC=1 SCAR=0.0
45 15 my $fh = shift;
46 15 my $header_hash = shift;
47  
48 15 my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
49 15 $dom->setStandalone(1);
50  
51 15 my $doc = XML::LibXML::Element->new('KeePassFile');
52 15 $dom->setDocumentElement($doc);
53  
54 15 my $meta = XML::LibXML::Element->new('Meta');
55 15 $doc->appendChild($meta);
56 15 $self->_write_xml_meta($meta, $header_hash);
57  
58 15 my $root = XML::LibXML::Element->new('Root');
59 15 $doc->appendChild($root);
60 15 $self->_write_xml_root($root);
61  
62 15 $dom->toFH($fh, 1);
63 }
64  
65 sub _write_xml_meta {
66 15 ▶ my $self = shift;
Subroutine _write_xml_meta: called CC=7 SCAR=19.7
67 15 my $node = shift;
68 15 my $header_hash = shift;
69  
70 15 my $meta = $self->kdbx->meta;
71 15 ▶ local $meta->{generator} = $self->kdbx->user_agent_string // __PACKAGE__;
Condition50%1/2
$self->kdbx->user_agent_string // __PACKAGE__
l!l
150
MC/DC0%0/1
$self->kdbx->user_agent_string // __PACKAGE__
$self->kdbx->user_agent_string
Truth table50%1/2
A
A= $self->kdbx->user_agent_string
Aresult
11
00
72 15 local $meta->{header_hash} = $header_hash;
73  
74 $self->_write_xml_from_pairs($node, $meta,
75 Generator => 'text',
76 $self->kdbx->version < KDBX_VERSION_4_0 && defined $meta->{header_hash} ? (
77 15 ▶ HeaderHash => 'binary',
Branch: 3 branches
BranchTrueFalseTotal
$self->kdbx->version < 262144 && defined $meta->{'header_hash'} ? : 8 7 15
$self->kdbx->version >= 262144 ? : 7 8 15
$self->kdbx->version < 262144 || $self->binaries ? : 8 7 15
Condition66%4/6
$self->kdbx->version < 262144 and defined $meta->{"header_hash"}
!ll&&!rl&&r
708
$self->kdbx->version < 262144 or $self->binaries
l!l&&r!l&&!r
807
MC/DC50%1/2
$self->kdbx->version < 262144 and defined $meta->{"header_hash"}
$self->kdbx->version < 262144 defined $meta->{"header_hash"}
MC/DC50%1/2
$self->kdbx->version < 262144 or $self->binaries
$self->kdbx->version < 262144 $self->binaries
Truth table66%2/3
A and B
A= $self->kdbx->version < 262144B= defined $meta->{"header_hash"}
ABresult
0X0
100
111
Truth table66%2/3
A or B
A= $self->kdbx->version < 262144B= $self->binaries
ABresult
1X1
011
000
78 ) : (),
79 DatabaseName => 'text',
80 DatabaseNameChanged => 'datetime',
81 DatabaseDescription => 'text',
82 DatabaseDescriptionChanged => 'datetime',
83 DefaultUserName => 'text',
84 DefaultUserNameChanged => 'datetime',
85 MaintenanceHistoryDays => 'number',
86 Color => 'text',
87 MasterKeyChanged => 'datetime',
88 MasterKeyChangeRec => 'number',
89 MasterKeyChangeForce => 'number',
90 MemoryProtection => \&_write_xml_memory_protection,
91 CustomIcons => \&_write_xml_custom_icons,
92 RecycleBinEnabled => 'bool',
93 RecycleBinUUID => 'uuid',
94 RecycleBinChanged => 'datetime',
95 EntryTemplatesGroup => 'uuid',
96 EntryTemplatesGroupChanged => 'datetime',
97 LastSelectedGroup => 'uuid',
98 LastTopVisibleGroup => 'uuid',
99 HistoryMaxItems => 'number',
100 HistoryMaxSize => 'number',
101 $self->kdbx->version >= KDBX_VERSION_4_0 ? (
102 SettingsChanged => 'datetime',
103 ) : (),
104 $self->kdbx->version < KDBX_VERSION_4_0 || $self->binaries ? (
105 Binaries => \&_write_xml_binaries,
106 ) : (),
107 CustomData => \&_write_xml_custom_data,
108 );
109 }
110  
111 sub _write_xml_memory_protection {
112 15 ▶ my $self = shift;
Subroutine _write_xml_memory_protection: called CC=1 SCAR=0.0
113 15 my $node = shift;
114  
115 15 my $memory_protection = $self->kdbx->meta->{memory_protection};
116  
117 15 $self->_write_xml_from_pairs($node, $memory_protection,
118 ProtectTitle => 'bool',
119 ProtectUserName => 'bool',
120 ProtectPassword => 'bool',
121 ProtectURL => 'bool',
122 ProtectNotes => 'bool',
123 # AutoEnableVisualHiding => 'bool',
124 );
125 }
126  
127 sub _write_xml_binaries {
128 8 ▶ my $self = shift;
Subroutine _write_xml_binaries: called CC=8 SCAR=21.6
129 8 my $node = shift;
130  
131 8 my $kdbx = $self->kdbx;
132  
133 8 my $new_ref = keys %{$self->_binaries_written};
134 8 my $written = $self->_binaries_written;
135  
136 8 my $entries = $kdbx->entries(history => 1);
137 8 ▶ while (my $entry = $entries->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $entries->next) 7 8 15
138 7 for my $key (keys %{$entry->binaries}) {
139 3 my $binary = $entry->binaries->{$key};
140 3 ▶ if (defined $binary->{ref} && defined $kdbx->binaries->{$binary->{ref}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}) 0 3 3
Condition33%1/3
defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}
defined $binary->{"ref"} defined $kdbx->binaries->{$binary->{"ref"}}
Truth table33%1/3
A and B
A= defined $binary->{"ref"}B= defined $kdbx->binaries->{$binary->{"ref"}}
ABresult
0X0
100
111
141 0 $binary = $kdbx->binaries->{$binary->{ref}};
142 }
143  
144 3 ▶ if (!defined $binary->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $binary->{"value"}) 0 3 3
145 0 alert "Skipping binary which has no value: $key", key => $key;
146 0 next;
147 }
148  
149 3 my $hash = digest_data('SHA256', $binary->{value});
150 3 ▶ if (defined $written->{$hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written->{$hash}) { } 1 2 3
151 # nothing
152 }
153 else {
154 2 my $binary_node = $node->addNewChild(undef, 'Binary');
155 2 $binary_node->setAttribute('ID', _encode_text($new_ref));
156 2 ▶ $binary_node->setAttribute('Protected', _encode_bool(true)) if $binary->{protect};
Branch: 1 branch
BranchTrueFalseTotal
if $binary->{"protect"} 0 2 2
157 2 $self->_write_xml_compressed_content($binary_node, \$binary->{value}, $binary->{protect});
158 2 $written->{$hash} = $new_ref++;
159 }
160 }
161 }
162 }
163  
164 sub _write_xml_compressed_content {
165 2 ▶ my $self = shift;
Subroutine _write_xml_compressed_content: called CC=7 SCAR=23.7
166 2 my $node = shift;
167 2 my $value = shift;
168 2 my $protect = shift;
169  
170 2 my @cleanup;
171  
172 my $encoded;
173 2 ▶ if (utf8::is_utf8($$value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($$value)) 0 2 2
174 0 $encoded = encode('UTF-8', $$value);
175 0 push @cleanup, erase_scoped $encoded;
176 0 $value = \$encoded;
177 }
178  
179 2 my $should_compress = $self->compress_binaries;
180 2 ▶ my $try_compress = $should_compress || !defined $should_compress;
Condition33%1/3
$should_compress || !defined($should_compress)
l!l&&r!l&&!r
020
MC/DC0%0/2
$should_compress || !defined($should_compress)
$should_compress !defined($should_compress)
Truth table33%1/3
A || not(B)
A= $should_compressB= !defined($should_compress)
ABresult
1X1
011
000
181  
182 2 my $compressed;
183 2 ▶ if ($try_compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try_compress) 2 0 2
184 2 $compressed = gzip($$value);
185 2 push @cleanup, erase_scoped $compressed;
186  
187 2 ▶ if ($should_compress || length($compressed) < length($$value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_compress or length $compressed < length $$value) 0 2 2
Condition33%1/3
$should_compress or length $compressed < length $$value
l!l&&r!l&&!r
002
MC/DC0%0/2
$should_compress or length $compressed < length $$value
$should_compress length $compressed < length $$value
Truth table33%1/3
A or B
A= $should_compressB= length $compressed < length $$value
ABresult
1X1
011
000
188 0 $value = \$compressed;
189 0 $node->setAttribute('Compressed', _encode_bool(true));
190 }
191 }
192  
193 2 my $encrypted;
194 2 ▶ if ($protect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protect) 0 2 2
195 0 $encrypted = $self->_random_stream->crypt($$value);
196 0 push @cleanup, erase_scoped $encrypted;
197 0 $value = \$encrypted;
198 }
199  
200 2 $node->appendText(_encode_binary($$value));
201 }
202  
203 sub _write_xml_custom_icons {
204 15 ▶ my $self = shift;
Subroutine _write_xml_custom_icons: called CC=5 SCAR=26.5
205 15 my $node = shift;
206  
207 15 my $custom_icons = $self->kdbx->custom_icons;
208  
209 15 for my $icon (@$custom_icons) {
210 0 ▶ $icon->{uuid} && $icon->{data} or next;
Branch: 1 branch
BranchTrueFalseTotal
$icon->{"uuid"} and $icon->{"data"} or next 0 0 0
Condition0%0/3
$icon->{"uuid"} and $icon->{"data"}
!ll&&!rl&&r
000
MC/DC0%0/2
$icon->{"uuid"} and $icon->{"data"}
$icon->{"uuid"} $icon->{"data"}
Truth table0%0/3
A and B
A= $icon->{"uuid"}B= $icon->{"data"}
ABresult
0X0
100
111
211 0 my $icon_node = $node->addNewChild(undef, 'Icon');
212  
213 0 ▶ $self->_write_xml_from_pairs($icon_node, $icon,
Branch: 1 branch
BranchTrueFalseTotal
262145 <= $self->kdbx->version ? : 0 0 0
214 UUID => 'uuid',
215 Data => 'binary',
216 KDBX_VERSION_4_1 <= $self->kdbx->version ? (
217 Name => 'text',
218 LastModificationTime => 'datetime',
219 ) : (),
220 );
221 }
222 }
223  
224 sub _write_xml_custom_data {
225 26 ▶ my $self = shift;
Subroutine _write_xml_custom_data: called CC=5 SCAR=16.6
226 26 my $node = shift;
227 26 ▶ my $custom_data = shift || {};
Condition50%1/2
shift() || {}
l!l
260
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
228  
229 26 for my $key (sort keys %$custom_data) {
230 10 my $item = $custom_data->{$key};
231 10 my $item_node = $node->addNewChild(undef, 'Item');
232  
233 10 ▶ local $item->{key} = $key if !defined $item->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"key"} 0 10 10
234  
235 10 ▶ $self->_write_xml_from_pairs($item_node, $item,
Branch: 1 branch
BranchTrueFalseTotal
262145 <= $self->kdbx->version ? : 0 10 10
236 Key => 'text',
237 Value => 'text',
238 KDBX_VERSION_4_1 <= $self->kdbx->version ? (
239 LastModificationTime => 'datetime',
240 ) : (),
241 );
242 }
243 }
244  
245 sub _write_xml_root {
246 15 ▶ my $self = shift;
Subroutine _write_xml_root: called CC=2 SCAR=6.9
247 15 my $node = shift;
248 15 my $kdbx = $self->kdbx;
249  
250 15 my $guard = $kdbx->unlock_scoped;
251  
252 15 ▶ if (my $group = $kdbx->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $kdbx->root) 15 0 15
253 15 my $group_node = $node->addNewChild(undef, 'Group');
254 15 $self->_write_xml_group($group_node, $group->_committed);
255 }
256  
257 15 undef $guard; # re-lock if needed, as early as possible
258  
259 15 my $deleted_objects_node = $node->addNewChild(undef, 'DeletedObjects');
260 15 $self->_write_xml_deleted_objects($deleted_objects_node);
261 }
262  
263 sub _write_xml_group {
264 23 ▶ my $self = shift;
Subroutine _write_xml_group: called CC=7 SCAR=19.7
265 23 my $node = shift;
266 23 my $group = shift;
267  
268 $self->_write_xml_from_pairs($node, $group,
269 UUID => 'uuid',
270 Name => 'text',
271 Notes => 'text',
272 KDBX_VERSION_4_1 <= $self->kdbx->version ? (
273 Tags => 'text',
274 ) : (),
275 IconID => 'number',
276 defined $group->{custom_icon_uuid} ? (
277 23 ▶ CustomIconUUID => 'uuid',
Branch: 4 branches
BranchTrueFalseTotal
262145 <= $self->kdbx->version ? : 0 23 23
defined $group->{'custom_icon_uuid'} ? : 0 23 23
262144 <= $self->kdbx->version ? : 9 14 23
262145 <= $self->kdbx->version ? : 0 23 23
278 ) : (),
279 Times => \&_write_xml_times,
280 IsExpanded => 'bool',
281 DefaultAutoTypeSequence => 'text',
282 EnableAutoType => 'tristate',
283 EnableSearching => 'tristate',
284 LastTopVisibleEntry => 'uuid',
285 KDBX_VERSION_4_0 <= $self->kdbx->version ? (
286 CustomData => \&_write_xml_custom_data,
287 ) : (),
288 KDBX_VERSION_4_1 <= $self->kdbx->version ? (
289 PreviousParentGroup => 'uuid',
290 ) : (),
291 );
292  
293 23 for my $entry (@{$group->entries}) {
294 6 my $entry_node = $node->addNewChild(undef, 'Entry');
295 6 $self->_write_xml_entry($entry_node, $entry->_committed);
296 }
297  
298 23 for my $group (@{$group->groups}) {
299 8 my $group_node = $node->addNewChild(undef, 'Group');
300 8 $self->_write_xml_group($group_node, $group->_committed);
301 }
302 }
303  
304 sub _write_xml_entry {
305 9 ▶ my $self = shift;
Subroutine _write_xml_entry: called CC=17 SCAR=31.4
306 9 my $node = shift;
307 9 my $entry = shift;
308 9 my $in_history = shift;
309  
310 $self->_write_xml_from_pairs($node, $entry,
311 UUID => 'uuid',
312 IconID => 'number',
313 defined $entry->{custom_icon_uuid} ? (
314 9 ▶ CustomIconUUID => 'uuid',
Branch: 2 branches
BranchTrueFalseTotal
defined $entry->{'custom_icon_uuid'} ? : 0 9 9
262145 <= $self->kdbx->version ? : 0 9 9
315 ) : (),
316 ForegroundColor => 'text',
317 BackgroundColor => 'text',
318 OverrideURL => 'text',
319 Tags => 'text',
320 Times => \&_write_xml_times,
321 KDBX_VERSION_4_1 <= $self->kdbx->version ? (
322 QualityCheck => 'bool',
323 PreviousParentGroup => 'uuid',
324 ) : (),
325 );
326  
327 9 ▶ for my $key (sort keys %{$entry->{strings} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$entry->{"strings"} or {} 0 9 9
328 45 my $string = $entry->{strings}{$key};
329 45 my $string_node = $node->addNewChild(undef, 'String');
330 45 ▶ local $string->{key} = $string->{key} // $key;
Condition33%1/3
$string->{"key"} // $key
l!l&&r!l&&!r
0450
MC/DC0%0/2
$string->{"key"} // $key
$string->{"key"} $key
Truth table33%1/3
A // B
A= $string->{"key"}B= $key
ABresult
1X1
011
000
331 45 $self->_write_xml_entry_string($string_node, $string);
332 }
333  
334 9 my $kdbx = $self->kdbx;
335 9 my $new_ref = keys %{$self->_binaries_written};
336 9 my $written = $self->_binaries_written;
337  
338 9 ▶ for my $key (sort keys %{$entry->{binaries} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$entry->{"binaries"} or {} 0 9 9
339 3 my $binary = $entry->binaries->{$key};
340 3 ▶ if (defined $binary->{ref} && defined $kdbx->binaries->{$binary->{ref}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}) 0 3 3
Condition33%1/3
defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $binary->{"ref"} and defined $kdbx->binaries->{$binary->{"ref"}}
defined $binary->{"ref"} defined $kdbx->binaries->{$binary->{"ref"}}
Truth table33%1/3
A and B
A= defined $binary->{"ref"}B= defined $kdbx->binaries->{$binary->{"ref"}}
ABresult
0X0
100
111
341 0 $binary = $kdbx->binaries->{$binary->{ref}};
342 }
343  
344 3 ▶ if (!defined $binary->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $binary->{"value"}) 0 3 3
345 0 alert "Skipping binary which has no value: $key", key => $key;
346 0 next;
347 }
348  
349 3 my $binary_node = $node->addNewChild(undef, 'Binary');
350 3 $binary_node->addNewChild(undef, 'Key')->appendText(_encode_text($key));
351 3 my $value_node = $binary_node->addNewChild(undef, 'Value');
352  
353 3 my $hash = digest_data('SHA256', $binary->{value});
354 3 ▶ if (defined $written->{$hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written->{$hash}) { } 3 0 3
355 # write reference
356 3 $value_node->setAttribute('Ref', _encode_text($written->{$hash}));
357 }
358 else {
359 # write actual binary
360 0 ▶ $value_node->setAttribute('Protected', _encode_bool(true)) if $binary->{protect};
Branch: 1 branch
BranchTrueFalseTotal
if $binary->{"protect"} 0 0 0
361 0 $self->_write_xml_compressed_content($value_node, \$binary->{value}, $binary->{protect});
362 0 $written->{$hash} = $new_ref++;
363 }
364 }
365  
366 9 $self->_write_xml_from_pairs($node, $entry,
367 AutoType => \&_write_xml_entry_auto_type,
368 );
369  
370 9 ▶ $self->_write_xml_from_pairs($node, $entry,
Branch: 1 branch
BranchTrueFalseTotal
262144 <= $self->kdbx->version ? : 2 7 9
371 KDBX_VERSION_4_0 <= $self->kdbx->version ? (
372 CustomData => \&_write_xml_custom_data,
373 ) : (),
374 );
375  
376 9 ▶ if (!$in_history) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_history) 6 3 9
377 6 ▶ if (my @history = @{$entry->history}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@history) = @{$entry->history;}) 2 4 6
378 2 my $history_node = $node->addNewChild(undef, 'History');
379 2 for my $historical (@history) {
380 3 my $historical_node = $history_node->addNewChild(undef, 'Entry');
381 3 $self->_write_xml_entry($historical_node, $historical->_committed, 1);
382 }
383 }
384 }
385 }
386  
387 sub _write_xml_entry_auto_type {
388 9 ▶ my $self = shift;
Subroutine _write_xml_entry_auto_type: called CC=3 SCAR=11.0
389 9 my $node = shift;
390 9 my $autotype = shift;
391  
392 9 $self->_write_xml_from_pairs($node, $autotype,
393 Enabled => 'bool',
394 DataTransferObfuscation => 'number',
395 DefaultSequence => 'text',
396 );
397  
398 9 ▶ for my $association (@{$autotype->{associations} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$autotype->{"associations"} or [] 0 9 9
399 3 my $association_node = $node->addNewChild(undef, 'Association');
400 3 $self->_write_xml_from_pairs($association_node, $association,
401 Window => 'text',
402 KeystrokeSequence => 'text',
403 );
404 }
405 }
406  
407 sub _write_xml_times {
408 32 ▶ my $self = shift;
Subroutine _write_xml_times: called CC=1 SCAR=0.0
409 32 my $node = shift;
410 32 my $times = shift;
411  
412 32 $self->_write_xml_from_pairs($node, $times,
413 LastModificationTime => 'datetime',
414 CreationTime => 'datetime',
415 LastAccessTime => 'datetime',
416 ExpiryTime => 'datetime',
417 Expires => 'bool',
418 UsageCount => 'number',
419 LocationChanged => 'datetime',
420 );
421 }
422  
423 sub _write_xml_entry_string {
424 45 ▶ my $self = shift;
Subroutine _write_xml_entry_string: called CC=7 SCAR=19.5
425 45 my $node = shift;
426 45 my $string = shift;
427  
428 45 my @cleanup;
429  
430 45 my $kdbx = $self->kdbx;
431 45 my $key = $string->{key};
432  
433 45 $node->addNewChild(undef, 'Key')->appendText(_encode_text($key));
434 45 my $value_node = $node->addNewChild(undef, 'Value');
435  
436 45 ▶ my $value = $string->{value} || '';
Condition100%2/2
$string->{'value'} || ''
l!l
2421
MC/DC100%1/1
$string->{'value'} || ''
$string->{'value'}
Truth table100%2/2
A
A= $string->{'value'}
Aresult
11
00
437  
438 45 my $memory_protection = $kdbx->meta->{memory_protection};
439 45 my $memprot_key = 'protect_' . snakify($key);
440 45 ▶ my $protect = $string->{protect} || $memory_protection->{$memprot_key};
Condition100%3/3
$string->{'protect'} || $memory_protection->{$memprot_key}
l!l&&r!l&&!r
10233
MC/DC100%2/2
$string->{'protect'} || $memory_protection->{$memprot_key}
$string->{'protect'} $memory_protection->{$memprot_key}
Truth table100%3/3
A || B
A= $string->{'protect'}B= $memory_protection->{$memprot_key}
ABresult
1X1
011
000
441  
442 45 ▶ if ($protect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protect) { } 12 33 45
443 12 ▶ if ($self->allow_protection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_protection) { } 12 0 12
444 12 my $encoded;
445 12 ▶ if (utf8::is_utf8($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($value)) 6 6 12
446 6 $encoded = encode('UTF-8', $value);
447 6 push @cleanup, erase_scoped $encoded;
448 6 $value = $encoded;
449 }
450  
451 12 $value_node->setAttribute('Protected', _encode_bool(true));
452 12 $value = _encode_binary($self->_random_stream->crypt(\$value));
453 }
454 else {
455 0 $value_node->setAttribute('ProtectInMemory', _encode_bool(true));
456 0 $value = _encode_text($value);
457 }
458 }
459 else {
460 33 $value = _encode_text($value);
461 }
462  
463 45 ▶ $value_node->appendText($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 45 0 45
464 }
465  
466 sub _write_xml_deleted_objects {
467 15 ▶ my $self = shift;
Subroutine _write_xml_deleted_objects: called CC=3 SCAR=13.5
468 15 my $node = shift;
469  
470 15 my $objects = $self->kdbx->deleted_objects;
471  
472 15 ▶ for my $uuid (sort keys %{$objects || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$objects or {} 0 15 15
473 0 my $object = $objects->{$uuid};
474 0 local $object->{uuid} = $uuid;
475 0 my $object_node = $node->addNewChild(undef, 'DeletedObject');
476 0 $self->_write_xml_from_pairs($object_node, $object,
477 UUID => 'uuid',
478 DeletionTime => 'datetime',
479 );
480 }
481 }
482  
483 ##############################################################################
484  
485 sub _write_xml_from_pairs {
486 134 ▶ my $self = shift;
Subroutine _write_xml_from_pairs: called CC=6 SCAR=17.9
487 134 my $node = shift;
488 134 my $hash = shift;
489 134 my @spec = @_;
490  
491 134 ▶ while (@spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spec) 1048 134 1182
492 1048 my ($name, $type) = splice @spec, 0, 2;
493 1048 my $key = snakify($name);
494  
495 1048 ▶ if (ref $type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'CODE') { } 105 943 1048
496 105 my $child_node = $node->addNewChild(undef, $name);
497 105 $self->$type($child_node, $hash->{$key});
498 }
499 else {
500 943 ▶ next if !exists $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 12 931 943
501 931 my $child_node = $node->addNewChild(undef, $name);
502 931 ▶ $type = 'datetime_binary' if $type eq 'datetime' && $self->compress_datetimes;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "datetime" and $self->compress_datetimes 104 827 931
Condition100%3/3
$type eq "datetime" and $self->compress_datetimes
!ll&&!rl&&r
675152104
MC/DC100%2/2
$type eq "datetime" and $self->compress_datetimes
$type eq "datetime" $self->compress_datetimes
Truth table100%3/3
A and B
A= $type eq "datetime"B= $self->compress_datetimes
ABresult
0X0
100
111
503 931 $child_node->appendText(_encode_primitive($hash->{$key}, $type));
504 }
505 }
506 }
507  
508 ##############################################################################
509  
510 931 ▶sub _encode_primitive { goto &{__PACKAGE__."::_encode_$_[1]"} }
Subroutine _encode_primitive: called CC=1 SCAR=0.0
511  
512 sub _encode_binary {
513 137 ▶ return '' if !defined $_[0] || (ref $_[0] && !defined $$_[0]);
Subroutine _encode_binary: called CC=5 SCAR=17.8
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] and not defined $_->[0] 16 121 137
Condition50%3/6
not defined $_[0] or ref $_[0] and not defined $_->[0]
l!l&&r!l&&!r
160121
ref $_[0] and not defined $_->[0]
!ll&&!rl&&r
12100
MC/DC0%0/3
not defined $_[0] or ref $_[0] and not defined $_->[0]
not defined $_[0] ref $_[0] not defined $_->[0]
Truth table25%1/4
not(A) or B and not(C)
A= not defined $_[0]B= ref $_[0]C= not defined $_->[0]
ABCresult
1XX1
0111
00X0
0100
514 121 ▶ return encode_b64(ref $_[0] ? $$_[0] : $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 121 121
515 }
516  
517 sub _encode_bool {
518 166 ▶ local $_ = shift;
Subroutine _encode_bool: called CC=2 SCAR=6.9
519 166 ▶ return $_ ? 'True' : 'False';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 73 93 166
520 }
521  
522 sub _encode_datetime {
523 152 ▶ local $_ = shift;
Subroutine _encode_datetime: called CC=1 SCAR=0.0
524 152 return $_->strftime('%Y-%m-%dT%H:%M:%SZ');
525 }
526  
527 sub _encode_datetime_binary {
528 104 ▶ local $_ = shift;
Subroutine _encode_datetime_binary: called CC=1 SCAR=0.0
529 104 my $seconds_since_ad1 = $_ + TIME_SECONDS_AD1_TO_UNIX_EPOCH;
530 104 my $buf = pack_Ql($seconds_since_ad1->epoch);
531 104 return eval { encode_b64($buf) };
532 }
533  
534 sub _encode_tristate {
535 46 ▶ local $_ = shift // return 'null';
Subroutine _encode_tristate: called CC=3 SCAR=17.3
Condition50%1/2
shift() // (return "null")
l!l
046
MC/DC0%0/1
shift() // (return "null")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
536 0 ▶ return $_ ? 'True' : 'False';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
537 }
538  
539 sub _encode_number {
540 144 ▶ local $_ = shift // return;
Subroutine _encode_number: called CC=4 SCAR=14.9
Condition50%1/2
shift() // (return)
l!l
1440
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
541 144 ▶ looks_like_number($_) || isdual($_) or throw 'Expected number', text => $_;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) or &isdual($_) or throw("Expected number", "text", $_) 0 144 144
Condition66%2/3
&looks_like_number($_) or &isdual($_)
l!l&&r!l&&!r
122220
MC/DC0%0/2
&looks_like_number($_) or &isdual($_)
&looks_like_number($_) &isdual($_)
Truth table66%2/3
A or B
A= &looks_like_number($_)B= &isdual($_)
ABresult
1X1
011
000
542 144 return _encode_text($_+0);
543 }
544  
545 sub _encode_text {
546 438 ▶ return '' if !defined $_[0];
Subroutine _encode_text: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 438 438
547 438 return $_[0];
548 }
549  
550 115 ▶sub _encode_uuid { _encode_binary(@_) }
Subroutine _encode_uuid: called CC=1 SCAR=0.0
551  
552 6 1;
553  
554 __END__
« KDBX.pm ↑ Summary KDBX/Safe.pm »