… | |
… | |
20 | #ifndef HvNAMELEN |
20 | #ifndef HvNAMELEN |
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 |
|
|
26 | #ifndef SvREFCNT_inc_NN |
|
|
27 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
25 | #endif |
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 | |
… | |
… | |
354 | |
357 | |
355 | ++enc->depth; |
358 | ++enc->depth; |
356 | |
359 | |
357 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
360 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
358 | |
361 | |
359 | if (SvMAGICAL (av)) |
362 | if (ecb_expect_false (SvMAGICAL (av))) |
360 | for (i = 0; i <= len; ++i) |
363 | for (i = 0; i <= len; ++i) |
361 | { |
364 | { |
362 | SV **svp = av_fetch (av, i, 0); |
365 | SV **svp = av_fetch (av, i, 0); |
363 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
366 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
364 | } |
367 | } |
… | |
… | |
383 | ++enc->depth; |
386 | ++enc->depth; |
384 | |
387 | |
385 | int pairs = hv_iterinit (hv); |
388 | int pairs = hv_iterinit (hv); |
386 | int mg = SvMAGICAL (hv); |
389 | int mg = SvMAGICAL (hv); |
387 | |
390 | |
388 | if (mg) |
391 | if (ecb_expect_false (mg)) |
389 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
392 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
390 | else |
393 | else |
391 | encode_uint (enc, MAJOR_MAP, pairs); |
394 | encode_uint (enc, MAJOR_MAP, pairs); |
392 | |
395 | |
393 | while ((he = hv_iternext (hv))) |
396 | while ((he = hv_iternext (hv))) |
… | |
… | |
398 | 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)); |
399 | |
402 | |
400 | 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)); |
401 | } |
404 | } |
402 | |
405 | |
403 | if (mg) |
406 | if (ecb_expect_false (mg)) |
404 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
407 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
405 | |
408 | |
406 | --enc->depth; |
409 | --enc->depth; |
407 | } |
410 | } |
408 | |
411 | |
… | |
… | |
451 | } |
454 | } |
452 | |
455 | |
453 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
456 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
454 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
457 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
455 | { |
458 | { |
456 | if (!enc->shareable) |
459 | if (ecb_expect_false (!enc->shareable)) |
457 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
460 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
458 | |
461 | |
459 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
462 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
460 | |
463 | |
461 | if (SvOK (*svp)) |
464 | if (SvOK (*svp)) |
… | |
… | |
507 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
510 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
508 | { |
511 | { |
509 | dSP; |
512 | dSP; |
510 | |
513 | |
511 | ENTER; SAVETMPS; |
514 | ENTER; SAVETMPS; |
512 | SAVESTACK_POS (); |
|
|
513 | PUSHMARK (SP); |
515 | PUSHMARK (SP); |
514 | EXTEND (SP, 2); |
516 | EXTEND (SP, 2); |
515 | // 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 |
516 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
518 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
517 | PUSHs (sv_cbor); |
519 | PUSHs (sv_cbor); |
… | |
… | |
526 | |
528 | |
527 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
529 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
528 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
530 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
529 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
531 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
530 | |
532 | |
531 | while (count) |
533 | { |
|
|
534 | int i; |
|
|
535 | |
|
|
536 | for (i = 0; i < count; ++i) |
532 | encode_sv (enc, SP[1 - count--]); |
537 | encode_sv (enc, SP[i + 1 - count]); |
|
|
538 | |
|
|
539 | SP -= count; |
|
|
540 | } |
533 | |
541 | |
534 | PUTBACK; |
542 | PUTBACK; |
535 | |
543 | |
536 | FREETMPS; LEAVE; |
544 | FREETMPS; LEAVE; |
537 | } |
545 | } |
… | |
… | |
560 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
568 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
561 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
569 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
562 | //TODO: maybe I32? |
570 | //TODO: maybe I32? |
563 | else if (ecb_expect_false (nv == (float)nv)) |
571 | else if (ecb_expect_false (nv == (float)nv)) |
564 | { |
572 | { |
|
|
573 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
574 | |
565 | uint32_t fp = ecb_float_to_binary32 (nv); |
575 | uint32_t fp = ecb_float_to_binary32 (nv); |
566 | |
|
|
567 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
568 | |
576 | |
569 | if (!ecb_big_endian ()) |
577 | if (!ecb_big_endian ()) |
570 | fp = ecb_bswap32 (fp); |
578 | fp = ecb_bswap32 (fp); |
571 | |
579 | |
572 | memcpy (enc->cur, &fp, 4); |
580 | memcpy (enc->cur, &fp, 4); |
573 | enc->cur += 4; |
581 | enc->cur += 4; |
574 | } |
582 | } |
575 | else |
583 | else |
576 | { |
584 | { |
|
|
585 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
586 | |
577 | uint64_t fp = ecb_double_to_binary64 (nv); |
587 | uint64_t fp = ecb_double_to_binary64 (nv); |
578 | |
|
|
579 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
580 | |
588 | |
581 | if (!ecb_big_endian ()) |
589 | if (!ecb_big_endian ()) |
582 | fp = ecb_bswap64 (fp); |
590 | fp = ecb_bswap64 (fp); |
583 | |
591 | |
584 | memcpy (enc->cur, &fp, 8); |
592 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
622 | static SV * |
630 | static SV * |
623 | encode_cbor (SV *scalar, CBOR *cbor) |
631 | encode_cbor (SV *scalar, CBOR *cbor) |
624 | { |
632 | { |
625 | enc_t enc = { 0 }; |
633 | enc_t enc = { 0 }; |
626 | |
634 | |
627 | enc.cbor = *cbor; |
635 | enc.cbor = *cbor; |
628 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
636 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
629 | enc.cur = SvPVX (enc.sv); |
637 | enc.cur = SvPVX (enc.sv); |
630 | enc.end = SvEND (enc.sv); |
638 | enc.end = SvEND (enc.sv); |
631 | |
639 | |
632 | SvPOK_only (enc.sv); |
640 | SvPOK_only (enc.sv); |
633 | |
641 | |
634 | if (cbor->flags & F_PACK_STRINGS) |
642 | if (cbor->flags & F_PACK_STRINGS) |
635 | { |
643 | { |
… | |
… | |
693 | |
701 | |
694 | ecb_cold static void |
702 | ecb_cold static void |
695 | err_unexpected_end (dec_t *dec) |
703 | err_unexpected_end (dec_t *dec) |
696 | { |
704 | { |
697 | err_set (dec, "unexpected end of CBOR data"); |
705 | err_set (dec, "unexpected end of CBOR data"); |
698 | } |
|
|
699 | |
|
|
700 | ecb_cold static void |
|
|
701 | err_nesting_exceeded (dec_t *dec) |
|
|
702 | { |
|
|
703 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
704 | } |
706 | } |
705 | |
707 | |
706 | #define ERR_DO(do) SB do; goto fail; SE |
708 | #define ERR_DO(do) SB do; goto fail; SE |
707 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
709 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
708 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
710 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
… | |
… | |
783 | |
785 | |
784 | for (;;) |
786 | for (;;) |
785 | { |
787 | { |
786 | WANT (1); |
788 | WANT (1); |
787 | |
789 | |
788 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
790 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
789 | { |
791 | { |
790 | ++dec->cur; |
792 | ++dec->cur; |
791 | break; |
793 | break; |
792 | } |
794 | } |
793 | |
795 | |
… | |
… | |
807 | |
809 | |
808 | DEC_DEC_DEPTH; |
810 | DEC_DEC_DEPTH; |
809 | return newRV_noinc ((SV *)av); |
811 | return newRV_noinc ((SV *)av); |
810 | |
812 | |
811 | fail: |
813 | fail: |
812 | SvREFCNT_dec (av); |
814 | SvREFCNT_dec_NN (av); |
813 | DEC_DEC_DEPTH; |
815 | DEC_DEC_DEPTH; |
814 | return &PL_sv_undef; |
816 | return &PL_sv_undef; |
815 | } |
817 | } |
816 | |
818 | |
817 | static void |
819 | static void |
… | |
… | |
880 | |
882 | |
881 | return; |
883 | return; |
882 | } |
884 | } |
883 | |
885 | |
884 | hv_store_ent (hv, k, v, 0); |
886 | hv_store_ent (hv, k, v, 0); |
885 | SvREFCNT_dec (k); |
887 | SvREFCNT_dec_NN (k); |
886 | |
888 | |
887 | fail: |
889 | fail: |
888 | ; |
890 | ; |
889 | } |
891 | } |
890 | |
892 | |
… | |
… | |
901 | |
903 | |
902 | for (;;) |
904 | for (;;) |
903 | { |
905 | { |
904 | WANT (1); |
906 | WANT (1); |
905 | |
907 | |
906 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
908 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
907 | { |
909 | { |
908 | ++dec->cur; |
910 | ++dec->cur; |
909 | break; |
911 | break; |
910 | } |
912 | } |
911 | |
913 | |
… | |
… | |
924 | |
926 | |
925 | DEC_DEC_DEPTH; |
927 | DEC_DEC_DEPTH; |
926 | return newRV_noinc ((SV *)hv); |
928 | return newRV_noinc ((SV *)hv); |
927 | |
929 | |
928 | fail: |
930 | fail: |
929 | SvREFCNT_dec (hv); |
931 | SvREFCNT_dec_NN (hv); |
930 | DEC_DEC_DEPTH; |
932 | DEC_DEC_DEPTH; |
931 | return &PL_sv_undef; |
933 | return &PL_sv_undef; |
932 | } |
934 | } |
933 | |
935 | |
934 | static SV * |
936 | static SV * |
935 | decode_str (dec_t *dec, int utf8) |
937 | decode_str (dec_t *dec, int utf8) |
936 | { |
938 | { |
937 | SV *sv = 0; |
939 | SV *sv = 0; |
938 | |
940 | |
939 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
941 | if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF)) |
940 | { |
942 | { |
941 | // indefinite length strings |
943 | // indefinite length strings |
942 | ++dec->cur; |
944 | ++dec->cur; |
943 | |
945 | |
944 | U8 major = *dec->cur & MAJOR_MISC; |
946 | U8 major = *dec->cur & MAJOR_MISC; |
… | |
… | |
1012 | sv = newRV_noinc (decode_sv (dec)); |
1014 | sv = newRV_noinc (decode_sv (dec)); |
1013 | break; |
1015 | break; |
1014 | |
1016 | |
1015 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1017 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1016 | { |
1018 | { |
1017 | // do nmot use SAVETMPS/FREETMPS, as these will |
1019 | // do not use SAVETMPS/FREETMPS, as these will |
1018 | // erase mortalised caches, e.g. "shareable" |
1020 | // erase mortalised caches, e.g. "shareable" |
1019 | ENTER; |
1021 | ENTER; |
1020 | |
1022 | |
1021 | SAVESPTR (dec->stringref); |
1023 | SAVESPTR (dec->stringref); |
1022 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1024 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
… | |
… | |
1032 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1034 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1033 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
1035 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
1034 | |
1036 | |
1035 | UV idx = decode_uint (dec); |
1037 | UV idx = decode_uint (dec); |
1036 | |
1038 | |
1037 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
1039 | if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref))) |
1038 | 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)"); |
1039 | |
1041 | |
1040 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
1042 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
1041 | } |
1043 | } |
1042 | break; |
1044 | break; |
… | |
… | |
1070 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1072 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1071 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
1073 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
1072 | |
1074 | |
1073 | UV idx = decode_uint (dec); |
1075 | UV idx = decode_uint (dec); |
1074 | |
1076 | |
1075 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
1077 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1076 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1078 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1077 | |
1079 | |
1078 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1080 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1079 | |
1081 | |
1080 | if (sv == &PL_sv_undef) |
1082 | if (sv == &PL_sv_undef) |
… | |
… | |
1126 | { |
1128 | { |
1127 | FREETMPS; LEAVE; |
1129 | FREETMPS; LEAVE; |
1128 | ERR_ERRSV; |
1130 | ERR_ERRSV; |
1129 | } |
1131 | } |
1130 | |
1132 | |
1131 | SvREFCNT_dec (sv); |
1133 | SvREFCNT_dec_NN (sv); |
1132 | sv = SvREFCNT_inc (POPs); |
1134 | sv = SvREFCNT_inc (POPs); |
1133 | |
1135 | |
1134 | PUTBACK; |
1136 | PUTBACK; |
1135 | |
1137 | |
1136 | FREETMPS; LEAVE; |
1138 | FREETMPS; LEAVE; |
… | |
… | |
1144 | |
1146 | |
1145 | sv = decode_sv (dec); |
1147 | sv = decode_sv (dec); |
1146 | |
1148 | |
1147 | dSP; |
1149 | dSP; |
1148 | ENTER; SAVETMPS; |
1150 | ENTER; SAVETMPS; |
1149 | SAVESTACK_POS (); |
|
|
1150 | PUSHMARK (SP); |
1151 | PUSHMARK (SP); |
1151 | EXTEND (SP, 2); |
1152 | EXTEND (SP, 2); |
1152 | PUSHs (tag_sv); |
1153 | PUSHs (tag_sv); |
1153 | PUSHs (sv); |
1154 | PUSHs (sv); |
1154 | |
1155 | |
… | |
… | |
1156 | 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); |
1157 | SPAGAIN; |
1158 | SPAGAIN; |
1158 | |
1159 | |
1159 | if (SvTRUE (ERRSV)) |
1160 | if (SvTRUE (ERRSV)) |
1160 | { |
1161 | { |
1161 | SvREFCNT_dec (tag_sv); |
1162 | SvREFCNT_dec_NN (tag_sv); |
1162 | FREETMPS; LEAVE; |
1163 | FREETMPS; LEAVE; |
1163 | ERR_ERRSV; |
1164 | ERR_ERRSV; |
1164 | } |
1165 | } |
1165 | |
1166 | |
1166 | if (count) |
1167 | if (count) |
1167 | { |
1168 | { |
1168 | SvREFCNT_dec (tag_sv); |
1169 | SvREFCNT_dec_NN (tag_sv); |
1169 | SvREFCNT_dec (sv); |
1170 | SvREFCNT_dec_NN (sv); |
1170 | sv = SvREFCNT_inc (POPs); |
1171 | sv = SvREFCNT_inc_NN (TOPs); |
|
|
1172 | SP -= count; |
1171 | } |
1173 | } |
1172 | else |
1174 | else |
1173 | { |
1175 | { |
1174 | AV *av = newAV (); |
1176 | AV *av = newAV (); |
1175 | av_push (av, tag_sv); |
1177 | av_push (av, tag_sv); |
… | |
… | |
1318 | for (i = av_len (dec.shareable) + 1; i--; ) |
1320 | for (i = av_len (dec.shareable) + 1; i--; ) |
1319 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1321 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1320 | sv_setsv (*svp, &PL_sv_undef); |
1322 | sv_setsv (*svp, &PL_sv_undef); |
1321 | } |
1323 | } |
1322 | |
1324 | |
1323 | SvREFCNT_dec (sv); |
1325 | SvREFCNT_dec_NN (sv); |
1324 | |
1326 | |
1325 | if (dec.err_sv) |
1327 | if (dec.err_sv) |
1326 | sv_2mortal (dec.err_sv); |
1328 | sv_2mortal (dec.err_sv); |
1327 | |
1329 | |
1328 | 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); |
… | |
… | |
1671 | cbor_init (&cbor); |
1673 | cbor_init (&cbor); |
1672 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1674 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1673 | XPUSHs (cborstr); |
1675 | XPUSHs (cborstr); |
1674 | } |
1676 | } |
1675 | |
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 | |