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.39 by root, Sun Dec 1 14:45:03 2013 UTC vs.
Revision 1.46 by root, Sun Dec 14 05:57:22 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))
128typedef struct { 129typedef struct {
129 U32 flags; 130 U32 flags;
130 U32 max_depth; 131 U32 max_depth;
131 STRLEN max_size; 132 STRLEN max_size;
132 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.
133} CBOR; 139} CBOR;
134 140
135ecb_inline void 141ecb_inline void
136cbor_init (CBOR *cbor) 142cbor_init (CBOR *cbor)
137{ 143{
141 147
142ecb_inline void 148ecb_inline void
143cbor_free (CBOR *cbor) 149cbor_free (CBOR *cbor)
144{ 150{
145 SvREFCNT_dec (cbor->filter); 151 SvREFCNT_dec (cbor->filter);
152 SvREFCNT_dec (cbor->incr_count);
146} 153}
147 154
148///////////////////////////////////////////////////////////////////////////// 155/////////////////////////////////////////////////////////////////////////////
149// utility functions 156// utility functions
150 157
317 324
318 ++enc->depth; 325 ++enc->depth;
319 326
320 encode_uint (enc, MAJOR_ARRAY, len + 1); 327 encode_uint (enc, MAJOR_ARRAY, len + 1);
321 328
329 if (SvMAGICAL (av))
322 for (i = 0; i <= len; ++i) 330 for (i = 0; i <= len; ++i)
323 { 331 {
324 SV **svp = av_fetch (av, i, 0); 332 SV **svp = av_fetch (av, i, 0);
325 encode_sv (enc, svp ? *svp : &PL_sv_undef); 333 encode_sv (enc, svp ? *svp : &PL_sv_undef);
326 } 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 }
327 341
328 --enc->depth; 342 --enc->depth;
329} 343}
330 344
331static void 345static void
730{ 744{
731 // for speed reasons, we specialcase single-string 745 // for speed reasons, we specialcase single-string
732 // byte or utf-8 strings as keys, but only when !stringref 746 // byte or utf-8 strings as keys, but only when !stringref
733 747
734 if (ecb_expect_true (!dec->stringref)) 748 if (ecb_expect_true (!dec->stringref))
735 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 749 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
736 { 750 {
737 I32 len = decode_uint (dec); 751 I32 len = decode_uint (dec);
738 char *key = (char *)dec->cur; 752 char *key = (char *)dec->cur;
739 753
740 dec->cur += len; 754 dec->cur += len;
741 755
742 hv_store (hv, key, len, decode_sv (dec), 0); 756 hv_store (hv, key, len, decode_sv (dec), 0);
743 757
744 return; 758 return;
745 } 759 }
746 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 760 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 761 {
748 I32 len = decode_uint (dec); 762 I32 len = decode_uint (dec);
749 char *key = (char *)dec->cur; 763 char *key = (char *)dec->cur;
750 764
751 dec->cur += len; 765 dec->cur += len;
1132 1146
1133 return newSVnv (ecb_binary64_to_double (fp)); 1147 return newSVnv (ecb_binary64_to_double (fp));
1134 } 1148 }
1135 1149
1136 // 0..19 unassigned simple 1150 // 0..19 unassigned simple
1137 // 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
1138 default: 1154 default:
1139 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1155 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1140 } 1156 }
1141 1157
1142 break; 1158 break;
1143 } 1159 }
1144 1160
1191 sv = sv_2mortal (sv); 1207 sv = sv_2mortal (sv);
1192 1208
1193 return sv; 1209 return sv;
1194} 1210}
1195 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_BYTES >> MAJOR_SHIFT:
1270 case MAJOR_TEXT >> MAJOR_SHIFT:
1271 case MAJOR_ARRAY >> MAJOR_SHIFT:
1272 case MAJOR_MAP >> MAJOR_SHIFT:
1273 {
1274 UV len;
1275
1276 if (ecb_expect_false (ilen))
1277 {
1278 len = 0;
1279
1280 do {
1281 len = (len << 8) | *++p;
1282 } while (--ilen);
1283 }
1284 else
1285 len = m;
1286
1287 switch (major)
1288 {
1289 case MAJOR_BYTES >> MAJOR_SHIFT:
1290 case MAJOR_TEXT >> MAJOR_SHIFT:
1291 self->incr_need += len;
1292 if (ecb_expect_false (self->incr_need > cur))
1293 return 0;
1294
1295 break;
1296
1297 case MAJOR_MAP >> MAJOR_SHIFT:
1298 len <<= 1;
1299 case MAJOR_ARRAY >> MAJOR_SHIFT:
1300 if (len)
1301 {
1302 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1303 count = len + 1;
1304 }
1305 break;
1306 }
1307 }
1308 }
1309 }
1310
1311 self->incr_pos = self->incr_need;
1312
1313 if (count > 0)
1314 {
1315 while (!--count)
1316 {
1317 if (!AvFILLp (self->incr_count))
1318 return 1; // done
1319
1320 SvREFCNT_dec_NN (av_pop (self->incr_count));
1321 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1322 }
1323
1324 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1325 }
1326 }
1327
1328 return 0;
1329}
1330
1331
1196///////////////////////////////////////////////////////////////////////////// 1332/////////////////////////////////////////////////////////////////////////////
1197// XS interface functions 1333// XS interface functions
1198 1334
1199MODULE = CBOR::XS PACKAGE = CBOR::XS 1335MODULE = CBOR::XS PACKAGE = CBOR::XS
1200 1336
1319 EXTEND (SP, 2); 1455 EXTEND (SP, 2);
1320 PUSHs (sv); 1456 PUSHs (sv);
1321 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1457 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1322} 1458}
1323 1459
1460void incr_parse (CBOR *self, SV *cborstr)
1461 ALIAS:
1462 incr_parse_multiple = 1
1463 PPCODE:
1464{
1465 if (SvUTF8 (cborstr))
1466 sv_utf8_downgrade (cborstr, 0);
1467
1468 if (!self->incr_count)
1469 {
1470 self->incr_count = newAV ();
1471 self->incr_pos = 0;
1472 self->incr_need = 1;
1473
1474 av_push (self->incr_count, newSViv (1));
1475 }
1476
1477 do
1478 {
1479 if (!incr_parse (self, cborstr))
1480 {
1481 if (self->incr_need > self->max_size && self->max_size)
1482 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1483 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1484
1485 break;
1486 }
1487
1488 SV *sv;
1489 char *offset;
1490
1491 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1492 XPUSHs (sv);
1493
1494 sv_chop (cborstr, offset);
1495
1496 av_clear (self->incr_count);
1497 av_push (self->incr_count, newSViv (1));
1498
1499 self->incr_pos = 0;
1500 self->incr_need = self->incr_pos + 1;
1501 }
1502 while (ix);
1503}
1504
1505void incr_reset (CBOR *self)
1506 CODE:
1507{
1508 SvREFCNT_dec (self->incr_count);
1509 self->incr_count = 0;
1510}
1511
1324void DESTROY (CBOR *self) 1512void DESTROY (CBOR *self)
1325 PPCODE: 1513 PPCODE:
1326 cbor_free (self); 1514 cbor_free (self);
1327 1515
1328PROTOTYPES: ENABLE 1516PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines