MCPcopy Create free account
hub / github.com/ByConity/ByConity / parse_string

Function parse_string

contrib/cJSON/cJSON.cpp:714–840  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

712
713/* Parse the input text into an unescaped cinput, and populate item. */
714static 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;

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls 3

utf16_literal_to_utf8Function · 0.85
allocateMethod · 0.45
deallocateMethod · 0.45

Tested by

no test coverage detected