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.52 by root, Sun Apr 24 19:31:55 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_TEXT_KEYS 0x00000020UL
105#define F_UTF8_STRINGS 0x00000020UL 106#define F_TEXT_STRINGS 0x00000040UL
106#define F_VALIDATE_UTF8 0x00000040UL 107#define F_VALIDATE_UTF8 0x00000080UL
107 108
108#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
109 110
110#define SB do { 111#define SB do {
111#define SE } while (0) 112#define SE } while (0)
277encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
278{ 279{
279 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
280} 281}
281 282
283// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
284ecb_noinline static void
285encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
286{
287 STRLEN ulen = len;
288 U8 *p, *pend = (U8 *)str + len;
289
290 for (p = (U8 *)str; p < pend; ++p)
291 ulen += *p >> 7; // count set high bits
292
293 encode_uint (enc, MAJOR_TEXT, ulen);
294
295 need (enc, ulen);
296 for (p = (U8 *)str; p < pend; ++p)
297 if (*p < 0x80)
298 *enc->cur++ = *p;
299 else
300 {
301 *enc->cur++ = 0xc0 + (*p >> 6);
302 *enc->cur++ = 0x80 + (*p & 63);
303 }
304}
305
282ecb_inline void 306ecb_inline void
283encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
284{ 308{
285 if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) 309 if (ecb_expect_false (upgrade_utf8))
286 if (!utf8) 310 if (!utf8)
287 { 311 {
288 // exceptional path for bytze strings that need to be utf8-encoded 312 encode_str_utf8 (enc, utf8, str, len);
289 STRLEN ulen = len;
290 U8 *p, *pend = (U8 *)str + len;
291
292 for (p = (U8 *)str; p < pend; ++p)
293 ulen += *p >> 7; // count set high bits
294
295 encode_uint (enc, MAJOR_TEXT, ulen);
296
297 need (enc, ulen);
298 for (p = (U8 *)str; p < pend; ++p)
299 if (*p < 0x80)
300 *enc->cur++ = *p;
301 else
302 {
303 *enc->cur++ = 0xc0 + (*p >> 6);
304 *enc->cur++ = 0x80 + (*p & 63);
305 }
306
307 return; 313 return;
308 } 314 }
309 315
310 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
311 need (enc, len); 317 need (enc, len);
312 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
313 enc->cur += len; 319 enc->cur += len;
314} 320}
315 321
316static void 322ecb_inline void
317encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
318{ 324{
319 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
320 { 326 {
321 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
322 328
333 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
334 ++enc->stringref_idx; 340 ++enc->stringref_idx;
335 } 341 }
336 } 342 }
337 343
338 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
339} 345}
340 346
341static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
342 348
343static void 349static void
389 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
390 { 396 {
391 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
392 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
393 else 399 else
394 encode_strref (enc, 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));
395 401
396 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));
397 } 403 }
398 404
399 if (mg) 405 if (mg)
517 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
518 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 524 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
519 525
520 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
521 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
522 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
523 529
524 while (count) 530 while (count)
525 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
526 532
527 PUTBACK; 533 PUTBACK;
586 592
587 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
588 { 594 {
589 STRLEN len; 595 STRLEN len;
590 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
591 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
592 } 598 }
593 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
594 encode_nv (enc, sv); 600 encode_nv (enc, sv);
595 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
596 { 602 {
655 U32 depth; // recursion depth 661 U32 depth; // recursion depth
656 U32 maxdepth; // recursion depth limit 662 U32 maxdepth; // recursion depth limit
657 AV *shareable; 663 AV *shareable;
658 AV *stringref; 664 AV *stringref;
659 SV *decode_tagged; 665 SV *decode_tagged;
666 SV *err_sv; // optional sv for error, needs to be freed
660} dec_t; 667} dec_t;
661 668
662#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);
663 676
664#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;
665 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
666#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)
667#define DEC_DEC_DEPTH --dec->depth 712#define DEC_DEC_DEPTH --dec->depth
668 713
669static UV 714static UV
670decode_uint (dec_t *dec) 715decode_uint (dec_t *dec)
671{ 716{
748 av_push (av, decode_sv (dec)); 793 av_push (av, decode_sv (dec));
749 } 794 }
750 } 795 }
751 else 796 else
752 { 797 {
753 int i, len = decode_uint (dec); 798 UV i, len = decode_uint (dec);
754 799
755 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
756 av_fill (av, len - 1); 801 av_fill (av, len - 1);
757 802
758 for (i = 0; i < len; ++i) 803 for (i = 0; i < len; ++i)
775 // byte or utf-8 strings as keys, but only when !stringref 820 // byte or utf-8 strings as keys, but only when !stringref
776 821
777 if (ecb_expect_true (!dec->stringref)) 822 if (ecb_expect_true (!dec->stringref))
778 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 823 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
779 { 824 {
780 I32 len = decode_uint (dec); 825 STRLEN len = decode_uint (dec);
781 char *key = (char *)dec->cur; 826 char *key = (char *)dec->cur;
782 827
783 WANT (len); 828 WANT (len);
784 dec->cur += len; 829 dec->cur += len;
785 830
787 832
788 return; 833 return;
789 } 834 }
790 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))
791 { 836 {
792 I32 len = decode_uint (dec); 837 STRLEN len = decode_uint (dec);
793 char *key = (char *)dec->cur; 838 char *key = (char *)dec->cur;
794 839
795 WANT (len); 840 WANT (len);
796 dec->cur += len; 841 dec->cur += len;
797 842
804 return; 849 return;
805 } 850 }
806 851
807 SV *k = decode_sv (dec); 852 SV *k = decode_sv (dec);
808 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 }
809 882
810 hv_store_ent (hv, k, v, 0); 883 hv_store_ent (hv, k, v, 0);
811 SvREFCNT_dec (k); 884 SvREFCNT_dec (k);
812 885
813fail: 886fail:
838 decode_he (dec, hv); 911 decode_he (dec, hv);
839 } 912 }
840 } 913 }
841 else 914 else
842 { 915 {
843 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
844 919
845 while (pairs--) 920 while (pairs--)
846 decode_he (dec, hv); 921 decode_he (dec, hv);
847 } 922 }
848 923
936 sv = newRV_noinc (decode_sv (dec)); 1011 sv = newRV_noinc (decode_sv (dec));
937 break; 1012 break;
938 1013
939 case CBOR_TAG_STRINGREF_NAMESPACE: 1014 case CBOR_TAG_STRINGREF_NAMESPACE:
940 { 1015 {
1016 // do nmot use SAVETMPS/FREETMPS, as these will
1017 // erase mortalised caches, e.g. "shareable"
941 ENTER; SAVETMPS; 1018 ENTER;
942 1019
943 SAVESPTR (dec->stringref); 1020 SAVESPTR (dec->stringref);
944 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1021 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
945 1022
946 sv = decode_sv (dec); 1023 sv = decode_sv (dec);
947 1024
948 FREETMPS; LEAVE; 1025 LEAVE;
949 } 1026 }
950 break; 1027 break;
951 1028
952 case CBOR_TAG_STRINGREF: 1029 case CBOR_TAG_STRINGREF:
953 { 1030 {
1042 SPAGAIN; 1119 SPAGAIN;
1043 1120
1044 if (SvTRUE (ERRSV)) 1121 if (SvTRUE (ERRSV))
1045 { 1122 {
1046 FREETMPS; LEAVE; 1123 FREETMPS; LEAVE;
1047 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1124 ERR_ERRSV;
1048 } 1125 }
1049 1126
1050 SvREFCNT_dec (sv); 1127 SvREFCNT_dec (sv);
1051 sv = SvREFCNT_inc (POPs); 1128 sv = SvREFCNT_inc (POPs);
1052 1129
1056 } 1133 }
1057 break; 1134 break;
1058 1135
1059 default: 1136 default:
1060 { 1137 {
1138 SV *tag_sv = newSVuv (tag);
1139
1061 sv = decode_sv (dec); 1140 sv = decode_sv (dec);
1062 1141
1063 dSP; 1142 dSP;
1064 ENTER; SAVETMPS; 1143 ENTER; SAVETMPS;
1065 SAVESTACK_POS (); 1144 SAVESTACK_POS ();
1066 PUSHMARK (SP); 1145 PUSHMARK (SP);
1067 EXTEND (SP, 2); 1146 EXTEND (SP, 2);
1068 PUSHs (newSVuv (tag)); 1147 PUSHs (tag_sv);
1069 PUSHs (sv); 1148 PUSHs (sv);
1070 1149
1071 PUTBACK; 1150 PUTBACK;
1072 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);
1073 SPAGAIN; 1152 SPAGAIN;
1074 1153
1075 if (SvTRUE (ERRSV)) 1154 if (SvTRUE (ERRSV))
1076 { 1155 {
1156 SvREFCNT_dec (tag_sv);
1077 FREETMPS; LEAVE; 1157 FREETMPS; LEAVE;
1078 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1158 ERR_ERRSV;
1079 } 1159 }
1080 1160
1081 if (count) 1161 if (count)
1082 { 1162 {
1163 SvREFCNT_dec (tag_sv);
1083 SvREFCNT_dec (sv); 1164 SvREFCNT_dec (sv);
1084 sv = SvREFCNT_inc (POPs); 1165 sv = SvREFCNT_inc (POPs);
1085 } 1166 }
1086 else 1167 else
1087 { 1168 {
1088 AV *av = newAV (); 1169 AV *av = newAV ();
1089 av_push (av, newSVuv (tag)); 1170 av_push (av, tag_sv);
1090 av_push (av, sv); 1171 av_push (av, sv);
1091 1172
1092 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1173 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1093 ? cbor_tagged_stash 1174 ? cbor_tagged_stash
1094 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1175 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1223 1304
1224 if (dec.err) 1305 if (dec.err)
1225 { 1306 {
1226 if (dec.shareable) 1307 if (dec.shareable)
1227 { 1308 {
1228 // need to break cyclic links, which whould all be in shareable 1309 // need to break cyclic links, which would all be in shareable
1229 int i; 1310 int i;
1230 SV **svp; 1311 SV **svp;
1231 1312
1232 for (i = av_len (dec.shareable) + 1; i--; ) 1313 for (i = av_len (dec.shareable) + 1; i--; )
1233 if ((svp = av_fetch (dec.shareable, i, 0))) 1314 if ((svp = av_fetch (dec.shareable, i, 0)))
1234 sv_setsv (*svp, &PL_sv_undef); 1315 sv_setsv (*svp, &PL_sv_undef);
1235 } 1316 }
1236 1317
1237 SvREFCNT_dec (sv); 1318 SvREFCNT_dec (sv);
1319
1320 if (dec.err_sv)
1321 sv_2mortal (dec.err_sv);
1322
1238 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);
1239 } 1324 }
1240 1325
1241 sv = sv_2mortal (sv); 1326 sv = sv_2mortal (sv);
1242 1327
1386 1471
1387 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1472 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1388 1473
1389 sv_cbor = newSVpv ("CBOR", 0); 1474 sv_cbor = newSVpv ("CBOR", 0);
1390 SvREADONLY_on (sv_cbor); 1475 SvREADONLY_on (sv_cbor);
1476
1477 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1391} 1478}
1392 1479
1393PROTOTYPES: DISABLE 1480PROTOTYPES: DISABLE
1394 1481
1395void CLONE (...) 1482void CLONE (...)
1416 shrink = F_SHRINK 1503 shrink = F_SHRINK
1417 allow_unknown = F_ALLOW_UNKNOWN 1504 allow_unknown = F_ALLOW_UNKNOWN
1418 allow_sharing = F_ALLOW_SHARING 1505 allow_sharing = F_ALLOW_SHARING
1419 allow_cycles = F_ALLOW_CYCLES 1506 allow_cycles = F_ALLOW_CYCLES
1420 pack_strings = F_PACK_STRINGS 1507 pack_strings = F_PACK_STRINGS
1508 text_keys = F_TEXT_KEYS
1421 utf8_strings = F_UTF8_STRINGS 1509 text_strings = F_TEXT_STRINGS
1422 validate_utf8 = F_VALIDATE_UTF8 1510 validate_utf8 = F_VALIDATE_UTF8
1423 PPCODE: 1511 PPCODE:
1424{ 1512{
1425 if (enable) 1513 if (enable)
1426 self->flags |= ix; 1514 self->flags |= ix;
1435 get_shrink = F_SHRINK 1523 get_shrink = F_SHRINK
1436 get_allow_unknown = F_ALLOW_UNKNOWN 1524 get_allow_unknown = F_ALLOW_UNKNOWN
1437 get_allow_sharing = F_ALLOW_SHARING 1525 get_allow_sharing = F_ALLOW_SHARING
1438 get_allow_cycles = F_ALLOW_CYCLES 1526 get_allow_cycles = F_ALLOW_CYCLES
1439 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
1440 get_validate_utf8 = F_VALIDATE_UTF8 1530 get_validate_utf8 = F_VALIDATE_UTF8
1441 PPCODE: 1531 PPCODE:
1442 XPUSHs (boolSV (self->flags & ix)); 1532 XPUSHs (boolSV (self->flags & ix));
1443 1533
1444void 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