… | |
… | |
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)) |
… | |
… | |
99 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_TEXT_KEYS 0x00000020UL |
|
|
106 | #define F_TEXT_STRINGS 0x00000040UL |
104 | #define F_VALIDATE_UTF8 0x00000020UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
105 | |
108 | |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
107 | |
110 | |
108 | #define SB do { |
111 | #define SB do { |
109 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
275 | encode_tag (enc_t *enc, UV tag) |
278 | encode_tag (enc_t *enc, UV tag) |
276 | { |
279 | { |
277 | encode_uint (enc, MAJOR_TAG, tag); |
280 | encode_uint (enc, MAJOR_TAG, tag); |
278 | } |
281 | } |
279 | |
282 | |
|
|
283 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
284 | ecb_noinline static void |
|
|
285 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
286 | { |
|
|
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 | |
280 | ecb_inline void |
306 | ecb_inline void |
281 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
307 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
282 | { |
308 | { |
|
|
309 | if (ecb_expect_false (upgrade_utf8)) |
|
|
310 | if (!utf8) |
|
|
311 | { |
|
|
312 | encode_str_utf8 (enc, utf8, str, len); |
|
|
313 | return; |
|
|
314 | } |
|
|
315 | |
283 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
284 | need (enc, len); |
317 | need (enc, len); |
285 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
286 | enc->cur += len; |
319 | enc->cur += len; |
287 | } |
320 | } |
288 | |
321 | |
289 | static void |
322 | ecb_inline void |
290 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
291 | { |
324 | { |
292 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
293 | { |
326 | { |
294 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
295 | |
328 | |
… | |
… | |
306 | sv_setuv (*svp, enc->stringref_idx); |
339 | sv_setuv (*svp, enc->stringref_idx); |
307 | ++enc->stringref_idx; |
340 | ++enc->stringref_idx; |
308 | } |
341 | } |
309 | } |
342 | } |
310 | |
343 | |
311 | encode_str (enc, utf8, str, len); |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
312 | } |
345 | } |
313 | |
346 | |
314 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
315 | |
348 | |
316 | static void |
349 | static void |
… | |
… | |
323 | |
356 | |
324 | ++enc->depth; |
357 | ++enc->depth; |
325 | |
358 | |
326 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
359 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | |
360 | |
|
|
361 | if (SvMAGICAL (av)) |
328 | for (i = 0; i <= len; ++i) |
362 | for (i = 0; i <= len; ++i) |
329 | { |
363 | { |
330 | SV **svp = av_fetch (av, i, 0); |
364 | SV **svp = av_fetch (av, i, 0); |
331 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
365 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
332 | } |
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 | } |
333 | |
373 | |
334 | --enc->depth; |
374 | --enc->depth; |
335 | } |
375 | } |
336 | |
376 | |
337 | static void |
377 | static void |
… | |
… | |
355 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
356 | { |
396 | { |
357 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
358 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
359 | else |
399 | else |
360 | encode_strref (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)); |
361 | |
401 | |
362 | 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)); |
363 | } |
403 | } |
364 | |
404 | |
365 | if (mg) |
405 | if (mg) |
… | |
… | |
441 | |
481 | |
442 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
443 | { |
483 | { |
444 | dSP; |
484 | dSP; |
445 | |
485 | |
446 | ENTER; SAVETMPS; PUSHMARK (SP); |
486 | ENTER; SAVETMPS; |
|
|
487 | PUSHMARK (SP); |
447 | // 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 |
448 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
489 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
449 | |
490 | |
450 | PUTBACK; |
491 | PUTBACK; |
451 | // G_SCALAR ensures that return value is 1 |
492 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
464 | } |
505 | } |
465 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
506 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
466 | { |
507 | { |
467 | dSP; |
508 | dSP; |
468 | |
509 | |
469 | ENTER; SAVETMPS; PUSHMARK (SP); |
510 | ENTER; SAVETMPS; |
|
|
511 | SAVESTACK_POS (); |
|
|
512 | PUSHMARK (SP); |
470 | EXTEND (SP, 2); |
513 | EXTEND (SP, 2); |
471 | // 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 |
472 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
515 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
473 | PUSHs (sv_cbor); |
516 | PUSHs (sv_cbor); |
474 | |
517 | |
… | |
… | |
480 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | 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)); |
524 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
482 | |
525 | |
483 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
484 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
485 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
486 | |
529 | |
487 | while (count) |
530 | while (count) |
488 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
489 | |
532 | |
490 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
549 | |
592 | |
550 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
551 | { |
594 | { |
552 | STRLEN len; |
595 | STRLEN len; |
553 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
554 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
555 | } |
598 | } |
556 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
557 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
558 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
559 | { |
602 | { |
… | |
… | |
576 | } |
619 | } |
577 | |
620 | |
578 | static SV * |
621 | static SV * |
579 | encode_cbor (SV *scalar, CBOR *cbor) |
622 | encode_cbor (SV *scalar, CBOR *cbor) |
580 | { |
623 | { |
581 | enc_t enc = { }; |
624 | enc_t enc = { 0 }; |
582 | |
625 | |
583 | enc.cbor = *cbor; |
626 | enc.cbor = *cbor; |
584 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
585 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
586 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
… | |
… | |
618 | U32 depth; // recursion depth |
661 | U32 depth; // recursion depth |
619 | U32 maxdepth; // recursion depth limit |
662 | U32 maxdepth; // recursion depth limit |
620 | AV *shareable; |
663 | AV *shareable; |
621 | AV *stringref; |
664 | AV *stringref; |
622 | SV *decode_tagged; |
665 | SV *decode_tagged; |
|
|
666 | SV *err_sv; // optional sv for error, needs to be freed |
623 | } dec_t; |
667 | } dec_t; |
624 | |
668 | |
625 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
669 | // set dec->err to ERRSV |
|
|
670 | ecb_cold static void |
|
|
671 | err_errsv (dec_t *dec) |
|
|
672 | { |
|
|
673 | if (!dec->err) |
|
|
674 | { |
|
|
675 | dec->err_sv = newSVsv (ERRSV); |
626 | |
676 | |
627 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
677 | // chop off the trailing \n |
|
|
678 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
679 | *SvEND (dec->err_sv) = 0; |
628 | |
680 | |
|
|
681 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
682 | } |
|
|
683 | } |
|
|
684 | |
|
|
685 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
686 | ecb_cold static void |
|
|
687 | err_set (dec_t *dec, const char *reason) |
|
|
688 | { |
|
|
689 | if (!dec->err) |
|
|
690 | dec->err = reason; |
|
|
691 | } |
|
|
692 | |
|
|
693 | ecb_cold static void |
|
|
694 | err_unexpected_end (dec_t *dec) |
|
|
695 | { |
|
|
696 | err_set (dec, "unexpected end of CBOR data"); |
|
|
697 | } |
|
|
698 | |
|
|
699 | ecb_cold static void |
|
|
700 | err_nesting_exceeded (dec_t *dec) |
|
|
701 | { |
|
|
702 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
703 | } |
|
|
704 | |
|
|
705 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
706 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
707 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
708 | |
|
|
709 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) |
|
|
710 | |
629 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
711 | #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 |
712 | #define DEC_DEC_DEPTH --dec->depth |
631 | |
713 | |
632 | static UV |
714 | static UV |
633 | decode_uint (dec_t *dec) |
715 | decode_uint (dec_t *dec) |
634 | { |
716 | { |
… | |
… | |
711 | av_push (av, decode_sv (dec)); |
793 | av_push (av, decode_sv (dec)); |
712 | } |
794 | } |
713 | } |
795 | } |
714 | else |
796 | else |
715 | { |
797 | { |
716 | int i, len = decode_uint (dec); |
798 | UV i, len = decode_uint (dec); |
717 | |
799 | |
718 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
800 | 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); |
801 | av_fill (av, len - 1); |
720 | |
802 | |
721 | for (i = 0; i < len; ++i) |
803 | for (i = 0; i < len; ++i) |
… | |
… | |
738 | // byte or utf-8 strings as keys, but only when !stringref |
820 | // byte or utf-8 strings as keys, but only when !stringref |
739 | |
821 | |
740 | if (ecb_expect_true (!dec->stringref)) |
822 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
823 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | { |
824 | { |
743 | I32 len = decode_uint (dec); |
825 | STRLEN len = decode_uint (dec); |
744 | char *key = (char *)dec->cur; |
826 | char *key = (char *)dec->cur; |
745 | |
827 | |
|
|
828 | WANT (len); |
746 | dec->cur += len; |
829 | dec->cur += len; |
747 | |
830 | |
748 | hv_store (hv, key, len, decode_sv (dec), 0); |
831 | hv_store (hv, key, len, decode_sv (dec), 0); |
749 | |
832 | |
750 | return; |
833 | return; |
751 | } |
834 | } |
752 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
835 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | { |
836 | { |
754 | I32 len = decode_uint (dec); |
837 | STRLEN len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
838 | char *key = (char *)dec->cur; |
756 | |
839 | |
|
|
840 | WANT (len); |
757 | dec->cur += len; |
841 | dec->cur += len; |
758 | |
842 | |
759 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
843 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
760 | if (!is_utf8_string (key, len)) |
844 | if (!is_utf8_string (key, len)) |
761 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
845 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
765 | return; |
849 | return; |
766 | } |
850 | } |
767 | |
851 | |
768 | SV *k = decode_sv (dec); |
852 | SV *k = decode_sv (dec); |
769 | SV *v = decode_sv (dec); |
853 | SV *v = decode_sv (dec); |
|
|
854 | |
|
|
855 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
856 | // methods, and this is hopefully the only place where it can happen, |
|
|
857 | // so if there is a chance of an exception, take the very slow path. |
|
|
858 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
859 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
860 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
861 | { |
|
|
862 | dSP; |
|
|
863 | |
|
|
864 | ENTER; SAVETMPS; |
|
|
865 | PUSHMARK (SP); |
|
|
866 | EXTEND (SP, 3); |
|
|
867 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
868 | PUSHs (sv_2mortal (k)); |
|
|
869 | PUSHs (sv_2mortal (v)); |
|
|
870 | |
|
|
871 | PUTBACK; |
|
|
872 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
873 | SPAGAIN; |
|
|
874 | |
|
|
875 | FREETMPS; LEAVE; |
|
|
876 | |
|
|
877 | if (SvTRUE (ERRSV)) |
|
|
878 | ERR_ERRSV; |
|
|
879 | |
|
|
880 | return; |
|
|
881 | } |
770 | |
882 | |
771 | hv_store_ent (hv, k, v, 0); |
883 | hv_store_ent (hv, k, v, 0); |
772 | SvREFCNT_dec (k); |
884 | SvREFCNT_dec (k); |
773 | |
885 | |
774 | fail: |
886 | fail: |
… | |
… | |
799 | decode_he (dec, hv); |
911 | decode_he (dec, hv); |
800 | } |
912 | } |
801 | } |
913 | } |
802 | else |
914 | else |
803 | { |
915 | { |
804 | int pairs = decode_uint (dec); |
916 | UV pairs = decode_uint (dec); |
|
|
917 | |
|
|
918 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
805 | |
919 | |
806 | while (pairs--) |
920 | while (pairs--) |
807 | decode_he (dec, hv); |
921 | decode_he (dec, hv); |
808 | } |
922 | } |
809 | |
923 | |
… | |
… | |
897 | sv = newRV_noinc (decode_sv (dec)); |
1011 | sv = newRV_noinc (decode_sv (dec)); |
898 | break; |
1012 | break; |
899 | |
1013 | |
900 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1014 | case CBOR_TAG_STRINGREF_NAMESPACE: |
901 | { |
1015 | { |
|
|
1016 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
1017 | // erase mortalised caches, e.g. "shareable" |
902 | ENTER; SAVETMPS; |
1018 | ENTER; |
903 | |
1019 | |
904 | SAVESPTR (dec->stringref); |
1020 | SAVESPTR (dec->stringref); |
905 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1021 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
906 | |
1022 | |
907 | sv = decode_sv (dec); |
1023 | sv = decode_sv (dec); |
908 | |
1024 | |
909 | FREETMPS; LEAVE; |
1025 | LEAVE; |
910 | } |
1026 | } |
911 | break; |
1027 | break; |
912 | |
1028 | |
913 | case CBOR_TAG_STRINGREF: |
1029 | case CBOR_TAG_STRINGREF: |
914 | { |
1030 | { |
… | |
… | |
984 | if (!method) |
1100 | if (!method) |
985 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1101 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
986 | |
1102 | |
987 | dSP; |
1103 | dSP; |
988 | |
1104 | |
989 | ENTER; SAVETMPS; PUSHMARK (SP); |
1105 | ENTER; SAVETMPS; |
|
|
1106 | PUSHMARK (SP); |
990 | EXTEND (SP, len + 1); |
1107 | EXTEND (SP, len + 1); |
991 | // we re-bless the reference to get overload and other niceties right |
1108 | // we re-bless the reference to get overload and other niceties right |
992 | PUSHs (*av_fetch (av, 0, 1)); |
1109 | PUSHs (*av_fetch (av, 0, 1)); |
993 | PUSHs (sv_cbor); |
1110 | PUSHs (sv_cbor); |
994 | |
1111 | |
… | |
… | |
1002 | SPAGAIN; |
1119 | SPAGAIN; |
1003 | |
1120 | |
1004 | if (SvTRUE (ERRSV)) |
1121 | if (SvTRUE (ERRSV)) |
1005 | { |
1122 | { |
1006 | FREETMPS; LEAVE; |
1123 | FREETMPS; LEAVE; |
1007 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1124 | ERR_ERRSV; |
1008 | } |
1125 | } |
1009 | |
1126 | |
1010 | SvREFCNT_dec (sv); |
1127 | SvREFCNT_dec (sv); |
1011 | sv = SvREFCNT_inc (POPs); |
1128 | sv = SvREFCNT_inc (POPs); |
1012 | |
1129 | |
… | |
… | |
1016 | } |
1133 | } |
1017 | break; |
1134 | break; |
1018 | |
1135 | |
1019 | default: |
1136 | default: |
1020 | { |
1137 | { |
|
|
1138 | SV *tag_sv = newSVuv (tag); |
|
|
1139 | |
1021 | sv = decode_sv (dec); |
1140 | sv = decode_sv (dec); |
1022 | |
1141 | |
1023 | dSP; |
1142 | dSP; |
1024 | ENTER; SAVETMPS; PUSHMARK (SP); |
1143 | ENTER; SAVETMPS; |
|
|
1144 | SAVESTACK_POS (); |
|
|
1145 | PUSHMARK (SP); |
1025 | EXTEND (SP, 2); |
1146 | EXTEND (SP, 2); |
1026 | PUSHs (newSVuv (tag)); |
1147 | PUSHs (tag_sv); |
1027 | PUSHs (sv); |
1148 | PUSHs (sv); |
1028 | |
1149 | |
1029 | PUTBACK; |
1150 | PUTBACK; |
1030 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1151 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1031 | SPAGAIN; |
1152 | SPAGAIN; |
1032 | |
1153 | |
1033 | if (SvTRUE (ERRSV)) |
1154 | if (SvTRUE (ERRSV)) |
1034 | { |
1155 | { |
|
|
1156 | SvREFCNT_dec (tag_sv); |
1035 | FREETMPS; LEAVE; |
1157 | FREETMPS; LEAVE; |
1036 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1158 | ERR_ERRSV; |
1037 | } |
1159 | } |
1038 | |
1160 | |
1039 | if (count) |
1161 | if (count) |
1040 | { |
1162 | { |
|
|
1163 | SvREFCNT_dec (tag_sv); |
1041 | SvREFCNT_dec (sv); |
1164 | SvREFCNT_dec (sv); |
1042 | sv = SvREFCNT_inc (POPs); |
1165 | sv = SvREFCNT_inc (POPs); |
1043 | } |
1166 | } |
1044 | else |
1167 | else |
1045 | { |
1168 | { |
1046 | AV *av = newAV (); |
1169 | AV *av = newAV (); |
1047 | av_push (av, newSVuv (tag)); |
1170 | av_push (av, tag_sv); |
1048 | av_push (av, sv); |
1171 | av_push (av, sv); |
1049 | |
1172 | |
1050 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1173 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1051 | ? cbor_tagged_stash |
1174 | ? cbor_tagged_stash |
1052 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1175 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
… | |
… | |
1155 | } |
1278 | } |
1156 | |
1279 | |
1157 | static SV * |
1280 | static SV * |
1158 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1281 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1159 | { |
1282 | { |
1160 | dec_t dec = { }; |
1283 | dec_t dec = { 0 }; |
1161 | SV *sv; |
1284 | SV *sv; |
1162 | STRLEN len; |
1285 | STRLEN len; |
1163 | char *data = SvPVbyte (string, len); |
1286 | char *data = SvPVbyte (string, len); |
1164 | |
1287 | |
1165 | if (len > cbor->max_size && cbor->max_size) |
1288 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1181 | |
1304 | |
1182 | if (dec.err) |
1305 | if (dec.err) |
1183 | { |
1306 | { |
1184 | if (dec.shareable) |
1307 | if (dec.shareable) |
1185 | { |
1308 | { |
1186 | // need to break cyclic links, which whould all be in shareable |
1309 | // need to break cyclic links, which would all be in shareable |
1187 | int i; |
1310 | int i; |
1188 | SV **svp; |
1311 | SV **svp; |
1189 | |
1312 | |
1190 | for (i = av_len (dec.shareable) + 1; i--; ) |
1313 | for (i = av_len (dec.shareable) + 1; i--; ) |
1191 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1314 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1192 | sv_setsv (*svp, &PL_sv_undef); |
1315 | sv_setsv (*svp, &PL_sv_undef); |
1193 | } |
1316 | } |
1194 | |
1317 | |
1195 | SvREFCNT_dec (sv); |
1318 | SvREFCNT_dec (sv); |
|
|
1319 | |
|
|
1320 | if (dec.err_sv) |
|
|
1321 | sv_2mortal (dec.err_sv); |
|
|
1322 | |
1196 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1323 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1197 | } |
1324 | } |
1198 | |
1325 | |
1199 | sv = sv_2mortal (sv); |
1326 | sv = sv_2mortal (sv); |
1200 | |
1327 | |
… | |
… | |
1256 | |
1383 | |
1257 | int major = *p >> MAJOR_SHIFT; |
1384 | int major = *p >> MAJOR_SHIFT; |
1258 | |
1385 | |
1259 | switch (major) |
1386 | switch (major) |
1260 | { |
1387 | { |
|
|
1388 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1389 | ++count; // tags merely prefix another value |
|
|
1390 | break; |
|
|
1391 | |
1261 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1392 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1262 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1393 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1263 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1394 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1264 | case MAJOR_MAP >> MAJOR_SHIFT: |
1395 | case MAJOR_MAP >> MAJOR_SHIFT: |
1265 | { |
1396 | { |
… | |
… | |
1340 | |
1471 | |
1341 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1472 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1342 | |
1473 | |
1343 | sv_cbor = newSVpv ("CBOR", 0); |
1474 | sv_cbor = newSVpv ("CBOR", 0); |
1344 | SvREADONLY_on (sv_cbor); |
1475 | SvREADONLY_on (sv_cbor); |
|
|
1476 | |
|
|
1477 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1345 | } |
1478 | } |
1346 | |
1479 | |
1347 | PROTOTYPES: DISABLE |
1480 | PROTOTYPES: DISABLE |
1348 | |
1481 | |
1349 | void CLONE (...) |
1482 | void CLONE (...) |
… | |
… | |
1370 | shrink = F_SHRINK |
1503 | shrink = F_SHRINK |
1371 | allow_unknown = F_ALLOW_UNKNOWN |
1504 | allow_unknown = F_ALLOW_UNKNOWN |
1372 | allow_sharing = F_ALLOW_SHARING |
1505 | allow_sharing = F_ALLOW_SHARING |
1373 | allow_cycles = F_ALLOW_CYCLES |
1506 | allow_cycles = F_ALLOW_CYCLES |
1374 | pack_strings = F_PACK_STRINGS |
1507 | pack_strings = F_PACK_STRINGS |
|
|
1508 | text_keys = F_TEXT_KEYS |
|
|
1509 | text_strings = F_TEXT_STRINGS |
1375 | validate_utf8 = F_VALIDATE_UTF8 |
1510 | validate_utf8 = F_VALIDATE_UTF8 |
1376 | PPCODE: |
1511 | PPCODE: |
1377 | { |
1512 | { |
1378 | if (enable) |
1513 | if (enable) |
1379 | self->flags |= ix; |
1514 | self->flags |= ix; |
… | |
… | |
1388 | get_shrink = F_SHRINK |
1523 | get_shrink = F_SHRINK |
1389 | get_allow_unknown = F_ALLOW_UNKNOWN |
1524 | get_allow_unknown = F_ALLOW_UNKNOWN |
1390 | get_allow_sharing = F_ALLOW_SHARING |
1525 | get_allow_sharing = F_ALLOW_SHARING |
1391 | get_allow_cycles = F_ALLOW_CYCLES |
1526 | get_allow_cycles = F_ALLOW_CYCLES |
1392 | get_pack_strings = F_PACK_STRINGS |
1527 | get_pack_strings = F_PACK_STRINGS |
|
|
1528 | get_text_keys = F_TEXT_KEYS |
|
|
1529 | get_text_strings = F_TEXT_STRINGS |
1393 | get_validate_utf8 = F_VALIDATE_UTF8 |
1530 | get_validate_utf8 = F_VALIDATE_UTF8 |
1394 | PPCODE: |
1531 | PPCODE: |
1395 | XPUSHs (boolSV (self->flags & ix)); |
1532 | XPUSHs (boolSV (self->flags & ix)); |
1396 | |
1533 | |
1397 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1534 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |