… | |
… | |
28 | #endif |
28 | #endif |
29 | #ifndef SvREFCNT_dec_NN |
29 | #ifndef SvREFCNT_dec_NN |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
31 | #endif |
31 | #endif |
32 | |
32 | |
|
|
33 | // perl's is_utf8_string interprets len=0 as "calculate len", but we want it to mean 0 |
|
|
34 | #define cbor_is_utf8_string(str,len) (!(len) || is_utf8_string ((str), (len))) |
|
|
35 | |
33 | // known major and minor types |
36 | // known major and minor types |
34 | enum cbor_type |
37 | enum cbor_type |
35 | { |
38 | { |
36 | MAJOR_SHIFT = 5, |
39 | MAJOR_SHIFT = 5, |
37 | MINOR_MASK = 0x1f, |
40 | MINOR_MASK = 0x1f, |
… | |
… | |
99 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
102 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
100 | |
103 | |
101 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
104 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
102 | }; |
105 | }; |
103 | |
106 | |
|
|
107 | // known forced types, also hardcoded in CBOR.pm |
|
|
108 | enum |
|
|
109 | { |
|
|
110 | AS_CBOR = 0, |
|
|
111 | AS_INT = 1, |
|
|
112 | AS_BYTES = 2, |
|
|
113 | AS_TEXT = 3, |
|
|
114 | AS_FLOAT16 = 4, |
|
|
115 | AS_FLOAT32 = 5, |
|
|
116 | AS_FLOAT64 = 6, |
|
|
117 | AS_MAP = 7, |
|
|
118 | // possibly future enhancements: (generic) float, (generic) string |
|
|
119 | }; |
|
|
120 | |
104 | #define F_SHRINK 0x00000001UL |
121 | #define F_SHRINK 0x00000001UL |
105 | #define F_ALLOW_UNKNOWN 0x00000002UL |
122 | #define F_ALLOW_UNKNOWN 0x00000002UL |
106 | #define F_ALLOW_SHARING 0x00000004UL |
123 | #define F_ALLOW_SHARING 0x00000004UL |
107 | #define F_ALLOW_CYCLES 0x00000008UL |
124 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
125 | #define F_ALLOW_WEAK_CYCLES 0x00000010UL |
108 | #define F_FORBID_OBJECTS 0x00000010UL |
126 | #define F_FORBID_OBJECTS 0x00000020UL |
109 | #define F_PACK_STRINGS 0x00000020UL |
127 | #define F_PACK_STRINGS 0x00000040UL |
110 | #define F_TEXT_KEYS 0x00000040UL |
128 | #define F_TEXT_KEYS 0x00000080UL |
111 | #define F_TEXT_STRINGS 0x00000080UL |
129 | #define F_TEXT_STRINGS 0x00000100UL |
112 | #define F_VALIDATE_UTF8 0x00000100UL |
130 | #define F_VALIDATE_UTF8 0x00000200UL |
113 | |
131 | |
114 | #define INIT_SIZE 32 // initial scalar size to be allocated |
132 | #define INIT_SIZE 32 // initial scalar size to be allocated |
115 | |
133 | |
116 | #define SB do { |
134 | #define SB do { |
117 | #define SE } while (0) |
135 | #define SE } while (0) |
… | |
… | |
187 | #endif |
205 | #endif |
188 | } |
206 | } |
189 | } |
207 | } |
190 | |
208 | |
191 | // minimum length of a string to be registered for stringref |
209 | // minimum length of a string to be registered for stringref |
192 | ecb_inline int |
210 | ecb_inline STRLEN |
193 | minimum_string_length (UV idx) |
211 | minimum_string_length (UV idx) |
194 | { |
212 | { |
195 | return idx <= 23 ? 3 |
213 | return idx <= 23 ? 3 |
196 | : idx <= 0xffU ? 4 |
214 | : idx <= 0xffU ? 4 |
197 | : idx <= 0xffffU ? 5 |
215 | : idx <= 0xffffU ? 5 |
… | |
… | |
226 | enc->cur = SvPVX (enc->sv) + cur; |
244 | enc->cur = SvPVX (enc->sv) + cur; |
227 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
245 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
228 | } |
246 | } |
229 | } |
247 | } |
230 | |
248 | |
|
|
249 | static void encode_sv (enc_t *enc, SV *sv); |
|
|
250 | |
231 | ecb_inline void |
251 | ecb_inline void |
232 | encode_ch (enc_t *enc, char ch) |
252 | encode_ch (enc_t *enc, char ch) |
233 | { |
253 | { |
234 | need (enc, 1); |
254 | need (enc, 1); |
235 | *enc->cur++ = ch; |
255 | *enc->cur++ = ch; |
236 | } |
256 | } |
237 | |
257 | |
|
|
258 | // used for tags, intregers, element counts and so on |
238 | static void |
259 | static void |
239 | encode_uint (enc_t *enc, int major, UV len) |
260 | encode_uint (enc_t *enc, int major, UV len) |
240 | { |
261 | { |
241 | need (enc, 9); |
262 | need (enc, 9); |
242 | |
263 | |
… | |
… | |
273 | *enc->cur++ = len >> 8; |
294 | *enc->cur++ = len >> 8; |
274 | *enc->cur++ = len; |
295 | *enc->cur++ = len; |
275 | } |
296 | } |
276 | } |
297 | } |
277 | |
298 | |
|
|
299 | // encodes a perl value into a CBOR integer |
|
|
300 | ecb_inline void |
|
|
301 | encode_int (enc_t *enc, SV *sv) |
|
|
302 | { |
|
|
303 | if (SvIsUV (sv)) |
|
|
304 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
|
|
305 | else if (SvIVX (sv) >= 0) |
|
|
306 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
|
|
307 | else |
|
|
308 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
|
|
309 | } |
|
|
310 | |
278 | ecb_inline void |
311 | ecb_inline void |
279 | encode_tag (enc_t *enc, UV tag) |
312 | encode_tag (enc_t *enc, UV tag) |
280 | { |
313 | { |
281 | encode_uint (enc, MAJOR_TAG, tag); |
314 | encode_uint (enc, MAJOR_TAG, tag); |
282 | } |
315 | } |
… | |
… | |
343 | } |
376 | } |
344 | |
377 | |
345 | encode_str (enc, upgrade_utf8, utf8, str, len); |
378 | encode_str (enc, upgrade_utf8, utf8, str, len); |
346 | } |
379 | } |
347 | |
380 | |
348 | static void encode_sv (enc_t *enc, SV *sv); |
381 | ecb_inline void |
|
|
382 | encode_float16 (enc_t *enc, NV nv) |
|
|
383 | { |
|
|
384 | need (enc, 1+2); |
|
|
385 | |
|
|
386 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT16; |
|
|
387 | |
|
|
388 | uint16_t fp = ecb_float_to_binary16 (nv); |
|
|
389 | |
|
|
390 | if (!ecb_big_endian ()) |
|
|
391 | fp = ecb_bswap16 (fp); |
|
|
392 | |
|
|
393 | memcpy (enc->cur, &fp, 2); |
|
|
394 | enc->cur += 2; |
|
|
395 | } |
|
|
396 | |
|
|
397 | ecb_inline void |
|
|
398 | encode_float32 (enc_t *enc, NV nv) |
|
|
399 | { |
|
|
400 | need (enc, 1+4); |
|
|
401 | |
|
|
402 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
403 | |
|
|
404 | uint32_t fp = ecb_float_to_binary32 (nv); |
|
|
405 | |
|
|
406 | if (!ecb_big_endian ()) |
|
|
407 | fp = ecb_bswap32 (fp); |
|
|
408 | |
|
|
409 | memcpy (enc->cur, &fp, 4); |
|
|
410 | enc->cur += 4; |
|
|
411 | } |
|
|
412 | |
|
|
413 | ecb_inline void |
|
|
414 | encode_float64 (enc_t *enc, NV nv) |
|
|
415 | { |
|
|
416 | need (enc, 1+8); |
|
|
417 | |
|
|
418 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
419 | |
|
|
420 | uint64_t fp = ecb_double_to_binary64 (nv); |
|
|
421 | |
|
|
422 | if (!ecb_big_endian ()) |
|
|
423 | fp = ecb_bswap64 (fp); |
|
|
424 | |
|
|
425 | memcpy (enc->cur, &fp, 8); |
|
|
426 | enc->cur += 8; |
|
|
427 | } |
|
|
428 | |
|
|
429 | ecb_inline void |
|
|
430 | encode_bool (enc_t *enc, int istrue) |
|
|
431 | { |
|
|
432 | encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
433 | } |
|
|
434 | |
|
|
435 | // encodes an arrayref containing key-value pairs as CBOR map |
|
|
436 | ecb_inline void |
|
|
437 | encode_array_as_map (enc_t *enc, SV *sv) |
|
|
438 | { |
|
|
439 | if (enc->depth >= enc->cbor.max_depth) |
|
|
440 | croak (ERR_NESTING_EXCEEDED); |
|
|
441 | |
|
|
442 | ++enc->depth; |
|
|
443 | |
|
|
444 | // as_map does error checking for us, but we re-check in case |
|
|
445 | // things have changed. |
|
|
446 | |
|
|
447 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
448 | croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)"); |
|
|
449 | |
|
|
450 | AV *av = (AV *)SvRV (sv); |
|
|
451 | int i, len = av_len (av); |
|
|
452 | |
|
|
453 | if (!(len & 1)) |
|
|
454 | croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)"); |
|
|
455 | |
|
|
456 | encode_uint (enc, MAJOR_MAP, (len + 1) >> 1); |
|
|
457 | |
|
|
458 | for (i = 0; i <= len; ++i) |
|
|
459 | { |
|
|
460 | SV **svp = av_fetch (av, i, 0); |
|
|
461 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
|
|
462 | } |
|
|
463 | |
|
|
464 | --enc->depth; |
|
|
465 | } |
|
|
466 | |
|
|
467 | ecb_inline void |
|
|
468 | encode_forced (enc_t *enc, UV type, SV *sv) |
|
|
469 | { |
|
|
470 | switch (type) |
|
|
471 | { |
|
|
472 | case AS_CBOR: |
|
|
473 | { |
|
|
474 | STRLEN len; |
|
|
475 | char *str = SvPVbyte (sv, len); |
|
|
476 | |
|
|
477 | need (enc, len); |
|
|
478 | memcpy (enc->cur, str, len); |
|
|
479 | enc->cur += len; |
|
|
480 | } |
|
|
481 | break; |
|
|
482 | |
|
|
483 | case AS_BYTES: |
|
|
484 | { |
|
|
485 | STRLEN len; |
|
|
486 | char *str = SvPVbyte (sv, len); |
|
|
487 | encode_strref (enc, 0, 0, str, len); |
|
|
488 | } |
|
|
489 | break; |
|
|
490 | |
|
|
491 | case AS_TEXT: |
|
|
492 | { |
|
|
493 | STRLEN len; |
|
|
494 | char *str = SvPVutf8 (sv, len); |
|
|
495 | encode_strref (enc, 1, 1, str, len); |
|
|
496 | } |
|
|
497 | break; |
|
|
498 | |
|
|
499 | case AS_INT: encode_int (enc, sv); break; |
|
|
500 | |
|
|
501 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
|
|
502 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
|
|
503 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
|
|
504 | |
|
|
505 | case AS_MAP: encode_array_as_map (enc, sv); break; |
|
|
506 | |
|
|
507 | default: |
|
|
508 | croak ("encountered malformed CBOR::XS::Tagged object"); |
|
|
509 | } |
|
|
510 | } |
349 | |
511 | |
350 | static void |
512 | static void |
351 | encode_av (enc_t *enc, AV *av) |
513 | encode_av (enc_t *enc, AV *av) |
352 | { |
514 | { |
353 | int i, len = av_len (av); |
515 | int i, len = av_len (av); |
… | |
… | |
431 | |
593 | |
432 | HV *stash = SvSTASH (sv); |
594 | HV *stash = SvSTASH (sv); |
433 | |
595 | |
434 | if (stash == boolean_stash) |
596 | if (stash == boolean_stash) |
435 | { |
597 | { |
436 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
598 | encode_bool (enc, SvIV (sv)); |
437 | return; |
599 | return; |
438 | } |
600 | } |
439 | else if (stash == error_stash) |
601 | else if (stash == error_stash) |
440 | { |
602 | { |
441 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
603 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
… | |
… | |
444 | else if (stash == tagged_stash) |
606 | else if (stash == tagged_stash) |
445 | { |
607 | { |
446 | if (svt != SVt_PVAV) |
608 | if (svt != SVt_PVAV) |
447 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
609 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
448 | |
610 | |
|
|
611 | switch (av_len ((AV *)sv)) |
|
|
612 | { |
|
|
613 | case 2-1: |
|
|
614 | // actually a tagged value |
449 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
615 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
450 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
616 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
617 | break; |
|
|
618 | |
|
|
619 | case 3-1: |
|
|
620 | // a forced type [value, type, undef] |
|
|
621 | encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1)); |
|
|
622 | break; |
|
|
623 | |
|
|
624 | default: |
|
|
625 | croak ("encountered malformed CBOR::XS::Tagged object"); |
|
|
626 | } |
451 | |
627 | |
452 | return; |
628 | return; |
453 | } |
629 | } |
454 | } |
630 | } |
455 | |
631 | |
… | |
… | |
567 | |
743 | |
568 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
744 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
569 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
745 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
570 | //TODO: maybe I32? |
746 | //TODO: maybe I32? |
571 | else if (ecb_expect_false (nv == (float)nv)) |
747 | else if (ecb_expect_false (nv == (float)nv)) |
572 | { |
748 | encode_float32 (enc, nv); |
573 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
574 | |
|
|
575 | uint32_t fp = ecb_float_to_binary32 (nv); |
|
|
576 | |
|
|
577 | if (!ecb_big_endian ()) |
|
|
578 | fp = ecb_bswap32 (fp); |
|
|
579 | |
|
|
580 | memcpy (enc->cur, &fp, 4); |
|
|
581 | enc->cur += 4; |
|
|
582 | } |
|
|
583 | else |
749 | else |
584 | { |
750 | encode_float64 (enc, nv); |
585 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
586 | |
|
|
587 | uint64_t fp = ecb_double_to_binary64 (nv); |
|
|
588 | |
|
|
589 | if (!ecb_big_endian ()) |
|
|
590 | fp = ecb_bswap64 (fp); |
|
|
591 | |
|
|
592 | memcpy (enc->cur, &fp, 8); |
|
|
593 | enc->cur += 8; |
|
|
594 | } |
|
|
595 | } |
751 | } |
596 | |
752 | |
597 | static void |
753 | static void |
598 | encode_sv (enc_t *enc, SV *sv) |
754 | encode_sv (enc_t *enc, SV *sv) |
599 | { |
755 | { |
… | |
… | |
606 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
762 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
607 | } |
763 | } |
608 | else if (SvNOKp (sv)) |
764 | else if (SvNOKp (sv)) |
609 | encode_nv (enc, sv); |
765 | encode_nv (enc, sv); |
610 | else if (SvIOKp (sv)) |
766 | else if (SvIOKp (sv)) |
611 | { |
767 | encode_int (enc, sv); |
612 | if (SvIsUV (sv)) |
|
|
613 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
|
|
614 | else if (SvIVX (sv) >= 0) |
|
|
615 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
|
|
616 | else |
|
|
617 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
|
|
618 | } |
|
|
619 | else if (SvROK (sv)) |
768 | else if (SvROK (sv)) |
620 | encode_rv (enc, SvRV (sv)); |
769 | encode_rv (enc, SvRV (sv)); |
621 | else if (!SvOK (sv)) |
770 | else if (!SvOK (sv)) |
622 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
771 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
623 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
772 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
… | |
… | |
842 | |
991 | |
843 | WANT (len); |
992 | WANT (len); |
844 | dec->cur += len; |
993 | dec->cur += len; |
845 | |
994 | |
846 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
995 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
847 | if (!is_utf8_string (key, len)) |
996 | if (!cbor_is_utf8_string ((U8 *)key, len)) |
848 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
997 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
849 | |
998 | |
850 | hv_store (hv, key, -len, decode_sv (dec), 0); |
999 | hv_store (hv, key, -len, decode_sv (dec), 0); |
851 | |
1000 | |
852 | return; |
1001 | return; |
… | |
… | |
981 | } |
1130 | } |
982 | |
1131 | |
983 | if (utf8) |
1132 | if (utf8) |
984 | { |
1133 | { |
985 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
1134 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
986 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
1135 | if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv))) |
987 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
1136 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
988 | |
1137 | |
989 | SvUTF8_on (sv); |
1138 | SvUTF8_on (sv); |
990 | } |
1139 | } |
991 | |
1140 | |
… | |
… | |
1046 | case CBOR_TAG_VALUE_SHAREABLE: |
1195 | case CBOR_TAG_VALUE_SHAREABLE: |
1047 | { |
1196 | { |
1048 | if (ecb_expect_false (!dec->shareable)) |
1197 | if (ecb_expect_false (!dec->shareable)) |
1049 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1198 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1050 | |
1199 | |
1051 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
1200 | if (ecb_expect_false (dec->cbor.flags & (F_ALLOW_CYCLES | F_ALLOW_WEAK_CYCLES))) |
1052 | { |
1201 | { |
|
|
1202 | // if cycles are allowed, then we store an AV as value |
|
|
1203 | // while it is being decoded, and gather unresolved |
|
|
1204 | // references in it, to be re4solved after decoding. |
|
|
1205 | int idx, i; |
1053 | sv = newSV (0); |
1206 | AV *av = newAV (); |
1054 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
1207 | av_push (dec->shareable, (SV *)av); |
|
|
1208 | idx = AvFILLp (dec->shareable); |
1055 | |
1209 | |
1056 | SV *osv = decode_sv (dec); |
1210 | sv = decode_sv (dec); |
1057 | sv_setsv (sv, osv); |
1211 | |
|
|
1212 | // the AV now contains \undef for all unresolved references, |
|
|
1213 | // so we fix them up here. |
|
|
1214 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1215 | SvRV_set (AvARRAY (av)[i], SvREFCNT_inc_NN (SvRV (sv))); |
|
|
1216 | |
|
|
1217 | // weaken all recursive references |
|
|
1218 | if (dec->cbor.flags & F_ALLOW_WEAK_CYCLES) |
|
|
1219 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1220 | sv_rvweaken (AvARRAY (av)[i]); |
|
|
1221 | |
|
|
1222 | // now replace the AV by a reference to the completed value |
1058 | SvREFCNT_dec_NN (osv); |
1223 | SvREFCNT_dec_NN ((SV *)av); |
|
|
1224 | AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv); |
1059 | } |
1225 | } |
1060 | else |
1226 | else |
1061 | { |
1227 | { |
1062 | av_push (dec->shareable, &PL_sv_undef); |
1228 | av_push (dec->shareable, &PL_sv_undef); |
1063 | int idx = AvFILLp (dec->shareable); |
1229 | int idx = AvFILLp (dec->shareable); |
1064 | sv = decode_sv (dec); |
1230 | sv = decode_sv (dec); |
1065 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
1231 | AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv); |
1066 | } |
1232 | } |
1067 | } |
1233 | } |
1068 | break; |
1234 | break; |
1069 | |
1235 | |
1070 | case CBOR_TAG_VALUE_SHAREDREF: |
1236 | case CBOR_TAG_VALUE_SHAREDREF: |
… | |
… | |
1075 | UV idx = decode_uint (dec); |
1241 | UV idx = decode_uint (dec); |
1076 | |
1242 | |
1077 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1243 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1078 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1244 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1079 | |
1245 | |
1080 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1246 | sv = AvARRAY (dec->shareable)[idx]; |
1081 | |
1247 | |
1082 | if (sv == &PL_sv_undef) |
1248 | // reference to cycle, we create a new \undef and use that, and also |
|
|
1249 | // registerr it in the AV for later fixing |
|
|
1250 | if (ecb_expect_false (SvTYPE (sv) == SVt_PVAV)) |
|
|
1251 | { |
|
|
1252 | AV *av = (AV *)sv; |
|
|
1253 | sv = newRV_noinc (&PL_sv_undef); |
|
|
1254 | av_push (av, SvREFCNT_inc_NN (sv)); |
|
|
1255 | } |
|
|
1256 | else if (ecb_expect_false (sv == &PL_sv_undef)) // not yet decoded, but cycles not allowed |
1083 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
1257 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
|
|
1258 | else // we decoded the object earlier, no cycle |
|
|
1259 | sv = newSVsv (sv); |
1084 | } |
1260 | } |
1085 | break; |
1261 | break; |
1086 | |
1262 | |
1087 | case CBOR_TAG_PERL_OBJECT: |
1263 | case CBOR_TAG_PERL_OBJECT: |
1088 | { |
1264 | { |
… | |
… | |
1325 | SvREFCNT_dec_NN (sv); |
1501 | SvREFCNT_dec_NN (sv); |
1326 | |
1502 | |
1327 | if (dec.err_sv) |
1503 | if (dec.err_sv) |
1328 | sv_2mortal (dec.err_sv); |
1504 | sv_2mortal (dec.err_sv); |
1329 | |
1505 | |
1330 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1506 | croak ("%s, at offset %ld (octet 0x%02x)", dec.err, (long)(dec.cur - (U8 *)data), (int)(uint8_t)*dec.cur); |
1331 | } |
1507 | } |
1332 | |
1508 | |
1333 | sv = sv_2mortal (sv); |
1509 | sv = sv_2mortal (sv); |
1334 | |
1510 | |
1335 | return sv; |
1511 | return sv; |
… | |
… | |
1424 | |
1600 | |
1425 | break; |
1601 | break; |
1426 | |
1602 | |
1427 | case MAJOR_MAP >> MAJOR_SHIFT: |
1603 | case MAJOR_MAP >> MAJOR_SHIFT: |
1428 | len <<= 1; |
1604 | len <<= 1; |
|
|
1605 | /* FALLTHROUGH */ |
1429 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1606 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1430 | if (len) |
1607 | if (len) |
1431 | { |
1608 | { |
1432 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1609 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1433 | count = len + 1; |
1610 | count = len + 1; |
… | |
… | |
1505 | ))); |
1682 | ))); |
1506 | } |
1683 | } |
1507 | |
1684 | |
1508 | void shrink (CBOR *self, int enable = 1) |
1685 | void shrink (CBOR *self, int enable = 1) |
1509 | ALIAS: |
1686 | ALIAS: |
1510 | shrink = F_SHRINK |
1687 | shrink = F_SHRINK |
1511 | allow_unknown = F_ALLOW_UNKNOWN |
1688 | allow_unknown = F_ALLOW_UNKNOWN |
1512 | allow_sharing = F_ALLOW_SHARING |
1689 | allow_sharing = F_ALLOW_SHARING |
1513 | allow_cycles = F_ALLOW_CYCLES |
1690 | allow_cycles = F_ALLOW_CYCLES |
|
|
1691 | allow_weak_cycles = F_ALLOW_WEAK_CYCLES |
1514 | forbid_objects = F_FORBID_OBJECTS |
1692 | forbid_objects = F_FORBID_OBJECTS |
1515 | pack_strings = F_PACK_STRINGS |
1693 | pack_strings = F_PACK_STRINGS |
1516 | text_keys = F_TEXT_KEYS |
1694 | text_keys = F_TEXT_KEYS |
1517 | text_strings = F_TEXT_STRINGS |
1695 | text_strings = F_TEXT_STRINGS |
1518 | validate_utf8 = F_VALIDATE_UTF8 |
1696 | validate_utf8 = F_VALIDATE_UTF8 |
1519 | PPCODE: |
1697 | PPCODE: |
1520 | { |
1698 | { |
1521 | if (enable) |
1699 | if (enable) |
1522 | self->flags |= ix; |
1700 | self->flags |= ix; |
1523 | else |
1701 | else |
… | |
… | |
1526 | XPUSHs (ST (0)); |
1704 | XPUSHs (ST (0)); |
1527 | } |
1705 | } |
1528 | |
1706 | |
1529 | void get_shrink (CBOR *self) |
1707 | void get_shrink (CBOR *self) |
1530 | ALIAS: |
1708 | ALIAS: |
1531 | get_shrink = F_SHRINK |
1709 | get_shrink = F_SHRINK |
1532 | get_allow_unknown = F_ALLOW_UNKNOWN |
1710 | get_allow_unknown = F_ALLOW_UNKNOWN |
1533 | get_allow_sharing = F_ALLOW_SHARING |
1711 | get_allow_sharing = F_ALLOW_SHARING |
1534 | get_allow_cycles = F_ALLOW_CYCLES |
1712 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1713 | get_allow_weak_cycles = F_ALLOW_WEAK_CYCLES |
1535 | get_forbid_objects = F_FORBID_OBJECTS |
1714 | get_forbid_objects = F_FORBID_OBJECTS |
1536 | get_pack_strings = F_PACK_STRINGS |
1715 | get_pack_strings = F_PACK_STRINGS |
1537 | get_text_keys = F_TEXT_KEYS |
1716 | get_text_keys = F_TEXT_KEYS |
1538 | get_text_strings = F_TEXT_STRINGS |
1717 | get_text_strings = F_TEXT_STRINGS |
1539 | get_validate_utf8 = F_VALIDATE_UTF8 |
1718 | get_validate_utf8 = F_VALIDATE_UTF8 |
1540 | PPCODE: |
1719 | PPCODE: |
1541 | XPUSHs (boolSV (self->flags & ix)); |
1720 | XPUSHs (boolSV (self->flags & ix)); |
1542 | |
1721 | |
1543 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1722 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1544 | PPCODE: |
1723 | PPCODE: |