MCPcopy Create free account
hub / github.com/esnet/iperf / parse_string

Function parse_string

src/cjson.c:784–910  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…