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.51 by root, Sun Apr 24 13:15:19 2016 UTC vs.
Revision 1.57 by root, Fri Nov 25 12:16:12 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 SV *sv = sv_newmortal ();
289 char *s; STRLEN l;
290
291 sv_setpvn (sv, str, len);
292
293 s = SvPVutf8 (sv, l);
294 encode_str (enc, 1, s, l); 312 encode_str_utf8 (enc, utf8, str, len);
295 return; 313 return;
296 } 314 }
297 315
298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
299 need (enc, len); 317 need (enc, len);
300 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
301 enc->cur += len; 319 enc->cur += len;
302} 320}
303 321
304static void 322ecb_inline void
305encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
306{ 324{
307 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
308 { 326 {
309 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
310 328
321 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
322 ++enc->stringref_idx; 340 ++enc->stringref_idx;
323 } 341 }
324 } 342 }
325 343
326 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
327} 345}
328 346
329static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
330 348
331static void 349static void
377 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
378 { 396 {
379 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
380 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
381 else 399 else
382 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));
383 401
384 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));
385 } 403 }
386 404
387 if (mg) 405 if (mg)
505 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
506 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));
507 525
508 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
509 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
510 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
511 529
512 while (count) 530 while (count)
513 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
514 532
515 PUTBACK; 533 PUTBACK;
574 592
575 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
576 { 594 {
577 STRLEN len; 595 STRLEN len;
578 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
579 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
580 } 598 }
581 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
582 encode_nv (enc, sv); 600 encode_nv (enc, sv);
583 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
584 { 602 {
647 SV *decode_tagged; 665 SV *decode_tagged;
648} dec_t; 666} dec_t;
649 667
650#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
651 669
652#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")
653 671
654#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)
655#define DEC_DEC_DEPTH --dec->depth 673#define DEC_DEC_DEPTH --dec->depth
656 674
657static UV 675static UV
658decode_uint (dec_t *dec) 676decode_uint (dec_t *dec)
659{ 677{
736 av_push (av, decode_sv (dec)); 754 av_push (av, decode_sv (dec));
737 } 755 }
738 } 756 }
739 else 757 else
740 { 758 {
741 int i, len = decode_uint (dec); 759 UV i, len = decode_uint (dec);
742 760
743 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
744 av_fill (av, len - 1); 762 av_fill (av, len - 1);
745 763
746 for (i = 0; i < len; ++i) 764 for (i = 0; i < len; ++i)
763 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
764 782
765 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
766 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
767 { 785 {
768 I32 len = decode_uint (dec); 786 STRLEN len = decode_uint (dec);
769 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
770 788
771 WANT (len); 789 WANT (len);
772 dec->cur += len; 790 dec->cur += len;
773 791
775 793
776 return; 794 return;
777 } 795 }
778 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))
779 { 797 {
780 I32 len = decode_uint (dec); 798 STRLEN len = decode_uint (dec);
781 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
782 800
783 WANT (len); 801 WANT (len);
784 dec->cur += len; 802 dec->cur += len;
785 803
826 decode_he (dec, hv); 844 decode_he (dec, hv);
827 } 845 }
828 } 846 }
829 else 847 else
830 { 848 {
831 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
832 852
833 while (pairs--) 853 while (pairs--)
834 decode_he (dec, hv); 854 decode_he (dec, hv);
835 } 855 }
836 856
924 sv = newRV_noinc (decode_sv (dec)); 944 sv = newRV_noinc (decode_sv (dec));
925 break; 945 break;
926 946
927 case CBOR_TAG_STRINGREF_NAMESPACE: 947 case CBOR_TAG_STRINGREF_NAMESPACE:
928 { 948 {
949 // do nmot use SAVETMPS/FREETMPS, as these will
950 // erase mortalised caches, e.g. "shareable"
929 ENTER; SAVETMPS; 951 ENTER;
930 952
931 SAVESPTR (dec->stringref); 953 SAVESPTR (dec->stringref);
932 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 954 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
933 955
934 sv = decode_sv (dec); 956 sv = decode_sv (dec);
935 957
936 FREETMPS; LEAVE; 958 LEAVE;
937 } 959 }
938 break; 960 break;
939 961
940 case CBOR_TAG_STRINGREF: 962 case CBOR_TAG_STRINGREF:
941 { 963 {
1374 1396
1375 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1397 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1376 1398
1377 sv_cbor = newSVpv ("CBOR", 0); 1399 sv_cbor = newSVpv ("CBOR", 0);
1378 SvREADONLY_on (sv_cbor); 1400 SvREADONLY_on (sv_cbor);
1401
1402 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1379} 1403}
1380 1404
1381PROTOTYPES: DISABLE 1405PROTOTYPES: DISABLE
1382 1406
1383void CLONE (...) 1407void CLONE (...)
1404 shrink = F_SHRINK 1428 shrink = F_SHRINK
1405 allow_unknown = F_ALLOW_UNKNOWN 1429 allow_unknown = F_ALLOW_UNKNOWN
1406 allow_sharing = F_ALLOW_SHARING 1430 allow_sharing = F_ALLOW_SHARING
1407 allow_cycles = F_ALLOW_CYCLES 1431 allow_cycles = F_ALLOW_CYCLES
1408 pack_strings = F_PACK_STRINGS 1432 pack_strings = F_PACK_STRINGS
1433 text_keys = F_TEXT_KEYS
1409 utf8_strings = F_UTF8_STRINGS 1434 text_strings = F_TEXT_STRINGS
1410 validate_utf8 = F_VALIDATE_UTF8 1435 validate_utf8 = F_VALIDATE_UTF8
1411 PPCODE: 1436 PPCODE:
1412{ 1437{
1413 if (enable) 1438 if (enable)
1414 self->flags |= ix; 1439 self->flags |= ix;
1423 get_shrink = F_SHRINK 1448 get_shrink = F_SHRINK
1424 get_allow_unknown = F_ALLOW_UNKNOWN 1449 get_allow_unknown = F_ALLOW_UNKNOWN
1425 get_allow_sharing = F_ALLOW_SHARING 1450 get_allow_sharing = F_ALLOW_SHARING
1426 get_allow_cycles = F_ALLOW_CYCLES 1451 get_allow_cycles = F_ALLOW_CYCLES
1427 get_pack_strings = F_PACK_STRINGS 1452 get_pack_strings = F_PACK_STRINGS
1453 get_text_keys = F_TEXT_KEYS
1454 get_text_strings = F_TEXT_STRINGS
1428 get_validate_utf8 = F_VALIDATE_UTF8 1455 get_validate_utf8 = F_VALIDATE_UTF8
1429 PPCODE: 1456 PPCODE:
1430 XPUSHs (boolSV (self->flags & ix)); 1457 XPUSHs (boolSV (self->flags & ix));
1431 1458
1432void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1459void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines