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.56 by root, Fri Nov 25 11:33:03 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 {
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 (dec->cur + len > dec->end)) 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{
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
910 sv = newRV_noinc (decode_sv (dec)); 942 sv = newRV_noinc (decode_sv (dec));
911 break; 943 break;
912 944
913 case CBOR_TAG_STRINGREF_NAMESPACE: 945 case CBOR_TAG_STRINGREF_NAMESPACE:
914 { 946 {
947 // do nmot use SAVETMPS/FREETMPS, as these will
948 // erase mortalised caches, e.g. "shareable"
915 ENTER; SAVETMPS; 949 ENTER;
916 950
917 SAVESPTR (dec->stringref); 951 SAVESPTR (dec->stringref);
918 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 952 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
919 953
920 sv = decode_sv (dec); 954 sv = decode_sv (dec);
921 955
922 FREETMPS; LEAVE; 956 LEAVE;
923 } 957 }
924 break; 958 break;
925 959
926 case CBOR_TAG_STRINGREF: 960 case CBOR_TAG_STRINGREF:
927 { 961 {
1360 1394
1361 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1395 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1362 1396
1363 sv_cbor = newSVpv ("CBOR", 0); 1397 sv_cbor = newSVpv ("CBOR", 0);
1364 SvREADONLY_on (sv_cbor); 1398 SvREADONLY_on (sv_cbor);
1399
1400 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1365} 1401}
1366 1402
1367PROTOTYPES: DISABLE 1403PROTOTYPES: DISABLE
1368 1404
1369void CLONE (...) 1405void CLONE (...)
1390 shrink = F_SHRINK 1426 shrink = F_SHRINK
1391 allow_unknown = F_ALLOW_UNKNOWN 1427 allow_unknown = F_ALLOW_UNKNOWN
1392 allow_sharing = F_ALLOW_SHARING 1428 allow_sharing = F_ALLOW_SHARING
1393 allow_cycles = F_ALLOW_CYCLES 1429 allow_cycles = F_ALLOW_CYCLES
1394 pack_strings = F_PACK_STRINGS 1430 pack_strings = F_PACK_STRINGS
1431 text_keys = F_TEXT_KEYS
1432 text_strings = F_TEXT_STRINGS
1395 validate_utf8 = F_VALIDATE_UTF8 1433 validate_utf8 = F_VALIDATE_UTF8
1396 PPCODE: 1434 PPCODE:
1397{ 1435{
1398 if (enable) 1436 if (enable)
1399 self->flags |= ix; 1437 self->flags |= ix;
1408 get_shrink = F_SHRINK 1446 get_shrink = F_SHRINK
1409 get_allow_unknown = F_ALLOW_UNKNOWN 1447 get_allow_unknown = F_ALLOW_UNKNOWN
1410 get_allow_sharing = F_ALLOW_SHARING 1448 get_allow_sharing = F_ALLOW_SHARING
1411 get_allow_cycles = F_ALLOW_CYCLES 1449 get_allow_cycles = F_ALLOW_CYCLES
1412 get_pack_strings = F_PACK_STRINGS 1450 get_pack_strings = F_PACK_STRINGS
1451 get_text_keys = F_TEXT_KEYS
1452 get_text_strings = F_TEXT_STRINGS
1413 get_validate_utf8 = F_VALIDATE_UTF8 1453 get_validate_utf8 = F_VALIDATE_UTF8
1414 PPCODE: 1454 PPCODE:
1415 XPUSHs (boolSV (self->flags & ix)); 1455 XPUSHs (boolSV (self->flags & ix));
1416 1456
1417void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1457void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines