ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.69 by root, Mon Nov 30 20:38:25 2020 UTC vs.
Revision 1.71 by root, Fri Mar 19 17:30:27 2021 UTC

109 AS_BYTES = 2, 109 AS_BYTES = 2,
110 AS_TEXT = 3, 110 AS_TEXT = 3,
111 AS_FLOAT16 = 4, 111 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5, 112 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6, 113 AS_FLOAT64 = 6,
114 AS_MAP = 7,
114 // possibly future enhancements: (generic) float, (generic) string 115 // possibly future enhancements: (generic) float, (generic) string
115}; 116};
116 117
117#define F_SHRINK 0x00000001UL 118#define F_SHRINK 0x00000001UL
118#define F_ALLOW_UNKNOWN 0x00000002UL 119#define F_ALLOW_UNKNOWN 0x00000002UL
200#endif 201#endif
201 } 202 }
202} 203}
203 204
204// minimum length of a string to be registered for stringref 205// minimum length of a string to be registered for stringref
205ecb_inline int 206ecb_inline STRLEN
206minimum_string_length (UV idx) 207minimum_string_length (UV idx)
207{ 208{
208 return idx <= 23 ? 3 209 return idx <= 23 ? 3
209 : idx <= 0xffU ? 4 210 : idx <= 0xffU ? 4
210 : idx <= 0xffffU ? 5 211 : idx <= 0xffffU ? 5
238 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 239 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
239 enc->cur = SvPVX (enc->sv) + cur; 240 enc->cur = SvPVX (enc->sv) + cur;
240 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 241 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
241 } 242 }
242} 243}
244
245static void encode_sv (enc_t *enc, SV *sv);
243 246
244ecb_inline void 247ecb_inline void
245encode_ch (enc_t *enc, char ch) 248encode_ch (enc_t *enc, char ch)
246{ 249{
247 need (enc, 1); 250 need (enc, 1);
423encode_bool (enc_t *enc, int istrue) 426encode_bool (enc_t *enc, int istrue)
424{ 427{
425 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 428 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
426} 429}
427 430
431// encodes an arrayref containing key-value pairs as CBOR map
432ecb_inline void
433encode_array_as_map (enc_t *enc, SV *sv)
434{
435 if (enc->depth >= enc->cbor.max_depth)
436 croak (ERR_NESTING_EXCEEDED);
437
438 ++enc->depth;
439
440 // as_map does error checking for us, but we re-check in case
441 // things have changed.
442
443 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
444 croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)");
445
446 AV *av = (AV *)SvRV (sv);
447 int i, len = av_len (av);
448
449 if (!(len & 1))
450 croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)");
451
452 encode_uint (enc, MAJOR_MAP, (len + 1) >> 1);
453
454 for (i = 0; i <= len; ++i)
455 {
456 SV **svp = av_fetch (av, i, 0);
457 encode_sv (enc, svp ? *svp : &PL_sv_undef);
458 }
459
460 --enc->depth;
461}
462
428ecb_inline void 463ecb_inline void
429encode_forced (enc_t *enc, UV type, SV *sv) 464encode_forced (enc_t *enc, UV type, SV *sv)
430{ 465{
431 switch (type) 466 switch (type)
432 { 467 {
461 496
462 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
463 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
464 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
465 500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
466 default: 503 default:
467 croak ("encountered malformed CBOR::XS::Tagged object"); 504 croak ("encountered malformed CBOR::XS::Tagged object");
468 } 505 }
469} 506}
470
471static void encode_sv (enc_t *enc, SV *sv);
472 507
473static void 508static void
474encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
475{ 510{
476 int i, len = av_len (av); 511 int i, len = av_len (av);
952 987
953 WANT (len); 988 WANT (len);
954 dec->cur += len; 989 dec->cur += len;
955 990
956 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
957 if (!is_utf8_string (key, len)) 992 if (!is_utf8_string ((U8 *)key, len))
958 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
959 994
960 hv_store (hv, key, -len, decode_sv (dec), 0); 995 hv_store (hv, key, -len, decode_sv (dec), 0);
961 996
962 return; 997 return;
1534 1569
1535 break; 1570 break;
1536 1571
1537 case MAJOR_MAP >> MAJOR_SHIFT: 1572 case MAJOR_MAP >> MAJOR_SHIFT:
1538 len <<= 1; 1573 len <<= 1;
1574 /* FALLTHROUGH */
1539 case MAJOR_ARRAY >> MAJOR_SHIFT: 1575 case MAJOR_ARRAY >> MAJOR_SHIFT:
1540 if (len) 1576 if (len)
1541 { 1577 {
1542 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1578 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1543 count = len + 1; 1579 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines