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

Function parse_string

TheForceEngine/TFE_System/cJSON.c:778–904  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 2

parse_valueFunction · 0.85
parse_objectFunction · 0.85

Calls 2

utf16_literal_to_utf8Function · 0.85
allocateMethod · 0.45

Tested by

no test coverage detected