line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CBOR::PP::Decode; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
39
|
use strict; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
171
|
|
4
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
469
|
|
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, |
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 (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
|
|
|
|
|
|
|
my ($byte1, $offset, $lead3bits); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $len; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# This ensures that pieces of indefinite-length strings |
77
|
|
|
|
|
|
|
# are all of the same type. |
78
|
|
|
|
|
|
|
our $_lead3_must_be; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
use constant { |
81
|
6
|
|
|
|
|
9620
|
_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
|
|
|
6
|
|
34
|
}; |
|
6
|
|
|
|
|
12
|
|
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
|
|
|
404
|
1
|
29368
|
$offset = 0; |
96
|
|
|
|
|
|
|
|
97
|
404
|
|
|
|
|
631
|
for ($_[0]) { |
98
|
404
|
|
|
|
|
708
|
$byte1 = ord( substr( $_, $offset, 1 ) ); |
99
|
404
|
|
|
|
|
571
|
$lead3bits = 0xe0 & $byte1; |
100
|
|
|
|
|
|
|
|
101
|
404
|
50
|
66
|
|
|
750
|
die "Improper lead3 ($lead3bits) within streamed $_lead3_must_be!" if $_lead3_must_be && $lead3bits != $_lead3_must_be; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#use Text::Control; |
104
|
|
|
|
|
|
|
#print Text::Control::to_hex($_) . $/; |
105
|
|
|
|
|
|
|
|
106
|
404
|
100
|
|
|
|
1000
|
if ($lead3bits == _LEAD3_UINT()) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
107
|
203
|
100
|
|
|
|
507
|
return (1, ord) if $byte1 < 0x18; |
108
|
|
|
|
|
|
|
|
109
|
27
|
100
|
|
|
|
90
|
return (2, unpack('x C', $_)) if $byte1 == 0x18; |
110
|
|
|
|
|
|
|
|
111
|
13
|
100
|
|
|
|
37
|
return (3, unpack('x n', $_)) if $byte1 == 0x19; |
112
|
|
|
|
|
|
|
|
113
|
11
|
100
|
|
|
|
45
|
return (5, unpack('x N', $_)) if $byte1 == 0x1a; |
114
|
|
|
|
|
|
|
|
115
|
6
|
|
|
|
|
41
|
return (9, unpack('x Q>', $_)); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_NEGINT()) { |
119
|
5
|
100
|
|
|
|
19
|
return (1, 0x1f - ord()) if $byte1 < 0x38; |
120
|
|
|
|
|
|
|
|
121
|
2
|
100
|
|
|
|
12
|
return (2, -unpack( 'x C', $_) - 1) if $byte1 == 0x38; |
122
|
|
|
|
|
|
|
|
123
|
1
|
50
|
|
|
|
8
|
return (3, -unpack( 'x n', $_) - 1) if $byte1 == 0x39; |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
0
|
return (5, -unpack( 'x N', $_) - 1) if $byte1 == 0x3a; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
return (9, -unpack( 'x Q>', $_) - 1); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_BINSTR()) { |
131
|
59
|
|
|
|
|
81
|
my $hdrlen; |
132
|
|
|
|
|
|
|
|
133
|
59
|
100
|
|
|
|
112
|
if ($byte1 < 0x58) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
134
|
58
|
|
|
|
|
74
|
$len = $byte1 - 0x40; |
135
|
58
|
|
|
|
|
74
|
$hdrlen = 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
elsif ($byte1 == 0x58) { |
138
|
0
|
|
|
|
|
0
|
$len = unpack 'x C', $_; |
139
|
0
|
|
|
|
|
0
|
$hdrlen = 2; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
elsif ($byte1 == 0x59) { |
142
|
0
|
|
|
|
|
0
|
$len = unpack 'x n', $_; |
143
|
0
|
|
|
|
|
0
|
$hdrlen = 3; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ($byte1 == 0x5a) { |
146
|
0
|
|
|
|
|
0
|
$len = unpack 'x N', $_; |
147
|
0
|
|
|
|
|
0
|
$hdrlen = 5; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
elsif ($byte1 == 0x5b) { |
150
|
0
|
|
|
|
|
0
|
$len = unpack 'x Q>', $_; |
151
|
0
|
|
|
|
|
0
|
$hdrlen = 9; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
elsif ($byte1 == 0x5f) { |
154
|
1
|
|
|
|
|
25
|
return _stringstream(); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
else { |
157
|
0
|
|
|
|
|
0
|
die "Invalid lead byte: $byte1"; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
58
|
|
|
|
|
247
|
return ($hdrlen + $len, substr( $_, $hdrlen, $len )); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_UTF8STR()) { |
164
|
38
|
|
|
|
|
54
|
my $hdrlen; |
165
|
|
|
|
|
|
|
|
166
|
38
|
100
|
|
|
|
66
|
if ($byte1 < 0x78) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
167
|
37
|
|
|
|
|
54
|
$len = $byte1 - 0x60; |
168
|
37
|
|
|
|
|
46
|
$hdrlen = 1; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
elsif ($byte1 == 0x78) { |
171
|
0
|
|
|
|
|
0
|
$len = unpack 'x C', $_; |
172
|
0
|
|
|
|
|
0
|
$hdrlen = 2; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
elsif ($byte1 == 0x79) { |
175
|
0
|
|
|
|
|
0
|
$len = unpack 'x n', $_; |
176
|
0
|
|
|
|
|
0
|
$hdrlen = 3; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
elsif ($byte1 == 0x7a) { |
179
|
0
|
|
|
|
|
0
|
$len = unpack 'x N', $_; |
180
|
0
|
|
|
|
|
0
|
$hdrlen = 5; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
elsif ($byte1 == 0x7b) { |
183
|
0
|
|
|
|
|
0
|
$len = unpack 'x Q>', $_; |
184
|
0
|
|
|
|
|
0
|
$hdrlen = 9; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
elsif ($byte1 == 0x7f) { |
187
|
1
|
|
|
|
|
8
|
return _stringstream(); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
else { |
190
|
0
|
|
|
|
|
0
|
die "Invalid lead byte: $byte1"; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
37
|
|
|
|
|
69
|
my $v = substr( $_, $hdrlen, $len ); |
194
|
37
|
|
|
|
|
80
|
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
|
|
|
|
|
75
|
utf8::upgrade($v); |
200
|
|
|
|
|
|
|
|
201
|
37
|
|
|
|
|
112
|
return ($hdrlen + $len, $v); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_ARRAY()) { |
205
|
48
|
|
|
|
|
89
|
my $total; |
206
|
|
|
|
|
|
|
|
207
|
48
|
100
|
|
|
|
102
|
if ($byte1 < 0x98) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
208
|
37
|
|
|
|
|
52
|
$len = $byte1 - 0x80; |
209
|
37
|
|
|
|
|
47
|
$total = 1; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
elsif ($byte1 == 0x98) { |
212
|
3
|
|
|
|
|
11
|
$len = unpack 'x C', $_; |
213
|
3
|
|
|
|
|
5
|
$total = 2; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
elsif ($byte1 == 0x99) { |
216
|
0
|
|
|
|
|
0
|
$len = unpack 'x n', $_; |
217
|
0
|
|
|
|
|
0
|
$total = 3; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
elsif ($byte1 == 0x9a) { |
220
|
0
|
|
|
|
|
0
|
$len = unpack 'x N', $_; |
221
|
0
|
|
|
|
|
0
|
$total = 5; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
elsif ($byte1 == 0x9b) { |
224
|
0
|
|
|
|
|
0
|
$len = unpack 'x Q>', $_; |
225
|
0
|
|
|
|
|
0
|
$total = 9; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
elsif ($byte1 == 0x9f) { |
228
|
8
|
|
|
|
|
19
|
return _arraystream(); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
else { |
231
|
0
|
|
|
|
|
0
|
die "Invalid lead byte: $byte1"; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# pre-fill the array |
235
|
40
|
|
|
|
|
92
|
my @val = (undef) x $len; |
236
|
|
|
|
|
|
|
|
237
|
40
|
|
|
|
|
56
|
my $cur_len; |
238
|
|
|
|
|
|
|
|
239
|
40
|
|
|
|
|
84
|
for my $i ( 0 .. ($len - 1) ) { |
240
|
153
|
|
|
|
|
272
|
($cur_len, $val[$i]) = decode( substr( $_, $total ) ); |
241
|
153
|
|
|
|
|
283
|
$total += $cur_len; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
40
|
|
|
|
|
149
|
return( $total, \@val ); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_HASH()) { |
248
|
16
|
|
|
|
|
29
|
my ($len, $total); |
249
|
|
|
|
|
|
|
|
250
|
16
|
100
|
|
|
|
43
|
if ($byte1 < 0xb8) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
251
|
13
|
|
|
|
|
19
|
$len = $byte1 - 0xa0; |
252
|
13
|
|
|
|
|
21
|
$total = 1; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
elsif ($byte1 == 0xb8) { |
255
|
0
|
|
|
|
|
0
|
$len = unpack 'x C', $_; |
256
|
0
|
|
|
|
|
0
|
$total = 2; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
elsif ($byte1 == 0xb9) { |
259
|
0
|
|
|
|
|
0
|
$len = unpack 'x n', $_; |
260
|
0
|
|
|
|
|
0
|
$total = 3; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
elsif ($byte1 == 0xba) { |
263
|
0
|
|
|
|
|
0
|
$len = unpack 'x N', $_; |
264
|
0
|
|
|
|
|
0
|
$total = 5; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
elsif ($byte1 == 0xbb) { |
267
|
0
|
|
|
|
|
0
|
$len = unpack 'x Q>', $_; |
268
|
0
|
|
|
|
|
0
|
$total = 9; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
elsif ($byte1 == 0xbf) { |
271
|
3
|
|
|
|
|
7
|
return _hashstream(); |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
else { |
274
|
0
|
|
|
|
|
0
|
die "Invalid lead byte: $byte1"; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
13
|
|
|
|
|
17
|
my %val; |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
my $cur_len; |
280
|
|
|
|
|
|
|
|
281
|
13
|
|
|
|
|
28
|
while ( $len > 0 ) { |
282
|
25
|
|
|
|
|
50
|
($cur_len, my $key) = decode( substr( $_, $total ) ); |
283
|
25
|
|
|
|
|
47
|
$total += $cur_len; |
284
|
|
|
|
|
|
|
|
285
|
25
|
|
|
|
|
47
|
( $cur_len, $val{$key} ) = decode( substr( $_, $total ) ); |
286
|
25
|
|
|
|
|
46
|
$total += $cur_len; |
287
|
|
|
|
|
|
|
|
288
|
25
|
|
|
|
|
65
|
$len--; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
13
|
|
|
|
|
56
|
return( $total, \%val ); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# tags … just ignore for now |
295
|
|
|
|
|
|
|
elsif ($lead3bits == _LEAD3_TAG()) { |
296
|
6
|
|
|
|
|
10
|
my $taglen; |
297
|
|
|
|
|
|
|
|
298
|
6
|
100
|
|
|
|
15
|
if ($byte1 < 0xd8) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
299
|
4
|
|
|
|
|
6
|
$taglen = 1; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
elsif ($byte1 == 0xd8) { |
302
|
2
|
|
|
|
|
3
|
$taglen = 2; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
elsif ($byte1 == 0xd9) { |
305
|
0
|
|
|
|
|
0
|
$taglen = 3; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
elsif ($byte1 == 0xda) { |
308
|
0
|
|
|
|
|
0
|
$taglen = 5; |
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
elsif ($byte1 == 0xdb) { |
311
|
0
|
|
|
|
|
0
|
$taglen = 9; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
else { |
314
|
0
|
|
|
|
|
0
|
die "Invalid lead byte: $byte1"; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
6
|
|
|
|
|
17
|
my @ret = decode( substr( $_, $taglen ) ); |
318
|
6
|
|
|
|
|
20
|
return( $taglen + $ret[0], $ret[1] ); |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# floats, true, false, null, undefined |
322
|
|
|
|
|
|
|
else { |
323
|
29
|
100
|
66
|
|
|
147
|
if ($byte1 == 0xf4) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
324
|
2
|
|
|
|
|
12
|
require Types::Serialiser; |
325
|
2
|
|
|
|
|
7
|
return ( 1, Types::Serialiser::false() ); |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
elsif ($byte1 == 0xf5) { |
328
|
4
|
|
|
|
|
22
|
require Types::Serialiser; |
329
|
4
|
|
|
|
|
12
|
return ( 1, Types::Serialiser::true() ); |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
elsif ($byte1 == 0xf6 || $byte1 == 0xf7) { |
332
|
2
|
|
|
|
|
11
|
return (1, undef); |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
elsif ($byte1 == 0xf9) { |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# Adapted from the Python code in RFC 7049 appendix D: |
337
|
11
|
|
|
|
|
32
|
my $half = unpack 'x n', $_; |
338
|
11
|
|
|
|
|
30
|
my $valu = (($half & 0x7fff) << 13) | (($half & 0x8000) << 16); |
339
|
|
|
|
|
|
|
|
340
|
11
|
100
|
|
|
|
26
|
if (($half & 0x7c00) != 0x7c00) { |
341
|
8
|
|
|
|
|
86
|
return( unpack('f>', pack('N', $valu)) * (2**112) ); |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
3
|
|
|
|
|
27
|
return ( 3, unpack('f>', pack('N', $valu | 0x7f800000)) ); |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
elsif ($byte1 == 0xfa) { |
347
|
4
|
|
|
|
|
16
|
return ( 5, unpack( 'x f>', $_ ) ); |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
elsif ($byte1 == 0xfb) { |
350
|
6
|
|
|
|
|
24
|
return ( 5, unpack( 'x d>', $_ ) ); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
0
|
|
|
|
|
0
|
die sprintf('can’t decode special value: %v.02x', $_); |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
sub _stringstream { |
359
|
2
|
|
|
2
|
|
5
|
my $full = q<>; |
360
|
|
|
|
|
|
|
|
361
|
2
|
|
|
|
|
4
|
my $i = 1; |
362
|
|
|
|
|
|
|
|
363
|
2
|
|
|
|
|
4
|
local $_lead3_must_be = $lead3bits; |
364
|
|
|
|
|
|
|
|
365
|
2
|
|
|
|
|
3
|
while (1) { |
366
|
6
|
50
|
|
|
|
14
|
die 'Incomplete indefinite-length string!' if $i >= length(); |
367
|
|
|
|
|
|
|
|
368
|
6
|
100
|
|
|
|
17
|
if ("\xff" eq substr( $_, $i, 1 )) { |
369
|
2
|
|
|
|
|
3
|
$i++; |
370
|
2
|
|
|
|
|
4
|
last; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
4
|
|
|
|
|
10
|
my ($len, $chunk) = decode( substr( $_, $i ) ); |
374
|
|
|
|
|
|
|
|
375
|
4
|
|
|
|
|
10
|
$full .= $chunk; |
376
|
4
|
|
|
|
|
5
|
$i += $len; |
377
|
|
|
|
|
|
|
} |
378
|
|
|
|
|
|
|
|
379
|
2
|
100
|
|
|
|
6
|
utf8::decode($full) if $lead3bits == _LEAD3_UTF8STR(); |
380
|
|
|
|
|
|
|
|
381
|
2
|
|
|
|
|
20
|
return ($i, $full); |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
sub _arraystream { |
385
|
8
|
|
|
8
|
|
13
|
my @full; |
386
|
|
|
|
|
|
|
|
387
|
8
|
|
|
|
|
11
|
my $i = 1; |
388
|
|
|
|
|
|
|
|
389
|
8
|
|
|
|
|
9
|
while (1) { |
390
|
47
|
50
|
|
|
|
85
|
die 'Incomplete indefinite-length array!' if $i >= length(); |
391
|
|
|
|
|
|
|
|
392
|
47
|
100
|
|
|
|
84
|
if ("\xff" eq substr( $_, $i, 1 )) { |
393
|
8
|
|
|
|
|
11
|
$i++; |
394
|
8
|
|
|
|
|
12
|
last; |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
39
|
|
|
|
|
70
|
my ($len, $chunk) = decode( substr( $_, $i ) ); |
398
|
|
|
|
|
|
|
|
399
|
39
|
|
|
|
|
74
|
push @full, $chunk; |
400
|
39
|
|
|
|
|
47
|
$i += $len; |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
8
|
|
|
|
|
21
|
return ($i, \@full); |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
sub _hashstream { |
407
|
3
|
|
|
3
|
|
5
|
my %full; |
408
|
|
|
|
|
|
|
|
409
|
3
|
|
|
|
|
5
|
my $i = 1; |
410
|
|
|
|
|
|
|
|
411
|
3
|
|
|
|
|
4
|
while (1) { |
412
|
8
|
50
|
|
|
|
19
|
die 'Incomplete indefinite-length map!' if $i >= length(); |
413
|
|
|
|
|
|
|
|
414
|
8
|
100
|
|
|
|
18
|
if ("\xff" eq substr( $_, $i, 1 )) { |
415
|
3
|
|
|
|
|
6
|
$i++; |
416
|
3
|
|
|
|
|
4
|
last; |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
5
|
|
|
|
|
10
|
my ($len, $key) = decode( substr( $_, $i ) ); |
420
|
5
|
|
|
|
|
11
|
$i += $len; |
421
|
|
|
|
|
|
|
|
422
|
5
|
50
|
|
|
|
13
|
if ( "\xff" eq substr( $_, $i, 1 ) ) { |
423
|
0
|
|
|
|
|
0
|
die "Odd number of elements in map! (Last key: “$key”)"; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
5
|
|
|
|
|
12
|
($len, $full{$key}) = decode( substr( $_, $i ) ); |
427
|
5
|
|
|
|
|
14
|
$i += $len; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
3
|
|
|
|
|
9
|
return ($i, \%full); |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
1; |