… | |
… | |
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 | |
|
|
13 | #define ECB_NO_THREADS 1 |
12 | #include "ecb.h" |
14 | #include "ecb.h" |
13 | |
15 | |
14 | // compatibility with perl <5.18 |
16 | // compatibility with perl <5.18 |
15 | #ifndef HvNAMELEN_get |
17 | #ifndef HvNAMELEN_get |
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
18 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
21 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
22 | #endif |
21 | #ifndef HvNAMEUTF8 |
23 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
24 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
25 | #endif |
|
|
26 | #ifndef SvREFCNT_inc_NN |
|
|
27 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
28 | #endif |
24 | #ifndef SvREFCNT_dec_NN |
29 | #ifndef SvREFCNT_dec_NN |
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
26 | #endif |
31 | #endif |
27 | |
32 | |
28 | // known major and minor types |
33 | // known major and minor types |
… | |
… | |
94 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
99 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
95 | |
100 | |
96 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
101 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
97 | }; |
102 | }; |
98 | |
103 | |
|
|
104 | // known forced types, also hardcoded in CBOR.pm |
|
|
105 | enum |
|
|
106 | { |
|
|
107 | AS_CBOR = 0, |
|
|
108 | AS_INT = 1, |
|
|
109 | AS_BYTES = 2, |
|
|
110 | AS_TEXT = 3, |
|
|
111 | AS_FLOAT16 = 4, |
|
|
112 | AS_FLOAT32 = 5, |
|
|
113 | AS_FLOAT64 = 6, |
|
|
114 | AS_MAP = 7, |
|
|
115 | // possibly future enhancements: (generic) float, (generic) string |
|
|
116 | }; |
|
|
117 | |
99 | #define F_SHRINK 0x00000001UL |
118 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
119 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
120 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
121 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
122 | #define F_FORBID_OBJECTS 0x00000010UL |
102 | #define F_PACK_STRINGS 0x00000008UL |
123 | #define F_PACK_STRINGS 0x00000020UL |
|
|
124 | #define F_TEXT_KEYS 0x00000040UL |
|
|
125 | #define F_TEXT_STRINGS 0x00000080UL |
|
|
126 | #define F_VALIDATE_UTF8 0x00000100UL |
103 | |
127 | |
104 | #define INIT_SIZE 32 // initial scalar size to be allocated |
128 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | |
129 | |
106 | #define SB do { |
130 | #define SB do { |
107 | #define SE } while (0) |
131 | #define SE } while (0) |
… | |
… | |
126 | typedef struct { |
150 | typedef struct { |
127 | U32 flags; |
151 | U32 flags; |
128 | U32 max_depth; |
152 | U32 max_depth; |
129 | STRLEN max_size; |
153 | STRLEN max_size; |
130 | SV *filter; |
154 | SV *filter; |
|
|
155 | |
|
|
156 | // for the incremental parser |
|
|
157 | STRLEN incr_pos; // the current offset into the text |
|
|
158 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
159 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
131 | } CBOR; |
160 | } CBOR; |
132 | |
161 | |
133 | ecb_inline void |
162 | ecb_inline void |
134 | cbor_init (CBOR *cbor) |
163 | cbor_init (CBOR *cbor) |
135 | { |
164 | { |
… | |
… | |
139 | |
168 | |
140 | ecb_inline void |
169 | ecb_inline void |
141 | cbor_free (CBOR *cbor) |
170 | cbor_free (CBOR *cbor) |
142 | { |
171 | { |
143 | SvREFCNT_dec (cbor->filter); |
172 | SvREFCNT_dec (cbor->filter); |
|
|
173 | SvREFCNT_dec (cbor->incr_count); |
144 | } |
174 | } |
145 | |
175 | |
146 | ///////////////////////////////////////////////////////////////////////////// |
176 | ///////////////////////////////////////////////////////////////////////////// |
147 | // utility functions |
177 | // utility functions |
148 | |
178 | |
… | |
… | |
174 | |
204 | |
175 | // minimum length of a string to be registered for stringref |
205 | // minimum length of a string to be registered for stringref |
176 | ecb_inline int |
206 | ecb_inline int |
177 | minimum_string_length (UV idx) |
207 | minimum_string_length (UV idx) |
178 | { |
208 | { |
179 | return idx > 23 |
209 | return idx <= 23 ? 3 |
180 | ? idx > 0xffU |
210 | : idx <= 0xffU ? 4 |
181 | ? idx > 0xffffU |
211 | : idx <= 0xffffU ? 5 |
182 | ? idx > 0xffffffffU |
212 | : idx <= 0xffffffffU ? 7 |
183 | ? 11 |
213 | : 11; |
184 | : 7 |
|
|
185 | : 5 |
|
|
186 | : 4 |
|
|
187 | : 3; |
|
|
188 | } |
214 | } |
189 | |
215 | |
190 | ///////////////////////////////////////////////////////////////////////////// |
216 | ///////////////////////////////////////////////////////////////////////////// |
191 | // encoder |
217 | // encoder |
192 | |
218 | |
… | |
… | |
205 | } enc_t; |
231 | } enc_t; |
206 | |
232 | |
207 | ecb_inline void |
233 | ecb_inline void |
208 | need (enc_t *enc, STRLEN len) |
234 | need (enc_t *enc, STRLEN len) |
209 | { |
235 | { |
210 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
236 | if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
211 | { |
237 | { |
212 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
238 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
213 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
239 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
214 | enc->cur = SvPVX (enc->sv) + cur; |
240 | enc->cur = SvPVX (enc->sv) + cur; |
215 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
241 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
216 | } |
242 | } |
217 | } |
243 | } |
218 | |
244 | |
|
|
245 | static void encode_sv (enc_t *enc, SV *sv); |
|
|
246 | |
219 | ecb_inline void |
247 | ecb_inline void |
220 | encode_ch (enc_t *enc, char ch) |
248 | encode_ch (enc_t *enc, char ch) |
221 | { |
249 | { |
222 | need (enc, 1); |
250 | need (enc, 1); |
223 | *enc->cur++ = ch; |
251 | *enc->cur++ = ch; |
224 | } |
252 | } |
225 | |
253 | |
|
|
254 | // used for tags, intregers, element counts and so on |
226 | static void |
255 | static void |
227 | encode_uint (enc_t *enc, int major, UV len) |
256 | encode_uint (enc_t *enc, int major, UV len) |
228 | { |
257 | { |
229 | need (enc, 9); |
258 | need (enc, 9); |
230 | |
259 | |
… | |
… | |
261 | *enc->cur++ = len >> 8; |
290 | *enc->cur++ = len >> 8; |
262 | *enc->cur++ = len; |
291 | *enc->cur++ = len; |
263 | } |
292 | } |
264 | } |
293 | } |
265 | |
294 | |
|
|
295 | // encodes a perl value into a CBOR integer |
|
|
296 | ecb_inline void |
|
|
297 | encode_int (enc_t *enc, SV *sv) |
|
|
298 | { |
|
|
299 | if (SvIsUV (sv)) |
|
|
300 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
|
|
301 | else if (SvIVX (sv) >= 0) |
|
|
302 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
|
|
303 | else |
|
|
304 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
|
|
305 | } |
|
|
306 | |
266 | ecb_inline void |
307 | ecb_inline void |
267 | encode_tag (enc_t *enc, UV tag) |
308 | encode_tag (enc_t *enc, UV tag) |
268 | { |
309 | { |
269 | encode_uint (enc, MAJOR_TAG, tag); |
310 | encode_uint (enc, MAJOR_TAG, tag); |
270 | } |
311 | } |
271 | |
312 | |
|
|
313 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
314 | ecb_noinline static void |
|
|
315 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
316 | { |
|
|
317 | STRLEN ulen = len; |
|
|
318 | U8 *p, *pend = (U8 *)str + len; |
|
|
319 | |
|
|
320 | for (p = (U8 *)str; p < pend; ++p) |
|
|
321 | ulen += *p >> 7; // count set high bits |
|
|
322 | |
|
|
323 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
324 | |
|
|
325 | need (enc, ulen); |
|
|
326 | for (p = (U8 *)str; p < pend; ++p) |
|
|
327 | if (*p < 0x80) |
|
|
328 | *enc->cur++ = *p; |
|
|
329 | else |
|
|
330 | { |
|
|
331 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
332 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
333 | } |
|
|
334 | } |
|
|
335 | |
272 | ecb_inline void |
336 | ecb_inline void |
273 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
337 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
274 | { |
338 | { |
|
|
339 | if (ecb_expect_false (upgrade_utf8)) |
|
|
340 | if (!utf8) |
|
|
341 | { |
|
|
342 | encode_str_utf8 (enc, utf8, str, len); |
|
|
343 | return; |
|
|
344 | } |
|
|
345 | |
275 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
346 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
276 | need (enc, len); |
347 | need (enc, len); |
277 | memcpy (enc->cur, str, len); |
348 | memcpy (enc->cur, str, len); |
278 | enc->cur += len; |
349 | enc->cur += len; |
279 | } |
350 | } |
280 | |
351 | |
281 | static void |
352 | ecb_inline void |
282 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
353 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
283 | { |
354 | { |
284 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
355 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
285 | { |
356 | { |
286 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
357 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
287 | |
358 | |
… | |
… | |
298 | sv_setuv (*svp, enc->stringref_idx); |
369 | sv_setuv (*svp, enc->stringref_idx); |
299 | ++enc->stringref_idx; |
370 | ++enc->stringref_idx; |
300 | } |
371 | } |
301 | } |
372 | } |
302 | |
373 | |
303 | encode_str (enc, utf8, str, len); |
374 | encode_str (enc, upgrade_utf8, utf8, str, len); |
304 | } |
375 | } |
305 | |
376 | |
306 | static void encode_sv (enc_t *enc, SV *sv); |
377 | ecb_inline void |
|
|
378 | encode_float16 (enc_t *enc, NV nv) |
|
|
379 | { |
|
|
380 | need (enc, 1+2); |
|
|
381 | |
|
|
382 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT16; |
|
|
383 | |
|
|
384 | uint16_t fp = ecb_float_to_binary16 (nv); |
|
|
385 | |
|
|
386 | if (!ecb_big_endian ()) |
|
|
387 | fp = ecb_bswap16 (fp); |
|
|
388 | |
|
|
389 | memcpy (enc->cur, &fp, 2); |
|
|
390 | enc->cur += 2; |
|
|
391 | } |
|
|
392 | |
|
|
393 | ecb_inline void |
|
|
394 | encode_float32 (enc_t *enc, NV nv) |
|
|
395 | { |
|
|
396 | need (enc, 1+4); |
|
|
397 | |
|
|
398 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
399 | |
|
|
400 | uint32_t fp = ecb_float_to_binary32 (nv); |
|
|
401 | |
|
|
402 | if (!ecb_big_endian ()) |
|
|
403 | fp = ecb_bswap32 (fp); |
|
|
404 | |
|
|
405 | memcpy (enc->cur, &fp, 4); |
|
|
406 | enc->cur += 4; |
|
|
407 | } |
|
|
408 | |
|
|
409 | ecb_inline void |
|
|
410 | encode_float64 (enc_t *enc, NV nv) |
|
|
411 | { |
|
|
412 | need (enc, 1+8); |
|
|
413 | |
|
|
414 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
415 | |
|
|
416 | uint64_t fp = ecb_double_to_binary64 (nv); |
|
|
417 | |
|
|
418 | if (!ecb_big_endian ()) |
|
|
419 | fp = ecb_bswap64 (fp); |
|
|
420 | |
|
|
421 | memcpy (enc->cur, &fp, 8); |
|
|
422 | enc->cur += 8; |
|
|
423 | } |
|
|
424 | |
|
|
425 | ecb_inline void |
|
|
426 | encode_bool (enc_t *enc, int istrue) |
|
|
427 | { |
|
|
428 | encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
429 | } |
|
|
430 | |
|
|
431 | // encodes an arrayref containing key-value pairs as CBOR map |
|
|
432 | ecb_inline void |
|
|
433 | encode_array_as_map (enc_t *enc, SV *sv) |
|
|
434 | { |
|
|
435 | if (enc->depth >= enc->cbor.max_depth) |
|
|
436 | croak (ERR_NESTING_EXCEEDED); |
|
|
437 | |
|
|
438 | ++enc->depth; |
|
|
439 | |
|
|
440 | // as_map does error checking for us, but we re-check in case |
|
|
441 | // things have changed. |
|
|
442 | |
|
|
443 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
444 | croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)"); |
|
|
445 | |
|
|
446 | AV *av = (AV *)SvRV (sv); |
|
|
447 | int i, len = av_len (av); |
|
|
448 | |
|
|
449 | if (!(len & 1)) |
|
|
450 | croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)"); |
|
|
451 | |
|
|
452 | encode_uint (enc, MAJOR_MAP, (len + 1) >> 1); |
|
|
453 | |
|
|
454 | for (i = 0; i <= len; ++i) |
|
|
455 | { |
|
|
456 | SV **svp = av_fetch (av, i, 0); |
|
|
457 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
|
|
458 | } |
|
|
459 | |
|
|
460 | --enc->depth; |
|
|
461 | } |
|
|
462 | |
|
|
463 | ecb_inline void |
|
|
464 | encode_forced (enc_t *enc, UV type, SV *sv) |
|
|
465 | { |
|
|
466 | switch (type) |
|
|
467 | { |
|
|
468 | case AS_CBOR: |
|
|
469 | { |
|
|
470 | STRLEN len; |
|
|
471 | char *str = SvPVbyte (sv, len); |
|
|
472 | |
|
|
473 | need (enc, len); |
|
|
474 | memcpy (enc->cur, str, len); |
|
|
475 | enc->cur += len; |
|
|
476 | } |
|
|
477 | break; |
|
|
478 | |
|
|
479 | case AS_BYTES: |
|
|
480 | { |
|
|
481 | STRLEN len; |
|
|
482 | char *str = SvPVbyte (sv, len); |
|
|
483 | encode_strref (enc, 0, 0, str, len); |
|
|
484 | } |
|
|
485 | break; |
|
|
486 | |
|
|
487 | case AS_TEXT: |
|
|
488 | { |
|
|
489 | STRLEN len; |
|
|
490 | char *str = SvPVutf8 (sv, len); |
|
|
491 | encode_strref (enc, 1, 1, str, len); |
|
|
492 | } |
|
|
493 | break; |
|
|
494 | |
|
|
495 | case AS_INT: encode_int (enc, sv); break; |
|
|
496 | |
|
|
497 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
|
|
498 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
|
|
499 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
|
|
500 | |
|
|
501 | case AS_MAP: encode_array_as_map (enc, sv); break; |
|
|
502 | |
|
|
503 | default: |
|
|
504 | croak ("encountered malformed CBOR::XS::Tagged object"); |
|
|
505 | } |
|
|
506 | } |
307 | |
507 | |
308 | static void |
508 | static void |
309 | encode_av (enc_t *enc, AV *av) |
509 | encode_av (enc_t *enc, AV *av) |
310 | { |
510 | { |
311 | int i, len = av_len (av); |
511 | int i, len = av_len (av); |
… | |
… | |
315 | |
515 | |
316 | ++enc->depth; |
516 | ++enc->depth; |
317 | |
517 | |
318 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
518 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
319 | |
519 | |
|
|
520 | if (ecb_expect_false (SvMAGICAL (av))) |
320 | for (i = 0; i <= len; ++i) |
521 | for (i = 0; i <= len; ++i) |
321 | { |
522 | { |
322 | SV **svp = av_fetch (av, i, 0); |
523 | SV **svp = av_fetch (av, i, 0); |
323 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
524 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | } |
525 | } |
|
|
526 | else |
|
|
527 | for (i = 0; i <= len; ++i) |
|
|
528 | { |
|
|
529 | SV *sv = AvARRAY (av)[i]; |
|
|
530 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
531 | } |
325 | |
532 | |
326 | --enc->depth; |
533 | --enc->depth; |
327 | } |
534 | } |
328 | |
535 | |
329 | static void |
536 | static void |
… | |
… | |
337 | ++enc->depth; |
544 | ++enc->depth; |
338 | |
545 | |
339 | int pairs = hv_iterinit (hv); |
546 | int pairs = hv_iterinit (hv); |
340 | int mg = SvMAGICAL (hv); |
547 | int mg = SvMAGICAL (hv); |
341 | |
548 | |
342 | if (mg) |
549 | if (ecb_expect_false (mg)) |
343 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
550 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
344 | else |
551 | else |
345 | encode_uint (enc, MAJOR_MAP, pairs); |
552 | encode_uint (enc, MAJOR_MAP, pairs); |
346 | |
553 | |
347 | while ((he = hv_iternext (hv))) |
554 | while ((he = hv_iternext (hv))) |
348 | { |
555 | { |
349 | if (HeKLEN (he) == HEf_SVKEY) |
556 | if (HeKLEN (he) == HEf_SVKEY) |
350 | encode_sv (enc, HeSVKEY (he)); |
557 | encode_sv (enc, HeSVKEY (he)); |
351 | else |
558 | else |
352 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
559 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
353 | |
560 | |
354 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
561 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
355 | } |
562 | } |
356 | |
563 | |
357 | if (mg) |
564 | if (ecb_expect_false (mg)) |
358 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
565 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
359 | |
566 | |
360 | --enc->depth; |
567 | --enc->depth; |
361 | } |
568 | } |
362 | |
569 | |
… | |
… | |
382 | |
589 | |
383 | HV *stash = SvSTASH (sv); |
590 | HV *stash = SvSTASH (sv); |
384 | |
591 | |
385 | if (stash == boolean_stash) |
592 | if (stash == boolean_stash) |
386 | { |
593 | { |
387 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
594 | encode_bool (enc, SvIV (sv)); |
388 | return; |
595 | return; |
389 | } |
596 | } |
390 | else if (stash == error_stash) |
597 | else if (stash == error_stash) |
391 | { |
598 | { |
392 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
599 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
… | |
… | |
395 | else if (stash == tagged_stash) |
602 | else if (stash == tagged_stash) |
396 | { |
603 | { |
397 | if (svt != SVt_PVAV) |
604 | if (svt != SVt_PVAV) |
398 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
605 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
399 | |
606 | |
|
|
607 | switch (av_len ((AV *)sv)) |
|
|
608 | { |
|
|
609 | case 2-1: |
|
|
610 | // actually a tagged value |
400 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
611 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
401 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
612 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
613 | break; |
|
|
614 | |
|
|
615 | case 3-1: |
|
|
616 | // a forced type [value, type, undef] |
|
|
617 | encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1)); |
|
|
618 | break; |
|
|
619 | |
|
|
620 | default: |
|
|
621 | croak ("encountered malformed CBOR::XS::Tagged object"); |
|
|
622 | } |
402 | |
623 | |
403 | return; |
624 | return; |
404 | } |
625 | } |
405 | } |
626 | } |
406 | |
627 | |
407 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
628 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
408 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
629 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
409 | { |
630 | { |
410 | if (!enc->shareable) |
631 | if (ecb_expect_false (!enc->shareable)) |
411 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
632 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
412 | |
633 | |
413 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
634 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
414 | |
635 | |
415 | if (SvOK (*svp)) |
636 | if (SvOK (*svp)) |
… | |
… | |
429 | if (ecb_expect_false (SvOBJECT (sv))) |
650 | if (ecb_expect_false (SvOBJECT (sv))) |
430 | { |
651 | { |
431 | HV *stash = SvSTASH (sv); |
652 | HV *stash = SvSTASH (sv); |
432 | GV *method; |
653 | GV *method; |
433 | |
654 | |
|
|
655 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
656 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
657 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
434 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
658 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
435 | { |
659 | { |
436 | dSP; |
660 | dSP; |
437 | |
661 | |
438 | ENTER; SAVETMPS; PUSHMARK (SP); |
662 | ENTER; SAVETMPS; |
|
|
663 | PUSHMARK (SP); |
439 | // we re-bless the reference to get overload and other niceties right |
664 | // we re-bless the reference to get overload and other niceties right |
440 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
665 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
441 | |
666 | |
442 | PUTBACK; |
667 | PUTBACK; |
443 | // G_SCALAR ensures that return value is 1 |
668 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
456 | } |
681 | } |
457 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
682 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
458 | { |
683 | { |
459 | dSP; |
684 | dSP; |
460 | |
685 | |
461 | ENTER; SAVETMPS; PUSHMARK (SP); |
686 | ENTER; SAVETMPS; |
|
|
687 | PUSHMARK (SP); |
462 | EXTEND (SP, 2); |
688 | EXTEND (SP, 2); |
463 | // we re-bless the reference to get overload and other niceties right |
689 | // we re-bless the reference to get overload and other niceties right |
464 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
690 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
465 | PUSHs (sv_cbor); |
691 | PUSHs (sv_cbor); |
466 | |
692 | |
… | |
… | |
472 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
698 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
473 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
699 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
474 | |
700 | |
475 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
701 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
476 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
702 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
477 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
703 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
478 | |
704 | |
479 | while (count) |
705 | { |
|
|
706 | int i; |
|
|
707 | |
|
|
708 | for (i = 0; i < count; ++i) |
480 | encode_sv (enc, SP[1 - count--]); |
709 | encode_sv (enc, SP[i + 1 - count]); |
|
|
710 | |
|
|
711 | SP -= count; |
|
|
712 | } |
481 | |
713 | |
482 | PUTBACK; |
714 | PUTBACK; |
483 | |
715 | |
484 | FREETMPS; LEAVE; |
716 | FREETMPS; LEAVE; |
485 | } |
717 | } |
… | |
… | |
507 | |
739 | |
508 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
740 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
509 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
741 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
510 | //TODO: maybe I32? |
742 | //TODO: maybe I32? |
511 | else if (ecb_expect_false (nv == (float)nv)) |
743 | else if (ecb_expect_false (nv == (float)nv)) |
512 | { |
744 | encode_float32 (enc, nv); |
513 | uint32_t fp = ecb_float_to_binary32 (nv); |
|
|
514 | |
|
|
515 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
|
|
516 | |
|
|
517 | if (!ecb_big_endian ()) |
|
|
518 | fp = ecb_bswap32 (fp); |
|
|
519 | |
|
|
520 | memcpy (enc->cur, &fp, 4); |
|
|
521 | enc->cur += 4; |
|
|
522 | } |
|
|
523 | else |
745 | else |
524 | { |
746 | encode_float64 (enc, nv); |
525 | uint64_t fp = ecb_double_to_binary64 (nv); |
|
|
526 | |
|
|
527 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
|
|
528 | |
|
|
529 | if (!ecb_big_endian ()) |
|
|
530 | fp = ecb_bswap64 (fp); |
|
|
531 | |
|
|
532 | memcpy (enc->cur, &fp, 8); |
|
|
533 | enc->cur += 8; |
|
|
534 | } |
|
|
535 | } |
747 | } |
536 | |
748 | |
537 | static void |
749 | static void |
538 | encode_sv (enc_t *enc, SV *sv) |
750 | encode_sv (enc_t *enc, SV *sv) |
539 | { |
751 | { |
… | |
… | |
541 | |
753 | |
542 | if (SvPOKp (sv)) |
754 | if (SvPOKp (sv)) |
543 | { |
755 | { |
544 | STRLEN len; |
756 | STRLEN len; |
545 | char *str = SvPV (sv, len); |
757 | char *str = SvPV (sv, len); |
546 | encode_strref (enc, SvUTF8 (sv), str, len); |
758 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
547 | } |
759 | } |
548 | else if (SvNOKp (sv)) |
760 | else if (SvNOKp (sv)) |
549 | encode_nv (enc, sv); |
761 | encode_nv (enc, sv); |
550 | else if (SvIOKp (sv)) |
762 | else if (SvIOKp (sv)) |
551 | { |
763 | encode_int (enc, sv); |
552 | if (SvIsUV (sv)) |
|
|
553 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
|
|
554 | else if (SvIVX (sv) >= 0) |
|
|
555 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
|
|
556 | else |
|
|
557 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
|
|
558 | } |
|
|
559 | else if (SvROK (sv)) |
764 | else if (SvROK (sv)) |
560 | encode_rv (enc, SvRV (sv)); |
765 | encode_rv (enc, SvRV (sv)); |
561 | else if (!SvOK (sv)) |
766 | else if (!SvOK (sv)) |
562 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
767 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
563 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
768 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
… | |
… | |
568 | } |
773 | } |
569 | |
774 | |
570 | static SV * |
775 | static SV * |
571 | encode_cbor (SV *scalar, CBOR *cbor) |
776 | encode_cbor (SV *scalar, CBOR *cbor) |
572 | { |
777 | { |
573 | enc_t enc = { }; |
778 | enc_t enc = { 0 }; |
574 | |
779 | |
575 | enc.cbor = *cbor; |
780 | enc.cbor = *cbor; |
576 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
781 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
577 | enc.cur = SvPVX (enc.sv); |
782 | enc.cur = SvPVX (enc.sv); |
578 | enc.end = SvEND (enc.sv); |
783 | enc.end = SvEND (enc.sv); |
579 | |
784 | |
580 | SvPOK_only (enc.sv); |
785 | SvPOK_only (enc.sv); |
581 | |
786 | |
582 | if (cbor->flags & F_PACK_STRINGS) |
787 | if (cbor->flags & F_PACK_STRINGS) |
583 | { |
788 | { |
… | |
… | |
610 | U32 depth; // recursion depth |
815 | U32 depth; // recursion depth |
611 | U32 maxdepth; // recursion depth limit |
816 | U32 maxdepth; // recursion depth limit |
612 | AV *shareable; |
817 | AV *shareable; |
613 | AV *stringref; |
818 | AV *stringref; |
614 | SV *decode_tagged; |
819 | SV *decode_tagged; |
|
|
820 | SV *err_sv; // optional sv for error, needs to be freed |
615 | } dec_t; |
821 | } dec_t; |
616 | |
822 | |
617 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
823 | // set dec->err to ERRSV |
|
|
824 | ecb_cold static void |
|
|
825 | err_errsv (dec_t *dec) |
|
|
826 | { |
|
|
827 | if (!dec->err) |
|
|
828 | { |
|
|
829 | dec->err_sv = newSVsv (ERRSV); |
618 | |
830 | |
619 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
831 | // chop off the trailing \n |
|
|
832 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
833 | *SvEND (dec->err_sv) = 0; |
620 | |
834 | |
|
|
835 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
836 | } |
|
|
837 | } |
|
|
838 | |
|
|
839 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
840 | ecb_cold static void |
|
|
841 | err_set (dec_t *dec, const char *reason) |
|
|
842 | { |
|
|
843 | if (!dec->err) |
|
|
844 | dec->err = reason; |
|
|
845 | } |
|
|
846 | |
|
|
847 | ecb_cold static void |
|
|
848 | err_unexpected_end (dec_t *dec) |
|
|
849 | { |
|
|
850 | err_set (dec, "unexpected end of CBOR data"); |
|
|
851 | } |
|
|
852 | |
|
|
853 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
854 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
855 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
856 | |
|
|
857 | #define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec)) |
|
|
858 | |
621 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
859 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
622 | #define DEC_DEC_DEPTH --dec->depth |
860 | #define DEC_DEC_DEPTH --dec->depth |
623 | |
861 | |
624 | static UV |
862 | static UV |
625 | decode_uint (dec_t *dec) |
863 | decode_uint (dec_t *dec) |
626 | { |
864 | { |
… | |
… | |
692 | |
930 | |
693 | for (;;) |
931 | for (;;) |
694 | { |
932 | { |
695 | WANT (1); |
933 | WANT (1); |
696 | |
934 | |
697 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
935 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
698 | { |
936 | { |
699 | ++dec->cur; |
937 | ++dec->cur; |
700 | break; |
938 | break; |
701 | } |
939 | } |
702 | |
940 | |
703 | av_push (av, decode_sv (dec)); |
941 | av_push (av, decode_sv (dec)); |
704 | } |
942 | } |
705 | } |
943 | } |
706 | else |
944 | else |
707 | { |
945 | { |
708 | int i, len = decode_uint (dec); |
946 | UV i, len = decode_uint (dec); |
709 | |
947 | |
710 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
948 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
711 | av_fill (av, len - 1); |
949 | av_fill (av, len - 1); |
712 | |
950 | |
713 | for (i = 0; i < len; ++i) |
951 | for (i = 0; i < len; ++i) |
… | |
… | |
716 | |
954 | |
717 | DEC_DEC_DEPTH; |
955 | DEC_DEC_DEPTH; |
718 | return newRV_noinc ((SV *)av); |
956 | return newRV_noinc ((SV *)av); |
719 | |
957 | |
720 | fail: |
958 | fail: |
721 | SvREFCNT_dec (av); |
959 | SvREFCNT_dec_NN (av); |
722 | DEC_DEC_DEPTH; |
960 | DEC_DEC_DEPTH; |
723 | return &PL_sv_undef; |
961 | return &PL_sv_undef; |
724 | } |
962 | } |
725 | |
963 | |
726 | static void |
964 | static void |
… | |
… | |
728 | { |
966 | { |
729 | // for speed reasons, we specialcase single-string |
967 | // for speed reasons, we specialcase single-string |
730 | // byte or utf-8 strings as keys, but only when !stringref |
968 | // byte or utf-8 strings as keys, but only when !stringref |
731 | |
969 | |
732 | if (ecb_expect_true (!dec->stringref)) |
970 | if (ecb_expect_true (!dec->stringref)) |
733 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
971 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
734 | { |
972 | { |
735 | I32 len = decode_uint (dec); |
973 | STRLEN len = decode_uint (dec); |
736 | char *key = (char *)dec->cur; |
974 | char *key = (char *)dec->cur; |
737 | |
975 | |
|
|
976 | WANT (len); |
738 | dec->cur += len; |
977 | dec->cur += len; |
739 | |
978 | |
740 | if (ecb_expect_false (dec->stringref)) |
|
|
741 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
742 | |
|
|
743 | hv_store (hv, key, len, decode_sv (dec), 0); |
979 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | |
980 | |
745 | return; |
981 | return; |
746 | } |
982 | } |
747 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
983 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
748 | { |
984 | { |
749 | I32 len = decode_uint (dec); |
985 | STRLEN len = decode_uint (dec); |
750 | char *key = (char *)dec->cur; |
986 | char *key = (char *)dec->cur; |
751 | |
987 | |
|
|
988 | WANT (len); |
752 | dec->cur += len; |
989 | dec->cur += len; |
753 | |
990 | |
754 | if (ecb_expect_false (dec->stringref)) |
991 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
755 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
992 | if (!is_utf8_string (key, len)) |
|
|
993 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
756 | |
994 | |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
995 | hv_store (hv, key, -len, decode_sv (dec), 0); |
758 | |
996 | |
759 | return; |
997 | return; |
760 | } |
998 | } |
761 | |
999 | |
762 | SV *k = decode_sv (dec); |
1000 | SV *k = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
1001 | SV *v = decode_sv (dec); |
764 | |
1002 | |
|
|
1003 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
1004 | // methods, and this is hopefully the only place where it can happen, |
|
|
1005 | // so if there is a chance of an exception, take the very slow path. |
|
|
1006 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
1007 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
1008 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
1009 | { |
|
|
1010 | dSP; |
|
|
1011 | |
|
|
1012 | ENTER; SAVETMPS; |
|
|
1013 | PUSHMARK (SP); |
|
|
1014 | EXTEND (SP, 3); |
|
|
1015 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
1016 | PUSHs (sv_2mortal (k)); |
|
|
1017 | PUSHs (sv_2mortal (v)); |
|
|
1018 | |
|
|
1019 | PUTBACK; |
|
|
1020 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
1021 | SPAGAIN; |
|
|
1022 | |
|
|
1023 | FREETMPS; LEAVE; |
|
|
1024 | |
|
|
1025 | if (SvTRUE (ERRSV)) |
|
|
1026 | ERR_ERRSV; |
|
|
1027 | |
|
|
1028 | return; |
|
|
1029 | } |
|
|
1030 | |
765 | hv_store_ent (hv, k, v, 0); |
1031 | hv_store_ent (hv, k, v, 0); |
766 | SvREFCNT_dec (k); |
1032 | SvREFCNT_dec_NN (k); |
|
|
1033 | |
|
|
1034 | fail: |
|
|
1035 | ; |
767 | } |
1036 | } |
768 | |
1037 | |
769 | static SV * |
1038 | static SV * |
770 | decode_hv (dec_t *dec) |
1039 | decode_hv (dec_t *dec) |
771 | { |
1040 | { |
… | |
… | |
779 | |
1048 | |
780 | for (;;) |
1049 | for (;;) |
781 | { |
1050 | { |
782 | WANT (1); |
1051 | WANT (1); |
783 | |
1052 | |
784 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
1053 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err) |
785 | { |
1054 | { |
786 | ++dec->cur; |
1055 | ++dec->cur; |
787 | break; |
1056 | break; |
788 | } |
1057 | } |
789 | |
1058 | |
790 | decode_he (dec, hv); |
1059 | decode_he (dec, hv); |
791 | } |
1060 | } |
792 | } |
1061 | } |
793 | else |
1062 | else |
794 | { |
1063 | { |
795 | int pairs = decode_uint (dec); |
1064 | UV pairs = decode_uint (dec); |
|
|
1065 | |
|
|
1066 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
796 | |
1067 | |
797 | while (pairs--) |
1068 | while (pairs--) |
798 | decode_he (dec, hv); |
1069 | decode_he (dec, hv); |
799 | } |
1070 | } |
800 | |
1071 | |
801 | DEC_DEC_DEPTH; |
1072 | DEC_DEC_DEPTH; |
802 | return newRV_noinc ((SV *)hv); |
1073 | return newRV_noinc ((SV *)hv); |
803 | |
1074 | |
804 | fail: |
1075 | fail: |
805 | SvREFCNT_dec (hv); |
1076 | SvREFCNT_dec_NN (hv); |
806 | DEC_DEC_DEPTH; |
1077 | DEC_DEC_DEPTH; |
807 | return &PL_sv_undef; |
1078 | return &PL_sv_undef; |
808 | } |
1079 | } |
809 | |
1080 | |
810 | static SV * |
1081 | static SV * |
811 | decode_str (dec_t *dec, int utf8) |
1082 | decode_str (dec_t *dec, int utf8) |
812 | { |
1083 | { |
813 | SV *sv = 0; |
1084 | SV *sv = 0; |
814 | |
1085 | |
815 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
1086 | if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF)) |
816 | { |
1087 | { |
817 | // indefinite length strings |
1088 | // indefinite length strings |
818 | ++dec->cur; |
1089 | ++dec->cur; |
819 | |
1090 | |
820 | U8 major = *dec->cur & MAJOR_MISC; |
1091 | U8 major = *dec->cur & MAJOR_MISC; |
… | |
… | |
853 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
1124 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
854 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
1125 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
855 | } |
1126 | } |
856 | |
1127 | |
857 | if (utf8) |
1128 | if (utf8) |
|
|
1129 | { |
|
|
1130 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
1131 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
1132 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
1133 | |
858 | SvUTF8_on (sv); |
1134 | SvUTF8_on (sv); |
|
|
1135 | } |
859 | |
1136 | |
860 | return sv; |
1137 | return sv; |
861 | |
1138 | |
862 | fail: |
1139 | fail: |
863 | SvREFCNT_dec (sv); |
1140 | SvREFCNT_dec (sv); |
… | |
… | |
882 | sv = newRV_noinc (decode_sv (dec)); |
1159 | sv = newRV_noinc (decode_sv (dec)); |
883 | break; |
1160 | break; |
884 | |
1161 | |
885 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1162 | case CBOR_TAG_STRINGREF_NAMESPACE: |
886 | { |
1163 | { |
|
|
1164 | // do not use SAVETMPS/FREETMPS, as these will |
|
|
1165 | // erase mortalised caches, e.g. "shareable" |
887 | ENTER; SAVETMPS; |
1166 | ENTER; |
888 | |
1167 | |
889 | SAVESPTR (dec->stringref); |
1168 | SAVESPTR (dec->stringref); |
890 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1169 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
891 | |
1170 | |
892 | sv = decode_sv (dec); |
1171 | sv = decode_sv (dec); |
893 | |
1172 | |
894 | FREETMPS; LEAVE; |
1173 | LEAVE; |
895 | } |
1174 | } |
896 | break; |
1175 | break; |
897 | |
1176 | |
898 | case CBOR_TAG_STRINGREF: |
1177 | case CBOR_TAG_STRINGREF: |
899 | { |
1178 | { |
900 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1179 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
901 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
1180 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
902 | |
1181 | |
903 | UV idx = decode_uint (dec); |
1182 | UV idx = decode_uint (dec); |
904 | |
1183 | |
905 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
1184 | if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref))) |
906 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
1185 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
907 | |
1186 | |
908 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
1187 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
909 | } |
1188 | } |
910 | break; |
1189 | break; |
… | |
… | |
912 | case CBOR_TAG_VALUE_SHAREABLE: |
1191 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | { |
1192 | { |
914 | if (ecb_expect_false (!dec->shareable)) |
1193 | if (ecb_expect_false (!dec->shareable)) |
915 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
1194 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | |
1195 | |
|
|
1196 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
1197 | { |
917 | sv = newSV (0); |
1198 | sv = newSV (0); |
918 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
1199 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
919 | |
1200 | |
920 | SV *osv = decode_sv (dec); |
1201 | SV *osv = decode_sv (dec); |
921 | sv_setsv (sv, osv); |
1202 | sv_setsv (sv, osv); |
922 | SvREFCNT_dec_NN (osv); |
1203 | SvREFCNT_dec_NN (osv); |
|
|
1204 | } |
|
|
1205 | else |
|
|
1206 | { |
|
|
1207 | av_push (dec->shareable, &PL_sv_undef); |
|
|
1208 | int idx = AvFILLp (dec->shareable); |
|
|
1209 | sv = decode_sv (dec); |
|
|
1210 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
1211 | } |
923 | } |
1212 | } |
924 | break; |
1213 | break; |
925 | |
1214 | |
926 | case CBOR_TAG_VALUE_SHAREDREF: |
1215 | case CBOR_TAG_VALUE_SHAREDREF: |
927 | { |
1216 | { |
928 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
1217 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
929 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
1218 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
930 | |
1219 | |
931 | UV idx = decode_uint (dec); |
1220 | UV idx = decode_uint (dec); |
932 | |
1221 | |
933 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
1222 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
934 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1223 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
935 | |
1224 | |
936 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1225 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
1226 | |
|
|
1227 | if (sv == &PL_sv_undef) |
|
|
1228 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
937 | } |
1229 | } |
938 | break; |
1230 | break; |
939 | |
1231 | |
940 | case CBOR_TAG_PERL_OBJECT: |
1232 | case CBOR_TAG_PERL_OBJECT: |
941 | { |
1233 | { |
|
|
1234 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1235 | goto filter; |
|
|
1236 | |
942 | sv = decode_sv (dec); |
1237 | sv = decode_sv (dec); |
943 | |
1238 | |
944 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1239 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
945 | ERR ("corrupted CBOR data (non-array perl object)"); |
1240 | ERR ("corrupted CBOR data (non-array perl object)"); |
946 | |
1241 | |
… | |
… | |
956 | if (!method) |
1251 | if (!method) |
957 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1252 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
958 | |
1253 | |
959 | dSP; |
1254 | dSP; |
960 | |
1255 | |
961 | ENTER; SAVETMPS; PUSHMARK (SP); |
1256 | ENTER; SAVETMPS; |
|
|
1257 | PUSHMARK (SP); |
962 | EXTEND (SP, len + 1); |
1258 | EXTEND (SP, len + 1); |
963 | // we re-bless the reference to get overload and other niceties right |
1259 | // we re-bless the reference to get overload and other niceties right |
964 | PUSHs (*av_fetch (av, 0, 1)); |
1260 | PUSHs (*av_fetch (av, 0, 1)); |
965 | PUSHs (sv_cbor); |
1261 | PUSHs (sv_cbor); |
966 | |
1262 | |
… | |
… | |
974 | SPAGAIN; |
1270 | SPAGAIN; |
975 | |
1271 | |
976 | if (SvTRUE (ERRSV)) |
1272 | if (SvTRUE (ERRSV)) |
977 | { |
1273 | { |
978 | FREETMPS; LEAVE; |
1274 | FREETMPS; LEAVE; |
979 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1275 | ERR_ERRSV; |
980 | } |
1276 | } |
981 | |
1277 | |
982 | SvREFCNT_dec (sv); |
1278 | SvREFCNT_dec_NN (sv); |
983 | sv = SvREFCNT_inc (POPs); |
1279 | sv = SvREFCNT_inc (POPs); |
984 | |
1280 | |
985 | PUTBACK; |
1281 | PUTBACK; |
986 | |
1282 | |
987 | FREETMPS; LEAVE; |
1283 | FREETMPS; LEAVE; |
988 | } |
1284 | } |
989 | break; |
1285 | break; |
990 | |
1286 | |
991 | default: |
1287 | default: |
|
|
1288 | filter: |
992 | { |
1289 | { |
|
|
1290 | SV *tag_sv = newSVuv (tag); |
|
|
1291 | |
993 | sv = decode_sv (dec); |
1292 | sv = decode_sv (dec); |
994 | |
1293 | |
995 | dSP; |
1294 | dSP; |
996 | ENTER; SAVETMPS; PUSHMARK (SP); |
1295 | ENTER; SAVETMPS; |
|
|
1296 | PUSHMARK (SP); |
997 | EXTEND (SP, 2); |
1297 | EXTEND (SP, 2); |
998 | PUSHs (newSVuv (tag)); |
1298 | PUSHs (tag_sv); |
999 | PUSHs (sv); |
1299 | PUSHs (sv); |
1000 | |
1300 | |
1001 | PUTBACK; |
1301 | PUTBACK; |
1002 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1302 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1003 | SPAGAIN; |
1303 | SPAGAIN; |
1004 | |
1304 | |
1005 | if (SvTRUE (ERRSV)) |
1305 | if (SvTRUE (ERRSV)) |
1006 | { |
1306 | { |
|
|
1307 | SvREFCNT_dec_NN (tag_sv); |
1007 | FREETMPS; LEAVE; |
1308 | FREETMPS; LEAVE; |
1008 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1309 | ERR_ERRSV; |
1009 | } |
1310 | } |
1010 | |
1311 | |
1011 | if (count) |
1312 | if (count) |
1012 | { |
1313 | { |
|
|
1314 | SvREFCNT_dec_NN (tag_sv); |
1013 | SvREFCNT_dec (sv); |
1315 | SvREFCNT_dec_NN (sv); |
1014 | sv = SvREFCNT_inc (POPs); |
1316 | sv = SvREFCNT_inc_NN (TOPs); |
|
|
1317 | SP -= count; |
1015 | } |
1318 | } |
1016 | else |
1319 | else |
1017 | { |
1320 | { |
1018 | AV *av = newAV (); |
1321 | AV *av = newAV (); |
1019 | av_push (av, newSVuv (tag)); |
1322 | av_push (av, tag_sv); |
1020 | av_push (av, sv); |
1323 | av_push (av, sv); |
1021 | |
1324 | |
1022 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1325 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1023 | ? cbor_tagged_stash |
1326 | ? cbor_tagged_stash |
1024 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1327 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
… | |
… | |
1110 | |
1413 | |
1111 | return newSVnv (ecb_binary64_to_double (fp)); |
1414 | return newSVnv (ecb_binary64_to_double (fp)); |
1112 | } |
1415 | } |
1113 | |
1416 | |
1114 | // 0..19 unassigned simple |
1417 | // 0..19 unassigned simple |
1115 | // 24 reserved + unassigned (reserved values are not encodable) |
1418 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1419 | // 28-30 unassigned misc |
|
|
1420 | // 31 break code |
1116 | default: |
1421 | default: |
1117 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1422 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1118 | } |
1423 | } |
1119 | |
1424 | |
1120 | break; |
1425 | break; |
1121 | } |
1426 | } |
1122 | |
1427 | |
… | |
… | |
1125 | } |
1430 | } |
1126 | |
1431 | |
1127 | static SV * |
1432 | static SV * |
1128 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1433 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1129 | { |
1434 | { |
1130 | dec_t dec = { }; |
1435 | dec_t dec = { 0 }; |
1131 | SV *sv; |
1436 | SV *sv; |
1132 | STRLEN len; |
1437 | STRLEN len; |
1133 | char *data = SvPVbyte (string, len); |
1438 | char *data = SvPVbyte (string, len); |
1134 | |
1439 | |
1135 | if (len > cbor->max_size && cbor->max_size) |
1440 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1149 | if (dec.cur != dec.end && !dec.err) |
1454 | if (dec.cur != dec.end && !dec.err) |
1150 | dec.err = "garbage after CBOR object"; |
1455 | dec.err = "garbage after CBOR object"; |
1151 | |
1456 | |
1152 | if (dec.err) |
1457 | if (dec.err) |
1153 | { |
1458 | { |
|
|
1459 | if (dec.shareable) |
|
|
1460 | { |
|
|
1461 | // need to break cyclic links, which would all be in shareable |
|
|
1462 | int i; |
|
|
1463 | SV **svp; |
|
|
1464 | |
|
|
1465 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1466 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1467 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1468 | } |
|
|
1469 | |
1154 | SvREFCNT_dec (sv); |
1470 | SvREFCNT_dec_NN (sv); |
|
|
1471 | |
|
|
1472 | if (dec.err_sv) |
|
|
1473 | sv_2mortal (dec.err_sv); |
|
|
1474 | |
1155 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1475 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1156 | } |
1476 | } |
1157 | |
1477 | |
1158 | sv = sv_2mortal (sv); |
1478 | sv = sv_2mortal (sv); |
1159 | |
1479 | |
1160 | return sv; |
1480 | return sv; |
1161 | } |
1481 | } |
1162 | |
1482 | |
|
|
1483 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1484 | // incremental parser |
|
|
1485 | |
|
|
1486 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1487 | |
|
|
1488 | // returns 0 for notyet, 1 for success or error |
|
|
1489 | static int |
|
|
1490 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1491 | { |
|
|
1492 | STRLEN cur; |
|
|
1493 | SvPV (cborstr, cur); |
|
|
1494 | |
|
|
1495 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1496 | { |
|
|
1497 | // table of integer count bytes |
|
|
1498 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1499 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1500 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1501 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1502 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1503 | }; |
|
|
1504 | |
|
|
1505 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1506 | U8 m = *p & MINOR_MASK; |
|
|
1507 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1508 | I8 ilen = incr_len[m]; |
|
|
1509 | |
|
|
1510 | self->incr_need = self->incr_pos + 1; |
|
|
1511 | |
|
|
1512 | if (ecb_expect_false (ilen < 0)) |
|
|
1513 | { |
|
|
1514 | if (m != MINOR_INDEF) |
|
|
1515 | return 1; // error |
|
|
1516 | |
|
|
1517 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1518 | { |
|
|
1519 | if (count >= 0) |
|
|
1520 | return 1; // error |
|
|
1521 | |
|
|
1522 | count = 1; |
|
|
1523 | } |
|
|
1524 | else |
|
|
1525 | { |
|
|
1526 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1527 | count = -1; |
|
|
1528 | } |
|
|
1529 | } |
|
|
1530 | else |
|
|
1531 | { |
|
|
1532 | self->incr_need += ilen; |
|
|
1533 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1534 | return 0; |
|
|
1535 | |
|
|
1536 | int major = *p >> MAJOR_SHIFT; |
|
|
1537 | |
|
|
1538 | switch (major) |
|
|
1539 | { |
|
|
1540 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1541 | ++count; // tags merely prefix another value |
|
|
1542 | break; |
|
|
1543 | |
|
|
1544 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1545 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1546 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1547 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1548 | { |
|
|
1549 | UV len; |
|
|
1550 | |
|
|
1551 | if (ecb_expect_false (ilen)) |
|
|
1552 | { |
|
|
1553 | len = 0; |
|
|
1554 | |
|
|
1555 | do { |
|
|
1556 | len = (len << 8) | *++p; |
|
|
1557 | } while (--ilen); |
|
|
1558 | } |
|
|
1559 | else |
|
|
1560 | len = m; |
|
|
1561 | |
|
|
1562 | switch (major) |
|
|
1563 | { |
|
|
1564 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1565 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1566 | self->incr_need += len; |
|
|
1567 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1568 | return 0; |
|
|
1569 | |
|
|
1570 | break; |
|
|
1571 | |
|
|
1572 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1573 | len <<= 1; |
|
|
1574 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1575 | if (len) |
|
|
1576 | { |
|
|
1577 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1578 | count = len + 1; |
|
|
1579 | } |
|
|
1580 | break; |
|
|
1581 | } |
|
|
1582 | } |
|
|
1583 | } |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | self->incr_pos = self->incr_need; |
|
|
1587 | |
|
|
1588 | if (count > 0) |
|
|
1589 | { |
|
|
1590 | while (!--count) |
|
|
1591 | { |
|
|
1592 | if (!AvFILLp (self->incr_count)) |
|
|
1593 | return 1; // done |
|
|
1594 | |
|
|
1595 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1596 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1600 | } |
|
|
1601 | } |
|
|
1602 | |
|
|
1603 | return 0; |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | |
1163 | ///////////////////////////////////////////////////////////////////////////// |
1607 | ///////////////////////////////////////////////////////////////////////////// |
1164 | // XS interface functions |
1608 | // XS interface functions |
1165 | |
1609 | |
1166 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1610 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1167 | |
1611 | |
… | |
… | |
1179 | |
1623 | |
1180 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1624 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1181 | |
1625 | |
1182 | sv_cbor = newSVpv ("CBOR", 0); |
1626 | sv_cbor = newSVpv ("CBOR", 0); |
1183 | SvREADONLY_on (sv_cbor); |
1627 | SvREADONLY_on (sv_cbor); |
|
|
1628 | |
|
|
1629 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1184 | } |
1630 | } |
1185 | |
1631 | |
1186 | PROTOTYPES: DISABLE |
1632 | PROTOTYPES: DISABLE |
1187 | |
1633 | |
1188 | void CLONE (...) |
1634 | void CLONE (...) |
… | |
… | |
1207 | void shrink (CBOR *self, int enable = 1) |
1653 | void shrink (CBOR *self, int enable = 1) |
1208 | ALIAS: |
1654 | ALIAS: |
1209 | shrink = F_SHRINK |
1655 | shrink = F_SHRINK |
1210 | allow_unknown = F_ALLOW_UNKNOWN |
1656 | allow_unknown = F_ALLOW_UNKNOWN |
1211 | allow_sharing = F_ALLOW_SHARING |
1657 | allow_sharing = F_ALLOW_SHARING |
|
|
1658 | allow_cycles = F_ALLOW_CYCLES |
|
|
1659 | forbid_objects = F_FORBID_OBJECTS |
1212 | pack_strings = F_PACK_STRINGS |
1660 | pack_strings = F_PACK_STRINGS |
|
|
1661 | text_keys = F_TEXT_KEYS |
|
|
1662 | text_strings = F_TEXT_STRINGS |
|
|
1663 | validate_utf8 = F_VALIDATE_UTF8 |
1213 | PPCODE: |
1664 | PPCODE: |
1214 | { |
1665 | { |
1215 | if (enable) |
1666 | if (enable) |
1216 | self->flags |= ix; |
1667 | self->flags |= ix; |
1217 | else |
1668 | else |
… | |
… | |
1223 | void get_shrink (CBOR *self) |
1674 | void get_shrink (CBOR *self) |
1224 | ALIAS: |
1675 | ALIAS: |
1225 | get_shrink = F_SHRINK |
1676 | get_shrink = F_SHRINK |
1226 | get_allow_unknown = F_ALLOW_UNKNOWN |
1677 | get_allow_unknown = F_ALLOW_UNKNOWN |
1227 | get_allow_sharing = F_ALLOW_SHARING |
1678 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1679 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1680 | get_forbid_objects = F_FORBID_OBJECTS |
1228 | get_pack_strings = F_PACK_STRINGS |
1681 | get_pack_strings = F_PACK_STRINGS |
|
|
1682 | get_text_keys = F_TEXT_KEYS |
|
|
1683 | get_text_strings = F_TEXT_STRINGS |
|
|
1684 | get_validate_utf8 = F_VALIDATE_UTF8 |
1229 | PPCODE: |
1685 | PPCODE: |
1230 | XPUSHs (boolSV (self->flags & ix)); |
1686 | XPUSHs (boolSV (self->flags & ix)); |
1231 | |
1687 | |
1232 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1688 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1233 | PPCODE: |
1689 | PPCODE: |
… | |
… | |
1282 | EXTEND (SP, 2); |
1738 | EXTEND (SP, 2); |
1283 | PUSHs (sv); |
1739 | PUSHs (sv); |
1284 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1740 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1285 | } |
1741 | } |
1286 | |
1742 | |
|
|
1743 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1744 | ALIAS: |
|
|
1745 | incr_parse_multiple = 1 |
|
|
1746 | PPCODE: |
|
|
1747 | { |
|
|
1748 | if (SvUTF8 (cborstr)) |
|
|
1749 | sv_utf8_downgrade (cborstr, 0); |
|
|
1750 | |
|
|
1751 | if (!self->incr_count) |
|
|
1752 | { |
|
|
1753 | self->incr_count = newAV (); |
|
|
1754 | self->incr_pos = 0; |
|
|
1755 | self->incr_need = 1; |
|
|
1756 | |
|
|
1757 | av_push (self->incr_count, newSViv (1)); |
|
|
1758 | } |
|
|
1759 | |
|
|
1760 | do |
|
|
1761 | { |
|
|
1762 | if (!incr_parse (self, cborstr)) |
|
|
1763 | { |
|
|
1764 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1765 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1766 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1767 | |
|
|
1768 | break; |
|
|
1769 | } |
|
|
1770 | |
|
|
1771 | SV *sv; |
|
|
1772 | char *offset; |
|
|
1773 | |
|
|
1774 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1775 | XPUSHs (sv); |
|
|
1776 | |
|
|
1777 | sv_chop (cborstr, offset); |
|
|
1778 | |
|
|
1779 | av_clear (self->incr_count); |
|
|
1780 | av_push (self->incr_count, newSViv (1)); |
|
|
1781 | |
|
|
1782 | self->incr_pos = 0; |
|
|
1783 | self->incr_need = self->incr_pos + 1; |
|
|
1784 | } |
|
|
1785 | while (ix); |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | void incr_reset (CBOR *self) |
|
|
1789 | CODE: |
|
|
1790 | { |
|
|
1791 | SvREFCNT_dec (self->incr_count); |
|
|
1792 | self->incr_count = 0; |
|
|
1793 | } |
|
|
1794 | |
1287 | void DESTROY (CBOR *self) |
1795 | void DESTROY (CBOR *self) |
1288 | PPCODE: |
1796 | PPCODE: |
1289 | cbor_free (self); |
1797 | cbor_free (self); |
1290 | |
1798 | |
1291 | PROTOTYPES: ENABLE |
1799 | PROTOTYPES: ENABLE |
… | |
… | |
1310 | cbor_init (&cbor); |
1818 | cbor_init (&cbor); |
1311 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1819 | PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
1312 | XPUSHs (cborstr); |
1820 | XPUSHs (cborstr); |
1313 | } |
1821 | } |
1314 | |
1822 | |
|
|
1823 | #ifdef __AFL_COMPILER |
|
|
1824 | |
|
|
1825 | void |
|
|
1826 | afl_init () |
|
|
1827 | CODE: |
|
|
1828 | __AFL_INIT (); |
|
|
1829 | |
|
|
1830 | int |
|
|
1831 | afl_loop (unsigned int count = 10000) |
|
|
1832 | CODE: |
|
|
1833 | RETVAL = __AFL_LOOP (count); |
|
|
1834 | OUTPUT: |
|
|
1835 | RETVAL |
|
|
1836 | |
|
|
1837 | #endif |
|
|
1838 | |