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