… | |
… | |
99 | #define F_SHRINK 0x00000001UL |
99 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
|
|
104 | #define F_VALIDATE_UTF8 0x00000020UL |
104 | |
105 | |
105 | #define INIT_SIZE 32 // initial scalar size to be allocated |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
106 | |
107 | |
107 | #define SB do { |
108 | #define SB do { |
108 | #define SE } while (0) |
109 | #define SE } while (0) |
… | |
… | |
736 | I32 len = decode_uint (dec); |
737 | I32 len = decode_uint (dec); |
737 | char *key = (char *)dec->cur; |
738 | char *key = (char *)dec->cur; |
738 | |
739 | |
739 | dec->cur += len; |
740 | dec->cur += len; |
740 | |
741 | |
741 | if (ecb_expect_false (dec->stringref)) |
|
|
742 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
743 | |
|
|
744 | hv_store (hv, key, len, decode_sv (dec), 0); |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
745 | |
743 | |
746 | return; |
744 | return; |
747 | } |
745 | } |
748 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
… | |
… | |
750 | I32 len = decode_uint (dec); |
748 | I32 len = decode_uint (dec); |
751 | char *key = (char *)dec->cur; |
749 | char *key = (char *)dec->cur; |
752 | |
750 | |
753 | dec->cur += len; |
751 | dec->cur += len; |
754 | |
752 | |
755 | if (ecb_expect_false (dec->stringref)) |
753 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
756 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
754 | if (!is_utf8_string (key, len)) |
|
|
755 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
757 | |
756 | |
758 | hv_store (hv, key, -len, decode_sv (dec), 0); |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
759 | |
758 | |
760 | return; |
759 | return; |
761 | } |
760 | } |
… | |
… | |
763 | SV *k = decode_sv (dec); |
762 | SV *k = decode_sv (dec); |
764 | SV *v = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
765 | |
764 | |
766 | hv_store_ent (hv, k, v, 0); |
765 | hv_store_ent (hv, k, v, 0); |
767 | SvREFCNT_dec (k); |
766 | SvREFCNT_dec (k); |
|
|
767 | |
|
|
768 | fail: |
|
|
769 | ; |
768 | } |
770 | } |
769 | |
771 | |
770 | static SV * |
772 | static SV * |
771 | decode_hv (dec_t *dec) |
773 | decode_hv (dec_t *dec) |
772 | { |
774 | { |
… | |
… | |
854 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
856 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
855 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
857 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
856 | } |
858 | } |
857 | |
859 | |
858 | if (utf8) |
860 | if (utf8) |
|
|
861 | { |
|
|
862 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
863 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
864 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
865 | |
859 | SvUTF8_on (sv); |
866 | SvUTF8_on (sv); |
|
|
867 | } |
860 | |
868 | |
861 | return sv; |
869 | return sv; |
862 | |
870 | |
863 | fail: |
871 | fail: |
864 | SvREFCNT_dec (sv); |
872 | SvREFCNT_dec (sv); |
… | |
… | |
1163 | if (dec.cur != dec.end && !dec.err) |
1171 | if (dec.cur != dec.end && !dec.err) |
1164 | dec.err = "garbage after CBOR object"; |
1172 | dec.err = "garbage after CBOR object"; |
1165 | |
1173 | |
1166 | if (dec.err) |
1174 | if (dec.err) |
1167 | { |
1175 | { |
|
|
1176 | if (dec.shareable) |
|
|
1177 | { |
|
|
1178 | // need to break cyclic links, which whould all be in shareable |
|
|
1179 | int i; |
|
|
1180 | SV **svp; |
|
|
1181 | |
|
|
1182 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1183 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1184 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1185 | } |
|
|
1186 | |
1168 | SvREFCNT_dec (sv); |
1187 | SvREFCNT_dec (sv); |
1169 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1188 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1170 | } |
1189 | } |
1171 | |
1190 | |
1172 | sv = sv_2mortal (sv); |
1191 | sv = sv_2mortal (sv); |
… | |
… | |
1223 | shrink = F_SHRINK |
1242 | shrink = F_SHRINK |
1224 | allow_unknown = F_ALLOW_UNKNOWN |
1243 | allow_unknown = F_ALLOW_UNKNOWN |
1225 | allow_sharing = F_ALLOW_SHARING |
1244 | allow_sharing = F_ALLOW_SHARING |
1226 | allow_cycles = F_ALLOW_CYCLES |
1245 | allow_cycles = F_ALLOW_CYCLES |
1227 | pack_strings = F_PACK_STRINGS |
1246 | pack_strings = F_PACK_STRINGS |
|
|
1247 | validate_utf8 = F_VALIDATE_UTF8 |
1228 | PPCODE: |
1248 | PPCODE: |
1229 | { |
1249 | { |
1230 | if (enable) |
1250 | if (enable) |
1231 | self->flags |= ix; |
1251 | self->flags |= ix; |
1232 | else |
1252 | else |
… | |
… | |
1240 | get_shrink = F_SHRINK |
1260 | get_shrink = F_SHRINK |
1241 | get_allow_unknown = F_ALLOW_UNKNOWN |
1261 | get_allow_unknown = F_ALLOW_UNKNOWN |
1242 | get_allow_sharing = F_ALLOW_SHARING |
1262 | get_allow_sharing = F_ALLOW_SHARING |
1243 | get_allow_cycles = F_ALLOW_CYCLES |
1263 | get_allow_cycles = F_ALLOW_CYCLES |
1244 | get_pack_strings = F_PACK_STRINGS |
1264 | get_pack_strings = F_PACK_STRINGS |
|
|
1265 | get_validate_utf8 = F_VALIDATE_UTF8 |
1245 | PPCODE: |
1266 | PPCODE: |
1246 | XPUSHs (boolSV (self->flags & ix)); |
1267 | XPUSHs (boolSV (self->flags & ix)); |
1247 | |
1268 | |
1248 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1269 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1249 | PPCODE: |
1270 | PPCODE: |