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.45 by root, Sun Dec 14 05:48:39 2014 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 encode_sv (enc, AvARRAY (av)[i]);
326 338
327 --enc->depth; 339 --enc->depth;
328} 340}
329 341
330static void 342static void
729{ 741{
730 // for speed reasons, we specialcase single-string 742 // for speed reasons, we specialcase single-string
731 // byte or utf-8 strings as keys, but only when !stringref 743 // byte or utf-8 strings as keys, but only when !stringref
732 744
733 if (ecb_expect_true (!dec->stringref)) 745 if (ecb_expect_true (!dec->stringref))
734 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 746 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
735 { 747 {
736 I32 len = decode_uint (dec); 748 I32 len = decode_uint (dec);
737 char *key = (char *)dec->cur; 749 char *key = (char *)dec->cur;
738 750
739 dec->cur += len; 751 dec->cur += len;
740 752
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); 753 hv_store (hv, key, len, decode_sv (dec), 0);
745 754
746 return; 755 return;
747 } 756 }
748 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 757 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
749 { 758 {
750 I32 len = decode_uint (dec); 759 I32 len = decode_uint (dec);
751 char *key = (char *)dec->cur; 760 char *key = (char *)dec->cur;
752 761
753 dec->cur += len; 762 dec->cur += len;
754 763
755 if (ecb_expect_false (dec->stringref)) 764 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
756 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 765 if (!is_utf8_string (key, len))
766 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
757 767
758 hv_store (hv, key, -len, decode_sv (dec), 0); 768 hv_store (hv, key, -len, decode_sv (dec), 0);
759 769
760 return; 770 return;
761 } 771 }
763 SV *k = decode_sv (dec); 773 SV *k = decode_sv (dec);
764 SV *v = decode_sv (dec); 774 SV *v = decode_sv (dec);
765 775
766 hv_store_ent (hv, k, v, 0); 776 hv_store_ent (hv, k, v, 0);
767 SvREFCNT_dec (k); 777 SvREFCNT_dec (k);
778
779fail:
780 ;
768} 781}
769 782
770static SV * 783static SV *
771decode_hv (dec_t *dec) 784decode_hv (dec_t *dec)
772{ 785{
854 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 867 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
855 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 868 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
856 } 869 }
857 870
858 if (utf8) 871 if (utf8)
872 {
873 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
874 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
875 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
876
859 SvUTF8_on (sv); 877 SvUTF8_on (sv);
878 }
860 879
861 return sv; 880 return sv;
862 881
863fail: 882fail:
864 SvREFCNT_dec (sv); 883 SvREFCNT_dec (sv);
1124 1143
1125 return newSVnv (ecb_binary64_to_double (fp)); 1144 return newSVnv (ecb_binary64_to_double (fp));
1126 } 1145 }
1127 1146
1128 // 0..19 unassigned simple 1147 // 0..19 unassigned simple
1129 // 24 reserved + unassigned (reserved values are not encodable) 1148 // 24 reserved + unassigned simple (reserved values are not encodable)
1149 // 28-30 unassigned misc
1150 // 31 break code
1130 default: 1151 default:
1131 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1152 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1132 } 1153 }
1133 1154
1134 break; 1155 break;
1135 } 1156 }
1136 1157
1163 if (dec.cur != dec.end && !dec.err) 1184 if (dec.cur != dec.end && !dec.err)
1164 dec.err = "garbage after CBOR object"; 1185 dec.err = "garbage after CBOR object";
1165 1186
1166 if (dec.err) 1187 if (dec.err)
1167 { 1188 {
1189 if (dec.shareable)
1190 {
1191 // need to break cyclic links, which whould all be in shareable
1192 int i;
1193 SV **svp;
1194
1195 for (i = av_len (dec.shareable) + 1; i--; )
1196 if ((svp = av_fetch (dec.shareable, i, 0)))
1197 sv_setsv (*svp, &PL_sv_undef);
1198 }
1199
1168 SvREFCNT_dec (sv); 1200 SvREFCNT_dec (sv);
1169 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1201 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1170 } 1202 }
1171 1203
1172 sv = sv_2mortal (sv); 1204 sv = sv_2mortal (sv);
1173 1205
1174 return sv; 1206 return sv;
1175} 1207}
1176 1208
1209/////////////////////////////////////////////////////////////////////////////
1210// incremental parser
1211
1212#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1213
1214// returns 0 for notyet, 1 for success or error
1215static int
1216incr_parse (CBOR *self, SV *cborstr)
1217{
1218 STRLEN cur;
1219 SvPV (cborstr, cur);
1220
1221 while (ecb_expect_true (self->incr_need <= cur))
1222 {
1223 // table of integer count bytes
1224 static I8 incr_len[MINOR_MASK + 1] = {
1225 0, 0, 0, 0, 0, 0, 0, 0,
1226 0, 0, 0, 0, 0, 0, 0, 0,
1227 0, 0, 0, 0, 0, 0, 0, 0,
1228 1, 2, 4, 8,-1,-1,-1,-2
1229 };
1230
1231 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1232 U8 m = *p & MINOR_MASK;
1233 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1234 I8 ilen = incr_len[m];
1235
1236 self->incr_need = self->incr_pos + 1;
1237
1238 if (ecb_expect_false (ilen < 0))
1239 {
1240 if (m != MINOR_INDEF)
1241 return 1; // error
1242
1243 if (*p == (MAJOR_MISC | MINOR_INDEF))
1244 {
1245 if (count >= 0)
1246 return 1; // error
1247
1248 count = 1;
1249 }
1250 else
1251 {
1252 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1253 count = -1;
1254 }
1255 }
1256 else
1257 {
1258 self->incr_need += ilen;
1259 if (ecb_expect_false (self->incr_need > cur))
1260 return 0;
1261
1262 int major = *p >> MAJOR_SHIFT;
1263
1264 switch (major)
1265 {
1266 case MAJOR_BYTES >> MAJOR_SHIFT:
1267 case MAJOR_TEXT >> MAJOR_SHIFT:
1268 case MAJOR_ARRAY >> MAJOR_SHIFT:
1269 case MAJOR_MAP >> MAJOR_SHIFT:
1270 {
1271 UV len;
1272
1273 if (ecb_expect_false (ilen))
1274 {
1275 len = 0;
1276
1277 do {
1278 len = (len << 8) | *++p;
1279 } while (--ilen);
1280 }
1281 else
1282 len = m;
1283
1284 switch (major)
1285 {
1286 case MAJOR_BYTES >> MAJOR_SHIFT:
1287 case MAJOR_TEXT >> MAJOR_SHIFT:
1288 self->incr_need += len;
1289 if (ecb_expect_false (self->incr_need > cur))
1290 return 0;
1291
1292 break;
1293
1294 case MAJOR_MAP >> MAJOR_SHIFT:
1295 len <<= 1;
1296 case MAJOR_ARRAY >> MAJOR_SHIFT:
1297 if (len)
1298 {
1299 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1300 count = len + 1;
1301 }
1302 break;
1303 }
1304 }
1305 }
1306 }
1307
1308 self->incr_pos = self->incr_need;
1309
1310 if (count > 0)
1311 {
1312 while (!--count)
1313 {
1314 if (!AvFILLp (self->incr_count))
1315 return 1; // done
1316
1317 SvREFCNT_dec_NN (av_pop (self->incr_count));
1318 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1319 }
1320
1321 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1322 }
1323 }
1324
1325 return 0;
1326}
1327
1328
1177///////////////////////////////////////////////////////////////////////////// 1329/////////////////////////////////////////////////////////////////////////////
1178// XS interface functions 1330// XS interface functions
1179 1331
1180MODULE = CBOR::XS PACKAGE = CBOR::XS 1332MODULE = CBOR::XS PACKAGE = CBOR::XS
1181 1333
1223 shrink = F_SHRINK 1375 shrink = F_SHRINK
1224 allow_unknown = F_ALLOW_UNKNOWN 1376 allow_unknown = F_ALLOW_UNKNOWN
1225 allow_sharing = F_ALLOW_SHARING 1377 allow_sharing = F_ALLOW_SHARING
1226 allow_cycles = F_ALLOW_CYCLES 1378 allow_cycles = F_ALLOW_CYCLES
1227 pack_strings = F_PACK_STRINGS 1379 pack_strings = F_PACK_STRINGS
1380 validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1381 PPCODE:
1229{ 1382{
1230 if (enable) 1383 if (enable)
1231 self->flags |= ix; 1384 self->flags |= ix;
1232 else 1385 else
1240 get_shrink = F_SHRINK 1393 get_shrink = F_SHRINK
1241 get_allow_unknown = F_ALLOW_UNKNOWN 1394 get_allow_unknown = F_ALLOW_UNKNOWN
1242 get_allow_sharing = F_ALLOW_SHARING 1395 get_allow_sharing = F_ALLOW_SHARING
1243 get_allow_cycles = F_ALLOW_CYCLES 1396 get_allow_cycles = F_ALLOW_CYCLES
1244 get_pack_strings = F_PACK_STRINGS 1397 get_pack_strings = F_PACK_STRINGS
1398 get_validate_utf8 = F_VALIDATE_UTF8
1245 PPCODE: 1399 PPCODE:
1246 XPUSHs (boolSV (self->flags & ix)); 1400 XPUSHs (boolSV (self->flags & ix));
1247 1401
1248void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1402void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1249 PPCODE: 1403 PPCODE:
1298 EXTEND (SP, 2); 1452 EXTEND (SP, 2);
1299 PUSHs (sv); 1453 PUSHs (sv);
1300 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1454 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1301} 1455}
1302 1456
1457void incr_parse (CBOR *self, SV *cborstr)
1458 ALIAS:
1459 incr_parse_multiple = 1
1460 PPCODE:
1461{
1462 if (SvUTF8 (cborstr))
1463 sv_utf8_downgrade (cborstr, 0);
1464
1465 if (!self->incr_count)
1466 {
1467 self->incr_count = newAV ();
1468 self->incr_pos = 0;
1469 self->incr_need = 1;
1470
1471 av_push (self->incr_count, newSViv (1));
1472 }
1473
1474 do
1475 {
1476 if (!incr_parse (self, cborstr))
1477 {
1478 if (self->incr_need > self->max_size && self->max_size)
1479 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1480 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1481
1482 break;
1483 }
1484
1485 SV *sv;
1486 char *offset;
1487
1488 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1489 XPUSHs (sv);
1490
1491 sv_chop (cborstr, offset);
1492
1493 av_clear (self->incr_count);
1494 av_push (self->incr_count, newSViv (1));
1495
1496 self->incr_pos = 0;
1497 self->incr_need = self->incr_pos + 1;
1498 }
1499 while (ix);
1500}
1501
1502void incr_reset (CBOR *self)
1503 CODE:
1504{
1505 SvREFCNT_dec (self->incr_count);
1506 self->incr_count = 0;
1507}
1508
1303void DESTROY (CBOR *self) 1509void DESTROY (CBOR *self)
1304 PPCODE: 1510 PPCODE:
1305 cbor_free (self); 1511 cbor_free (self);
1306 1512
1307PROTOTYPES: ENABLE 1513PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines