… | |
… | |
116 | AS_FLOAT64 = 6, |
116 | AS_FLOAT64 = 6, |
117 | AS_MAP = 7, |
117 | AS_MAP = 7, |
118 | // possibly future enhancements: (generic) float, (generic) string |
118 | // possibly future enhancements: (generic) float, (generic) string |
119 | }; |
119 | }; |
120 | |
120 | |
121 | #define F_SHRINK 0x00000001UL |
121 | #define F_SHRINK 0x00000001UL |
122 | #define F_ALLOW_UNKNOWN 0x00000002UL |
122 | #define F_ALLOW_UNKNOWN 0x00000002UL |
123 | #define F_ALLOW_SHARING 0x00000004UL |
123 | #define F_ALLOW_SHARING 0x00000004UL |
124 | #define F_ALLOW_CYCLES 0x00000008UL |
124 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
125 | #define F_ALLOW_WEAK_CYCLES 0x00000010UL |
125 | #define F_FORBID_OBJECTS 0x00000010UL |
126 | #define F_FORBID_OBJECTS 0x00000020UL |
126 | #define F_PACK_STRINGS 0x00000020UL |
127 | #define F_PACK_STRINGS 0x00000040UL |
127 | #define F_TEXT_KEYS 0x00000040UL |
128 | #define F_TEXT_KEYS 0x00000080UL |
128 | #define F_TEXT_STRINGS 0x00000080UL |
129 | #define F_TEXT_STRINGS 0x00000100UL |
129 | #define F_VALIDATE_UTF8 0x00000100UL |
130 | #define F_VALIDATE_UTF8 0x00000200UL |
130 | |
131 | |
131 | #define INIT_SIZE 32 // initial scalar size to be allocated |
132 | #define INIT_SIZE 32 // initial scalar size to be allocated |
132 | |
133 | |
133 | #define SB do { |
134 | #define SB do { |
134 | #define SE } while (0) |
135 | #define SE } while (0) |
… | |
… | |
1194 | case CBOR_TAG_VALUE_SHAREABLE: |
1195 | case CBOR_TAG_VALUE_SHAREABLE: |
1195 | { |
1196 | { |
1196 | if (ecb_expect_false (!dec->shareable)) |
1197 | if (ecb_expect_false (!dec->shareable)) |
1197 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1198 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1198 | |
1199 | |
1199 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
1200 | if (ecb_expect_false (dec->cbor.flags & (F_ALLOW_CYCLES | F_ALLOW_WEAK_CYCLES))) |
1200 | { |
1201 | { |
1201 | // if cycles are allowed, then we store an AV as value |
1202 | // if cycles are allowed, then we store an AV as value |
1202 | // while it is being decoded, and gather unresolved |
1203 | // while it is being decoded, and gather unresolved |
1203 | // references in it, to be re4solved after decoding. |
1204 | // references in it, to be re4solved after decoding. |
1204 | int idx, i; |
1205 | int idx, i; |
… | |
… | |
1210 | |
1211 | |
1211 | // the AV now contains \undef for all unresolved references, |
1212 | // the AV now contains \undef for all unresolved references, |
1212 | // so we fix them up here. |
1213 | // so we fix them up here. |
1213 | for (i = 0; i <= AvFILLp (av); ++i) |
1214 | for (i = 0; i <= AvFILLp (av); ++i) |
1214 | SvRV_set (AvARRAY (av)[i], SvREFCNT_inc_NN (SvRV (sv))); |
1215 | SvRV_set (AvARRAY (av)[i], SvREFCNT_inc_NN (SvRV (sv))); |
|
|
1216 | |
|
|
1217 | // weaken all recursive references |
|
|
1218 | if (dec->cbor.flags & F_ALLOW_WEAK_CYCLES) |
|
|
1219 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1220 | sv_rvweaken (AvARRAY (av)[i]); |
1215 | |
1221 | |
1216 | // now replace the AV by a reference to the completed value |
1222 | // now replace the AV by a reference to the completed value |
1217 | SvREFCNT_dec_NN ((SV *)av); |
1223 | SvREFCNT_dec_NN ((SV *)av); |
1218 | AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv); |
1224 | AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv); |
1219 | } |
1225 | } |
… | |
… | |
1239 | |
1245 | |
1240 | sv = AvARRAY (dec->shareable)[idx]; |
1246 | sv = AvARRAY (dec->shareable)[idx]; |
1241 | |
1247 | |
1242 | // reference to cycle, we create a new \undef and use that, and also |
1248 | // reference to cycle, we create a new \undef and use that, and also |
1243 | // registerr it in the AV for later fixing |
1249 | // registerr it in the AV for later fixing |
1244 | if (SvTYPE (sv) == SVt_PVAV) |
1250 | if (ecb_expect_false (SvTYPE (sv) == SVt_PVAV)) |
1245 | { |
1251 | { |
1246 | AV *av = (AV *)sv; |
1252 | AV *av = (AV *)sv; |
1247 | sv = newRV_noinc (&PL_sv_undef); |
1253 | sv = newRV_noinc (&PL_sv_undef); |
1248 | av_push (av, SvREFCNT_inc_NN (sv)); |
1254 | av_push (av, SvREFCNT_inc_NN (sv)); |
1249 | } |
1255 | } |
1250 | else if (sv == &PL_sv_undef) // not yet decoded, but cycles not allowed |
1256 | else if (ecb_expect_false (sv == &PL_sv_undef)) // not yet decoded, but cycles not allowed |
1251 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
1257 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
1252 | else // we decoded the object earlier, no cycle |
1258 | else // we decoded the object earlier, no cycle |
1253 | sv = newSVsv (sv); |
1259 | sv = newSVsv (sv); |
1254 | } |
1260 | } |
1255 | break; |
1261 | break; |
… | |
… | |
1676 | ))); |
1682 | ))); |
1677 | } |
1683 | } |
1678 | |
1684 | |
1679 | void shrink (CBOR *self, int enable = 1) |
1685 | void shrink (CBOR *self, int enable = 1) |
1680 | ALIAS: |
1686 | ALIAS: |
1681 | shrink = F_SHRINK |
1687 | shrink = F_SHRINK |
1682 | allow_unknown = F_ALLOW_UNKNOWN |
1688 | allow_unknown = F_ALLOW_UNKNOWN |
1683 | allow_sharing = F_ALLOW_SHARING |
1689 | allow_sharing = F_ALLOW_SHARING |
1684 | allow_cycles = F_ALLOW_CYCLES |
1690 | allow_cycles = F_ALLOW_CYCLES |
|
|
1691 | allow_weak_cycles = F_ALLOW_WEAK_CYCLES |
1685 | forbid_objects = F_FORBID_OBJECTS |
1692 | forbid_objects = F_FORBID_OBJECTS |
1686 | pack_strings = F_PACK_STRINGS |
1693 | pack_strings = F_PACK_STRINGS |
1687 | text_keys = F_TEXT_KEYS |
1694 | text_keys = F_TEXT_KEYS |
1688 | text_strings = F_TEXT_STRINGS |
1695 | text_strings = F_TEXT_STRINGS |
1689 | validate_utf8 = F_VALIDATE_UTF8 |
1696 | validate_utf8 = F_VALIDATE_UTF8 |
1690 | PPCODE: |
1697 | PPCODE: |
1691 | { |
1698 | { |
1692 | if (enable) |
1699 | if (enable) |
1693 | self->flags |= ix; |
1700 | self->flags |= ix; |
1694 | else |
1701 | else |
… | |
… | |
1697 | XPUSHs (ST (0)); |
1704 | XPUSHs (ST (0)); |
1698 | } |
1705 | } |
1699 | |
1706 | |
1700 | void get_shrink (CBOR *self) |
1707 | void get_shrink (CBOR *self) |
1701 | ALIAS: |
1708 | ALIAS: |
1702 | get_shrink = F_SHRINK |
1709 | get_shrink = F_SHRINK |
1703 | get_allow_unknown = F_ALLOW_UNKNOWN |
1710 | get_allow_unknown = F_ALLOW_UNKNOWN |
1704 | get_allow_sharing = F_ALLOW_SHARING |
1711 | get_allow_sharing = F_ALLOW_SHARING |
1705 | get_allow_cycles = F_ALLOW_CYCLES |
1712 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1713 | get_allow_weak_cycles = F_ALLOW_WEAK_CYCLES |
1706 | get_forbid_objects = F_FORBID_OBJECTS |
1714 | get_forbid_objects = F_FORBID_OBJECTS |
1707 | get_pack_strings = F_PACK_STRINGS |
1715 | get_pack_strings = F_PACK_STRINGS |
1708 | get_text_keys = F_TEXT_KEYS |
1716 | get_text_keys = F_TEXT_KEYS |
1709 | get_text_strings = F_TEXT_STRINGS |
1717 | get_text_strings = F_TEXT_STRINGS |
1710 | get_validate_utf8 = F_VALIDATE_UTF8 |
1718 | get_validate_utf8 = F_VALIDATE_UTF8 |
1711 | PPCODE: |
1719 | PPCODE: |
1712 | XPUSHs (boolSV (self->flags & ix)); |
1720 | XPUSHs (boolSV (self->flags & ix)); |
1713 | |
1721 | |
1714 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1722 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1715 | PPCODE: |
1723 | PPCODE: |