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.53 by root, Sun Apr 24 19:51:41 2016 UTC vs.
Revision 1.59 by root, Fri Nov 25 23:37:27 2016 UTC

100#define F_SHRINK 0x00000001UL 100#define F_SHRINK 0x00000001UL
101#define F_ALLOW_UNKNOWN 0x00000002UL 101#define F_ALLOW_UNKNOWN 0x00000002UL
102#define F_ALLOW_SHARING 0x00000004UL 102#define F_ALLOW_SHARING 0x00000004UL
103#define F_ALLOW_CYCLES 0x00000008UL 103#define F_ALLOW_CYCLES 0x00000008UL
104#define F_PACK_STRINGS 0x00000010UL 104#define F_PACK_STRINGS 0x00000010UL
105#define F_UTF8_STRINGS 0x00000020UL
106#define F_UTF8_KEYS 0x00000040UL 105#define F_TEXT_KEYS 0x00000020UL
106#define F_TEXT_STRINGS 0x00000040UL
107#define F_VALIDATE_UTF8 0x00000080UL 107#define F_VALIDATE_UTF8 0x00000080UL
108 108
109#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
110 110
111#define SB do { 111#define SB do {
317 need (enc, len); 317 need (enc, len);
318 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
319 enc->cur += len; 319 enc->cur += len;
320} 320}
321 321
322ecb_inline 322ecb_inline void
323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
324{ 324{
325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
326 { 326 {
327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
395 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
396 { 396 {
397 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
398 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
399 else 399 else
400 encode_strref (enc, enc->cbor.flags & (F_UTF8_KEYS | F_UTF8_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 400 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
401 401
402 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 402 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
403 } 403 }
404 404
405 if (mg) 405 if (mg)
592 592
593 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
594 { 594 {
595 STRLEN len; 595 STRLEN len;
596 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
597 encode_strref (enc, enc->cbor.flags & F_UTF8_STRINGS, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
598 } 598 }
599 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
600 encode_nv (enc, sv); 600 encode_nv (enc, sv);
601 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
602 { 602 {
661 U32 depth; // recursion depth 661 U32 depth; // recursion depth
662 U32 maxdepth; // recursion depth limit 662 U32 maxdepth; // recursion depth limit
663 AV *shareable; 663 AV *shareable;
664 AV *stringref; 664 AV *stringref;
665 SV *decode_tagged; 665 SV *decode_tagged;
666 SV *err_sv; // optional sv for error, needs to be freed
666} dec_t; 667} dec_t;
667 668
668#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 669// set dec->err to ERRSV
670ecb_cold static void
671err_errsv (dec_t *dec)
672{
673 if (!dec->err)
674 {
675 dec->err_sv = newSVsv (ERRSV);
669 676
670#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 677 // chop off the trailing \n
678 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
679 *SvEND (dec->err_sv) = 0;
671 680
681 dec->err = SvPVutf8_nolen (dec->err_sv);
682 }
683}
684
685// the following functions are used to reduce code size and help the compiler to optimise
686ecb_cold static void
687err_set (dec_t *dec, const char *reason)
688{
689 if (!dec->err)
690 dec->err = reason;
691}
692
693ecb_cold static void
694err_unexpected_end (dec_t *dec)
695{
696 err_set (dec, "unexpected end of CBOR data");
697}
698
699ecb_cold static void
700err_nesting_exceeded (dec_t *dec)
701{
702 err_set (dec, ERR_NESTING_EXCEEDED);
703}
704
705#define ERR_DO(do) SB do; goto fail; SE
706#define ERR(reason) ERR_DO (err_set (dec, reason))
707#define ERR_ERRSV ERR_DO (err_errsv (dec))
708
709#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec))
710
672#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 711#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
673#define DEC_DEC_DEPTH --dec->depth 712#define DEC_DEC_DEPTH --dec->depth
674 713
675static UV 714static UV
676decode_uint (dec_t *dec) 715decode_uint (dec_t *dec)
677{ 716{
754 av_push (av, decode_sv (dec)); 793 av_push (av, decode_sv (dec));
755 } 794 }
756 } 795 }
757 else 796 else
758 { 797 {
759 int i, len = decode_uint (dec); 798 UV i, len = decode_uint (dec);
760 799
761 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays 800 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
762 av_fill (av, len - 1); 801 av_fill (av, len - 1);
763 802
764 for (i = 0; i < len; ++i) 803 for (i = 0; i < len; ++i)
781 // byte or utf-8 strings as keys, but only when !stringref 820 // byte or utf-8 strings as keys, but only when !stringref
782 821
783 if (ecb_expect_true (!dec->stringref)) 822 if (ecb_expect_true (!dec->stringref))
784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 823 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
785 { 824 {
786 I32 len = decode_uint (dec); 825 STRLEN len = decode_uint (dec);
787 char *key = (char *)dec->cur; 826 char *key = (char *)dec->cur;
788 827
789 WANT (len); 828 WANT (len);
790 dec->cur += len; 829 dec->cur += len;
791 830
793 832
794 return; 833 return;
795 } 834 }
796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 835 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
797 { 836 {
798 I32 len = decode_uint (dec); 837 STRLEN len = decode_uint (dec);
799 char *key = (char *)dec->cur; 838 char *key = (char *)dec->cur;
800 839
801 WANT (len); 840 WANT (len);
802 dec->cur += len; 841 dec->cur += len;
803 842
810 return; 849 return;
811 } 850 }
812 851
813 SV *k = decode_sv (dec); 852 SV *k = decode_sv (dec);
814 SV *v = decode_sv (dec); 853 SV *v = decode_sv (dec);
854
855 // we leak memory if uncaught exceptions are thrown by random magical
856 // methods, and this is hopefully the only place where it can happen,
857 // so if there is a chance of an exception, take the very slow path.
858 // since catching exceptions is "undocumented/internal/forbidden" by
859 // the new p5p powers, we need to call out to a perl function :/
860 if (ecb_expect_false (SvAMAGIC (k)))
861 {
862 dSP;
863
864 ENTER; SAVETMPS;
865 PUSHMARK (SP);
866 EXTEND (SP, 3);
867 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
868 PUSHs (sv_2mortal (k));
869 PUSHs (sv_2mortal (v));
870
871 PUTBACK;
872 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
873 SPAGAIN;
874
875 FREETMPS; LEAVE;
876
877 if (SvTRUE (ERRSV))
878 ERR_ERRSV;
879
880 return;
881 }
815 882
816 hv_store_ent (hv, k, v, 0); 883 hv_store_ent (hv, k, v, 0);
817 SvREFCNT_dec (k); 884 SvREFCNT_dec (k);
818 885
819fail: 886fail:
844 decode_he (dec, hv); 911 decode_he (dec, hv);
845 } 912 }
846 } 913 }
847 else 914 else
848 { 915 {
849 int pairs = decode_uint (dec); 916 UV pairs = decode_uint (dec);
917
918 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
850 919
851 while (pairs--) 920 while (pairs--)
852 decode_he (dec, hv); 921 decode_he (dec, hv);
853 } 922 }
854 923
942 sv = newRV_noinc (decode_sv (dec)); 1011 sv = newRV_noinc (decode_sv (dec));
943 break; 1012 break;
944 1013
945 case CBOR_TAG_STRINGREF_NAMESPACE: 1014 case CBOR_TAG_STRINGREF_NAMESPACE:
946 { 1015 {
1016 // do nmot use SAVETMPS/FREETMPS, as these will
1017 // erase mortalised caches, e.g. "shareable"
947 ENTER; SAVETMPS; 1018 ENTER;
948 1019
949 SAVESPTR (dec->stringref); 1020 SAVESPTR (dec->stringref);
950 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1021 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
951 1022
952 sv = decode_sv (dec); 1023 sv = decode_sv (dec);
953 1024
954 FREETMPS; LEAVE; 1025 LEAVE;
955 } 1026 }
956 break; 1027 break;
957 1028
958 case CBOR_TAG_STRINGREF: 1029 case CBOR_TAG_STRINGREF:
959 { 1030 {
1048 SPAGAIN; 1119 SPAGAIN;
1049 1120
1050 if (SvTRUE (ERRSV)) 1121 if (SvTRUE (ERRSV))
1051 { 1122 {
1052 FREETMPS; LEAVE; 1123 FREETMPS; LEAVE;
1053 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1124 ERR_ERRSV;
1054 } 1125 }
1055 1126
1056 SvREFCNT_dec (sv); 1127 SvREFCNT_dec (sv);
1057 sv = SvREFCNT_inc (POPs); 1128 sv = SvREFCNT_inc (POPs);
1058 1129
1062 } 1133 }
1063 break; 1134 break;
1064 1135
1065 default: 1136 default:
1066 { 1137 {
1138 SV *tag_sv = newSVuv (tag);
1139
1067 sv = decode_sv (dec); 1140 sv = decode_sv (dec);
1068 1141
1069 dSP; 1142 dSP;
1070 ENTER; SAVETMPS; 1143 ENTER; SAVETMPS;
1071 SAVESTACK_POS (); 1144 SAVESTACK_POS ();
1072 PUSHMARK (SP); 1145 PUSHMARK (SP);
1073 EXTEND (SP, 2); 1146 EXTEND (SP, 2);
1074 PUSHs (newSVuv (tag)); 1147 PUSHs (tag_sv);
1075 PUSHs (sv); 1148 PUSHs (sv);
1076 1149
1077 PUTBACK; 1150 PUTBACK;
1078 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1151 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1079 SPAGAIN; 1152 SPAGAIN;
1080 1153
1081 if (SvTRUE (ERRSV)) 1154 if (SvTRUE (ERRSV))
1082 { 1155 {
1156 SvREFCNT_dec (tag_sv);
1083 FREETMPS; LEAVE; 1157 FREETMPS; LEAVE;
1084 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1158 ERR_ERRSV;
1085 } 1159 }
1086 1160
1087 if (count) 1161 if (count)
1088 { 1162 {
1163 SvREFCNT_dec (tag_sv);
1089 SvREFCNT_dec (sv); 1164 SvREFCNT_dec (sv);
1090 sv = SvREFCNT_inc (POPs); 1165 sv = SvREFCNT_inc (POPs);
1091 } 1166 }
1092 else 1167 else
1093 { 1168 {
1094 AV *av = newAV (); 1169 AV *av = newAV ();
1095 av_push (av, newSVuv (tag)); 1170 av_push (av, tag_sv);
1096 av_push (av, sv); 1171 av_push (av, sv);
1097 1172
1098 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1173 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1099 ? cbor_tagged_stash 1174 ? cbor_tagged_stash
1100 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1175 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1229 1304
1230 if (dec.err) 1305 if (dec.err)
1231 { 1306 {
1232 if (dec.shareable) 1307 if (dec.shareable)
1233 { 1308 {
1234 // need to break cyclic links, which whould all be in shareable 1309 // need to break cyclic links, which would all be in shareable
1235 int i; 1310 int i;
1236 SV **svp; 1311 SV **svp;
1237 1312
1238 for (i = av_len (dec.shareable) + 1; i--; ) 1313 for (i = av_len (dec.shareable) + 1; i--; )
1239 if ((svp = av_fetch (dec.shareable, i, 0))) 1314 if ((svp = av_fetch (dec.shareable, i, 0)))
1240 sv_setsv (*svp, &PL_sv_undef); 1315 sv_setsv (*svp, &PL_sv_undef);
1241 } 1316 }
1242 1317
1243 SvREFCNT_dec (sv); 1318 SvREFCNT_dec (sv);
1319
1320 if (dec.err_sv)
1321 sv_2mortal (dec.err_sv);
1322
1244 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1323 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1245 } 1324 }
1246 1325
1247 sv = sv_2mortal (sv); 1326 sv = sv_2mortal (sv);
1248 1327
1392 1471
1393 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1472 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1394 1473
1395 sv_cbor = newSVpv ("CBOR", 0); 1474 sv_cbor = newSVpv ("CBOR", 0);
1396 SvREADONLY_on (sv_cbor); 1475 SvREADONLY_on (sv_cbor);
1476
1477 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1397} 1478}
1398 1479
1399PROTOTYPES: DISABLE 1480PROTOTYPES: DISABLE
1400 1481
1401void CLONE (...) 1482void CLONE (...)
1422 shrink = F_SHRINK 1503 shrink = F_SHRINK
1423 allow_unknown = F_ALLOW_UNKNOWN 1504 allow_unknown = F_ALLOW_UNKNOWN
1424 allow_sharing = F_ALLOW_SHARING 1505 allow_sharing = F_ALLOW_SHARING
1425 allow_cycles = F_ALLOW_CYCLES 1506 allow_cycles = F_ALLOW_CYCLES
1426 pack_strings = F_PACK_STRINGS 1507 pack_strings = F_PACK_STRINGS
1508 text_keys = F_TEXT_KEYS
1427 utf8_strings = F_UTF8_STRINGS 1509 text_strings = F_TEXT_STRINGS
1428 validate_utf8 = F_VALIDATE_UTF8 1510 validate_utf8 = F_VALIDATE_UTF8
1429 PPCODE: 1511 PPCODE:
1430{ 1512{
1431 if (enable) 1513 if (enable)
1432 self->flags |= ix; 1514 self->flags |= ix;
1441 get_shrink = F_SHRINK 1523 get_shrink = F_SHRINK
1442 get_allow_unknown = F_ALLOW_UNKNOWN 1524 get_allow_unknown = F_ALLOW_UNKNOWN
1443 get_allow_sharing = F_ALLOW_SHARING 1525 get_allow_sharing = F_ALLOW_SHARING
1444 get_allow_cycles = F_ALLOW_CYCLES 1526 get_allow_cycles = F_ALLOW_CYCLES
1445 get_pack_strings = F_PACK_STRINGS 1527 get_pack_strings = F_PACK_STRINGS
1528 get_text_keys = F_TEXT_KEYS
1529 get_text_strings = F_TEXT_STRINGS
1446 get_validate_utf8 = F_VALIDATE_UTF8 1530 get_validate_utf8 = F_VALIDATE_UTF8
1447 PPCODE: 1531 PPCODE:
1448 XPUSHs (boolSV (self->flags & ix)); 1532 XPUSHs (boolSV (self->flags & ix));
1449 1533
1450void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1534void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines