… | |
… | |
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 | |
191 | ? idx > 0xffU |
193 | return idx <= 23 ? 3 |
192 | ? idx > 0xffffU |
194 | : idx <= 0xffU ? 4 |
|
|
195 | : idx <= 0xffffU ? 5 |
193 | ? idx > 0xffffffffU |
196 | : idx <= 0xffffffffU ? 7 |
194 | ? 11 |
197 | : 11; |
195 | : 7 |
|
|
196 | : 5 |
|
|
197 | : 4 |
|
|
198 | : 3; |
|
|
199 | } |
198 | } |
200 | |
199 | |
201 | ///////////////////////////////////////////////////////////////////////////// |
200 | ///////////////////////////////////////////////////////////////////////////// |
202 | // encoder |
201 | // encoder |
203 | |
202 | |
… | |
… | |
216 | } enc_t; |
215 | } enc_t; |
217 | |
216 | |
218 | ecb_inline void |
217 | ecb_inline void |
219 | need (enc_t *enc, STRLEN len) |
218 | need (enc_t *enc, STRLEN len) |
220 | { |
219 | { |
221 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
220 | if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
222 | { |
221 | { |
223 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
222 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
224 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
223 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
225 | enc->cur = SvPVX (enc->sv) + cur; |
224 | enc->cur = SvPVX (enc->sv) + cur; |
226 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
225 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
… | |
… | |
477 | if (ecb_expect_false (SvOBJECT (sv))) |
476 | if (ecb_expect_false (SvOBJECT (sv))) |
478 | { |
477 | { |
479 | HV *stash = SvSTASH (sv); |
478 | HV *stash = SvSTASH (sv); |
480 | GV *method; |
479 | GV *method; |
481 | |
480 | |
|
|
481 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
482 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
483 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
484 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
483 | { |
485 | { |
484 | dSP; |
486 | dSP; |
485 | |
487 | |
486 | ENTER; SAVETMPS; |
488 | ENTER; SAVETMPS; |
487 | PUSHMARK (SP); |
489 | PUSHMARK (SP); |
… | |
… | |
704 | |
706 | |
705 | #define ERR_DO(do) SB do; goto fail; SE |
707 | #define ERR_DO(do) SB do; goto fail; SE |
706 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
708 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
707 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
709 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
708 | |
710 | |
709 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) |
711 | #define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec)) |
710 | |
712 | |
711 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
713 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
712 | #define DEC_DEC_DEPTH --dec->depth |
714 | #define DEC_DEC_DEPTH --dec->depth |
713 | |
715 | |
714 | static UV |
716 | static UV |
… | |
… | |
1081 | } |
1083 | } |
1082 | break; |
1084 | break; |
1083 | |
1085 | |
1084 | case CBOR_TAG_PERL_OBJECT: |
1086 | case CBOR_TAG_PERL_OBJECT: |
1085 | { |
1087 | { |
|
|
1088 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1089 | goto filter; |
|
|
1090 | |
1086 | sv = decode_sv (dec); |
1091 | sv = decode_sv (dec); |
1087 | |
1092 | |
1088 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1093 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1089 | ERR ("corrupted CBOR data (non-array perl object)"); |
1094 | ERR ("corrupted CBOR data (non-array perl object)"); |
1090 | |
1095 | |
… | |
… | |
1132 | FREETMPS; LEAVE; |
1137 | FREETMPS; LEAVE; |
1133 | } |
1138 | } |
1134 | break; |
1139 | break; |
1135 | |
1140 | |
1136 | default: |
1141 | default: |
|
|
1142 | filter: |
1137 | { |
1143 | { |
1138 | SV *tag_sv = newSVuv (tag); |
1144 | SV *tag_sv = newSVuv (tag); |
1139 | |
1145 | |
1140 | sv = decode_sv (dec); |
1146 | sv = decode_sv (dec); |
1141 | |
1147 | |
… | |
… | |
1502 | ALIAS: |
1508 | ALIAS: |
1503 | shrink = F_SHRINK |
1509 | shrink = F_SHRINK |
1504 | allow_unknown = F_ALLOW_UNKNOWN |
1510 | allow_unknown = F_ALLOW_UNKNOWN |
1505 | allow_sharing = F_ALLOW_SHARING |
1511 | allow_sharing = F_ALLOW_SHARING |
1506 | allow_cycles = F_ALLOW_CYCLES |
1512 | allow_cycles = F_ALLOW_CYCLES |
|
|
1513 | forbid_objects = F_FORBID_OBJECTS |
1507 | pack_strings = F_PACK_STRINGS |
1514 | pack_strings = F_PACK_STRINGS |
1508 | text_keys = F_TEXT_KEYS |
1515 | text_keys = F_TEXT_KEYS |
1509 | text_strings = F_TEXT_STRINGS |
1516 | text_strings = F_TEXT_STRINGS |
1510 | validate_utf8 = F_VALIDATE_UTF8 |
1517 | validate_utf8 = F_VALIDATE_UTF8 |
1511 | PPCODE: |
1518 | PPCODE: |
… | |
… | |
1522 | ALIAS: |
1529 | ALIAS: |
1523 | get_shrink = F_SHRINK |
1530 | get_shrink = F_SHRINK |
1524 | get_allow_unknown = F_ALLOW_UNKNOWN |
1531 | get_allow_unknown = F_ALLOW_UNKNOWN |
1525 | get_allow_sharing = F_ALLOW_SHARING |
1532 | get_allow_sharing = F_ALLOW_SHARING |
1526 | get_allow_cycles = F_ALLOW_CYCLES |
1533 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1534 | get_forbid_objects = F_FORBID_OBJECTS |
1527 | get_pack_strings = F_PACK_STRINGS |
1535 | get_pack_strings = F_PACK_STRINGS |
1528 | get_text_keys = F_TEXT_KEYS |
1536 | get_text_keys = F_TEXT_KEYS |
1529 | get_text_strings = F_TEXT_STRINGS |
1537 | get_text_strings = F_TEXT_STRINGS |
1530 | get_validate_utf8 = F_VALIDATE_UTF8 |
1538 | get_validate_utf8 = F_VALIDATE_UTF8 |
1531 | PPCODE: |
1539 | PPCODE: |