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.64 by root, Tue Jun 27 02:03:24 2017 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))
563 if (ecb_expect_false (nv == (NV)(U32)nv)) 563 if (ecb_expect_false (nv == (NV)(U32)nv))
564 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 564 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
565 //TODO: maybe I32? 565 //TODO: maybe I32?
566 else if (ecb_expect_false (nv == (float)nv)) 566 else if (ecb_expect_false (nv == (float)nv))
567 { 567 {
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
568 uint32_t fp = ecb_float_to_binary32 (nv); 570 uint32_t fp = ecb_float_to_binary32 (nv);
569
570 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
571 571
572 if (!ecb_big_endian ()) 572 if (!ecb_big_endian ())
573 fp = ecb_bswap32 (fp); 573 fp = ecb_bswap32 (fp);
574 574
575 memcpy (enc->cur, &fp, 4); 575 memcpy (enc->cur, &fp, 4);
576 enc->cur += 4; 576 enc->cur += 4;
577 } 577 }
578 else 578 else
579 { 579 {
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
580 uint64_t fp = ecb_double_to_binary64 (nv); 582 uint64_t fp = ecb_double_to_binary64 (nv);
581
582 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
583 583
584 if (!ecb_big_endian ()) 584 if (!ecb_big_endian ())
585 fp = ecb_bswap64 (fp); 585 fp = ecb_bswap64 (fp);
586 586
587 memcpy (enc->cur, &fp, 8); 587 memcpy (enc->cur, &fp, 8);
625static SV * 625static SV *
626encode_cbor (SV *scalar, CBOR *cbor) 626encode_cbor (SV *scalar, CBOR *cbor)
627{ 627{
628 enc_t enc = { 0 }; 628 enc_t enc = { 0 };
629 629
630 enc.cbor = *cbor; 630 enc.cbor = *cbor;
631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
632 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
633 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
634 634
635 SvPOK_only (enc.sv); 635 SvPOK_only (enc.sv);
636 636
637 if (cbor->flags & F_PACK_STRINGS) 637 if (cbor->flags & F_PACK_STRINGS)
638 { 638 {
698err_unexpected_end (dec_t *dec) 698err_unexpected_end (dec_t *dec)
699{ 699{
700 err_set (dec, "unexpected end of CBOR data"); 700 err_set (dec, "unexpected end of CBOR data");
701} 701}
702 702
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 703#define ERR_DO(do) SB do; goto fail; SE
710#define ERR(reason) ERR_DO (err_set (dec, reason)) 704#define ERR(reason) ERR_DO (err_set (dec, reason))
711#define ERR_ERRSV ERR_DO (err_errsv (dec)) 705#define ERR_ERRSV ERR_DO (err_errsv (dec))
712 706
713#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) 707#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
714 708
715#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) 709#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 710#define DEC_DEC_DEPTH --dec->depth
717 711
718static UV 712static UV
786 780
787 for (;;) 781 for (;;)
788 { 782 {
789 WANT (1); 783 WANT (1);
790 784
791 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 785 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
792 { 786 {
793 ++dec->cur; 787 ++dec->cur;
794 break; 788 break;
795 } 789 }
796 790
810 804
811 DEC_DEC_DEPTH; 805 DEC_DEC_DEPTH;
812 return newRV_noinc ((SV *)av); 806 return newRV_noinc ((SV *)av);
813 807
814fail: 808fail:
815 SvREFCNT_dec (av); 809 SvREFCNT_dec_NN (av);
816 DEC_DEC_DEPTH; 810 DEC_DEC_DEPTH;
817 return &PL_sv_undef; 811 return &PL_sv_undef;
818} 812}
819 813
820static void 814static void
883 877
884 return; 878 return;
885 } 879 }
886 880
887 hv_store_ent (hv, k, v, 0); 881 hv_store_ent (hv, k, v, 0);
888 SvREFCNT_dec (k); 882 SvREFCNT_dec_NN (k);
889 883
890fail: 884fail:
891 ; 885 ;
892} 886}
893 887
904 898
905 for (;;) 899 for (;;)
906 { 900 {
907 WANT (1); 901 WANT (1);
908 902
909 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 903 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
910 { 904 {
911 ++dec->cur; 905 ++dec->cur;
912 break; 906 break;
913 } 907 }
914 908
927 921
928 DEC_DEC_DEPTH; 922 DEC_DEC_DEPTH;
929 return newRV_noinc ((SV *)hv); 923 return newRV_noinc ((SV *)hv);
930 924
931fail: 925fail:
932 SvREFCNT_dec (hv); 926 SvREFCNT_dec_NN (hv);
933 DEC_DEC_DEPTH; 927 DEC_DEC_DEPTH;
934 return &PL_sv_undef; 928 return &PL_sv_undef;
935} 929}
936 930
937static SV * 931static SV *
938decode_str (dec_t *dec, int utf8) 932decode_str (dec_t *dec, int utf8)
939{ 933{
940 SV *sv = 0; 934 SV *sv = 0;
941 935
942 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 936 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
943 { 937 {
944 // indefinite length strings 938 // indefinite length strings
945 ++dec->cur; 939 ++dec->cur;
946 940
947 U8 major = *dec->cur & MAJOR_MISC; 941 U8 major = *dec->cur & MAJOR_MISC;
1015 sv = newRV_noinc (decode_sv (dec)); 1009 sv = newRV_noinc (decode_sv (dec));
1016 break; 1010 break;
1017 1011
1018 case CBOR_TAG_STRINGREF_NAMESPACE: 1012 case CBOR_TAG_STRINGREF_NAMESPACE:
1019 { 1013 {
1020 // do nmot use SAVETMPS/FREETMPS, as these will 1014 // do not use SAVETMPS/FREETMPS, as these will
1021 // erase mortalised caches, e.g. "shareable" 1015 // erase mortalised caches, e.g. "shareable"
1022 ENTER; 1016 ENTER;
1023 1017
1024 SAVESPTR (dec->stringref); 1018 SAVESPTR (dec->stringref);
1025 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1019 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
1035 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1029 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1036 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1030 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
1037 1031
1038 UV idx = decode_uint (dec); 1032 UV idx = decode_uint (dec);
1039 1033
1040 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1034 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
1041 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); 1035 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
1042 1036
1043 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1037 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
1044 } 1038 }
1045 break; 1039 break;
1073 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1067 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1074 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1068 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
1075 1069
1076 UV idx = decode_uint (dec); 1070 UV idx = decode_uint (dec);
1077 1071
1078 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1072 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1079 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1073 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1080 1074
1081 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1075 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1082 1076
1083 if (sv == &PL_sv_undef) 1077 if (sv == &PL_sv_undef)
1129 { 1123 {
1130 FREETMPS; LEAVE; 1124 FREETMPS; LEAVE;
1131 ERR_ERRSV; 1125 ERR_ERRSV;
1132 } 1126 }
1133 1127
1134 SvREFCNT_dec (sv); 1128 SvREFCNT_dec_NN (sv);
1135 sv = SvREFCNT_inc (POPs); 1129 sv = SvREFCNT_inc (POPs);
1136 1130
1137 PUTBACK; 1131 PUTBACK;
1138 1132
1139 FREETMPS; LEAVE; 1133 FREETMPS; LEAVE;
1159 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1153 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1160 SPAGAIN; 1154 SPAGAIN;
1161 1155
1162 if (SvTRUE (ERRSV)) 1156 if (SvTRUE (ERRSV))
1163 { 1157 {
1164 SvREFCNT_dec (tag_sv); 1158 SvREFCNT_dec_NN (tag_sv);
1165 FREETMPS; LEAVE; 1159 FREETMPS; LEAVE;
1166 ERR_ERRSV; 1160 ERR_ERRSV;
1167 } 1161 }
1168 1162
1169 if (count) 1163 if (count)
1170 { 1164 {
1171 SvREFCNT_dec (tag_sv); 1165 SvREFCNT_dec_NN (tag_sv);
1172 SvREFCNT_dec (sv); 1166 SvREFCNT_dec_NN (sv);
1173 sv = SvREFCNT_inc (POPs); 1167 sv = SvREFCNT_inc_NN (POPs);
1174 } 1168 }
1175 else 1169 else
1176 { 1170 {
1177 AV *av = newAV (); 1171 AV *av = newAV ();
1178 av_push (av, tag_sv); 1172 av_push (av, tag_sv);
1321 for (i = av_len (dec.shareable) + 1; i--; ) 1315 for (i = av_len (dec.shareable) + 1; i--; )
1322 if ((svp = av_fetch (dec.shareable, i, 0))) 1316 if ((svp = av_fetch (dec.shareable, i, 0)))
1323 sv_setsv (*svp, &PL_sv_undef); 1317 sv_setsv (*svp, &PL_sv_undef);
1324 } 1318 }
1325 1319
1326 SvREFCNT_dec (sv); 1320 SvREFCNT_dec_NN (sv);
1327 1321
1328 if (dec.err_sv) 1322 if (dec.err_sv)
1329 sv_2mortal (dec.err_sv); 1323 sv_2mortal (dec.err_sv);
1330 1324
1331 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1325 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1674 cbor_init (&cbor); 1668 cbor_init (&cbor);
1675 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1669 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1676 XPUSHs (cborstr); 1670 XPUSHs (cborstr);
1677} 1671}
1678 1672
1673#ifdef __AFL_COMPILER
1674
1675void
1676afl_init ()
1677 CODE:
1678 __AFL_INIT ();
1679
1680int
1681afl_loop (unsigned int count = 10000)
1682 CODE:
1683 RETVAL = __AFL_LOOP (count);
1684 OUTPUT:
1685 RETVAL
1686
1687#endif
1688

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines