X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FUtil.pm;h=a09d2863d9177d69f902140f2159c0083bf50bc0;hb=331a54019664704eb4a10186cb4abd7a2a722f30;hp=2d830742e949e3730a452fc25ef6713f7d2a3d03;hpb=f63182fc62b25269b1c38588dca2b3535ed1a1a2;p=chaz%2Fp5-File-KDBX diff --git a/lib/File/KDBX/Util.pm b/lib/File/KDBX/Util.pm index 2d83074..a09d286 100644 --- a/lib/File/KDBX/Util.pm +++ b/lib/File/KDBX/Util.pm @@ -7,36 +7,50 @@ use strict; use Crypt::PRNG qw(random_bytes random_string); use Encode qw(decode encode); use Exporter qw(import); +use File::KDBX::Constants qw(:bool); use File::KDBX::Error; use List::Util 1.33 qw(any all); use Module::Load; -use Ref::Util qw(is_arrayref is_coderef is_hashref is_ref is_refref is_scalarref is_ref); -use Scalar::Util qw(blessed isdual looks_like_number readonly refaddr); +use Ref::Util qw(is_arrayref is_coderef is_hashref is_ref is_refref is_scalarref); +use Scalar::Util qw(blessed looks_like_number readonly); +use Time::Piece; +use boolean; use namespace::clean -except => 'import'; our $VERSION = '999.999'; # VERSION our %EXPORT_TAGS = ( - assert => [qw(assert_64bit)], + assert => [qw(DEBUG assert assert_64bit)], + class => [qw(extends has list_attributes)], clone => [qw(clone clone_nomagic)], + coercion => [qw(to_bool to_number to_string to_time to_tristate to_uuid)], crypt => [qw(pad_pkcs7)], - debug => [qw(dumper)], + debug => [qw(DEBUG dumper)], fork => [qw(can_fork)], function => [qw(memoize recurse_limit)], empty => [qw(empty nonempty)], erase => [qw(erase erase_scoped)], gzip => [qw(gzip gunzip)], - io => [qw(read_all)], + io => [qw(is_readable is_writable read_all)], load => [qw(load_optional load_xs try_load_optional)], search => [qw(query search simple_expression_query)], text => [qw(snakify trim)], - uuid => [qw(format_uuid generate_uuid is_uuid uuid)], + uuid => [qw(format_uuid generate_uuid is_uuid uuid UUID_NULL)], uri => [qw(split_url uri_escape_utf8 uri_unescape_utf8)], ); $EXPORT_TAGS{all} = [map { @$_ } values %EXPORT_TAGS]; our @EXPORT_OK = @{$EXPORT_TAGS{all}}; +BEGIN { + my $debug = $ENV{DEBUG}; + $debug = looks_like_number($debug) ? (0 + $debug) : ($debug ? 1 : 0); + *DEBUG = $debug == 1 ? sub() { 1 } : + $debug == 2 ? sub() { 2 } : + $debug == 3 ? sub() { 3 } : + $debug == 4 ? sub() { 4 } : sub() { 0 }; +} + my %OPS = ( 'eq' => 2, # binary 'ne' => 2, @@ -80,6 +94,63 @@ my %OP_NEG = ( '=~' => '!~', '!~' => '=~', ); +my %ATTRIBUTES; + +=func load_xs + + $bool = load_xs(); + $bool = load_xs($version); + +Attempt to load L. Return truthy if C is loaded. If C<$version> is given, it will check +that at least the given version is loaded. + +=cut + +my $XS_LOADED; +sub load_xs { + my $version = shift; + + goto IS_LOADED if defined $XS_LOADED; + + if ($ENV{PERL_ONLY} || (exists $ENV{PERL_FILE_KDBX_XS} && !$ENV{PERL_FILE_KDBX_XS})) { + return $XS_LOADED = FALSE; + } + + $XS_LOADED = !!eval { require File::KDBX::XS; 1 }; + + IS_LOADED: + { + local $@; + return $XS_LOADED if !$version; + return !!eval { File::KDBX::XS->VERSION($version); 1 }; + } +} + +=func assert + + assert { ... }; + +Write an executable comment. Only executed if C is set in the environment. + +=cut + +sub assert(&) { ## no critic (ProhibitSubroutinePrototypes) + return if !DEBUG; + my $code = shift; + return if $code->(); + + (undef, my $file, my $line) = caller; + $file =~ s!([^/\\]+)$!$1!; + my $assertion = ''; + if (try_load_optional('B::Deparse')) { + my $deparse = B::Deparse->new(qw{-P -x9}); + $assertion = $deparse->coderef2text($code); + $assertion =~ s/^\{(?:\s*(?:package[^;]+|use[^;]+);)*\s*(.*?);\s*\}$/$1/s; + $assertion =~ s/\s+/ /gs; + $assertion = ": $assertion"; + } + die "$0: $file:$line: Assertion failed$assertion\n"; +} =func assert_64bit @@ -119,13 +190,11 @@ sub can_fork { return 1; } -=func clone_nomagic - - $clone = clone_nomagic($thing); +=func clone -Clone deeply without keeping [most of] the magic. + $clone = clone($thing); -B At the moment the implementation is naïve and won't respond well to nontrivial data. +Clone deeply. This is an unadorned alias to L C. =cut @@ -134,6 +203,17 @@ sub clone { goto &Storable::dclone; } +=func clone_nomagic + + $clone = clone_nomagic($thing); + +Clone deeply without keeping [most of] the magic. + +B At the moment the implementation is naïve and won't respond well to nontrivial data or recursive +structures. + +=cut + sub clone_nomagic { my $thing = shift; if (is_arrayref($thing)) { @@ -153,7 +233,8 @@ sub clone_nomagic { =func dumper - $str = dumper $struct; + $str = dumper $thing; + dumper $thing; # in void context, prints to STDERR Like L but slightly terser in some cases relevent to L. @@ -178,7 +259,8 @@ sub dumper { # boolean $str =~ s/bless\( do\{\\\(my \$o = ([01])\)\}, 'boolean' \)/boolean($1)/gs; # Time::Piece - $str =~ s/bless\([^\)]+?(\d+)'?,\s+\d+,?\s+\], 'Time::Piece' \)/Time::Piece->new($1)/gs; + $str =~ s/bless\([^\)]+?(\d+)'?,\s+\d+,?\s+\], 'Time::Piece' \),/ + "scalar gmtime($1), # " . scalar gmtime($1)->datetime/ges; print STDERR $str if !defined wantarray; push @dumps, $str; @@ -232,7 +314,17 @@ Overwrite the memory used by one or more string. =cut -# use File::KDBX::XS; +BEGIN { + if (load_xs) { + *_CowREFCNT = \&File::KDBX::XS::CowREFCNT; + } + elsif (eval { require B::COW; 1 }) { + *_CowREFCNT = \&B::COW::cowrefcnt; + } + else { + *_CowREFCNT = sub { undef }; + } +} sub erase { # Only bother zeroing out memory if we have the last SvPV COW reference, otherwise we'll end up just @@ -241,10 +333,8 @@ sub erase { for (@_) { if (!is_ref($_)) { next if !defined $_ || readonly $_; - if (USE_COWREFCNT()) { - my $cowrefcnt = B::COW::cowrefcnt($_); - goto FREE_NONREF if defined $cowrefcnt && 1 < $cowrefcnt; - } + my $cowrefcnt = _CowREFCNT($_); + goto FREE_NONREF if defined $cowrefcnt && 1 < $cowrefcnt; # if (__PACKAGE__->can('erase_xs')) { # erase_xs($_); # } @@ -258,10 +348,8 @@ sub erase { } elsif (is_scalarref($_)) { next if !defined $$_ || readonly $$_; - if (USE_COWREFCNT()) { - my $cowrefcnt = B::COW::cowrefcnt($$_); - goto FREE_REF if defined $cowrefcnt && 1 < $cowrefcnt; - } + my $cowrefcnt = _CowREFCNT($$_); + goto FREE_REF if defined $cowrefcnt && 1 < $cowrefcnt; # if (__PACKAGE__->can('erase_xs')) { # erase_xs($$_); # } @@ -301,6 +389,7 @@ See L. =cut sub erase_scoped { + throw 'Programmer error: Cannot call erase_scoped in void context' if !defined wantarray; my @args; for (@_) { !is_ref($_) || is_arrayref($_) || is_hashref($_) || is_scalarref($_) @@ -311,6 +400,79 @@ sub erase_scoped { return Scope::Guard->new(sub { erase(@args) }); } +=func extends + + extends $class; + +Set up the current module to inheret from another module. + +=cut + +sub extends { + my $parent = shift; + my $caller = caller; + # load $parent; + eval qq[require $parent]; + no strict 'refs'; ## no critic (ProhibitNoStrict) + @{"${caller}::ISA"} = $parent; +} + +=func has + + has $name => %options; + +Create an attribute getter/setter. Possible options: + +=for :list +* C - Either "rw" (default) or "ro" +* C - Default value +* C - Coercive function + +=cut + +sub has { + my $name = shift; + my %args = @_ % 2 == 1 ? (default => shift, @_) : @_; + + my ($package, $file, $line) = caller; + + my $d = $args{default}; + my $default = is_arrayref($d) ? sub { [@$d] } : is_hashref($d) ? sub { +{%$d} } : $d; + my $coerce = $args{coerce}; + my $is = $args{is} || 'rw'; + + my $store = $args{store}; + ($store, $name) = split(/\./, $name, 2) if $name =~ /\./; + push @{$ATTRIBUTES{$package} //= []}, $name; + + my $store_code = ''; + $store_code = qq{->$store} if $store; + my $member = qq{\$_[0]$store_code\->{'$name'}}; + + my $default_code = is_coderef $default ? q{scalar $default->($_[0])} + : defined $default ? q{$default} + : q{undef}; + my $get = qq{$member //= $default_code;}; + + my $set = ''; + if ($is eq 'rw') { + $set = is_coderef $coerce ? qq{$member = scalar \$coerce->(\@_[1..\$#_]) if \$#_;} + : defined $coerce ? qq{$member = do { local @_ = (\@_[1..\$#_]); $coerce } if \$#_;} + : qq{$member = \$_[1] if \$#_;}; + } + + $line -= 4; + my $code = <-style mode is readable, writable or both. + +=cut + +sub is_readable { $_[0] !~ /^[aw]b?$/ } +sub is_writable { $_[0] !~ /^rb?$/ } + =func is_uuid $bool = is_uuid($thing); @@ -424,6 +600,19 @@ Check if a thing is a UUID (i.e. scalar string of length 16). sub is_uuid { defined $_[0] && !is_ref($_[0]) && length($_[0]) == 16 } +=func list_attributes + + @attributes = list_attributes($package); + +Get a list of attributes for a class. + +=cut + +sub list_attributes { + my $package = shift; + return @{$ATTRIBUTES{$package} // []}; +} + =func load_optional $package = load_optional($package); @@ -436,41 +625,14 @@ sub load_optional { for my $module (@_) { eval { load $module }; if (my $err = $@) { - warn $err if $ENV{DEBUG}; - throw "Missing dependency: Please install $module to use this feature.\n", module => $module; + throw "Missing dependency: Please install $module to use this feature.\n", + module => $module, + error => $err; } } return wantarray ? @_ : $_[0]; } -=func load_xs - - $bool = load_xs(); - $bool = load_xs($version); - -Attempt to load L. Return truthy if C is loaded. If C<$version> is given, it will check -that at least the given version is loaded. - -=cut - -sub load_xs { - my $version = shift; - - require File::KDBX; - - my $has_xs = File::KDBX->can('XS_LOADED'); - return $has_xs->() && ($version ? eval { File::KDBX::XS->VERSION($version); 1 } : 1) if $has_xs; - - my $try_xs = 1; - $try_xs = 0 if $ENV{PERL_ONLY} || (exists $ENV{PERL_FILE_KDBX_XS} && !$ENV{PERL_FILE_KDBX_XS}); - - my $use_xs = 0; - $use_xs = try_load_optional('File::KDBX::XS') if $try_xs; - - *File::KDBX::XS_LOADED = *File::KDBX::XS_LOADED = $use_xs ? sub() { 1 } : sub() { 0 }; - return $version ? eval { File::KDBX::XS->VERSION($version); 1 } : 1; -} - =func memoize \&memoized_code = memoize(\&code, ...); @@ -585,7 +747,6 @@ This is the search engine described with many examples at L. sub search { my $list = shift; my $query = shift; - # my %args = @_; if (is_coderef($query) && !@_) { # already a query @@ -597,12 +758,9 @@ sub search { $query = query($query, @_); } - # my $limit = $args{limit}; - my @match; for my $item (@$list) { push @match, $item if $query->($item); - # last if defined $limit && $limit <= @match; } return \@match; } @@ -610,6 +768,7 @@ sub search { =func simple_expression_query $query = simple_expression_query($expression, @fields); + $query = simple_expression_query($expression, $operator, @fields); Generate a query, like L, to be used with L but built from a "simple expression" as L. @@ -711,6 +870,39 @@ sub split_url { return ($scheme, $auth, $host, $port, $path, $query, $hash, $username, $password); } +=func to_bool + +=func to_number + +=func to_string + +=func to_time + +=func to_tristate + +=func to_uuid + +Various typecasting / coercive functions. + +=cut + +sub to_bool { $_[0] // return; boolean($_[0]) } +sub to_number { $_[0] // return; 0+$_[0] } +sub to_string { $_[0] // return; "$_[0]" } +sub to_time { + $_[0] // return; + return gmtime($_[0]) if looks_like_number($_[0]); + return Time::Piece->strptime($_[0], '%Y-%m-%d %H:%M:%S') if !blessed $_[0]; + return $_[0]; +} +sub to_tristate { $_[0] // return; boolean($_[0]) } +sub to_uuid { + my $str = to_string(@_) // return; + return sprintf('%016s', $str) if length($str) < 16; + return substr($str, 0, 16) if 16 < length($str); + return $str; +} + =func trim $string = trim($string); @@ -738,7 +930,7 @@ sub try_load_optional { for my $module (@_) { eval { load $module }; if (my $err = $@) { - warn $err if $ENV{DEBUG}; + warn $err if 3 <= DEBUG; return; } } @@ -762,6 +954,14 @@ sub uri_escape_utf8 { return $_; } +=func uri_unescape_utf8 + + $string = uri_unescape_utf8($string); + +Inverse of L. + +=cut + sub uri_unescape_utf8 { local $_ = shift // return; s/\%([A-Fa-f0-9]{2})/chr(hex($1))/; @@ -787,10 +987,13 @@ sub uuid { } -BEGIN { - my $use_cowrefcnt = eval { require B::COW; 1 }; - *USE_COWREFCNT = $use_cowrefcnt ? sub() { 1 } : sub() { 0 }; -} +=func UUID_NULL + +Get the null UUID (i.e. string of 16 null bytes). + +=cut + +sub UUID_NULL() { "\0" x 16 } ### --------------------------------------------------------------------------