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.69 by root, Mon Nov 30 20:38:25 2020 UTC

99 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 99 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
100 100
101 CBOR_TAG_MAGIC = 55799, // self-describe cbor 101 CBOR_TAG_MAGIC = 55799, // self-describe cbor
102}; 102};
103 103
104// known forced types, also hardcoded in CBOR.pm
105enum
106{
107 AS_CBOR = 0,
108 AS_INT = 1,
109 AS_BYTES = 2,
110 AS_TEXT = 3,
111 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6,
114 // possibly future enhancements: (generic) float, (generic) string
115};
116
104#define F_SHRINK 0x00000001UL 117#define F_SHRINK 0x00000001UL
105#define F_ALLOW_UNKNOWN 0x00000002UL 118#define F_ALLOW_UNKNOWN 0x00000002UL
106#define F_ALLOW_SHARING 0x00000004UL 119#define F_ALLOW_SHARING 0x00000004UL
107#define F_ALLOW_CYCLES 0x00000008UL 120#define F_ALLOW_CYCLES 0x00000008UL
108#define F_FORBID_OBJECTS 0x00000010UL 121#define F_FORBID_OBJECTS 0x00000010UL
233{ 246{
234 need (enc, 1); 247 need (enc, 1);
235 *enc->cur++ = ch; 248 *enc->cur++ = ch;
236} 249}
237 250
251// used for tags, intregers, element counts and so on
238static void 252static void
239encode_uint (enc_t *enc, int major, UV len) 253encode_uint (enc_t *enc, int major, UV len)
240{ 254{
241 need (enc, 9); 255 need (enc, 9);
242 256
273 *enc->cur++ = len >> 8; 287 *enc->cur++ = len >> 8;
274 *enc->cur++ = len; 288 *enc->cur++ = len;
275 } 289 }
276} 290}
277 291
292// encodes a perl value into a CBOR integer
293ecb_inline void
294encode_int (enc_t *enc, SV *sv)
295{
296 if (SvIsUV (sv))
297 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
298 else if (SvIVX (sv) >= 0)
299 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
300 else
301 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
302}
303
278ecb_inline void 304ecb_inline void
279encode_tag (enc_t *enc, UV tag) 305encode_tag (enc_t *enc, UV tag)
280{ 306{
281 encode_uint (enc, MAJOR_TAG, tag); 307 encode_uint (enc, MAJOR_TAG, tag);
282} 308}
341 ++enc->stringref_idx; 367 ++enc->stringref_idx;
342 } 368 }
343 } 369 }
344 370
345 encode_str (enc, upgrade_utf8, utf8, str, len); 371 encode_str (enc, upgrade_utf8, utf8, str, len);
372}
373
374ecb_inline void
375encode_float16 (enc_t *enc, NV nv)
376{
377 need (enc, 1+2);
378
379 *enc->cur++ = MAJOR_MISC | MISC_FLOAT16;
380
381 uint16_t fp = ecb_float_to_binary16 (nv);
382
383 if (!ecb_big_endian ())
384 fp = ecb_bswap16 (fp);
385
386 memcpy (enc->cur, &fp, 2);
387 enc->cur += 2;
388}
389
390ecb_inline void
391encode_float32 (enc_t *enc, NV nv)
392{
393 need (enc, 1+4);
394
395 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
396
397 uint32_t fp = ecb_float_to_binary32 (nv);
398
399 if (!ecb_big_endian ())
400 fp = ecb_bswap32 (fp);
401
402 memcpy (enc->cur, &fp, 4);
403 enc->cur += 4;
404}
405
406ecb_inline void
407encode_float64 (enc_t *enc, NV nv)
408{
409 need (enc, 1+8);
410
411 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
412
413 uint64_t fp = ecb_double_to_binary64 (nv);
414
415 if (!ecb_big_endian ())
416 fp = ecb_bswap64 (fp);
417
418 memcpy (enc->cur, &fp, 8);
419 enc->cur += 8;
420}
421
422ecb_inline void
423encode_bool (enc_t *enc, int istrue)
424{
425 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
426}
427
428ecb_inline void
429encode_forced (enc_t *enc, UV type, SV *sv)
430{
431 switch (type)
432 {
433 case AS_CBOR:
434 {
435 STRLEN len;
436 char *str = SvPVbyte (sv, len);
437
438 need (enc, len);
439 memcpy (enc->cur, str, len);
440 enc->cur += len;
441 }
442 break;
443
444 case AS_BYTES:
445 {
446 STRLEN len;
447 char *str = SvPVbyte (sv, len);
448 encode_strref (enc, 0, 0, str, len);
449 }
450 break;
451
452 case AS_TEXT:
453 {
454 STRLEN len;
455 char *str = SvPVutf8 (sv, len);
456 encode_strref (enc, 1, 1, str, len);
457 }
458 break;
459
460 case AS_INT: encode_int (enc, sv); break;
461
462 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
463 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
464 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
465
466 default:
467 croak ("encountered malformed CBOR::XS::Tagged object");
468 }
346} 469}
347 470
348static void encode_sv (enc_t *enc, SV *sv); 471static void encode_sv (enc_t *enc, SV *sv);
349 472
350static void 473static void
431 554
432 HV *stash = SvSTASH (sv); 555 HV *stash = SvSTASH (sv);
433 556
434 if (stash == boolean_stash) 557 if (stash == boolean_stash)
435 { 558 {
436 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 559 encode_bool (enc, SvIV (sv));
437 return; 560 return;
438 } 561 }
439 else if (stash == error_stash) 562 else if (stash == error_stash)
440 { 563 {
441 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 564 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
444 else if (stash == tagged_stash) 567 else if (stash == tagged_stash)
445 { 568 {
446 if (svt != SVt_PVAV) 569 if (svt != SVt_PVAV)
447 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 570 croak ("encountered CBOR::XS::Tagged object that isn't an array");
448 571
572 switch (av_len ((AV *)sv))
573 {
574 case 2-1:
575 // actually a tagged value
449 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); 576 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
450 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 577 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
578 break;
579
580 case 3-1:
581 // a forced type [value, type, undef]
582 encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1));
583 break;
584
585 default:
586 croak ("encountered malformed CBOR::XS::Tagged object");
587 }
451 588
452 return; 589 return;
453 } 590 }
454 } 591 }
455 592
510 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 647 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
511 { 648 {
512 dSP; 649 dSP;
513 650
514 ENTER; SAVETMPS; 651 ENTER; SAVETMPS;
515 SAVESTACK_POS ();
516 PUSHMARK (SP); 652 PUSHMARK (SP);
517 EXTEND (SP, 2); 653 EXTEND (SP, 2);
518 // we re-bless the reference to get overload and other niceties right 654 // we re-bless the reference to get overload and other niceties right
519 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 655 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
520 PUSHs (sv_cbor); 656 PUSHs (sv_cbor);
529 665
530 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 666 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
531 encode_uint (enc, MAJOR_ARRAY, count + 1); 667 encode_uint (enc, MAJOR_ARRAY, count + 1);
532 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 668 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
533 669
534 while (count) 670 {
671 int i;
672
673 for (i = 0; i < count; ++i)
535 encode_sv (enc, SP[1 - count--]); 674 encode_sv (enc, SP[i + 1 - count]);
675
676 SP -= count;
677 }
536 678
537 PUTBACK; 679 PUTBACK;
538 680
539 FREETMPS; LEAVE; 681 FREETMPS; LEAVE;
540 } 682 }
562 704
563 if (ecb_expect_false (nv == (NV)(U32)nv)) 705 if (ecb_expect_false (nv == (NV)(U32)nv))
564 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 706 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
565 //TODO: maybe I32? 707 //TODO: maybe I32?
566 else if (ecb_expect_false (nv == (float)nv)) 708 else if (ecb_expect_false (nv == (float)nv))
567 { 709 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 710 else
579 { 711 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} 712}
591 713
592static void 714static void
593encode_sv (enc_t *enc, SV *sv) 715encode_sv (enc_t *enc, SV *sv)
594{ 716{
601 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); 723 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
602 } 724 }
603 else if (SvNOKp (sv)) 725 else if (SvNOKp (sv))
604 encode_nv (enc, sv); 726 encode_nv (enc, sv);
605 else if (SvIOKp (sv)) 727 else if (SvIOKp (sv))
606 { 728 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)) 729 else if (SvROK (sv))
615 encode_rv (enc, SvRV (sv)); 730 encode_rv (enc, SvRV (sv));
616 else if (!SvOK (sv)) 731 else if (!SvOK (sv))
617 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 732 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
618 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 733 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
1141 1256
1142 sv = decode_sv (dec); 1257 sv = decode_sv (dec);
1143 1258
1144 dSP; 1259 dSP;
1145 ENTER; SAVETMPS; 1260 ENTER; SAVETMPS;
1146 SAVESTACK_POS ();
1147 PUSHMARK (SP); 1261 PUSHMARK (SP);
1148 EXTEND (SP, 2); 1262 EXTEND (SP, 2);
1149 PUSHs (tag_sv); 1263 PUSHs (tag_sv);
1150 PUSHs (sv); 1264 PUSHs (sv);
1151 1265
1162 1276
1163 if (count) 1277 if (count)
1164 { 1278 {
1165 SvREFCNT_dec_NN (tag_sv); 1279 SvREFCNT_dec_NN (tag_sv);
1166 SvREFCNT_dec_NN (sv); 1280 SvREFCNT_dec_NN (sv);
1167 sv = SvREFCNT_inc_NN (POPs); 1281 sv = SvREFCNT_inc_NN (TOPs);
1282 SP -= count;
1168 } 1283 }
1169 else 1284 else
1170 { 1285 {
1171 AV *av = newAV (); 1286 AV *av = newAV ();
1172 av_push (av, tag_sv); 1287 av_push (av, tag_sv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines