… | |
… | |
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 |
|
|
129 | ecb_inline int |
|
|
130 | minimum_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 |
133 | typedef struct |
147 | typedef 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 | |
145 | ecb_inline void |
160 | ecb_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 | |
|
|
219 | ecb_inline void |
|
|
220 | encode_tag (enc_t *enc, UV tag) |
|
|
221 | { |
|
|
222 | encode_uint (enc, 0xc0, tag); |
|
|
223 | } |
|
|
224 | |
204 | static void |
225 | static void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
226 | encode_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 | |
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static void encode_sv (enc_t *enc, SV *sv); |
253 | static void encode_sv (enc_t *enc, SV *sv); |
220 | |
254 | |
221 | static void |
255 | static void |
222 | encode_av (enc_t *enc, AV *av) |
256 | encode_av (enc_t *enc, AV *av) |
223 | { |
257 | { |
… | |
… | |
235 | SV **svp = av_fetch (av, i, 0); |
269 | SV **svp = av_fetch (av, i, 0); |
236 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
270 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
237 | } |
271 | } |
238 | |
272 | |
239 | --enc->depth; |
273 | --enc->depth; |
|
|
274 | } |
|
|
275 | |
|
|
276 | ecb_inline void |
|
|
277 | encode_he (enc_t *enc, HE *he) |
|
|
278 | { |
240 | } |
279 | } |
241 | |
280 | |
242 | static void |
281 | static void |
243 | encode_hv (enc_t *enc, HV *hv) |
282 | encode_hv (enc_t *enc, HV *hv) |
244 | { |
283 | { |
… | |
… | |
475 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
514 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
476 | enc.cur = SvPVX (enc.sv); |
515 | enc.cur = SvPVX (enc.sv); |
477 | enc.end = SvEND (enc.sv); |
516 | enc.end = SvEND (enc.sv); |
478 | |
517 | |
479 | 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 | |
480 | encode_sv (&enc, scalar); |
527 | encode_sv (&enc, scalar); |
481 | |
528 | |
482 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
529 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
483 | *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 |
484 | |
531 | |
… | |
… | |
499 | const char *err; // parse error, if != 0 |
546 | const char *err; // parse error, if != 0 |
500 | CBOR cbor; |
547 | CBOR cbor; |
501 | U32 depth; // recursion depth |
548 | U32 depth; // recursion depth |
502 | U32 maxdepth; // recursion depth limit |
549 | U32 maxdepth; // recursion depth limit |
503 | AV *shareable; |
550 | AV *shareable; |
|
|
551 | AV *stringref; |
504 | } dec_t; |
552 | } dec_t; |
505 | |
553 | |
506 | #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 |
507 | |
555 | |
508 | #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") |
… | |
… | |
606 | |
654 | |
607 | static void |
655 | static void |
608 | decode_he (dec_t *dec, HV *hv) |
656 | decode_he (dec_t *dec, HV *hv) |
609 | { |
657 | { |
610 | // for speed reasons, we specialcase single-string |
658 | // for speed reasons, we specialcase single-string |
611 | // byte or utf-8 strings as keys. |
659 | // byte or utf-8 strings as keys, but only when !stringref |
612 | |
660 | |
|
|
661 | if (ecb_expect_true (!dec->stringref)) |
613 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
662 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
614 | { |
663 | { |
615 | I32 len = decode_uint (dec); |
664 | I32 len = decode_uint (dec); |
616 | char *key = (char *)dec->cur; |
665 | char *key = (char *)dec->cur; |
617 | |
666 | |
618 | dec->cur += len; |
667 | dec->cur += len; |
619 | |
668 | |
|
|
669 | if (ecb_expect_false (dec->stringref)) |
|
|
670 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
671 | |
620 | hv_store (hv, key, len, decode_sv (dec), 0); |
672 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
673 | |
|
|
674 | return; |
621 | } |
675 | } |
622 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
676 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
623 | { |
677 | { |
624 | I32 len = decode_uint (dec); |
678 | I32 len = decode_uint (dec); |
625 | char *key = (char *)dec->cur; |
679 | char *key = (char *)dec->cur; |
626 | |
680 | |
627 | dec->cur += len; |
681 | dec->cur += len; |
628 | |
682 | |
|
|
683 | if (ecb_expect_false (dec->stringref)) |
|
|
684 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
685 | |
629 | hv_store (hv, key, -len, decode_sv (dec), 0); |
686 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
687 | |
|
|
688 | return; |
630 | } |
689 | } |
631 | else |
690 | |
632 | { |
|
|
633 | SV *k = decode_sv (dec); |
691 | SV *k = decode_sv (dec); |
634 | SV *v = decode_sv (dec); |
692 | SV *v = decode_sv (dec); |
635 | |
693 | |
636 | hv_store_ent (hv, k, v, 0); |
694 | hv_store_ent (hv, k, v, 0); |
637 | SvREFCNT_dec (k); |
695 | SvREFCNT_dec (k); |
638 | } |
|
|
639 | } |
696 | } |
640 | |
697 | |
641 | static SV * |
698 | static SV * |
642 | decode_hv (dec_t *dec) |
699 | decode_hv (dec_t *dec) |
643 | { |
700 | { |
… | |
… | |
709 | STRLEN len = decode_uint (dec); |
766 | STRLEN len = decode_uint (dec); |
710 | |
767 | |
711 | WANT (len); |
768 | WANT (len); |
712 | sv = newSVpvn (dec->cur, len); |
769 | sv = newSVpvn (dec->cur, len); |
713 | 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)); |
714 | } |
775 | } |
715 | |
776 | |
716 | if (utf8) |
777 | if (utf8) |
717 | SvUTF8_on (sv); |
778 | SvUTF8_on (sv); |
718 | |
779 | |
… | |
… | |
732 | WANT (1); |
793 | WANT (1); |
733 | |
794 | |
734 | switch (tag) |
795 | switch (tag) |
735 | { |
796 | { |
736 | case CBOR_TAG_MAGIC: |
797 | case CBOR_TAG_MAGIC: |
737 | return decode_sv (dec); |
798 | sv = decode_sv (dec); |
|
|
799 | break; |
738 | |
800 | |
739 | case CBOR_TAG_INDIRECTION: |
801 | case CBOR_TAG_INDIRECTION: |
740 | return newRV_noinc (decode_sv (dec)); |
802 | sv = newRV_noinc (decode_sv (dec)); |
|
|
803 | break; |
|
|
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; |
741 | |
831 | |
742 | case CBOR_TAG_VALUE_SHAREABLE: |
832 | case CBOR_TAG_VALUE_SHAREABLE: |
743 | { |
833 | { |
744 | if (ecb_expect_false (!dec->shareable)) |
834 | if (ecb_expect_false (!dec->shareable)) |
745 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
835 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
… | |
… | |
749 | |
839 | |
750 | SV *osv = decode_sv (dec); |
840 | SV *osv = decode_sv (dec); |
751 | sv_setsv (sv, osv); |
841 | sv_setsv (sv, osv); |
752 | SvREFCNT_dec_NN (osv); |
842 | SvREFCNT_dec_NN (osv); |
753 | } |
843 | } |
754 | |
844 | break; |
755 | return sv; |
|
|
756 | |
845 | |
757 | case CBOR_TAG_VALUE_SHAREDREF: |
846 | case CBOR_TAG_VALUE_SHAREDREF: |
758 | { |
847 | { |
759 | if ((*dec->cur >> 5) != 0) |
848 | if ((*dec->cur >> 5) != 0) |
760 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
849 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
761 | |
850 | |
762 | UV idx = decode_uint (dec); |
851 | UV idx = decode_uint (dec); |
763 | |
852 | |
764 | if (!dec->shareable || idx > AvFILLp (dec->shareable)) |
853 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
765 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
854 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
766 | |
855 | |
767 | return SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
856 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
768 | } |
857 | } |
|
|
858 | break; |
769 | |
859 | |
770 | case CBOR_TAG_PERL_OBJECT: |
860 | case CBOR_TAG_PERL_OBJECT: |
771 | { |
861 | { |
772 | sv = decode_sv (dec); |
862 | sv = decode_sv (dec); |
773 | |
863 | |
… | |
… | |
813 | sv = SvREFCNT_inc (POPs); |
903 | sv = SvREFCNT_inc (POPs); |
814 | |
904 | |
815 | PUTBACK; |
905 | PUTBACK; |
816 | |
906 | |
817 | FREETMPS; LEAVE; |
907 | FREETMPS; LEAVE; |
818 | |
|
|
819 | return sv; |
|
|
820 | } |
908 | } |
|
|
909 | break; |
821 | |
910 | |
822 | default: |
911 | default: |
823 | { |
912 | { |
824 | sv = decode_sv (dec); |
913 | sv = decode_sv (dec); |
825 | |
914 | |
… | |
… | |
829 | |
918 | |
830 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
919 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
831 | ? cbor_tagged_stash |
920 | ? cbor_tagged_stash |
832 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
921 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
833 | |
922 | |
834 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
923 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
835 | } |
924 | } |
|
|
925 | break; |
836 | } |
926 | } |
|
|
927 | |
|
|
928 | return sv; |
837 | |
929 | |
838 | fail: |
930 | fail: |
839 | SvREFCNT_dec (sv); |
931 | SvREFCNT_dec (sv); |
840 | return &PL_sv_undef; |
932 | return &PL_sv_undef; |
841 | } |
933 | } |
… | |
… | |
1012 | void shrink (CBOR *self, int enable = 1) |
1104 | void shrink (CBOR *self, int enable = 1) |
1013 | ALIAS: |
1105 | ALIAS: |
1014 | shrink = F_SHRINK |
1106 | shrink = F_SHRINK |
1015 | allow_unknown = F_ALLOW_UNKNOWN |
1107 | allow_unknown = F_ALLOW_UNKNOWN |
1016 | allow_sharing = F_ALLOW_SHARING |
1108 | allow_sharing = F_ALLOW_SHARING |
1017 | dedup_keys = F_DEDUP_KEYS |
1109 | allow_stringref = F_ALLOW_STRINGREF |
1018 | dedup_strings = F_DEDUP_STRINGS |
|
|
1019 | PPCODE: |
1110 | PPCODE: |
1020 | { |
1111 | { |
1021 | if (enable) |
1112 | if (enable) |
1022 | self->flags |= ix; |
1113 | self->flags |= ix; |
1023 | else |
1114 | else |
… | |
… | |
1029 | void get_shrink (CBOR *self) |
1120 | void get_shrink (CBOR *self) |
1030 | ALIAS: |
1121 | ALIAS: |
1031 | get_shrink = F_SHRINK |
1122 | get_shrink = F_SHRINK |
1032 | get_allow_unknown = F_ALLOW_UNKNOWN |
1123 | get_allow_unknown = F_ALLOW_UNKNOWN |
1033 | get_allow_sharing = F_ALLOW_SHARING |
1124 | get_allow_sharing = F_ALLOW_SHARING |
1034 | get_dedup_keys = F_DEDUP_KEYS |
1125 | get_allow_stringref = F_ALLOW_STRINGREF |
1035 | get_dedup_strings = F_DEDUP_STRINGS |
|
|
1036 | PPCODE: |
1126 | PPCODE: |
1037 | XPUSHs (boolSV (self->flags & ix)); |
1127 | XPUSHs (boolSV (self->flags & ix)); |
1038 | |
1128 | |
1039 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1129 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1040 | PPCODE: |
1130 | PPCODE: |