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.39 by root, Sun Dec 1 14:45:03 2013 UTC vs.
Revision 1.40 by root, Tue Dec 10 13:54:40 2013 UTC

128typedef struct { 128typedef struct {
129 U32 flags; 129 U32 flags;
130 U32 max_depth; 130 U32 max_depth;
131 STRLEN max_size; 131 STRLEN max_size;
132 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.
133} CBOR; 138} CBOR;
134 139
135ecb_inline void 140ecb_inline void
136cbor_init (CBOR *cbor) 141cbor_init (CBOR *cbor)
137{ 142{
141 146
142ecb_inline void 147ecb_inline void
143cbor_free (CBOR *cbor) 148cbor_free (CBOR *cbor)
144{ 149{
145 SvREFCNT_dec (cbor->filter); 150 SvREFCNT_dec (cbor->filter);
151 SvREFCNT_dec (cbor->incr_count);
146} 152}
147 153
148///////////////////////////////////////////////////////////////////////////// 154/////////////////////////////////////////////////////////////////////////////
149// utility functions 155// utility functions
150 156
1132 1138
1133 return newSVnv (ecb_binary64_to_double (fp)); 1139 return newSVnv (ecb_binary64_to_double (fp));
1134 } 1140 }
1135 1141
1136 // 0..19 unassigned simple 1142 // 0..19 unassigned simple
1137 // 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
1138 default: 1146 default:
1139 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1147 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1140 } 1148 }
1141 1149
1142 break; 1150 break;
1143 } 1151 }
1144 1152
1191 sv = sv_2mortal (sv); 1199 sv = sv_2mortal (sv);
1192 1200
1193 return sv; 1201 return sv;
1194} 1202}
1195 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
1196///////////////////////////////////////////////////////////////////////////// 1324/////////////////////////////////////////////////////////////////////////////
1197// XS interface functions 1325// XS interface functions
1198 1326
1199MODULE = CBOR::XS PACKAGE = CBOR::XS 1327MODULE = CBOR::XS PACKAGE = CBOR::XS
1200 1328
1319 EXTEND (SP, 2); 1447 EXTEND (SP, 2);
1320 PUSHs (sv); 1448 PUSHs (sv);
1321 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1449 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1322} 1450}
1323 1451
1452void incr_parse (CBOR *self, SV *cborstr, int chop = 0)
1453 PPCODE:
1454{
1455 if (SvUTF8 (cborstr))
1456 sv_utf8_downgrade (cborstr, 0);
1457
1458 if (!self->incr_count)
1459 {
1460 self->incr_count = newAV ();
1461 self->incr_pos = 0;
1462 self->incr_need = 1;
1463
1464 av_push (self->incr_count, newSViv (1));
1465 }
1466
1467 for (;;)
1468 {
1469 if (!incr_parse (self, cborstr))
1470 {
1471 if (self->incr_need > self->max_size && self->max_size)
1472 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1473 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1474
1475 break;
1476 }
1477
1478 SV *sv;
1479 char *offset;
1480
1481 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1482 XPUSHs (sv);
1483
1484 av_clear (self->incr_count);
1485 av_push (self->incr_count, newSViv (1));
1486
1487 if (chop)
1488 {
1489 self->incr_pos = 0;
1490 sv_chop (cborstr, offset);
1491 }
1492 else
1493 self->incr_pos = offset - SvPVX (cborstr);
1494
1495 self->incr_need = self->incr_pos + 1;
1496 }
1497}
1498
1499void incr_reset (CBOR *self)
1500 CODE:
1501{
1502 SvREFCNT_dec (self->incr_count);
1503 self->incr_count = 0;
1504}
1505
1324void DESTROY (CBOR *self) 1506void DESTROY (CBOR *self)
1325 PPCODE: 1507 PPCODE:
1326 cbor_free (self); 1508 cbor_free (self);
1327 1509
1328PROTOTYPES: ENABLE 1510PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines