… | |
… | |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
|
|
12 | #define ECB_NO_THREADS 1 |
12 | #include "ecb.h" |
13 | #include "ecb.h" |
13 | |
14 | |
14 | // compatibility with perl <5.18 |
15 | // compatibility with perl <5.18 |
15 | #ifndef HvNAMELEN_get |
16 | #ifndef HvNAMELEN_get |
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
17 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
… | |
… | |
128 | typedef struct { |
129 | typedef struct { |
129 | U32 flags; |
130 | U32 flags; |
130 | U32 max_depth; |
131 | U32 max_depth; |
131 | STRLEN max_size; |
132 | STRLEN max_size; |
132 | SV *filter; |
133 | SV *filter; |
|
|
134 | |
|
|
135 | // for the incremental parser |
|
|
136 | STRLEN incr_pos; // the current offset into the text |
|
|
137 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
138 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
133 | } CBOR; |
139 | } CBOR; |
134 | |
140 | |
135 | ecb_inline void |
141 | ecb_inline void |
136 | cbor_init (CBOR *cbor) |
142 | cbor_init (CBOR *cbor) |
137 | { |
143 | { |
… | |
… | |
141 | |
147 | |
142 | ecb_inline void |
148 | ecb_inline void |
143 | cbor_free (CBOR *cbor) |
149 | cbor_free (CBOR *cbor) |
144 | { |
150 | { |
145 | SvREFCNT_dec (cbor->filter); |
151 | SvREFCNT_dec (cbor->filter); |
|
|
152 | SvREFCNT_dec (cbor->incr_count); |
146 | } |
153 | } |
147 | |
154 | |
148 | ///////////////////////////////////////////////////////////////////////////// |
155 | ///////////////////////////////////////////////////////////////////////////// |
149 | // utility functions |
156 | // utility functions |
150 | |
157 | |
… | |
… | |
317 | |
324 | |
318 | ++enc->depth; |
325 | ++enc->depth; |
319 | |
326 | |
320 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
321 | |
328 | |
|
|
329 | if (SvMAGICAL (av)) |
322 | for (i = 0; i <= len; ++i) |
330 | for (i = 0; i <= len; ++i) |
323 | { |
331 | { |
324 | SV **svp = av_fetch (av, i, 0); |
332 | SV **svp = av_fetch (av, i, 0); |
325 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
333 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
326 | } |
334 | } |
|
|
335 | else |
|
|
336 | for (i = 0; i <= len; ++i) |
|
|
337 | encode_sv (enc, AvARRAY (av)[i]); |
327 | |
338 | |
328 | --enc->depth; |
339 | --enc->depth; |
329 | } |
340 | } |
330 | |
341 | |
331 | static void |
342 | static void |
… | |
… | |
730 | { |
741 | { |
731 | // for speed reasons, we specialcase single-string |
742 | // for speed reasons, we specialcase single-string |
732 | // byte or utf-8 strings as keys, but only when !stringref |
743 | // byte or utf-8 strings as keys, but only when !stringref |
733 | |
744 | |
734 | if (ecb_expect_true (!dec->stringref)) |
745 | if (ecb_expect_true (!dec->stringref)) |
735 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
746 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
736 | { |
747 | { |
737 | I32 len = decode_uint (dec); |
748 | I32 len = decode_uint (dec); |
738 | char *key = (char *)dec->cur; |
749 | char *key = (char *)dec->cur; |
739 | |
750 | |
740 | dec->cur += len; |
751 | dec->cur += len; |
741 | |
752 | |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
753 | hv_store (hv, key, len, decode_sv (dec), 0); |
743 | |
754 | |
744 | return; |
755 | return; |
745 | } |
756 | } |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
757 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
747 | { |
758 | { |
748 | I32 len = decode_uint (dec); |
759 | I32 len = decode_uint (dec); |
749 | char *key = (char *)dec->cur; |
760 | char *key = (char *)dec->cur; |
750 | |
761 | |
751 | dec->cur += len; |
762 | dec->cur += len; |
… | |
… | |
1132 | |
1143 | |
1133 | return newSVnv (ecb_binary64_to_double (fp)); |
1144 | return newSVnv (ecb_binary64_to_double (fp)); |
1134 | } |
1145 | } |
1135 | |
1146 | |
1136 | // 0..19 unassigned simple |
1147 | // 0..19 unassigned simple |
1137 | // 24 reserved + unassigned (reserved values are not encodable) |
1148 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1149 | // 28-30 unassigned misc |
|
|
1150 | // 31 break code |
1138 | default: |
1151 | default: |
1139 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1152 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1140 | } |
1153 | } |
1141 | |
1154 | |
1142 | break; |
1155 | break; |
1143 | } |
1156 | } |
1144 | |
1157 | |
… | |
… | |
1191 | sv = sv_2mortal (sv); |
1204 | sv = sv_2mortal (sv); |
1192 | |
1205 | |
1193 | return sv; |
1206 | return sv; |
1194 | } |
1207 | } |
1195 | |
1208 | |
|
|
1209 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1210 | // incremental parser |
|
|
1211 | |
|
|
1212 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1213 | |
|
|
1214 | // returns 0 for notyet, 1 for success or error |
|
|
1215 | static int |
|
|
1216 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1217 | { |
|
|
1218 | STRLEN cur; |
|
|
1219 | SvPV (cborstr, cur); |
|
|
1220 | |
|
|
1221 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1222 | { |
|
|
1223 | // table of integer count bytes |
|
|
1224 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1225 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1226 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1227 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1228 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1229 | }; |
|
|
1230 | |
|
|
1231 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1232 | U8 m = *p & MINOR_MASK; |
|
|
1233 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1234 | I8 ilen = incr_len[m]; |
|
|
1235 | |
|
|
1236 | self->incr_need = self->incr_pos + 1; |
|
|
1237 | |
|
|
1238 | if (ecb_expect_false (ilen < 0)) |
|
|
1239 | { |
|
|
1240 | if (m != MINOR_INDEF) |
|
|
1241 | return 1; // error |
|
|
1242 | |
|
|
1243 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1244 | { |
|
|
1245 | if (count >= 0) |
|
|
1246 | return 1; // error |
|
|
1247 | |
|
|
1248 | count = 1; |
|
|
1249 | } |
|
|
1250 | else |
|
|
1251 | { |
|
|
1252 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1253 | count = -1; |
|
|
1254 | } |
|
|
1255 | } |
|
|
1256 | else |
|
|
1257 | { |
|
|
1258 | self->incr_need += ilen; |
|
|
1259 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1260 | return 0; |
|
|
1261 | |
|
|
1262 | int major = *p >> MAJOR_SHIFT; |
|
|
1263 | |
|
|
1264 | switch (major) |
|
|
1265 | { |
|
|
1266 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1267 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1268 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1269 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1270 | { |
|
|
1271 | UV len; |
|
|
1272 | |
|
|
1273 | if (ecb_expect_false (ilen)) |
|
|
1274 | { |
|
|
1275 | len = 0; |
|
|
1276 | |
|
|
1277 | do { |
|
|
1278 | len = (len << 8) | *++p; |
|
|
1279 | } while (--ilen); |
|
|
1280 | } |
|
|
1281 | else |
|
|
1282 | len = m; |
|
|
1283 | |
|
|
1284 | switch (major) |
|
|
1285 | { |
|
|
1286 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1287 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1288 | self->incr_need += len; |
|
|
1289 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1290 | return 0; |
|
|
1291 | |
|
|
1292 | break; |
|
|
1293 | |
|
|
1294 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1295 | len <<= 1; |
|
|
1296 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1297 | if (len) |
|
|
1298 | { |
|
|
1299 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1300 | count = len + 1; |
|
|
1301 | } |
|
|
1302 | break; |
|
|
1303 | } |
|
|
1304 | } |
|
|
1305 | } |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | self->incr_pos = self->incr_need; |
|
|
1309 | |
|
|
1310 | if (count > 0) |
|
|
1311 | { |
|
|
1312 | while (!--count) |
|
|
1313 | { |
|
|
1314 | if (!AvFILLp (self->incr_count)) |
|
|
1315 | return 1; // done |
|
|
1316 | |
|
|
1317 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1318 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1319 | } |
|
|
1320 | |
|
|
1321 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1322 | } |
|
|
1323 | } |
|
|
1324 | |
|
|
1325 | return 0; |
|
|
1326 | } |
|
|
1327 | |
|
|
1328 | |
1196 | ///////////////////////////////////////////////////////////////////////////// |
1329 | ///////////////////////////////////////////////////////////////////////////// |
1197 | // XS interface functions |
1330 | // XS interface functions |
1198 | |
1331 | |
1199 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1332 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1200 | |
1333 | |
… | |
… | |
1319 | EXTEND (SP, 2); |
1452 | EXTEND (SP, 2); |
1320 | PUSHs (sv); |
1453 | PUSHs (sv); |
1321 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1454 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1322 | } |
1455 | } |
1323 | |
1456 | |
|
|
1457 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1458 | ALIAS: |
|
|
1459 | incr_parse_multiple = 1 |
|
|
1460 | PPCODE: |
|
|
1461 | { |
|
|
1462 | if (SvUTF8 (cborstr)) |
|
|
1463 | sv_utf8_downgrade (cborstr, 0); |
|
|
1464 | |
|
|
1465 | if (!self->incr_count) |
|
|
1466 | { |
|
|
1467 | self->incr_count = newAV (); |
|
|
1468 | self->incr_pos = 0; |
|
|
1469 | self->incr_need = 1; |
|
|
1470 | |
|
|
1471 | av_push (self->incr_count, newSViv (1)); |
|
|
1472 | } |
|
|
1473 | |
|
|
1474 | do |
|
|
1475 | { |
|
|
1476 | if (!incr_parse (self, cborstr)) |
|
|
1477 | { |
|
|
1478 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1479 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1480 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1481 | |
|
|
1482 | break; |
|
|
1483 | } |
|
|
1484 | |
|
|
1485 | SV *sv; |
|
|
1486 | char *offset; |
|
|
1487 | |
|
|
1488 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1489 | XPUSHs (sv); |
|
|
1490 | |
|
|
1491 | sv_chop (cborstr, offset); |
|
|
1492 | |
|
|
1493 | av_clear (self->incr_count); |
|
|
1494 | av_push (self->incr_count, newSViv (1)); |
|
|
1495 | |
|
|
1496 | self->incr_pos = 0; |
|
|
1497 | self->incr_need = self->incr_pos + 1; |
|
|
1498 | } |
|
|
1499 | while (ix); |
|
|
1500 | } |
|
|
1501 | |
|
|
1502 | void incr_reset (CBOR *self) |
|
|
1503 | CODE: |
|
|
1504 | { |
|
|
1505 | SvREFCNT_dec (self->incr_count); |
|
|
1506 | self->incr_count = 0; |
|
|
1507 | } |
|
|
1508 | |
1324 | void DESTROY (CBOR *self) |
1509 | void DESTROY (CBOR *self) |
1325 | PPCODE: |
1510 | PPCODE: |
1326 | cbor_free (self); |
1511 | cbor_free (self); |
1327 | |
1512 | |
1328 | PROTOTYPES: ENABLE |
1513 | PROTOTYPES: ENABLE |