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.60 by root, Sat Nov 26 00:47:02 2016 UTC vs.
Revision 1.65 by root, Thu Nov 15 19:52:41 2018 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
12#define ECB_NO_THREADS 1 13#define ECB_NO_THREADS 1
13#include "ecb.h" 14#include "ecb.h"
14 15
15// compatibility with perl <5.18 16// compatibility with perl <5.18
20# define HvNAMELEN(hv) HvNAMELEN_get (hv) 21# define HvNAMELEN(hv) HvNAMELEN_get (hv)
21#endif 22#endif
22#ifndef HvNAMEUTF8 23#ifndef HvNAMEUTF8
23# define HvNAMEUTF8(hv) 0 24# define HvNAMEUTF8(hv) 0
24#endif 25#endif
26#ifndef SvREFCNT_inc_NN
27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
28#endif
25#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
26# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
27#endif 31#endif
28 32
29// known major and minor types 33// known major and minor types
186 190
187// minimum length of a string to be registered for stringref 191// minimum length of a string to be registered for stringref
188ecb_inline int 192ecb_inline int
189minimum_string_length (UV idx) 193minimum_string_length (UV idx)
190{ 194{
191 return idx > 23 195 return idx <= 23 ? 3
192 ? idx > 0xffU 196 : idx <= 0xffU ? 4
193 ? idx > 0xffffU 197 : idx <= 0xffffU ? 5
194 ? idx > 0xffffffffU 198 : idx <= 0xffffffffU ? 7
195 ? 11 199 : 11;
196 : 7
197 : 5
198 : 4
199 : 3;
200} 200}
201 201
202///////////////////////////////////////////////////////////////////////////// 202/////////////////////////////////////////////////////////////////////////////
203// encoder 203// encoder
204 204
217} enc_t; 217} enc_t;
218 218
219ecb_inline void 219ecb_inline void
220need (enc_t *enc, STRLEN len) 220need (enc_t *enc, STRLEN len)
221{ 221{
222 if (ecb_expect_false (enc->cur + len >= enc->end)) 222 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
223 { 223 {
224 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 224 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
225 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 225 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
226 enc->cur = SvPVX (enc->sv) + cur; 226 enc->cur = SvPVX (enc->sv) + cur;
227 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 227 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
357 357
358 ++enc->depth; 358 ++enc->depth;
359 359
360 encode_uint (enc, MAJOR_ARRAY, len + 1); 360 encode_uint (enc, MAJOR_ARRAY, len + 1);
361 361
362 if (SvMAGICAL (av)) 362 if (ecb_expect_false (SvMAGICAL (av)))
363 for (i = 0; i <= len; ++i) 363 for (i = 0; i <= len; ++i)
364 { 364 {
365 SV **svp = av_fetch (av, i, 0); 365 SV **svp = av_fetch (av, i, 0);
366 encode_sv (enc, svp ? *svp : &PL_sv_undef); 366 encode_sv (enc, svp ? *svp : &PL_sv_undef);
367 } 367 }
386 ++enc->depth; 386 ++enc->depth;
387 387
388 int pairs = hv_iterinit (hv); 388 int pairs = hv_iterinit (hv);
389 int mg = SvMAGICAL (hv); 389 int mg = SvMAGICAL (hv);
390 390
391 if (mg) 391 if (ecb_expect_false (mg))
392 encode_ch (enc, MAJOR_MAP | MINOR_INDEF); 392 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
393 else 393 else
394 encode_uint (enc, MAJOR_MAP, pairs); 394 encode_uint (enc, MAJOR_MAP, pairs);
395 395
396 while ((he = hv_iternext (hv))) 396 while ((he = hv_iternext (hv)))
401 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 401 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
402 402
403 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 403 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
404 } 404 }
405 405
406 if (mg) 406 if (ecb_expect_false (mg))
407 encode_ch (enc, MAJOR_MISC | MINOR_INDEF); 407 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
408 408
409 --enc->depth; 409 --enc->depth;
410} 410}
411 411
454 } 454 }
455 455
456 if (ecb_expect_false (SvREFCNT (sv) > 1) 456 if (ecb_expect_false (SvREFCNT (sv) > 1)
457 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) 457 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
458 { 458 {
459 if (!enc->shareable) 459 if (ecb_expect_false (!enc->shareable))
460 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); 460 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
461 461
462 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); 462 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
463 463
464 if (SvOK (*svp)) 464 if (SvOK (*svp))
510 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 510 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
511 { 511 {
512 dSP; 512 dSP;
513 513
514 ENTER; SAVETMPS; 514 ENTER; SAVETMPS;
515 SAVESTACK_POS ();
516 PUSHMARK (SP); 515 PUSHMARK (SP);
517 EXTEND (SP, 2); 516 EXTEND (SP, 2);
518 // we re-bless the reference to get overload and other niceties right 517 // we re-bless the reference to get overload and other niceties right
519 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 518 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
520 PUSHs (sv_cbor); 519 PUSHs (sv_cbor);
529 528
530 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 529 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
531 encode_uint (enc, MAJOR_ARRAY, count + 1); 530 encode_uint (enc, MAJOR_ARRAY, count + 1);
532 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 531 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
533 532
534 while (count) 533 {
534 int i;
535
536 for (i = 0; i < count; ++i)
535 encode_sv (enc, SP[1 - count--]); 537 encode_sv (enc, SP[i + 1 - count]);
538
539 SP -= count;
540 }
536 541
537 PUTBACK; 542 PUTBACK;
538 543
539 FREETMPS; LEAVE; 544 FREETMPS; LEAVE;
540 } 545 }
563 if (ecb_expect_false (nv == (NV)(U32)nv)) 568 if (ecb_expect_false (nv == (NV)(U32)nv))
564 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 569 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
565 //TODO: maybe I32? 570 //TODO: maybe I32?
566 else if (ecb_expect_false (nv == (float)nv)) 571 else if (ecb_expect_false (nv == (float)nv))
567 { 572 {
573 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
574
568 uint32_t fp = ecb_float_to_binary32 (nv); 575 uint32_t fp = ecb_float_to_binary32 (nv);
569
570 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
571 576
572 if (!ecb_big_endian ()) 577 if (!ecb_big_endian ())
573 fp = ecb_bswap32 (fp); 578 fp = ecb_bswap32 (fp);
574 579
575 memcpy (enc->cur, &fp, 4); 580 memcpy (enc->cur, &fp, 4);
576 enc->cur += 4; 581 enc->cur += 4;
577 } 582 }
578 else 583 else
579 { 584 {
585 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
586
580 uint64_t fp = ecb_double_to_binary64 (nv); 587 uint64_t fp = ecb_double_to_binary64 (nv);
581
582 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
583 588
584 if (!ecb_big_endian ()) 589 if (!ecb_big_endian ())
585 fp = ecb_bswap64 (fp); 590 fp = ecb_bswap64 (fp);
586 591
587 memcpy (enc->cur, &fp, 8); 592 memcpy (enc->cur, &fp, 8);
625static SV * 630static SV *
626encode_cbor (SV *scalar, CBOR *cbor) 631encode_cbor (SV *scalar, CBOR *cbor)
627{ 632{
628 enc_t enc = { 0 }; 633 enc_t enc = { 0 };
629 634
630 enc.cbor = *cbor; 635 enc.cbor = *cbor;
631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 636 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
632 enc.cur = SvPVX (enc.sv); 637 enc.cur = SvPVX (enc.sv);
633 enc.end = SvEND (enc.sv); 638 enc.end = SvEND (enc.sv);
634 639
635 SvPOK_only (enc.sv); 640 SvPOK_only (enc.sv);
636 641
637 if (cbor->flags & F_PACK_STRINGS) 642 if (cbor->flags & F_PACK_STRINGS)
638 { 643 {
698err_unexpected_end (dec_t *dec) 703err_unexpected_end (dec_t *dec)
699{ 704{
700 err_set (dec, "unexpected end of CBOR data"); 705 err_set (dec, "unexpected end of CBOR data");
701} 706}
702 707
703ecb_cold static void
704err_nesting_exceeded (dec_t *dec)
705{
706 err_set (dec, ERR_NESTING_EXCEEDED);
707}
708
709#define ERR_DO(do) SB do; goto fail; SE 708#define ERR_DO(do) SB do; goto fail; SE
710#define ERR(reason) ERR_DO (err_set (dec, reason)) 709#define ERR(reason) ERR_DO (err_set (dec, reason))
711#define ERR_ERRSV ERR_DO (err_errsv (dec)) 710#define ERR_ERRSV ERR_DO (err_errsv (dec))
712 711
713#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) 712#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
714 713
715#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) 714#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
716#define DEC_DEC_DEPTH --dec->depth 715#define DEC_DEC_DEPTH --dec->depth
717 716
718static UV 717static UV
786 785
787 for (;;) 786 for (;;)
788 { 787 {
789 WANT (1); 788 WANT (1);
790 789
791 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 790 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
792 { 791 {
793 ++dec->cur; 792 ++dec->cur;
794 break; 793 break;
795 } 794 }
796 795
810 809
811 DEC_DEC_DEPTH; 810 DEC_DEC_DEPTH;
812 return newRV_noinc ((SV *)av); 811 return newRV_noinc ((SV *)av);
813 812
814fail: 813fail:
815 SvREFCNT_dec (av); 814 SvREFCNT_dec_NN (av);
816 DEC_DEC_DEPTH; 815 DEC_DEC_DEPTH;
817 return &PL_sv_undef; 816 return &PL_sv_undef;
818} 817}
819 818
820static void 819static void
883 882
884 return; 883 return;
885 } 884 }
886 885
887 hv_store_ent (hv, k, v, 0); 886 hv_store_ent (hv, k, v, 0);
888 SvREFCNT_dec (k); 887 SvREFCNT_dec_NN (k);
889 888
890fail: 889fail:
891 ; 890 ;
892} 891}
893 892
904 903
905 for (;;) 904 for (;;)
906 { 905 {
907 WANT (1); 906 WANT (1);
908 907
909 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 908 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
910 { 909 {
911 ++dec->cur; 910 ++dec->cur;
912 break; 911 break;
913 } 912 }
914 913
927 926
928 DEC_DEC_DEPTH; 927 DEC_DEC_DEPTH;
929 return newRV_noinc ((SV *)hv); 928 return newRV_noinc ((SV *)hv);
930 929
931fail: 930fail:
932 SvREFCNT_dec (hv); 931 SvREFCNT_dec_NN (hv);
933 DEC_DEC_DEPTH; 932 DEC_DEC_DEPTH;
934 return &PL_sv_undef; 933 return &PL_sv_undef;
935} 934}
936 935
937static SV * 936static SV *
938decode_str (dec_t *dec, int utf8) 937decode_str (dec_t *dec, int utf8)
939{ 938{
940 SV *sv = 0; 939 SV *sv = 0;
941 940
942 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 941 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
943 { 942 {
944 // indefinite length strings 943 // indefinite length strings
945 ++dec->cur; 944 ++dec->cur;
946 945
947 U8 major = *dec->cur & MAJOR_MISC; 946 U8 major = *dec->cur & MAJOR_MISC;
1015 sv = newRV_noinc (decode_sv (dec)); 1014 sv = newRV_noinc (decode_sv (dec));
1016 break; 1015 break;
1017 1016
1018 case CBOR_TAG_STRINGREF_NAMESPACE: 1017 case CBOR_TAG_STRINGREF_NAMESPACE:
1019 { 1018 {
1020 // do nmot use SAVETMPS/FREETMPS, as these will 1019 // do not use SAVETMPS/FREETMPS, as these will
1021 // erase mortalised caches, e.g. "shareable" 1020 // erase mortalised caches, e.g. "shareable"
1022 ENTER; 1021 ENTER;
1023 1022
1024 SAVESPTR (dec->stringref); 1023 SAVESPTR (dec->stringref);
1025 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1024 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
1035 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1034 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1036 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1035 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
1037 1036
1038 UV idx = decode_uint (dec); 1037 UV idx = decode_uint (dec);
1039 1038
1040 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1039 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
1041 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); 1040 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
1042 1041
1043 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1042 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
1044 } 1043 }
1045 break; 1044 break;
1073 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1072 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1074 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1073 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
1075 1074
1076 UV idx = decode_uint (dec); 1075 UV idx = decode_uint (dec);
1077 1076
1078 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1077 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1079 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1078 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1080 1079
1081 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1080 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1082 1081
1083 if (sv == &PL_sv_undef) 1082 if (sv == &PL_sv_undef)
1129 { 1128 {
1130 FREETMPS; LEAVE; 1129 FREETMPS; LEAVE;
1131 ERR_ERRSV; 1130 ERR_ERRSV;
1132 } 1131 }
1133 1132
1134 SvREFCNT_dec (sv); 1133 SvREFCNT_dec_NN (sv);
1135 sv = SvREFCNT_inc (POPs); 1134 sv = SvREFCNT_inc (POPs);
1136 1135
1137 PUTBACK; 1136 PUTBACK;
1138 1137
1139 FREETMPS; LEAVE; 1138 FREETMPS; LEAVE;
1147 1146
1148 sv = decode_sv (dec); 1147 sv = decode_sv (dec);
1149 1148
1150 dSP; 1149 dSP;
1151 ENTER; SAVETMPS; 1150 ENTER; SAVETMPS;
1152 SAVESTACK_POS ();
1153 PUSHMARK (SP); 1151 PUSHMARK (SP);
1154 EXTEND (SP, 2); 1152 EXTEND (SP, 2);
1155 PUSHs (tag_sv); 1153 PUSHs (tag_sv);
1156 PUSHs (sv); 1154 PUSHs (sv);
1157 1155
1159 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);
1160 SPAGAIN; 1158 SPAGAIN;
1161 1159
1162 if (SvTRUE (ERRSV)) 1160 if (SvTRUE (ERRSV))
1163 { 1161 {
1164 SvREFCNT_dec (tag_sv); 1162 SvREFCNT_dec_NN (tag_sv);
1165 FREETMPS; LEAVE; 1163 FREETMPS; LEAVE;
1166 ERR_ERRSV; 1164 ERR_ERRSV;
1167 } 1165 }
1168 1166
1169 if (count) 1167 if (count)
1170 { 1168 {
1171 SvREFCNT_dec (tag_sv); 1169 SvREFCNT_dec_NN (tag_sv);
1172 SvREFCNT_dec (sv); 1170 SvREFCNT_dec_NN (sv);
1173 sv = SvREFCNT_inc (POPs); 1171 sv = SvREFCNT_inc_NN (TOPs);
1172 SP -= count;
1174 } 1173 }
1175 else 1174 else
1176 { 1175 {
1177 AV *av = newAV (); 1176 AV *av = newAV ();
1178 av_push (av, tag_sv); 1177 av_push (av, tag_sv);
1321 for (i = av_len (dec.shareable) + 1; i--; ) 1320 for (i = av_len (dec.shareable) + 1; i--; )
1322 if ((svp = av_fetch (dec.shareable, i, 0))) 1321 if ((svp = av_fetch (dec.shareable, i, 0)))
1323 sv_setsv (*svp, &PL_sv_undef); 1322 sv_setsv (*svp, &PL_sv_undef);
1324 } 1323 }
1325 1324
1326 SvREFCNT_dec (sv); 1325 SvREFCNT_dec_NN (sv);
1327 1326
1328 if (dec.err_sv) 1327 if (dec.err_sv)
1329 sv_2mortal (dec.err_sv); 1328 sv_2mortal (dec.err_sv);
1330 1329
1331 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1330 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1674 cbor_init (&cbor); 1673 cbor_init (&cbor);
1675 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1674 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1676 XPUSHs (cborstr); 1675 XPUSHs (cborstr);
1677} 1676}
1678 1677
1678#ifdef __AFL_COMPILER
1679
1680void
1681afl_init ()
1682 CODE:
1683 __AFL_INIT ();
1684
1685int
1686afl_loop (unsigned int count = 10000)
1687 CODE:
1688 RETVAL = __AFL_LOOP (count);
1689 OUTPUT:
1690 RETVAL
1691
1692#endif
1693

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines