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.35 by root, Sat Nov 30 17:19:34 2013 UTC vs.
Revision 1.51 by root, Sun Apr 24 13:15:19 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_VALIDATE_UTF8 0x00000040UL
103 107
104#define INIT_SIZE 32 // initial scalar size to be allocated 108#define INIT_SIZE 32 // initial scalar size to be allocated
105 109
106#define SB do { 110#define SB do {
107#define SE } while (0) 111#define SE } while (0)
126typedef struct { 130typedef struct {
127 U32 flags; 131 U32 flags;
128 U32 max_depth; 132 U32 max_depth;
129 STRLEN max_size; 133 STRLEN max_size;
130 SV *filter; 134 SV *filter;
135
136 // for the incremental parser
137 STRLEN incr_pos; // the current offset into the text
138 STRLEN incr_need; // minimum bytes needed to decode
139 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
131} CBOR; 140} CBOR;
132 141
133ecb_inline void 142ecb_inline void
134cbor_init (CBOR *cbor) 143cbor_init (CBOR *cbor)
135{ 144{
139 148
140ecb_inline void 149ecb_inline void
141cbor_free (CBOR *cbor) 150cbor_free (CBOR *cbor)
142{ 151{
143 SvREFCNT_dec (cbor->filter); 152 SvREFCNT_dec (cbor->filter);
153 SvREFCNT_dec (cbor->incr_count);
144} 154}
145 155
146///////////////////////////////////////////////////////////////////////////// 156/////////////////////////////////////////////////////////////////////////////
147// utility functions 157// utility functions
148 158
228{ 238{
229 need (enc, 9); 239 need (enc, 9);
230 240
231 if (ecb_expect_true (len < LENGTH_EXT1)) 241 if (ecb_expect_true (len < LENGTH_EXT1))
232 *enc->cur++ = major | len; 242 *enc->cur++ = major | len;
233 else if (ecb_expect_true (len <= 0xff)) 243 else if (ecb_expect_true (len <= 0xffU))
234 { 244 {
235 *enc->cur++ = major | LENGTH_EXT1; 245 *enc->cur++ = major | LENGTH_EXT1;
236 *enc->cur++ = len; 246 *enc->cur++ = len;
237 } 247 }
238 else if (len <= 0xffff) 248 else if (len <= 0xffffU)
239 { 249 {
240 *enc->cur++ = major | LENGTH_EXT2; 250 *enc->cur++ = major | LENGTH_EXT2;
241 *enc->cur++ = len >> 8; 251 *enc->cur++ = len >> 8;
242 *enc->cur++ = len; 252 *enc->cur++ = len;
243 } 253 }
244 else if (len <= 0xffffffff) 254 else if (len <= 0xffffffffU)
245 { 255 {
246 *enc->cur++ = major | LENGTH_EXT4; 256 *enc->cur++ = major | LENGTH_EXT4;
247 *enc->cur++ = len >> 24; 257 *enc->cur++ = len >> 24;
248 *enc->cur++ = len >> 16; 258 *enc->cur++ = len >> 16;
249 *enc->cur++ = len >> 8; 259 *enc->cur++ = len >> 8;
270} 280}
271 281
272ecb_inline void 282ecb_inline void
273encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 283encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
274{ 284{
285 if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS))
286 if (!utf8)
287 {
288 SV *sv = sv_newmortal ();
289 char *s; STRLEN l;
290
291 sv_setpvn (sv, str, len);
292
293 s = SvPVutf8 (sv, l);
294 encode_str (enc, 1, s, l);
295 return;
296 }
297
275 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
276 need (enc, len); 299 need (enc, len);
277 memcpy (enc->cur, str, len); 300 memcpy (enc->cur, str, len);
278 enc->cur += len; 301 enc->cur += len;
279} 302}
315 338
316 ++enc->depth; 339 ++enc->depth;
317 340
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 341 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 342
343 if (SvMAGICAL (av))
320 for (i = 0; i <= len; ++i) 344 for (i = 0; i <= len; ++i)
321 { 345 {
322 SV **svp = av_fetch (av, i, 0); 346 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 347 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 348 }
349 else
350 for (i = 0; i <= len; ++i)
351 {
352 SV *sv = AvARRAY (av)[i];
353 encode_sv (enc, sv ? sv : &PL_sv_undef);
354 }
325 355
326 --enc->depth; 356 --enc->depth;
327} 357}
328 358
329static void 359static void
433 463
434 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 464 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
435 { 465 {
436 dSP; 466 dSP;
437 467
438 ENTER; SAVETMPS; PUSHMARK (SP); 468 ENTER; SAVETMPS;
469 PUSHMARK (SP);
439 // we re-bless the reference to get overload and other niceties right 470 // we re-bless the reference to get overload and other niceties right
440 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 471 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
441 472
442 PUTBACK; 473 PUTBACK;
443 // G_SCALAR ensures that return value is 1 474 // G_SCALAR ensures that return value is 1
456 } 487 }
457 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 488 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
458 { 489 {
459 dSP; 490 dSP;
460 491
461 ENTER; SAVETMPS; PUSHMARK (SP); 492 ENTER; SAVETMPS;
493 SAVESTACK_POS ();
494 PUSHMARK (SP);
462 EXTEND (SP, 2); 495 EXTEND (SP, 2);
463 // we re-bless the reference to get overload and other niceties right 496 // we re-bless the reference to get overload and other niceties right
464 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 497 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
465 PUSHs (sv_cbor); 498 PUSHs (sv_cbor);
466 499
568} 601}
569 602
570static SV * 603static SV *
571encode_cbor (SV *scalar, CBOR *cbor) 604encode_cbor (SV *scalar, CBOR *cbor)
572{ 605{
573 enc_t enc = { }; 606 enc_t enc = { 0 };
574 607
575 enc.cbor = *cbor; 608 enc.cbor = *cbor;
576 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 609 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
577 enc.cur = SvPVX (enc.sv); 610 enc.cur = SvPVX (enc.sv);
578 enc.end = SvEND (enc.sv); 611 enc.end = SvEND (enc.sv);
627 U8 m = *dec->cur & MINOR_MASK; 660 U8 m = *dec->cur & MINOR_MASK;
628 ++dec->cur; 661 ++dec->cur;
629 662
630 if (ecb_expect_true (m < LENGTH_EXT1)) 663 if (ecb_expect_true (m < LENGTH_EXT1))
631 return m; 664 return m;
632 665 else if (ecb_expect_true (m == LENGTH_EXT1))
633 switch (m)
634 { 666 {
635 case LENGTH_EXT1:
636 WANT (1); 667 WANT (1);
637 dec->cur += 1; 668 dec->cur += 1;
638 return dec->cur[-1]; 669 return dec->cur[-1];
639 670 }
640 case LENGTH_EXT2: 671 else if (ecb_expect_true (m == LENGTH_EXT2))
672 {
641 WANT (2); 673 WANT (2);
642 dec->cur += 2; 674 dec->cur += 2;
643 return (((UV)dec->cur[-2]) << 8) 675 return (((UV)dec->cur[-2]) << 8)
644 | ((UV)dec->cur[-1]); 676 | ((UV)dec->cur[-1]);
645 677 }
646 case LENGTH_EXT4: 678 else if (ecb_expect_true (m == LENGTH_EXT4))
679 {
647 WANT (4); 680 WANT (4);
648 dec->cur += 4; 681 dec->cur += 4;
649 return (((UV)dec->cur[-4]) << 24) 682 return (((UV)dec->cur[-4]) << 24)
650 | (((UV)dec->cur[-3]) << 16) 683 | (((UV)dec->cur[-3]) << 16)
651 | (((UV)dec->cur[-2]) << 8) 684 | (((UV)dec->cur[-2]) << 8)
652 | ((UV)dec->cur[-1]); 685 | ((UV)dec->cur[-1]);
653 686 }
654 case LENGTH_EXT8: 687 else if (ecb_expect_true (m == LENGTH_EXT8))
688 {
655 WANT (8); 689 WANT (8);
656 dec->cur += 8; 690 dec->cur += 8;
657 691
658 return 692 return
659#if UVSIZE < 8 693#if UVSIZE < 8
660 0 694 0
661#else 695#else
662 (((UV)dec->cur[-8]) << 56) 696 (((UV)dec->cur[-8]) << 56)
663 | (((UV)dec->cur[-7]) << 48) 697 | (((UV)dec->cur[-7]) << 48)
664 | (((UV)dec->cur[-6]) << 40) 698 | (((UV)dec->cur[-6]) << 40)
665 | (((UV)dec->cur[-5]) << 32) 699 | (((UV)dec->cur[-5]) << 32)
666#endif 700#endif
667 | (((UV)dec->cur[-4]) << 24) 701 | (((UV)dec->cur[-4]) << 24)
668 | (((UV)dec->cur[-3]) << 16) 702 | (((UV)dec->cur[-3]) << 16)
669 | (((UV)dec->cur[-2]) << 8) 703 | (((UV)dec->cur[-2]) << 8)
670 | ((UV)dec->cur[-1]); 704 | ((UV)dec->cur[-1]);
671 705 }
672 default: 706 else
673 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 707 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
674 }
675 708
676fail: 709fail:
677 return 0; 710 return 0;
678} 711}
679 712
705 } 738 }
706 else 739 else
707 { 740 {
708 int i, len = decode_uint (dec); 741 int i, len = decode_uint (dec);
709 742
743 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
710 av_fill (av, len - 1); 744 av_fill (av, len - 1);
711 745
712 for (i = 0; i < len; ++i) 746 for (i = 0; i < len; ++i)
713 AvARRAY (av)[i] = decode_sv (dec); 747 AvARRAY (av)[i] = decode_sv (dec);
714 } 748 }
727{ 761{
728 // for speed reasons, we specialcase single-string 762 // for speed reasons, we specialcase single-string
729 // byte or utf-8 strings as keys, but only when !stringref 763 // byte or utf-8 strings as keys, but only when !stringref
730 764
731 if (ecb_expect_true (!dec->stringref)) 765 if (ecb_expect_true (!dec->stringref))
732 if ((*dec->cur - MAJOR_BYTES) <= 27) 766 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
733 { 767 {
734 I32 len = decode_uint (dec); 768 I32 len = decode_uint (dec);
735 char *key = (char *)dec->cur; 769 char *key = (char *)dec->cur;
736 770
771 WANT (len);
737 dec->cur += len; 772 dec->cur += len;
738 773
739 if (ecb_expect_false (dec->stringref))
740 av_push (dec->stringref, newSVpvn (key, len));
741
742 hv_store (hv, key, len, decode_sv (dec), 0); 774 hv_store (hv, key, len, decode_sv (dec), 0);
743 775
744 return; 776 return;
745 } 777 }
746 else if ((*dec->cur - MAJOR_TEXT) <= 27) 778 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 779 {
748 I32 len = decode_uint (dec); 780 I32 len = decode_uint (dec);
749 char *key = (char *)dec->cur; 781 char *key = (char *)dec->cur;
750 782
783 WANT (len);
751 dec->cur += len; 784 dec->cur += len;
752 785
753 if (ecb_expect_false (dec->stringref)) 786 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 787 if (!is_utf8_string (key, len))
788 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
755 789
756 hv_store (hv, key, -len, decode_sv (dec), 0); 790 hv_store (hv, key, -len, decode_sv (dec), 0);
757 791
758 return; 792 return;
759 } 793 }
761 SV *k = decode_sv (dec); 795 SV *k = decode_sv (dec);
762 SV *v = decode_sv (dec); 796 SV *v = decode_sv (dec);
763 797
764 hv_store_ent (hv, k, v, 0); 798 hv_store_ent (hv, k, v, 0);
765 SvREFCNT_dec (k); 799 SvREFCNT_dec (k);
800
801fail:
802 ;
766} 803}
767 804
768static SV * 805static SV *
769decode_hv (dec_t *dec) 806decode_hv (dec_t *dec)
770{ 807{
852 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 889 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
853 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 890 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
854 } 891 }
855 892
856 if (utf8) 893 if (utf8)
894 {
895 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
896 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
897 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
898
857 SvUTF8_on (sv); 899 SvUTF8_on (sv);
900 }
858 901
859 return sv; 902 return sv;
860 903
861fail: 904fail:
862 SvREFCNT_dec (sv); 905 SvREFCNT_dec (sv);
911 case CBOR_TAG_VALUE_SHAREABLE: 954 case CBOR_TAG_VALUE_SHAREABLE:
912 { 955 {
913 if (ecb_expect_false (!dec->shareable)) 956 if (ecb_expect_false (!dec->shareable))
914 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 957 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
915 958
959 if (dec->cbor.flags & F_ALLOW_CYCLES)
960 {
916 sv = newSV (0); 961 sv = newSV (0);
917 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 962 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
918 963
919 SV *osv = decode_sv (dec); 964 SV *osv = decode_sv (dec);
920 sv_setsv (sv, osv); 965 sv_setsv (sv, osv);
921 SvREFCNT_dec_NN (osv); 966 SvREFCNT_dec_NN (osv);
967 }
968 else
969 {
970 av_push (dec->shareable, &PL_sv_undef);
971 int idx = AvFILLp (dec->shareable);
972 sv = decode_sv (dec);
973 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
974 }
922 } 975 }
923 break; 976 break;
924 977
925 case CBOR_TAG_VALUE_SHAREDREF: 978 case CBOR_TAG_VALUE_SHAREDREF:
926 { 979 {
931 984
932 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 985 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
933 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 986 ERR ("corrupted CBOR data (sharedref index out of bounds)");
934 987
935 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 988 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
989
990 if (sv == &PL_sv_undef)
991 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
936 } 992 }
937 break; 993 break;
938 994
939 case CBOR_TAG_PERL_OBJECT: 995 case CBOR_TAG_PERL_OBJECT:
940 { 996 {
955 if (!method) 1011 if (!method)
956 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1012 ERR ("cannot decode perl-object (package does not have a THAW method)");
957 1013
958 dSP; 1014 dSP;
959 1015
960 ENTER; SAVETMPS; PUSHMARK (SP); 1016 ENTER; SAVETMPS;
1017 PUSHMARK (SP);
961 EXTEND (SP, len + 1); 1018 EXTEND (SP, len + 1);
962 // we re-bless the reference to get overload and other niceties right 1019 // we re-bless the reference to get overload and other niceties right
963 PUSHs (*av_fetch (av, 0, 1)); 1020 PUSHs (*av_fetch (av, 0, 1));
964 PUSHs (sv_cbor); 1021 PUSHs (sv_cbor);
965 1022
990 default: 1047 default:
991 { 1048 {
992 sv = decode_sv (dec); 1049 sv = decode_sv (dec);
993 1050
994 dSP; 1051 dSP;
995 ENTER; SAVETMPS; PUSHMARK (SP); 1052 ENTER; SAVETMPS;
1053 SAVESTACK_POS ();
1054 PUSHMARK (SP);
996 EXTEND (SP, 2); 1055 EXTEND (SP, 2);
997 PUSHs (newSVuv (tag)); 1056 PUSHs (newSVuv (tag));
998 PUSHs (sv); 1057 PUSHs (sv);
999 1058
1000 PUTBACK; 1059 PUTBACK;
1109 1168
1110 return newSVnv (ecb_binary64_to_double (fp)); 1169 return newSVnv (ecb_binary64_to_double (fp));
1111 } 1170 }
1112 1171
1113 // 0..19 unassigned simple 1172 // 0..19 unassigned simple
1114 // 24 reserved + unassigned (reserved values are not encodable) 1173 // 24 reserved + unassigned simple (reserved values are not encodable)
1174 // 28-30 unassigned misc
1175 // 31 break code
1115 default: 1176 default:
1116 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1177 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1117 } 1178 }
1118 1179
1119 break; 1180 break;
1120 } 1181 }
1121 1182
1124} 1185}
1125 1186
1126static SV * 1187static SV *
1127decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1188decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1128{ 1189{
1129 dec_t dec = { }; 1190 dec_t dec = { 0 };
1130 SV *sv; 1191 SV *sv;
1131 STRLEN len; 1192 STRLEN len;
1132 char *data = SvPVbyte (string, len); 1193 char *data = SvPVbyte (string, len);
1133 1194
1134 if (len > cbor->max_size && cbor->max_size) 1195 if (len > cbor->max_size && cbor->max_size)
1148 if (dec.cur != dec.end && !dec.err) 1209 if (dec.cur != dec.end && !dec.err)
1149 dec.err = "garbage after CBOR object"; 1210 dec.err = "garbage after CBOR object";
1150 1211
1151 if (dec.err) 1212 if (dec.err)
1152 { 1213 {
1214 if (dec.shareable)
1215 {
1216 // need to break cyclic links, which whould all be in shareable
1217 int i;
1218 SV **svp;
1219
1220 for (i = av_len (dec.shareable) + 1; i--; )
1221 if ((svp = av_fetch (dec.shareable, i, 0)))
1222 sv_setsv (*svp, &PL_sv_undef);
1223 }
1224
1153 SvREFCNT_dec (sv); 1225 SvREFCNT_dec (sv);
1154 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1226 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1155 } 1227 }
1156 1228
1157 sv = sv_2mortal (sv); 1229 sv = sv_2mortal (sv);
1158 1230
1159 return sv; 1231 return sv;
1160} 1232}
1161 1233
1234/////////////////////////////////////////////////////////////////////////////
1235// incremental parser
1236
1237#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1238
1239// returns 0 for notyet, 1 for success or error
1240static int
1241incr_parse (CBOR *self, SV *cborstr)
1242{
1243 STRLEN cur;
1244 SvPV (cborstr, cur);
1245
1246 while (ecb_expect_true (self->incr_need <= cur))
1247 {
1248 // table of integer count bytes
1249 static I8 incr_len[MINOR_MASK + 1] = {
1250 0, 0, 0, 0, 0, 0, 0, 0,
1251 0, 0, 0, 0, 0, 0, 0, 0,
1252 0, 0, 0, 0, 0, 0, 0, 0,
1253 1, 2, 4, 8,-1,-1,-1,-2
1254 };
1255
1256 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1257 U8 m = *p & MINOR_MASK;
1258 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1259 I8 ilen = incr_len[m];
1260
1261 self->incr_need = self->incr_pos + 1;
1262
1263 if (ecb_expect_false (ilen < 0))
1264 {
1265 if (m != MINOR_INDEF)
1266 return 1; // error
1267
1268 if (*p == (MAJOR_MISC | MINOR_INDEF))
1269 {
1270 if (count >= 0)
1271 return 1; // error
1272
1273 count = 1;
1274 }
1275 else
1276 {
1277 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1278 count = -1;
1279 }
1280 }
1281 else
1282 {
1283 self->incr_need += ilen;
1284 if (ecb_expect_false (self->incr_need > cur))
1285 return 0;
1286
1287 int major = *p >> MAJOR_SHIFT;
1288
1289 switch (major)
1290 {
1291 case MAJOR_TAG >> MAJOR_SHIFT:
1292 ++count; // tags merely prefix another value
1293 break;
1294
1295 case MAJOR_BYTES >> MAJOR_SHIFT:
1296 case MAJOR_TEXT >> MAJOR_SHIFT:
1297 case MAJOR_ARRAY >> MAJOR_SHIFT:
1298 case MAJOR_MAP >> MAJOR_SHIFT:
1299 {
1300 UV len;
1301
1302 if (ecb_expect_false (ilen))
1303 {
1304 len = 0;
1305
1306 do {
1307 len = (len << 8) | *++p;
1308 } while (--ilen);
1309 }
1310 else
1311 len = m;
1312
1313 switch (major)
1314 {
1315 case MAJOR_BYTES >> MAJOR_SHIFT:
1316 case MAJOR_TEXT >> MAJOR_SHIFT:
1317 self->incr_need += len;
1318 if (ecb_expect_false (self->incr_need > cur))
1319 return 0;
1320
1321 break;
1322
1323 case MAJOR_MAP >> MAJOR_SHIFT:
1324 len <<= 1;
1325 case MAJOR_ARRAY >> MAJOR_SHIFT:
1326 if (len)
1327 {
1328 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1329 count = len + 1;
1330 }
1331 break;
1332 }
1333 }
1334 }
1335 }
1336
1337 self->incr_pos = self->incr_need;
1338
1339 if (count > 0)
1340 {
1341 while (!--count)
1342 {
1343 if (!AvFILLp (self->incr_count))
1344 return 1; // done
1345
1346 SvREFCNT_dec_NN (av_pop (self->incr_count));
1347 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1348 }
1349
1350 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1351 }
1352 }
1353
1354 return 0;
1355}
1356
1357
1162///////////////////////////////////////////////////////////////////////////// 1358/////////////////////////////////////////////////////////////////////////////
1163// XS interface functions 1359// XS interface functions
1164 1360
1165MODULE = CBOR::XS PACKAGE = CBOR::XS 1361MODULE = CBOR::XS PACKAGE = CBOR::XS
1166 1362
1206void shrink (CBOR *self, int enable = 1) 1402void shrink (CBOR *self, int enable = 1)
1207 ALIAS: 1403 ALIAS:
1208 shrink = F_SHRINK 1404 shrink = F_SHRINK
1209 allow_unknown = F_ALLOW_UNKNOWN 1405 allow_unknown = F_ALLOW_UNKNOWN
1210 allow_sharing = F_ALLOW_SHARING 1406 allow_sharing = F_ALLOW_SHARING
1407 allow_cycles = F_ALLOW_CYCLES
1211 pack_strings = F_PACK_STRINGS 1408 pack_strings = F_PACK_STRINGS
1409 utf8_strings = F_UTF8_STRINGS
1410 validate_utf8 = F_VALIDATE_UTF8
1212 PPCODE: 1411 PPCODE:
1213{ 1412{
1214 if (enable) 1413 if (enable)
1215 self->flags |= ix; 1414 self->flags |= ix;
1216 else 1415 else
1222void get_shrink (CBOR *self) 1421void get_shrink (CBOR *self)
1223 ALIAS: 1422 ALIAS:
1224 get_shrink = F_SHRINK 1423 get_shrink = F_SHRINK
1225 get_allow_unknown = F_ALLOW_UNKNOWN 1424 get_allow_unknown = F_ALLOW_UNKNOWN
1226 get_allow_sharing = F_ALLOW_SHARING 1425 get_allow_sharing = F_ALLOW_SHARING
1426 get_allow_cycles = F_ALLOW_CYCLES
1227 get_pack_strings = F_PACK_STRINGS 1427 get_pack_strings = F_PACK_STRINGS
1428 get_validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1429 PPCODE:
1229 XPUSHs (boolSV (self->flags & ix)); 1430 XPUSHs (boolSV (self->flags & ix));
1230 1431
1231void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1432void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1232 PPCODE: 1433 PPCODE:
1281 EXTEND (SP, 2); 1482 EXTEND (SP, 2);
1282 PUSHs (sv); 1483 PUSHs (sv);
1283 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1484 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1284} 1485}
1285 1486
1487void incr_parse (CBOR *self, SV *cborstr)
1488 ALIAS:
1489 incr_parse_multiple = 1
1490 PPCODE:
1491{
1492 if (SvUTF8 (cborstr))
1493 sv_utf8_downgrade (cborstr, 0);
1494
1495 if (!self->incr_count)
1496 {
1497 self->incr_count = newAV ();
1498 self->incr_pos = 0;
1499 self->incr_need = 1;
1500
1501 av_push (self->incr_count, newSViv (1));
1502 }
1503
1504 do
1505 {
1506 if (!incr_parse (self, cborstr))
1507 {
1508 if (self->incr_need > self->max_size && self->max_size)
1509 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1510 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1511
1512 break;
1513 }
1514
1515 SV *sv;
1516 char *offset;
1517
1518 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1519 XPUSHs (sv);
1520
1521 sv_chop (cborstr, offset);
1522
1523 av_clear (self->incr_count);
1524 av_push (self->incr_count, newSViv (1));
1525
1526 self->incr_pos = 0;
1527 self->incr_need = self->incr_pos + 1;
1528 }
1529 while (ix);
1530}
1531
1532void incr_reset (CBOR *self)
1533 CODE:
1534{
1535 SvREFCNT_dec (self->incr_count);
1536 self->incr_count = 0;
1537}
1538
1286void DESTROY (CBOR *self) 1539void DESTROY (CBOR *self)
1287 PPCODE: 1540 PPCODE:
1288 cbor_free (self); 1541 cbor_free (self);
1289 1542
1290PROTOTYPES: ENABLE 1543PROTOTYPES: ENABLE
1291 1544
1292void encode_cbor (SV *scalar) 1545void encode_cbor (SV *scalar)
1546 ALIAS:
1547 encode_cbor = 0
1548 encode_cbor_sharing = F_ALLOW_SHARING
1293 PPCODE: 1549 PPCODE:
1294{ 1550{
1295 CBOR cbor; 1551 CBOR cbor;
1296 cbor_init (&cbor); 1552 cbor_init (&cbor);
1553 cbor.flags |= ix;
1297 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1554 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1298 XPUSHs (scalar); 1555 XPUSHs (scalar);
1299} 1556}
1300 1557
1301void decode_cbor (SV *cborstr) 1558void decode_cbor (SV *cborstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines