… | |
… | |
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, |
… | |
… | |
109 | AS_BYTES = 2, |
112 | AS_BYTES = 2, |
110 | AS_TEXT = 3, |
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); |
… | |
… | |
423 | encode_bool (enc_t *enc, int istrue) |
429 | encode_bool (enc_t *enc, int istrue) |
424 | { |
430 | { |
425 | encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
431 | encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
426 | } |
432 | } |
427 | |
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 | |
428 | ecb_inline void |
466 | ecb_inline void |
429 | encode_forced (enc_t *enc, UV type, SV *sv) |
467 | encode_forced (enc_t *enc, UV type, SV *sv) |
430 | { |
468 | { |
431 | switch (type) |
469 | switch (type) |
432 | { |
470 | { |
… | |
… | |
461 | |
499 | |
462 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
500 | case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; |
463 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
501 | case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; |
464 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
502 | case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; |
465 | |
503 | |
|
|
504 | case AS_MAP: encode_array_as_map (enc, sv); break; |
|
|
505 | |
466 | default: |
506 | default: |
467 | croak ("encountered malformed CBOR::XS::Tagged object"); |
507 | croak ("encountered malformed CBOR::XS::Tagged object"); |
468 | } |
508 | } |
469 | } |
509 | } |
470 | |
|
|
471 | static void encode_sv (enc_t *enc, SV *sv); |
|
|
472 | |
510 | |
473 | static void |
511 | static void |
474 | encode_av (enc_t *enc, AV *av) |
512 | encode_av (enc_t *enc, AV *av) |
475 | { |
513 | { |
476 | int i, len = av_len (av); |
514 | int i, len = av_len (av); |
… | |
… | |
952 | |
990 | |
953 | WANT (len); |
991 | WANT (len); |
954 | dec->cur += len; |
992 | dec->cur += len; |
955 | |
993 | |
956 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
994 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
957 | if (!is_utf8_string (key, len)) |
995 | if (!cbor_is_utf8_string ((U8 *)key, len)) |
958 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
996 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
959 | |
997 | |
960 | hv_store (hv, key, -len, decode_sv (dec), 0); |
998 | hv_store (hv, key, -len, decode_sv (dec), 0); |
961 | |
999 | |
962 | return; |
1000 | return; |
… | |
… | |
1091 | } |
1129 | } |
1092 | |
1130 | |
1093 | if (utf8) |
1131 | if (utf8) |
1094 | { |
1132 | { |
1095 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
1133 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
1096 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
1134 | if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv))) |
1097 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
1135 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
1098 | |
1136 | |
1099 | SvUTF8_on (sv); |
1137 | SvUTF8_on (sv); |
1100 | } |
1138 | } |
1101 | |
1139 | |
… | |
… | |
1534 | |
1572 | |
1535 | break; |
1573 | break; |
1536 | |
1574 | |
1537 | case MAJOR_MAP >> MAJOR_SHIFT: |
1575 | case MAJOR_MAP >> MAJOR_SHIFT: |
1538 | len <<= 1; |
1576 | len <<= 1; |
|
|
1577 | /* FALLTHROUGH */ |
1539 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1578 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1540 | if (len) |
1579 | if (len) |
1541 | { |
1580 | { |
1542 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1581 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
1543 | count = len + 1; |
1582 | count = len + 1; |