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.37 by root, Sat Nov 30 18:13:53 2013 UTC vs.
Revision 1.43 by root, Sun Jan 5 14:24:54 2014 UTC

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)
127typedef struct { 128typedef struct {
128 U32 flags; 129 U32 flags;
129 U32 max_depth; 130 U32 max_depth;
130 STRLEN max_size; 131 STRLEN max_size;
131 SV *filter; 132 SV *filter;
133
134 // for the incremental parser
135 STRLEN incr_pos; // the current offset into the text
136 STRLEN incr_need; // minimum bytes needed to decode
137 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
132} CBOR; 138} CBOR;
133 139
134ecb_inline void 140ecb_inline void
135cbor_init (CBOR *cbor) 141cbor_init (CBOR *cbor)
136{ 142{
140 146
141ecb_inline void 147ecb_inline void
142cbor_free (CBOR *cbor) 148cbor_free (CBOR *cbor)
143{ 149{
144 SvREFCNT_dec (cbor->filter); 150 SvREFCNT_dec (cbor->filter);
151 SvREFCNT_dec (cbor->incr_count);
145} 152}
146 153
147///////////////////////////////////////////////////////////////////////////// 154/////////////////////////////////////////////////////////////////////////////
148// utility functions 155// utility functions
149 156
729{ 736{
730 // for speed reasons, we specialcase single-string 737 // for speed reasons, we specialcase single-string
731 // byte or utf-8 strings as keys, but only when !stringref 738 // byte or utf-8 strings as keys, but only when !stringref
732 739
733 if (ecb_expect_true (!dec->stringref)) 740 if (ecb_expect_true (!dec->stringref))
734 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 741 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
735 { 742 {
736 I32 len = decode_uint (dec); 743 I32 len = decode_uint (dec);
737 char *key = (char *)dec->cur; 744 char *key = (char *)dec->cur;
738 745
739 dec->cur += len; 746 dec->cur += len;
740 747
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); 748 hv_store (hv, key, len, decode_sv (dec), 0);
745 749
746 return; 750 return;
747 } 751 }
748 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 752 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
749 { 753 {
750 I32 len = decode_uint (dec); 754 I32 len = decode_uint (dec);
751 char *key = (char *)dec->cur; 755 char *key = (char *)dec->cur;
752 756
753 dec->cur += len; 757 dec->cur += len;
754 758
755 if (ecb_expect_false (dec->stringref)) 759 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
756 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 760 if (!is_utf8_string (key, len))
761 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
757 762
758 hv_store (hv, key, -len, decode_sv (dec), 0); 763 hv_store (hv, key, -len, decode_sv (dec), 0);
759 764
760 return; 765 return;
761 } 766 }
763 SV *k = decode_sv (dec); 768 SV *k = decode_sv (dec);
764 SV *v = decode_sv (dec); 769 SV *v = decode_sv (dec);
765 770
766 hv_store_ent (hv, k, v, 0); 771 hv_store_ent (hv, k, v, 0);
767 SvREFCNT_dec (k); 772 SvREFCNT_dec (k);
773
774fail:
775 ;
768} 776}
769 777
770static SV * 778static SV *
771decode_hv (dec_t *dec) 779decode_hv (dec_t *dec)
772{ 780{
854 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 862 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
855 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 863 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
856 } 864 }
857 865
858 if (utf8) 866 if (utf8)
867 {
868 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
869 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
870 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
871
859 SvUTF8_on (sv); 872 SvUTF8_on (sv);
873 }
860 874
861 return sv; 875 return sv;
862 876
863fail: 877fail:
864 SvREFCNT_dec (sv); 878 SvREFCNT_dec (sv);
1124 1138
1125 return newSVnv (ecb_binary64_to_double (fp)); 1139 return newSVnv (ecb_binary64_to_double (fp));
1126 } 1140 }
1127 1141
1128 // 0..19 unassigned simple 1142 // 0..19 unassigned simple
1129 // 24 reserved + unassigned (reserved values are not encodable) 1143 // 24 reserved + unassigned simple (reserved values are not encodable)
1144 // 28-30 unassigned misc
1145 // 31 break code
1130 default: 1146 default:
1131 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1147 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1132 } 1148 }
1133 1149
1134 break; 1150 break;
1135 } 1151 }
1136 1152
1163 if (dec.cur != dec.end && !dec.err) 1179 if (dec.cur != dec.end && !dec.err)
1164 dec.err = "garbage after CBOR object"; 1180 dec.err = "garbage after CBOR object";
1165 1181
1166 if (dec.err) 1182 if (dec.err)
1167 { 1183 {
1184 if (dec.shareable)
1185 {
1186 // need to break cyclic links, which whould all be in shareable
1187 int i;
1188 SV **svp;
1189
1190 for (i = av_len (dec.shareable) + 1; i--; )
1191 if ((svp = av_fetch (dec.shareable, i, 0)))
1192 sv_setsv (*svp, &PL_sv_undef);
1193 }
1194
1168 SvREFCNT_dec (sv); 1195 SvREFCNT_dec (sv);
1169 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1196 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1170 } 1197 }
1171 1198
1172 sv = sv_2mortal (sv); 1199 sv = sv_2mortal (sv);
1173 1200
1174 return sv; 1201 return sv;
1175} 1202}
1176 1203
1204/////////////////////////////////////////////////////////////////////////////
1205// incremental parser
1206
1207#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1208
1209// returns 0 for notyet, 1 for success or error
1210static int
1211incr_parse (CBOR *self, SV *cborstr)
1212{
1213 STRLEN cur;
1214 SvPV (cborstr, cur);
1215
1216 while (ecb_expect_true (self->incr_need <= cur))
1217 {
1218 // table of integer count bytes
1219 static I8 incr_len[MINOR_MASK + 1] = {
1220 0, 0, 0, 0, 0, 0, 0, 0,
1221 0, 0, 0, 0, 0, 0, 0, 0,
1222 0, 0, 0, 0, 0, 0, 0, 0,
1223 1, 2, 4, 8,-1,-1,-1,-2
1224 };
1225
1226 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1227 U8 m = *p & MINOR_MASK;
1228 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1229 I8 ilen = incr_len[m];
1230
1231 self->incr_need = self->incr_pos + 1;
1232
1233 if (ecb_expect_false (ilen < 0))
1234 {
1235 if (m != MINOR_INDEF)
1236 return 1; // error
1237
1238 if (*p == (MAJOR_MISC | MINOR_INDEF))
1239 {
1240 if (count >= 0)
1241 return 1; // error
1242
1243 count = 1;
1244 }
1245 else
1246 {
1247 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1248 count = -1;
1249 }
1250 }
1251 else
1252 {
1253 self->incr_need += ilen;
1254 if (ecb_expect_false (self->incr_need > cur))
1255 return 0;
1256
1257 int major = *p >> MAJOR_SHIFT;
1258
1259 switch (major)
1260 {
1261 case MAJOR_BYTES >> MAJOR_SHIFT:
1262 case MAJOR_TEXT >> MAJOR_SHIFT:
1263 case MAJOR_ARRAY >> MAJOR_SHIFT:
1264 case MAJOR_MAP >> MAJOR_SHIFT:
1265 {
1266 UV len;
1267
1268 if (ecb_expect_false (ilen))
1269 {
1270 len = 0;
1271
1272 do {
1273 len = (len << 8) | *++p;
1274 } while (--ilen);
1275 }
1276 else
1277 len = m;
1278
1279 switch (major)
1280 {
1281 case MAJOR_BYTES >> MAJOR_SHIFT:
1282 case MAJOR_TEXT >> MAJOR_SHIFT:
1283 self->incr_need += len;
1284 if (ecb_expect_false (self->incr_need > cur))
1285 return 0;
1286
1287 break;
1288
1289 case MAJOR_MAP >> MAJOR_SHIFT:
1290 len <<= 1;
1291 case MAJOR_ARRAY >> MAJOR_SHIFT:
1292 if (len)
1293 {
1294 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1295 count = len + 1;
1296 }
1297 break;
1298 }
1299 }
1300 }
1301 }
1302
1303 self->incr_pos = self->incr_need;
1304
1305 if (count > 0)
1306 {
1307 while (!--count)
1308 {
1309 if (!AvFILLp (self->incr_count))
1310 return 1; // done
1311
1312 SvREFCNT_dec_NN (av_pop (self->incr_count));
1313 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1314 }
1315
1316 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1317 }
1318 }
1319
1320 return 0;
1321}
1322
1323
1177///////////////////////////////////////////////////////////////////////////// 1324/////////////////////////////////////////////////////////////////////////////
1178// XS interface functions 1325// XS interface functions
1179 1326
1180MODULE = CBOR::XS PACKAGE = CBOR::XS 1327MODULE = CBOR::XS PACKAGE = CBOR::XS
1181 1328
1223 shrink = F_SHRINK 1370 shrink = F_SHRINK
1224 allow_unknown = F_ALLOW_UNKNOWN 1371 allow_unknown = F_ALLOW_UNKNOWN
1225 allow_sharing = F_ALLOW_SHARING 1372 allow_sharing = F_ALLOW_SHARING
1226 allow_cycles = F_ALLOW_CYCLES 1373 allow_cycles = F_ALLOW_CYCLES
1227 pack_strings = F_PACK_STRINGS 1374 pack_strings = F_PACK_STRINGS
1375 validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1376 PPCODE:
1229{ 1377{
1230 if (enable) 1378 if (enable)
1231 self->flags |= ix; 1379 self->flags |= ix;
1232 else 1380 else
1240 get_shrink = F_SHRINK 1388 get_shrink = F_SHRINK
1241 get_allow_unknown = F_ALLOW_UNKNOWN 1389 get_allow_unknown = F_ALLOW_UNKNOWN
1242 get_allow_sharing = F_ALLOW_SHARING 1390 get_allow_sharing = F_ALLOW_SHARING
1243 get_allow_cycles = F_ALLOW_CYCLES 1391 get_allow_cycles = F_ALLOW_CYCLES
1244 get_pack_strings = F_PACK_STRINGS 1392 get_pack_strings = F_PACK_STRINGS
1393 get_validate_utf8 = F_VALIDATE_UTF8
1245 PPCODE: 1394 PPCODE:
1246 XPUSHs (boolSV (self->flags & ix)); 1395 XPUSHs (boolSV (self->flags & ix));
1247 1396
1248void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1397void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1249 PPCODE: 1398 PPCODE:
1298 EXTEND (SP, 2); 1447 EXTEND (SP, 2);
1299 PUSHs (sv); 1448 PUSHs (sv);
1300 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1449 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1301} 1450}
1302 1451
1452void incr_parse (CBOR *self, SV *cborstr)
1453 ALIAS:
1454 incr_parse_multiple = 1
1455 PPCODE:
1456{
1457 if (SvUTF8 (cborstr))
1458 sv_utf8_downgrade (cborstr, 0);
1459
1460 if (!self->incr_count)
1461 {
1462 self->incr_count = newAV ();
1463 self->incr_pos = 0;
1464 self->incr_need = 1;
1465
1466 av_push (self->incr_count, newSViv (1));
1467 }
1468
1469 do
1470 {
1471 if (!incr_parse (self, cborstr))
1472 {
1473 if (self->incr_need > self->max_size && self->max_size)
1474 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1475 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1476
1477 break;
1478 }
1479
1480 SV *sv;
1481 char *offset;
1482
1483 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1484 XPUSHs (sv);
1485
1486 sv_chop (cborstr, offset);
1487
1488 av_clear (self->incr_count);
1489 av_push (self->incr_count, newSViv (1));
1490
1491 self->incr_pos = 0;
1492 self->incr_need = self->incr_pos + 1;
1493 }
1494 while (ix);
1495}
1496
1497void incr_reset (CBOR *self)
1498 CODE:
1499{
1500 SvREFCNT_dec (self->incr_count);
1501 self->incr_count = 0;
1502}
1503
1303void DESTROY (CBOR *self) 1504void DESTROY (CBOR *self)
1304 PPCODE: 1505 PPCODE:
1305 cbor_free (self); 1506 cbor_free (self);
1306 1507
1307PROTOTYPES: ENABLE 1508PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines