… | |
… | |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
|
|
12 | #define ECB_NO_THREADS 1 |
12 | #include "ecb.h" |
13 | #include "ecb.h" |
13 | |
14 | |
14 | // compatibility with perl <5.18 |
15 | // compatibility with perl <5.18 |
15 | #ifndef HvNAMELEN_get |
16 | #ifndef HvNAMELEN_get |
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
17 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
… | |
… | |
97 | }; |
98 | }; |
98 | |
99 | |
99 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
103 | #define F_ALLOW_CYCLES 0x00000008UL |
102 | #define F_PACK_STRINGS 0x00000008UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_VALIDATE_UTF8 0x00000020UL |
103 | |
106 | |
104 | #define INIT_SIZE 32 // initial scalar size to be allocated |
107 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | |
108 | |
106 | #define SB do { |
109 | #define SB do { |
107 | #define SE } while (0) |
110 | #define SE } while (0) |
… | |
… | |
126 | typedef struct { |
129 | typedef struct { |
127 | U32 flags; |
130 | U32 flags; |
128 | U32 max_depth; |
131 | U32 max_depth; |
129 | STRLEN max_size; |
132 | STRLEN max_size; |
130 | SV *filter; |
133 | SV *filter; |
|
|
134 | |
|
|
135 | // for the incremental parser |
|
|
136 | STRLEN incr_pos; // the current offset into the text |
|
|
137 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
138 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
131 | } CBOR; |
139 | } CBOR; |
132 | |
140 | |
133 | ecb_inline void |
141 | ecb_inline void |
134 | cbor_init (CBOR *cbor) |
142 | cbor_init (CBOR *cbor) |
135 | { |
143 | { |
… | |
… | |
139 | |
147 | |
140 | ecb_inline void |
148 | ecb_inline void |
141 | cbor_free (CBOR *cbor) |
149 | cbor_free (CBOR *cbor) |
142 | { |
150 | { |
143 | SvREFCNT_dec (cbor->filter); |
151 | SvREFCNT_dec (cbor->filter); |
|
|
152 | SvREFCNT_dec (cbor->incr_count); |
144 | } |
153 | } |
145 | |
154 | |
146 | ///////////////////////////////////////////////////////////////////////////// |
155 | ///////////////////////////////////////////////////////////////////////////// |
147 | // utility functions |
156 | // utility functions |
148 | |
157 | |
… | |
… | |
228 | { |
237 | { |
229 | need (enc, 9); |
238 | need (enc, 9); |
230 | |
239 | |
231 | if (ecb_expect_true (len < LENGTH_EXT1)) |
240 | if (ecb_expect_true (len < LENGTH_EXT1)) |
232 | *enc->cur++ = major | len; |
241 | *enc->cur++ = major | len; |
233 | else if (ecb_expect_true (len <= 0xff)) |
242 | else if (ecb_expect_true (len <= 0xffU)) |
234 | { |
243 | { |
235 | *enc->cur++ = major | LENGTH_EXT1; |
244 | *enc->cur++ = major | LENGTH_EXT1; |
236 | *enc->cur++ = len; |
245 | *enc->cur++ = len; |
237 | } |
246 | } |
238 | else if (len <= 0xffff) |
247 | else if (len <= 0xffffU) |
239 | { |
248 | { |
240 | *enc->cur++ = major | LENGTH_EXT2; |
249 | *enc->cur++ = major | LENGTH_EXT2; |
241 | *enc->cur++ = len >> 8; |
250 | *enc->cur++ = len >> 8; |
242 | *enc->cur++ = len; |
251 | *enc->cur++ = len; |
243 | } |
252 | } |
244 | else if (len <= 0xffffffff) |
253 | else if (len <= 0xffffffffU) |
245 | { |
254 | { |
246 | *enc->cur++ = major | LENGTH_EXT4; |
255 | *enc->cur++ = major | LENGTH_EXT4; |
247 | *enc->cur++ = len >> 24; |
256 | *enc->cur++ = len >> 24; |
248 | *enc->cur++ = len >> 16; |
257 | *enc->cur++ = len >> 16; |
249 | *enc->cur++ = len >> 8; |
258 | *enc->cur++ = len >> 8; |
… | |
… | |
315 | |
324 | |
316 | ++enc->depth; |
325 | ++enc->depth; |
317 | |
326 | |
318 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
327 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
319 | |
328 | |
|
|
329 | if (SvMAGICAL (av)) |
320 | for (i = 0; i <= len; ++i) |
330 | for (i = 0; i <= len; ++i) |
321 | { |
331 | { |
322 | SV **svp = av_fetch (av, i, 0); |
332 | SV **svp = av_fetch (av, i, 0); |
323 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
333 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | } |
334 | } |
|
|
335 | else |
|
|
336 | for (i = 0; i <= len; ++i) |
|
|
337 | { |
|
|
338 | SV *sv = AvARRAY (av)[i]; |
|
|
339 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
340 | } |
325 | |
341 | |
326 | --enc->depth; |
342 | --enc->depth; |
327 | } |
343 | } |
328 | |
344 | |
329 | static void |
345 | static void |
… | |
… | |
627 | U8 m = *dec->cur & MINOR_MASK; |
643 | U8 m = *dec->cur & MINOR_MASK; |
628 | ++dec->cur; |
644 | ++dec->cur; |
629 | |
645 | |
630 | if (ecb_expect_true (m < LENGTH_EXT1)) |
646 | if (ecb_expect_true (m < LENGTH_EXT1)) |
631 | return m; |
647 | return m; |
632 | |
648 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
633 | switch (m) |
|
|
634 | { |
649 | { |
635 | case LENGTH_EXT1: |
|
|
636 | WANT (1); |
650 | WANT (1); |
637 | dec->cur += 1; |
651 | dec->cur += 1; |
638 | return dec->cur[-1]; |
652 | return dec->cur[-1]; |
639 | |
653 | } |
640 | case LENGTH_EXT2: |
654 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
655 | { |
641 | WANT (2); |
656 | WANT (2); |
642 | dec->cur += 2; |
657 | dec->cur += 2; |
643 | return (((UV)dec->cur[-2]) << 8) |
658 | return (((UV)dec->cur[-2]) << 8) |
644 | | ((UV)dec->cur[-1]); |
659 | | ((UV)dec->cur[-1]); |
645 | |
660 | } |
646 | case LENGTH_EXT4: |
661 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
662 | { |
647 | WANT (4); |
663 | WANT (4); |
648 | dec->cur += 4; |
664 | dec->cur += 4; |
649 | return (((UV)dec->cur[-4]) << 24) |
665 | return (((UV)dec->cur[-4]) << 24) |
650 | | (((UV)dec->cur[-3]) << 16) |
666 | | (((UV)dec->cur[-3]) << 16) |
651 | | (((UV)dec->cur[-2]) << 8) |
667 | | (((UV)dec->cur[-2]) << 8) |
652 | | ((UV)dec->cur[-1]); |
668 | | ((UV)dec->cur[-1]); |
653 | |
669 | } |
654 | case LENGTH_EXT8: |
670 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
671 | { |
655 | WANT (8); |
672 | WANT (8); |
656 | dec->cur += 8; |
673 | dec->cur += 8; |
657 | |
674 | |
658 | return |
675 | return |
659 | #if UVSIZE < 8 |
676 | #if UVSIZE < 8 |
660 | 0 |
677 | 0 |
661 | #else |
678 | #else |
662 | (((UV)dec->cur[-8]) << 56) |
679 | (((UV)dec->cur[-8]) << 56) |
663 | | (((UV)dec->cur[-7]) << 48) |
680 | | (((UV)dec->cur[-7]) << 48) |
664 | | (((UV)dec->cur[-6]) << 40) |
681 | | (((UV)dec->cur[-6]) << 40) |
665 | | (((UV)dec->cur[-5]) << 32) |
682 | | (((UV)dec->cur[-5]) << 32) |
666 | #endif |
683 | #endif |
667 | | (((UV)dec->cur[-4]) << 24) |
684 | | (((UV)dec->cur[-4]) << 24) |
668 | | (((UV)dec->cur[-3]) << 16) |
685 | | (((UV)dec->cur[-3]) << 16) |
669 | | (((UV)dec->cur[-2]) << 8) |
686 | | (((UV)dec->cur[-2]) << 8) |
670 | | ((UV)dec->cur[-1]); |
687 | | ((UV)dec->cur[-1]); |
671 | |
688 | } |
672 | default: |
689 | else |
673 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
690 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
674 | } |
|
|
675 | |
691 | |
676 | fail: |
692 | fail: |
677 | return 0; |
693 | return 0; |
678 | } |
694 | } |
679 | |
695 | |
… | |
… | |
705 | } |
721 | } |
706 | else |
722 | else |
707 | { |
723 | { |
708 | int i, len = decode_uint (dec); |
724 | int i, len = decode_uint (dec); |
709 | |
725 | |
|
|
726 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
710 | av_fill (av, len - 1); |
727 | av_fill (av, len - 1); |
711 | |
728 | |
712 | for (i = 0; i < len; ++i) |
729 | for (i = 0; i < len; ++i) |
713 | AvARRAY (av)[i] = decode_sv (dec); |
730 | AvARRAY (av)[i] = decode_sv (dec); |
714 | } |
731 | } |
… | |
… | |
727 | { |
744 | { |
728 | // for speed reasons, we specialcase single-string |
745 | // for speed reasons, we specialcase single-string |
729 | // byte or utf-8 strings as keys, but only when !stringref |
746 | // byte or utf-8 strings as keys, but only when !stringref |
730 | |
747 | |
731 | if (ecb_expect_true (!dec->stringref)) |
748 | if (ecb_expect_true (!dec->stringref)) |
732 | if ((*dec->cur - MAJOR_BYTES) <= 27) |
749 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
733 | { |
750 | { |
734 | I32 len = decode_uint (dec); |
751 | I32 len = decode_uint (dec); |
735 | char *key = (char *)dec->cur; |
752 | char *key = (char *)dec->cur; |
736 | |
753 | |
737 | dec->cur += len; |
754 | dec->cur += len; |
738 | |
755 | |
739 | if (ecb_expect_false (dec->stringref)) |
|
|
740 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
741 | |
|
|
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
756 | hv_store (hv, key, len, decode_sv (dec), 0); |
743 | |
757 | |
744 | return; |
758 | return; |
745 | } |
759 | } |
746 | else if ((*dec->cur - MAJOR_TEXT) <= 27) |
760 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
747 | { |
761 | { |
748 | I32 len = decode_uint (dec); |
762 | I32 len = decode_uint (dec); |
749 | char *key = (char *)dec->cur; |
763 | char *key = (char *)dec->cur; |
750 | |
764 | |
751 | dec->cur += len; |
765 | dec->cur += len; |
752 | |
766 | |
753 | if (ecb_expect_false (dec->stringref)) |
767 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
754 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
768 | if (!is_utf8_string (key, len)) |
|
|
769 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
755 | |
770 | |
756 | hv_store (hv, key, -len, decode_sv (dec), 0); |
771 | hv_store (hv, key, -len, decode_sv (dec), 0); |
757 | |
772 | |
758 | return; |
773 | return; |
759 | } |
774 | } |
… | |
… | |
761 | SV *k = decode_sv (dec); |
776 | SV *k = decode_sv (dec); |
762 | SV *v = decode_sv (dec); |
777 | SV *v = decode_sv (dec); |
763 | |
778 | |
764 | hv_store_ent (hv, k, v, 0); |
779 | hv_store_ent (hv, k, v, 0); |
765 | SvREFCNT_dec (k); |
780 | SvREFCNT_dec (k); |
|
|
781 | |
|
|
782 | fail: |
|
|
783 | ; |
766 | } |
784 | } |
767 | |
785 | |
768 | static SV * |
786 | static SV * |
769 | decode_hv (dec_t *dec) |
787 | decode_hv (dec_t *dec) |
770 | { |
788 | { |
… | |
… | |
852 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
870 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
853 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
871 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
854 | } |
872 | } |
855 | |
873 | |
856 | if (utf8) |
874 | if (utf8) |
|
|
875 | { |
|
|
876 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
877 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
878 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
879 | |
857 | SvUTF8_on (sv); |
880 | SvUTF8_on (sv); |
|
|
881 | } |
858 | |
882 | |
859 | return sv; |
883 | return sv; |
860 | |
884 | |
861 | fail: |
885 | fail: |
862 | SvREFCNT_dec (sv); |
886 | SvREFCNT_dec (sv); |
… | |
… | |
911 | case CBOR_TAG_VALUE_SHAREABLE: |
935 | case CBOR_TAG_VALUE_SHAREABLE: |
912 | { |
936 | { |
913 | if (ecb_expect_false (!dec->shareable)) |
937 | if (ecb_expect_false (!dec->shareable)) |
914 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
938 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
915 | |
939 | |
|
|
940 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
941 | { |
916 | sv = newSV (0); |
942 | sv = newSV (0); |
917 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
943 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
918 | |
944 | |
919 | SV *osv = decode_sv (dec); |
945 | SV *osv = decode_sv (dec); |
920 | sv_setsv (sv, osv); |
946 | sv_setsv (sv, osv); |
921 | SvREFCNT_dec_NN (osv); |
947 | SvREFCNT_dec_NN (osv); |
|
|
948 | } |
|
|
949 | else |
|
|
950 | { |
|
|
951 | av_push (dec->shareable, &PL_sv_undef); |
|
|
952 | int idx = AvFILLp (dec->shareable); |
|
|
953 | sv = decode_sv (dec); |
|
|
954 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
955 | } |
922 | } |
956 | } |
923 | break; |
957 | break; |
924 | |
958 | |
925 | case CBOR_TAG_VALUE_SHAREDREF: |
959 | case CBOR_TAG_VALUE_SHAREDREF: |
926 | { |
960 | { |
… | |
… | |
931 | |
965 | |
932 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
966 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
933 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
967 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
934 | |
968 | |
935 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
969 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
970 | |
|
|
971 | if (sv == &PL_sv_undef) |
|
|
972 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
936 | } |
973 | } |
937 | break; |
974 | break; |
938 | |
975 | |
939 | case CBOR_TAG_PERL_OBJECT: |
976 | case CBOR_TAG_PERL_OBJECT: |
940 | { |
977 | { |
… | |
… | |
1109 | |
1146 | |
1110 | return newSVnv (ecb_binary64_to_double (fp)); |
1147 | return newSVnv (ecb_binary64_to_double (fp)); |
1111 | } |
1148 | } |
1112 | |
1149 | |
1113 | // 0..19 unassigned simple |
1150 | // 0..19 unassigned simple |
1114 | // 24 reserved + unassigned (reserved values are not encodable) |
1151 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1152 | // 28-30 unassigned misc |
|
|
1153 | // 31 break code |
1115 | default: |
1154 | default: |
1116 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1155 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1117 | } |
1156 | } |
1118 | |
1157 | |
1119 | break; |
1158 | break; |
1120 | } |
1159 | } |
1121 | |
1160 | |
… | |
… | |
1148 | if (dec.cur != dec.end && !dec.err) |
1187 | if (dec.cur != dec.end && !dec.err) |
1149 | dec.err = "garbage after CBOR object"; |
1188 | dec.err = "garbage after CBOR object"; |
1150 | |
1189 | |
1151 | if (dec.err) |
1190 | if (dec.err) |
1152 | { |
1191 | { |
|
|
1192 | if (dec.shareable) |
|
|
1193 | { |
|
|
1194 | // need to break cyclic links, which whould all be in shareable |
|
|
1195 | int i; |
|
|
1196 | SV **svp; |
|
|
1197 | |
|
|
1198 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1199 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1200 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1201 | } |
|
|
1202 | |
1153 | SvREFCNT_dec (sv); |
1203 | SvREFCNT_dec (sv); |
1154 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1204 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1155 | } |
1205 | } |
1156 | |
1206 | |
1157 | sv = sv_2mortal (sv); |
1207 | sv = sv_2mortal (sv); |
1158 | |
1208 | |
1159 | return sv; |
1209 | return sv; |
1160 | } |
1210 | } |
1161 | |
1211 | |
|
|
1212 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1213 | // incremental parser |
|
|
1214 | |
|
|
1215 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1216 | |
|
|
1217 | // returns 0 for notyet, 1 for success or error |
|
|
1218 | static int |
|
|
1219 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1220 | { |
|
|
1221 | STRLEN cur; |
|
|
1222 | SvPV (cborstr, cur); |
|
|
1223 | |
|
|
1224 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1225 | { |
|
|
1226 | // table of integer count bytes |
|
|
1227 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1228 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1229 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1230 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1231 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1232 | }; |
|
|
1233 | |
|
|
1234 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1235 | U8 m = *p & MINOR_MASK; |
|
|
1236 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1237 | I8 ilen = incr_len[m]; |
|
|
1238 | |
|
|
1239 | self->incr_need = self->incr_pos + 1; |
|
|
1240 | |
|
|
1241 | if (ecb_expect_false (ilen < 0)) |
|
|
1242 | { |
|
|
1243 | if (m != MINOR_INDEF) |
|
|
1244 | return 1; // error |
|
|
1245 | |
|
|
1246 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1247 | { |
|
|
1248 | if (count >= 0) |
|
|
1249 | return 1; // error |
|
|
1250 | |
|
|
1251 | count = 1; |
|
|
1252 | } |
|
|
1253 | else |
|
|
1254 | { |
|
|
1255 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1256 | count = -1; |
|
|
1257 | } |
|
|
1258 | } |
|
|
1259 | else |
|
|
1260 | { |
|
|
1261 | self->incr_need += ilen; |
|
|
1262 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1263 | return 0; |
|
|
1264 | |
|
|
1265 | int major = *p >> MAJOR_SHIFT; |
|
|
1266 | |
|
|
1267 | switch (major) |
|
|
1268 | { |
|
|
1269 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1270 | ++count; // tags merely prefix another value |
|
|
1271 | break; |
|
|
1272 | |
|
|
1273 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1274 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1275 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1276 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1277 | { |
|
|
1278 | UV len; |
|
|
1279 | |
|
|
1280 | if (ecb_expect_false (ilen)) |
|
|
1281 | { |
|
|
1282 | len = 0; |
|
|
1283 | |
|
|
1284 | do { |
|
|
1285 | len = (len << 8) | *++p; |
|
|
1286 | } while (--ilen); |
|
|
1287 | } |
|
|
1288 | else |
|
|
1289 | len = m; |
|
|
1290 | |
|
|
1291 | switch (major) |
|
|
1292 | { |
|
|
1293 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1294 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1295 | self->incr_need += len; |
|
|
1296 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1297 | return 0; |
|
|
1298 | |
|
|
1299 | break; |
|
|
1300 | |
|
|
1301 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1302 | len <<= 1; |
|
|
1303 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1304 | if (len) |
|
|
1305 | { |
|
|
1306 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1307 | count = len + 1; |
|
|
1308 | } |
|
|
1309 | break; |
|
|
1310 | } |
|
|
1311 | } |
|
|
1312 | } |
|
|
1313 | } |
|
|
1314 | |
|
|
1315 | self->incr_pos = self->incr_need; |
|
|
1316 | |
|
|
1317 | if (count > 0) |
|
|
1318 | { |
|
|
1319 | while (!--count) |
|
|
1320 | { |
|
|
1321 | if (!AvFILLp (self->incr_count)) |
|
|
1322 | return 1; // done |
|
|
1323 | |
|
|
1324 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1325 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1326 | } |
|
|
1327 | |
|
|
1328 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1329 | } |
|
|
1330 | } |
|
|
1331 | |
|
|
1332 | return 0; |
|
|
1333 | } |
|
|
1334 | |
|
|
1335 | |
1162 | ///////////////////////////////////////////////////////////////////////////// |
1336 | ///////////////////////////////////////////////////////////////////////////// |
1163 | // XS interface functions |
1337 | // XS interface functions |
1164 | |
1338 | |
1165 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1339 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1166 | |
1340 | |
… | |
… | |
1206 | void shrink (CBOR *self, int enable = 1) |
1380 | void shrink (CBOR *self, int enable = 1) |
1207 | ALIAS: |
1381 | ALIAS: |
1208 | shrink = F_SHRINK |
1382 | shrink = F_SHRINK |
1209 | allow_unknown = F_ALLOW_UNKNOWN |
1383 | allow_unknown = F_ALLOW_UNKNOWN |
1210 | allow_sharing = F_ALLOW_SHARING |
1384 | allow_sharing = F_ALLOW_SHARING |
|
|
1385 | allow_cycles = F_ALLOW_CYCLES |
1211 | pack_strings = F_PACK_STRINGS |
1386 | pack_strings = F_PACK_STRINGS |
|
|
1387 | validate_utf8 = F_VALIDATE_UTF8 |
1212 | PPCODE: |
1388 | PPCODE: |
1213 | { |
1389 | { |
1214 | if (enable) |
1390 | if (enable) |
1215 | self->flags |= ix; |
1391 | self->flags |= ix; |
1216 | else |
1392 | else |
… | |
… | |
1222 | void get_shrink (CBOR *self) |
1398 | void get_shrink (CBOR *self) |
1223 | ALIAS: |
1399 | ALIAS: |
1224 | get_shrink = F_SHRINK |
1400 | get_shrink = F_SHRINK |
1225 | get_allow_unknown = F_ALLOW_UNKNOWN |
1401 | get_allow_unknown = F_ALLOW_UNKNOWN |
1226 | get_allow_sharing = F_ALLOW_SHARING |
1402 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1403 | get_allow_cycles = F_ALLOW_CYCLES |
1227 | get_pack_strings = F_PACK_STRINGS |
1404 | get_pack_strings = F_PACK_STRINGS |
|
|
1405 | get_validate_utf8 = F_VALIDATE_UTF8 |
1228 | PPCODE: |
1406 | PPCODE: |
1229 | XPUSHs (boolSV (self->flags & ix)); |
1407 | XPUSHs (boolSV (self->flags & ix)); |
1230 | |
1408 | |
1231 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1409 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1232 | PPCODE: |
1410 | PPCODE: |
… | |
… | |
1281 | EXTEND (SP, 2); |
1459 | EXTEND (SP, 2); |
1282 | PUSHs (sv); |
1460 | PUSHs (sv); |
1283 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1461 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1284 | } |
1462 | } |
1285 | |
1463 | |
|
|
1464 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1465 | ALIAS: |
|
|
1466 | incr_parse_multiple = 1 |
|
|
1467 | PPCODE: |
|
|
1468 | { |
|
|
1469 | if (SvUTF8 (cborstr)) |
|
|
1470 | sv_utf8_downgrade (cborstr, 0); |
|
|
1471 | |
|
|
1472 | if (!self->incr_count) |
|
|
1473 | { |
|
|
1474 | self->incr_count = newAV (); |
|
|
1475 | self->incr_pos = 0; |
|
|
1476 | self->incr_need = 1; |
|
|
1477 | |
|
|
1478 | av_push (self->incr_count, newSViv (1)); |
|
|
1479 | } |
|
|
1480 | |
|
|
1481 | do |
|
|
1482 | { |
|
|
1483 | if (!incr_parse (self, cborstr)) |
|
|
1484 | { |
|
|
1485 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1486 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1487 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1488 | |
|
|
1489 | break; |
|
|
1490 | } |
|
|
1491 | |
|
|
1492 | SV *sv; |
|
|
1493 | char *offset; |
|
|
1494 | |
|
|
1495 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1496 | XPUSHs (sv); |
|
|
1497 | |
|
|
1498 | sv_chop (cborstr, offset); |
|
|
1499 | |
|
|
1500 | av_clear (self->incr_count); |
|
|
1501 | av_push (self->incr_count, newSViv (1)); |
|
|
1502 | |
|
|
1503 | self->incr_pos = 0; |
|
|
1504 | self->incr_need = self->incr_pos + 1; |
|
|
1505 | } |
|
|
1506 | while (ix); |
|
|
1507 | } |
|
|
1508 | |
|
|
1509 | void incr_reset (CBOR *self) |
|
|
1510 | CODE: |
|
|
1511 | { |
|
|
1512 | SvREFCNT_dec (self->incr_count); |
|
|
1513 | self->incr_count = 0; |
|
|
1514 | } |
|
|
1515 | |
1286 | void DESTROY (CBOR *self) |
1516 | void DESTROY (CBOR *self) |
1287 | PPCODE: |
1517 | PPCODE: |
1288 | cbor_free (self); |
1518 | cbor_free (self); |
1289 | |
1519 | |
1290 | PROTOTYPES: ENABLE |
1520 | PROTOTYPES: ENABLE |
1291 | |
1521 | |
1292 | void encode_cbor (SV *scalar) |
1522 | void encode_cbor (SV *scalar) |
|
|
1523 | ALIAS: |
|
|
1524 | encode_cbor = 0 |
|
|
1525 | encode_cbor_sharing = F_ALLOW_SHARING |
1293 | PPCODE: |
1526 | PPCODE: |
1294 | { |
1527 | { |
1295 | CBOR cbor; |
1528 | CBOR cbor; |
1296 | cbor_init (&cbor); |
1529 | cbor_init (&cbor); |
|
|
1530 | cbor.flags |= ix; |
1297 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1531 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1298 | XPUSHs (scalar); |
1532 | XPUSHs (scalar); |
1299 | } |
1533 | } |
1300 | |
1534 | |
1301 | void decode_cbor (SV *cborstr) |
1535 | void decode_cbor (SV *cborstr) |