… | |
… | |
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)) |
… | |
… | |
99 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_UTF8_STRINGS 0x00000020UL |
104 | #define F_VALIDATE_UTF8 0x00000020UL |
106 | #define F_VALIDATE_UTF8 0x00000040UL |
105 | |
107 | |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | #define INIT_SIZE 32 // initial scalar size to be allocated |
107 | |
109 | |
108 | #define SB do { |
110 | #define SB do { |
109 | #define SE } while (0) |
111 | #define SE } while (0) |
… | |
… | |
278 | } |
280 | } |
279 | |
281 | |
280 | ecb_inline void |
282 | ecb_inline void |
281 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
282 | { |
284 | { |
|
|
285 | if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) |
|
|
286 | if (!utf8) |
|
|
287 | { |
|
|
288 | // exceptional path for bytze strings that need to be utf8-encoded |
|
|
289 | STRLEN ulen = len; |
|
|
290 | U8 *p, *pend = (U8 *)str + len; |
|
|
291 | |
|
|
292 | for (p = (U8 *)str; p < pend; ++p) |
|
|
293 | ulen += *p >> 7; // count set high bits |
|
|
294 | |
|
|
295 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
296 | |
|
|
297 | need (enc, ulen); |
|
|
298 | for (p = (U8 *)str; p < pend; ++p) |
|
|
299 | if (*p < 0x80) |
|
|
300 | *enc->cur++ = *p; |
|
|
301 | else |
|
|
302 | { |
|
|
303 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
304 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
305 | } |
|
|
306 | |
|
|
307 | return; |
|
|
308 | } |
|
|
309 | |
283 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
310 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
284 | need (enc, len); |
311 | need (enc, len); |
285 | memcpy (enc->cur, str, len); |
312 | memcpy (enc->cur, str, len); |
286 | enc->cur += len; |
313 | enc->cur += len; |
287 | } |
314 | } |
… | |
… | |
323 | |
350 | |
324 | ++enc->depth; |
351 | ++enc->depth; |
325 | |
352 | |
326 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
353 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | |
354 | |
|
|
355 | if (SvMAGICAL (av)) |
328 | for (i = 0; i <= len; ++i) |
356 | for (i = 0; i <= len; ++i) |
329 | { |
357 | { |
330 | SV **svp = av_fetch (av, i, 0); |
358 | SV **svp = av_fetch (av, i, 0); |
331 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
359 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
332 | } |
360 | } |
|
|
361 | else |
|
|
362 | for (i = 0; i <= len; ++i) |
|
|
363 | { |
|
|
364 | SV *sv = AvARRAY (av)[i]; |
|
|
365 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
366 | } |
333 | |
367 | |
334 | --enc->depth; |
368 | --enc->depth; |
335 | } |
369 | } |
336 | |
370 | |
337 | static void |
371 | static void |
… | |
… | |
441 | |
475 | |
442 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
476 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
443 | { |
477 | { |
444 | dSP; |
478 | dSP; |
445 | |
479 | |
446 | ENTER; SAVETMPS; PUSHMARK (SP); |
480 | ENTER; SAVETMPS; |
|
|
481 | PUSHMARK (SP); |
447 | // we re-bless the reference to get overload and other niceties right |
482 | // we re-bless the reference to get overload and other niceties right |
448 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
483 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
449 | |
484 | |
450 | PUTBACK; |
485 | PUTBACK; |
451 | // G_SCALAR ensures that return value is 1 |
486 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
464 | } |
499 | } |
465 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
500 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
466 | { |
501 | { |
467 | dSP; |
502 | dSP; |
468 | |
503 | |
469 | ENTER; SAVETMPS; PUSHMARK (SP); |
504 | ENTER; SAVETMPS; |
|
|
505 | SAVESTACK_POS (); |
|
|
506 | PUSHMARK (SP); |
470 | EXTEND (SP, 2); |
507 | EXTEND (SP, 2); |
471 | // we re-bless the reference to get overload and other niceties right |
508 | // we re-bless the reference to get overload and other niceties right |
472 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
509 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
473 | PUSHs (sv_cbor); |
510 | PUSHs (sv_cbor); |
474 | |
511 | |
… | |
… | |
576 | } |
613 | } |
577 | |
614 | |
578 | static SV * |
615 | static SV * |
579 | encode_cbor (SV *scalar, CBOR *cbor) |
616 | encode_cbor (SV *scalar, CBOR *cbor) |
580 | { |
617 | { |
581 | enc_t enc = { }; |
618 | enc_t enc = { 0 }; |
582 | |
619 | |
583 | enc.cbor = *cbor; |
620 | enc.cbor = *cbor; |
584 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
621 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
585 | enc.cur = SvPVX (enc.sv); |
622 | enc.cur = SvPVX (enc.sv); |
586 | enc.end = SvEND (enc.sv); |
623 | enc.end = SvEND (enc.sv); |
… | |
… | |
736 | { |
773 | { |
737 | // for speed reasons, we specialcase single-string |
774 | // for speed reasons, we specialcase single-string |
738 | // byte or utf-8 strings as keys, but only when !stringref |
775 | // byte or utf-8 strings as keys, but only when !stringref |
739 | |
776 | |
740 | if (ecb_expect_true (!dec->stringref)) |
777 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
778 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | { |
779 | { |
743 | I32 len = decode_uint (dec); |
780 | I32 len = decode_uint (dec); |
744 | char *key = (char *)dec->cur; |
781 | char *key = (char *)dec->cur; |
745 | |
782 | |
|
|
783 | WANT (len); |
746 | dec->cur += len; |
784 | dec->cur += len; |
747 | |
785 | |
748 | hv_store (hv, key, len, decode_sv (dec), 0); |
786 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | |
787 | |
750 | return; |
788 | return; |
751 | } |
789 | } |
752 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
790 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | { |
791 | { |
754 | I32 len = decode_uint (dec); |
792 | I32 len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
793 | char *key = (char *)dec->cur; |
756 | |
794 | |
|
|
795 | WANT (len); |
757 | dec->cur += len; |
796 | dec->cur += len; |
758 | |
797 | |
759 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
798 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
760 | if (!is_utf8_string (key, len)) |
799 | if (!is_utf8_string (key, len)) |
761 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
800 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
984 | if (!method) |
1023 | if (!method) |
985 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1024 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
986 | |
1025 | |
987 | dSP; |
1026 | dSP; |
988 | |
1027 | |
989 | ENTER; SAVETMPS; PUSHMARK (SP); |
1028 | ENTER; SAVETMPS; |
|
|
1029 | PUSHMARK (SP); |
990 | EXTEND (SP, len + 1); |
1030 | EXTEND (SP, len + 1); |
991 | // we re-bless the reference to get overload and other niceties right |
1031 | // we re-bless the reference to get overload and other niceties right |
992 | PUSHs (*av_fetch (av, 0, 1)); |
1032 | PUSHs (*av_fetch (av, 0, 1)); |
993 | PUSHs (sv_cbor); |
1033 | PUSHs (sv_cbor); |
994 | |
1034 | |
… | |
… | |
1019 | default: |
1059 | default: |
1020 | { |
1060 | { |
1021 | sv = decode_sv (dec); |
1061 | sv = decode_sv (dec); |
1022 | |
1062 | |
1023 | dSP; |
1063 | dSP; |
1024 | ENTER; SAVETMPS; PUSHMARK (SP); |
1064 | ENTER; SAVETMPS; |
|
|
1065 | SAVESTACK_POS (); |
|
|
1066 | PUSHMARK (SP); |
1025 | EXTEND (SP, 2); |
1067 | EXTEND (SP, 2); |
1026 | PUSHs (newSVuv (tag)); |
1068 | PUSHs (newSVuv (tag)); |
1027 | PUSHs (sv); |
1069 | PUSHs (sv); |
1028 | |
1070 | |
1029 | PUTBACK; |
1071 | PUTBACK; |
… | |
… | |
1155 | } |
1197 | } |
1156 | |
1198 | |
1157 | static SV * |
1199 | static SV * |
1158 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1200 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1159 | { |
1201 | { |
1160 | dec_t dec = { }; |
1202 | dec_t dec = { 0 }; |
1161 | SV *sv; |
1203 | SV *sv; |
1162 | STRLEN len; |
1204 | STRLEN len; |
1163 | char *data = SvPVbyte (string, len); |
1205 | char *data = SvPVbyte (string, len); |
1164 | |
1206 | |
1165 | if (len > cbor->max_size && cbor->max_size) |
1207 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1256 | |
1298 | |
1257 | int major = *p >> MAJOR_SHIFT; |
1299 | int major = *p >> MAJOR_SHIFT; |
1258 | |
1300 | |
1259 | switch (major) |
1301 | switch (major) |
1260 | { |
1302 | { |
|
|
1303 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1304 | ++count; // tags merely prefix another value |
|
|
1305 | break; |
|
|
1306 | |
1261 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1307 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1262 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1308 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1263 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1309 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1264 | case MAJOR_MAP >> MAJOR_SHIFT: |
1310 | case MAJOR_MAP >> MAJOR_SHIFT: |
1265 | { |
1311 | { |
… | |
… | |
1370 | shrink = F_SHRINK |
1416 | shrink = F_SHRINK |
1371 | allow_unknown = F_ALLOW_UNKNOWN |
1417 | allow_unknown = F_ALLOW_UNKNOWN |
1372 | allow_sharing = F_ALLOW_SHARING |
1418 | allow_sharing = F_ALLOW_SHARING |
1373 | allow_cycles = F_ALLOW_CYCLES |
1419 | allow_cycles = F_ALLOW_CYCLES |
1374 | pack_strings = F_PACK_STRINGS |
1420 | pack_strings = F_PACK_STRINGS |
|
|
1421 | utf8_strings = F_UTF8_STRINGS |
1375 | validate_utf8 = F_VALIDATE_UTF8 |
1422 | validate_utf8 = F_VALIDATE_UTF8 |
1376 | PPCODE: |
1423 | PPCODE: |
1377 | { |
1424 | { |
1378 | if (enable) |
1425 | if (enable) |
1379 | self->flags |= ix; |
1426 | self->flags |= ix; |
… | |
… | |
1447 | EXTEND (SP, 2); |
1494 | EXTEND (SP, 2); |
1448 | PUSHs (sv); |
1495 | PUSHs (sv); |
1449 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1496 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | } |
1497 | } |
1451 | |
1498 | |
1452 | void incr_parse (CBOR *self, SV *cborstr, int chop = 0) |
1499 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1500 | ALIAS: |
|
|
1501 | incr_parse_multiple = 1 |
1453 | PPCODE: |
1502 | PPCODE: |
1454 | { |
1503 | { |
1455 | if (SvUTF8 (cborstr)) |
1504 | if (SvUTF8 (cborstr)) |
1456 | sv_utf8_downgrade (cborstr, 0); |
1505 | sv_utf8_downgrade (cborstr, 0); |
1457 | |
1506 | |
… | |
… | |
1462 | self->incr_need = 1; |
1511 | self->incr_need = 1; |
1463 | |
1512 | |
1464 | av_push (self->incr_count, newSViv (1)); |
1513 | av_push (self->incr_count, newSViv (1)); |
1465 | } |
1514 | } |
1466 | |
1515 | |
1467 | for (;;) |
1516 | do |
1468 | { |
1517 | { |
1469 | if (!incr_parse (self, cborstr)) |
1518 | if (!incr_parse (self, cborstr)) |
1470 | { |
1519 | { |
1471 | if (self->incr_need > self->max_size && self->max_size) |
1520 | 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", |
1521 | 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); |
1522 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
1474 | |
1523 | |
1475 | break; |
1524 | break; |
1476 | } |
1525 | } |
1477 | |
1526 | |
1478 | SV *sv; |
1527 | SV *sv; |
1479 | char *offset; |
1528 | char *offset; |
1480 | |
1529 | |
1481 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
1530 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
1482 | XPUSHs (sv); |
1531 | XPUSHs (sv); |
1483 | |
1532 | |
|
|
1533 | sv_chop (cborstr, offset); |
|
|
1534 | |
1484 | av_clear (self->incr_count); |
1535 | av_clear (self->incr_count); |
1485 | av_push (self->incr_count, newSViv (1)); |
1536 | av_push (self->incr_count, newSViv (1)); |
1486 | |
1537 | |
1487 | if (chop) |
|
|
1488 | { |
|
|
1489 | self->incr_pos = 0; |
1538 | 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; |
1539 | self->incr_need = self->incr_pos + 1; |
1496 | } |
1540 | } |
|
|
1541 | while (ix); |
1497 | } |
1542 | } |
1498 | |
1543 | |
1499 | void incr_reset (CBOR *self) |
1544 | void incr_reset (CBOR *self) |
1500 | CODE: |
1545 | CODE: |
1501 | { |
1546 | { |