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.72 by root, Thu Oct 21 01:14:58 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)
985 } 986 }
986 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 987 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
987 { 988 {
988 STRLEN len = decode_uint (dec); 989 STRLEN len = decode_uint (dec);
989 char *key = (char *)dec->cur; 990 char *key = (char *)dec->cur;
990 printf ("len %d\n", len);//D
991 991
992 WANT (len); 992 WANT (len);
993 dec->cur += len; 993 dec->cur += len;
994 994
995 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 995 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1195 case CBOR_TAG_VALUE_SHAREABLE: 1195 case CBOR_TAG_VALUE_SHAREABLE:
1196 { 1196 {
1197 if (ecb_expect_false (!dec->shareable)) 1197 if (ecb_expect_false (!dec->shareable))
1198 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1198 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
1199 1199
1200 if (dec->cbor.flags & F_ALLOW_CYCLES) 1200 if (ecb_expect_false (dec->cbor.flags & (F_ALLOW_CYCLES | F_ALLOW_WEAK_CYCLES)))
1201 { 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;
1202 sv = newSV (0); 1206 AV *av = newAV ();
1203 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1207 av_push (dec->shareable, (SV *)av);
1208 idx = AvFILLp (dec->shareable);
1204 1209
1205 SV *osv = decode_sv (dec); 1210 sv = decode_sv (dec);
1206 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
1207 SvREFCNT_dec_NN (osv); 1223 SvREFCNT_dec_NN ((SV *)av);
1224 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1208 } 1225 }
1209 else 1226 else
1210 { 1227 {
1211 av_push (dec->shareable, &PL_sv_undef); 1228 av_push (dec->shareable, &PL_sv_undef);
1212 int idx = AvFILLp (dec->shareable); 1229 int idx = AvFILLp (dec->shareable);
1213 sv = decode_sv (dec); 1230 sv = decode_sv (dec);
1214 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); 1231 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1215 } 1232 }
1216 } 1233 }
1217 break; 1234 break;
1218 1235
1219 case CBOR_TAG_VALUE_SHAREDREF: 1236 case CBOR_TAG_VALUE_SHAREDREF:
1224 UV idx = decode_uint (dec); 1241 UV idx = decode_uint (dec);
1225 1242
1226 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1243 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1227 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1244 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1228 1245
1229 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1246 sv = AvARRAY (dec->shareable)[idx];
1230 1247
1231 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
1232 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);
1233 } 1260 }
1234 break; 1261 break;
1235 1262
1236 case CBOR_TAG_PERL_OBJECT: 1263 case CBOR_TAG_PERL_OBJECT:
1237 { 1264 {
1474 SvREFCNT_dec_NN (sv); 1501 SvREFCNT_dec_NN (sv);
1475 1502
1476 if (dec.err_sv) 1503 if (dec.err_sv)
1477 sv_2mortal (dec.err_sv); 1504 sv_2mortal (dec.err_sv);
1478 1505
1479 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1506 croak ("%s, at offset %ld (octet 0x%02x)", dec.err, (long)(dec.cur - (U8 *)data), (int)(uint8_t)*dec.cur);
1480 } 1507 }
1481 1508
1482 sv = sv_2mortal (sv); 1509 sv = sv_2mortal (sv);
1483 1510
1484 return sv; 1511 return sv;
1655 ))); 1682 )));
1656} 1683}
1657 1684
1658void shrink (CBOR *self, int enable = 1) 1685void shrink (CBOR *self, int enable = 1)
1659 ALIAS: 1686 ALIAS:
1660 shrink = F_SHRINK 1687 shrink = F_SHRINK
1661 allow_unknown = F_ALLOW_UNKNOWN 1688 allow_unknown = F_ALLOW_UNKNOWN
1662 allow_sharing = F_ALLOW_SHARING 1689 allow_sharing = F_ALLOW_SHARING
1663 allow_cycles = F_ALLOW_CYCLES 1690 allow_cycles = F_ALLOW_CYCLES
1691 allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1664 forbid_objects = F_FORBID_OBJECTS 1692 forbid_objects = F_FORBID_OBJECTS
1665 pack_strings = F_PACK_STRINGS 1693 pack_strings = F_PACK_STRINGS
1666 text_keys = F_TEXT_KEYS 1694 text_keys = F_TEXT_KEYS
1667 text_strings = F_TEXT_STRINGS 1695 text_strings = F_TEXT_STRINGS
1668 validate_utf8 = F_VALIDATE_UTF8 1696 validate_utf8 = F_VALIDATE_UTF8
1669 PPCODE: 1697 PPCODE:
1670{ 1698{
1671 if (enable) 1699 if (enable)
1672 self->flags |= ix; 1700 self->flags |= ix;
1673 else 1701 else
1676 XPUSHs (ST (0)); 1704 XPUSHs (ST (0));
1677} 1705}
1678 1706
1679void get_shrink (CBOR *self) 1707void get_shrink (CBOR *self)
1680 ALIAS: 1708 ALIAS:
1681 get_shrink = F_SHRINK 1709 get_shrink = F_SHRINK
1682 get_allow_unknown = F_ALLOW_UNKNOWN 1710 get_allow_unknown = F_ALLOW_UNKNOWN
1683 get_allow_sharing = F_ALLOW_SHARING 1711 get_allow_sharing = F_ALLOW_SHARING
1684 get_allow_cycles = F_ALLOW_CYCLES 1712 get_allow_cycles = F_ALLOW_CYCLES
1713 get_allow_weak_cycles = F_ALLOW_WEAK_CYCLES
1685 get_forbid_objects = F_FORBID_OBJECTS 1714 get_forbid_objects = F_FORBID_OBJECTS
1686 get_pack_strings = F_PACK_STRINGS 1715 get_pack_strings = F_PACK_STRINGS
1687 get_text_keys = F_TEXT_KEYS 1716 get_text_keys = F_TEXT_KEYS
1688 get_text_strings = F_TEXT_STRINGS 1717 get_text_strings = F_TEXT_STRINGS
1689 get_validate_utf8 = F_VALIDATE_UTF8 1718 get_validate_utf8 = F_VALIDATE_UTF8
1690 PPCODE: 1719 PPCODE:
1691 XPUSHs (boolSV (self->flags & ix)); 1720 XPUSHs (boolSV (self->flags & ix));
1692 1721
1693void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1722void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1694 PPCODE: 1723 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines