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.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))
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 encode_sv (enc, AvARRAY (av)[i]);
325 338
326 --enc->depth; 339 --enc->depth;
327} 340}
328 341
329static void 342static void
728{ 741{
729 // for speed reasons, we specialcase single-string 742 // for speed reasons, we specialcase single-string
730 // byte or utf-8 strings as keys, but only when !stringref 743 // byte or utf-8 strings as keys, but only when !stringref
731 744
732 if (ecb_expect_true (!dec->stringref)) 745 if (ecb_expect_true (!dec->stringref))
733 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 746 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
734 { 747 {
735 I32 len = decode_uint (dec); 748 I32 len = decode_uint (dec);
736 char *key = (char *)dec->cur; 749 char *key = (char *)dec->cur;
737 750
738 dec->cur += len; 751 dec->cur += len;
739 752
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); 753 hv_store (hv, key, len, decode_sv (dec), 0);
744 754
745 return; 755 return;
746 } 756 }
747 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 757 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
748 { 758 {
749 I32 len = decode_uint (dec); 759 I32 len = decode_uint (dec);
750 char *key = (char *)dec->cur; 760 char *key = (char *)dec->cur;
751 761
752 dec->cur += len; 762 dec->cur += len;
753 763
754 if (ecb_expect_false (dec->stringref)) 764 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
755 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)");
756 767
757 hv_store (hv, key, -len, decode_sv (dec), 0); 768 hv_store (hv, key, -len, decode_sv (dec), 0);
758 769
759 return; 770 return;
760 } 771 }
762 SV *k = decode_sv (dec); 773 SV *k = decode_sv (dec);
763 SV *v = decode_sv (dec); 774 SV *v = decode_sv (dec);
764 775
765 hv_store_ent (hv, k, v, 0); 776 hv_store_ent (hv, k, v, 0);
766 SvREFCNT_dec (k); 777 SvREFCNT_dec (k);
778
779fail:
780 ;
767} 781}
768 782
769static SV * 783static SV *
770decode_hv (dec_t *dec) 784decode_hv (dec_t *dec)
771{ 785{
853 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 867 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
854 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 868 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
855 } 869 }
856 870
857 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
858 SvUTF8_on (sv); 877 SvUTF8_on (sv);
878 }
859 879
860 return sv; 880 return sv;
861 881
862fail: 882fail:
863 SvREFCNT_dec (sv); 883 SvREFCNT_dec (sv);
912 case CBOR_TAG_VALUE_SHAREABLE: 932 case CBOR_TAG_VALUE_SHAREABLE:
913 { 933 {
914 if (ecb_expect_false (!dec->shareable)) 934 if (ecb_expect_false (!dec->shareable))
915 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 935 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
916 936
937 if (dec->cbor.flags & F_ALLOW_CYCLES)
938 {
917 sv = newSV (0); 939 sv = newSV (0);
918 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 940 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
919 941
920 SV *osv = decode_sv (dec); 942 SV *osv = decode_sv (dec);
921 sv_setsv (sv, osv); 943 sv_setsv (sv, osv);
922 SvREFCNT_dec_NN (osv); 944 SvREFCNT_dec_NN (osv);
945 }
946 else
947 {
948 av_push (dec->shareable, &PL_sv_undef);
949 int idx = AvFILLp (dec->shareable);
950 sv = decode_sv (dec);
951 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
952 }
923 } 953 }
924 break; 954 break;
925 955
926 case CBOR_TAG_VALUE_SHAREDREF: 956 case CBOR_TAG_VALUE_SHAREDREF:
927 { 957 {
932 962
933 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 963 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
934 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 964 ERR ("corrupted CBOR data (sharedref index out of bounds)");
935 965
936 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 966 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
967
968 if (sv == &PL_sv_undef)
969 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
937 } 970 }
938 break; 971 break;
939 972
940 case CBOR_TAG_PERL_OBJECT: 973 case CBOR_TAG_PERL_OBJECT:
941 { 974 {
1110 1143
1111 return newSVnv (ecb_binary64_to_double (fp)); 1144 return newSVnv (ecb_binary64_to_double (fp));
1112 } 1145 }
1113 1146
1114 // 0..19 unassigned simple 1147 // 0..19 unassigned simple
1115 // 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
1116 default: 1151 default:
1117 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1152 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1118 } 1153 }
1119 1154
1120 break; 1155 break;
1121 } 1156 }
1122 1157
1149 if (dec.cur != dec.end && !dec.err) 1184 if (dec.cur != dec.end && !dec.err)
1150 dec.err = "garbage after CBOR object"; 1185 dec.err = "garbage after CBOR object";
1151 1186
1152 if (dec.err) 1187 if (dec.err)
1153 { 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
1154 SvREFCNT_dec (sv); 1200 SvREFCNT_dec (sv);
1155 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);
1156 } 1202 }
1157 1203
1158 sv = sv_2mortal (sv); 1204 sv = sv_2mortal (sv);
1159 1205
1160 return sv; 1206 return sv;
1161} 1207}
1162 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
1163///////////////////////////////////////////////////////////////////////////// 1329/////////////////////////////////////////////////////////////////////////////
1164// XS interface functions 1330// XS interface functions
1165 1331
1166MODULE = CBOR::XS PACKAGE = CBOR::XS 1332MODULE = CBOR::XS PACKAGE = CBOR::XS
1167 1333
1207void shrink (CBOR *self, int enable = 1) 1373void shrink (CBOR *self, int enable = 1)
1208 ALIAS: 1374 ALIAS:
1209 shrink = F_SHRINK 1375 shrink = F_SHRINK
1210 allow_unknown = F_ALLOW_UNKNOWN 1376 allow_unknown = F_ALLOW_UNKNOWN
1211 allow_sharing = F_ALLOW_SHARING 1377 allow_sharing = F_ALLOW_SHARING
1378 allow_cycles = F_ALLOW_CYCLES
1212 pack_strings = F_PACK_STRINGS 1379 pack_strings = F_PACK_STRINGS
1380 validate_utf8 = F_VALIDATE_UTF8
1213 PPCODE: 1381 PPCODE:
1214{ 1382{
1215 if (enable) 1383 if (enable)
1216 self->flags |= ix; 1384 self->flags |= ix;
1217 else 1385 else
1223void get_shrink (CBOR *self) 1391void get_shrink (CBOR *self)
1224 ALIAS: 1392 ALIAS:
1225 get_shrink = F_SHRINK 1393 get_shrink = F_SHRINK
1226 get_allow_unknown = F_ALLOW_UNKNOWN 1394 get_allow_unknown = F_ALLOW_UNKNOWN
1227 get_allow_sharing = F_ALLOW_SHARING 1395 get_allow_sharing = F_ALLOW_SHARING
1396 get_allow_cycles = F_ALLOW_CYCLES
1228 get_pack_strings = F_PACK_STRINGS 1397 get_pack_strings = F_PACK_STRINGS
1398 get_validate_utf8 = F_VALIDATE_UTF8
1229 PPCODE: 1399 PPCODE:
1230 XPUSHs (boolSV (self->flags & ix)); 1400 XPUSHs (boolSV (self->flags & ix));
1231 1401
1232void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1402void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1233 PPCODE: 1403 PPCODE:
1282 EXTEND (SP, 2); 1452 EXTEND (SP, 2);
1283 PUSHs (sv); 1453 PUSHs (sv);
1284 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1454 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1285} 1455}
1286 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
1287void DESTROY (CBOR *self) 1509void DESTROY (CBOR *self)
1288 PPCODE: 1510 PPCODE:
1289 cbor_free (self); 1511 cbor_free (self);
1290 1512
1291PROTOTYPES: ENABLE 1513PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines