MCPcopy Index your code
hub / github.com/antirez/botlib / parse_string

Function parse_string

cJSON.c:769–895  ·  view source on GitHub ↗

Parse the input text into an unescaped cinput, and populate item. */

Source from the content-addressed store, hash-verified

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

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls 1

utf16_literal_to_utf8Function · 0.85

Tested by

no test coverage detected