… | |
… | |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_UTF8_STRINGS 0x00000020UL |
105 | #define F_VALIDATE_UTF8 0x00000020UL |
106 | #define F_VALIDATE_UTF8 0x00000040UL |
106 | |
107 | |
107 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | |
109 | |
109 | #define SB do { |
110 | #define SB do { |
110 | #define SE } while (0) |
111 | #define SE } while (0) |
… | |
… | |
279 | } |
280 | } |
280 | |
281 | |
281 | ecb_inline void |
282 | ecb_inline void |
282 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | { |
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 | |
284 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
310 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
285 | need (enc, len); |
311 | need (enc, len); |
286 | memcpy (enc->cur, str, len); |
312 | memcpy (enc->cur, str, len); |
287 | enc->cur += len; |
313 | enc->cur += len; |
288 | } |
314 | } |
… | |
… | |
449 | |
475 | |
450 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
476 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
451 | { |
477 | { |
452 | dSP; |
478 | dSP; |
453 | |
479 | |
454 | ENTER; SAVETMPS; PUSHMARK (SP); |
480 | ENTER; SAVETMPS; |
|
|
481 | PUSHMARK (SP); |
455 | // 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 |
456 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
483 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
457 | |
484 | |
458 | PUTBACK; |
485 | PUTBACK; |
459 | // G_SCALAR ensures that return value is 1 |
486 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
472 | } |
499 | } |
473 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
500 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
474 | { |
501 | { |
475 | dSP; |
502 | dSP; |
476 | |
503 | |
477 | ENTER; SAVETMPS; PUSHMARK (SP); |
504 | ENTER; SAVETMPS; |
|
|
505 | SAVESTACK_POS (); |
|
|
506 | PUSHMARK (SP); |
478 | EXTEND (SP, 2); |
507 | EXTEND (SP, 2); |
479 | // 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 |
480 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
509 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
481 | PUSHs (sv_cbor); |
510 | PUSHs (sv_cbor); |
482 | |
511 | |
… | |
… | |
584 | } |
613 | } |
585 | |
614 | |
586 | static SV * |
615 | static SV * |
587 | encode_cbor (SV *scalar, CBOR *cbor) |
616 | encode_cbor (SV *scalar, CBOR *cbor) |
588 | { |
617 | { |
589 | enc_t enc = { }; |
618 | enc_t enc = { 0 }; |
590 | |
619 | |
591 | enc.cbor = *cbor; |
620 | enc.cbor = *cbor; |
592 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
621 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
593 | enc.cur = SvPVX (enc.sv); |
622 | enc.cur = SvPVX (enc.sv); |
594 | enc.end = SvEND (enc.sv); |
623 | enc.end = SvEND (enc.sv); |
… | |
… | |
749 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
778 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
750 | { |
779 | { |
751 | I32 len = decode_uint (dec); |
780 | I32 len = decode_uint (dec); |
752 | char *key = (char *)dec->cur; |
781 | char *key = (char *)dec->cur; |
753 | |
782 | |
|
|
783 | WANT (len); |
754 | dec->cur += len; |
784 | dec->cur += len; |
755 | |
785 | |
756 | hv_store (hv, key, len, decode_sv (dec), 0); |
786 | hv_store (hv, key, len, decode_sv (dec), 0); |
757 | |
787 | |
758 | return; |
788 | return; |
759 | } |
789 | } |
760 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
790 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
761 | { |
791 | { |
762 | I32 len = decode_uint (dec); |
792 | I32 len = decode_uint (dec); |
763 | char *key = (char *)dec->cur; |
793 | char *key = (char *)dec->cur; |
764 | |
794 | |
|
|
795 | WANT (len); |
765 | dec->cur += len; |
796 | dec->cur += len; |
766 | |
797 | |
767 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
798 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
768 | if (!is_utf8_string (key, len)) |
799 | if (!is_utf8_string (key, len)) |
769 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
800 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
992 | if (!method) |
1023 | if (!method) |
993 | 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)"); |
994 | |
1025 | |
995 | dSP; |
1026 | dSP; |
996 | |
1027 | |
997 | ENTER; SAVETMPS; PUSHMARK (SP); |
1028 | ENTER; SAVETMPS; |
|
|
1029 | PUSHMARK (SP); |
998 | EXTEND (SP, len + 1); |
1030 | EXTEND (SP, len + 1); |
999 | // 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 |
1000 | PUSHs (*av_fetch (av, 0, 1)); |
1032 | PUSHs (*av_fetch (av, 0, 1)); |
1001 | PUSHs (sv_cbor); |
1033 | PUSHs (sv_cbor); |
1002 | |
1034 | |
… | |
… | |
1027 | default: |
1059 | default: |
1028 | { |
1060 | { |
1029 | sv = decode_sv (dec); |
1061 | sv = decode_sv (dec); |
1030 | |
1062 | |
1031 | dSP; |
1063 | dSP; |
1032 | ENTER; SAVETMPS; PUSHMARK (SP); |
1064 | ENTER; SAVETMPS; |
|
|
1065 | SAVESTACK_POS (); |
|
|
1066 | PUSHMARK (SP); |
1033 | EXTEND (SP, 2); |
1067 | EXTEND (SP, 2); |
1034 | PUSHs (newSVuv (tag)); |
1068 | PUSHs (newSVuv (tag)); |
1035 | PUSHs (sv); |
1069 | PUSHs (sv); |
1036 | |
1070 | |
1037 | PUTBACK; |
1071 | PUTBACK; |
… | |
… | |
1163 | } |
1197 | } |
1164 | |
1198 | |
1165 | static SV * |
1199 | static SV * |
1166 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1200 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1167 | { |
1201 | { |
1168 | dec_t dec = { }; |
1202 | dec_t dec = { 0 }; |
1169 | SV *sv; |
1203 | SV *sv; |
1170 | STRLEN len; |
1204 | STRLEN len; |
1171 | char *data = SvPVbyte (string, len); |
1205 | char *data = SvPVbyte (string, len); |
1172 | |
1206 | |
1173 | if (len > cbor->max_size && cbor->max_size) |
1207 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1382 | shrink = F_SHRINK |
1416 | shrink = F_SHRINK |
1383 | allow_unknown = F_ALLOW_UNKNOWN |
1417 | allow_unknown = F_ALLOW_UNKNOWN |
1384 | allow_sharing = F_ALLOW_SHARING |
1418 | allow_sharing = F_ALLOW_SHARING |
1385 | allow_cycles = F_ALLOW_CYCLES |
1419 | allow_cycles = F_ALLOW_CYCLES |
1386 | pack_strings = F_PACK_STRINGS |
1420 | pack_strings = F_PACK_STRINGS |
|
|
1421 | utf8_strings = F_UTF8_STRINGS |
1387 | validate_utf8 = F_VALIDATE_UTF8 |
1422 | validate_utf8 = F_VALIDATE_UTF8 |
1388 | PPCODE: |
1423 | PPCODE: |
1389 | { |
1424 | { |
1390 | if (enable) |
1425 | if (enable) |
1391 | self->flags |= ix; |
1426 | self->flags |= ix; |