Parse the input text into an unescaped cinput, and populate item. */
| 779 | |
| 780 | /* Parse the input text into an unescaped cinput, and populate item. */ |
| 781 | static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) |
| 782 | { |
| 783 | const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; |
| 784 | const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; |
| 785 | unsigned char *output_pointer = NULL; |
| 786 | unsigned char *output = NULL; |
| 787 | |
| 788 | /* not a string */ |
| 789 | if (buffer_at_offset(input_buffer)[0] != '\"') |
| 790 | { |
| 791 | goto fail; |
| 792 | } |
| 793 | |
| 794 | { |
| 795 | /* calculate approximate size of the output (overestimate) */ |
| 796 | size_t allocation_length = 0; |
| 797 | size_t skipped_bytes = 0; |
| 798 | while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) |
| 799 | { |
| 800 | /* is escape sequence */ |
| 801 | if (input_end[0] == '\\') |
| 802 | { |
| 803 | if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) |
| 804 | { |
| 805 | /* prevent buffer overflow when last input character is a backslash */ |
| 806 | goto fail; |
| 807 | } |
| 808 | skipped_bytes++; |
| 809 | input_end++; |
| 810 | } |
| 811 | input_end++; |
| 812 | } |
| 813 | if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) |
| 814 | { |
| 815 | goto fail; /* string ended unexpectedly */ |
| 816 | } |
| 817 | |
| 818 | /* This is at most how much we need for the output */ |
| 819 | allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; |
| 820 | output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); |
| 821 | if (output == NULL) |
| 822 | { |
| 823 | goto fail; /* allocation failure */ |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | output_pointer = output; |
| 828 | /* loop through the string literal */ |
| 829 | while (input_pointer < input_end) |
| 830 | { |
| 831 | if (*input_pointer != '\\') |
| 832 | { |
| 833 | *output_pointer++ = *input_pointer++; |
| 834 | } |
| 835 | /* escape sequence */ |
| 836 | else |
| 837 | { |
| 838 | unsigned char sequence_length = 2; |
no test coverage detected