… | |
… | |
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 | |
… | |
… | |
728 | { |
737 | { |
729 | // for speed reasons, we specialcase single-string |
738 | // for speed reasons, we specialcase single-string |
730 | // byte or utf-8 strings as keys, but only when !stringref |
739 | // byte or utf-8 strings as keys, but only when !stringref |
731 | |
740 | |
732 | if (ecb_expect_true (!dec->stringref)) |
741 | if (ecb_expect_true (!dec->stringref)) |
733 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
742 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
734 | { |
743 | { |
735 | I32 len = decode_uint (dec); |
744 | I32 len = decode_uint (dec); |
736 | char *key = (char *)dec->cur; |
745 | char *key = (char *)dec->cur; |
737 | |
746 | |
738 | dec->cur += len; |
747 | dec->cur += len; |
739 | |
748 | |
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); |
749 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | |
750 | |
745 | return; |
751 | return; |
746 | } |
752 | } |
747 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
753 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
748 | { |
754 | { |
749 | I32 len = decode_uint (dec); |
755 | I32 len = decode_uint (dec); |
750 | char *key = (char *)dec->cur; |
756 | char *key = (char *)dec->cur; |
751 | |
757 | |
752 | dec->cur += len; |
758 | dec->cur += len; |
753 | |
759 | |
754 | if (ecb_expect_false (dec->stringref)) |
760 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
755 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
761 | if (!is_utf8_string (key, len)) |
|
|
762 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
756 | |
763 | |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
764 | hv_store (hv, key, -len, decode_sv (dec), 0); |
758 | |
765 | |
759 | return; |
766 | return; |
760 | } |
767 | } |
… | |
… | |
762 | SV *k = decode_sv (dec); |
769 | SV *k = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
770 | SV *v = decode_sv (dec); |
764 | |
771 | |
765 | hv_store_ent (hv, k, v, 0); |
772 | hv_store_ent (hv, k, v, 0); |
766 | SvREFCNT_dec (k); |
773 | SvREFCNT_dec (k); |
|
|
774 | |
|
|
775 | fail: |
|
|
776 | ; |
767 | } |
777 | } |
768 | |
778 | |
769 | static SV * |
779 | static SV * |
770 | decode_hv (dec_t *dec) |
780 | decode_hv (dec_t *dec) |
771 | { |
781 | { |
… | |
… | |
853 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
863 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
854 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
864 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
855 | } |
865 | } |
856 | |
866 | |
857 | if (utf8) |
867 | if (utf8) |
|
|
868 | { |
|
|
869 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
870 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
871 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
872 | |
858 | SvUTF8_on (sv); |
873 | SvUTF8_on (sv); |
|
|
874 | } |
859 | |
875 | |
860 | return sv; |
876 | return sv; |
861 | |
877 | |
862 | fail: |
878 | fail: |
863 | SvREFCNT_dec (sv); |
879 | SvREFCNT_dec (sv); |
… | |
… | |
912 | case CBOR_TAG_VALUE_SHAREABLE: |
928 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | { |
929 | { |
914 | if (ecb_expect_false (!dec->shareable)) |
930 | if (ecb_expect_false (!dec->shareable)) |
915 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
931 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | |
932 | |
|
|
933 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
934 | { |
917 | sv = newSV (0); |
935 | sv = newSV (0); |
918 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
936 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
919 | |
937 | |
920 | SV *osv = decode_sv (dec); |
938 | SV *osv = decode_sv (dec); |
921 | sv_setsv (sv, osv); |
939 | sv_setsv (sv, osv); |
922 | SvREFCNT_dec_NN (osv); |
940 | SvREFCNT_dec_NN (osv); |
|
|
941 | } |
|
|
942 | else |
|
|
943 | { |
|
|
944 | av_push (dec->shareable, &PL_sv_undef); |
|
|
945 | int idx = AvFILLp (dec->shareable); |
|
|
946 | sv = decode_sv (dec); |
|
|
947 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
948 | } |
923 | } |
949 | } |
924 | break; |
950 | break; |
925 | |
951 | |
926 | case CBOR_TAG_VALUE_SHAREDREF: |
952 | case CBOR_TAG_VALUE_SHAREDREF: |
927 | { |
953 | { |
… | |
… | |
932 | |
958 | |
933 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
959 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
934 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
960 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
935 | |
961 | |
936 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
962 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
963 | |
|
|
964 | if (sv == &PL_sv_undef) |
|
|
965 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
937 | } |
966 | } |
938 | break; |
967 | break; |
939 | |
968 | |
940 | case CBOR_TAG_PERL_OBJECT: |
969 | case CBOR_TAG_PERL_OBJECT: |
941 | { |
970 | { |
… | |
… | |
1110 | |
1139 | |
1111 | return newSVnv (ecb_binary64_to_double (fp)); |
1140 | return newSVnv (ecb_binary64_to_double (fp)); |
1112 | } |
1141 | } |
1113 | |
1142 | |
1114 | // 0..19 unassigned simple |
1143 | // 0..19 unassigned simple |
1115 | // 24 reserved + unassigned (reserved values are not encodable) |
1144 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1145 | // 28-30 unassigned misc |
|
|
1146 | // 31 break code |
1116 | default: |
1147 | default: |
1117 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1148 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1118 | } |
1149 | } |
1119 | |
1150 | |
1120 | break; |
1151 | break; |
1121 | } |
1152 | } |
1122 | |
1153 | |
… | |
… | |
1149 | if (dec.cur != dec.end && !dec.err) |
1180 | if (dec.cur != dec.end && !dec.err) |
1150 | dec.err = "garbage after CBOR object"; |
1181 | dec.err = "garbage after CBOR object"; |
1151 | |
1182 | |
1152 | if (dec.err) |
1183 | if (dec.err) |
1153 | { |
1184 | { |
|
|
1185 | if (dec.shareable) |
|
|
1186 | { |
|
|
1187 | // need to break cyclic links, which whould all be in shareable |
|
|
1188 | int i; |
|
|
1189 | SV **svp; |
|
|
1190 | |
|
|
1191 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1192 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1193 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1194 | } |
|
|
1195 | |
1154 | SvREFCNT_dec (sv); |
1196 | SvREFCNT_dec (sv); |
1155 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1197 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1156 | } |
1198 | } |
1157 | |
1199 | |
1158 | sv = sv_2mortal (sv); |
1200 | sv = sv_2mortal (sv); |
1159 | |
1201 | |
1160 | return sv; |
1202 | return sv; |
1161 | } |
1203 | } |
1162 | |
1204 | |
|
|
1205 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1206 | // incremental parser |
|
|
1207 | |
|
|
1208 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1209 | |
|
|
1210 | // returns 0 for notyet, 1 for success or error |
|
|
1211 | static int |
|
|
1212 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1213 | { |
|
|
1214 | STRLEN cur; |
|
|
1215 | SvPV (cborstr, cur); |
|
|
1216 | |
|
|
1217 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1218 | { |
|
|
1219 | // table of integer count bytes |
|
|
1220 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1221 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1222 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1223 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1224 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1225 | }; |
|
|
1226 | |
|
|
1227 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1228 | U8 m = *p & MINOR_MASK; |
|
|
1229 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1230 | I8 ilen = incr_len[m]; |
|
|
1231 | |
|
|
1232 | self->incr_need = self->incr_pos + 1; |
|
|
1233 | |
|
|
1234 | if (ecb_expect_false (ilen < 0)) |
|
|
1235 | { |
|
|
1236 | if (m != MINOR_INDEF) |
|
|
1237 | return 1; // error |
|
|
1238 | |
|
|
1239 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1240 | { |
|
|
1241 | if (count >= 0) |
|
|
1242 | return 1; // error |
|
|
1243 | |
|
|
1244 | count = 1; |
|
|
1245 | } |
|
|
1246 | else |
|
|
1247 | { |
|
|
1248 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1249 | count = -1; |
|
|
1250 | } |
|
|
1251 | } |
|
|
1252 | else |
|
|
1253 | { |
|
|
1254 | self->incr_need += ilen; |
|
|
1255 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1256 | return 0; |
|
|
1257 | |
|
|
1258 | int major = *p >> MAJOR_SHIFT; |
|
|
1259 | |
|
|
1260 | switch (major) |
|
|
1261 | { |
|
|
1262 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1263 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1264 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1265 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1266 | { |
|
|
1267 | UV len; |
|
|
1268 | |
|
|
1269 | if (ecb_expect_false (ilen)) |
|
|
1270 | { |
|
|
1271 | len = 0; |
|
|
1272 | |
|
|
1273 | do { |
|
|
1274 | len = (len << 8) | *++p; |
|
|
1275 | } while (--ilen); |
|
|
1276 | } |
|
|
1277 | else |
|
|
1278 | len = m; |
|
|
1279 | |
|
|
1280 | switch (major) |
|
|
1281 | { |
|
|
1282 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1283 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1284 | self->incr_need += len; |
|
|
1285 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1286 | return 0; |
|
|
1287 | |
|
|
1288 | break; |
|
|
1289 | |
|
|
1290 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1291 | len <<= 1; |
|
|
1292 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1293 | if (len) |
|
|
1294 | { |
|
|
1295 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1296 | count = len + 1; |
|
|
1297 | } |
|
|
1298 | break; |
|
|
1299 | } |
|
|
1300 | } |
|
|
1301 | } |
|
|
1302 | } |
|
|
1303 | |
|
|
1304 | self->incr_pos = self->incr_need; |
|
|
1305 | |
|
|
1306 | if (count > 0) |
|
|
1307 | { |
|
|
1308 | while (!--count) |
|
|
1309 | { |
|
|
1310 | if (!AvFILLp (self->incr_count)) |
|
|
1311 | return 1; // done |
|
|
1312 | |
|
|
1313 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1314 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1318 | } |
|
|
1319 | } |
|
|
1320 | |
|
|
1321 | return 0; |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | |
1163 | ///////////////////////////////////////////////////////////////////////////// |
1325 | ///////////////////////////////////////////////////////////////////////////// |
1164 | // XS interface functions |
1326 | // XS interface functions |
1165 | |
1327 | |
1166 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1328 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1167 | |
1329 | |
… | |
… | |
1207 | void shrink (CBOR *self, int enable = 1) |
1369 | void shrink (CBOR *self, int enable = 1) |
1208 | ALIAS: |
1370 | ALIAS: |
1209 | shrink = F_SHRINK |
1371 | shrink = F_SHRINK |
1210 | allow_unknown = F_ALLOW_UNKNOWN |
1372 | allow_unknown = F_ALLOW_UNKNOWN |
1211 | allow_sharing = F_ALLOW_SHARING |
1373 | allow_sharing = F_ALLOW_SHARING |
|
|
1374 | allow_cycles = F_ALLOW_CYCLES |
1212 | pack_strings = F_PACK_STRINGS |
1375 | pack_strings = F_PACK_STRINGS |
|
|
1376 | validate_utf8 = F_VALIDATE_UTF8 |
1213 | PPCODE: |
1377 | PPCODE: |
1214 | { |
1378 | { |
1215 | if (enable) |
1379 | if (enable) |
1216 | self->flags |= ix; |
1380 | self->flags |= ix; |
1217 | else |
1381 | else |
… | |
… | |
1223 | void get_shrink (CBOR *self) |
1387 | void get_shrink (CBOR *self) |
1224 | ALIAS: |
1388 | ALIAS: |
1225 | get_shrink = F_SHRINK |
1389 | get_shrink = F_SHRINK |
1226 | get_allow_unknown = F_ALLOW_UNKNOWN |
1390 | get_allow_unknown = F_ALLOW_UNKNOWN |
1227 | get_allow_sharing = F_ALLOW_SHARING |
1391 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1392 | get_allow_cycles = F_ALLOW_CYCLES |
1228 | get_pack_strings = F_PACK_STRINGS |
1393 | get_pack_strings = F_PACK_STRINGS |
|
|
1394 | get_validate_utf8 = F_VALIDATE_UTF8 |
1229 | PPCODE: |
1395 | PPCODE: |
1230 | XPUSHs (boolSV (self->flags & ix)); |
1396 | XPUSHs (boolSV (self->flags & ix)); |
1231 | |
1397 | |
1232 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1398 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1233 | PPCODE: |
1399 | PPCODE: |
… | |
… | |
1282 | EXTEND (SP, 2); |
1448 | EXTEND (SP, 2); |
1283 | PUSHs (sv); |
1449 | PUSHs (sv); |
1284 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1450 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1285 | } |
1451 | } |
1286 | |
1452 | |
|
|
1453 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1454 | ALIAS: |
|
|
1455 | incr_parse_multiple = 1 |
|
|
1456 | PPCODE: |
|
|
1457 | { |
|
|
1458 | if (SvUTF8 (cborstr)) |
|
|
1459 | sv_utf8_downgrade (cborstr, 0); |
|
|
1460 | |
|
|
1461 | if (!self->incr_count) |
|
|
1462 | { |
|
|
1463 | self->incr_count = newAV (); |
|
|
1464 | self->incr_pos = 0; |
|
|
1465 | self->incr_need = 1; |
|
|
1466 | |
|
|
1467 | av_push (self->incr_count, newSViv (1)); |
|
|
1468 | } |
|
|
1469 | |
|
|
1470 | do |
|
|
1471 | { |
|
|
1472 | if (!incr_parse (self, cborstr)) |
|
|
1473 | { |
|
|
1474 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1475 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1476 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1477 | |
|
|
1478 | break; |
|
|
1479 | } |
|
|
1480 | |
|
|
1481 | SV *sv; |
|
|
1482 | char *offset; |
|
|
1483 | |
|
|
1484 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1485 | XPUSHs (sv); |
|
|
1486 | |
|
|
1487 | sv_chop (cborstr, offset); |
|
|
1488 | |
|
|
1489 | av_clear (self->incr_count); |
|
|
1490 | av_push (self->incr_count, newSViv (1)); |
|
|
1491 | |
|
|
1492 | self->incr_pos = 0; |
|
|
1493 | self->incr_need = self->incr_pos + 1; |
|
|
1494 | } |
|
|
1495 | while (ix); |
|
|
1496 | } |
|
|
1497 | |
|
|
1498 | void incr_reset (CBOR *self) |
|
|
1499 | CODE: |
|
|
1500 | { |
|
|
1501 | SvREFCNT_dec (self->incr_count); |
|
|
1502 | self->incr_count = 0; |
|
|
1503 | } |
|
|
1504 | |
1287 | void DESTROY (CBOR *self) |
1505 | void DESTROY (CBOR *self) |
1288 | PPCODE: |
1506 | PPCODE: |
1289 | cbor_free (self); |
1507 | cbor_free (self); |
1290 | |
1508 | |
1291 | PROTOTYPES: ENABLE |
1509 | PROTOTYPES: ENABLE |