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.39 by root, Sun Dec 1 14:45:03 2013 UTC vs.
Revision 1.61 by root, Sat Nov 26 02:10:19 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))
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
102#define F_ALLOW_CYCLES 0x00000008UL 104#define F_ALLOW_CYCLES 0x00000008UL
105#define F_FORBID_OBJECTS 0x00000010UL
103#define F_PACK_STRINGS 0x00000010UL 106#define F_PACK_STRINGS 0x00000020UL
107#define F_TEXT_KEYS 0x00000040UL
108#define F_TEXT_STRINGS 0x00000080UL
104#define F_VALIDATE_UTF8 0x00000020UL 109#define F_VALIDATE_UTF8 0x00000100UL
105 110
106#define INIT_SIZE 32 // initial scalar size to be allocated 111#define INIT_SIZE 32 // initial scalar size to be allocated
107 112
108#define SB do { 113#define SB do {
109#define SE } while (0) 114#define SE } while (0)
128typedef struct { 133typedef struct {
129 U32 flags; 134 U32 flags;
130 U32 max_depth; 135 U32 max_depth;
131 STRLEN max_size; 136 STRLEN max_size;
132 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.
133} CBOR; 143} CBOR;
134 144
135ecb_inline void 145ecb_inline void
136cbor_init (CBOR *cbor) 146cbor_init (CBOR *cbor)
137{ 147{
141 151
142ecb_inline void 152ecb_inline void
143cbor_free (CBOR *cbor) 153cbor_free (CBOR *cbor)
144{ 154{
145 SvREFCNT_dec (cbor->filter); 155 SvREFCNT_dec (cbor->filter);
156 SvREFCNT_dec (cbor->incr_count);
146} 157}
147 158
148///////////////////////////////////////////////////////////////////////////// 159/////////////////////////////////////////////////////////////////////////////
149// utility functions 160// utility functions
150 161
176 187
177// minimum length of a string to be registered for stringref 188// minimum length of a string to be registered for stringref
178ecb_inline int 189ecb_inline int
179minimum_string_length (UV idx) 190minimum_string_length (UV idx)
180{ 191{
181 return idx > 23 192
182 ? idx > 0xffU 193 return idx <= 23 ? 3
183 ? idx > 0xffffU 194 : idx <= 0xffU ? 4
195 : idx <= 0xffffU ? 5
184 ? idx > 0xffffffffU 196 : idx <= 0xffffffffU ? 7
185 ? 11 197 : 11;
186 : 7
187 : 5
188 : 4
189 : 3;
190} 198}
191 199
192///////////////////////////////////////////////////////////////////////////// 200/////////////////////////////////////////////////////////////////////////////
193// encoder 201// encoder
194 202
207} enc_t; 215} enc_t;
208 216
209ecb_inline void 217ecb_inline void
210need (enc_t *enc, STRLEN len) 218need (enc_t *enc, STRLEN len)
211{ 219{
212 if (ecb_expect_false (enc->cur + len >= enc->end)) 220 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
213 { 221 {
214 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 222 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
215 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 223 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
216 enc->cur = SvPVX (enc->sv) + cur; 224 enc->cur = SvPVX (enc->sv) + cur;
217 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 225 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
269encode_tag (enc_t *enc, UV tag) 277encode_tag (enc_t *enc, UV tag)
270{ 278{
271 encode_uint (enc, MAJOR_TAG, tag); 279 encode_uint (enc, MAJOR_TAG, tag);
272} 280}
273 281
282// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
283ecb_noinline static void
284encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
285{
286 STRLEN ulen = len;
287 U8 *p, *pend = (U8 *)str + len;
288
289 for (p = (U8 *)str; p < pend; ++p)
290 ulen += *p >> 7; // count set high bits
291
292 encode_uint (enc, MAJOR_TEXT, ulen);
293
294 need (enc, ulen);
295 for (p = (U8 *)str; p < pend; ++p)
296 if (*p < 0x80)
297 *enc->cur++ = *p;
298 else
299 {
300 *enc->cur++ = 0xc0 + (*p >> 6);
301 *enc->cur++ = 0x80 + (*p & 63);
302 }
303}
304
274ecb_inline void 305ecb_inline void
275encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 306encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
276{ 307{
308 if (ecb_expect_false (upgrade_utf8))
309 if (!utf8)
310 {
311 encode_str_utf8 (enc, utf8, str, len);
312 return;
313 }
314
277 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 315 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
278 need (enc, len); 316 need (enc, len);
279 memcpy (enc->cur, str, len); 317 memcpy (enc->cur, str, len);
280 enc->cur += len; 318 enc->cur += len;
281} 319}
282 320
283static void 321ecb_inline void
284encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 322encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
285{ 323{
286 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 324 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
287 { 325 {
288 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 326 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
289 327
300 sv_setuv (*svp, enc->stringref_idx); 338 sv_setuv (*svp, enc->stringref_idx);
301 ++enc->stringref_idx; 339 ++enc->stringref_idx;
302 } 340 }
303 } 341 }
304 342
305 encode_str (enc, utf8, str, len); 343 encode_str (enc, upgrade_utf8, utf8, str, len);
306} 344}
307 345
308static void encode_sv (enc_t *enc, SV *sv); 346static void encode_sv (enc_t *enc, SV *sv);
309 347
310static void 348static void
317 355
318 ++enc->depth; 356 ++enc->depth;
319 357
320 encode_uint (enc, MAJOR_ARRAY, len + 1); 358 encode_uint (enc, MAJOR_ARRAY, len + 1);
321 359
360 if (SvMAGICAL (av))
322 for (i = 0; i <= len; ++i) 361 for (i = 0; i <= len; ++i)
323 { 362 {
324 SV **svp = av_fetch (av, i, 0); 363 SV **svp = av_fetch (av, i, 0);
325 encode_sv (enc, svp ? *svp : &PL_sv_undef); 364 encode_sv (enc, svp ? *svp : &PL_sv_undef);
326 } 365 }
366 else
367 for (i = 0; i <= len; ++i)
368 {
369 SV *sv = AvARRAY (av)[i];
370 encode_sv (enc, sv ? sv : &PL_sv_undef);
371 }
327 372
328 --enc->depth; 373 --enc->depth;
329} 374}
330 375
331static void 376static void
349 while ((he = hv_iternext (hv))) 394 while ((he = hv_iternext (hv)))
350 { 395 {
351 if (HeKLEN (he) == HEf_SVKEY) 396 if (HeKLEN (he) == HEf_SVKEY)
352 encode_sv (enc, HeSVKEY (he)); 397 encode_sv (enc, HeSVKEY (he));
353 else 398 else
354 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 399 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
355 400
356 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 401 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
357 } 402 }
358 403
359 if (mg) 404 if (mg)
431 if (ecb_expect_false (SvOBJECT (sv))) 476 if (ecb_expect_false (SvOBJECT (sv)))
432 { 477 {
433 HV *stash = SvSTASH (sv); 478 HV *stash = SvSTASH (sv);
434 GV *method; 479 GV *method;
435 480
481 if (enc->cbor.flags & F_FORBID_OBJECTS)
482 croak ("encountered object '%s', but forbid_objects is enabled",
483 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
436 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 484 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
437 { 485 {
438 dSP; 486 dSP;
439 487
440 ENTER; SAVETMPS; PUSHMARK (SP); 488 ENTER; SAVETMPS;
489 PUSHMARK (SP);
441 // we re-bless the reference to get overload and other niceties right 490 // we re-bless the reference to get overload and other niceties right
442 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 491 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
443 492
444 PUTBACK; 493 PUTBACK;
445 // G_SCALAR ensures that return value is 1 494 // G_SCALAR ensures that return value is 1
458 } 507 }
459 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 508 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
460 { 509 {
461 dSP; 510 dSP;
462 511
463 ENTER; SAVETMPS; PUSHMARK (SP); 512 ENTER; SAVETMPS;
513 SAVESTACK_POS ();
514 PUSHMARK (SP);
464 EXTEND (SP, 2); 515 EXTEND (SP, 2);
465 // we re-bless the reference to get overload and other niceties right 516 // we re-bless the reference to get overload and other niceties right
466 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 517 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
467 PUSHs (sv_cbor); 518 PUSHs (sv_cbor);
468 519
474 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 525 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
475 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 526 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
476 527
477 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 528 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
478 encode_uint (enc, MAJOR_ARRAY, count + 1); 529 encode_uint (enc, MAJOR_ARRAY, count + 1);
479 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 530 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
480 531
481 while (count) 532 while (count)
482 encode_sv (enc, SP[1 - count--]); 533 encode_sv (enc, SP[1 - count--]);
483 534
484 PUTBACK; 535 PUTBACK;
543 594
544 if (SvPOKp (sv)) 595 if (SvPOKp (sv))
545 { 596 {
546 STRLEN len; 597 STRLEN len;
547 char *str = SvPV (sv, len); 598 char *str = SvPV (sv, len);
548 encode_strref (enc, SvUTF8 (sv), str, len); 599 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
549 } 600 }
550 else if (SvNOKp (sv)) 601 else if (SvNOKp (sv))
551 encode_nv (enc, sv); 602 encode_nv (enc, sv);
552 else if (SvIOKp (sv)) 603 else if (SvIOKp (sv))
553 { 604 {
570} 621}
571 622
572static SV * 623static SV *
573encode_cbor (SV *scalar, CBOR *cbor) 624encode_cbor (SV *scalar, CBOR *cbor)
574{ 625{
575 enc_t enc = { }; 626 enc_t enc = { 0 };
576 627
577 enc.cbor = *cbor; 628 enc.cbor = *cbor;
578 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 629 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
579 enc.cur = SvPVX (enc.sv); 630 enc.cur = SvPVX (enc.sv);
580 enc.end = SvEND (enc.sv); 631 enc.end = SvEND (enc.sv);
612 U32 depth; // recursion depth 663 U32 depth; // recursion depth
613 U32 maxdepth; // recursion depth limit 664 U32 maxdepth; // recursion depth limit
614 AV *shareable; 665 AV *shareable;
615 AV *stringref; 666 AV *stringref;
616 SV *decode_tagged; 667 SV *decode_tagged;
668 SV *err_sv; // optional sv for error, needs to be freed
617} dec_t; 669} dec_t;
618 670
619#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 671// set dec->err to ERRSV
672ecb_cold static void
673err_errsv (dec_t *dec)
674{
675 if (!dec->err)
676 {
677 dec->err_sv = newSVsv (ERRSV);
620 678
621#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 679 // chop off the trailing \n
680 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
681 *SvEND (dec->err_sv) = 0;
622 682
683 dec->err = SvPVutf8_nolen (dec->err_sv);
684 }
685}
686
687// the following functions are used to reduce code size and help the compiler to optimise
688ecb_cold static void
689err_set (dec_t *dec, const char *reason)
690{
691 if (!dec->err)
692 dec->err = reason;
693}
694
695ecb_cold static void
696err_unexpected_end (dec_t *dec)
697{
698 err_set (dec, "unexpected end of CBOR data");
699}
700
701ecb_cold static void
702err_nesting_exceeded (dec_t *dec)
703{
704 err_set (dec, ERR_NESTING_EXCEEDED);
705}
706
707#define ERR_DO(do) SB do; goto fail; SE
708#define ERR(reason) ERR_DO (err_set (dec, reason))
709#define ERR_ERRSV ERR_DO (err_errsv (dec))
710
711#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
712
623#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 713#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
624#define DEC_DEC_DEPTH --dec->depth 714#define DEC_DEC_DEPTH --dec->depth
625 715
626static UV 716static UV
627decode_uint (dec_t *dec) 717decode_uint (dec_t *dec)
628{ 718{
705 av_push (av, decode_sv (dec)); 795 av_push (av, decode_sv (dec));
706 } 796 }
707 } 797 }
708 else 798 else
709 { 799 {
710 int i, len = decode_uint (dec); 800 UV i, len = decode_uint (dec);
711 801
712 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays 802 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
713 av_fill (av, len - 1); 803 av_fill (av, len - 1);
714 804
715 for (i = 0; i < len; ++i) 805 for (i = 0; i < len; ++i)
730{ 820{
731 // for speed reasons, we specialcase single-string 821 // for speed reasons, we specialcase single-string
732 // byte or utf-8 strings as keys, but only when !stringref 822 // byte or utf-8 strings as keys, but only when !stringref
733 823
734 if (ecb_expect_true (!dec->stringref)) 824 if (ecb_expect_true (!dec->stringref))
735 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 825 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
736 { 826 {
737 I32 len = decode_uint (dec); 827 STRLEN len = decode_uint (dec);
738 char *key = (char *)dec->cur; 828 char *key = (char *)dec->cur;
739 829
830 WANT (len);
740 dec->cur += len; 831 dec->cur += len;
741 832
742 hv_store (hv, key, len, decode_sv (dec), 0); 833 hv_store (hv, key, len, decode_sv (dec), 0);
743 834
744 return; 835 return;
745 } 836 }
746 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 837 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 838 {
748 I32 len = decode_uint (dec); 839 STRLEN len = decode_uint (dec);
749 char *key = (char *)dec->cur; 840 char *key = (char *)dec->cur;
750 841
842 WANT (len);
751 dec->cur += len; 843 dec->cur += len;
752 844
753 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 845 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 if (!is_utf8_string (key, len)) 846 if (!is_utf8_string (key, len))
755 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 847 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
759 return; 851 return;
760 } 852 }
761 853
762 SV *k = decode_sv (dec); 854 SV *k = decode_sv (dec);
763 SV *v = decode_sv (dec); 855 SV *v = decode_sv (dec);
856
857 // we leak memory if uncaught exceptions are thrown by random magical
858 // methods, and this is hopefully the only place where it can happen,
859 // so if there is a chance of an exception, take the very slow path.
860 // since catching exceptions is "undocumented/internal/forbidden" by
861 // the new p5p powers, we need to call out to a perl function :/
862 if (ecb_expect_false (SvAMAGIC (k)))
863 {
864 dSP;
865
866 ENTER; SAVETMPS;
867 PUSHMARK (SP);
868 EXTEND (SP, 3);
869 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
870 PUSHs (sv_2mortal (k));
871 PUSHs (sv_2mortal (v));
872
873 PUTBACK;
874 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
875 SPAGAIN;
876
877 FREETMPS; LEAVE;
878
879 if (SvTRUE (ERRSV))
880 ERR_ERRSV;
881
882 return;
883 }
764 884
765 hv_store_ent (hv, k, v, 0); 885 hv_store_ent (hv, k, v, 0);
766 SvREFCNT_dec (k); 886 SvREFCNT_dec (k);
767 887
768fail: 888fail:
793 decode_he (dec, hv); 913 decode_he (dec, hv);
794 } 914 }
795 } 915 }
796 else 916 else
797 { 917 {
798 int pairs = decode_uint (dec); 918 UV pairs = decode_uint (dec);
919
920 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
799 921
800 while (pairs--) 922 while (pairs--)
801 decode_he (dec, hv); 923 decode_he (dec, hv);
802 } 924 }
803 925
891 sv = newRV_noinc (decode_sv (dec)); 1013 sv = newRV_noinc (decode_sv (dec));
892 break; 1014 break;
893 1015
894 case CBOR_TAG_STRINGREF_NAMESPACE: 1016 case CBOR_TAG_STRINGREF_NAMESPACE:
895 { 1017 {
1018 // do nmot use SAVETMPS/FREETMPS, as these will
1019 // erase mortalised caches, e.g. "shareable"
896 ENTER; SAVETMPS; 1020 ENTER;
897 1021
898 SAVESPTR (dec->stringref); 1022 SAVESPTR (dec->stringref);
899 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1023 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
900 1024
901 sv = decode_sv (dec); 1025 sv = decode_sv (dec);
902 1026
903 FREETMPS; LEAVE; 1027 LEAVE;
904 } 1028 }
905 break; 1029 break;
906 1030
907 case CBOR_TAG_STRINGREF: 1031 case CBOR_TAG_STRINGREF:
908 { 1032 {
959 } 1083 }
960 break; 1084 break;
961 1085
962 case CBOR_TAG_PERL_OBJECT: 1086 case CBOR_TAG_PERL_OBJECT:
963 { 1087 {
1088 if (dec->cbor.flags & F_FORBID_OBJECTS)
1089 goto filter;
1090
964 sv = decode_sv (dec); 1091 sv = decode_sv (dec);
965 1092
966 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 1093 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
967 ERR ("corrupted CBOR data (non-array perl object)"); 1094 ERR ("corrupted CBOR data (non-array perl object)");
968 1095
978 if (!method) 1105 if (!method)
979 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1106 ERR ("cannot decode perl-object (package does not have a THAW method)");
980 1107
981 dSP; 1108 dSP;
982 1109
983 ENTER; SAVETMPS; PUSHMARK (SP); 1110 ENTER; SAVETMPS;
1111 PUSHMARK (SP);
984 EXTEND (SP, len + 1); 1112 EXTEND (SP, len + 1);
985 // we re-bless the reference to get overload and other niceties right 1113 // we re-bless the reference to get overload and other niceties right
986 PUSHs (*av_fetch (av, 0, 1)); 1114 PUSHs (*av_fetch (av, 0, 1));
987 PUSHs (sv_cbor); 1115 PUSHs (sv_cbor);
988 1116
996 SPAGAIN; 1124 SPAGAIN;
997 1125
998 if (SvTRUE (ERRSV)) 1126 if (SvTRUE (ERRSV))
999 { 1127 {
1000 FREETMPS; LEAVE; 1128 FREETMPS; LEAVE;
1001 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1129 ERR_ERRSV;
1002 } 1130 }
1003 1131
1004 SvREFCNT_dec (sv); 1132 SvREFCNT_dec (sv);
1005 sv = SvREFCNT_inc (POPs); 1133 sv = SvREFCNT_inc (POPs);
1006 1134
1009 FREETMPS; LEAVE; 1137 FREETMPS; LEAVE;
1010 } 1138 }
1011 break; 1139 break;
1012 1140
1013 default: 1141 default:
1142 filter:
1014 { 1143 {
1144 SV *tag_sv = newSVuv (tag);
1145
1015 sv = decode_sv (dec); 1146 sv = decode_sv (dec);
1016 1147
1017 dSP; 1148 dSP;
1018 ENTER; SAVETMPS; PUSHMARK (SP); 1149 ENTER; SAVETMPS;
1150 SAVESTACK_POS ();
1151 PUSHMARK (SP);
1019 EXTEND (SP, 2); 1152 EXTEND (SP, 2);
1020 PUSHs (newSVuv (tag)); 1153 PUSHs (tag_sv);
1021 PUSHs (sv); 1154 PUSHs (sv);
1022 1155
1023 PUTBACK; 1156 PUTBACK;
1024 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1157 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1025 SPAGAIN; 1158 SPAGAIN;
1026 1159
1027 if (SvTRUE (ERRSV)) 1160 if (SvTRUE (ERRSV))
1028 { 1161 {
1162 SvREFCNT_dec (tag_sv);
1029 FREETMPS; LEAVE; 1163 FREETMPS; LEAVE;
1030 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1164 ERR_ERRSV;
1031 } 1165 }
1032 1166
1033 if (count) 1167 if (count)
1034 { 1168 {
1169 SvREFCNT_dec (tag_sv);
1035 SvREFCNT_dec (sv); 1170 SvREFCNT_dec (sv);
1036 sv = SvREFCNT_inc (POPs); 1171 sv = SvREFCNT_inc (POPs);
1037 } 1172 }
1038 else 1173 else
1039 { 1174 {
1040 AV *av = newAV (); 1175 AV *av = newAV ();
1041 av_push (av, newSVuv (tag)); 1176 av_push (av, tag_sv);
1042 av_push (av, sv); 1177 av_push (av, sv);
1043 1178
1044 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1179 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1045 ? cbor_tagged_stash 1180 ? cbor_tagged_stash
1046 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1181 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1132 1267
1133 return newSVnv (ecb_binary64_to_double (fp)); 1268 return newSVnv (ecb_binary64_to_double (fp));
1134 } 1269 }
1135 1270
1136 // 0..19 unassigned simple 1271 // 0..19 unassigned simple
1137 // 24 reserved + unassigned (reserved values are not encodable) 1272 // 24 reserved + unassigned simple (reserved values are not encodable)
1273 // 28-30 unassigned misc
1274 // 31 break code
1138 default: 1275 default:
1139 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1276 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1140 } 1277 }
1141 1278
1142 break; 1279 break;
1143 } 1280 }
1144 1281
1147} 1284}
1148 1285
1149static SV * 1286static SV *
1150decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1287decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1151{ 1288{
1152 dec_t dec = { }; 1289 dec_t dec = { 0 };
1153 SV *sv; 1290 SV *sv;
1154 STRLEN len; 1291 STRLEN len;
1155 char *data = SvPVbyte (string, len); 1292 char *data = SvPVbyte (string, len);
1156 1293
1157 if (len > cbor->max_size && cbor->max_size) 1294 if (len > cbor->max_size && cbor->max_size)
1173 1310
1174 if (dec.err) 1311 if (dec.err)
1175 { 1312 {
1176 if (dec.shareable) 1313 if (dec.shareable)
1177 { 1314 {
1178 // need to break cyclic links, which whould all be in shareable 1315 // need to break cyclic links, which would all be in shareable
1179 int i; 1316 int i;
1180 SV **svp; 1317 SV **svp;
1181 1318
1182 for (i = av_len (dec.shareable) + 1; i--; ) 1319 for (i = av_len (dec.shareable) + 1; i--; )
1183 if ((svp = av_fetch (dec.shareable, i, 0))) 1320 if ((svp = av_fetch (dec.shareable, i, 0)))
1184 sv_setsv (*svp, &PL_sv_undef); 1321 sv_setsv (*svp, &PL_sv_undef);
1185 } 1322 }
1186 1323
1187 SvREFCNT_dec (sv); 1324 SvREFCNT_dec (sv);
1325
1326 if (dec.err_sv)
1327 sv_2mortal (dec.err_sv);
1328
1188 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1329 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1189 } 1330 }
1190 1331
1191 sv = sv_2mortal (sv); 1332 sv = sv_2mortal (sv);
1192 1333
1193 return sv; 1334 return sv;
1194} 1335}
1195 1336
1337/////////////////////////////////////////////////////////////////////////////
1338// incremental parser
1339
1340#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1341
1342// returns 0 for notyet, 1 for success or error
1343static int
1344incr_parse (CBOR *self, SV *cborstr)
1345{
1346 STRLEN cur;
1347 SvPV (cborstr, cur);
1348
1349 while (ecb_expect_true (self->incr_need <= cur))
1350 {
1351 // table of integer count bytes
1352 static I8 incr_len[MINOR_MASK + 1] = {
1353 0, 0, 0, 0, 0, 0, 0, 0,
1354 0, 0, 0, 0, 0, 0, 0, 0,
1355 0, 0, 0, 0, 0, 0, 0, 0,
1356 1, 2, 4, 8,-1,-1,-1,-2
1357 };
1358
1359 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1360 U8 m = *p & MINOR_MASK;
1361 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1362 I8 ilen = incr_len[m];
1363
1364 self->incr_need = self->incr_pos + 1;
1365
1366 if (ecb_expect_false (ilen < 0))
1367 {
1368 if (m != MINOR_INDEF)
1369 return 1; // error
1370
1371 if (*p == (MAJOR_MISC | MINOR_INDEF))
1372 {
1373 if (count >= 0)
1374 return 1; // error
1375
1376 count = 1;
1377 }
1378 else
1379 {
1380 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1381 count = -1;
1382 }
1383 }
1384 else
1385 {
1386 self->incr_need += ilen;
1387 if (ecb_expect_false (self->incr_need > cur))
1388 return 0;
1389
1390 int major = *p >> MAJOR_SHIFT;
1391
1392 switch (major)
1393 {
1394 case MAJOR_TAG >> MAJOR_SHIFT:
1395 ++count; // tags merely prefix another value
1396 break;
1397
1398 case MAJOR_BYTES >> MAJOR_SHIFT:
1399 case MAJOR_TEXT >> MAJOR_SHIFT:
1400 case MAJOR_ARRAY >> MAJOR_SHIFT:
1401 case MAJOR_MAP >> MAJOR_SHIFT:
1402 {
1403 UV len;
1404
1405 if (ecb_expect_false (ilen))
1406 {
1407 len = 0;
1408
1409 do {
1410 len = (len << 8) | *++p;
1411 } while (--ilen);
1412 }
1413 else
1414 len = m;
1415
1416 switch (major)
1417 {
1418 case MAJOR_BYTES >> MAJOR_SHIFT:
1419 case MAJOR_TEXT >> MAJOR_SHIFT:
1420 self->incr_need += len;
1421 if (ecb_expect_false (self->incr_need > cur))
1422 return 0;
1423
1424 break;
1425
1426 case MAJOR_MAP >> MAJOR_SHIFT:
1427 len <<= 1;
1428 case MAJOR_ARRAY >> MAJOR_SHIFT:
1429 if (len)
1430 {
1431 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1432 count = len + 1;
1433 }
1434 break;
1435 }
1436 }
1437 }
1438 }
1439
1440 self->incr_pos = self->incr_need;
1441
1442 if (count > 0)
1443 {
1444 while (!--count)
1445 {
1446 if (!AvFILLp (self->incr_count))
1447 return 1; // done
1448
1449 SvREFCNT_dec_NN (av_pop (self->incr_count));
1450 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1451 }
1452
1453 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1454 }
1455 }
1456
1457 return 0;
1458}
1459
1460
1196///////////////////////////////////////////////////////////////////////////// 1461/////////////////////////////////////////////////////////////////////////////
1197// XS interface functions 1462// XS interface functions
1198 1463
1199MODULE = CBOR::XS PACKAGE = CBOR::XS 1464MODULE = CBOR::XS PACKAGE = CBOR::XS
1200 1465
1212 1477
1213 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1478 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1214 1479
1215 sv_cbor = newSVpv ("CBOR", 0); 1480 sv_cbor = newSVpv ("CBOR", 0);
1216 SvREADONLY_on (sv_cbor); 1481 SvREADONLY_on (sv_cbor);
1482
1483 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1217} 1484}
1218 1485
1219PROTOTYPES: DISABLE 1486PROTOTYPES: DISABLE
1220 1487
1221void CLONE (...) 1488void CLONE (...)
1241 ALIAS: 1508 ALIAS:
1242 shrink = F_SHRINK 1509 shrink = F_SHRINK
1243 allow_unknown = F_ALLOW_UNKNOWN 1510 allow_unknown = F_ALLOW_UNKNOWN
1244 allow_sharing = F_ALLOW_SHARING 1511 allow_sharing = F_ALLOW_SHARING
1245 allow_cycles = F_ALLOW_CYCLES 1512 allow_cycles = F_ALLOW_CYCLES
1513 forbid_objects = F_FORBID_OBJECTS
1246 pack_strings = F_PACK_STRINGS 1514 pack_strings = F_PACK_STRINGS
1515 text_keys = F_TEXT_KEYS
1516 text_strings = F_TEXT_STRINGS
1247 validate_utf8 = F_VALIDATE_UTF8 1517 validate_utf8 = F_VALIDATE_UTF8
1248 PPCODE: 1518 PPCODE:
1249{ 1519{
1250 if (enable) 1520 if (enable)
1251 self->flags |= ix; 1521 self->flags |= ix;
1259 ALIAS: 1529 ALIAS:
1260 get_shrink = F_SHRINK 1530 get_shrink = F_SHRINK
1261 get_allow_unknown = F_ALLOW_UNKNOWN 1531 get_allow_unknown = F_ALLOW_UNKNOWN
1262 get_allow_sharing = F_ALLOW_SHARING 1532 get_allow_sharing = F_ALLOW_SHARING
1263 get_allow_cycles = F_ALLOW_CYCLES 1533 get_allow_cycles = F_ALLOW_CYCLES
1534 get_forbid_objects = F_FORBID_OBJECTS
1264 get_pack_strings = F_PACK_STRINGS 1535 get_pack_strings = F_PACK_STRINGS
1536 get_text_keys = F_TEXT_KEYS
1537 get_text_strings = F_TEXT_STRINGS
1265 get_validate_utf8 = F_VALIDATE_UTF8 1538 get_validate_utf8 = F_VALIDATE_UTF8
1266 PPCODE: 1539 PPCODE:
1267 XPUSHs (boolSV (self->flags & ix)); 1540 XPUSHs (boolSV (self->flags & ix));
1268 1541
1269void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1542void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1319 EXTEND (SP, 2); 1592 EXTEND (SP, 2);
1320 PUSHs (sv); 1593 PUSHs (sv);
1321 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1594 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1322} 1595}
1323 1596
1597void incr_parse (CBOR *self, SV *cborstr)
1598 ALIAS:
1599 incr_parse_multiple = 1
1600 PPCODE:
1601{
1602 if (SvUTF8 (cborstr))
1603 sv_utf8_downgrade (cborstr, 0);
1604
1605 if (!self->incr_count)
1606 {
1607 self->incr_count = newAV ();
1608 self->incr_pos = 0;
1609 self->incr_need = 1;
1610
1611 av_push (self->incr_count, newSViv (1));
1612 }
1613
1614 do
1615 {
1616 if (!incr_parse (self, cborstr))
1617 {
1618 if (self->incr_need > self->max_size && self->max_size)
1619 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1620 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1621
1622 break;
1623 }
1624
1625 SV *sv;
1626 char *offset;
1627
1628 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1629 XPUSHs (sv);
1630
1631 sv_chop (cborstr, offset);
1632
1633 av_clear (self->incr_count);
1634 av_push (self->incr_count, newSViv (1));
1635
1636 self->incr_pos = 0;
1637 self->incr_need = self->incr_pos + 1;
1638 }
1639 while (ix);
1640}
1641
1642void incr_reset (CBOR *self)
1643 CODE:
1644{
1645 SvREFCNT_dec (self->incr_count);
1646 self->incr_count = 0;
1647}
1648
1324void DESTROY (CBOR *self) 1649void DESTROY (CBOR *self)
1325 PPCODE: 1650 PPCODE:
1326 cbor_free (self); 1651 cbor_free (self);
1327 1652
1328PROTOTYPES: ENABLE 1653PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines