… | |
… | |
128 | typedef struct { |
128 | typedef 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 | |
135 | ecb_inline void |
140 | ecb_inline void |
136 | cbor_init (CBOR *cbor) |
141 | cbor_init (CBOR *cbor) |
137 | { |
142 | { |
… | |
… | |
141 | |
146 | |
142 | ecb_inline void |
147 | ecb_inline void |
143 | cbor_free (CBOR *cbor) |
148 | cbor_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 | |
… | |
… | |
1171 | if (dec.cur != dec.end && !dec.err) |
1179 | if (dec.cur != dec.end && !dec.err) |
1172 | dec.err = "garbage after CBOR object"; |
1180 | dec.err = "garbage after CBOR object"; |
1173 | |
1181 | |
1174 | if (dec.err) |
1182 | if (dec.err) |
1175 | { |
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 | |
1176 | SvREFCNT_dec (sv); |
1195 | SvREFCNT_dec (sv); |
1177 | 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); |
1178 | } |
1197 | } |
1179 | |
1198 | |
1180 | sv = sv_2mortal (sv); |
1199 | sv = sv_2mortal (sv); |
1181 | |
1200 | |
1182 | return sv; |
1201 | return sv; |
1183 | } |
1202 | } |
1184 | |
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 |
|
|
1210 | static int |
|
|
1211 | incr_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 | |
1185 | ///////////////////////////////////////////////////////////////////////////// |
1324 | ///////////////////////////////////////////////////////////////////////////// |
1186 | // XS interface functions |
1325 | // XS interface functions |
1187 | |
1326 | |
1188 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1327 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1189 | |
1328 | |
… | |
… | |
1308 | EXTEND (SP, 2); |
1447 | EXTEND (SP, 2); |
1309 | PUSHs (sv); |
1448 | PUSHs (sv); |
1310 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1449 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1311 | } |
1450 | } |
1312 | |
1451 | |
|
|
1452 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
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 | do |
|
|
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 | sv_chop (cborstr, offset); |
|
|
1485 | |
|
|
1486 | av_clear (self->incr_count); |
|
|
1487 | av_push (self->incr_count, newSViv (1)); |
|
|
1488 | |
|
|
1489 | self->incr_pos = 0; |
|
|
1490 | self->incr_need = self->incr_pos + 1; |
|
|
1491 | } |
|
|
1492 | while (GIMME_V == G_ARRAY); |
|
|
1493 | } |
|
|
1494 | |
|
|
1495 | void incr_reset (CBOR *self) |
|
|
1496 | CODE: |
|
|
1497 | { |
|
|
1498 | SvREFCNT_dec (self->incr_count); |
|
|
1499 | self->incr_count = 0; |
|
|
1500 | } |
|
|
1501 | |
1313 | void DESTROY (CBOR *self) |
1502 | void DESTROY (CBOR *self) |
1314 | PPCODE: |
1503 | PPCODE: |
1315 | cbor_free (self); |
1504 | cbor_free (self); |
1316 | |
1505 | |
1317 | PROTOTYPES: ENABLE |
1506 | PROTOTYPES: ENABLE |