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.71 by root, Fri Mar 19 17:30:27 2021 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_inc_NN
27# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
28#endif
24#ifndef SvREFCNT_dec_NN 29#ifndef SvREFCNT_dec_NN
25# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 30# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
26#endif 31#endif
27 32
28// known major and minor types 33// known major and minor types
94 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 99 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
95 100
96 CBOR_TAG_MAGIC = 55799, // self-describe cbor 101 CBOR_TAG_MAGIC = 55799, // self-describe cbor
97}; 102};
98 103
104// known forced types, also hardcoded in CBOR.pm
105enum
106{
107 AS_CBOR = 0,
108 AS_INT = 1,
109 AS_BYTES = 2,
110 AS_TEXT = 3,
111 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6,
114 AS_MAP = 7,
115 // possibly future enhancements: (generic) float, (generic) string
116};
117
99#define F_SHRINK 0x00000001UL 118#define F_SHRINK 0x00000001UL
100#define F_ALLOW_UNKNOWN 0x00000002UL 119#define F_ALLOW_UNKNOWN 0x00000002UL
101#define F_ALLOW_SHARING 0x00000004UL 120#define F_ALLOW_SHARING 0x00000004UL
121#define F_ALLOW_CYCLES 0x00000008UL
122#define F_FORBID_OBJECTS 0x00000010UL
102#define F_PACK_STRINGS 0x00000008UL 123#define F_PACK_STRINGS 0x00000020UL
124#define F_TEXT_KEYS 0x00000040UL
125#define F_TEXT_STRINGS 0x00000080UL
126#define F_VALIDATE_UTF8 0x00000100UL
103 127
104#define INIT_SIZE 32 // initial scalar size to be allocated 128#define INIT_SIZE 32 // initial scalar size to be allocated
105 129
106#define SB do { 130#define SB do {
107#define SE } while (0) 131#define SE } while (0)
126typedef struct { 150typedef struct {
127 U32 flags; 151 U32 flags;
128 U32 max_depth; 152 U32 max_depth;
129 STRLEN max_size; 153 STRLEN max_size;
130 SV *filter; 154 SV *filter;
155
156 // for the incremental parser
157 STRLEN incr_pos; // the current offset into the text
158 STRLEN incr_need; // minimum bytes needed to decode
159 AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef.
131} CBOR; 160} CBOR;
132 161
133ecb_inline void 162ecb_inline void
134cbor_init (CBOR *cbor) 163cbor_init (CBOR *cbor)
135{ 164{
139 168
140ecb_inline void 169ecb_inline void
141cbor_free (CBOR *cbor) 170cbor_free (CBOR *cbor)
142{ 171{
143 SvREFCNT_dec (cbor->filter); 172 SvREFCNT_dec (cbor->filter);
173 SvREFCNT_dec (cbor->incr_count);
144} 174}
145 175
146///////////////////////////////////////////////////////////////////////////// 176/////////////////////////////////////////////////////////////////////////////
147// utility functions 177// utility functions
148 178
171#endif 201#endif
172 } 202 }
173} 203}
174 204
175// minimum length of a string to be registered for stringref 205// minimum length of a string to be registered for stringref
176ecb_inline int 206ecb_inline STRLEN
177minimum_string_length (UV idx) 207minimum_string_length (UV idx)
178{ 208{
179 return idx > 23 209 return idx <= 23 ? 3
180 ? idx > 0xffU 210 : idx <= 0xffU ? 4
181 ? idx > 0xffffU 211 : idx <= 0xffffU ? 5
182 ? idx > 0xffffffffU 212 : idx <= 0xffffffffU ? 7
183 ? 11 213 : 11;
184 : 7
185 : 5
186 : 4
187 : 3;
188} 214}
189 215
190///////////////////////////////////////////////////////////////////////////// 216/////////////////////////////////////////////////////////////////////////////
191// encoder 217// encoder
192 218
205} enc_t; 231} enc_t;
206 232
207ecb_inline void 233ecb_inline void
208need (enc_t *enc, STRLEN len) 234need (enc_t *enc, STRLEN len)
209{ 235{
210 if (ecb_expect_false (enc->cur + len >= enc->end)) 236 if (ecb_expect_false ((uintptr_t)(enc->end - enc->cur) < len))
211 { 237 {
212 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 238 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
213 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 239 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
214 enc->cur = SvPVX (enc->sv) + cur; 240 enc->cur = SvPVX (enc->sv) + cur;
215 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 241 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
216 } 242 }
217} 243}
218 244
245static void encode_sv (enc_t *enc, SV *sv);
246
219ecb_inline void 247ecb_inline void
220encode_ch (enc_t *enc, char ch) 248encode_ch (enc_t *enc, char ch)
221{ 249{
222 need (enc, 1); 250 need (enc, 1);
223 *enc->cur++ = ch; 251 *enc->cur++ = ch;
224} 252}
225 253
254// used for tags, intregers, element counts and so on
226static void 255static void
227encode_uint (enc_t *enc, int major, UV len) 256encode_uint (enc_t *enc, int major, UV len)
228{ 257{
229 need (enc, 9); 258 need (enc, 9);
230 259
231 if (ecb_expect_true (len < LENGTH_EXT1)) 260 if (ecb_expect_true (len < LENGTH_EXT1))
232 *enc->cur++ = major | len; 261 *enc->cur++ = major | len;
233 else if (ecb_expect_true (len <= 0xff)) 262 else if (ecb_expect_true (len <= 0xffU))
234 { 263 {
235 *enc->cur++ = major | LENGTH_EXT1; 264 *enc->cur++ = major | LENGTH_EXT1;
236 *enc->cur++ = len; 265 *enc->cur++ = len;
237 } 266 }
238 else if (len <= 0xffff) 267 else if (len <= 0xffffU)
239 { 268 {
240 *enc->cur++ = major | LENGTH_EXT2; 269 *enc->cur++ = major | LENGTH_EXT2;
241 *enc->cur++ = len >> 8; 270 *enc->cur++ = len >> 8;
242 *enc->cur++ = len; 271 *enc->cur++ = len;
243 } 272 }
244 else if (len <= 0xffffffff) 273 else if (len <= 0xffffffffU)
245 { 274 {
246 *enc->cur++ = major | LENGTH_EXT4; 275 *enc->cur++ = major | LENGTH_EXT4;
247 *enc->cur++ = len >> 24; 276 *enc->cur++ = len >> 24;
248 *enc->cur++ = len >> 16; 277 *enc->cur++ = len >> 16;
249 *enc->cur++ = len >> 8; 278 *enc->cur++ = len >> 8;
261 *enc->cur++ = len >> 8; 290 *enc->cur++ = len >> 8;
262 *enc->cur++ = len; 291 *enc->cur++ = len;
263 } 292 }
264} 293}
265 294
295// encodes a perl value into a CBOR integer
296ecb_inline void
297encode_int (enc_t *enc, SV *sv)
298{
299 if (SvIsUV (sv))
300 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
301 else if (SvIVX (sv) >= 0)
302 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
303 else
304 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
305}
306
266ecb_inline void 307ecb_inline void
267encode_tag (enc_t *enc, UV tag) 308encode_tag (enc_t *enc, UV tag)
268{ 309{
269 encode_uint (enc, MAJOR_TAG, tag); 310 encode_uint (enc, MAJOR_TAG, tag);
270} 311}
271 312
313// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
314ecb_noinline static void
315encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
316{
317 STRLEN ulen = len;
318 U8 *p, *pend = (U8 *)str + len;
319
320 for (p = (U8 *)str; p < pend; ++p)
321 ulen += *p >> 7; // count set high bits
322
323 encode_uint (enc, MAJOR_TEXT, ulen);
324
325 need (enc, ulen);
326 for (p = (U8 *)str; p < pend; ++p)
327 if (*p < 0x80)
328 *enc->cur++ = *p;
329 else
330 {
331 *enc->cur++ = 0xc0 + (*p >> 6);
332 *enc->cur++ = 0x80 + (*p & 63);
333 }
334}
335
272ecb_inline void 336ecb_inline void
273encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 337encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
274{ 338{
339 if (ecb_expect_false (upgrade_utf8))
340 if (!utf8)
341 {
342 encode_str_utf8 (enc, utf8, str, len);
343 return;
344 }
345
275 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 346 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
276 need (enc, len); 347 need (enc, len);
277 memcpy (enc->cur, str, len); 348 memcpy (enc->cur, str, len);
278 enc->cur += len; 349 enc->cur += len;
279} 350}
280 351
281static void 352ecb_inline void
282encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 353encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 354{
284 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 355 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
285 { 356 {
286 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 357 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
287 358
298 sv_setuv (*svp, enc->stringref_idx); 369 sv_setuv (*svp, enc->stringref_idx);
299 ++enc->stringref_idx; 370 ++enc->stringref_idx;
300 } 371 }
301 } 372 }
302 373
303 encode_str (enc, utf8, str, len); 374 encode_str (enc, upgrade_utf8, utf8, str, len);
304} 375}
305 376
306static void encode_sv (enc_t *enc, SV *sv); 377ecb_inline void
378encode_float16 (enc_t *enc, NV nv)
379{
380 need (enc, 1+2);
381
382 *enc->cur++ = MAJOR_MISC | MISC_FLOAT16;
383
384 uint16_t fp = ecb_float_to_binary16 (nv);
385
386 if (!ecb_big_endian ())
387 fp = ecb_bswap16 (fp);
388
389 memcpy (enc->cur, &fp, 2);
390 enc->cur += 2;
391}
392
393ecb_inline void
394encode_float32 (enc_t *enc, NV nv)
395{
396 need (enc, 1+4);
397
398 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
399
400 uint32_t fp = ecb_float_to_binary32 (nv);
401
402 if (!ecb_big_endian ())
403 fp = ecb_bswap32 (fp);
404
405 memcpy (enc->cur, &fp, 4);
406 enc->cur += 4;
407}
408
409ecb_inline void
410encode_float64 (enc_t *enc, NV nv)
411{
412 need (enc, 1+8);
413
414 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
415
416 uint64_t fp = ecb_double_to_binary64 (nv);
417
418 if (!ecb_big_endian ())
419 fp = ecb_bswap64 (fp);
420
421 memcpy (enc->cur, &fp, 8);
422 enc->cur += 8;
423}
424
425ecb_inline void
426encode_bool (enc_t *enc, int istrue)
427{
428 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
429}
430
431// encodes an arrayref containing key-value pairs as CBOR map
432ecb_inline void
433encode_array_as_map (enc_t *enc, SV *sv)
434{
435 if (enc->depth >= enc->cbor.max_depth)
436 croak (ERR_NESTING_EXCEEDED);
437
438 ++enc->depth;
439
440 // as_map does error checking for us, but we re-check in case
441 // things have changed.
442
443 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
444 croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)");
445
446 AV *av = (AV *)SvRV (sv);
447 int i, len = av_len (av);
448
449 if (!(len & 1))
450 croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)");
451
452 encode_uint (enc, MAJOR_MAP, (len + 1) >> 1);
453
454 for (i = 0; i <= len; ++i)
455 {
456 SV **svp = av_fetch (av, i, 0);
457 encode_sv (enc, svp ? *svp : &PL_sv_undef);
458 }
459
460 --enc->depth;
461}
462
463ecb_inline void
464encode_forced (enc_t *enc, UV type, SV *sv)
465{
466 switch (type)
467 {
468 case AS_CBOR:
469 {
470 STRLEN len;
471 char *str = SvPVbyte (sv, len);
472
473 need (enc, len);
474 memcpy (enc->cur, str, len);
475 enc->cur += len;
476 }
477 break;
478
479 case AS_BYTES:
480 {
481 STRLEN len;
482 char *str = SvPVbyte (sv, len);
483 encode_strref (enc, 0, 0, str, len);
484 }
485 break;
486
487 case AS_TEXT:
488 {
489 STRLEN len;
490 char *str = SvPVutf8 (sv, len);
491 encode_strref (enc, 1, 1, str, len);
492 }
493 break;
494
495 case AS_INT: encode_int (enc, sv); break;
496
497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
503 default:
504 croak ("encountered malformed CBOR::XS::Tagged object");
505 }
506}
307 507
308static void 508static void
309encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
310{ 510{
311 int i, len = av_len (av); 511 int i, len = av_len (av);
315 515
316 ++enc->depth; 516 ++enc->depth;
317 517
318 encode_uint (enc, MAJOR_ARRAY, len + 1); 518 encode_uint (enc, MAJOR_ARRAY, len + 1);
319 519
520 if (ecb_expect_false (SvMAGICAL (av)))
320 for (i = 0; i <= len; ++i) 521 for (i = 0; i <= len; ++i)
321 { 522 {
322 SV **svp = av_fetch (av, i, 0); 523 SV **svp = av_fetch (av, i, 0);
323 encode_sv (enc, svp ? *svp : &PL_sv_undef); 524 encode_sv (enc, svp ? *svp : &PL_sv_undef);
324 } 525 }
526 else
527 for (i = 0; i <= len; ++i)
528 {
529 SV *sv = AvARRAY (av)[i];
530 encode_sv (enc, sv ? sv : &PL_sv_undef);
531 }
325 532
326 --enc->depth; 533 --enc->depth;
327} 534}
328 535
329static void 536static void
337 ++enc->depth; 544 ++enc->depth;
338 545
339 int pairs = hv_iterinit (hv); 546 int pairs = hv_iterinit (hv);
340 int mg = SvMAGICAL (hv); 547 int mg = SvMAGICAL (hv);
341 548
342 if (mg) 549 if (ecb_expect_false (mg))
343 encode_ch (enc, MAJOR_MAP | MINOR_INDEF); 550 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
344 else 551 else
345 encode_uint (enc, MAJOR_MAP, pairs); 552 encode_uint (enc, MAJOR_MAP, pairs);
346 553
347 while ((he = hv_iternext (hv))) 554 while ((he = hv_iternext (hv)))
348 { 555 {
349 if (HeKLEN (he) == HEf_SVKEY) 556 if (HeKLEN (he) == HEf_SVKEY)
350 encode_sv (enc, HeSVKEY (he)); 557 encode_sv (enc, HeSVKEY (he));
351 else 558 else
352 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 559 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
353 560
354 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 561 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
355 } 562 }
356 563
357 if (mg) 564 if (ecb_expect_false (mg))
358 encode_ch (enc, MAJOR_MISC | MINOR_INDEF); 565 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
359 566
360 --enc->depth; 567 --enc->depth;
361} 568}
362 569
382 589
383 HV *stash = SvSTASH (sv); 590 HV *stash = SvSTASH (sv);
384 591
385 if (stash == boolean_stash) 592 if (stash == boolean_stash)
386 { 593 {
387 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 594 encode_bool (enc, SvIV (sv));
388 return; 595 return;
389 } 596 }
390 else if (stash == error_stash) 597 else if (stash == error_stash)
391 { 598 {
392 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 599 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
395 else if (stash == tagged_stash) 602 else if (stash == tagged_stash)
396 { 603 {
397 if (svt != SVt_PVAV) 604 if (svt != SVt_PVAV)
398 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 605 croak ("encountered CBOR::XS::Tagged object that isn't an array");
399 606
607 switch (av_len ((AV *)sv))
608 {
609 case 2-1:
610 // actually a tagged value
400 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); 611 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
401 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 612 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
613 break;
614
615 case 3-1:
616 // a forced type [value, type, undef]
617 encode_forced (enc, SvUV (*av_fetch ((AV *)sv, 1, 1)), *av_fetch ((AV *)sv, 0, 1));
618 break;
619
620 default:
621 croak ("encountered malformed CBOR::XS::Tagged object");
622 }
402 623
403 return; 624 return;
404 } 625 }
405 } 626 }
406 627
407 if (ecb_expect_false (SvREFCNT (sv) > 1) 628 if (ecb_expect_false (SvREFCNT (sv) > 1)
408 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) 629 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
409 { 630 {
410 if (!enc->shareable) 631 if (ecb_expect_false (!enc->shareable))
411 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); 632 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
412 633
413 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); 634 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
414 635
415 if (SvOK (*svp)) 636 if (SvOK (*svp))
429 if (ecb_expect_false (SvOBJECT (sv))) 650 if (ecb_expect_false (SvOBJECT (sv)))
430 { 651 {
431 HV *stash = SvSTASH (sv); 652 HV *stash = SvSTASH (sv);
432 GV *method; 653 GV *method;
433 654
655 if (enc->cbor.flags & F_FORBID_OBJECTS)
656 croak ("encountered object '%s', but forbid_objects is enabled",
657 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
434 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 658 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
435 { 659 {
436 dSP; 660 dSP;
437 661
438 ENTER; SAVETMPS; PUSHMARK (SP); 662 ENTER; SAVETMPS;
663 PUSHMARK (SP);
439 // we re-bless the reference to get overload and other niceties right 664 // we re-bless the reference to get overload and other niceties right
440 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 665 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
441 666
442 PUTBACK; 667 PUTBACK;
443 // G_SCALAR ensures that return value is 1 668 // G_SCALAR ensures that return value is 1
456 } 681 }
457 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) 682 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
458 { 683 {
459 dSP; 684 dSP;
460 685
461 ENTER; SAVETMPS; PUSHMARK (SP); 686 ENTER; SAVETMPS;
687 PUSHMARK (SP);
462 EXTEND (SP, 2); 688 EXTEND (SP, 2);
463 // we re-bless the reference to get overload and other niceties right 689 // we re-bless the reference to get overload and other niceties right
464 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); 690 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
465 PUSHs (sv_cbor); 691 PUSHs (sv_cbor);
466 692
472 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 698 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
473 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 699 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
474 700
475 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 701 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
476 encode_uint (enc, MAJOR_ARRAY, count + 1); 702 encode_uint (enc, MAJOR_ARRAY, count + 1);
477 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 703 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
478 704
479 while (count) 705 {
706 int i;
707
708 for (i = 0; i < count; ++i)
480 encode_sv (enc, SP[1 - count--]); 709 encode_sv (enc, SP[i + 1 - count]);
710
711 SP -= count;
712 }
481 713
482 PUTBACK; 714 PUTBACK;
483 715
484 FREETMPS; LEAVE; 716 FREETMPS; LEAVE;
485 } 717 }
507 739
508 if (ecb_expect_false (nv == (NV)(U32)nv)) 740 if (ecb_expect_false (nv == (NV)(U32)nv))
509 encode_uint (enc, MAJOR_POS_INT, (U32)nv); 741 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
510 //TODO: maybe I32? 742 //TODO: maybe I32?
511 else if (ecb_expect_false (nv == (float)nv)) 743 else if (ecb_expect_false (nv == (float)nv))
512 { 744 encode_float32 (enc, nv);
513 uint32_t fp = ecb_float_to_binary32 (nv);
514
515 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
516
517 if (!ecb_big_endian ())
518 fp = ecb_bswap32 (fp);
519
520 memcpy (enc->cur, &fp, 4);
521 enc->cur += 4;
522 }
523 else 745 else
524 { 746 encode_float64 (enc, nv);
525 uint64_t fp = ecb_double_to_binary64 (nv);
526
527 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
528
529 if (!ecb_big_endian ())
530 fp = ecb_bswap64 (fp);
531
532 memcpy (enc->cur, &fp, 8);
533 enc->cur += 8;
534 }
535} 747}
536 748
537static void 749static void
538encode_sv (enc_t *enc, SV *sv) 750encode_sv (enc_t *enc, SV *sv)
539{ 751{
541 753
542 if (SvPOKp (sv)) 754 if (SvPOKp (sv))
543 { 755 {
544 STRLEN len; 756 STRLEN len;
545 char *str = SvPV (sv, len); 757 char *str = SvPV (sv, len);
546 encode_strref (enc, SvUTF8 (sv), str, len); 758 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
547 } 759 }
548 else if (SvNOKp (sv)) 760 else if (SvNOKp (sv))
549 encode_nv (enc, sv); 761 encode_nv (enc, sv);
550 else if (SvIOKp (sv)) 762 else if (SvIOKp (sv))
551 { 763 encode_int (enc, sv);
552 if (SvIsUV (sv))
553 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
554 else if (SvIVX (sv) >= 0)
555 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
556 else
557 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
558 }
559 else if (SvROK (sv)) 764 else if (SvROK (sv))
560 encode_rv (enc, SvRV (sv)); 765 encode_rv (enc, SvRV (sv));
561 else if (!SvOK (sv)) 766 else if (!SvOK (sv))
562 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 767 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
563 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 768 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
568} 773}
569 774
570static SV * 775static SV *
571encode_cbor (SV *scalar, CBOR *cbor) 776encode_cbor (SV *scalar, CBOR *cbor)
572{ 777{
573 enc_t enc = { }; 778 enc_t enc = { 0 };
574 779
575 enc.cbor = *cbor; 780 enc.cbor = *cbor;
576 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 781 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
577 enc.cur = SvPVX (enc.sv); 782 enc.cur = SvPVX (enc.sv);
578 enc.end = SvEND (enc.sv); 783 enc.end = SvEND (enc.sv);
579 784
580 SvPOK_only (enc.sv); 785 SvPOK_only (enc.sv);
581 786
582 if (cbor->flags & F_PACK_STRINGS) 787 if (cbor->flags & F_PACK_STRINGS)
583 { 788 {
610 U32 depth; // recursion depth 815 U32 depth; // recursion depth
611 U32 maxdepth; // recursion depth limit 816 U32 maxdepth; // recursion depth limit
612 AV *shareable; 817 AV *shareable;
613 AV *stringref; 818 AV *stringref;
614 SV *decode_tagged; 819 SV *decode_tagged;
820 SV *err_sv; // optional sv for error, needs to be freed
615} dec_t; 821} dec_t;
616 822
617#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 823// set dec->err to ERRSV
824ecb_cold static void
825err_errsv (dec_t *dec)
826{
827 if (!dec->err)
828 {
829 dec->err_sv = newSVsv (ERRSV);
618 830
619#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") 831 // chop off the trailing \n
832 SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1);
833 *SvEND (dec->err_sv) = 0;
620 834
835 dec->err = SvPVutf8_nolen (dec->err_sv);
836 }
837}
838
839// the following functions are used to reduce code size and help the compiler to optimise
840ecb_cold static void
841err_set (dec_t *dec, const char *reason)
842{
843 if (!dec->err)
844 dec->err = reason;
845}
846
847ecb_cold static void
848err_unexpected_end (dec_t *dec)
849{
850 err_set (dec, "unexpected end of CBOR data");
851}
852
853#define ERR_DO(do) SB do; goto fail; SE
854#define ERR(reason) ERR_DO (err_set (dec, reason))
855#define ERR_ERRSV ERR_DO (err_errsv (dec))
856
857#define WANT(len) if (ecb_expect_false ((uintptr_t)(dec->end - dec->cur) < (STRLEN)len)) ERR_DO (err_unexpected_end (dec))
858
621#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 859#define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED)
622#define DEC_DEC_DEPTH --dec->depth 860#define DEC_DEC_DEPTH --dec->depth
623 861
624static UV 862static UV
625decode_uint (dec_t *dec) 863decode_uint (dec_t *dec)
626{ 864{
627 U8 m = *dec->cur & MINOR_MASK; 865 U8 m = *dec->cur & MINOR_MASK;
628 ++dec->cur; 866 ++dec->cur;
629 867
630 if (ecb_expect_true (m < LENGTH_EXT1)) 868 if (ecb_expect_true (m < LENGTH_EXT1))
631 return m; 869 return m;
632 870 else if (ecb_expect_true (m == LENGTH_EXT1))
633 switch (m)
634 { 871 {
635 case LENGTH_EXT1:
636 WANT (1); 872 WANT (1);
637 dec->cur += 1; 873 dec->cur += 1;
638 return dec->cur[-1]; 874 return dec->cur[-1];
639 875 }
640 case LENGTH_EXT2: 876 else if (ecb_expect_true (m == LENGTH_EXT2))
877 {
641 WANT (2); 878 WANT (2);
642 dec->cur += 2; 879 dec->cur += 2;
643 return (((UV)dec->cur[-2]) << 8) 880 return (((UV)dec->cur[-2]) << 8)
644 | ((UV)dec->cur[-1]); 881 | ((UV)dec->cur[-1]);
645 882 }
646 case LENGTH_EXT4: 883 else if (ecb_expect_true (m == LENGTH_EXT4))
884 {
647 WANT (4); 885 WANT (4);
648 dec->cur += 4; 886 dec->cur += 4;
649 return (((UV)dec->cur[-4]) << 24) 887 return (((UV)dec->cur[-4]) << 24)
650 | (((UV)dec->cur[-3]) << 16) 888 | (((UV)dec->cur[-3]) << 16)
651 | (((UV)dec->cur[-2]) << 8) 889 | (((UV)dec->cur[-2]) << 8)
652 | ((UV)dec->cur[-1]); 890 | ((UV)dec->cur[-1]);
653 891 }
654 case LENGTH_EXT8: 892 else if (ecb_expect_true (m == LENGTH_EXT8))
893 {
655 WANT (8); 894 WANT (8);
656 dec->cur += 8; 895 dec->cur += 8;
657 896
658 return 897 return
659#if UVSIZE < 8 898#if UVSIZE < 8
660 0 899 0
661#else 900#else
662 (((UV)dec->cur[-8]) << 56) 901 (((UV)dec->cur[-8]) << 56)
663 | (((UV)dec->cur[-7]) << 48) 902 | (((UV)dec->cur[-7]) << 48)
664 | (((UV)dec->cur[-6]) << 40) 903 | (((UV)dec->cur[-6]) << 40)
665 | (((UV)dec->cur[-5]) << 32) 904 | (((UV)dec->cur[-5]) << 32)
666#endif 905#endif
667 | (((UV)dec->cur[-4]) << 24) 906 | (((UV)dec->cur[-4]) << 24)
668 | (((UV)dec->cur[-3]) << 16) 907 | (((UV)dec->cur[-3]) << 16)
669 | (((UV)dec->cur[-2]) << 8) 908 | (((UV)dec->cur[-2]) << 8)
670 | ((UV)dec->cur[-1]); 909 | ((UV)dec->cur[-1]);
671 910 }
672 default: 911 else
673 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 912 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
674 }
675 913
676fail: 914fail:
677 return 0; 915 return 0;
678} 916}
679 917
692 930
693 for (;;) 931 for (;;)
694 { 932 {
695 WANT (1); 933 WANT (1);
696 934
697 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 935 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
698 { 936 {
699 ++dec->cur; 937 ++dec->cur;
700 break; 938 break;
701 } 939 }
702 940
703 av_push (av, decode_sv (dec)); 941 av_push (av, decode_sv (dec));
704 } 942 }
705 } 943 }
706 else 944 else
707 { 945 {
708 int i, len = decode_uint (dec); 946 UV i, len = decode_uint (dec);
709 947
948 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); 949 av_fill (av, len - 1);
711 950
712 for (i = 0; i < len; ++i) 951 for (i = 0; i < len; ++i)
713 AvARRAY (av)[i] = decode_sv (dec); 952 AvARRAY (av)[i] = decode_sv (dec);
714 } 953 }
715 954
716 DEC_DEC_DEPTH; 955 DEC_DEC_DEPTH;
717 return newRV_noinc ((SV *)av); 956 return newRV_noinc ((SV *)av);
718 957
719fail: 958fail:
720 SvREFCNT_dec (av); 959 SvREFCNT_dec_NN (av);
721 DEC_DEC_DEPTH; 960 DEC_DEC_DEPTH;
722 return &PL_sv_undef; 961 return &PL_sv_undef;
723} 962}
724 963
725static void 964static void
727{ 966{
728 // for speed reasons, we specialcase single-string 967 // for speed reasons, we specialcase single-string
729 // byte or utf-8 strings as keys, but only when !stringref 968 // byte or utf-8 strings as keys, but only when !stringref
730 969
731 if (ecb_expect_true (!dec->stringref)) 970 if (ecb_expect_true (!dec->stringref))
732 if ((*dec->cur - MAJOR_BYTES) <= 27) 971 if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
733 { 972 {
734 I32 len = decode_uint (dec); 973 STRLEN len = decode_uint (dec);
735 char *key = (char *)dec->cur; 974 char *key = (char *)dec->cur;
736 975
976 WANT (len);
737 dec->cur += len; 977 dec->cur += len;
738 978
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); 979 hv_store (hv, key, len, decode_sv (dec), 0);
743 980
744 return; 981 return;
745 } 982 }
746 else if ((*dec->cur - MAJOR_TEXT) <= 27) 983 else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
747 { 984 {
748 I32 len = decode_uint (dec); 985 STRLEN len = decode_uint (dec);
749 char *key = (char *)dec->cur; 986 char *key = (char *)dec->cur;
750 987
988 WANT (len);
751 dec->cur += len; 989 dec->cur += len;
752 990
753 if (ecb_expect_false (dec->stringref)) 991 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
754 av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); 992 if (!is_utf8_string ((U8 *)key, len))
993 ERR ("corrupted CBOR data (invalid UTF-8 in map key)");
755 994
756 hv_store (hv, key, -len, decode_sv (dec), 0); 995 hv_store (hv, key, -len, decode_sv (dec), 0);
757 996
758 return; 997 return;
759 } 998 }
760 999
761 SV *k = decode_sv (dec); 1000 SV *k = decode_sv (dec);
762 SV *v = decode_sv (dec); 1001 SV *v = decode_sv (dec);
763 1002
1003 // we leak memory if uncaught exceptions are thrown by random magical
1004 // methods, and this is hopefully the only place where it can happen,
1005 // so if there is a chance of an exception, take the very slow path.
1006 // since catching exceptions is "undocumented/internal/forbidden" by
1007 // the new p5p powers, we need to call out to a perl function :/
1008 if (ecb_expect_false (SvAMAGIC (k)))
1009 {
1010 dSP;
1011
1012 ENTER; SAVETMPS;
1013 PUSHMARK (SP);
1014 EXTEND (SP, 3);
1015 PUSHs (sv_2mortal (newRV_inc ((SV *)hv)));
1016 PUSHs (sv_2mortal (k));
1017 PUSHs (sv_2mortal (v));
1018
1019 PUTBACK;
1020 call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL);
1021 SPAGAIN;
1022
1023 FREETMPS; LEAVE;
1024
1025 if (SvTRUE (ERRSV))
1026 ERR_ERRSV;
1027
1028 return;
1029 }
1030
764 hv_store_ent (hv, k, v, 0); 1031 hv_store_ent (hv, k, v, 0);
765 SvREFCNT_dec (k); 1032 SvREFCNT_dec_NN (k);
1033
1034fail:
1035 ;
766} 1036}
767 1037
768static SV * 1038static SV *
769decode_hv (dec_t *dec) 1039decode_hv (dec_t *dec)
770{ 1040{
778 1048
779 for (;;) 1049 for (;;)
780 { 1050 {
781 WANT (1); 1051 WANT (1);
782 1052
783 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) 1053 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF) || dec->err)
784 { 1054 {
785 ++dec->cur; 1055 ++dec->cur;
786 break; 1056 break;
787 } 1057 }
788 1058
789 decode_he (dec, hv); 1059 decode_he (dec, hv);
790 } 1060 }
791 } 1061 }
792 else 1062 else
793 { 1063 {
794 int pairs = decode_uint (dec); 1064 UV pairs = decode_uint (dec);
1065
1066 WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes
795 1067
796 while (pairs--) 1068 while (pairs--)
797 decode_he (dec, hv); 1069 decode_he (dec, hv);
798 } 1070 }
799 1071
800 DEC_DEC_DEPTH; 1072 DEC_DEC_DEPTH;
801 return newRV_noinc ((SV *)hv); 1073 return newRV_noinc ((SV *)hv);
802 1074
803fail: 1075fail:
804 SvREFCNT_dec (hv); 1076 SvREFCNT_dec_NN (hv);
805 DEC_DEC_DEPTH; 1077 DEC_DEC_DEPTH;
806 return &PL_sv_undef; 1078 return &PL_sv_undef;
807} 1079}
808 1080
809static SV * 1081static SV *
810decode_str (dec_t *dec, int utf8) 1082decode_str (dec_t *dec, int utf8)
811{ 1083{
812 SV *sv = 0; 1084 SV *sv = 0;
813 1085
814 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) 1086 if (ecb_expect_false ((*dec->cur & MINOR_MASK) == MINOR_INDEF))
815 { 1087 {
816 // indefinite length strings 1088 // indefinite length strings
817 ++dec->cur; 1089 ++dec->cur;
818 1090
819 U8 major = *dec->cur & MAJOR_MISC; 1091 U8 major = *dec->cur & MAJOR_MISC;
852 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) 1124 && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1))
853 av_push (dec->stringref, SvREFCNT_inc_NN (sv)); 1125 av_push (dec->stringref, SvREFCNT_inc_NN (sv));
854 } 1126 }
855 1127
856 if (utf8) 1128 if (utf8)
1129 {
1130 if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8))
1131 if (!is_utf8_string (SvPVX (sv), SvCUR (sv)))
1132 ERR ("corrupted CBOR data (invalid UTF-8 in text string)");
1133
857 SvUTF8_on (sv); 1134 SvUTF8_on (sv);
1135 }
858 1136
859 return sv; 1137 return sv;
860 1138
861fail: 1139fail:
862 SvREFCNT_dec (sv); 1140 SvREFCNT_dec (sv);
881 sv = newRV_noinc (decode_sv (dec)); 1159 sv = newRV_noinc (decode_sv (dec));
882 break; 1160 break;
883 1161
884 case CBOR_TAG_STRINGREF_NAMESPACE: 1162 case CBOR_TAG_STRINGREF_NAMESPACE:
885 { 1163 {
1164 // do not use SAVETMPS/FREETMPS, as these will
1165 // erase mortalised caches, e.g. "shareable"
886 ENTER; SAVETMPS; 1166 ENTER;
887 1167
888 SAVESPTR (dec->stringref); 1168 SAVESPTR (dec->stringref);
889 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 1169 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
890 1170
891 sv = decode_sv (dec); 1171 sv = decode_sv (dec);
892 1172
893 FREETMPS; LEAVE; 1173 LEAVE;
894 } 1174 }
895 break; 1175 break;
896 1176
897 case CBOR_TAG_STRINGREF: 1177 case CBOR_TAG_STRINGREF:
898 { 1178 {
899 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1179 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
900 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 1180 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
901 1181
902 UV idx = decode_uint (dec); 1182 UV idx = decode_uint (dec);
903 1183
904 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 1184 if (!dec->stringref || idx >= (UV)(1 + AvFILLp (dec->stringref)))
905 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); 1185 ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)");
906 1186
907 sv = newSVsv (AvARRAY (dec->stringref)[idx]); 1187 sv = newSVsv (AvARRAY (dec->stringref)[idx]);
908 } 1188 }
909 break; 1189 break;
911 case CBOR_TAG_VALUE_SHAREABLE: 1191 case CBOR_TAG_VALUE_SHAREABLE:
912 { 1192 {
913 if (ecb_expect_false (!dec->shareable)) 1193 if (ecb_expect_false (!dec->shareable))
914 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1194 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
915 1195
1196 if (dec->cbor.flags & F_ALLOW_CYCLES)
1197 {
916 sv = newSV (0); 1198 sv = newSV (0);
917 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1199 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
918 1200
919 SV *osv = decode_sv (dec); 1201 SV *osv = decode_sv (dec);
920 sv_setsv (sv, osv); 1202 sv_setsv (sv, osv);
921 SvREFCNT_dec_NN (osv); 1203 SvREFCNT_dec_NN (osv);
1204 }
1205 else
1206 {
1207 av_push (dec->shareable, &PL_sv_undef);
1208 int idx = AvFILLp (dec->shareable);
1209 sv = decode_sv (dec);
1210 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
1211 }
922 } 1212 }
923 break; 1213 break;
924 1214
925 case CBOR_TAG_VALUE_SHAREDREF: 1215 case CBOR_TAG_VALUE_SHAREDREF:
926 { 1216 {
927 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) 1217 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
928 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 1218 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
929 1219
930 UV idx = decode_uint (dec); 1220 UV idx = decode_uint (dec);
931 1221
932 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 1222 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
933 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1223 ERR ("corrupted CBOR data (sharedref index out of bounds)");
934 1224
935 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 1225 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
1226
1227 if (sv == &PL_sv_undef)
1228 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
936 } 1229 }
937 break; 1230 break;
938 1231
939 case CBOR_TAG_PERL_OBJECT: 1232 case CBOR_TAG_PERL_OBJECT:
940 { 1233 {
1234 if (dec->cbor.flags & F_FORBID_OBJECTS)
1235 goto filter;
1236
941 sv = decode_sv (dec); 1237 sv = decode_sv (dec);
942 1238
943 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) 1239 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
944 ERR ("corrupted CBOR data (non-array perl object)"); 1240 ERR ("corrupted CBOR data (non-array perl object)");
945 1241
955 if (!method) 1251 if (!method)
956 ERR ("cannot decode perl-object (package does not have a THAW method)"); 1252 ERR ("cannot decode perl-object (package does not have a THAW method)");
957 1253
958 dSP; 1254 dSP;
959 1255
960 ENTER; SAVETMPS; PUSHMARK (SP); 1256 ENTER; SAVETMPS;
1257 PUSHMARK (SP);
961 EXTEND (SP, len + 1); 1258 EXTEND (SP, len + 1);
962 // we re-bless the reference to get overload and other niceties right 1259 // we re-bless the reference to get overload and other niceties right
963 PUSHs (*av_fetch (av, 0, 1)); 1260 PUSHs (*av_fetch (av, 0, 1));
964 PUSHs (sv_cbor); 1261 PUSHs (sv_cbor);
965 1262
973 SPAGAIN; 1270 SPAGAIN;
974 1271
975 if (SvTRUE (ERRSV)) 1272 if (SvTRUE (ERRSV))
976 { 1273 {
977 FREETMPS; LEAVE; 1274 FREETMPS; LEAVE;
978 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1275 ERR_ERRSV;
979 } 1276 }
980 1277
981 SvREFCNT_dec (sv); 1278 SvREFCNT_dec_NN (sv);
982 sv = SvREFCNT_inc (POPs); 1279 sv = SvREFCNT_inc (POPs);
983 1280
984 PUTBACK; 1281 PUTBACK;
985 1282
986 FREETMPS; LEAVE; 1283 FREETMPS; LEAVE;
987 } 1284 }
988 break; 1285 break;
989 1286
990 default: 1287 default:
1288 filter:
991 { 1289 {
1290 SV *tag_sv = newSVuv (tag);
1291
992 sv = decode_sv (dec); 1292 sv = decode_sv (dec);
993 1293
994 dSP; 1294 dSP;
995 ENTER; SAVETMPS; PUSHMARK (SP); 1295 ENTER; SAVETMPS;
1296 PUSHMARK (SP);
996 EXTEND (SP, 2); 1297 EXTEND (SP, 2);
997 PUSHs (newSVuv (tag)); 1298 PUSHs (tag_sv);
998 PUSHs (sv); 1299 PUSHs (sv);
999 1300
1000 PUTBACK; 1301 PUTBACK;
1001 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); 1302 int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL);
1002 SPAGAIN; 1303 SPAGAIN;
1003 1304
1004 if (SvTRUE (ERRSV)) 1305 if (SvTRUE (ERRSV))
1005 { 1306 {
1307 SvREFCNT_dec_NN (tag_sv);
1006 FREETMPS; LEAVE; 1308 FREETMPS; LEAVE;
1007 ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); 1309 ERR_ERRSV;
1008 } 1310 }
1009 1311
1010 if (count) 1312 if (count)
1011 { 1313 {
1314 SvREFCNT_dec_NN (tag_sv);
1012 SvREFCNT_dec (sv); 1315 SvREFCNT_dec_NN (sv);
1013 sv = SvREFCNT_inc (POPs); 1316 sv = SvREFCNT_inc_NN (TOPs);
1317 SP -= count;
1014 } 1318 }
1015 else 1319 else
1016 { 1320 {
1017 AV *av = newAV (); 1321 AV *av = newAV ();
1018 av_push (av, newSVuv (tag)); 1322 av_push (av, tag_sv);
1019 av_push (av, sv); 1323 av_push (av, sv);
1020 1324
1021 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 1325 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
1022 ? cbor_tagged_stash 1326 ? cbor_tagged_stash
1023 : gv_stashpv ("CBOR::XS::Tagged" , 1); 1327 : gv_stashpv ("CBOR::XS::Tagged" , 1);
1109 1413
1110 return newSVnv (ecb_binary64_to_double (fp)); 1414 return newSVnv (ecb_binary64_to_double (fp));
1111 } 1415 }
1112 1416
1113 // 0..19 unassigned simple 1417 // 0..19 unassigned simple
1114 // 24 reserved + unassigned (reserved values are not encodable) 1418 // 24 reserved + unassigned simple (reserved values are not encodable)
1419 // 28-30 unassigned misc
1420 // 31 break code
1115 default: 1421 default:
1116 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1422 ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)");
1117 } 1423 }
1118 1424
1119 break; 1425 break;
1120 } 1426 }
1121 1427
1124} 1430}
1125 1431
1126static SV * 1432static SV *
1127decode_cbor (SV *string, CBOR *cbor, char **offset_return) 1433decode_cbor (SV *string, CBOR *cbor, char **offset_return)
1128{ 1434{
1129 dec_t dec = { }; 1435 dec_t dec = { 0 };
1130 SV *sv; 1436 SV *sv;
1131 STRLEN len; 1437 STRLEN len;
1132 char *data = SvPVbyte (string, len); 1438 char *data = SvPVbyte (string, len);
1133 1439
1134 if (len > cbor->max_size && cbor->max_size) 1440 if (len > cbor->max_size && cbor->max_size)
1148 if (dec.cur != dec.end && !dec.err) 1454 if (dec.cur != dec.end && !dec.err)
1149 dec.err = "garbage after CBOR object"; 1455 dec.err = "garbage after CBOR object";
1150 1456
1151 if (dec.err) 1457 if (dec.err)
1152 { 1458 {
1459 if (dec.shareable)
1460 {
1461 // need to break cyclic links, which would all be in shareable
1462 int i;
1463 SV **svp;
1464
1465 for (i = av_len (dec.shareable) + 1; i--; )
1466 if ((svp = av_fetch (dec.shareable, i, 0)))
1467 sv_setsv (*svp, &PL_sv_undef);
1468 }
1469
1153 SvREFCNT_dec (sv); 1470 SvREFCNT_dec_NN (sv);
1471
1472 if (dec.err_sv)
1473 sv_2mortal (dec.err_sv);
1474
1154 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); 1475 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
1155 } 1476 }
1156 1477
1157 sv = sv_2mortal (sv); 1478 sv = sv_2mortal (sv);
1158 1479
1159 return sv; 1480 return sv;
1160} 1481}
1161 1482
1483/////////////////////////////////////////////////////////////////////////////
1484// incremental parser
1485
1486#define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0)
1487
1488// returns 0 for notyet, 1 for success or error
1489static int
1490incr_parse (CBOR *self, SV *cborstr)
1491{
1492 STRLEN cur;
1493 SvPV (cborstr, cur);
1494
1495 while (ecb_expect_true (self->incr_need <= cur))
1496 {
1497 // table of integer count bytes
1498 static I8 incr_len[MINOR_MASK + 1] = {
1499 0, 0, 0, 0, 0, 0, 0, 0,
1500 0, 0, 0, 0, 0, 0, 0, 0,
1501 0, 0, 0, 0, 0, 0, 0, 0,
1502 1, 2, 4, 8,-1,-1,-1,-2
1503 };
1504
1505 const U8 *p = SvPVX (cborstr) + self->incr_pos;
1506 U8 m = *p & MINOR_MASK;
1507 IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1508 I8 ilen = incr_len[m];
1509
1510 self->incr_need = self->incr_pos + 1;
1511
1512 if (ecb_expect_false (ilen < 0))
1513 {
1514 if (m != MINOR_INDEF)
1515 return 1; // error
1516
1517 if (*p == (MAJOR_MISC | MINOR_INDEF))
1518 {
1519 if (count >= 0)
1520 return 1; // error
1521
1522 count = 1;
1523 }
1524 else
1525 {
1526 av_push (self->incr_count, newSViv (-1)); //TODO: nest
1527 count = -1;
1528 }
1529 }
1530 else
1531 {
1532 self->incr_need += ilen;
1533 if (ecb_expect_false (self->incr_need > cur))
1534 return 0;
1535
1536 int major = *p >> MAJOR_SHIFT;
1537
1538 switch (major)
1539 {
1540 case MAJOR_TAG >> MAJOR_SHIFT:
1541 ++count; // tags merely prefix another value
1542 break;
1543
1544 case MAJOR_BYTES >> MAJOR_SHIFT:
1545 case MAJOR_TEXT >> MAJOR_SHIFT:
1546 case MAJOR_ARRAY >> MAJOR_SHIFT:
1547 case MAJOR_MAP >> MAJOR_SHIFT:
1548 {
1549 UV len;
1550
1551 if (ecb_expect_false (ilen))
1552 {
1553 len = 0;
1554
1555 do {
1556 len = (len << 8) | *++p;
1557 } while (--ilen);
1558 }
1559 else
1560 len = m;
1561
1562 switch (major)
1563 {
1564 case MAJOR_BYTES >> MAJOR_SHIFT:
1565 case MAJOR_TEXT >> MAJOR_SHIFT:
1566 self->incr_need += len;
1567 if (ecb_expect_false (self->incr_need > cur))
1568 return 0;
1569
1570 break;
1571
1572 case MAJOR_MAP >> MAJOR_SHIFT:
1573 len <<= 1;
1574 /* FALLTHROUGH */
1575 case MAJOR_ARRAY >> MAJOR_SHIFT:
1576 if (len)
1577 {
1578 av_push (self->incr_count, newSViv (len + 1)); //TODO: nest
1579 count = len + 1;
1580 }
1581 break;
1582 }
1583 }
1584 }
1585 }
1586
1587 self->incr_pos = self->incr_need;
1588
1589 if (count > 0)
1590 {
1591 while (!--count)
1592 {
1593 if (!AvFILLp (self->incr_count))
1594 return 1; // done
1595
1596 SvREFCNT_dec_NN (av_pop (self->incr_count));
1597 count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]);
1598 }
1599
1600 SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count;
1601 }
1602 }
1603
1604 return 0;
1605}
1606
1607
1162///////////////////////////////////////////////////////////////////////////// 1608/////////////////////////////////////////////////////////////////////////////
1163// XS interface functions 1609// XS interface functions
1164 1610
1165MODULE = CBOR::XS PACKAGE = CBOR::XS 1611MODULE = CBOR::XS PACKAGE = CBOR::XS
1166 1612
1178 1624
1179 default_filter = newSVpv ("CBOR::XS::default_filter", 0); 1625 default_filter = newSVpv ("CBOR::XS::default_filter", 0);
1180 1626
1181 sv_cbor = newSVpv ("CBOR", 0); 1627 sv_cbor = newSVpv ("CBOR", 0);
1182 SvREADONLY_on (sv_cbor); 1628 SvREADONLY_on (sv_cbor);
1629
1630 assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1));
1183} 1631}
1184 1632
1185PROTOTYPES: DISABLE 1633PROTOTYPES: DISABLE
1186 1634
1187void CLONE (...) 1635void CLONE (...)
1206void shrink (CBOR *self, int enable = 1) 1654void shrink (CBOR *self, int enable = 1)
1207 ALIAS: 1655 ALIAS:
1208 shrink = F_SHRINK 1656 shrink = F_SHRINK
1209 allow_unknown = F_ALLOW_UNKNOWN 1657 allow_unknown = F_ALLOW_UNKNOWN
1210 allow_sharing = F_ALLOW_SHARING 1658 allow_sharing = F_ALLOW_SHARING
1659 allow_cycles = F_ALLOW_CYCLES
1660 forbid_objects = F_FORBID_OBJECTS
1211 pack_strings = F_PACK_STRINGS 1661 pack_strings = F_PACK_STRINGS
1662 text_keys = F_TEXT_KEYS
1663 text_strings = F_TEXT_STRINGS
1664 validate_utf8 = F_VALIDATE_UTF8
1212 PPCODE: 1665 PPCODE:
1213{ 1666{
1214 if (enable) 1667 if (enable)
1215 self->flags |= ix; 1668 self->flags |= ix;
1216 else 1669 else
1222void get_shrink (CBOR *self) 1675void get_shrink (CBOR *self)
1223 ALIAS: 1676 ALIAS:
1224 get_shrink = F_SHRINK 1677 get_shrink = F_SHRINK
1225 get_allow_unknown = F_ALLOW_UNKNOWN 1678 get_allow_unknown = F_ALLOW_UNKNOWN
1226 get_allow_sharing = F_ALLOW_SHARING 1679 get_allow_sharing = F_ALLOW_SHARING
1680 get_allow_cycles = F_ALLOW_CYCLES
1681 get_forbid_objects = F_FORBID_OBJECTS
1227 get_pack_strings = F_PACK_STRINGS 1682 get_pack_strings = F_PACK_STRINGS
1683 get_text_keys = F_TEXT_KEYS
1684 get_text_strings = F_TEXT_STRINGS
1685 get_validate_utf8 = F_VALIDATE_UTF8
1228 PPCODE: 1686 PPCODE:
1229 XPUSHs (boolSV (self->flags & ix)); 1687 XPUSHs (boolSV (self->flags & ix));
1230 1688
1231void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1689void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1232 PPCODE: 1690 PPCODE:
1281 EXTEND (SP, 2); 1739 EXTEND (SP, 2);
1282 PUSHs (sv); 1740 PUSHs (sv);
1283 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1741 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1284} 1742}
1285 1743
1744void incr_parse (CBOR *self, SV *cborstr)
1745 ALIAS:
1746 incr_parse_multiple = 1
1747 PPCODE:
1748{
1749 if (SvUTF8 (cborstr))
1750 sv_utf8_downgrade (cborstr, 0);
1751
1752 if (!self->incr_count)
1753 {
1754 self->incr_count = newAV ();
1755 self->incr_pos = 0;
1756 self->incr_need = 1;
1757
1758 av_push (self->incr_count, newSViv (1));
1759 }
1760
1761 do
1762 {
1763 if (!incr_parse (self, cborstr))
1764 {
1765 if (self->incr_need > self->max_size && self->max_size)
1766 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
1767 (unsigned long)self->incr_need, (unsigned long)self->max_size);
1768
1769 break;
1770 }
1771
1772 SV *sv;
1773 char *offset;
1774
1775 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1776 XPUSHs (sv);
1777
1778 sv_chop (cborstr, offset);
1779
1780 av_clear (self->incr_count);
1781 av_push (self->incr_count, newSViv (1));
1782
1783 self->incr_pos = 0;
1784 self->incr_need = self->incr_pos + 1;
1785 }
1786 while (ix);
1787}
1788
1789void incr_reset (CBOR *self)
1790 CODE:
1791{
1792 SvREFCNT_dec (self->incr_count);
1793 self->incr_count = 0;
1794}
1795
1286void DESTROY (CBOR *self) 1796void DESTROY (CBOR *self)
1287 PPCODE: 1797 PPCODE:
1288 cbor_free (self); 1798 cbor_free (self);
1289 1799
1290PROTOTYPES: ENABLE 1800PROTOTYPES: ENABLE
1291 1801
1292void encode_cbor (SV *scalar) 1802void encode_cbor (SV *scalar)
1803 ALIAS:
1804 encode_cbor = 0
1805 encode_cbor_sharing = F_ALLOW_SHARING
1293 PPCODE: 1806 PPCODE:
1294{ 1807{
1295 CBOR cbor; 1808 CBOR cbor;
1296 cbor_init (&cbor); 1809 cbor_init (&cbor);
1810 cbor.flags |= ix;
1297 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1811 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1298 XPUSHs (scalar); 1812 XPUSHs (scalar);
1299} 1813}
1300 1814
1301void decode_cbor (SV *cborstr) 1815void decode_cbor (SV *cborstr)
1305 cbor_init (&cbor); 1819 cbor_init (&cbor);
1306 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; 1820 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1307 XPUSHs (cborstr); 1821 XPUSHs (cborstr);
1308} 1822}
1309 1823
1824#ifdef __AFL_COMPILER
1825
1826void
1827afl_init ()
1828 CODE:
1829 __AFL_INIT ();
1830
1831int
1832afl_loop (unsigned int count = 10000)
1833 CODE:
1834 RETVAL = __AFL_LOOP (count);
1835 OUTPUT:
1836 RETVAL
1837
1838#endif
1839

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines