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.25 by root, Fri Nov 22 05:54:07 2013 UTC

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 = 24, // http://cbor.schmorp.de/perl-object 29 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object
30 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object 30 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object
31 CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing 31 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing
32 CBOR_TAG_VALUE_SHAREDREF = 27, // 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 33 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref
34 CBOR_TAG_STRINGREF = 28, // 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 35 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
36 36
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)
124 SvPV_renew (sv, SvCUR (sv) + 1); 123 SvPV_renew (sv, SvCUR (sv) + 1);
125#endif 124#endif
126 } 125 }
127} 126}
128 127
128// minimum length of a string to be registered for stringref
129ecb_inline int
130minimum_string_length (UV idx)
131{
132 return idx > 23
133 ? idx > 0xffU
134 ? idx > 0xffffU
135 ? idx > 0xffffffffU
136 ? 7
137 : 6
138 : 5
139 : 4
140 : 3;
141}
142
129///////////////////////////////////////////////////////////////////////////// 143/////////////////////////////////////////////////////////////////////////////
130// encoder 144// encoder
131 145
132// structure used for encoding CBOR 146// structure used for encoding CBOR
133typedef struct 147typedef struct
135 char *cur; // SvPVX (sv) + current output position 149 char *cur; // SvPVX (sv) + current output position
136 char *end; // SvEND (sv) 150 char *end; // SvEND (sv)
137 SV *sv; // result scalar 151 SV *sv; // result scalar
138 CBOR cbor; 152 CBOR cbor;
139 U32 depth; // recursion level 153 U32 depth; // recursion level
140 HV *stringref; // string => index, or 0 154 HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8)
155 UV stringref_idx;
141 HV *sharable; // ptr => index, or 0 156 HV *shareable; // ptr => index, or 0
142 HV *sharable_idx; 157 UV shareable_idx;
143} enc_t; 158} enc_t;
144 159
145ecb_inline void 160ecb_inline void
146need (enc_t *enc, STRLEN len) 161need (enc_t *enc, STRLEN len)
147{ 162{
199 *enc->cur++ = len >> 8; 214 *enc->cur++ = len >> 8;
200 *enc->cur++ = len; 215 *enc->cur++ = len;
201 } 216 }
202} 217}
203 218
219ecb_inline void
220encode_tag (enc_t *enc, UV tag)
221{
222 encode_uint (enc, 0xc0, tag);
223}
224
204static void 225static void
205encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 226encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
206{ 227{
228 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF))
229 {
230 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
231
232 if (SvOK (*svp))
233 {
234 // already registered, use stringref
235 encode_tag (enc, CBOR_TAG_STRINGREF);
236 encode_uint (enc, 0x00, SvUV (*svp));
237 return;
238 }
239 else if (len >= minimum_string_length (enc->stringref_idx))
240 {
241 // register only
242 sv_setuv (*svp, enc->stringref_idx);
243 ++enc->stringref_idx;
244 }
245 }
246
207 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 247 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
208 need (enc, len); 248 need (enc, len);
209 memcpy (enc->cur, str, len); 249 memcpy (enc->cur, str, len);
210 enc->cur += len; 250 enc->cur += len;
211} 251}
212 252
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); 253static void encode_sv (enc_t *enc, SV *sv);
254 254
255static void 255static void
256encode_av (enc_t *enc, AV *av) 256encode_av (enc_t *enc, AV *av)
257{ 257{
269 SV **svp = av_fetch (av, i, 0); 269 SV **svp = av_fetch (av, i, 0);
270 encode_sv (enc, svp ? *svp : &PL_sv_undef); 270 encode_sv (enc, svp ? *svp : &PL_sv_undef);
271 } 271 }
272 272
273 --enc->depth; 273 --enc->depth;
274}
275
276ecb_inline void
277encode_he (enc_t *enc, HE *he)
278{
274} 279}
275 280
276static void 281static void
277encode_hv (enc_t *enc, HV *hv) 282encode_hv (enc_t *enc, HV *hv)
278{ 283{
309 314
310// encode objects, arrays and special \0=false and \1=true values. 315// encode objects, arrays and special \0=false and \1=true values.
311static void 316static void
312encode_rv (enc_t *enc, SV *sv) 317encode_rv (enc_t *enc, SV *sv)
313{ 318{
314 svtype svt;
315
316 SvGETMAGIC (sv); 319 SvGETMAGIC (sv);
320
321 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
322 && ecb_expect_false (SvREFCNT (sv) > 1))
323 {
324 if (!enc->shareable)
325 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
326
327 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
328
329 if (SvOK (*svp))
330 {
331 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
332 encode_uint (enc, 0x00, SvUV (*svp));
333 return;
334 }
335 else
336 {
337 sv_setuv (*svp, enc->shareable_idx);
338 ++enc->shareable_idx;
339 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
340 }
341 }
342
317 svt = SvTYPE (sv); 343 svtype svt = SvTYPE (sv);
318
319 if (encode_sharable (enc, sv))
320 return;
321 344
322 if (ecb_expect_false (SvOBJECT (sv))) 345 if (ecb_expect_false (SvOBJECT (sv)))
323 { 346 {
324 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash 347 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
325 ? types_boolean_stash 348 ? types_boolean_stash
452static void 475static void
453encode_sv (enc_t *enc, SV *sv) 476encode_sv (enc_t *enc, SV *sv)
454{ 477{
455 SvGETMAGIC (sv); 478 SvGETMAGIC (sv);
456 479
457 if (encode_sharable (enc, sv))
458 return;
459
460 if (SvPOKp (sv)) 480 if (SvPOKp (sv))
461 { 481 {
462 STRLEN len; 482 STRLEN len;
463 char *str = SvPV (sv, len); 483 char *str = SvPV (sv, len);
464 encode_str (enc, SvUTF8 (sv), str, len); 484 encode_str (enc, SvUTF8 (sv), str, len);
494 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 514 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
495 enc.cur = SvPVX (enc.sv); 515 enc.cur = SvPVX (enc.sv);
496 enc.end = SvEND (enc.sv); 516 enc.end = SvEND (enc.sv);
497 517
498 SvPOK_only (enc.sv); 518 SvPOK_only (enc.sv);
519
520 if (cbor->flags & F_ALLOW_STRINGREF)
521 {
522 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
523 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
524 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
525 }
526
499 encode_sv (&enc, scalar); 527 encode_sv (&enc, scalar);
500 528
501 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 529 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
502 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 530 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
503 531
517 U8 *end; // end of input string 545 U8 *end; // end of input string
518 const char *err; // parse error, if != 0 546 const char *err; // parse error, if != 0
519 CBOR cbor; 547 CBOR cbor;
520 U32 depth; // recursion depth 548 U32 depth; // recursion depth
521 U32 maxdepth; // recursion depth limit 549 U32 maxdepth; // recursion depth limit
522 AV *sharable; 550 AV *shareable;
551 AV *stringref;
523} dec_t; 552} dec_t;
524 553
525#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 554#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
526 555
527#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 556#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
625 654
626static void 655static void
627decode_he (dec_t *dec, HV *hv) 656decode_he (dec_t *dec, HV *hv)
628{ 657{
629 // for speed reasons, we specialcase single-string 658 // for speed reasons, we specialcase single-string
630 // byte or utf-8 strings as keys. 659 // byte or utf-8 strings as keys, but only when !stringref
631 660
661 if (ecb_expect_true (!dec->stringref))
632 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 662 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
633 { 663 {
634 I32 len = decode_uint (dec); 664 I32 len = decode_uint (dec);
635 char *key = (char *)dec->cur; 665 char *key = (char *)dec->cur;
636 666
637 dec->cur += len; 667 dec->cur += len;
638 668
669 if (ecb_expect_false (dec->stringref))
670 av_push (dec->stringref, newSVpvn (key, len));
671
639 hv_store (hv, key, len, decode_sv (dec), 0); 672 hv_store (hv, key, len, decode_sv (dec), 0);
673
674 return;
640 } 675 }
641 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 676 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
642 { 677 {
643 I32 len = decode_uint (dec); 678 I32 len = decode_uint (dec);
644 char *key = (char *)dec->cur; 679 char *key = (char *)dec->cur;
645 680
646 dec->cur += len; 681 dec->cur += len;
647 682
683 if (ecb_expect_false (dec->stringref))
684 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
685
648 hv_store (hv, key, -len, decode_sv (dec), 0); 686 hv_store (hv, key, -len, decode_sv (dec), 0);
687
688 return;
649 } 689 }
650 else 690
651 {
652 SV *k = decode_sv (dec); 691 SV *k = decode_sv (dec);
653 SV *v = decode_sv (dec); 692 SV *v = decode_sv (dec);
654 693
655 hv_store_ent (hv, k, v, 0); 694 hv_store_ent (hv, k, v, 0);
656 SvREFCNT_dec (k); 695 SvREFCNT_dec (k);
657 }
658} 696}
659 697
660static SV * 698static SV *
661decode_hv (dec_t *dec) 699decode_hv (dec_t *dec)
662{ 700{
728 STRLEN len = decode_uint (dec); 766 STRLEN len = decode_uint (dec);
729 767
730 WANT (len); 768 WANT (len);
731 sv = newSVpvn (dec->cur, len); 769 sv = newSVpvn (dec->cur, len);
732 dec->cur += len; 770 dec->cur += len;
771
772 if (ecb_expect_false (dec->stringref)
773 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
774 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
733 } 775 }
734 776
735 if (utf8) 777 if (utf8)
736 SvUTF8_on (sv); 778 SvUTF8_on (sv);
737 779
743} 785}
744 786
745static SV * 787static SV *
746decode_tagged (dec_t *dec) 788decode_tagged (dec_t *dec)
747{ 789{
790 SV *sv = 0;
748 UV tag = decode_uint (dec); 791 UV tag = decode_uint (dec);
749 SV *sv = decode_sv (dec); 792
793 WANT (1);
750 794
751 switch (tag) 795 switch (tag)
752 { 796 {
753 case CBOR_TAG_MAGIC: 797 case CBOR_TAG_MAGIC:
754 return sv; 798 sv = decode_sv (dec);
799 break;
755 800
756 case CBOR_TAG_INDIRECTION: 801 case CBOR_TAG_INDIRECTION:
757 return newRV_noinc (sv); 802 sv = newRV_noinc (decode_sv (dec));
803 break;
758 804
805 case CBOR_TAG_STRINGREF_NAMESPACE:
806 {
807 ENTER; SAVETMPS;
808
809 SAVESPTR (dec->stringref);
810 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
811
812 sv = decode_sv (dec);
813
814 FREETMPS; LEAVE;
815 }
816 break;
817
818 case CBOR_TAG_STRINGREF:
819 {
820 if ((*dec->cur >> 5) != 0)
821 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
822
823 UV idx = decode_uint (dec);
824
825 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
826 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
827
828 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
829 }
830 break;
831
759 case CBOR_TAG_VALUE_SHARABLE: 832 case CBOR_TAG_VALUE_SHAREABLE:
833 {
760 if (ecb_expect_false (!dec->sharable)) 834 if (ecb_expect_false (!dec->shareable))
761 dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); 835 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
762 836
837 sv = newSV (0);
763 av_push (dec->sharable, SvREFCNT_inc_NN (sv)); 838 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
764 839
765 return sv; 840 SV *osv = decode_sv (dec);
841 sv_setsv (sv, osv);
842 SvREFCNT_dec_NN (osv);
843 }
844 break;
766 845
767 case CBOR_TAG_VALUE_SHAREDREF: 846 case CBOR_TAG_VALUE_SHAREDREF:
768 { 847 {
769 // TODO: should verify that the sv atcually was a CBOR unsigned integer 848 if ((*dec->cur >> 5) != 0)
770 UV idx = SvUV (sv); 849 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
771 850
851 UV idx = decode_uint (dec);
852
772 if (!dec->sharable || idx > AvFILLp (dec->sharable)) 853 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
773 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 854 ERR ("corrupted CBOR data (sharedref index out of bounds)");
774 855
775 SvREFCNT_dec (sv);
776
777 return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); 856 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
778 } 857 }
858 break;
779 859
780 case CBOR_TAG_PERL_OBJECT: 860 case CBOR_TAG_PERL_OBJECT:
781 { 861 {
862 sv = decode_sv (dec);
863
782 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 864 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
783 ERR ("corrupted CBOR data (non-array perl object)"); 865 ERR ("corrupted CBOR data (non-array perl object)");
784 866
785 AV *av = (AV *)SvRV (sv); 867 AV *av = (AV *)SvRV (sv);
786 int len = av_len (av) + 1; 868 int len = av_len (av) + 1;
821 sv = SvREFCNT_inc (POPs); 903 sv = SvREFCNT_inc (POPs);
822 904
823 PUTBACK; 905 PUTBACK;
824 906
825 FREETMPS; LEAVE; 907 FREETMPS; LEAVE;
826
827 return sv;
828 } 908 }
909 break;
829 910
830 default: 911 default:
831 { 912 {
913 sv = decode_sv (dec);
914
832 AV *av = newAV (); 915 AV *av = newAV ();
833 av_push (av, newSVuv (tag)); 916 av_push (av, newSVuv (tag));
834 av_push (av, sv); 917 av_push (av, sv);
835 918
836 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 919 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
837 ? cbor_tagged_stash 920 ? cbor_tagged_stash
838 : gv_stashpv ("CBOR::XS::Tagged" , 1); 921 : gv_stashpv ("CBOR::XS::Tagged" , 1);
839 922
840 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 923 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
841 } 924 }
925 break;
842 } 926 }
927
928 return sv;
843 929
844fail: 930fail:
845 SvREFCNT_dec (sv); 931 SvREFCNT_dec (sv);
846 return &PL_sv_undef; 932 return &PL_sv_undef;
847} 933}
1018void shrink (CBOR *self, int enable = 1) 1104void shrink (CBOR *self, int enable = 1)
1019 ALIAS: 1105 ALIAS:
1020 shrink = F_SHRINK 1106 shrink = F_SHRINK
1021 allow_unknown = F_ALLOW_UNKNOWN 1107 allow_unknown = F_ALLOW_UNKNOWN
1022 allow_sharing = F_ALLOW_SHARING 1108 allow_sharing = F_ALLOW_SHARING
1023 dedup_keys = F_DEDUP_KEYS 1109 allow_stringref = F_ALLOW_STRINGREF
1024 dedup_strings = F_DEDUP_STRINGS
1025 PPCODE: 1110 PPCODE:
1026{ 1111{
1027 if (enable) 1112 if (enable)
1028 self->flags |= ix; 1113 self->flags |= ix;
1029 else 1114 else
1035void get_shrink (CBOR *self) 1120void get_shrink (CBOR *self)
1036 ALIAS: 1121 ALIAS:
1037 get_shrink = F_SHRINK 1122 get_shrink = F_SHRINK
1038 get_allow_unknown = F_ALLOW_UNKNOWN 1123 get_allow_unknown = F_ALLOW_UNKNOWN
1039 get_allow_sharing = F_ALLOW_SHARING 1124 get_allow_sharing = F_ALLOW_SHARING
1040 get_dedup_keys = F_DEDUP_KEYS 1125 get_allow_stringref = F_ALLOW_STRINGREF
1041 get_dedup_strings = F_DEDUP_STRINGS
1042 PPCODE: 1126 PPCODE:
1043 XPUSHs (boolSV (self->flags & ix)); 1127 XPUSHs (boolSV (self->flags & ix));
1044 1128
1045void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1129void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1046 PPCODE: 1130 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines