MCPcopy Create free account
hub / github.com/DaveGamble/cJSON / parse_string

Function parse_string

cJSON.c:819–946  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

817
818/* Parse the input text into an unescaped cinput, and populate item. */
819static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
820{
821 const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
822 const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
823 unsigned char *output_pointer = NULL;
824 unsigned char *output = NULL;
825
826 /* not a string */
827 if (buffer_at_offset(input_buffer)[0] != '\"')
828 {
829 goto fail;
830 }
831
832 {
833 /* calculate approximate size of the output (overestimate) */
834 size_t allocation_length = 0;
835 size_t skipped_bytes = 0;
836 while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
837 {
838 /* is escape sequence */
839 if (input_end[0] == '\\')
840 {
841 if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
842 {
843 /* prevent buffer overflow when last input character is a backslash */
844 goto fail;
845 }
846 skipped_bytes++;
847 input_end++;
848 }
849 input_end++;
850 }
851 if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
852 {
853 goto fail; /* string ended unexpectedly */
854 }
855
856 /* This is at most how much we need for the output */
857 allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
858 output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
859 if (output == NULL)
860 {
861 goto fail; /* allocation failure */
862 }
863 }
864
865 output_pointer = output;
866 /* loop through the string literal */
867 while (input_pointer < input_end)
868 {
869 if (*input_pointer != '\\')
870 {
871 *output_pointer++ = *input_pointer++;
872 }
873 /* escape sequence */
874 else
875 {
876 unsigned char sequence_length = 2;

Callers 4

parse_valueFunction · 0.85
parse_objectFunction · 0.85
assert_parse_stringFunction · 0.85
assert_not_parse_stringFunction · 0.85

Calls 1

utf16_literal_to_utf8Function · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…