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.36 by root, Sat Nov 30 17:37:45 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))
97}; 98};
98 99
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
103#define F_ALLOW_CYCLES 0x00000008UL
102#define F_PACK_STRINGS 0x00000008UL 104#define F_PACK_STRINGS 0x00000010UL
105#define F_VALIDATE_UTF8 0x00000020UL
103 106
104#define INIT_SIZE 32 // initial scalar size to be allocated 107#define INIT_SIZE 32 // initial scalar size to be allocated
105 108
106#define SB do { 109#define SB do {
107#define SE } while (0) 110#define SE } while (0)
126typedef struct { 129typedef struct {
127 U32 flags; 130 U32 flags;
128 U32 max_depth; 131 U32 max_depth;
129 STRLEN max_size; 132 STRLEN max_size;
130 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.
131} CBOR; 139} CBOR;
132 140
133ecb_inline void 141ecb_inline void
134cbor_init (CBOR *cbor) 142cbor_init (CBOR *cbor)
135{ 143{
139 147
140ecb_inline void 148ecb_inline void
141cbor_free (CBOR *cbor) 149cbor_free (CBOR *cbor)
142{ 150{
143 SvREFCNT_dec (cbor->filter); 151 SvREFCNT_dec (cbor->filter);
152 SvREFCNT_dec (cbor->incr_count);
144} 153}
145 154
146///////////////////////////////////////////////////////////////////////////// 155/////////////////////////////////////////////////////////////////////////////
147// utility functions 156// utility functions
148 157
315 324
316 ++enc->depth; 325 ++enc->depth;
317 326
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 327 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 328
329 if (SvMAGICAL (av))
320 for (i = 0; i <= len; ++i) 330 for (i = 0; i <= len; ++i)
321 { 331 {
322 SV **svp = av_fetch (av, i, 0); 332 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 333 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 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 }
325 341
326 --enc->depth; 342 --enc->depth;
327} 343}
328 344
329static void 345static void
728{ 744{
729 // for speed reasons, we specialcase single-string 745 // for speed reasons, we specialcase single-string
730 // byte or utf-8 strings as keys, but only when !stringref 746 // byte or utf-8 strings as keys, but only when !stringref
731 747
732 if (ecb_expect_true (!dec->stringref)) 748 if (ecb_expect_true (!dec->stringref))
733 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 749 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
734 { 750 {
735 I32 len = decode_uint (dec); 751 I32 len = decode_uint (dec);
736 char *key = (char *)dec->cur; 752 char *key = (char *)dec->cur;
737 753
738 dec->cur += len; 754 dec->cur += len;
739 755
740 if (ecb_expect_false (dec->stringref))
741 av_push (dec->stringref, newSVpvn (key, len));
742
743 hv_store (hv, key, len, decode_sv (dec), 0); 756 hv_store (hv, key, len, decode_sv (dec), 0);
744 757
745 return; 758 return;
746 } 759 }
747 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 760 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
748 { 761 {
749 I32 len = decode_uint (dec); 762 I32 len = decode_uint (dec);
750 char *key = (char *)dec->cur; 763 char *key = (char *)dec->cur;
751 764
752 dec->cur += len; 765 dec->cur += len;
753 766
754 if (ecb_expect_false (dec->stringref)) 767 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
755 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)");
756 770
757 hv_store (hv, key, -len, decode_sv (dec), 0); 771 hv_store (hv, key, -len, decode_sv (dec), 0);
758 772
759 return; 773 return;
760 } 774 }
762 SV *k = decode_sv (dec); 776 SV *k = decode_sv (dec);
763 SV *v = decode_sv (dec); 777 SV *v = decode_sv (dec);
764 778
765 hv_store_ent (hv, k, v, 0); 779 hv_store_ent (hv, k, v, 0);
766 SvREFCNT_dec (k); 780 SvREFCNT_dec (k);
781
782fail:
783 ;
767} 784}
768 785
769static SV * 786static SV *
770decode_hv (dec_t *dec) 787decode_hv (dec_t *dec)
771{ 788{
853 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 870 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
854 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 871 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
855 } 872 }
856 873
857 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
858 SvUTF8_on (sv); 880 SvUTF8_on (sv);
881 }
859 882
860 return sv; 883 return sv;
861 884
862fail: 885fail:
863 SvREFCNT_dec (sv); 886 SvREFCNT_dec (sv);
912 case CBOR_TAG_VALUE_SHAREABLE: 935 case CBOR_TAG_VALUE_SHAREABLE:
913 { 936 {
914 if (ecb_expect_false (!dec->shareable)) 937 if (ecb_expect_false (!dec->shareable))
915 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 938 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
916 939
940 if (dec->cbor.flags & F_ALLOW_CYCLES)
941 {
917 sv = newSV (0); 942 sv = newSV (0);
918 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 943 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
919 944
920 SV *osv = decode_sv (dec); 945 SV *osv = decode_sv (dec);
921 sv_setsv (sv, osv); 946 sv_setsv (sv, osv);
922 SvREFCNT_dec_NN (osv); 947 SvREFCNT_dec_NN (osv);
948 }
949 else
950 {
951 av_push (dec->shareable, &PL_sv_undef);
952 int idx = AvFILLp (dec->shareable);
953 sv = decode_sv (dec);
954 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
955 }
923 } 956 }
924 break; 957 break;
925 958
926 case CBOR_TAG_VALUE_SHAREDREF: 959 case CBOR_TAG_VALUE_SHAREDREF:
927 { 960 {
932 965
933 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 966 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
934 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 967 ERR ("corrupted CBOR data (sharedref index out of bounds)");
935 968
936 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 969 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
970
971 if (sv == &PL_sv_undef)
972 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
937 } 973 }
938 break; 974 break;
939 975
940 case CBOR_TAG_PERL_OBJECT: 976 case CBOR_TAG_PERL_OBJECT:
941 { 977 {
1110 1146
1111 return newSVnv (ecb_binary64_to_double (fp)); 1147 return newSVnv (ecb_binary64_to_double (fp));
1112 } 1148 }
1113 1149
1114 // 0..19 unassigned simple 1150 // 0..19 unassigned simple
1115 // 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
1116 default: 1154 default:
1117 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1155 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1118 } 1156 }
1119 1157
1120 break; 1158 break;
1121 } 1159 }
1122 1160
1149 if (dec.cur != dec.end && !dec.err) 1187 if (dec.cur != dec.end && !dec.err)
1150 dec.err = "garbage after CBOR object"; 1188 dec.err = "garbage after CBOR object";
1151 1189
1152 if (dec.err) 1190 if (dec.err)
1153 { 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
1154 SvREFCNT_dec (sv); 1203 SvREFCNT_dec (sv);
1155 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);
1156 } 1205 }
1157 1206
1158 sv = sv_2mortal (sv); 1207 sv = sv_2mortal (sv);
1159 1208
1160 return sv; 1209 return sv;
1161} 1210}
1162 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
1163///////////////////////////////////////////////////////////////////////////// 1332/////////////////////////////////////////////////////////////////////////////
1164// XS interface functions 1333// XS interface functions
1165 1334
1166MODULE = CBOR::XS PACKAGE = CBOR::XS 1335MODULE = CBOR::XS PACKAGE = CBOR::XS
1167 1336
1207void shrink (CBOR *self, int enable = 1) 1376void shrink (CBOR *self, int enable = 1)
1208 ALIAS: 1377 ALIAS:
1209 shrink = F_SHRINK 1378 shrink = F_SHRINK
1210 allow_unknown = F_ALLOW_UNKNOWN 1379 allow_unknown = F_ALLOW_UNKNOWN
1211 allow_sharing = F_ALLOW_SHARING 1380 allow_sharing = F_ALLOW_SHARING
1381 allow_cycles = F_ALLOW_CYCLES
1212 pack_strings = F_PACK_STRINGS 1382 pack_strings = F_PACK_STRINGS
1383 validate_utf8 = F_VALIDATE_UTF8
1213 PPCODE: 1384 PPCODE:
1214{ 1385{
1215 if (enable) 1386 if (enable)
1216 self->flags |= ix; 1387 self->flags |= ix;
1217 else 1388 else
1223void get_shrink (CBOR *self) 1394void get_shrink (CBOR *self)
1224 ALIAS: 1395 ALIAS:
1225 get_shrink = F_SHRINK 1396 get_shrink = F_SHRINK
1226 get_allow_unknown = F_ALLOW_UNKNOWN 1397 get_allow_unknown = F_ALLOW_UNKNOWN
1227 get_allow_sharing = F_ALLOW_SHARING 1398 get_allow_sharing = F_ALLOW_SHARING
1399 get_allow_cycles = F_ALLOW_CYCLES
1228 get_pack_strings = F_PACK_STRINGS 1400 get_pack_strings = F_PACK_STRINGS
1401 get_validate_utf8 = F_VALIDATE_UTF8
1229 PPCODE: 1402 PPCODE:
1230 XPUSHs (boolSV (self->flags & ix)); 1403 XPUSHs (boolSV (self->flags & ix));
1231 1404
1232void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1405void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1233 PPCODE: 1406 PPCODE:
1282 EXTEND (SP, 2); 1455 EXTEND (SP, 2);
1283 PUSHs (sv); 1456 PUSHs (sv);
1284 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1457 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1285} 1458}
1286 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
1287void DESTROY (CBOR *self) 1512void DESTROY (CBOR *self)
1288 PPCODE: 1513 PPCODE:
1289 cbor_free (self); 1514 cbor_free (self);
1290 1515
1291PROTOTYPES: ENABLE 1516PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines