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.18 by root, Wed Nov 20 01:09:46 2013 UTC vs.
Revision 1.28 by root, Sat Nov 23 18:30:59 2013 UTC

19# define HvNAMELEN(hv) HvNAMELEN_get (hv) 19# define HvNAMELEN(hv) HvNAMELEN_get (hv)
20#endif 20#endif
21#ifndef HvNAMEUTF8 21#ifndef HvNAMEUTF8
22# define HvNAMEUTF8(hv) 0 22# define HvNAMEUTF8(hv) 0
23#endif 23#endif
24#ifndef SvREFCNT_dec_NN
25# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
26#endif
24 27
25// known tags 28// known tags
26enum cbor_tag 29enum cbor_tag
27{ 30{
28 // inofficial extensions (pending iana registration) 31 // inofficial extensions (pending iana registration)
29 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object 32 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object
30 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object 33 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object
31 CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing 34 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing
32 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing 35 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing
33 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref 36 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref
34 CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref 37 CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref
35 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection 38 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
36 39
54 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 57 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
55 58
56 CBOR_TAG_MAGIC = 55799 // self-describe cbor 59 CBOR_TAG_MAGIC = 55799 // self-describe cbor
57}; 60};
58 61
59#define F_SHRINK 0x00000001UL 62#define F_SHRINK 0x00000001UL
60#define F_ALLOW_UNKNOWN 0x00000002UL 63#define F_ALLOW_UNKNOWN 0x00000002UL
61#define F_ALLOW_SHARING 0x00000004UL //TODO 64#define F_ALLOW_SHARING 0x00000004UL //TODO
62#define F_DEDUP_STRINGS 0x00000008UL //TODO 65#define F_ALLOW_STRINGREF 0x00000008UL //TODO
63#define F_DEDUP_KEYS 0x00000010UL //TODO
64 66
65#define INIT_SIZE 32 // initial scalar size to be allocated 67#define INIT_SIZE 32 // initial scalar size to be allocated
66 68
67#define SB do { 69#define SB do {
68#define SE } while (0) 70#define SE } while (0)
80# define CBOR_SLOW 0 82# define CBOR_SLOW 0
81# define CBOR_STASH cbor_stash 83# define CBOR_STASH cbor_stash
82#endif 84#endif
83 85
84static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: 86static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
85static SV *types_true, *types_false, *types_error, *sv_cbor; 87static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter;
86 88
87typedef struct { 89typedef struct {
88 U32 flags; 90 U32 flags;
89 U32 max_depth; 91 U32 max_depth;
90 STRLEN max_size; 92 STRLEN max_size;
93 SV *filter;
91} CBOR; 94} CBOR;
92 95
93ecb_inline void 96ecb_inline void
94cbor_init (CBOR *cbor) 97cbor_init (CBOR *cbor)
95{ 98{
96 Zero (cbor, 1, CBOR); 99 Zero (cbor, 1, CBOR);
97 cbor->max_depth = 512; 100 cbor->max_depth = 512;
101}
102
103ecb_inline void
104cbor_free (CBOR *cbor)
105{
106 SvREFCNT_dec (cbor->filter);
98} 107}
99 108
100///////////////////////////////////////////////////////////////////////////// 109/////////////////////////////////////////////////////////////////////////////
101// utility functions 110// utility functions
102 111
124 SvPV_renew (sv, SvCUR (sv) + 1); 133 SvPV_renew (sv, SvCUR (sv) + 1);
125#endif 134#endif
126 } 135 }
127} 136}
128 137
138// minimum length of a string to be registered for stringref
139ecb_inline int
140minimum_string_length (UV idx)
141{
142 return idx > 23
143 ? idx > 0xffU
144 ? idx > 0xffffU
145 ? idx > 0xffffffffU
146 ? 7
147 : 6
148 : 5
149 : 4
150 : 3;
151}
152
129///////////////////////////////////////////////////////////////////////////// 153/////////////////////////////////////////////////////////////////////////////
130// encoder 154// encoder
131 155
132// structure used for encoding CBOR 156// structure used for encoding CBOR
133typedef struct 157typedef struct
135 char *cur; // SvPVX (sv) + current output position 159 char *cur; // SvPVX (sv) + current output position
136 char *end; // SvEND (sv) 160 char *end; // SvEND (sv)
137 SV *sv; // result scalar 161 SV *sv; // result scalar
138 CBOR cbor; 162 CBOR cbor;
139 U32 depth; // recursion level 163 U32 depth; // recursion level
140 HV *stringref; // string => index, or 0 164 HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8)
165 UV stringref_idx;
141 HV *sharable; // ptr => index, or 0 166 HV *shareable; // ptr => index, or 0
142 HV *sharable_idx; 167 UV shareable_idx;
143} enc_t; 168} enc_t;
144 169
145ecb_inline void 170ecb_inline void
146need (enc_t *enc, STRLEN len) 171need (enc_t *enc, STRLEN len)
147{ 172{
199 *enc->cur++ = len >> 8; 224 *enc->cur++ = len >> 8;
200 *enc->cur++ = len; 225 *enc->cur++ = len;
201 } 226 }
202} 227}
203 228
229ecb_inline void
230encode_tag (enc_t *enc, UV tag)
231{
232 encode_uint (enc, 0xc0, tag);
233}
234
204static void 235static void
205encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 236encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
206{ 237{
238 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF))
239 {
240 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
241
242 if (SvOK (*svp))
243 {
244 // already registered, use stringref
245 encode_tag (enc, CBOR_TAG_STRINGREF);
246 encode_uint (enc, 0x00, SvUV (*svp));
247 return;
248 }
249 else if (len >= minimum_string_length (enc->stringref_idx))
250 {
251 // register only
252 sv_setuv (*svp, enc->stringref_idx);
253 ++enc->stringref_idx;
254 }
255 }
256
207 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 257 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
208 need (enc, len); 258 need (enc, len);
209 memcpy (enc->cur, str, len); 259 memcpy (enc->cur, str, len);
210 enc->cur += len; 260 enc->cur += len;
211} 261}
212 262
213ecb_inline void
214encode_tag (enc_t *enc, UV tag)
215{
216 encode_uint (enc, 0xc0, tag);
217}
218
219static int
220encode_sharable2 (enc_t *enc, SV *sv)
221{
222 if (!enc->sharable)
223 enc->sharable = (HV *)sv_2mortal ((SV *)newHV ());
224
225 SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1);
226
227 if (SvOK (*svp))
228 {
229 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
230 encode_uint (enc, 0x00, SvUV (*svp));
231
232 return 1;
233 }
234 else
235 {
236 sv_setuv (*svp, enc->sharable_idx++);
237 encode_tag (enc, CBOR_TAG_VALUE_SHARABLE);
238
239 return 0;
240 }
241}
242
243ecb_inline int
244encode_sharable (enc_t *enc, SV *sv)
245{
246 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
247 && ecb_expect_false (SvREFCNT (sv) > 1))
248 return encode_sharable2 (enc, sv);
249
250 return 0;
251}
252
253static void encode_sv (enc_t *enc, SV *sv); 263static void encode_sv (enc_t *enc, SV *sv);
254 264
255static void 265static void
256encode_av (enc_t *enc, AV *av) 266encode_av (enc_t *enc, AV *av)
257{ 267{
309 319
310// encode objects, arrays and special \0=false and \1=true values. 320// encode objects, arrays and special \0=false and \1=true values.
311static void 321static void
312encode_rv (enc_t *enc, SV *sv) 322encode_rv (enc_t *enc, SV *sv)
313{ 323{
314 svtype svt;
315
316 SvGETMAGIC (sv); 324 SvGETMAGIC (sv);
325
326 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
327 && ecb_expect_false (SvREFCNT (sv) > 1))
328 {
329 if (!enc->shareable)
330 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
331
332 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
333
334 if (SvOK (*svp))
335 {
336 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
337 encode_uint (enc, 0x00, SvUV (*svp));
338 return;
339 }
340 else
341 {
342 sv_setuv (*svp, enc->shareable_idx);
343 ++enc->shareable_idx;
344 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
345 }
346 }
347
317 svt = SvTYPE (sv); 348 svtype svt = SvTYPE (sv);
318
319 if (encode_sharable (enc, sv))
320 return;
321 349
322 if (ecb_expect_false (SvOBJECT (sv))) 350 if (ecb_expect_false (SvOBJECT (sv)))
323 { 351 {
324 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash 352 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
325 ? types_boolean_stash 353 ? types_boolean_stash
452static void 480static void
453encode_sv (enc_t *enc, SV *sv) 481encode_sv (enc_t *enc, SV *sv)
454{ 482{
455 SvGETMAGIC (sv); 483 SvGETMAGIC (sv);
456 484
457 if (encode_sharable (enc, sv))
458 return;
459
460 if (SvPOKp (sv)) 485 if (SvPOKp (sv))
461 { 486 {
462 STRLEN len; 487 STRLEN len;
463 char *str = SvPV (sv, len); 488 char *str = SvPV (sv, len);
464 encode_str (enc, SvUTF8 (sv), str, len); 489 encode_str (enc, SvUTF8 (sv), str, len);
494 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 519 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
495 enc.cur = SvPVX (enc.sv); 520 enc.cur = SvPVX (enc.sv);
496 enc.end = SvEND (enc.sv); 521 enc.end = SvEND (enc.sv);
497 522
498 SvPOK_only (enc.sv); 523 SvPOK_only (enc.sv);
524
525 if (cbor->flags & F_ALLOW_STRINGREF)
526 {
527 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
528 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
529 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
530 }
531
499 encode_sv (&enc, scalar); 532 encode_sv (&enc, scalar);
500 533
501 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 534 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
502 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 535 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
503 536
517 U8 *end; // end of input string 550 U8 *end; // end of input string
518 const char *err; // parse error, if != 0 551 const char *err; // parse error, if != 0
519 CBOR cbor; 552 CBOR cbor;
520 U32 depth; // recursion depth 553 U32 depth; // recursion depth
521 U32 maxdepth; // recursion depth limit 554 U32 maxdepth; // recursion depth limit
522 AV *sharable; 555 AV *shareable;
556 AV *stringref;
557 SV *decode_tagged;
523} dec_t; 558} dec_t;
524 559
525#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 560#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
526 561
527#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 562#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
625 660
626static void 661static void
627decode_he (dec_t *dec, HV *hv) 662decode_he (dec_t *dec, HV *hv)
628{ 663{
629 // for speed reasons, we specialcase single-string 664 // for speed reasons, we specialcase single-string
630 // byte or utf-8 strings as keys. 665 // byte or utf-8 strings as keys, but only when !stringref
631 666
667 if (ecb_expect_true (!dec->stringref))
632 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 668 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
633 { 669 {
634 I32 len = decode_uint (dec); 670 I32 len = decode_uint (dec);
635 char *key = (char *)dec->cur; 671 char *key = (char *)dec->cur;
636 672
637 dec->cur += len; 673 dec->cur += len;
638 674
675 if (ecb_expect_false (dec->stringref))
676 av_push (dec->stringref, newSVpvn (key, len));
677
639 hv_store (hv, key, len, decode_sv (dec), 0); 678 hv_store (hv, key, len, decode_sv (dec), 0);
679
680 return;
640 } 681 }
641 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 682 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
642 { 683 {
643 I32 len = decode_uint (dec); 684 I32 len = decode_uint (dec);
644 char *key = (char *)dec->cur; 685 char *key = (char *)dec->cur;
645 686
646 dec->cur += len; 687 dec->cur += len;
647 688
689 if (ecb_expect_false (dec->stringref))
690 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
691
648 hv_store (hv, key, -len, decode_sv (dec), 0); 692 hv_store (hv, key, -len, decode_sv (dec), 0);
693
694 return;
649 } 695 }
650 else 696
651 {
652 SV *k = decode_sv (dec); 697 SV *k = decode_sv (dec);
653 SV *v = decode_sv (dec); 698 SV *v = decode_sv (dec);
654 699
655 hv_store_ent (hv, k, v, 0); 700 hv_store_ent (hv, k, v, 0);
656 SvREFCNT_dec (k); 701 SvREFCNT_dec (k);
657 }
658} 702}
659 703
660static SV * 704static SV *
661decode_hv (dec_t *dec) 705decode_hv (dec_t *dec)
662{ 706{
728 STRLEN len = decode_uint (dec); 772 STRLEN len = decode_uint (dec);
729 773
730 WANT (len); 774 WANT (len);
731 sv = newSVpvn (dec->cur, len); 775 sv = newSVpvn (dec->cur, len);
732 dec->cur += len; 776 dec->cur += len;
777
778 if (ecb_expect_false (dec->stringref)
779 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
780 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
733 } 781 }
734 782
735 if (utf8) 783 if (utf8)
736 SvUTF8_on (sv); 784 SvUTF8_on (sv);
737 785
743} 791}
744 792
745static SV * 793static SV *
746decode_tagged (dec_t *dec) 794decode_tagged (dec_t *dec)
747{ 795{
796 SV *sv = 0;
748 UV tag = decode_uint (dec); 797 UV tag = decode_uint (dec);
749 SV *sv = decode_sv (dec); 798
799 WANT (1);
750 800
751 switch (tag) 801 switch (tag)
752 { 802 {
753 case CBOR_TAG_MAGIC: 803 case CBOR_TAG_MAGIC:
754 return sv; 804 sv = decode_sv (dec);
805 break;
755 806
756 case CBOR_TAG_INDIRECTION: 807 case CBOR_TAG_INDIRECTION:
757 return newRV_noinc (sv); 808 sv = newRV_noinc (decode_sv (dec));
809 break;
758 810
811 case CBOR_TAG_STRINGREF_NAMESPACE:
812 {
813 ENTER; SAVETMPS;
814
815 SAVESPTR (dec->stringref);
816 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
817
818 sv = decode_sv (dec);
819
820 FREETMPS; LEAVE;
821 }
822 break;
823
824 case CBOR_TAG_STRINGREF:
825 {
826 if ((*dec->cur >> 5) != 0)
827 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
828
829 UV idx = decode_uint (dec);
830
831 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
832 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
833
834 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
835 }
836 break;
837
759 case CBOR_TAG_VALUE_SHARABLE: 838 case CBOR_TAG_VALUE_SHAREABLE:
839 {
760 if (ecb_expect_false (!dec->sharable)) 840 if (ecb_expect_false (!dec->shareable))
761 dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); 841 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
762 842
843 sv = newSV (0);
763 av_push (dec->sharable, SvREFCNT_inc_NN (sv)); 844 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
764 845
765 return sv; 846 SV *osv = decode_sv (dec);
847 sv_setsv (sv, osv);
848 SvREFCNT_dec_NN (osv);
849 }
850 break;
766 851
767 case CBOR_TAG_VALUE_SHAREDREF: 852 case CBOR_TAG_VALUE_SHAREDREF:
768 { 853 {
769 // TODO: should verify that the sv atcually was a CBOR unsigned integer 854 if ((*dec->cur >> 5) != 0)
770 UV idx = SvUV (sv); 855 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
771 856
857 UV idx = decode_uint (dec);
858
772 if (!dec->sharable || idx > AvFILLp (dec->sharable)) 859 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
773 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 860 ERR ("corrupted CBOR data (sharedref index out of bounds)");
774 861
775 SvREFCNT_dec (sv);
776
777 return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); 862 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
778 } 863 }
864 break;
779 865
780 case CBOR_TAG_PERL_OBJECT: 866 case CBOR_TAG_PERL_OBJECT:
781 { 867 {
868 sv = decode_sv (dec);
869
782 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 870 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
783 ERR ("corrupted CBOR data (non-array perl object)"); 871 ERR ("corrupted CBOR data (non-array perl object)");
784 872
785 AV *av = (AV *)SvRV (sv); 873 AV *av = (AV *)SvRV (sv);
786 int len = av_len (av) + 1; 874 int len = av_len (av) + 1;
821 sv = SvREFCNT_inc (POPs); 909 sv = SvREFCNT_inc (POPs);
822 910
823 PUTBACK; 911 PUTBACK;
824 912
825 FREETMPS; LEAVE; 913 FREETMPS; LEAVE;
826
827 return sv;
828 } 914 }
915 break;
829 916
830 default: 917 default:
831 { 918 {
919 sv = decode_sv (dec);
920
921 dSP;
922 ENTER; SAVETMPS; PUSHMARK (SP);
923 EXTEND (SP, 2);
924 PUSHs (newSVuv (tag));
925 PUSHs (sv);
926
927 PUTBACK;
928 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
929 SPAGAIN;
930
931 if (SvTRUE (ERRSV))
932 {
933 FREETMPS; LEAVE;
934 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV))));
935 }
936
937 if (count)
938 {
939 SvREFCNT_dec (sv);
940 sv = SvREFCNT_inc (POPs);
941 }
942 else
943 {
832 AV *av = newAV (); 944 AV *av = newAV ();
833 av_push (av, newSVuv (tag)); 945 av_push (av, newSVuv (tag));
834 av_push (av, sv); 946 av_push (av, sv);
835 947
836 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 948 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
837 ? cbor_tagged_stash 949 ? cbor_tagged_stash
838 : gv_stashpv ("CBOR::XS::Tagged" , 1); 950 : gv_stashpv ("CBOR::XS::Tagged" , 1);
839
840 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 951 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
841 } 952 }
953
954 PUTBACK;
955
956 FREETMPS; LEAVE;
957 }
958 break;
842 } 959 }
960
961 return sv;
843 962
844fail: 963fail:
845 SvREFCNT_dec (sv); 964 SvREFCNT_dec (sv);
846 return &PL_sv_undef; 965 return &PL_sv_undef;
847} 966}
988 1107
989 types_true = get_bool ("Types::Serialiser::true" ); 1108 types_true = get_bool ("Types::Serialiser::true" );
990 types_false = get_bool ("Types::Serialiser::false"); 1109 types_false = get_bool ("Types::Serialiser::false");
991 types_error = get_bool ("Types::Serialiser::error"); 1110 types_error = get_bool ("Types::Serialiser::error");
992 1111
1112 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1113
993 sv_cbor = newSVpv ("CBOR", 0); 1114 sv_cbor = newSVpv ("CBOR", 0);
994 SvREADONLY_on (sv_cbor); 1115 SvREADONLY_on (sv_cbor);
995} 1116}
996 1117
997PROTOTYPES: DISABLE 1118PROTOTYPES: DISABLE
1018void shrink (CBOR *self, int enable = 1) 1139void shrink (CBOR *self, int enable = 1)
1019 ALIAS: 1140 ALIAS:
1020 shrink = F_SHRINK 1141 shrink = F_SHRINK
1021 allow_unknown = F_ALLOW_UNKNOWN 1142 allow_unknown = F_ALLOW_UNKNOWN
1022 allow_sharing = F_ALLOW_SHARING 1143 allow_sharing = F_ALLOW_SHARING
1023 dedup_keys = F_DEDUP_KEYS 1144 allow_stringref = F_ALLOW_STRINGREF
1024 dedup_strings = F_DEDUP_STRINGS
1025 PPCODE: 1145 PPCODE:
1026{ 1146{
1027 if (enable) 1147 if (enable)
1028 self->flags |= ix; 1148 self->flags |= ix;
1029 else 1149 else
1035void get_shrink (CBOR *self) 1155void get_shrink (CBOR *self)
1036 ALIAS: 1156 ALIAS:
1037 get_shrink = F_SHRINK 1157 get_shrink = F_SHRINK
1038 get_allow_unknown = F_ALLOW_UNKNOWN 1158 get_allow_unknown = F_ALLOW_UNKNOWN
1039 get_allow_sharing = F_ALLOW_SHARING 1159 get_allow_sharing = F_ALLOW_SHARING
1040 get_dedup_keys = F_DEDUP_KEYS 1160 get_allow_stringref = F_ALLOW_STRINGREF
1041 get_dedup_strings = F_DEDUP_STRINGS
1042 PPCODE: 1161 PPCODE:
1043 XPUSHs (boolSV (self->flags & ix)); 1162 XPUSHs (boolSV (self->flags & ix));
1044 1163
1045void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1164void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1046 PPCODE: 1165 PPCODE:
1059 XPUSHs (ST (0)); 1178 XPUSHs (ST (0));
1060 1179
1061int get_max_size (CBOR *self) 1180int get_max_size (CBOR *self)
1062 CODE: 1181 CODE:
1063 RETVAL = self->max_size; 1182 RETVAL = self->max_size;
1183 OUTPUT:
1184 RETVAL
1185
1186void filter (CBOR *self, SV *filter = 0)
1187 PPCODE:
1188 SvREFCNT_dec (self->filter);
1189 self->filter = filter ? newSVsv (filter) : filter;
1190 XPUSHs (ST (0));
1191
1192SV *get_filter (CBOR *self)
1193 CODE:
1194 RETVAL = self->filter ? self->filter : NEWSV (0, 0);
1064 OUTPUT: 1195 OUTPUT:
1065 RETVAL 1196 RETVAL
1066 1197
1067void encode (CBOR *self, SV *scalar) 1198void encode (CBOR *self, SV *scalar)
1068 PPCODE: 1199 PPCODE:
1083 EXTEND (SP, 2); 1214 EXTEND (SP, 2);
1084 PUSHs (sv); 1215 PUSHs (sv);
1085 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1216 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1086} 1217}
1087 1218
1219void DESTROY (CBOR *self)
1220 PPCODE:
1221 cbor_free (self);
1222
1088PROTOTYPES: ENABLE 1223PROTOTYPES: ENABLE
1089 1224
1090void encode_cbor (SV *scalar) 1225void encode_cbor (SV *scalar)
1091 PPCODE: 1226 PPCODE:
1092{ 1227{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines