… | |
… | |
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 |
… | |
… | |
441 | |
449 | |
442 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
450 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
443 | { |
451 | { |
444 | dSP; |
452 | dSP; |
445 | |
453 | |
446 | ENTER; SAVETMPS; PUSHMARK (SP); |
454 | ENTER; SAVETMPS; |
|
|
455 | PUSHMARK (SP); |
447 | // we re-bless the reference to get overload and other niceties right |
456 | // we re-bless the reference to get overload and other niceties right |
448 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
457 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
449 | |
458 | |
450 | PUTBACK; |
459 | PUTBACK; |
451 | // G_SCALAR ensures that return value is 1 |
460 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
464 | } |
473 | } |
465 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
474 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
466 | { |
475 | { |
467 | dSP; |
476 | dSP; |
468 | |
477 | |
469 | ENTER; SAVETMPS; PUSHMARK (SP); |
478 | ENTER; SAVETMPS; |
|
|
479 | SAVESTACK_POS (); |
|
|
480 | PUSHMARK (SP); |
470 | EXTEND (SP, 2); |
481 | EXTEND (SP, 2); |
471 | // 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 |
472 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
483 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
473 | PUSHs (sv_cbor); |
484 | PUSHs (sv_cbor); |
474 | |
485 | |
… | |
… | |
576 | } |
587 | } |
577 | |
588 | |
578 | static SV * |
589 | static SV * |
579 | encode_cbor (SV *scalar, CBOR *cbor) |
590 | encode_cbor (SV *scalar, CBOR *cbor) |
580 | { |
591 | { |
581 | enc_t enc = { }; |
592 | enc_t enc = { 0 }; |
582 | |
593 | |
583 | enc.cbor = *cbor; |
594 | enc.cbor = *cbor; |
584 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
595 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
585 | enc.cur = SvPVX (enc.sv); |
596 | enc.cur = SvPVX (enc.sv); |
586 | enc.end = SvEND (enc.sv); |
597 | enc.end = SvEND (enc.sv); |
… | |
… | |
736 | { |
747 | { |
737 | // for speed reasons, we specialcase single-string |
748 | // for speed reasons, we specialcase single-string |
738 | // byte or utf-8 strings as keys, but only when !stringref |
749 | // byte or utf-8 strings as keys, but only when !stringref |
739 | |
750 | |
740 | if (ecb_expect_true (!dec->stringref)) |
751 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
752 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | { |
753 | { |
743 | I32 len = decode_uint (dec); |
754 | I32 len = decode_uint (dec); |
744 | char *key = (char *)dec->cur; |
755 | char *key = (char *)dec->cur; |
745 | |
756 | |
|
|
757 | WANT (len); |
746 | dec->cur += len; |
758 | dec->cur += len; |
747 | |
759 | |
748 | hv_store (hv, key, len, decode_sv (dec), 0); |
760 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | |
761 | |
750 | return; |
762 | return; |
751 | } |
763 | } |
752 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
764 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | { |
765 | { |
754 | I32 len = decode_uint (dec); |
766 | I32 len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
767 | char *key = (char *)dec->cur; |
756 | |
768 | |
|
|
769 | WANT (len); |
757 | dec->cur += len; |
770 | dec->cur += len; |
758 | |
771 | |
759 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
772 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
760 | if (!is_utf8_string (key, len)) |
773 | if (!is_utf8_string (key, len)) |
761 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
774 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
984 | if (!method) |
997 | if (!method) |
985 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
998 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
986 | |
999 | |
987 | dSP; |
1000 | dSP; |
988 | |
1001 | |
989 | ENTER; SAVETMPS; PUSHMARK (SP); |
1002 | ENTER; SAVETMPS; |
|
|
1003 | PUSHMARK (SP); |
990 | EXTEND (SP, len + 1); |
1004 | EXTEND (SP, len + 1); |
991 | // we re-bless the reference to get overload and other niceties right |
1005 | // we re-bless the reference to get overload and other niceties right |
992 | PUSHs (*av_fetch (av, 0, 1)); |
1006 | PUSHs (*av_fetch (av, 0, 1)); |
993 | PUSHs (sv_cbor); |
1007 | PUSHs (sv_cbor); |
994 | |
1008 | |
… | |
… | |
1019 | default: |
1033 | default: |
1020 | { |
1034 | { |
1021 | sv = decode_sv (dec); |
1035 | sv = decode_sv (dec); |
1022 | |
1036 | |
1023 | dSP; |
1037 | dSP; |
1024 | ENTER; SAVETMPS; PUSHMARK (SP); |
1038 | ENTER; SAVETMPS; |
|
|
1039 | SAVESTACK_POS (); |
|
|
1040 | PUSHMARK (SP); |
1025 | EXTEND (SP, 2); |
1041 | EXTEND (SP, 2); |
1026 | PUSHs (newSVuv (tag)); |
1042 | PUSHs (newSVuv (tag)); |
1027 | PUSHs (sv); |
1043 | PUSHs (sv); |
1028 | |
1044 | |
1029 | PUTBACK; |
1045 | PUTBACK; |
… | |
… | |
1155 | } |
1171 | } |
1156 | |
1172 | |
1157 | static SV * |
1173 | static SV * |
1158 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1174 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1159 | { |
1175 | { |
1160 | dec_t dec = { }; |
1176 | dec_t dec = { 0 }; |
1161 | SV *sv; |
1177 | SV *sv; |
1162 | STRLEN len; |
1178 | STRLEN len; |
1163 | char *data = SvPVbyte (string, len); |
1179 | char *data = SvPVbyte (string, len); |
1164 | |
1180 | |
1165 | if (len > cbor->max_size && cbor->max_size) |
1181 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1256 | |
1272 | |
1257 | int major = *p >> MAJOR_SHIFT; |
1273 | int major = *p >> MAJOR_SHIFT; |
1258 | |
1274 | |
1259 | switch (major) |
1275 | switch (major) |
1260 | { |
1276 | { |
|
|
1277 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1278 | ++count; // tags merely prefix another value |
|
|
1279 | break; |
|
|
1280 | |
1261 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1281 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1262 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1282 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1263 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1283 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1264 | case MAJOR_MAP >> MAJOR_SHIFT: |
1284 | case MAJOR_MAP >> MAJOR_SHIFT: |
1265 | { |
1285 | { |
… | |
… | |
1448 | PUSHs (sv); |
1468 | PUSHs (sv); |
1449 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1469 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | } |
1470 | } |
1451 | |
1471 | |
1452 | void incr_parse (CBOR *self, SV *cborstr) |
1472 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1473 | ALIAS: |
|
|
1474 | incr_parse_multiple = 1 |
1453 | PPCODE: |
1475 | PPCODE: |
1454 | { |
1476 | { |
1455 | if (SvUTF8 (cborstr)) |
1477 | if (SvUTF8 (cborstr)) |
1456 | sv_utf8_downgrade (cborstr, 0); |
1478 | sv_utf8_downgrade (cborstr, 0); |
1457 | |
1479 | |
… | |
… | |
1487 | av_push (self->incr_count, newSViv (1)); |
1509 | av_push (self->incr_count, newSViv (1)); |
1488 | |
1510 | |
1489 | self->incr_pos = 0; |
1511 | self->incr_pos = 0; |
1490 | self->incr_need = self->incr_pos + 1; |
1512 | self->incr_need = self->incr_pos + 1; |
1491 | } |
1513 | } |
1492 | while (GIMME_V == G_ARRAY); |
1514 | while (ix); |
1493 | } |
1515 | } |
1494 | |
1516 | |
1495 | void incr_reset (CBOR *self) |
1517 | void incr_reset (CBOR *self) |
1496 | CODE: |
1518 | CODE: |
1497 | { |
1519 | { |