… | |
… | |
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_UTF8_STRINGS 0x00000020UL |
|
|
106 | #define F_VALIDATE_UTF8 0x00000040UL |
103 | |
107 | |
104 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | |
109 | |
106 | #define SB do { |
110 | #define SB do { |
107 | #define SE } while (0) |
111 | #define SE } while (0) |
… | |
… | |
126 | typedef struct { |
130 | typedef struct { |
127 | U32 flags; |
131 | U32 flags; |
128 | U32 max_depth; |
132 | U32 max_depth; |
129 | STRLEN max_size; |
133 | STRLEN max_size; |
130 | SV *filter; |
134 | SV *filter; |
|
|
135 | |
|
|
136 | // for the incremental parser |
|
|
137 | STRLEN incr_pos; // the current offset into the text |
|
|
138 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
139 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
131 | } CBOR; |
140 | } CBOR; |
132 | |
141 | |
133 | ecb_inline void |
142 | ecb_inline void |
134 | cbor_init (CBOR *cbor) |
143 | cbor_init (CBOR *cbor) |
135 | { |
144 | { |
… | |
… | |
139 | |
148 | |
140 | ecb_inline void |
149 | ecb_inline void |
141 | cbor_free (CBOR *cbor) |
150 | cbor_free (CBOR *cbor) |
142 | { |
151 | { |
143 | SvREFCNT_dec (cbor->filter); |
152 | SvREFCNT_dec (cbor->filter); |
|
|
153 | SvREFCNT_dec (cbor->incr_count); |
144 | } |
154 | } |
145 | |
155 | |
146 | ///////////////////////////////////////////////////////////////////////////// |
156 | ///////////////////////////////////////////////////////////////////////////// |
147 | // utility functions |
157 | // utility functions |
148 | |
158 | |
… | |
… | |
270 | } |
280 | } |
271 | |
281 | |
272 | ecb_inline void |
282 | ecb_inline void |
273 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
274 | { |
284 | { |
|
|
285 | if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) |
|
|
286 | if (!utf8) |
|
|
287 | { |
|
|
288 | SV *sv = sv_newmortal (); |
|
|
289 | char *s; STRLEN l; |
|
|
290 | |
|
|
291 | sv_setpvn (sv, str, len); |
|
|
292 | |
|
|
293 | s = SvPVutf8 (sv, l); |
|
|
294 | encode_str (enc, 1, s, l); |
|
|
295 | return; |
|
|
296 | } |
|
|
297 | |
275 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
298 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
276 | need (enc, len); |
299 | need (enc, len); |
277 | memcpy (enc->cur, str, len); |
300 | memcpy (enc->cur, str, len); |
278 | enc->cur += len; |
301 | enc->cur += len; |
279 | } |
302 | } |
… | |
… | |
315 | |
338 | |
316 | ++enc->depth; |
339 | ++enc->depth; |
317 | |
340 | |
318 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
341 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
319 | |
342 | |
|
|
343 | if (SvMAGICAL (av)) |
320 | for (i = 0; i <= len; ++i) |
344 | for (i = 0; i <= len; ++i) |
321 | { |
345 | { |
322 | SV **svp = av_fetch (av, i, 0); |
346 | SV **svp = av_fetch (av, i, 0); |
323 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
347 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | } |
348 | } |
|
|
349 | else |
|
|
350 | for (i = 0; i <= len; ++i) |
|
|
351 | { |
|
|
352 | SV *sv = AvARRAY (av)[i]; |
|
|
353 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
354 | } |
325 | |
355 | |
326 | --enc->depth; |
356 | --enc->depth; |
327 | } |
357 | } |
328 | |
358 | |
329 | static void |
359 | static void |
… | |
… | |
433 | |
463 | |
434 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
464 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
435 | { |
465 | { |
436 | dSP; |
466 | dSP; |
437 | |
467 | |
438 | ENTER; SAVETMPS; PUSHMARK (SP); |
468 | ENTER; SAVETMPS; |
|
|
469 | PUSHMARK (SP); |
439 | // we re-bless the reference to get overload and other niceties right |
470 | // we re-bless the reference to get overload and other niceties right |
440 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
471 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
441 | |
472 | |
442 | PUTBACK; |
473 | PUTBACK; |
443 | // G_SCALAR ensures that return value is 1 |
474 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
456 | } |
487 | } |
457 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
488 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
458 | { |
489 | { |
459 | dSP; |
490 | dSP; |
460 | |
491 | |
461 | ENTER; SAVETMPS; PUSHMARK (SP); |
492 | ENTER; SAVETMPS; |
|
|
493 | SAVESTACK_POS (); |
|
|
494 | PUSHMARK (SP); |
462 | EXTEND (SP, 2); |
495 | EXTEND (SP, 2); |
463 | // we re-bless the reference to get overload and other niceties right |
496 | // we re-bless the reference to get overload and other niceties right |
464 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
497 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
465 | PUSHs (sv_cbor); |
498 | PUSHs (sv_cbor); |
466 | |
499 | |
… | |
… | |
568 | } |
601 | } |
569 | |
602 | |
570 | static SV * |
603 | static SV * |
571 | encode_cbor (SV *scalar, CBOR *cbor) |
604 | encode_cbor (SV *scalar, CBOR *cbor) |
572 | { |
605 | { |
573 | enc_t enc = { }; |
606 | enc_t enc = { 0 }; |
574 | |
607 | |
575 | enc.cbor = *cbor; |
608 | enc.cbor = *cbor; |
576 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
609 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
577 | enc.cur = SvPVX (enc.sv); |
610 | enc.cur = SvPVX (enc.sv); |
578 | enc.end = SvEND (enc.sv); |
611 | enc.end = SvEND (enc.sv); |
… | |
… | |
728 | { |
761 | { |
729 | // for speed reasons, we specialcase single-string |
762 | // for speed reasons, we specialcase single-string |
730 | // byte or utf-8 strings as keys, but only when !stringref |
763 | // byte or utf-8 strings as keys, but only when !stringref |
731 | |
764 | |
732 | if (ecb_expect_true (!dec->stringref)) |
765 | if (ecb_expect_true (!dec->stringref)) |
733 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
766 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
734 | { |
767 | { |
735 | I32 len = decode_uint (dec); |
768 | I32 len = decode_uint (dec); |
736 | char *key = (char *)dec->cur; |
769 | char *key = (char *)dec->cur; |
737 | |
770 | |
|
|
771 | WANT (len); |
738 | dec->cur += len; |
772 | dec->cur += len; |
739 | |
773 | |
740 | if (ecb_expect_false (dec->stringref)) |
|
|
741 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
742 | |
|
|
743 | hv_store (hv, key, len, decode_sv (dec), 0); |
774 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | |
775 | |
745 | return; |
776 | return; |
746 | } |
777 | } |
747 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
778 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
748 | { |
779 | { |
749 | I32 len = decode_uint (dec); |
780 | I32 len = decode_uint (dec); |
750 | char *key = (char *)dec->cur; |
781 | char *key = (char *)dec->cur; |
751 | |
782 | |
|
|
783 | WANT (len); |
752 | dec->cur += len; |
784 | dec->cur += len; |
753 | |
785 | |
754 | if (ecb_expect_false (dec->stringref)) |
786 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
755 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
787 | if (!is_utf8_string (key, len)) |
|
|
788 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
756 | |
789 | |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
790 | hv_store (hv, key, -len, decode_sv (dec), 0); |
758 | |
791 | |
759 | return; |
792 | return; |
760 | } |
793 | } |
… | |
… | |
762 | SV *k = decode_sv (dec); |
795 | SV *k = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
796 | SV *v = decode_sv (dec); |
764 | |
797 | |
765 | hv_store_ent (hv, k, v, 0); |
798 | hv_store_ent (hv, k, v, 0); |
766 | SvREFCNT_dec (k); |
799 | SvREFCNT_dec (k); |
|
|
800 | |
|
|
801 | fail: |
|
|
802 | ; |
767 | } |
803 | } |
768 | |
804 | |
769 | static SV * |
805 | static SV * |
770 | decode_hv (dec_t *dec) |
806 | decode_hv (dec_t *dec) |
771 | { |
807 | { |
… | |
… | |
853 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
889 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
854 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
890 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
855 | } |
891 | } |
856 | |
892 | |
857 | if (utf8) |
893 | if (utf8) |
|
|
894 | { |
|
|
895 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
896 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
897 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
898 | |
858 | SvUTF8_on (sv); |
899 | SvUTF8_on (sv); |
|
|
900 | } |
859 | |
901 | |
860 | return sv; |
902 | return sv; |
861 | |
903 | |
862 | fail: |
904 | fail: |
863 | SvREFCNT_dec (sv); |
905 | SvREFCNT_dec (sv); |
… | |
… | |
912 | case CBOR_TAG_VALUE_SHAREABLE: |
954 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | { |
955 | { |
914 | if (ecb_expect_false (!dec->shareable)) |
956 | if (ecb_expect_false (!dec->shareable)) |
915 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
957 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | |
958 | |
|
|
959 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
960 | { |
917 | sv = newSV (0); |
961 | sv = newSV (0); |
918 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
962 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
919 | |
963 | |
920 | SV *osv = decode_sv (dec); |
964 | SV *osv = decode_sv (dec); |
921 | sv_setsv (sv, osv); |
965 | sv_setsv (sv, osv); |
922 | SvREFCNT_dec_NN (osv); |
966 | SvREFCNT_dec_NN (osv); |
|
|
967 | } |
|
|
968 | else |
|
|
969 | { |
|
|
970 | av_push (dec->shareable, &PL_sv_undef); |
|
|
971 | int idx = AvFILLp (dec->shareable); |
|
|
972 | sv = decode_sv (dec); |
|
|
973 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
974 | } |
923 | } |
975 | } |
924 | break; |
976 | break; |
925 | |
977 | |
926 | case CBOR_TAG_VALUE_SHAREDREF: |
978 | case CBOR_TAG_VALUE_SHAREDREF: |
927 | { |
979 | { |
… | |
… | |
932 | |
984 | |
933 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
985 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
934 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
986 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
935 | |
987 | |
936 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
988 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
989 | |
|
|
990 | if (sv == &PL_sv_undef) |
|
|
991 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
937 | } |
992 | } |
938 | break; |
993 | break; |
939 | |
994 | |
940 | case CBOR_TAG_PERL_OBJECT: |
995 | case CBOR_TAG_PERL_OBJECT: |
941 | { |
996 | { |
… | |
… | |
956 | if (!method) |
1011 | if (!method) |
957 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1012 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
958 | |
1013 | |
959 | dSP; |
1014 | dSP; |
960 | |
1015 | |
961 | ENTER; SAVETMPS; PUSHMARK (SP); |
1016 | ENTER; SAVETMPS; |
|
|
1017 | PUSHMARK (SP); |
962 | EXTEND (SP, len + 1); |
1018 | EXTEND (SP, len + 1); |
963 | // we re-bless the reference to get overload and other niceties right |
1019 | // we re-bless the reference to get overload and other niceties right |
964 | PUSHs (*av_fetch (av, 0, 1)); |
1020 | PUSHs (*av_fetch (av, 0, 1)); |
965 | PUSHs (sv_cbor); |
1021 | PUSHs (sv_cbor); |
966 | |
1022 | |
… | |
… | |
991 | default: |
1047 | default: |
992 | { |
1048 | { |
993 | sv = decode_sv (dec); |
1049 | sv = decode_sv (dec); |
994 | |
1050 | |
995 | dSP; |
1051 | dSP; |
996 | ENTER; SAVETMPS; PUSHMARK (SP); |
1052 | ENTER; SAVETMPS; |
|
|
1053 | SAVESTACK_POS (); |
|
|
1054 | PUSHMARK (SP); |
997 | EXTEND (SP, 2); |
1055 | EXTEND (SP, 2); |
998 | PUSHs (newSVuv (tag)); |
1056 | PUSHs (newSVuv (tag)); |
999 | PUSHs (sv); |
1057 | PUSHs (sv); |
1000 | |
1058 | |
1001 | PUTBACK; |
1059 | PUTBACK; |
… | |
… | |
1110 | |
1168 | |
1111 | return newSVnv (ecb_binary64_to_double (fp)); |
1169 | return newSVnv (ecb_binary64_to_double (fp)); |
1112 | } |
1170 | } |
1113 | |
1171 | |
1114 | // 0..19 unassigned simple |
1172 | // 0..19 unassigned simple |
1115 | // 24 reserved + unassigned (reserved values are not encodable) |
1173 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1174 | // 28-30 unassigned misc |
|
|
1175 | // 31 break code |
1116 | default: |
1176 | default: |
1117 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1177 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1118 | } |
1178 | } |
1119 | |
1179 | |
1120 | break; |
1180 | break; |
1121 | } |
1181 | } |
1122 | |
1182 | |
… | |
… | |
1125 | } |
1185 | } |
1126 | |
1186 | |
1127 | static SV * |
1187 | static SV * |
1128 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1188 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1129 | { |
1189 | { |
1130 | dec_t dec = { }; |
1190 | dec_t dec = { 0 }; |
1131 | SV *sv; |
1191 | SV *sv; |
1132 | STRLEN len; |
1192 | STRLEN len; |
1133 | char *data = SvPVbyte (string, len); |
1193 | char *data = SvPVbyte (string, len); |
1134 | |
1194 | |
1135 | if (len > cbor->max_size && cbor->max_size) |
1195 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1149 | if (dec.cur != dec.end && !dec.err) |
1209 | if (dec.cur != dec.end && !dec.err) |
1150 | dec.err = "garbage after CBOR object"; |
1210 | dec.err = "garbage after CBOR object"; |
1151 | |
1211 | |
1152 | if (dec.err) |
1212 | if (dec.err) |
1153 | { |
1213 | { |
|
|
1214 | if (dec.shareable) |
|
|
1215 | { |
|
|
1216 | // need to break cyclic links, which whould all be in shareable |
|
|
1217 | int i; |
|
|
1218 | SV **svp; |
|
|
1219 | |
|
|
1220 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1221 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1222 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1223 | } |
|
|
1224 | |
1154 | SvREFCNT_dec (sv); |
1225 | SvREFCNT_dec (sv); |
1155 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1226 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1156 | } |
1227 | } |
1157 | |
1228 | |
1158 | sv = sv_2mortal (sv); |
1229 | sv = sv_2mortal (sv); |
1159 | |
1230 | |
1160 | return sv; |
1231 | return sv; |
1161 | } |
1232 | } |
1162 | |
1233 | |
|
|
1234 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1235 | // incremental parser |
|
|
1236 | |
|
|
1237 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1238 | |
|
|
1239 | // returns 0 for notyet, 1 for success or error |
|
|
1240 | static int |
|
|
1241 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1242 | { |
|
|
1243 | STRLEN cur; |
|
|
1244 | SvPV (cborstr, cur); |
|
|
1245 | |
|
|
1246 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1247 | { |
|
|
1248 | // table of integer count bytes |
|
|
1249 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1250 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1251 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1252 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1253 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1254 | }; |
|
|
1255 | |
|
|
1256 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1257 | U8 m = *p & MINOR_MASK; |
|
|
1258 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1259 | I8 ilen = incr_len[m]; |
|
|
1260 | |
|
|
1261 | self->incr_need = self->incr_pos + 1; |
|
|
1262 | |
|
|
1263 | if (ecb_expect_false (ilen < 0)) |
|
|
1264 | { |
|
|
1265 | if (m != MINOR_INDEF) |
|
|
1266 | return 1; // error |
|
|
1267 | |
|
|
1268 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1269 | { |
|
|
1270 | if (count >= 0) |
|
|
1271 | return 1; // error |
|
|
1272 | |
|
|
1273 | count = 1; |
|
|
1274 | } |
|
|
1275 | else |
|
|
1276 | { |
|
|
1277 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1278 | count = -1; |
|
|
1279 | } |
|
|
1280 | } |
|
|
1281 | else |
|
|
1282 | { |
|
|
1283 | self->incr_need += ilen; |
|
|
1284 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1285 | return 0; |
|
|
1286 | |
|
|
1287 | int major = *p >> MAJOR_SHIFT; |
|
|
1288 | |
|
|
1289 | switch (major) |
|
|
1290 | { |
|
|
1291 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1292 | ++count; // tags merely prefix another value |
|
|
1293 | break; |
|
|
1294 | |
|
|
1295 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1296 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1297 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1298 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1299 | { |
|
|
1300 | UV len; |
|
|
1301 | |
|
|
1302 | if (ecb_expect_false (ilen)) |
|
|
1303 | { |
|
|
1304 | len = 0; |
|
|
1305 | |
|
|
1306 | do { |
|
|
1307 | len = (len << 8) | *++p; |
|
|
1308 | } while (--ilen); |
|
|
1309 | } |
|
|
1310 | else |
|
|
1311 | len = m; |
|
|
1312 | |
|
|
1313 | switch (major) |
|
|
1314 | { |
|
|
1315 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1316 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1317 | self->incr_need += len; |
|
|
1318 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1319 | return 0; |
|
|
1320 | |
|
|
1321 | break; |
|
|
1322 | |
|
|
1323 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1324 | len <<= 1; |
|
|
1325 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1326 | if (len) |
|
|
1327 | { |
|
|
1328 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1329 | count = len + 1; |
|
|
1330 | } |
|
|
1331 | break; |
|
|
1332 | } |
|
|
1333 | } |
|
|
1334 | } |
|
|
1335 | } |
|
|
1336 | |
|
|
1337 | self->incr_pos = self->incr_need; |
|
|
1338 | |
|
|
1339 | if (count > 0) |
|
|
1340 | { |
|
|
1341 | while (!--count) |
|
|
1342 | { |
|
|
1343 | if (!AvFILLp (self->incr_count)) |
|
|
1344 | return 1; // done |
|
|
1345 | |
|
|
1346 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1347 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1348 | } |
|
|
1349 | |
|
|
1350 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1351 | } |
|
|
1352 | } |
|
|
1353 | |
|
|
1354 | return 0; |
|
|
1355 | } |
|
|
1356 | |
|
|
1357 | |
1163 | ///////////////////////////////////////////////////////////////////////////// |
1358 | ///////////////////////////////////////////////////////////////////////////// |
1164 | // XS interface functions |
1359 | // XS interface functions |
1165 | |
1360 | |
1166 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1361 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1167 | |
1362 | |
… | |
… | |
1207 | void shrink (CBOR *self, int enable = 1) |
1402 | void shrink (CBOR *self, int enable = 1) |
1208 | ALIAS: |
1403 | ALIAS: |
1209 | shrink = F_SHRINK |
1404 | shrink = F_SHRINK |
1210 | allow_unknown = F_ALLOW_UNKNOWN |
1405 | allow_unknown = F_ALLOW_UNKNOWN |
1211 | allow_sharing = F_ALLOW_SHARING |
1406 | allow_sharing = F_ALLOW_SHARING |
|
|
1407 | allow_cycles = F_ALLOW_CYCLES |
1212 | pack_strings = F_PACK_STRINGS |
1408 | pack_strings = F_PACK_STRINGS |
|
|
1409 | utf8_strings = F_UTF8_STRINGS |
|
|
1410 | validate_utf8 = F_VALIDATE_UTF8 |
1213 | PPCODE: |
1411 | PPCODE: |
1214 | { |
1412 | { |
1215 | if (enable) |
1413 | if (enable) |
1216 | self->flags |= ix; |
1414 | self->flags |= ix; |
1217 | else |
1415 | else |
… | |
… | |
1223 | void get_shrink (CBOR *self) |
1421 | void get_shrink (CBOR *self) |
1224 | ALIAS: |
1422 | ALIAS: |
1225 | get_shrink = F_SHRINK |
1423 | get_shrink = F_SHRINK |
1226 | get_allow_unknown = F_ALLOW_UNKNOWN |
1424 | get_allow_unknown = F_ALLOW_UNKNOWN |
1227 | get_allow_sharing = F_ALLOW_SHARING |
1425 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1426 | get_allow_cycles = F_ALLOW_CYCLES |
1228 | get_pack_strings = F_PACK_STRINGS |
1427 | get_pack_strings = F_PACK_STRINGS |
|
|
1428 | get_validate_utf8 = F_VALIDATE_UTF8 |
1229 | PPCODE: |
1429 | PPCODE: |
1230 | XPUSHs (boolSV (self->flags & ix)); |
1430 | XPUSHs (boolSV (self->flags & ix)); |
1231 | |
1431 | |
1232 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1432 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1233 | PPCODE: |
1433 | PPCODE: |
… | |
… | |
1282 | EXTEND (SP, 2); |
1482 | EXTEND (SP, 2); |
1283 | PUSHs (sv); |
1483 | PUSHs (sv); |
1284 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1484 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1285 | } |
1485 | } |
1286 | |
1486 | |
|
|
1487 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1488 | ALIAS: |
|
|
1489 | incr_parse_multiple = 1 |
|
|
1490 | PPCODE: |
|
|
1491 | { |
|
|
1492 | if (SvUTF8 (cborstr)) |
|
|
1493 | sv_utf8_downgrade (cborstr, 0); |
|
|
1494 | |
|
|
1495 | if (!self->incr_count) |
|
|
1496 | { |
|
|
1497 | self->incr_count = newAV (); |
|
|
1498 | self->incr_pos = 0; |
|
|
1499 | self->incr_need = 1; |
|
|
1500 | |
|
|
1501 | av_push (self->incr_count, newSViv (1)); |
|
|
1502 | } |
|
|
1503 | |
|
|
1504 | do |
|
|
1505 | { |
|
|
1506 | if (!incr_parse (self, cborstr)) |
|
|
1507 | { |
|
|
1508 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1509 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1510 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1511 | |
|
|
1512 | break; |
|
|
1513 | } |
|
|
1514 | |
|
|
1515 | SV *sv; |
|
|
1516 | char *offset; |
|
|
1517 | |
|
|
1518 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1519 | XPUSHs (sv); |
|
|
1520 | |
|
|
1521 | sv_chop (cborstr, offset); |
|
|
1522 | |
|
|
1523 | av_clear (self->incr_count); |
|
|
1524 | av_push (self->incr_count, newSViv (1)); |
|
|
1525 | |
|
|
1526 | self->incr_pos = 0; |
|
|
1527 | self->incr_need = self->incr_pos + 1; |
|
|
1528 | } |
|
|
1529 | while (ix); |
|
|
1530 | } |
|
|
1531 | |
|
|
1532 | void incr_reset (CBOR *self) |
|
|
1533 | CODE: |
|
|
1534 | { |
|
|
1535 | SvREFCNT_dec (self->incr_count); |
|
|
1536 | self->incr_count = 0; |
|
|
1537 | } |
|
|
1538 | |
1287 | void DESTROY (CBOR *self) |
1539 | void DESTROY (CBOR *self) |
1288 | PPCODE: |
1540 | PPCODE: |
1289 | cbor_free (self); |
1541 | cbor_free (self); |
1290 | |
1542 | |
1291 | PROTOTYPES: ENABLE |
1543 | PROTOTYPES: ENABLE |