ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.63 by root, Sat Nov 26 04:50:58 2016 UTC vs.
Revision 1.80 by root, Fri Sep 8 20:03:06 2023 UTC

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
34enum cbor_type 37enum 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
108enum
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
192ecb_inline int 210ecb_inline STRLEN
193minimum_string_length (UV idx) 211minimum_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
249static void encode_sv (enc_t *enc, SV *sv);
250
231ecb_inline void 251ecb_inline void
232encode_ch (enc_t *enc, char ch) 252encode_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
238static void 259static void
239encode_uint (enc_t *enc, int major, UV len) 260encode_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
300ecb_inline void
301encode_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
278ecb_inline void 311ecb_inline void
279encode_tag (enc_t *enc, UV tag) 312encode_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
348static void encode_sv (enc_t *enc, SV *sv); 381ecb_inline void
382encode_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
397ecb_inline void
398encode_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
413ecb_inline void
414encode_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
429ecb_inline void
430encode_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
436ecb_inline void
437encode_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
467ecb_inline void
468encode_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
350static void 512static void
351encode_av (enc_t *enc, AV *av) 513encode_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
510 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 686 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
511 { 687 {
512 dSP; 688 dSP;
513 689
514 ENTER; SAVETMPS; 690 ENTER; SAVETMPS;
515 SAVESTACK_POS ();
516 PUSHMARK (SP); 691 PUSHMARK (SP);
517 EXTEND (SP, 2); 692 EXTEND (SP, 2);
518 // we re-bless the reference to get overload and other niceties right 693 // we re-bless the reference to get overload and other niceties right
519 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 694 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
520 PUSHs (sv_cbor); 695 PUSHs (sv_cbor);
529 704
530 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 705 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
531 encode_uint (enc, MAJOR_ARRAY, count + 1); 706 encode_uint (enc, MAJOR_ARRAY, count + 1);
532 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 707 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
533 708
534 while (count) 709 {
710 int i;
711
712 for (i = 0; i < count; ++i)
535 encode_sv (enc, SP[1 - count--]); 713 encode_sv (enc, SP[i + 1 - count]);
714
715 SP -= count;
716 }
536 717
537 PUTBACK; 718 PUTBACK;
538 719
539 FREETMPS; LEAVE; 720 FREETMPS; LEAVE;
540 } 721 }
562 743
563 if (ecb_expect_false (nv == (NV)(U32)nv)) 744 if (ecb_expect_false (nv == (NV)(U32)nv))
564 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 745 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
565 //TODO: maybe I32? 746 //TODO: maybe I32?
566 else if (ecb_expect_false (nv == (float)nv)) 747 else if (ecb_expect_false (nv == (float)nv))
567 { 748 encode_float32 (enc, nv);
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
570 uint32_t fp = ecb_float_to_binary32 (nv);
571
572 if (!ecb_big_endian ())
573 fp = ecb_bswap32 (fp);
574
575 memcpy (enc->cur, &fp, 4);
576 enc->cur += 4;
577 }
578 else 749 else
579 { 750 encode_float64 (enc, nv);
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
582 uint64_t fp = ecb_double_to_binary64 (nv);
583
584 if (!ecb_big_endian ())
585 fp = ecb_bswap64 (fp);
586
587 memcpy (enc->cur, &fp, 8);
588 enc->cur += 8;
589 }
590} 751}
591 752
592static void 753static void
593encode_sv (enc_t *enc, SV *sv) 754encode_sv (enc_t *enc, SV *sv)
594{ 755{
601 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);
602 } 763 }
603 else if (SvNOKp (sv)) 764 else if (SvNOKp (sv))
604 encode_nv (enc, sv); 765 encode_nv (enc, sv);
605 else if (SvIOKp (sv)) 766 else if (SvIOKp (sv))
606 { 767 encode_int (enc, sv);
607 if (SvIsUV (sv))
608 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
609 else if (SvIVX (sv) >= 0)
610 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
611 else
612 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
613 }
614 else if (SvROK (sv)) 768 else if (SvROK (sv))
615 encode_rv (enc, SvRV (sv)); 769 encode_rv (enc, SvRV (sv));
616 else if (!SvOK (sv)) 770 else if (!SvOK (sv))
617 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 771 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
618 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 772 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
780 934
781 for (;;) 935 for (;;)
782 { 936 {
783 WANT (1); 937 WANT (1);
784 938
785 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 939 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
786 { 940 {
787 ++dec->cur; 941 ++dec->cur;
788 break; 942 break;
789 } 943 }
790 944
837 991
838 WANT (len); 992 WANT (len);
839 dec->cur += len; 993 dec->cur += len;
840 994
841 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 995 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
842 if (!is_utf8_string (key, len)) 996 if (!cbor_is_utf8_string ((U8 *)key, len))
843 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 997 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
844 998
845 hv_store (hv, key, -len, decode_sv (dec), 0); 999 hv_store (hv, key, -len, decode_sv (dec), 0);
846 1000
847 return; 1001 return;
898 1052
899 for (;;) 1053 for (;;)
900 { 1054 {
901 WANT (1); 1055 WANT (1);
902 1056
903 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 1057 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
904 { 1058 {
905 ++dec->cur; 1059 ++dec->cur;
906 break; 1060 break;
907 } 1061 }
908 1062
976 } 1130 }
977 1131
978 if (utf8) 1132 if (utf8)
979 { 1133 {
980 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1134 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
981 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1135 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
982 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1136 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
983 1137
984 SvUTF8_on (sv); 1138 SvUTF8_on (sv);
985 } 1139 }
986 1140
1029 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1183 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1030 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1184 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
1031 1185
1032 UV idx = decode_uint (dec); 1186 UV idx = decode_uint (dec);
1033 1187
1034 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1188 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
1035 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); 1189 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
1036 1190
1037 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1191 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
1038 } 1192 }
1039 break; 1193 break;
1041 case CBOR_TAG_VALUE_SHAREABLE: 1195 case CBOR_TAG_VALUE_SHAREABLE:
1042 { 1196 {
1043 if (ecb_expect_false (!dec->shareable)) 1197 if (ecb_expect_false (!dec->shareable))
1044 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1198 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
1045 1199
1046 if (dec->cbor.flags & F_ALLOW_CYCLES) 1200 if (ecb_expect_false (dec->cbor.flags & (F_ALLOW_CYCLES | F_ALLOW_WEAK_CYCLES)))
1047 { 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;
1048 sv = newSV (0); 1206 AV *av = newAV ();
1049 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1207 av_push (dec->shareable, (SV *)av);
1208 idx = AvFILLp (dec->shareable);
1050 1209
1051 SV *osv = decode_sv (dec); 1210 sv = decode_sv (dec);
1052 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
1053 SvREFCNT_dec_NN (osv); 1223 SvREFCNT_dec_NN ((SV *)av);
1224 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1054 } 1225 }
1055 else 1226 else
1056 { 1227 {
1057 av_push (dec->shareable, &PL_sv_undef); 1228 av_push (dec->shareable, &PL_sv_undef);
1058 int idx = AvFILLp (dec->shareable); 1229 int idx = AvFILLp (dec->shareable);
1059 sv = decode_sv (dec); 1230 sv = decode_sv (dec);
1060 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); 1231 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1061 } 1232 }
1062 } 1233 }
1063 break; 1234 break;
1064 1235
1065 case CBOR_TAG_VALUE_SHAREDREF: 1236 case CBOR_TAG_VALUE_SHAREDREF:
1067 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1238 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1068 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1239 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
1069 1240
1070 UV idx = decode_uint (dec); 1241 UV idx = decode_uint (dec);
1071 1242
1072 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1243 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1073 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1244 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1074 1245
1075 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1246 sv = AvARRAY (dec->shareable)[idx];
1076 1247
1077 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
1078 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);
1079 } 1260 }
1080 break; 1261 break;
1081 1262
1082 case CBOR_TAG_PERL_OBJECT: 1263 case CBOR_TAG_PERL_OBJECT:
1083 { 1264 {
1141 1322
1142 sv = decode_sv (dec); 1323 sv = decode_sv (dec);
1143 1324
1144 dSP; 1325 dSP;
1145 ENTER; SAVETMPS; 1326 ENTER; SAVETMPS;
1146 SAVESTACK_POS ();
1147 PUSHMARK (SP); 1327 PUSHMARK (SP);
1148 EXTEND (SP, 2); 1328 EXTEND (SP, 2);
1149 PUSHs (tag_sv); 1329 PUSHs (tag_sv);
1150 PUSHs (sv); 1330 PUSHs (sv);
1151 1331
1162 1342
1163 if (count) 1343 if (count)
1164 { 1344 {
1165 SvREFCNT_dec_NN (tag_sv); 1345 SvREFCNT_dec_NN (tag_sv);
1166 SvREFCNT_dec_NN (sv); 1346 SvREFCNT_dec_NN (sv);
1167 sv = SvREFCNT_inc_NN (POPs); 1347 sv = SvREFCNT_inc_NN (TOPs);
1348 SP -= count;
1168 } 1349 }
1169 else 1350 else
1170 { 1351 {
1171 AV *av = newAV (); 1352 AV *av = newAV ();
1172 av_push (av, tag_sv); 1353 av_push (av, tag_sv);
1320 SvREFCNT_dec_NN (sv); 1501 SvREFCNT_dec_NN (sv);
1321 1502
1322 if (dec.err_sv) 1503 if (dec.err_sv)
1323 sv_2mortal (dec.err_sv); 1504 sv_2mortal (dec.err_sv);
1324 1505
1325 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);
1326 } 1507 }
1327 1508
1328 sv = sv_2mortal (sv); 1509 sv = sv_2mortal (sv);
1329 1510
1330 return sv; 1511 return sv;
1419 1600
1420 break; 1601 break;
1421 1602
1422 case MAJOR_MAP >> MAJOR_SHIFT: 1603 case MAJOR_MAP >> MAJOR_SHIFT:
1423 len <<= 1; 1604 len <<= 1;
1605 /* FALLTHROUGH */
1424 case MAJOR_ARRAY >> MAJOR_SHIFT: 1606 case MAJOR_ARRAY >> MAJOR_SHIFT:
1425 if (len) 1607 if (len)
1426 { 1608 {
1427 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1609 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1428 count = len + 1; 1610 count = len + 1;
1500 ))); 1682 )));
1501} 1683}
1502 1684
1503void shrink (CBOR *self, int enable = 1) 1685void shrink (CBOR *self, int enable = 1)
1504 ALIAS: 1686 ALIAS:
1505 shrink = F_SHRINK 1687 shrink = F_SHRINK
1506 allow_unknown = F_ALLOW_UNKNOWN 1688 allow_unknown = F_ALLOW_UNKNOWN
1507 allow_sharing = F_ALLOW_SHARING 1689 allow_sharing = F_ALLOW_SHARING
1508 allow_cycles = F_ALLOW_CYCLES 1690 allow_cycles = F_ALLOW_CYCLES
1691 allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1509 forbid_objects = F_FORBID_OBJECTS 1692 forbid_objects = F_FORBID_OBJECTS
1510 pack_strings = F_PACK_STRINGS 1693 pack_strings = F_PACK_STRINGS
1511 text_keys = F_TEXT_KEYS 1694 text_keys = F_TEXT_KEYS
1512 text_strings = F_TEXT_STRINGS 1695 text_strings = F_TEXT_STRINGS
1513 validate_utf8 = F_VALIDATE_UTF8 1696 validate_utf8 = F_VALIDATE_UTF8
1514 PPCODE: 1697 PPCODE:
1515{ 1698{
1516 if (enable) 1699 if (enable)
1517 self->flags |= ix; 1700 self->flags |= ix;
1518 else 1701 else
1521 XPUSHs (ST (0)); 1704 XPUSHs (ST (0));
1522} 1705}
1523 1706
1524void get_shrink (CBOR *self) 1707void get_shrink (CBOR *self)
1525 ALIAS: 1708 ALIAS:
1526 get_shrink = F_SHRINK 1709 get_shrink = F_SHRINK
1527 get_allow_unknown = F_ALLOW_UNKNOWN 1710 get_allow_unknown = F_ALLOW_UNKNOWN
1528 get_allow_sharing = F_ALLOW_SHARING 1711 get_allow_sharing = F_ALLOW_SHARING
1529 get_allow_cycles = F_ALLOW_CYCLES 1712 get_allow_cycles = F_ALLOW_CYCLES
1713 get_allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1530 get_forbid_objects = F_FORBID_OBJECTS 1714 get_forbid_objects = F_FORBID_OBJECTS
1531 get_pack_strings = F_PACK_STRINGS 1715 get_pack_strings = F_PACK_STRINGS
1532 get_text_keys = F_TEXT_KEYS 1716 get_text_keys = F_TEXT_KEYS
1533 get_text_strings = F_TEXT_STRINGS 1717 get_text_strings = F_TEXT_STRINGS
1534 get_validate_utf8 = F_VALIDATE_UTF8 1718 get_validate_utf8 = F_VALIDATE_UTF8
1535 PPCODE: 1719 PPCODE:
1536 XPUSHs (boolSV (self->flags & ix)); 1720 XPUSHs (boolSV (self->flags & ix));
1537 1721
1538void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1722void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1539 PPCODE: 1723 PPCODE:
1668 cbor_init (&cbor); 1852 cbor_init (&cbor);
1669 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1853 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1670 XPUSHs (cborstr); 1854 XPUSHs (cborstr);
1671} 1855}
1672 1856
1857#ifdef __AFL_COMPILER
1858
1859void
1860afl_init ()
1861 CODE:
1862 __AFL_INIT ();
1863
1864int
1865afl_loop (unsigned int count = 10000)
1866 CODE:
1867 RETVAL = __AFL_LOOP (count);
1868 OUTPUT:
1869 RETVAL
1870
1871#endif
1872

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines