… | |
… | |
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)) |
… | |
… | |
323 | |
324 | |
324 | ++enc->depth; |
325 | ++enc->depth; |
325 | |
326 | |
326 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | |
328 | |
|
|
329 | if (SvMAGICAL (av)) |
328 | for (i = 0; i <= len; ++i) |
330 | for (i = 0; i <= len; ++i) |
329 | { |
331 | { |
330 | SV **svp = av_fetch (av, i, 0); |
332 | SV **svp = av_fetch (av, i, 0); |
331 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
333 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
332 | } |
334 | } |
|
|
335 | else |
|
|
336 | for (i = 0; i <= len; ++i) |
|
|
337 | { |
|
|
338 | SV *sv = AvARRAY (av)[i]; |
|
|
339 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
340 | } |
333 | |
341 | |
334 | --enc->depth; |
342 | --enc->depth; |
335 | } |
343 | } |
336 | |
344 | |
337 | static void |
345 | static void |
… | |
… | |
576 | } |
584 | } |
577 | |
585 | |
578 | static SV * |
586 | static SV * |
579 | encode_cbor (SV *scalar, CBOR *cbor) |
587 | encode_cbor (SV *scalar, CBOR *cbor) |
580 | { |
588 | { |
581 | enc_t enc = { }; |
589 | enc_t enc = { 0 }; |
582 | |
590 | |
583 | enc.cbor = *cbor; |
591 | enc.cbor = *cbor; |
584 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
592 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
585 | enc.cur = SvPVX (enc.sv); |
593 | enc.cur = SvPVX (enc.sv); |
586 | enc.end = SvEND (enc.sv); |
594 | enc.end = SvEND (enc.sv); |
… | |
… | |
736 | { |
744 | { |
737 | // for speed reasons, we specialcase single-string |
745 | // for speed reasons, we specialcase single-string |
738 | // byte or utf-8 strings as keys, but only when !stringref |
746 | // byte or utf-8 strings as keys, but only when !stringref |
739 | |
747 | |
740 | if (ecb_expect_true (!dec->stringref)) |
748 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
749 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | { |
750 | { |
743 | I32 len = decode_uint (dec); |
751 | I32 len = decode_uint (dec); |
744 | char *key = (char *)dec->cur; |
752 | char *key = (char *)dec->cur; |
745 | |
753 | |
|
|
754 | WANT (len); |
746 | dec->cur += len; |
755 | dec->cur += len; |
747 | |
756 | |
748 | hv_store (hv, key, len, decode_sv (dec), 0); |
757 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | |
758 | |
750 | return; |
759 | return; |
751 | } |
760 | } |
752 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
761 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | { |
762 | { |
754 | I32 len = decode_uint (dec); |
763 | I32 len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
764 | char *key = (char *)dec->cur; |
756 | |
765 | |
|
|
766 | WANT (len); |
757 | dec->cur += len; |
767 | dec->cur += len; |
758 | |
768 | |
759 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
769 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
760 | if (!is_utf8_string (key, len)) |
770 | if (!is_utf8_string (key, len)) |
761 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
771 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
1155 | } |
1165 | } |
1156 | |
1166 | |
1157 | static SV * |
1167 | static SV * |
1158 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1168 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1159 | { |
1169 | { |
1160 | dec_t dec = { }; |
1170 | dec_t dec = { 0 }; |
1161 | SV *sv; |
1171 | SV *sv; |
1162 | STRLEN len; |
1172 | STRLEN len; |
1163 | char *data = SvPVbyte (string, len); |
1173 | char *data = SvPVbyte (string, len); |
1164 | |
1174 | |
1165 | if (len > cbor->max_size && cbor->max_size) |
1175 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1256 | |
1266 | |
1257 | int major = *p >> MAJOR_SHIFT; |
1267 | int major = *p >> MAJOR_SHIFT; |
1258 | |
1268 | |
1259 | switch (major) |
1269 | switch (major) |
1260 | { |
1270 | { |
|
|
1271 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1272 | ++count; // tags merely prefix another value |
|
|
1273 | break; |
|
|
1274 | |
1261 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1275 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1262 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1276 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1263 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1277 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1264 | case MAJOR_MAP >> MAJOR_SHIFT: |
1278 | case MAJOR_MAP >> MAJOR_SHIFT: |
1265 | { |
1279 | { |
… | |
… | |
1448 | PUSHs (sv); |
1462 | PUSHs (sv); |
1449 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1463 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | } |
1464 | } |
1451 | |
1465 | |
1452 | void incr_parse (CBOR *self, SV *cborstr) |
1466 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1467 | ALIAS: |
|
|
1468 | incr_parse_multiple = 1 |
1453 | PPCODE: |
1469 | PPCODE: |
1454 | { |
1470 | { |
1455 | if (SvUTF8 (cborstr)) |
1471 | if (SvUTF8 (cborstr)) |
1456 | sv_utf8_downgrade (cborstr, 0); |
1472 | sv_utf8_downgrade (cborstr, 0); |
1457 | |
1473 | |
… | |
… | |
1487 | av_push (self->incr_count, newSViv (1)); |
1503 | av_push (self->incr_count, newSViv (1)); |
1488 | |
1504 | |
1489 | self->incr_pos = 0; |
1505 | self->incr_pos = 0; |
1490 | self->incr_need = self->incr_pos + 1; |
1506 | self->incr_need = self->incr_pos + 1; |
1491 | } |
1507 | } |
1492 | while (GIMME_V == G_ARRAY); |
1508 | while (ix); |
1493 | } |
1509 | } |
1494 | |
1510 | |
1495 | void incr_reset (CBOR *self) |
1511 | void incr_reset (CBOR *self) |
1496 | CODE: |
1512 | CODE: |
1497 | { |
1513 | { |