cbor_free_decode.c

filter: Statementstmt 94.8% 350 / 369 Branchbran 85.7% 96 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.7% 446 / 481 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cbor_free_encode.c ↑ Summary cbor_free_boolean.c »
1 #define PERL_NO_GET_CONTEXT
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #include "cbor_free_common.h"
8 #include "cbor_free_decode.h"
9  
10 #include <stdlib.h>
11  
12 // For ntohs and ntohl
13 #include <arpa/inet.h>
14  
15 #define _DECODE_CHECK_FOR_OVERAGE( decstate, len) \
16 if ((len + decstate->curbyte) > decstate->end) { \
17 _croak_incomplete( aTHX_ decstate, (len + decstate->curbyte) - decstate->end ); \
18 }
19  
20 //----------------------------------------------------------------------
21  
22 // Basically ntohll(), but it accepts a pointer.
23 19 static inline UV _buffer_u64_to_uv( unsigned char *buffer ) {
24 19 UV num = 0;
25  
26 #if IS_64_BIT
27 19 num |= *(buffer++);
28 19 num <<= 8;
29  
30 19 num |= *(buffer++);
31 19 num <<= 8;
32  
33 19 num |= *(buffer++);
34 19 num <<= 8;
35  
36 19 num |= *(buffer++);
37 19 num <<= 8;
38 #else
39 buffer += 4;
40 #endif
41  
42 19 num |= *(buffer++);
43 19 num <<= 8;
44  
45 19 num |= *(buffer++);
46 19 num <<= 8;
47  
48 19 num |= *(buffer++);
49 19 num <<= 8;
50  
51 19 num |= *(buffer++);
52  
53 19 return num;
54 }
55  
56 const char *MAJOR_TYPE_DESCRIPTION[] = {
57 "unsigned integer",
58 "negative integer",
59 "byte string",
60 "text string",
61 "array",
62 "map",
63 "tag",
64 "miscellaneous",
65 };
66  
67 //----------------------------------------------------------------------
68  
69 void _free_decode_state(decode_ctx* decode_state);
70  
71 //----------------------------------------------------------------------
72  
73 102 SV *_call_with_arguments( pTHX_ SV* cb, U8 count, SV** args ) {
74 // --- Almost all copy-paste from “perlcall” … blegh!
75 102 dSP;
76  
77 102 ENTER;
78 102 SAVETMPS;
79  
80 102 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 102 102
81 102 ▶ EXTEND(SP, count);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, count); 0 102 102
82  
83 U8 i;
84 206 ▶ for (i=0; i<count; i++) PUSHs( sv_2mortal(args[i]) );
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<count; i++) PUSHs( sv_2mortal(args[i]) ); 104 102 206
85  
86 102 PUTBACK;
87  
88 102 call_sv(cb, G_SCALAR);
89  
90 100 SV *ret = newSVsv(POPs);
91  
92 100 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 100 0 100
93 100 LEAVE;
94  
95 100 return ret;
96 }
97  
98 //----------------------------------------------------------------------
99 // Croakers
100  
101 static const char* UV_TO_STR_TMPL = (sizeof(UV) == 8 ? "%llu" : "%lu");
102 static const char* IV_TO_STR_TMPL = (sizeof(UV) == 8 ? "%lld" : "%ld");
103  
104 254 UV _uv_to_str(UV num, char *numstr, const char strlen) {
105 254 return my_snprintf( numstr, strlen, UV_TO_STR_TMPL, num );
106 }
107  
108 2 UV _iv_to_str(IV num, char *numstr, const char strlen) {
109 2 return my_snprintf( numstr, strlen, IV_TO_STR_TMPL, num );
110 }
111  
112 124 void _croak_incomplete( pTHX_ decode_ctx* decstate, STRLEN lack ) {
113 124 _free_decode_state(decstate);
114  
115 char lackstr[24];
116 124 _uv_to_str( lack, lackstr, sizeof(lackstr) );
117  
118 124 char * words[3] = { "Incomplete", lackstr, NULL };
119  
120 124 _die( G_DISCARD, words );
121 }
122  
123 49 void _croak_invalid_control( pTHX_ decode_ctx* decstate ) {
124 49 const uint8_t ord = (uint8_t) *(decstate->curbyte);
125 49 STRLEN offset = decstate->curbyte - decstate->start;
126  
127 char ordstr[24];
128 char offsetstr[24];
129  
130 49 _uv_to_str(ord, ordstr, sizeof(ordstr));
131 49 _uv_to_str(offset, offsetstr, sizeof(offsetstr));
132  
133 49 char * words[] = { "InvalidControl", ordstr, offsetstr, NULL };
134  
135 49 _free_decode_state(decstate);
136  
137 49 _die( G_DISCARD, words );
138 }
139  
140 2 void _croak_invalid_utf8( pTHX_ decode_ctx* decstate, char *string, STRLEN len ) {
141 2 _free_decode_state(decstate);
142  
143 4 SV* args[2] = {
144 2 newSVpvs("InvalidUTF8"),
145 2 newSVpvn(string, len),
146 };
147  
148 2 _call_with_arguments(
149 aTHX_
150 newSVpvs("CBOR::Free::_die"),
151 2,
152 args
153 );
154  
155 assert(0);
156 0 }
157  
158 7 void _croak_invalid_map_key( pTHX_ decode_ctx* decstate, const uint8_t byte, STRLEN offset ) {
159 7 _free_decode_state(decstate);
160  
161 char bytebuf[5];
162  
163 char *bytestr;
164  
165 7 switch (byte) {
166 2 case CBOR_FALSE:
167 2 bytestr = "false";
168 2 break;
169 1 case CBOR_TRUE:
170 1 bytestr = "true";
171 1 break;
172 1 case CBOR_NULL:
173 1 bytestr = "null";
174 1 break;
175 1 case CBOR_UNDEFINED:
176 1 bytestr = "undefined";
177 1 break;
178 2 default:
179 2 switch ((byte & 0xe0) >> 5) {
180 1 case CBOR_TYPE_ARRAY:
181 1 bytestr = "array";
182 1 break;
183 1 case CBOR_TYPE_MAP:
184 1 bytestr = "map";
185 1 break;
186 0 default:
187 0 my_snprintf( bytebuf, 5, "0x%02x", byte );
188 0 bytestr = bytebuf;
189 }
190 }
191  
192 char offsetstr[20];
193 7 _uv_to_str( offset, offsetstr, sizeof(offsetstr) );
194  
195 7 char * words[] = { "InvalidMapKey", bytestr, offsetstr, NULL };
196  
197 7 _die( G_DISCARD, words);
198 }
199  
200 0 void _croak_cannot_decode_64bit( pTHX_ decode_ctx* decstate, const uint8_t *u64bytes, STRLEN offset ) {
201 0 _free_decode_state(decstate);
202  
203 char numhex[20];
204 0 numhex[19] = 0;
205  
206 0 my_snprintf( numhex, sizeof(numhex), "%02x%02x_%02x%02x_%02x%02x_%02x%02x", u64bytes[0], u64bytes[1], u64bytes[2], u64bytes[3], u64bytes[4], u64bytes[5], u64bytes[6], u64bytes[7] );
207  
208 char offsetstr[20];
209 0 _uv_to_str( offset, offsetstr, sizeof(offsetstr) );
210  
211 0 char * words[] = { "CannotDecode64Bit", numhex, offsetstr, NULL };
212  
213 0 _die( G_DISCARD, words );
214 }
215  
216 1 void _croak_cannot_decode_negative( pTHX_ decode_ctx* decstate, UV abs, STRLEN offset ) {
217 1 _free_decode_state(decstate);
218  
219 char absstr[40];
220 1 _uv_to_str( abs, absstr, sizeof(absstr) );
221  
222 char offsetstr[20];
223 1 _uv_to_str( offset, offsetstr, sizeof(offsetstr) );
224  
225 1 char * words[] = { "NegativeIntTooLow", absstr, offsetstr, NULL };
226  
227 1 _die( G_DISCARD, words );
228 }
229  
230 8 void _warn_unhandled_tag( pTHX_ UV tagnum, U8 value_major_type ) {
231 char tmpl[255];
232 8 my_snprintf( tmpl, sizeof(tmpl), "Ignoring unrecognized CBOR tag #%s (major type %%u, %%s)!", UV_TO_STR_TMPL );
233  
234 8 warn(tmpl, tagnum, value_major_type, MAJOR_TYPE_DESCRIPTION[value_major_type]);
235 8 }
236  
237 //----------------------------------------------------------------------
238  
239 57 static inline void _validate_utf8_string_if_needed( pTHX_ decode_ctx* decstate, char *buffer, STRLEN len ) {
240  
241 57 ▶ if (!decstate->naive_utf8 && !is_utf8_string( (U8 *)buffer, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!decstate->naive_utf8 && !is_utf8_string( (U8 *)buffer, len)) { 55 2 57
if (!decstate->naive_utf8 && !is_utf8_string( (U8 *)buffer, len)) { 2 53 55
242 2 _croak_invalid_utf8( aTHX_ decstate, buffer, len );
243 }
244 55 }
245  
246 //----------------------------------------------------------------------
247 // DECODER:
248 //----------------------------------------------------------------------
249  
250 // Needed because of mutual recursion of functions.
251 SV *_decode( pTHX_ decode_ctx* decstate );
252  
253 1163 static inline UV _parse_for_uint_len2( pTHX_ decode_ctx* decstate ) {
254 1163 union control_byte *control = (union control_byte *) decstate->curbyte;
255  
256 UV ret;
257  
258 1163 switch (control->pieces.length_type) {
259 149 case CBOR_LENGTH_SMALL:
260  
261 149 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 2);
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 2); 6 143 149
262  
263 143 ++decstate->curbyte;
264  
265 143 ret = (uint8_t) decstate->curbyte[0];
266  
267 143 ++decstate->curbyte;
268  
269 143 break;
270  
271 39 case CBOR_LENGTH_MEDIUM:
272 39 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 3);
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 3); 10 29 39
273  
274 29 ++decstate->curbyte;
275  
276 29 ret = ntohs( *((uint16_t *) decstate->curbyte) );
277  
278 29 decstate->curbyte += 2;
279  
280 29 break;
281  
282 142 case CBOR_LENGTH_LARGE:
283 142 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 5);
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 5); 20 122 142
284  
285 122 ++decstate->curbyte;
286  
287 122 ret = ntohl( *((uint32_t *) decstate->curbyte) );
288  
289 122 decstate->curbyte += 4;
290  
291 122 break;
292  
293 59 case CBOR_LENGTH_HUGE:
294 59 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 9);
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 9); 40 19 59
295  
296 19 ++decstate->curbyte;
297  
298 #if !IS_64_BIT
299  
300 if (decstate->curbyte[0] || decstate->curbyte[1] || decstate->curbyte[2] || decstate->curbyte[3]) {
301 _croak_cannot_decode_64bit( aTHX_ decstate, (const uint8_t *) decstate->curbyte, decstate->curbyte - decstate->start );
302 }
303 #endif
304 19 ret = _buffer_u64_to_uv( (uint8_t *) decstate->curbyte );
305  
306 19 decstate->curbyte += 8;
307  
308 19 break;
309  
310 21 case 0x1c:
311 case 0x1d:
312 case 0x1e:
313 case 0x1f: // indefinite must be handled outside this function.
314 21 _croak_invalid_control( aTHX_ decstate );
315 0 break;
316  
317 753 default:
318 753 ret = (uint8_t) control->pieces.length_type;
319  
320 753 decstate->curbyte++;
321 }
322  
323 1066 return ret;
324 }
325  
326 //----------------------------------------------------------------------
327  
328 120 SV *_decode_array( pTHX_ decode_ctx* decstate ) {
329 120 union control_byte *control = (union control_byte *) decstate->curbyte;
330  
331 120 AV *array = newAV();
332 120 SV *cur = NULL;
333  
334 120 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 10 110 120
335 10 ++decstate->curbyte;
336  
337 50 ▶ while (*(decstate->curbyte) != '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
while (*(decstate->curbyte) != '\xff') { 40 10 50
338  
339 40 cur = _decode( aTHX_ decstate );
340 40 av_push(array, cur);
341 //sv_2mortal(cur);
342 }
343  
344 10 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 1 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 1 ); 0 10 10
345  
346 10 ++decstate->curbyte;
347 }
348 else {
349 110 SSize_t array_length = _parse_for_uint_len2( aTHX_ decstate );
350  
351 107 ▶ if (array_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_length) { 98 9 107
352 98 av_fill(array, array_length - 1);
353  
354 SSize_t i;
355 534 ▶ for (i=0; i<array_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<array_length; i++) { 488 46 534
356 488 cur = _decode( aTHX_ decstate );
357  
358 436 ▶ if (!av_store(array, i, cur)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(array, i, cur)) { 0 436 436
359 0 _croak("Failed to store item in array!");
360 }
361 }
362 }
363 }
364  
365 65 return newRV_noinc( (SV *) array);
366 }
367  
368 641 UV _decode_uint( pTHX_ decode_ctx* decstate ) {
369 641 union control_byte *control = (union control_byte *) decstate->curbyte;
370  
371 641 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 1 640 641
372 1 _croak_invalid_control( aTHX_ decstate );
373 }
374  
375 640 return _parse_for_uint_len2( aTHX_ decstate );
376 }
377  
378 45 IV _decode_negint( pTHX_ decode_ctx* decstate ) {
379 45 union control_byte *control = (union control_byte *) decstate->curbyte;
380  
381 45 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 1 44 45
382 1 _croak_invalid_control( aTHX_ decstate );
383 }
384  
385 44 UV positive = _parse_for_uint_len2( aTHX_ decstate );
386  
387 #if IS_64_BIT
388 26 ▶ if (positive >= 0x8000000000000000U) {
Branch: 1 branch
BranchTrueFalseTotal
if (positive >= 0x8000000000000000U) { 1 25 26
389 1 _croak_cannot_decode_negative( aTHX_ decstate, positive, decstate->curbyte - decstate->start - 8 );
390 }
391 #else
392 if (positive >= 0x80000000U) {
393 STRLEN offset = decstate->curbyte - decstate->start;
394  
395 if (control->pieces.length_type == 0x1a) {
396 offset -= 4;
397 }
398 else {
399 offset -= 8;
400 }
401  
402 _croak_cannot_decode_negative( aTHX_ decstate, positive, offset );
403 }
404 #endif
405  
406 25 return( -1 - (int64_t) positive );
407 }
408  
409 186 struct numbuf _decode_str( pTHX_ decode_ctx* decstate ) {
410 186 union control_byte *control = (union control_byte *) decstate->curbyte;
411  
412 struct numbuf ret;
413  
414 186 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 8 178 186
415 8 ++decstate->curbyte;
416  
417 //TODO: Parse it as a string, not an SV.
418 8 SV *tempsv = newSVpvs("");
419  
420 19 ▶ while (*(decstate->curbyte) != '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
while (*(decstate->curbyte) != '\xff') { 13 6 19
421 //TODO: Require the same major type.
422  
423 13 SV *cur = _decode( aTHX_ decstate );
424  
425 11 sv_catsv(tempsv, cur);
426 }
427  
428 6 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 1 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 1 ); 0 6 6
429  
430 6 ++decstate->curbyte;
431  
432 6 ret.buffer = SvPV_nolen(tempsv);
433 6 ret.num.uv = SvCUR(tempsv);
434  
435 6 return ret;
436 }
437  
438 178 ret.num.uv = _parse_for_uint_len2( aTHX_ decstate );
439  
440 142 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, ret.num.uv );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, ret.num.uv ); 22 120 142
441  
442 120 ret.buffer = decstate->curbyte;
443  
444 120 decstate->curbyte += ret.num.uv;
445  
446 120 return ret;
447 }
448  
449 72 void _decode_hash_entry( pTHX_ decode_ctx* decstate, HV *hash ) {
450 72 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 1 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 1 ); 2 70 72
451  
452 70 union control_byte *control = (union control_byte *) decstate->curbyte;
453  
454 struct numbuf my_key;
455 70 my_key.buffer = NULL;
456  
457 // This is going to be a hash key, so it can’t usefully be
458 // anything but a string/PV.
459 I32 keylen;
460 char *keystr;
461  
462 70 switch (control->pieces.major_type) {
463 23 case CBOR_TYPE_UINT:
464 23 my_key.num.uv = _decode_uint( aTHX_ decstate );
465  
466 22 keystr = (char *) decstate->scratch.bytes;
467 22 keylen = _uv_to_str( my_key.num.uv, keystr, sizeof(decstate->scratch.bytes));
468  
469 22 break;
470  
471 2 case CBOR_TYPE_NEGINT:
472 2 my_key.num.iv = _decode_negint( aTHX_ decstate );
473  
474 2 keystr = (char *) decstate->scratch.bytes;
475 2 keylen = _iv_to_str( my_key.num.iv, keystr, sizeof(decstate->scratch.bytes));
476  
477 2 break;
478  
479 38 case CBOR_TYPE_BINARY:
480 case CBOR_TYPE_UTF8:
481 38 my_key = _decode_str( aTHX_ decstate );
482  
483 38 ▶ if (my_key.num.uv > 0x7fffffffU) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_key.num.uv > 0x7fffffffU) { 0 38 38
484 0 _croak("key too long!");
485 }
486  
487 38 keystr = my_key.buffer;
488  
489 38 ▶ if (control->pieces.major_type == CBOR_TYPE_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.major_type == CBOR_TYPE_UTF8) { 18 20 38
490 18 _validate_utf8_string_if_needed( aTHX_ decstate, keystr, my_key.num.uv );
491  
492 17 keylen = -my_key.num.uv;
493 }
494 else {
495 20 keylen = my_key.num.uv;
496 }
497  
498 37 break;
499  
500 7 default:
501 7 _croak_invalid_map_key( aTHX_ decstate, decstate->curbyte[0], decstate->curbyte - decstate->start );
502 }
503  
504 61 SV *curval = _decode( aTHX_ decstate );
505  
506 59 hv_store(hash, keystr, keylen, curval, 0);
507 59 }
508  
509 45 SV *_decode_map( pTHX_ decode_ctx* decstate ) {
510 45 union control_byte *control = (union control_byte *) decstate->curbyte;
511  
512 45 HV *hash = newHV();
513  
514 45 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 4 41 45
515 4 ++decstate->curbyte;
516  
517 9 ▶ while (decstate->curbyte[0] != '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
while (decstate->curbyte[0] != '\xff') { 5 4 9
518 5 _decode_hash_entry( aTHX_ decstate, hash );
519 }
520  
521 4 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 1 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 1 ); 0 4 4
522  
523 4 ++decstate->curbyte;
524 }
525 else {
526 41 SSize_t keycount = _parse_for_uint_len2( aTHX_ decstate );
527  
528 38 ▶ if (keycount) {
Branch: 1 branch
BranchTrueFalseTotal
if (keycount) { 33 5 38
529 87 ▶ while (keycount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (keycount > 0) { 67 20 87
530 67 _decode_hash_entry( aTHX_ decstate, hash );
531 54 --keycount;
532 }
533 }
534 }
535  
536 29 return newRV_noinc( (SV *) hash);
537 }
538  
539 //----------------------------------------------------------------------
540  
541 // Taken from RFC 7049:
542 7 double decode_half_float(uint8_t *halfp) {
543 7 int half = (halfp[0] << 8) + halfp[1];
544 7 int exp = (half >> 10) & 0x1f;
545 7 int mant = half & 0x3ff;
546 double val;
547 7 ▶ if (exp == 0) val = ldexp(mant, -24);
Branch: 1 branch
BranchTrueFalseTotal
if (exp == 0) val = ldexp(mant, -24); 0 7 7
548 7 ▶ else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
Branch: 1 branch
BranchTrueFalseTotal
else if (exp != 31) val = ldexp(mant + 1024, exp - 25); 1 6 7
549 6 ▶ else val = mant == 0 ? INFINITY : NAN;
Branch: 1 branch
BranchTrueFalseTotal
else val = mant == 0 ? INFINITY : NAN; 4 2 6
550 7 ▶ return half & 0x8000 ? -val : val;
Branch: 1 branch
BranchTrueFalseTotal
return half & 0x8000 ? -val : val; 2 5 7
551 }
552  
553 24 static inline float _decode_float_to_host( pTHX_ decode_ctx* decstate, uint8_t *ptr ) {
554 24 *((uint32_t *) decstate->scratch.bytes) = ntohl( *((uint32_t *) ptr) );
555  
556 24 return decstate->scratch.as_float;
557 }
558  
559 62 static inline double _decode_double_to_le( decode_ctx* decstate, uint8_t *ptr ) {
560 62 decstate->scratch.bytes[0] = ptr[7];
561 62 decstate->scratch.bytes[1] = ptr[6];
562 62 decstate->scratch.bytes[2] = ptr[5];
563 62 decstate->scratch.bytes[3] = ptr[4];
564 62 decstate->scratch.bytes[4] = ptr[3];
565 62 decstate->scratch.bytes[5] = ptr[2];
566 62 decstate->scratch.bytes[6] = ptr[1];
567 62 decstate->scratch.bytes[7] = ptr[0];
568  
569 62 return decstate->scratch.as_double;
570 }
571  
572 //----------------------------------------------------------------------
573  
574 148 static inline SV *_decode_str_to_sv( pTHX_ decode_ctx* decstate ) {
575 148 struct numbuf decoded_str = _decode_str( aTHX_ decstate );
576  
577 88 return newSVpvn( decoded_str.buffer, decoded_str.num.uv );
578 }
579  
580 1279 SV *_decode( pTHX_ decode_ctx* decstate ) {
581 1279 SV *ret = NULL;
582  
583 1279 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 1);
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 1); 10 1269 1279
584  
585 1269 union control_byte *control = (union control_byte *) decstate->curbyte;
586  
587 1269 switch (control->pieces.major_type) {
588 618 case CBOR_TYPE_UINT:
589 618 ret = newSVuv( _decode_uint( aTHX_ decstate ) );
590  
591 599 break;
592 43 case CBOR_TYPE_NEGINT:
593 43 ret = newSViv( _decode_negint( aTHX_ decstate ) );
594  
595 23 break;
596 148 case CBOR_TYPE_BINARY:
597 case CBOR_TYPE_UTF8:
598 148 ret = _decode_str_to_sv( aTHX_ decstate );
599  
600 88 ▶ if (CBOR_TYPE_UTF8 == control->pieces.major_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (CBOR_TYPE_UTF8 == control->pieces.major_type) { 39 49 88
601 78 _validate_utf8_string_if_needed( aTHX_ decstate, SvPV_nolen(ret), SvCUR(ret));
602  
603 // Always set the UTF8 flag, even if it’s not needed.
604 // This helps ensure that text strings will round-trip
605 // through Perl.
606 38 SvUTF8_on(ret);
607 }
608  
609 87 break;
610 120 case CBOR_TYPE_ARRAY:
611 120 ret = _decode_array( aTHX_ decstate );
612  
613 65 break;
614 45 case CBOR_TYPE_MAP:
615 45 ret = _decode_map( aTHX_ decstate );
616  
617 29 break;
618 148 case CBOR_TYPE_TAG:
619  
620 148 ▶ if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (control->pieces.length_type == CBOR_LENGTH_INDEFINITE) { 1 147 148
621 1 _croak_invalid_control( aTHX_ decstate );
622 }
623  
624 147 UV tagnum = _parse_for_uint_len2( aTHX_ decstate );
625  
626 129 U8 value_major_type = ((union control_byte *) decstate->curbyte)->pieces.major_type;
627  
628 132 ▶ if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) {
Branch: 2 branches
BranchTrueFalseTotal
if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) { 3 126 129
if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) { 3 0 3
629 3 ▶ if (value_major_type != CBOR_TYPE_UINT) {
Branch: 1 branch
BranchTrueFalseTotal
if (value_major_type != CBOR_TYPE_UINT) { 0 3 3
630 char tmpl[255];
631 0 my_snprintf( tmpl, sizeof(tmpl), "Shared ref type must be uint, not %%u (%%s)!" );
632 0 croak(tmpl, value_major_type, MAJOR_TYPE_DESCRIPTION[value_major_type]);
633 }
634  
635 3 UV refnum = _parse_for_uint_len2( aTHX_ decstate );
636  
637 3 ▶ if (refnum >= decstate->reflistlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (refnum >= decstate->reflistlen) { 0 3 3
638 0 _croak("Missing shareable!");
639 }
640  
641 3 ret = decstate->reflist[refnum];
642 3 SvREFCNT_inc(ret);
643 }
644 else {
645 126 ret = _decode( aTHX_ decstate );
646  
647 121 ▶ if (tagnum == CBOR_TAG_INDIRECTION) {
Branch: 1 branch
BranchTrueFalseTotal
if (tagnum == CBOR_TAG_INDIRECTION) { 10 111 121
648 10 ret = newRV_inc(ret);
649 }
650 111 ▶ else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) {
Branch: 2 branches
BranchTrueFalseTotal
else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) { 3 108 111
else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) { 3 0 3
651 3 ++decstate->reflistlen;
652 3 ▶ Renew( decstate->reflist, decstate->reflistlen, void * );
Branch: 1 branch
BranchTrueFalseTotal
Renew( decstate->reflist, decstate->reflistlen, void * ); 0 3 3
653  
654 3 decstate->reflist[ decstate->reflistlen - 1 ] = (SV *) ret;
655 }
656  
657 108 ▶ else if (decstate->tag_handler) {
Branch: 1 branch
BranchTrueFalseTotal
else if (decstate->tag_handler) { 101 7 108
658 101 HV *my_tag_handler = decstate->tag_handler;
659  
660 101 SV **handler_cr = hv_fetch( my_tag_handler, (char *) &tagnum, sizeof(UV), 0 );
661  
662 101 ▶ if (handler_cr && *handler_cr && SvOK(*handler_cr)) {
Branch: 3 branches
BranchTrueFalseTotal
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 100 1 101
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 100 0 100
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 100 0 100
663 100 ret = _call_with_arguments( aTHX_ *handler_cr, 1, &ret );
664 }
665 else {
666 1 _warn_unhandled_tag( aTHX_ tagnum, value_major_type );
667 }
668 }
669 else {
670 7 _warn_unhandled_tag( aTHX_ tagnum, value_major_type );
671 }
672 }
673  
674 124 break;
675 147 case CBOR_TYPE_OTHER:
676 147 switch (control->u8) {
677 3 case CBOR_FALSE:
678 3 ret = newSVsv( cbf_get_false() );
679 3 ++decstate->curbyte;
680 3 break;
681  
682 6 case CBOR_TRUE:
683 6 ret = newSVsv( cbf_get_true() );
684 6 ++decstate->curbyte;
685 6 break;
686  
687 6 case CBOR_NULL:
688 case CBOR_UNDEFINED:
689 6 ret = newSVsv( &PL_sv_undef );
690 6 ++decstate->curbyte;
691 6 break;
692  
693 9 case CBOR_HALF_FLOAT:
694 9 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 3 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 3 ); 2 7 9
695  
696 7 ret = newSVnv( decode_half_float( (uint8_t *) (1 + decstate->curbyte) ) );
697  
698 7 decstate->curbyte += 3;
699 7 break;
700  
701 28 case CBOR_FLOAT:
702 28 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 5 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 5 ); 4 24 28
703  
704 float decoded_flt;
705  
706 #if IS_LITTLE_ENDIAN
707 24 decoded_flt = _decode_float_to_host( aTHX_ decstate, (uint8_t *) (1 + decstate->curbyte ) );
708 #else
709 decoded_flt = *( (float *) (1 + decstate->curbyte) );
710 #endif
711  
712 24 ret = newSVnv( (NV) decoded_flt );
713  
714 24 decstate->curbyte += 5;
715 24 break;
716  
717 70 case CBOR_DOUBLE:
718 70 ▶ _DECODE_CHECK_FOR_OVERAGE( decstate, 9 );
Branch: 1 branch
BranchTrueFalseTotal
_DECODE_CHECK_FOR_OVERAGE( decstate, 9 ); 8 62 70
719  
720 double decoded_dbl;
721  
722 #if IS_LITTLE_ENDIAN
723 62 decoded_dbl = _decode_double_to_le( decstate, (uint8_t *) (1 + decstate->curbyte ) );
724 #else
725 decoded_dbl = *( (double *) (1 + decstate->curbyte) );
726 #endif
727  
728 62 ret = newSVnv( (NV) decoded_dbl );
729  
730 62 decstate->curbyte += 9;
731 62 break;
732  
733 25 default:
734 25 _croak_invalid_control( aTHX_ decstate );
735 }
736  
737 108 break;
738  
739 0 default:
740 0 _croak("Unknown type!");
741 }
742  
743 1035 return ret;
744 }
745  
746 551 decode_ctx _create_decode_state( pTHX_ SV *cbor, HV *tag_handler, UV flags ) {
747 STRLEN cborlen;
748  
749 551 char *cborstr = SvPV(cbor, cborlen);
750  
751 551 decode_ctx decode_state = {
752 cborstr, // start
753 cborlen, // size
754 cborstr, // curbyte
755 551 cborstr + cborlen, // end
756 tag_handler,
757 NULL, // reflist
758 0, // reflistlen
759 551 !!(flags & CBF_FLAG_NAIVE_UTF8),
760 };
761  
762 551 ▶ if (flags & CBF_FLAG_PRESERVE_REFERENCES) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & CBF_FLAG_PRESERVE_REFERENCES) { 1 550 551
763 1 Newx( decode_state.reflist, 0, void * );
764 }
765  
766 551 return decode_state;
767 }
768  
769 551 void _free_decode_state(decode_ctx* decode_state) {
770 551 ▶ if (decode_state->reflist) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->reflist) { 1 550 551
771 1 Safefree(decode_state->reflist);
772 }
773 551 }
774  
775 551 SV *cbf_decode( pTHX_ SV *cbor, HV *tag_handler, UV flags ) {
776  
777 551 decode_ctx decode_state = _create_decode_state( aTHX_ cbor, tag_handler, flags);
778  
779 551 SV *RETVAL = _decode( aTHX_ &decode_state );
780  
781 368 _free_decode_state(&decode_state);
782  
783 368 ▶ if (decode_state.curbyte != decode_state.end) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state.curbyte != decode_state.end) { 1 367 368
784 1 STRLEN bytes_count = decode_state.end - decode_state.curbyte;
785  
786 char numstr[24];
787 1 _uv_to_str(bytes_count, numstr, 24);
788  
789 1 char * words[2] = { numstr, NULL };
790  
791 1 call_argv("CBOR::Free::_warn_decode_leftover", G_DISCARD, words);
792 }
793  
794 368 return RETVAL;
795 }
« cbor_free_encode.c ↑ Summary cbor_free_boolean.c »