MCPcopy Create free account
hub / github.com/OpenIPC/firmware / parse_string

Function parse_string

general/package/xmdp/src/cjson/cJSON.c:767–893  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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