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.58 by root, Fri Nov 25 13:27:29 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))
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_TEXT_KEYS 0x00000020UL
106#define F_TEXT_STRINGS 0x00000040UL
104#define F_VALIDATE_UTF8 0x00000020UL 107#define F_VALIDATE_UTF8 0x00000080UL
105 108
106#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
107 110
108#define SB do { 111#define SB do {
109#define SE } while (0) 112#define SE } while (0)
128typedef struct { 131typedef struct {
129 U32 flags; 132 U32 flags;
130 U32 max_depth; 133 U32 max_depth;
131 STRLEN max_size; 134 STRLEN max_size;
132 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.
133} CBOR; 141} CBOR;
134 142
135ecb_inline void 143ecb_inline void
136cbor_init (CBOR *cbor) 144cbor_init (CBOR *cbor)
137{ 145{
141 149
142ecb_inline void 150ecb_inline void
143cbor_free (CBOR *cbor) 151cbor_free (CBOR *cbor)
144{ 152{
145 SvREFCNT_dec (cbor->filter); 153 SvREFCNT_dec (cbor->filter);
154 SvREFCNT_dec (cbor->incr_count);
146} 155}
147 156
148///////////////////////////////////////////////////////////////////////////// 157/////////////////////////////////////////////////////////////////////////////
149// utility functions 158// utility functions
150 159
269encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
270{ 279{
271 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
272} 281}
273 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
274ecb_inline void 306ecb_inline void
275encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
276{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
277 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
278 need (enc, len); 317 need (enc, len);
279 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
280 enc->cur += len; 319 enc->cur += len;
281} 320}
282 321
283static void 322ecb_inline void
284encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
285{ 324{
286 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
287 { 326 {
288 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
289 328
300 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
301 ++enc->stringref_idx; 340 ++enc->stringref_idx;
302 } 341 }
303 } 342 }
304 343
305 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
306} 345}
307 346
308static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
309 348
310static void 349static void
317 356
318 ++enc->depth; 357 ++enc->depth;
319 358
320 encode_uint (enc, MAJOR_ARRAY, len + 1); 359 encode_uint (enc, MAJOR_ARRAY, len + 1);
321 360
361 if (SvMAGICAL (av))
322 for (i = 0; i <= len; ++i) 362 for (i = 0; i <= len; ++i)
323 { 363 {
324 SV **svp = av_fetch (av, i, 0); 364 SV **svp = av_fetch (av, i, 0);
325 encode_sv (enc, svp ? *svp : &PL_sv_undef); 365 encode_sv (enc, svp ? *svp : &PL_sv_undef);
326 } 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 }
327 373
328 --enc->depth; 374 --enc->depth;
329} 375}
330 376
331static void 377static void
349 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
350 { 396 {
351 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
352 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
353 else 399 else
354 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 400 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
355 401
356 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));
357 } 403 }
358 404
359 if (mg) 405 if (mg)
435 481
436 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 482 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
437 { 483 {
438 dSP; 484 dSP;
439 485
440 ENTER; SAVETMPS; PUSHMARK (SP); 486 ENTER; SAVETMPS;
487 PUSHMARK (SP);
441 // 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
442 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 489 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
443 490
444 PUTBACK; 491 PUTBACK;
445 // G_SCALAR ensures that return value is 1 492 // G_SCALAR ensures that return value is 1
458 } 505 }
459 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 506 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
460 { 507 {
461 dSP; 508 dSP;
462 509
463 ENTER; SAVETMPS; PUSHMARK (SP); 510 ENTER; SAVETMPS;
511 SAVESTACK_POS ();
512 PUSHMARK (SP);
464 EXTEND (SP, 2); 513 EXTEND (SP, 2);
465 // 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
466 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 515 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
467 PUSHs (sv_cbor); 516 PUSHs (sv_cbor);
468 517
474 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
475 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));
476 525
477 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
478 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
479 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
480 529
481 while (count) 530 while (count)
482 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
483 532
484 PUTBACK; 533 PUTBACK;
543 592
544 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
545 { 594 {
546 STRLEN len; 595 STRLEN len;
547 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
548 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
549 } 598 }
550 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
551 encode_nv (enc, sv); 600 encode_nv (enc, sv);
552 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
553 { 602 {
570} 619}
571 620
572static SV * 621static SV *
573encode_cbor (SV *scalar, CBOR *cbor) 622encode_cbor (SV *scalar, CBOR *cbor)
574{ 623{
575 enc_t enc = { }; 624 enc_t enc = { 0 };
576 625
577 enc.cbor = *cbor; 626 enc.cbor = *cbor;
578 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
579 enc.cur = SvPVX (enc.sv); 628 enc.cur = SvPVX (enc.sv);
580 enc.end = SvEND (enc.sv); 629 enc.end = SvEND (enc.sv);
616 SV *decode_tagged; 665 SV *decode_tagged;
617} dec_t; 666} dec_t;
618 667
619#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 668#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
620 669
621#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 670#define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data")
622 671
623#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 672#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
624#define DEC_DEC_DEPTH --dec->depth 673#define DEC_DEC_DEPTH --dec->depth
625 674
626static UV 675static UV
627decode_uint (dec_t *dec) 676decode_uint (dec_t *dec)
628{ 677{
705 av_push (av, decode_sv (dec)); 754 av_push (av, decode_sv (dec));
706 } 755 }
707 } 756 }
708 else 757 else
709 { 758 {
710 int i, len = decode_uint (dec); 759 UV i, len = decode_uint (dec);
711 760
712 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays 761 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
713 av_fill (av, len - 1); 762 av_fill (av, len - 1);
714 763
715 for (i = 0; i < len; ++i) 764 for (i = 0; i < len; ++i)
730{ 779{
731 // for speed reasons, we specialcase single-string 780 // for speed reasons, we specialcase single-string
732 // byte or utf-8 strings as keys, but only when !stringref 781 // byte or utf-8 strings as keys, but only when !stringref
733 782
734 if (ecb_expect_true (!dec->stringref)) 783 if (ecb_expect_true (!dec->stringref))
735 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) 784 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
736 { 785 {
737 I32 len = decode_uint (dec); 786 STRLEN len = decode_uint (dec);
738 char *key = (char *)dec->cur; 787 char *key = (char *)dec->cur;
739 788
789 WANT (len);
740 dec->cur += len; 790 dec->cur += len;
741 791
742 hv_store (hv, key, len, decode_sv (dec), 0); 792 hv_store (hv, key, len, decode_sv (dec), 0);
743 793
744 return; 794 return;
745 } 795 }
746 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) 796 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 797 {
748 I32 len = decode_uint (dec); 798 STRLEN len = decode_uint (dec);
749 char *key = (char *)dec->cur; 799 char *key = (char *)dec->cur;
750 800
801 WANT (len);
751 dec->cur += len; 802 dec->cur += len;
752 803
753 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) 804 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 if (!is_utf8_string (key, len)) 805 if (!is_utf8_string (key, len))
755 ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); 806 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
793 decode_he (dec, hv); 844 decode_he (dec, hv);
794 } 845 }
795 } 846 }
796 else 847 else
797 { 848 {
798 int pairs = decode_uint (dec); 849 UV pairs = decode_uint (dec);
850
851 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
799 852
800 while (pairs--) 853 while (pairs--)
801 decode_he (dec, hv); 854 decode_he (dec, hv);
802 } 855 }
803 856
891 sv = newRV_noinc (decode_sv (dec)); 944 sv = newRV_noinc (decode_sv (dec));
892 break; 945 break;
893 946
894 case CBOR_TAG_STRINGREF_NAMESPACE: 947 case CBOR_TAG_STRINGREF_NAMESPACE:
895 { 948 {
949 // do nmot use SAVETMPS/FREETMPS, as these will
950 // erase mortalised caches, e.g. "shareable"
896 ENTER; SAVETMPS; 951 ENTER;
897 952
898 SAVESPTR (dec->stringref); 953 SAVESPTR (dec->stringref);
899 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 954 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
900 955
901 sv = decode_sv (dec); 956 sv = decode_sv (dec);
902 957
903 FREETMPS; LEAVE; 958 LEAVE;
904 } 959 }
905 break; 960 break;
906 961
907 case CBOR_TAG_STRINGREF: 962 case CBOR_TAG_STRINGREF:
908 { 963 {
978 if (!method) 1033 if (!method)
979 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1034 ERR ("cannot decode perl-object (package does not have a THAW method)");
980 1035
981 dSP; 1036 dSP;
982 1037
983 ENTER; SAVETMPS; PUSHMARK (SP); 1038 ENTER; SAVETMPS;
1039 PUSHMARK (SP);
984 EXTEND (SP, len + 1); 1040 EXTEND (SP, len + 1);
985 // we re-bless the reference to get overload and other niceties right 1041 // we re-bless the reference to get overload and other niceties right
986 PUSHs (*av_fetch (av, 0, 1)); 1042 PUSHs (*av_fetch (av, 0, 1));
987 PUSHs (sv_cbor); 1043 PUSHs (sv_cbor);
988 1044
1010 } 1066 }
1011 break; 1067 break;
1012 1068
1013 default: 1069 default:
1014 { 1070 {
1071 SV *tag_sv = newSVuv (tag);
1072
1015 sv = decode_sv (dec); 1073 sv = decode_sv (dec);
1016 1074
1017 dSP; 1075 dSP;
1018 ENTER; SAVETMPS; PUSHMARK (SP); 1076 ENTER; SAVETMPS;
1077 SAVESTACK_POS ();
1078 PUSHMARK (SP);
1019 EXTEND (SP, 2); 1079 EXTEND (SP, 2);
1020 PUSHs (newSVuv (tag)); 1080 PUSHs (tag_sv);
1021 PUSHs (sv); 1081 PUSHs (sv);
1022 1082
1023 PUTBACK; 1083 PUTBACK;
1024 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1084 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1025 SPAGAIN; 1085 SPAGAIN;
1026 1086
1027 if (SvTRUE (ERRSV)) 1087 if (SvTRUE (ERRSV))
1028 { 1088 {
1089 SvREFCNT_dec (tag_sv);
1029 FREETMPS; LEAVE; 1090 FREETMPS; LEAVE;
1030 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1091 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV))));
1031 } 1092 }
1032 1093
1033 if (count) 1094 if (count)
1034 { 1095 {
1096 SvREFCNT_dec (tag_sv);
1035 SvREFCNT_dec (sv); 1097 SvREFCNT_dec (sv);
1036 sv = SvREFCNT_inc (POPs); 1098 sv = SvREFCNT_inc (POPs);
1037 } 1099 }
1038 else 1100 else
1039 { 1101 {
1040 AV *av = newAV (); 1102 AV *av = newAV ();
1041 av_push (av, newSVuv (tag)); 1103 av_push (av, tag_sv);
1042 av_push (av, sv); 1104 av_push (av, sv);
1043 1105
1044 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1106 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1045 ? cbor_tagged_stash 1107 ? cbor_tagged_stash
1046 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1108 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1132 1194
1133 return newSVnv (ecb_binary64_to_double (fp)); 1195 return newSVnv (ecb_binary64_to_double (fp));
1134 } 1196 }
1135 1197
1136 // 0..19 unassigned simple 1198 // 0..19 unassigned simple
1137 // 24 reserved + unassigned (reserved values are not encodable) 1199 // 24 reserved + unassigned simple (reserved values are not encodable)
1200 // 28-30 unassigned misc
1201 // 31 break code
1138 default: 1202 default:
1139 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1203 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1140 } 1204 }
1141 1205
1142 break; 1206 break;
1143 } 1207 }
1144 1208
1147} 1211}
1148 1212
1149static SV * 1213static SV *
1150decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1214decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1151{ 1215{
1152 dec_t dec = { }; 1216 dec_t dec = { 0 };
1153 SV *sv; 1217 SV *sv;
1154 STRLEN len; 1218 STRLEN len;
1155 char *data = SvPVbyte (string, len); 1219 char *data = SvPVbyte (string, len);
1156 1220
1157 if (len > cbor->max_size && cbor->max_size) 1221 if (len > cbor->max_size && cbor->max_size)
1173 1237
1174 if (dec.err) 1238 if (dec.err)
1175 { 1239 {
1176 if (dec.shareable) 1240 if (dec.shareable)
1177 { 1241 {
1178 // need to break cyclic links, which whould all be in shareable 1242 // need to break cyclic links, which would all be in shareable
1179 int i; 1243 int i;
1180 SV **svp; 1244 SV **svp;
1181 1245
1182 for (i = av_len (dec.shareable) + 1; i--; ) 1246 for (i = av_len (dec.shareable) + 1; i--; )
1183 if ((svp = av_fetch (dec.shareable, i, 0))) 1247 if ((svp = av_fetch (dec.shareable, i, 0)))
1192 1256
1193 return sv; 1257 return sv;
1194} 1258}
1195 1259
1196///////////////////////////////////////////////////////////////////////////// 1260/////////////////////////////////////////////////////////////////////////////
1261// incremental parser
1262
1263#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1264
1265// returns 0 for notyet, 1 for success or error
1266static int
1267incr_parse (CBOR *self, SV *cborstr)
1268{
1269 STRLEN cur;
1270 SvPV (cborstr, cur);
1271
1272 while (ecb_expect_true (self->incr_need <= cur))
1273 {
1274 // table of integer count bytes
1275 static I8 incr_len[MINOR_MASK + 1] = {
1276 0, 0, 0, 0, 0, 0, 0, 0,
1277 0, 0, 0, 0, 0, 0, 0, 0,
1278 0, 0, 0, 0, 0, 0, 0, 0,
1279 1, 2, 4, 8,-1,-1,-1,-2
1280 };
1281
1282 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1283 U8 m = *p & MINOR_MASK;
1284 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1285 I8 ilen = incr_len[m];
1286
1287 self->incr_need = self->incr_pos + 1;
1288
1289 if (ecb_expect_false (ilen < 0))
1290 {
1291 if (m != MINOR_INDEF)
1292 return 1; // error
1293
1294 if (*p == (MAJOR_MISC | MINOR_INDEF))
1295 {
1296 if (count >= 0)
1297 return 1; // error
1298
1299 count = 1;
1300 }
1301 else
1302 {
1303 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1304 count = -1;
1305 }
1306 }
1307 else
1308 {
1309 self->incr_need += ilen;
1310 if (ecb_expect_false (self->incr_need > cur))
1311 return 0;
1312
1313 int major = *p >> MAJOR_SHIFT;
1314
1315 switch (major)
1316 {
1317 case MAJOR_TAG >> MAJOR_SHIFT:
1318 ++count; // tags merely prefix another value
1319 break;
1320
1321 case MAJOR_BYTES >> MAJOR_SHIFT:
1322 case MAJOR_TEXT >> MAJOR_SHIFT:
1323 case MAJOR_ARRAY >> MAJOR_SHIFT:
1324 case MAJOR_MAP >> MAJOR_SHIFT:
1325 {
1326 UV len;
1327
1328 if (ecb_expect_false (ilen))
1329 {
1330 len = 0;
1331
1332 do {
1333 len = (len << 8) | *++p;
1334 } while (--ilen);
1335 }
1336 else
1337 len = m;
1338
1339 switch (major)
1340 {
1341 case MAJOR_BYTES >> MAJOR_SHIFT:
1342 case MAJOR_TEXT >> MAJOR_SHIFT:
1343 self->incr_need += len;
1344 if (ecb_expect_false (self->incr_need > cur))
1345 return 0;
1346
1347 break;
1348
1349 case MAJOR_MAP >> MAJOR_SHIFT:
1350 len <<= 1;
1351 case MAJOR_ARRAY >> MAJOR_SHIFT:
1352 if (len)
1353 {
1354 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1355 count = len + 1;
1356 }
1357 break;
1358 }
1359 }
1360 }
1361 }
1362
1363 self->incr_pos = self->incr_need;
1364
1365 if (count > 0)
1366 {
1367 while (!--count)
1368 {
1369 if (!AvFILLp (self->incr_count))
1370 return 1; // done
1371
1372 SvREFCNT_dec_NN (av_pop (self->incr_count));
1373 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1374 }
1375
1376 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1377 }
1378 }
1379
1380 return 0;
1381}
1382
1383
1384/////////////////////////////////////////////////////////////////////////////
1197// XS interface functions 1385// XS interface functions
1198 1386
1199MODULE = CBOR::XS PACKAGE = CBOR::XS 1387MODULE = CBOR::XS PACKAGE = CBOR::XS
1200 1388
1201BOOT: 1389BOOT:
1212 1400
1213 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1401 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1214 1402
1215 sv_cbor = newSVpv ("CBOR", 0); 1403 sv_cbor = newSVpv ("CBOR", 0);
1216 SvREADONLY_on (sv_cbor); 1404 SvREADONLY_on (sv_cbor);
1405
1406 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1217} 1407}
1218 1408
1219PROTOTYPES: DISABLE 1409PROTOTYPES: DISABLE
1220 1410
1221void CLONE (...) 1411void CLONE (...)
1242 shrink = F_SHRINK 1432 shrink = F_SHRINK
1243 allow_unknown = F_ALLOW_UNKNOWN 1433 allow_unknown = F_ALLOW_UNKNOWN
1244 allow_sharing = F_ALLOW_SHARING 1434 allow_sharing = F_ALLOW_SHARING
1245 allow_cycles = F_ALLOW_CYCLES 1435 allow_cycles = F_ALLOW_CYCLES
1246 pack_strings = F_PACK_STRINGS 1436 pack_strings = F_PACK_STRINGS
1437 text_keys = F_TEXT_KEYS
1438 text_strings = F_TEXT_STRINGS
1247 validate_utf8 = F_VALIDATE_UTF8 1439 validate_utf8 = F_VALIDATE_UTF8
1248 PPCODE: 1440 PPCODE:
1249{ 1441{
1250 if (enable) 1442 if (enable)
1251 self->flags |= ix; 1443 self->flags |= ix;
1260 get_shrink = F_SHRINK 1452 get_shrink = F_SHRINK
1261 get_allow_unknown = F_ALLOW_UNKNOWN 1453 get_allow_unknown = F_ALLOW_UNKNOWN
1262 get_allow_sharing = F_ALLOW_SHARING 1454 get_allow_sharing = F_ALLOW_SHARING
1263 get_allow_cycles = F_ALLOW_CYCLES 1455 get_allow_cycles = F_ALLOW_CYCLES
1264 get_pack_strings = F_PACK_STRINGS 1456 get_pack_strings = F_PACK_STRINGS
1457 get_text_keys = F_TEXT_KEYS
1458 get_text_strings = F_TEXT_STRINGS
1265 get_validate_utf8 = F_VALIDATE_UTF8 1459 get_validate_utf8 = F_VALIDATE_UTF8
1266 PPCODE: 1460 PPCODE:
1267 XPUSHs (boolSV (self->flags & ix)); 1461 XPUSHs (boolSV (self->flags & ix));
1268 1462
1269void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1463void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1319 EXTEND (SP, 2); 1513 EXTEND (SP, 2);
1320 PUSHs (sv); 1514 PUSHs (sv);
1321 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1515 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1322} 1516}
1323 1517
1518void incr_parse (CBOR *self, SV *cborstr)
1519 ALIAS:
1520 incr_parse_multiple = 1
1521 PPCODE:
1522{
1523 if (SvUTF8 (cborstr))
1524 sv_utf8_downgrade (cborstr, 0);
1525
1526 if (!self->incr_count)
1527 {
1528 self->incr_count = newAV ();
1529 self->incr_pos = 0;
1530 self->incr_need = 1;
1531
1532 av_push (self->incr_count, newSViv (1));
1533 }
1534
1535 do
1536 {
1537 if (!incr_parse (self, cborstr))
1538 {
1539 if (self->incr_need > self->max_size && self->max_size)
1540 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1541 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1542
1543 break;
1544 }
1545
1546 SV *sv;
1547 char *offset;
1548
1549 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1550 XPUSHs (sv);
1551
1552 sv_chop (cborstr, offset);
1553
1554 av_clear (self->incr_count);
1555 av_push (self->incr_count, newSViv (1));
1556
1557 self->incr_pos = 0;
1558 self->incr_need = self->incr_pos + 1;
1559 }
1560 while (ix);
1561}
1562
1563void incr_reset (CBOR *self)
1564 CODE:
1565{
1566 SvREFCNT_dec (self->incr_count);
1567 self->incr_count = 0;
1568}
1569
1324void DESTROY (CBOR *self) 1570void DESTROY (CBOR *self)
1325 PPCODE: 1571 PPCODE:
1326 cbor_free (self); 1572 cbor_free (self);
1327 1573
1328PROTOTYPES: ENABLE 1574PROTOTYPES: ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines