… | |
… | |
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)) |
… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
21 | #endif |
21 | #ifndef HvNAMEUTF8 |
22 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
24 | #endif |
|
|
25 | #ifndef SvREFCNT_dec_NN |
|
|
26 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
27 | #endif |
|
|
28 | |
|
|
29 | // known major and minor types |
|
|
30 | enum cbor_type |
|
|
31 | { |
|
|
32 | MAJOR_SHIFT = 5, |
|
|
33 | MINOR_MASK = 0x1f, |
|
|
34 | |
|
|
35 | MAJOR_POS_INT = 0 << MAJOR_SHIFT, |
|
|
36 | MAJOR_NEG_INT = 1 << MAJOR_SHIFT, |
|
|
37 | MAJOR_BYTES = 2 << MAJOR_SHIFT, |
|
|
38 | MAJOR_TEXT = 3 << MAJOR_SHIFT, |
|
|
39 | MAJOR_ARRAY = 4 << MAJOR_SHIFT, |
|
|
40 | MAJOR_MAP = 5 << MAJOR_SHIFT, |
|
|
41 | MAJOR_TAG = 6 << MAJOR_SHIFT, |
|
|
42 | MAJOR_MISC = 7 << MAJOR_SHIFT, |
|
|
43 | |
|
|
44 | // INT/STRING/ARRAY/MAP subtypes |
|
|
45 | LENGTH_EXT1 = 24, |
|
|
46 | LENGTH_EXT2 = 25, |
|
|
47 | LENGTH_EXT4 = 26, |
|
|
48 | LENGTH_EXT8 = 27, |
|
|
49 | |
|
|
50 | // SIMPLE types (effectively MISC subtypes) |
|
|
51 | SIMPLE_FALSE = 20, |
|
|
52 | SIMPLE_TRUE = 21, |
|
|
53 | SIMPLE_NULL = 22, |
|
|
54 | SIMPLE_UNDEF = 23, |
|
|
55 | |
|
|
56 | // MISC subtype (unused) |
|
|
57 | MISC_EXT1 = 24, |
|
|
58 | MISC_FLOAT16 = 25, |
|
|
59 | MISC_FLOAT32 = 26, |
|
|
60 | MISC_FLOAT64 = 27, |
|
|
61 | |
|
|
62 | // BYTES/TEXT/ARRAY/MAP |
|
|
63 | MINOR_INDEF = 31, |
|
|
64 | }; |
24 | |
65 | |
25 | // known tags |
66 | // known tags |
26 | enum cbor_tag |
67 | enum cbor_tag |
27 | { |
68 | { |
28 | // inofficial extensions (pending iana registration) |
69 | // extensions |
|
|
70 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
71 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
72 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing |
73 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
74 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
75 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
76 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
77 | |
37 | // rfc7049 |
78 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
79 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
80 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
81 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
41 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
82 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
42 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
83 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
43 | CBOR_TAG_BIGFLOAT = 5, // array |
84 | CBOR_TAG_BIGFLOAT = 5, // array |
44 | |
85 | |
45 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
86 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
46 | CBOR_TAG_CONV_B64 = 22, // base64, any |
87 | CBOR_TAG_CONV_B64 = 22, // base64, any |
47 | CBOR_TAG_CONV_HEX = 23, // base16, any |
88 | CBOR_TAG_CONV_HEX = 23, // base16, any |
48 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
89 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
49 | |
90 | |
50 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
91 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
51 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
92 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
52 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
93 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
53 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
94 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
95 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
96 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
97 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
57 | }; |
98 | }; |
58 | |
99 | |
59 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
102 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
103 | #define F_ALLOW_CYCLES 0x00000008UL |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
104 | #define F_PACK_STRINGS 0x00000010UL |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
105 | #define F_TEXT_KEYS 0x00000020UL |
|
|
106 | #define F_TEXT_STRINGS 0x00000040UL |
|
|
107 | #define F_VALIDATE_UTF8 0x00000080UL |
64 | |
108 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
110 | |
67 | #define SB do { |
111 | #define SB do { |
68 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
124 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
125 | # define CBOR_STASH cbor_stash |
82 | #endif |
126 | #endif |
83 | |
127 | |
84 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
128 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
85 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
129 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
130 | |
87 | typedef struct { |
131 | typedef struct { |
88 | U32 flags; |
132 | U32 flags; |
89 | U32 max_depth; |
133 | U32 max_depth; |
90 | STRLEN max_size; |
134 | STRLEN max_size; |
|
|
135 | SV *filter; |
|
|
136 | |
|
|
137 | // for the incremental parser |
|
|
138 | STRLEN incr_pos; // the current offset into the text |
|
|
139 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
140 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
91 | } CBOR; |
141 | } CBOR; |
92 | |
142 | |
93 | ecb_inline void |
143 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
144 | cbor_init (CBOR *cbor) |
95 | { |
145 | { |
96 | Zero (cbor, 1, CBOR); |
146 | Zero (cbor, 1, CBOR); |
97 | cbor->max_depth = 512; |
147 | cbor->max_depth = 512; |
|
|
148 | } |
|
|
149 | |
|
|
150 | ecb_inline void |
|
|
151 | cbor_free (CBOR *cbor) |
|
|
152 | { |
|
|
153 | SvREFCNT_dec (cbor->filter); |
|
|
154 | SvREFCNT_dec (cbor->incr_count); |
98 | } |
155 | } |
99 | |
156 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
157 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
158 | // utility functions |
102 | |
159 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
181 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
182 | #endif |
126 | } |
183 | } |
127 | } |
184 | } |
128 | |
185 | |
|
|
186 | // minimum length of a string to be registered for stringref |
|
|
187 | ecb_inline int |
|
|
188 | minimum_string_length (UV idx) |
|
|
189 | { |
|
|
190 | return idx > 23 |
|
|
191 | ? idx > 0xffU |
|
|
192 | ? idx > 0xffffU |
|
|
193 | ? idx > 0xffffffffU |
|
|
194 | ? 11 |
|
|
195 | : 7 |
|
|
196 | : 5 |
|
|
197 | : 4 |
|
|
198 | : 3; |
|
|
199 | } |
|
|
200 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
201 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
202 | // encoder |
131 | |
203 | |
132 | // structure used for encoding CBOR |
204 | // structure used for encoding CBOR |
133 | typedef struct |
205 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
207 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
208 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
209 | SV *sv; // result scalar |
138 | CBOR cbor; |
210 | CBOR cbor; |
139 | U32 depth; // recursion level |
211 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
212 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
213 | UV stringref_idx; |
141 | HV *sharable; // ptr => index, or 0 |
214 | HV *shareable; // ptr => index, or 0 |
142 | HV *sharable_idx; |
215 | UV shareable_idx; |
143 | } enc_t; |
216 | } enc_t; |
144 | |
217 | |
145 | ecb_inline void |
218 | ecb_inline void |
146 | need (enc_t *enc, STRLEN len) |
219 | need (enc_t *enc, STRLEN len) |
147 | { |
220 | { |
… | |
… | |
164 | static void |
237 | static void |
165 | encode_uint (enc_t *enc, int major, UV len) |
238 | encode_uint (enc_t *enc, int major, UV len) |
166 | { |
239 | { |
167 | need (enc, 9); |
240 | need (enc, 9); |
168 | |
241 | |
169 | if (len < 24) |
242 | if (ecb_expect_true (len < LENGTH_EXT1)) |
170 | *enc->cur++ = major | len; |
243 | *enc->cur++ = major | len; |
171 | else if (len <= 0xff) |
244 | else if (ecb_expect_true (len <= 0xffU)) |
172 | { |
245 | { |
173 | *enc->cur++ = major | 24; |
246 | *enc->cur++ = major | LENGTH_EXT1; |
174 | *enc->cur++ = len; |
247 | *enc->cur++ = len; |
175 | } |
248 | } |
176 | else if (len <= 0xffff) |
249 | else if (len <= 0xffffU) |
177 | { |
250 | { |
178 | *enc->cur++ = major | 25; |
251 | *enc->cur++ = major | LENGTH_EXT2; |
179 | *enc->cur++ = len >> 8; |
252 | *enc->cur++ = len >> 8; |
180 | *enc->cur++ = len; |
253 | *enc->cur++ = len; |
181 | } |
254 | } |
182 | else if (len <= 0xffffffff) |
255 | else if (len <= 0xffffffffU) |
183 | { |
256 | { |
184 | *enc->cur++ = major | 26; |
257 | *enc->cur++ = major | LENGTH_EXT4; |
185 | *enc->cur++ = len >> 24; |
258 | *enc->cur++ = len >> 24; |
186 | *enc->cur++ = len >> 16; |
259 | *enc->cur++ = len >> 16; |
187 | *enc->cur++ = len >> 8; |
260 | *enc->cur++ = len >> 8; |
188 | *enc->cur++ = len; |
261 | *enc->cur++ = len; |
189 | } |
262 | } |
190 | else |
263 | else |
191 | { |
264 | { |
192 | *enc->cur++ = major | 27; |
265 | *enc->cur++ = major | LENGTH_EXT8; |
193 | *enc->cur++ = len >> 56; |
266 | *enc->cur++ = len >> 56; |
194 | *enc->cur++ = len >> 48; |
267 | *enc->cur++ = len >> 48; |
195 | *enc->cur++ = len >> 40; |
268 | *enc->cur++ = len >> 40; |
196 | *enc->cur++ = len >> 32; |
269 | *enc->cur++ = len >> 32; |
197 | *enc->cur++ = len >> 24; |
270 | *enc->cur++ = len >> 24; |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
272 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
273 | *enc->cur++ = len; |
201 | } |
274 | } |
202 | } |
275 | } |
203 | |
276 | |
204 | static void |
277 | ecb_inline void |
|
|
278 | encode_tag (enc_t *enc, UV tag) |
|
|
279 | { |
|
|
280 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
281 | } |
|
|
282 | |
|
|
283 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
284 | ecb_noinline static void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
285 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
286 | { |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
287 | STRLEN ulen = len; |
|
|
288 | U8 *p, *pend = (U8 *)str + len; |
|
|
289 | |
|
|
290 | for (p = (U8 *)str; p < pend; ++p) |
|
|
291 | ulen += *p >> 7; // count set high bits |
|
|
292 | |
|
|
293 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
294 | |
|
|
295 | need (enc, ulen); |
|
|
296 | for (p = (U8 *)str; p < pend; ++p) |
|
|
297 | if (*p < 0x80) |
|
|
298 | *enc->cur++ = *p; |
|
|
299 | else |
|
|
300 | { |
|
|
301 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
302 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
|
|
306 | ecb_inline void |
|
|
307 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
|
|
308 | { |
|
|
309 | if (ecb_expect_false (upgrade_utf8)) |
|
|
310 | if (!utf8) |
|
|
311 | { |
|
|
312 | encode_str_utf8 (enc, utf8, str, len); |
|
|
313 | return; |
|
|
314 | } |
|
|
315 | |
|
|
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
208 | need (enc, len); |
317 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
319 | enc->cur += len; |
211 | } |
320 | } |
212 | |
321 | |
213 | ecb_inline void |
322 | ecb_inline void |
214 | encode_tag (enc_t *enc, UV tag) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
215 | { |
324 | { |
216 | encode_uint (enc, 0xc0, tag); |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
217 | } |
326 | { |
|
|
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
218 | |
328 | |
219 | static int |
|
|
220 | encode_sharable2 (enc_t *enc, SV *sv) |
|
|
221 | { |
|
|
222 | if (!enc->sharable) |
|
|
223 | enc->sharable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
224 | |
|
|
225 | SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1); |
|
|
226 | |
|
|
227 | if (SvOK (*svp)) |
329 | if (SvOK (*svp)) |
228 | { |
330 | { |
|
|
331 | // already registered, use stringref |
229 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
332 | encode_tag (enc, CBOR_TAG_STRINGREF); |
230 | encode_uint (enc, 0x00, SvUV (*svp)); |
333 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
231 | |
|
|
232 | return 1; |
334 | return; |
233 | } |
335 | } |
234 | else |
336 | else if (len >= minimum_string_length (enc->stringref_idx)) |
235 | { |
337 | { |
|
|
338 | // register only |
236 | sv_setuv (*svp, enc->sharable_idx++); |
339 | sv_setuv (*svp, enc->stringref_idx); |
237 | encode_tag (enc, CBOR_TAG_VALUE_SHARABLE); |
340 | ++enc->stringref_idx; |
238 | |
341 | } |
239 | return 0; |
|
|
240 | } |
342 | } |
241 | } |
|
|
242 | |
343 | |
243 | ecb_inline int |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
244 | encode_sharable (enc_t *enc, SV *sv) |
|
|
245 | { |
|
|
246 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
247 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
248 | return encode_sharable2 (enc, sv); |
|
|
249 | |
|
|
250 | return 0; |
|
|
251 | } |
345 | } |
252 | |
346 | |
253 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
348 | |
255 | static void |
349 | static void |
… | |
… | |
260 | if (enc->depth >= enc->cbor.max_depth) |
354 | if (enc->depth >= enc->cbor.max_depth) |
261 | croak (ERR_NESTING_EXCEEDED); |
355 | croak (ERR_NESTING_EXCEEDED); |
262 | |
356 | |
263 | ++enc->depth; |
357 | ++enc->depth; |
264 | |
358 | |
265 | encode_uint (enc, 0x80, len + 1); |
359 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
266 | |
360 | |
|
|
361 | if (SvMAGICAL (av)) |
267 | for (i = 0; i <= len; ++i) |
362 | for (i = 0; i <= len; ++i) |
268 | { |
363 | { |
269 | SV **svp = av_fetch (av, i, 0); |
364 | SV **svp = av_fetch (av, i, 0); |
270 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
365 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
271 | } |
366 | } |
|
|
367 | else |
|
|
368 | for (i = 0; i <= len; ++i) |
|
|
369 | { |
|
|
370 | SV *sv = AvARRAY (av)[i]; |
|
|
371 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
372 | } |
272 | |
373 | |
273 | --enc->depth; |
374 | --enc->depth; |
274 | } |
375 | } |
275 | |
376 | |
276 | static void |
377 | static void |
… | |
… | |
285 | |
386 | |
286 | int pairs = hv_iterinit (hv); |
387 | int pairs = hv_iterinit (hv); |
287 | int mg = SvMAGICAL (hv); |
388 | int mg = SvMAGICAL (hv); |
288 | |
389 | |
289 | if (mg) |
390 | if (mg) |
290 | encode_ch (enc, 0xa0 | 31); |
391 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
291 | else |
392 | else |
292 | encode_uint (enc, 0xa0, pairs); |
393 | encode_uint (enc, MAJOR_MAP, pairs); |
293 | |
394 | |
294 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
295 | { |
396 | { |
296 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
297 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
298 | else |
399 | else |
299 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
400 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
300 | |
401 | |
301 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
302 | } |
403 | } |
303 | |
404 | |
304 | if (mg) |
405 | if (mg) |
305 | encode_ch (enc, 0xe0 | 31); |
406 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
306 | |
407 | |
307 | --enc->depth; |
408 | --enc->depth; |
308 | } |
409 | } |
309 | |
410 | |
310 | // encode objects, arrays and special \0=false and \1=true values. |
411 | // encode objects, arrays and special \0=false and \1=true values. |
311 | static void |
412 | static void |
312 | encode_rv (enc_t *enc, SV *sv) |
413 | encode_rv (enc_t *enc, SV *sv) |
313 | { |
414 | { |
314 | svtype svt; |
|
|
315 | |
|
|
316 | SvGETMAGIC (sv); |
415 | SvGETMAGIC (sv); |
|
|
416 | |
317 | svt = SvTYPE (sv); |
417 | svtype svt = SvTYPE (sv); |
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
|
|
321 | |
418 | |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
419 | if (ecb_expect_false (SvOBJECT (sv))) |
323 | { |
420 | { |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
421 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
325 | ? types_boolean_stash |
422 | ? types_boolean_stash |
… | |
… | |
330 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
427 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
331 | ? cbor_tagged_stash |
428 | ? cbor_tagged_stash |
332 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
429 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
333 | |
430 | |
334 | HV *stash = SvSTASH (sv); |
431 | HV *stash = SvSTASH (sv); |
335 | GV *method; |
|
|
336 | |
432 | |
337 | if (stash == boolean_stash) |
433 | if (stash == boolean_stash) |
338 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
434 | { |
|
|
435 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
436 | return; |
|
|
437 | } |
339 | else if (stash == error_stash) |
438 | else if (stash == error_stash) |
340 | encode_ch (enc, 0xe0 | 23); |
439 | { |
|
|
440 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
441 | return; |
|
|
442 | } |
341 | else if (stash == tagged_stash) |
443 | else if (stash == tagged_stash) |
342 | { |
444 | { |
343 | if (svt != SVt_PVAV) |
445 | if (svt != SVt_PVAV) |
344 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
446 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
345 | |
447 | |
346 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
448 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
347 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
449 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
450 | |
|
|
451 | return; |
|
|
452 | } |
|
|
453 | } |
|
|
454 | |
|
|
455 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
456 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
457 | { |
|
|
458 | if (!enc->shareable) |
|
|
459 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
460 | |
|
|
461 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
462 | |
|
|
463 | if (SvOK (*svp)) |
348 | } |
464 | { |
|
|
465 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
466 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
467 | return; |
|
|
468 | } |
|
|
469 | else |
|
|
470 | { |
|
|
471 | sv_setuv (*svp, enc->shareable_idx); |
|
|
472 | ++enc->shareable_idx; |
|
|
473 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
474 | } |
|
|
475 | } |
|
|
476 | |
|
|
477 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
478 | { |
|
|
479 | HV *stash = SvSTASH (sv); |
|
|
480 | GV *method; |
|
|
481 | |
349 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
350 | { |
483 | { |
351 | dSP; |
484 | dSP; |
352 | |
485 | |
353 | ENTER; SAVETMPS; PUSHMARK (SP); |
486 | ENTER; SAVETMPS; |
|
|
487 | PUSHMARK (SP); |
354 | // we re-bless the reference to get overload and other niceties right |
488 | // we re-bless the reference to get overload and other niceties right |
355 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
489 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
356 | |
490 | |
357 | PUTBACK; |
491 | PUTBACK; |
358 | // G_SCALAR ensures that return value is 1 |
492 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
371 | } |
505 | } |
372 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
506 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
373 | { |
507 | { |
374 | dSP; |
508 | dSP; |
375 | |
509 | |
376 | ENTER; SAVETMPS; PUSHMARK (SP); |
510 | ENTER; SAVETMPS; |
|
|
511 | SAVESTACK_POS (); |
|
|
512 | PUSHMARK (SP); |
377 | EXTEND (SP, 2); |
513 | EXTEND (SP, 2); |
378 | // we re-bless the reference to get overload and other niceties right |
514 | // we re-bless the reference to get overload and other niceties right |
379 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
515 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
380 | PUSHs (sv_cbor); |
516 | PUSHs (sv_cbor); |
381 | |
517 | |
… | |
… | |
386 | // catch this surprisingly common error |
522 | // catch this surprisingly common error |
387 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
388 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
524 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
389 | |
525 | |
390 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
391 | encode_uint (enc, 0x80, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
392 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
393 | |
529 | |
394 | while (count) |
530 | while (count) |
395 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
396 | |
532 | |
397 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
418 | { |
554 | { |
419 | double nv = SvNVX (sv); |
555 | double nv = SvNVX (sv); |
420 | |
556 | |
421 | need (enc, 9); |
557 | need (enc, 9); |
422 | |
558 | |
423 | if (ecb_expect_false (nv == (U32)nv)) |
559 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
424 | encode_uint (enc, 0x00, (U32)nv); |
560 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
425 | //TODO: maybe I32? |
561 | //TODO: maybe I32? |
426 | else if (ecb_expect_false (nv == (float)nv)) |
562 | else if (ecb_expect_false (nv == (float)nv)) |
427 | { |
563 | { |
428 | uint32_t fp = ecb_float_to_binary32 (nv); |
564 | uint32_t fp = ecb_float_to_binary32 (nv); |
429 | |
565 | |
430 | *enc->cur++ = 0xe0 | 26; |
566 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
431 | |
567 | |
432 | if (!ecb_big_endian ()) |
568 | if (!ecb_big_endian ()) |
433 | fp = ecb_bswap32 (fp); |
569 | fp = ecb_bswap32 (fp); |
434 | |
570 | |
435 | memcpy (enc->cur, &fp, 4); |
571 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
437 | } |
573 | } |
438 | else |
574 | else |
439 | { |
575 | { |
440 | uint64_t fp = ecb_double_to_binary64 (nv); |
576 | uint64_t fp = ecb_double_to_binary64 (nv); |
441 | |
577 | |
442 | *enc->cur++ = 0xe0 | 27; |
578 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
443 | |
579 | |
444 | if (!ecb_big_endian ()) |
580 | if (!ecb_big_endian ()) |
445 | fp = ecb_bswap64 (fp); |
581 | fp = ecb_bswap64 (fp); |
446 | |
582 | |
447 | memcpy (enc->cur, &fp, 8); |
583 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
452 | static void |
588 | static void |
453 | encode_sv (enc_t *enc, SV *sv) |
589 | encode_sv (enc_t *enc, SV *sv) |
454 | { |
590 | { |
455 | SvGETMAGIC (sv); |
591 | SvGETMAGIC (sv); |
456 | |
592 | |
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
|
|
459 | |
|
|
460 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
461 | { |
594 | { |
462 | STRLEN len; |
595 | STRLEN len; |
463 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
464 | encode_str (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
465 | } |
598 | } |
466 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
467 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
468 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
469 | { |
602 | { |
470 | if (SvIsUV (sv)) |
603 | if (SvIsUV (sv)) |
471 | encode_uint (enc, 0x00, SvUVX (sv)); |
604 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
472 | else if (SvIVX (sv) >= 0) |
605 | else if (SvIVX (sv) >= 0) |
473 | encode_uint (enc, 0x00, SvIVX (sv)); |
606 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
474 | else |
607 | else |
475 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
608 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
476 | } |
609 | } |
477 | else if (SvROK (sv)) |
610 | else if (SvROK (sv)) |
478 | encode_rv (enc, SvRV (sv)); |
611 | encode_rv (enc, SvRV (sv)); |
479 | else if (!SvOK (sv)) |
612 | else if (!SvOK (sv)) |
480 | encode_ch (enc, 0xe0 | 22); |
613 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
481 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
614 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
482 | encode_ch (enc, 0xe0 | 23); |
615 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
483 | else |
616 | else |
484 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
617 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
485 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
618 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
486 | } |
619 | } |
487 | |
620 | |
488 | static SV * |
621 | static SV * |
489 | encode_cbor (SV *scalar, CBOR *cbor) |
622 | encode_cbor (SV *scalar, CBOR *cbor) |
490 | { |
623 | { |
491 | enc_t enc = { }; |
624 | enc_t enc = { 0 }; |
492 | |
625 | |
493 | enc.cbor = *cbor; |
626 | enc.cbor = *cbor; |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
495 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
497 | |
630 | |
498 | SvPOK_only (enc.sv); |
631 | SvPOK_only (enc.sv); |
|
|
632 | |
|
|
633 | if (cbor->flags & F_PACK_STRINGS) |
|
|
634 | { |
|
|
635 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
636 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
637 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
638 | } |
|
|
639 | |
499 | encode_sv (&enc, scalar); |
640 | encode_sv (&enc, scalar); |
500 | |
641 | |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
642 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
502 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
643 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
503 | |
644 | |
… | |
… | |
517 | U8 *end; // end of input string |
658 | U8 *end; // end of input string |
518 | const char *err; // parse error, if != 0 |
659 | const char *err; // parse error, if != 0 |
519 | CBOR cbor; |
660 | CBOR cbor; |
520 | U32 depth; // recursion depth |
661 | U32 depth; // recursion depth |
521 | U32 maxdepth; // recursion depth limit |
662 | U32 maxdepth; // recursion depth limit |
522 | AV *sharable; |
663 | AV *shareable; |
|
|
664 | AV *stringref; |
|
|
665 | SV *decode_tagged; |
523 | } dec_t; |
666 | } dec_t; |
524 | |
667 | |
525 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
668 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
526 | |
669 | |
527 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
670 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data") |
528 | |
671 | |
529 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
672 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
530 | #define DEC_DEC_DEPTH --dec->depth |
673 | #define DEC_DEC_DEPTH --dec->depth |
531 | |
674 | |
532 | static UV |
675 | static UV |
533 | decode_uint (dec_t *dec) |
676 | decode_uint (dec_t *dec) |
534 | { |
677 | { |
535 | switch (*dec->cur & 31) |
678 | U8 m = *dec->cur & MINOR_MASK; |
536 | { |
679 | ++dec->cur; |
537 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
538 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
539 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
540 | return *dec->cur++ & 31; |
|
|
541 | |
680 | |
542 | case 24: |
681 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
682 | return m; |
|
|
683 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
684 | { |
543 | WANT (2); |
685 | WANT (1); |
544 | dec->cur += 2; |
686 | dec->cur += 1; |
545 | return dec->cur[-1]; |
687 | return dec->cur[-1]; |
546 | |
688 | } |
547 | case 25: |
689 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
690 | { |
548 | WANT (3); |
691 | WANT (2); |
549 | dec->cur += 3; |
692 | dec->cur += 2; |
550 | return (((UV)dec->cur[-2]) << 8) |
693 | return (((UV)dec->cur[-2]) << 8) |
551 | | ((UV)dec->cur[-1]); |
694 | | ((UV)dec->cur[-1]); |
552 | |
695 | } |
553 | case 26: |
696 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
697 | { |
554 | WANT (5); |
698 | WANT (4); |
555 | dec->cur += 5; |
699 | dec->cur += 4; |
556 | return (((UV)dec->cur[-4]) << 24) |
700 | return (((UV)dec->cur[-4]) << 24) |
557 | | (((UV)dec->cur[-3]) << 16) |
701 | | (((UV)dec->cur[-3]) << 16) |
558 | | (((UV)dec->cur[-2]) << 8) |
702 | | (((UV)dec->cur[-2]) << 8) |
559 | | ((UV)dec->cur[-1]); |
703 | | ((UV)dec->cur[-1]); |
560 | |
704 | } |
561 | case 27: |
705 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
706 | { |
562 | WANT (9); |
707 | WANT (8); |
563 | dec->cur += 9; |
708 | dec->cur += 8; |
|
|
709 | |
|
|
710 | return |
|
|
711 | #if UVSIZE < 8 |
|
|
712 | 0 |
|
|
713 | #else |
564 | return (((UV)dec->cur[-8]) << 56) |
714 | (((UV)dec->cur[-8]) << 56) |
565 | | (((UV)dec->cur[-7]) << 48) |
715 | | (((UV)dec->cur[-7]) << 48) |
566 | | (((UV)dec->cur[-6]) << 40) |
716 | | (((UV)dec->cur[-6]) << 40) |
567 | | (((UV)dec->cur[-5]) << 32) |
717 | | (((UV)dec->cur[-5]) << 32) |
|
|
718 | #endif |
568 | | (((UV)dec->cur[-4]) << 24) |
719 | | (((UV)dec->cur[-4]) << 24) |
569 | | (((UV)dec->cur[-3]) << 16) |
720 | | (((UV)dec->cur[-3]) << 16) |
570 | | (((UV)dec->cur[-2]) << 8) |
721 | | (((UV)dec->cur[-2]) << 8) |
571 | | ((UV)dec->cur[-1]); |
722 | | ((UV)dec->cur[-1]); |
572 | |
723 | } |
573 | default: |
724 | else |
574 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
725 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
575 | } |
|
|
576 | |
726 | |
577 | fail: |
727 | fail: |
578 | return 0; |
728 | return 0; |
579 | } |
729 | } |
580 | |
730 | |
… | |
… | |
585 | { |
735 | { |
586 | AV *av = newAV (); |
736 | AV *av = newAV (); |
587 | |
737 | |
588 | DEC_INC_DEPTH; |
738 | DEC_INC_DEPTH; |
589 | |
739 | |
590 | if ((*dec->cur & 31) == 31) |
740 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
591 | { |
741 | { |
592 | ++dec->cur; |
742 | ++dec->cur; |
593 | |
743 | |
594 | for (;;) |
744 | for (;;) |
595 | { |
745 | { |
596 | WANT (1); |
746 | WANT (1); |
597 | |
747 | |
598 | if (*dec->cur == (0xe0 | 31)) |
748 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
599 | { |
749 | { |
600 | ++dec->cur; |
750 | ++dec->cur; |
601 | break; |
751 | break; |
602 | } |
752 | } |
603 | |
753 | |
604 | av_push (av, decode_sv (dec)); |
754 | av_push (av, decode_sv (dec)); |
605 | } |
755 | } |
606 | } |
756 | } |
607 | else |
757 | else |
608 | { |
758 | { |
609 | int i, len = decode_uint (dec); |
759 | UV i, len = decode_uint (dec); |
610 | |
760 | |
|
|
761 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
611 | av_fill (av, len - 1); |
762 | av_fill (av, len - 1); |
612 | |
763 | |
613 | for (i = 0; i < len; ++i) |
764 | for (i = 0; i < len; ++i) |
614 | AvARRAY (av)[i] = decode_sv (dec); |
765 | AvARRAY (av)[i] = decode_sv (dec); |
615 | } |
766 | } |
… | |
… | |
625 | |
776 | |
626 | static void |
777 | static void |
627 | decode_he (dec_t *dec, HV *hv) |
778 | decode_he (dec_t *dec, HV *hv) |
628 | { |
779 | { |
629 | // for speed reasons, we specialcase single-string |
780 | // for speed reasons, we specialcase single-string |
630 | // byte or utf-8 strings as keys. |
781 | // byte or utf-8 strings as keys, but only when !stringref |
631 | |
782 | |
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
783 | if (ecb_expect_true (!dec->stringref)) |
|
|
784 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
633 | { |
785 | { |
634 | I32 len = decode_uint (dec); |
786 | STRLEN len = decode_uint (dec); |
635 | char *key = (char *)dec->cur; |
787 | char *key = (char *)dec->cur; |
636 | |
788 | |
|
|
789 | WANT (len); |
637 | dec->cur += len; |
790 | dec->cur += len; |
638 | |
791 | |
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
792 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
793 | |
|
|
794 | return; |
640 | } |
795 | } |
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
796 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
642 | { |
797 | { |
643 | I32 len = decode_uint (dec); |
798 | STRLEN len = decode_uint (dec); |
644 | char *key = (char *)dec->cur; |
799 | char *key = (char *)dec->cur; |
645 | |
800 | |
|
|
801 | WANT (len); |
646 | dec->cur += len; |
802 | dec->cur += len; |
647 | |
803 | |
|
|
804 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
805 | if (!is_utf8_string (key, len)) |
|
|
806 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
|
|
807 | |
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
808 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
809 | |
|
|
810 | return; |
649 | } |
811 | } |
650 | else |
812 | |
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
813 | SV *k = decode_sv (dec); |
653 | SV *v = decode_sv (dec); |
814 | SV *v = decode_sv (dec); |
654 | |
815 | |
655 | hv_store_ent (hv, k, v, 0); |
816 | hv_store_ent (hv, k, v, 0); |
656 | SvREFCNT_dec (k); |
817 | SvREFCNT_dec (k); |
657 | } |
818 | |
|
|
819 | fail: |
|
|
820 | ; |
658 | } |
821 | } |
659 | |
822 | |
660 | static SV * |
823 | static SV * |
661 | decode_hv (dec_t *dec) |
824 | decode_hv (dec_t *dec) |
662 | { |
825 | { |
663 | HV *hv = newHV (); |
826 | HV *hv = newHV (); |
664 | |
827 | |
665 | DEC_INC_DEPTH; |
828 | DEC_INC_DEPTH; |
666 | |
829 | |
667 | if ((*dec->cur & 31) == 31) |
830 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
668 | { |
831 | { |
669 | ++dec->cur; |
832 | ++dec->cur; |
670 | |
833 | |
671 | for (;;) |
834 | for (;;) |
672 | { |
835 | { |
673 | WANT (1); |
836 | WANT (1); |
674 | |
837 | |
675 | if (*dec->cur == (0xe0 | 31)) |
838 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
676 | { |
839 | { |
677 | ++dec->cur; |
840 | ++dec->cur; |
678 | break; |
841 | break; |
679 | } |
842 | } |
680 | |
843 | |
681 | decode_he (dec, hv); |
844 | decode_he (dec, hv); |
682 | } |
845 | } |
683 | } |
846 | } |
684 | else |
847 | else |
685 | { |
848 | { |
686 | int pairs = decode_uint (dec); |
849 | UV pairs = decode_uint (dec); |
|
|
850 | |
|
|
851 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
687 | |
852 | |
688 | while (pairs--) |
853 | while (pairs--) |
689 | decode_he (dec, hv); |
854 | decode_he (dec, hv); |
690 | } |
855 | } |
691 | |
856 | |
… | |
… | |
701 | static SV * |
866 | static SV * |
702 | decode_str (dec_t *dec, int utf8) |
867 | decode_str (dec_t *dec, int utf8) |
703 | { |
868 | { |
704 | SV *sv = 0; |
869 | SV *sv = 0; |
705 | |
870 | |
706 | if ((*dec->cur & 31) == 31) |
871 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
707 | { |
872 | { |
|
|
873 | // indefinite length strings |
708 | ++dec->cur; |
874 | ++dec->cur; |
709 | |
875 | |
|
|
876 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
877 | |
710 | sv = newSVpvn ("", 0); |
878 | sv = newSVpvn ("", 0); |
711 | |
879 | |
712 | // not very fast, and certainly not robust against illegal input |
|
|
713 | for (;;) |
880 | for (;;) |
714 | { |
881 | { |
715 | WANT (1); |
882 | WANT (1); |
716 | |
883 | |
717 | if (*dec->cur == (0xe0 | 31)) |
884 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
885 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
718 | { |
886 | { |
719 | ++dec->cur; |
887 | ++dec->cur; |
720 | break; |
888 | break; |
721 | } |
889 | } |
|
|
890 | else |
|
|
891 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
722 | |
892 | |
723 | sv_catsv (sv, decode_sv (dec)); |
893 | STRLEN len = decode_uint (dec); |
|
|
894 | |
|
|
895 | WANT (len); |
|
|
896 | sv_catpvn (sv, dec->cur, len); |
|
|
897 | dec->cur += len; |
724 | } |
898 | } |
725 | } |
899 | } |
726 | else |
900 | else |
727 | { |
901 | { |
728 | STRLEN len = decode_uint (dec); |
902 | STRLEN len = decode_uint (dec); |
729 | |
903 | |
730 | WANT (len); |
904 | WANT (len); |
731 | sv = newSVpvn (dec->cur, len); |
905 | sv = newSVpvn (dec->cur, len); |
732 | dec->cur += len; |
906 | dec->cur += len; |
|
|
907 | |
|
|
908 | if (ecb_expect_false (dec->stringref) |
|
|
909 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
910 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
733 | } |
911 | } |
734 | |
912 | |
735 | if (utf8) |
913 | if (utf8) |
|
|
914 | { |
|
|
915 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
916 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
917 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
918 | |
736 | SvUTF8_on (sv); |
919 | SvUTF8_on (sv); |
|
|
920 | } |
737 | |
921 | |
738 | return sv; |
922 | return sv; |
739 | |
923 | |
740 | fail: |
924 | fail: |
741 | SvREFCNT_dec (sv); |
925 | SvREFCNT_dec (sv); |
… | |
… | |
743 | } |
927 | } |
744 | |
928 | |
745 | static SV * |
929 | static SV * |
746 | decode_tagged (dec_t *dec) |
930 | decode_tagged (dec_t *dec) |
747 | { |
931 | { |
|
|
932 | SV *sv = 0; |
748 | UV tag = decode_uint (dec); |
933 | UV tag = decode_uint (dec); |
749 | SV *sv = decode_sv (dec); |
934 | |
|
|
935 | WANT (1); |
750 | |
936 | |
751 | switch (tag) |
937 | switch (tag) |
752 | { |
938 | { |
753 | case CBOR_TAG_MAGIC: |
939 | case CBOR_TAG_MAGIC: |
754 | return sv; |
940 | sv = decode_sv (dec); |
|
|
941 | break; |
755 | |
942 | |
756 | case CBOR_TAG_INDIRECTION: |
943 | case CBOR_TAG_INDIRECTION: |
757 | return newRV_noinc (sv); |
944 | sv = newRV_noinc (decode_sv (dec)); |
|
|
945 | break; |
758 | |
946 | |
|
|
947 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
948 | { |
|
|
949 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
950 | // erase mortalised caches, e.g. "shareable" |
|
|
951 | ENTER; |
|
|
952 | |
|
|
953 | SAVESPTR (dec->stringref); |
|
|
954 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
955 | |
|
|
956 | sv = decode_sv (dec); |
|
|
957 | |
|
|
958 | LEAVE; |
|
|
959 | } |
|
|
960 | break; |
|
|
961 | |
|
|
962 | case CBOR_TAG_STRINGREF: |
|
|
963 | { |
|
|
964 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
965 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
966 | |
|
|
967 | UV idx = decode_uint (dec); |
|
|
968 | |
|
|
969 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
970 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
971 | |
|
|
972 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
973 | } |
|
|
974 | break; |
|
|
975 | |
759 | case CBOR_TAG_VALUE_SHARABLE: |
976 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
977 | { |
760 | if (ecb_expect_false (!dec->sharable)) |
978 | if (ecb_expect_false (!dec->shareable)) |
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
979 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
762 | |
980 | |
|
|
981 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
982 | { |
|
|
983 | sv = newSV (0); |
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
984 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
764 | |
985 | |
765 | return sv; |
986 | SV *osv = decode_sv (dec); |
|
|
987 | sv_setsv (sv, osv); |
|
|
988 | SvREFCNT_dec_NN (osv); |
|
|
989 | } |
|
|
990 | else |
|
|
991 | { |
|
|
992 | av_push (dec->shareable, &PL_sv_undef); |
|
|
993 | int idx = AvFILLp (dec->shareable); |
|
|
994 | sv = decode_sv (dec); |
|
|
995 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
996 | } |
|
|
997 | } |
|
|
998 | break; |
766 | |
999 | |
767 | case CBOR_TAG_VALUE_SHAREDREF: |
1000 | case CBOR_TAG_VALUE_SHAREDREF: |
768 | { |
1001 | { |
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
1002 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
770 | UV idx = SvUV (sv); |
1003 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
771 | |
1004 | |
|
|
1005 | UV idx = decode_uint (dec); |
|
|
1006 | |
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
1007 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1008 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
774 | |
1009 | |
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
1010 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
1011 | |
|
|
1012 | if (sv == &PL_sv_undef) |
|
|
1013 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
778 | } |
1014 | } |
|
|
1015 | break; |
779 | |
1016 | |
780 | case CBOR_TAG_PERL_OBJECT: |
1017 | case CBOR_TAG_PERL_OBJECT: |
781 | { |
1018 | { |
|
|
1019 | sv = decode_sv (dec); |
|
|
1020 | |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1021 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
1022 | ERR ("corrupted CBOR data (non-array perl object)"); |
784 | |
1023 | |
785 | AV *av = (AV *)SvRV (sv); |
1024 | AV *av = (AV *)SvRV (sv); |
786 | int len = av_len (av) + 1; |
1025 | int len = av_len (av) + 1; |
… | |
… | |
794 | if (!method) |
1033 | if (!method) |
795 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1034 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
796 | |
1035 | |
797 | dSP; |
1036 | dSP; |
798 | |
1037 | |
799 | ENTER; SAVETMPS; PUSHMARK (SP); |
1038 | ENTER; SAVETMPS; |
|
|
1039 | PUSHMARK (SP); |
800 | EXTEND (SP, len + 1); |
1040 | EXTEND (SP, len + 1); |
801 | // we re-bless the reference to get overload and other niceties right |
1041 | // we re-bless the reference to get overload and other niceties right |
802 | PUSHs (*av_fetch (av, 0, 1)); |
1042 | PUSHs (*av_fetch (av, 0, 1)); |
803 | PUSHs (sv_cbor); |
1043 | PUSHs (sv_cbor); |
804 | |
1044 | |
… | |
… | |
821 | sv = SvREFCNT_inc (POPs); |
1061 | sv = SvREFCNT_inc (POPs); |
822 | |
1062 | |
823 | PUTBACK; |
1063 | PUTBACK; |
824 | |
1064 | |
825 | FREETMPS; LEAVE; |
1065 | FREETMPS; LEAVE; |
826 | |
|
|
827 | return sv; |
|
|
828 | } |
1066 | } |
|
|
1067 | break; |
829 | |
1068 | |
830 | default: |
1069 | default: |
831 | { |
1070 | { |
|
|
1071 | sv = decode_sv (dec); |
|
|
1072 | |
|
|
1073 | dSP; |
|
|
1074 | ENTER; SAVETMPS; |
|
|
1075 | SAVESTACK_POS (); |
|
|
1076 | PUSHMARK (SP); |
|
|
1077 | EXTEND (SP, 2); |
|
|
1078 | PUSHs (newSVuv (tag)); |
|
|
1079 | PUSHs (sv); |
|
|
1080 | |
|
|
1081 | PUTBACK; |
|
|
1082 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
1083 | SPAGAIN; |
|
|
1084 | |
|
|
1085 | if (SvTRUE (ERRSV)) |
|
|
1086 | { |
|
|
1087 | FREETMPS; LEAVE; |
|
|
1088 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
1089 | } |
|
|
1090 | |
|
|
1091 | if (count) |
|
|
1092 | { |
|
|
1093 | SvREFCNT_dec (sv); |
|
|
1094 | sv = SvREFCNT_inc (POPs); |
|
|
1095 | } |
|
|
1096 | else |
|
|
1097 | { |
832 | AV *av = newAV (); |
1098 | AV *av = newAV (); |
833 | av_push (av, newSVuv (tag)); |
1099 | av_push (av, newSVuv (tag)); |
834 | av_push (av, sv); |
1100 | av_push (av, sv); |
835 | |
1101 | |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1102 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
837 | ? cbor_tagged_stash |
1103 | ? cbor_tagged_stash |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1104 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
839 | |
|
|
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
1105 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
841 | } |
1106 | } |
|
|
1107 | |
|
|
1108 | PUTBACK; |
|
|
1109 | |
|
|
1110 | FREETMPS; LEAVE; |
|
|
1111 | } |
|
|
1112 | break; |
842 | } |
1113 | } |
|
|
1114 | |
|
|
1115 | return sv; |
843 | |
1116 | |
844 | fail: |
1117 | fail: |
845 | SvREFCNT_dec (sv); |
1118 | SvREFCNT_dec (sv); |
846 | return &PL_sv_undef; |
1119 | return &PL_sv_undef; |
847 | } |
1120 | } |
… | |
… | |
849 | static SV * |
1122 | static SV * |
850 | decode_sv (dec_t *dec) |
1123 | decode_sv (dec_t *dec) |
851 | { |
1124 | { |
852 | WANT (1); |
1125 | WANT (1); |
853 | |
1126 | |
854 | switch (*dec->cur >> 5) |
1127 | switch (*dec->cur >> MAJOR_SHIFT) |
855 | { |
1128 | { |
856 | case 0: // unsigned int |
1129 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
857 | return newSVuv (decode_uint (dec)); |
1130 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
858 | case 1: // negative int |
1131 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
859 | return newSViv (-1 - (IV)decode_uint (dec)); |
1132 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
860 | case 2: // octet string |
1133 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
861 | return decode_str (dec, 0); |
1134 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
862 | case 3: // utf-8 string |
1135 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
863 | return decode_str (dec, 1); |
1136 | |
864 | case 4: // array |
1137 | case MAJOR_MISC >> MAJOR_SHIFT: |
865 | return decode_av (dec); |
|
|
866 | case 5: // map |
|
|
867 | return decode_hv (dec); |
|
|
868 | case 6: // tag |
|
|
869 | return decode_tagged (dec); |
|
|
870 | case 7: // misc |
|
|
871 | switch (*dec->cur++ & 31) |
1138 | switch (*dec->cur++ & MINOR_MASK) |
872 | { |
1139 | { |
873 | case 20: |
1140 | case SIMPLE_FALSE: |
874 | #if CBOR_SLOW |
1141 | #if CBOR_SLOW |
875 | types_false = get_bool ("Types::Serialiser::false"); |
1142 | types_false = get_bool ("Types::Serialiser::false"); |
876 | #endif |
1143 | #endif |
877 | return newSVsv (types_false); |
1144 | return newSVsv (types_false); |
878 | case 21: |
1145 | case SIMPLE_TRUE: |
879 | #if CBOR_SLOW |
1146 | #if CBOR_SLOW |
880 | types_true = get_bool ("Types::Serialiser::true"); |
1147 | types_true = get_bool ("Types::Serialiser::true"); |
881 | #endif |
1148 | #endif |
882 | return newSVsv (types_true); |
1149 | return newSVsv (types_true); |
883 | case 22: |
1150 | case SIMPLE_NULL: |
884 | return newSVsv (&PL_sv_undef); |
1151 | return newSVsv (&PL_sv_undef); |
885 | case 23: |
1152 | case SIMPLE_UNDEF: |
886 | #if CBOR_SLOW |
1153 | #if CBOR_SLOW |
887 | types_error = get_bool ("Types::Serialiser::error"); |
1154 | types_error = get_bool ("Types::Serialiser::error"); |
888 | #endif |
1155 | #endif |
889 | return newSVsv (types_error); |
1156 | return newSVsv (types_error); |
890 | |
1157 | |
891 | case 25: |
1158 | case MISC_FLOAT16: |
892 | { |
1159 | { |
893 | WANT (2); |
1160 | WANT (2); |
894 | |
1161 | |
895 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1162 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
896 | dec->cur += 2; |
1163 | dec->cur += 2; |
897 | |
1164 | |
898 | return newSVnv (ecb_binary16_to_float (fp)); |
1165 | return newSVnv (ecb_binary16_to_float (fp)); |
899 | } |
1166 | } |
900 | |
1167 | |
901 | case 26: |
1168 | case MISC_FLOAT32: |
902 | { |
1169 | { |
903 | uint32_t fp; |
1170 | uint32_t fp; |
904 | WANT (4); |
1171 | WANT (4); |
905 | memcpy (&fp, dec->cur, 4); |
1172 | memcpy (&fp, dec->cur, 4); |
906 | dec->cur += 4; |
1173 | dec->cur += 4; |
… | |
… | |
909 | fp = ecb_bswap32 (fp); |
1176 | fp = ecb_bswap32 (fp); |
910 | |
1177 | |
911 | return newSVnv (ecb_binary32_to_float (fp)); |
1178 | return newSVnv (ecb_binary32_to_float (fp)); |
912 | } |
1179 | } |
913 | |
1180 | |
914 | case 27: |
1181 | case MISC_FLOAT64: |
915 | { |
1182 | { |
916 | uint64_t fp; |
1183 | uint64_t fp; |
917 | WANT (8); |
1184 | WANT (8); |
918 | memcpy (&fp, dec->cur, 8); |
1185 | memcpy (&fp, dec->cur, 8); |
919 | dec->cur += 8; |
1186 | dec->cur += 8; |
… | |
… | |
922 | fp = ecb_bswap64 (fp); |
1189 | fp = ecb_bswap64 (fp); |
923 | |
1190 | |
924 | return newSVnv (ecb_binary64_to_double (fp)); |
1191 | return newSVnv (ecb_binary64_to_double (fp)); |
925 | } |
1192 | } |
926 | |
1193 | |
927 | // 0..19 unassigned |
1194 | // 0..19 unassigned simple |
928 | // 24 reserved + unassigned (reserved values are not encodable) |
1195 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1196 | // 28-30 unassigned misc |
|
|
1197 | // 31 break code |
929 | default: |
1198 | default: |
930 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1199 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
931 | } |
1200 | } |
932 | |
1201 | |
933 | break; |
1202 | break; |
934 | } |
1203 | } |
935 | |
1204 | |
… | |
… | |
938 | } |
1207 | } |
939 | |
1208 | |
940 | static SV * |
1209 | static SV * |
941 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1210 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
942 | { |
1211 | { |
943 | dec_t dec = { }; |
1212 | dec_t dec = { 0 }; |
944 | SV *sv; |
1213 | SV *sv; |
945 | STRLEN len; |
1214 | STRLEN len; |
946 | char *data = SvPVbyte (string, len); |
1215 | char *data = SvPVbyte (string, len); |
947 | |
1216 | |
948 | if (len > cbor->max_size && cbor->max_size) |
1217 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
962 | if (dec.cur != dec.end && !dec.err) |
1231 | if (dec.cur != dec.end && !dec.err) |
963 | dec.err = "garbage after CBOR object"; |
1232 | dec.err = "garbage after CBOR object"; |
964 | |
1233 | |
965 | if (dec.err) |
1234 | if (dec.err) |
966 | { |
1235 | { |
|
|
1236 | if (dec.shareable) |
|
|
1237 | { |
|
|
1238 | // need to break cyclic links, which whould all be in shareable |
|
|
1239 | int i; |
|
|
1240 | SV **svp; |
|
|
1241 | |
|
|
1242 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1243 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1244 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1245 | } |
|
|
1246 | |
967 | SvREFCNT_dec (sv); |
1247 | SvREFCNT_dec (sv); |
968 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1248 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
969 | } |
1249 | } |
970 | |
1250 | |
971 | sv = sv_2mortal (sv); |
1251 | sv = sv_2mortal (sv); |
972 | |
1252 | |
973 | return sv; |
1253 | return sv; |
974 | } |
1254 | } |
975 | |
1255 | |
|
|
1256 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1257 | // incremental parser |
|
|
1258 | |
|
|
1259 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1260 | |
|
|
1261 | // returns 0 for notyet, 1 for success or error |
|
|
1262 | static int |
|
|
1263 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1264 | { |
|
|
1265 | STRLEN cur; |
|
|
1266 | SvPV (cborstr, cur); |
|
|
1267 | |
|
|
1268 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1269 | { |
|
|
1270 | // table of integer count bytes |
|
|
1271 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1272 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1273 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1274 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1275 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1276 | }; |
|
|
1277 | |
|
|
1278 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1279 | U8 m = *p & MINOR_MASK; |
|
|
1280 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1281 | I8 ilen = incr_len[m]; |
|
|
1282 | |
|
|
1283 | self->incr_need = self->incr_pos + 1; |
|
|
1284 | |
|
|
1285 | if (ecb_expect_false (ilen < 0)) |
|
|
1286 | { |
|
|
1287 | if (m != MINOR_INDEF) |
|
|
1288 | return 1; // error |
|
|
1289 | |
|
|
1290 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1291 | { |
|
|
1292 | if (count >= 0) |
|
|
1293 | return 1; // error |
|
|
1294 | |
|
|
1295 | count = 1; |
|
|
1296 | } |
|
|
1297 | else |
|
|
1298 | { |
|
|
1299 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1300 | count = -1; |
|
|
1301 | } |
|
|
1302 | } |
|
|
1303 | else |
|
|
1304 | { |
|
|
1305 | self->incr_need += ilen; |
|
|
1306 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1307 | return 0; |
|
|
1308 | |
|
|
1309 | int major = *p >> MAJOR_SHIFT; |
|
|
1310 | |
|
|
1311 | switch (major) |
|
|
1312 | { |
|
|
1313 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1314 | ++count; // tags merely prefix another value |
|
|
1315 | break; |
|
|
1316 | |
|
|
1317 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1318 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1319 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1320 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1321 | { |
|
|
1322 | UV len; |
|
|
1323 | |
|
|
1324 | if (ecb_expect_false (ilen)) |
|
|
1325 | { |
|
|
1326 | len = 0; |
|
|
1327 | |
|
|
1328 | do { |
|
|
1329 | len = (len << 8) | *++p; |
|
|
1330 | } while (--ilen); |
|
|
1331 | } |
|
|
1332 | else |
|
|
1333 | len = m; |
|
|
1334 | |
|
|
1335 | switch (major) |
|
|
1336 | { |
|
|
1337 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1338 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1339 | self->incr_need += len; |
|
|
1340 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1341 | return 0; |
|
|
1342 | |
|
|
1343 | break; |
|
|
1344 | |
|
|
1345 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1346 | len <<= 1; |
|
|
1347 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1348 | if (len) |
|
|
1349 | { |
|
|
1350 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1351 | count = len + 1; |
|
|
1352 | } |
|
|
1353 | break; |
|
|
1354 | } |
|
|
1355 | } |
|
|
1356 | } |
|
|
1357 | } |
|
|
1358 | |
|
|
1359 | self->incr_pos = self->incr_need; |
|
|
1360 | |
|
|
1361 | if (count > 0) |
|
|
1362 | { |
|
|
1363 | while (!--count) |
|
|
1364 | { |
|
|
1365 | if (!AvFILLp (self->incr_count)) |
|
|
1366 | return 1; // done |
|
|
1367 | |
|
|
1368 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1369 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1370 | } |
|
|
1371 | |
|
|
1372 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1373 | } |
|
|
1374 | } |
|
|
1375 | |
|
|
1376 | return 0; |
|
|
1377 | } |
|
|
1378 | |
|
|
1379 | |
976 | ///////////////////////////////////////////////////////////////////////////// |
1380 | ///////////////////////////////////////////////////////////////////////////// |
977 | // XS interface functions |
1381 | // XS interface functions |
978 | |
1382 | |
979 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1383 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
980 | |
1384 | |
… | |
… | |
988 | |
1392 | |
989 | types_true = get_bool ("Types::Serialiser::true" ); |
1393 | types_true = get_bool ("Types::Serialiser::true" ); |
990 | types_false = get_bool ("Types::Serialiser::false"); |
1394 | types_false = get_bool ("Types::Serialiser::false"); |
991 | types_error = get_bool ("Types::Serialiser::error"); |
1395 | types_error = get_bool ("Types::Serialiser::error"); |
992 | |
1396 | |
|
|
1397 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1398 | |
993 | sv_cbor = newSVpv ("CBOR", 0); |
1399 | sv_cbor = newSVpv ("CBOR", 0); |
994 | SvREADONLY_on (sv_cbor); |
1400 | SvREADONLY_on (sv_cbor); |
|
|
1401 | |
|
|
1402 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
995 | } |
1403 | } |
996 | |
1404 | |
997 | PROTOTYPES: DISABLE |
1405 | PROTOTYPES: DISABLE |
998 | |
1406 | |
999 | void CLONE (...) |
1407 | void CLONE (...) |
… | |
… | |
1018 | void shrink (CBOR *self, int enable = 1) |
1426 | void shrink (CBOR *self, int enable = 1) |
1019 | ALIAS: |
1427 | ALIAS: |
1020 | shrink = F_SHRINK |
1428 | shrink = F_SHRINK |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
1429 | allow_unknown = F_ALLOW_UNKNOWN |
1022 | allow_sharing = F_ALLOW_SHARING |
1430 | allow_sharing = F_ALLOW_SHARING |
1023 | dedup_keys = F_DEDUP_KEYS |
1431 | allow_cycles = F_ALLOW_CYCLES |
1024 | dedup_strings = F_DEDUP_STRINGS |
1432 | pack_strings = F_PACK_STRINGS |
|
|
1433 | text_keys = F_TEXT_KEYS |
|
|
1434 | text_strings = F_TEXT_STRINGS |
|
|
1435 | validate_utf8 = F_VALIDATE_UTF8 |
1025 | PPCODE: |
1436 | PPCODE: |
1026 | { |
1437 | { |
1027 | if (enable) |
1438 | if (enable) |
1028 | self->flags |= ix; |
1439 | self->flags |= ix; |
1029 | else |
1440 | else |
… | |
… | |
1035 | void get_shrink (CBOR *self) |
1446 | void get_shrink (CBOR *self) |
1036 | ALIAS: |
1447 | ALIAS: |
1037 | get_shrink = F_SHRINK |
1448 | get_shrink = F_SHRINK |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
1449 | get_allow_unknown = F_ALLOW_UNKNOWN |
1039 | get_allow_sharing = F_ALLOW_SHARING |
1450 | get_allow_sharing = F_ALLOW_SHARING |
1040 | get_dedup_keys = F_DEDUP_KEYS |
1451 | get_allow_cycles = F_ALLOW_CYCLES |
1041 | get_dedup_strings = F_DEDUP_STRINGS |
1452 | get_pack_strings = F_PACK_STRINGS |
|
|
1453 | get_text_keys = F_TEXT_KEYS |
|
|
1454 | get_text_strings = F_TEXT_STRINGS |
|
|
1455 | get_validate_utf8 = F_VALIDATE_UTF8 |
1042 | PPCODE: |
1456 | PPCODE: |
1043 | XPUSHs (boolSV (self->flags & ix)); |
1457 | XPUSHs (boolSV (self->flags & ix)); |
1044 | |
1458 | |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1459 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1046 | PPCODE: |
1460 | PPCODE: |
… | |
… | |
1059 | XPUSHs (ST (0)); |
1473 | XPUSHs (ST (0)); |
1060 | |
1474 | |
1061 | int get_max_size (CBOR *self) |
1475 | int get_max_size (CBOR *self) |
1062 | CODE: |
1476 | CODE: |
1063 | RETVAL = self->max_size; |
1477 | RETVAL = self->max_size; |
|
|
1478 | OUTPUT: |
|
|
1479 | RETVAL |
|
|
1480 | |
|
|
1481 | void filter (CBOR *self, SV *filter = 0) |
|
|
1482 | PPCODE: |
|
|
1483 | SvREFCNT_dec (self->filter); |
|
|
1484 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1485 | XPUSHs (ST (0)); |
|
|
1486 | |
|
|
1487 | SV *get_filter (CBOR *self) |
|
|
1488 | CODE: |
|
|
1489 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1064 | OUTPUT: |
1490 | OUTPUT: |
1065 | RETVAL |
1491 | RETVAL |
1066 | |
1492 | |
1067 | void encode (CBOR *self, SV *scalar) |
1493 | void encode (CBOR *self, SV *scalar) |
1068 | PPCODE: |
1494 | PPCODE: |
… | |
… | |
1083 | EXTEND (SP, 2); |
1509 | EXTEND (SP, 2); |
1084 | PUSHs (sv); |
1510 | PUSHs (sv); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1511 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1086 | } |
1512 | } |
1087 | |
1513 | |
|
|
1514 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1515 | ALIAS: |
|
|
1516 | incr_parse_multiple = 1 |
|
|
1517 | PPCODE: |
|
|
1518 | { |
|
|
1519 | if (SvUTF8 (cborstr)) |
|
|
1520 | sv_utf8_downgrade (cborstr, 0); |
|
|
1521 | |
|
|
1522 | if (!self->incr_count) |
|
|
1523 | { |
|
|
1524 | self->incr_count = newAV (); |
|
|
1525 | self->incr_pos = 0; |
|
|
1526 | self->incr_need = 1; |
|
|
1527 | |
|
|
1528 | av_push (self->incr_count, newSViv (1)); |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | do |
|
|
1532 | { |
|
|
1533 | if (!incr_parse (self, cborstr)) |
|
|
1534 | { |
|
|
1535 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1536 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1537 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1538 | |
|
|
1539 | break; |
|
|
1540 | } |
|
|
1541 | |
|
|
1542 | SV *sv; |
|
|
1543 | char *offset; |
|
|
1544 | |
|
|
1545 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1546 | XPUSHs (sv); |
|
|
1547 | |
|
|
1548 | sv_chop (cborstr, offset); |
|
|
1549 | |
|
|
1550 | av_clear (self->incr_count); |
|
|
1551 | av_push (self->incr_count, newSViv (1)); |
|
|
1552 | |
|
|
1553 | self->incr_pos = 0; |
|
|
1554 | self->incr_need = self->incr_pos + 1; |
|
|
1555 | } |
|
|
1556 | while (ix); |
|
|
1557 | } |
|
|
1558 | |
|
|
1559 | void incr_reset (CBOR *self) |
|
|
1560 | CODE: |
|
|
1561 | { |
|
|
1562 | SvREFCNT_dec (self->incr_count); |
|
|
1563 | self->incr_count = 0; |
|
|
1564 | } |
|
|
1565 | |
|
|
1566 | void DESTROY (CBOR *self) |
|
|
1567 | PPCODE: |
|
|
1568 | cbor_free (self); |
|
|
1569 | |
1088 | PROTOTYPES: ENABLE |
1570 | PROTOTYPES: ENABLE |
1089 | |
1571 | |
1090 | void encode_cbor (SV *scalar) |
1572 | void encode_cbor (SV *scalar) |
|
|
1573 | ALIAS: |
|
|
1574 | encode_cbor = 0 |
|
|
1575 | encode_cbor_sharing = F_ALLOW_SHARING |
1091 | PPCODE: |
1576 | PPCODE: |
1092 | { |
1577 | { |
1093 | CBOR cbor; |
1578 | CBOR cbor; |
1094 | cbor_init (&cbor); |
1579 | cbor_init (&cbor); |
|
|
1580 | cbor.flags |= ix; |
1095 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1581 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1096 | XPUSHs (scalar); |
1582 | XPUSHs (scalar); |
1097 | } |
1583 | } |
1098 | |
1584 | |
1099 | void decode_cbor (SV *cborstr) |
1585 | void decode_cbor (SV *cborstr) |