cbor_free_decode.c

filter: Statementstmt 92.9% 433 / 466 Branchbran 93.0% 175 / 188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.9% 608 / 654 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cbor_free_encode.c ↑ Summary cbor_free_boolean.c »
1 #define NO_XSLOCKS
2 #include "easyxs/init.h"
3  
4 #include "cbor_free_common.h"
5 #include "cbor_free_decode.h"
6  
7 #include <stdlib.h>
8 #include <stdbool.h>
9  
10 // For ntohs and ntohl
11 #include <arpa/inet.h>
12  
13 #define _IS_INCOMPLETE(decstate, len) \
14 (len > (decstate->end - decstate->curbyte))
15  
16 #define _SET_INCOMPLETE(decstate, len) \
17 decstate->incomplete_by = len - (decstate->end - decstate->curbyte);
18  
19 #define _RETURN_IF_INCOMPLETE( decstate, len, toreturn ) \
20 if (_IS_INCOMPLETE(decstate, len)) { \
21 _SET_INCOMPLETE(decstate, len); \
22 return toreturn; \
23 }
24  
25 #define _RETURN_IF_SET_INCOMPLETE(decstate, toreturn) \
26 if (decstate->incomplete_by) return toreturn;
27  
28 #define SHOULD_VALIDATE_UTF8(decstate, major_type) \
29 major_type == CBOR_TYPE_UTF8 \
30 || decstate->string_decode_mode == CBF_STRING_DECODE_ALWAYS
31  
32 //----------------------------------------------------------------------
33  
34 // Basically ntohll(), but it accepts a pointer.
35 19 static inline UV _buffer_u64_to_uv( unsigned char *buffer ) {
36 #ifdef CBF_64BIT_INET
37 return ntohll( *( (uint64_t*) buffer ) );
38 #else
39  
40 return (
41 #if IS_64_BIT
42 38 ( ((UV) ntohl( *( (uint32_t*) buffer ) )) << 32 ) +
43 #endif
44 19 ntohl( *( (uint32_t*) buffer + 1 ) )
45 );
46 #endif
47 }
48  
49 const char *MAJOR_TYPE_DESCRIPTION[] = {
50 "unsigned integer",
51 "negative integer",
52 "byte string",
53 "text string",
54 "array",
55 "map",
56 "tag",
57 "miscellaneous",
58 };
59  
60 //----------------------------------------------------------------------
61 // Croakers
62  
63 static const char* UV_TO_STR_TMPL = (sizeof(UV) == 8 ? "%llu" : "%lu");
64 static const char* IV_TO_STR_TMPL = (sizeof(UV) == 8 ? "%lld" : "%ld");
65  
66 26 UV _uv_to_str(UV num, char *numstr, const char strlen) {
67 26 return my_snprintf( numstr, strlen, UV_TO_STR_TMPL, num );
68 }
69  
70 2 UV _iv_to_str(IV num, char *numstr, const char strlen) {
71 2 return my_snprintf( numstr, strlen, IV_TO_STR_TMPL, num );
72 }
73  
74 214 void _free_decode_state_if_not_persistent( pTHX_ decode_ctx* decstate ) {
75 214 ▶ if (!(decstate->flags & CBF_FLAG_PERSIST_STATE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(decstate->flags & CBF_FLAG_PERSIST_STATE)) { 195 19 214
76 195 free_decode_state(aTHX_ decstate);
77 }
78 214 }
79  
80 146 static inline void _croak_incomplete( pTHX_ decode_ctx* decstate ) {
81  
82 292 SV* args[2] = {
83 146 newSVpvs("Incomplete"),
84 146 newSVuv(decstate->incomplete_by),
85 };
86  
87 146 _free_decode_state_if_not_persistent(aTHX_ decstate);
88  
89 146 cbf_die_with_arguments( aTHX_ 2, args );
90 0 }
91  
92 49 static inline void _croak_invalid_control( pTHX_ decode_ctx* decstate ) {
93 49 const uint8_t ord = (uint8_t) *(decstate->curbyte);
94 49 UV offset = decstate->curbyte - decstate->start;
95  
96 49 _free_decode_state_if_not_persistent(aTHX_ decstate);
97  
98 147 SV* args[3] = {
99 49 newSVpvs("InvalidControl"),
100 49 newSVuv(ord),
101 49 newSVuv(offset),
102 };
103  
104 49 cbf_die_with_arguments( aTHX_ 3, args );
105  
106 assert(0);
107 0 }
108  
109 2 void _croak_invalid_utf8( pTHX_ decode_ctx* decstate, char *string, STRLEN len ) {
110 2 _free_decode_state_if_not_persistent(aTHX_ decstate);
111  
112 4 SV* args[2] = {
113 2 newSVpvs("InvalidUTF8"),
114 2 newSVpvn(string, len),
115 };
116  
117 2 cbf_die_with_arguments( aTHX_ 2, args );
118  
119 assert(0);
120 0 }
121  
122 8 void _croak_invalid_map_key( pTHX_ decode_ctx* decstate ) {
123 8 const uint8_t byte = decstate->curbyte[0];
124 8 UV offset = decstate->curbyte - decstate->start;
125  
126 8 _free_decode_state_if_not_persistent(aTHX_ decstate);
127  
128 char bytebuf[5];
129  
130 char *bytestr;
131  
132 8 switch (byte) {
133 2 case CBOR_FALSE:
134 2 bytestr = "false";
135 2 break;
136 1 case CBOR_TRUE:
137 1 bytestr = "true";
138 1 break;
139 1 case CBOR_NULL:
140 1 bytestr = "null";
141 1 break;
142 1 case CBOR_UNDEFINED:
143 1 bytestr = "undefined";
144 1 break;
145 0 case CBOR_HALF_FLOAT:
146 0 bytestr = "half-float";
147 0 case CBOR_FLOAT:
148 0 bytestr = "float";
149 1 case CBOR_DOUBLE:
150 1 bytestr = "double float";
151 1 break;
152 2 default:
153 2 switch ((byte & 0xe0) >> 5) {
154 1 case CBOR_TYPE_ARRAY:
155 1 bytestr = "array";
156 1 break;
157 1 case CBOR_TYPE_MAP:
158 1 bytestr = "map";
159 1 break;
160 0 default:
161 0 my_snprintf( bytebuf, 5, "0x%02x", byte );
162 0 bytestr = bytebuf;
163 }
164 }
165  
166 24 SV* args[3] = {
167 8 newSVpvs("InvalidMapKey"),
168 8 newSVpv(bytestr, 0),
169 8 newSVuv(offset),
170 };
171  
172 8 cbf_die_with_arguments( aTHX_ 3, args );
173  
174 assert(0);
175 0 }
176  
177 8 void _croak_duplicate_map_key( pTHX_ decode_ctx* decstate, UV offset ) {
178 8 _free_decode_state_if_not_persistent(aTHX_ decstate);
179  
180 16 SV* args[2] = {
181 8 newSVpvs("DuplicateMapKey"),
182 8 newSVuv(offset),
183 };
184  
185 8 cbf_die_with_arguments( aTHX_ 2, args );
186  
187 assert(0);
188 0 }
189  
190 0 void _croak_cannot_decode_64bit( pTHX_ decode_ctx* decstate ) {
191 0 UV offset = decstate->curbyte - decstate->start;
192  
193 0 _free_decode_state_if_not_persistent(aTHX_ decstate);
194  
195 0 SV* args[3] = {
196 0 newSVpvs("CannotDecode64Bit"),
197 0 newSVpvn( decstate->curbyte, 8),
198 0 newSVuv(offset),
199 };
200  
201 0 cbf_die_with_arguments( aTHX_ 3, args );
202  
203 assert(0);
204 0 }
205  
206 1 void _croak_cannot_decode_negative( pTHX_ decode_ctx* decstate, UV abs, STRLEN offset ) {
207 1 _free_decode_state_if_not_persistent(aTHX_ decstate);
208  
209 3 SV* args[3] = {
210 1 newSVpvs("NegativeIntTooLow"),
211 1 newSVuv(abs),
212 1 newSVuv(offset),
213 };
214  
215 1 cbf_die_with_arguments( aTHX_ 3, args );
216  
217 assert(0);
218 0 }
219  
220 33 void _warn_unhandled_tag( pTHX_ UV tagnum, U8 value_major_type ) {
221 char tmpl[255];
222 33 my_snprintf( tmpl, sizeof(tmpl), "Ignoring unrecognized CBOR tag #%s (major type %%u, %%s)!", UV_TO_STR_TMPL );
223  
224 33 warn(tmpl, tagnum, value_major_type, MAJOR_TYPE_DESCRIPTION[value_major_type]);
225 33 }
226  
227 //----------------------------------------------------------------------
228  
229 118 static inline void _validate_utf8_string_if_needed( pTHX_ decode_ctx* decstate, char *buffer, STRLEN len ) {
230  
231 118 ▶ if (!(decstate->flags & CBF_FLAG_NAIVE_UTF8) && !is_utf8_string( (U8 *)buffer, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(decstate->flags & CBF_FLAG_NAIVE_UTF8) && !is_utf8_string( (U8 *)buffer, len)) { 115 3 118
if (!(decstate->flags & CBF_FLAG_NAIVE_UTF8) && !is_utf8_string( (U8 *)buffer, len)) { 2 113 115
232 2 _croak_invalid_utf8( aTHX_ decstate, buffer, len );
233 }
234 116 }
235  
236 //----------------------------------------------------------------------
237 // DECODER:
238 //----------------------------------------------------------------------
239  
240 // Sets incomplete_by.
241 6183 static inline UV _parse_for_uint_len2( pTHX_ decode_ctx* decstate ) {
242 UV ret;
243  
244 6183 switch (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte)) {
245 270 case CBOR_LENGTH_SMALL:
246  
247 270 ▶ _RETURN_IF_INCOMPLETE( decstate, 2, 0 );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 2, 0 ); 6 264 270
248  
249 264 ++decstate->curbyte;
250  
251 264 ret = (uint8_t) decstate->curbyte[0];
252  
253 264 ++decstate->curbyte;
254  
255 264 break;
256  
257 56 case CBOR_LENGTH_MEDIUM:
258 56 ▶ _RETURN_IF_INCOMPLETE( decstate, 3, 0);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 3, 0); 10 46 56
259  
260 46 ++decstate->curbyte;
261  
262 46 ret = ntohs( *((uint16_t *) decstate->curbyte) );
263  
264 46 decstate->curbyte += 2;
265  
266 46 break;
267  
268 162 case CBOR_LENGTH_LARGE:
269 162 ▶ _RETURN_IF_INCOMPLETE( decstate, 5, 0);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 5, 0); 34 128 162
270  
271 128 ++decstate->curbyte;
272  
273 128 ret = ntohl( *((uint32_t *) decstate->curbyte) );
274  
275 128 decstate->curbyte += 4;
276  
277 128 break;
278  
279 59 case CBOR_LENGTH_HUGE:
280 59 ▶ _RETURN_IF_INCOMPLETE( decstate, 9, 0);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 9, 0); 40 19 59
281  
282 19 ++decstate->curbyte;
283  
284 #if !IS_64_BIT
285  
286 if (*( (uint32_t*) decstate->curbyte )) {
287 _croak_cannot_decode_64bit( aTHX_ decstate );
288 }
289 #endif
290 19 ret = _buffer_u64_to_uv( (uint8_t *) decstate->curbyte );
291  
292 19 decstate->curbyte += 8;
293  
294 19 break;
295  
296 21 case 0x1c:
297 case 0x1d:
298 case 0x1e:
299 case 0x1f: // indefinite must be handled outside this function.
300 21 _croak_invalid_control( aTHX_ decstate );
301 0 return 0; // Silence compiler warning.
302  
303 5615 default:
304 5615 ret = CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte);
305  
306 5615 decstate->curbyte++;
307 }
308  
309 6072 return ret;
310 }
311  
312 //----------------------------------------------------------------------
313  
314 // Sets incomplete_by.
315 171 SV *_decode_array( pTHX_ decode_ctx* decstate ) {
316 171 AV *array = newAV();
317 171 sv_2mortal( (SV *) array );
318  
319 171 SV *cur = NULL;
320  
321 171 ▶ if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) { 39 132 171
322 39 ++decstate->curbyte;
323  
324 while (1) {
325 83 ▶ _RETURN_IF_INCOMPLETE( decstate, 1, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 1, NULL ); 4 79 83
326  
327 79 ▶ if ( decstate->curbyte[0] == '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->curbyte[0] == '\xff') { 12 67 79
328 12 ++decstate->curbyte;
329 12 break;
330 }
331  
332 67 cur = cbf_decode_one( aTHX_ decstate );
333  
334 67 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 23 44 67
335  
336 44 av_push(array, cur);
337 }
338 }
339 else {
340 132 SSize_t array_length = _parse_for_uint_len2( aTHX_ decstate );
341 129 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 2 127 129
342  
343 127 ▶ if (array_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_length) { 108 19 127
344 108 av_fill(array, array_length - 1);
345  
346 SSize_t i;
347 587 ▶ for (i=0; i<array_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<array_length; i++) { 533 54 587
348 533 cur = cbf_decode_one( aTHX_ decstate );
349 484 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 5 479 484
350  
351 479 ▶ if (!av_store(array, i, cur)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(array, i, cur)) { 0 479 479
352 0 _croak("Failed to store item in array!");
353 }
354 }
355 }
356 }
357  
358 85 return newRV_inc( (SV *) array );
359 }
360  
361 // Sets incomplete_by.
362 1928 UV _decode_uint( pTHX_ decode_ctx* decstate ) {
363  
364 1928 ▶ if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) { 1 1927 1928
365 1 _croak_invalid_control( aTHX_ decstate );
366 }
367  
368 1927 return _parse_for_uint_len2( aTHX_ decstate );
369 }
370  
371 // Sets incomplete_by.
372 46 IV _decode_negint( pTHX_ decode_ctx* decstate ) {
373  
374 46 ▶ if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) { 1 45 46
375 1 _croak_invalid_control( aTHX_ decstate );
376 }
377  
378 45 UV positive = _parse_for_uint_len2( aTHX_ decstate );
379 42 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, 0);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, 0); 16 26 42
380  
381 #if IS_64_BIT
382 26 ▶ if (positive >= 0x8000000000000000U) {
Branch: 1 branch
BranchTrueFalseTotal
if (positive >= 0x8000000000000000U) { 1 25 26
383 1 _croak_cannot_decode_negative( aTHX_ decstate, positive, decstate->curbyte - decstate->start - 8 );
384 }
385 #else
386 if (positive >= 0x80000000U) {
387 STRLEN offset = decstate->curbyte - decstate->start;
388  
389 if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_LARGE) {
390 offset -= 4;
391 }
392 else {
393 offset -= 8;
394 }
395  
396 _croak_cannot_decode_negative( aTHX_ decstate, positive, offset );
397 }
398 #endif
399  
400 25 return( -1 - (int64_t) positive );
401 }
402  
403 // Sets incomplete_by.
404 // Return indicates whether string_h has SV.
405 3810 bool _decode_str( pTHX_ decode_ctx* decstate, union numbuf_or_sv* string_u ) {
406  
407 3810 ▶ if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) { 13 3797 3810
408 13 ++decstate->curbyte;
409  
410 13 SV *string = newSVpvs(""); /* 5.10.0 lacks newSVpvs_flags() */
411 13 sv_2mortal(string);
412 13 string_u->sv = string;
413  
414 14 while (1) {
415 27 ▶ _RETURN_IF_INCOMPLETE( decstate, 1, false );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 1, false ); 2 25 27
416  
417 25 ▶ if (decstate->curbyte[0] == '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
if (decstate->curbyte[0] == '\xff') { 9 16 25
418 9 ++decstate->curbyte;
419 9 break;
420 }
421  
422 //TODO: Require the same major type.
423  
424 16 SV *cur = cbf_decode_one( aTHX_ decstate );
425  
426 16 ▶ _RETURN_IF_SET_INCOMPLETE( decstate, false );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE( decstate, false ); 2 14 16
427  
428 14 sv_2mortal(cur);
429  
430 14 sv_catsv(string, cur);
431 }
432  
433 9 SvREFCNT_inc(string);
434  
435 9 return true;
436 }
437  
438 3797 string_u->numbuf.num.uv = _parse_for_uint_len2( aTHX_ decstate );
439 3791 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, false);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, false); 41 3750 3791
440  
441 3750 ▶ _RETURN_IF_INCOMPLETE( decstate, string_u->numbuf.num.uv, false );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, string_u->numbuf.num.uv, false ); 32 3718 3750
442  
443 3718 string_u->numbuf.buffer = decstate->curbyte;
444  
445 3718 decstate->curbyte += string_u->numbuf.num.uv;
446  
447 3718 return false;
448 }
449  
450 // Sets incomplete_by.
451 2618 void _decode_hash_entry( pTHX_ decode_ctx* decstate, HV *hash ) {
452 2624 ▶ _RETURN_IF_INCOMPLETE( decstate, 1, );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 1, ); 2 2616 2618
453  
454 2616 UV key_offset = decstate->curbyte - decstate->start;
455  
456 union numbuf_or_sv my_key;
457 2616 my_key.numbuf.buffer = NULL;
458  
459 // This is going to be a hash key, so it can’t usefully be
460 // anything but a string/PV.
461 I32 keylen;
462 char *keystr;
463  
464 2616 bool my_key_has_sv = false;
465  
466 2616 uint8_t major_type = CONTROL_BYTE_MAJOR_TYPE(*decstate->curbyte);
467  
468 2616 switch (major_type) {
469 26 case CBOR_TYPE_UINT:
470 26 my_key.numbuf.num.uv = _decode_uint( aTHX_ decstate );
471 26 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, ); 1 25 26
472  
473 25 keystr = (char *) decstate->scratch.bytes;
474 25 keylen = _uv_to_str( my_key.numbuf.num.uv, keystr, sizeof(decstate->scratch.bytes));
475 // fprintf(stderr, "key (%p) is uint: %.*s\n", keystr, keylen, keystr);
476  
477 25 break;
478  
479 3 case CBOR_TYPE_NEGINT:
480 3 my_key.numbuf.num.iv = _decode_negint( aTHX_ decstate );
481 3 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, ); 1 2 3
482  
483 2 keystr = (char *) decstate->scratch.bytes;
484 2 keylen = _iv_to_str( my_key.numbuf.num.iv, keystr, sizeof(decstate->scratch.bytes));
485  
486 2 break;
487  
488 2579 case CBOR_TYPE_BINARY:
489 case CBOR_TYPE_UTF8:
490 2579 my_key_has_sv = _decode_str( aTHX_ decstate, &my_key );
491 2579 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, ); 4 2575 2579
492  
493 2575 ▶ if (!my_key_has_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!my_key_has_sv) { 2570 5 2575
494 2570 ▶ if (my_key.numbuf.num.uv > 0x7fffffffU) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_key.numbuf.num.uv > 0x7fffffffU) { 0 2570 2570
495 0 _croak("key too long!");
496 }
497  
498 2570 keystr = my_key.numbuf.buffer;
499  
500 2570 ▶ if (SHOULD_VALIDATE_UTF8(decstate, major_type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SHOULD_VALIDATE_UTF8(decstate, major_type)) { 2520 50 2570
if (SHOULD_VALIDATE_UTF8(decstate, major_type)) { 0 2520 2520
501 50 _validate_utf8_string_if_needed( aTHX_ decstate, keystr, my_key.numbuf.num.uv );
502  
503 49 ▶ keylen = decstate->string_decode_mode == CBF_STRING_DECODE_NEVER ? my_key.numbuf.num.uv : -my_key.numbuf.num.uv;
Branch: 1 branch
BranchTrueFalseTotal
keylen = decstate->string_decode_mode == CBF_STRING_DECODE_NEVER ? my_key.numbuf.num.uv : -my_key.numbuf.num.uv; 0 49 49
504 }
505 else {
506 2520 keylen = my_key.numbuf.num.uv;
507 }
508 }
509  
510 2574 break;
511  
512 8 default:
513 8 _croak_invalid_map_key( aTHX_ decstate);
514 0 return; // Silence compiler warning.
515 }
516  
517 2601 ▶ if (decstate->flags & CBF_FLAG_REJECT_DUPLICATE_KEYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (decstate->flags & CBF_FLAG_REJECT_DUPLICATE_KEYS) { 20 2581 2601
518 20 bool duplicate = my_key_has_sv
519 2 ? hv_exists_ent(hash, my_key.sv, 0)
520 22 ▶ : hv_exists(hash, keystr, keylen);
Branch: 1 branch
BranchTrueFalseTotal
: hv_exists(hash, keystr, keylen); 2 18 20
521  
522 20 ▶ if (duplicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (duplicate) { 8 12 20
523 8 ▶ if (my_key_has_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_key_has_sv) { 1 7 8
524 1 SvREFCNT_dec( my_key.sv );
525 }
526  
527 8 _croak_duplicate_map_key( aTHX_ decstate, key_offset );
528 }
529 }
530  
531 2593 SV *curval = cbf_decode_one( aTHX_ decstate );
532  
533 2592 ▶ if (decstate->incomplete_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (decstate->incomplete_by) { 3 2589 2592
534 3 ▶ if (my_key_has_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_key_has_sv) { 1 2 3
535 1 SvREFCNT_dec( my_key.sv );
536 }
537 }
538 2589 ▶ else if (my_key_has_sv) {
Branch: 1 branch
BranchTrueFalseTotal
else if (my_key_has_sv) { 3 2586 2589
539 3 hv_store_ent(hash, my_key.sv, curval, 0);
540 }
541 else {
542 2586 hv_store(hash, keystr, keylen, curval, 0);
543 }
544 }
545  
546 // Sets incomplete_by.
547 81 SV *_decode_map( pTHX_ decode_ctx* decstate ) {
548  
549 81 HV *hash = newHV();
550 81 sv_2mortal( (SV *) hash );
551  
552 81 ▶ if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(*decstate->curbyte) == CBOR_LENGTH_INDEFINITE) { 8 73 81
553 8 ++decstate->curbyte;
554  
555 while (1) {
556 13 ▶ _RETURN_IF_INCOMPLETE( decstate, 1, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 1, NULL ); 2 11 13
557  
558 11 ▶ if (decstate->curbyte[0] == '\xff') {
Branch: 1 branch
BranchTrueFalseTotal
if (decstate->curbyte[0] == '\xff') { 4 7 11
559 4 ++decstate->curbyte;
560 4 break;
561 }
562  
563 7 _decode_hash_entry( aTHX_ decstate, hash );
564  
565 // TODO: Recursively decref all hash members.
566 7 ▶ if ( decstate->incomplete_by ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->incomplete_by ) { 2 5 7
567 2 return NULL;
568 }
569 }
570 }
571 else {
572 73 SSize_t keycount = _parse_for_uint_len2( aTHX_ decstate );
573 70 ▶ if ( decstate->incomplete_by ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->incomplete_by ) { 5 65 70
574 5 return NULL;
575 }
576  
577 65 ▶ if (keycount) {
Branch: 1 branch
BranchTrueFalseTotal
if (keycount) { 53 12 65
578 2637 ▶ while (keycount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (keycount > 0) { 2611 26 2637
579 2611 _decode_hash_entry( aTHX_ decstate, hash );
580  
581 // TODO: Recursively decref all hash members.
582 2593 ▶ if ( decstate->incomplete_by ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->incomplete_by ) { 9 2584 2593
583 9 return NULL;
584 }
585  
586 2584 --keycount;
587 }
588 }
589 }
590  
591 42 return newRV_inc( (SV *) hash);
592 }
593  
594 //----------------------------------------------------------------------
595  
596 // Taken from RFC 7049:
597 7 double decode_half_float(uint8_t *halfp) {
598 7 int half = (halfp[0] << 8) + halfp[1];
599 7 int exp = (half >> 10) & 0x1f;
600 7 int mant = half & 0x3ff;
601 double val;
602 7 ▶ if (exp == 0) val = ldexp(mant, -24);
Branch: 1 branch
BranchTrueFalseTotal
if (exp == 0) val = ldexp(mant, -24); 0 7 7
603 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
604 6 ▶ else val = mant == 0 ? INFINITY : NAN;
Branch: 1 branch
BranchTrueFalseTotal
else val = mant == 0 ? INFINITY : NAN; 4 2 6
605 7 ▶ return half & 0x8000 ? -val : val;
Branch: 1 branch
BranchTrueFalseTotal
return half & 0x8000 ? -val : val; 2 5 7
606 }
607  
608 24 static inline float _decode_float_to_host( pTHX_ decode_ctx* decstate, uint8_t *ptr ) {
609 24 *((uint32_t *) decstate->scratch.bytes) = ntohl( *((uint32_t *) ptr) );
610  
611 24 return decstate->scratch.as_float;
612 }
613  
614 63 static inline double _decode_double_to_le( decode_ctx* decstate, uint8_t *ptr ) {
615 63 decstate->scratch.bytes[0] = ptr[7];
616 63 decstate->scratch.bytes[1] = ptr[6];
617 63 decstate->scratch.bytes[2] = ptr[5];
618 63 decstate->scratch.bytes[3] = ptr[4];
619 63 decstate->scratch.bytes[4] = ptr[3];
620 63 decstate->scratch.bytes[5] = ptr[2];
621 63 decstate->scratch.bytes[6] = ptr[1];
622 63 decstate->scratch.bytes[7] = ptr[0];
623  
624 63 return decstate->scratch.as_double;
625 }
626  
627 //----------------------------------------------------------------------
628  
629 // Sets incomplete_by.
630 1231 static inline SV *_decode_str_to_sv( pTHX_ decode_ctx* decstate ) {
631 union numbuf_or_sv string;
632  
633 1231 ▶ if (_decode_str( aTHX_ decstate, &string )) {
Branch: 1 branch
BranchTrueFalseTotal
if (_decode_str( aTHX_ decstate, &string )) { 4 1221 1225
634 4 return string.sv;
635 }
636  
637 1221 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 73 1148 1221
638  
639 1148 return newSVpvn( string.numbuf.buffer, string.numbuf.num.uv );
640 }
641  
642 // Sets incomplete_by.
643 4012 SV *cbf_decode_one( pTHX_ decode_ctx* decstate ) {
644 4012 SV *ret = NULL;
645  
646 4012 ▶ _RETURN_IF_INCOMPLETE( decstate, 1, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 1, NULL ); 11 4001 4012
647  
648 4001 uint8_t control_byte = *decstate->curbyte;
649  
650 // fprintf(stderr, "major type: %d\n", control->pieces.major_type);
651  
652 4001 switch (CONTROL_BYTE_MAJOR_TYPE(control_byte)) {
653 1902 case CBOR_TYPE_UINT:
654 1902 ret = newSVuv( _decode_uint( aTHX_ decstate ) );
655 1898 ▶ if ( decstate->incomplete_by ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->incomplete_by ) { 15 1883 1898
656 15 SvREFCNT_dec(ret);
657 189 return NULL;
658 }
659  
660 1883 break;
661 43 case CBOR_TYPE_NEGINT:
662 43 ret = newSViv( _decode_negint( aTHX_ decstate ) );
663 38 ▶ if ( decstate->incomplete_by ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( decstate->incomplete_by ) { 15 23 38
664 15 SvREFCNT_dec(ret);
665 15 return NULL;
666 }
667  
668 23 break;
669 1231 case CBOR_TYPE_BINARY:
670 case CBOR_TYPE_UTF8:
671 1231 ret = _decode_str_to_sv( aTHX_ decstate );
672 1225 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 73 1152 1225
673  
674 1152 ▶ if (SHOULD_VALIDATE_UTF8(decstate, CONTROL_BYTE_MAJOR_TYPE(control_byte))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SHOULD_VALIDATE_UTF8(decstate, CONTROL_BYTE_MAJOR_TYPE(control_byte))) { 1086 66 1152
if (SHOULD_VALIDATE_UTF8(decstate, CONTROL_BYTE_MAJOR_TYPE(control_byte))) { 2 1084 1086
675 136 _validate_utf8_string_if_needed( aTHX_ decstate, SvPV_nolen(ret), SvCUR(ret));
676  
677 // Always set the UTF8 flag, even if it’s not needed.
678 // This helps ensure that text strings will round-trip
679 // through Perl.
680 67 ▶ if (decstate->string_decode_mode != CBF_STRING_DECODE_NEVER) SvUTF8_on(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (decstate->string_decode_mode != CBF_STRING_DECODE_NEVER) SvUTF8_on(ret); 65 2 67
681 }
682  
683 1151 break;
684 171 case CBOR_TYPE_ARRAY:
685 171 ret = _decode_array( aTHX_ decstate );
686 119 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 34 85 119
687  
688 85 break;
689 81 case CBOR_TYPE_MAP:
690 81 ret = _decode_map( aTHX_ decstate );
691 60 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 18 42 60
692  
693 42 break;
694 198 case CBOR_TYPE_TAG:
695  
696 198 ▶ if (CONTROL_BYTE_LENGTH_TYPE(control_byte) == CBOR_LENGTH_INDEFINITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONTROL_BYTE_LENGTH_TYPE(control_byte) == CBOR_LENGTH_INDEFINITE) { 1 197 198
697 1 _croak_invalid_control( aTHX_ decstate );
698 }
699  
700 197 UV tagnum = _parse_for_uint_len2( aTHX_ decstate );
701 194 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 15 179 194
702  
703 179 uint8_t value_major_type = CONTROL_BYTE_MAJOR_TYPE(*decstate->curbyte);
704  
705 191 ▶ if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) {
Branch: 2 branches
BranchTrueFalseTotal
if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) { 24 155 179
if (tagnum == CBOR_TAG_SHAREDREF && decstate->reflist) { 12 12 24
706 12 ▶ if (value_major_type != CBOR_TYPE_UINT) {
Branch: 1 branch
BranchTrueFalseTotal
if (value_major_type != CBOR_TYPE_UINT) { 0 12 12
707 char tmpl[255];
708 0 my_snprintf( tmpl, sizeof(tmpl), "Shared ref type must be uint, not %%u (%%s)!" );
709 0 croak(tmpl, value_major_type, MAJOR_TYPE_DESCRIPTION[value_major_type]);
710 }
711  
712 12 UV refnum = _parse_for_uint_len2( aTHX_ decstate );
713 12 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 0 12 12
714  
715 12 ▶ if (refnum >= decstate->reflistlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (refnum >= decstate->reflistlen) { 0 12 12
716 0 _croak("Missing shareable!");
717 }
718  
719 12 ret = decstate->reflist[refnum];
720 12 SvREFCNT_inc(ret);
721 }
722 else {
723 167 ret = cbf_decode_one( aTHX_ decstate );
724 167 ▶ _RETURN_IF_SET_INCOMPLETE(decstate, NULL);
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_SET_INCOMPLETE(decstate, NULL); 5 162 167
725  
726 162 ▶ if (tagnum == CBOR_TAG_INDIRECTION) {
Branch: 1 branch
BranchTrueFalseTotal
if (tagnum == CBOR_TAG_INDIRECTION) { 17 145 162
727 17 ret = newRV_noinc(ret);
728 }
729 145 ▶ else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) {
Branch: 2 branches
BranchTrueFalseTotal
else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) { 24 121 145
else if (tagnum == CBOR_TAG_SHAREABLE && decstate->reflist) { 12 12 24
730 12 ++decstate->reflistlen;
731 12 ▶ Renew( decstate->reflist, decstate->reflistlen, void * );
Branch: 1 branch
BranchTrueFalseTotal
Renew( decstate->reflist, decstate->reflistlen, void * ); 0 12 12
732  
733 12 decstate->reflist[ decstate->reflistlen - 1 ] = (SV *) ret;
734 }
735  
736 133 ▶ else if (decstate->tag_handler) {
Branch: 1 branch
BranchTrueFalseTotal
else if (decstate->tag_handler) { 102 31 133
737 102 HV *my_tag_handler = decstate->tag_handler;
738  
739 102 SV **handler_cr = hv_fetch( my_tag_handler, (char *) &tagnum, sizeof(UV), 0 );
740  
741 102 ▶ if (handler_cr && *handler_cr && SvOK(*handler_cr)) {
Branch: 3 branches
BranchTrueFalseTotal
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 101 1 102
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 101 0 101
if (handler_cr && *handler_cr && SvOK(*handler_cr)) { 100 1 101
742 100 ret = cbf_call_scalar_with_arguments( aTHX_ *handler_cr, 1, &ret );
743 }
744 else {
745 2 _warn_unhandled_tag( aTHX_ tagnum, value_major_type );
746 }
747 }
748 else {
749 31 _warn_unhandled_tag( aTHX_ tagnum, value_major_type );
750 }
751 }
752  
753 174 break;
754 375 case CBOR_TYPE_OTHER:
755 375 switch (control_byte) {
756 3 case CBOR_FALSE:
757 3 ret = newSVsv( cbf_get_false() );
758 3 ++decstate->curbyte;
759 3 break;
760  
761 6 case CBOR_TRUE:
762 6 ret = newSVsv( cbf_get_true() );
763 6 ++decstate->curbyte;
764 6 break;
765  
766 233 case CBOR_NULL:
767 case CBOR_UNDEFINED:
768 233 ret = &PL_sv_undef;
769 233 ++decstate->curbyte;
770 233 break;
771  
772 9 case CBOR_HALF_FLOAT:
773 9 ▶ _RETURN_IF_INCOMPLETE( decstate, 3, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 3, NULL ); 2 7 9
774  
775 7 ret = newSVnv( decode_half_float( (uint8_t *) (1 + decstate->curbyte) ) );
776  
777 7 decstate->curbyte += 3;
778 7 break;
779  
780 28 case CBOR_FLOAT:
781 28 ▶ _RETURN_IF_INCOMPLETE( decstate, 5, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 5, NULL ); 4 24 28
782  
783 float decoded_flt;
784  
785 #if IS_LITTLE_ENDIAN
786 24 decoded_flt = _decode_float_to_host( aTHX_ decstate, (uint8_t *) (1 + decstate->curbyte ) );
787 #else
788 decoded_flt = *( (float *) (1 + decstate->curbyte) );
789 #endif
790  
791 24 ret = newSVnv( (NV) decoded_flt );
792  
793 24 decstate->curbyte += 5;
794 24 break;
795  
796 71 case CBOR_DOUBLE:
797 71 ▶ _RETURN_IF_INCOMPLETE( decstate, 9, NULL );
Branch: 1 branch
BranchTrueFalseTotal
_RETURN_IF_INCOMPLETE( decstate, 9, NULL ); 8 63 71
798  
799 double decoded_dbl;
800  
801 #if IS_LITTLE_ENDIAN
802 63 decoded_dbl = _decode_double_to_le( decstate, (uint8_t *) (1 + decstate->curbyte ) );
803 #else
804 decoded_dbl = *( (double *) (1 + decstate->curbyte) );
805 #endif
806  
807 63 ret = newSVnv( (NV) decoded_dbl );
808  
809 63 decstate->curbyte += 9;
810 63 break;
811  
812 25 default:
813 25 _croak_invalid_control( aTHX_ decstate );
814 }
815  
816 336 break;
817  
818 0 default:
819 0 _croak("Unknown type!");
820 }
821  
822 3694 return ret;
823 }
824  
825 /*
826 * Possible states:
827 *
828 * 1) We’re initializing.
829 * 2) We just concat’ed two SVPVs (same as initializing).
830 * 3) We just shortened from the beginning.
831 */
832  
833 643 void renew_decode_state_buffer( pTHX_ decode_ctx *decode_state, SV *cbor ) {
834 643 STRLEN cborlen = SvCUR(cbor);
835  
836 643 char *cborstr = SvPVX(cbor);
837  
838 STRLEN offset;
839 643 ▶ if (decode_state->curbyte == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->curbyte == NULL) { 611 32 643
840 611 offset = 0;
841 }
842 else {
843 32 offset = decode_state->curbyte - decode_state->start;
844 }
845  
846 643 decode_state->start = cborstr;
847 643 decode_state->size = cborlen;
848 643 decode_state->curbyte = cborstr + offset;
849 643 decode_state->end = cborstr + cborlen;
850 643 }
851  
852 18 void advance_decode_state_buffer( pTHX_ decode_ctx *decode_state ) {
853 18 STRLEN diff = decode_state->curbyte - decode_state->start;
854  
855 18 decode_state->start = decode_state->curbyte;
856 18 decode_state->size -= diff;
857 18 }
858  
859 582 decode_ctx* create_decode_state( pTHX_ SV *cbor, HV *tag_handler, UV flags ) {
860 decode_ctx *decode_state;
861 582 Newx( decode_state, 1, decode_ctx );
862  
863 582 decode_state->curbyte = NULL;
864  
865 582 ▶ if (cbor) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbor) { 474 108 582
866 474 renew_decode_state_buffer( aTHX_ decode_state, cbor );
867 }
868  
869 582 decode_state->tag_handler = tag_handler;
870 582 ▶ if (NULL != tag_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != tag_handler) { 0 582 582
871 0 SvREFCNT_inc((SV *) tag_handler);
872 }
873  
874 582 decode_state->reflist = NULL;
875 582 decode_state->reflistlen = 0;
876 582 decode_state->flags = flags;
877 582 decode_state->incomplete_by = 0;
878  
879 582 decode_state->string_decode_mode = CBF_STRING_DECODE_CBOR;
880  
881 582 ▶ if (flags & CBF_FLAG_PRESERVE_REFERENCES) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & CBF_FLAG_PRESERVE_REFERENCES) { 0 582 582
882 0 ensure_reflist_exists( aTHX_ decode_state );
883 }
884  
885 582 return decode_state;
886 }
887  
888 4 void ensure_reflist_exists( pTHX_ decode_ctx* decode_state) {
889 4 ▶ if (NULL == decode_state->reflist) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == decode_state->reflist) { 4 0 4
890 4 Newx( decode_state->reflist, 0, void * );
891 }
892 4 }
893  
894 586 void delete_reflist( pTHX_ decode_ctx* decode_state) {
895 586 ▶ if (NULL != decode_state->reflist) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != decode_state->reflist) { 4 582 586
896 4 Safefree(decode_state->reflist);
897 4 decode_state->reflist = NULL;
898 4 decode_state->reflistlen = 0;
899 }
900 586 }
901  
902 4 void reset_reflist_if_needed( pTHX_ decode_ctx* decode_state) {
903 4 ▶ if (decode_state->reflistlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->reflistlen) { 2 2 4
904 2 delete_reflist( aTHX_ decode_state );
905 2 ensure_reflist_exists( aTHX_ decode_state );
906 }
907 4 }
908  
909 582 void free_decode_state( pTHX_ decode_ctx* decode_state) {
910 582 delete_reflist( aTHX_ decode_state );
911  
912 582 ▶ if (NULL != decode_state->tag_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != decode_state->tag_handler) { 102 480 582
913 102 SvREFCNT_dec((SV *) decode_state->tag_handler);
914 102 decode_state->tag_handler = NULL;
915 }
916  
917 582 Safefree(decode_state);
918 582 }
919  
920 601 SV *cbf_decode_document( pTHX_ decode_ctx *decode_state ) {
921 601 SV *RETVAL = cbf_decode_one( aTHX_ decode_state );
922  
923 534 ▶ if (decode_state->incomplete_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->incomplete_by) { 146 388 534
924 146 _croak_incomplete( aTHX_ decode_state );
925 }
926  
927 388 ▶ if (decode_state->curbyte != decode_state->end) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->curbyte != decode_state->end) { 1 387 388
928 1 STRLEN bytes_count = decode_state->end - decode_state->curbyte;
929  
930 char numstr[24];
931 1 _uv_to_str(bytes_count, numstr, 24);
932  
933 1 char * words[2] = { numstr, NULL };
934  
935 1 call_argv("CBOR::Free::_warn_decode_leftover", G_DISCARD, words);
936 }
937  
938 388 return RETVAL;
939 }
940  
941 464 SV *cbf_decode( pTHX_ SV *cbor, HV *tag_handler, UV flags ) {
942  
943 464 decode_ctx *decode_state = create_decode_state( aTHX_ cbor, tag_handler, flags);
944  
945 464 SV *RETVAL = cbf_decode_document( aTHX_ decode_state );
946  
947 269 free_decode_state( aTHX_ decode_state);
948  
949 269 return RETVAL;
950 }
« cbor_free_encode.c ↑ Summary cbor_free_boolean.c »