| 1 | package CBOR::Free::Decoder; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | CBOR::Free::Decoder | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $decoder = CBOR::Free::Decoder->new()->set_tag_handlers( | ||||||||||||||||||||||||||||
| 12 | 2 => sub { DateTime->from_epoch( epoch => shift() ) }, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Enable shared/circular references: | ||||||||||||||||||||||||||||
| 16 | $decoder->preserve_references(); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This class provides an object-oriented interface to L<CBOR::Free>’s | ||||||||||||||||||||||||||||
| 21 | decoder. This interface allows interpretation of tagged values. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use CBOR::Free (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 $obj = I<CLASS>->new() | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Creates a new CBOR decoder object. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 104 | ▶ | sub new { bless { _flags => 0 } } # TODO: implement in XS, and store a context. | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 $data = I<OBJ>->decode( $CBOR ) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Same as L<CBOR::Free>’s static function of the same name but applies | ||||||||||||||||||||||||||||
| 46 | any tag handlers configured in C<set_tag_handlers()>. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | As in L<CBOR::Free>, any unrecognized tags prompt a warning but are | ||||||||||||||||||||||||||||
| 49 | otherwise ignored. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head2 $enabled_yn = I<OBJ>->preserve_references( [$ENABLE] ) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Enables/disables recognition of CBOR’s shared references. (If no | ||||||||||||||||||||||||||||
| 58 | argument is given, shared references will be enabled.) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | B<HANDLE WITH CARE.> This option can cause CBOR::Free to create circular | ||||||||||||||||||||||||||||
| 61 | references, which can cause memory leaks if not handled properly. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub preserve_references { | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if (@_ < 2 || !!$_[1]) { | ||||||||||||||||||||||||||
|
Subroutine
preserve_references: called CC=3 SCAR=12.5
Pod
documented
Branch: 1 branch
Condition33%1/3
@_ < 2 or not !$_[1]
MC/DC0%0/2
@_ < 2 or not !$_[1]
@_ < 2
not !$_[1]
Truth table33%1/3
A or B
A= @_ < 2B= not !$_[1]
| |||||||||||||||||||||||||||||
| 67 | 1 | $_[0]{'_flags'} |= _FLAG_PRESERVE_REFERENCES(); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | return !!($_[0]{'_flags'} & _FLAG_PRESERVE_REFERENCES()); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 $enabled_yn = I<OBJ>->naive_utf8( [$ENABLE] ) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Same interface as C<preserve_references()>, but this option tells I<OBJ> | ||||||||||||||||||||||||||||
| 78 | to forgo UTF-8 validation of CBOR text strings when enabled. This speeds up | ||||||||||||||||||||||||||||
| 79 | decoding of text strings but may confuse Perl if invalid UTF-8 is given in | ||||||||||||||||||||||||||||
| 80 | a CBOR text string. That may or may not break your application. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | This I<should> be safe in contexts—such as IPC—where you control the CBOR | ||||||||||||||||||||||||||||
| 83 | serialization and can thus ensure validity of the encoded text. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | If in doubt, leave this off. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub naive_utf8 { | ||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if (@_ < 2 || !!$_[1]) { | ||||||||||||||||||||||||||
|
Subroutine
naive_utf8: called CC=3 SCAR=12.5
Pod
documented
Branch: 1 branch
Condition33%1/3
@_ < 2 or not !$_[1]
MC/DC0%0/2
@_ < 2 or not !$_[1]
@_ < 2
not !$_[1]
Truth table33%1/3
A or B
A= @_ < 2B= not !$_[1]
| |||||||||||||||||||||||||||||
| 91 | 2 | $_[0]{'_flags'} |= _FLAG_NAIVE_UTF8(); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | return !!($_[0]{'_flags'} & _FLAG_NAIVE_UTF8()); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 I<OBJ>->set_tag_handlers( %TAG_CALLBACK ) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Takes a list of key/value pairs where each key is a tag (i.e., number) | ||||||||||||||||||||||||||||
| 102 | and each value is a coderef that CBOR::Free will run when that tag is | ||||||||||||||||||||||||||||
| 103 | seen during a decode operation. The coderef will receive the tagged value, | ||||||||||||||||||||||||||||
| 104 | and its (scalar) return will be inserted into the decoded data structure. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | To unset a tag handler, assign undef to it. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | This returns the I<OBJ>. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | B<NOTE:> Handlers assigned here will only fire if CBOR::Free itself | ||||||||||||||||||||||||||||
| 111 | doesn’t decode the tag. For example, a handler for the “indirection” tag | ||||||||||||||||||||||||||||
| 112 | here will be ignored. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | ▶ | use constant _TAG_PACK_TMPL => eval { pack 'Q' } ? 'Q' : 'L'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub set_tag_handlers { | ||||||||||||||||||||||||||||
| 119 | 101 | ▶ | my ($self, @tag_cb) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_tag_handlers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 101 | ▶ | while (my ($tag, $cb) = splice @tag_cb, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 101 | $self->{'_tag_decode_callback'}{ pack( _TAG_PACK_TMPL(), $tag ) } = $cb; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 101 | return $self; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 4 | 1; | |||||||||||||||||||||||||||