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.66 by root, Sun Nov 29 21:32:01 2020 UTC vs.
Revision 1.71 by root, Fri Mar 19 17:30:27 2021 UTC

103 103
104// known forced types, also hardcoded in CBOR.pm 104// known forced types, also hardcoded in CBOR.pm
105enum 105enum
106{ 106{
107 AS_CBOR = 0, 107 AS_CBOR = 0,
108 AS_INT = 1,
108 AS_BYTES = 1, 109 AS_BYTES = 2,
109 AS_TEXT = 2, 110 AS_TEXT = 3,
110 AS_FLOAT16 = 3, 111 AS_FLOAT16 = 4,
111 AS_FLOAT32 = 4, 112 AS_FLOAT32 = 5,
112 AS_FLOAT64 = 5, 113 AS_FLOAT64 = 6,
114 AS_MAP = 7,
113 // possibly future enhancements: float, integer 115 // possibly future enhancements: (generic) float, (generic) string
114}; 116};
115 117
116#define F_SHRINK 0x00000001UL 118#define F_SHRINK 0x00000001UL
117#define F_ALLOW_UNKNOWN 0x00000002UL 119#define F_ALLOW_UNKNOWN 0x00000002UL
118#define F_ALLOW_SHARING 0x00000004UL 120#define F_ALLOW_SHARING 0x00000004UL
199#endif 201#endif
200 } 202 }
201} 203}
202 204
203// minimum length of a string to be registered for stringref 205// minimum length of a string to be registered for stringref
204ecb_inline int 206ecb_inline STRLEN
205minimum_string_length (UV idx) 207minimum_string_length (UV idx)
206{ 208{
207 return idx <= 23 ? 3 209 return idx <= 23 ? 3
208 : idx <= 0xffU ? 4 210 : idx <= 0xffU ? 4
209 : idx <= 0xffffU ? 5 211 : idx <= 0xffffU ? 5
237 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 239 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
238 enc->cur = SvPVX (enc->sv) + cur; 240 enc->cur = SvPVX (enc->sv) + cur;
239 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 241 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
240 } 242 }
241} 243}
244
245static void encode_sv (enc_t *enc, SV *sv);
242 246
243ecb_inline void 247ecb_inline void
244encode_ch (enc_t *enc, char ch) 248encode_ch (enc_t *enc, char ch)
245{ 249{
246 need (enc, 1); 250 need (enc, 1);
287 *enc->cur++ = len; 291 *enc->cur++ = len;
288 } 292 }
289} 293}
290 294
291// encodes a perl value into a CBOR integer 295// encodes a perl value into a CBOR integer
292ecb_inline 296ecb_inline void
293encode_int (enc_t *enc, SV *sv) 297encode_int (enc_t *enc, SV *sv)
294{ 298{
295 if (SvIsUV (sv)) 299 if (SvIsUV (sv))
296 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); 300 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
297 else if (SvIVX (sv) >= 0) 301 else if (SvIVX (sv) >= 0)
417 memcpy (enc->cur, &fp, 8); 421 memcpy (enc->cur, &fp, 8);
418 enc->cur += 8; 422 enc->cur += 8;
419} 423}
420 424
421ecb_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
422encode_forced (enc_t *enc, UV type, SV *sv) 464encode_forced (enc_t *enc, UV type, SV *sv)
423{ 465{
424 switch (type) 466 switch (type)
425 { 467 {
426 case AS_CBOR: 468 case AS_CBOR:
448 char *str = SvPVutf8 (sv, len); 490 char *str = SvPVutf8 (sv, len);
449 encode_strref (enc, 1, 1, str, len); 491 encode_strref (enc, 1, 1, str, len);
450 } 492 }
451 break; 493 break;
452 494
495 case AS_INT: encode_int (enc, sv); break;
496
453 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
454 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
455 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
456 500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
457 default: 503 default:
458 croak ("encountered malformed CBOR::XS::Tagged object"); 504 croak ("encountered malformed CBOR::XS::Tagged object");
459 } 505 }
460} 506}
461
462static void encode_sv (enc_t *enc, SV *sv);
463 507
464static void 508static void
465encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
466{ 510{
467 int i, len = av_len (av); 511 int i, len = av_len (av);
545 589
546 HV *stash = SvSTASH (sv); 590 HV *stash = SvSTASH (sv);
547 591
548 if (stash == boolean_stash) 592 if (stash == boolean_stash)
549 { 593 {
550 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 594 encode_bool (enc, SvIV (sv));
551 return; 595 return;
552 } 596 }
553 else if (stash == error_stash) 597 else if (stash == error_stash)
554 { 598 {
555 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 599 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
714 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); 758 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
715 } 759 }
716 else if (SvNOKp (sv)) 760 else if (SvNOKp (sv))
717 encode_nv (enc, sv); 761 encode_nv (enc, sv);
718 else if (SvIOKp (sv)) 762 else if (SvIOKp (sv))
719 encode_int (e,v sv); 763 encode_int (enc, sv);
720 else if (SvROK (sv)) 764 else if (SvROK (sv))
721 encode_rv (enc, SvRV (sv)); 765 encode_rv (enc, SvRV (sv));
722 else if (!SvOK (sv)) 766 else if (!SvOK (sv))
723 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 767 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
724 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 768 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
943 987
944 WANT (len); 988 WANT (len);
945 dec->cur += len; 989 dec->cur += len;
946 990
947 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
948 if (!is_utf8_string (key, len)) 992 if (!is_utf8_string ((U8 *)key, len))
949 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
950 994
951 hv_store (hv, key, -len, decode_sv (dec), 0); 995 hv_store (hv, key, -len, decode_sv (dec), 0);
952 996
953 return; 997 return;
1525 1569
1526 break; 1570 break;
1527 1571
1528 case MAJOR_MAP >> MAJOR_SHIFT: 1572 case MAJOR_MAP >> MAJOR_SHIFT:
1529 len <<= 1; 1573 len <<= 1;
1574 /* FALLTHROUGH */
1530 case MAJOR_ARRAY >> MAJOR_SHIFT: 1575 case MAJOR_ARRAY >> MAJOR_SHIFT:
1531 if (len) 1576 if (len)
1532 { 1577 {
1533 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1578 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1534 count = len + 1; 1579 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines