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.16 by root, Tue Oct 29 20:59:16 2013 UTC vs.
Revision 1.27 by root, Fri Nov 22 15:28:38 2013 UTC

24 24
25// known tags 25// known tags
26enum cbor_tag 26enum cbor_tag
27{ 27{
28 // inofficial extensions (pending iana registration) 28 // inofficial extensions (pending iana registration)
29 CBOR_TAG_PERL_OBJECT = 256, 29 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object
30 CBOR_TAG_GENERIC_OBJECT = 257, 30 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object
31 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing
32 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing
33 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref
34 CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref
35 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
31 36
32 // rfc7049 37 // rfc7049
33 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 38 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
34 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any 39 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
35 CBOR_TAG_POS_BIGNUM = 2, // byte string 40 CBOR_TAG_POS_BIGNUM = 2, // byte string
49 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 54 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
50 55
51 CBOR_TAG_MAGIC = 55799 // self-describe cbor 56 CBOR_TAG_MAGIC = 55799 // self-describe cbor
52}; 57};
53 58
54#define F_SHRINK 0x00000200UL 59#define F_SHRINK 0x00000001UL
55#define F_ALLOW_UNKNOWN 0x00002000UL 60#define F_ALLOW_UNKNOWN 0x00000002UL
61#define F_ALLOW_SHARING 0x00000004UL //TODO
62#define F_ALLOW_STRINGREF 0x00000008UL //TODO
56 63
57#define INIT_SIZE 32 // initial scalar size to be allocated 64#define INIT_SIZE 32 // initial scalar size to be allocated
58 65
59#define SB do { 66#define SB do {
60#define SE } while (0) 67#define SE } while (0)
72# define CBOR_SLOW 0 79# define CBOR_SLOW 0
73# define CBOR_STASH cbor_stash 80# define CBOR_STASH cbor_stash
74#endif 81#endif
75 82
76static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: 83static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
77static SV *types_true, *types_false, *types_error, *sv_cbor; 84static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter;
78 85
79typedef struct { 86typedef struct {
80 U32 flags; 87 U32 flags;
81 U32 max_depth; 88 U32 max_depth;
82 STRLEN max_size; 89 STRLEN max_size;
90 SV *filter;
83} CBOR; 91} CBOR;
84 92
85ecb_inline void 93ecb_inline void
86cbor_init (CBOR *cbor) 94cbor_init (CBOR *cbor)
87{ 95{
88 Zero (cbor, 1, CBOR); 96 Zero (cbor, 1, CBOR);
89 cbor->max_depth = 512; 97 cbor->max_depth = 512;
98}
99
100ecb_inline void
101cbor_free (CBOR *cbor)
102{
103 SvREFCNT_dec (cbor->filter);
90} 104}
91 105
92///////////////////////////////////////////////////////////////////////////// 106/////////////////////////////////////////////////////////////////////////////
93// utility functions 107// utility functions
94 108
116 SvPV_renew (sv, SvCUR (sv) + 1); 130 SvPV_renew (sv, SvCUR (sv) + 1);
117#endif 131#endif
118 } 132 }
119} 133}
120 134
121///////////////////////////////////////////////////////////////////////////// 135// minimum length of a string to be registered for stringref
122// fp hell 136ecb_inline int
123 137minimum_string_length (UV idx)
124//TODO 138{
139 return idx > 23
140 ? idx > 0xffU
141 ? idx > 0xffffU
142 ? idx > 0xffffffffU
143 ? 7
144 : 6
145 : 5
146 : 4
147 : 3;
148}
125 149
126///////////////////////////////////////////////////////////////////////////// 150/////////////////////////////////////////////////////////////////////////////
127// encoder 151// encoder
128 152
129// structure used for encoding CBOR 153// structure used for encoding CBOR
132 char *cur; // SvPVX (sv) + current output position 156 char *cur; // SvPVX (sv) + current output position
133 char *end; // SvEND (sv) 157 char *end; // SvEND (sv)
134 SV *sv; // result scalar 158 SV *sv; // result scalar
135 CBOR cbor; 159 CBOR cbor;
136 U32 depth; // recursion level 160 U32 depth; // recursion level
161 HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8)
162 UV stringref_idx;
163 HV *shareable; // ptr => index, or 0
164 UV shareable_idx;
137} enc_t; 165} enc_t;
138 166
139ecb_inline void 167ecb_inline void
140need (enc_t *enc, STRLEN len) 168need (enc_t *enc, STRLEN len)
141{ 169{
193 *enc->cur++ = len >> 8; 221 *enc->cur++ = len >> 8;
194 *enc->cur++ = len; 222 *enc->cur++ = len;
195 } 223 }
196} 224}
197 225
226ecb_inline void
227encode_tag (enc_t *enc, UV tag)
228{
229 encode_uint (enc, 0xc0, tag);
230}
231
198static void 232static void
199encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 233encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
200{ 234{
235 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF))
236 {
237 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
238
239 if (SvOK (*svp))
240 {
241 // already registered, use stringref
242 encode_tag (enc, CBOR_TAG_STRINGREF);
243 encode_uint (enc, 0x00, SvUV (*svp));
244 return;
245 }
246 else if (len >= minimum_string_length (enc->stringref_idx))
247 {
248 // register only
249 sv_setuv (*svp, enc->stringref_idx);
250 ++enc->stringref_idx;
251 }
252 }
253
201 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 254 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
202 need (enc, len); 255 need (enc, len);
203 memcpy (enc->cur, str, len); 256 memcpy (enc->cur, str, len);
204 enc->cur += len; 257 enc->cur += len;
205} 258}
263 316
264// encode objects, arrays and special \0=false and \1=true values. 317// encode objects, arrays and special \0=false and \1=true values.
265static void 318static void
266encode_rv (enc_t *enc, SV *sv) 319encode_rv (enc_t *enc, SV *sv)
267{ 320{
268 svtype svt;
269
270 SvGETMAGIC (sv); 321 SvGETMAGIC (sv);
322
323 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
324 && ecb_expect_false (SvREFCNT (sv) > 1))
325 {
326 if (!enc->shareable)
327 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
328
329 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
330
331 if (SvOK (*svp))
332 {
333 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
334 encode_uint (enc, 0x00, SvUV (*svp));
335 return;
336 }
337 else
338 {
339 sv_setuv (*svp, enc->shareable_idx);
340 ++enc->shareable_idx;
341 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
342 }
343 }
344
271 svt = SvTYPE (sv); 345 svtype svt = SvTYPE (sv);
272 346
273 if (ecb_expect_false (SvOBJECT (sv))) 347 if (ecb_expect_false (SvOBJECT (sv)))
274 { 348 {
275 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash 349 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
276 ? types_boolean_stash 350 ? types_boolean_stash
336 410
337 // catch this surprisingly common error 411 // catch this surprisingly common error
338 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 412 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
339 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 413 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
340 414
341 encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); 415 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
342 encode_uint (enc, 0x80, count + 1); 416 encode_uint (enc, 0x80, count + 1);
343 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 417 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
344 418
345 while (count) 419 while (count)
346 encode_sv (enc, SP[1 - count--]); 420 encode_sv (enc, SP[1 - count--]);
355 } 429 }
356 else if (svt == SVt_PVHV) 430 else if (svt == SVt_PVHV)
357 encode_hv (enc, (HV *)sv); 431 encode_hv (enc, (HV *)sv);
358 else if (svt == SVt_PVAV) 432 else if (svt == SVt_PVAV)
359 encode_av (enc, (AV *)sv); 433 encode_av (enc, (AV *)sv);
360 else if (svt < SVt_PVAV)
361 {
362 STRLEN len = 0;
363 char *pv = svt ? SvPV (sv, len) : 0;
364
365 if (len == 1 && *pv == '1')
366 encode_ch (enc, 0xe0 | 21);
367 else if (len == 1 && *pv == '0')
368 encode_ch (enc, 0xe0 | 20);
369 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
370 encode_ch (enc, 0xe0 | 23);
371 else
372 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
373 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
374 }
375 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
376 encode_ch (enc, 0xe0 | 23);
377 else 434 else
378 croak ("encountered %s, but CBOR can only represent references to arrays or hashes", 435 {
379 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 436 encode_tag (enc, CBOR_TAG_INDIRECTION);
437 encode_sv (enc, sv);
438 }
380} 439}
381 440
382static void 441static void
383encode_nv (enc_t *enc, SV *sv) 442encode_nv (enc_t *enc, SV *sv)
384{ 443{
449} 508}
450 509
451static SV * 510static SV *
452encode_cbor (SV *scalar, CBOR *cbor) 511encode_cbor (SV *scalar, CBOR *cbor)
453{ 512{
454 enc_t enc; 513 enc_t enc = { };
455 514
456 enc.cbor = *cbor; 515 enc.cbor = *cbor;
457 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 516 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
458 enc.cur = SvPVX (enc.sv); 517 enc.cur = SvPVX (enc.sv);
459 enc.end = SvEND (enc.sv); 518 enc.end = SvEND (enc.sv);
460 enc.depth = 0;
461 519
462 SvPOK_only (enc.sv); 520 SvPOK_only (enc.sv);
521
522 if (cbor->flags & F_ALLOW_STRINGREF)
523 {
524 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
525 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
526 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
527 }
528
463 encode_sv (&enc, scalar); 529 encode_sv (&enc, scalar);
464 530
465 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 531 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
466 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 532 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
467 533
481 U8 *end; // end of input string 547 U8 *end; // end of input string
482 const char *err; // parse error, if != 0 548 const char *err; // parse error, if != 0
483 CBOR cbor; 549 CBOR cbor;
484 U32 depth; // recursion depth 550 U32 depth; // recursion depth
485 U32 maxdepth; // recursion depth limit 551 U32 maxdepth; // recursion depth limit
552 AV *shareable;
553 AV *stringref;
554 SV *decode_tagged;
486} dec_t; 555} dec_t;
487 556
488#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 557#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
489 558
490#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 559#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
588 657
589static void 658static void
590decode_he (dec_t *dec, HV *hv) 659decode_he (dec_t *dec, HV *hv)
591{ 660{
592 // for speed reasons, we specialcase single-string 661 // for speed reasons, we specialcase single-string
593 // byte or utf-8 strings as keys. 662 // byte or utf-8 strings as keys, but only when !stringref
594 663
664 if (ecb_expect_true (!dec->stringref))
595 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 665 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
596 { 666 {
597 I32 len = decode_uint (dec); 667 I32 len = decode_uint (dec);
598 char *key = (char *)dec->cur; 668 char *key = (char *)dec->cur;
599 669
600 dec->cur += len; 670 dec->cur += len;
601 671
672 if (ecb_expect_false (dec->stringref))
673 av_push (dec->stringref, newSVpvn (key, len));
674
602 hv_store (hv, key, len, decode_sv (dec), 0); 675 hv_store (hv, key, len, decode_sv (dec), 0);
676
677 return;
603 } 678 }
604 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 679 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
605 { 680 {
606 I32 len = decode_uint (dec); 681 I32 len = decode_uint (dec);
607 char *key = (char *)dec->cur; 682 char *key = (char *)dec->cur;
608 683
609 dec->cur += len; 684 dec->cur += len;
610 685
686 if (ecb_expect_false (dec->stringref))
687 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
688
611 hv_store (hv, key, -len, decode_sv (dec), 0); 689 hv_store (hv, key, -len, decode_sv (dec), 0);
690
691 return;
612 } 692 }
613 else 693
614 {
615 SV *k = decode_sv (dec); 694 SV *k = decode_sv (dec);
616 SV *v = decode_sv (dec); 695 SV *v = decode_sv (dec);
617 696
618 hv_store_ent (hv, k, v, 0); 697 hv_store_ent (hv, k, v, 0);
619 SvREFCNT_dec (k); 698 SvREFCNT_dec (k);
620 }
621} 699}
622 700
623static SV * 701static SV *
624decode_hv (dec_t *dec) 702decode_hv (dec_t *dec)
625{ 703{
691 STRLEN len = decode_uint (dec); 769 STRLEN len = decode_uint (dec);
692 770
693 WANT (len); 771 WANT (len);
694 sv = newSVpvn (dec->cur, len); 772 sv = newSVpvn (dec->cur, len);
695 dec->cur += len; 773 dec->cur += len;
774
775 if (ecb_expect_false (dec->stringref)
776 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
777 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
696 } 778 }
697 779
698 if (utf8) 780 if (utf8)
699 SvUTF8_on (sv); 781 SvUTF8_on (sv);
700 782
706} 788}
707 789
708static SV * 790static SV *
709decode_tagged (dec_t *dec) 791decode_tagged (dec_t *dec)
710{ 792{
793 SV *sv = 0;
711 UV tag = decode_uint (dec); 794 UV tag = decode_uint (dec);
795
796 WANT (1);
797
798 switch (tag)
799 {
800 case CBOR_TAG_MAGIC:
801 sv = decode_sv (dec);
802 break;
803
804 case CBOR_TAG_INDIRECTION:
805 sv = newRV_noinc (decode_sv (dec));
806 break;
807
808 case CBOR_TAG_STRINGREF_NAMESPACE:
809 {
810 ENTER; SAVETMPS;
811
812 SAVESPTR (dec->stringref);
813 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
814
815 sv = decode_sv (dec);
816
817 FREETMPS; LEAVE;
818 }
819 break;
820
821 case CBOR_TAG_STRINGREF:
822 {
823 if ((*dec->cur >> 5) != 0)
824 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
825
826 UV idx = decode_uint (dec);
827
828 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
829 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
830
831 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
832 }
833 break;
834
835 case CBOR_TAG_VALUE_SHAREABLE:
836 {
837 if (ecb_expect_false (!dec->shareable))
838 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
839
840 sv = newSV (0);
841 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
842
712 SV *sv = decode_sv (dec); 843 SV *osv = decode_sv (dec);
844 sv_setsv (sv, osv);
845 SvREFCNT_dec_NN (osv);
846 }
847 break;
713 848
714 if (tag == CBOR_TAG_MAGIC) 849 case CBOR_TAG_VALUE_SHAREDREF:
715 return sv; 850 {
716 else if (tag == CBOR_TAG_PERL_OBJECT) 851 if ((*dec->cur >> 5) != 0)
717 { 852 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
853
854 UV idx = decode_uint (dec);
855
856 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
857 ERR ("corrupted CBOR data (sharedref index out of bounds)");
858
859 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
860 }
861 break;
862
863 case CBOR_TAG_PERL_OBJECT:
864 {
865 sv = decode_sv (dec);
866
718 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 867 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
719 ERR ("corrupted CBOR data (non-array perl object)"); 868 ERR ("corrupted CBOR data (non-array perl object)");
720 869
721 AV *av = (AV *)SvRV (sv); 870 AV *av = (AV *)SvRV (sv);
722 int len = av_len (av) + 1; 871 int len = av_len (av) + 1;
723 HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); 872 HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0);
724 873
725 if (!stash) 874 if (!stash)
726 ERR ("cannot decode perl-object (package does not exist)"); 875 ERR ("cannot decode perl-object (package does not exist)");
727 876
728 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); 877 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
729 878
730 if (!method) 879 if (!method)
731 ERR ("cannot decode perl-object (package does not have a THAW method)"); 880 ERR ("cannot decode perl-object (package does not have a THAW method)");
732 881
733 dSP; 882 dSP;
734 883
735 ENTER; SAVETMPS; PUSHMARK (SP); 884 ENTER; SAVETMPS; PUSHMARK (SP);
736 EXTEND (SP, len + 1); 885 EXTEND (SP, len + 1);
737 // we re-bless the reference to get overload and other niceties right 886 // we re-bless the reference to get overload and other niceties right
738 PUSHs (*av_fetch (av, 0, 1)); 887 PUSHs (*av_fetch (av, 0, 1));
739 PUSHs (sv_cbor); 888 PUSHs (sv_cbor);
740 889
741 int i; 890 int i;
742 891
743 for (i = 1; i < len; ++i) 892 for (i = 1; i < len; ++i)
744 PUSHs (*av_fetch (av, i, 1)); 893 PUSHs (*av_fetch (av, i, 1));
745 894
746 PUTBACK; 895 PUTBACK;
747 call_sv ((SV *)GvCV (method), G_SCALAR); 896 call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL);
748 SPAGAIN; 897 SPAGAIN;
749 898
899 if (SvTRUE (ERRSV))
900 {
901 FREETMPS; LEAVE;
902 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV))));
903 }
904
750 SvREFCNT_dec (sv); 905 SvREFCNT_dec (sv);
751 sv = SvREFCNT_inc (POPs); 906 sv = SvREFCNT_inc (POPs);
752 907
753 PUTBACK; 908 PUTBACK;
754 909
755 FREETMPS; LEAVE; 910 FREETMPS; LEAVE;
911 }
912 break;
756 913
757 return sv; 914 default:
758 } 915 {
759 else 916 sv = decode_sv (dec);
760 { 917
918 dSP;
919 ENTER; SAVETMPS; PUSHMARK (SP);
920 EXTEND (SP, 2);
921 PUSHs (newSVuv (tag));
922 PUSHs (sv);
923
924 PUTBACK;
925 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
926 SPAGAIN;
927
928 if (SvTRUE (ERRSV))
929 {
930 FREETMPS; LEAVE;
931 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV))));
932 }
933
934 if (count)
935 {
936 SvREFCNT_dec (sv);
937 sv = SvREFCNT_inc (POPs);
938 }
939 else
940 {
761 AV *av = newAV (); 941 AV *av = newAV ();
762 av_push (av, newSVuv (tag)); 942 av_push (av, newSVuv (tag));
763 av_push (av, sv); 943 av_push (av, sv);
764 944
765 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 945 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
766 ? cbor_tagged_stash 946 ? cbor_tagged_stash
767 : gv_stashpv ("CBOR::XS::Tagged" , 1); 947 : gv_stashpv ("CBOR::XS::Tagged" , 1);
768
769 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 948 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
949 }
950
951 PUTBACK;
952
953 FREETMPS; LEAVE;
954 }
955 break;
770 } 956 }
957
958 return sv;
771 959
772fail: 960fail:
773 SvREFCNT_dec (sv); 961 SvREFCNT_dec (sv);
774 return &PL_sv_undef; 962 return &PL_sv_undef;
775} 963}
866} 1054}
867 1055
868static SV * 1056static SV *
869decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1057decode_cbor (SV *string, CBOR *cbor, char **offset_return)
870{ 1058{
871 dec_t dec; 1059 dec_t dec = { };
872 SV *sv; 1060 SV *sv;
873 STRLEN len; 1061 STRLEN len;
874 char *data = SvPVbyte (string, len); 1062 char *data = SvPVbyte (string, len);
875 1063
876 if (len > cbor->max_size && cbor->max_size) 1064 if (len > cbor->max_size && cbor->max_size)
878 (unsigned long)len, (unsigned long)cbor->max_size); 1066 (unsigned long)len, (unsigned long)cbor->max_size);
879 1067
880 dec.cbor = *cbor; 1068 dec.cbor = *cbor;
881 dec.cur = (U8 *)data; 1069 dec.cur = (U8 *)data;
882 dec.end = (U8 *)data + len; 1070 dec.end = (U8 *)data + len;
883 dec.err = 0;
884 dec.depth = 0;
885 1071
886 sv = decode_sv (&dec); 1072 sv = decode_sv (&dec);
887 1073
888 if (offset_return) 1074 if (offset_return)
889 *offset_return = dec.cur; 1075 *offset_return = dec.cur;
917 types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); 1103 types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1);
918 1104
919 types_true = get_bool ("Types::Serialiser::true" ); 1105 types_true = get_bool ("Types::Serialiser::true" );
920 types_false = get_bool ("Types::Serialiser::false"); 1106 types_false = get_bool ("Types::Serialiser::false");
921 types_error = get_bool ("Types::Serialiser::error"); 1107 types_error = get_bool ("Types::Serialiser::error");
1108
1109 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
922 1110
923 sv_cbor = newSVpv ("CBOR", 0); 1111 sv_cbor = newSVpv ("CBOR", 0);
924 SvREADONLY_on (sv_cbor); 1112 SvREADONLY_on (sv_cbor);
925} 1113}
926 1114
947 1135
948void shrink (CBOR *self, int enable = 1) 1136void shrink (CBOR *self, int enable = 1)
949 ALIAS: 1137 ALIAS:
950 shrink = F_SHRINK 1138 shrink = F_SHRINK
951 allow_unknown = F_ALLOW_UNKNOWN 1139 allow_unknown = F_ALLOW_UNKNOWN
1140 allow_sharing = F_ALLOW_SHARING
1141 allow_stringref = F_ALLOW_STRINGREF
952 PPCODE: 1142 PPCODE:
953{ 1143{
954 if (enable) 1144 if (enable)
955 self->flags |= ix; 1145 self->flags |= ix;
956 else 1146 else
961 1151
962void get_shrink (CBOR *self) 1152void get_shrink (CBOR *self)
963 ALIAS: 1153 ALIAS:
964 get_shrink = F_SHRINK 1154 get_shrink = F_SHRINK
965 get_allow_unknown = F_ALLOW_UNKNOWN 1155 get_allow_unknown = F_ALLOW_UNKNOWN
1156 get_allow_sharing = F_ALLOW_SHARING
1157 get_allow_stringref = F_ALLOW_STRINGREF
966 PPCODE: 1158 PPCODE:
967 XPUSHs (boolSV (self->flags & ix)); 1159 XPUSHs (boolSV (self->flags & ix));
968 1160
969void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1161void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
970 PPCODE: 1162 PPCODE:
983 XPUSHs (ST (0)); 1175 XPUSHs (ST (0));
984 1176
985int get_max_size (CBOR *self) 1177int get_max_size (CBOR *self)
986 CODE: 1178 CODE:
987 RETVAL = self->max_size; 1179 RETVAL = self->max_size;
1180 OUTPUT:
1181 RETVAL
1182
1183void filter (CBOR *self, SV *filter = 0)
1184 PPCODE:
1185 SvREFCNT_dec (self->filter);
1186 self->filter = filter ? newSVsv (filter) : filter;
1187 XPUSHs (ST (0));
1188
1189SV *get_filter (CBOR *self)
1190 CODE:
1191 RETVAL = self->filter ? self->filter : NEWSV (0, 0);
988 OUTPUT: 1192 OUTPUT:
989 RETVAL 1193 RETVAL
990 1194
991void encode (CBOR *self, SV *scalar) 1195void encode (CBOR *self, SV *scalar)
992 PPCODE: 1196 PPCODE:
1007 EXTEND (SP, 2); 1211 EXTEND (SP, 2);
1008 PUSHs (sv); 1212 PUSHs (sv);
1009 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1213 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1010} 1214}
1011 1215
1216void DESTROY (CBOR *self)
1217 PPCODE:
1218 cbor_free (self);
1219
1012PROTOTYPES: ENABLE 1220PROTOTYPES: ENABLE
1013 1221
1014void encode_cbor (SV *scalar) 1222void encode_cbor (SV *scalar)
1015 PPCODE: 1223 PPCODE:
1016{ 1224{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines