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.27 by root, Fri Nov 22 15:28:38 2013 UTC

54 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 54 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
55 55
56 CBOR_TAG_MAGIC = 55799 // self-describe cbor 56 CBOR_TAG_MAGIC = 55799 // self-describe cbor
57}; 57};
58 58
59#define F_SHRINK 0x00000001UL 59#define F_SHRINK 0x00000001UL
60#define F_ALLOW_UNKNOWN 0x00000002UL 60#define F_ALLOW_UNKNOWN 0x00000002UL
61#define F_ALLOW_SHARING 0x00000004UL //TODO 61#define F_ALLOW_SHARING 0x00000004UL //TODO
62#define F_DEDUP_STRINGS 0x00000008UL //TODO 62#define F_ALLOW_STRINGREF 0x00000008UL //TODO
63#define F_DEDUP_KEYS 0x00000010UL //TODO
64 63
65#define INIT_SIZE 32 // initial scalar size to be allocated 64#define INIT_SIZE 32 // initial scalar size to be allocated
66 65
67#define SB do { 66#define SB do {
68#define SE } while (0) 67#define SE } while (0)
80# define CBOR_SLOW 0 79# define CBOR_SLOW 0
81# define CBOR_STASH cbor_stash 80# define CBOR_STASH cbor_stash
82#endif 81#endif
83 82
84static 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::
85static SV *types_true, *types_false, *types_error, *sv_cbor; 84static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter;
86 85
87typedef struct { 86typedef struct {
88 U32 flags; 87 U32 flags;
89 U32 max_depth; 88 U32 max_depth;
90 STRLEN max_size; 89 STRLEN max_size;
90 SV *filter;
91} CBOR; 91} CBOR;
92 92
93ecb_inline void 93ecb_inline void
94cbor_init (CBOR *cbor) 94cbor_init (CBOR *cbor)
95{ 95{
96 Zero (cbor, 1, CBOR); 96 Zero (cbor, 1, CBOR);
97 cbor->max_depth = 512; 97 cbor->max_depth = 512;
98}
99
100ecb_inline void
101cbor_free (CBOR *cbor)
102{
103 SvREFCNT_dec (cbor->filter);
98} 104}
99 105
100///////////////////////////////////////////////////////////////////////////// 106/////////////////////////////////////////////////////////////////////////////
101// utility functions 107// utility functions
102 108
122 SvPV_shrink_to_cur (sv); 128 SvPV_shrink_to_cur (sv);
123#elif defined (SvPV_renew) 129#elif defined (SvPV_renew)
124 SvPV_renew (sv, SvCUR (sv) + 1); 130 SvPV_renew (sv, SvCUR (sv) + 1);
125#endif 131#endif
126 } 132 }
133}
134
135// minimum length of a string to be registered for stringref
136ecb_inline int
137minimum_string_length (UV idx)
138{
139 return idx > 23
140 ? idx > 0xffU
141 ? idx > 0xffffU
142 ? idx > 0xffffffffU
143 ? 7
144 : 6
145 : 5
146 : 4
147 : 3;
127} 148}
128 149
129///////////////////////////////////////////////////////////////////////////// 150/////////////////////////////////////////////////////////////////////////////
130// encoder 151// encoder
131 152
200 *enc->cur++ = len >> 8; 221 *enc->cur++ = len >> 8;
201 *enc->cur++ = len; 222 *enc->cur++ = len;
202 } 223 }
203} 224}
204 225
226ecb_inline void
227encode_tag (enc_t *enc, UV tag)
228{
229 encode_uint (enc, 0xc0, tag);
230}
231
205static void 232static void
206encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 233encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
207{ 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
208 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 254 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
209 need (enc, len); 255 need (enc, len);
210 memcpy (enc->cur, str, len); 256 memcpy (enc->cur, str, len);
211 enc->cur += len; 257 enc->cur += len;
212} 258}
213 259
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); 260static void encode_sv (enc_t *enc, SV *sv);
221 261
222static void 262static void
223encode_av (enc_t *enc, AV *av) 263encode_av (enc_t *enc, AV *av)
224{ 264{
236 SV **svp = av_fetch (av, i, 0); 276 SV **svp = av_fetch (av, i, 0);
237 encode_sv (enc, svp ? *svp : &PL_sv_undef); 277 encode_sv (enc, svp ? *svp : &PL_sv_undef);
238 } 278 }
239 279
240 --enc->depth; 280 --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} 281}
251 282
252static void 283static void
253encode_hv (enc_t *enc, HV *hv) 284encode_hv (enc_t *enc, HV *hv)
254{ 285{
267 else 298 else
268 encode_uint (enc, 0xa0, pairs); 299 encode_uint (enc, 0xa0, pairs);
269 300
270 while ((he = hv_iternext (hv))) 301 while ((he = hv_iternext (hv)))
271 { 302 {
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) 303 if (HeKLEN (he) == HEf_SVKEY)
277 svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return HE :/ 304 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 305 else
294 encode_he (enc, he); 306 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
295 307
296 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 308 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
297 } 309 }
298 310
299 if (mg) 311 if (mg)
505 enc.cur = SvPVX (enc.sv); 517 enc.cur = SvPVX (enc.sv);
506 enc.end = SvEND (enc.sv); 518 enc.end = SvEND (enc.sv);
507 519
508 SvPOK_only (enc.sv); 520 SvPOK_only (enc.sv);
509 521
510 if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) 522 if (cbor->flags & F_ALLOW_STRINGREF)
511 { 523 {
512 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 524 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
513 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 525 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
514 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 526 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
515 } 527 }
537 CBOR cbor; 549 CBOR cbor;
538 U32 depth; // recursion depth 550 U32 depth; // recursion depth
539 U32 maxdepth; // recursion depth limit 551 U32 maxdepth; // recursion depth limit
540 AV *shareable; 552 AV *shareable;
541 AV *stringref; 553 AV *stringref;
554 SV *decode_tagged;
542} dec_t; 555} dec_t;
543 556
544#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
545 558
546#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")
644 657
645static void 658static void
646decode_he (dec_t *dec, HV *hv) 659decode_he (dec_t *dec, HV *hv)
647{ 660{
648 // for speed reasons, we specialcase single-string 661 // for speed reasons, we specialcase single-string
649 // byte or utf-8 strings as keys. 662 // byte or utf-8 strings as keys, but only when !stringref
650 663
664 if (ecb_expect_true (!dec->stringref))
651 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 665 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
652 { 666 {
653 I32 len = decode_uint (dec); 667 I32 len = decode_uint (dec);
654 char *key = (char *)dec->cur; 668 char *key = (char *)dec->cur;
655 669
656 dec->cur += len; 670 dec->cur += len;
657 671
658 if (ecb_expect_false (dec->stringref)) 672 if (ecb_expect_false (dec->stringref))
659 av_push (dec->stringref, newSVpvn (key, len)); 673 av_push (dec->stringref, newSVpvn (key, len));
660 674
661 hv_store (hv, key, len, decode_sv (dec), 0); 675 hv_store (hv, key, len, decode_sv (dec), 0);
676
677 return;
662 } 678 }
663 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 679 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
664 { 680 {
665 I32 len = decode_uint (dec); 681 I32 len = decode_uint (dec);
666 char *key = (char *)dec->cur; 682 char *key = (char *)dec->cur;
667 683
668 dec->cur += len; 684 dec->cur += len;
669 685
670 if (ecb_expect_false (dec->stringref)) 686 if (ecb_expect_false (dec->stringref))
671 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 687 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
672 688
673 hv_store (hv, key, -len, decode_sv (dec), 0); 689 hv_store (hv, key, -len, decode_sv (dec), 0);
690
691 return;
674 } 692 }
675 else 693
676 {
677 SV *k = decode_sv (dec); 694 SV *k = decode_sv (dec);
678 SV *v = decode_sv (dec); 695 SV *v = decode_sv (dec);
679 696
680 hv_store_ent (hv, k, v, 0); 697 hv_store_ent (hv, k, v, 0);
681 SvREFCNT_dec (k); 698 SvREFCNT_dec (k);
682 }
683} 699}
684 700
685static SV * 701static SV *
686decode_hv (dec_t *dec) 702decode_hv (dec_t *dec)
687{ 703{
753 STRLEN len = decode_uint (dec); 769 STRLEN len = decode_uint (dec);
754 770
755 WANT (len); 771 WANT (len);
756 sv = newSVpvn (dec->cur, len); 772 sv = newSVpvn (dec->cur, len);
757 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));
758 } 778 }
759 779
760 if (utf8) 780 if (utf8)
761 SvUTF8_on (sv); 781 SvUTF8_on (sv);
762
763 if (ecb_expect_false (dec->stringref))
764 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
765 782
766 return sv; 783 return sv;
767 784
768fail: 785fail:
769 SvREFCNT_dec (sv); 786 SvREFCNT_dec (sv);
896 913
897 default: 914 default:
898 { 915 {
899 sv = decode_sv (dec); 916 sv = decode_sv (dec);
900 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 {
901 AV *av = newAV (); 941 AV *av = newAV ();
902 av_push (av, newSVuv (tag)); 942 av_push (av, newSVuv (tag));
903 av_push (av, sv); 943 av_push (av, sv);
904 944
905 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 945 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
906 ? cbor_tagged_stash 946 ? cbor_tagged_stash
907 : gv_stashpv ("CBOR::XS::Tagged" , 1); 947 : gv_stashpv ("CBOR::XS::Tagged" , 1);
908
909 sv = 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;
910 } 954 }
911 break; 955 break;
912 } 956 }
913 957
914 return sv; 958 return sv;
1060 1104
1061 types_true = get_bool ("Types::Serialiser::true" ); 1105 types_true = get_bool ("Types::Serialiser::true" );
1062 types_false = get_bool ("Types::Serialiser::false"); 1106 types_false = get_bool ("Types::Serialiser::false");
1063 types_error = get_bool ("Types::Serialiser::error"); 1107 types_error = get_bool ("Types::Serialiser::error");
1064 1108
1109 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1110
1065 sv_cbor = newSVpv ("CBOR", 0); 1111 sv_cbor = newSVpv ("CBOR", 0);
1066 SvREADONLY_on (sv_cbor); 1112 SvREADONLY_on (sv_cbor);
1067} 1113}
1068 1114
1069PROTOTYPES: DISABLE 1115PROTOTYPES: DISABLE
1090void shrink (CBOR *self, int enable = 1) 1136void shrink (CBOR *self, int enable = 1)
1091 ALIAS: 1137 ALIAS:
1092 shrink = F_SHRINK 1138 shrink = F_SHRINK
1093 allow_unknown = F_ALLOW_UNKNOWN 1139 allow_unknown = F_ALLOW_UNKNOWN
1094 allow_sharing = F_ALLOW_SHARING 1140 allow_sharing = F_ALLOW_SHARING
1095 dedup_keys = F_DEDUP_KEYS 1141 allow_stringref = F_ALLOW_STRINGREF
1096 dedup_strings = F_DEDUP_STRINGS
1097 PPCODE: 1142 PPCODE:
1098{ 1143{
1099 if (enable) 1144 if (enable)
1100 self->flags |= ix; 1145 self->flags |= ix;
1101 else 1146 else
1107void get_shrink (CBOR *self) 1152void get_shrink (CBOR *self)
1108 ALIAS: 1153 ALIAS:
1109 get_shrink = F_SHRINK 1154 get_shrink = F_SHRINK
1110 get_allow_unknown = F_ALLOW_UNKNOWN 1155 get_allow_unknown = F_ALLOW_UNKNOWN
1111 get_allow_sharing = F_ALLOW_SHARING 1156 get_allow_sharing = F_ALLOW_SHARING
1112 get_dedup_keys = F_DEDUP_KEYS 1157 get_allow_stringref = F_ALLOW_STRINGREF
1113 get_dedup_strings = F_DEDUP_STRINGS
1114 PPCODE: 1158 PPCODE:
1115 XPUSHs (boolSV (self->flags & ix)); 1159 XPUSHs (boolSV (self->flags & ix));
1116 1160
1117void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1161void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1118 PPCODE: 1162 PPCODE:
1131 XPUSHs (ST (0)); 1175 XPUSHs (ST (0));
1132 1176
1133int get_max_size (CBOR *self) 1177int get_max_size (CBOR *self)
1134 CODE: 1178 CODE:
1135 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);
1136 OUTPUT: 1192 OUTPUT:
1137 RETVAL 1193 RETVAL
1138 1194
1139void encode (CBOR *self, SV *scalar) 1195void encode (CBOR *self, SV *scalar)
1140 PPCODE: 1196 PPCODE:
1155 EXTEND (SP, 2); 1211 EXTEND (SP, 2);
1156 PUSHs (sv); 1212 PUSHs (sv);
1157 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1213 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1158} 1214}
1159 1215
1216void DESTROY (CBOR *self)
1217 PPCODE:
1218 cbor_free (self);
1219
1160PROTOTYPES: ENABLE 1220PROTOTYPES: ENABLE
1161 1221
1162void encode_cbor (SV *scalar) 1222void encode_cbor (SV *scalar)
1163 PPCODE: 1223 PPCODE:
1164{ 1224{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines