ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.41 by root, Tue Dec 10 14:01:52 2013 UTC vs.
Revision 1.61 by root, Sat Nov 26 02:10:19 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines