… | |
… | |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
33 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
36 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
38 | |
39 | #define SB do { |
39 | #define SB do { |
40 | #define SE } while (0) |
40 | #define SE } while (0) |
41 | |
41 | |
42 | #if __GNUC__ >= 3 |
42 | #if __GNUC__ >= 3 |
… | |
… | |
646 | static SV * |
646 | static SV * |
647 | decode_str (dec_t *dec) |
647 | decode_str (dec_t *dec) |
648 | { |
648 | { |
649 | SV *sv = 0; |
649 | SV *sv = 0; |
650 | int utf8 = 0; |
650 | int utf8 = 0; |
|
|
651 | char *dec_cur = dec->cur; |
651 | |
652 | |
652 | do |
653 | do |
653 | { |
654 | { |
654 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
655 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
655 | char *cur = buf; |
656 | char *cur = buf; |
656 | |
657 | |
657 | do |
658 | do |
658 | { |
659 | { |
659 | unsigned char ch = *(unsigned char *)dec->cur++; |
660 | unsigned char ch = *(unsigned char *)dec_cur++; |
660 | |
661 | |
661 | if (expect_false (ch == '"')) |
662 | if (expect_false (ch == '"')) |
662 | { |
663 | { |
663 | --dec->cur; |
664 | --dec_cur; |
664 | break; |
665 | break; |
665 | } |
666 | } |
666 | else if (expect_false (ch == '\\')) |
667 | else if (expect_false (ch == '\\')) |
667 | { |
668 | { |
668 | switch (*dec->cur) |
669 | switch (*dec_cur) |
669 | { |
670 | { |
670 | case '\\': |
671 | case '\\': |
671 | case '/': |
672 | case '/': |
672 | case '"': *cur++ = *dec->cur++; break; |
673 | case '"': *cur++ = *dec_cur++; break; |
673 | |
674 | |
674 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
675 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
675 | case 't': ++dec->cur; *cur++ = '\011'; break; |
676 | case 't': ++dec_cur; *cur++ = '\011'; break; |
676 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
677 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
677 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
678 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
678 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
679 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
679 | |
680 | |
680 | case 'u': |
681 | case 'u': |
681 | { |
682 | { |
682 | UV lo, hi; |
683 | UV lo, hi; |
683 | ++dec->cur; |
684 | ++dec_cur; |
684 | |
685 | |
|
|
686 | dec->cur = dec_cur; |
685 | hi = decode_4hex (dec); |
687 | hi = decode_4hex (dec); |
|
|
688 | dec_cur = dec->cur; |
686 | if (hi == (UV)-1) |
689 | if (hi == (UV)-1) |
687 | goto fail; |
690 | goto fail; |
688 | |
691 | |
689 | // possibly a surrogate pair |
692 | // possibly a surrogate pair |
690 | if (hi >= 0xd800) |
693 | if (hi >= 0xd800) |
691 | if (hi < 0xdc00) |
694 | if (hi < 0xdc00) |
692 | { |
695 | { |
693 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
696 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
694 | ERR ("missing low surrogate character in surrogate pair"); |
697 | ERR ("missing low surrogate character in surrogate pair"); |
695 | |
698 | |
696 | dec->cur += 2; |
699 | dec_cur += 2; |
697 | |
700 | |
|
|
701 | dec->cur = dec_cur; |
698 | lo = decode_4hex (dec); |
702 | lo = decode_4hex (dec); |
|
|
703 | dec_cur = dec->cur; |
699 | if (lo == (UV)-1) |
704 | if (lo == (UV)-1) |
700 | goto fail; |
705 | goto fail; |
701 | |
706 | |
702 | if (lo < 0xdc00 || lo >= 0xe000) |
707 | if (lo < 0xdc00 || lo >= 0xe000) |
703 | ERR ("surrogate pair expected"); |
708 | ERR ("surrogate pair expected"); |
… | |
… | |
717 | *cur++ = hi; |
722 | *cur++ = hi; |
718 | } |
723 | } |
719 | break; |
724 | break; |
720 | |
725 | |
721 | default: |
726 | default: |
722 | --dec->cur; |
727 | --dec_cur; |
723 | ERR ("illegal backslash escape sequence in string"); |
728 | ERR ("illegal backslash escape sequence in string"); |
724 | } |
729 | } |
725 | } |
730 | } |
726 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
731 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
727 | *cur++ = ch; |
732 | *cur++ = ch; |
728 | else if (ch >= 0x80) |
733 | else if (ch >= 0x80) |
729 | { |
734 | { |
730 | STRLEN clen; |
735 | STRLEN clen; |
731 | UV uch; |
736 | UV uch; |
732 | |
737 | |
733 | --dec->cur; |
738 | --dec_cur; |
734 | |
739 | |
735 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
740 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
736 | if (clen == (STRLEN)-1) |
741 | if (clen == (STRLEN)-1) |
737 | ERR ("malformed UTF-8 character in JSON string"); |
742 | ERR ("malformed UTF-8 character in JSON string"); |
738 | |
743 | |
739 | do |
744 | do |
740 | *cur++ = *dec->cur++; |
745 | *cur++ = *dec_cur++; |
741 | while (--clen); |
746 | while (--clen); |
742 | |
747 | |
743 | utf8 = 1; |
748 | utf8 = 1; |
744 | } |
749 | } |
745 | else |
750 | else |
746 | { |
751 | { |
747 | --dec->cur; |
752 | --dec_cur; |
748 | |
753 | |
749 | if (!ch) |
754 | if (!ch) |
750 | ERR ("unexpected end of string while parsing JSON string"); |
755 | ERR ("unexpected end of string while parsing JSON string"); |
751 | else |
756 | else |
752 | ERR ("invalid character encountered while parsing JSON string"); |
757 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
765 | } |
770 | } |
766 | else |
771 | else |
767 | sv = newSVpvn (buf, len); |
772 | sv = newSVpvn (buf, len); |
768 | } |
773 | } |
769 | } |
774 | } |
770 | while (*dec->cur != '"'); |
775 | while (*dec_cur != '"'); |
771 | |
776 | |
772 | ++dec->cur; |
777 | ++dec_cur; |
773 | |
778 | |
774 | if (sv) |
779 | if (sv) |
775 | { |
780 | { |
776 | SvPOK_only (sv); |
781 | SvPOK_only (sv); |
777 | *SvEND (sv) = 0; |
782 | *SvEND (sv) = 0; |
… | |
… | |
780 | SvUTF8_on (sv); |
785 | SvUTF8_on (sv); |
781 | } |
786 | } |
782 | else |
787 | else |
783 | sv = newSVpvn ("", 0); |
788 | sv = newSVpvn ("", 0); |
784 | |
789 | |
|
|
790 | dec->cur = dec_cur; |
785 | return sv; |
791 | return sv; |
786 | |
792 | |
787 | fail: |
793 | fail: |
|
|
794 | dec->cur = dec_cur; |
788 | return 0; |
795 | return 0; |
789 | } |
796 | } |
790 | |
797 | |
791 | static SV * |
798 | static SV * |
792 | decode_num (dec_t *dec) |
799 | decode_num (dec_t *dec) |