… | |
… | |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
|
|
12 | #define ECB_NO_THREADS 1 |
12 | #include "ecb.h" |
13 | #include "ecb.h" |
13 | |
14 | |
14 | // compatibility with perl <5.18 |
15 | // compatibility with perl <5.18 |
15 | #ifndef HvNAMELEN_get |
16 | #ifndef HvNAMELEN_get |
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
17 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
… | |
… | |
99 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_TEXT_KEYS 0x00000020UL |
|
|
106 | #define F_TEXT_STRINGS 0x00000040UL |
104 | #define F_VALIDATE_UTF8 0x00000020UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
105 | |
108 | |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
107 | |
110 | |
108 | #define SB do { |
111 | #define SB do { |
109 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
128 | typedef struct { |
131 | typedef struct { |
129 | U32 flags; |
132 | U32 flags; |
130 | U32 max_depth; |
133 | U32 max_depth; |
131 | STRLEN max_size; |
134 | STRLEN max_size; |
132 | SV *filter; |
135 | SV *filter; |
|
|
136 | |
|
|
137 | // for the incremental parser |
|
|
138 | STRLEN incr_pos; // the current offset into the text |
|
|
139 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
140 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
133 | } CBOR; |
141 | } CBOR; |
134 | |
142 | |
135 | ecb_inline void |
143 | ecb_inline void |
136 | cbor_init (CBOR *cbor) |
144 | cbor_init (CBOR *cbor) |
137 | { |
145 | { |
… | |
… | |
141 | |
149 | |
142 | ecb_inline void |
150 | ecb_inline void |
143 | cbor_free (CBOR *cbor) |
151 | cbor_free (CBOR *cbor) |
144 | { |
152 | { |
145 | SvREFCNT_dec (cbor->filter); |
153 | SvREFCNT_dec (cbor->filter); |
|
|
154 | SvREFCNT_dec (cbor->incr_count); |
146 | } |
155 | } |
147 | |
156 | |
148 | ///////////////////////////////////////////////////////////////////////////// |
157 | ///////////////////////////////////////////////////////////////////////////// |
149 | // utility functions |
158 | // utility functions |
150 | |
159 | |
… | |
… | |
269 | encode_tag (enc_t *enc, UV tag) |
278 | encode_tag (enc_t *enc, UV tag) |
270 | { |
279 | { |
271 | encode_uint (enc, MAJOR_TAG, tag); |
280 | encode_uint (enc, MAJOR_TAG, tag); |
272 | } |
281 | } |
273 | |
282 | |
|
|
283 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
284 | ecb_noinline static void |
|
|
285 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
286 | { |
|
|
287 | STRLEN ulen = len; |
|
|
288 | U8 *p, *pend = (U8 *)str + len; |
|
|
289 | |
|
|
290 | for (p = (U8 *)str; p < pend; ++p) |
|
|
291 | ulen += *p >> 7; // count set high bits |
|
|
292 | |
|
|
293 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
294 | |
|
|
295 | need (enc, ulen); |
|
|
296 | for (p = (U8 *)str; p < pend; ++p) |
|
|
297 | if (*p < 0x80) |
|
|
298 | *enc->cur++ = *p; |
|
|
299 | else |
|
|
300 | { |
|
|
301 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
302 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
274 | ecb_inline void |
306 | ecb_inline void |
275 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
307 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
276 | { |
308 | { |
|
|
309 | if (ecb_expect_false (upgrade_utf8)) |
|
|
310 | if (!utf8) |
|
|
311 | { |
|
|
312 | encode_str_utf8 (enc, utf8, str, len); |
|
|
313 | return; |
|
|
314 | } |
|
|
315 | |
277 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
278 | need (enc, len); |
317 | need (enc, len); |
279 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
280 | enc->cur += len; |
319 | enc->cur += len; |
281 | } |
320 | } |
282 | |
321 | |
283 | static void |
322 | ecb_inline void |
284 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
285 | { |
324 | { |
286 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
287 | { |
326 | { |
288 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
289 | |
328 | |
… | |
… | |
300 | sv_setuv (*svp, enc->stringref_idx); |
339 | sv_setuv (*svp, enc->stringref_idx); |
301 | ++enc->stringref_idx; |
340 | ++enc->stringref_idx; |
302 | } |
341 | } |
303 | } |
342 | } |
304 | |
343 | |
305 | encode_str (enc, utf8, str, len); |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
306 | } |
345 | } |
307 | |
346 | |
308 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
309 | |
348 | |
310 | static void |
349 | static void |
… | |
… | |
317 | |
356 | |
318 | ++enc->depth; |
357 | ++enc->depth; |
319 | |
358 | |
320 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
359 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
321 | |
360 | |
|
|
361 | if (SvMAGICAL (av)) |
322 | for (i = 0; i <= len; ++i) |
362 | for (i = 0; i <= len; ++i) |
323 | { |
363 | { |
324 | SV **svp = av_fetch (av, i, 0); |
364 | SV **svp = av_fetch (av, i, 0); |
325 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
365 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
326 | } |
366 | } |
|
|
367 | else |
|
|
368 | for (i = 0; i <= len; ++i) |
|
|
369 | { |
|
|
370 | SV *sv = AvARRAY (av)[i]; |
|
|
371 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
372 | } |
327 | |
373 | |
328 | --enc->depth; |
374 | --enc->depth; |
329 | } |
375 | } |
330 | |
376 | |
331 | static void |
377 | static void |
… | |
… | |
349 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
350 | { |
396 | { |
351 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
352 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
353 | else |
399 | else |
354 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
400 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
355 | |
401 | |
356 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
357 | } |
403 | } |
358 | |
404 | |
359 | if (mg) |
405 | if (mg) |
… | |
… | |
435 | |
481 | |
436 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
437 | { |
483 | { |
438 | dSP; |
484 | dSP; |
439 | |
485 | |
440 | ENTER; SAVETMPS; PUSHMARK (SP); |
486 | ENTER; SAVETMPS; |
|
|
487 | PUSHMARK (SP); |
441 | // we re-bless the reference to get overload and other niceties right |
488 | // we re-bless the reference to get overload and other niceties right |
442 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
489 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
443 | |
490 | |
444 | PUTBACK; |
491 | PUTBACK; |
445 | // G_SCALAR ensures that return value is 1 |
492 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
458 | } |
505 | } |
459 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
506 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
460 | { |
507 | { |
461 | dSP; |
508 | dSP; |
462 | |
509 | |
463 | ENTER; SAVETMPS; PUSHMARK (SP); |
510 | ENTER; SAVETMPS; |
|
|
511 | SAVESTACK_POS (); |
|
|
512 | PUSHMARK (SP); |
464 | EXTEND (SP, 2); |
513 | EXTEND (SP, 2); |
465 | // we re-bless the reference to get overload and other niceties right |
514 | // we re-bless the reference to get overload and other niceties right |
466 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
515 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
467 | PUSHs (sv_cbor); |
516 | PUSHs (sv_cbor); |
468 | |
517 | |
… | |
… | |
474 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
475 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
524 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
476 | |
525 | |
477 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
478 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
479 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
480 | |
529 | |
481 | while (count) |
530 | while (count) |
482 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
483 | |
532 | |
484 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
543 | |
592 | |
544 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
545 | { |
594 | { |
546 | STRLEN len; |
595 | STRLEN len; |
547 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
548 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
549 | } |
598 | } |
550 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
551 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
552 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
553 | { |
602 | { |
… | |
… | |
570 | } |
619 | } |
571 | |
620 | |
572 | static SV * |
621 | static SV * |
573 | encode_cbor (SV *scalar, CBOR *cbor) |
622 | encode_cbor (SV *scalar, CBOR *cbor) |
574 | { |
623 | { |
575 | enc_t enc = { }; |
624 | enc_t enc = { 0 }; |
576 | |
625 | |
577 | enc.cbor = *cbor; |
626 | enc.cbor = *cbor; |
578 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
579 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
580 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
… | |
… | |
612 | U32 depth; // recursion depth |
661 | U32 depth; // recursion depth |
613 | U32 maxdepth; // recursion depth limit |
662 | U32 maxdepth; // recursion depth limit |
614 | AV *shareable; |
663 | AV *shareable; |
615 | AV *stringref; |
664 | AV *stringref; |
616 | SV *decode_tagged; |
665 | SV *decode_tagged; |
|
|
666 | SV *err_sv; // optional sv for error, needs to be freed |
617 | } dec_t; |
667 | } dec_t; |
618 | |
668 | |
619 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
669 | // set dec->err to ERRSV |
|
|
670 | ecb_cold static void |
|
|
671 | err_errsv (dec_t *dec) |
|
|
672 | { |
|
|
673 | if (!dec->err) |
|
|
674 | { |
|
|
675 | dec->err_sv = newSVsv (ERRSV); |
620 | |
676 | |
621 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
677 | // chop off the trailing \n |
|
|
678 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
679 | *SvEND (dec->err_sv) = 0; |
622 | |
680 | |
|
|
681 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
682 | } |
|
|
683 | } |
|
|
684 | |
|
|
685 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
686 | ecb_cold static void |
|
|
687 | err_set (dec_t *dec, const char *reason) |
|
|
688 | { |
|
|
689 | if (!dec->err) |
|
|
690 | dec->err = reason; |
|
|
691 | } |
|
|
692 | |
|
|
693 | ecb_cold static void |
|
|
694 | err_unexpected_end (dec_t *dec) |
|
|
695 | { |
|
|
696 | err_set (dec, "unexpected end of CBOR data"); |
|
|
697 | } |
|
|
698 | |
|
|
699 | ecb_cold static void |
|
|
700 | err_nesting_exceeded (dec_t *dec) |
|
|
701 | { |
|
|
702 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
703 | } |
|
|
704 | |
|
|
705 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
706 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
707 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
708 | |
|
|
709 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) |
|
|
710 | |
623 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
711 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
624 | #define DEC_DEC_DEPTH --dec->depth |
712 | #define DEC_DEC_DEPTH --dec->depth |
625 | |
713 | |
626 | static UV |
714 | static UV |
627 | decode_uint (dec_t *dec) |
715 | decode_uint (dec_t *dec) |
628 | { |
716 | { |
… | |
… | |
705 | av_push (av, decode_sv (dec)); |
793 | av_push (av, decode_sv (dec)); |
706 | } |
794 | } |
707 | } |
795 | } |
708 | else |
796 | else |
709 | { |
797 | { |
710 | int i, len = decode_uint (dec); |
798 | UV i, len = decode_uint (dec); |
711 | |
799 | |
712 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
800 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
713 | av_fill (av, len - 1); |
801 | av_fill (av, len - 1); |
714 | |
802 | |
715 | for (i = 0; i < len; ++i) |
803 | for (i = 0; i < len; ++i) |
… | |
… | |
730 | { |
818 | { |
731 | // for speed reasons, we specialcase single-string |
819 | // for speed reasons, we specialcase single-string |
732 | // byte or utf-8 strings as keys, but only when !stringref |
820 | // byte or utf-8 strings as keys, but only when !stringref |
733 | |
821 | |
734 | if (ecb_expect_true (!dec->stringref)) |
822 | if (ecb_expect_true (!dec->stringref)) |
735 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
823 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
736 | { |
824 | { |
737 | I32 len = decode_uint (dec); |
825 | STRLEN len = decode_uint (dec); |
738 | char *key = (char *)dec->cur; |
826 | char *key = (char *)dec->cur; |
739 | |
827 | |
|
|
828 | WANT (len); |
740 | dec->cur += len; |
829 | dec->cur += len; |
741 | |
830 | |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
831 | hv_store (hv, key, len, decode_sv (dec), 0); |
743 | |
832 | |
744 | return; |
833 | return; |
745 | } |
834 | } |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
835 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
747 | { |
836 | { |
748 | I32 len = decode_uint (dec); |
837 | STRLEN len = decode_uint (dec); |
749 | char *key = (char *)dec->cur; |
838 | char *key = (char *)dec->cur; |
750 | |
839 | |
|
|
840 | WANT (len); |
751 | dec->cur += len; |
841 | dec->cur += len; |
752 | |
842 | |
753 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
843 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
754 | if (!is_utf8_string (key, len)) |
844 | if (!is_utf8_string (key, len)) |
755 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
845 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
759 | return; |
849 | return; |
760 | } |
850 | } |
761 | |
851 | |
762 | SV *k = decode_sv (dec); |
852 | SV *k = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
853 | SV *v = decode_sv (dec); |
|
|
854 | |
|
|
855 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
856 | // methods, and this is hopefully the only place where it can happen, |
|
|
857 | // so if there is a chance of an exception, take the very slow path. |
|
|
858 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
859 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
860 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
861 | { |
|
|
862 | dSP; |
|
|
863 | |
|
|
864 | ENTER; SAVETMPS; |
|
|
865 | PUSHMARK (SP); |
|
|
866 | EXTEND (SP, 3); |
|
|
867 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
868 | PUSHs (sv_2mortal (k)); |
|
|
869 | PUSHs (sv_2mortal (v)); |
|
|
870 | |
|
|
871 | PUTBACK; |
|
|
872 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
873 | SPAGAIN; |
|
|
874 | |
|
|
875 | FREETMPS; LEAVE; |
|
|
876 | |
|
|
877 | if (SvTRUE (ERRSV)) |
|
|
878 | ERR_ERRSV; |
|
|
879 | |
|
|
880 | return; |
|
|
881 | } |
764 | |
882 | |
765 | hv_store_ent (hv, k, v, 0); |
883 | hv_store_ent (hv, k, v, 0); |
766 | SvREFCNT_dec (k); |
884 | SvREFCNT_dec (k); |
767 | |
885 | |
768 | fail: |
886 | fail: |
… | |
… | |
793 | decode_he (dec, hv); |
911 | decode_he (dec, hv); |
794 | } |
912 | } |
795 | } |
913 | } |
796 | else |
914 | else |
797 | { |
915 | { |
798 | int pairs = decode_uint (dec); |
916 | UV pairs = decode_uint (dec); |
|
|
917 | |
|
|
918 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
799 | |
919 | |
800 | while (pairs--) |
920 | while (pairs--) |
801 | decode_he (dec, hv); |
921 | decode_he (dec, hv); |
802 | } |
922 | } |
803 | |
923 | |
… | |
… | |
891 | sv = newRV_noinc (decode_sv (dec)); |
1011 | sv = newRV_noinc (decode_sv (dec)); |
892 | break; |
1012 | break; |
893 | |
1013 | |
894 | case CBOR_TAG_STRINGREF_NAMESPACE: |
1014 | case CBOR_TAG_STRINGREF_NAMESPACE: |
895 | { |
1015 | { |
|
|
1016 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
1017 | // erase mortalised caches, e.g. "shareable" |
896 | ENTER; SAVETMPS; |
1018 | ENTER; |
897 | |
1019 | |
898 | SAVESPTR (dec->stringref); |
1020 | SAVESPTR (dec->stringref); |
899 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
1021 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
900 | |
1022 | |
901 | sv = decode_sv (dec); |
1023 | sv = decode_sv (dec); |
902 | |
1024 | |
903 | FREETMPS; LEAVE; |
1025 | LEAVE; |
904 | } |
1026 | } |
905 | break; |
1027 | break; |
906 | |
1028 | |
907 | case CBOR_TAG_STRINGREF: |
1029 | case CBOR_TAG_STRINGREF: |
908 | { |
1030 | { |
… | |
… | |
978 | if (!method) |
1100 | if (!method) |
979 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1101 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
980 | |
1102 | |
981 | dSP; |
1103 | dSP; |
982 | |
1104 | |
983 | ENTER; SAVETMPS; PUSHMARK (SP); |
1105 | ENTER; SAVETMPS; |
|
|
1106 | PUSHMARK (SP); |
984 | EXTEND (SP, len + 1); |
1107 | EXTEND (SP, len + 1); |
985 | // we re-bless the reference to get overload and other niceties right |
1108 | // we re-bless the reference to get overload and other niceties right |
986 | PUSHs (*av_fetch (av, 0, 1)); |
1109 | PUSHs (*av_fetch (av, 0, 1)); |
987 | PUSHs (sv_cbor); |
1110 | PUSHs (sv_cbor); |
988 | |
1111 | |
… | |
… | |
996 | SPAGAIN; |
1119 | SPAGAIN; |
997 | |
1120 | |
998 | if (SvTRUE (ERRSV)) |
1121 | if (SvTRUE (ERRSV)) |
999 | { |
1122 | { |
1000 | FREETMPS; LEAVE; |
1123 | FREETMPS; LEAVE; |
1001 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1124 | ERR_ERRSV; |
1002 | } |
1125 | } |
1003 | |
1126 | |
1004 | SvREFCNT_dec (sv); |
1127 | SvREFCNT_dec (sv); |
1005 | sv = SvREFCNT_inc (POPs); |
1128 | sv = SvREFCNT_inc (POPs); |
1006 | |
1129 | |
… | |
… | |
1010 | } |
1133 | } |
1011 | break; |
1134 | break; |
1012 | |
1135 | |
1013 | default: |
1136 | default: |
1014 | { |
1137 | { |
|
|
1138 | SV *tag_sv = newSVuv (tag); |
|
|
1139 | |
1015 | sv = decode_sv (dec); |
1140 | sv = decode_sv (dec); |
1016 | |
1141 | |
1017 | dSP; |
1142 | dSP; |
1018 | ENTER; SAVETMPS; PUSHMARK (SP); |
1143 | ENTER; SAVETMPS; |
|
|
1144 | SAVESTACK_POS (); |
|
|
1145 | PUSHMARK (SP); |
1019 | EXTEND (SP, 2); |
1146 | EXTEND (SP, 2); |
1020 | PUSHs (newSVuv (tag)); |
1147 | PUSHs (tag_sv); |
1021 | PUSHs (sv); |
1148 | PUSHs (sv); |
1022 | |
1149 | |
1023 | PUTBACK; |
1150 | PUTBACK; |
1024 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1151 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
1025 | SPAGAIN; |
1152 | SPAGAIN; |
1026 | |
1153 | |
1027 | if (SvTRUE (ERRSV)) |
1154 | if (SvTRUE (ERRSV)) |
1028 | { |
1155 | { |
|
|
1156 | SvREFCNT_dec (tag_sv); |
1029 | FREETMPS; LEAVE; |
1157 | FREETMPS; LEAVE; |
1030 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
1158 | ERR_ERRSV; |
1031 | } |
1159 | } |
1032 | |
1160 | |
1033 | if (count) |
1161 | if (count) |
1034 | { |
1162 | { |
|
|
1163 | SvREFCNT_dec (tag_sv); |
1035 | SvREFCNT_dec (sv); |
1164 | SvREFCNT_dec (sv); |
1036 | sv = SvREFCNT_inc (POPs); |
1165 | sv = SvREFCNT_inc (POPs); |
1037 | } |
1166 | } |
1038 | else |
1167 | else |
1039 | { |
1168 | { |
1040 | AV *av = newAV (); |
1169 | AV *av = newAV (); |
1041 | av_push (av, newSVuv (tag)); |
1170 | av_push (av, tag_sv); |
1042 | av_push (av, sv); |
1171 | av_push (av, sv); |
1043 | |
1172 | |
1044 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1173 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1045 | ? cbor_tagged_stash |
1174 | ? cbor_tagged_stash |
1046 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1175 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
… | |
… | |
1132 | |
1261 | |
1133 | return newSVnv (ecb_binary64_to_double (fp)); |
1262 | return newSVnv (ecb_binary64_to_double (fp)); |
1134 | } |
1263 | } |
1135 | |
1264 | |
1136 | // 0..19 unassigned simple |
1265 | // 0..19 unassigned simple |
1137 | // 24 reserved + unassigned (reserved values are not encodable) |
1266 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1267 | // 28-30 unassigned misc |
|
|
1268 | // 31 break code |
1138 | default: |
1269 | default: |
1139 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1270 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
1140 | } |
1271 | } |
1141 | |
1272 | |
1142 | break; |
1273 | break; |
1143 | } |
1274 | } |
1144 | |
1275 | |
… | |
… | |
1147 | } |
1278 | } |
1148 | |
1279 | |
1149 | static SV * |
1280 | static SV * |
1150 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1281 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1151 | { |
1282 | { |
1152 | dec_t dec = { }; |
1283 | dec_t dec = { 0 }; |
1153 | SV *sv; |
1284 | SV *sv; |
1154 | STRLEN len; |
1285 | STRLEN len; |
1155 | char *data = SvPVbyte (string, len); |
1286 | char *data = SvPVbyte (string, len); |
1156 | |
1287 | |
1157 | if (len > cbor->max_size && cbor->max_size) |
1288 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1173 | |
1304 | |
1174 | if (dec.err) |
1305 | if (dec.err) |
1175 | { |
1306 | { |
1176 | if (dec.shareable) |
1307 | if (dec.shareable) |
1177 | { |
1308 | { |
1178 | // need to break cyclic links, which whould all be in shareable |
1309 | // need to break cyclic links, which would all be in shareable |
1179 | int i; |
1310 | int i; |
1180 | SV **svp; |
1311 | SV **svp; |
1181 | |
1312 | |
1182 | for (i = av_len (dec.shareable) + 1; i--; ) |
1313 | for (i = av_len (dec.shareable) + 1; i--; ) |
1183 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1314 | if ((svp = av_fetch (dec.shareable, i, 0))) |
1184 | sv_setsv (*svp, &PL_sv_undef); |
1315 | sv_setsv (*svp, &PL_sv_undef); |
1185 | } |
1316 | } |
1186 | |
1317 | |
1187 | SvREFCNT_dec (sv); |
1318 | SvREFCNT_dec (sv); |
|
|
1319 | |
|
|
1320 | if (dec.err_sv) |
|
|
1321 | sv_2mortal (dec.err_sv); |
|
|
1322 | |
1188 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1323 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
1189 | } |
1324 | } |
1190 | |
1325 | |
1191 | sv = sv_2mortal (sv); |
1326 | sv = sv_2mortal (sv); |
1192 | |
1327 | |
1193 | return sv; |
1328 | return sv; |
1194 | } |
1329 | } |
1195 | |
1330 | |
|
|
1331 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1332 | // incremental parser |
|
|
1333 | |
|
|
1334 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1335 | |
|
|
1336 | // returns 0 for notyet, 1 for success or error |
|
|
1337 | static int |
|
|
1338 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1339 | { |
|
|
1340 | STRLEN cur; |
|
|
1341 | SvPV (cborstr, cur); |
|
|
1342 | |
|
|
1343 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1344 | { |
|
|
1345 | // table of integer count bytes |
|
|
1346 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1347 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1348 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1349 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1350 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1351 | }; |
|
|
1352 | |
|
|
1353 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1354 | U8 m = *p & MINOR_MASK; |
|
|
1355 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1356 | I8 ilen = incr_len[m]; |
|
|
1357 | |
|
|
1358 | self->incr_need = self->incr_pos + 1; |
|
|
1359 | |
|
|
1360 | if (ecb_expect_false (ilen < 0)) |
|
|
1361 | { |
|
|
1362 | if (m != MINOR_INDEF) |
|
|
1363 | return 1; // error |
|
|
1364 | |
|
|
1365 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1366 | { |
|
|
1367 | if (count >= 0) |
|
|
1368 | return 1; // error |
|
|
1369 | |
|
|
1370 | count = 1; |
|
|
1371 | } |
|
|
1372 | else |
|
|
1373 | { |
|
|
1374 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1375 | count = -1; |
|
|
1376 | } |
|
|
1377 | } |
|
|
1378 | else |
|
|
1379 | { |
|
|
1380 | self->incr_need += ilen; |
|
|
1381 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1382 | return 0; |
|
|
1383 | |
|
|
1384 | int major = *p >> MAJOR_SHIFT; |
|
|
1385 | |
|
|
1386 | switch (major) |
|
|
1387 | { |
|
|
1388 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1389 | ++count; // tags merely prefix another value |
|
|
1390 | break; |
|
|
1391 | |
|
|
1392 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1393 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1394 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1395 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1396 | { |
|
|
1397 | UV len; |
|
|
1398 | |
|
|
1399 | if (ecb_expect_false (ilen)) |
|
|
1400 | { |
|
|
1401 | len = 0; |
|
|
1402 | |
|
|
1403 | do { |
|
|
1404 | len = (len << 8) | *++p; |
|
|
1405 | } while (--ilen); |
|
|
1406 | } |
|
|
1407 | else |
|
|
1408 | len = m; |
|
|
1409 | |
|
|
1410 | switch (major) |
|
|
1411 | { |
|
|
1412 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1413 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1414 | self->incr_need += len; |
|
|
1415 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1416 | return 0; |
|
|
1417 | |
|
|
1418 | break; |
|
|
1419 | |
|
|
1420 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1421 | len <<= 1; |
|
|
1422 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1423 | if (len) |
|
|
1424 | { |
|
|
1425 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1426 | count = len + 1; |
|
|
1427 | } |
|
|
1428 | break; |
|
|
1429 | } |
|
|
1430 | } |
|
|
1431 | } |
|
|
1432 | } |
|
|
1433 | |
|
|
1434 | self->incr_pos = self->incr_need; |
|
|
1435 | |
|
|
1436 | if (count > 0) |
|
|
1437 | { |
|
|
1438 | while (!--count) |
|
|
1439 | { |
|
|
1440 | if (!AvFILLp (self->incr_count)) |
|
|
1441 | return 1; // done |
|
|
1442 | |
|
|
1443 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1444 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1445 | } |
|
|
1446 | |
|
|
1447 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1448 | } |
|
|
1449 | } |
|
|
1450 | |
|
|
1451 | return 0; |
|
|
1452 | } |
|
|
1453 | |
|
|
1454 | |
1196 | ///////////////////////////////////////////////////////////////////////////// |
1455 | ///////////////////////////////////////////////////////////////////////////// |
1197 | // XS interface functions |
1456 | // XS interface functions |
1198 | |
1457 | |
1199 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1458 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1200 | |
1459 | |
… | |
… | |
1212 | |
1471 | |
1213 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1472 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1214 | |
1473 | |
1215 | sv_cbor = newSVpv ("CBOR", 0); |
1474 | sv_cbor = newSVpv ("CBOR", 0); |
1216 | SvREADONLY_on (sv_cbor); |
1475 | SvREADONLY_on (sv_cbor); |
|
|
1476 | |
|
|
1477 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1217 | } |
1478 | } |
1218 | |
1479 | |
1219 | PROTOTYPES: DISABLE |
1480 | PROTOTYPES: DISABLE |
1220 | |
1481 | |
1221 | void CLONE (...) |
1482 | void CLONE (...) |
… | |
… | |
1242 | shrink = F_SHRINK |
1503 | shrink = F_SHRINK |
1243 | allow_unknown = F_ALLOW_UNKNOWN |
1504 | allow_unknown = F_ALLOW_UNKNOWN |
1244 | allow_sharing = F_ALLOW_SHARING |
1505 | allow_sharing = F_ALLOW_SHARING |
1245 | allow_cycles = F_ALLOW_CYCLES |
1506 | allow_cycles = F_ALLOW_CYCLES |
1246 | pack_strings = F_PACK_STRINGS |
1507 | pack_strings = F_PACK_STRINGS |
|
|
1508 | text_keys = F_TEXT_KEYS |
|
|
1509 | text_strings = F_TEXT_STRINGS |
1247 | validate_utf8 = F_VALIDATE_UTF8 |
1510 | validate_utf8 = F_VALIDATE_UTF8 |
1248 | PPCODE: |
1511 | PPCODE: |
1249 | { |
1512 | { |
1250 | if (enable) |
1513 | if (enable) |
1251 | self->flags |= ix; |
1514 | self->flags |= ix; |
… | |
… | |
1260 | get_shrink = F_SHRINK |
1523 | get_shrink = F_SHRINK |
1261 | get_allow_unknown = F_ALLOW_UNKNOWN |
1524 | get_allow_unknown = F_ALLOW_UNKNOWN |
1262 | get_allow_sharing = F_ALLOW_SHARING |
1525 | get_allow_sharing = F_ALLOW_SHARING |
1263 | get_allow_cycles = F_ALLOW_CYCLES |
1526 | get_allow_cycles = F_ALLOW_CYCLES |
1264 | get_pack_strings = F_PACK_STRINGS |
1527 | get_pack_strings = F_PACK_STRINGS |
|
|
1528 | get_text_keys = F_TEXT_KEYS |
|
|
1529 | get_text_strings = F_TEXT_STRINGS |
1265 | get_validate_utf8 = F_VALIDATE_UTF8 |
1530 | get_validate_utf8 = F_VALIDATE_UTF8 |
1266 | PPCODE: |
1531 | PPCODE: |
1267 | XPUSHs (boolSV (self->flags & ix)); |
1532 | XPUSHs (boolSV (self->flags & ix)); |
1268 | |
1533 | |
1269 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1534 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
… | |
… | |
1319 | EXTEND (SP, 2); |
1584 | EXTEND (SP, 2); |
1320 | PUSHs (sv); |
1585 | PUSHs (sv); |
1321 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1586 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1322 | } |
1587 | } |
1323 | |
1588 | |
|
|
1589 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1590 | ALIAS: |
|
|
1591 | incr_parse_multiple = 1 |
|
|
1592 | PPCODE: |
|
|
1593 | { |
|
|
1594 | if (SvUTF8 (cborstr)) |
|
|
1595 | sv_utf8_downgrade (cborstr, 0); |
|
|
1596 | |
|
|
1597 | if (!self->incr_count) |
|
|
1598 | { |
|
|
1599 | self->incr_count = newAV (); |
|
|
1600 | self->incr_pos = 0; |
|
|
1601 | self->incr_need = 1; |
|
|
1602 | |
|
|
1603 | av_push (self->incr_count, newSViv (1)); |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | do |
|
|
1607 | { |
|
|
1608 | if (!incr_parse (self, cborstr)) |
|
|
1609 | { |
|
|
1610 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1611 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1612 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1613 | |
|
|
1614 | break; |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | SV *sv; |
|
|
1618 | char *offset; |
|
|
1619 | |
|
|
1620 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1621 | XPUSHs (sv); |
|
|
1622 | |
|
|
1623 | sv_chop (cborstr, offset); |
|
|
1624 | |
|
|
1625 | av_clear (self->incr_count); |
|
|
1626 | av_push (self->incr_count, newSViv (1)); |
|
|
1627 | |
|
|
1628 | self->incr_pos = 0; |
|
|
1629 | self->incr_need = self->incr_pos + 1; |
|
|
1630 | } |
|
|
1631 | while (ix); |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | void incr_reset (CBOR *self) |
|
|
1635 | CODE: |
|
|
1636 | { |
|
|
1637 | SvREFCNT_dec (self->incr_count); |
|
|
1638 | self->incr_count = 0; |
|
|
1639 | } |
|
|
1640 | |
1324 | void DESTROY (CBOR *self) |
1641 | void DESTROY (CBOR *self) |
1325 | PPCODE: |
1642 | PPCODE: |
1326 | cbor_free (self); |
1643 | cbor_free (self); |
1327 | |
1644 | |
1328 | PROTOTYPES: ENABLE |
1645 | PROTOTYPES: ENABLE |