… | |
… | |
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)) |
… | |
… | |
98 | |
100 | |
99 | #define F_SHRINK 0x00000001UL |
101 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
105 | #define F_FORBID_OBJECTS 0x00000010UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
106 | #define F_PACK_STRINGS 0x00000020UL |
|
|
107 | #define F_TEXT_KEYS 0x00000040UL |
|
|
108 | #define F_TEXT_STRINGS 0x00000080UL |
104 | #define F_VALIDATE_UTF8 0x00000020UL |
109 | #define F_VALIDATE_UTF8 0x00000100UL |
105 | |
110 | |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
111 | #define INIT_SIZE 32 // initial scalar size to be allocated |
107 | |
112 | |
108 | #define SB do { |
113 | #define SB do { |
109 | #define SE } while (0) |
114 | #define SE } while (0) |
… | |
… | |
182 | |
187 | |
183 | // minimum length of a string to be registered for stringref |
188 | // minimum length of a string to be registered for stringref |
184 | ecb_inline int |
189 | ecb_inline int |
185 | minimum_string_length (UV idx) |
190 | minimum_string_length (UV idx) |
186 | { |
191 | { |
187 | return idx > 23 |
192 | return idx <= 23 ? 3 |
188 | ? idx > 0xffU |
193 | : idx <= 0xffU ? 4 |
189 | ? idx > 0xffffU |
194 | : idx <= 0xffffU ? 5 |
190 | ? idx > 0xffffffffU |
195 | : idx <= 0xffffffffU ? 7 |
191 | ? 11 |
196 | : 11; |
192 | : 7 |
|
|
193 | : 5 |
|
|
194 | : 4 |
|
|
195 | : 3; |
|
|
196 | } |
197 | } |
197 | |
198 | |
198 | ///////////////////////////////////////////////////////////////////////////// |
199 | ///////////////////////////////////////////////////////////////////////////// |
199 | // encoder |
200 | // encoder |
200 | |
201 | |
… | |
… | |
213 | } enc_t; |
214 | } enc_t; |
214 | |
215 | |
215 | ecb_inline void |
216 | ecb_inline void |
216 | need (enc_t *enc, STRLEN len) |
217 | need (enc_t *enc, STRLEN len) |
217 | { |
218 | { |
218 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
219 | if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
219 | { |
220 | { |
220 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
221 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
221 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
222 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
222 | enc->cur = SvPVX (enc->sv) + cur; |
223 | enc->cur = SvPVX (enc->sv) + cur; |
223 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
224 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
… | |
… | |
275 | encode_tag (enc_t *enc, UV tag) |
276 | encode_tag (enc_t *enc, UV tag) |
276 | { |
277 | { |
277 | encode_uint (enc, MAJOR_TAG, tag); |
278 | encode_uint (enc, MAJOR_TAG, tag); |
278 | } |
279 | } |
279 | |
280 | |
|
|
281 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
282 | ecb_noinline static void |
|
|
283 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
284 | { |
|
|
285 | STRLEN ulen = len; |
|
|
286 | U8 *p, *pend = (U8 *)str + len; |
|
|
287 | |
|
|
288 | for (p = (U8 *)str; p < pend; ++p) |
|
|
289 | ulen += *p >> 7; // count set high bits |
|
|
290 | |
|
|
291 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
292 | |
|
|
293 | need (enc, ulen); |
|
|
294 | for (p = (U8 *)str; p < pend; ++p) |
|
|
295 | if (*p < 0x80) |
|
|
296 | *enc->cur++ = *p; |
|
|
297 | else |
|
|
298 | { |
|
|
299 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
300 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
280 | ecb_inline void |
304 | ecb_inline void |
281 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
305 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
282 | { |
306 | { |
|
|
307 | if (ecb_expect_false (upgrade_utf8)) |
|
|
308 | if (!utf8) |
|
|
309 | { |
|
|
310 | encode_str_utf8 (enc, utf8, str, len); |
|
|
311 | return; |
|
|
312 | } |
|
|
313 | |
283 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
314 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
284 | need (enc, len); |
315 | need (enc, len); |
285 | memcpy (enc->cur, str, len); |
316 | memcpy (enc->cur, str, len); |
286 | enc->cur += len; |
317 | enc->cur += len; |
287 | } |
318 | } |
288 | |
319 | |
289 | static void |
320 | ecb_inline void |
290 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
321 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
291 | { |
322 | { |
292 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
323 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
293 | { |
324 | { |
294 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
325 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
295 | |
326 | |
… | |
… | |
306 | sv_setuv (*svp, enc->stringref_idx); |
337 | sv_setuv (*svp, enc->stringref_idx); |
307 | ++enc->stringref_idx; |
338 | ++enc->stringref_idx; |
308 | } |
339 | } |
309 | } |
340 | } |
310 | |
341 | |
311 | encode_str (enc, utf8, str, len); |
342 | encode_str (enc, upgrade_utf8, utf8, str, len); |
312 | } |
343 | } |
313 | |
344 | |
314 | static void encode_sv (enc_t *enc, SV *sv); |
345 | static void encode_sv (enc_t *enc, SV *sv); |
315 | |
346 | |
316 | static void |
347 | static void |
… | |
… | |
323 | |
354 | |
324 | ++enc->depth; |
355 | ++enc->depth; |
325 | |
356 | |
326 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
357 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | |
358 | |
|
|
359 | if (SvMAGICAL (av)) |
328 | for (i = 0; i <= len; ++i) |
360 | for (i = 0; i <= len; ++i) |
329 | { |
361 | { |
330 | SV **svp = av_fetch (av, i, 0); |
362 | SV **svp = av_fetch (av, i, 0); |
331 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
363 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
332 | } |
364 | } |
|
|
365 | else |
|
|
366 | for (i = 0; i <= len; ++i) |
|
|
367 | { |
|
|
368 | SV *sv = AvARRAY (av)[i]; |
|
|
369 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
370 | } |
333 | |
371 | |
334 | --enc->depth; |
372 | --enc->depth; |
335 | } |
373 | } |
336 | |
374 | |
337 | static void |
375 | static void |
… | |
… | |
355 | while ((he = hv_iternext (hv))) |
393 | while ((he = hv_iternext (hv))) |
356 | { |
394 | { |
357 | if (HeKLEN (he) == HEf_SVKEY) |
395 | if (HeKLEN (he) == HEf_SVKEY) |
358 | encode_sv (enc, HeSVKEY (he)); |
396 | encode_sv (enc, HeSVKEY (he)); |
359 | else |
397 | else |
360 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
398 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
361 | |
399 | |
362 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
400 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
363 | } |
401 | } |
364 | |
402 | |
365 | if (mg) |
403 | if (mg) |
… | |
… | |
437 | if (ecb_expect_false (SvOBJECT (sv))) |
475 | if (ecb_expect_false (SvOBJECT (sv))) |
438 | { |
476 | { |
439 | HV *stash = SvSTASH (sv); |
477 | HV *stash = SvSTASH (sv); |
440 | GV *method; |
478 | GV *method; |
441 | |
479 | |
|
|
480 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
481 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
482 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
442 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
483 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
443 | { |
484 | { |
444 | dSP; |
485 | dSP; |
445 | |
486 | |
446 | ENTER; SAVETMPS; PUSHMARK (SP); |
487 | ENTER; SAVETMPS; |
|
|
488 | PUSHMARK (SP); |
447 | // we re-bless the reference to get overload and other niceties right |
489 | // we re-bless the reference to get overload and other niceties right |
448 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
490 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
449 | |
491 | |
450 | PUTBACK; |
492 | PUTBACK; |
451 | // G_SCALAR ensures that return value is 1 |
493 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
464 | } |
506 | } |
465 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
507 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
466 | { |
508 | { |
467 | dSP; |
509 | dSP; |
468 | |
510 | |
469 | ENTER; SAVETMPS; PUSHMARK (SP); |
511 | ENTER; SAVETMPS; |
|
|
512 | SAVESTACK_POS (); |
|
|
513 | PUSHMARK (SP); |
470 | EXTEND (SP, 2); |
514 | EXTEND (SP, 2); |
471 | // we re-bless the reference to get overload and other niceties right |
515 | // we re-bless the reference to get overload and other niceties right |
472 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
516 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
473 | PUSHs (sv_cbor); |
517 | PUSHs (sv_cbor); |
474 | |
518 | |
… | |
… | |
480 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
524 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
481 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
525 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
482 | |
526 | |
483 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
527 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
484 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
528 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
485 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
529 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
486 | |
530 | |
487 | while (count) |
531 | while (count) |
488 | encode_sv (enc, SP[1 - count--]); |
532 | encode_sv (enc, SP[1 - count--]); |
489 | |
533 | |
490 | PUTBACK; |
534 | PUTBACK; |
… | |
… | |
549 | |
593 | |
550 | if (SvPOKp (sv)) |
594 | if (SvPOKp (sv)) |
551 | { |
595 | { |
552 | STRLEN len; |
596 | STRLEN len; |
553 | char *str = SvPV (sv, len); |
597 | char *str = SvPV (sv, len); |
554 | encode_strref (enc, SvUTF8 (sv), str, len); |
598 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
555 | } |
599 | } |
556 | else if (SvNOKp (sv)) |
600 | else if (SvNOKp (sv)) |
557 | encode_nv (enc, sv); |
601 | encode_nv (enc, sv); |
558 | else if (SvIOKp (sv)) |
602 | else if (SvIOKp (sv)) |
559 | { |
603 | { |
… | |
… | |
576 | } |
620 | } |
577 | |
621 | |
578 | static SV * |
622 | static SV * |
579 | encode_cbor (SV *scalar, CBOR *cbor) |
623 | encode_cbor (SV *scalar, CBOR *cbor) |
580 | { |
624 | { |
581 | enc_t enc = { }; |
625 | enc_t enc = { 0 }; |
582 | |
626 | |
583 | enc.cbor = *cbor; |
627 | enc.cbor = *cbor; |
584 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
585 | enc.cur = SvPVX (enc.sv); |
629 | enc.cur = SvPVX (enc.sv); |
586 | enc.end = SvEND (enc.sv); |
630 | enc.end = SvEND (enc.sv); |
… | |
… | |
618 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
619 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
620 | AV *shareable; |
664 | AV *shareable; |
621 | AV *stringref; |
665 | AV *stringref; |
622 | SV *decode_tagged; |
666 | SV *decode_tagged; |
|
|
667 | SV *err_sv; // optional sv for error, needs to be freed |
623 | } dec_t; |
668 | } dec_t; |
624 | |
669 | |
625 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
670 | // set dec->err to ERRSV |
|
|
671 | ecb_cold static void |
|
|
672 | err_errsv (dec_t *dec) |
|
|
673 | { |
|
|
674 | if (!dec->err) |
|
|
675 | { |
|
|
676 | dec->err_sv = newSVsv (ERRSV); |
626 | |
677 | |
627 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
678 | // chop off the trailing \n |
|
|
679 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
680 | *SvEND (dec->err_sv) = 0; |
628 | |
681 | |
|
|
682 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
683 | } |
|
|
684 | } |
|
|
685 | |
|
|
686 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
687 | ecb_cold static void |
|
|
688 | err_set (dec_t *dec, const char *reason) |
|
|
689 | { |
|
|
690 | if (!dec->err) |
|
|
691 | dec->err = reason; |
|
|
692 | } |
|
|
693 | |
|
|
694 | ecb_cold static void |
|
|
695 | err_unexpected_end (dec_t *dec) |
|
|
696 | { |
|
|
697 | err_set (dec, "unexpected end of CBOR data"); |
|
|
698 | } |
|
|
699 | |
|
|
700 | ecb_cold static void |
|
|
701 | err_nesting_exceeded (dec_t *dec) |
|
|
702 | { |
|
|
703 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
704 | } |
|
|
705 | |
|
|
706 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
707 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
708 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
709 | |
|
|
710 | #define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec)) |
|
|
711 | |
629 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
712 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
630 | #define DEC_DEC_DEPTH --dec->depth |
713 | #define DEC_DEC_DEPTH --dec->depth |
631 | |
714 | |
632 | static UV |
715 | static UV |
633 | decode_uint (dec_t *dec) |
716 | decode_uint (dec_t *dec) |
634 | { |
717 | { |
… | |
… | |
711 | av_push (av, decode_sv (dec)); |
794 | av_push (av, decode_sv (dec)); |
712 | } |
795 | } |
713 | } |
796 | } |
714 | else |
797 | else |
715 | { |
798 | { |
716 | int i, len = decode_uint (dec); |
799 | UV i, len = decode_uint (dec); |
717 | |
800 | |
718 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
801 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
719 | av_fill (av, len - 1); |
802 | av_fill (av, len - 1); |
720 | |
803 | |
721 | for (i = 0; i < len; ++i) |
804 | for (i = 0; i < len; ++i) |
… | |
… | |
736 | { |
819 | { |
737 | // for speed reasons, we specialcase single-string |
820 | // for speed reasons, we specialcase single-string |
738 | // byte or utf-8 strings as keys, but only when !stringref |
821 | // byte or utf-8 strings as keys, but only when !stringref |
739 | |
822 | |
740 | if (ecb_expect_true (!dec->stringref)) |
823 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
824 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | { |
825 | { |
743 | I32 len = decode_uint (dec); |
826 | STRLEN len = decode_uint (dec); |
744 | char *key = (char *)dec->cur; |
827 | char *key = (char *)dec->cur; |
745 | |
828 | |
|
|
829 | WANT (len); |
746 | dec->cur += len; |
830 | dec->cur += len; |
747 | |
831 | |
748 | hv_store (hv, key, len, decode_sv (dec), 0); |
832 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | |
833 | |
750 | return; |
834 | return; |
751 | } |
835 | } |
752 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
836 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | { |
837 | { |
754 | I32 len = decode_uint (dec); |
838 | STRLEN len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
839 | char *key = (char *)dec->cur; |
756 | |
840 | |
|
|
841 | WANT (len); |
757 | dec->cur += len; |
842 | dec->cur += len; |
758 | |
843 | |
759 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
844 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
760 | if (!is_utf8_string (key, len)) |
845 | if (!is_utf8_string (key, len)) |
761 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
846 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
765 | return; |
850 | return; |
766 | } |
851 | } |
767 | |
852 | |
768 | SV *k = decode_sv (dec); |
853 | SV *k = decode_sv (dec); |
769 | SV *v = decode_sv (dec); |
854 | SV *v = decode_sv (dec); |
|
|
855 | |
|
|
856 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
857 | // methods, and this is hopefully the only place where it can happen, |
|
|
858 | // so if there is a chance of an exception, take the very slow path. |
|
|
859 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
860 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
861 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
862 | { |
|
|
863 | dSP; |
|
|
864 | |
|
|
865 | ENTER; SAVETMPS; |
|
|
866 | PUSHMARK (SP); |
|
|
867 | EXTEND (SP, 3); |
|
|
868 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
869 | PUSHs (sv_2mortal (k)); |
|
|
870 | PUSHs (sv_2mortal (v)); |
|
|
871 | |
|
|
872 | PUTBACK; |
|
|
873 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
874 | SPAGAIN; |
|
|
875 | |
|
|
876 | FREETMPS; LEAVE; |
|
|
877 | |
|
|
878 | if (SvTRUE (ERRSV)) |
|
|
879 | ERR_ERRSV; |
|
|
880 | |
|
|
881 | return; |
|
|
882 | } |
770 | |
883 | |
771 | hv_store_ent (hv, k, v, 0); |
884 | hv_store_ent (hv, k, v, 0); |
772 | SvREFCNT_dec (k); |
885 | SvREFCNT_dec (k); |
773 | |
886 | |
774 | fail: |
887 | fail: |
… | |
… | |
799 | decode_he (dec, hv); |
912 | decode_he (dec, hv); |
800 | } |
913 | } |
801 | } |
914 | } |
802 | else |
915 | else |
803 | { |
916 | { |
804 | int pairs = decode_uint (dec); |
917 | UV pairs = decode_uint (dec); |
|
|
918 | |
|
|
919 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
805 | |
920 | |
806 | while (pairs--) |
921 | while (pairs--) |
807 | decode_he (dec, hv); |
922 | decode_he (dec, hv); |
808 | } |
923 | } |
809 | |
924 | |
… | |
… | |
897 | sv = newRV_noinc (decode_sv (dec)); |
1012 | sv = newRV_noinc (decode_sv (dec)); |
898 | break; |
1013 | break; |
899 | |
1014 | |
900 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1015 | case CBOR_TAG_STRINGREF_NAMESPACE: |
901 | { |
1016 | { |
|
|
1017 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
1018 | // erase mortalised caches, e.g. "shareable" |
902 | ENTER; SAVETMPS; |
1019 | ENTER; |
903 | |
1020 | |
904 | SAVESPTR (dec->stringref); |
1021 | SAVESPTR (dec->stringref); |
905 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1022 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
906 | |
1023 | |
907 | sv = decode_sv (dec); |
1024 | sv = decode_sv (dec); |
908 | |
1025 | |
909 | FREETMPS; LEAVE; |
1026 | LEAVE; |
910 | } |
1027 | } |
911 | break; |
1028 | break; |
912 | |
1029 | |
913 | case CBOR_TAG_STRINGREF: |
1030 | case CBOR_TAG_STRINGREF: |
914 | { |
1031 | { |
… | |
… | |
965 | } |
1082 | } |
966 | break; |
1083 | break; |
967 | |
1084 | |
968 | case CBOR_TAG_PERL_OBJECT: |
1085 | case CBOR_TAG_PERL_OBJECT: |
969 | { |
1086 | { |
|
|
1087 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1088 | goto filter; |
|
|
1089 | |
970 | sv = decode_sv (dec); |
1090 | sv = decode_sv (dec); |
971 | |
1091 | |
972 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
1092 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
973 | ERR ("corrupted CBOR data (non-array perl object)"); |
1093 | ERR ("corrupted CBOR data (non-array perl object)"); |
974 | |
1094 | |
… | |
… | |
984 | if (!method) |
1104 | if (!method) |
985 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1105 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
986 | |
1106 | |
987 | dSP; |
1107 | dSP; |
988 | |
1108 | |
989 | ENTER; SAVETMPS; PUSHMARK (SP); |
1109 | ENTER; SAVETMPS; |
|
|
1110 | PUSHMARK (SP); |
990 | EXTEND (SP, len + 1); |
1111 | EXTEND (SP, len + 1); |
991 | // we re-bless the reference to get overload and other niceties right |
1112 | // we re-bless the reference to get overload and other niceties right |
992 | PUSHs (*av_fetch (av, 0, 1)); |
1113 | PUSHs (*av_fetch (av, 0, 1)); |
993 | PUSHs (sv_cbor); |
1114 | PUSHs (sv_cbor); |
994 | |
1115 | |
… | |
… | |
1002 | SPAGAIN; |
1123 | SPAGAIN; |
1003 | |
1124 | |
1004 | if (SvTRUE (ERRSV)) |
1125 | if (SvTRUE (ERRSV)) |
1005 | { |
1126 | { |
1006 | FREETMPS; LEAVE; |
1127 | FREETMPS; LEAVE; |
1007 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1128 | ERR_ERRSV; |
1008 | } |
1129 | } |
1009 | |
1130 | |
1010 | SvREFCNT_dec (sv); |
1131 | SvREFCNT_dec (sv); |
1011 | sv = SvREFCNT_inc (POPs); |
1132 | sv = SvREFCNT_inc (POPs); |
1012 | |
1133 | |
… | |
… | |
1015 | FREETMPS; LEAVE; |
1136 | FREETMPS; LEAVE; |
1016 | } |
1137 | } |
1017 | break; |
1138 | break; |
1018 | |
1139 | |
1019 | default: |
1140 | default: |
|
|
1141 | filter: |
1020 | { |
1142 | { |
|
|
1143 | SV *tag_sv = newSVuv (tag); |
|
|
1144 | |
1021 | sv = decode_sv (dec); |
1145 | sv = decode_sv (dec); |
1022 | |
1146 | |
1023 | dSP; |
1147 | dSP; |
1024 | ENTER; SAVETMPS; PUSHMARK (SP); |
1148 | ENTER; SAVETMPS; |
|
|
1149 | SAVESTACK_POS (); |
|
|
1150 | PUSHMARK (SP); |
1025 | EXTEND (SP, 2); |
1151 | EXTEND (SP, 2); |
1026 | PUSHs (newSVuv (tag)); |
1152 | PUSHs (tag_sv); |
1027 | PUSHs (sv); |
1153 | PUSHs (sv); |
1028 | |
1154 | |
1029 | PUTBACK; |
1155 | PUTBACK; |
1030 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1156 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1031 | SPAGAIN; |
1157 | SPAGAIN; |
1032 | |
1158 | |
1033 | if (SvTRUE (ERRSV)) |
1159 | if (SvTRUE (ERRSV)) |
1034 | { |
1160 | { |
|
|
1161 | SvREFCNT_dec (tag_sv); |
1035 | FREETMPS; LEAVE; |
1162 | FREETMPS; LEAVE; |
1036 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1163 | ERR_ERRSV; |
1037 | } |
1164 | } |
1038 | |
1165 | |
1039 | if (count) |
1166 | if (count) |
1040 | { |
1167 | { |
|
|
1168 | SvREFCNT_dec (tag_sv); |
1041 | SvREFCNT_dec (sv); |
1169 | SvREFCNT_dec (sv); |
1042 | sv = SvREFCNT_inc (POPs); |
1170 | sv = SvREFCNT_inc (POPs); |
1043 | } |
1171 | } |
1044 | else |
1172 | else |
1045 | { |
1173 | { |
1046 | AV *av = newAV (); |
1174 | AV *av = newAV (); |
1047 | av_push (av, newSVuv (tag)); |
1175 | av_push (av, tag_sv); |
1048 | av_push (av, sv); |
1176 | av_push (av, sv); |
1049 | |
1177 | |
1050 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1178 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1051 | ? cbor_tagged_stash |
1179 | ? cbor_tagged_stash |
1052 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1180 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
… | |
… | |
1155 | } |
1283 | } |
1156 | |
1284 | |
1157 | static SV * |
1285 | static SV * |
1158 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1286 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1159 | { |
1287 | { |
1160 | dec_t dec = { }; |
1288 | dec_t dec = { 0 }; |
1161 | SV *sv; |
1289 | SV *sv; |
1162 | STRLEN len; |
1290 | STRLEN len; |
1163 | char *data = SvPVbyte (string, len); |
1291 | char *data = SvPVbyte (string, len); |
1164 | |
1292 | |
1165 | if (len > cbor->max_size && cbor->max_size) |
1293 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1181 | |
1309 | |
1182 | if (dec.err) |
1310 | if (dec.err) |
1183 | { |
1311 | { |
1184 | if (dec.shareable) |
1312 | if (dec.shareable) |
1185 | { |
1313 | { |
1186 | // need to break cyclic links, which whould all be in shareable |
1314 | // need to break cyclic links, which would all be in shareable |
1187 | int i; |
1315 | int i; |
1188 | SV **svp; |
1316 | SV **svp; |
1189 | |
1317 | |
1190 | for (i = av_len (dec.shareable) + 1; i--; ) |
1318 | for (i = av_len (dec.shareable) + 1; i--; ) |
1191 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1319 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1192 | sv_setsv (*svp, &PL_sv_undef); |
1320 | sv_setsv (*svp, &PL_sv_undef); |
1193 | } |
1321 | } |
1194 | |
1322 | |
1195 | SvREFCNT_dec (sv); |
1323 | SvREFCNT_dec (sv); |
|
|
1324 | |
|
|
1325 | if (dec.err_sv) |
|
|
1326 | sv_2mortal (dec.err_sv); |
|
|
1327 | |
1196 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1328 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1197 | } |
1329 | } |
1198 | |
1330 | |
1199 | sv = sv_2mortal (sv); |
1331 | sv = sv_2mortal (sv); |
1200 | |
1332 | |
… | |
… | |
1256 | |
1388 | |
1257 | int major = *p >> MAJOR_SHIFT; |
1389 | int major = *p >> MAJOR_SHIFT; |
1258 | |
1390 | |
1259 | switch (major) |
1391 | switch (major) |
1260 | { |
1392 | { |
|
|
1393 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1394 | ++count; // tags merely prefix another value |
|
|
1395 | break; |
|
|
1396 | |
1261 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1397 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1262 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1398 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1263 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1399 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1264 | case MAJOR_MAP >> MAJOR_SHIFT: |
1400 | case MAJOR_MAP >> MAJOR_SHIFT: |
1265 | { |
1401 | { |
… | |
… | |
1340 | |
1476 | |
1341 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1477 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1342 | |
1478 | |
1343 | sv_cbor = newSVpv ("CBOR", 0); |
1479 | sv_cbor = newSVpv ("CBOR", 0); |
1344 | SvREADONLY_on (sv_cbor); |
1480 | SvREADONLY_on (sv_cbor); |
|
|
1481 | |
|
|
1482 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1345 | } |
1483 | } |
1346 | |
1484 | |
1347 | PROTOTYPES: DISABLE |
1485 | PROTOTYPES: DISABLE |
1348 | |
1486 | |
1349 | void CLONE (...) |
1487 | void CLONE (...) |
… | |
… | |
1369 | ALIAS: |
1507 | ALIAS: |
1370 | shrink = F_SHRINK |
1508 | shrink = F_SHRINK |
1371 | allow_unknown = F_ALLOW_UNKNOWN |
1509 | allow_unknown = F_ALLOW_UNKNOWN |
1372 | allow_sharing = F_ALLOW_SHARING |
1510 | allow_sharing = F_ALLOW_SHARING |
1373 | allow_cycles = F_ALLOW_CYCLES |
1511 | allow_cycles = F_ALLOW_CYCLES |
|
|
1512 | forbid_objects = F_FORBID_OBJECTS |
1374 | pack_strings = F_PACK_STRINGS |
1513 | pack_strings = F_PACK_STRINGS |
|
|
1514 | text_keys = F_TEXT_KEYS |
|
|
1515 | text_strings = F_TEXT_STRINGS |
1375 | validate_utf8 = F_VALIDATE_UTF8 |
1516 | validate_utf8 = F_VALIDATE_UTF8 |
1376 | PPCODE: |
1517 | PPCODE: |
1377 | { |
1518 | { |
1378 | if (enable) |
1519 | if (enable) |
1379 | self->flags |= ix; |
1520 | self->flags |= ix; |
… | |
… | |
1387 | ALIAS: |
1528 | ALIAS: |
1388 | get_shrink = F_SHRINK |
1529 | get_shrink = F_SHRINK |
1389 | get_allow_unknown = F_ALLOW_UNKNOWN |
1530 | get_allow_unknown = F_ALLOW_UNKNOWN |
1390 | get_allow_sharing = F_ALLOW_SHARING |
1531 | get_allow_sharing = F_ALLOW_SHARING |
1391 | get_allow_cycles = F_ALLOW_CYCLES |
1532 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1533 | get_forbid_objects = F_FORBID_OBJECTS |
1392 | get_pack_strings = F_PACK_STRINGS |
1534 | get_pack_strings = F_PACK_STRINGS |
|
|
1535 | get_text_keys = F_TEXT_KEYS |
|
|
1536 | get_text_strings = F_TEXT_STRINGS |
1393 | get_validate_utf8 = F_VALIDATE_UTF8 |
1537 | get_validate_utf8 = F_VALIDATE_UTF8 |
1394 | PPCODE: |
1538 | PPCODE: |
1395 | XPUSHs (boolSV (self->flags & ix)); |
1539 | XPUSHs (boolSV (self->flags & ix)); |
1396 | |
1540 | |
1397 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1541 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
… | |
… | |
1447 | EXTEND (SP, 2); |
1591 | EXTEND (SP, 2); |
1448 | PUSHs (sv); |
1592 | PUSHs (sv); |
1449 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1593 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | } |
1594 | } |
1451 | |
1595 | |
1452 | void incr_parse (CBOR *self, SV *cborstr, int chop = 0) |
1596 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1597 | ALIAS: |
|
|
1598 | incr_parse_multiple = 1 |
1453 | PPCODE: |
1599 | PPCODE: |
1454 | { |
1600 | { |
1455 | if (SvUTF8 (cborstr)) |
1601 | if (SvUTF8 (cborstr)) |
1456 | sv_utf8_downgrade (cborstr, 0); |
1602 | sv_utf8_downgrade (cborstr, 0); |
1457 | |
1603 | |
… | |
… | |
1462 | self->incr_need = 1; |
1608 | self->incr_need = 1; |
1463 | |
1609 | |
1464 | av_push (self->incr_count, newSViv (1)); |
1610 | av_push (self->incr_count, newSViv (1)); |
1465 | } |
1611 | } |
1466 | |
1612 | |
1467 | for (;;) |
1613 | do |
1468 | { |
1614 | { |
1469 | if (!incr_parse (self, cborstr)) |
1615 | if (!incr_parse (self, cborstr)) |
1470 | { |
1616 | { |
1471 | if (self->incr_need > self->max_size && self->max_size) |
1617 | if (self->incr_need > self->max_size && self->max_size) |
1472 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
1618 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
1473 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
1619 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
1474 | |
1620 | |
1475 | break; |
1621 | break; |
1476 | } |
1622 | } |
1477 | |
1623 | |
1478 | SV *sv; |
1624 | SV *sv; |
1479 | char *offset; |
1625 | char *offset; |
1480 | |
1626 | |
1481 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
1627 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
1482 | XPUSHs (sv); |
1628 | XPUSHs (sv); |
1483 | |
1629 | |
|
|
1630 | sv_chop (cborstr, offset); |
|
|
1631 | |
1484 | av_clear (self->incr_count); |
1632 | av_clear (self->incr_count); |
1485 | av_push (self->incr_count, newSViv (1)); |
1633 | av_push (self->incr_count, newSViv (1)); |
1486 | |
1634 | |
1487 | if (chop) |
|
|
1488 | { |
|
|
1489 | self->incr_pos = 0; |
1635 | self->incr_pos = 0; |
1490 | sv_chop (cborstr, offset); |
|
|
1491 | } |
|
|
1492 | else |
|
|
1493 | self->incr_pos = offset - SvPVX (cborstr); |
|
|
1494 | |
|
|
1495 | self->incr_need = self->incr_pos + 1; |
1636 | self->incr_need = self->incr_pos + 1; |
1496 | } |
1637 | } |
|
|
1638 | while (ix); |
1497 | } |
1639 | } |
1498 | |
1640 | |
1499 | void incr_reset (CBOR *self) |
1641 | void incr_reset (CBOR *self) |
1500 | CODE: |
1642 | CODE: |
1501 | { |
1643 | { |