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.72 by root, Thu Oct 21 01:14:58 2021 UTC

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
34enum cbor_type 37enum 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
205ecb_inline int 209ecb_inline STRLEN
206minimum_string_length (UV idx) 210minimum_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
248static void encode_sv (enc_t *enc, SV *sv);
243 249
244ecb_inline void 250ecb_inline void
245encode_ch (enc_t *enc, char ch) 251encode_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
422ecb_inline void 428ecb_inline void
429encode_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
435ecb_inline void
436encode_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
466ecb_inline void
423encode_forced (enc_t *enc, UV type, SV *sv) 467encode_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
464static void encode_sv (enc_t *enc, SV *sv);
465 510
466static void 511static void
467encode_av (enc_t *enc, AV *av) 512encode_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);
940 } 985 }
941 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 986 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
942 { 987 {
943 STRLEN len = decode_uint (dec); 988 STRLEN len = decode_uint (dec);
944 char *key = (char *)dec->cur; 989 char *key = (char *)dec->cur;
990 printf ("len %d\n", len);//D
945 991
946 WANT (len); 992 WANT (len);
947 dec->cur += len; 993 dec->cur += len;
948 994
949 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 995 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
950 if (!is_utf8_string (key, len)) 996 if (!cbor_is_utf8_string ((U8 *)key, len))
951 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 997 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
952 998
953 hv_store (hv, key, -len, decode_sv (dec), 0); 999 hv_store (hv, key, -len, decode_sv (dec), 0);
954 1000
955 return; 1001 return;
1084 } 1130 }
1085 1131
1086 if (utf8) 1132 if (utf8)
1087 { 1133 {
1088 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1134 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1089 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1135 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
1090 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1136 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
1091 1137
1092 SvUTF8_on (sv); 1138 SvUTF8_on (sv);
1093 } 1139 }
1094 1140
1527 1573
1528 break; 1574 break;
1529 1575
1530 case MAJOR_MAP >> MAJOR_SHIFT: 1576 case MAJOR_MAP >> MAJOR_SHIFT:
1531 len <<= 1; 1577 len <<= 1;
1578 /* FALLTHROUGH */
1532 case MAJOR_ARRAY >> MAJOR_SHIFT: 1579 case MAJOR_ARRAY >> MAJOR_SHIFT:
1533 if (len) 1580 if (len)
1534 { 1581 {
1535 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1582 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1536 count = len + 1; 1583 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines