… | |
… | |
6 | #include <string.h> |
6 | #include <string.h> |
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 | #include <inttypes.h> |
11 | |
12 | |
12 | #define ECB_NO_THREADS 1 |
13 | #define ECB_NO_THREADS 1 |
13 | #include "ecb.h" |
14 | #include "ecb.h" |
14 | |
15 | |
15 | // compatibility with perl <5.18 |
16 | // compatibility with perl <5.18 |
… | |
… | |
99 | |
100 | |
100 | #define F_SHRINK 0x00000001UL |
101 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
105 | #define F_FORBID_OBJECTS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
106 | #define F_PACK_STRINGS 0x00000020UL |
105 | #define F_TEXT_KEYS 0x00000020UL |
107 | #define F_TEXT_KEYS 0x00000040UL |
106 | #define F_TEXT_STRINGS 0x00000040UL |
108 | #define F_TEXT_STRINGS 0x00000080UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
109 | #define F_VALIDATE_UTF8 0x00000100UL |
108 | |
110 | |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
111 | #define INIT_SIZE 32 // initial scalar size to be allocated |
110 | |
112 | |
111 | #define SB do { |
113 | #define SB do { |
112 | #define SE } while (0) |
114 | #define SE } while (0) |
… | |
… | |
185 | |
187 | |
186 | // minimum length of a string to be registered for stringref |
188 | // minimum length of a string to be registered for stringref |
187 | ecb_inline int |
189 | ecb_inline int |
188 | minimum_string_length (UV idx) |
190 | minimum_string_length (UV idx) |
189 | { |
191 | { |
190 | return idx > 23 |
192 | return idx <= 23 ? 3 |
191 | ? idx > 0xffU |
193 | : idx <= 0xffU ? 4 |
192 | ? idx > 0xffffU |
194 | : idx <= 0xffffU ? 5 |
193 | ? idx > 0xffffffffU |
195 | : idx <= 0xffffffffU ? 7 |
194 | ? 11 |
196 | : 11; |
195 | : 7 |
|
|
196 | : 5 |
|
|
197 | : 4 |
|
|
198 | : 3; |
|
|
199 | } |
197 | } |
200 | |
198 | |
201 | ///////////////////////////////////////////////////////////////////////////// |
199 | ///////////////////////////////////////////////////////////////////////////// |
202 | // encoder |
200 | // encoder |
203 | |
201 | |
… | |
… | |
216 | } enc_t; |
214 | } enc_t; |
217 | |
215 | |
218 | ecb_inline void |
216 | ecb_inline void |
219 | need (enc_t *enc, STRLEN len) |
217 | need (enc_t *enc, STRLEN len) |
220 | { |
218 | { |
221 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
219 | if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
222 | { |
220 | { |
223 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
221 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
224 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
222 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
225 | enc->cur = SvPVX (enc->sv) + cur; |
223 | enc->cur = SvPVX (enc->sv) + cur; |
226 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
224 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
… | |
… | |
477 | if (ecb_expect_false (SvOBJECT (sv))) |
475 | if (ecb_expect_false (SvOBJECT (sv))) |
478 | { |
476 | { |
479 | HV *stash = SvSTASH (sv); |
477 | HV *stash = SvSTASH (sv); |
480 | GV *method; |
478 | GV *method; |
481 | |
479 | |
|
|
480 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
481 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
482 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
483 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
483 | { |
484 | { |
484 | dSP; |
485 | dSP; |
485 | |
486 | |
486 | ENTER; SAVETMPS; |
487 | ENTER; SAVETMPS; |
487 | PUSHMARK (SP); |
488 | PUSHMARK (SP); |
… | |
… | |
661 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
662 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
663 | AV *shareable; |
664 | AV *shareable; |
664 | AV *stringref; |
665 | AV *stringref; |
665 | SV *decode_tagged; |
666 | SV *decode_tagged; |
|
|
667 | SV *err_sv; // optional sv for error, needs to be freed |
666 | } dec_t; |
668 | } dec_t; |
667 | |
669 | |
668 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
670 | // set dec->err to ERRSV |
|
|
671 | ecb_cold static void |
|
|
672 | err_errsv (dec_t *dec) |
|
|
673 | { |
|
|
674 | if (!dec->err) |
|
|
675 | { |
|
|
676 | dec->err_sv = newSVsv (ERRSV); |
669 | |
677 | |
|
|
678 | // chop off the trailing \n |
|
|
679 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
680 | *SvEND (dec->err_sv) = 0; |
|
|
681 | |
|
|
682 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
683 | } |
|
|
684 | } |
|
|
685 | |
|
|
686 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
687 | ecb_cold static void |
|
|
688 | err_set (dec_t *dec, const char *reason) |
|
|
689 | { |
|
|
690 | if (!dec->err) |
|
|
691 | dec->err = reason; |
|
|
692 | } |
|
|
693 | |
|
|
694 | ecb_cold static void |
|
|
695 | err_unexpected_end (dec_t *dec) |
|
|
696 | { |
|
|
697 | err_set (dec, "unexpected end of CBOR data"); |
|
|
698 | } |
|
|
699 | |
|
|
700 | ecb_cold static void |
|
|
701 | err_nesting_exceeded (dec_t *dec) |
|
|
702 | { |
|
|
703 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
704 | } |
|
|
705 | |
|
|
706 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
707 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
708 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
709 | |
670 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data") |
710 | #define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec)) |
671 | |
711 | |
672 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
712 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
673 | #define DEC_DEC_DEPTH --dec->depth |
713 | #define DEC_DEC_DEPTH --dec->depth |
674 | |
714 | |
675 | static UV |
715 | static UV |
… | |
… | |
811 | } |
851 | } |
812 | |
852 | |
813 | SV *k = decode_sv (dec); |
853 | SV *k = decode_sv (dec); |
814 | SV *v = decode_sv (dec); |
854 | SV *v = decode_sv (dec); |
815 | |
855 | |
|
|
856 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
857 | // methods, and this is hopefully the only place where it can happen, |
|
|
858 | // so if there is a chance of an exception, take the very slow path. |
|
|
859 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
860 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
861 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
862 | { |
|
|
863 | dSP; |
|
|
864 | |
|
|
865 | ENTER; SAVETMPS; |
|
|
866 | PUSHMARK (SP); |
|
|
867 | EXTEND (SP, 3); |
|
|
868 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
869 | PUSHs (sv_2mortal (k)); |
|
|
870 | PUSHs (sv_2mortal (v)); |
|
|
871 | |
|
|
872 | PUTBACK; |
|
|
873 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
874 | SPAGAIN; |
|
|
875 | |
|
|
876 | FREETMPS; LEAVE; |
|
|
877 | |
|
|
878 | if (SvTRUE (ERRSV)) |
|
|
879 | ERR_ERRSV; |
|
|
880 | |
|
|
881 | return; |
|
|
882 | } |
|
|
883 | |
816 | hv_store_ent (hv, k, v, 0); |
884 | hv_store_ent (hv, k, v, 0); |
817 | SvREFCNT_dec (k); |
885 | SvREFCNT_dec (k); |
818 | |
886 | |
819 | fail: |
887 | fail: |
820 | ; |
888 | ; |
… | |
… | |
1014 | } |
1082 | } |
1015 | break; |
1083 | break; |
1016 | |
1084 | |
1017 | case CBOR_TAG_PERL_OBJECT: |
1085 | case CBOR_TAG_PERL_OBJECT: |
1018 | { |
1086 | { |
|
|
1087 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1088 | goto filter; |
|
|
1089 | |
1019 | sv = decode_sv (dec); |
1090 | sv = decode_sv (dec); |
1020 | |
1091 | |
1021 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1092 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1022 | ERR ("corrupted CBOR data (non-array perl object)"); |
1093 | ERR ("corrupted CBOR data (non-array perl object)"); |
1023 | |
1094 | |
… | |
… | |
1052 | SPAGAIN; |
1123 | SPAGAIN; |
1053 | |
1124 | |
1054 | if (SvTRUE (ERRSV)) |
1125 | if (SvTRUE (ERRSV)) |
1055 | { |
1126 | { |
1056 | FREETMPS; LEAVE; |
1127 | FREETMPS; LEAVE; |
1057 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1128 | ERR_ERRSV; |
1058 | } |
1129 | } |
1059 | |
1130 | |
1060 | SvREFCNT_dec (sv); |
1131 | SvREFCNT_dec (sv); |
1061 | sv = SvREFCNT_inc (POPs); |
1132 | sv = SvREFCNT_inc (POPs); |
1062 | |
1133 | |
… | |
… | |
1065 | FREETMPS; LEAVE; |
1136 | FREETMPS; LEAVE; |
1066 | } |
1137 | } |
1067 | break; |
1138 | break; |
1068 | |
1139 | |
1069 | default: |
1140 | default: |
|
|
1141 | filter: |
1070 | { |
1142 | { |
1071 | SV *tag_sv = newSVuv (tag); |
1143 | SV *tag_sv = newSVuv (tag); |
1072 | |
1144 | |
1073 | sv = decode_sv (dec); |
1145 | sv = decode_sv (dec); |
1074 | |
1146 | |
… | |
… | |
1086 | |
1158 | |
1087 | if (SvTRUE (ERRSV)) |
1159 | if (SvTRUE (ERRSV)) |
1088 | { |
1160 | { |
1089 | SvREFCNT_dec (tag_sv); |
1161 | SvREFCNT_dec (tag_sv); |
1090 | FREETMPS; LEAVE; |
1162 | FREETMPS; LEAVE; |
1091 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1163 | ERR_ERRSV; |
1092 | } |
1164 | } |
1093 | |
1165 | |
1094 | if (count) |
1166 | if (count) |
1095 | { |
1167 | { |
1096 | SvREFCNT_dec (tag_sv); |
1168 | SvREFCNT_dec (tag_sv); |
… | |
… | |
1247 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1319 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1248 | sv_setsv (*svp, &PL_sv_undef); |
1320 | sv_setsv (*svp, &PL_sv_undef); |
1249 | } |
1321 | } |
1250 | |
1322 | |
1251 | SvREFCNT_dec (sv); |
1323 | SvREFCNT_dec (sv); |
|
|
1324 | |
|
|
1325 | if (dec.err_sv) |
|
|
1326 | sv_2mortal (dec.err_sv); |
|
|
1327 | |
1252 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1328 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1253 | } |
1329 | } |
1254 | |
1330 | |
1255 | sv = sv_2mortal (sv); |
1331 | sv = sv_2mortal (sv); |
1256 | |
1332 | |
… | |
… | |
1431 | ALIAS: |
1507 | ALIAS: |
1432 | shrink = F_SHRINK |
1508 | shrink = F_SHRINK |
1433 | allow_unknown = F_ALLOW_UNKNOWN |
1509 | allow_unknown = F_ALLOW_UNKNOWN |
1434 | allow_sharing = F_ALLOW_SHARING |
1510 | allow_sharing = F_ALLOW_SHARING |
1435 | allow_cycles = F_ALLOW_CYCLES |
1511 | allow_cycles = F_ALLOW_CYCLES |
|
|
1512 | forbid_objects = F_FORBID_OBJECTS |
1436 | pack_strings = F_PACK_STRINGS |
1513 | pack_strings = F_PACK_STRINGS |
1437 | text_keys = F_TEXT_KEYS |
1514 | text_keys = F_TEXT_KEYS |
1438 | text_strings = F_TEXT_STRINGS |
1515 | text_strings = F_TEXT_STRINGS |
1439 | validate_utf8 = F_VALIDATE_UTF8 |
1516 | validate_utf8 = F_VALIDATE_UTF8 |
1440 | PPCODE: |
1517 | PPCODE: |
… | |
… | |
1451 | ALIAS: |
1528 | ALIAS: |
1452 | get_shrink = F_SHRINK |
1529 | get_shrink = F_SHRINK |
1453 | get_allow_unknown = F_ALLOW_UNKNOWN |
1530 | get_allow_unknown = F_ALLOW_UNKNOWN |
1454 | get_allow_sharing = F_ALLOW_SHARING |
1531 | get_allow_sharing = F_ALLOW_SHARING |
1455 | get_allow_cycles = F_ALLOW_CYCLES |
1532 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1533 | get_forbid_objects = F_FORBID_OBJECTS |
1456 | get_pack_strings = F_PACK_STRINGS |
1534 | get_pack_strings = F_PACK_STRINGS |
1457 | get_text_keys = F_TEXT_KEYS |
1535 | get_text_keys = F_TEXT_KEYS |
1458 | get_text_strings = F_TEXT_STRINGS |
1536 | get_text_strings = F_TEXT_STRINGS |
1459 | get_validate_utf8 = F_VALIDATE_UTF8 |
1537 | get_validate_utf8 = F_VALIDATE_UTF8 |
1460 | PPCODE: |
1538 | PPCODE: |