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.45 by root, Sun Dec 14 05:48:39 2014 UTC vs.
Revision 1.55 by root, Fri Nov 25 06:13:16 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
332 SV **svp = av_fetch (av, i, 0); 364 SV **svp = av_fetch (av, i, 0);
333 encode_sv (enc, svp ? *svp : &PL_sv_undef); 365 encode_sv (enc, svp ? *svp : &PL_sv_undef);
334 } 366 }
335 else 367 else
336 for (i = 0; i <= len; ++i) 368 for (i = 0; i <= len; ++i)
337 encode_sv (enc, AvARRAY (av)[i]); 369 {
370 SV *sv = AvARRAY (av)[i];
371 encode_sv (enc, sv ? sv : &PL_sv_undef);
372 }
338 373
339 --enc->depth; 374 --enc->depth;
340} 375}
341 376
342static void 377static void
360 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
361 { 396 {
362 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
363 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
364 else 399 else
365 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));
366 401
367 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));
368 } 403 }
369 404
370 if (mg) 405 if (mg)
446 481
447 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
448 { 483 {
449 dSP; 484 dSP;
450 485
451 ENTER; SAVETMPS; PUSHMARK (SP); 486 ENTER; SAVETMPS;
487 PUSHMARK (SP);
452 // we re-bless the reference to get overload and other niceties right 488 // we re-bless the reference to get overload and other niceties right
453 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 489 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
454 490
455 PUTBACK; 491 PUTBACK;
456 // G_SCALAR ensures that return value is 1 492 // G_SCALAR ensures that return value is 1
469 } 505 }
470 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 506 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
471 { 507 {
472 dSP; 508 dSP;
473 509
474 ENTER; SAVETMPS; PUSHMARK (SP); 510 ENTER; SAVETMPS;
511 SAVESTACK_POS ();
512 PUSHMARK (SP);
475 EXTEND (SP, 2); 513 EXTEND (SP, 2);
476 // we re-bless the reference to get overload and other niceties right 514 // we re-bless the reference to get overload and other niceties right
477 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 515 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
478 PUSHs (sv_cbor); 516 PUSHs (sv_cbor);
479 517
485 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
486 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));
487 525
488 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
489 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
490 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
491 529
492 while (count) 530 while (count)
493 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
494 532
495 PUTBACK; 533 PUTBACK;
554 592
555 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
556 { 594 {
557 STRLEN len; 595 STRLEN len;
558 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
559 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
560 } 598 }
561 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
562 encode_nv (enc, sv); 600 encode_nv (enc, sv);
563 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
564 { 602 {
581} 619}
582 620
583static SV * 621static SV *
584encode_cbor (SV *scalar, CBOR *cbor) 622encode_cbor (SV *scalar, CBOR *cbor)
585{ 623{
586 enc_t enc = { }; 624 enc_t enc = { 0 };
587 625
588 enc.cbor = *cbor; 626 enc.cbor = *cbor;
589 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
590 enc.cur = SvPVX (enc.sv); 628 enc.cur = SvPVX (enc.sv);
591 enc.end = SvEND (enc.sv); 629 enc.end = SvEND (enc.sv);
746 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
747 { 785 {
748 I32 len = decode_uint (dec); 786 I32 len = decode_uint (dec);
749 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
750 788
789 WANT (len);
751 dec->cur += len; 790 dec->cur += len;
752 791
753 hv_store (hv, key, len, decode_sv (dec), 0); 792 hv_store (hv, key, len, decode_sv (dec), 0);
754 793
755 return; 794 return;
756 } 795 }
757 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))
758 { 797 {
759 I32 len = decode_uint (dec); 798 I32 len = decode_uint (dec);
760 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
761 800
801 WANT (len);
762 dec->cur += len; 802 dec->cur += len;
763 803
764 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 804 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
765 if (!is_utf8_string (key, len)) 805 if (!is_utf8_string (key, len))
766 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 806 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
902 sv = newRV_noinc (decode_sv (dec)); 942 sv = newRV_noinc (decode_sv (dec));
903 break; 943 break;
904 944
905 case CBOR_TAG_STRINGREF_NAMESPACE: 945 case CBOR_TAG_STRINGREF_NAMESPACE:
906 { 946 {
947 // do nmot use SAVETMPS/FREETMPS, as these will
948 // erase mortalised caches, e.g. "shareable"
907 ENTER; SAVETMPS; 949 ENTER;
908 950
909 SAVESPTR (dec->stringref); 951 SAVESPTR (dec->stringref);
910 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 952 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
911 953
912 sv = decode_sv (dec); 954 sv = decode_sv (dec);
913 955
914 FREETMPS; LEAVE; 956 LEAVE;
915 } 957 }
916 break; 958 break;
917 959
918 case CBOR_TAG_STRINGREF: 960 case CBOR_TAG_STRINGREF:
919 { 961 {
989 if (!method) 1031 if (!method)
990 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1032 ERR ("cannot decode perl-object (package does not have a THAW method)");
991 1033
992 dSP; 1034 dSP;
993 1035
994 ENTER; SAVETMPS; PUSHMARK (SP); 1036 ENTER; SAVETMPS;
1037 PUSHMARK (SP);
995 EXTEND (SP, len + 1); 1038 EXTEND (SP, len + 1);
996 // we re-bless the reference to get overload and other niceties right 1039 // we re-bless the reference to get overload and other niceties right
997 PUSHs (*av_fetch (av, 0, 1)); 1040 PUSHs (*av_fetch (av, 0, 1));
998 PUSHs (sv_cbor); 1041 PUSHs (sv_cbor);
999 1042
1024 default: 1067 default:
1025 { 1068 {
1026 sv = decode_sv (dec); 1069 sv = decode_sv (dec);
1027 1070
1028 dSP; 1071 dSP;
1029 ENTER; SAVETMPS; PUSHMARK (SP); 1072 ENTER; SAVETMPS;
1073 SAVESTACK_POS ();
1074 PUSHMARK (SP);
1030 EXTEND (SP, 2); 1075 EXTEND (SP, 2);
1031 PUSHs (newSVuv (tag)); 1076 PUSHs (newSVuv (tag));
1032 PUSHs (sv); 1077 PUSHs (sv);
1033 1078
1034 PUTBACK; 1079 PUTBACK;
1160} 1205}
1161 1206
1162static SV * 1207static SV *
1163decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1208decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1164{ 1209{
1165 dec_t dec = { }; 1210 dec_t dec = { 0 };
1166 SV *sv; 1211 SV *sv;
1167 STRLEN len; 1212 STRLEN len;
1168 char *data = SvPVbyte (string, len); 1213 char *data = SvPVbyte (string, len);
1169 1214
1170 if (len > cbor->max_size && cbor->max_size) 1215 if (len > cbor->max_size && cbor->max_size)
1261 1306
1262 int major = *p >> MAJOR_SHIFT; 1307 int major = *p >> MAJOR_SHIFT;
1263 1308
1264 switch (major) 1309 switch (major)
1265 { 1310 {
1311 case MAJOR_TAG >> MAJOR_SHIFT:
1312 ++count; // tags merely prefix another value
1313 break;
1314
1266 case MAJOR_BYTES >> MAJOR_SHIFT: 1315 case MAJOR_BYTES >> MAJOR_SHIFT:
1267 case MAJOR_TEXT >> MAJOR_SHIFT: 1316 case MAJOR_TEXT >> MAJOR_SHIFT:
1268 case MAJOR_ARRAY >> MAJOR_SHIFT: 1317 case MAJOR_ARRAY >> MAJOR_SHIFT:
1269 case MAJOR_MAP >> MAJOR_SHIFT: 1318 case MAJOR_MAP >> MAJOR_SHIFT:
1270 { 1319 {
1375 shrink = F_SHRINK 1424 shrink = F_SHRINK
1376 allow_unknown = F_ALLOW_UNKNOWN 1425 allow_unknown = F_ALLOW_UNKNOWN
1377 allow_sharing = F_ALLOW_SHARING 1426 allow_sharing = F_ALLOW_SHARING
1378 allow_cycles = F_ALLOW_CYCLES 1427 allow_cycles = F_ALLOW_CYCLES
1379 pack_strings = F_PACK_STRINGS 1428 pack_strings = F_PACK_STRINGS
1429 text_keys = F_TEXT_KEYS
1430 text_strings = F_TEXT_STRINGS
1380 validate_utf8 = F_VALIDATE_UTF8 1431 validate_utf8 = F_VALIDATE_UTF8
1381 PPCODE: 1432 PPCODE:
1382{ 1433{
1383 if (enable) 1434 if (enable)
1384 self->flags |= ix; 1435 self->flags |= ix;
1393 get_shrink = F_SHRINK 1444 get_shrink = F_SHRINK
1394 get_allow_unknown = F_ALLOW_UNKNOWN 1445 get_allow_unknown = F_ALLOW_UNKNOWN
1395 get_allow_sharing = F_ALLOW_SHARING 1446 get_allow_sharing = F_ALLOW_SHARING
1396 get_allow_cycles = F_ALLOW_CYCLES 1447 get_allow_cycles = F_ALLOW_CYCLES
1397 get_pack_strings = F_PACK_STRINGS 1448 get_pack_strings = F_PACK_STRINGS
1449 get_text_keys = F_TEXT_KEYS
1450 get_text_strings = F_TEXT_STRINGS
1398 get_validate_utf8 = F_VALIDATE_UTF8 1451 get_validate_utf8 = F_VALIDATE_UTF8
1399 PPCODE: 1452 PPCODE:
1400 XPUSHs (boolSV (self->flags & ix)); 1453 XPUSHs (boolSV (self->flags & ix));
1401 1454
1402void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1455void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines