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.42 by root, Tue Dec 10 15:31:40 2013 UTC vs.
Revision 1.57 by root, Fri Nov 25 12:16:12 2016 UTC

7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11 11
12#define ECB_NO_THREADS 1
12#include "ecb.h" 13#include "ecb.h"
13 14
14// compatibility with perl <5.18 15// compatibility with perl <5.18
15#ifndef HvNAMELEN_get 16#ifndef HvNAMELEN_get
16# define HvNAMELEN_get(hv) strlen (HvNAME (hv)) 17# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
99#define F_SHRINK 0x00000001UL 100#define F_SHRINK 0x00000001UL
100#define F_ALLOW_UNKNOWN 0x00000002UL 101#define F_ALLOW_UNKNOWN 0x00000002UL
101#define F_ALLOW_SHARING 0x00000004UL 102#define F_ALLOW_SHARING 0x00000004UL
102#define F_ALLOW_CYCLES 0x00000008UL 103#define F_ALLOW_CYCLES 0x00000008UL
103#define F_PACK_STRINGS 0x00000010UL 104#define F_PACK_STRINGS 0x00000010UL
105#define F_TEXT_KEYS 0x00000020UL
106#define F_TEXT_STRINGS 0x00000040UL
104#define F_VALIDATE_UTF8 0x00000020UL 107#define F_VALIDATE_UTF8 0x00000080UL
105 108
106#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
107 110
108#define SB do { 111#define SB do {
109#define SE } while (0) 112#define SE } while (0)
275encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
276{ 279{
277 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
278} 281}
279 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
280ecb_inline void 306ecb_inline void
281encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
282{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
283 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
284 need (enc, len); 317 need (enc, len);
285 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
286 enc->cur += len; 319 enc->cur += len;
287} 320}
288 321
289static void 322ecb_inline void
290encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
291{ 324{
292 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
293 { 326 {
294 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
295 328
306 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
307 ++enc->stringref_idx; 340 ++enc->stringref_idx;
308 } 341 }
309 } 342 }
310 343
311 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
312} 345}
313 346
314static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
315 348
316static void 349static void
323 356
324 ++enc->depth; 357 ++enc->depth;
325 358
326 encode_uint (enc, MAJOR_ARRAY, len + 1); 359 encode_uint (enc, MAJOR_ARRAY, len + 1);
327 360
361 if (SvMAGICAL (av))
328 for (i = 0; i <= len; ++i) 362 for (i = 0; i <= len; ++i)
329 { 363 {
330 SV **svp = av_fetch (av, i, 0); 364 SV **svp = av_fetch (av, i, 0);
331 encode_sv (enc, svp ? *svp : &PL_sv_undef); 365 encode_sv (enc, svp ? *svp : &PL_sv_undef);
332 } 366 }
367 else
368 for (i = 0; i <= len; ++i)
369 {
370 SV *sv = AvARRAY (av)[i];
371 encode_sv (enc, sv ? sv : &PL_sv_undef);
372 }
333 373
334 --enc->depth; 374 --enc->depth;
335} 375}
336 376
337static void 377static void
355 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
356 { 396 {
357 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
358 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
359 else 399 else
360 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));
361 401
362 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));
363 } 403 }
364 404
365 if (mg) 405 if (mg)
441 481
442 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
443 { 483 {
444 dSP; 484 dSP;
445 485
446 ENTER; SAVETMPS; PUSHMARK (SP); 486 ENTER; SAVETMPS;
487 PUSHMARK (SP);
447 // 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
448 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 489 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
449 490
450 PUTBACK; 491 PUTBACK;
451 // G_SCALAR ensures that return value is 1 492 // G_SCALAR ensures that return value is 1
464 } 505 }
465 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 506 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
466 { 507 {
467 dSP; 508 dSP;
468 509
469 ENTER; SAVETMPS; PUSHMARK (SP); 510 ENTER; SAVETMPS;
511 SAVESTACK_POS ();
512 PUSHMARK (SP);
470 EXTEND (SP, 2); 513 EXTEND (SP, 2);
471 // 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
472 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 515 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
473 PUSHs (sv_cbor); 516 PUSHs (sv_cbor);
474 517
480 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
481 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));
482 525
483 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
484 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
485 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
486 529
487 while (count) 530 while (count)
488 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
489 532
490 PUTBACK; 533 PUTBACK;
549 592
550 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
551 { 594 {
552 STRLEN len; 595 STRLEN len;
553 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
554 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
555 } 598 }
556 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
557 encode_nv (enc, sv); 600 encode_nv (enc, sv);
558 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
559 { 602 {
576} 619}
577 620
578static SV * 621static SV *
579encode_cbor (SV *scalar, CBOR *cbor) 622encode_cbor (SV *scalar, CBOR *cbor)
580{ 623{
581 enc_t enc = { }; 624 enc_t enc = { 0 };
582 625
583 enc.cbor = *cbor; 626 enc.cbor = *cbor;
584 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
585 enc.cur = SvPVX (enc.sv); 628 enc.cur = SvPVX (enc.sv);
586 enc.end = SvEND (enc.sv); 629 enc.end = SvEND (enc.sv);
622 SV *decode_tagged; 665 SV *decode_tagged;
623} dec_t; 666} dec_t;
624 667
625#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
626 669
627#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")
628 671
629#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)
630#define DEC_DEC_DEPTH --dec->depth 673#define DEC_DEC_DEPTH --dec->depth
631 674
632static UV 675static UV
633decode_uint (dec_t *dec) 676decode_uint (dec_t *dec)
634{ 677{
711 av_push (av, decode_sv (dec)); 754 av_push (av, decode_sv (dec));
712 } 755 }
713 } 756 }
714 else 757 else
715 { 758 {
716 int i, len = decode_uint (dec); 759 UV i, len = decode_uint (dec);
717 760
718 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
719 av_fill (av, len - 1); 762 av_fill (av, len - 1);
720 763
721 for (i = 0; i < len; ++i) 764 for (i = 0; i < len; ++i)
736{ 779{
737 // for speed reasons, we specialcase single-string 780 // for speed reasons, we specialcase single-string
738 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
739 782
740 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
741 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
742 { 785 {
743 I32 len = decode_uint (dec); 786 STRLEN len = decode_uint (dec);
744 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
745 788
789 WANT (len);
746 dec->cur += len; 790 dec->cur += len;
747 791
748 hv_store (hv, key, len, decode_sv (dec), 0); 792 hv_store (hv, key, len, decode_sv (dec), 0);
749 793
750 return; 794 return;
751 } 795 }
752 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
753 { 797 {
754 I32 len = decode_uint (dec); 798 STRLEN len = decode_uint (dec);
755 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
756 800
801 WANT (len);
757 dec->cur += len; 802 dec->cur += len;
758 803
759 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 804 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
760 if (!is_utf8_string (key, len)) 805 if (!is_utf8_string (key, len))
761 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 806 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
799 decode_he (dec, hv); 844 decode_he (dec, hv);
800 } 845 }
801 } 846 }
802 else 847 else
803 { 848 {
804 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
805 852
806 while (pairs--) 853 while (pairs--)
807 decode_he (dec, hv); 854 decode_he (dec, hv);
808 } 855 }
809 856
897 sv = newRV_noinc (decode_sv (dec)); 944 sv = newRV_noinc (decode_sv (dec));
898 break; 945 break;
899 946
900 case CBOR_TAG_STRINGREF_NAMESPACE: 947 case CBOR_TAG_STRINGREF_NAMESPACE:
901 { 948 {
949 // do nmot use SAVETMPS/FREETMPS, as these will
950 // erase mortalised caches, e.g. "shareable"
902 ENTER; SAVETMPS; 951 ENTER;
903 952
904 SAVESPTR (dec->stringref); 953 SAVESPTR (dec->stringref);
905 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 954 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
906 955
907 sv = decode_sv (dec); 956 sv = decode_sv (dec);
908 957
909 FREETMPS; LEAVE; 958 LEAVE;
910 } 959 }
911 break; 960 break;
912 961
913 case CBOR_TAG_STRINGREF: 962 case CBOR_TAG_STRINGREF:
914 { 963 {
984 if (!method) 1033 if (!method)
985 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1034 ERR ("cannot decode perl-object (package does not have a THAW method)");
986 1035
987 dSP; 1036 dSP;
988 1037
989 ENTER; SAVETMPS; PUSHMARK (SP); 1038 ENTER; SAVETMPS;
1039 PUSHMARK (SP);
990 EXTEND (SP, len + 1); 1040 EXTEND (SP, len + 1);
991 // we re-bless the reference to get overload and other niceties right 1041 // we re-bless the reference to get overload and other niceties right
992 PUSHs (*av_fetch (av, 0, 1)); 1042 PUSHs (*av_fetch (av, 0, 1));
993 PUSHs (sv_cbor); 1043 PUSHs (sv_cbor);
994 1044
1019 default: 1069 default:
1020 { 1070 {
1021 sv = decode_sv (dec); 1071 sv = decode_sv (dec);
1022 1072
1023 dSP; 1073 dSP;
1024 ENTER; SAVETMPS; PUSHMARK (SP); 1074 ENTER; SAVETMPS;
1075 SAVESTACK_POS ();
1076 PUSHMARK (SP);
1025 EXTEND (SP, 2); 1077 EXTEND (SP, 2);
1026 PUSHs (newSVuv (tag)); 1078 PUSHs (newSVuv (tag));
1027 PUSHs (sv); 1079 PUSHs (sv);
1028 1080
1029 PUTBACK; 1081 PUTBACK;
1155} 1207}
1156 1208
1157static SV * 1209static SV *
1158decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1210decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1159{ 1211{
1160 dec_t dec = { }; 1212 dec_t dec = { 0 };
1161 SV *sv; 1213 SV *sv;
1162 STRLEN len; 1214 STRLEN len;
1163 char *data = SvPVbyte (string, len); 1215 char *data = SvPVbyte (string, len);
1164 1216
1165 if (len > cbor->max_size && cbor->max_size) 1217 if (len > cbor->max_size && cbor->max_size)
1256 1308
1257 int major = *p >> MAJOR_SHIFT; 1309 int major = *p >> MAJOR_SHIFT;
1258 1310
1259 switch (major) 1311 switch (major)
1260 { 1312 {
1313 case MAJOR_TAG >> MAJOR_SHIFT:
1314 ++count; // tags merely prefix another value
1315 break;
1316
1261 case MAJOR_BYTES >> MAJOR_SHIFT: 1317 case MAJOR_BYTES >> MAJOR_SHIFT:
1262 case MAJOR_TEXT >> MAJOR_SHIFT: 1318 case MAJOR_TEXT >> MAJOR_SHIFT:
1263 case MAJOR_ARRAY >> MAJOR_SHIFT: 1319 case MAJOR_ARRAY >> MAJOR_SHIFT:
1264 case MAJOR_MAP >> MAJOR_SHIFT: 1320 case MAJOR_MAP >> MAJOR_SHIFT:
1265 { 1321 {
1340 1396
1341 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1397 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1342 1398
1343 sv_cbor = newSVpv ("CBOR", 0); 1399 sv_cbor = newSVpv ("CBOR", 0);
1344 SvREADONLY_on (sv_cbor); 1400 SvREADONLY_on (sv_cbor);
1401
1402 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1345} 1403}
1346 1404
1347PROTOTYPES: DISABLE 1405PROTOTYPES: DISABLE
1348 1406
1349void CLONE (...) 1407void CLONE (...)
1370 shrink = F_SHRINK 1428 shrink = F_SHRINK
1371 allow_unknown = F_ALLOW_UNKNOWN 1429 allow_unknown = F_ALLOW_UNKNOWN
1372 allow_sharing = F_ALLOW_SHARING 1430 allow_sharing = F_ALLOW_SHARING
1373 allow_cycles = F_ALLOW_CYCLES 1431 allow_cycles = F_ALLOW_CYCLES
1374 pack_strings = F_PACK_STRINGS 1432 pack_strings = F_PACK_STRINGS
1433 text_keys = F_TEXT_KEYS
1434 text_strings = F_TEXT_STRINGS
1375 validate_utf8 = F_VALIDATE_UTF8 1435 validate_utf8 = F_VALIDATE_UTF8
1376 PPCODE: 1436 PPCODE:
1377{ 1437{
1378 if (enable) 1438 if (enable)
1379 self->flags |= ix; 1439 self->flags |= ix;
1388 get_shrink = F_SHRINK 1448 get_shrink = F_SHRINK
1389 get_allow_unknown = F_ALLOW_UNKNOWN 1449 get_allow_unknown = F_ALLOW_UNKNOWN
1390 get_allow_sharing = F_ALLOW_SHARING 1450 get_allow_sharing = F_ALLOW_SHARING
1391 get_allow_cycles = F_ALLOW_CYCLES 1451 get_allow_cycles = F_ALLOW_CYCLES
1392 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
1393 get_validate_utf8 = F_VALIDATE_UTF8 1455 get_validate_utf8 = F_VALIDATE_UTF8
1394 PPCODE: 1456 PPCODE:
1395 XPUSHs (boolSV (self->flags & ix)); 1457 XPUSHs (boolSV (self->flags & ix));
1396 1458
1397void 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