| 1 | package CBOR::PP::Encode; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | CBOR::PP::Decode | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | my $perlvar = CBOR::PP::Decode::decode($binary); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | This implements a L<CBOR|https://tools.ietf.org/html/rfc7049> encoder | ||||||||||||||||||||||||||||||||||||||||||
| 19 | in pure Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 MAPPING PERL TO CBOR | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =item * Scalars that look like unsigned integers are encoded as such. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | UTF-8 strings and strings that fit 7-bit ASCII (including floats and | ||||||||||||||||||||||||||||||||||||||||||
| 27 | negatives) are encoded as text. Any other scalar is encoded as binary. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | Note that there is no “right way” to determine whether an arbitrary | ||||||||||||||||||||||||||||||||||||||||||
| 30 | Perl (non-reference) scalar should be encoded as a string or as a number. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | The above seems a reasonable enough approach. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | =item * UTF8-flagged strings are encoded as text; others are encoded as | ||||||||||||||||||||||||||||||||||||||||||
| 34 | binary. This is a “best-guess” merely; Perl’s UTF8 flag doesn’t reliably | ||||||||||||||||||||||||||||||||||||||||||
| 35 | indicate whether a given string is a text or a byte string. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =item * undef, Types::Serialiser::true(), and Types::Serialiser::false() | ||||||||||||||||||||||||||||||||||||||||||
| 38 | are encoded as null, true, and false, respectively. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =item * There is no support for streamed (i.e., indefinite-length) | ||||||||||||||||||||||||||||||||||||||||||
| 41 | objects. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =item * There is no Perl value that maps to CBOR’s undefined value. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =item * Add canonicalization support. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =item * Optimize as may be feasible. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE) | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | This code is licensed under the same license as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | use CBOR::PP::X; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | ▶ | use CBOR::PP::Tagged; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head2 $obj = tag( $NUMBER, $VALUE ) | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Returns an object that represents a value and its CBOR tag number. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | For example, to encode a date/time string, you could do: | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | my $tagged = tag(0, '2013-03-21T20:04:00Z') | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | C<encode()> recognizes objects that this function returns and | ||||||||||||||||||||||||||||||||||||||||||
| 84 | turns them into tagged CBOR values. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | sub tag { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | return CBOR::PP::Tagged->new(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 $cbor = encode( $VALUE, \%OPTS ) | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | Returns a CBOR string that represents the passed $VALUE. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | For now this is only called as a static method but may eventually | ||||||||||||||||||||||||||||||||||||||||||
| 101 | be an instance method as well, for example, to define options like | ||||||||||||||||||||||||||||||||||||||||||
| 102 | canonicalization. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 6 | my ($numkeys); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 6 | our $_depth = 0; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # Avoid tripping Perl’s warning: | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | ▶ | use constant _MAX_RECURSION => 98; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | sub encode { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # There’s a lot of ugliness in here for the sake of speed. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # For example, ideally each major type would have its own function, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # but we realize significant savings by putting everything into | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # one big function. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 479 | ▶ | local $_depth = $_depth + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=39 SCAR=46.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 479 | ▶ | die CBOR::PP::X->create('Recursion', sprintf("Refuse to encode() more than %d times at once!", _MAX_RECURSION())) if $_depth > _MAX_RECURSION(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 477 | for ($_[0]) { | |||||||||||||||||||||||||||||||||||||||||
| 124 | 477 | ▶ | if (!ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # undef => null | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 230 | ▶ | return "\xf6" if !defined; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | # empty string | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 228 | ▶ | return utf8::is_utf8($_) ? "\x60" : "\x40" if !length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # unsigned int | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 225 | ▶ | if (!$_ || (!tr<0-9><>c && 0 != rindex($_, 0, 0))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $_ or not tr/0-9//c and 0 != rindex($_, 0, 0)
not tr/0-9//c and 0 != rindex($_, 0, 0)
MC/DC100%3/3
not $_ or not tr/0-9//c and 0 != rindex($_, 0, 0)
not $_
not tr/0-9//c
0 != rindex($_, 0, 0)
Truth table100%4/4
not(A) or not(B) and C
A= not $_B= not tr/0-9//cC= 0 != rindex($_, 0, 0)
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 158 | ▶ | return chr $_ if ($_ < 24); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 24 | ▶ | return pack('CC', 0x18, $_) if $_ < 0x100; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 12 | ▶ | return pack('Cn', 0x19, $_) if ($_ < 0x10000); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | ▶ | return pack('CN', 0x1a, $_) if ($_ <= 0xffffffff); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | return pack('C Q>', 0x1b, $_); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # negative int | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # elsif ( 0 == rindex($_, '-', 0) && (substr($_, 1) !~ tr<0-9><>c) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # return chr( 0x20 - $_ ) if ($_ > -25); | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # return pack( 'CC', 0x38, -$_ ) if $_ >= -0x100; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # return pack( 'Cv', 0x39, -$_ ) if $_ >= -0x10000; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # return pack( 'CV', 0x3a, -$_ ) if $_ >= -0x100000000; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # return pack( 'C Q>', 0x3b, -$_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 67 | ▶ | if (utf8::is_utf8($_)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | # Perl doesn’t seem to have a way to pack() a | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # a UTF-8 string directly to bytes??? | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | utf8::encode(my $bytes = $_); | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 6 | ▶ | return pack('Ca*', 0x60 + length($bytes), $bytes) if (length() < 24); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return pack('CCa*', 0x78, length($bytes), $bytes) if (length() < 0x100); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return pack('Cna*', 0x79, length($bytes), $bytes) if (length() < 0x10000); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return pack('CNa*', 0x7a, length($bytes), $bytes) if (length() <= 0xffffffff); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | return pack('C Q> a*', 0x7b, length($bytes), $bytes); | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 61 | ▶ | return pack('Ca*', 0x40 + length, $_) if (length() < 24); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return pack('CCa*', 0x58, length, $_) if (length() < 0x100); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return pack('Cna*', 0x59, length, $_) if (length() < 0x10000); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | return pack('CNa*', 0x5a, length, $_) if (length() <= 0xffffffff); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | return pack('C Q> a*', 0x5b, length, $_); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | elsif (ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 227 | my $hdr; | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 227 | ▶ | if (@$_ < 24) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 224 | $hdr = chr( 0x80 + @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | elsif (@$_ < 0x100) { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 3 | $hdr = pack( 'CC', 0x98, 0 + @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | elsif (@$_ < 0x10000) { | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $hdr = pack( 'Cn', 0x99, 0 + @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | elsif (@$_ <= 0xffffffff) { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $hdr = pack( 'CN', 0x9a, 0 + @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $hdr = pack( 'C Q>', 0x9b, 0 + @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 227 | return join( q<>, $hdr, map { encode($_, $_[1]) } @$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | elsif (ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 14 | my $hdr; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 14 | $numkeys = keys %$_; | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 14 | ▶ | if ($numkeys < 24) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 14 | $hdr = chr( 0xa0 + $numkeys ); | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | elsif ($numkeys < 0x100) { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $hdr = pack( 'CC', 0xb8, $numkeys ); | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | elsif ($numkeys < 0x10000) { | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $hdr = pack( 'Cn', 0xb9, $numkeys ); | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | elsif ($numkeys <= 0xffffffff) { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $hdr = pack( 'CN', 0xba, $numkeys ); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $hdr = pack( 'C Q>', 0xbb, $numkeys ); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 14 | ▶ | if ($_[1] && $_[1]->{'canonical'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[1] and $_[1]{"canonical"}
MC/DC50%1/2
$_[1] and $_[1]{"canonical"}
$_[1]
$_[1]{"canonical"}
Truth table66%2/3
A and B
A= $_[1]B= $_[1]{"canonical"}
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | my $hr = $_; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 2 | ▶ | my @keys = sort { (length($a) <=> length($b)) || ($a cmp $b) } keys %$_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 2 | return join( q<>, $hdr, map { encode($_), encode($hr->{$_}, $_[1]) } @keys ); | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 12 | return join( q<>, $hdr, map { encode($_, $_[1]) } %$_ ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | elsif (ref()->isa('JSON::PP::Boolean')) { | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 5 | ▶ | return $_ ? "\xf5" : "\xf4"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | elsif (ref()->isa('CBOR::PP::Tagged')) { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | my $numstr = encode( $_->[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | substr($numstr, 0, 1) &= "\x1f"; # zero out the first three bits | |||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | substr($numstr, 0, 1) |= "\xc0"; # now assign the first three | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | return( $numstr . encode( $_->[1], $_[1] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | die "Can’t encode “$_” as CBOR!"; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||