… | |
… | |
6 | #include <string.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
|
|
11 | #include <inttypes.h> |
11 | |
12 | |
12 | #define ECB_NO_THREADS 1 |
13 | #define ECB_NO_THREADS 1 |
13 | #include "ecb.h" |
14 | #include "ecb.h" |
14 | |
15 | |
15 | // compatibility with perl <5.18 |
16 | // compatibility with perl <5.18 |
… | |
… | |
20 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
21 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
21 | #endif |
22 | #endif |
22 | #ifndef HvNAMEUTF8 |
23 | #ifndef HvNAMEUTF8 |
23 | # define HvNAMEUTF8(hv) 0 |
24 | # define HvNAMEUTF8(hv) 0 |
24 | #endif |
25 | #endif |
|
|
26 | #ifndef SvREFCNT_inc_NN |
|
|
27 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
28 | #endif |
25 | #ifndef SvREFCNT_dec_NN |
29 | #ifndef SvREFCNT_dec_NN |
26 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
27 | #endif |
31 | #endif |
28 | |
32 | |
29 | // known major and minor types |
33 | // known major and minor types |
… | |
… | |
99 | |
103 | |
100 | #define F_SHRINK 0x00000001UL |
104 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
105 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
106 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
107 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
108 | #define F_FORBID_OBJECTS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
109 | #define F_PACK_STRINGS 0x00000020UL |
105 | #define F_TEXT_KEYS 0x00000020UL |
110 | #define F_TEXT_KEYS 0x00000040UL |
106 | #define F_TEXT_STRINGS 0x00000040UL |
111 | #define F_TEXT_STRINGS 0x00000080UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
112 | #define F_VALIDATE_UTF8 0x00000100UL |
108 | |
113 | |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
114 | #define INIT_SIZE 32 // initial scalar size to be allocated |
110 | |
115 | |
111 | #define SB do { |
116 | #define SB do { |
112 | #define SE } while (0) |
117 | #define SE } while (0) |
… | |
… | |
185 | |
190 | |
186 | // minimum length of a string to be registered for stringref |
191 | // minimum length of a string to be registered for stringref |
187 | ecb_inline int |
192 | ecb_inline int |
188 | minimum_string_length (UV idx) |
193 | minimum_string_length (UV idx) |
189 | { |
194 | { |
190 | return idx > 23 |
195 | return idx <= 23 ? 3 |
191 | ? idx > 0xffU |
196 | : idx <= 0xffU ? 4 |
192 | ? idx > 0xffffU |
197 | : idx <= 0xffffU ? 5 |
193 | ? idx > 0xffffffffU |
198 | : idx <= 0xffffffffU ? 7 |
194 | ? 11 |
199 | : 11; |
195 | : 7 |
|
|
196 | : 5 |
|
|
197 | : 4 |
|
|
198 | : 3; |
|
|
199 | } |
200 | } |
200 | |
201 | |
201 | ///////////////////////////////////////////////////////////////////////////// |
202 | ///////////////////////////////////////////////////////////////////////////// |
202 | // encoder |
203 | // encoder |
203 | |
204 | |
… | |
… | |
216 | } enc_t; |
217 | } enc_t; |
217 | |
218 | |
218 | ecb_inline void |
219 | ecb_inline void |
219 | need (enc_t *enc, STRLEN len) |
220 | need (enc_t *enc, STRLEN len) |
220 | { |
221 | { |
221 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
222 | if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
222 | { |
223 | { |
223 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
224 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
224 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
225 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
225 | enc->cur = SvPVX (enc->sv) + cur; |
226 | enc->cur = SvPVX (enc->sv) + cur; |
226 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
227 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
… | |
… | |
356 | |
357 | |
357 | ++enc->depth; |
358 | ++enc->depth; |
358 | |
359 | |
359 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
360 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
360 | |
361 | |
361 | if (SvMAGICAL (av)) |
362 | if (ecb_expect_false (SvMAGICAL (av))) |
362 | for (i = 0; i <= len; ++i) |
363 | for (i = 0; i <= len; ++i) |
363 | { |
364 | { |
364 | SV **svp = av_fetch (av, i, 0); |
365 | SV **svp = av_fetch (av, i, 0); |
365 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
366 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
366 | } |
367 | } |
… | |
… | |
385 | ++enc->depth; |
386 | ++enc->depth; |
386 | |
387 | |
387 | int pairs = hv_iterinit (hv); |
388 | int pairs = hv_iterinit (hv); |
388 | int mg = SvMAGICAL (hv); |
389 | int mg = SvMAGICAL (hv); |
389 | |
390 | |
390 | if (mg) |
391 | if (ecb_expect_false (mg)) |
391 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
392 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
392 | else |
393 | else |
393 | encode_uint (enc, MAJOR_MAP, pairs); |
394 | encode_uint (enc, MAJOR_MAP, pairs); |
394 | |
395 | |
395 | while ((he = hv_iternext (hv))) |
396 | while ((he = hv_iternext (hv))) |
… | |
… | |
400 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
401 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
401 | |
402 | |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
403 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
403 | } |
404 | } |
404 | |
405 | |
405 | if (mg) |
406 | if (ecb_expect_false (mg)) |
406 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
407 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
407 | |
408 | |
408 | --enc->depth; |
409 | --enc->depth; |
409 | } |
410 | } |
410 | |
411 | |
… | |
… | |
453 | } |
454 | } |
454 | |
455 | |
455 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
456 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
456 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
457 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
457 | { |
458 | { |
458 | if (!enc->shareable) |
459 | if (ecb_expect_false (!enc->shareable)) |
459 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
460 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
460 | |
461 | |
461 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
462 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
462 | |
463 | |
463 | if (SvOK (*svp)) |
464 | if (SvOK (*svp)) |
… | |
… | |
477 | if (ecb_expect_false (SvOBJECT (sv))) |
478 | if (ecb_expect_false (SvOBJECT (sv))) |
478 | { |
479 | { |
479 | HV *stash = SvSTASH (sv); |
480 | HV *stash = SvSTASH (sv); |
480 | GV *method; |
481 | GV *method; |
481 | |
482 | |
|
|
483 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
484 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
485 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
486 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
483 | { |
487 | { |
484 | dSP; |
488 | dSP; |
485 | |
489 | |
486 | ENTER; SAVETMPS; |
490 | ENTER; SAVETMPS; |
487 | PUSHMARK (SP); |
491 | PUSHMARK (SP); |
… | |
… | |
506 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
510 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
507 | { |
511 | { |
508 | dSP; |
512 | dSP; |
509 | |
513 | |
510 | ENTER; SAVETMPS; |
514 | ENTER; SAVETMPS; |
511 | SAVESTACK_POS (); |
|
|
512 | PUSHMARK (SP); |
515 | PUSHMARK (SP); |
513 | EXTEND (SP, 2); |
516 | EXTEND (SP, 2); |
514 | // we re-bless the reference to get overload and other niceties right |
517 | // we re-bless the reference to get overload and other niceties right |
515 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
518 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
516 | PUSHs (sv_cbor); |
519 | PUSHs (sv_cbor); |
… | |
… | |
525 | |
528 | |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
529 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
530 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
531 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
529 | |
532 | |
530 | while (count) |
533 | { |
|
|
534 | int i; |
|
|
535 | |
|
|
536 | for (i = 0; i < count; ++i) |
531 | encode_sv (enc, SP[1 - count--]); |
537 | encode_sv (enc, SP[i + 1 - count]); |
|
|
538 | |
|
|
539 | SP -= count; |
|
|
540 | } |
532 | |
541 | |
533 | PUTBACK; |
542 | PUTBACK; |
534 | |
543 | |
535 | FREETMPS; LEAVE; |
544 | FREETMPS; LEAVE; |
536 | } |
545 | } |
… | |
… | |
559 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
568 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
560 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
569 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
561 | //TODO: maybe I32? |
570 | //TODO: maybe I32? |
562 | else if (ecb_expect_false (nv == (float)nv)) |
571 | else if (ecb_expect_false (nv == (float)nv)) |
563 | { |
572 | { |
|
|
573 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
574 | |
564 | uint32_t fp = ecb_float_to_binary32 (nv); |
575 | uint32_t fp = ecb_float_to_binary32 (nv); |
565 | |
|
|
566 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
567 | |
576 | |
568 | if (!ecb_big_endian ()) |
577 | if (!ecb_big_endian ()) |
569 | fp = ecb_bswap32 (fp); |
578 | fp = ecb_bswap32 (fp); |
570 | |
579 | |
571 | memcpy (enc->cur, &fp, 4); |
580 | memcpy (enc->cur, &fp, 4); |
572 | enc->cur += 4; |
581 | enc->cur += 4; |
573 | } |
582 | } |
574 | else |
583 | else |
575 | { |
584 | { |
|
|
585 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
586 | |
576 | uint64_t fp = ecb_double_to_binary64 (nv); |
587 | uint64_t fp = ecb_double_to_binary64 (nv); |
577 | |
|
|
578 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
579 | |
588 | |
580 | if (!ecb_big_endian ()) |
589 | if (!ecb_big_endian ()) |
581 | fp = ecb_bswap64 (fp); |
590 | fp = ecb_bswap64 (fp); |
582 | |
591 | |
583 | memcpy (enc->cur, &fp, 8); |
592 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
621 | static SV * |
630 | static SV * |
622 | encode_cbor (SV *scalar, CBOR *cbor) |
631 | encode_cbor (SV *scalar, CBOR *cbor) |
623 | { |
632 | { |
624 | enc_t enc = { 0 }; |
633 | enc_t enc = { 0 }; |
625 | |
634 | |
626 | enc.cbor = *cbor; |
635 | enc.cbor = *cbor; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
636 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
637 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
638 | enc.end = SvEND (enc.sv); |
630 | |
639 | |
631 | SvPOK_only (enc.sv); |
640 | SvPOK_only (enc.sv); |
632 | |
641 | |
633 | if (cbor->flags & F_PACK_STRINGS) |
642 | if (cbor->flags & F_PACK_STRINGS) |
634 | { |
643 | { |
… | |
… | |
694 | err_unexpected_end (dec_t *dec) |
703 | err_unexpected_end (dec_t *dec) |
695 | { |
704 | { |
696 | err_set (dec, "unexpected end of CBOR data"); |
705 | err_set (dec, "unexpected end of CBOR data"); |
697 | } |
706 | } |
698 | |
707 | |
699 | ecb_cold static void |
|
|
700 | err_nesting_exceeded (dec_t *dec) |
|
|
701 | { |
|
|
702 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
703 | } |
|
|
704 | |
|
|
705 | #define ERR_DO(do) SB do; goto fail; SE |
708 | #define ERR_DO(do) SB do; goto fail; SE |
706 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
709 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
707 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
710 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
708 | |
711 | |
709 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) |
712 | #define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec)) |
710 | |
713 | |
711 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
714 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
712 | #define DEC_DEC_DEPTH --dec->depth |
715 | #define DEC_DEC_DEPTH --dec->depth |
713 | |
716 | |
714 | static UV |
717 | static UV |
… | |
… | |
782 | |
785 | |
783 | for (;;) |
786 | for (;;) |
784 | { |
787 | { |
785 | WANT (1); |
788 | WANT (1); |
786 | |
789 | |
787 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
790 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
788 | { |
791 | { |
789 | ++dec->cur; |
792 | ++dec->cur; |
790 | break; |
793 | break; |
791 | } |
794 | } |
792 | |
795 | |
… | |
… | |
806 | |
809 | |
807 | DEC_DEC_DEPTH; |
810 | DEC_DEC_DEPTH; |
808 | return newRV_noinc ((SV *)av); |
811 | return newRV_noinc ((SV *)av); |
809 | |
812 | |
810 | fail: |
813 | fail: |
811 | SvREFCNT_dec (av); |
814 | SvREFCNT_dec_NN (av); |
812 | DEC_DEC_DEPTH; |
815 | DEC_DEC_DEPTH; |
813 | return &PL_sv_undef; |
816 | return &PL_sv_undef; |
814 | } |
817 | } |
815 | |
818 | |
816 | static void |
819 | static void |
… | |
… | |
879 | |
882 | |
880 | return; |
883 | return; |
881 | } |
884 | } |
882 | |
885 | |
883 | hv_store_ent (hv, k, v, 0); |
886 | hv_store_ent (hv, k, v, 0); |
884 | SvREFCNT_dec (k); |
887 | SvREFCNT_dec_NN (k); |
885 | |
888 | |
886 | fail: |
889 | fail: |
887 | ; |
890 | ; |
888 | } |
891 | } |
889 | |
892 | |
… | |
… | |
900 | |
903 | |
901 | for (;;) |
904 | for (;;) |
902 | { |
905 | { |
903 | WANT (1); |
906 | WANT (1); |
904 | |
907 | |
905 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
908 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
906 | { |
909 | { |
907 | ++dec->cur; |
910 | ++dec->cur; |
908 | break; |
911 | break; |
909 | } |
912 | } |
910 | |
913 | |
… | |
… | |
923 | |
926 | |
924 | DEC_DEC_DEPTH; |
927 | DEC_DEC_DEPTH; |
925 | return newRV_noinc ((SV *)hv); |
928 | return newRV_noinc ((SV *)hv); |
926 | |
929 | |
927 | fail: |
930 | fail: |
928 | SvREFCNT_dec (hv); |
931 | SvREFCNT_dec_NN (hv); |
929 | DEC_DEC_DEPTH; |
932 | DEC_DEC_DEPTH; |
930 | return &PL_sv_undef; |
933 | return &PL_sv_undef; |
931 | } |
934 | } |
932 | |
935 | |
933 | static SV * |
936 | static SV * |
934 | decode_str (dec_t *dec, int utf8) |
937 | decode_str (dec_t *dec, int utf8) |
935 | { |
938 | { |
936 | SV *sv = 0; |
939 | SV *sv = 0; |
937 | |
940 | |
938 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
941 | if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF)) |
939 | { |
942 | { |
940 | // indefinite length strings |
943 | // indefinite length strings |
941 | ++dec->cur; |
944 | ++dec->cur; |
942 | |
945 | |
943 | U8 major = *dec->cur & MAJOR_MISC; |
946 | U8 major = *dec->cur & MAJOR_MISC; |
… | |
… | |
1011 | sv = newRV_noinc (decode_sv (dec)); |
1014 | sv = newRV_noinc (decode_sv (dec)); |
1012 | break; |
1015 | break; |
1013 | |
1016 | |
1014 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1017 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1015 | { |
1018 | { |
1016 | // do nmot use SAVETMPS/FREETMPS, as these will |
1019 | // do not use SAVETMPS/FREETMPS, as these will |
1017 | // erase mortalised caches, e.g. "shareable" |
1020 | // erase mortalised caches, e.g. "shareable" |
1018 | ENTER; |
1021 | ENTER; |
1019 | |
1022 | |
1020 | SAVESPTR (dec->stringref); |
1023 | SAVESPTR (dec->stringref); |
1021 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1024 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
… | |
… | |
1031 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1034 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1032 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
1035 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
1033 | |
1036 | |
1034 | UV idx = decode_uint (dec); |
1037 | UV idx = decode_uint (dec); |
1035 | |
1038 | |
1036 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
1039 | if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref))) |
1037 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
1040 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
1038 | |
1041 | |
1039 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
1042 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
1040 | } |
1043 | } |
1041 | break; |
1044 | break; |
… | |
… | |
1069 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1072 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1070 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
1073 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
1071 | |
1074 | |
1072 | UV idx = decode_uint (dec); |
1075 | UV idx = decode_uint (dec); |
1073 | |
1076 | |
1074 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
1077 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1075 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1078 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1076 | |
1079 | |
1077 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1080 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1078 | |
1081 | |
1079 | if (sv == &PL_sv_undef) |
1082 | if (sv == &PL_sv_undef) |
… | |
… | |
1081 | } |
1084 | } |
1082 | break; |
1085 | break; |
1083 | |
1086 | |
1084 | case CBOR_TAG_PERL_OBJECT: |
1087 | case CBOR_TAG_PERL_OBJECT: |
1085 | { |
1088 | { |
|
|
1089 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1090 | goto filter; |
|
|
1091 | |
1086 | sv = decode_sv (dec); |
1092 | sv = decode_sv (dec); |
1087 | |
1093 | |
1088 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1094 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1089 | ERR ("corrupted CBOR data (non-array perl object)"); |
1095 | ERR ("corrupted CBOR data (non-array perl object)"); |
1090 | |
1096 | |
… | |
… | |
1122 | { |
1128 | { |
1123 | FREETMPS; LEAVE; |
1129 | FREETMPS; LEAVE; |
1124 | ERR_ERRSV; |
1130 | ERR_ERRSV; |
1125 | } |
1131 | } |
1126 | |
1132 | |
1127 | SvREFCNT_dec (sv); |
1133 | SvREFCNT_dec_NN (sv); |
1128 | sv = SvREFCNT_inc (POPs); |
1134 | sv = SvREFCNT_inc (POPs); |
1129 | |
1135 | |
1130 | PUTBACK; |
1136 | PUTBACK; |
1131 | |
1137 | |
1132 | FREETMPS; LEAVE; |
1138 | FREETMPS; LEAVE; |
1133 | } |
1139 | } |
1134 | break; |
1140 | break; |
1135 | |
1141 | |
1136 | default: |
1142 | default: |
|
|
1143 | filter: |
1137 | { |
1144 | { |
1138 | SV *tag_sv = newSVuv (tag); |
1145 | SV *tag_sv = newSVuv (tag); |
1139 | |
1146 | |
1140 | sv = decode_sv (dec); |
1147 | sv = decode_sv (dec); |
1141 | |
1148 | |
1142 | dSP; |
1149 | dSP; |
1143 | ENTER; SAVETMPS; |
1150 | ENTER; SAVETMPS; |
1144 | SAVESTACK_POS (); |
|
|
1145 | PUSHMARK (SP); |
1151 | PUSHMARK (SP); |
1146 | EXTEND (SP, 2); |
1152 | EXTEND (SP, 2); |
1147 | PUSHs (tag_sv); |
1153 | PUSHs (tag_sv); |
1148 | PUSHs (sv); |
1154 | PUSHs (sv); |
1149 | |
1155 | |
… | |
… | |
1151 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1157 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1152 | SPAGAIN; |
1158 | SPAGAIN; |
1153 | |
1159 | |
1154 | if (SvTRUE (ERRSV)) |
1160 | if (SvTRUE (ERRSV)) |
1155 | { |
1161 | { |
1156 | SvREFCNT_dec (tag_sv); |
1162 | SvREFCNT_dec_NN (tag_sv); |
1157 | FREETMPS; LEAVE; |
1163 | FREETMPS; LEAVE; |
1158 | ERR_ERRSV; |
1164 | ERR_ERRSV; |
1159 | } |
1165 | } |
1160 | |
1166 | |
1161 | if (count) |
1167 | if (count) |
1162 | { |
1168 | { |
1163 | SvREFCNT_dec (tag_sv); |
1169 | SvREFCNT_dec_NN (tag_sv); |
1164 | SvREFCNT_dec (sv); |
1170 | SvREFCNT_dec_NN (sv); |
1165 | sv = SvREFCNT_inc (POPs); |
1171 | sv = SvREFCNT_inc_NN (TOPs); |
|
|
1172 | SP -= count; |
1166 | } |
1173 | } |
1167 | else |
1174 | else |
1168 | { |
1175 | { |
1169 | AV *av = newAV (); |
1176 | AV *av = newAV (); |
1170 | av_push (av, tag_sv); |
1177 | av_push (av, tag_sv); |
… | |
… | |
1313 | for (i = av_len (dec.shareable) + 1; i--; ) |
1320 | for (i = av_len (dec.shareable) + 1; i--; ) |
1314 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1321 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1315 | sv_setsv (*svp, &PL_sv_undef); |
1322 | sv_setsv (*svp, &PL_sv_undef); |
1316 | } |
1323 | } |
1317 | |
1324 | |
1318 | SvREFCNT_dec (sv); |
1325 | SvREFCNT_dec_NN (sv); |
1319 | |
1326 | |
1320 | if (dec.err_sv) |
1327 | if (dec.err_sv) |
1321 | sv_2mortal (dec.err_sv); |
1328 | sv_2mortal (dec.err_sv); |
1322 | |
1329 | |
1323 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1330 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
… | |
… | |
1502 | ALIAS: |
1509 | ALIAS: |
1503 | shrink = F_SHRINK |
1510 | shrink = F_SHRINK |
1504 | allow_unknown = F_ALLOW_UNKNOWN |
1511 | allow_unknown = F_ALLOW_UNKNOWN |
1505 | allow_sharing = F_ALLOW_SHARING |
1512 | allow_sharing = F_ALLOW_SHARING |
1506 | allow_cycles = F_ALLOW_CYCLES |
1513 | allow_cycles = F_ALLOW_CYCLES |
|
|
1514 | forbid_objects = F_FORBID_OBJECTS |
1507 | pack_strings = F_PACK_STRINGS |
1515 | pack_strings = F_PACK_STRINGS |
1508 | text_keys = F_TEXT_KEYS |
1516 | text_keys = F_TEXT_KEYS |
1509 | text_strings = F_TEXT_STRINGS |
1517 | text_strings = F_TEXT_STRINGS |
1510 | validate_utf8 = F_VALIDATE_UTF8 |
1518 | validate_utf8 = F_VALIDATE_UTF8 |
1511 | PPCODE: |
1519 | PPCODE: |
… | |
… | |
1522 | ALIAS: |
1530 | ALIAS: |
1523 | get_shrink = F_SHRINK |
1531 | get_shrink = F_SHRINK |
1524 | get_allow_unknown = F_ALLOW_UNKNOWN |
1532 | get_allow_unknown = F_ALLOW_UNKNOWN |
1525 | get_allow_sharing = F_ALLOW_SHARING |
1533 | get_allow_sharing = F_ALLOW_SHARING |
1526 | get_allow_cycles = F_ALLOW_CYCLES |
1534 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1535 | get_forbid_objects = F_FORBID_OBJECTS |
1527 | get_pack_strings = F_PACK_STRINGS |
1536 | get_pack_strings = F_PACK_STRINGS |
1528 | get_text_keys = F_TEXT_KEYS |
1537 | get_text_keys = F_TEXT_KEYS |
1529 | get_text_strings = F_TEXT_STRINGS |
1538 | get_text_strings = F_TEXT_STRINGS |
1530 | get_validate_utf8 = F_VALIDATE_UTF8 |
1539 | get_validate_utf8 = F_VALIDATE_UTF8 |
1531 | PPCODE: |
1540 | PPCODE: |
… | |
… | |
1664 | cbor_init (&cbor); |
1673 | cbor_init (&cbor); |
1665 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1674 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1666 | XPUSHs (cborstr); |
1675 | XPUSHs (cborstr); |
1667 | } |
1676 | } |
1668 | |
1677 | |
|
|
1678 | #ifdef __AFL_COMPILER |
|
|
1679 | |
|
|
1680 | void |
|
|
1681 | afl_init () |
|
|
1682 | CODE: |
|
|
1683 | __AFL_INIT (); |
|
|
1684 | |
|
|
1685 | int |
|
|
1686 | afl_loop (unsigned int count = 10000) |
|
|
1687 | CODE: |
|
|
1688 | RETVAL = __AFL_LOOP (count); |
|
|
1689 | OUTPUT: |
|
|
1690 | RETVAL |
|
|
1691 | |
|
|
1692 | #endif |
|
|
1693 | |