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.26 by root, Fri Nov 22 09:40:13 2013 UTC vs.
Revision 1.61 by root, Sat Nov 26 02:10:19 2016 UTC

6#include <string.h> 6#include <string.h>
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#include <inttypes.h>
11 12
13#define ECB_NO_THREADS 1
12#include "ecb.h" 14#include "ecb.h"
13 15
14// compatibility with perl <5.18 16// compatibility with perl <5.18
15#ifndef HvNAMELEN_get 17#ifndef HvNAMELEN_get
16# define HvNAMELEN_get(hv) strlen (HvNAME (hv)) 18# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
19# define HvNAMELEN(hv) HvNAMELEN_get (hv) 21# define HvNAMELEN(hv) HvNAMELEN_get (hv)
20#endif 22#endif
21#ifndef HvNAMEUTF8 23#ifndef HvNAMEUTF8
22# define HvNAMEUTF8(hv) 0 24# define HvNAMEUTF8(hv) 0
23#endif 25#endif
26#ifndef SvREFCNT_dec_NN
27# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
28#endif
29
30// known major and minor types
31enum cbor_type
32{
33 MAJOR_SHIFT = 5,
34 MINOR_MASK = 0x1f,
35
36 MAJOR_POS_INT = 0 << MAJOR_SHIFT,
37 MAJOR_NEG_INT = 1 << MAJOR_SHIFT,
38 MAJOR_BYTES = 2 << MAJOR_SHIFT,
39 MAJOR_TEXT = 3 << MAJOR_SHIFT,
40 MAJOR_ARRAY = 4 << MAJOR_SHIFT,
41 MAJOR_MAP = 5 << MAJOR_SHIFT,
42 MAJOR_TAG = 6 << MAJOR_SHIFT,
43 MAJOR_MISC = 7 << MAJOR_SHIFT,
44
45 // INT/STRING/ARRAY/MAP subtypes
46 LENGTH_EXT1 = 24,
47 LENGTH_EXT2 = 25,
48 LENGTH_EXT4 = 26,
49 LENGTH_EXT8 = 27,
50
51 // SIMPLE types (effectively MISC subtypes)
52 SIMPLE_FALSE = 20,
53 SIMPLE_TRUE = 21,
54 SIMPLE_NULL = 22,
55 SIMPLE_UNDEF = 23,
56
57 // MISC subtype (unused)
58 MISC_EXT1 = 24,
59 MISC_FLOAT16 = 25,
60 MISC_FLOAT32 = 26,
61 MISC_FLOAT64 = 27,
62
63 // BYTES/TEXT/ARRAY/MAP
64 MINOR_INDEF = 31,
65};
24 66
25// known tags 67// known tags
26enum cbor_tag 68enum cbor_tag
27{ 69{
28 // inofficial extensions (pending iana registration) 70 // extensions
71 CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref
29 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object 72 CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object
30 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object 73 CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object
31 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing 74 CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing
32 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing 75 CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing
33 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref 76 CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref
34 CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref
35 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection 77 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
36 78
37 // rfc7049 79 // rfc7049
38 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 80 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
39 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any 81 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
40 CBOR_TAG_POS_BIGNUM = 2, // byte string 82 CBOR_TAG_POS_BIGNUM = 2, // byte string
41 CBOR_TAG_NEG_BIGNUM = 3, // byte string 83 CBOR_TAG_NEG_BIGNUM = 3, // byte string
42 CBOR_TAG_DECIMAL = 4, // decimal fraction, array 84 CBOR_TAG_DECIMAL = 4, // decimal fraction, array
43 CBOR_TAG_BIGFLOAT = 5, // array 85 CBOR_TAG_BIGFLOAT = 5, // array
44 86
45 CBOR_TAG_CONV_B64U = 21, // base64url, any 87 CBOR_TAG_CONV_B64U = 21, // base64url, any
46 CBOR_TAG_CONV_B64 = 22, // base64, any 88 CBOR_TAG_CONV_B64 = 22, // base64, any
47 CBOR_TAG_CONV_HEX = 23, // base16, any 89 CBOR_TAG_CONV_HEX = 23, // base16, any
48 CBOR_TAG_CBOR = 24, // embedded cbor, byte string 90 CBOR_TAG_CBOR = 24, // embedded cbor, byte string
49 91
50 CBOR_TAG_URI = 32, // URI rfc3986, utf-8 92 CBOR_TAG_URI = 32, // URI rfc3986, utf-8
51 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 93 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8
52 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 94 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
53 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 95 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
54 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 96 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
55 97
56 CBOR_TAG_MAGIC = 55799 // self-describe cbor 98 CBOR_TAG_MAGIC = 55799, // self-describe cbor
57}; 99};
58 100
59#define F_SHRINK 0x00000001UL 101#define F_SHRINK 0x00000001UL
60#define F_ALLOW_UNKNOWN 0x00000002UL 102#define F_ALLOW_UNKNOWN 0x00000002UL
61#define F_ALLOW_SHARING 0x00000004UL //TODO 103#define F_ALLOW_SHARING 0x00000004UL
62#define F_ALLOW_STRINGREF 0x00000008UL //TODO 104#define F_ALLOW_CYCLES 0x00000008UL
105#define F_FORBID_OBJECTS 0x00000010UL
106#define F_PACK_STRINGS 0x00000020UL
107#define F_TEXT_KEYS 0x00000040UL
108#define F_TEXT_STRINGS 0x00000080UL
109#define F_VALIDATE_UTF8 0x00000100UL
63 110
64#define INIT_SIZE 32 // initial scalar size to be allocated 111#define INIT_SIZE 32 // initial scalar size to be allocated
65 112
66#define SB do { 113#define SB do {
67#define SE } while (0) 114#define SE } while (0)
79# define CBOR_SLOW 0 126# define CBOR_SLOW 0
80# define CBOR_STASH cbor_stash 127# define CBOR_STASH cbor_stash
81#endif 128#endif
82 129
83static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: 130static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
84static SV *types_true, *types_false, *types_error, *sv_cbor; 131static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter;
85 132
86typedef struct { 133typedef struct {
87 U32 flags; 134 U32 flags;
88 U32 max_depth; 135 U32 max_depth;
89 STRLEN max_size; 136 STRLEN max_size;
137 SV *filter;
138
139 // for the incremental parser
140 STRLEN incr_pos; // the current offset into the text
141 STRLEN incr_need; // minimum bytes needed to decode
142 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
90} CBOR; 143} CBOR;
91 144
92ecb_inline void 145ecb_inline void
93cbor_init (CBOR *cbor) 146cbor_init (CBOR *cbor)
94{ 147{
95 Zero (cbor, 1, CBOR); 148 Zero (cbor, 1, CBOR);
96 cbor->max_depth = 512; 149 cbor->max_depth = 512;
150}
151
152ecb_inline void
153cbor_free (CBOR *cbor)
154{
155 SvREFCNT_dec (cbor->filter);
156 SvREFCNT_dec (cbor->incr_count);
97} 157}
98 158
99///////////////////////////////////////////////////////////////////////////// 159/////////////////////////////////////////////////////////////////////////////
100// utility functions 160// utility functions
101 161
127 187
128// minimum length of a string to be registered for stringref 188// minimum length of a string to be registered for stringref
129ecb_inline int 189ecb_inline int
130minimum_string_length (UV idx) 190minimum_string_length (UV idx)
131{ 191{
132 return idx > 23 192
133 ? idx > 0xffU 193 return idx <= 23 ? 3
134 ? idx > 0xffffU 194 : idx <= 0xffU ? 4
195 : idx <= 0xffffU ? 5
135 ? idx > 0xffffffffU 196 : idx <= 0xffffffffU ? 7
136 ? 7 197 : 11;
137 : 6
138 : 5
139 : 4
140 : 3;
141} 198}
142 199
143///////////////////////////////////////////////////////////////////////////// 200/////////////////////////////////////////////////////////////////////////////
144// encoder 201// encoder
145 202
158} enc_t; 215} enc_t;
159 216
160ecb_inline void 217ecb_inline void
161need (enc_t *enc, STRLEN len) 218need (enc_t *enc, STRLEN len)
162{ 219{
163 if (ecb_expect_false (enc->cur + len >= enc->end)) 220 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
164 { 221 {
165 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 222 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
166 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 223 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
167 enc->cur = SvPVX (enc->sv) + cur; 224 enc->cur = SvPVX (enc->sv) + cur;
168 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 225 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
179static void 236static void
180encode_uint (enc_t *enc, int major, UV len) 237encode_uint (enc_t *enc, int major, UV len)
181{ 238{
182 need (enc, 9); 239 need (enc, 9);
183 240
184 if (len < 24) 241 if (ecb_expect_true (len < LENGTH_EXT1))
185 *enc->cur++ = major | len; 242 *enc->cur++ = major | len;
186 else if (len <= 0xff) 243 else if (ecb_expect_true (len <= 0xffU))
187 { 244 {
188 *enc->cur++ = major | 24; 245 *enc->cur++ = major | LENGTH_EXT1;
189 *enc->cur++ = len; 246 *enc->cur++ = len;
190 } 247 }
191 else if (len <= 0xffff) 248 else if (len <= 0xffffU)
192 { 249 {
193 *enc->cur++ = major | 25; 250 *enc->cur++ = major | LENGTH_EXT2;
194 *enc->cur++ = len >> 8; 251 *enc->cur++ = len >> 8;
195 *enc->cur++ = len; 252 *enc->cur++ = len;
196 } 253 }
197 else if (len <= 0xffffffff) 254 else if (len <= 0xffffffffU)
198 { 255 {
199 *enc->cur++ = major | 26; 256 *enc->cur++ = major | LENGTH_EXT4;
200 *enc->cur++ = len >> 24; 257 *enc->cur++ = len >> 24;
201 *enc->cur++ = len >> 16; 258 *enc->cur++ = len >> 16;
202 *enc->cur++ = len >> 8; 259 *enc->cur++ = len >> 8;
203 *enc->cur++ = len; 260 *enc->cur++ = len;
204 } 261 }
205 else 262 else
206 { 263 {
207 *enc->cur++ = major | 27; 264 *enc->cur++ = major | LENGTH_EXT8;
208 *enc->cur++ = len >> 56; 265 *enc->cur++ = len >> 56;
209 *enc->cur++ = len >> 48; 266 *enc->cur++ = len >> 48;
210 *enc->cur++ = len >> 40; 267 *enc->cur++ = len >> 40;
211 *enc->cur++ = len >> 32; 268 *enc->cur++ = len >> 32;
212 *enc->cur++ = len >> 24; 269 *enc->cur++ = len >> 24;
217} 274}
218 275
219ecb_inline void 276ecb_inline void
220encode_tag (enc_t *enc, UV tag) 277encode_tag (enc_t *enc, UV tag)
221{ 278{
222 encode_uint (enc, 0xc0, tag); 279 encode_uint (enc, MAJOR_TAG, tag);
223} 280}
224 281
225static void 282// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
283ecb_noinline static void
226encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 284encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
227{ 285{
286 STRLEN ulen = len;
287 U8 *p, *pend = (U8 *)str + len;
288
289 for (p = (U8 *)str; p < pend; ++p)
290 ulen += *p >> 7; // count set high bits
291
292 encode_uint (enc, MAJOR_TEXT, ulen);
293
294 need (enc, ulen);
295 for (p = (U8 *)str; p < pend; ++p)
296 if (*p < 0x80)
297 *enc->cur++ = *p;
298 else
299 {
300 *enc->cur++ = 0xc0 + (*p >> 6);
301 *enc->cur++ = 0x80 + (*p & 63);
302 }
303}
304
305ecb_inline void
306encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
307{
308 if (ecb_expect_false (upgrade_utf8))
309 if (!utf8)
310 {
311 encode_str_utf8 (enc, utf8, str, len);
312 return;
313 }
314
315 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
316 need (enc, len);
317 memcpy (enc->cur, str, len);
318 enc->cur += len;
319}
320
321ecb_inline void
322encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
323{
228 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) 324 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
229 { 325 {
230 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 326 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
231 327
232 if (SvOK (*svp)) 328 if (SvOK (*svp))
233 { 329 {
234 // already registered, use stringref 330 // already registered, use stringref
235 encode_tag (enc, CBOR_TAG_STRINGREF); 331 encode_tag (enc, CBOR_TAG_STRINGREF);
236 encode_uint (enc, 0x00, SvUV (*svp)); 332 encode_uint (enc, MAJOR_POS_INT, SvUV (*svp));
237 return; 333 return;
238 } 334 }
239 else if (len >= minimum_string_length (enc->stringref_idx)) 335 else if (len >= minimum_string_length (enc->stringref_idx))
240 { 336 {
241 // register only 337 // register only
242 sv_setuv (*svp, enc->stringref_idx); 338 sv_setuv (*svp, enc->stringref_idx);
243 ++enc->stringref_idx; 339 ++enc->stringref_idx;
244 } 340 }
245 } 341 }
246 342
247 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 343 encode_str (enc, upgrade_utf8, utf8, str, len);
248 need (enc, len);
249 memcpy (enc->cur, str, len);
250 enc->cur += len;
251} 344}
252 345
253static void encode_sv (enc_t *enc, SV *sv); 346static void encode_sv (enc_t *enc, SV *sv);
254 347
255static void 348static void
260 if (enc->depth >= enc->cbor.max_depth) 353 if (enc->depth >= enc->cbor.max_depth)
261 croak (ERR_NESTING_EXCEEDED); 354 croak (ERR_NESTING_EXCEEDED);
262 355
263 ++enc->depth; 356 ++enc->depth;
264 357
265 encode_uint (enc, 0x80, len + 1); 358 encode_uint (enc, MAJOR_ARRAY, len + 1);
266 359
360 if (SvMAGICAL (av))
267 for (i = 0; i <= len; ++i) 361 for (i = 0; i <= len; ++i)
268 { 362 {
269 SV **svp = av_fetch (av, i, 0); 363 SV **svp = av_fetch (av, i, 0);
270 encode_sv (enc, svp ? *svp : &PL_sv_undef); 364 encode_sv (enc, svp ? *svp : &PL_sv_undef);
271 } 365 }
366 else
367 for (i = 0; i <= len; ++i)
368 {
369 SV *sv = AvARRAY (av)[i];
370 encode_sv (enc, sv ? sv : &PL_sv_undef);
371 }
272 372
273 --enc->depth; 373 --enc->depth;
274} 374}
275 375
276static void 376static void
285 385
286 int pairs = hv_iterinit (hv); 386 int pairs = hv_iterinit (hv);
287 int mg = SvMAGICAL (hv); 387 int mg = SvMAGICAL (hv);
288 388
289 if (mg) 389 if (mg)
290 encode_ch (enc, 0xa0 | 31); 390 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
291 else 391 else
292 encode_uint (enc, 0xa0, pairs); 392 encode_uint (enc, MAJOR_MAP, pairs);
293 393
294 while ((he = hv_iternext (hv))) 394 while ((he = hv_iternext (hv)))
295 { 395 {
296 if (HeKLEN (he) == HEf_SVKEY) 396 if (HeKLEN (he) == HEf_SVKEY)
297 encode_sv (enc, HeSVKEY (he)); 397 encode_sv (enc, HeSVKEY (he));
298 else 398 else
299 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 399 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
300 400
301 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 401 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
302 } 402 }
303 403
304 if (mg) 404 if (mg)
305 encode_ch (enc, 0xe0 | 31); 405 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
306 406
307 --enc->depth; 407 --enc->depth;
308} 408}
309 409
310// encode objects, arrays and special \0=false and \1=true values. 410// encode objects, arrays and special \0=false and \1=true values.
311static void 411static void
312encode_rv (enc_t *enc, SV *sv) 412encode_rv (enc_t *enc, SV *sv)
313{ 413{
314 SvGETMAGIC (sv); 414 SvGETMAGIC (sv);
315
316 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
317 && ecb_expect_false (SvREFCNT (sv) > 1))
318 {
319 if (!enc->shareable)
320 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
321
322 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
323
324 if (SvOK (*svp))
325 {
326 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
327 encode_uint (enc, 0x00, SvUV (*svp));
328 return;
329 }
330 else
331 {
332 sv_setuv (*svp, enc->shareable_idx);
333 ++enc->shareable_idx;
334 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
335 }
336 }
337 415
338 svtype svt = SvTYPE (sv); 416 svtype svt = SvTYPE (sv);
339 417
340 if (ecb_expect_false (SvOBJECT (sv))) 418 if (ecb_expect_false (SvOBJECT (sv)))
341 { 419 {
348 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 426 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
349 ? cbor_tagged_stash 427 ? cbor_tagged_stash
350 : gv_stashpv ("CBOR::XS::Tagged" , 1); 428 : gv_stashpv ("CBOR::XS::Tagged" , 1);
351 429
352 HV *stash = SvSTASH (sv); 430 HV *stash = SvSTASH (sv);
353 GV *method;
354 431
355 if (stash == boolean_stash) 432 if (stash == boolean_stash)
356 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); 433 {
434 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
435 return;
436 }
357 else if (stash == error_stash) 437 else if (stash == error_stash)
358 encode_ch (enc, 0xe0 | 23); 438 {
439 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
440 return;
441 }
359 else if (stash == tagged_stash) 442 else if (stash == tagged_stash)
360 { 443 {
361 if (svt != SVt_PVAV) 444 if (svt != SVt_PVAV)
362 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 445 croak ("encountered CBOR::XS::Tagged object that isn't an array");
363 446
364 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); 447 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
365 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 448 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
449
450 return;
451 }
452 }
453
454 if (ecb_expect_false (SvREFCNT (sv) > 1)
455 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
456 {
457 if (!enc->shareable)
458 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
459
460 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
461
462 if (SvOK (*svp))
366 } 463 {
464 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
465 encode_uint (enc, MAJOR_POS_INT, SvUV (*svp));
466 return;
467 }
468 else
469 {
470 sv_setuv (*svp, enc->shareable_idx);
471 ++enc->shareable_idx;
472 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
473 }
474 }
475
476 if (ecb_expect_false (SvOBJECT (sv)))
477 {
478 HV *stash = SvSTASH (sv);
479 GV *method;
480
481 if (enc->cbor.flags & F_FORBID_OBJECTS)
482 croak ("encountered object '%s', but forbid_objects is enabled",
483 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
367 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 484 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
368 { 485 {
369 dSP; 486 dSP;
370 487
371 ENTER; SAVETMPS; PUSHMARK (SP); 488 ENTER; SAVETMPS;
489 PUSHMARK (SP);
372 // we re-bless the reference to get overload and other niceties right 490 // we re-bless the reference to get overload and other niceties right
373 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 491 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
374 492
375 PUTBACK; 493 PUTBACK;
376 // G_SCALAR ensures that return value is 1 494 // G_SCALAR ensures that return value is 1
389 } 507 }
390 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 508 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
391 { 509 {
392 dSP; 510 dSP;
393 511
394 ENTER; SAVETMPS; PUSHMARK (SP); 512 ENTER; SAVETMPS;
513 SAVESTACK_POS ();
514 PUSHMARK (SP);
395 EXTEND (SP, 2); 515 EXTEND (SP, 2);
396 // we re-bless the reference to get overload and other niceties right 516 // we re-bless the reference to get overload and other niceties right
397 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 517 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
398 PUSHs (sv_cbor); 518 PUSHs (sv_cbor);
399 519
404 // catch this surprisingly common error 524 // catch this surprisingly common error
405 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 525 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
406 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 526 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
407 527
408 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 528 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
409 encode_uint (enc, 0x80, count + 1); 529 encode_uint (enc, MAJOR_ARRAY, count + 1);
410 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 530 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
411 531
412 while (count) 532 while (count)
413 encode_sv (enc, SP[1 - count--]); 533 encode_sv (enc, SP[1 - count--]);
414 534
415 PUTBACK; 535 PUTBACK;
436{ 556{
437 double nv = SvNVX (sv); 557 double nv = SvNVX (sv);
438 558
439 need (enc, 9); 559 need (enc, 9);
440 560
441 if (ecb_expect_false (nv == (U32)nv)) 561 if (ecb_expect_false (nv == (NV)(U32)nv))
442 encode_uint (enc, 0x00, (U32)nv); 562 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
443 //TODO: maybe I32? 563 //TODO: maybe I32?
444 else if (ecb_expect_false (nv == (float)nv)) 564 else if (ecb_expect_false (nv == (float)nv))
445 { 565 {
446 uint32_t fp = ecb_float_to_binary32 (nv); 566 uint32_t fp = ecb_float_to_binary32 (nv);
447 567
448 *enc->cur++ = 0xe0 | 26; 568 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
449 569
450 if (!ecb_big_endian ()) 570 if (!ecb_big_endian ())
451 fp = ecb_bswap32 (fp); 571 fp = ecb_bswap32 (fp);
452 572
453 memcpy (enc->cur, &fp, 4); 573 memcpy (enc->cur, &fp, 4);
455 } 575 }
456 else 576 else
457 { 577 {
458 uint64_t fp = ecb_double_to_binary64 (nv); 578 uint64_t fp = ecb_double_to_binary64 (nv);
459 579
460 *enc->cur++ = 0xe0 | 27; 580 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
461 581
462 if (!ecb_big_endian ()) 582 if (!ecb_big_endian ())
463 fp = ecb_bswap64 (fp); 583 fp = ecb_bswap64 (fp);
464 584
465 memcpy (enc->cur, &fp, 8); 585 memcpy (enc->cur, &fp, 8);
474 594
475 if (SvPOKp (sv)) 595 if (SvPOKp (sv))
476 { 596 {
477 STRLEN len; 597 STRLEN len;
478 char *str = SvPV (sv, len); 598 char *str = SvPV (sv, len);
479 encode_str (enc, SvUTF8 (sv), str, len); 599 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
480 } 600 }
481 else if (SvNOKp (sv)) 601 else if (SvNOKp (sv))
482 encode_nv (enc, sv); 602 encode_nv (enc, sv);
483 else if (SvIOKp (sv)) 603 else if (SvIOKp (sv))
484 { 604 {
485 if (SvIsUV (sv)) 605 if (SvIsUV (sv))
486 encode_uint (enc, 0x00, SvUVX (sv)); 606 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
487 else if (SvIVX (sv) >= 0) 607 else if (SvIVX (sv) >= 0)
488 encode_uint (enc, 0x00, SvIVX (sv)); 608 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
489 else 609 else
490 encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); 610 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
491 } 611 }
492 else if (SvROK (sv)) 612 else if (SvROK (sv))
493 encode_rv (enc, SvRV (sv)); 613 encode_rv (enc, SvRV (sv));
494 else if (!SvOK (sv)) 614 else if (!SvOK (sv))
495 encode_ch (enc, 0xe0 | 22); 615 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
496 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 616 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
497 encode_ch (enc, 0xe0 | 23); 617 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
498 else 618 else
499 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", 619 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data",
500 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 620 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
501} 621}
502 622
503static SV * 623static SV *
504encode_cbor (SV *scalar, CBOR *cbor) 624encode_cbor (SV *scalar, CBOR *cbor)
505{ 625{
506 enc_t enc = { }; 626 enc_t enc = { 0 };
507 627
508 enc.cbor = *cbor; 628 enc.cbor = *cbor;
509 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 629 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
510 enc.cur = SvPVX (enc.sv); 630 enc.cur = SvPVX (enc.sv);
511 enc.end = SvEND (enc.sv); 631 enc.end = SvEND (enc.sv);
512 632
513 SvPOK_only (enc.sv); 633 SvPOK_only (enc.sv);
514 634
515 if (cbor->flags & F_ALLOW_STRINGREF) 635 if (cbor->flags & F_PACK_STRINGS)
516 { 636 {
517 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 637 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
518 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 638 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
519 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 639 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
520 } 640 }
542 CBOR cbor; 662 CBOR cbor;
543 U32 depth; // recursion depth 663 U32 depth; // recursion depth
544 U32 maxdepth; // recursion depth limit 664 U32 maxdepth; // recursion depth limit
545 AV *shareable; 665 AV *shareable;
546 AV *stringref; 666 AV *stringref;
667 SV *decode_tagged;
668 SV *err_sv; // optional sv for error, needs to be freed
547} dec_t; 669} dec_t;
548 670
549#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 671// set dec->err to ERRSV
672ecb_cold static void
673err_errsv (dec_t *dec)
674{
675 if (!dec->err)
676 {
677 dec->err_sv = newSVsv (ERRSV);
550 678
551#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 679 // chop off the trailing \n
680 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
681 *SvEND (dec->err_sv) = 0;
552 682
683 dec->err = SvPVutf8_nolen (dec->err_sv);
684 }
685}
686
687// the following functions are used to reduce code size and help the compiler to optimise
688ecb_cold static void
689err_set (dec_t *dec, const char *reason)
690{
691 if (!dec->err)
692 dec->err = reason;
693}
694
695ecb_cold static void
696err_unexpected_end (dec_t *dec)
697{
698 err_set (dec, "unexpected end of CBOR data");
699}
700
701ecb_cold static void
702err_nesting_exceeded (dec_t *dec)
703{
704 err_set (dec, ERR_NESTING_EXCEEDED);
705}
706
707#define ERR_DO(do) SB do; goto fail; SE
708#define ERR(reason) ERR_DO (err_set (dec, reason))
709#define ERR_ERRSV ERR_DO (err_errsv (dec))
710
711#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
712
553#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 713#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
554#define DEC_DEC_DEPTH --dec->depth 714#define DEC_DEC_DEPTH --dec->depth
555 715
556static UV 716static UV
557decode_uint (dec_t *dec) 717decode_uint (dec_t *dec)
558{ 718{
559 switch (*dec->cur & 31) 719 U8 m = *dec->cur & MINOR_MASK;
560 { 720 ++dec->cur;
561 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
562 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
563 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
564 return *dec->cur++ & 31;
565 721
566 case 24: 722 if (ecb_expect_true (m < LENGTH_EXT1))
723 return m;
724 else if (ecb_expect_true (m == LENGTH_EXT1))
725 {
567 WANT (2); 726 WANT (1);
568 dec->cur += 2; 727 dec->cur += 1;
569 return dec->cur[-1]; 728 return dec->cur[-1];
570 729 }
571 case 25: 730 else if (ecb_expect_true (m == LENGTH_EXT2))
731 {
572 WANT (3); 732 WANT (2);
573 dec->cur += 3; 733 dec->cur += 2;
574 return (((UV)dec->cur[-2]) << 8) 734 return (((UV)dec->cur[-2]) << 8)
575 | ((UV)dec->cur[-1]); 735 | ((UV)dec->cur[-1]);
576 736 }
577 case 26: 737 else if (ecb_expect_true (m == LENGTH_EXT4))
738 {
578 WANT (5); 739 WANT (4);
579 dec->cur += 5; 740 dec->cur += 4;
580 return (((UV)dec->cur[-4]) << 24) 741 return (((UV)dec->cur[-4]) << 24)
581 | (((UV)dec->cur[-3]) << 16) 742 | (((UV)dec->cur[-3]) << 16)
582 | (((UV)dec->cur[-2]) << 8) 743 | (((UV)dec->cur[-2]) << 8)
583 | ((UV)dec->cur[-1]); 744 | ((UV)dec->cur[-1]);
584 745 }
585 case 27: 746 else if (ecb_expect_true (m == LENGTH_EXT8))
747 {
586 WANT (9); 748 WANT (8);
587 dec->cur += 9; 749 dec->cur += 8;
750
751 return
752#if UVSIZE < 8
753 0
754#else
588 return (((UV)dec->cur[-8]) << 56) 755 (((UV)dec->cur[-8]) << 56)
589 | (((UV)dec->cur[-7]) << 48) 756 | (((UV)dec->cur[-7]) << 48)
590 | (((UV)dec->cur[-6]) << 40) 757 | (((UV)dec->cur[-6]) << 40)
591 | (((UV)dec->cur[-5]) << 32) 758 | (((UV)dec->cur[-5]) << 32)
759#endif
592 | (((UV)dec->cur[-4]) << 24) 760 | (((UV)dec->cur[-4]) << 24)
593 | (((UV)dec->cur[-3]) << 16) 761 | (((UV)dec->cur[-3]) << 16)
594 | (((UV)dec->cur[-2]) << 8) 762 | (((UV)dec->cur[-2]) << 8)
595 | ((UV)dec->cur[-1]); 763 | ((UV)dec->cur[-1]);
596 764 }
597 default: 765 else
598 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 766 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
599 }
600 767
601fail: 768fail:
602 return 0; 769 return 0;
603} 770}
604 771
609{ 776{
610 AV *av = newAV (); 777 AV *av = newAV ();
611 778
612 DEC_INC_DEPTH; 779 DEC_INC_DEPTH;
613 780
614 if ((*dec->cur & 31) == 31) 781 if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF))
615 { 782 {
616 ++dec->cur; 783 ++dec->cur;
617 784
618 for (;;) 785 for (;;)
619 { 786 {
620 WANT (1); 787 WANT (1);
621 788
622 if (*dec->cur == (0xe0 | 31)) 789 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
623 { 790 {
624 ++dec->cur; 791 ++dec->cur;
625 break; 792 break;
626 } 793 }
627 794
628 av_push (av, decode_sv (dec)); 795 av_push (av, decode_sv (dec));
629 } 796 }
630 } 797 }
631 else 798 else
632 { 799 {
633 int i, len = decode_uint (dec); 800 UV i, len = decode_uint (dec);
634 801
802 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
635 av_fill (av, len - 1); 803 av_fill (av, len - 1);
636 804
637 for (i = 0; i < len; ++i) 805 for (i = 0; i < len; ++i)
638 AvARRAY (av)[i] = decode_sv (dec); 806 AvARRAY (av)[i] = decode_sv (dec);
639 } 807 }
652{ 820{
653 // for speed reasons, we specialcase single-string 821 // for speed reasons, we specialcase single-string
654 // byte or utf-8 strings as keys, but only when !stringref 822 // byte or utf-8 strings as keys, but only when !stringref
655 823
656 if (ecb_expect_true (!dec->stringref)) 824 if (ecb_expect_true (!dec->stringref))
657 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 825 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
658 { 826 {
659 I32 len = decode_uint (dec); 827 STRLEN len = decode_uint (dec);
660 char *key = (char *)dec->cur; 828 char *key = (char *)dec->cur;
661 829
830 WANT (len);
662 dec->cur += len; 831 dec->cur += len;
663 832
664 if (ecb_expect_false (dec->stringref))
665 av_push (dec->stringref, newSVpvn (key, len));
666
667 hv_store (hv, key, len, decode_sv (dec), 0); 833 hv_store (hv, key, len, decode_sv (dec), 0);
668 834
669 return; 835 return;
670 } 836 }
671 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 837 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
672 { 838 {
673 I32 len = decode_uint (dec); 839 STRLEN len = decode_uint (dec);
674 char *key = (char *)dec->cur; 840 char *key = (char *)dec->cur;
675 841
842 WANT (len);
676 dec->cur += len; 843 dec->cur += len;
677 844
678 if (ecb_expect_false (dec->stringref)) 845 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
679 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 846 if (!is_utf8_string (key, len))
847 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
680 848
681 hv_store (hv, key, -len, decode_sv (dec), 0); 849 hv_store (hv, key, -len, decode_sv (dec), 0);
682 850
683 return; 851 return;
684 } 852 }
685 853
686 SV *k = decode_sv (dec); 854 SV *k = decode_sv (dec);
687 SV *v = decode_sv (dec); 855 SV *v = decode_sv (dec);
688 856
857 // we leak memory if uncaught exceptions are thrown by random magical
858 // methods, and this is hopefully the only place where it can happen,
859 // so if there is a chance of an exception, take the very slow path.
860 // since catching exceptions is "undocumented/internal/forbidden" by
861 // the new p5p powers, we need to call out to a perl function :/
862 if (ecb_expect_false (SvAMAGIC (k)))
863 {
864 dSP;
865
866 ENTER; SAVETMPS;
867 PUSHMARK (SP);
868 EXTEND (SP, 3);
869 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
870 PUSHs (sv_2mortal (k));
871 PUSHs (sv_2mortal (v));
872
873 PUTBACK;
874 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
875 SPAGAIN;
876
877 FREETMPS; LEAVE;
878
879 if (SvTRUE (ERRSV))
880 ERR_ERRSV;
881
882 return;
883 }
884
689 hv_store_ent (hv, k, v, 0); 885 hv_store_ent (hv, k, v, 0);
690 SvREFCNT_dec (k); 886 SvREFCNT_dec (k);
887
888fail:
889 ;
691} 890}
692 891
693static SV * 892static SV *
694decode_hv (dec_t *dec) 893decode_hv (dec_t *dec)
695{ 894{
696 HV *hv = newHV (); 895 HV *hv = newHV ();
697 896
698 DEC_INC_DEPTH; 897 DEC_INC_DEPTH;
699 898
700 if ((*dec->cur & 31) == 31) 899 if (*dec->cur == (MAJOR_MAP | MINOR_INDEF))
701 { 900 {
702 ++dec->cur; 901 ++dec->cur;
703 902
704 for (;;) 903 for (;;)
705 { 904 {
706 WANT (1); 905 WANT (1);
707 906
708 if (*dec->cur == (0xe0 | 31)) 907 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
709 { 908 {
710 ++dec->cur; 909 ++dec->cur;
711 break; 910 break;
712 } 911 }
713 912
714 decode_he (dec, hv); 913 decode_he (dec, hv);
715 } 914 }
716 } 915 }
717 else 916 else
718 { 917 {
719 int pairs = decode_uint (dec); 918 UV pairs = decode_uint (dec);
919
920 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
720 921
721 while (pairs--) 922 while (pairs--)
722 decode_he (dec, hv); 923 decode_he (dec, hv);
723 } 924 }
724 925
734static SV * 935static SV *
735decode_str (dec_t *dec, int utf8) 936decode_str (dec_t *dec, int utf8)
736{ 937{
737 SV *sv = 0; 938 SV *sv = 0;
738 939
739 if ((*dec->cur & 31) == 31) 940 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF)
740 { 941 {
942 // indefinite length strings
741 ++dec->cur; 943 ++dec->cur;
742 944
945 U8 major = *dec->cur & MAJOR_MISC;
946
743 sv = newSVpvn ("", 0); 947 sv = newSVpvn ("", 0);
744 948
745 // not very fast, and certainly not robust against illegal input
746 for (;;) 949 for (;;)
747 { 950 {
748 WANT (1); 951 WANT (1);
749 952
750 if (*dec->cur == (0xe0 | 31)) 953 if ((*dec->cur - major) > LENGTH_EXT8)
954 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
751 { 955 {
752 ++dec->cur; 956 ++dec->cur;
753 break; 957 break;
754 } 958 }
959 else
960 ERR ("corrupted CBOR data (invalid chunks in indefinite length string)");
755 961
756 sv_catsv (sv, decode_sv (dec)); 962 STRLEN len = decode_uint (dec);
963
964 WANT (len);
965 sv_catpvn (sv, dec->cur, len);
966 dec->cur += len;
757 } 967 }
758 } 968 }
759 else 969 else
760 { 970 {
761 STRLEN len = decode_uint (dec); 971 STRLEN len = decode_uint (dec);
768 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 978 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
769 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 979 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
770 } 980 }
771 981
772 if (utf8) 982 if (utf8)
983 {
984 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
985 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
986 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
987
773 SvUTF8_on (sv); 988 SvUTF8_on (sv);
989 }
774 990
775 return sv; 991 return sv;
776 992
777fail: 993fail:
778 SvREFCNT_dec (sv); 994 SvREFCNT_dec (sv);
797 sv = newRV_noinc (decode_sv (dec)); 1013 sv = newRV_noinc (decode_sv (dec));
798 break; 1014 break;
799 1015
800 case CBOR_TAG_STRINGREF_NAMESPACE: 1016 case CBOR_TAG_STRINGREF_NAMESPACE:
801 { 1017 {
1018 // do nmot use SAVETMPS/FREETMPS, as these will
1019 // erase mortalised caches, e.g. "shareable"
802 ENTER; SAVETMPS; 1020 ENTER;
803 1021
804 SAVESPTR (dec->stringref); 1022 SAVESPTR (dec->stringref);
805 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1023 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
806 1024
807 sv = decode_sv (dec); 1025 sv = decode_sv (dec);
808 1026
809 FREETMPS; LEAVE; 1027 LEAVE;
810 } 1028 }
811 break; 1029 break;
812 1030
813 case CBOR_TAG_STRINGREF: 1031 case CBOR_TAG_STRINGREF:
814 { 1032 {
815 if ((*dec->cur >> 5) != 0) 1033 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
816 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1034 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
817 1035
818 UV idx = decode_uint (dec); 1036 UV idx = decode_uint (dec);
819 1037
820 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1038 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
827 case CBOR_TAG_VALUE_SHAREABLE: 1045 case CBOR_TAG_VALUE_SHAREABLE:
828 { 1046 {
829 if (ecb_expect_false (!dec->shareable)) 1047 if (ecb_expect_false (!dec->shareable))
830 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1048 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
831 1049
1050 if (dec->cbor.flags & F_ALLOW_CYCLES)
1051 {
832 sv = newSV (0); 1052 sv = newSV (0);
833 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1053 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
834 1054
835 SV *osv = decode_sv (dec); 1055 SV *osv = decode_sv (dec);
836 sv_setsv (sv, osv); 1056 sv_setsv (sv, osv);
837 SvREFCNT_dec_NN (osv); 1057 SvREFCNT_dec_NN (osv);
1058 }
1059 else
1060 {
1061 av_push (dec->shareable, &PL_sv_undef);
1062 int idx = AvFILLp (dec->shareable);
1063 sv = decode_sv (dec);
1064 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
1065 }
838 } 1066 }
839 break; 1067 break;
840 1068
841 case CBOR_TAG_VALUE_SHAREDREF: 1069 case CBOR_TAG_VALUE_SHAREDREF:
842 { 1070 {
843 if ((*dec->cur >> 5) != 0) 1071 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
844 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1072 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
845 1073
846 UV idx = decode_uint (dec); 1074 UV idx = decode_uint (dec);
847 1075
848 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1076 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
849 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1077 ERR ("corrupted CBOR data (sharedref index out of bounds)");
850 1078
851 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1079 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1080
1081 if (sv == &PL_sv_undef)
1082 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
852 } 1083 }
853 break; 1084 break;
854 1085
855 case CBOR_TAG_PERL_OBJECT: 1086 case CBOR_TAG_PERL_OBJECT:
856 { 1087 {
1088 if (dec->cbor.flags & F_FORBID_OBJECTS)
1089 goto filter;
1090
857 sv = decode_sv (dec); 1091 sv = decode_sv (dec);
858 1092
859 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 1093 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
860 ERR ("corrupted CBOR data (non-array perl object)"); 1094 ERR ("corrupted CBOR data (non-array perl object)");
861 1095
871 if (!method) 1105 if (!method)
872 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1106 ERR ("cannot decode perl-object (package does not have a THAW method)");
873 1107
874 dSP; 1108 dSP;
875 1109
876 ENTER; SAVETMPS; PUSHMARK (SP); 1110 ENTER; SAVETMPS;
1111 PUSHMARK (SP);
877 EXTEND (SP, len + 1); 1112 EXTEND (SP, len + 1);
878 // we re-bless the reference to get overload and other niceties right 1113 // we re-bless the reference to get overload and other niceties right
879 PUSHs (*av_fetch (av, 0, 1)); 1114 PUSHs (*av_fetch (av, 0, 1));
880 PUSHs (sv_cbor); 1115 PUSHs (sv_cbor);
881 1116
889 SPAGAIN; 1124 SPAGAIN;
890 1125
891 if (SvTRUE (ERRSV)) 1126 if (SvTRUE (ERRSV))
892 { 1127 {
893 FREETMPS; LEAVE; 1128 FREETMPS; LEAVE;
894 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1129 ERR_ERRSV;
895 } 1130 }
896 1131
897 SvREFCNT_dec (sv); 1132 SvREFCNT_dec (sv);
898 sv = SvREFCNT_inc (POPs); 1133 sv = SvREFCNT_inc (POPs);
899 1134
902 FREETMPS; LEAVE; 1137 FREETMPS; LEAVE;
903 } 1138 }
904 break; 1139 break;
905 1140
906 default: 1141 default:
1142 filter:
907 { 1143 {
1144 SV *tag_sv = newSVuv (tag);
1145
908 sv = decode_sv (dec); 1146 sv = decode_sv (dec);
909 1147
1148 dSP;
1149 ENTER; SAVETMPS;
1150 SAVESTACK_POS ();
1151 PUSHMARK (SP);
1152 EXTEND (SP, 2);
1153 PUSHs (tag_sv);
1154 PUSHs (sv);
1155
1156 PUTBACK;
1157 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1158 SPAGAIN;
1159
1160 if (SvTRUE (ERRSV))
1161 {
1162 SvREFCNT_dec (tag_sv);
1163 FREETMPS; LEAVE;
1164 ERR_ERRSV;
1165 }
1166
1167 if (count)
1168 {
1169 SvREFCNT_dec (tag_sv);
1170 SvREFCNT_dec (sv);
1171 sv = SvREFCNT_inc (POPs);
1172 }
1173 else
1174 {
910 AV *av = newAV (); 1175 AV *av = newAV ();
911 av_push (av, newSVuv (tag)); 1176 av_push (av, tag_sv);
912 av_push (av, sv); 1177 av_push (av, sv);
913 1178
914 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1179 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
915 ? cbor_tagged_stash 1180 ? cbor_tagged_stash
916 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1181 : gv_stashpv ("CBOR::XS::Tagged" , 1);
917
918 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); 1182 sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash);
1183 }
1184
1185 PUTBACK;
1186
1187 FREETMPS; LEAVE;
919 } 1188 }
920 break; 1189 break;
921 } 1190 }
922 1191
923 return sv; 1192 return sv;
930static SV * 1199static SV *
931decode_sv (dec_t *dec) 1200decode_sv (dec_t *dec)
932{ 1201{
933 WANT (1); 1202 WANT (1);
934 1203
935 switch (*dec->cur >> 5) 1204 switch (*dec->cur >> MAJOR_SHIFT)
936 { 1205 {
937 case 0: // unsigned int 1206 case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec));
938 return newSVuv (decode_uint (dec)); 1207 case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec));
939 case 1: // negative int 1208 case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0);
940 return newSViv (-1 - (IV)decode_uint (dec)); 1209 case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1);
941 case 2: // octet string 1210 case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec);
942 return decode_str (dec, 0); 1211 case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec);
943 case 3: // utf-8 string 1212 case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec);
944 return decode_str (dec, 1); 1213
945 case 4: // array 1214 case MAJOR_MISC >> MAJOR_SHIFT:
946 return decode_av (dec);
947 case 5: // map
948 return decode_hv (dec);
949 case 6: // tag
950 return decode_tagged (dec);
951 case 7: // misc
952 switch (*dec->cur++ & 31) 1215 switch (*dec->cur++ & MINOR_MASK)
953 { 1216 {
954 case 20: 1217 case SIMPLE_FALSE:
955#if CBOR_SLOW 1218#if CBOR_SLOW
956 types_false = get_bool ("Types::Serialiser::false"); 1219 types_false = get_bool ("Types::Serialiser::false");
957#endif 1220#endif
958 return newSVsv (types_false); 1221 return newSVsv (types_false);
959 case 21: 1222 case SIMPLE_TRUE:
960#if CBOR_SLOW 1223#if CBOR_SLOW
961 types_true = get_bool ("Types::Serialiser::true"); 1224 types_true = get_bool ("Types::Serialiser::true");
962#endif 1225#endif
963 return newSVsv (types_true); 1226 return newSVsv (types_true);
964 case 22: 1227 case SIMPLE_NULL:
965 return newSVsv (&PL_sv_undef); 1228 return newSVsv (&PL_sv_undef);
966 case 23: 1229 case SIMPLE_UNDEF:
967#if CBOR_SLOW 1230#if CBOR_SLOW
968 types_error = get_bool ("Types::Serialiser::error"); 1231 types_error = get_bool ("Types::Serialiser::error");
969#endif 1232#endif
970 return newSVsv (types_error); 1233 return newSVsv (types_error);
971 1234
972 case 25: 1235 case MISC_FLOAT16:
973 { 1236 {
974 WANT (2); 1237 WANT (2);
975 1238
976 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; 1239 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1];
977 dec->cur += 2; 1240 dec->cur += 2;
978 1241
979 return newSVnv (ecb_binary16_to_float (fp)); 1242 return newSVnv (ecb_binary16_to_float (fp));
980 } 1243 }
981 1244
982 case 26: 1245 case MISC_FLOAT32:
983 { 1246 {
984 uint32_t fp; 1247 uint32_t fp;
985 WANT (4); 1248 WANT (4);
986 memcpy (&fp, dec->cur, 4); 1249 memcpy (&fp, dec->cur, 4);
987 dec->cur += 4; 1250 dec->cur += 4;
990 fp = ecb_bswap32 (fp); 1253 fp = ecb_bswap32 (fp);
991 1254
992 return newSVnv (ecb_binary32_to_float (fp)); 1255 return newSVnv (ecb_binary32_to_float (fp));
993 } 1256 }
994 1257
995 case 27: 1258 case MISC_FLOAT64:
996 { 1259 {
997 uint64_t fp; 1260 uint64_t fp;
998 WANT (8); 1261 WANT (8);
999 memcpy (&fp, dec->cur, 8); 1262 memcpy (&fp, dec->cur, 8);
1000 dec->cur += 8; 1263 dec->cur += 8;
1003 fp = ecb_bswap64 (fp); 1266 fp = ecb_bswap64 (fp);
1004 1267
1005 return newSVnv (ecb_binary64_to_double (fp)); 1268 return newSVnv (ecb_binary64_to_double (fp));
1006 } 1269 }
1007 1270
1008 // 0..19 unassigned 1271 // 0..19 unassigned simple
1009 // 24 reserved + unassigned (reserved values are not encodable) 1272 // 24 reserved + unassigned simple (reserved values are not encodable)
1273 // 28-30 unassigned misc
1274 // 31 break code
1010 default: 1275 default:
1011 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1276 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1012 } 1277 }
1013 1278
1014 break; 1279 break;
1015 } 1280 }
1016 1281
1019} 1284}
1020 1285
1021static SV * 1286static SV *
1022decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1287decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1023{ 1288{
1024 dec_t dec = { }; 1289 dec_t dec = { 0 };
1025 SV *sv; 1290 SV *sv;
1026 STRLEN len; 1291 STRLEN len;
1027 char *data = SvPVbyte (string, len); 1292 char *data = SvPVbyte (string, len);
1028 1293
1029 if (len > cbor->max_size && cbor->max_size) 1294 if (len > cbor->max_size && cbor->max_size)
1043 if (dec.cur != dec.end && !dec.err) 1308 if (dec.cur != dec.end && !dec.err)
1044 dec.err = "garbage after CBOR object"; 1309 dec.err = "garbage after CBOR object";
1045 1310
1046 if (dec.err) 1311 if (dec.err)
1047 { 1312 {
1313 if (dec.shareable)
1314 {
1315 // need to break cyclic links, which would all be in shareable
1316 int i;
1317 SV **svp;
1318
1319 for (i = av_len (dec.shareable) + 1; i--; )
1320 if ((svp = av_fetch (dec.shareable, i, 0)))
1321 sv_setsv (*svp, &PL_sv_undef);
1322 }
1323
1048 SvREFCNT_dec (sv); 1324 SvREFCNT_dec (sv);
1325
1326 if (dec.err_sv)
1327 sv_2mortal (dec.err_sv);
1328
1049 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1329 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1050 } 1330 }
1051 1331
1052 sv = sv_2mortal (sv); 1332 sv = sv_2mortal (sv);
1053 1333
1054 return sv; 1334 return sv;
1055} 1335}
1056 1336
1337/////////////////////////////////////////////////////////////////////////////
1338// incremental parser
1339
1340#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1341
1342// returns 0 for notyet, 1 for success or error
1343static int
1344incr_parse (CBOR *self, SV *cborstr)
1345{
1346 STRLEN cur;
1347 SvPV (cborstr, cur);
1348
1349 while (ecb_expect_true (self->incr_need <= cur))
1350 {
1351 // table of integer count bytes
1352 static I8 incr_len[MINOR_MASK + 1] = {
1353 0, 0, 0, 0, 0, 0, 0, 0,
1354 0, 0, 0, 0, 0, 0, 0, 0,
1355 0, 0, 0, 0, 0, 0, 0, 0,
1356 1, 2, 4, 8,-1,-1,-1,-2
1357 };
1358
1359 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1360 U8 m = *p & MINOR_MASK;
1361 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1362 I8 ilen = incr_len[m];
1363
1364 self->incr_need = self->incr_pos + 1;
1365
1366 if (ecb_expect_false (ilen < 0))
1367 {
1368 if (m != MINOR_INDEF)
1369 return 1; // error
1370
1371 if (*p == (MAJOR_MISC | MINOR_INDEF))
1372 {
1373 if (count >= 0)
1374 return 1; // error
1375
1376 count = 1;
1377 }
1378 else
1379 {
1380 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1381 count = -1;
1382 }
1383 }
1384 else
1385 {
1386 self->incr_need += ilen;
1387 if (ecb_expect_false (self->incr_need > cur))
1388 return 0;
1389
1390 int major = *p >> MAJOR_SHIFT;
1391
1392 switch (major)
1393 {
1394 case MAJOR_TAG >> MAJOR_SHIFT:
1395 ++count; // tags merely prefix another value
1396 break;
1397
1398 case MAJOR_BYTES >> MAJOR_SHIFT:
1399 case MAJOR_TEXT >> MAJOR_SHIFT:
1400 case MAJOR_ARRAY >> MAJOR_SHIFT:
1401 case MAJOR_MAP >> MAJOR_SHIFT:
1402 {
1403 UV len;
1404
1405 if (ecb_expect_false (ilen))
1406 {
1407 len = 0;
1408
1409 do {
1410 len = (len << 8) | *++p;
1411 } while (--ilen);
1412 }
1413 else
1414 len = m;
1415
1416 switch (major)
1417 {
1418 case MAJOR_BYTES >> MAJOR_SHIFT:
1419 case MAJOR_TEXT >> MAJOR_SHIFT:
1420 self->incr_need += len;
1421 if (ecb_expect_false (self->incr_need > cur))
1422 return 0;
1423
1424 break;
1425
1426 case MAJOR_MAP >> MAJOR_SHIFT:
1427 len <<= 1;
1428 case MAJOR_ARRAY >> MAJOR_SHIFT:
1429 if (len)
1430 {
1431 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1432 count = len + 1;
1433 }
1434 break;
1435 }
1436 }
1437 }
1438 }
1439
1440 self->incr_pos = self->incr_need;
1441
1442 if (count > 0)
1443 {
1444 while (!--count)
1445 {
1446 if (!AvFILLp (self->incr_count))
1447 return 1; // done
1448
1449 SvREFCNT_dec_NN (av_pop (self->incr_count));
1450 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1451 }
1452
1453 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1454 }
1455 }
1456
1457 return 0;
1458}
1459
1460
1057///////////////////////////////////////////////////////////////////////////// 1461/////////////////////////////////////////////////////////////////////////////
1058// XS interface functions 1462// XS interface functions
1059 1463
1060MODULE = CBOR::XS PACKAGE = CBOR::XS 1464MODULE = CBOR::XS PACKAGE = CBOR::XS
1061 1465
1069 1473
1070 types_true = get_bool ("Types::Serialiser::true" ); 1474 types_true = get_bool ("Types::Serialiser::true" );
1071 types_false = get_bool ("Types::Serialiser::false"); 1475 types_false = get_bool ("Types::Serialiser::false");
1072 types_error = get_bool ("Types::Serialiser::error"); 1476 types_error = get_bool ("Types::Serialiser::error");
1073 1477
1478 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1479
1074 sv_cbor = newSVpv ("CBOR", 0); 1480 sv_cbor = newSVpv ("CBOR", 0);
1075 SvREADONLY_on (sv_cbor); 1481 SvREADONLY_on (sv_cbor);
1482
1483 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1076} 1484}
1077 1485
1078PROTOTYPES: DISABLE 1486PROTOTYPES: DISABLE
1079 1487
1080void CLONE (...) 1488void CLONE (...)
1099void shrink (CBOR *self, int enable = 1) 1507void shrink (CBOR *self, int enable = 1)
1100 ALIAS: 1508 ALIAS:
1101 shrink = F_SHRINK 1509 shrink = F_SHRINK
1102 allow_unknown = F_ALLOW_UNKNOWN 1510 allow_unknown = F_ALLOW_UNKNOWN
1103 allow_sharing = F_ALLOW_SHARING 1511 allow_sharing = F_ALLOW_SHARING
1104 allow_stringref = F_ALLOW_STRINGREF 1512 allow_cycles = F_ALLOW_CYCLES
1513 forbid_objects = F_FORBID_OBJECTS
1514 pack_strings = F_PACK_STRINGS
1515 text_keys = F_TEXT_KEYS
1516 text_strings = F_TEXT_STRINGS
1517 validate_utf8 = F_VALIDATE_UTF8
1105 PPCODE: 1518 PPCODE:
1106{ 1519{
1107 if (enable) 1520 if (enable)
1108 self->flags |= ix; 1521 self->flags |= ix;
1109 else 1522 else
1115void get_shrink (CBOR *self) 1528void get_shrink (CBOR *self)
1116 ALIAS: 1529 ALIAS:
1117 get_shrink = F_SHRINK 1530 get_shrink = F_SHRINK
1118 get_allow_unknown = F_ALLOW_UNKNOWN 1531 get_allow_unknown = F_ALLOW_UNKNOWN
1119 get_allow_sharing = F_ALLOW_SHARING 1532 get_allow_sharing = F_ALLOW_SHARING
1120 get_allow_stringref = F_ALLOW_STRINGREF 1533 get_allow_cycles = F_ALLOW_CYCLES
1534 get_forbid_objects = F_FORBID_OBJECTS
1535 get_pack_strings = F_PACK_STRINGS
1536 get_text_keys = F_TEXT_KEYS
1537 get_text_strings = F_TEXT_STRINGS
1538 get_validate_utf8 = F_VALIDATE_UTF8
1121 PPCODE: 1539 PPCODE:
1122 XPUSHs (boolSV (self->flags & ix)); 1540 XPUSHs (boolSV (self->flags & ix));
1123 1541
1124void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1542void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1125 PPCODE: 1543 PPCODE:
1138 XPUSHs (ST (0)); 1556 XPUSHs (ST (0));
1139 1557
1140int get_max_size (CBOR *self) 1558int get_max_size (CBOR *self)
1141 CODE: 1559 CODE:
1142 RETVAL = self->max_size; 1560 RETVAL = self->max_size;
1561 OUTPUT:
1562 RETVAL
1563
1564void filter (CBOR *self, SV *filter = 0)
1565 PPCODE:
1566 SvREFCNT_dec (self->filter);
1567 self->filter = filter ? newSVsv (filter) : filter;
1568 XPUSHs (ST (0));
1569
1570SV *get_filter (CBOR *self)
1571 CODE:
1572 RETVAL = self->filter ? self->filter : NEWSV (0, 0);
1143 OUTPUT: 1573 OUTPUT:
1144 RETVAL 1574 RETVAL
1145 1575
1146void encode (CBOR *self, SV *scalar) 1576void encode (CBOR *self, SV *scalar)
1147 PPCODE: 1577 PPCODE:
1162 EXTEND (SP, 2); 1592 EXTEND (SP, 2);
1163 PUSHs (sv); 1593 PUSHs (sv);
1164 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1594 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1165} 1595}
1166 1596
1597void incr_parse (CBOR *self, SV *cborstr)
1598 ALIAS:
1599 incr_parse_multiple = 1
1600 PPCODE:
1601{
1602 if (SvUTF8 (cborstr))
1603 sv_utf8_downgrade (cborstr, 0);
1604
1605 if (!self->incr_count)
1606 {
1607 self->incr_count = newAV ();
1608 self->incr_pos = 0;
1609 self->incr_need = 1;
1610
1611 av_push (self->incr_count, newSViv (1));
1612 }
1613
1614 do
1615 {
1616 if (!incr_parse (self, cborstr))
1617 {
1618 if (self->incr_need > self->max_size && self->max_size)
1619 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1620 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1621
1622 break;
1623 }
1624
1625 SV *sv;
1626 char *offset;
1627
1628 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1629 XPUSHs (sv);
1630
1631 sv_chop (cborstr, offset);
1632
1633 av_clear (self->incr_count);
1634 av_push (self->incr_count, newSViv (1));
1635
1636 self->incr_pos = 0;
1637 self->incr_need = self->incr_pos + 1;
1638 }
1639 while (ix);
1640}
1641
1642void incr_reset (CBOR *self)
1643 CODE:
1644{
1645 SvREFCNT_dec (self->incr_count);
1646 self->incr_count = 0;
1647}
1648
1649void DESTROY (CBOR *self)
1650 PPCODE:
1651 cbor_free (self);
1652
1167PROTOTYPES: ENABLE 1653PROTOTYPES: ENABLE
1168 1654
1169void encode_cbor (SV *scalar) 1655void encode_cbor (SV *scalar)
1656 ALIAS:
1657 encode_cbor = 0
1658 encode_cbor_sharing = F_ALLOW_SHARING
1170 PPCODE: 1659 PPCODE:
1171{ 1660{
1172 CBOR cbor; 1661 CBOR cbor;
1173 cbor_init (&cbor); 1662 cbor_init (&cbor);
1663 cbor.flags |= ix;
1174 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1664 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1175 XPUSHs (scalar); 1665 XPUSHs (scalar);
1176} 1666}
1177 1667
1178void decode_cbor (SV *cborstr) 1668void decode_cbor (SV *cborstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines