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

Function utf16_literal_to_utf8

cJSON.c:698–816  ·  view source on GitHub ↗

converts a UTF-16 literal to UTF-8 * A literal can be one or two sequences of the form \uXXXX */

Source from the content-addressed store, hash-verified

696/* converts a UTF-16 literal to UTF-8
697 * A literal can be one or two sequences of the form \uXXXX */
698static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
699{
700 long unsigned int codepoint = 0;
701 unsigned int first_code = 0;
702 const unsigned char *first_sequence = input_pointer;
703 unsigned char utf8_length = 0;
704 unsigned char utf8_position = 0;
705 unsigned char sequence_length = 0;
706 unsigned char first_byte_mark = 0;
707
708 if ((input_end - first_sequence) < 6)
709 {
710 /* input ends unexpectedly */
711 goto fail;
712 }
713
714 /* get the first utf16 sequence */
715 first_code = parse_hex4(first_sequence + 2);
716
717 /* check that the code is valid */
718 if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
719 {
720 goto fail;
721 }
722
723 /* UTF16 surrogate pair */
724 if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
725 {
726 const unsigned char *second_sequence = first_sequence + 6;
727 unsigned int second_code = 0;
728 sequence_length = 12; /* \uXXXX\uXXXX */
729
730 if ((input_end - second_sequence) < 6)
731 {
732 /* input ends unexpectedly */
733 goto fail;
734 }
735
736 if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
737 {
738 /* missing second half of the surrogate pair */
739 goto fail;
740 }
741
742 /* get the second utf16 sequence */
743 second_code = parse_hex4(second_sequence + 2);
744 /* check that the code is valid */
745 if ((second_code < 0xDC00) || (second_code > 0xDFFF))
746 {
747 /* invalid second half of the surrogate pair */
748 goto fail;
749 }
750
751
752 /* calculate the unicode codepoint from the surrogate pair */
753 codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
754 }
755 else

Callers 1

parse_stringFunction · 0.85

Calls 1

parse_hex4Function · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…