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.26 by root, Fri Nov 22 09:40:13 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{
309 309
310// encode objects, arrays and special \0=false and \1=true values. 310// encode objects, arrays and special \0=false and \1=true values.
311static void 311static void
312encode_rv (enc_t *enc, SV *sv) 312encode_rv (enc_t *enc, SV *sv)
313{ 313{
314 svtype svt;
315
316 SvGETMAGIC (sv); 314 SvGETMAGIC (sv);
315
316 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
317 && ecb_expect_false (SvREFCNT (sv) > 1))
318 {
319 if (!enc->shareable)
320 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
321
322 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
323
324 if (SvOK (*svp))
325 {
326 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
327 encode_uint (enc, 0x00, SvUV (*svp));
328 return;
329 }
330 else
331 {
332 sv_setuv (*svp, enc->shareable_idx);
333 ++enc->shareable_idx;
334 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
335 }
336 }
337
317 svt = SvTYPE (sv); 338 svtype svt = SvTYPE (sv);
318
319 if (encode_sharable (enc, sv))
320 return;
321 339
322 if (ecb_expect_false (SvOBJECT (sv))) 340 if (ecb_expect_false (SvOBJECT (sv)))
323 { 341 {
324 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash 342 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
325 ? types_boolean_stash 343 ? types_boolean_stash
452static void 470static void
453encode_sv (enc_t *enc, SV *sv) 471encode_sv (enc_t *enc, SV *sv)
454{ 472{
455 SvGETMAGIC (sv); 473 SvGETMAGIC (sv);
456 474
457 if (encode_sharable (enc, sv))
458 return;
459
460 if (SvPOKp (sv)) 475 if (SvPOKp (sv))
461 { 476 {
462 STRLEN len; 477 STRLEN len;
463 char *str = SvPV (sv, len); 478 char *str = SvPV (sv, len);
464 encode_str (enc, SvUTF8 (sv), str, len); 479 encode_str (enc, SvUTF8 (sv), str, len);
494 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 509 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
495 enc.cur = SvPVX (enc.sv); 510 enc.cur = SvPVX (enc.sv);
496 enc.end = SvEND (enc.sv); 511 enc.end = SvEND (enc.sv);
497 512
498 SvPOK_only (enc.sv); 513 SvPOK_only (enc.sv);
514
515 if (cbor->flags & F_ALLOW_STRINGREF)
516 {
517 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
518 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
519 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
520 }
521
499 encode_sv (&enc, scalar); 522 encode_sv (&enc, scalar);
500 523
501 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 524 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
502 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 525 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
503 526
517 U8 *end; // end of input string 540 U8 *end; // end of input string
518 const char *err; // parse error, if != 0 541 const char *err; // parse error, if != 0
519 CBOR cbor; 542 CBOR cbor;
520 U32 depth; // recursion depth 543 U32 depth; // recursion depth
521 U32 maxdepth; // recursion depth limit 544 U32 maxdepth; // recursion depth limit
522 AV *sharable; 545 AV *shareable;
546 AV *stringref;
523} dec_t; 547} dec_t;
524 548
525#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 549#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
526 550
527#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 551#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
625 649
626static void 650static void
627decode_he (dec_t *dec, HV *hv) 651decode_he (dec_t *dec, HV *hv)
628{ 652{
629 // for speed reasons, we specialcase single-string 653 // for speed reasons, we specialcase single-string
630 // byte or utf-8 strings as keys. 654 // byte or utf-8 strings as keys, but only when !stringref
631 655
656 if (ecb_expect_true (!dec->stringref))
632 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 657 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
633 { 658 {
634 I32 len = decode_uint (dec); 659 I32 len = decode_uint (dec);
635 char *key = (char *)dec->cur; 660 char *key = (char *)dec->cur;
636 661
637 dec->cur += len; 662 dec->cur += len;
638 663
664 if (ecb_expect_false (dec->stringref))
665 av_push (dec->stringref, newSVpvn (key, len));
666
639 hv_store (hv, key, len, decode_sv (dec), 0); 667 hv_store (hv, key, len, decode_sv (dec), 0);
668
669 return;
640 } 670 }
641 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 671 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
642 { 672 {
643 I32 len = decode_uint (dec); 673 I32 len = decode_uint (dec);
644 char *key = (char *)dec->cur; 674 char *key = (char *)dec->cur;
645 675
646 dec->cur += len; 676 dec->cur += len;
647 677
678 if (ecb_expect_false (dec->stringref))
679 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
680
648 hv_store (hv, key, -len, decode_sv (dec), 0); 681 hv_store (hv, key, -len, decode_sv (dec), 0);
682
683 return;
649 } 684 }
650 else 685
651 {
652 SV *k = decode_sv (dec); 686 SV *k = decode_sv (dec);
653 SV *v = decode_sv (dec); 687 SV *v = decode_sv (dec);
654 688
655 hv_store_ent (hv, k, v, 0); 689 hv_store_ent (hv, k, v, 0);
656 SvREFCNT_dec (k); 690 SvREFCNT_dec (k);
657 }
658} 691}
659 692
660static SV * 693static SV *
661decode_hv (dec_t *dec) 694decode_hv (dec_t *dec)
662{ 695{
728 STRLEN len = decode_uint (dec); 761 STRLEN len = decode_uint (dec);
729 762
730 WANT (len); 763 WANT (len);
731 sv = newSVpvn (dec->cur, len); 764 sv = newSVpvn (dec->cur, len);
732 dec->cur += len; 765 dec->cur += len;
766
767 if (ecb_expect_false (dec->stringref)
768 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
769 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
733 } 770 }
734 771
735 if (utf8) 772 if (utf8)
736 SvUTF8_on (sv); 773 SvUTF8_on (sv);
737 774
743} 780}
744 781
745static SV * 782static SV *
746decode_tagged (dec_t *dec) 783decode_tagged (dec_t *dec)
747{ 784{
785 SV *sv = 0;
748 UV tag = decode_uint (dec); 786 UV tag = decode_uint (dec);
749 SV *sv = decode_sv (dec); 787
788 WANT (1);
750 789
751 switch (tag) 790 switch (tag)
752 { 791 {
753 case CBOR_TAG_MAGIC: 792 case CBOR_TAG_MAGIC:
754 return sv; 793 sv = decode_sv (dec);
794 break;
755 795
756 case CBOR_TAG_INDIRECTION: 796 case CBOR_TAG_INDIRECTION:
757 return newRV_noinc (sv); 797 sv = newRV_noinc (decode_sv (dec));
798 break;
758 799
800 case CBOR_TAG_STRINGREF_NAMESPACE:
801 {
802 ENTER; SAVETMPS;
803
804 SAVESPTR (dec->stringref);
805 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
806
807 sv = decode_sv (dec);
808
809 FREETMPS; LEAVE;
810 }
811 break;
812
813 case CBOR_TAG_STRINGREF:
814 {
815 if ((*dec->cur >> 5) != 0)
816 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
817
818 UV idx = decode_uint (dec);
819
820 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
821 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
822
823 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
824 }
825 break;
826
759 case CBOR_TAG_VALUE_SHARABLE: 827 case CBOR_TAG_VALUE_SHAREABLE:
828 {
760 if (ecb_expect_false (!dec->sharable)) 829 if (ecb_expect_false (!dec->shareable))
761 dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); 830 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
762 831
832 sv = newSV (0);
763 av_push (dec->sharable, SvREFCNT_inc_NN (sv)); 833 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
764 834
765 return sv; 835 SV *osv = decode_sv (dec);
836 sv_setsv (sv, osv);
837 SvREFCNT_dec_NN (osv);
838 }
839 break;
766 840
767 case CBOR_TAG_VALUE_SHAREDREF: 841 case CBOR_TAG_VALUE_SHAREDREF:
768 { 842 {
769 // TODO: should verify that the sv atcually was a CBOR unsigned integer 843 if ((*dec->cur >> 5) != 0)
770 UV idx = SvUV (sv); 844 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
771 845
846 UV idx = decode_uint (dec);
847
772 if (!dec->sharable || idx > AvFILLp (dec->sharable)) 848 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
773 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 849 ERR ("corrupted CBOR data (sharedref index out of bounds)");
774 850
775 SvREFCNT_dec (sv);
776
777 return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); 851 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
778 } 852 }
853 break;
779 854
780 case CBOR_TAG_PERL_OBJECT: 855 case CBOR_TAG_PERL_OBJECT:
781 { 856 {
857 sv = decode_sv (dec);
858
782 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 859 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
783 ERR ("corrupted CBOR data (non-array perl object)"); 860 ERR ("corrupted CBOR data (non-array perl object)");
784 861
785 AV *av = (AV *)SvRV (sv); 862 AV *av = (AV *)SvRV (sv);
786 int len = av_len (av) + 1; 863 int len = av_len (av) + 1;
821 sv = SvREFCNT_inc (POPs); 898 sv = SvREFCNT_inc (POPs);
822 899
823 PUTBACK; 900 PUTBACK;
824 901
825 FREETMPS; LEAVE; 902 FREETMPS; LEAVE;
826
827 return sv;
828 } 903 }
904 break;
829 905
830 default: 906 default:
831 { 907 {
908 sv = decode_sv (dec);
909
832 AV *av = newAV (); 910 AV *av = newAV ();
833 av_push (av, newSVuv (tag)); 911 av_push (av, newSVuv (tag));
834 av_push (av, sv); 912 av_push (av, sv);
835 913
836 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 914 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
837 ? cbor_tagged_stash 915 ? cbor_tagged_stash
838 : gv_stashpv ("CBOR::XS::Tagged" , 1); 916 : gv_stashpv ("CBOR::XS::Tagged" , 1);
839 917
840 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 918 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
841 } 919 }
920 break;
842 } 921 }
922
923 return sv;
843 924
844fail: 925fail:
845 SvREFCNT_dec (sv); 926 SvREFCNT_dec (sv);
846 return &PL_sv_undef; 927 return &PL_sv_undef;
847} 928}
1018void shrink (CBOR *self, int enable = 1) 1099void shrink (CBOR *self, int enable = 1)
1019 ALIAS: 1100 ALIAS:
1020 shrink = F_SHRINK 1101 shrink = F_SHRINK
1021 allow_unknown = F_ALLOW_UNKNOWN 1102 allow_unknown = F_ALLOW_UNKNOWN
1022 allow_sharing = F_ALLOW_SHARING 1103 allow_sharing = F_ALLOW_SHARING
1023 dedup_keys = F_DEDUP_KEYS 1104 allow_stringref = F_ALLOW_STRINGREF
1024 dedup_strings = F_DEDUP_STRINGS
1025 PPCODE: 1105 PPCODE:
1026{ 1106{
1027 if (enable) 1107 if (enable)
1028 self->flags |= ix; 1108 self->flags |= ix;
1029 else 1109 else
1035void get_shrink (CBOR *self) 1115void get_shrink (CBOR *self)
1036 ALIAS: 1116 ALIAS:
1037 get_shrink = F_SHRINK 1117 get_shrink = F_SHRINK
1038 get_allow_unknown = F_ALLOW_UNKNOWN 1118 get_allow_unknown = F_ALLOW_UNKNOWN
1039 get_allow_sharing = F_ALLOW_SHARING 1119 get_allow_sharing = F_ALLOW_SHARING
1040 get_dedup_keys = F_DEDUP_KEYS 1120 get_allow_stringref = F_ALLOW_STRINGREF
1041 get_dedup_strings = F_DEDUP_STRINGS
1042 PPCODE: 1121 PPCODE:
1043 XPUSHs (boolSV (self->flags & ix)); 1122 XPUSHs (boolSV (self->flags & ix));
1044 1123
1045void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1124void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1046 PPCODE: 1125 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines