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.62 by root, Sat Nov 26 02:11:52 2016 UTC

6#include <string.h> 6#include <string.h>
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#include <inttypes.h>
11 12
13#define ECB_NO_THREADS 1
12#include "ecb.h" 14#include "ecb.h"
13 15
14// compatibility with perl <5.18 16// compatibility with perl <5.18
15#ifndef HvNAMELEN_get 17#ifndef HvNAMELEN_get
16# define HvNAMELEN_get(hv) strlen (HvNAME (hv)) 18# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
97}; 99};
98 100
99#define F_SHRINK 0x00000001UL 101#define F_SHRINK 0x00000001UL
100#define F_ALLOW_UNKNOWN 0x00000002UL 102#define F_ALLOW_UNKNOWN 0x00000002UL
101#define F_ALLOW_SHARING 0x00000004UL 103#define F_ALLOW_SHARING 0x00000004UL
104#define F_ALLOW_CYCLES 0x00000008UL
105#define F_FORBID_OBJECTS 0x00000010UL
102#define F_PACK_STRINGS 0x00000008UL 106#define F_PACK_STRINGS 0x00000020UL
107#define F_TEXT_KEYS 0x00000040UL
108#define F_TEXT_STRINGS 0x00000080UL
109#define F_VALIDATE_UTF8 0x00000100UL
103 110
104#define INIT_SIZE 32 // initial scalar size to be allocated 111#define INIT_SIZE 32 // initial scalar size to be allocated
105 112
106#define SB do { 113#define SB do {
107#define SE } while (0) 114#define SE } while (0)
126typedef struct { 133typedef struct {
127 U32 flags; 134 U32 flags;
128 U32 max_depth; 135 U32 max_depth;
129 STRLEN max_size; 136 STRLEN max_size;
130 SV *filter; 137 SV *filter;
138
139 // for the incremental parser
140 STRLEN incr_pos; // the current offset into the text
141 STRLEN incr_need; // minimum bytes needed to decode
142 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
131} CBOR; 143} CBOR;
132 144
133ecb_inline void 145ecb_inline void
134cbor_init (CBOR *cbor) 146cbor_init (CBOR *cbor)
135{ 147{
139 151
140ecb_inline void 152ecb_inline void
141cbor_free (CBOR *cbor) 153cbor_free (CBOR *cbor)
142{ 154{
143 SvREFCNT_dec (cbor->filter); 155 SvREFCNT_dec (cbor->filter);
156 SvREFCNT_dec (cbor->incr_count);
144} 157}
145 158
146///////////////////////////////////////////////////////////////////////////// 159/////////////////////////////////////////////////////////////////////////////
147// utility functions 160// utility functions
148 161
174 187
175// minimum length of a string to be registered for stringref 188// minimum length of a string to be registered for stringref
176ecb_inline int 189ecb_inline int
177minimum_string_length (UV idx) 190minimum_string_length (UV idx)
178{ 191{
179 return idx > 23 192 return idx <= 23 ? 3
180 ? idx > 0xffU 193 : idx <= 0xffU ? 4
181 ? idx > 0xffffU 194 : idx <= 0xffffU ? 5
182 ? idx > 0xffffffffU 195 : idx <= 0xffffffffU ? 7
183 ? 11 196 : 11;
184 : 7
185 : 5
186 : 4
187 : 3;
188} 197}
189 198
190///////////////////////////////////////////////////////////////////////////// 199/////////////////////////////////////////////////////////////////////////////
191// encoder 200// encoder
192 201
205} enc_t; 214} enc_t;
206 215
207ecb_inline void 216ecb_inline void
208need (enc_t *enc, STRLEN len) 217need (enc_t *enc, STRLEN len)
209{ 218{
210 if (ecb_expect_false (enc->cur + len >= enc->end)) 219 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
211 { 220 {
212 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 221 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
213 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 222 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
214 enc->cur = SvPVX (enc->sv) + cur; 223 enc->cur = SvPVX (enc->sv) + cur;
215 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 224 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
228{ 237{
229 need (enc, 9); 238 need (enc, 9);
230 239
231 if (ecb_expect_true (len < LENGTH_EXT1)) 240 if (ecb_expect_true (len < LENGTH_EXT1))
232 *enc->cur++ = major | len; 241 *enc->cur++ = major | len;
233 else if (ecb_expect_true (len <= 0xff)) 242 else if (ecb_expect_true (len <= 0xffU))
234 { 243 {
235 *enc->cur++ = major | LENGTH_EXT1; 244 *enc->cur++ = major | LENGTH_EXT1;
236 *enc->cur++ = len; 245 *enc->cur++ = len;
237 } 246 }
238 else if (len <= 0xffff) 247 else if (len <= 0xffffU)
239 { 248 {
240 *enc->cur++ = major | LENGTH_EXT2; 249 *enc->cur++ = major | LENGTH_EXT2;
241 *enc->cur++ = len >> 8; 250 *enc->cur++ = len >> 8;
242 *enc->cur++ = len; 251 *enc->cur++ = len;
243 } 252 }
244 else if (len <= 0xffffffff) 253 else if (len <= 0xffffffffU)
245 { 254 {
246 *enc->cur++ = major | LENGTH_EXT4; 255 *enc->cur++ = major | LENGTH_EXT4;
247 *enc->cur++ = len >> 24; 256 *enc->cur++ = len >> 24;
248 *enc->cur++ = len >> 16; 257 *enc->cur++ = len >> 16;
249 *enc->cur++ = len >> 8; 258 *enc->cur++ = len >> 8;
267encode_tag (enc_t *enc, UV tag) 276encode_tag (enc_t *enc, UV tag)
268{ 277{
269 encode_uint (enc, MAJOR_TAG, tag); 278 encode_uint (enc, MAJOR_TAG, tag);
270} 279}
271 280
281// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
282ecb_noinline static void
283encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
284{
285 STRLEN ulen = len;
286 U8 *p, *pend = (U8 *)str + len;
287
288 for (p = (U8 *)str; p < pend; ++p)
289 ulen += *p >> 7; // count set high bits
290
291 encode_uint (enc, MAJOR_TEXT, ulen);
292
293 need (enc, ulen);
294 for (p = (U8 *)str; p < pend; ++p)
295 if (*p < 0x80)
296 *enc->cur++ = *p;
297 else
298 {
299 *enc->cur++ = 0xc0 + (*p >> 6);
300 *enc->cur++ = 0x80 + (*p & 63);
301 }
302}
303
272ecb_inline void 304ecb_inline void
273encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 305encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
274{ 306{
307 if (ecb_expect_false (upgrade_utf8))
308 if (!utf8)
309 {
310 encode_str_utf8 (enc, utf8, str, len);
311 return;
312 }
313
275 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 314 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
276 need (enc, len); 315 need (enc, len);
277 memcpy (enc->cur, str, len); 316 memcpy (enc->cur, str, len);
278 enc->cur += len; 317 enc->cur += len;
279} 318}
280 319
281static void 320ecb_inline void
282encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 321encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 322{
284 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 323 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
285 { 324 {
286 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 325 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
287 326
298 sv_setuv (*svp, enc->stringref_idx); 337 sv_setuv (*svp, enc->stringref_idx);
299 ++enc->stringref_idx; 338 ++enc->stringref_idx;
300 } 339 }
301 } 340 }
302 341
303 encode_str (enc, utf8, str, len); 342 encode_str (enc, upgrade_utf8, utf8, str, len);
304} 343}
305 344
306static void encode_sv (enc_t *enc, SV *sv); 345static void encode_sv (enc_t *enc, SV *sv);
307 346
308static void 347static void
315 354
316 ++enc->depth; 355 ++enc->depth;
317 356
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 357 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 358
359 if (SvMAGICAL (av))
320 for (i = 0; i <= len; ++i) 360 for (i = 0; i <= len; ++i)
321 { 361 {
322 SV **svp = av_fetch (av, i, 0); 362 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 363 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 364 }
365 else
366 for (i = 0; i <= len; ++i)
367 {
368 SV *sv = AvARRAY (av)[i];
369 encode_sv (enc, sv ? sv : &PL_sv_undef);
370 }
325 371
326 --enc->depth; 372 --enc->depth;
327} 373}
328 374
329static void 375static void
347 while ((he = hv_iternext (hv))) 393 while ((he = hv_iternext (hv)))
348 { 394 {
349 if (HeKLEN (he) == HEf_SVKEY) 395 if (HeKLEN (he) == HEf_SVKEY)
350 encode_sv (enc, HeSVKEY (he)); 396 encode_sv (enc, HeSVKEY (he));
351 else 397 else
352 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 398 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
353 399
354 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 400 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
355 } 401 }
356 402
357 if (mg) 403 if (mg)
429 if (ecb_expect_false (SvOBJECT (sv))) 475 if (ecb_expect_false (SvOBJECT (sv)))
430 { 476 {
431 HV *stash = SvSTASH (sv); 477 HV *stash = SvSTASH (sv);
432 GV *method; 478 GV *method;
433 479
480 if (enc->cbor.flags & F_FORBID_OBJECTS)
481 croak ("encountered object '%s', but forbid_objects is enabled",
482 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
434 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 483 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
435 { 484 {
436 dSP; 485 dSP;
437 486
438 ENTER; SAVETMPS; PUSHMARK (SP); 487 ENTER; SAVETMPS;
488 PUSHMARK (SP);
439 // we re-bless the reference to get overload and other niceties right 489 // we re-bless the reference to get overload and other niceties right
440 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 490 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
441 491
442 PUTBACK; 492 PUTBACK;
443 // G_SCALAR ensures that return value is 1 493 // G_SCALAR ensures that return value is 1
456 } 506 }
457 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 507 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
458 { 508 {
459 dSP; 509 dSP;
460 510
461 ENTER; SAVETMPS; PUSHMARK (SP); 511 ENTER; SAVETMPS;
512 SAVESTACK_POS ();
513 PUSHMARK (SP);
462 EXTEND (SP, 2); 514 EXTEND (SP, 2);
463 // we re-bless the reference to get overload and other niceties right 515 // we re-bless the reference to get overload and other niceties right
464 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 516 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
465 PUSHs (sv_cbor); 517 PUSHs (sv_cbor);
466 518
472 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 524 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)); 525 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
474 526
475 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 527 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
476 encode_uint (enc, MAJOR_ARRAY, count + 1); 528 encode_uint (enc, MAJOR_ARRAY, count + 1);
477 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 529 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
478 530
479 while (count) 531 while (count)
480 encode_sv (enc, SP[1 - count--]); 532 encode_sv (enc, SP[1 - count--]);
481 533
482 PUTBACK; 534 PUTBACK;
541 593
542 if (SvPOKp (sv)) 594 if (SvPOKp (sv))
543 { 595 {
544 STRLEN len; 596 STRLEN len;
545 char *str = SvPV (sv, len); 597 char *str = SvPV (sv, len);
546 encode_strref (enc, SvUTF8 (sv), str, len); 598 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
547 } 599 }
548 else if (SvNOKp (sv)) 600 else if (SvNOKp (sv))
549 encode_nv (enc, sv); 601 encode_nv (enc, sv);
550 else if (SvIOKp (sv)) 602 else if (SvIOKp (sv))
551 { 603 {
568} 620}
569 621
570static SV * 622static SV *
571encode_cbor (SV *scalar, CBOR *cbor) 623encode_cbor (SV *scalar, CBOR *cbor)
572{ 624{
573 enc_t enc = { }; 625 enc_t enc = { 0 };
574 626
575 enc.cbor = *cbor; 627 enc.cbor = *cbor;
576 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 628 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
577 enc.cur = SvPVX (enc.sv); 629 enc.cur = SvPVX (enc.sv);
578 enc.end = SvEND (enc.sv); 630 enc.end = SvEND (enc.sv);
610 U32 depth; // recursion depth 662 U32 depth; // recursion depth
611 U32 maxdepth; // recursion depth limit 663 U32 maxdepth; // recursion depth limit
612 AV *shareable; 664 AV *shareable;
613 AV *stringref; 665 AV *stringref;
614 SV *decode_tagged; 666 SV *decode_tagged;
667 SV *err_sv; // optional sv for error, needs to be freed
615} dec_t; 668} dec_t;
616 669
617#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 670// set dec->err to ERRSV
671ecb_cold static void
672err_errsv (dec_t *dec)
673{
674 if (!dec->err)
675 {
676 dec->err_sv = newSVsv (ERRSV);
618 677
619#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 678 // chop off the trailing \n
679 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
680 *SvEND (dec->err_sv) = 0;
620 681
682 dec->err = SvPVutf8_nolen (dec->err_sv);
683 }
684}
685
686// the following functions are used to reduce code size and help the compiler to optimise
687ecb_cold static void
688err_set (dec_t *dec, const char *reason)
689{
690 if (!dec->err)
691 dec->err = reason;
692}
693
694ecb_cold static void
695err_unexpected_end (dec_t *dec)
696{
697 err_set (dec, "unexpected end of CBOR data");
698}
699
700ecb_cold static void
701err_nesting_exceeded (dec_t *dec)
702{
703 err_set (dec, ERR_NESTING_EXCEEDED);
704}
705
706#define ERR_DO(do) SB do; goto fail; SE
707#define ERR(reason) ERR_DO (err_set (dec, reason))
708#define ERR_ERRSV ERR_DO (err_errsv (dec))
709
710#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
711
621#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 712#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
622#define DEC_DEC_DEPTH --dec->depth 713#define DEC_DEC_DEPTH --dec->depth
623 714
624static UV 715static UV
625decode_uint (dec_t *dec) 716decode_uint (dec_t *dec)
626{ 717{
627 U8 m = *dec->cur & MINOR_MASK; 718 U8 m = *dec->cur & MINOR_MASK;
628 ++dec->cur; 719 ++dec->cur;
629 720
630 if (ecb_expect_true (m < LENGTH_EXT1)) 721 if (ecb_expect_true (m < LENGTH_EXT1))
631 return m; 722 return m;
632 723 else if (ecb_expect_true (m == LENGTH_EXT1))
633 switch (m)
634 { 724 {
635 case LENGTH_EXT1:
636 WANT (1); 725 WANT (1);
637 dec->cur += 1; 726 dec->cur += 1;
638 return dec->cur[-1]; 727 return dec->cur[-1];
639 728 }
640 case LENGTH_EXT2: 729 else if (ecb_expect_true (m == LENGTH_EXT2))
730 {
641 WANT (2); 731 WANT (2);
642 dec->cur += 2; 732 dec->cur += 2;
643 return (((UV)dec->cur[-2]) << 8) 733 return (((UV)dec->cur[-2]) << 8)
644 | ((UV)dec->cur[-1]); 734 | ((UV)dec->cur[-1]);
645 735 }
646 case LENGTH_EXT4: 736 else if (ecb_expect_true (m == LENGTH_EXT4))
737 {
647 WANT (4); 738 WANT (4);
648 dec->cur += 4; 739 dec->cur += 4;
649 return (((UV)dec->cur[-4]) << 24) 740 return (((UV)dec->cur[-4]) << 24)
650 | (((UV)dec->cur[-3]) << 16) 741 | (((UV)dec->cur[-3]) << 16)
651 | (((UV)dec->cur[-2]) << 8) 742 | (((UV)dec->cur[-2]) << 8)
652 | ((UV)dec->cur[-1]); 743 | ((UV)dec->cur[-1]);
653 744 }
654 case LENGTH_EXT8: 745 else if (ecb_expect_true (m == LENGTH_EXT8))
746 {
655 WANT (8); 747 WANT (8);
656 dec->cur += 8; 748 dec->cur += 8;
657 749
658 return 750 return
659#if UVSIZE < 8 751#if UVSIZE < 8
660 0 752 0
661#else 753#else
662 (((UV)dec->cur[-8]) << 56) 754 (((UV)dec->cur[-8]) << 56)
663 | (((UV)dec->cur[-7]) << 48) 755 | (((UV)dec->cur[-7]) << 48)
664 | (((UV)dec->cur[-6]) << 40) 756 | (((UV)dec->cur[-6]) << 40)
665 | (((UV)dec->cur[-5]) << 32) 757 | (((UV)dec->cur[-5]) << 32)
666#endif 758#endif
667 | (((UV)dec->cur[-4]) << 24) 759 | (((UV)dec->cur[-4]) << 24)
668 | (((UV)dec->cur[-3]) << 16) 760 | (((UV)dec->cur[-3]) << 16)
669 | (((UV)dec->cur[-2]) << 8) 761 | (((UV)dec->cur[-2]) << 8)
670 | ((UV)dec->cur[-1]); 762 | ((UV)dec->cur[-1]);
671 763 }
672 default: 764 else
673 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 765 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
674 }
675 766
676fail: 767fail:
677 return 0; 768 return 0;
678} 769}
679 770
703 av_push (av, decode_sv (dec)); 794 av_push (av, decode_sv (dec));
704 } 795 }
705 } 796 }
706 else 797 else
707 { 798 {
708 int i, len = decode_uint (dec); 799 UV i, len = decode_uint (dec);
709 800
801 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); 802 av_fill (av, len - 1);
711 803
712 for (i = 0; i < len; ++i) 804 for (i = 0; i < len; ++i)
713 AvARRAY (av)[i] = decode_sv (dec); 805 AvARRAY (av)[i] = decode_sv (dec);
714 } 806 }
727{ 819{
728 // for speed reasons, we specialcase single-string 820 // for speed reasons, we specialcase single-string
729 // byte or utf-8 strings as keys, but only when !stringref 821 // byte or utf-8 strings as keys, but only when !stringref
730 822
731 if (ecb_expect_true (!dec->stringref)) 823 if (ecb_expect_true (!dec->stringref))
732 if ((*dec->cur - MAJOR_BYTES) <= 27) 824 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
733 { 825 {
734 I32 len = decode_uint (dec); 826 STRLEN len = decode_uint (dec);
735 char *key = (char *)dec->cur; 827 char *key = (char *)dec->cur;
736 828
829 WANT (len);
737 dec->cur += len; 830 dec->cur += len;
738 831
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); 832 hv_store (hv, key, len, decode_sv (dec), 0);
743 833
744 return; 834 return;
745 } 835 }
746 else if ((*dec->cur - MAJOR_TEXT) <= 27) 836 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 837 {
748 I32 len = decode_uint (dec); 838 STRLEN len = decode_uint (dec);
749 char *key = (char *)dec->cur; 839 char *key = (char *)dec->cur;
750 840
841 WANT (len);
751 dec->cur += len; 842 dec->cur += len;
752 843
753 if (ecb_expect_false (dec->stringref)) 844 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 845 if (!is_utf8_string (key, len))
846 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
755 847
756 hv_store (hv, key, -len, decode_sv (dec), 0); 848 hv_store (hv, key, -len, decode_sv (dec), 0);
757 849
758 return; 850 return;
759 } 851 }
760 852
761 SV *k = decode_sv (dec); 853 SV *k = decode_sv (dec);
762 SV *v = decode_sv (dec); 854 SV *v = decode_sv (dec);
763 855
856 // we leak memory if uncaught exceptions are thrown by random magical
857 // methods, and this is hopefully the only place where it can happen,
858 // so if there is a chance of an exception, take the very slow path.
859 // since catching exceptions is "undocumented/internal/forbidden" by
860 // the new p5p powers, we need to call out to a perl function :/
861 if (ecb_expect_false (SvAMAGIC (k)))
862 {
863 dSP;
864
865 ENTER; SAVETMPS;
866 PUSHMARK (SP);
867 EXTEND (SP, 3);
868 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
869 PUSHs (sv_2mortal (k));
870 PUSHs (sv_2mortal (v));
871
872 PUTBACK;
873 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
874 SPAGAIN;
875
876 FREETMPS; LEAVE;
877
878 if (SvTRUE (ERRSV))
879 ERR_ERRSV;
880
881 return;
882 }
883
764 hv_store_ent (hv, k, v, 0); 884 hv_store_ent (hv, k, v, 0);
765 SvREFCNT_dec (k); 885 SvREFCNT_dec (k);
886
887fail:
888 ;
766} 889}
767 890
768static SV * 891static SV *
769decode_hv (dec_t *dec) 892decode_hv (dec_t *dec)
770{ 893{
789 decode_he (dec, hv); 912 decode_he (dec, hv);
790 } 913 }
791 } 914 }
792 else 915 else
793 { 916 {
794 int pairs = decode_uint (dec); 917 UV pairs = decode_uint (dec);
918
919 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
795 920
796 while (pairs--) 921 while (pairs--)
797 decode_he (dec, hv); 922 decode_he (dec, hv);
798 } 923 }
799 924
852 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 977 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
853 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 978 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
854 } 979 }
855 980
856 if (utf8) 981 if (utf8)
982 {
983 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
984 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
985 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
986
857 SvUTF8_on (sv); 987 SvUTF8_on (sv);
988 }
858 989
859 return sv; 990 return sv;
860 991
861fail: 992fail:
862 SvREFCNT_dec (sv); 993 SvREFCNT_dec (sv);
881 sv = newRV_noinc (decode_sv (dec)); 1012 sv = newRV_noinc (decode_sv (dec));
882 break; 1013 break;
883 1014
884 case CBOR_TAG_STRINGREF_NAMESPACE: 1015 case CBOR_TAG_STRINGREF_NAMESPACE:
885 { 1016 {
1017 // do nmot use SAVETMPS/FREETMPS, as these will
1018 // erase mortalised caches, e.g. "shareable"
886 ENTER; SAVETMPS; 1019 ENTER;
887 1020
888 SAVESPTR (dec->stringref); 1021 SAVESPTR (dec->stringref);
889 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1022 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
890 1023
891 sv = decode_sv (dec); 1024 sv = decode_sv (dec);
892 1025
893 FREETMPS; LEAVE; 1026 LEAVE;
894 } 1027 }
895 break; 1028 break;
896 1029
897 case CBOR_TAG_STRINGREF: 1030 case CBOR_TAG_STRINGREF:
898 { 1031 {
911 case CBOR_TAG_VALUE_SHAREABLE: 1044 case CBOR_TAG_VALUE_SHAREABLE:
912 { 1045 {
913 if (ecb_expect_false (!dec->shareable)) 1046 if (ecb_expect_false (!dec->shareable))
914 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1047 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
915 1048
1049 if (dec->cbor.flags & F_ALLOW_CYCLES)
1050 {
916 sv = newSV (0); 1051 sv = newSV (0);
917 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1052 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
918 1053
919 SV *osv = decode_sv (dec); 1054 SV *osv = decode_sv (dec);
920 sv_setsv (sv, osv); 1055 sv_setsv (sv, osv);
921 SvREFCNT_dec_NN (osv); 1056 SvREFCNT_dec_NN (osv);
1057 }
1058 else
1059 {
1060 av_push (dec->shareable, &PL_sv_undef);
1061 int idx = AvFILLp (dec->shareable);
1062 sv = decode_sv (dec);
1063 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
1064 }
922 } 1065 }
923 break; 1066 break;
924 1067
925 case CBOR_TAG_VALUE_SHAREDREF: 1068 case CBOR_TAG_VALUE_SHAREDREF:
926 { 1069 {
931 1074
932 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1075 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
933 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1076 ERR ("corrupted CBOR data (sharedref index out of bounds)");
934 1077
935 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1078 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1079
1080 if (sv == &PL_sv_undef)
1081 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
936 } 1082 }
937 break; 1083 break;
938 1084
939 case CBOR_TAG_PERL_OBJECT: 1085 case CBOR_TAG_PERL_OBJECT:
940 { 1086 {
1087 if (dec->cbor.flags & F_FORBID_OBJECTS)
1088 goto filter;
1089
941 sv = decode_sv (dec); 1090 sv = decode_sv (dec);
942 1091
943 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 1092 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
944 ERR ("corrupted CBOR data (non-array perl object)"); 1093 ERR ("corrupted CBOR data (non-array perl object)");
945 1094
955 if (!method) 1104 if (!method)
956 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1105 ERR ("cannot decode perl-object (package does not have a THAW method)");
957 1106
958 dSP; 1107 dSP;
959 1108
960 ENTER; SAVETMPS; PUSHMARK (SP); 1109 ENTER; SAVETMPS;
1110 PUSHMARK (SP);
961 EXTEND (SP, len + 1); 1111 EXTEND (SP, len + 1);
962 // we re-bless the reference to get overload and other niceties right 1112 // we re-bless the reference to get overload and other niceties right
963 PUSHs (*av_fetch (av, 0, 1)); 1113 PUSHs (*av_fetch (av, 0, 1));
964 PUSHs (sv_cbor); 1114 PUSHs (sv_cbor);
965 1115
973 SPAGAIN; 1123 SPAGAIN;
974 1124
975 if (SvTRUE (ERRSV)) 1125 if (SvTRUE (ERRSV))
976 { 1126 {
977 FREETMPS; LEAVE; 1127 FREETMPS; LEAVE;
978 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1128 ERR_ERRSV;
979 } 1129 }
980 1130
981 SvREFCNT_dec (sv); 1131 SvREFCNT_dec (sv);
982 sv = SvREFCNT_inc (POPs); 1132 sv = SvREFCNT_inc (POPs);
983 1133
986 FREETMPS; LEAVE; 1136 FREETMPS; LEAVE;
987 } 1137 }
988 break; 1138 break;
989 1139
990 default: 1140 default:
1141 filter:
991 { 1142 {
1143 SV *tag_sv = newSVuv (tag);
1144
992 sv = decode_sv (dec); 1145 sv = decode_sv (dec);
993 1146
994 dSP; 1147 dSP;
995 ENTER; SAVETMPS; PUSHMARK (SP); 1148 ENTER; SAVETMPS;
1149 SAVESTACK_POS ();
1150 PUSHMARK (SP);
996 EXTEND (SP, 2); 1151 EXTEND (SP, 2);
997 PUSHs (newSVuv (tag)); 1152 PUSHs (tag_sv);
998 PUSHs (sv); 1153 PUSHs (sv);
999 1154
1000 PUTBACK; 1155 PUTBACK;
1001 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1156 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1002 SPAGAIN; 1157 SPAGAIN;
1003 1158
1004 if (SvTRUE (ERRSV)) 1159 if (SvTRUE (ERRSV))
1005 { 1160 {
1161 SvREFCNT_dec (tag_sv);
1006 FREETMPS; LEAVE; 1162 FREETMPS; LEAVE;
1007 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1163 ERR_ERRSV;
1008 } 1164 }
1009 1165
1010 if (count) 1166 if (count)
1011 { 1167 {
1168 SvREFCNT_dec (tag_sv);
1012 SvREFCNT_dec (sv); 1169 SvREFCNT_dec (sv);
1013 sv = SvREFCNT_inc (POPs); 1170 sv = SvREFCNT_inc (POPs);
1014 } 1171 }
1015 else 1172 else
1016 { 1173 {
1017 AV *av = newAV (); 1174 AV *av = newAV ();
1018 av_push (av, newSVuv (tag)); 1175 av_push (av, tag_sv);
1019 av_push (av, sv); 1176 av_push (av, sv);
1020 1177
1021 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1178 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1022 ? cbor_tagged_stash 1179 ? cbor_tagged_stash
1023 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1180 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1109 1266
1110 return newSVnv (ecb_binary64_to_double (fp)); 1267 return newSVnv (ecb_binary64_to_double (fp));
1111 } 1268 }
1112 1269
1113 // 0..19 unassigned simple 1270 // 0..19 unassigned simple
1114 // 24 reserved + unassigned (reserved values are not encodable) 1271 // 24 reserved + unassigned simple (reserved values are not encodable)
1272 // 28-30 unassigned misc
1273 // 31 break code
1115 default: 1274 default:
1116 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1275 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1117 } 1276 }
1118 1277
1119 break; 1278 break;
1120 } 1279 }
1121 1280
1124} 1283}
1125 1284
1126static SV * 1285static SV *
1127decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1286decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1128{ 1287{
1129 dec_t dec = { }; 1288 dec_t dec = { 0 };
1130 SV *sv; 1289 SV *sv;
1131 STRLEN len; 1290 STRLEN len;
1132 char *data = SvPVbyte (string, len); 1291 char *data = SvPVbyte (string, len);
1133 1292
1134 if (len > cbor->max_size && cbor->max_size) 1293 if (len > cbor->max_size && cbor->max_size)
1148 if (dec.cur != dec.end && !dec.err) 1307 if (dec.cur != dec.end && !dec.err)
1149 dec.err = "garbage after CBOR object"; 1308 dec.err = "garbage after CBOR object";
1150 1309
1151 if (dec.err) 1310 if (dec.err)
1152 { 1311 {
1312 if (dec.shareable)
1313 {
1314 // need to break cyclic links, which would all be in shareable
1315 int i;
1316 SV **svp;
1317
1318 for (i = av_len (dec.shareable) + 1; i--; )
1319 if ((svp = av_fetch (dec.shareable, i, 0)))
1320 sv_setsv (*svp, &PL_sv_undef);
1321 }
1322
1153 SvREFCNT_dec (sv); 1323 SvREFCNT_dec (sv);
1324
1325 if (dec.err_sv)
1326 sv_2mortal (dec.err_sv);
1327
1154 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1328 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1155 } 1329 }
1156 1330
1157 sv = sv_2mortal (sv); 1331 sv = sv_2mortal (sv);
1158 1332
1159 return sv; 1333 return sv;
1160} 1334}
1161 1335
1336/////////////////////////////////////////////////////////////////////////////
1337// incremental parser
1338
1339#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1340
1341// returns 0 for notyet, 1 for success or error
1342static int
1343incr_parse (CBOR *self, SV *cborstr)
1344{
1345 STRLEN cur;
1346 SvPV (cborstr, cur);
1347
1348 while (ecb_expect_true (self->incr_need <= cur))
1349 {
1350 // table of integer count bytes
1351 static I8 incr_len[MINOR_MASK + 1] = {
1352 0, 0, 0, 0, 0, 0, 0, 0,
1353 0, 0, 0, 0, 0, 0, 0, 0,
1354 0, 0, 0, 0, 0, 0, 0, 0,
1355 1, 2, 4, 8,-1,-1,-1,-2
1356 };
1357
1358 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1359 U8 m = *p & MINOR_MASK;
1360 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1361 I8 ilen = incr_len[m];
1362
1363 self->incr_need = self->incr_pos + 1;
1364
1365 if (ecb_expect_false (ilen < 0))
1366 {
1367 if (m != MINOR_INDEF)
1368 return 1; // error
1369
1370 if (*p == (MAJOR_MISC | MINOR_INDEF))
1371 {
1372 if (count >= 0)
1373 return 1; // error
1374
1375 count = 1;
1376 }
1377 else
1378 {
1379 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1380 count = -1;
1381 }
1382 }
1383 else
1384 {
1385 self->incr_need += ilen;
1386 if (ecb_expect_false (self->incr_need > cur))
1387 return 0;
1388
1389 int major = *p >> MAJOR_SHIFT;
1390
1391 switch (major)
1392 {
1393 case MAJOR_TAG >> MAJOR_SHIFT:
1394 ++count; // tags merely prefix another value
1395 break;
1396
1397 case MAJOR_BYTES >> MAJOR_SHIFT:
1398 case MAJOR_TEXT >> MAJOR_SHIFT:
1399 case MAJOR_ARRAY >> MAJOR_SHIFT:
1400 case MAJOR_MAP >> MAJOR_SHIFT:
1401 {
1402 UV len;
1403
1404 if (ecb_expect_false (ilen))
1405 {
1406 len = 0;
1407
1408 do {
1409 len = (len << 8) | *++p;
1410 } while (--ilen);
1411 }
1412 else
1413 len = m;
1414
1415 switch (major)
1416 {
1417 case MAJOR_BYTES >> MAJOR_SHIFT:
1418 case MAJOR_TEXT >> MAJOR_SHIFT:
1419 self->incr_need += len;
1420 if (ecb_expect_false (self->incr_need > cur))
1421 return 0;
1422
1423 break;
1424
1425 case MAJOR_MAP >> MAJOR_SHIFT:
1426 len <<= 1;
1427 case MAJOR_ARRAY >> MAJOR_SHIFT:
1428 if (len)
1429 {
1430 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1431 count = len + 1;
1432 }
1433 break;
1434 }
1435 }
1436 }
1437 }
1438
1439 self->incr_pos = self->incr_need;
1440
1441 if (count > 0)
1442 {
1443 while (!--count)
1444 {
1445 if (!AvFILLp (self->incr_count))
1446 return 1; // done
1447
1448 SvREFCNT_dec_NN (av_pop (self->incr_count));
1449 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1450 }
1451
1452 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1453 }
1454 }
1455
1456 return 0;
1457}
1458
1459
1162///////////////////////////////////////////////////////////////////////////// 1460/////////////////////////////////////////////////////////////////////////////
1163// XS interface functions 1461// XS interface functions
1164 1462
1165MODULE = CBOR::XS PACKAGE = CBOR::XS 1463MODULE = CBOR::XS PACKAGE = CBOR::XS
1166 1464
1178 1476
1179 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1477 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1180 1478
1181 sv_cbor = newSVpv ("CBOR", 0); 1479 sv_cbor = newSVpv ("CBOR", 0);
1182 SvREADONLY_on (sv_cbor); 1480 SvREADONLY_on (sv_cbor);
1481
1482 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1183} 1483}
1184 1484
1185PROTOTYPES: DISABLE 1485PROTOTYPES: DISABLE
1186 1486
1187void CLONE (...) 1487void CLONE (...)
1206void shrink (CBOR *self, int enable = 1) 1506void shrink (CBOR *self, int enable = 1)
1207 ALIAS: 1507 ALIAS:
1208 shrink = F_SHRINK 1508 shrink = F_SHRINK
1209 allow_unknown = F_ALLOW_UNKNOWN 1509 allow_unknown = F_ALLOW_UNKNOWN
1210 allow_sharing = F_ALLOW_SHARING 1510 allow_sharing = F_ALLOW_SHARING
1511 allow_cycles = F_ALLOW_CYCLES
1512 forbid_objects = F_FORBID_OBJECTS
1211 pack_strings = F_PACK_STRINGS 1513 pack_strings = F_PACK_STRINGS
1514 text_keys = F_TEXT_KEYS
1515 text_strings = F_TEXT_STRINGS
1516 validate_utf8 = F_VALIDATE_UTF8
1212 PPCODE: 1517 PPCODE:
1213{ 1518{
1214 if (enable) 1519 if (enable)
1215 self->flags |= ix; 1520 self->flags |= ix;
1216 else 1521 else
1222void get_shrink (CBOR *self) 1527void get_shrink (CBOR *self)
1223 ALIAS: 1528 ALIAS:
1224 get_shrink = F_SHRINK 1529 get_shrink = F_SHRINK
1225 get_allow_unknown = F_ALLOW_UNKNOWN 1530 get_allow_unknown = F_ALLOW_UNKNOWN
1226 get_allow_sharing = F_ALLOW_SHARING 1531 get_allow_sharing = F_ALLOW_SHARING
1532 get_allow_cycles = F_ALLOW_CYCLES
1533 get_forbid_objects = F_FORBID_OBJECTS
1227 get_pack_strings = F_PACK_STRINGS 1534 get_pack_strings = F_PACK_STRINGS
1535 get_text_keys = F_TEXT_KEYS
1536 get_text_strings = F_TEXT_STRINGS
1537 get_validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1538 PPCODE:
1229 XPUSHs (boolSV (self->flags & ix)); 1539 XPUSHs (boolSV (self->flags & ix));
1230 1540
1231void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1541void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1232 PPCODE: 1542 PPCODE:
1281 EXTEND (SP, 2); 1591 EXTEND (SP, 2);
1282 PUSHs (sv); 1592 PUSHs (sv);
1283 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1593 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1284} 1594}
1285 1595
1596void incr_parse (CBOR *self, SV *cborstr)
1597 ALIAS:
1598 incr_parse_multiple = 1
1599 PPCODE:
1600{
1601 if (SvUTF8 (cborstr))
1602 sv_utf8_downgrade (cborstr, 0);
1603
1604 if (!self->incr_count)
1605 {
1606 self->incr_count = newAV ();
1607 self->incr_pos = 0;
1608 self->incr_need = 1;
1609
1610 av_push (self->incr_count, newSViv (1));
1611 }
1612
1613 do
1614 {
1615 if (!incr_parse (self, cborstr))
1616 {
1617 if (self->incr_need > self->max_size && self->max_size)
1618 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1619 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1620
1621 break;
1622 }
1623
1624 SV *sv;
1625 char *offset;
1626
1627 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1628 XPUSHs (sv);
1629
1630 sv_chop (cborstr, offset);
1631
1632 av_clear (self->incr_count);
1633 av_push (self->incr_count, newSViv (1));
1634
1635 self->incr_pos = 0;
1636 self->incr_need = self->incr_pos + 1;
1637 }
1638 while (ix);
1639}
1640
1641void incr_reset (CBOR *self)
1642 CODE:
1643{
1644 SvREFCNT_dec (self->incr_count);
1645 self->incr_count = 0;
1646}
1647
1286void DESTROY (CBOR *self) 1648void DESTROY (CBOR *self)
1287 PPCODE: 1649 PPCODE:
1288 cbor_free (self); 1650 cbor_free (self);
1289 1651
1290PROTOTYPES: ENABLE 1652PROTOTYPES: ENABLE
1291 1653
1292void encode_cbor (SV *scalar) 1654void encode_cbor (SV *scalar)
1655 ALIAS:
1656 encode_cbor = 0
1657 encode_cbor_sharing = F_ALLOW_SHARING
1293 PPCODE: 1658 PPCODE:
1294{ 1659{
1295 CBOR cbor; 1660 CBOR cbor;
1296 cbor_init (&cbor); 1661 cbor_init (&cbor);
1662 cbor.flags |= ix;
1297 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1663 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1298 XPUSHs (scalar); 1664 XPUSHs (scalar);
1299} 1665}
1300 1666
1301void decode_cbor (SV *cborstr) 1667void decode_cbor (SV *cborstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines