ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.74 by root, Wed Oct 27 14:59:22 2021 UTC vs.
Revision 1.80 by root, Fri Sep 8 20:03:06 2023 UTC

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 {
1202 // if cycles are allowed, then we store an AV as value
1203 // while it is being decoded, and gather unresolved
1204 // references in it, to be re4solved after decoding.
1205 int idx, i;
1201 sv = newSV (0); 1206 AV *av = newAV ();
1202 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1207 av_push (dec->shareable, (SV *)av);
1208 idx = AvFILLp (dec->shareable);
1203 1209
1204 SV *osv = decode_sv (dec); 1210 sv = decode_sv (dec);
1205 sv_setsv (sv, osv); 1211
1212 // the AV now contains \undef for all unresolved references,
1213 // so we fix them up here.
1214 for (i = 0; i <= AvFILLp (av); ++i)
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]);
1221
1222 // now replace the AV by a reference to the completed value
1206 SvREFCNT_dec_NN (osv); 1223 SvREFCNT_dec_NN ((SV *)av);
1224 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1207 } 1225 }
1208 else 1226 else
1209 { 1227 {
1210 av_push (dec->shareable, &PL_sv_undef); 1228 av_push (dec->shareable, &PL_sv_undef);
1211 int idx = AvFILLp (dec->shareable); 1229 int idx = AvFILLp (dec->shareable);
1212 sv = decode_sv (dec); 1230 sv = decode_sv (dec);
1213 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); 1231 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1214 } 1232 }
1215 } 1233 }
1216 break; 1234 break;
1217 1235
1218 case CBOR_TAG_VALUE_SHAREDREF: 1236 case CBOR_TAG_VALUE_SHAREDREF:
1223 UV idx = decode_uint (dec); 1241 UV idx = decode_uint (dec);
1224 1242
1225 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1243 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1226 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1244 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1227 1245
1228 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1246 sv = AvARRAY (dec->shareable)[idx];
1229 1247
1230 if (sv == &PL_sv_undef) 1248 // reference to cycle, we create a new \undef and use that, and also
1249 // registerr it in the AV for later fixing
1250 if (ecb_expect_false (SvTYPE (sv) == SVt_PVAV))
1251 {
1252 AV *av = (AV *)sv;
1253 sv = newRV_noinc (&PL_sv_undef);
1254 av_push (av, SvREFCNT_inc_NN (sv));
1255 }
1256 else if (ecb_expect_false (sv == &PL_sv_undef)) // not yet decoded, but cycles not allowed
1231 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");
1258 else // we decoded the object earlier, no cycle
1259 sv = newSVsv (sv);
1232 } 1260 }
1233 break; 1261 break;
1234 1262
1235 case CBOR_TAG_PERL_OBJECT: 1263 case CBOR_TAG_PERL_OBJECT:
1236 { 1264 {
1654 ))); 1682 )));
1655} 1683}
1656 1684
1657void shrink (CBOR *self, int enable = 1) 1685void shrink (CBOR *self, int enable = 1)
1658 ALIAS: 1686 ALIAS:
1659 shrink = F_SHRINK 1687 shrink = F_SHRINK
1660 allow_unknown = F_ALLOW_UNKNOWN 1688 allow_unknown = F_ALLOW_UNKNOWN
1661 allow_sharing = F_ALLOW_SHARING 1689 allow_sharing = F_ALLOW_SHARING
1662 allow_cycles = F_ALLOW_CYCLES 1690 allow_cycles = F_ALLOW_CYCLES
1691 allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1663 forbid_objects = F_FORBID_OBJECTS 1692 forbid_objects = F_FORBID_OBJECTS
1664 pack_strings = F_PACK_STRINGS 1693 pack_strings = F_PACK_STRINGS
1665 text_keys = F_TEXT_KEYS 1694 text_keys = F_TEXT_KEYS
1666 text_strings = F_TEXT_STRINGS 1695 text_strings = F_TEXT_STRINGS
1667 validate_utf8 = F_VALIDATE_UTF8 1696 validate_utf8 = F_VALIDATE_UTF8
1668 PPCODE: 1697 PPCODE:
1669{ 1698{
1670 if (enable) 1699 if (enable)
1671 self->flags |= ix; 1700 self->flags |= ix;
1672 else 1701 else
1675 XPUSHs (ST (0)); 1704 XPUSHs (ST (0));
1676} 1705}
1677 1706
1678void get_shrink (CBOR *self) 1707void get_shrink (CBOR *self)
1679 ALIAS: 1708 ALIAS:
1680 get_shrink = F_SHRINK 1709 get_shrink = F_SHRINK
1681 get_allow_unknown = F_ALLOW_UNKNOWN 1710 get_allow_unknown = F_ALLOW_UNKNOWN
1682 get_allow_sharing = F_ALLOW_SHARING 1711 get_allow_sharing = F_ALLOW_SHARING
1683 get_allow_cycles = F_ALLOW_CYCLES 1712 get_allow_cycles = F_ALLOW_CYCLES
1713 get_allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1684 get_forbid_objects = F_FORBID_OBJECTS 1714 get_forbid_objects = F_FORBID_OBJECTS
1685 get_pack_strings = F_PACK_STRINGS 1715 get_pack_strings = F_PACK_STRINGS
1686 get_text_keys = F_TEXT_KEYS 1716 get_text_keys = F_TEXT_KEYS
1687 get_text_strings = F_TEXT_STRINGS 1717 get_text_strings = F_TEXT_STRINGS
1688 get_validate_utf8 = F_VALIDATE_UTF8 1718 get_validate_utf8 = F_VALIDATE_UTF8
1689 PPCODE: 1719 PPCODE:
1690 XPUSHs (boolSV (self->flags & ix)); 1720 XPUSHs (boolSV (self->flags & ix));
1691 1721
1692void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1722void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1693 PPCODE: 1723 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines