]> Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Util.pm
630b1818daa3ef607c53fabe1d40fd38bc8fbdfe
[chaz/p5-File-KDBX] / lib / File / KDBX / Util.pm
1 package File::KDBX::Util;
2 # ABSTRACT: Utility functions for working with KDBX files
3
4 use warnings;
5 use strict;
6
7 use Crypt::PRNG qw(random_bytes random_string);
8 use Encode qw(decode encode);
9 use Exporter qw(import);
10 use File::KDBX::Constants qw(:bool);
11 use File::KDBX::Error;
12 use List::Util 1.33 qw(any all);
13 use Module::Load;
14 use Ref::Util qw(is_arrayref is_coderef is_hashref is_ref is_refref is_scalarref);
15 use Scalar::Util qw(blessed readonly);
16 use namespace::clean -except => 'import';
17
18 our $VERSION = '999.999'; # VERSION
19
20 our %EXPORT_TAGS = (
21 assert => [qw(assert_64bit)],
22 clone => [qw(clone clone_nomagic)],
23 crypt => [qw(pad_pkcs7)],
24 debug => [qw(dumper)],
25 fork => [qw(can_fork)],
26 function => [qw(memoize recurse_limit)],
27 empty => [qw(empty nonempty)],
28 erase => [qw(erase erase_scoped)],
29 gzip => [qw(gzip gunzip)],
30 io => [qw(is_readable is_writable read_all)],
31 load => [qw(load_optional load_xs try_load_optional)],
32 search => [qw(query search simple_expression_query)],
33 text => [qw(snakify trim)],
34 uuid => [qw(format_uuid generate_uuid is_uuid uuid)],
35 uri => [qw(split_url uri_escape_utf8 uri_unescape_utf8)],
36 );
37
38 $EXPORT_TAGS{all} = [map { @$_ } values %EXPORT_TAGS];
39 our @EXPORT_OK = @{$EXPORT_TAGS{all}};
40
41 my %OPS = (
42 'eq' => 2, # binary
43 'ne' => 2,
44 'lt' => 2,
45 'gt' => 2,
46 'le' => 2,
47 'ge' => 2,
48 '==' => 2,
49 '!=' => 2,
50 '<' => 2,
51 '>' => 2,
52 '<=' => 2,
53 '>=' => 2,
54 '=~' => 2,
55 '!~' => 2,
56 '!' => 1, # unary
57 '!!' => 1,
58 '-not' => 1, # special
59 '-false' => 1,
60 '-true' => 1,
61 '-defined' => 1,
62 '-undef' => 1,
63 '-empty' => 1,
64 '-nonempty' => 1,
65 '-or' => -1,
66 '-and' => -1,
67 );
68 my %OP_NEG = (
69 'eq' => 'ne',
70 'ne' => 'eq',
71 'lt' => 'ge',
72 'gt' => 'le',
73 'le' => 'gt',
74 'ge' => 'lt',
75 '==' => '!=',
76 '!=' => '==',
77 '<' => '>=',
78 '>' => '<=',
79 '<=' => '>',
80 '>=' => '<',
81 '=~' => '!~',
82 '!~' => '=~',
83 );
84
85 =func load_xs
86
87 $bool = load_xs();
88 $bool = load_xs($version);
89
90 Attempt to load L<File::KDBX::XS>. Return truthy if C<XS> is loaded. If C<$version> is given, it will check
91 that at least the given version is loaded.
92
93 =cut
94
95 my $XS_LOADED;
96 sub load_xs {
97 my $version = shift;
98
99 goto IS_LOADED if defined $XS_LOADED;
100
101 if ($ENV{PERL_ONLY} || (exists $ENV{PERL_FILE_KDBX_XS} && !$ENV{PERL_FILE_KDBX_XS})) {
102 return $XS_LOADED = FALSE;
103 }
104
105 $XS_LOADED = !!eval { require File::KDBX::XS; 1 };
106
107 IS_LOADED:
108 {
109 local $@;
110 return $XS_LOADED if !$version;
111 return !!eval { File::KDBX::XS->VERSION($version); 1 };
112 }
113 }
114
115 =func assert_64bit
116
117 assert_64bit();
118
119 Throw if perl doesn't support 64-bit IVs.
120
121 =cut
122
123 sub assert_64bit() {
124 require Config;
125 $Config::Config{ivsize} < 8
126 and throw "64-bit perl is required to use this feature.\n", ivsize => $Config::Config{ivsize};
127 }
128
129 =func can_fork
130
131 $bool = can_fork;
132
133 Determine if perl can fork, with logic lifted from L<Test2::Util/CAN_FORK>.
134
135 =cut
136
137 sub can_fork {
138 require Config;
139 return 1 if $Config::Config{d_fork};
140 return 0 if $^O ne 'MSWin32' && $^O ne 'NetWare';
141 return 0 if !$Config::Config{useithreads};
142 return 0 if $Config::Config{ccflags} !~ /-DPERL_IMPLICIT_SYS/;
143 return 0 if $] < 5.008001;
144 if ($] == 5.010000 && $Config::Config{ccname} eq 'gcc' && $Config::Config{gccversion}) {
145 return 0 if $Config::Config{gccversion} !~ m/^(\d+)\.(\d+)/;
146 my @parts = split(/[\.\s]+/, $Config::Config{gccversion});
147 return 0 if $parts[0] > 4 || ($parts[0] == 4 && $parts[1] >= 8);
148 }
149 return 0 if $INC{'Devel/Cover.pm'};
150 return 1;
151 }
152
153 =func clone
154
155 $clone = clone($thing);
156
157 Clone deeply. This is an unadorned alias to L<Storable> C<dclone>.
158
159 =cut
160
161 sub clone {
162 require Storable;
163 goto &Storable::dclone;
164 }
165
166 =func clone_nomagic
167
168 $clone = clone_nomagic($thing);
169
170 Clone deeply without keeping [most of] the magic.
171
172 B<WARNING:> At the moment the implementation is naïve and won't respond well to nontrivial data or recursive
173 structures.
174
175 =cut
176
177 sub clone_nomagic {
178 my $thing = shift;
179 if (is_arrayref($thing)) {
180 my @arr = map { clone_nomagic($_) } @$thing;
181 return \@arr;
182 }
183 elsif (is_hashref($thing)) {
184 my %hash;
185 $hash{$_} = clone_nomagic($thing->{$_}) for keys %$thing;
186 return \%hash;
187 }
188 elsif (is_ref($thing)) {
189 return clone($thing);
190 }
191 return $thing;
192 }
193
194 =func dumper
195
196 $str = dumper $thing;
197 dumper $thing; # in void context, prints to STDERR
198
199 Like L<Data::Dumper> but slightly terser in some cases relevent to L<File::KDBX>.
200
201 =cut
202
203 sub dumper {
204 require Data::Dumper;
205 # avoid "once" warnings
206 local $Data::Dumper::Deepcopy = $Data::Dumper::Deepcopy = 1;
207 local $Data::Dumper::Deparse = $Data::Dumper::Deparse = 1;
208 local $Data::Dumper::Indent = 1;
209 local $Data::Dumper::Quotekeys = 0;
210 local $Data::Dumper::Sortkeys = 1;
211 local $Data::Dumper::Terse = 1;
212 local $Data::Dumper::Trailingcomma = 1;
213 local $Data::Dumper::Useqq = 1;
214
215 my @dumps;
216 for my $struct (@_) {
217 my $str = Data::Dumper::Dumper($struct);
218
219 # boolean
220 $str =~ s/bless\( do\{\\\(my \$o = ([01])\)\}, 'boolean' \)/boolean($1)/gs;
221 # Time::Piece
222 $str =~ s/bless\([^\)]+?(\d+)'?,\s+\d+,?\s+\], 'Time::Piece' \)/Time::Piece->new($1)/gs;
223
224 print STDERR $str if !defined wantarray;
225 push @dumps, $str;
226 return $str;
227 }
228 return join("\n", @dumps);
229 }
230
231 =func empty
232
233 =func nonempty
234
235 $bool = empty $thing;
236
237 $bool = nonempty $thing;
238
239 Test whether a thing is empty (or nonempty). An empty thing is one of these:
240
241 =for :list
242 * nonexistent
243 * C<undef>
244 * zero-length string
245 * zero-length array
246 * hash with zero keys
247 * reference to an empty thing (recursive)
248
249 Note in particular that zero C<0> is not considered empty because it is an actual value.
250
251 =cut
252
253 sub empty { _empty(@_) }
254 sub nonempty { !_empty(@_) }
255
256 sub _empty {
257 return 1 if @_ == 0;
258 local $_ = shift;
259 return !defined $_
260 || $_ eq ''
261 || (is_arrayref($_) && @$_ == 0)
262 || (is_hashref($_) && keys %$_ == 0)
263 || (is_scalarref($_) && (!defined $$_ || $$_ eq ''))
264 || (is_refref($_) && _empty($$_));
265 }
266
267 =func erase
268
269 erase($string, ...);
270 erase(\$string, ...);
271
272 Overwrite the memory used by one or more string.
273
274 =cut
275
276 BEGIN {
277 if (load_xs) {
278 *_CowREFCNT = \&File::KDBX::XS::CowREFCNT;
279 }
280 elsif (eval { require B::COW; 1 }) {
281 *_CowREFCNT = \&B::COW::cowrefcnt;
282 }
283 else {
284 *_CowREFCNT = sub { undef };
285 }
286 }
287
288 sub erase {
289 # Only bother zeroing out memory if we have the last SvPV COW reference, otherwise we'll end up just
290 # creating a copy and erasing the copy.
291 # TODO - Is this worth doing? Need some benchmarking.
292 for (@_) {
293 if (!is_ref($_)) {
294 next if !defined $_ || readonly $_;
295 my $cowrefcnt = _CowREFCNT($_);
296 goto FREE_NONREF if defined $cowrefcnt && 1 < $cowrefcnt;
297 # if (__PACKAGE__->can('erase_xs')) {
298 # erase_xs($_);
299 # }
300 # else {
301 substr($_, 0, length($_), "\0" x length($_));
302 # }
303 FREE_NONREF: {
304 no warnings 'uninitialized';
305 undef $_;
306 }
307 }
308 elsif (is_scalarref($_)) {
309 next if !defined $$_ || readonly $$_;
310 my $cowrefcnt = _CowREFCNT($$_);
311 goto FREE_REF if defined $cowrefcnt && 1 < $cowrefcnt;
312 # if (__PACKAGE__->can('erase_xs')) {
313 # erase_xs($$_);
314 # }
315 # else {
316 substr($$_, 0, length($$_), "\0" x length($$_));
317 # }
318 FREE_REF: {
319 no warnings 'uninitialized';
320 undef $$_;
321 }
322 }
323 elsif (is_arrayref($_)) {
324 erase(@$_);
325 @$_ = ();
326 }
327 elsif (is_hashref($_)) {
328 erase(values %$_);
329 %$_ = ();
330 }
331 else {
332 throw 'Cannot erase this type of scalar', type => ref $_, what => $_;
333 }
334 }
335 }
336
337 =func erase_scoped
338
339 $scope_guard = erase_scoped($string, ...);
340 $scope_guard = erase_scoped(\$string, ...);
341 undef $scope_guard; # erase happens here
342
343 Get a scope guard that will cause scalars to be erased later (i.e. when the scope ends). This is useful if you
344 want to make sure a string gets erased after you're done with it, even if the scope ends abnormally.
345
346 See L</erase>.
347
348 =cut
349
350 sub erase_scoped {
351 my @args;
352 for (@_) {
353 !is_ref($_) || is_arrayref($_) || is_hashref($_) || is_scalarref($_)
354 or throw 'Cannot erase this type of scalar', type => ref $_, what => $_;
355 push @args, is_ref($_) ? $_ : \$_;
356 }
357 require Scope::Guard;
358 return Scope::Guard->new(sub { erase(@args) });
359 }
360
361 =func format_uuid
362
363 $string_uuid = format_uuid($raw_uuid);
364 $string_uuid = format_uuid($raw_uuid, $delimiter);
365
366 Format a 128-bit UUID (given as a string of 16 octets) into a hexidecimal string, optionally with a delimiter
367 to break up the UUID visually into five parts. Examples:
368
369 my $uuid = uuid('01234567-89AB-CDEF-0123-456789ABCDEF');
370 say format_uuid($uuid); # -> 0123456789ABCDEF0123456789ABCDEF
371 say format_uuid($uuid, '-'); # -> 01234567-89AB-CDEF-0123-456789ABCDEF
372
373 This is the inverse of L</uuid>.
374
375 =cut
376
377 sub format_uuid {
378 local $_ = shift // "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
379 my $delim = shift // '';
380 length($_) == 16 or throw 'Must provide a 16-bytes UUID', size => length($_), str => $_;
381 return uc(join($delim, unpack('H8 H4 H4 H4 H12', $_)));
382 }
383
384 =func generate_uuid
385
386 $uuid = generate_uuid;
387 $uuid = generate_uuid(\%set);
388 $uuid = generate_uuid(\&test_uuid);
389
390 Generate a new random UUID. It's pretty unlikely that this will generate a repeat, but if you're worried about
391 that you can provide either a set of existing UUIDs (as a hashref where the keys are the elements of a set) or
392 a function to check for existing UUIDs, and this will be sure to not return a UUID already in provided set.
393 Perhaps an example will make it clear:
394
395 my %uuid_set = (
396 uuid('12345678-9ABC-DEFG-1234-56789ABCDEFG') => 'whatever',
397 );
398 $uuid = generate_uuid(\%uuid_set);
399 # OR
400 $uuid = generate_uuid(sub { !$uuid_set{$_} });
401
402 Here, C<$uuid> can't be "12345678-9ABC-DEFG-1234-56789ABCDEFG". This example uses L</uuid> to easily pack
403 a 16-byte UUID from a literal, but it otherwise is not a consequential part of the example.
404
405 =cut
406
407 sub generate_uuid {
408 my $set = @_ % 2 == 1 ? shift : undef;
409 my %args = @_;
410 my $test = $set //= $args{test};
411 $test = sub { !$set->{$_} } if is_hashref($test);
412 $test //= sub { 1 };
413 my $printable = $args{printable} // $args{print};
414 local $_ = '';
415 do {
416 $_ = $printable ? random_string(16) : random_bytes(16);
417 } while (!$test->($_));
418 return $_;
419 }
420
421 =func gunzip
422
423 $unzipped = gunzip($string);
424
425 Decompress an octet stream.
426
427 =cut
428
429 sub gunzip {
430 load_optional('Compress::Raw::Zlib');
431 local $_ = shift;
432 my ($i, $status) = Compress::Raw::Zlib::Inflate->new(-WindowBits => 31);
433 $status == Compress::Raw::Zlib::Z_OK()
434 or throw 'Failed to initialize compression library', status => $status;
435 $status = $i->inflate($_, my $out);
436 $status == Compress::Raw::Zlib::Z_STREAM_END()
437 or throw 'Failed to decompress data', status => $status;
438 return $out;
439 }
440
441 =func gzip
442
443 $zipped = gzip($string);
444
445 Compress an octet stream.
446
447 =cut
448
449 sub gzip {
450 load_optional('Compress::Raw::Zlib');
451 local $_ = shift;
452 my ($d, $status) = Compress::Raw::Zlib::Deflate->new(-WindowBits => 31, -AppendOutput => 1);
453 $status == Compress::Raw::Zlib::Z_OK()
454 or throw 'Failed to initialize compression library', status => $status;
455 $status = $d->deflate($_, my $out);
456 $status == Compress::Raw::Zlib::Z_OK()
457 or throw 'Failed to compress data', status => $status;
458 $status = $d->flush($out);
459 $status == Compress::Raw::Zlib::Z_OK()
460 or throw 'Failed to compress data', status => $status;
461 return $out;
462 }
463
464 =func is_readable
465
466 =func is_writable
467
468 $bool = is_readable($mode);
469 $bool = is_writable($mode);
470
471 Determine of an C<fopen>-style mode is readable, writable or both.
472
473 =cut
474
475 sub is_readable { $_[0] !~ /^[aw]b?$/ }
476 sub is_writable { $_[0] !~ /^rb?$/ }
477
478 =func is_uuid
479
480 $bool = is_uuid($thing);
481
482 Check if a thing is a UUID (i.e. scalar string of length 16).
483
484 =cut
485
486 sub is_uuid { defined $_[0] && !is_ref($_[0]) && length($_[0]) == 16 }
487
488 =func load_optional
489
490 $package = load_optional($package);
491
492 Load a module that isn't required but can provide extra functionality. Throw if the module is not available.
493
494 =cut
495
496 sub load_optional {
497 for my $module (@_) {
498 eval { load $module };
499 if (my $err = $@) {
500 warn $err if $ENV{DEBUG};
501 throw "Missing dependency: Please install $module to use this feature.\n", module => $module;
502 }
503 }
504 return wantarray ? @_ : $_[0];
505 }
506
507 =func memoize
508
509 \&memoized_code = memoize(\&code, ...);
510
511 Memoize a function. Extra arguments are passed through to C<&code> when it is called.
512
513 =cut
514
515 sub memoize {
516 my $func = shift;
517 my @args = @_;
518 my %cache;
519 return sub { $cache{join("\0", grep { defined } @_)} //= $func->(@args, @_) };
520 }
521
522 =func pad_pkcs7
523
524 $padded_string = pad_pkcs7($string, $block_size),
525
526 Pad a block using the PKCS#7 method.
527
528 =cut
529
530 sub pad_pkcs7 {
531 my $data = shift // throw 'Must provide a string to pad';
532 my $size = shift or throw 'Must provide block size';
533
534 0 <= $size && $size < 256
535 or throw 'Cannot add PKCS7 padding to a large block size', size => $size;
536
537 my $pad_len = $size - length($data) % $size;
538 $data .= chr($pad_len) x $pad_len;
539 }
540
541 =func query
542
543 $query = query(@where);
544 $query->(\%data);
545
546 Generate a function that will run a series of tests on a passed hashref and return true or false depending on
547 if the data record in the hash matched the specified logic.
548
549 The logic can be specified in a manner similar to L<SQL::Abstract/"WHERE CLAUSES"> which was the inspiration
550 for this function, but this code is distinct, supporting an overlapping but not identical feature set and
551 having its own bugs.
552
553 See L<File::KDBX/QUERY> for examples.
554
555 =cut
556
557 sub query { _query(undef, '-or', \@_) }
558
559 =func read_all
560
561 $size = read_all($fh, my $buffer, $size);
562 $size = read_all($fh, my $buffer, $size, $offset);
563
564 Like L<functions/read> but returns C<undef> if not all C<$size> bytes are read. This is considered an error,
565 distinguishable from other errors by C<$!> not being set.
566
567 =cut
568
569 sub read_all($$$;$) { ## no critic (ProhibitSubroutinePrototypes)
570 my $result = @_ == 3 ? read($_[0], $_[1], $_[2])
571 : read($_[0], $_[1], $_[2], $_[3]);
572 return if !defined $result;
573 return if $result != $_[2];
574 return $result;
575 }
576
577 =func recurse_limit
578
579 \&limited_code = recurse_limit(\&code);
580 \&limited_code = recurse_limit(\&code, $max_depth);
581 \&limited_code = recurse_limit(\&code, $max_depth, \&error_handler);
582
583 Wrap a function with a guard to prevent deep recursion.
584
585 =cut
586
587 sub recurse_limit {
588 my $func = shift;
589 my $max_depth = shift // 200;
590 my $error = shift // sub {};
591 my $depth = 0;
592 return sub { return $error->(@_) if $max_depth < ++$depth; $func->(@_) };
593 };
594
595 =func search
596
597 # Generate a query on-the-fly:
598 \@matches = search(\@records, @where);
599
600 # Use a pre-compiled query:
601 $query = query(@where);
602 \@matches = search(\@records, $query);
603
604 # Use a simple expression:
605 \@matches = search(\@records, \'query terms', @fields);
606 \@matches = search(\@records, \'query terms', $operator, @fields);
607
608 # Use your own subroutine:
609 \@matches = search(\@records, \&query);
610 \@matches = search(\@records, sub { $record = shift; ... });
611
612 Execute a linear search over an array of records using a L</query>. A "record" is usually a hash.
613
614 This is the search engine described with many examples at L<File::KDBX/QUERY>.
615
616 =cut
617
618 sub search {
619 my $list = shift;
620 my $query = shift;
621 # my %args = @_;
622
623 if (is_coderef($query) && !@_) {
624 # already a query
625 }
626 elsif (is_scalarref($query)) {
627 $query = simple_expression_query($$query, @_);
628 }
629 else {
630 $query = query($query, @_);
631 }
632
633 # my $limit = $args{limit};
634
635 my @match;
636 for my $item (@$list) {
637 push @match, $item if $query->($item);
638 # last if defined $limit && $limit <= @match;
639 }
640 return \@match;
641 }
642
643 =func simple_expression_query
644
645 $query = simple_expression_query($expression, @fields);
646
647 Generate a query, like L</query>, to be used with L</search> but built from a "simple expression" as
648 L<described here|https://keepass.info/help/base/search.html#mode_se>.
649
650 An expression is a string with one or more space-separated terms. Terms with spaces can be enclosed in double
651 quotes. Terms are negated if they are prefixed with a minus sign. A record must match every term on at least
652 one of the given fields.
653
654 =cut
655
656 sub simple_expression_query {
657 my $expr = shift;
658 my $op = @_ && ($OPS{$_[0] || ''} || 0) == 2 ? shift : '=~';
659
660 my $neg_op = $OP_NEG{$op};
661 my $is_re = $op eq '=~' || $op eq '!~';
662
663 require Text::ParseWords;
664 my @terms = Text::ParseWords::shellwords($expr);
665
666 my @query = qw(-and);
667
668 for my $term (@terms) {
669 my @subquery = qw(-or);
670
671 my $neg = $term =~ s/^-//;
672 my $condition = [($neg ? $neg_op : $op) => ($is_re ? qr/\Q$term\E/i : $term)];
673
674 for my $field (@_) {
675 push @subquery, $field => $condition;
676 }
677
678 push @query, \@subquery;
679 }
680
681 return query(\@query);
682 }
683
684 =func snakify
685
686 $string = snakify($string);
687
688 Turn a CamelCase string into snake_case.
689
690 =cut
691
692 sub snakify {
693 local $_ = shift;
694 s/UserName/Username/g;
695 s/([a-z])([A-Z0-9])/${1}_${2}/g;
696 s/([A-Z0-9]+)([A-Z0-9])(?![A-Z0-9]|$)/${1}_${2}/g;
697 return lc($_);
698 }
699
700 =func split_url
701
702 ($scheme, $auth, $host, $port, $path, $query, $hash, $usename, $password) = split_url($url);
703
704 Split a URL into its parts.
705
706 For example, C<http://user:pass@localhost:4000/path?query#hash> gets split like:
707
708 =for :list
709 * C<http>
710 * C<user:pass>
711 * C<host>
712 * C<4000>
713 * C</path>
714 * C<?query>
715 * C<#hash>
716 * C<user>
717 * C<pass>
718
719 =cut
720
721 sub split_url {
722 local $_ = shift;
723 my ($scheme, $auth, $host, $port, $path, $query, $hash) =~ m!
724 ^([^:/\?\#]+) ://
725 (?:([^\@]+)\@)
726 ([^:/\?\#]*)
727 (?::(\d+))?
728 ([^\?\#]*)
729 (\?[^\#]*)?
730 (\#(.*))?
731 !x;
732
733 $scheme = lc($scheme);
734
735 $host ||= 'localhost';
736 $host = lc($host);
737
738 $path = "/$path" if $path !~ m!^/!;
739
740 $port ||= $scheme eq 'http' ? 80 : $scheme eq 'https' ? 433 : undef;
741
742 my ($username, $password) = split($auth, ':', 2);
743
744 return ($scheme, $auth, $host, $port, $path, $query, $hash, $username, $password);
745 }
746
747 =func trim
748
749 $string = trim($string);
750
751 The ubiquitous C<trim> function. Removes all whitespace from both ends of a string.
752
753 =cut
754
755 sub trim($) { ## no critic (ProhibitSubroutinePrototypes)
756 local $_ = shift // return;
757 s/^\s*//;
758 s/\s*$//;
759 return $_;
760 }
761
762 =func try_load_optional
763
764 $package = try_load_optional($package);
765
766 Try to load a module that isn't required but can provide extra functionality, and return true if successful.
767
768 =cut
769
770 sub try_load_optional {
771 for my $module (@_) {
772 eval { load $module };
773 if (my $err = $@) {
774 warn $err if $ENV{DEBUG};
775 return;
776 }
777 }
778 return @_;
779 }
780
781 =func uri_escape_utf8
782
783 $string = uri_escape_utf8($string);
784
785 Percent-encode arbitrary text strings, like for a URI.
786
787 =cut
788
789 my %ESC = map { chr($_) => sprintf('%%%02X', $_) } 0..255;
790 sub uri_escape_utf8 {
791 local $_ = shift // return;
792 $_ = encode('UTF-8', $_);
793 # RFC 3986 section 2.3 unreserved characters
794 s/([^A-Za-z0-9\-\._~])/$ESC{$1}/ge;
795 return $_;
796 }
797
798 =func uri_unescape_utf8
799
800 $string = uri_unescape_utf8($string);
801
802 Inverse of L</uri_escape_utf8>.
803
804 =cut
805
806 sub uri_unescape_utf8 {
807 local $_ = shift // return;
808 s/\%([A-Fa-f0-9]{2})/chr(hex($1))/;
809 return decode('UTF-8', $_);
810 }
811
812 =func uuid
813
814 $raw_uuid = uuid($string_uuid);
815
816 Pack a 128-bit UUID (given as a hexidecimal string with optional C<->'s, like
817 C<12345678-9ABC-DEFG-1234-56789ABCDEFG>) into a string of exactly 16 octets.
818
819 This is the inverse of L</format_uuid>.
820
821 =cut
822
823 sub uuid {
824 local $_ = shift // return "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
825 s/-//g;
826 /^[A-Fa-f0-9]{32}$/ or throw 'Must provide a formatted 128-bit UUID';
827 return pack('H32', $_);
828
829 }
830
831 ### --------------------------------------------------------------------------
832
833 # Determine if an array looks like keypairs from a hash.
834 sub _looks_like_keypairs {
835 my $arr = shift;
836 return 0 if @$arr % 2 == 1;
837 for (my $i = 0; $i < @$arr; $i += 2) {
838 return 0 if is_ref($arr->[$i]);
839 }
840 return 1;
841 }
842
843 sub _is_operand_plain {
844 local $_ = shift;
845 return !(is_hashref($_) || is_arrayref($_));
846 }
847
848 sub _query {
849 # dumper \@_;
850 my $subject = shift;
851 my $op = shift // throw 'Must specify a query operator';
852 my $operand = shift;
853
854 return _query_simple($op, $subject) if defined $subject && !is_ref($op) && ($OPS{$subject} || 2) < 2;
855 return _query_simple($subject, $op, $operand) if _is_operand_plain($operand);
856 return _query_inverse(_query($subject, '-or', $operand)) if $op eq '-not' || $op eq '-false';
857 return _query($subject, '-and', [%$operand]) if is_hashref($operand);
858
859 my @queries;
860
861 my @atoms = @$operand;
862 while (@atoms) {
863 if (_looks_like_keypairs(\@atoms)) {
864 my ($atom, $operand) = splice @atoms, 0, 2;
865 if (my $op_type = $OPS{$atom}) {
866 if ($op_type == 1 && _is_operand_plain($operand)) { # unary
867 push @queries, _query_simple($operand, $atom);
868 }
869 else {
870 push @queries, _query($subject, $atom, $operand);
871 }
872 }
873 elsif (!is_ref($atom)) {
874 push @queries, _query($atom, 'eq', $operand);
875 }
876 }
877 else {
878 my $atom = shift @atoms;
879 if ($OPS{$atom}) { # apply new operator over the rest
880 push @queries, _query($subject, $atom, \@atoms);
881 last;
882 }
883 else { # apply original operator over this one
884 push @queries, _query($subject, $op, $atom);
885 }
886 }
887 }
888
889 if (@queries == 1) {
890 return $queries[0];
891 }
892 elsif ($op eq '-and') {
893 return _query_all(@queries);
894 }
895 elsif ($op eq '-or') {
896 return _query_any(@queries);
897 }
898 throw 'Malformed query';
899 }
900
901 sub _query_simple {
902 my $subject = shift;
903 my $op = shift // 'eq';
904 my $operand = shift;
905
906 # these special operators can also act as simple operators
907 $op = '!!' if $op eq '-true';
908 $op = '!' if $op eq '-false';
909 $op = '!' if $op eq '-not';
910
911 defined $subject or throw 'Subject is not set in query';
912 $OPS{$op} >= 0 or throw 'Cannot use a non-simple operator in a simple query';
913 if (empty($operand)) {
914 if ($OPS{$op} < 2) {
915 # no operand needed
916 }
917 # Allow field => undef and field => {'ne' => undef} to do the (arguably) right thing.
918 elsif ($op eq 'eq' || $op eq '==') {
919 $op = '-empty';
920 }
921 elsif ($op eq 'ne' || $op eq '!=') {
922 $op = '-nonempty';
923 }
924 else {
925 throw 'Operand is required';
926 }
927 }
928
929 my $field = sub { blessed $_[0] && $_[0]->can($subject) ? $_[0]->$subject : $_[0]->{$subject} };
930
931 my %map = (
932 'eq' => sub { local $_ = $field->(@_); defined && $_ eq $operand },
933 'ne' => sub { local $_ = $field->(@_); defined && $_ ne $operand },
934 'lt' => sub { local $_ = $field->(@_); defined && $_ lt $operand },
935 'gt' => sub { local $_ = $field->(@_); defined && $_ gt $operand },
936 'le' => sub { local $_ = $field->(@_); defined && $_ le $operand },
937 'ge' => sub { local $_ = $field->(@_); defined && $_ ge $operand },
938 '==' => sub { local $_ = $field->(@_); defined && $_ == $operand },
939 '!=' => sub { local $_ = $field->(@_); defined && $_ != $operand },
940 '<' => sub { local $_ = $field->(@_); defined && $_ < $operand },
941 '>' => sub { local $_ = $field->(@_); defined && $_ > $operand },
942 '<=' => sub { local $_ = $field->(@_); defined && $_ <= $operand },
943 '>=' => sub { local $_ = $field->(@_); defined && $_ >= $operand },
944 '=~' => sub { local $_ = $field->(@_); defined && $_ =~ $operand },
945 '!~' => sub { local $_ = $field->(@_); defined && $_ !~ $operand },
946 '!' => sub { local $_ = $field->(@_); ! $_ },
947 '!!' => sub { local $_ = $field->(@_); !!$_ },
948 '-defined' => sub { local $_ = $field->(@_); defined $_ },
949 '-undef' => sub { local $_ = $field->(@_); !defined $_ },
950 '-nonempty' => sub { local $_ = $field->(@_); nonempty $_ },
951 '-empty' => sub { local $_ = $field->(@_); empty $_ },
952 );
953
954 return $map{$op} // throw "Unexpected operator in query: $op",
955 subject => $subject,
956 operator => $op,
957 operand => $operand;
958 }
959
960 sub _query_inverse {
961 my $query = shift;
962 return sub { !$query->(@_) };
963 }
964
965 sub _query_all {
966 my @queries = @_;
967 return sub {
968 my $val = shift;
969 all { $_->($val) } @queries;
970 };
971 }
972
973 sub _query_any {
974 my @queries = @_;
975 return sub {
976 my $val = shift;
977 any { $_->($val) } @queries;
978 };
979 }
980
981 1;
This page took 0.0903 seconds and 3 git commands to generate.