KDBX/Loader.pm

filter: Statementstmt 88.1% 134 / 152 Branchbran 48.6% 35 / 72 Conditioncond 34.2% 12 / 35 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 86.2% 25 / 29 Podpod 100% 11 / 11 totaltotal 67.7% 218 / 322 SCAR 47.4
CC
49
Cov
70%
load_handle
30.0
_read
10.6
load
9.2
CRAP 114.6
« KDBX/Object.pm ↑ Summary KDBX/Key/File.pm »
1 package File::KDBX::Loader;
2 # ABSTRACT: Load KDBX files
3  
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use File::KDBX::Constants qw(:magic :header :version);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use File::KDBX::Util qw(:class :io);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::KDBX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Ref::Util qw(is_ref is_scalarref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Scalar::Util qw(looks_like_number openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 our $VERSION = '0.904'; # VERSION
18  
19  
20 sub new {
21 30 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 30 my $self = bless {}, $class;
23 30 $self->init(@_);
24 }
25  
26  
27 sub init {
28 60 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
29 60 my %args = @_;
30  
31 60 @$self{keys %args} = values %args;
32  
33 60 return $self;
34 }
35  
36 sub _rebless {
37 51 ▶ my $self = shift;
Subroutine _rebless: called CC=7 SCAR=21.0
38 51 ▶ my $format = shift // $self->format;
Condition66%2/3
shift() // $self->format
l!l&&r!l&&!r
21030
MC/DC50%1/2
shift() // $self->format
shift() $self->format
Truth table66%2/3
A // B
A= shift()B= $self->format
ABresult
1X1
011
000
39  
40 51 my $sig2 = $self->kdbx->sig2;
41 51 my $version = $self->kdbx->version;
42  
43 51 my $subclass;
44  
45 51 ▶ if (defined $format) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $format) { } 21 30 51
elsif (defined $sig2 and $sig2 == 3041655653) { } 0 30 30
elsif (&looks_like_number($version)) { } 30 0 30
46 21 $subclass = $format;
47 }
48 ▶ elsif (defined $sig2 && $sig2 == KDBX_SIG2_1) {
Condition33%1/3
defined $sig2 and $sig2 == 3041655653
!ll&&!rl&&r
0300
MC/DC0%0/2
defined $sig2 and $sig2 == 3041655653
defined $sig2 $sig2 == 3041655653
Truth table33%1/3
A and B
A= defined $sig2B= $sig2 == 3041655653
ABresult
0X0
100
111
49 0 $subclass = 'KDB';
50 }
51 elsif (looks_like_number($version)) {
52 30 my $major = $version & KDBX_VERSION_MAJOR_MASK;
53 30 my %subclasses = (
54 KDBX_VERSION_2_0() => 'V3',
55 KDBX_VERSION_3_0() => 'V3',
56 KDBX_VERSION_4_0() => 'V4',
57 );
58 30 ▶ $subclass = $subclasses{$major}
Branch: 1 branch
BranchTrueFalseTotal
$subclass = $subclasses{$major} or throw(sprintf("Unsupported KDBX file version: %x", $version), "version", $version) 0 30 30
59 or throw sprintf('Unsupported KDBX file version: %x', $version), version => $version;
60 }
61 else {
62 0 throw sprintf('Unknown file version: %s', $version), version => $version;
63 }
64  
65 51 Module::Load::load "File::KDBX::Loader::$subclass";
66 51 bless $self, "File::KDBX::Loader::$subclass";
67 }
68  
69  
70 sub reset {
71 0 ▶ my $self = shift;
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
72 0 %$self = ();
73 0 return $self;
74 }
75  
76  
77 sub load {
78 9 ▶ my $self = shift;
Subroutine load: called CC=6 SCAR=22.1
Pod documented
79 9 my $src = shift;
80 9 ▶ return $self->load_handle($src, @_) if openhandle($src) || $src eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle($src) or $src eq "-" 0 9 9
Condition33%1/3
&openhandle($src) or $src eq "-"
l!l&&r!l&&!r
009
MC/DC0%0/2
&openhandle($src) or $src eq "-"
&openhandle($src) $src eq "-"
Truth table33%1/3
A or B
A= &openhandle($src)B= $src eq "-"
ABresult
1X1
011
000
81 9 ▶ return $self->load_string($src, @_) if is_scalarref($src);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_scalarref($src) 0 9 9
82 9 ▶ return $self->load_file($src, @_) if !is_ref($src) && defined $src;
Branch: 1 branch
BranchTrueFalseTotal
if not Ref::Util::XS::is_ref($src) and defined $src 9 0 9
Condition33%1/3
not Ref::Util::XS::is_ref($src) and defined $src
!ll&&!rl&&r
009
MC/DC0%0/2
not Ref::Util::XS::is_ref($src) and defined $src
not Ref::Util::XS::is_ref($src) defined $src
Truth table33%1/3
not(A) and B
A= not Ref::Util::XS::is_ref($src)B= defined $src
ABresult
0X0
100
111
83 0 throw 'Programmer error: Must pass a stringref, filepath or IO handle to read';
84 }
85  
86  
87 sub load_string {
88 21 ▶ my $self = shift;
Subroutine load_string: called CC=7 SCAR=20.6
Pod documented
89 21 ▶ my $str = shift or throw 'Expected string to load';
Branch: 1 branch
BranchTrueFalseTotal
my $str = shift() or throw("Expected string to load") 0 21 21
90 21 ▶ my %args = @_ % 2 == 0 ? @_ : (key => shift, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 21 21
91  
92 21 my $key = delete $args{key};
93 21 ▶ $args{kdbx} //= $self->kdbx;
Condition33%1/3
$args{"kdbx"} //= $self->kdbx
l!l&&r!l&&!r
0210
MC/DC0%0/2
$args{"kdbx"} //= $self->kdbx
$args{"kdbx"} $self->kdbx
Truth table33%1/3
A //= B
A= $args{"kdbx"}B= $self->kdbx
ABresult
1X1
011
000
94  
95 21 ▶ my $ref = is_scalarref($str) ? $str : \$str;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_scalarref($str) ? : 3 18 21
96  
97 21 ▶ open(my $fh, '<', $ref) or throw "Failed to open string buffer: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $ref or throw("Failed to open string buffer: $!") 0 21 21
98  
99 21 ▶ $self = $self->new if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 21 21
100 21 $self->init(%args, fh => $fh)->_read($fh, $key);
101 12 return $args{kdbx};
102 }
103  
104  
105 sub load_file {
106 9 ▶ my $self = shift;
Subroutine load_file: called CC=5 SCAR=17.1
Pod documented
107 9 my $filepath = shift;
108 9 ▶ my %args = @_ % 2 == 0 ? @_ : (key => shift, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 9 9
109  
110 9 my $key = delete $args{key};
111 9 ▶ $args{kdbx} //= $self->kdbx;
Condition33%1/3
$args{"kdbx"} //= $self->kdbx
l!l&&r!l&&!r
090
MC/DC0%0/2
$args{"kdbx"} //= $self->kdbx
$args{"kdbx"} $self->kdbx
Truth table33%1/3
A //= B
A= $args{"kdbx"}B= $self->kdbx
ABresult
1X1
011
000
112  
113 9 ▶ open(my $fh, '<:raw', $filepath) or throw 'Open file failed', filepath => $filepath;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $filepath or throw("Open file failed", "filepath", $filepath) 0 9 9
114  
115 9 ▶ $self = $self->new if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 9 9
116 9 $self->init(%args, fh => $fh, filepath => $filepath)->_read($fh, $key);
117 8 return $args{kdbx};
118 }
119  
120  
121 sub load_handle {
122 0 ▶ my $self = shift;
Subroutine load_handle: not called CC=5 SCAR=34.0
Pod documented
123 0 my $fh = shift;
124 0 ▶ my %args = @_ % 2 == 0 ? @_ : (key => shift, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 0 0
125  
126 0 ▶ $fh = *STDIN if $fh eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $fh eq "-" 0 0 0
127  
128 0 my $key = delete $args{key};
129 0 ▶ $args{kdbx} //= $self->kdbx;
Condition0%0/3
$args{"kdbx"} //= $self->kdbx
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"kdbx"} //= $self->kdbx
$args{"kdbx"} $self->kdbx
Truth table0%0/3
A //= B
A= $args{"kdbx"}B= $self->kdbx
ABresult
1X1
011
000
130  
131 0 ▶ $self = $self->new if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
132 0 $self->init(%args, fh => $fh)->_read($fh, $key);
133 0 return $args{kdbx};
134 }
135  
136  
137 sub kdbx {
138 346 ▶ my $self = shift;
Subroutine kdbx: called CC=4 SCAR=15.6
Pod documented
139 346 ▶ return File::KDBX->new if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 346 346
140 346 ▶ $self->{kdbx} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 346 346
141 346 ▶ $self->{kdbx} //= File::KDBX->new;
Condition33%1/3
$self->{"kdbx"} //= "File::KDBX"->new
l!l&&r!l&&!r
34600
MC/DC0%0/2
$self->{"kdbx"} //= "File::KDBX"->new
$self->{"kdbx"} "File::KDBX"->new
Truth table33%1/3
A //= B
A= $self->{"kdbx"}B= "File::KDBX"->new
ABresult
1X1
011
000
142 30 ▶}
Subroutine format: called CC=3 SCAR=12.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 30 30
143 21 ▶ 
Subroutine inner_format: called CC=3 SCAR=12.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 21 21
144 30 ▶ 
Condition50%1/2
$_[0]{"format"} //= undef
l!l
030
MC/DC0%0/1
$_[0]{"format"} //= undef
$_[0]{"format"}
Truth table50%1/2
A
A= $_[0]{"format"}
Aresult
11
00
145 21 ▶has format => undef, is => 'ro';
Condition33%1/3
$_[0]{"inner_format"} //= $default
l!l&&r!l&&!r
0210
MC/DC0%0/2
$_[0]{"inner_format"} //= $default
$_[0]{"inner_format"} $default
Truth table33%1/3
A //= B
A= $_[0]{"inner_format"}B= $default
ABresult
1X1
011
000
146 12 has inner_format => 'XML', is => 'ro';
147  
148  
149 sub read_magic_numbers {
150 30 ▶ my $self = shift;
Subroutine read_magic_numbers: called CC=6 SCAR=19.1
Pod documented
151 30 my $fh = shift;
152 30 ▶ my $kdbx = shift // $self->kdbx;
Condition33%1/3
shift() // $self->kdbx
l!l&&r!l&&!r
3000
MC/DC0%0/2
shift() // $self->kdbx
shift() $self->kdbx
Truth table33%1/3
A // B
A= shift()B= $self->kdbx
ABresult
1X1
011
000
153  
154 30 ▶ read_all $fh, my $magic, 12 or throw 'Failed to read file signature';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $magic, 12) or throw("Failed to read file signature") 0 30 30
155  
156 30 my ($sig1, $sig2, $version) = unpack('L<3', $magic);
157  
158 30 ▶ if ($kdbx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kdbx) 30 0 30
159 30 $kdbx->sig1($sig1);
160 30 $kdbx->sig2($sig2);
161 30 $kdbx->version($version);
162 30 ▶ $self->_rebless if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 30 0 30
163 }
164  
165 30 ▶ return wantarray ? ($sig1, $sig2, $version, $magic) : $magic;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 30 30
166 }
167  
168 0 ▶sub _fh { $_[0]->{fh} or throw 'IO handle not set' }
Subroutine _fh: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"fh"} or throw("IO handle not set") 0 0 0
169  
170 sub _read {
171 30 ▶ my $self = shift;
Subroutine _read: called CC=9 SCAR=23.6
172 30 my $fh = shift;
173 30 my $key = shift;
174  
175 30 my $kdbx = $self->kdbx;
176 30 ▶ $key //= $kdbx->key ? $kdbx->key->reload : undef;
Branch: 1 branch
BranchTrueFalseTotal
$kdbx->key ? : 0 0 0
Condition33%1/3
$key //= $kdbx->key ? $kdbx->key->reload : undef
l!l&&r!l&&!r
3000
MC/DC0%0/2
$key //= $kdbx->key ? $kdbx->key->reload : undef
$key $kdbx->key ? $kdbx->key->reload : undef
Truth table33%1/3
A //= B
A= $keyB= $kdbx->key ? $kdbx->key->reload : undef
ABresult
1X1
011
000
177 30 $kdbx->reset;
178  
179 30 ▶ read_all $fh, my $buf, 1 or throw 'Failed to read the first byte', type => 'parser';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $buf, 1) or throw("Failed to read the first byte", "type", "parser") 0 30 30
180 30 my $first = ord($buf);
181 30 $fh->ungetc($first);
182 30 ▶ if ($first != KDBX_SIG1_FIRST_BYTE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first != 3) 0 30 30
183 # not a KDBX file... try skipping the outer layer
184 0 return $self->_read_inner_body($fh);
185 }
186  
187 30 my $magic = $self->read_magic_numbers($fh, $kdbx);
188 30 ▶ $kdbx->sig1 == KDBX_SIG1 or throw 'Invalid file signature', type => 'parser', sig1 => $kdbx->sig1;
Branch: 1 branch
BranchTrueFalseTotal
$kdbx->sig1 == 2594363651 or throw("Invalid file signature", "type", "parser", "sig1", $kdbx->sig1) 0 30 30
189  
190 30 ▶ if (ref($self) =~ /::(?:KDB|V[34])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /::(?:KDB|V[34])$/) 30 0 30
191 30 ▶ defined $key or throw 'Must provide a master key', type => 'key.missing';
Branch: 1 branch
BranchTrueFalseTotal
defined $key or throw("Must provide a master key", "type", "key.missing") 0 30 30
192 }
193  
194 30 my $headers = $self->_read_headers($fh);
195  
196 30 eval {
197 30 $self->_read_body($fh, $key, "$magic$headers");
198 };
199 30 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 10 20 30
200 10 throw "Failed to load KDBX file: $err",
201 error => $err,
202 compression_error => $IO::Uncompress::Gunzip::GunzipError,
203 crypt_error => $File::KDBX::IO::Crypt::ERROR,
204 hash_error => $File::KDBX::IO::HashBLock::ERROR,
205 hmac_error => $File::KDBX::IO::HmacBLock::ERROR;
206 }
207 }
208  
209 sub _read_headers {
210 30 ▶ my $self = shift;
Subroutine _read_headers: called CC=3 SCAR=11.0
211 30 my $fh = shift;
212  
213 30 my $headers = $self->kdbx->headers;
214 30 my $all_raw = '';
215  
216 30 ▶ while (my ($type, $val, $raw) = $self->_read_header($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_read_header($fh)) 247 0 247
217 247 $all_raw .= $raw;
218 247 ▶ last if $type == HEADER_END;
Branch: 1 branch
BranchTrueFalseTotal
if $type == "end" 30 217 247
219 217 $headers->{$type} = $val;
220 }
221  
222 30 return $all_raw;
223 }
224  
225 0 ▶sub _read_body { die "Not implemented" }
Subroutine _read_body: not called CC=1 SCAR=6.9
226  
227 sub _read_inner_body {
228 21 ▶ my $self = shift;
Subroutine _read_inner_body: called CC=1 SCAR=0.0
229  
230 21 my $current_pkg = ref $self;
231 21 require Scope::Guard;
232 21 ▶ my $guard = Scope::Guard->new(sub { bless $self, $current_pkg });
Subroutine __ANON__: called CC=1 SCAR=0.0
233  
234 21 $self->_rebless($self->inner_format);
235 21 $self->_read_inner_body(@_);
236 }
237  
238 12 1;
239  
240 __END__
« KDBX/Object.pm ↑ Summary KDBX/Key/File.pm »