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.35 by root, Sat Nov 30 17:19:34 2013 UTC vs.
Revision 1.55 by root, Fri Nov 25 06:13:16 2016 UTC

7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11 11
12#define ECB_NO_THREADS 1
12#include "ecb.h" 13#include "ecb.h"
13 14
14// compatibility with perl <5.18 15// compatibility with perl <5.18
15#ifndef HvNAMELEN_get 16#ifndef HvNAMELEN_get
16# define HvNAMELEN_get(hv) strlen (HvNAME (hv)) 17# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
97}; 98};
98 99
99#define F_SHRINK 0x00000001UL 100#define F_SHRINK 0x00000001UL
100#define F_ALLOW_UNKNOWN 0x00000002UL 101#define F_ALLOW_UNKNOWN 0x00000002UL
101#define F_ALLOW_SHARING 0x00000004UL 102#define F_ALLOW_SHARING 0x00000004UL
103#define F_ALLOW_CYCLES 0x00000008UL
102#define F_PACK_STRINGS 0x00000008UL 104#define F_PACK_STRINGS 0x00000010UL
105#define F_TEXT_KEYS 0x00000020UL
106#define F_TEXT_STRINGS 0x00000040UL
107#define F_VALIDATE_UTF8 0x00000080UL
103 108
104#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
105 110
106#define SB do { 111#define SB do {
107#define SE } while (0) 112#define SE } while (0)
126typedef struct { 131typedef struct {
127 U32 flags; 132 U32 flags;
128 U32 max_depth; 133 U32 max_depth;
129 STRLEN max_size; 134 STRLEN max_size;
130 SV *filter; 135 SV *filter;
136
137 // for the incremental parser
138 STRLEN incr_pos; // the current offset into the text
139 STRLEN incr_need; // minimum bytes needed to decode
140 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
131} CBOR; 141} CBOR;
132 142
133ecb_inline void 143ecb_inline void
134cbor_init (CBOR *cbor) 144cbor_init (CBOR *cbor)
135{ 145{
139 149
140ecb_inline void 150ecb_inline void
141cbor_free (CBOR *cbor) 151cbor_free (CBOR *cbor)
142{ 152{
143 SvREFCNT_dec (cbor->filter); 153 SvREFCNT_dec (cbor->filter);
154 SvREFCNT_dec (cbor->incr_count);
144} 155}
145 156
146///////////////////////////////////////////////////////////////////////////// 157/////////////////////////////////////////////////////////////////////////////
147// utility functions 158// utility functions
148 159
228{ 239{
229 need (enc, 9); 240 need (enc, 9);
230 241
231 if (ecb_expect_true (len < LENGTH_EXT1)) 242 if (ecb_expect_true (len < LENGTH_EXT1))
232 *enc->cur++ = major | len; 243 *enc->cur++ = major | len;
233 else if (ecb_expect_true (len <= 0xff)) 244 else if (ecb_expect_true (len <= 0xffU))
234 { 245 {
235 *enc->cur++ = major | LENGTH_EXT1; 246 *enc->cur++ = major | LENGTH_EXT1;
236 *enc->cur++ = len; 247 *enc->cur++ = len;
237 } 248 }
238 else if (len <= 0xffff) 249 else if (len <= 0xffffU)
239 { 250 {
240 *enc->cur++ = major | LENGTH_EXT2; 251 *enc->cur++ = major | LENGTH_EXT2;
241 *enc->cur++ = len >> 8; 252 *enc->cur++ = len >> 8;
242 *enc->cur++ = len; 253 *enc->cur++ = len;
243 } 254 }
244 else if (len <= 0xffffffff) 255 else if (len <= 0xffffffffU)
245 { 256 {
246 *enc->cur++ = major | LENGTH_EXT4; 257 *enc->cur++ = major | LENGTH_EXT4;
247 *enc->cur++ = len >> 24; 258 *enc->cur++ = len >> 24;
248 *enc->cur++ = len >> 16; 259 *enc->cur++ = len >> 16;
249 *enc->cur++ = len >> 8; 260 *enc->cur++ = len >> 8;
267encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
268{ 279{
269 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
270} 281}
271 282
283// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
284ecb_noinline static void
285encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
286{
287 STRLEN ulen = len;
288 U8 *p, *pend = (U8 *)str + len;
289
290 for (p = (U8 *)str; p < pend; ++p)
291 ulen += *p >> 7; // count set high bits
292
293 encode_uint (enc, MAJOR_TEXT, ulen);
294
295 need (enc, ulen);
296 for (p = (U8 *)str; p < pend; ++p)
297 if (*p < 0x80)
298 *enc->cur++ = *p;
299 else
300 {
301 *enc->cur++ = 0xc0 + (*p >> 6);
302 *enc->cur++ = 0x80 + (*p & 63);
303 }
304}
305
272ecb_inline void 306ecb_inline void
273encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
274{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
275 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
276 need (enc, len); 317 need (enc, len);
277 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
278 enc->cur += len; 319 enc->cur += len;
279} 320}
280 321
281static void 322ecb_inline void
282encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 324{
284 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
285 { 326 {
286 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
287 328
298 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
299 ++enc->stringref_idx; 340 ++enc->stringref_idx;
300 } 341 }
301 } 342 }
302 343
303 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
304} 345}
305 346
306static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
307 348
308static void 349static void
315 356
316 ++enc->depth; 357 ++enc->depth;
317 358
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 359 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 360
361 if (SvMAGICAL (av))
320 for (i = 0; i <= len; ++i) 362 for (i = 0; i <= len; ++i)
321 { 363 {
322 SV **svp = av_fetch (av, i, 0); 364 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 365 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 366 }
367 else
368 for (i = 0; i <= len; ++i)
369 {
370 SV *sv = AvARRAY (av)[i];
371 encode_sv (enc, sv ? sv : &PL_sv_undef);
372 }
325 373
326 --enc->depth; 374 --enc->depth;
327} 375}
328 376
329static void 377static void
347 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
348 { 396 {
349 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
350 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
351 else 399 else
352 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 400 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
353 401
354 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 402 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
355 } 403 }
356 404
357 if (mg) 405 if (mg)
433 481
434 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
435 { 483 {
436 dSP; 484 dSP;
437 485
438 ENTER; SAVETMPS; PUSHMARK (SP); 486 ENTER; SAVETMPS;
487 PUSHMARK (SP);
439 // we re-bless the reference to get overload and other niceties right 488 // we re-bless the reference to get overload and other niceties right
440 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 489 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
441 490
442 PUTBACK; 491 PUTBACK;
443 // G_SCALAR ensures that return value is 1 492 // G_SCALAR ensures that return value is 1
456 } 505 }
457 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 506 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
458 { 507 {
459 dSP; 508 dSP;
460 509
461 ENTER; SAVETMPS; PUSHMARK (SP); 510 ENTER; SAVETMPS;
511 SAVESTACK_POS ();
512 PUSHMARK (SP);
462 EXTEND (SP, 2); 513 EXTEND (SP, 2);
463 // we re-bless the reference to get overload and other niceties right 514 // we re-bless the reference to get overload and other niceties right
464 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 515 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
465 PUSHs (sv_cbor); 516 PUSHs (sv_cbor);
466 517
472 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
473 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 524 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
474 525
475 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
476 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
477 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
478 529
479 while (count) 530 while (count)
480 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
481 532
482 PUTBACK; 533 PUTBACK;
541 592
542 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
543 { 594 {
544 STRLEN len; 595 STRLEN len;
545 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
546 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
547 } 598 }
548 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
549 encode_nv (enc, sv); 600 encode_nv (enc, sv);
550 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
551 { 602 {
568} 619}
569 620
570static SV * 621static SV *
571encode_cbor (SV *scalar, CBOR *cbor) 622encode_cbor (SV *scalar, CBOR *cbor)
572{ 623{
573 enc_t enc = { }; 624 enc_t enc = { 0 };
574 625
575 enc.cbor = *cbor; 626 enc.cbor = *cbor;
576 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
577 enc.cur = SvPVX (enc.sv); 628 enc.cur = SvPVX (enc.sv);
578 enc.end = SvEND (enc.sv); 629 enc.end = SvEND (enc.sv);
627 U8 m = *dec->cur & MINOR_MASK; 678 U8 m = *dec->cur & MINOR_MASK;
628 ++dec->cur; 679 ++dec->cur;
629 680
630 if (ecb_expect_true (m < LENGTH_EXT1)) 681 if (ecb_expect_true (m < LENGTH_EXT1))
631 return m; 682 return m;
632 683 else if (ecb_expect_true (m == LENGTH_EXT1))
633 switch (m)
634 { 684 {
635 case LENGTH_EXT1:
636 WANT (1); 685 WANT (1);
637 dec->cur += 1; 686 dec->cur += 1;
638 return dec->cur[-1]; 687 return dec->cur[-1];
639 688 }
640 case LENGTH_EXT2: 689 else if (ecb_expect_true (m == LENGTH_EXT2))
690 {
641 WANT (2); 691 WANT (2);
642 dec->cur += 2; 692 dec->cur += 2;
643 return (((UV)dec->cur[-2]) << 8) 693 return (((UV)dec->cur[-2]) << 8)
644 | ((UV)dec->cur[-1]); 694 | ((UV)dec->cur[-1]);
645 695 }
646 case LENGTH_EXT4: 696 else if (ecb_expect_true (m == LENGTH_EXT4))
697 {
647 WANT (4); 698 WANT (4);
648 dec->cur += 4; 699 dec->cur += 4;
649 return (((UV)dec->cur[-4]) << 24) 700 return (((UV)dec->cur[-4]) << 24)
650 | (((UV)dec->cur[-3]) << 16) 701 | (((UV)dec->cur[-3]) << 16)
651 | (((UV)dec->cur[-2]) << 8) 702 | (((UV)dec->cur[-2]) << 8)
652 | ((UV)dec->cur[-1]); 703 | ((UV)dec->cur[-1]);
653 704 }
654 case LENGTH_EXT8: 705 else if (ecb_expect_true (m == LENGTH_EXT8))
706 {
655 WANT (8); 707 WANT (8);
656 dec->cur += 8; 708 dec->cur += 8;
657 709
658 return 710 return
659#if UVSIZE < 8 711#if UVSIZE < 8
660 0 712 0
661#else 713#else
662 (((UV)dec->cur[-8]) << 56) 714 (((UV)dec->cur[-8]) << 56)
663 | (((UV)dec->cur[-7]) << 48) 715 | (((UV)dec->cur[-7]) << 48)
664 | (((UV)dec->cur[-6]) << 40) 716 | (((UV)dec->cur[-6]) << 40)
665 | (((UV)dec->cur[-5]) << 32) 717 | (((UV)dec->cur[-5]) << 32)
666#endif 718#endif
667 | (((UV)dec->cur[-4]) << 24) 719 | (((UV)dec->cur[-4]) << 24)
668 | (((UV)dec->cur[-3]) << 16) 720 | (((UV)dec->cur[-3]) << 16)
669 | (((UV)dec->cur[-2]) << 8) 721 | (((UV)dec->cur[-2]) << 8)
670 | ((UV)dec->cur[-1]); 722 | ((UV)dec->cur[-1]);
671 723 }
672 default: 724 else
673 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 725 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
674 }
675 726
676fail: 727fail:
677 return 0; 728 return 0;
678} 729}
679 730
705 } 756 }
706 else 757 else
707 { 758 {
708 int i, len = decode_uint (dec); 759 int i, len = decode_uint (dec);
709 760
761 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
710 av_fill (av, len - 1); 762 av_fill (av, len - 1);
711 763
712 for (i = 0; i < len; ++i) 764 for (i = 0; i < len; ++i)
713 AvARRAY (av)[i] = decode_sv (dec); 765 AvARRAY (av)[i] = decode_sv (dec);
714 } 766 }
727{ 779{
728 // for speed reasons, we specialcase single-string 780 // for speed reasons, we specialcase single-string
729 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
730 782
731 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
732 if ((*dec->cur - MAJOR_BYTES) <= 27) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
733 { 785 {
734 I32 len = decode_uint (dec); 786 I32 len = decode_uint (dec);
735 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
736 788
789 WANT (len);
737 dec->cur += len; 790 dec->cur += len;
738 791
739 if (ecb_expect_false (dec->stringref))
740 av_push (dec->stringref, newSVpvn (key, len));
741
742 hv_store (hv, key, len, decode_sv (dec), 0); 792 hv_store (hv, key, len, decode_sv (dec), 0);
743 793
744 return; 794 return;
745 } 795 }
746 else if ((*dec->cur - MAJOR_TEXT) <= 27) 796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 797 {
748 I32 len = decode_uint (dec); 798 I32 len = decode_uint (dec);
749 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
750 800
801 WANT (len);
751 dec->cur += len; 802 dec->cur += len;
752 803
753 if (ecb_expect_false (dec->stringref)) 804 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 805 if (!is_utf8_string (key, len))
806 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
755 807
756 hv_store (hv, key, -len, decode_sv (dec), 0); 808 hv_store (hv, key, -len, decode_sv (dec), 0);
757 809
758 return; 810 return;
759 } 811 }
761 SV *k = decode_sv (dec); 813 SV *k = decode_sv (dec);
762 SV *v = decode_sv (dec); 814 SV *v = decode_sv (dec);
763 815
764 hv_store_ent (hv, k, v, 0); 816 hv_store_ent (hv, k, v, 0);
765 SvREFCNT_dec (k); 817 SvREFCNT_dec (k);
818
819fail:
820 ;
766} 821}
767 822
768static SV * 823static SV *
769decode_hv (dec_t *dec) 824decode_hv (dec_t *dec)
770{ 825{
852 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 907 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
853 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 908 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
854 } 909 }
855 910
856 if (utf8) 911 if (utf8)
912 {
913 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
914 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
915 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
916
857 SvUTF8_on (sv); 917 SvUTF8_on (sv);
918 }
858 919
859 return sv; 920 return sv;
860 921
861fail: 922fail:
862 SvREFCNT_dec (sv); 923 SvREFCNT_dec (sv);
881 sv = newRV_noinc (decode_sv (dec)); 942 sv = newRV_noinc (decode_sv (dec));
882 break; 943 break;
883 944
884 case CBOR_TAG_STRINGREF_NAMESPACE: 945 case CBOR_TAG_STRINGREF_NAMESPACE:
885 { 946 {
947 // do nmot use SAVETMPS/FREETMPS, as these will
948 // erase mortalised caches, e.g. "shareable"
886 ENTER; SAVETMPS; 949 ENTER;
887 950
888 SAVESPTR (dec->stringref); 951 SAVESPTR (dec->stringref);
889 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 952 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
890 953
891 sv = decode_sv (dec); 954 sv = decode_sv (dec);
892 955
893 FREETMPS; LEAVE; 956 LEAVE;
894 } 957 }
895 break; 958 break;
896 959
897 case CBOR_TAG_STRINGREF: 960 case CBOR_TAG_STRINGREF:
898 { 961 {
911 case CBOR_TAG_VALUE_SHAREABLE: 974 case CBOR_TAG_VALUE_SHAREABLE:
912 { 975 {
913 if (ecb_expect_false (!dec->shareable)) 976 if (ecb_expect_false (!dec->shareable))
914 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 977 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
915 978
979 if (dec->cbor.flags & F_ALLOW_CYCLES)
980 {
916 sv = newSV (0); 981 sv = newSV (0);
917 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 982 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
918 983
919 SV *osv = decode_sv (dec); 984 SV *osv = decode_sv (dec);
920 sv_setsv (sv, osv); 985 sv_setsv (sv, osv);
921 SvREFCNT_dec_NN (osv); 986 SvREFCNT_dec_NN (osv);
987 }
988 else
989 {
990 av_push (dec->shareable, &PL_sv_undef);
991 int idx = AvFILLp (dec->shareable);
992 sv = decode_sv (dec);
993 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
994 }
922 } 995 }
923 break; 996 break;
924 997
925 case CBOR_TAG_VALUE_SHAREDREF: 998 case CBOR_TAG_VALUE_SHAREDREF:
926 { 999 {
931 1004
932 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1005 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
933 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1006 ERR ("corrupted CBOR data (sharedref index out of bounds)");
934 1007
935 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1008 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1009
1010 if (sv == &PL_sv_undef)
1011 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
936 } 1012 }
937 break; 1013 break;
938 1014
939 case CBOR_TAG_PERL_OBJECT: 1015 case CBOR_TAG_PERL_OBJECT:
940 { 1016 {
955 if (!method) 1031 if (!method)
956 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1032 ERR ("cannot decode perl-object (package does not have a THAW method)");
957 1033
958 dSP; 1034 dSP;
959 1035
960 ENTER; SAVETMPS; PUSHMARK (SP); 1036 ENTER; SAVETMPS;
1037 PUSHMARK (SP);
961 EXTEND (SP, len + 1); 1038 EXTEND (SP, len + 1);
962 // we re-bless the reference to get overload and other niceties right 1039 // we re-bless the reference to get overload and other niceties right
963 PUSHs (*av_fetch (av, 0, 1)); 1040 PUSHs (*av_fetch (av, 0, 1));
964 PUSHs (sv_cbor); 1041 PUSHs (sv_cbor);
965 1042
990 default: 1067 default:
991 { 1068 {
992 sv = decode_sv (dec); 1069 sv = decode_sv (dec);
993 1070
994 dSP; 1071 dSP;
995 ENTER; SAVETMPS; PUSHMARK (SP); 1072 ENTER; SAVETMPS;
1073 SAVESTACK_POS ();
1074 PUSHMARK (SP);
996 EXTEND (SP, 2); 1075 EXTEND (SP, 2);
997 PUSHs (newSVuv (tag)); 1076 PUSHs (newSVuv (tag));
998 PUSHs (sv); 1077 PUSHs (sv);
999 1078
1000 PUTBACK; 1079 PUTBACK;
1109 1188
1110 return newSVnv (ecb_binary64_to_double (fp)); 1189 return newSVnv (ecb_binary64_to_double (fp));
1111 } 1190 }
1112 1191
1113 // 0..19 unassigned simple 1192 // 0..19 unassigned simple
1114 // 24 reserved + unassigned (reserved values are not encodable) 1193 // 24 reserved + unassigned simple (reserved values are not encodable)
1194 // 28-30 unassigned misc
1195 // 31 break code
1115 default: 1196 default:
1116 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1197 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1117 } 1198 }
1118 1199
1119 break; 1200 break;
1120 } 1201 }
1121 1202
1124} 1205}
1125 1206
1126static SV * 1207static SV *
1127decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1208decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1128{ 1209{
1129 dec_t dec = { }; 1210 dec_t dec = { 0 };
1130 SV *sv; 1211 SV *sv;
1131 STRLEN len; 1212 STRLEN len;
1132 char *data = SvPVbyte (string, len); 1213 char *data = SvPVbyte (string, len);
1133 1214
1134 if (len > cbor->max_size && cbor->max_size) 1215 if (len > cbor->max_size && cbor->max_size)
1148 if (dec.cur != dec.end && !dec.err) 1229 if (dec.cur != dec.end && !dec.err)
1149 dec.err = "garbage after CBOR object"; 1230 dec.err = "garbage after CBOR object";
1150 1231
1151 if (dec.err) 1232 if (dec.err)
1152 { 1233 {
1234 if (dec.shareable)
1235 {
1236 // need to break cyclic links, which whould all be in shareable
1237 int i;
1238 SV **svp;
1239
1240 for (i = av_len (dec.shareable) + 1; i--; )
1241 if ((svp = av_fetch (dec.shareable, i, 0)))
1242 sv_setsv (*svp, &PL_sv_undef);
1243 }
1244
1153 SvREFCNT_dec (sv); 1245 SvREFCNT_dec (sv);
1154 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1246 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1155 } 1247 }
1156 1248
1157 sv = sv_2mortal (sv); 1249 sv = sv_2mortal (sv);
1158 1250
1159 return sv; 1251 return sv;
1160} 1252}
1161 1253
1254/////////////////////////////////////////////////////////////////////////////
1255// incremental parser
1256
1257#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1258
1259// returns 0 for notyet, 1 for success or error
1260static int
1261incr_parse (CBOR *self, SV *cborstr)
1262{
1263 STRLEN cur;
1264 SvPV (cborstr, cur);
1265
1266 while (ecb_expect_true (self->incr_need <= cur))
1267 {
1268 // table of integer count bytes
1269 static I8 incr_len[MINOR_MASK + 1] = {
1270 0, 0, 0, 0, 0, 0, 0, 0,
1271 0, 0, 0, 0, 0, 0, 0, 0,
1272 0, 0, 0, 0, 0, 0, 0, 0,
1273 1, 2, 4, 8,-1,-1,-1,-2
1274 };
1275
1276 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1277 U8 m = *p & MINOR_MASK;
1278 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1279 I8 ilen = incr_len[m];
1280
1281 self->incr_need = self->incr_pos + 1;
1282
1283 if (ecb_expect_false (ilen < 0))
1284 {
1285 if (m != MINOR_INDEF)
1286 return 1; // error
1287
1288 if (*p == (MAJOR_MISC | MINOR_INDEF))
1289 {
1290 if (count >= 0)
1291 return 1; // error
1292
1293 count = 1;
1294 }
1295 else
1296 {
1297 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1298 count = -1;
1299 }
1300 }
1301 else
1302 {
1303 self->incr_need += ilen;
1304 if (ecb_expect_false (self->incr_need > cur))
1305 return 0;
1306
1307 int major = *p >> MAJOR_SHIFT;
1308
1309 switch (major)
1310 {
1311 case MAJOR_TAG >> MAJOR_SHIFT:
1312 ++count; // tags merely prefix another value
1313 break;
1314
1315 case MAJOR_BYTES >> MAJOR_SHIFT:
1316 case MAJOR_TEXT >> MAJOR_SHIFT:
1317 case MAJOR_ARRAY >> MAJOR_SHIFT:
1318 case MAJOR_MAP >> MAJOR_SHIFT:
1319 {
1320 UV len;
1321
1322 if (ecb_expect_false (ilen))
1323 {
1324 len = 0;
1325
1326 do {
1327 len = (len << 8) | *++p;
1328 } while (--ilen);
1329 }
1330 else
1331 len = m;
1332
1333 switch (major)
1334 {
1335 case MAJOR_BYTES >> MAJOR_SHIFT:
1336 case MAJOR_TEXT >> MAJOR_SHIFT:
1337 self->incr_need += len;
1338 if (ecb_expect_false (self->incr_need > cur))
1339 return 0;
1340
1341 break;
1342
1343 case MAJOR_MAP >> MAJOR_SHIFT:
1344 len <<= 1;
1345 case MAJOR_ARRAY >> MAJOR_SHIFT:
1346 if (len)
1347 {
1348 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1349 count = len + 1;
1350 }
1351 break;
1352 }
1353 }
1354 }
1355 }
1356
1357 self->incr_pos = self->incr_need;
1358
1359 if (count > 0)
1360 {
1361 while (!--count)
1362 {
1363 if (!AvFILLp (self->incr_count))
1364 return 1; // done
1365
1366 SvREFCNT_dec_NN (av_pop (self->incr_count));
1367 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1368 }
1369
1370 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1371 }
1372 }
1373
1374 return 0;
1375}
1376
1377
1162///////////////////////////////////////////////////////////////////////////// 1378/////////////////////////////////////////////////////////////////////////////
1163// XS interface functions 1379// XS interface functions
1164 1380
1165MODULE = CBOR::XS PACKAGE = CBOR::XS 1381MODULE = CBOR::XS PACKAGE = CBOR::XS
1166 1382
1206void shrink (CBOR *self, int enable = 1) 1422void shrink (CBOR *self, int enable = 1)
1207 ALIAS: 1423 ALIAS:
1208 shrink = F_SHRINK 1424 shrink = F_SHRINK
1209 allow_unknown = F_ALLOW_UNKNOWN 1425 allow_unknown = F_ALLOW_UNKNOWN
1210 allow_sharing = F_ALLOW_SHARING 1426 allow_sharing = F_ALLOW_SHARING
1427 allow_cycles = F_ALLOW_CYCLES
1211 pack_strings = F_PACK_STRINGS 1428 pack_strings = F_PACK_STRINGS
1429 text_keys = F_TEXT_KEYS
1430 text_strings = F_TEXT_STRINGS
1431 validate_utf8 = F_VALIDATE_UTF8
1212 PPCODE: 1432 PPCODE:
1213{ 1433{
1214 if (enable) 1434 if (enable)
1215 self->flags |= ix; 1435 self->flags |= ix;
1216 else 1436 else
1222void get_shrink (CBOR *self) 1442void get_shrink (CBOR *self)
1223 ALIAS: 1443 ALIAS:
1224 get_shrink = F_SHRINK 1444 get_shrink = F_SHRINK
1225 get_allow_unknown = F_ALLOW_UNKNOWN 1445 get_allow_unknown = F_ALLOW_UNKNOWN
1226 get_allow_sharing = F_ALLOW_SHARING 1446 get_allow_sharing = F_ALLOW_SHARING
1447 get_allow_cycles = F_ALLOW_CYCLES
1227 get_pack_strings = F_PACK_STRINGS 1448 get_pack_strings = F_PACK_STRINGS
1449 get_text_keys = F_TEXT_KEYS
1450 get_text_strings = F_TEXT_STRINGS
1451 get_validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1452 PPCODE:
1229 XPUSHs (boolSV (self->flags & ix)); 1453 XPUSHs (boolSV (self->flags & ix));
1230 1454
1231void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1455void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1232 PPCODE: 1456 PPCODE:
1281 EXTEND (SP, 2); 1505 EXTEND (SP, 2);
1282 PUSHs (sv); 1506 PUSHs (sv);
1283 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1507 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1284} 1508}
1285 1509
1510void incr_parse (CBOR *self, SV *cborstr)
1511 ALIAS:
1512 incr_parse_multiple = 1
1513 PPCODE:
1514{
1515 if (SvUTF8 (cborstr))
1516 sv_utf8_downgrade (cborstr, 0);
1517
1518 if (!self->incr_count)
1519 {
1520 self->incr_count = newAV ();
1521 self->incr_pos = 0;
1522 self->incr_need = 1;
1523
1524 av_push (self->incr_count, newSViv (1));
1525 }
1526
1527 do
1528 {
1529 if (!incr_parse (self, cborstr))
1530 {
1531 if (self->incr_need > self->max_size && self->max_size)
1532 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1533 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1534
1535 break;
1536 }
1537
1538 SV *sv;
1539 char *offset;
1540
1541 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1542 XPUSHs (sv);
1543
1544 sv_chop (cborstr, offset);
1545
1546 av_clear (self->incr_count);
1547 av_push (self->incr_count, newSViv (1));
1548
1549 self->incr_pos = 0;
1550 self->incr_need = self->incr_pos + 1;
1551 }
1552 while (ix);
1553}
1554
1555void incr_reset (CBOR *self)
1556 CODE:
1557{
1558 SvREFCNT_dec (self->incr_count);
1559 self->incr_count = 0;
1560}
1561
1286void DESTROY (CBOR *self) 1562void DESTROY (CBOR *self)
1287 PPCODE: 1563 PPCODE:
1288 cbor_free (self); 1564 cbor_free (self);
1289 1565
1290PROTOTYPES: ENABLE 1566PROTOTYPES: ENABLE
1291 1567
1292void encode_cbor (SV *scalar) 1568void encode_cbor (SV *scalar)
1569 ALIAS:
1570 encode_cbor = 0
1571 encode_cbor_sharing = F_ALLOW_SHARING
1293 PPCODE: 1572 PPCODE:
1294{ 1573{
1295 CBOR cbor; 1574 CBOR cbor;
1296 cbor_init (&cbor); 1575 cbor_init (&cbor);
1576 cbor.flags |= ix;
1297 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1577 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1298 XPUSHs (scalar); 1578 XPUSHs (scalar);
1299} 1579}
1300 1580
1301void decode_cbor (SV *cborstr) 1581void decode_cbor (SV *cborstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines