… | |
… | |
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 | |
… | |
… | |
730 | { |
737 | { |
731 | // for speed reasons, we specialcase single-string |
738 | // for speed reasons, we specialcase single-string |
732 | // byte or utf-8 strings as keys, but only when !stringref |
739 | // byte or utf-8 strings as keys, but only when !stringref |
733 | |
740 | |
734 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true (!dec->stringref)) |
735 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
736 | { |
743 | { |
737 | I32 len = decode_uint (dec); |
744 | I32 len = decode_uint (dec); |
738 | char *key = (char *)dec->cur; |
745 | char *key = (char *)dec->cur; |
739 | |
746 | |
740 | dec->cur += len; |
747 | dec->cur += len; |
741 | |
748 | |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | hv_store (hv, key, len, decode_sv (dec), 0); |
743 | |
750 | |
744 | return; |
751 | return; |
745 | } |
752 | } |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
747 | { |
754 | { |
748 | I32 len = decode_uint (dec); |
755 | I32 len = decode_uint (dec); |
749 | char *key = (char *)dec->cur; |
756 | char *key = (char *)dec->cur; |
750 | |
757 | |
751 | dec->cur += len; |
758 | dec->cur += len; |
… | |
… | |
1132 | |
1139 | |
1133 | return newSVnv (ecb_binary64_to_double (fp)); |
1140 | return newSVnv (ecb_binary64_to_double (fp)); |
1134 | } |
1141 | } |
1135 | |
1142 | |
1136 | // 0..19 unassigned simple |
1143 | // 0..19 unassigned simple |
1137 | // 24 reserved + unassigned (reserved values are not encodable) |
1144 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1145 | // 28-30 unassigned misc |
|
|
1146 | // 31 break code |
1138 | default: |
1147 | default: |
1139 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1148 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1140 | } |
1149 | } |
1141 | |
1150 | |
1142 | break; |
1151 | break; |
1143 | } |
1152 | } |
1144 | |
1153 | |
… | |
… | |
1191 | sv = sv_2mortal (sv); |
1200 | sv = sv_2mortal (sv); |
1192 | |
1201 | |
1193 | return sv; |
1202 | return sv; |
1194 | } |
1203 | } |
1195 | |
1204 | |
|
|
1205 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1206 | // incremental parser |
|
|
1207 | |
|
|
1208 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1209 | |
|
|
1210 | // returns 0 for notyet, 1 for success or error |
|
|
1211 | static int |
|
|
1212 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1213 | { |
|
|
1214 | STRLEN cur; |
|
|
1215 | SvPV (cborstr, cur); |
|
|
1216 | |
|
|
1217 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1218 | { |
|
|
1219 | // table of integer count bytes |
|
|
1220 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1221 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1222 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1223 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1224 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1225 | }; |
|
|
1226 | |
|
|
1227 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1228 | U8 m = *p & MINOR_MASK; |
|
|
1229 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1230 | I8 ilen = incr_len[m]; |
|
|
1231 | |
|
|
1232 | self->incr_need = self->incr_pos + 1; |
|
|
1233 | |
|
|
1234 | if (ecb_expect_false (ilen < 0)) |
|
|
1235 | { |
|
|
1236 | if (m != MINOR_INDEF) |
|
|
1237 | return 1; // error |
|
|
1238 | |
|
|
1239 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1240 | { |
|
|
1241 | if (count >= 0) |
|
|
1242 | return 1; // error |
|
|
1243 | |
|
|
1244 | count = 1; |
|
|
1245 | } |
|
|
1246 | else |
|
|
1247 | { |
|
|
1248 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1249 | count = -1; |
|
|
1250 | } |
|
|
1251 | } |
|
|
1252 | else |
|
|
1253 | { |
|
|
1254 | self->incr_need += ilen; |
|
|
1255 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1256 | return 0; |
|
|
1257 | |
|
|
1258 | int major = *p >> MAJOR_SHIFT; |
|
|
1259 | |
|
|
1260 | switch (major) |
|
|
1261 | { |
|
|
1262 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1263 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1264 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1265 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1266 | { |
|
|
1267 | UV len; |
|
|
1268 | |
|
|
1269 | if (ecb_expect_false (ilen)) |
|
|
1270 | { |
|
|
1271 | len = 0; |
|
|
1272 | |
|
|
1273 | do { |
|
|
1274 | len = (len << 8) | *++p; |
|
|
1275 | } while (--ilen); |
|
|
1276 | } |
|
|
1277 | else |
|
|
1278 | len = m; |
|
|
1279 | |
|
|
1280 | switch (major) |
|
|
1281 | { |
|
|
1282 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1283 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1284 | self->incr_need += len; |
|
|
1285 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1286 | return 0; |
|
|
1287 | |
|
|
1288 | break; |
|
|
1289 | |
|
|
1290 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1291 | len <<= 1; |
|
|
1292 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1293 | if (len) |
|
|
1294 | { |
|
|
1295 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1296 | count = len + 1; |
|
|
1297 | } |
|
|
1298 | break; |
|
|
1299 | } |
|
|
1300 | } |
|
|
1301 | } |
|
|
1302 | } |
|
|
1303 | |
|
|
1304 | self->incr_pos = self->incr_need; |
|
|
1305 | |
|
|
1306 | if (count > 0) |
|
|
1307 | { |
|
|
1308 | while (!--count) |
|
|
1309 | { |
|
|
1310 | if (!AvFILLp (self->incr_count)) |
|
|
1311 | return 1; // done |
|
|
1312 | |
|
|
1313 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1314 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1318 | } |
|
|
1319 | } |
|
|
1320 | |
|
|
1321 | return 0; |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | |
1196 | ///////////////////////////////////////////////////////////////////////////// |
1325 | ///////////////////////////////////////////////////////////////////////////// |
1197 | // XS interface functions |
1326 | // XS interface functions |
1198 | |
1327 | |
1199 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1328 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1200 | |
1329 | |
… | |
… | |
1319 | EXTEND (SP, 2); |
1448 | EXTEND (SP, 2); |
1320 | PUSHs (sv); |
1449 | PUSHs (sv); |
1321 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1322 | } |
1451 | } |
1323 | |
1452 | |
|
|
1453 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1454 | ALIAS: |
|
|
1455 | incr_parse_multiple = 1 |
|
|
1456 | PPCODE: |
|
|
1457 | { |
|
|
1458 | if (SvUTF8 (cborstr)) |
|
|
1459 | sv_utf8_downgrade (cborstr, 0); |
|
|
1460 | |
|
|
1461 | if (!self->incr_count) |
|
|
1462 | { |
|
|
1463 | self->incr_count = newAV (); |
|
|
1464 | self->incr_pos = 0; |
|
|
1465 | self->incr_need = 1; |
|
|
1466 | |
|
|
1467 | av_push (self->incr_count, newSViv (1)); |
|
|
1468 | } |
|
|
1469 | |
|
|
1470 | do |
|
|
1471 | { |
|
|
1472 | if (!incr_parse (self, cborstr)) |
|
|
1473 | { |
|
|
1474 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1475 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1476 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1477 | |
|
|
1478 | break; |
|
|
1479 | } |
|
|
1480 | |
|
|
1481 | SV *sv; |
|
|
1482 | char *offset; |
|
|
1483 | |
|
|
1484 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1485 | XPUSHs (sv); |
|
|
1486 | |
|
|
1487 | sv_chop (cborstr, offset); |
|
|
1488 | |
|
|
1489 | av_clear (self->incr_count); |
|
|
1490 | av_push (self->incr_count, newSViv (1)); |
|
|
1491 | |
|
|
1492 | self->incr_pos = 0; |
|
|
1493 | self->incr_need = self->incr_pos + 1; |
|
|
1494 | } |
|
|
1495 | while (ix); |
|
|
1496 | } |
|
|
1497 | |
|
|
1498 | void incr_reset (CBOR *self) |
|
|
1499 | CODE: |
|
|
1500 | { |
|
|
1501 | SvREFCNT_dec (self->incr_count); |
|
|
1502 | self->incr_count = 0; |
|
|
1503 | } |
|
|
1504 | |
1324 | void DESTROY (CBOR *self) |
1505 | void DESTROY (CBOR *self) |
1325 | PPCODE: |
1506 | PPCODE: |
1326 | cbor_free (self); |
1507 | cbor_free (self); |
1327 | |
1508 | |
1328 | PROTOTYPES: ENABLE |
1509 | PROTOTYPES: ENABLE |