… | |
… | |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
105 | #define F_UTF8_STRINGS 0x00000020UL |
|
|
106 | #define F_UTF8_KEYS 0x00000040UL |
105 | #define F_TEXT_KEYS 0x00000020UL |
|
|
106 | #define F_TEXT_STRINGS 0x00000040UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
108 | |
108 | |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
110 | |
110 | |
111 | #define SB do { |
111 | #define SB do { |
… | |
… | |
317 | need (enc, len); |
317 | need (enc, len); |
318 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
319 | enc->cur += len; |
319 | enc->cur += len; |
320 | } |
320 | } |
321 | |
321 | |
322 | ecb_inline |
322 | ecb_inline void |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
324 | { |
324 | { |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
326 | { |
326 | { |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
… | |
… | |
395 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
396 | { |
396 | { |
397 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
398 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
399 | else |
399 | else |
400 | encode_strref (enc, enc->cbor.flags & (F_UTF8_KEYS | F_UTF8_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
400 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
401 | |
401 | |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
403 | } |
403 | } |
404 | |
404 | |
405 | if (mg) |
405 | if (mg) |
… | |
… | |
592 | |
592 | |
593 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
594 | { |
594 | { |
595 | STRLEN len; |
595 | STRLEN len; |
596 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
597 | encode_strref (enc, enc->cbor.flags & F_UTF8_STRINGS, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
598 | } |
598 | } |
599 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
600 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
601 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
602 | { |
602 | { |
… | |
… | |
665 | SV *decode_tagged; |
665 | SV *decode_tagged; |
666 | } dec_t; |
666 | } dec_t; |
667 | |
667 | |
668 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
668 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
669 | |
669 | |
670 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
670 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data") |
671 | |
671 | |
672 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
672 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
673 | #define DEC_DEC_DEPTH --dec->depth |
673 | #define DEC_DEC_DEPTH --dec->depth |
674 | |
674 | |
675 | static UV |
675 | static UV |
676 | decode_uint (dec_t *dec) |
676 | decode_uint (dec_t *dec) |
677 | { |
677 | { |
… | |
… | |
754 | av_push (av, decode_sv (dec)); |
754 | av_push (av, decode_sv (dec)); |
755 | } |
755 | } |
756 | } |
756 | } |
757 | else |
757 | else |
758 | { |
758 | { |
759 | int i, len = decode_uint (dec); |
759 | UV i, len = decode_uint (dec); |
760 | |
760 | |
761 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
761 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
762 | av_fill (av, len - 1); |
762 | av_fill (av, len - 1); |
763 | |
763 | |
764 | for (i = 0; i < len; ++i) |
764 | for (i = 0; i < len; ++i) |
… | |
… | |
781 | // byte or utf-8 strings as keys, but only when !stringref |
781 | // byte or utf-8 strings as keys, but only when !stringref |
782 | |
782 | |
783 | if (ecb_expect_true (!dec->stringref)) |
783 | if (ecb_expect_true (!dec->stringref)) |
784 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
784 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
785 | { |
785 | { |
786 | I32 len = decode_uint (dec); |
786 | STRLEN len = decode_uint (dec); |
787 | char *key = (char *)dec->cur; |
787 | char *key = (char *)dec->cur; |
788 | |
788 | |
789 | WANT (len); |
789 | WANT (len); |
790 | dec->cur += len; |
790 | dec->cur += len; |
791 | |
791 | |
… | |
… | |
793 | |
793 | |
794 | return; |
794 | return; |
795 | } |
795 | } |
796 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
796 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
797 | { |
797 | { |
798 | I32 len = decode_uint (dec); |
798 | STRLEN len = decode_uint (dec); |
799 | char *key = (char *)dec->cur; |
799 | char *key = (char *)dec->cur; |
800 | |
800 | |
801 | WANT (len); |
801 | WANT (len); |
802 | dec->cur += len; |
802 | dec->cur += len; |
803 | |
803 | |
… | |
… | |
844 | decode_he (dec, hv); |
844 | decode_he (dec, hv); |
845 | } |
845 | } |
846 | } |
846 | } |
847 | else |
847 | else |
848 | { |
848 | { |
849 | int pairs = decode_uint (dec); |
849 | UV pairs = decode_uint (dec); |
|
|
850 | |
|
|
851 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
850 | |
852 | |
851 | while (pairs--) |
853 | while (pairs--) |
852 | decode_he (dec, hv); |
854 | decode_he (dec, hv); |
853 | } |
855 | } |
854 | |
856 | |
… | |
… | |
942 | sv = newRV_noinc (decode_sv (dec)); |
944 | sv = newRV_noinc (decode_sv (dec)); |
943 | break; |
945 | break; |
944 | |
946 | |
945 | case CBOR_TAG_STRINGREF_NAMESPACE: |
947 | case CBOR_TAG_STRINGREF_NAMESPACE: |
946 | { |
948 | { |
|
|
949 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
950 | // erase mortalised caches, e.g. "shareable" |
947 | ENTER; SAVETMPS; |
951 | ENTER; |
948 | |
952 | |
949 | SAVESPTR (dec->stringref); |
953 | SAVESPTR (dec->stringref); |
950 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
954 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
951 | |
955 | |
952 | sv = decode_sv (dec); |
956 | sv = decode_sv (dec); |
953 | |
957 | |
954 | FREETMPS; LEAVE; |
958 | LEAVE; |
955 | } |
959 | } |
956 | break; |
960 | break; |
957 | |
961 | |
958 | case CBOR_TAG_STRINGREF: |
962 | case CBOR_TAG_STRINGREF: |
959 | { |
963 | { |
… | |
… | |
1062 | } |
1066 | } |
1063 | break; |
1067 | break; |
1064 | |
1068 | |
1065 | default: |
1069 | default: |
1066 | { |
1070 | { |
|
|
1071 | SV *tag_sv = newSVuv (tag); |
|
|
1072 | |
1067 | sv = decode_sv (dec); |
1073 | sv = decode_sv (dec); |
1068 | |
1074 | |
1069 | dSP; |
1075 | dSP; |
1070 | ENTER; SAVETMPS; |
1076 | ENTER; SAVETMPS; |
1071 | SAVESTACK_POS (); |
1077 | SAVESTACK_POS (); |
1072 | PUSHMARK (SP); |
1078 | PUSHMARK (SP); |
1073 | EXTEND (SP, 2); |
1079 | EXTEND (SP, 2); |
1074 | PUSHs (newSVuv (tag)); |
1080 | PUSHs (tag_sv); |
1075 | PUSHs (sv); |
1081 | PUSHs (sv); |
1076 | |
1082 | |
1077 | PUTBACK; |
1083 | PUTBACK; |
1078 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1084 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1079 | SPAGAIN; |
1085 | SPAGAIN; |
1080 | |
1086 | |
1081 | if (SvTRUE (ERRSV)) |
1087 | if (SvTRUE (ERRSV)) |
1082 | { |
1088 | { |
|
|
1089 | SvREFCNT_dec (tag_sv); |
1083 | FREETMPS; LEAVE; |
1090 | FREETMPS; LEAVE; |
1084 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1091 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1085 | } |
1092 | } |
1086 | |
1093 | |
1087 | if (count) |
1094 | if (count) |
1088 | { |
1095 | { |
|
|
1096 | SvREFCNT_dec (tag_sv); |
1089 | SvREFCNT_dec (sv); |
1097 | SvREFCNT_dec (sv); |
1090 | sv = SvREFCNT_inc (POPs); |
1098 | sv = SvREFCNT_inc (POPs); |
1091 | } |
1099 | } |
1092 | else |
1100 | else |
1093 | { |
1101 | { |
1094 | AV *av = newAV (); |
1102 | AV *av = newAV (); |
1095 | av_push (av, newSVuv (tag)); |
1103 | av_push (av, tag_sv); |
1096 | av_push (av, sv); |
1104 | av_push (av, sv); |
1097 | |
1105 | |
1098 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1106 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1099 | ? cbor_tagged_stash |
1107 | ? cbor_tagged_stash |
1100 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1108 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
… | |
… | |
1229 | |
1237 | |
1230 | if (dec.err) |
1238 | if (dec.err) |
1231 | { |
1239 | { |
1232 | if (dec.shareable) |
1240 | if (dec.shareable) |
1233 | { |
1241 | { |
1234 | // need to break cyclic links, which whould all be in shareable |
1242 | // need to break cyclic links, which would all be in shareable |
1235 | int i; |
1243 | int i; |
1236 | SV **svp; |
1244 | SV **svp; |
1237 | |
1245 | |
1238 | for (i = av_len (dec.shareable) + 1; i--; ) |
1246 | for (i = av_len (dec.shareable) + 1; i--; ) |
1239 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1247 | if ((svp = av_fetch (dec.shareable, i, 0))) |
… | |
… | |
1392 | |
1400 | |
1393 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1401 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1394 | |
1402 | |
1395 | sv_cbor = newSVpv ("CBOR", 0); |
1403 | sv_cbor = newSVpv ("CBOR", 0); |
1396 | SvREADONLY_on (sv_cbor); |
1404 | SvREADONLY_on (sv_cbor); |
|
|
1405 | |
|
|
1406 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1397 | } |
1407 | } |
1398 | |
1408 | |
1399 | PROTOTYPES: DISABLE |
1409 | PROTOTYPES: DISABLE |
1400 | |
1410 | |
1401 | void CLONE (...) |
1411 | void CLONE (...) |
… | |
… | |
1422 | shrink = F_SHRINK |
1432 | shrink = F_SHRINK |
1423 | allow_unknown = F_ALLOW_UNKNOWN |
1433 | allow_unknown = F_ALLOW_UNKNOWN |
1424 | allow_sharing = F_ALLOW_SHARING |
1434 | allow_sharing = F_ALLOW_SHARING |
1425 | allow_cycles = F_ALLOW_CYCLES |
1435 | allow_cycles = F_ALLOW_CYCLES |
1426 | pack_strings = F_PACK_STRINGS |
1436 | pack_strings = F_PACK_STRINGS |
|
|
1437 | text_keys = F_TEXT_KEYS |
1427 | utf8_strings = F_UTF8_STRINGS |
1438 | text_strings = F_TEXT_STRINGS |
1428 | validate_utf8 = F_VALIDATE_UTF8 |
1439 | validate_utf8 = F_VALIDATE_UTF8 |
1429 | PPCODE: |
1440 | PPCODE: |
1430 | { |
1441 | { |
1431 | if (enable) |
1442 | if (enable) |
1432 | self->flags |= ix; |
1443 | self->flags |= ix; |
… | |
… | |
1441 | get_shrink = F_SHRINK |
1452 | get_shrink = F_SHRINK |
1442 | get_allow_unknown = F_ALLOW_UNKNOWN |
1453 | get_allow_unknown = F_ALLOW_UNKNOWN |
1443 | get_allow_sharing = F_ALLOW_SHARING |
1454 | get_allow_sharing = F_ALLOW_SHARING |
1444 | get_allow_cycles = F_ALLOW_CYCLES |
1455 | get_allow_cycles = F_ALLOW_CYCLES |
1445 | get_pack_strings = F_PACK_STRINGS |
1456 | get_pack_strings = F_PACK_STRINGS |
|
|
1457 | get_text_keys = F_TEXT_KEYS |
|
|
1458 | get_text_strings = F_TEXT_STRINGS |
1446 | get_validate_utf8 = F_VALIDATE_UTF8 |
1459 | get_validate_utf8 = F_VALIDATE_UTF8 |
1447 | PPCODE: |
1460 | PPCODE: |
1448 | XPUSHs (boolSV (self->flags & ix)); |
1461 | XPUSHs (boolSV (self->flags & ix)); |
1449 | |
1462 | |
1450 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1463 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |