]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Loader.pm
Don't open already-open files on Windows
[chaz/p5-File-KDBX] / lib / File / KDBX / Loader.pm
index 844f038098e8426518b1f77f5d2dd42fef4c2164..3a3c184f95a7e6fa661c4ce11271cc819a472e0c 100644 (file)
@@ -6,7 +6,7 @@ use strict;
 
 use File::KDBX::Constants qw(:magic :header :version);
 use File::KDBX::Error;
-use File::KDBX::Util qw(:io);
+use File::KDBX::Util qw(:class :io);
 use File::KDBX;
 use IO::Handle;
 use Module::Load ();
@@ -98,14 +98,18 @@ sub reset {
 
 =method load
 
+    $kdbx = File::KDBX::Loader->load(\$string, %options);
     $kdbx = File::KDBX::Loader->load(\$string, $key);
+    $kdbx = File::KDBX::Loader->load(*IO, %options);
     $kdbx = File::KDBX::Loader->load(*IO, $key);
+    $kdbx = File::KDBX::Loader->load($filepath, %options);
     $kdbx = File::KDBX::Loader->load($filepath, $key);
-    $kdbx = $loader->load(...); # also instance method
 
-Load a KDBX file.
+Load a KDBX file. This works as an instance or a class method. The C<$key> is either
+a L<File::KDBX::Key> or a primitive castable to a Key object. Available options:
 
-The C<$key> is either a L<File::KDBX::Key> or a primitive that can be converted to a Key object.
+=for :list
+* C<key> - Alternative way to specify C<$key>
 
 =cut
 
@@ -120,11 +124,15 @@ sub load {
 
 =method load_string
 
+    $kdbx = File::KDBX::Loader->load_string($string, %options);
     $kdbx = File::KDBX::Loader->load_string($string, $key);
+    $kdbx = File::KDBX::Loader->load_string(\$string, %options);
     $kdbx = File::KDBX::Loader->load_string(\$string, $key);
-    $kdbx = $loader->load_string(...); # also instance method
 
-Load a KDBX file from a string / memory buffer.
+Load a KDBX file from a string / memory buffer. This works as an instance or class method. Available options:
+
+=for :list
+* C<key> - Alternative way to specify C<$key>
 
 =cut
 
@@ -147,10 +155,13 @@ sub load_string {
 
 =method load_file
 
+    $kdbx = File::KDBX::Loader->load_file($filepath, %options);
     $kdbx = File::KDBX::Loader->load_file($filepath, $key);
-    $kdbx = $loader->load_file(...); # also instance method
 
-Read a KDBX file from a filesystem.
+Read a KDBX file from a filesystem. This works as an instance or class method. Available options:
+
+=for :list
+* C<key> - Alternative way to specify C<$key>
 
 =cut
 
@@ -171,11 +182,16 @@ sub load_file {
 
 =method load_handle
 
+    $kdbx = File::KDBX::Loader->load_handle($fh, %options);
     $kdbx = File::KDBX::Loader->load_handle($fh, $key);
+    $kdbx = File::KDBX::Loader->load_handle(*IO, %options);
     $kdbx = File::KDBX::Loader->load_handle(*IO, $key);
-    $kdbx->load_handle(...); # also instance method
 
-Read a KDBX file from an input stream / file handle.
+Read a KDBX file from an input stream / file handle. This works as an instance or class method. Available
+options:
+
+=for :list
+* C<key> - Alternative way to specify C<$key>
 
 =cut
 
@@ -212,25 +228,30 @@ sub kdbx {
 
 =attr format
 
-TODO
-
-=cut
-
-sub format { $_[0]->{format} }
-sub inner_format { $_[0]->{inner_format} // 'XML' }
+Get the file format used for reading the database. Normally the format is auto-detected from the data stream.
+This auto-detection works well, so there's not really a good reason to explicitly specify the format.
+Possible formats:
 
-=attr min_version
+=for :list
+* C<V3>
+* C<V4>
+* C<KDB>
+* C<XML>
+* C<Raw>
 
-    $min_version = File::KDBX::Loader->min_version;
+=attr inner_format
 
-Get the minimum KDBX file version supported, which is 3.0 or C<0x00030000> as
-it is encoded.
+Get the format of the data inside the KDBX envelope. This only applies to C<V3> and C<V4> formats. Possible
+formats:
 
-To read older KDBX files unsupported by this module, try L<File::KeePass>.
+=for :list
+* C<XML> - Read the database groups and entries as XML (default)
+* C<Raw> - Read and store the result in L<File::KDBX/raw> without parsing
 
 =cut
 
-sub min_version { KDBX_VERSION_OLDEST }
+has format          => undef, is => 'ro';
+has inner_format    => 'XML', is => 'ro';
 
 =method read_magic_numbers
 
@@ -303,7 +324,17 @@ sub _read {
 
     my $headers = $self->_read_headers($fh);
 
-    $self->_read_body($fh, $key, "$magic$headers");
+    eval {
+        $self->_read_body($fh, $key, "$magic$headers");
+    };
+    if (my $err = $@) {
+        throw "Failed to load KDBX file: $err",
+            error               => $err,
+            compression_error   => $IO::Uncompress::Gunzip::GunzipError,
+            crypt_error         => $File::KDBX::IO::Crypt::ERROR,
+            hash_error          => $File::KDBX::IO::HashBLock::ERROR,
+            hmac_error          => $File::KDBX::IO::HmacBLock::ERROR;
+    }
 }
 
 sub _read_headers {
@@ -336,3 +367,9 @@ sub _read_inner_body {
 }
 
 1;
+__END__
+
+=head1 DESCRIPTION
+
+
+=cut
This page took 0.030797 seconds and 4 git commands to generate.