]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Loader/V4.pm
Add support for 32-bit perls
[chaz/p5-File-KDBX] / lib / File / KDBX / Loader / V4.pm
index fa8d21d867e220a321efe81c06bff1b30d1b4278..602b058eddd901bfddf26f13b08a3776b2522197 100644 (file)
@@ -22,13 +22,13 @@ use Crypt::Mac::HMAC qw(hmac);
 use Encode qw(decode);
 use File::KDBX::Constants qw(:header :inner_header :variant_map :compression);
 use File::KDBX::Error;
-use File::KDBX::Util qw(:io assert_64bit erase_scoped);
+use File::KDBX::Util qw(:class :int :io :load erase_scoped);
 use File::KDBX::IO::Crypt;
 use File::KDBX::IO::HmacBlock;
 use boolean;
 use namespace::clean;
 
-use parent 'File::KDBX::Loader';
+extends 'File::KDBX::Loader';
 
 our $VERSION = '999.999'; # VERSION
 
@@ -45,7 +45,7 @@ sub _read_header {
         $buf .= $val;
     }
 
-    $type = KDBX_HEADER($type);
+    $type = to_header_constant($type);
     if ($type == HEADER_END) {
         # done
     }
@@ -116,8 +116,7 @@ sub _read_variant_dictionary {
             ($val) = unpack('L<', $val);
         }
         elsif ($type == VMAP_TYPE_UINT64) {
-            assert_64bit;
-            ($val) = unpack('Q<', $val);
+            ($val) = unpack_Ql($val);
         }
         elsif ($type == VMAP_TYPE_BOOL) {
             ($val) = unpack('C', $val);
@@ -127,8 +126,7 @@ sub _read_variant_dictionary {
             ($val) = unpack('l<', $val);
         }
         elsif ($type == VMAP_TYPE_INT64) {
-            assert_64bit;
-            ($val) = unpack('q<', $val);
+            ($val) = unpack_ql($val);
         }
         elsif ($type == VMAP_TYPE_STRING) {
             $val = decode('UTF-8', $val);
@@ -198,7 +196,7 @@ sub _read_body {
 
     my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS};
     if ($compress == COMPRESSION_GZIP) {
-        require IO::Uncompress::Gunzip;
+        load_optional('IO::Uncompress::Gunzip');
         $fh = IO::Uncompress::Gunzip->new($fh)
             or throw "Failed to initialize compression library: $IO::Uncompress::Gunzip::GunzipError",
                 error => $IO::Uncompress::Gunzip::GunzipError;
@@ -225,10 +223,7 @@ sub _read_inner_header {
     my $fh   = shift;
     my $kdbx = $self->kdbx;
 
-    read_all $fh, my $buf, 5 or throw 'Expected inner header type and size',
-        compression_error   => $IO::Uncompress::Gunzip::GunzipError,
-        crypt_error         => $File::KDBX::IO::Crypt::ERROR,
-        hmac_error          => $File::KDBX::IO::HmacBLock::ERROR;
+    read_all $fh, my $buf, 5 or throw 'Expected inner header type and size';
     my ($type, $size) = unpack('C L<', $buf);
 
     my $val;
@@ -236,23 +231,18 @@ sub _read_inner_header {
         read_all $fh, $val, $size or throw 'Expected inner header value', type => $type, size => $size;
     }
 
-    my $dualtype = KDBX_INNER_HEADER($type);
-
-    if (!defined $dualtype) {
-        alert "Ignoring unknown inner header type ($type)", type => $type, size => $size, value => $val;
-        return wantarray ? ($type => $val) : $type;
-    }
-    elsif ($dualtype == INNER_HEADER_END) {
+    $type = to_inner_header_constant($type) // $type;
+    if ($type == INNER_HEADER_END) {
         # nothing
     }
-    elsif ($dualtype == INNER_HEADER_INNER_RANDOM_STREAM_ID) {
+    elsif ($type == INNER_HEADER_INNER_RANDOM_STREAM_ID) {
         $val = unpack('L<', $val);
-        $kdbx->inner_headers->{$dualtype} = $val;
+        $kdbx->inner_headers->{$type} = $val;
     }
-    elsif ($dualtype == INNER_HEADER_INNER_RANDOM_STREAM_KEY) {
-        $kdbx->inner_headers->{$dualtype} = $val;
+    elsif ($type == INNER_HEADER_INNER_RANDOM_STREAM_KEY) {
+        $kdbx->inner_headers->{$type} = $val;
     }
-    elsif ($dualtype == INNER_HEADER_BINARY) {
+    elsif ($type == INNER_HEADER_BINARY) {
         my $msize = $size - 1;
         my ($flags, $data) = unpack("C a$msize", $val);
         my $id = scalar keys %{$kdbx->binaries};
@@ -261,8 +251,12 @@ sub _read_inner_header {
             $flags & INNER_HEADER_BINARY_FLAG_PROTECT ? (protect => true) : (),
         };
     }
+    else {
+        alert "Ignoring unknown inner header type ($type)", type => $type, size => $size, value => $val;
+        return wantarray ? ($type => $val) : $type;
+    }
 
-    return wantarray ? ($dualtype => $val) : $dualtype;
+    return wantarray ? ($type => $val) : $type;
 }
 
 1;
This page took 0.035842 seconds and 4 git commands to generate.