… | |
… | |
28 | #endif |
28 | #endif |
29 | #ifndef SvREFCNT_dec_NN |
29 | #ifndef SvREFCNT_dec_NN |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
30 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
31 | #endif |
31 | #endif |
32 | |
32 | |
|
|
33 | // perl's is_utf8_string interprets len=0 as "calculate len", but we want it to mean 0 |
|
|
34 | #define cbor_is_utf8_string(str,len) (!(len) || is_utf8_string ((str), (len))) |
|
|
35 | |
33 | // known major and minor types |
36 | // known major and minor types |
34 | enum cbor_type |
37 | enum cbor_type |
35 | { |
38 | { |
36 | MAJOR_SHIFT = 5, |
39 | MAJOR_SHIFT = 5, |
37 | MINOR_MASK = 0x1f, |
40 | MINOR_MASK = 0x1f, |
… | |
… | |
103 | |
106 | |
104 | // known forced types, also hardcoded in CBOR.pm |
107 | // known forced types, also hardcoded in CBOR.pm |
105 | enum |
108 | enum |
106 | { |
109 | { |
107 | AS_CBOR = 0, |
110 | AS_CBOR = 0, |
108 | AS_BYTES = 1, |
|
|
109 | AS_TEXT = 2, |
|
|
110 | AS_INT = 3, |
111 | AS_INT = 1, |
|
|
112 | AS_BYTES = 2, |
|
|
113 | AS_TEXT = 3, |
111 | AS_FLOAT16 = 4, |
114 | AS_FLOAT16 = 4, |
112 | AS_FLOAT32 = 5, |
115 | AS_FLOAT32 = 5, |
113 | AS_FLOAT64 = 6, |
116 | AS_FLOAT64 = 6, |
|
|
117 | AS_MAP = 7, |
114 | // possibly future enhancements: (generic) float, (generic) string |
118 | // possibly future enhancements: (generic) float, (generic) string |
115 | }; |
119 | }; |
116 | |
120 | |
117 | #define F_SHRINK 0x00000001UL |
121 | #define F_SHRINK 0x00000001UL |
118 | #define F_ALLOW_UNKNOWN 0x00000002UL |
122 | #define F_ALLOW_UNKNOWN 0x00000002UL |
… | |
… | |
200 | #endif |
204 | #endif |
201 | } |
205 | } |
202 | } |
206 | } |
203 | |
207 | |
204 | // minimum length of a string to be registered for stringref |
208 | // minimum length of a string to be registered for stringref |
205 | ecb_inline int |
209 | ecb_inline STRLEN |
206 | minimum_string_length (UV idx) |
210 | minimum_string_length (UV idx) |
207 | { |
211 | { |
208 | return idx <= 23 ? 3 |
212 | return idx <= 23 ? 3 |
209 | : idx <= 0xffU ? 4 |
213 | : idx <= 0xffU ? 4 |
210 | : idx <= 0xffffU ? 5 |
214 | : idx <= 0xffffU ? 5 |
… | |
… | |
238 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
242 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
239 | enc->cur = SvPVX (enc->sv) + cur; |
243 | enc->cur = SvPVX (enc->sv) + cur; |
240 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
244 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
241 | } |
245 | } |
242 | } |
246 | } |
|
|
247 | |
|
|
248 | static void encode_sv (enc_t *enc, SV *sv); |
243 | |
249 | |
244 | ecb_inline void |
250 | ecb_inline void |
245 | encode_ch (enc_t *enc, char ch) |
251 | encode_ch (enc_t *enc, char ch) |
246 | { |
252 | { |
247 | need (enc, 1); |
253 | need (enc, 1); |
… | |
… | |
418 | memcpy (enc->cur, &fp, 8); |
424 | memcpy (enc->cur, &fp, 8); |
419 | enc->cur += 8; |
425 | enc->cur += 8; |
420 | } |
426 | } |
421 | |
427 | |
422 | ecb_inline void |
428 | ecb_inline void |
|
|
429 | encode_bool (enc_t *enc, int istrue) |
|
|
430 | { |
|
|
431 | encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
432 | } |
|
|
433 | |
|
|
434 | // encodes an arrayref containing key-value pairs as CBOR map |
|
|
435 | ecb_inline void |
|
|
436 | encode_array_as_map (enc_t *enc, SV *sv) |
|
|
437 | { |
|
|
438 | if (enc->depth >= enc->cbor.max_depth) |
|
|
439 | croak (ERR_NESTING_EXCEEDED); |
|
|
440 | |
|
|
441 | ++enc->depth; |
|
|
442 | |
|
|
443 | // as_map does error checking for us, but we re-check in case |
|
|
444 | // things have changed. |
|
|
445 | |
|
|
446 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
447 | croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)"); |
|
|
448 | |
|
|
449 | AV *av = (AV *)SvRV (sv); |
|
|
450 | int i, len = av_len (av); |
|
|
451 | |
|
|
452 | if (!(len & 1)) |
|
|
453 | croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)"); |
|
|
454 | |
|
|
455 | encode_uint (enc, MAJOR_MAP, (len + 1) >> 1); |
|
|
456 | |
|
|
457 | for (i = 0; i <= len; ++i) |
|
|
458 | { |
|
|
459 | SV **svp = av_fetch (av, i, 0); |
|
|
460 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
|
|
461 | } |
|
|
462 | |
|
|
463 | --enc->depth; |
|
|
464 | } |
|
|
465 | |
|
|
466 | ecb_inline void |
423 | encode_forced (enc_t *enc, UV type, SV *sv) |
467 | encode_forced (enc_t *enc, UV type, SV *sv) |
424 | { |
468 | { |
425 | switch (type) |
469 | switch (type) |
426 | { |
470 | { |
427 | case AS_CBOR: |
471 | case AS_CBOR: |
… | |
… | |
449 | char *str = SvPVutf8 (sv, len); |
493 | char *str = SvPVutf8 (sv, len); |
450 | encode_strref (enc, 1, 1, str, len); |
494 | encode_strref (enc, 1, 1, str, len); |
451 | } |
495 | } |
452 | break; |
496 | break; |
453 | |
497 | |
454 | case AS_INT: encode_int (enc, sv); break; |
498 | case AS_INT: encode_int (enc, sv); break; |
|
|
499 | |
455 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
500 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
456 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
501 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
457 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
502 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
458 | |
503 | |
|
|
504 | case AS_MAP: encode_array_as_map (enc, sv); break; |
|
|
505 | |
459 | default: |
506 | default: |
460 | croak ("encountered malformed CBOR::XS::Tagged object"); |
507 | croak ("encountered malformed CBOR::XS::Tagged object"); |
461 | } |
508 | } |
462 | } |
509 | } |
463 | |
|
|
464 | static void encode_sv (enc_t *enc, SV *sv); |
|
|
465 | |
510 | |
466 | static void |
511 | static void |
467 | encode_av (enc_t *enc, AV *av) |
512 | encode_av (enc_t *enc, AV *av) |
468 | { |
513 | { |
469 | int i, len = av_len (av); |
514 | int i, len = av_len (av); |
… | |
… | |
547 | |
592 | |
548 | HV *stash = SvSTASH (sv); |
593 | HV *stash = SvSTASH (sv); |
549 | |
594 | |
550 | if (stash == boolean_stash) |
595 | if (stash == boolean_stash) |
551 | { |
596 | { |
552 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
597 | encode_bool (enc, SvIV (sv)); |
553 | return; |
598 | return; |
554 | } |
599 | } |
555 | else if (stash == error_stash) |
600 | else if (stash == error_stash) |
556 | { |
601 | { |
557 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
602 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
… | |
… | |
945 | |
990 | |
946 | WANT (len); |
991 | WANT (len); |
947 | dec->cur += len; |
992 | dec->cur += len; |
948 | |
993 | |
949 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
994 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
950 | if (!is_utf8_string (key, len)) |
995 | if (!cbor_is_utf8_string ((U8 *)key, len)) |
951 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
996 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
952 | |
997 | |
953 | hv_store (hv, key, -len, decode_sv (dec), 0); |
998 | hv_store (hv, key, -len, decode_sv (dec), 0); |
954 | |
999 | |
955 | return; |
1000 | return; |
… | |
… | |
1084 | } |
1129 | } |
1085 | |
1130 | |
1086 | if (utf8) |
1131 | if (utf8) |
1087 | { |
1132 | { |
1088 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
1133 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
1089 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
1134 | if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv))) |
1090 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
1135 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
1091 | |
1136 | |
1092 | SvUTF8_on (sv); |
1137 | SvUTF8_on (sv); |
1093 | } |
1138 | } |
1094 | |
1139 | |
… | |
… | |
1178 | UV idx = decode_uint (dec); |
1223 | UV idx = decode_uint (dec); |
1179 | |
1224 | |
1180 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1225 | if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) |
1181 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1226 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
1182 | |
1227 | |
1183 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
1228 | sv = newSVsv (AvARRAY (dec->shareable)[idx]); |
1184 | |
1229 | |
1185 | if (sv == &PL_sv_undef) |
1230 | if (sv == &PL_sv_undef) |
1186 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
1231 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
1187 | } |
1232 | } |
1188 | break; |
1233 | break; |
… | |
… | |
1428 | SvREFCNT_dec_NN (sv); |
1473 | SvREFCNT_dec_NN (sv); |
1429 | |
1474 | |
1430 | if (dec.err_sv) |
1475 | if (dec.err_sv) |
1431 | sv_2mortal (dec.err_sv); |
1476 | sv_2mortal (dec.err_sv); |
1432 | |
1477 | |
1433 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1478 | croak ("%s, at offset %ld (octet 0x%02x)", dec.err, (long)(dec.cur - (U8 *)data), (int)(uint8_t)*dec.cur); |
1434 | } |
1479 | } |
1435 | |
1480 | |
1436 | sv = sv_2mortal (sv); |
1481 | sv = sv_2mortal (sv); |
1437 | |
1482 | |
1438 | return sv; |
1483 | return sv; |
… | |
… | |
1527 | |
1572 | |
1528 | break; |
1573 | break; |
1529 | |
1574 | |
1530 | case MAJOR_MAP >> MAJOR_SHIFT: |
1575 | case MAJOR_MAP >> MAJOR_SHIFT: |
1531 | len <<= 1; |
1576 | len <<= 1; |
|
|
1577 | /* FALLTHROUGH */ |
1532 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1578 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1533 | if (len) |
1579 | if (len) |
1534 | { |
1580 | { |
1535 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1581 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1536 | count = len + 1; |
1582 | count = len + 1; |