| 1 | package CBOR::PP::Decode; | ||||||||||||||||||||||||||||||||||
| 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 basic CBOR decoder in pure Perl. | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =head1 MAPPING CBOR TO PERL | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =over | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =item * All tags are ignored. (This could be iterated on later.) | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | =item * Indefinite-length objects are supported, but streamed parsing | ||||||||||||||||||||||||||||||||||
| 27 | is not; the data structure must be complete to be decoded. | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | =item * CBOR text strings are decoded to UTF8-flagged strings, while | ||||||||||||||||||||||||||||||||||
| 30 | binary strings are decoded to non-UTF8-flagged strings. In practical | ||||||||||||||||||||||||||||||||||
| 31 | terms, this means that a decoded CBOR binary string will have no code point | ||||||||||||||||||||||||||||||||||
| 32 | above 255, while a decoded CBOR text string can contain any valid Unicode | ||||||||||||||||||||||||||||||||||
| 33 | code point. | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | =item * null, undefined, true, and false become undef, undef, | ||||||||||||||||||||||||||||||||||
| 36 | Types::Serialiser::true(), and Types::Serialiser::false(), respectively. | ||||||||||||||||||||||||||||||||||
| 37 | (NB: undefined is deserialized as an error object in L<CBOR::XS>, | ||||||||||||||||||||||||||||||||||
| 38 | which doesn’t seem to make sense.) | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | =back | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | =head1 TODO | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | =over | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | =item * Add tag decode support via callbacks. | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =item * Make it faster by removing some of the internal buffer copying. | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | =back | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE) | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | This code is licensed under the same license as Perl itself. | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | =head2 $value = decode( $CBOR_BYTESTRING ) | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | Returns a Perl value that represents the serialized CBOR string. | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 6 | my ($byte1, $offset, $lead3bits); | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 6 | my $len; | |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # This ensures that pieces of indefinite-length strings | ||||||||||||||||||||||||||||||||||
| 77 | # are all of the same type. | ||||||||||||||||||||||||||||||||||
| 78 | 6 | our $_lead3_must_be; | |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | use constant { | ||||||||||||||||||||||||||||||||||
| 81 | 6 | _LEAD3_UINT => 0, | |||||||||||||||||||||||||||||||||
| 82 | _LEAD3_NEGINT => 1 << 5, | ||||||||||||||||||||||||||||||||||
| 83 | _LEAD3_BINSTR => 2 << 5, | ||||||||||||||||||||||||||||||||||
| 84 | _LEAD3_UTF8STR => 3 << 5, | ||||||||||||||||||||||||||||||||||
| 85 | _LEAD3_ARRAY => 4 << 5, | ||||||||||||||||||||||||||||||||||
| 86 | _LEAD3_HASH => 5 << 5, | ||||||||||||||||||||||||||||||||||
| 87 | _LEAD3_TAG => 6 << 5, | ||||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | }; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | # CBOR is much simpler to create than it is to parse! | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | # TODO: Optimize by removing the buffer duplication. | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | sub decode { | ||||||||||||||||||||||||||||||||||
| 95 | 404 | ▶ | $offset = 0; | ||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=58 SCAR=51.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | 404 | for ($_[0]) { | |||||||||||||||||||||||||||||||||
| 98 | 404 | $byte1 = ord( substr( $_, $offset, 1 ) ); | |||||||||||||||||||||||||||||||||
| 99 | 404 | $lead3bits = 0xe0 & $byte1; | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 404 | ▶ | die "Improper lead3 ($lead3bits) within streamed $_lead3_must_be!" if $_lead3_must_be && $lead3bits != $_lead3_must_be; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$CBOR::PP::Decode::_lead3_must_be and $lead3bits != $CBOR::PP::Decode::_lead3_must_be
MC/DC0%0/2
$CBOR::PP::Decode::_lead3_must_be and $lead3bits != $CBOR::PP::Decode::_lead3_must_be
$CBOR::PP::Decode::_lead3_must_be
$lead3bits != $CBOR::PP::Decode::_lead3_must_be
Truth table66%2/3
A and B
A= $CBOR::PP::Decode::_lead3_must_beB= $lead3bits != $CBOR::PP::Decode::_lead3_must_be
| |||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | #use Text::Control; | ||||||||||||||||||||||||||||||||||
| 104 | #print Text::Control::to_hex($_) . $/; | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 404 | ▶ | if ($lead3bits == _LEAD3_UINT()) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 107 | 203 | ▶ | return (1, ord) if $byte1 < 0x18; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 27 | ▶ | return (2, unpack('x C', $_)) if $byte1 == 0x18; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | 13 | ▶ | return (3, unpack('x n', $_)) if $byte1 == 0x19; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | 11 | ▶ | return (5, unpack('x N', $_)) if $byte1 == 0x1a; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 6 | return (9, unpack('x Q>', $_)); | |||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | elsif ($lead3bits == _LEAD3_NEGINT()) { | ||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | return (1, 0x1f - ord()) if $byte1 < 0x38; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | return (2, -unpack( 'x C', $_) - 1) if $byte1 == 0x38; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | return (3, -unpack( 'x n', $_) - 1) if $byte1 == 0x39; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return (5, -unpack( 'x N', $_) - 1) if $byte1 == 0x3a; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 0 | return (9, -unpack( 'x Q>', $_) - 1); | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | elsif ($lead3bits == _LEAD3_BINSTR()) { | ||||||||||||||||||||||||||||||||||
| 131 | 59 | my $hdrlen; | |||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | 59 | ▶ | if ($byte1 < 0x58) { | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 134 | 58 | $len = $byte1 - 0x40; | |||||||||||||||||||||||||||||||||
| 135 | 58 | $hdrlen = 1; | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | elsif ($byte1 == 0x58) { | ||||||||||||||||||||||||||||||||||
| 138 | 0 | $len = unpack 'x C', $_; | |||||||||||||||||||||||||||||||||
| 139 | 0 | $hdrlen = 2; | |||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | elsif ($byte1 == 0x59) { | ||||||||||||||||||||||||||||||||||
| 142 | 0 | $len = unpack 'x n', $_; | |||||||||||||||||||||||||||||||||
| 143 | 0 | $hdrlen = 3; | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | elsif ($byte1 == 0x5a) { | ||||||||||||||||||||||||||||||||||
| 146 | 0 | $len = unpack 'x N', $_; | |||||||||||||||||||||||||||||||||
| 147 | 0 | $hdrlen = 5; | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | elsif ($byte1 == 0x5b) { | ||||||||||||||||||||||||||||||||||
| 150 | 0 | $len = unpack 'x Q>', $_; | |||||||||||||||||||||||||||||||||
| 151 | 0 | $hdrlen = 9; | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | elsif ($byte1 == 0x5f) { | ||||||||||||||||||||||||||||||||||
| 154 | 1 | return _stringstream(); | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||||||||
| 157 | 0 | die "Invalid lead byte: $byte1"; | |||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 58 | return ($hdrlen + $len, substr( $_, $hdrlen, $len )); | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | elsif ($lead3bits == _LEAD3_UTF8STR()) { | ||||||||||||||||||||||||||||||||||
| 164 | 38 | my $hdrlen; | |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 38 | ▶ | if ($byte1 < 0x78) { | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 167 | 37 | $len = $byte1 - 0x60; | |||||||||||||||||||||||||||||||||
| 168 | 37 | $hdrlen = 1; | |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||
| 170 | elsif ($byte1 == 0x78) { | ||||||||||||||||||||||||||||||||||
| 171 | 0 | $len = unpack 'x C', $_; | |||||||||||||||||||||||||||||||||
| 172 | 0 | $hdrlen = 2; | |||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||
| 174 | elsif ($byte1 == 0x79) { | ||||||||||||||||||||||||||||||||||
| 175 | 0 | $len = unpack 'x n', $_; | |||||||||||||||||||||||||||||||||
| 176 | 0 | $hdrlen = 3; | |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | elsif ($byte1 == 0x7a) { | ||||||||||||||||||||||||||||||||||
| 179 | 0 | $len = unpack 'x N', $_; | |||||||||||||||||||||||||||||||||
| 180 | 0 | $hdrlen = 5; | |||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||
| 182 | elsif ($byte1 == 0x7b) { | ||||||||||||||||||||||||||||||||||
| 183 | 0 | $len = unpack 'x Q>', $_; | |||||||||||||||||||||||||||||||||
| 184 | 0 | $hdrlen = 9; | |||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||
| 186 | elsif ($byte1 == 0x7f) { | ||||||||||||||||||||||||||||||||||
| 187 | 1 | return _stringstream(); | |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||||
| 190 | 0 | die "Invalid lead byte: $byte1"; | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | 37 | my $v = substr( $_, $hdrlen, $len ); | |||||||||||||||||||||||||||||||||
| 194 | 37 | utf8::decode($v); | |||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||
| 196 | # A no-op if $v is already UTF8-flagged, but if it’s not, | ||||||||||||||||||||||||||||||||||
| 197 | # then this will apply the flag. We thus preserve the ability | ||||||||||||||||||||||||||||||||||
| 198 | # to round-trip a character string through Perl. | ||||||||||||||||||||||||||||||||||
| 199 | 37 | utf8::upgrade($v); | |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 37 | return ($hdrlen + $len, $v); | |||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | elsif ($lead3bits == _LEAD3_ARRAY()) { | ||||||||||||||||||||||||||||||||||
| 205 | 48 | my $total; | |||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||
| 207 | 48 | ▶ | if ($byte1 < 0x98) { | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 208 | 37 | $len = $byte1 - 0x80; | |||||||||||||||||||||||||||||||||
| 209 | 37 | $total = 1; | |||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | elsif ($byte1 == 0x98) { | ||||||||||||||||||||||||||||||||||
| 212 | 3 | $len = unpack 'x C', $_; | |||||||||||||||||||||||||||||||||
| 213 | 3 | $total = 2; | |||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||
| 215 | elsif ($byte1 == 0x99) { | ||||||||||||||||||||||||||||||||||
| 216 | 0 | $len = unpack 'x n', $_; | |||||||||||||||||||||||||||||||||
| 217 | 0 | $total = 3; | |||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||
| 219 | elsif ($byte1 == 0x9a) { | ||||||||||||||||||||||||||||||||||
| 220 | 0 | $len = unpack 'x N', $_; | |||||||||||||||||||||||||||||||||
| 221 | 0 | $total = 5; | |||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||
| 223 | elsif ($byte1 == 0x9b) { | ||||||||||||||||||||||||||||||||||
| 224 | 0 | $len = unpack 'x Q>', $_; | |||||||||||||||||||||||||||||||||
| 225 | 0 | $total = 9; | |||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | elsif ($byte1 == 0x9f) { | ||||||||||||||||||||||||||||||||||
| 228 | 8 | return _arraystream(); | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||||
| 231 | 0 | die "Invalid lead byte: $byte1"; | |||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | # pre-fill the array | ||||||||||||||||||||||||||||||||||
| 235 | 40 | my @val = (undef) x $len; | |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | 40 | my $cur_len; | |||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | 40 | for my $i ( 0 .. ($len - 1) ) { | |||||||||||||||||||||||||||||||||
| 240 | 153 | ($cur_len, $val[$i]) = decode( substr( $_, $total ) ); | |||||||||||||||||||||||||||||||||
| 241 | 153 | $total += $cur_len; | |||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||
| 244 | 40 | return( $total, \@val ); | |||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||
| 247 | elsif ($lead3bits == _LEAD3_HASH()) { | ||||||||||||||||||||||||||||||||||
| 248 | 16 | my ($len, $total); | |||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||
| 250 | 16 | ▶ | if ($byte1 < 0xb8) { | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 251 | 13 | $len = $byte1 - 0xa0; | |||||||||||||||||||||||||||||||||
| 252 | 13 | $total = 1; | |||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||
| 254 | elsif ($byte1 == 0xb8) { | ||||||||||||||||||||||||||||||||||
| 255 | 0 | $len = unpack 'x C', $_; | |||||||||||||||||||||||||||||||||
| 256 | 0 | $total = 2; | |||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||
| 258 | elsif ($byte1 == 0xb9) { | ||||||||||||||||||||||||||||||||||
| 259 | 0 | $len = unpack 'x n', $_; | |||||||||||||||||||||||||||||||||
| 260 | 0 | $total = 3; | |||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||
| 262 | elsif ($byte1 == 0xba) { | ||||||||||||||||||||||||||||||||||
| 263 | 0 | $len = unpack 'x N', $_; | |||||||||||||||||||||||||||||||||
| 264 | 0 | $total = 5; | |||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||
| 266 | elsif ($byte1 == 0xbb) { | ||||||||||||||||||||||||||||||||||
| 267 | 0 | $len = unpack 'x Q>', $_; | |||||||||||||||||||||||||||||||||
| 268 | 0 | $total = 9; | |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | elsif ($byte1 == 0xbf) { | ||||||||||||||||||||||||||||||||||
| 271 | 3 | return _hashstream(); | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | else { | ||||||||||||||||||||||||||||||||||
| 274 | 0 | die "Invalid lead byte: $byte1"; | |||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||
| 277 | 13 | my %val; | |||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | my $cur_len; | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | 13 | ▶ | while ( $len > 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 282 | 25 | ($cur_len, my $key) = decode( substr( $_, $total ) ); | |||||||||||||||||||||||||||||||||
| 283 | 25 | $total += $cur_len; | |||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||
| 285 | 25 | ( $cur_len, $val{$key} ) = decode( substr( $_, $total ) ); | |||||||||||||||||||||||||||||||||
| 286 | 25 | $total += $cur_len; | |||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | 25 | $len--; | |||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||
| 291 | 13 | return( $total, \%val ); | |||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||
| 294 | # tags … just ignore for now | ||||||||||||||||||||||||||||||||||
| 295 | elsif ($lead3bits == _LEAD3_TAG()) { | ||||||||||||||||||||||||||||||||||
| 296 | 6 | my $taglen; | |||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | 6 | ▶ | if ($byte1 < 0xd8) { | ||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||
| 299 | 4 | $taglen = 1; | |||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||
| 301 | elsif ($byte1 == 0xd8) { | ||||||||||||||||||||||||||||||||||
| 302 | 2 | $taglen = 2; | |||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||
| 304 | elsif ($byte1 == 0xd9) { | ||||||||||||||||||||||||||||||||||
| 305 | 0 | $taglen = 3; | |||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||
| 307 | elsif ($byte1 == 0xda) { | ||||||||||||||||||||||||||||||||||
| 308 | 0 | $taglen = 5; | |||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||
| 310 | elsif ($byte1 == 0xdb) { | ||||||||||||||||||||||||||||||||||
| 311 | 0 | $taglen = 9; | |||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||
| 313 | else { | ||||||||||||||||||||||||||||||||||
| 314 | 0 | die "Invalid lead byte: $byte1"; | |||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||
| 317 | 6 | my @ret = decode( substr( $_, $taglen ) ); | |||||||||||||||||||||||||||||||||
| 318 | 6 | return( $taglen + $ret[0], $ret[1] ); | |||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | # floats, true, false, null, undefined | ||||||||||||||||||||||||||||||||||
| 322 | else { | ||||||||||||||||||||||||||||||||||
| 323 | 29 | ▶ | if ($byte1 == 0xf4) { | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 324 | 2 | require Types::Serialiser; | |||||||||||||||||||||||||||||||||
| 325 | 2 | return ( 1, Types::Serialiser::false() ); | |||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||
| 327 | elsif ($byte1 == 0xf5) { | ||||||||||||||||||||||||||||||||||
| 328 | 4 | require Types::Serialiser; | |||||||||||||||||||||||||||||||||
| 329 | 4 | return ( 1, Types::Serialiser::true() ); | |||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||
| 331 | ▶ | elsif ($byte1 == 0xf6 || $byte1 == 0xf7) { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
$byte1 == 246 or $byte1 == 247
MC/DC50%1/2
$byte1 == 246 or $byte1 == 247
$byte1 == 246
$byte1 == 247
Truth table66%2/3
A or B
A= $byte1 == 246B= $byte1 == 247
| |||||||||||||||||||||||||||||||||||
| 332 | 2 | return (1, undef); | |||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||
| 334 | elsif ($byte1 == 0xf9) { | ||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||
| 336 | # Adapted from the Python code in RFC 7049 appendix D: | ||||||||||||||||||||||||||||||||||
| 337 | 11 | my $half = unpack 'x n', $_; | |||||||||||||||||||||||||||||||||
| 338 | 11 | my $valu = (($half & 0x7fff) << 13) | (($half & 0x8000) << 16); | |||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||
| 340 | 11 | ▶ | if (($half & 0x7c00) != 0x7c00) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 341 | 8 | return( unpack('f>', pack('N', $valu)) * (2**112) ); | |||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | 3 | return ( 3, unpack('f>', pack('N', $valu | 0x7f800000)) ); | |||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||
| 346 | elsif ($byte1 == 0xfa) { | ||||||||||||||||||||||||||||||||||
| 347 | 4 | return ( 5, unpack( 'x f>', $_ ) ); | |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||
| 349 | elsif ($byte1 == 0xfb) { | ||||||||||||||||||||||||||||||||||
| 350 | 6 | return ( 5, unpack( 'x d>', $_ ) ); | |||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | 0 | die sprintf('can’t decode special value: %v.02x', $_); | |||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | sub _stringstream { | ||||||||||||||||||||||||||||||||||
| 359 | 2 | ▶ | my $full = q<>; | ||||||||||||||||||||||||||||||||
|
Subroutine
_stringstream: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||
| 361 | 2 | my $i = 1; | |||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | 2 | local $_lead3_must_be = $lead3bits; | |||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||
| 365 | 2 | while (1) { | |||||||||||||||||||||||||||||||||
| 366 | 6 | ▶ | die 'Incomplete indefinite-length string!' if $i >= length(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||
| 368 | 6 | ▶ | if ("\xff" eq substr( $_, $i, 1 )) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 369 | 2 | $i++; | |||||||||||||||||||||||||||||||||
| 370 | 2 | last; | |||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | 4 | my ($len, $chunk) = decode( substr( $_, $i ) ); | |||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | 4 | $full .= $chunk; | |||||||||||||||||||||||||||||||||
| 376 | 4 | $i += $len; | |||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | 2 | ▶ | utf8::decode($full) if $lead3bits == _LEAD3_UTF8STR(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | 2 | return ($i, $full); | |||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | sub _arraystream { | ||||||||||||||||||||||||||||||||||
| 385 | 8 | ▶ | my @full; | ||||||||||||||||||||||||||||||||
|
Subroutine
_arraystream: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||
| 387 | 8 | my $i = 1; | |||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||
| 389 | 8 | while (1) { | |||||||||||||||||||||||||||||||||
| 390 | 47 | ▶ | die 'Incomplete indefinite-length array!' if $i >= length(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | 47 | ▶ | if ("\xff" eq substr( $_, $i, 1 )) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 393 | 8 | $i++; | |||||||||||||||||||||||||||||||||
| 394 | 8 | last; | |||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||
| 397 | 39 | my ($len, $chunk) = decode( substr( $_, $i ) ); | |||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | 39 | push @full, $chunk; | |||||||||||||||||||||||||||||||||
| 400 | 39 | $i += $len; | |||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||
| 403 | 8 | return ($i, \@full); | |||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||
| 406 | sub _hashstream { | ||||||||||||||||||||||||||||||||||
| 407 | 3 | ▶ | my %full; | ||||||||||||||||||||||||||||||||
|
Subroutine
_hashstream: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | 3 | my $i = 1; | |||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||
| 411 | 3 | while (1) { | |||||||||||||||||||||||||||||||||
| 412 | 8 | ▶ | die 'Incomplete indefinite-length map!' if $i >= length(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||
| 414 | 8 | ▶ | if ("\xff" eq substr( $_, $i, 1 )) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 415 | 3 | $i++; | |||||||||||||||||||||||||||||||||
| 416 | 3 | last; | |||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||
| 419 | 5 | my ($len, $key) = decode( substr( $_, $i ) ); | |||||||||||||||||||||||||||||||||
| 420 | 5 | $i += $len; | |||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||
| 422 | 5 | ▶ | if ( "\xff" eq substr( $_, $i, 1 ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 423 | 0 | die "Odd number of elements in map! (Last key: “$key”)"; | |||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||
| 426 | 5 | ($len, $full{$key}) = decode( substr( $_, $i ) ); | |||||||||||||||||||||||||||||||||
| 427 | 5 | $i += $len; | |||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||
| 430 | 3 | return ($i, \%full); | |||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||
| 433 | 6 | 1; | |||||||||||||||||||||||||||||||||