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.37 by root, Sat Nov 30 18:13:53 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
106#define F_VALIDATE_UTF8 0x00000040UL
104 107
105#define INIT_SIZE 32 // initial scalar size to be allocated 108#define INIT_SIZE 32 // initial scalar size to be allocated
106 109
107#define SB do { 110#define SB do {
108#define SE } while (0) 111#define SE } while (0)
127typedef struct { 130typedef struct {
128 U32 flags; 131 U32 flags;
129 U32 max_depth; 132 U32 max_depth;
130 STRLEN max_size; 133 STRLEN max_size;
131 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.
132} CBOR; 140} CBOR;
133 141
134ecb_inline void 142ecb_inline void
135cbor_init (CBOR *cbor) 143cbor_init (CBOR *cbor)
136{ 144{
140 148
141ecb_inline void 149ecb_inline void
142cbor_free (CBOR *cbor) 150cbor_free (CBOR *cbor)
143{ 151{
144 SvREFCNT_dec (cbor->filter); 152 SvREFCNT_dec (cbor->filter);
153 SvREFCNT_dec (cbor->incr_count);
145} 154}
146 155
147///////////////////////////////////////////////////////////////////////////// 156/////////////////////////////////////////////////////////////////////////////
148// utility functions 157// utility functions
149 158
271} 280}
272 281
273ecb_inline void 282ecb_inline void
274encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 283encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
275{ 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
276 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
277 need (enc, len); 299 need (enc, len);
278 memcpy (enc->cur, str, len); 300 memcpy (enc->cur, str, len);
279 enc->cur += len; 301 enc->cur += len;
280} 302}
316 338
317 ++enc->depth; 339 ++enc->depth;
318 340
319 encode_uint (enc, MAJOR_ARRAY, len + 1); 341 encode_uint (enc, MAJOR_ARRAY, len + 1);
320 342
343 if (SvMAGICAL (av))
321 for (i = 0; i <= len; ++i) 344 for (i = 0; i <= len; ++i)
322 { 345 {
323 SV **svp = av_fetch (av, i, 0); 346 SV **svp = av_fetch (av, i, 0);
324 encode_sv (enc, svp ? *svp : &PL_sv_undef); 347 encode_sv (enc, svp ? *svp : &PL_sv_undef);
325 } 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 }
326 355
327 --enc->depth; 356 --enc->depth;
328} 357}
329 358
330static void 359static void
434 463
435 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 464 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
436 { 465 {
437 dSP; 466 dSP;
438 467
439 ENTER; SAVETMPS; PUSHMARK (SP); 468 ENTER; SAVETMPS;
469 PUSHMARK (SP);
440 // 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
441 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 471 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
442 472
443 PUTBACK; 473 PUTBACK;
444 // G_SCALAR ensures that return value is 1 474 // G_SCALAR ensures that return value is 1
457 } 487 }
458 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 488 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
459 { 489 {
460 dSP; 490 dSP;
461 491
462 ENTER; SAVETMPS; PUSHMARK (SP); 492 ENTER; SAVETMPS;
493 SAVESTACK_POS ();
494 PUSHMARK (SP);
463 EXTEND (SP, 2); 495 EXTEND (SP, 2);
464 // 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
465 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 497 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
466 PUSHs (sv_cbor); 498 PUSHs (sv_cbor);
467 499
569} 601}
570 602
571static SV * 603static SV *
572encode_cbor (SV *scalar, CBOR *cbor) 604encode_cbor (SV *scalar, CBOR *cbor)
573{ 605{
574 enc_t enc = { }; 606 enc_t enc = { 0 };
575 607
576 enc.cbor = *cbor; 608 enc.cbor = *cbor;
577 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 609 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
578 enc.cur = SvPVX (enc.sv); 610 enc.cur = SvPVX (enc.sv);
579 enc.end = SvEND (enc.sv); 611 enc.end = SvEND (enc.sv);
729{ 761{
730 // for speed reasons, we specialcase single-string 762 // for speed reasons, we specialcase single-string
731 // byte or utf-8 strings as keys, but only when !stringref 763 // byte or utf-8 strings as keys, but only when !stringref
732 764
733 if (ecb_expect_true (!dec->stringref)) 765 if (ecb_expect_true (!dec->stringref))
734 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 766 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
735 { 767 {
736 I32 len = decode_uint (dec); 768 I32 len = decode_uint (dec);
737 char *key = (char *)dec->cur; 769 char *key = (char *)dec->cur;
738 770
771 WANT (len);
739 dec->cur += len; 772 dec->cur += len;
740 773
741 if (ecb_expect_false (dec->stringref))
742 av_push (dec->stringref, newSVpvn (key, len));
743
744 hv_store (hv, key, len, decode_sv (dec), 0); 774 hv_store (hv, key, len, decode_sv (dec), 0);
745 775
746 return; 776 return;
747 } 777 }
748 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 778 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
749 { 779 {
750 I32 len = decode_uint (dec); 780 I32 len = decode_uint (dec);
751 char *key = (char *)dec->cur; 781 char *key = (char *)dec->cur;
752 782
783 WANT (len);
753 dec->cur += len; 784 dec->cur += len;
754 785
755 if (ecb_expect_false (dec->stringref)) 786 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
756 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 787 if (!is_utf8_string (key, len))
788 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
757 789
758 hv_store (hv, key, -len, decode_sv (dec), 0); 790 hv_store (hv, key, -len, decode_sv (dec), 0);
759 791
760 return; 792 return;
761 } 793 }
763 SV *k = decode_sv (dec); 795 SV *k = decode_sv (dec);
764 SV *v = decode_sv (dec); 796 SV *v = decode_sv (dec);
765 797
766 hv_store_ent (hv, k, v, 0); 798 hv_store_ent (hv, k, v, 0);
767 SvREFCNT_dec (k); 799 SvREFCNT_dec (k);
800
801fail:
802 ;
768} 803}
769 804
770static SV * 805static SV *
771decode_hv (dec_t *dec) 806decode_hv (dec_t *dec)
772{ 807{
854 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 889 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
855 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 890 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
856 } 891 }
857 892
858 if (utf8) 893 if (utf8)
894 {
895 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
896 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
897 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
898
859 SvUTF8_on (sv); 899 SvUTF8_on (sv);
900 }
860 901
861 return sv; 902 return sv;
862 903
863fail: 904fail:
864 SvREFCNT_dec (sv); 905 SvREFCNT_dec (sv);
970 if (!method) 1011 if (!method)
971 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)");
972 1013
973 dSP; 1014 dSP;
974 1015
975 ENTER; SAVETMPS; PUSHMARK (SP); 1016 ENTER; SAVETMPS;
1017 PUSHMARK (SP);
976 EXTEND (SP, len + 1); 1018 EXTEND (SP, len + 1);
977 // 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
978 PUSHs (*av_fetch (av, 0, 1)); 1020 PUSHs (*av_fetch (av, 0, 1));
979 PUSHs (sv_cbor); 1021 PUSHs (sv_cbor);
980 1022
1005 default: 1047 default:
1006 { 1048 {
1007 sv = decode_sv (dec); 1049 sv = decode_sv (dec);
1008 1050
1009 dSP; 1051 dSP;
1010 ENTER; SAVETMPS; PUSHMARK (SP); 1052 ENTER; SAVETMPS;
1053 SAVESTACK_POS ();
1054 PUSHMARK (SP);
1011 EXTEND (SP, 2); 1055 EXTEND (SP, 2);
1012 PUSHs (newSVuv (tag)); 1056 PUSHs (newSVuv (tag));
1013 PUSHs (sv); 1057 PUSHs (sv);
1014 1058
1015 PUTBACK; 1059 PUTBACK;
1124 1168
1125 return newSVnv (ecb_binary64_to_double (fp)); 1169 return newSVnv (ecb_binary64_to_double (fp));
1126 } 1170 }
1127 1171
1128 // 0..19 unassigned simple 1172 // 0..19 unassigned simple
1129 // 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
1130 default: 1176 default:
1131 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1177 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1132 } 1178 }
1133 1179
1134 break; 1180 break;
1135 } 1181 }
1136 1182
1139} 1185}
1140 1186
1141static SV * 1187static SV *
1142decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1188decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1143{ 1189{
1144 dec_t dec = { }; 1190 dec_t dec = { 0 };
1145 SV *sv; 1191 SV *sv;
1146 STRLEN len; 1192 STRLEN len;
1147 char *data = SvPVbyte (string, len); 1193 char *data = SvPVbyte (string, len);
1148 1194
1149 if (len > cbor->max_size && cbor->max_size) 1195 if (len > cbor->max_size && cbor->max_size)
1163 if (dec.cur != dec.end && !dec.err) 1209 if (dec.cur != dec.end && !dec.err)
1164 dec.err = "garbage after CBOR object"; 1210 dec.err = "garbage after CBOR object";
1165 1211
1166 if (dec.err) 1212 if (dec.err)
1167 { 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
1168 SvREFCNT_dec (sv); 1225 SvREFCNT_dec (sv);
1169 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);
1170 } 1227 }
1171 1228
1172 sv = sv_2mortal (sv); 1229 sv = sv_2mortal (sv);
1173 1230
1174 return sv; 1231 return sv;
1175} 1232}
1176 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
1177///////////////////////////////////////////////////////////////////////////// 1358/////////////////////////////////////////////////////////////////////////////
1178// XS interface functions 1359// XS interface functions
1179 1360
1180MODULE = CBOR::XS PACKAGE = CBOR::XS 1361MODULE = CBOR::XS PACKAGE = CBOR::XS
1181 1362
1223 shrink = F_SHRINK 1404 shrink = F_SHRINK
1224 allow_unknown = F_ALLOW_UNKNOWN 1405 allow_unknown = F_ALLOW_UNKNOWN
1225 allow_sharing = F_ALLOW_SHARING 1406 allow_sharing = F_ALLOW_SHARING
1226 allow_cycles = F_ALLOW_CYCLES 1407 allow_cycles = F_ALLOW_CYCLES
1227 pack_strings = F_PACK_STRINGS 1408 pack_strings = F_PACK_STRINGS
1409 utf8_strings = F_UTF8_STRINGS
1410 validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1411 PPCODE:
1229{ 1412{
1230 if (enable) 1413 if (enable)
1231 self->flags |= ix; 1414 self->flags |= ix;
1232 else 1415 else
1240 get_shrink = F_SHRINK 1423 get_shrink = F_SHRINK
1241 get_allow_unknown = F_ALLOW_UNKNOWN 1424 get_allow_unknown = F_ALLOW_UNKNOWN
1242 get_allow_sharing = F_ALLOW_SHARING 1425 get_allow_sharing = F_ALLOW_SHARING
1243 get_allow_cycles = F_ALLOW_CYCLES 1426 get_allow_cycles = F_ALLOW_CYCLES
1244 get_pack_strings = F_PACK_STRINGS 1427 get_pack_strings = F_PACK_STRINGS
1428 get_validate_utf8 = F_VALIDATE_UTF8
1245 PPCODE: 1429 PPCODE:
1246 XPUSHs (boolSV (self->flags & ix)); 1430 XPUSHs (boolSV (self->flags & ix));
1247 1431
1248void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1432void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1249 PPCODE: 1433 PPCODE:
1298 EXTEND (SP, 2); 1482 EXTEND (SP, 2);
1299 PUSHs (sv); 1483 PUSHs (sv);
1300 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1484 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1301} 1485}
1302 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
1303void DESTROY (CBOR *self) 1539void DESTROY (CBOR *self)
1304 PPCODE: 1540 PPCODE:
1305 cbor_free (self); 1541 cbor_free (self);
1306 1542
1307PROTOTYPES: ENABLE 1543PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines