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.61 by root, Sat Nov 26 02:10:19 2016 UTC vs.
Revision 1.64 by root, Tue Jun 27 02:03:24 2017 UTC

21# define HvNAMELEN(hv) HvNAMELEN_get (hv) 21# define HvNAMELEN(hv) HvNAMELEN_get (hv)
22#endif 22#endif
23#ifndef HvNAMEUTF8 23#ifndef HvNAMEUTF8
24# define HvNAMEUTF8(hv) 0 24# define HvNAMEUTF8(hv) 0
25#endif 25#endif
26#ifndef SvREFCNT_inc_NN
27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
28#endif
26#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
27# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
28#endif 31#endif
29 32
30// known major and minor types 33// known major and minor types
187 190
188// minimum length of a string to be registered for stringref 191// minimum length of a string to be registered for stringref
189ecb_inline int 192ecb_inline int
190minimum_string_length (UV idx) 193minimum_string_length (UV idx)
191{ 194{
192
193 return idx <= 23 ? 3 195 return idx <= 23 ? 3
194 : idx <= 0xffU ? 4 196 : idx <= 0xffU ? 4
195 : idx <= 0xffffU ? 5 197 : idx <= 0xffffU ? 5
196 : idx <= 0xffffffffU ? 7 198 : idx <= 0xffffffffU ? 7
197 : 11; 199 : 11;
355 357
356 ++enc->depth; 358 ++enc->depth;
357 359
358 encode_uint (enc, MAJOR_ARRAY, len + 1); 360 encode_uint (enc, MAJOR_ARRAY, len + 1);
359 361
360 if (SvMAGICAL (av)) 362 if (ecb_expect_false (SvMAGICAL (av)))
361 for (i = 0; i <= len; ++i) 363 for (i = 0; i <= len; ++i)
362 { 364 {
363 SV **svp = av_fetch (av, i, 0); 365 SV **svp = av_fetch (av, i, 0);
364 encode_sv (enc, svp ? *svp : &PL_sv_undef); 366 encode_sv (enc, svp ? *svp : &PL_sv_undef);
365 } 367 }
384 ++enc->depth; 386 ++enc->depth;
385 387
386 int pairs = hv_iterinit (hv); 388 int pairs = hv_iterinit (hv);
387 int mg = SvMAGICAL (hv); 389 int mg = SvMAGICAL (hv);
388 390
389 if (mg) 391 if (ecb_expect_false (mg))
390 encode_ch (enc, MAJOR_MAP | MINOR_INDEF); 392 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
391 else 393 else
392 encode_uint (enc, MAJOR_MAP, pairs); 394 encode_uint (enc, MAJOR_MAP, pairs);
393 395
394 while ((he = hv_iternext (hv))) 396 while ((he = hv_iternext (hv)))
399 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));
400 402
401 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));
402 } 404 }
403 405
404 if (mg) 406 if (ecb_expect_false (mg))
405 encode_ch (enc, MAJOR_MISC | MINOR_INDEF); 407 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
406 408
407 --enc->depth; 409 --enc->depth;
408} 410}
409 411
452 } 454 }
453 455
454 if (ecb_expect_false (SvREFCNT (sv) > 1) 456 if (ecb_expect_false (SvREFCNT (sv) > 1)
455 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) 457 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
456 { 458 {
457 if (!enc->shareable) 459 if (ecb_expect_false (!enc->shareable))
458 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); 460 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
459 461
460 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); 462 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
461 463
462 if (SvOK (*svp)) 464 if (SvOK (*svp))
561 if (ecb_expect_false (nv == (NV)(U32)nv)) 563 if (ecb_expect_false (nv == (NV)(U32)nv))
562 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 564 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
563 //TODO: maybe I32? 565 //TODO: maybe I32?
564 else if (ecb_expect_false (nv == (float)nv)) 566 else if (ecb_expect_false (nv == (float)nv))
565 { 567 {
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569
566 uint32_t fp = ecb_float_to_binary32 (nv); 570 uint32_t fp = ecb_float_to_binary32 (nv);
567
568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
569 571
570 if (!ecb_big_endian ()) 572 if (!ecb_big_endian ())
571 fp = ecb_bswap32 (fp); 573 fp = ecb_bswap32 (fp);
572 574
573 memcpy (enc->cur, &fp, 4); 575 memcpy (enc->cur, &fp, 4);
574 enc->cur += 4; 576 enc->cur += 4;
575 } 577 }
576 else 578 else
577 { 579 {
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581
578 uint64_t fp = ecb_double_to_binary64 (nv); 582 uint64_t fp = ecb_double_to_binary64 (nv);
579
580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
581 583
582 if (!ecb_big_endian ()) 584 if (!ecb_big_endian ())
583 fp = ecb_bswap64 (fp); 585 fp = ecb_bswap64 (fp);
584 586
585 memcpy (enc->cur, &fp, 8); 587 memcpy (enc->cur, &fp, 8);
623static SV * 625static SV *
624encode_cbor (SV *scalar, CBOR *cbor) 626encode_cbor (SV *scalar, CBOR *cbor)
625{ 627{
626 enc_t enc = { 0 }; 628 enc_t enc = { 0 };
627 629
628 enc.cbor = *cbor; 630 enc.cbor = *cbor;
629 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
630 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
631 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
632 634
633 SvPOK_only (enc.sv); 635 SvPOK_only (enc.sv);
634 636
635 if (cbor->flags & F_PACK_STRINGS) 637 if (cbor->flags & F_PACK_STRINGS)
636 { 638 {
694 696
695ecb_cold static void 697ecb_cold static void
696err_unexpected_end (dec_t *dec) 698err_unexpected_end (dec_t *dec)
697{ 699{
698 err_set (dec, "unexpected end of CBOR data"); 700 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} 701}
706 702
707#define ERR_DO(do) SB do; goto fail; SE 703#define ERR_DO(do) SB do; goto fail; SE
708#define ERR(reason) ERR_DO (err_set (dec, reason)) 704#define ERR(reason) ERR_DO (err_set (dec, reason))
709#define ERR_ERRSV ERR_DO (err_errsv (dec)) 705#define ERR_ERRSV ERR_DO (err_errsv (dec))
784 780
785 for (;;) 781 for (;;)
786 { 782 {
787 WANT (1); 783 WANT (1);
788 784
789 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 785 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
790 { 786 {
791 ++dec->cur; 787 ++dec->cur;
792 break; 788 break;
793 } 789 }
794 790
808 804
809 DEC_DEC_DEPTH; 805 DEC_DEC_DEPTH;
810 return newRV_noinc ((SV *)av); 806 return newRV_noinc ((SV *)av);
811 807
812fail: 808fail:
813 SvREFCNT_dec (av); 809 SvREFCNT_dec_NN (av);
814 DEC_DEC_DEPTH; 810 DEC_DEC_DEPTH;
815 return &PL_sv_undef; 811 return &PL_sv_undef;
816} 812}
817 813
818static void 814static void
881 877
882 return; 878 return;
883 } 879 }
884 880
885 hv_store_ent (hv, k, v, 0); 881 hv_store_ent (hv, k, v, 0);
886 SvREFCNT_dec (k); 882 SvREFCNT_dec_NN (k);
887 883
888fail: 884fail:
889 ; 885 ;
890} 886}
891 887
902 898
903 for (;;) 899 for (;;)
904 { 900 {
905 WANT (1); 901 WANT (1);
906 902
907 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 903 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
908 { 904 {
909 ++dec->cur; 905 ++dec->cur;
910 break; 906 break;
911 } 907 }
912 908
925 921
926 DEC_DEC_DEPTH; 922 DEC_DEC_DEPTH;
927 return newRV_noinc ((SV *)hv); 923 return newRV_noinc ((SV *)hv);
928 924
929fail: 925fail:
930 SvREFCNT_dec (hv); 926 SvREFCNT_dec_NN (hv);
931 DEC_DEC_DEPTH; 927 DEC_DEC_DEPTH;
932 return &PL_sv_undef; 928 return &PL_sv_undef;
933} 929}
934 930
935static SV * 931static SV *
936decode_str (dec_t *dec, int utf8) 932decode_str (dec_t *dec, int utf8)
937{ 933{
938 SV *sv = 0; 934 SV *sv = 0;
939 935
940 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 936 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
941 { 937 {
942 // indefinite length strings 938 // indefinite length strings
943 ++dec->cur; 939 ++dec->cur;
944 940
945 U8 major = *dec->cur & MAJOR_MISC; 941 U8 major = *dec->cur & MAJOR_MISC;
1013 sv = newRV_noinc (decode_sv (dec)); 1009 sv = newRV_noinc (decode_sv (dec));
1014 break; 1010 break;
1015 1011
1016 case CBOR_TAG_STRINGREF_NAMESPACE: 1012 case CBOR_TAG_STRINGREF_NAMESPACE:
1017 { 1013 {
1018 // do nmot use SAVETMPS/FREETMPS, as these will 1014 // do not use SAVETMPS/FREETMPS, as these will
1019 // erase mortalised caches, e.g. "shareable" 1015 // erase mortalised caches, e.g. "shareable"
1020 ENTER; 1016 ENTER;
1021 1017
1022 SAVESPTR (dec->stringref); 1018 SAVESPTR (dec->stringref);
1023 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1019 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
1033 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1029 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1034 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1030 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
1035 1031
1036 UV idx = decode_uint (dec); 1032 UV idx = decode_uint (dec);
1037 1033
1038 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1034 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
1039 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)");
1040 1036
1041 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1037 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
1042 } 1038 }
1043 break; 1039 break;
1071 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1067 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
1072 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1068 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
1073 1069
1074 UV idx = decode_uint (dec); 1070 UV idx = decode_uint (dec);
1075 1071
1076 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1072 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1077 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1073 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1078 1074
1079 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1075 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1080 1076
1081 if (sv == &PL_sv_undef) 1077 if (sv == &PL_sv_undef)
1127 { 1123 {
1128 FREETMPS; LEAVE; 1124 FREETMPS; LEAVE;
1129 ERR_ERRSV; 1125 ERR_ERRSV;
1130 } 1126 }
1131 1127
1132 SvREFCNT_dec (sv); 1128 SvREFCNT_dec_NN (sv);
1133 sv = SvREFCNT_inc (POPs); 1129 sv = SvREFCNT_inc (POPs);
1134 1130
1135 PUTBACK; 1131 PUTBACK;
1136 1132
1137 FREETMPS; LEAVE; 1133 FREETMPS; LEAVE;
1157 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);
1158 SPAGAIN; 1154 SPAGAIN;
1159 1155
1160 if (SvTRUE (ERRSV)) 1156 if (SvTRUE (ERRSV))
1161 { 1157 {
1162 SvREFCNT_dec (tag_sv); 1158 SvREFCNT_dec_NN (tag_sv);
1163 FREETMPS; LEAVE; 1159 FREETMPS; LEAVE;
1164 ERR_ERRSV; 1160 ERR_ERRSV;
1165 } 1161 }
1166 1162
1167 if (count) 1163 if (count)
1168 { 1164 {
1169 SvREFCNT_dec (tag_sv); 1165 SvREFCNT_dec_NN (tag_sv);
1170 SvREFCNT_dec (sv); 1166 SvREFCNT_dec_NN (sv);
1171 sv = SvREFCNT_inc (POPs); 1167 sv = SvREFCNT_inc_NN (POPs);
1172 } 1168 }
1173 else 1169 else
1174 { 1170 {
1175 AV *av = newAV (); 1171 AV *av = newAV ();
1176 av_push (av, tag_sv); 1172 av_push (av, tag_sv);
1319 for (i = av_len (dec.shareable) + 1; i--; ) 1315 for (i = av_len (dec.shareable) + 1; i--; )
1320 if ((svp = av_fetch (dec.shareable, i, 0))) 1316 if ((svp = av_fetch (dec.shareable, i, 0)))
1321 sv_setsv (*svp, &PL_sv_undef); 1317 sv_setsv (*svp, &PL_sv_undef);
1322 } 1318 }
1323 1319
1324 SvREFCNT_dec (sv); 1320 SvREFCNT_dec_NN (sv);
1325 1321
1326 if (dec.err_sv) 1322 if (dec.err_sv)
1327 sv_2mortal (dec.err_sv); 1323 sv_2mortal (dec.err_sv);
1328 1324
1329 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);
1672 cbor_init (&cbor); 1668 cbor_init (&cbor);
1673 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1669 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1674 XPUSHs (cborstr); 1670 XPUSHs (cborstr);
1675} 1671}
1676 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