/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ // This API provides utility functions for working with and converting between // Unicode string representations. All UTF-16 and UTF-32 strings are expected to // be in the host platform's native endianness. // For more information: // - http://en.wikipedia.org/wiki/UTF-8 // - http://en.wikipedia.org/wiki/UTF-16 // - http://en.wikipedia.org/wiki/UTF-32 #ifndef __UTF_UTILITIES_H__ #define __UTF_UTILITIES_H__ #include #include #include #define UTF32_MAX 0x10FFFF #define UTF32_RESERVED_START 0xD800 #define UTF32_RESERVED_END 0xDFFF #define UTF32_MALFORMED_CHARACTER 0xFFFD #define UTF8_SEQUENCE_MAX 4 #define UTF16_SEQUENCE_MAX 2 // Functions for measuring strings (equivalent to strlen). Returns the number of array elements // before a null terminator, not the number of logical characters. size_t utf8StringLength(const uint8_t * utf8String); size_t utf16StringLength(const uint16_t * utf16String); size_t utf32StringLength(const uint32_t * utf32String); // Functions for checking the well-formedness of Unicode strings. bool utf8StringIsWellFormed(const uint8_t * utf8String, size_t length); bool utf16StringIsWellFormed(const uint16_t * utf16String, size_t length); bool utf32StringIsWellFormed(const uint32_t * utf32String, size_t length); // Functions for determining how long strings would be in different encodings. size_t utf8StringUTF16Length(const uint8_t * utf8String, size_t length); size_t utf8StringUTF32Length(const uint8_t * utf8String, size_t length); size_t utf16StringUTF8Length(const uint16_t * utf16String, size_t length); size_t utf16StringUTF32Length(const uint16_t * utf16String, size_t length); size_t utf32StringUTF8Length(const uint32_t * utf32String, size_t length); size_t utf32StringUTF16Length(const uint32_t * utf32String, size_t length); // Simple functions for converting between encodings. The returned pointer must be // freed by the caller. If any malformed characters are encountered, they are // represented in the converted string as the Unicode Replacement Character (U+FFFD). uint16_t * utf8StringToUTF16String(const uint8_t * utf8String, size_t length); uint32_t * utf8StringToUTF32String(const uint8_t * utf8String, size_t length); uint8_t * utf16StringToUTF8String(const uint16_t * utf16String, size_t length); uint32_t * utf16StringToUTF32String(const uint16_t * utf16String, size_t length); uint8_t * utf32StringToUTF8String(const uint32_t * utf32String, size_t length); uint16_t * utf32StringToUTF16String(const uint32_t * utf32String, size_t length); // Extended functions for converting between encodings. The return value is always // the length of the string in the specified encoding. These can be used for a // variety of purposes: // - If the outUTF*String parameter is NULL, nothing is written, but the function // still returns the length of the input string in the requested encoding. This // can be used to measure the space needed for the conversion to take place. // - If the outUTF*String parameter is non-NULL, the converted string is written // to it. This MUST be large enough to contain the entire transcoded input string, // plus a trailing NUL byte; in other words, it must be at least as large as // sizeof(uint_t) * (utf*StringToUTF*String(source, length, NULL, NULL) + 1). // - If the outMalformed parameter is non-NULL, a boolean value indicating the // malformedness of the input string is written to it. size_t utf8StringToUTF32StringExtended(const uint8_t * utf8String, size_t utf8Length, uint32_t * outUTF32String, bool * outMalformed); size_t utf32StringToUTF8StringExtended(const uint32_t * utf32String, size_t utf32Length, uint8_t * outUTF8String, bool * outMalformed); size_t utf16StringToUTF32StringExtended(const uint16_t * utf16String, size_t utf16Length, uint32_t * outUTF32String, bool * outMalformed); size_t utf32StringToUTF16StringExtended(const uint32_t * utf32String, size_t utf32Length, uint16_t * outUTF16String, bool * outMalformed); size_t utf8StringToUTF16StringExtended(const uint8_t * utf8String, size_t utf8Length, uint16_t * outUTF16String, bool * outMalformed); size_t utf16StringToUTF8StringExtended(const uint16_t * utf16String, size_t utf16Length, uint8_t * outUTF8String, bool * outMalformed); uint32_t utf32CodepointAtUTF8Index(const uint8_t * utf8String, size_t utf8Length, size_t index); uint32_t utf32CodepointAtUTF16Index(const uint16_t * utf16String, size_t utf16Length, size_t index); // Increments ioCharIndex by the number of characters required to read one character forward, returning its UTF-32 codepoint. // Returns UINT32_MAX if the next codepoint is malformed, and 0x0 if ioCharIndex is already beyond the end of the string. uint32_t nextUTF32CodepointInUTF8String(const uint8_t * utf8String, size_t utf8Length, size_t * ioCharIndex); uint32_t nextUTF32CodepointInUTF16String(const uint16_t * utf16String, size_t utf16Length, size_t * ioCharIndex); // Decrements ioCharIndex by the number of characters required to read one character backward, returning its UTF-32 codepoint // Returns UINT32_MAX if the previous codepoint is malformed, and 0x0 if ioCharIndex is already at the beginning of the string. // Any amount of ioCharIndex being beyond the end of the string is treated as if it's just beyond the final character. // If ioCharIndex is at an invalid position in the middle of a valid multi-character sequence, it will correct itself as if // it was exactly at the end of the sequence. Note that this behavior differs somewhat from nextUTF32Codepoint*(), which does // not look behind its current character index and will instead consider the character invalid if in the middle of a sequence. uint32_t previousUTF32CodepointInUTF8String(const uint8_t * utf8String, size_t utf8Length, size_t * ioCharIndex); uint32_t previousUTF32CodepointInUTF16String(const uint16_t * utf16String, size_t utf16Length, size_t * ioCharIndex); static inline unsigned int getUTF8ExpectedCharCount(uint8_t character) { if (character & 0x80) { if ((character & 0xE0) == 0xC0) { return 2; } if ((character & 0xF0) == 0xE0) { return 3; } if ((character & 0xF8) == 0xF0) { return 4; } return 0; } return 1; } static inline uint32_t combine2CharUTF8Sequence(uint8_t char1, uint8_t char2) { if ((char2 & 0xC0) == 0x80) { return ((char1 & 0x1F) << 6) | (char2 & 0x3F); } return UINT32_MAX; } static inline uint32_t combine3CharUTF8Sequence(uint8_t char1, uint8_t char2, uint8_t char3) { if ((char2 & 0xC0) == 0x80 && (char3 & 0xC0) == 0x80) { return ((char1 & 0x0F) << 12) | ((char2 & 0x3F) << 6) | (char3 & 0x3F); } return UINT32_MAX; } static inline uint32_t combine4CharUTF8Sequence(uint8_t char1, uint8_t char2, uint8_t char3, uint8_t char4) { if ((char2 & 0xC0) == 0x80 && (char3 & 0xC0) == 0x80 && (char4 & 0xC0) == 0x80) { return ((char1 & 0x07) << 18) | ((char2 & 0x3F) << 12) | ((char3 & 0x3F) << 6) | (char4 & 0x3F); } return UINT32_MAX; } static inline uint32_t combineUTF8Sequence(unsigned int length, const uint8_t * sequence) { switch (length) { case 1: return sequence[0]; case 2: return combine2CharUTF8Sequence(sequence[0], sequence[1]); case 3: return combine3CharUTF8Sequence(sequence[0], sequence[1], sequence[2]); case 4: return combine4CharUTF8Sequence(sequence[0], sequence[1], sequence[2], sequence[3]); } return UINT32_MAX; } static inline unsigned int getUTF16ExpectedCharCount(uint16_t char1) { if (char1 >= 0xD800 && char1 <= 0xDBFF) { return 2; } if (char1 >= 0xDC00 && char1 <= 0xDFFF) { return 0; } return 1; } static inline uint32_t combine2CharUTF16Sequence(uint16_t char1, uint16_t char2) { if (char2 >= 0xDC00 && char2 <= 0xDFFF) { return (((char1 & 0x03FF) << 10) | (char2 & 0x03FF)) + 0x00010000; } return UINT32_MAX; } static inline uint32_t combineUTF16Sequence(unsigned int length, const uint16_t * sequence) { switch (length) { case 1: return sequence[0]; case 2: return combine2CharUTF16Sequence(sequence[0], sequence[1]); } return UINT32_MAX; } #endif