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.75 by root, Thu Sep 7 23:52:24 2023 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,
103 106
104// known forced types, also hardcoded in CBOR.pm 107// known forced types, also hardcoded in CBOR.pm
105enum 108enum
106{ 109{
107 AS_CBOR = 0, 110 AS_CBOR = 0,
111 AS_INT = 1,
108 AS_BYTES = 1, 112 AS_BYTES = 2,
109 AS_TEXT = 2, 113 AS_TEXT = 3,
110 AS_FLOAT16 = 3, 114 AS_FLOAT16 = 4,
111 AS_FLOAT32 = 4, 115 AS_FLOAT32 = 5,
112 AS_FLOAT64 = 5, 116 AS_FLOAT64 = 6,
117 AS_MAP = 7,
113 // possibly future enhancements: float, integer 118 // possibly future enhancements: (generic) float, (generic) string
114}; 119};
115 120
116#define F_SHRINK 0x00000001UL 121#define F_SHRINK 0x00000001UL
117#define F_ALLOW_UNKNOWN 0x00000002UL 122#define F_ALLOW_UNKNOWN 0x00000002UL
118#define F_ALLOW_SHARING 0x00000004UL 123#define F_ALLOW_SHARING 0x00000004UL
199#endif 204#endif
200 } 205 }
201} 206}
202 207
203// minimum length of a string to be registered for stringref 208// minimum length of a string to be registered for stringref
204ecb_inline int 209ecb_inline STRLEN
205minimum_string_length (UV idx) 210minimum_string_length (UV idx)
206{ 211{
207 return idx <= 23 ? 3 212 return idx <= 23 ? 3
208 : idx <= 0xffU ? 4 213 : idx <= 0xffU ? 4
209 : idx <= 0xffffU ? 5 214 : idx <= 0xffffU ? 5
237 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 242 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
238 enc->cur = SvPVX (enc->sv) + cur; 243 enc->cur = SvPVX (enc->sv) + cur;
239 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 244 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
240 } 245 }
241} 246}
247
248static void encode_sv (enc_t *enc, SV *sv);
242 249
243ecb_inline void 250ecb_inline void
244encode_ch (enc_t *enc, char ch) 251encode_ch (enc_t *enc, char ch)
245{ 252{
246 need (enc, 1); 253 need (enc, 1);
287 *enc->cur++ = len; 294 *enc->cur++ = len;
288 } 295 }
289} 296}
290 297
291// encodes a perl value into a CBOR integer 298// encodes a perl value into a CBOR integer
292ecb_inline 299ecb_inline void
293encode_int (enc_t *enc, SV *sv) 300encode_int (enc_t *enc, SV *sv)
294{ 301{
295 if (SvIsUV (sv)) 302 if (SvIsUV (sv))
296 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); 303 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
297 else if (SvIVX (sv) >= 0) 304 else if (SvIVX (sv) >= 0)
417 memcpy (enc->cur, &fp, 8); 424 memcpy (enc->cur, &fp, 8);
418 enc->cur += 8; 425 enc->cur += 8;
419} 426}
420 427
421ecb_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
422encode_forced (enc_t *enc, UV type, SV *sv) 467encode_forced (enc_t *enc, UV type, SV *sv)
423{ 468{
424 switch (type) 469 switch (type)
425 { 470 {
426 case AS_CBOR: 471 case AS_CBOR:
448 char *str = SvPVutf8 (sv, len); 493 char *str = SvPVutf8 (sv, len);
449 encode_strref (enc, 1, 1, str, len); 494 encode_strref (enc, 1, 1, str, len);
450 } 495 }
451 break; 496 break;
452 497
498 case AS_INT: encode_int (enc, sv); break;
499
453 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 500 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
454 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 501 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
455 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 502 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
456 503
504 case AS_MAP: encode_array_as_map (enc, sv); break;
505
457 default: 506 default:
458 croak ("encountered malformed CBOR::XS::Tagged object"); 507 croak ("encountered malformed CBOR::XS::Tagged object");
459 } 508 }
460} 509}
461
462static void encode_sv (enc_t *enc, SV *sv);
463 510
464static void 511static void
465encode_av (enc_t *enc, AV *av) 512encode_av (enc_t *enc, AV *av)
466{ 513{
467 int i, len = av_len (av); 514 int i, len = av_len (av);
545 592
546 HV *stash = SvSTASH (sv); 593 HV *stash = SvSTASH (sv);
547 594
548 if (stash == boolean_stash) 595 if (stash == boolean_stash)
549 { 596 {
550 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 597 encode_bool (enc, SvIV (sv));
551 return; 598 return;
552 } 599 }
553 else if (stash == error_stash) 600 else if (stash == error_stash)
554 { 601 {
555 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 602 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
714 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); 761 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
715 } 762 }
716 else if (SvNOKp (sv)) 763 else if (SvNOKp (sv))
717 encode_nv (enc, sv); 764 encode_nv (enc, sv);
718 else if (SvIOKp (sv)) 765 else if (SvIOKp (sv))
719 encode_int (e,v sv); 766 encode_int (enc, sv);
720 else if (SvROK (sv)) 767 else if (SvROK (sv))
721 encode_rv (enc, SvRV (sv)); 768 encode_rv (enc, SvRV (sv));
722 else if (!SvOK (sv)) 769 else if (!SvOK (sv))
723 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 770 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
724 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 771 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
943 990
944 WANT (len); 991 WANT (len);
945 dec->cur += len; 992 dec->cur += len;
946 993
947 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 994 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
948 if (!is_utf8_string (key, len)) 995 if (!cbor_is_utf8_string ((U8 *)key, len))
949 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 996 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
950 997
951 hv_store (hv, key, -len, decode_sv (dec), 0); 998 hv_store (hv, key, -len, decode_sv (dec), 0);
952 999
953 return; 1000 return;
1082 } 1129 }
1083 1130
1084 if (utf8) 1131 if (utf8)
1085 { 1132 {
1086 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1133 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1087 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1134 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
1088 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1135 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
1089 1136
1090 SvUTF8_on (sv); 1137 SvUTF8_on (sv);
1091 } 1138 }
1092 1139
1176 UV idx = decode_uint (dec); 1223 UV idx = decode_uint (dec);
1177 1224
1178 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1225 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1179 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1226 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1180 1227
1181 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1228 sv = newRV_inc (AvARRAY (dec->shareable)[idx]);
1182 1229
1183 if (sv == &PL_sv_undef) 1230 if (sv == &PL_sv_undef)
1184 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); 1231 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
1185 } 1232 }
1186 break; 1233 break;
1426 SvREFCNT_dec_NN (sv); 1473 SvREFCNT_dec_NN (sv);
1427 1474
1428 if (dec.err_sv) 1475 if (dec.err_sv)
1429 sv_2mortal (dec.err_sv); 1476 sv_2mortal (dec.err_sv);
1430 1477
1431 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1478 croak ("%s, at offset %ld (octet 0x%02x)", dec.err, (long)(dec.cur - (U8 *)data), (int)(uint8_t)*dec.cur);
1432 } 1479 }
1433 1480
1434 sv = sv_2mortal (sv); 1481 sv = sv_2mortal (sv);
1435 1482
1436 return sv; 1483 return sv;
1525 1572
1526 break; 1573 break;
1527 1574
1528 case MAJOR_MAP >> MAJOR_SHIFT: 1575 case MAJOR_MAP >> MAJOR_SHIFT:
1529 len <<= 1; 1576 len <<= 1;
1577 /* FALLTHROUGH */
1530 case MAJOR_ARRAY >> MAJOR_SHIFT: 1578 case MAJOR_ARRAY >> MAJOR_SHIFT:
1531 if (len) 1579 if (len)
1532 { 1580 {
1533 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1581 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1534 count = len + 1; 1582 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines