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.61 by root, Sat Nov 26 02:10:19 2016 UTC vs.
Revision 1.71 by root, Fri Mar 19 17:30:27 2021 UTC

21# define HvNAMELEN(hv) HvNAMELEN_get (hv) 21# define HvNAMELEN(hv) HvNAMELEN_get (hv)
22#endif 22#endif
23#ifndef HvNAMEUTF8 23#ifndef HvNAMEUTF8
24# define HvNAMEUTF8(hv) 0 24# define HvNAMEUTF8(hv) 0
25#endif 25#endif
26#ifndef SvREFCNT_inc_NN
27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
28#endif
26#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
27# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
28#endif 31#endif
29 32
30// known major and minor types 33// known major and minor types
94 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 97 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
95 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 98 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
96 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 99 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
97 100
98 CBOR_TAG_MAGIC = 55799, // self-describe cbor 101 CBOR_TAG_MAGIC = 55799, // self-describe cbor
102};
103
104// known forced types, also hardcoded in CBOR.pm
105enum
106{
107 AS_CBOR = 0,
108 AS_INT = 1,
109 AS_BYTES = 2,
110 AS_TEXT = 3,
111 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6,
114 AS_MAP = 7,
115 // possibly future enhancements: (generic) float, (generic) string
99}; 116};
100 117
101#define F_SHRINK 0x00000001UL 118#define F_SHRINK 0x00000001UL
102#define F_ALLOW_UNKNOWN 0x00000002UL 119#define F_ALLOW_UNKNOWN 0x00000002UL
103#define F_ALLOW_SHARING 0x00000004UL 120#define F_ALLOW_SHARING 0x00000004UL
184#endif 201#endif
185 } 202 }
186} 203}
187 204
188// minimum length of a string to be registered for stringref 205// minimum length of a string to be registered for stringref
189ecb_inline int 206ecb_inline STRLEN
190minimum_string_length (UV idx) 207minimum_string_length (UV idx)
191{ 208{
192
193 return idx <= 23 ? 3 209 return idx <= 23 ? 3
194 : idx <= 0xffU ? 4 210 : idx <= 0xffU ? 4
195 : idx <= 0xffffU ? 5 211 : idx <= 0xffffU ? 5
196 : idx <= 0xffffffffU ? 7 212 : idx <= 0xffffffffU ? 7
197 : 11; 213 : 11;
224 enc->cur = SvPVX (enc->sv) + cur; 240 enc->cur = SvPVX (enc->sv) + cur;
225 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 241 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
226 } 242 }
227} 243}
228 244
245static void encode_sv (enc_t *enc, SV *sv);
246
229ecb_inline void 247ecb_inline void
230encode_ch (enc_t *enc, char ch) 248encode_ch (enc_t *enc, char ch)
231{ 249{
232 need (enc, 1); 250 need (enc, 1);
233 *enc->cur++ = ch; 251 *enc->cur++ = ch;
234} 252}
235 253
254// used for tags, intregers, element counts and so on
236static void 255static void
237encode_uint (enc_t *enc, int major, UV len) 256encode_uint (enc_t *enc, int major, UV len)
238{ 257{
239 need (enc, 9); 258 need (enc, 9);
240 259
271 *enc->cur++ = len >> 8; 290 *enc->cur++ = len >> 8;
272 *enc->cur++ = len; 291 *enc->cur++ = len;
273 } 292 }
274} 293}
275 294
295// encodes a perl value into a CBOR integer
296ecb_inline void
297encode_int (enc_t *enc, SV *sv)
298{
299 if (SvIsUV (sv))
300 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
301 else if (SvIVX (sv) >= 0)
302 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
303 else
304 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
305}
306
276ecb_inline void 307ecb_inline void
277encode_tag (enc_t *enc, UV tag) 308encode_tag (enc_t *enc, UV tag)
278{ 309{
279 encode_uint (enc, MAJOR_TAG, tag); 310 encode_uint (enc, MAJOR_TAG, tag);
280} 311}
341 } 372 }
342 373
343 encode_str (enc, upgrade_utf8, utf8, str, len); 374 encode_str (enc, upgrade_utf8, utf8, str, len);
344} 375}
345 376
346static void encode_sv (enc_t *enc, SV *sv); 377ecb_inline void
378encode_float16 (enc_t *enc, NV nv)
379{
380 need (enc, 1+2);
381
382 *enc->cur++ = MAJOR_MISC | MISC_FLOAT16;
383
384 uint16_t fp = ecb_float_to_binary16 (nv);
385
386 if (!ecb_big_endian ())
387 fp = ecb_bswap16 (fp);
388
389 memcpy (enc->cur, &fp, 2);
390 enc->cur += 2;
391}
392
393ecb_inline void
394encode_float32 (enc_t *enc, NV nv)
395{
396 need (enc, 1+4);
397
398 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
399
400 uint32_t fp = ecb_float_to_binary32 (nv);
401
402 if (!ecb_big_endian ())
403 fp = ecb_bswap32 (fp);
404
405 memcpy (enc->cur, &fp, 4);
406 enc->cur += 4;
407}
408
409ecb_inline void
410encode_float64 (enc_t *enc, NV nv)
411{
412 need (enc, 1+8);
413
414 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
415
416 uint64_t fp = ecb_double_to_binary64 (nv);
417
418 if (!ecb_big_endian ())
419 fp = ecb_bswap64 (fp);
420
421 memcpy (enc->cur, &fp, 8);
422 enc->cur += 8;
423}
424
425ecb_inline void
426encode_bool (enc_t *enc, int istrue)
427{
428 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
429}
430
431// encodes an arrayref containing key-value pairs as CBOR map
432ecb_inline void
433encode_array_as_map (enc_t *enc, SV *sv)
434{
435 if (enc->depth >= enc->cbor.max_depth)
436 croak (ERR_NESTING_EXCEEDED);
437
438 ++enc->depth;
439
440 // as_map does error checking for us, but we re-check in case
441 // things have changed.
442
443 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
444 croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)");
445
446 AV *av = (AV *)SvRV (sv);
447 int i, len = av_len (av);
448
449 if (!(len & 1))
450 croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)");
451
452 encode_uint (enc, MAJOR_MAP, (len + 1) >> 1);
453
454 for (i = 0; i <= len; ++i)
455 {
456 SV **svp = av_fetch (av, i, 0);
457 encode_sv (enc, svp ? *svp : &PL_sv_undef);
458 }
459
460 --enc->depth;
461}
462
463ecb_inline void
464encode_forced (enc_t *enc, UV type, SV *sv)
465{
466 switch (type)
467 {
468 case AS_CBOR:
469 {
470 STRLEN len;
471 char *str = SvPVbyte (sv, len);
472
473 need (enc, len);
474 memcpy (enc->cur, str, len);
475 enc->cur += len;
476 }
477 break;
478
479 case AS_BYTES:
480 {
481 STRLEN len;
482 char *str = SvPVbyte (sv, len);
483 encode_strref (enc, 0, 0, str, len);
484 }
485 break;
486
487 case AS_TEXT:
488 {
489 STRLEN len;
490 char *str = SvPVutf8 (sv, len);
491 encode_strref (enc, 1, 1, str, len);
492 }
493 break;
494
495 case AS_INT: encode_int (enc, sv); break;
496
497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
503 default:
504 croak ("encountered malformed CBOR::XS::Tagged object");
505 }
506}
347 507
348static void 508static void
349encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
350{ 510{
351 int i, len = av_len (av); 511 int i, len = av_len (av);
355 515
356 ++enc->depth; 516 ++enc->depth;
357 517
358 encode_uint (enc, MAJOR_ARRAY, len + 1); 518 encode_uint (enc, MAJOR_ARRAY, len + 1);
359 519
360 if (SvMAGICAL (av)) 520 if (ecb_expect_false (SvMAGICAL (av)))
361 for (i = 0; i <= len; ++i) 521 for (i = 0; i <= len; ++i)
362 { 522 {
363 SV **svp = av_fetch (av, i, 0); 523 SV **svp = av_fetch (av, i, 0);
364 encode_sv (enc, svp ? *svp : &PL_sv_undef); 524 encode_sv (enc, svp ? *svp : &PL_sv_undef);
365 } 525 }
384 ++enc->depth; 544 ++enc->depth;
385 545
386 int pairs = hv_iterinit (hv); 546 int pairs = hv_iterinit (hv);
387 int mg = SvMAGICAL (hv); 547 int mg = SvMAGICAL (hv);
388 548
389 if (mg) 549 if (ecb_expect_false (mg))
390 encode_ch (enc, MAJOR_MAP | MINOR_INDEF); 550 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
391 else 551 else
392 encode_uint (enc, MAJOR_MAP, pairs); 552 encode_uint (enc, MAJOR_MAP, pairs);
393 553
394 while ((he = hv_iternext (hv))) 554 while ((he = hv_iternext (hv)))
399 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 559 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
400 560
401 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 561 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
402 } 562 }
403 563
404 if (mg) 564 if (ecb_expect_false (mg))
405 encode_ch (enc, MAJOR_MISC | MINOR_INDEF); 565 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
406 566
407 --enc->depth; 567 --enc->depth;
408} 568}
409 569
429 589
430 HV *stash = SvSTASH (sv); 590 HV *stash = SvSTASH (sv);
431 591
432 if (stash == boolean_stash) 592 if (stash == boolean_stash)
433 { 593 {
434 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 594 encode_bool (enc, SvIV (sv));
435 return; 595 return;
436 } 596 }
437 else if (stash == error_stash) 597 else if (stash == error_stash)
438 { 598 {
439 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 599 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
442 else if (stash == tagged_stash) 602 else if (stash == tagged_stash)
443 { 603 {
444 if (svt != SVt_PVAV) 604 if (svt != SVt_PVAV)
445 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 605 croak ("encountered CBOR::XS::Tagged object that isn't an array");
446 606
607 switch (av_len ((AV *)sv))
608 {
609 case 2-1:
610 // actually a tagged value
447 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); 611 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
448 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 612 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
613 break;
614
615 case 3-1:
616 // a forced type [value, type, undef]
617 encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1));
618 break;
619
620 default:
621 croak ("encountered malformed CBOR::XS::Tagged object");
622 }
449 623
450 return; 624 return;
451 } 625 }
452 } 626 }
453 627
454 if (ecb_expect_false (SvREFCNT (sv) > 1) 628 if (ecb_expect_false (SvREFCNT (sv) > 1)
455 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) 629 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
456 { 630 {
457 if (!enc->shareable) 631 if (ecb_expect_false (!enc->shareable))
458 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); 632 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
459 633
460 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); 634 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
461 635
462 if (SvOK (*svp)) 636 if (SvOK (*svp))
508 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 682 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
509 { 683 {
510 dSP; 684 dSP;
511 685
512 ENTER; SAVETMPS; 686 ENTER; SAVETMPS;
513 SAVESTACK_POS ();
514 PUSHMARK (SP); 687 PUSHMARK (SP);
515 EXTEND (SP, 2); 688 EXTEND (SP, 2);
516 // we re-bless the reference to get overload and other niceties right 689 // we re-bless the reference to get overload and other niceties right
517 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 690 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
518 PUSHs (sv_cbor); 691 PUSHs (sv_cbor);
527 700
528 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 701 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
529 encode_uint (enc, MAJOR_ARRAY, count + 1); 702 encode_uint (enc, MAJOR_ARRAY, count + 1);
530 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 703 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
531 704
532 while (count) 705 {
706 int i;
707
708 for (i = 0; i < count; ++i)
533 encode_sv (enc, SP[1 - count--]); 709 encode_sv (enc, SP[i + 1 - count]);
710
711 SP -= count;
712 }
534 713
535 PUTBACK; 714 PUTBACK;
536 715
537 FREETMPS; LEAVE; 716 FREETMPS; LEAVE;
538 } 717 }
560 739
561 if (ecb_expect_false (nv == (NV)(U32)nv)) 740 if (ecb_expect_false (nv == (NV)(U32)nv))
562 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 741 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
563 //TODO: maybe I32? 742 //TODO: maybe I32?
564 else if (ecb_expect_false (nv == (float)nv)) 743 else if (ecb_expect_false (nv == (float)nv))
565 { 744 encode_float32 (enc, nv);
566 uint32_t fp = ecb_float_to_binary32 (nv);
567
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
570 if (!ecb_big_endian ())
571 fp = ecb_bswap32 (fp);
572
573 memcpy (enc->cur, &fp, 4);
574 enc->cur += 4;
575 }
576 else 745 else
577 { 746 encode_float64 (enc, nv);
578 uint64_t fp = ecb_double_to_binary64 (nv);
579
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
582 if (!ecb_big_endian ())
583 fp = ecb_bswap64 (fp);
584
585 memcpy (enc->cur, &fp, 8);
586 enc->cur += 8;
587 }
588} 747}
589 748
590static void 749static void
591encode_sv (enc_t *enc, SV *sv) 750encode_sv (enc_t *enc, SV *sv)
592{ 751{
599 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); 758 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
600 } 759 }
601 else if (SvNOKp (sv)) 760 else if (SvNOKp (sv))
602 encode_nv (enc, sv); 761 encode_nv (enc, sv);
603 else if (SvIOKp (sv)) 762 else if (SvIOKp (sv))
604 { 763 encode_int (enc, sv);
605 if (SvIsUV (sv))
606 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
607 else if (SvIVX (sv) >= 0)
608 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
609 else
610 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
611 }
612 else if (SvROK (sv)) 764 else if (SvROK (sv))
613 encode_rv (enc, SvRV (sv)); 765 encode_rv (enc, SvRV (sv));
614 else if (!SvOK (sv)) 766 else if (!SvOK (sv))
615 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 767 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
616 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 768 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
623static SV * 775static SV *
624encode_cbor (SV *scalar, CBOR *cbor) 776encode_cbor (SV *scalar, CBOR *cbor)
625{ 777{
626 enc_t enc = { 0 }; 778 enc_t enc = { 0 };
627 779
628 enc.cbor = *cbor; 780 enc.cbor = *cbor;
629 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 781 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
630 enc.cur = SvPVX (enc.sv); 782 enc.cur = SvPVX (enc.sv);
631 enc.end = SvEND (enc.sv); 783 enc.end = SvEND (enc.sv);
632 784
633 SvPOK_only (enc.sv); 785 SvPOK_only (enc.sv);
634 786
635 if (cbor->flags & F_PACK_STRINGS) 787 if (cbor->flags & F_PACK_STRINGS)
636 { 788 {
694 846
695ecb_cold static void 847ecb_cold static void
696err_unexpected_end (dec_t *dec) 848err_unexpected_end (dec_t *dec)
697{ 849{
698 err_set (dec, "unexpected end of CBOR data"); 850 err_set (dec, "unexpected end of CBOR data");
699}
700
701ecb_cold static void
702err_nesting_exceeded (dec_t *dec)
703{
704 err_set (dec, ERR_NESTING_EXCEEDED);
705} 851}
706 852
707#define ERR_DO(do) SB do; goto fail; SE 853#define ERR_DO(do) SB do; goto fail; SE
708#define ERR(reason) ERR_DO (err_set (dec, reason)) 854#define ERR(reason) ERR_DO (err_set (dec, reason))
709#define ERR_ERRSV ERR_DO (err_errsv (dec)) 855#define ERR_ERRSV ERR_DO (err_errsv (dec))
784 930
785 for (;;) 931 for (;;)
786 { 932 {
787 WANT (1); 933 WANT (1);
788 934
789 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 935 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
790 { 936 {
791 ++dec->cur; 937 ++dec->cur;
792 break; 938 break;
793 } 939 }
794 940
808 954
809 DEC_DEC_DEPTH; 955 DEC_DEC_DEPTH;
810 return newRV_noinc ((SV *)av); 956 return newRV_noinc ((SV *)av);
811 957
812fail: 958fail:
813 SvREFCNT_dec (av); 959 SvREFCNT_dec_NN (av);
814 DEC_DEC_DEPTH; 960 DEC_DEC_DEPTH;
815 return &PL_sv_undef; 961 return &PL_sv_undef;
816} 962}
817 963
818static void 964static void
841 987
842 WANT (len); 988 WANT (len);
843 dec->cur += len; 989 dec->cur += len;
844 990
845 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
846 if (!is_utf8_string (key, len)) 992 if (!is_utf8_string ((U8 *)key, len))
847 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
848 994
849 hv_store (hv, key, -len, decode_sv (dec), 0); 995 hv_store (hv, key, -len, decode_sv (dec), 0);
850 996
851 return; 997 return;
881 1027
882 return; 1028 return;
883 } 1029 }
884 1030
885 hv_store_ent (hv, k, v, 0); 1031 hv_store_ent (hv, k, v, 0);
886 SvREFCNT_dec (k); 1032 SvREFCNT_dec_NN (k);
887 1033
888fail: 1034fail:
889 ; 1035 ;
890} 1036}
891 1037
902 1048
903 for (;;) 1049 for (;;)
904 { 1050 {
905 WANT (1); 1051 WANT (1);
906 1052
907 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 1053 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
908 { 1054 {
909 ++dec->cur; 1055 ++dec->cur;
910 break; 1056 break;
911 } 1057 }
912 1058
925 1071
926 DEC_DEC_DEPTH; 1072 DEC_DEC_DEPTH;
927 return newRV_noinc ((SV *)hv); 1073 return newRV_noinc ((SV *)hv);
928 1074
929fail: 1075fail:
930 SvREFCNT_dec (hv); 1076 SvREFCNT_dec_NN (hv);
931 DEC_DEC_DEPTH; 1077 DEC_DEC_DEPTH;
932 return &PL_sv_undef; 1078 return &PL_sv_undef;
933} 1079}
934 1080
935static SV * 1081static SV *
936decode_str (dec_t *dec, int utf8) 1082decode_str (dec_t *dec, int utf8)
937{ 1083{
938 SV *sv = 0; 1084 SV *sv = 0;
939 1085
940 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 1086 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
941 { 1087 {
942 // indefinite length strings 1088 // indefinite length strings
943 ++dec->cur; 1089 ++dec->cur;
944 1090
945 U8 major = *dec->cur & MAJOR_MISC; 1091 U8 major = *dec->cur & MAJOR_MISC;
1013 sv = newRV_noinc (decode_sv (dec)); 1159 sv = newRV_noinc (decode_sv (dec));
1014 break; 1160 break;
1015 1161
1016 case CBOR_TAG_STRINGREF_NAMESPACE: 1162 case CBOR_TAG_STRINGREF_NAMESPACE:
1017 { 1163 {
1018 // do nmot use SAVETMPS/FREETMPS, as these will 1164 // do not use SAVETMPS/FREETMPS, as these will
1019 // erase mortalised caches, e.g. "shareable" 1165 // erase mortalised caches, e.g. "shareable"
1020 ENTER; 1166 ENTER;
1021 1167
1022 SAVESPTR (dec->stringref); 1168 SAVESPTR (dec->stringref);
1023 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1169 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
1033 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1179 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1034 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1180 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
1035 1181
1036 UV idx = decode_uint (dec); 1182 UV idx = decode_uint (dec);
1037 1183
1038 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1184 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
1039 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); 1185 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
1040 1186
1041 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1187 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
1042 } 1188 }
1043 break; 1189 break;
1071 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1217 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1072 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1218 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
1073 1219
1074 UV idx = decode_uint (dec); 1220 UV idx = decode_uint (dec);
1075 1221
1076 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1222 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1077 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1223 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1078 1224
1079 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1225 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1080 1226
1081 if (sv == &PL_sv_undef) 1227 if (sv == &PL_sv_undef)
1127 { 1273 {
1128 FREETMPS; LEAVE; 1274 FREETMPS; LEAVE;
1129 ERR_ERRSV; 1275 ERR_ERRSV;
1130 } 1276 }
1131 1277
1132 SvREFCNT_dec (sv); 1278 SvREFCNT_dec_NN (sv);
1133 sv = SvREFCNT_inc (POPs); 1279 sv = SvREFCNT_inc (POPs);
1134 1280
1135 PUTBACK; 1281 PUTBACK;
1136 1282
1137 FREETMPS; LEAVE; 1283 FREETMPS; LEAVE;
1145 1291
1146 sv = decode_sv (dec); 1292 sv = decode_sv (dec);
1147 1293
1148 dSP; 1294 dSP;
1149 ENTER; SAVETMPS; 1295 ENTER; SAVETMPS;
1150 SAVESTACK_POS ();
1151 PUSHMARK (SP); 1296 PUSHMARK (SP);
1152 EXTEND (SP, 2); 1297 EXTEND (SP, 2);
1153 PUSHs (tag_sv); 1298 PUSHs (tag_sv);
1154 PUSHs (sv); 1299 PUSHs (sv);
1155 1300
1157 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1302 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1158 SPAGAIN; 1303 SPAGAIN;
1159 1304
1160 if (SvTRUE (ERRSV)) 1305 if (SvTRUE (ERRSV))
1161 { 1306 {
1162 SvREFCNT_dec (tag_sv); 1307 SvREFCNT_dec_NN (tag_sv);
1163 FREETMPS; LEAVE; 1308 FREETMPS; LEAVE;
1164 ERR_ERRSV; 1309 ERR_ERRSV;
1165 } 1310 }
1166 1311
1167 if (count) 1312 if (count)
1168 { 1313 {
1169 SvREFCNT_dec (tag_sv); 1314 SvREFCNT_dec_NN (tag_sv);
1170 SvREFCNT_dec (sv); 1315 SvREFCNT_dec_NN (sv);
1171 sv = SvREFCNT_inc (POPs); 1316 sv = SvREFCNT_inc_NN (TOPs);
1317 SP -= count;
1172 } 1318 }
1173 else 1319 else
1174 { 1320 {
1175 AV *av = newAV (); 1321 AV *av = newAV ();
1176 av_push (av, tag_sv); 1322 av_push (av, tag_sv);
1319 for (i = av_len (dec.shareable) + 1; i--; ) 1465 for (i = av_len (dec.shareable) + 1; i--; )
1320 if ((svp = av_fetch (dec.shareable, i, 0))) 1466 if ((svp = av_fetch (dec.shareable, i, 0)))
1321 sv_setsv (*svp, &PL_sv_undef); 1467 sv_setsv (*svp, &PL_sv_undef);
1322 } 1468 }
1323 1469
1324 SvREFCNT_dec (sv); 1470 SvREFCNT_dec_NN (sv);
1325 1471
1326 if (dec.err_sv) 1472 if (dec.err_sv)
1327 sv_2mortal (dec.err_sv); 1473 sv_2mortal (dec.err_sv);
1328 1474
1329 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1475 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1423 1569
1424 break; 1570 break;
1425 1571
1426 case MAJOR_MAP >> MAJOR_SHIFT: 1572 case MAJOR_MAP >> MAJOR_SHIFT:
1427 len <<= 1; 1573 len <<= 1;
1574 /* FALLTHROUGH */
1428 case MAJOR_ARRAY >> MAJOR_SHIFT: 1575 case MAJOR_ARRAY >> MAJOR_SHIFT:
1429 if (len) 1576 if (len)
1430 { 1577 {
1431 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1578 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1432 count = len + 1; 1579 count = len + 1;
1672 cbor_init (&cbor); 1819 cbor_init (&cbor);
1673 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1820 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1674 XPUSHs (cborstr); 1821 XPUSHs (cborstr);
1675} 1822}
1676 1823
1824#ifdef __AFL_COMPILER
1825
1826void
1827afl_init ()
1828 CODE:
1829 __AFL_INIT ();
1830
1831int
1832afl_loop (unsigned int count = 10000)
1833 CODE:
1834 RETVAL = __AFL_LOOP (count);
1835 OUTPUT:
1836 RETVAL
1837
1838#endif
1839

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines