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.19 by root, Wed Nov 20 02:03:09 2013 UTC vs.
Revision 1.26 by root, Fri Nov 22 09:40:13 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)
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 *shareable; // ptr => index, or 0 156 HV *shareable; // ptr => index, or 0
142 UV shareable_idx; 157 UV shareable_idx;
143} enc_t; 158} enc_t;
144 159
145ecb_inline void 160ecb_inline void
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}
212
213ecb_inline void
214encode_tag (enc_t *enc, UV tag)
215{
216 encode_uint (enc, 0xc0, tag);
217} 251}
218 252
219static void encode_sv (enc_t *enc, SV *sv); 253static void encode_sv (enc_t *enc, SV *sv);
220 254
221static void 255static void
475 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 509 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
476 enc.cur = SvPVX (enc.sv); 510 enc.cur = SvPVX (enc.sv);
477 enc.end = SvEND (enc.sv); 511 enc.end = SvEND (enc.sv);
478 512
479 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
480 encode_sv (&enc, scalar); 522 encode_sv (&enc, scalar);
481 523
482 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 524 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
483 *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
484 526
499 const char *err; // parse error, if != 0 541 const char *err; // parse error, if != 0
500 CBOR cbor; 542 CBOR cbor;
501 U32 depth; // recursion depth 543 U32 depth; // recursion depth
502 U32 maxdepth; // recursion depth limit 544 U32 maxdepth; // recursion depth limit
503 AV *shareable; 545 AV *shareable;
546 AV *stringref;
504} dec_t; 547} dec_t;
505 548
506#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
507 550
508#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")
606 649
607static void 650static void
608decode_he (dec_t *dec, HV *hv) 651decode_he (dec_t *dec, HV *hv)
609{ 652{
610 // for speed reasons, we specialcase single-string 653 // for speed reasons, we specialcase single-string
611 // byte or utf-8 strings as keys. 654 // byte or utf-8 strings as keys, but only when !stringref
612 655
656 if (ecb_expect_true (!dec->stringref))
613 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 657 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
614 { 658 {
615 I32 len = decode_uint (dec); 659 I32 len = decode_uint (dec);
616 char *key = (char *)dec->cur; 660 char *key = (char *)dec->cur;
617 661
618 dec->cur += len; 662 dec->cur += len;
619 663
664 if (ecb_expect_false (dec->stringref))
665 av_push (dec->stringref, newSVpvn (key, len));
666
620 hv_store (hv, key, len, decode_sv (dec), 0); 667 hv_store (hv, key, len, decode_sv (dec), 0);
668
669 return;
621 } 670 }
622 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 671 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
623 { 672 {
624 I32 len = decode_uint (dec); 673 I32 len = decode_uint (dec);
625 char *key = (char *)dec->cur; 674 char *key = (char *)dec->cur;
626 675
627 dec->cur += len; 676 dec->cur += len;
628 677
678 if (ecb_expect_false (dec->stringref))
679 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1));
680
629 hv_store (hv, key, -len, decode_sv (dec), 0); 681 hv_store (hv, key, -len, decode_sv (dec), 0);
682
683 return;
630 } 684 }
631 else 685
632 {
633 SV *k = decode_sv (dec); 686 SV *k = decode_sv (dec);
634 SV *v = decode_sv (dec); 687 SV *v = decode_sv (dec);
635 688
636 hv_store_ent (hv, k, v, 0); 689 hv_store_ent (hv, k, v, 0);
637 SvREFCNT_dec (k); 690 SvREFCNT_dec (k);
638 }
639} 691}
640 692
641static SV * 693static SV *
642decode_hv (dec_t *dec) 694decode_hv (dec_t *dec)
643{ 695{
709 STRLEN len = decode_uint (dec); 761 STRLEN len = decode_uint (dec);
710 762
711 WANT (len); 763 WANT (len);
712 sv = newSVpvn (dec->cur, len); 764 sv = newSVpvn (dec->cur, len);
713 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));
714 } 770 }
715 771
716 if (utf8) 772 if (utf8)
717 SvUTF8_on (sv); 773 SvUTF8_on (sv);
718 774
732 WANT (1); 788 WANT (1);
733 789
734 switch (tag) 790 switch (tag)
735 { 791 {
736 case CBOR_TAG_MAGIC: 792 case CBOR_TAG_MAGIC:
737 return decode_sv (dec); 793 sv = decode_sv (dec);
794 break;
738 795
739 case CBOR_TAG_INDIRECTION: 796 case CBOR_TAG_INDIRECTION:
740 return newRV_noinc (decode_sv (dec)); 797 sv = newRV_noinc (decode_sv (dec));
798 break;
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;
741 826
742 case CBOR_TAG_VALUE_SHAREABLE: 827 case CBOR_TAG_VALUE_SHAREABLE:
743 { 828 {
744 if (ecb_expect_false (!dec->shareable)) 829 if (ecb_expect_false (!dec->shareable))
745 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 830 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
749 834
750 SV *osv = decode_sv (dec); 835 SV *osv = decode_sv (dec);
751 sv_setsv (sv, osv); 836 sv_setsv (sv, osv);
752 SvREFCNT_dec_NN (osv); 837 SvREFCNT_dec_NN (osv);
753 } 838 }
754 839 break;
755 return sv;
756 840
757 case CBOR_TAG_VALUE_SHAREDREF: 841 case CBOR_TAG_VALUE_SHAREDREF:
758 { 842 {
759 if ((*dec->cur >> 5) != 0) 843 if ((*dec->cur >> 5) != 0)
760 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 844 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
761 845
762 UV idx = decode_uint (dec); 846 UV idx = decode_uint (dec);
763 847
764 if (!dec->shareable || idx > AvFILLp (dec->shareable)) 848 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
765 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 849 ERR ("corrupted CBOR data (sharedref index out of bounds)");
766 850
767 return SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 851 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
768 } 852 }
853 break;
769 854
770 case CBOR_TAG_PERL_OBJECT: 855 case CBOR_TAG_PERL_OBJECT:
771 { 856 {
772 sv = decode_sv (dec); 857 sv = decode_sv (dec);
773 858
813 sv = SvREFCNT_inc (POPs); 898 sv = SvREFCNT_inc (POPs);
814 899
815 PUTBACK; 900 PUTBACK;
816 901
817 FREETMPS; LEAVE; 902 FREETMPS; LEAVE;
818
819 return sv;
820 } 903 }
904 break;
821 905
822 default: 906 default:
823 { 907 {
824 sv = decode_sv (dec); 908 sv = decode_sv (dec);
825 909
829 913
830 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 914 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
831 ? cbor_tagged_stash 915 ? cbor_tagged_stash
832 : gv_stashpv ("CBOR::XS::Tagged" , 1); 916 : gv_stashpv ("CBOR::XS::Tagged" , 1);
833 917
834 return sv_bless (newRV_noinc ((SV *)av), tagged_stash); 918 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
835 } 919 }
920 break;
836 } 921 }
922
923 return sv;
837 924
838fail: 925fail:
839 SvREFCNT_dec (sv); 926 SvREFCNT_dec (sv);
840 return &PL_sv_undef; 927 return &PL_sv_undef;
841} 928}
1012void shrink (CBOR *self, int enable = 1) 1099void shrink (CBOR *self, int enable = 1)
1013 ALIAS: 1100 ALIAS:
1014 shrink = F_SHRINK 1101 shrink = F_SHRINK
1015 allow_unknown = F_ALLOW_UNKNOWN 1102 allow_unknown = F_ALLOW_UNKNOWN
1016 allow_sharing = F_ALLOW_SHARING 1103 allow_sharing = F_ALLOW_SHARING
1017 dedup_keys = F_DEDUP_KEYS 1104 allow_stringref = F_ALLOW_STRINGREF
1018 dedup_strings = F_DEDUP_STRINGS
1019 PPCODE: 1105 PPCODE:
1020{ 1106{
1021 if (enable) 1107 if (enable)
1022 self->flags |= ix; 1108 self->flags |= ix;
1023 else 1109 else
1029void get_shrink (CBOR *self) 1115void get_shrink (CBOR *self)
1030 ALIAS: 1116 ALIAS:
1031 get_shrink = F_SHRINK 1117 get_shrink = F_SHRINK
1032 get_allow_unknown = F_ALLOW_UNKNOWN 1118 get_allow_unknown = F_ALLOW_UNKNOWN
1033 get_allow_sharing = F_ALLOW_SHARING 1119 get_allow_sharing = F_ALLOW_SHARING
1034 get_dedup_keys = F_DEDUP_KEYS 1120 get_allow_stringref = F_ALLOW_STRINGREF
1035 get_dedup_strings = F_DEDUP_STRINGS
1036 PPCODE: 1121 PPCODE:
1037 XPUSHs (boolSV (self->flags & ix)); 1122 XPUSHs (boolSV (self->flags & ix));
1038 1123
1039void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1124void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1040 PPCODE: 1125 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines