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.47 by root, Mon Apr 27 20:21:53 2015 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_VALIDATE_UTF8 0x00000020UL
104 106
105#define INIT_SIZE 32 // initial scalar size to be allocated 107#define INIT_SIZE 32 // initial scalar size to be allocated
106 108
107#define SB do { 109#define SB do {
108#define SE } while (0) 110#define SE } while (0)
127typedef struct { 129typedef struct {
128 U32 flags; 130 U32 flags;
129 U32 max_depth; 131 U32 max_depth;
130 STRLEN max_size; 132 STRLEN max_size;
131 SV *filter; 133 SV *filter;
134
135 // for the incremental parser
136 STRLEN incr_pos; // the current offset into the text
137 STRLEN incr_need; // minimum bytes needed to decode
138 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
132} CBOR; 139} CBOR;
133 140
134ecb_inline void 141ecb_inline void
135cbor_init (CBOR *cbor) 142cbor_init (CBOR *cbor)
136{ 143{
140 147
141ecb_inline void 148ecb_inline void
142cbor_free (CBOR *cbor) 149cbor_free (CBOR *cbor)
143{ 150{
144 SvREFCNT_dec (cbor->filter); 151 SvREFCNT_dec (cbor->filter);
152 SvREFCNT_dec (cbor->incr_count);
145} 153}
146 154
147///////////////////////////////////////////////////////////////////////////// 155/////////////////////////////////////////////////////////////////////////////
148// utility functions 156// utility functions
149 157
316 324
317 ++enc->depth; 325 ++enc->depth;
318 326
319 encode_uint (enc, MAJOR_ARRAY, len + 1); 327 encode_uint (enc, MAJOR_ARRAY, len + 1);
320 328
329 if (SvMAGICAL (av))
321 for (i = 0; i <= len; ++i) 330 for (i = 0; i <= len; ++i)
322 { 331 {
323 SV **svp = av_fetch (av, i, 0); 332 SV **svp = av_fetch (av, i, 0);
324 encode_sv (enc, svp ? *svp : &PL_sv_undef); 333 encode_sv (enc, svp ? *svp : &PL_sv_undef);
325 } 334 }
335 else
336 for (i = 0; i <= len; ++i)
337 {
338 SV *sv = AvARRAY (av)[i];
339 encode_sv (enc, sv ? sv : &PL_sv_undef);
340 }
326 341
327 --enc->depth; 342 --enc->depth;
328} 343}
329 344
330static void 345static void
729{ 744{
730 // for speed reasons, we specialcase single-string 745 // for speed reasons, we specialcase single-string
731 // byte or utf-8 strings as keys, but only when !stringref 746 // byte or utf-8 strings as keys, but only when !stringref
732 747
733 if (ecb_expect_true (!dec->stringref)) 748 if (ecb_expect_true (!dec->stringref))
734 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 749 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
735 { 750 {
736 I32 len = decode_uint (dec); 751 I32 len = decode_uint (dec);
737 char *key = (char *)dec->cur; 752 char *key = (char *)dec->cur;
738 753
739 dec->cur += len; 754 dec->cur += len;
740 755
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); 756 hv_store (hv, key, len, decode_sv (dec), 0);
745 757
746 return; 758 return;
747 } 759 }
748 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 760 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
749 { 761 {
750 I32 len = decode_uint (dec); 762 I32 len = decode_uint (dec);
751 char *key = (char *)dec->cur; 763 char *key = (char *)dec->cur;
752 764
753 dec->cur += len; 765 dec->cur += len;
754 766
755 if (ecb_expect_false (dec->stringref)) 767 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
756 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 768 if (!is_utf8_string (key, len))
769 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
757 770
758 hv_store (hv, key, -len, decode_sv (dec), 0); 771 hv_store (hv, key, -len, decode_sv (dec), 0);
759 772
760 return; 773 return;
761 } 774 }
763 SV *k = decode_sv (dec); 776 SV *k = decode_sv (dec);
764 SV *v = decode_sv (dec); 777 SV *v = decode_sv (dec);
765 778
766 hv_store_ent (hv, k, v, 0); 779 hv_store_ent (hv, k, v, 0);
767 SvREFCNT_dec (k); 780 SvREFCNT_dec (k);
781
782fail:
783 ;
768} 784}
769 785
770static SV * 786static SV *
771decode_hv (dec_t *dec) 787decode_hv (dec_t *dec)
772{ 788{
854 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 870 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
855 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 871 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
856 } 872 }
857 873
858 if (utf8) 874 if (utf8)
875 {
876 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
877 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
878 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
879
859 SvUTF8_on (sv); 880 SvUTF8_on (sv);
881 }
860 882
861 return sv; 883 return sv;
862 884
863fail: 885fail:
864 SvREFCNT_dec (sv); 886 SvREFCNT_dec (sv);
1124 1146
1125 return newSVnv (ecb_binary64_to_double (fp)); 1147 return newSVnv (ecb_binary64_to_double (fp));
1126 } 1148 }
1127 1149
1128 // 0..19 unassigned simple 1150 // 0..19 unassigned simple
1129 // 24 reserved + unassigned (reserved values are not encodable) 1151 // 24 reserved + unassigned simple (reserved values are not encodable)
1152 // 28-30 unassigned misc
1153 // 31 break code
1130 default: 1154 default:
1131 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1155 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1132 } 1156 }
1133 1157
1134 break; 1158 break;
1135 } 1159 }
1136 1160
1163 if (dec.cur != dec.end && !dec.err) 1187 if (dec.cur != dec.end && !dec.err)
1164 dec.err = "garbage after CBOR object"; 1188 dec.err = "garbage after CBOR object";
1165 1189
1166 if (dec.err) 1190 if (dec.err)
1167 { 1191 {
1192 if (dec.shareable)
1193 {
1194 // need to break cyclic links, which whould all be in shareable
1195 int i;
1196 SV **svp;
1197
1198 for (i = av_len (dec.shareable) + 1; i--; )
1199 if ((svp = av_fetch (dec.shareable, i, 0)))
1200 sv_setsv (*svp, &PL_sv_undef);
1201 }
1202
1168 SvREFCNT_dec (sv); 1203 SvREFCNT_dec (sv);
1169 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1204 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1170 } 1205 }
1171 1206
1172 sv = sv_2mortal (sv); 1207 sv = sv_2mortal (sv);
1173 1208
1174 return sv; 1209 return sv;
1175} 1210}
1176 1211
1212/////////////////////////////////////////////////////////////////////////////
1213// incremental parser
1214
1215#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1216
1217// returns 0 for notyet, 1 for success or error
1218static int
1219incr_parse (CBOR *self, SV *cborstr)
1220{
1221 STRLEN cur;
1222 SvPV (cborstr, cur);
1223
1224 while (ecb_expect_true (self->incr_need <= cur))
1225 {
1226 // table of integer count bytes
1227 static I8 incr_len[MINOR_MASK + 1] = {
1228 0, 0, 0, 0, 0, 0, 0, 0,
1229 0, 0, 0, 0, 0, 0, 0, 0,
1230 0, 0, 0, 0, 0, 0, 0, 0,
1231 1, 2, 4, 8,-1,-1,-1,-2
1232 };
1233
1234 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1235 U8 m = *p & MINOR_MASK;
1236 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1237 I8 ilen = incr_len[m];
1238
1239 self->incr_need = self->incr_pos + 1;
1240
1241 if (ecb_expect_false (ilen < 0))
1242 {
1243 if (m != MINOR_INDEF)
1244 return 1; // error
1245
1246 if (*p == (MAJOR_MISC | MINOR_INDEF))
1247 {
1248 if (count >= 0)
1249 return 1; // error
1250
1251 count = 1;
1252 }
1253 else
1254 {
1255 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1256 count = -1;
1257 }
1258 }
1259 else
1260 {
1261 self->incr_need += ilen;
1262 if (ecb_expect_false (self->incr_need > cur))
1263 return 0;
1264
1265 int major = *p >> MAJOR_SHIFT;
1266
1267 switch (major)
1268 {
1269 case MAJOR_TAG >> MAJOR_SHIFT:
1270 ++count; // tags merely prefix another value
1271 break;
1272
1273 case MAJOR_BYTES >> MAJOR_SHIFT:
1274 case MAJOR_TEXT >> MAJOR_SHIFT:
1275 case MAJOR_ARRAY >> MAJOR_SHIFT:
1276 case MAJOR_MAP >> MAJOR_SHIFT:
1277 {
1278 UV len;
1279
1280 if (ecb_expect_false (ilen))
1281 {
1282 len = 0;
1283
1284 do {
1285 len = (len << 8) | *++p;
1286 } while (--ilen);
1287 }
1288 else
1289 len = m;
1290
1291 switch (major)
1292 {
1293 case MAJOR_BYTES >> MAJOR_SHIFT:
1294 case MAJOR_TEXT >> MAJOR_SHIFT:
1295 self->incr_need += len;
1296 if (ecb_expect_false (self->incr_need > cur))
1297 return 0;
1298
1299 break;
1300
1301 case MAJOR_MAP >> MAJOR_SHIFT:
1302 len <<= 1;
1303 case MAJOR_ARRAY >> MAJOR_SHIFT:
1304 if (len)
1305 {
1306 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1307 count = len + 1;
1308 }
1309 break;
1310 }
1311 }
1312 }
1313 }
1314
1315 self->incr_pos = self->incr_need;
1316
1317 if (count > 0)
1318 {
1319 while (!--count)
1320 {
1321 if (!AvFILLp (self->incr_count))
1322 return 1; // done
1323
1324 SvREFCNT_dec_NN (av_pop (self->incr_count));
1325 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1326 }
1327
1328 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1329 }
1330 }
1331
1332 return 0;
1333}
1334
1335
1177///////////////////////////////////////////////////////////////////////////// 1336/////////////////////////////////////////////////////////////////////////////
1178// XS interface functions 1337// XS interface functions
1179 1338
1180MODULE = CBOR::XS PACKAGE = CBOR::XS 1339MODULE = CBOR::XS PACKAGE = CBOR::XS
1181 1340
1223 shrink = F_SHRINK 1382 shrink = F_SHRINK
1224 allow_unknown = F_ALLOW_UNKNOWN 1383 allow_unknown = F_ALLOW_UNKNOWN
1225 allow_sharing = F_ALLOW_SHARING 1384 allow_sharing = F_ALLOW_SHARING
1226 allow_cycles = F_ALLOW_CYCLES 1385 allow_cycles = F_ALLOW_CYCLES
1227 pack_strings = F_PACK_STRINGS 1386 pack_strings = F_PACK_STRINGS
1387 validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1388 PPCODE:
1229{ 1389{
1230 if (enable) 1390 if (enable)
1231 self->flags |= ix; 1391 self->flags |= ix;
1232 else 1392 else
1240 get_shrink = F_SHRINK 1400 get_shrink = F_SHRINK
1241 get_allow_unknown = F_ALLOW_UNKNOWN 1401 get_allow_unknown = F_ALLOW_UNKNOWN
1242 get_allow_sharing = F_ALLOW_SHARING 1402 get_allow_sharing = F_ALLOW_SHARING
1243 get_allow_cycles = F_ALLOW_CYCLES 1403 get_allow_cycles = F_ALLOW_CYCLES
1244 get_pack_strings = F_PACK_STRINGS 1404 get_pack_strings = F_PACK_STRINGS
1405 get_validate_utf8 = F_VALIDATE_UTF8
1245 PPCODE: 1406 PPCODE:
1246 XPUSHs (boolSV (self->flags & ix)); 1407 XPUSHs (boolSV (self->flags & ix));
1247 1408
1248void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1409void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1249 PPCODE: 1410 PPCODE:
1298 EXTEND (SP, 2); 1459 EXTEND (SP, 2);
1299 PUSHs (sv); 1460 PUSHs (sv);
1300 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1461 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1301} 1462}
1302 1463
1464void incr_parse (CBOR *self, SV *cborstr)
1465 ALIAS:
1466 incr_parse_multiple = 1
1467 PPCODE:
1468{
1469 if (SvUTF8 (cborstr))
1470 sv_utf8_downgrade (cborstr, 0);
1471
1472 if (!self->incr_count)
1473 {
1474 self->incr_count = newAV ();
1475 self->incr_pos = 0;
1476 self->incr_need = 1;
1477
1478 av_push (self->incr_count, newSViv (1));
1479 }
1480
1481 do
1482 {
1483 if (!incr_parse (self, cborstr))
1484 {
1485 if (self->incr_need > self->max_size && self->max_size)
1486 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1487 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1488
1489 break;
1490 }
1491
1492 SV *sv;
1493 char *offset;
1494
1495 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1496 XPUSHs (sv);
1497
1498 sv_chop (cborstr, offset);
1499
1500 av_clear (self->incr_count);
1501 av_push (self->incr_count, newSViv (1));
1502
1503 self->incr_pos = 0;
1504 self->incr_need = self->incr_pos + 1;
1505 }
1506 while (ix);
1507}
1508
1509void incr_reset (CBOR *self)
1510 CODE:
1511{
1512 SvREFCNT_dec (self->incr_count);
1513 self->incr_count = 0;
1514}
1515
1303void DESTROY (CBOR *self) 1516void DESTROY (CBOR *self)
1304 PPCODE: 1517 PPCODE:
1305 cbor_free (self); 1518 cbor_free (self);
1306 1519
1307PROTOTYPES: ENABLE 1520PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines