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.38 by root, Sun Dec 1 14:30: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)
128typedef struct { 130typedef struct {
129 U32 flags; 131 U32 flags;
130 U32 max_depth; 132 U32 max_depth;
131 STRLEN max_size; 133 STRLEN max_size;
132 SV *filter; 134 SV *filter;
135
136 // for the incremental parser
137 STRLEN incr_pos; // the current offset into the text
138 STRLEN incr_need; // minimum bytes needed to decode
139 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
133} CBOR; 140} CBOR;
134 141
135ecb_inline void 142ecb_inline void
136cbor_init (CBOR *cbor) 143cbor_init (CBOR *cbor)
137{ 144{
141 148
142ecb_inline void 149ecb_inline void
143cbor_free (CBOR *cbor) 150cbor_free (CBOR *cbor)
144{ 151{
145 SvREFCNT_dec (cbor->filter); 152 SvREFCNT_dec (cbor->filter);
153 SvREFCNT_dec (cbor->incr_count);
146} 154}
147 155
148///////////////////////////////////////////////////////////////////////////// 156/////////////////////////////////////////////////////////////////////////////
149// utility functions 157// utility functions
150 158
272} 280}
273 281
274ecb_inline void 282ecb_inline void
275encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 283encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
276{ 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
277 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
278 need (enc, len); 299 need (enc, len);
279 memcpy (enc->cur, str, len); 300 memcpy (enc->cur, str, len);
280 enc->cur += len; 301 enc->cur += len;
281} 302}
317 338
318 ++enc->depth; 339 ++enc->depth;
319 340
320 encode_uint (enc, MAJOR_ARRAY, len + 1); 341 encode_uint (enc, MAJOR_ARRAY, len + 1);
321 342
343 if (SvMAGICAL (av))
322 for (i = 0; i <= len; ++i) 344 for (i = 0; i <= len; ++i)
323 { 345 {
324 SV **svp = av_fetch (av, i, 0); 346 SV **svp = av_fetch (av, i, 0);
325 encode_sv (enc, svp ? *svp : &PL_sv_undef); 347 encode_sv (enc, svp ? *svp : &PL_sv_undef);
326 } 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 }
327 355
328 --enc->depth; 356 --enc->depth;
329} 357}
330 358
331static void 359static void
435 463
436 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 464 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
437 { 465 {
438 dSP; 466 dSP;
439 467
440 ENTER; SAVETMPS; PUSHMARK (SP); 468 ENTER; SAVETMPS;
469 PUSHMARK (SP);
441 // 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
442 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 471 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
443 472
444 PUTBACK; 473 PUTBACK;
445 // G_SCALAR ensures that return value is 1 474 // G_SCALAR ensures that return value is 1
458 } 487 }
459 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 488 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
460 { 489 {
461 dSP; 490 dSP;
462 491
463 ENTER; SAVETMPS; PUSHMARK (SP); 492 ENTER; SAVETMPS;
493 SAVESTACK_POS ();
494 PUSHMARK (SP);
464 EXTEND (SP, 2); 495 EXTEND (SP, 2);
465 // 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
466 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 497 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
467 PUSHs (sv_cbor); 498 PUSHs (sv_cbor);
468 499
570} 601}
571 602
572static SV * 603static SV *
573encode_cbor (SV *scalar, CBOR *cbor) 604encode_cbor (SV *scalar, CBOR *cbor)
574{ 605{
575 enc_t enc = { }; 606 enc_t enc = { 0 };
576 607
577 enc.cbor = *cbor; 608 enc.cbor = *cbor;
578 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 609 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
579 enc.cur = SvPVX (enc.sv); 610 enc.cur = SvPVX (enc.sv);
580 enc.end = SvEND (enc.sv); 611 enc.end = SvEND (enc.sv);
730{ 761{
731 // for speed reasons, we specialcase single-string 762 // for speed reasons, we specialcase single-string
732 // byte or utf-8 strings as keys, but only when !stringref 763 // byte or utf-8 strings as keys, but only when !stringref
733 764
734 if (ecb_expect_true (!dec->stringref)) 765 if (ecb_expect_true (!dec->stringref))
735 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 766 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
736 { 767 {
737 I32 len = decode_uint (dec); 768 I32 len = decode_uint (dec);
738 char *key = (char *)dec->cur; 769 char *key = (char *)dec->cur;
739 770
771 WANT (len);
740 dec->cur += len; 772 dec->cur += len;
741 773
742 hv_store (hv, key, len, decode_sv (dec), 0); 774 hv_store (hv, key, len, decode_sv (dec), 0);
743 775
744 return; 776 return;
745 } 777 }
746 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 778 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 779 {
748 I32 len = decode_uint (dec); 780 I32 len = decode_uint (dec);
749 char *key = (char *)dec->cur; 781 char *key = (char *)dec->cur;
750 782
783 WANT (len);
751 dec->cur += len; 784 dec->cur += len;
752 785
753 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 786 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 if (!is_utf8_string (key, len)) 787 if (!is_utf8_string (key, len))
755 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 788 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
978 if (!method) 1011 if (!method)
979 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)");
980 1013
981 dSP; 1014 dSP;
982 1015
983 ENTER; SAVETMPS; PUSHMARK (SP); 1016 ENTER; SAVETMPS;
1017 PUSHMARK (SP);
984 EXTEND (SP, len + 1); 1018 EXTEND (SP, len + 1);
985 // 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
986 PUSHs (*av_fetch (av, 0, 1)); 1020 PUSHs (*av_fetch (av, 0, 1));
987 PUSHs (sv_cbor); 1021 PUSHs (sv_cbor);
988 1022
1013 default: 1047 default:
1014 { 1048 {
1015 sv = decode_sv (dec); 1049 sv = decode_sv (dec);
1016 1050
1017 dSP; 1051 dSP;
1018 ENTER; SAVETMPS; PUSHMARK (SP); 1052 ENTER; SAVETMPS;
1053 SAVESTACK_POS ();
1054 PUSHMARK (SP);
1019 EXTEND (SP, 2); 1055 EXTEND (SP, 2);
1020 PUSHs (newSVuv (tag)); 1056 PUSHs (newSVuv (tag));
1021 PUSHs (sv); 1057 PUSHs (sv);
1022 1058
1023 PUTBACK; 1059 PUTBACK;
1132 1168
1133 return newSVnv (ecb_binary64_to_double (fp)); 1169 return newSVnv (ecb_binary64_to_double (fp));
1134 } 1170 }
1135 1171
1136 // 0..19 unassigned simple 1172 // 0..19 unassigned simple
1137 // 24 reserved + unassigned (reserved values are not encodable) 1173 // 24 reserved + unassigned simple (reserved values are not encodable)
1174 // 28-30 unassigned misc
1175 // 31 break code
1138 default: 1176 default:
1139 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1177 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1140 } 1178 }
1141 1179
1142 break; 1180 break;
1143 } 1181 }
1144 1182
1147} 1185}
1148 1186
1149static SV * 1187static SV *
1150decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1188decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1151{ 1189{
1152 dec_t dec = { }; 1190 dec_t dec = { 0 };
1153 SV *sv; 1191 SV *sv;
1154 STRLEN len; 1192 STRLEN len;
1155 char *data = SvPVbyte (string, len); 1193 char *data = SvPVbyte (string, len);
1156 1194
1157 if (len > cbor->max_size && cbor->max_size) 1195 if (len > cbor->max_size && cbor->max_size)
1171 if (dec.cur != dec.end && !dec.err) 1209 if (dec.cur != dec.end && !dec.err)
1172 dec.err = "garbage after CBOR object"; 1210 dec.err = "garbage after CBOR object";
1173 1211
1174 if (dec.err) 1212 if (dec.err)
1175 { 1213 {
1214 if (dec.shareable)
1215 {
1216 // need to break cyclic links, which whould all be in shareable
1217 int i;
1218 SV **svp;
1219
1220 for (i = av_len (dec.shareable) + 1; i--; )
1221 if ((svp = av_fetch (dec.shareable, i, 0)))
1222 sv_setsv (*svp, &PL_sv_undef);
1223 }
1224
1176 SvREFCNT_dec (sv); 1225 SvREFCNT_dec (sv);
1177 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1226 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1178 } 1227 }
1179 1228
1180 sv = sv_2mortal (sv); 1229 sv = sv_2mortal (sv);
1181 1230
1182 return sv; 1231 return sv;
1183} 1232}
1184 1233
1234/////////////////////////////////////////////////////////////////////////////
1235// incremental parser
1236
1237#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1238
1239// returns 0 for notyet, 1 for success or error
1240static int
1241incr_parse (CBOR *self, SV *cborstr)
1242{
1243 STRLEN cur;
1244 SvPV (cborstr, cur);
1245
1246 while (ecb_expect_true (self->incr_need <= cur))
1247 {
1248 // table of integer count bytes
1249 static I8 incr_len[MINOR_MASK + 1] = {
1250 0, 0, 0, 0, 0, 0, 0, 0,
1251 0, 0, 0, 0, 0, 0, 0, 0,
1252 0, 0, 0, 0, 0, 0, 0, 0,
1253 1, 2, 4, 8,-1,-1,-1,-2
1254 };
1255
1256 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1257 U8 m = *p & MINOR_MASK;
1258 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1259 I8 ilen = incr_len[m];
1260
1261 self->incr_need = self->incr_pos + 1;
1262
1263 if (ecb_expect_false (ilen < 0))
1264 {
1265 if (m != MINOR_INDEF)
1266 return 1; // error
1267
1268 if (*p == (MAJOR_MISC | MINOR_INDEF))
1269 {
1270 if (count >= 0)
1271 return 1; // error
1272
1273 count = 1;
1274 }
1275 else
1276 {
1277 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1278 count = -1;
1279 }
1280 }
1281 else
1282 {
1283 self->incr_need += ilen;
1284 if (ecb_expect_false (self->incr_need > cur))
1285 return 0;
1286
1287 int major = *p >> MAJOR_SHIFT;
1288
1289 switch (major)
1290 {
1291 case MAJOR_TAG >> MAJOR_SHIFT:
1292 ++count; // tags merely prefix another value
1293 break;
1294
1295 case MAJOR_BYTES >> MAJOR_SHIFT:
1296 case MAJOR_TEXT >> MAJOR_SHIFT:
1297 case MAJOR_ARRAY >> MAJOR_SHIFT:
1298 case MAJOR_MAP >> MAJOR_SHIFT:
1299 {
1300 UV len;
1301
1302 if (ecb_expect_false (ilen))
1303 {
1304 len = 0;
1305
1306 do {
1307 len = (len << 8) | *++p;
1308 } while (--ilen);
1309 }
1310 else
1311 len = m;
1312
1313 switch (major)
1314 {
1315 case MAJOR_BYTES >> MAJOR_SHIFT:
1316 case MAJOR_TEXT >> MAJOR_SHIFT:
1317 self->incr_need += len;
1318 if (ecb_expect_false (self->incr_need > cur))
1319 return 0;
1320
1321 break;
1322
1323 case MAJOR_MAP >> MAJOR_SHIFT:
1324 len <<= 1;
1325 case MAJOR_ARRAY >> MAJOR_SHIFT:
1326 if (len)
1327 {
1328 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1329 count = len + 1;
1330 }
1331 break;
1332 }
1333 }
1334 }
1335 }
1336
1337 self->incr_pos = self->incr_need;
1338
1339 if (count > 0)
1340 {
1341 while (!--count)
1342 {
1343 if (!AvFILLp (self->incr_count))
1344 return 1; // done
1345
1346 SvREFCNT_dec_NN (av_pop (self->incr_count));
1347 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1348 }
1349
1350 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1351 }
1352 }
1353
1354 return 0;
1355}
1356
1357
1185///////////////////////////////////////////////////////////////////////////// 1358/////////////////////////////////////////////////////////////////////////////
1186// XS interface functions 1359// XS interface functions
1187 1360
1188MODULE = CBOR::XS PACKAGE = CBOR::XS 1361MODULE = CBOR::XS PACKAGE = CBOR::XS
1189 1362
1231 shrink = F_SHRINK 1404 shrink = F_SHRINK
1232 allow_unknown = F_ALLOW_UNKNOWN 1405 allow_unknown = F_ALLOW_UNKNOWN
1233 allow_sharing = F_ALLOW_SHARING 1406 allow_sharing = F_ALLOW_SHARING
1234 allow_cycles = F_ALLOW_CYCLES 1407 allow_cycles = F_ALLOW_CYCLES
1235 pack_strings = F_PACK_STRINGS 1408 pack_strings = F_PACK_STRINGS
1409 utf8_strings = F_UTF8_STRINGS
1236 validate_utf8 = F_VALIDATE_UTF8 1410 validate_utf8 = F_VALIDATE_UTF8
1237 PPCODE: 1411 PPCODE:
1238{ 1412{
1239 if (enable) 1413 if (enable)
1240 self->flags |= ix; 1414 self->flags |= ix;
1308 EXTEND (SP, 2); 1482 EXTEND (SP, 2);
1309 PUSHs (sv); 1483 PUSHs (sv);
1310 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1484 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1311} 1485}
1312 1486
1487void incr_parse (CBOR *self, SV *cborstr)
1488 ALIAS:
1489 incr_parse_multiple = 1
1490 PPCODE:
1491{
1492 if (SvUTF8 (cborstr))
1493 sv_utf8_downgrade (cborstr, 0);
1494
1495 if (!self->incr_count)
1496 {
1497 self->incr_count = newAV ();
1498 self->incr_pos = 0;
1499 self->incr_need = 1;
1500
1501 av_push (self->incr_count, newSViv (1));
1502 }
1503
1504 do
1505 {
1506 if (!incr_parse (self, cborstr))
1507 {
1508 if (self->incr_need > self->max_size && self->max_size)
1509 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1510 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1511
1512 break;
1513 }
1514
1515 SV *sv;
1516 char *offset;
1517
1518 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1519 XPUSHs (sv);
1520
1521 sv_chop (cborstr, offset);
1522
1523 av_clear (self->incr_count);
1524 av_push (self->incr_count, newSViv (1));
1525
1526 self->incr_pos = 0;
1527 self->incr_need = self->incr_pos + 1;
1528 }
1529 while (ix);
1530}
1531
1532void incr_reset (CBOR *self)
1533 CODE:
1534{
1535 SvREFCNT_dec (self->incr_count);
1536 self->incr_count = 0;
1537}
1538
1313void DESTROY (CBOR *self) 1539void DESTROY (CBOR *self)
1314 PPCODE: 1540 PPCODE:
1315 cbor_free (self); 1541 cbor_free (self);
1316 1542
1317PROTOTYPES: ENABLE 1543PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines