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.20 by root, Wed Nov 20 11:06:42 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)
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
122 SvPV_shrink_to_cur (sv); 131 SvPV_shrink_to_cur (sv);
123#elif defined (SvPV_renew) 132#elif defined (SvPV_renew)
124 SvPV_renew (sv, SvCUR (sv) + 1); 133 SvPV_renew (sv, SvCUR (sv) + 1);
125#endif 134#endif
126 } 135 }
136}
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;
127} 151}
128 152
129///////////////////////////////////////////////////////////////////////////// 153/////////////////////////////////////////////////////////////////////////////
130// encoder 154// encoder
131 155
200 *enc->cur++ = len >> 8; 224 *enc->cur++ = len >> 8;
201 *enc->cur++ = len; 225 *enc->cur++ = len;
202 } 226 }
203} 227}
204 228
229ecb_inline void
230encode_tag (enc_t *enc, UV tag)
231{
232 encode_uint (enc, 0xc0, tag);
233}
234
205static void 235static void
206encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 236encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
207{ 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
208 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 257 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
209 need (enc, len); 258 need (enc, len);
210 memcpy (enc->cur, str, len); 259 memcpy (enc->cur, str, len);
211 enc->cur += len; 260 enc->cur += len;
212} 261}
213 262
214ecb_inline void
215encode_tag (enc_t *enc, UV tag)
216{
217 encode_uint (enc, 0xc0, tag);
218}
219
220static void encode_sv (enc_t *enc, SV *sv); 263static void encode_sv (enc_t *enc, SV *sv);
221 264
222static void 265static void
223encode_av (enc_t *enc, AV *av) 266encode_av (enc_t *enc, AV *av)
224{ 267{
236 SV **svp = av_fetch (av, i, 0); 279 SV **svp = av_fetch (av, i, 0);
237 encode_sv (enc, svp ? *svp : &PL_sv_undef); 280 encode_sv (enc, svp ? *svp : &PL_sv_undef);
238 } 281 }
239 282
240 --enc->depth; 283 --enc->depth;
241}
242
243ecb_inline void
244encode_he (enc_t *enc, HE *he)
245{
246 if (HeKLEN (he) == HEf_SVKEY)
247 encode_sv (enc, HeSVKEY (he));
248 else
249 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
250} 284}
251 285
252static void 286static void
253encode_hv (enc_t *enc, HV *hv) 287encode_hv (enc_t *enc, HV *hv)
254{ 288{
267 else 301 else
268 encode_uint (enc, 0xa0, pairs); 302 encode_uint (enc, 0xa0, pairs);
269 303
270 while ((he = hv_iternext (hv))) 304 while ((he = hv_iternext (hv)))
271 { 305 {
272 if (ecb_expect_false (enc->cbor.flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)))
273 {
274 SV **svp;
275
276 if (HeKLEN (he) == HEf_SVKEY) 306 if (HeKLEN (he) == HEf_SVKEY)
277 svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return HE :/ 307 encode_sv (enc, HeSVKEY (he));
278 else
279 svp = hv_fetch (enc->stringref[!! HeKUTF8 (he) ], HeKEY (he), HeKLEN (he), 1);
280
281 if (SvOK (*svp))
282 {
283 encode_tag (enc, CBOR_TAG_STRINGREF);
284 encode_uint (enc, 0x00, SvUV (*svp));
285 }
286 else
287 {
288 sv_setuv (*svp, enc->stringref_idx);
289 ++enc->stringref_idx;
290 encode_he (enc, he);
291 }
292 }
293 else 308 else
294 encode_he (enc, he); 309 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
295 310
296 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 311 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
297 } 312 }
298 313
299 if (mg) 314 if (mg)
505 enc.cur = SvPVX (enc.sv); 520 enc.cur = SvPVX (enc.sv);
506 enc.end = SvEND (enc.sv); 521 enc.end = SvEND (enc.sv);
507 522
508 SvPOK_only (enc.sv); 523 SvPOK_only (enc.sv);
509 524
510 if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) 525 if (cbor->flags & F_ALLOW_STRINGREF)
511 { 526 {
512 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 527 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
513 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 528 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
514 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 529 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
515 } 530 }
537 CBOR cbor; 552 CBOR cbor;
538 U32 depth; // recursion depth 553 U32 depth; // recursion depth
539 U32 maxdepth; // recursion depth limit 554 U32 maxdepth; // recursion depth limit
540 AV *shareable; 555 AV *shareable;
541 AV *stringref; 556 AV *stringref;
557 SV *decode_tagged;
542} dec_t; 558} dec_t;
543 559
544#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
545 561
546#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")
644 660
645static void 661static void
646decode_he (dec_t *dec, HV *hv) 662decode_he (dec_t *dec, HV *hv)
647{ 663{
648 // for speed reasons, we specialcase single-string 664 // for speed reasons, we specialcase single-string
649 // byte or utf-8 strings as keys. 665 // byte or utf-8 strings as keys, but only when !stringref
650 666
667 if (ecb_expect_true (!dec->stringref))
651 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 668 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
652 { 669 {
653 I32 len = decode_uint (dec); 670 I32 len = decode_uint (dec);
654 char *key = (char *)dec->cur; 671 char *key = (char *)dec->cur;
655 672
656 dec->cur += len; 673 dec->cur += len;
657 674
658 if (ecb_expect_false (dec->stringref)) 675 if (ecb_expect_false (dec->stringref))
659 av_push (dec->stringref, newSVpvn (key, len)); 676 av_push (dec->stringref, newSVpvn (key, len));
660 677
661 hv_store (hv, key, len, decode_sv (dec), 0); 678 hv_store (hv, key, len, decode_sv (dec), 0);
679
680 return;
662 } 681 }
663 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 682 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
664 { 683 {
665 I32 len = decode_uint (dec); 684 I32 len = decode_uint (dec);
666 char *key = (char *)dec->cur; 685 char *key = (char *)dec->cur;
667 686
668 dec->cur += len; 687 dec->cur += len;
669 688
670 if (ecb_expect_false (dec->stringref)) 689 if (ecb_expect_false (dec->stringref))
671 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 690 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
672 691
673 hv_store (hv, key, -len, decode_sv (dec), 0); 692 hv_store (hv, key, -len, decode_sv (dec), 0);
693
694 return;
674 } 695 }
675 else 696
676 {
677 SV *k = decode_sv (dec); 697 SV *k = decode_sv (dec);
678 SV *v = decode_sv (dec); 698 SV *v = decode_sv (dec);
679 699
680 hv_store_ent (hv, k, v, 0); 700 hv_store_ent (hv, k, v, 0);
681 SvREFCNT_dec (k); 701 SvREFCNT_dec (k);
682 }
683} 702}
684 703
685static SV * 704static SV *
686decode_hv (dec_t *dec) 705decode_hv (dec_t *dec)
687{ 706{
753 STRLEN len = decode_uint (dec); 772 STRLEN len = decode_uint (dec);
754 773
755 WANT (len); 774 WANT (len);
756 sv = newSVpvn (dec->cur, len); 775 sv = newSVpvn (dec->cur, len);
757 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));
758 } 781 }
759 782
760 if (utf8) 783 if (utf8)
761 SvUTF8_on (sv); 784 SvUTF8_on (sv);
762
763 if (ecb_expect_false (dec->stringref))
764 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
765 785
766 return sv; 786 return sv;
767 787
768fail: 788fail:
769 SvREFCNT_dec (sv); 789 SvREFCNT_dec (sv);
896 916
897 default: 917 default:
898 { 918 {
899 sv = decode_sv (dec); 919 sv = decode_sv (dec);
900 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 {
901 AV *av = newAV (); 944 AV *av = newAV ();
902 av_push (av, newSVuv (tag)); 945 av_push (av, newSVuv (tag));
903 av_push (av, sv); 946 av_push (av, sv);
904 947
905 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 948 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
906 ? cbor_tagged_stash 949 ? cbor_tagged_stash
907 : gv_stashpv ("CBOR::XS::Tagged" , 1); 950 : gv_stashpv ("CBOR::XS::Tagged" , 1);
908
909 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); 951 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
952 }
953
954 PUTBACK;
955
956 FREETMPS; LEAVE;
910 } 957 }
911 break; 958 break;
912 } 959 }
913 960
914 return sv; 961 return sv;
1060 1107
1061 types_true = get_bool ("Types::Serialiser::true" ); 1108 types_true = get_bool ("Types::Serialiser::true" );
1062 types_false = get_bool ("Types::Serialiser::false"); 1109 types_false = get_bool ("Types::Serialiser::false");
1063 types_error = get_bool ("Types::Serialiser::error"); 1110 types_error = get_bool ("Types::Serialiser::error");
1064 1111
1112 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1113
1065 sv_cbor = newSVpv ("CBOR", 0); 1114 sv_cbor = newSVpv ("CBOR", 0);
1066 SvREADONLY_on (sv_cbor); 1115 SvREADONLY_on (sv_cbor);
1067} 1116}
1068 1117
1069PROTOTYPES: DISABLE 1118PROTOTYPES: DISABLE
1090void shrink (CBOR *self, int enable = 1) 1139void shrink (CBOR *self, int enable = 1)
1091 ALIAS: 1140 ALIAS:
1092 shrink = F_SHRINK 1141 shrink = F_SHRINK
1093 allow_unknown = F_ALLOW_UNKNOWN 1142 allow_unknown = F_ALLOW_UNKNOWN
1094 allow_sharing = F_ALLOW_SHARING 1143 allow_sharing = F_ALLOW_SHARING
1095 dedup_keys = F_DEDUP_KEYS 1144 allow_stringref = F_ALLOW_STRINGREF
1096 dedup_strings = F_DEDUP_STRINGS
1097 PPCODE: 1145 PPCODE:
1098{ 1146{
1099 if (enable) 1147 if (enable)
1100 self->flags |= ix; 1148 self->flags |= ix;
1101 else 1149 else
1107void get_shrink (CBOR *self) 1155void get_shrink (CBOR *self)
1108 ALIAS: 1156 ALIAS:
1109 get_shrink = F_SHRINK 1157 get_shrink = F_SHRINK
1110 get_allow_unknown = F_ALLOW_UNKNOWN 1158 get_allow_unknown = F_ALLOW_UNKNOWN
1111 get_allow_sharing = F_ALLOW_SHARING 1159 get_allow_sharing = F_ALLOW_SHARING
1112 get_dedup_keys = F_DEDUP_KEYS 1160 get_allow_stringref = F_ALLOW_STRINGREF
1113 get_dedup_strings = F_DEDUP_STRINGS
1114 PPCODE: 1161 PPCODE:
1115 XPUSHs (boolSV (self->flags & ix)); 1162 XPUSHs (boolSV (self->flags & ix));
1116 1163
1117void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1164void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1118 PPCODE: 1165 PPCODE:
1131 XPUSHs (ST (0)); 1178 XPUSHs (ST (0));
1132 1179
1133int get_max_size (CBOR *self) 1180int get_max_size (CBOR *self)
1134 CODE: 1181 CODE:
1135 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);
1136 OUTPUT: 1195 OUTPUT:
1137 RETVAL 1196 RETVAL
1138 1197
1139void encode (CBOR *self, SV *scalar) 1198void encode (CBOR *self, SV *scalar)
1140 PPCODE: 1199 PPCODE:
1155 EXTEND (SP, 2); 1214 EXTEND (SP, 2);
1156 PUSHs (sv); 1215 PUSHs (sv);
1157 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1216 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1158} 1217}
1159 1218
1219void DESTROY (CBOR *self)
1220 PPCODE:
1221 cbor_free (self);
1222
1160PROTOTYPES: ENABLE 1223PROTOTYPES: ENABLE
1161 1224
1162void encode_cbor (SV *scalar) 1225void encode_cbor (SV *scalar)
1163 PPCODE: 1226 PPCODE:
1164{ 1227{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines