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.64 by root, Tue Jun 27 02:03:24 2017 UTC vs.
Revision 1.73 by root, Sat Oct 23 03:00:31 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,
97 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 100 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
98 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 101 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
99 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 102 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
100 103
101 CBOR_TAG_MAGIC = 55799, // self-describe cbor 104 CBOR_TAG_MAGIC = 55799, // self-describe cbor
105};
106
107// known forced types, also hardcoded in CBOR.pm
108enum
109{
110 AS_CBOR = 0,
111 AS_INT = 1,
112 AS_BYTES = 2,
113 AS_TEXT = 3,
114 AS_FLOAT16 = 4,
115 AS_FLOAT32 = 5,
116 AS_FLOAT64 = 6,
117 AS_MAP = 7,
118 // possibly future enhancements: (generic) float, (generic) string
102}; 119};
103 120
104#define F_SHRINK 0x00000001UL 121#define F_SHRINK 0x00000001UL
105#define F_ALLOW_UNKNOWN 0x00000002UL 122#define F_ALLOW_UNKNOWN 0x00000002UL
106#define F_ALLOW_SHARING 0x00000004UL 123#define F_ALLOW_SHARING 0x00000004UL
187#endif 204#endif
188 } 205 }
189} 206}
190 207
191// minimum length of a string to be registered for stringref 208// minimum length of a string to be registered for stringref
192ecb_inline int 209ecb_inline STRLEN
193minimum_string_length (UV idx) 210minimum_string_length (UV idx)
194{ 211{
195 return idx <= 23 ? 3 212 return idx <= 23 ? 3
196 : idx <= 0xffU ? 4 213 : idx <= 0xffU ? 4
197 : idx <= 0xffffU ? 5 214 : idx <= 0xffffU ? 5
226 enc->cur = SvPVX (enc->sv) + cur; 243 enc->cur = SvPVX (enc->sv) + cur;
227 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 244 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
228 } 245 }
229} 246}
230 247
248static void encode_sv (enc_t *enc, SV *sv);
249
231ecb_inline void 250ecb_inline void
232encode_ch (enc_t *enc, char ch) 251encode_ch (enc_t *enc, char ch)
233{ 252{
234 need (enc, 1); 253 need (enc, 1);
235 *enc->cur++ = ch; 254 *enc->cur++ = ch;
236} 255}
237 256
257// used for tags, intregers, element counts and so on
238static void 258static void
239encode_uint (enc_t *enc, int major, UV len) 259encode_uint (enc_t *enc, int major, UV len)
240{ 260{
241 need (enc, 9); 261 need (enc, 9);
242 262
273 *enc->cur++ = len >> 8; 293 *enc->cur++ = len >> 8;
274 *enc->cur++ = len; 294 *enc->cur++ = len;
275 } 295 }
276} 296}
277 297
298// encodes a perl value into a CBOR integer
299ecb_inline void
300encode_int (enc_t *enc, SV *sv)
301{
302 if (SvIsUV (sv))
303 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
304 else if (SvIVX (sv) >= 0)
305 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
306 else
307 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
308}
309
278ecb_inline void 310ecb_inline void
279encode_tag (enc_t *enc, UV tag) 311encode_tag (enc_t *enc, UV tag)
280{ 312{
281 encode_uint (enc, MAJOR_TAG, tag); 313 encode_uint (enc, MAJOR_TAG, tag);
282} 314}
343 } 375 }
344 376
345 encode_str (enc, upgrade_utf8, utf8, str, len); 377 encode_str (enc, upgrade_utf8, utf8, str, len);
346} 378}
347 379
348static void encode_sv (enc_t *enc, SV *sv); 380ecb_inline void
381encode_float16 (enc_t *enc, NV nv)
382{
383 need (enc, 1+2);
384
385 *enc->cur++ = MAJOR_MISC | MISC_FLOAT16;
386
387 uint16_t fp = ecb_float_to_binary16 (nv);
388
389 if (!ecb_big_endian ())
390 fp = ecb_bswap16 (fp);
391
392 memcpy (enc->cur, &fp, 2);
393 enc->cur += 2;
394}
395
396ecb_inline void
397encode_float32 (enc_t *enc, NV nv)
398{
399 need (enc, 1+4);
400
401 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
402
403 uint32_t fp = ecb_float_to_binary32 (nv);
404
405 if (!ecb_big_endian ())
406 fp = ecb_bswap32 (fp);
407
408 memcpy (enc->cur, &fp, 4);
409 enc->cur += 4;
410}
411
412ecb_inline void
413encode_float64 (enc_t *enc, NV nv)
414{
415 need (enc, 1+8);
416
417 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
418
419 uint64_t fp = ecb_double_to_binary64 (nv);
420
421 if (!ecb_big_endian ())
422 fp = ecb_bswap64 (fp);
423
424 memcpy (enc->cur, &fp, 8);
425 enc->cur += 8;
426}
427
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
467encode_forced (enc_t *enc, UV type, SV *sv)
468{
469 switch (type)
470 {
471 case AS_CBOR:
472 {
473 STRLEN len;
474 char *str = SvPVbyte (sv, len);
475
476 need (enc, len);
477 memcpy (enc->cur, str, len);
478 enc->cur += len;
479 }
480 break;
481
482 case AS_BYTES:
483 {
484 STRLEN len;
485 char *str = SvPVbyte (sv, len);
486 encode_strref (enc, 0, 0, str, len);
487 }
488 break;
489
490 case AS_TEXT:
491 {
492 STRLEN len;
493 char *str = SvPVutf8 (sv, len);
494 encode_strref (enc, 1, 1, str, len);
495 }
496 break;
497
498 case AS_INT: encode_int (enc, sv); break;
499
500 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
501 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
502 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
503
504 case AS_MAP: encode_array_as_map (enc, sv); break;
505
506 default:
507 croak ("encountered malformed CBOR::XS::Tagged object");
508 }
509}
349 510
350static void 511static void
351encode_av (enc_t *enc, AV *av) 512encode_av (enc_t *enc, AV *av)
352{ 513{
353 int i, len = av_len (av); 514 int i, len = av_len (av);
431 592
432 HV *stash = SvSTASH (sv); 593 HV *stash = SvSTASH (sv);
433 594
434 if (stash == boolean_stash) 595 if (stash == boolean_stash)
435 { 596 {
436 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 597 encode_bool (enc, SvIV (sv));
437 return; 598 return;
438 } 599 }
439 else if (stash == error_stash) 600 else if (stash == error_stash)
440 { 601 {
441 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 602 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
444 else if (stash == tagged_stash) 605 else if (stash == tagged_stash)
445 { 606 {
446 if (svt != SVt_PVAV) 607 if (svt != SVt_PVAV)
447 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 608 croak ("encountered CBOR::XS::Tagged object that isn't an array");
448 609
610 switch (av_len ((AV *)sv))
611 {
612 case 2-1:
613 // actually a tagged value
449 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); 614 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
450 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 615 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
616 break;
617
618 case 3-1:
619 // a forced type [value, type, undef]
620 encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1));
621 break;
622
623 default:
624 croak ("encountered malformed CBOR::XS::Tagged object");
625 }
451 626
452 return; 627 return;
453 } 628 }
454 } 629 }
455 630
510 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 685 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
511 { 686 {
512 dSP; 687 dSP;
513 688
514 ENTER; SAVETMPS; 689 ENTER; SAVETMPS;
515 SAVESTACK_POS ();
516 PUSHMARK (SP); 690 PUSHMARK (SP);
517 EXTEND (SP, 2); 691 EXTEND (SP, 2);
518 // we re-bless the reference to get overload and other niceties right 692 // we re-bless the reference to get overload and other niceties right
519 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 693 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
520 PUSHs (sv_cbor); 694 PUSHs (sv_cbor);
529 703
530 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 704 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
531 encode_uint (enc, MAJOR_ARRAY, count + 1); 705 encode_uint (enc, MAJOR_ARRAY, count + 1);
532 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 706 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
533 707
534 while (count) 708 {
709 int i;
710
711 for (i = 0; i < count; ++i)
535 encode_sv (enc, SP[1 - count--]); 712 encode_sv (enc, SP[i + 1 - count]);
713
714 SP -= count;
715 }
536 716
537 PUTBACK; 717 PUTBACK;
538 718
539 FREETMPS; LEAVE; 719 FREETMPS; LEAVE;
540 } 720 }
562 742
563 if (ecb_expect_false (nv == (NV)(U32)nv)) 743 if (ecb_expect_false (nv == (NV)(U32)nv))
564 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 744 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
565 //TODO: maybe I32? 745 //TODO: maybe I32?
566 else if (ecb_expect_false (nv == (float)nv)) 746 else if (ecb_expect_false (nv == (float)nv))
567 { 747 encode_float32 (enc, nv);
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
570 uint32_t fp = ecb_float_to_binary32 (nv);
571
572 if (!ecb_big_endian ())
573 fp = ecb_bswap32 (fp);
574
575 memcpy (enc->cur, &fp, 4);
576 enc->cur += 4;
577 }
578 else 748 else
579 { 749 encode_float64 (enc, nv);
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
582 uint64_t fp = ecb_double_to_binary64 (nv);
583
584 if (!ecb_big_endian ())
585 fp = ecb_bswap64 (fp);
586
587 memcpy (enc->cur, &fp, 8);
588 enc->cur += 8;
589 }
590} 750}
591 751
592static void 752static void
593encode_sv (enc_t *enc, SV *sv) 753encode_sv (enc_t *enc, SV *sv)
594{ 754{
601 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);
602 } 762 }
603 else if (SvNOKp (sv)) 763 else if (SvNOKp (sv))
604 encode_nv (enc, sv); 764 encode_nv (enc, sv);
605 else if (SvIOKp (sv)) 765 else if (SvIOKp (sv))
606 { 766 encode_int (enc, sv);
607 if (SvIsUV (sv))
608 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
609 else if (SvIVX (sv) >= 0)
610 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
611 else
612 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
613 }
614 else if (SvROK (sv)) 767 else if (SvROK (sv))
615 encode_rv (enc, SvRV (sv)); 768 encode_rv (enc, SvRV (sv));
616 else if (!SvOK (sv)) 769 else if (!SvOK (sv))
617 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 770 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
618 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 771 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
837 990
838 WANT (len); 991 WANT (len);
839 dec->cur += len; 992 dec->cur += len;
840 993
841 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 994 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
842 if (!is_utf8_string (key, len)) 995 if (!cbor_is_utf8_string ((U8 *)key, len))
843 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 996 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
844 997
845 hv_store (hv, key, -len, decode_sv (dec), 0); 998 hv_store (hv, key, -len, decode_sv (dec), 0);
846 999
847 return; 1000 return;
976 } 1129 }
977 1130
978 if (utf8) 1131 if (utf8)
979 { 1132 {
980 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 1133 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
981 if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) 1134 if (!cbor_is_utf8_string (SvPVX (sv), SvCUR (sv)))
982 ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); 1135 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
983 1136
984 SvUTF8_on (sv); 1137 SvUTF8_on (sv);
985 } 1138 }
986 1139
1141 1294
1142 sv = decode_sv (dec); 1295 sv = decode_sv (dec);
1143 1296
1144 dSP; 1297 dSP;
1145 ENTER; SAVETMPS; 1298 ENTER; SAVETMPS;
1146 SAVESTACK_POS ();
1147 PUSHMARK (SP); 1299 PUSHMARK (SP);
1148 EXTEND (SP, 2); 1300 EXTEND (SP, 2);
1149 PUSHs (tag_sv); 1301 PUSHs (tag_sv);
1150 PUSHs (sv); 1302 PUSHs (sv);
1151 1303
1162 1314
1163 if (count) 1315 if (count)
1164 { 1316 {
1165 SvREFCNT_dec_NN (tag_sv); 1317 SvREFCNT_dec_NN (tag_sv);
1166 SvREFCNT_dec_NN (sv); 1318 SvREFCNT_dec_NN (sv);
1167 sv = SvREFCNT_inc_NN (POPs); 1319 sv = SvREFCNT_inc_NN (TOPs);
1320 SP -= count;
1168 } 1321 }
1169 else 1322 else
1170 { 1323 {
1171 AV *av = newAV (); 1324 AV *av = newAV ();
1172 av_push (av, tag_sv); 1325 av_push (av, tag_sv);
1419 1572
1420 break; 1573 break;
1421 1574
1422 case MAJOR_MAP >> MAJOR_SHIFT: 1575 case MAJOR_MAP >> MAJOR_SHIFT:
1423 len <<= 1; 1576 len <<= 1;
1577 /* FALLTHROUGH */
1424 case MAJOR_ARRAY >> MAJOR_SHIFT: 1578 case MAJOR_ARRAY >> MAJOR_SHIFT:
1425 if (len) 1579 if (len)
1426 { 1580 {
1427 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest 1581 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1428 count = len + 1; 1582 count = len + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines