cbor_free_decode.c

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