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.68 by root, Mon Nov 30 18:30:29 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);
418 memcpy (enc->cur, &fp, 8); 421 memcpy (enc->cur, &fp, 8);
419 enc->cur += 8; 422 enc->cur += 8;
420} 423}
421 424
422ecb_inline void 425ecb_inline void
426encode_bool (enc_t *enc, int istrue)
427{
428 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
429}
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
463ecb_inline void
423encode_forced (enc_t *enc, UV type, SV *sv) 464encode_forced (enc_t *enc, UV type, SV *sv)
424{ 465{
425 switch (type) 466 switch (type)
426 { 467 {
427 case AS_CBOR: 468 case AS_CBOR:
449 char *str = SvPVutf8 (sv, len); 490 char *str = SvPVutf8 (sv, len);
450 encode_strref (enc, 1, 1, str, len); 491 encode_strref (enc, 1, 1, str, len);
451 } 492 }
452 break; 493 break;
453 494
454 case AS_INT: encode_int (enc, sv); break; 495 case AS_INT: encode_int (enc, sv); break;
496
455 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
456 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
457 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
458 500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
459 default: 503 default:
460 croak ("encountered malformed CBOR::XS::Tagged object"); 504 croak ("encountered malformed CBOR::XS::Tagged object");
461 } 505 }
462} 506}
463
464static void encode_sv (enc_t *enc, SV *sv);
465 507
466static void 508static void
467encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
468{ 510{
469 int i, len = av_len (av); 511 int i, len = av_len (av);
547 589
548 HV *stash = SvSTASH (sv); 590 HV *stash = SvSTASH (sv);
549 591
550 if (stash == boolean_stash) 592 if (stash == boolean_stash)
551 { 593 {
552 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 594 encode_bool (enc, SvIV (sv));
553 return; 595 return;
554 } 596 }
555 else if (stash == error_stash) 597 else if (stash == error_stash)
556 { 598 {
557 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 599 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
945 987
946 WANT (len); 988 WANT (len);
947 dec->cur += len; 989 dec->cur += len;
948 990
949 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
950 if (!is_utf8_string (key, len)) 992 if (!is_utf8_string ((U8 *)key, len))
951 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
952 994
953 hv_store (hv, key, -len, decode_sv (dec), 0); 995 hv_store (hv, key, -len, decode_sv (dec), 0);
954 996
955 return; 997 return;
1527 1569
1528 break; 1570 break;
1529 1571
1530 case MAJOR_MAP >> MAJOR_SHIFT: 1572 case MAJOR_MAP >> MAJOR_SHIFT:
1531 len <<= 1; 1573 len <<= 1;
1574 /* FALLTHROUGH */
1532 case MAJOR_ARRAY >> MAJOR_SHIFT: 1575 case MAJOR_ARRAY >> MAJOR_SHIFT:
1533 if (len) 1576 if (len)
1534 { 1577 {
1535 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1578 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1536 count = len + 1; 1579 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines