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.53 by root, Sun Apr 24 19:51:41 2016 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_UTF8_STRINGS 0x00000020UL
106#define F_UTF8_KEYS 0x00000040UL
107#define F_VALIDATE_UTF8 0x00000080UL
103 108
104#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
105 110
106#define SB do { 111#define SB do {
107#define SE } while (0) 112#define SE } while (0)
126typedef struct { 131typedef struct {
127 U32 flags; 132 U32 flags;
128 U32 max_depth; 133 U32 max_depth;
129 STRLEN max_size; 134 STRLEN max_size;
130 SV *filter; 135 SV *filter;
136
137 // for the incremental parser
138 STRLEN incr_pos; // the current offset into the text
139 STRLEN incr_need; // minimum bytes needed to decode
140 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
131} CBOR; 141} CBOR;
132 142
133ecb_inline void 143ecb_inline void
134cbor_init (CBOR *cbor) 144cbor_init (CBOR *cbor)
135{ 145{
139 149
140ecb_inline void 150ecb_inline void
141cbor_free (CBOR *cbor) 151cbor_free (CBOR *cbor)
142{ 152{
143 SvREFCNT_dec (cbor->filter); 153 SvREFCNT_dec (cbor->filter);
154 SvREFCNT_dec (cbor->incr_count);
144} 155}
145 156
146///////////////////////////////////////////////////////////////////////////// 157/////////////////////////////////////////////////////////////////////////////
147// utility functions 158// utility functions
148 159
267encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
268{ 279{
269 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
270} 281}
271 282
283// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
284ecb_noinline static void
285encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
286{
287 STRLEN ulen = len;
288 U8 *p, *pend = (U8 *)str + len;
289
290 for (p = (U8 *)str; p < pend; ++p)
291 ulen += *p >> 7; // count set high bits
292
293 encode_uint (enc, MAJOR_TEXT, ulen);
294
295 need (enc, ulen);
296 for (p = (U8 *)str; p < pend; ++p)
297 if (*p < 0x80)
298 *enc->cur++ = *p;
299 else
300 {
301 *enc->cur++ = 0xc0 + (*p >> 6);
302 *enc->cur++ = 0x80 + (*p & 63);
303 }
304}
305
272ecb_inline void 306ecb_inline void
273encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
274{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
275 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
276 need (enc, len); 317 need (enc, len);
277 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
278 enc->cur += len; 319 enc->cur += len;
279} 320}
280 321
281static void 322ecb_inline
282encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 324{
284 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
285 { 326 {
286 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
287 328
298 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
299 ++enc->stringref_idx; 340 ++enc->stringref_idx;
300 } 341 }
301 } 342 }
302 343
303 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
304} 345}
305 346
306static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
307 348
308static void 349static void
315 356
316 ++enc->depth; 357 ++enc->depth;
317 358
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 359 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 360
361 if (SvMAGICAL (av))
320 for (i = 0; i <= len; ++i) 362 for (i = 0; i <= len; ++i)
321 { 363 {
322 SV **svp = av_fetch (av, i, 0); 364 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 365 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 366 }
367 else
368 for (i = 0; i <= len; ++i)
369 {
370 SV *sv = AvARRAY (av)[i];
371 encode_sv (enc, sv ? sv : &PL_sv_undef);
372 }
325 373
326 --enc->depth; 374 --enc->depth;
327} 375}
328 376
329static void 377static void
347 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
348 { 396 {
349 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
350 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
351 else 399 else
352 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 400 encode_strref (enc, enc->cbor.flags & (F_UTF8_KEYS | F_UTF8_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
353 401
354 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 402 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
355 } 403 }
356 404
357 if (mg) 405 if (mg)
433 481
434 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
435 { 483 {
436 dSP; 484 dSP;
437 485
438 ENTER; SAVETMPS; PUSHMARK (SP); 486 ENTER; SAVETMPS;
487 PUSHMARK (SP);
439 // we re-bless the reference to get overload and other niceties right 488 // we re-bless the reference to get overload and other niceties right
440 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 489 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
441 490
442 PUTBACK; 491 PUTBACK;
443 // G_SCALAR ensures that return value is 1 492 // G_SCALAR ensures that return value is 1
456 } 505 }
457 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 506 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
458 { 507 {
459 dSP; 508 dSP;
460 509
461 ENTER; SAVETMPS; PUSHMARK (SP); 510 ENTER; SAVETMPS;
511 SAVESTACK_POS ();
512 PUSHMARK (SP);
462 EXTEND (SP, 2); 513 EXTEND (SP, 2);
463 // we re-bless the reference to get overload and other niceties right 514 // we re-bless the reference to get overload and other niceties right
464 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 515 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
465 PUSHs (sv_cbor); 516 PUSHs (sv_cbor);
466 517
472 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
473 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 524 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
474 525
475 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
476 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
477 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
478 529
479 while (count) 530 while (count)
480 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
481 532
482 PUTBACK; 533 PUTBACK;
541 592
542 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
543 { 594 {
544 STRLEN len; 595 STRLEN len;
545 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
546 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_UTF8_STRINGS, SvUTF8 (sv), str, len);
547 } 598 }
548 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
549 encode_nv (enc, sv); 600 encode_nv (enc, sv);
550 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
551 { 602 {
568} 619}
569 620
570static SV * 621static SV *
571encode_cbor (SV *scalar, CBOR *cbor) 622encode_cbor (SV *scalar, CBOR *cbor)
572{ 623{
573 enc_t enc = { }; 624 enc_t enc = { 0 };
574 625
575 enc.cbor = *cbor; 626 enc.cbor = *cbor;
576 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
577 enc.cur = SvPVX (enc.sv); 628 enc.cur = SvPVX (enc.sv);
578 enc.end = SvEND (enc.sv); 629 enc.end = SvEND (enc.sv);
728{ 779{
729 // for speed reasons, we specialcase single-string 780 // for speed reasons, we specialcase single-string
730 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
731 782
732 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
733 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
734 { 785 {
735 I32 len = decode_uint (dec); 786 I32 len = decode_uint (dec);
736 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
737 788
789 WANT (len);
738 dec->cur += len; 790 dec->cur += len;
739 791
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); 792 hv_store (hv, key, len, decode_sv (dec), 0);
744 793
745 return; 794 return;
746 } 795 }
747 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
748 { 797 {
749 I32 len = decode_uint (dec); 798 I32 len = decode_uint (dec);
750 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
751 800
801 WANT (len);
752 dec->cur += len; 802 dec->cur += len;
753 803
754 if (ecb_expect_false (dec->stringref)) 804 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
755 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 805 if (!is_utf8_string (key, len))
806 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
756 807
757 hv_store (hv, key, -len, decode_sv (dec), 0); 808 hv_store (hv, key, -len, decode_sv (dec), 0);
758 809
759 return; 810 return;
760 } 811 }
762 SV *k = decode_sv (dec); 813 SV *k = decode_sv (dec);
763 SV *v = decode_sv (dec); 814 SV *v = decode_sv (dec);
764 815
765 hv_store_ent (hv, k, v, 0); 816 hv_store_ent (hv, k, v, 0);
766 SvREFCNT_dec (k); 817 SvREFCNT_dec (k);
818
819fail:
820 ;
767} 821}
768 822
769static SV * 823static SV *
770decode_hv (dec_t *dec) 824decode_hv (dec_t *dec)
771{ 825{
853 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 907 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
854 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 908 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
855 } 909 }
856 910
857 if (utf8) 911 if (utf8)
912 {
913 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
914 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
915 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
916
858 SvUTF8_on (sv); 917 SvUTF8_on (sv);
918 }
859 919
860 return sv; 920 return sv;
861 921
862fail: 922fail:
863 SvREFCNT_dec (sv); 923 SvREFCNT_dec (sv);
912 case CBOR_TAG_VALUE_SHAREABLE: 972 case CBOR_TAG_VALUE_SHAREABLE:
913 { 973 {
914 if (ecb_expect_false (!dec->shareable)) 974 if (ecb_expect_false (!dec->shareable))
915 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 975 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
916 976
977 if (dec->cbor.flags & F_ALLOW_CYCLES)
978 {
917 sv = newSV (0); 979 sv = newSV (0);
918 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 980 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
919 981
920 SV *osv = decode_sv (dec); 982 SV *osv = decode_sv (dec);
921 sv_setsv (sv, osv); 983 sv_setsv (sv, osv);
922 SvREFCNT_dec_NN (osv); 984 SvREFCNT_dec_NN (osv);
985 }
986 else
987 {
988 av_push (dec->shareable, &PL_sv_undef);
989 int idx = AvFILLp (dec->shareable);
990 sv = decode_sv (dec);
991 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
992 }
923 } 993 }
924 break; 994 break;
925 995
926 case CBOR_TAG_VALUE_SHAREDREF: 996 case CBOR_TAG_VALUE_SHAREDREF:
927 { 997 {
932 1002
933 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1003 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
934 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1004 ERR ("corrupted CBOR data (sharedref index out of bounds)");
935 1005
936 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1006 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1007
1008 if (sv == &PL_sv_undef)
1009 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
937 } 1010 }
938 break; 1011 break;
939 1012
940 case CBOR_TAG_PERL_OBJECT: 1013 case CBOR_TAG_PERL_OBJECT:
941 { 1014 {
956 if (!method) 1029 if (!method)
957 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1030 ERR ("cannot decode perl-object (package does not have a THAW method)");
958 1031
959 dSP; 1032 dSP;
960 1033
961 ENTER; SAVETMPS; PUSHMARK (SP); 1034 ENTER; SAVETMPS;
1035 PUSHMARK (SP);
962 EXTEND (SP, len + 1); 1036 EXTEND (SP, len + 1);
963 // we re-bless the reference to get overload and other niceties right 1037 // we re-bless the reference to get overload and other niceties right
964 PUSHs (*av_fetch (av, 0, 1)); 1038 PUSHs (*av_fetch (av, 0, 1));
965 PUSHs (sv_cbor); 1039 PUSHs (sv_cbor);
966 1040
991 default: 1065 default:
992 { 1066 {
993 sv = decode_sv (dec); 1067 sv = decode_sv (dec);
994 1068
995 dSP; 1069 dSP;
996 ENTER; SAVETMPS; PUSHMARK (SP); 1070 ENTER; SAVETMPS;
1071 SAVESTACK_POS ();
1072 PUSHMARK (SP);
997 EXTEND (SP, 2); 1073 EXTEND (SP, 2);
998 PUSHs (newSVuv (tag)); 1074 PUSHs (newSVuv (tag));
999 PUSHs (sv); 1075 PUSHs (sv);
1000 1076
1001 PUTBACK; 1077 PUTBACK;
1110 1186
1111 return newSVnv (ecb_binary64_to_double (fp)); 1187 return newSVnv (ecb_binary64_to_double (fp));
1112 } 1188 }
1113 1189
1114 // 0..19 unassigned simple 1190 // 0..19 unassigned simple
1115 // 24 reserved + unassigned (reserved values are not encodable) 1191 // 24 reserved + unassigned simple (reserved values are not encodable)
1192 // 28-30 unassigned misc
1193 // 31 break code
1116 default: 1194 default:
1117 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1195 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1118 } 1196 }
1119 1197
1120 break; 1198 break;
1121 } 1199 }
1122 1200
1125} 1203}
1126 1204
1127static SV * 1205static SV *
1128decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1206decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1129{ 1207{
1130 dec_t dec = { }; 1208 dec_t dec = { 0 };
1131 SV *sv; 1209 SV *sv;
1132 STRLEN len; 1210 STRLEN len;
1133 char *data = SvPVbyte (string, len); 1211 char *data = SvPVbyte (string, len);
1134 1212
1135 if (len > cbor->max_size && cbor->max_size) 1213 if (len > cbor->max_size && cbor->max_size)
1149 if (dec.cur != dec.end && !dec.err) 1227 if (dec.cur != dec.end && !dec.err)
1150 dec.err = "garbage after CBOR object"; 1228 dec.err = "garbage after CBOR object";
1151 1229
1152 if (dec.err) 1230 if (dec.err)
1153 { 1231 {
1232 if (dec.shareable)
1233 {
1234 // need to break cyclic links, which whould all be in shareable
1235 int i;
1236 SV **svp;
1237
1238 for (i = av_len (dec.shareable) + 1; i--; )
1239 if ((svp = av_fetch (dec.shareable, i, 0)))
1240 sv_setsv (*svp, &PL_sv_undef);
1241 }
1242
1154 SvREFCNT_dec (sv); 1243 SvREFCNT_dec (sv);
1155 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1244 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1156 } 1245 }
1157 1246
1158 sv = sv_2mortal (sv); 1247 sv = sv_2mortal (sv);
1159 1248
1160 return sv; 1249 return sv;
1161} 1250}
1162 1251
1252/////////////////////////////////////////////////////////////////////////////
1253// incremental parser
1254
1255#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1256
1257// returns 0 for notyet, 1 for success or error
1258static int
1259incr_parse (CBOR *self, SV *cborstr)
1260{
1261 STRLEN cur;
1262 SvPV (cborstr, cur);
1263
1264 while (ecb_expect_true (self->incr_need <= cur))
1265 {
1266 // table of integer count bytes
1267 static I8 incr_len[MINOR_MASK + 1] = {
1268 0, 0, 0, 0, 0, 0, 0, 0,
1269 0, 0, 0, 0, 0, 0, 0, 0,
1270 0, 0, 0, 0, 0, 0, 0, 0,
1271 1, 2, 4, 8,-1,-1,-1,-2
1272 };
1273
1274 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1275 U8 m = *p & MINOR_MASK;
1276 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1277 I8 ilen = incr_len[m];
1278
1279 self->incr_need = self->incr_pos + 1;
1280
1281 if (ecb_expect_false (ilen < 0))
1282 {
1283 if (m != MINOR_INDEF)
1284 return 1; // error
1285
1286 if (*p == (MAJOR_MISC | MINOR_INDEF))
1287 {
1288 if (count >= 0)
1289 return 1; // error
1290
1291 count = 1;
1292 }
1293 else
1294 {
1295 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1296 count = -1;
1297 }
1298 }
1299 else
1300 {
1301 self->incr_need += ilen;
1302 if (ecb_expect_false (self->incr_need > cur))
1303 return 0;
1304
1305 int major = *p >> MAJOR_SHIFT;
1306
1307 switch (major)
1308 {
1309 case MAJOR_TAG >> MAJOR_SHIFT:
1310 ++count; // tags merely prefix another value
1311 break;
1312
1313 case MAJOR_BYTES >> MAJOR_SHIFT:
1314 case MAJOR_TEXT >> MAJOR_SHIFT:
1315 case MAJOR_ARRAY >> MAJOR_SHIFT:
1316 case MAJOR_MAP >> MAJOR_SHIFT:
1317 {
1318 UV len;
1319
1320 if (ecb_expect_false (ilen))
1321 {
1322 len = 0;
1323
1324 do {
1325 len = (len << 8) | *++p;
1326 } while (--ilen);
1327 }
1328 else
1329 len = m;
1330
1331 switch (major)
1332 {
1333 case MAJOR_BYTES >> MAJOR_SHIFT:
1334 case MAJOR_TEXT >> MAJOR_SHIFT:
1335 self->incr_need += len;
1336 if (ecb_expect_false (self->incr_need > cur))
1337 return 0;
1338
1339 break;
1340
1341 case MAJOR_MAP >> MAJOR_SHIFT:
1342 len <<= 1;
1343 case MAJOR_ARRAY >> MAJOR_SHIFT:
1344 if (len)
1345 {
1346 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1347 count = len + 1;
1348 }
1349 break;
1350 }
1351 }
1352 }
1353 }
1354
1355 self->incr_pos = self->incr_need;
1356
1357 if (count > 0)
1358 {
1359 while (!--count)
1360 {
1361 if (!AvFILLp (self->incr_count))
1362 return 1; // done
1363
1364 SvREFCNT_dec_NN (av_pop (self->incr_count));
1365 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1366 }
1367
1368 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1369 }
1370 }
1371
1372 return 0;
1373}
1374
1375
1163///////////////////////////////////////////////////////////////////////////// 1376/////////////////////////////////////////////////////////////////////////////
1164// XS interface functions 1377// XS interface functions
1165 1378
1166MODULE = CBOR::XS PACKAGE = CBOR::XS 1379MODULE = CBOR::XS PACKAGE = CBOR::XS
1167 1380
1207void shrink (CBOR *self, int enable = 1) 1420void shrink (CBOR *self, int enable = 1)
1208 ALIAS: 1421 ALIAS:
1209 shrink = F_SHRINK 1422 shrink = F_SHRINK
1210 allow_unknown = F_ALLOW_UNKNOWN 1423 allow_unknown = F_ALLOW_UNKNOWN
1211 allow_sharing = F_ALLOW_SHARING 1424 allow_sharing = F_ALLOW_SHARING
1425 allow_cycles = F_ALLOW_CYCLES
1212 pack_strings = F_PACK_STRINGS 1426 pack_strings = F_PACK_STRINGS
1427 utf8_strings = F_UTF8_STRINGS
1428 validate_utf8 = F_VALIDATE_UTF8
1213 PPCODE: 1429 PPCODE:
1214{ 1430{
1215 if (enable) 1431 if (enable)
1216 self->flags |= ix; 1432 self->flags |= ix;
1217 else 1433 else
1223void get_shrink (CBOR *self) 1439void get_shrink (CBOR *self)
1224 ALIAS: 1440 ALIAS:
1225 get_shrink = F_SHRINK 1441 get_shrink = F_SHRINK
1226 get_allow_unknown = F_ALLOW_UNKNOWN 1442 get_allow_unknown = F_ALLOW_UNKNOWN
1227 get_allow_sharing = F_ALLOW_SHARING 1443 get_allow_sharing = F_ALLOW_SHARING
1444 get_allow_cycles = F_ALLOW_CYCLES
1228 get_pack_strings = F_PACK_STRINGS 1445 get_pack_strings = F_PACK_STRINGS
1446 get_validate_utf8 = F_VALIDATE_UTF8
1229 PPCODE: 1447 PPCODE:
1230 XPUSHs (boolSV (self->flags & ix)); 1448 XPUSHs (boolSV (self->flags & ix));
1231 1449
1232void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1450void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1233 PPCODE: 1451 PPCODE:
1282 EXTEND (SP, 2); 1500 EXTEND (SP, 2);
1283 PUSHs (sv); 1501 PUSHs (sv);
1284 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1502 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1285} 1503}
1286 1504
1505void incr_parse (CBOR *self, SV *cborstr)
1506 ALIAS:
1507 incr_parse_multiple = 1
1508 PPCODE:
1509{
1510 if (SvUTF8 (cborstr))
1511 sv_utf8_downgrade (cborstr, 0);
1512
1513 if (!self->incr_count)
1514 {
1515 self->incr_count = newAV ();
1516 self->incr_pos = 0;
1517 self->incr_need = 1;
1518
1519 av_push (self->incr_count, newSViv (1));
1520 }
1521
1522 do
1523 {
1524 if (!incr_parse (self, cborstr))
1525 {
1526 if (self->incr_need > self->max_size && self->max_size)
1527 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1528 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1529
1530 break;
1531 }
1532
1533 SV *sv;
1534 char *offset;
1535
1536 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1537 XPUSHs (sv);
1538
1539 sv_chop (cborstr, offset);
1540
1541 av_clear (self->incr_count);
1542 av_push (self->incr_count, newSViv (1));
1543
1544 self->incr_pos = 0;
1545 self->incr_need = self->incr_pos + 1;
1546 }
1547 while (ix);
1548}
1549
1550void incr_reset (CBOR *self)
1551 CODE:
1552{
1553 SvREFCNT_dec (self->incr_count);
1554 self->incr_count = 0;
1555}
1556
1287void DESTROY (CBOR *self) 1557void DESTROY (CBOR *self)
1288 PPCODE: 1558 PPCODE:
1289 cbor_free (self); 1559 cbor_free (self);
1290 1560
1291PROTOTYPES: ENABLE 1561PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines