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.50 by root, Thu Feb 25 02:29:22 2016 UTC vs.
Revision 1.58 by root, Fri Nov 25 13:27:29 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
106#define F_TEXT_STRINGS 0x00000040UL
105#define F_VALIDATE_UTF8 0x00000020UL 107#define F_VALIDATE_UTF8 0x00000080UL
106 108
107#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
108 110
109#define SB do { 111#define SB do {
110#define SE } while (0) 112#define SE } while (0)
276encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
277{ 279{
278 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
279} 281}
280 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
281ecb_inline void 306ecb_inline void
282encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
284 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
285 need (enc, len); 317 need (enc, len);
286 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
287 enc->cur += len; 319 enc->cur += len;
288} 320}
289 321
290static void 322ecb_inline void
291encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
292{ 324{
293 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
294 { 326 {
295 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
296 328
307 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
308 ++enc->stringref_idx; 340 ++enc->stringref_idx;
309 } 341 }
310 } 342 }
311 343
312 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
313} 345}
314 346
315static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
316 348
317static void 349static void
363 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
364 { 396 {
365 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
366 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
367 else 399 else
368 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));
369 401
370 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));
371 } 403 }
372 404
373 if (mg) 405 if (mg)
491 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
492 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));
493 525
494 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
495 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
496 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
497 529
498 while (count) 530 while (count)
499 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
500 532
501 PUTBACK; 533 PUTBACK;
560 592
561 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
562 { 594 {
563 STRLEN len; 595 STRLEN len;
564 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
565 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
566 } 598 }
567 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
568 encode_nv (enc, sv); 600 encode_nv (enc, sv);
569 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
570 { 602 {
633 SV *decode_tagged; 665 SV *decode_tagged;
634} dec_t; 666} dec_t;
635 667
636#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 668#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
637 669
638#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 670#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data")
639 671
640#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 672#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
641#define DEC_DEC_DEPTH --dec->depth 673#define DEC_DEC_DEPTH --dec->depth
642 674
643static UV 675static UV
644decode_uint (dec_t *dec) 676decode_uint (dec_t *dec)
645{ 677{
722 av_push (av, decode_sv (dec)); 754 av_push (av, decode_sv (dec));
723 } 755 }
724 } 756 }
725 else 757 else
726 { 758 {
727 int i, len = decode_uint (dec); 759 UV i, len = decode_uint (dec);
728 760
729 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays 761 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
730 av_fill (av, len - 1); 762 av_fill (av, len - 1);
731 763
732 for (i = 0; i < len; ++i) 764 for (i = 0; i < len; ++i)
749 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
750 782
751 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
752 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
753 { 785 {
754 I32 len = decode_uint (dec); 786 STRLEN len = decode_uint (dec);
755 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
756 788
757 WANT (len); 789 WANT (len);
758 dec->cur += len; 790 dec->cur += len;
759 791
761 793
762 return; 794 return;
763 } 795 }
764 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
765 { 797 {
766 I32 len = decode_uint (dec); 798 STRLEN len = decode_uint (dec);
767 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
768 800
769 WANT (len); 801 WANT (len);
770 dec->cur += len; 802 dec->cur += len;
771 803
812 decode_he (dec, hv); 844 decode_he (dec, hv);
813 } 845 }
814 } 846 }
815 else 847 else
816 { 848 {
817 int pairs = decode_uint (dec); 849 UV pairs = decode_uint (dec);
850
851 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
818 852
819 while (pairs--) 853 while (pairs--)
820 decode_he (dec, hv); 854 decode_he (dec, hv);
821 } 855 }
822 856
910 sv = newRV_noinc (decode_sv (dec)); 944 sv = newRV_noinc (decode_sv (dec));
911 break; 945 break;
912 946
913 case CBOR_TAG_STRINGREF_NAMESPACE: 947 case CBOR_TAG_STRINGREF_NAMESPACE:
914 { 948 {
949 // do nmot use SAVETMPS/FREETMPS, as these will
950 // erase mortalised caches, e.g. "shareable"
915 ENTER; SAVETMPS; 951 ENTER;
916 952
917 SAVESPTR (dec->stringref); 953 SAVESPTR (dec->stringref);
918 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 954 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
919 955
920 sv = decode_sv (dec); 956 sv = decode_sv (dec);
921 957
922 FREETMPS; LEAVE; 958 LEAVE;
923 } 959 }
924 break; 960 break;
925 961
926 case CBOR_TAG_STRINGREF: 962 case CBOR_TAG_STRINGREF:
927 { 963 {
1030 } 1066 }
1031 break; 1067 break;
1032 1068
1033 default: 1069 default:
1034 { 1070 {
1071 SV *tag_sv = newSVuv (tag);
1072
1035 sv = decode_sv (dec); 1073 sv = decode_sv (dec);
1036 1074
1037 dSP; 1075 dSP;
1038 ENTER; SAVETMPS; 1076 ENTER; SAVETMPS;
1039 SAVESTACK_POS (); 1077 SAVESTACK_POS ();
1040 PUSHMARK (SP); 1078 PUSHMARK (SP);
1041 EXTEND (SP, 2); 1079 EXTEND (SP, 2);
1042 PUSHs (newSVuv (tag)); 1080 PUSHs (tag_sv);
1043 PUSHs (sv); 1081 PUSHs (sv);
1044 1082
1045 PUTBACK; 1083 PUTBACK;
1046 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1084 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1047 SPAGAIN; 1085 SPAGAIN;
1048 1086
1049 if (SvTRUE (ERRSV)) 1087 if (SvTRUE (ERRSV))
1050 { 1088 {
1089 SvREFCNT_dec (tag_sv);
1051 FREETMPS; LEAVE; 1090 FREETMPS; LEAVE;
1052 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1091 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV))));
1053 } 1092 }
1054 1093
1055 if (count) 1094 if (count)
1056 { 1095 {
1096 SvREFCNT_dec (tag_sv);
1057 SvREFCNT_dec (sv); 1097 SvREFCNT_dec (sv);
1058 sv = SvREFCNT_inc (POPs); 1098 sv = SvREFCNT_inc (POPs);
1059 } 1099 }
1060 else 1100 else
1061 { 1101 {
1062 AV *av = newAV (); 1102 AV *av = newAV ();
1063 av_push (av, newSVuv (tag)); 1103 av_push (av, tag_sv);
1064 av_push (av, sv); 1104 av_push (av, sv);
1065 1105
1066 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1106 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1067 ? cbor_tagged_stash 1107 ? cbor_tagged_stash
1068 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1108 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1197 1237
1198 if (dec.err) 1238 if (dec.err)
1199 { 1239 {
1200 if (dec.shareable) 1240 if (dec.shareable)
1201 { 1241 {
1202 // need to break cyclic links, which whould all be in shareable 1242 // need to break cyclic links, which would all be in shareable
1203 int i; 1243 int i;
1204 SV **svp; 1244 SV **svp;
1205 1245
1206 for (i = av_len (dec.shareable) + 1; i--; ) 1246 for (i = av_len (dec.shareable) + 1; i--; )
1207 if ((svp = av_fetch (dec.shareable, i, 0))) 1247 if ((svp = av_fetch (dec.shareable, i, 0)))
1360 1400
1361 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1401 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1362 1402
1363 sv_cbor = newSVpv ("CBOR", 0); 1403 sv_cbor = newSVpv ("CBOR", 0);
1364 SvREADONLY_on (sv_cbor); 1404 SvREADONLY_on (sv_cbor);
1405
1406 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1365} 1407}
1366 1408
1367PROTOTYPES: DISABLE 1409PROTOTYPES: DISABLE
1368 1410
1369void CLONE (...) 1411void CLONE (...)
1390 shrink = F_SHRINK 1432 shrink = F_SHRINK
1391 allow_unknown = F_ALLOW_UNKNOWN 1433 allow_unknown = F_ALLOW_UNKNOWN
1392 allow_sharing = F_ALLOW_SHARING 1434 allow_sharing = F_ALLOW_SHARING
1393 allow_cycles = F_ALLOW_CYCLES 1435 allow_cycles = F_ALLOW_CYCLES
1394 pack_strings = F_PACK_STRINGS 1436 pack_strings = F_PACK_STRINGS
1437 text_keys = F_TEXT_KEYS
1438 text_strings = F_TEXT_STRINGS
1395 validate_utf8 = F_VALIDATE_UTF8 1439 validate_utf8 = F_VALIDATE_UTF8
1396 PPCODE: 1440 PPCODE:
1397{ 1441{
1398 if (enable) 1442 if (enable)
1399 self->flags |= ix; 1443 self->flags |= ix;
1408 get_shrink = F_SHRINK 1452 get_shrink = F_SHRINK
1409 get_allow_unknown = F_ALLOW_UNKNOWN 1453 get_allow_unknown = F_ALLOW_UNKNOWN
1410 get_allow_sharing = F_ALLOW_SHARING 1454 get_allow_sharing = F_ALLOW_SHARING
1411 get_allow_cycles = F_ALLOW_CYCLES 1455 get_allow_cycles = F_ALLOW_CYCLES
1412 get_pack_strings = F_PACK_STRINGS 1456 get_pack_strings = F_PACK_STRINGS
1457 get_text_keys = F_TEXT_KEYS
1458 get_text_strings = F_TEXT_STRINGS
1413 get_validate_utf8 = F_VALIDATE_UTF8 1459 get_validate_utf8 = F_VALIDATE_UTF8
1414 PPCODE: 1460 PPCODE:
1415 XPUSHs (boolSV (self->flags & ix)); 1461 XPUSHs (boolSV (self->flags & ix));
1416 1462
1417void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1463void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines