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.41 by root, Tue Dec 10 14:01:52 2013 UTC vs.
Revision 1.51 by root, Sun Apr 24 13:15:19 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_UTF8_STRINGS 0x00000020UL
104#define F_VALIDATE_UTF8 0x00000020UL 106#define F_VALIDATE_UTF8 0x00000040UL
105 107
106#define INIT_SIZE 32 // initial scalar size to be allocated 108#define INIT_SIZE 32 // initial scalar size to be allocated
107 109
108#define SB do { 110#define SB do {
109#define SE } while (0) 111#define SE } while (0)
278} 280}
279 281
280ecb_inline void 282ecb_inline void
281encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 283encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
282{ 284{
285 if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS))
286 if (!utf8)
287 {
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);
295 return;
296 }
297
283 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
284 need (enc, len); 299 need (enc, len);
285 memcpy (enc->cur, str, len); 300 memcpy (enc->cur, str, len);
286 enc->cur += len; 301 enc->cur += len;
287} 302}
323 338
324 ++enc->depth; 339 ++enc->depth;
325 340
326 encode_uint (enc, MAJOR_ARRAY, len + 1); 341 encode_uint (enc, MAJOR_ARRAY, len + 1);
327 342
343 if (SvMAGICAL (av))
328 for (i = 0; i <= len; ++i) 344 for (i = 0; i <= len; ++i)
329 { 345 {
330 SV **svp = av_fetch (av, i, 0); 346 SV **svp = av_fetch (av, i, 0);
331 encode_sv (enc, svp ? *svp : &PL_sv_undef); 347 encode_sv (enc, svp ? *svp : &PL_sv_undef);
332 } 348 }
349 else
350 for (i = 0; i <= len; ++i)
351 {
352 SV *sv = AvARRAY (av)[i];
353 encode_sv (enc, sv ? sv : &PL_sv_undef);
354 }
333 355
334 --enc->depth; 356 --enc->depth;
335} 357}
336 358
337static void 359static void
441 463
442 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 464 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
443 { 465 {
444 dSP; 466 dSP;
445 467
446 ENTER; SAVETMPS; PUSHMARK (SP); 468 ENTER; SAVETMPS;
469 PUSHMARK (SP);
447 // we re-bless the reference to get overload and other niceties right 470 // we re-bless the reference to get overload and other niceties right
448 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 471 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
449 472
450 PUTBACK; 473 PUTBACK;
451 // G_SCALAR ensures that return value is 1 474 // G_SCALAR ensures that return value is 1
464 } 487 }
465 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 488 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
466 { 489 {
467 dSP; 490 dSP;
468 491
469 ENTER; SAVETMPS; PUSHMARK (SP); 492 ENTER; SAVETMPS;
493 SAVESTACK_POS ();
494 PUSHMARK (SP);
470 EXTEND (SP, 2); 495 EXTEND (SP, 2);
471 // we re-bless the reference to get overload and other niceties right 496 // we re-bless the reference to get overload and other niceties right
472 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 497 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
473 PUSHs (sv_cbor); 498 PUSHs (sv_cbor);
474 499
576} 601}
577 602
578static SV * 603static SV *
579encode_cbor (SV *scalar, CBOR *cbor) 604encode_cbor (SV *scalar, CBOR *cbor)
580{ 605{
581 enc_t enc = { }; 606 enc_t enc = { 0 };
582 607
583 enc.cbor = *cbor; 608 enc.cbor = *cbor;
584 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 609 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
585 enc.cur = SvPVX (enc.sv); 610 enc.cur = SvPVX (enc.sv);
586 enc.end = SvEND (enc.sv); 611 enc.end = SvEND (enc.sv);
736{ 761{
737 // for speed reasons, we specialcase single-string 762 // for speed reasons, we specialcase single-string
738 // byte or utf-8 strings as keys, but only when !stringref 763 // byte or utf-8 strings as keys, but only when !stringref
739 764
740 if (ecb_expect_true (!dec->stringref)) 765 if (ecb_expect_true (!dec->stringref))
741 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 766 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
742 { 767 {
743 I32 len = decode_uint (dec); 768 I32 len = decode_uint (dec);
744 char *key = (char *)dec->cur; 769 char *key = (char *)dec->cur;
745 770
771 WANT (len);
746 dec->cur += len; 772 dec->cur += len;
747 773
748 hv_store (hv, key, len, decode_sv (dec), 0); 774 hv_store (hv, key, len, decode_sv (dec), 0);
749 775
750 return; 776 return;
751 } 777 }
752 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 778 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
753 { 779 {
754 I32 len = decode_uint (dec); 780 I32 len = decode_uint (dec);
755 char *key = (char *)dec->cur; 781 char *key = (char *)dec->cur;
756 782
783 WANT (len);
757 dec->cur += len; 784 dec->cur += len;
758 785
759 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 786 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
760 if (!is_utf8_string (key, len)) 787 if (!is_utf8_string (key, len))
761 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 788 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
984 if (!method) 1011 if (!method)
985 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1012 ERR ("cannot decode perl-object (package does not have a THAW method)");
986 1013
987 dSP; 1014 dSP;
988 1015
989 ENTER; SAVETMPS; PUSHMARK (SP); 1016 ENTER; SAVETMPS;
1017 PUSHMARK (SP);
990 EXTEND (SP, len + 1); 1018 EXTEND (SP, len + 1);
991 // we re-bless the reference to get overload and other niceties right 1019 // we re-bless the reference to get overload and other niceties right
992 PUSHs (*av_fetch (av, 0, 1)); 1020 PUSHs (*av_fetch (av, 0, 1));
993 PUSHs (sv_cbor); 1021 PUSHs (sv_cbor);
994 1022
1019 default: 1047 default:
1020 { 1048 {
1021 sv = decode_sv (dec); 1049 sv = decode_sv (dec);
1022 1050
1023 dSP; 1051 dSP;
1024 ENTER; SAVETMPS; PUSHMARK (SP); 1052 ENTER; SAVETMPS;
1053 SAVESTACK_POS ();
1054 PUSHMARK (SP);
1025 EXTEND (SP, 2); 1055 EXTEND (SP, 2);
1026 PUSHs (newSVuv (tag)); 1056 PUSHs (newSVuv (tag));
1027 PUSHs (sv); 1057 PUSHs (sv);
1028 1058
1029 PUTBACK; 1059 PUTBACK;
1155} 1185}
1156 1186
1157static SV * 1187static SV *
1158decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1188decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1159{ 1189{
1160 dec_t dec = { }; 1190 dec_t dec = { 0 };
1161 SV *sv; 1191 SV *sv;
1162 STRLEN len; 1192 STRLEN len;
1163 char *data = SvPVbyte (string, len); 1193 char *data = SvPVbyte (string, len);
1164 1194
1165 if (len > cbor->max_size && cbor->max_size) 1195 if (len > cbor->max_size && cbor->max_size)
1256 1286
1257 int major = *p >> MAJOR_SHIFT; 1287 int major = *p >> MAJOR_SHIFT;
1258 1288
1259 switch (major) 1289 switch (major)
1260 { 1290 {
1291 case MAJOR_TAG >> MAJOR_SHIFT:
1292 ++count; // tags merely prefix another value
1293 break;
1294
1261 case MAJOR_BYTES >> MAJOR_SHIFT: 1295 case MAJOR_BYTES >> MAJOR_SHIFT:
1262 case MAJOR_TEXT >> MAJOR_SHIFT: 1296 case MAJOR_TEXT >> MAJOR_SHIFT:
1263 case MAJOR_ARRAY >> MAJOR_SHIFT: 1297 case MAJOR_ARRAY >> MAJOR_SHIFT:
1264 case MAJOR_MAP >> MAJOR_SHIFT: 1298 case MAJOR_MAP >> MAJOR_SHIFT:
1265 { 1299 {
1370 shrink = F_SHRINK 1404 shrink = F_SHRINK
1371 allow_unknown = F_ALLOW_UNKNOWN 1405 allow_unknown = F_ALLOW_UNKNOWN
1372 allow_sharing = F_ALLOW_SHARING 1406 allow_sharing = F_ALLOW_SHARING
1373 allow_cycles = F_ALLOW_CYCLES 1407 allow_cycles = F_ALLOW_CYCLES
1374 pack_strings = F_PACK_STRINGS 1408 pack_strings = F_PACK_STRINGS
1409 utf8_strings = F_UTF8_STRINGS
1375 validate_utf8 = F_VALIDATE_UTF8 1410 validate_utf8 = F_VALIDATE_UTF8
1376 PPCODE: 1411 PPCODE:
1377{ 1412{
1378 if (enable) 1413 if (enable)
1379 self->flags |= ix; 1414 self->flags |= ix;
1448 PUSHs (sv); 1483 PUSHs (sv);
1449 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1484 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1450} 1485}
1451 1486
1452void incr_parse (CBOR *self, SV *cborstr) 1487void incr_parse (CBOR *self, SV *cborstr)
1488 ALIAS:
1489 incr_parse_multiple = 1
1453 PPCODE: 1490 PPCODE:
1454{ 1491{
1455 if (SvUTF8 (cborstr)) 1492 if (SvUTF8 (cborstr))
1456 sv_utf8_downgrade (cborstr, 0); 1493 sv_utf8_downgrade (cborstr, 0);
1457 1494
1487 av_push (self->incr_count, newSViv (1)); 1524 av_push (self->incr_count, newSViv (1));
1488 1525
1489 self->incr_pos = 0; 1526 self->incr_pos = 0;
1490 self->incr_need = self->incr_pos + 1; 1527 self->incr_need = self->incr_pos + 1;
1491 } 1528 }
1492 while (GIMME_V == G_ARRAY); 1529 while (ix);
1493} 1530}
1494 1531
1495void incr_reset (CBOR *self) 1532void incr_reset (CBOR *self)
1496 CODE: 1533 CODE:
1497{ 1534{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines