Parse the input text into an unescaped cinput, and populate item. */
| 712 | |
| 713 | /* Parse the input text into an unescaped cinput, and populate item. */ |
| 714 | static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) |
| 715 | { |
| 716 | const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; |
| 717 | const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; |
| 718 | unsigned char *output_pointer = NULL; |
| 719 | unsigned char *output = NULL; |
| 720 | |
| 721 | /* not a string */ |
| 722 | if (buffer_at_offset(input_buffer)[0] != '\"') |
| 723 | { |
| 724 | goto fail; |
| 725 | } |
| 726 | |
| 727 | { |
| 728 | /* calculate approximate size of the output (overestimate) */ |
| 729 | size_t allocation_length = 0; |
| 730 | size_t skipped_bytes = 0; |
| 731 | while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) |
| 732 | { |
| 733 | /* is escape sequence */ |
| 734 | if (input_end[0] == '\\') |
| 735 | { |
| 736 | if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) |
| 737 | { |
| 738 | /* prevent buffer overflow when last input character is a backslash */ |
| 739 | goto fail; |
| 740 | } |
| 741 | skipped_bytes++; |
| 742 | input_end++; |
| 743 | } |
| 744 | input_end++; |
| 745 | } |
| 746 | if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) |
| 747 | { |
| 748 | goto fail; /* string ended unexpectedly */ |
| 749 | } |
| 750 | |
| 751 | /* This is at most how much we need for the output */ |
| 752 | allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; |
| 753 | output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); |
| 754 | if (output == NULL) |
| 755 | { |
| 756 | goto fail; /* allocation failure */ |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | output_pointer = output; |
| 761 | /* loop through the string literal */ |
| 762 | while (input_pointer < input_end) |
| 763 | { |
| 764 | if (*input_pointer != '\\') |
| 765 | { |
| 766 | *output_pointer++ = *input_pointer++; |
| 767 | } |
| 768 | /* escape sequence */ |
| 769 | else |
| 770 | { |
| 771 | unsigned char sequence_length = 2; |
no test coverage detected