+/* utf8_safe_number_of_u2s *****************************************************
+
+ Determine number of UTF-16 u2s needed for decoding the given UTF-8 string.
+ (For invalid UTF-8 the U+fffd replacement character will be counted.)
+
+ This function is safe even for invalid UTF-8 strings.
+
+ IN:
+ text..........zero-terminated(!) UTF-8 string (may be invalid)
+ must NOT be NULL
+ nbytes........strlen(text). (This is needed to completely emulate
+ the RI).
+
+ OUT:
+ the number of u2s needed to hold this string in UTF-16 encoding.
+ There is _no_ terminating zero included in this count.
+
+*******************************************************************************/
+
+s4 utf8_safe_number_of_u2s(const char *text, s4 nbytes) {
+ register const unsigned char *t;
+ register s4 byte;
+ register s4 len;
+ register const unsigned char *tlimit;
+ s4 byte1;
+ s4 byte2;
+ s4 byte3;
+ s4 value;
+ s4 skip;
+
+ assert(text);
+ assert(nbytes >= 0);
+
+ len = 0;
+ t = (const unsigned char *) text;
+ tlimit = t + nbytes;
+
+ /* CAUTION: Keep this code in sync with utf8_safe_convert_to_u2s! */
+
+ while (1) {
+ byte = *t++;
+
+ if (byte & 0x80) {
+ /* highest bit set, non-ASCII character */
+
+ if ((byte & 0xe0) == 0xc0) {
+ /* 2-byte: should be 110..... 10...... ? */
+
+ if ((*t++ & 0xc0) == 0x80)
+ ; /* valid 2-byte */
+ else
+ t--; /* invalid */
+ }
+ else if ((byte & 0xf0) == 0xe0) {
+ /* 3-byte: should be 1110.... 10...... 10...... */
+ /* ^t */
+
+ if (t + 2 > tlimit)
+ return len + 1; /* invalid, stop here */
+
+ if ((*t++ & 0xc0) == 0x80) {
+ if ((*t++ & 0xc0) == 0x80)
+ ; /* valid 3-byte */
+ else
+ t--; /* invalid */
+ }
+ else
+ t--; /* invalid */
+ }
+ else if ((byte & 0xf8) == 0xf0) {
+ /* 4-byte: should be 11110... 10...... 10...... 10...... */
+ /* ^t */
+
+ if (t + 3 > tlimit)
+ return len + 1; /* invalid, stop here */
+
+ if (((byte1 = *t++) & 0xc0) == 0x80) {
+ if (((byte2 = *t++) & 0xc0) == 0x80) {
+ if (((byte3 = *t++) & 0xc0) == 0x80) {
+ /* valid 4-byte UTF-8? */
+ value = ((byte & 0x07) << 18)
+ | ((byte1 & 0x3f) << 12)
+ | ((byte2 & 0x3f) << 6)
+ | ((byte3 & 0x3f) );
+
+ if (value > 0x10FFFF)
+ ; /* invalid */
+ else if (value > 0xFFFF)
+ len += 1; /* we need surrogates */
+ else
+ ; /* 16bit suffice */
+ }
+ else
+ t--; /* invalid */
+ }
+ else
+ t--; /* invalid */
+ }
+ else
+ t--; /* invalid */
+ }
+ else if ((byte & 0xfc) == 0xf8) {
+ /* invalid 5-byte */
+ if (t + 4 > tlimit)
+ return len + 1; /* invalid, stop here */
+
+ skip = 4;
+ for (; skip && ((*t & 0xc0) == 0x80); --skip)
+ t++;
+ }
+ else if ((byte & 0xfe) == 0xfc) {
+ /* invalid 6-byte */
+ if (t + 5 > tlimit)
+ return len + 1; /* invalid, stop here */
+
+ skip = 5;
+ for (; skip && ((*t & 0xc0) == 0x80); --skip)
+ t++;
+ }
+ else
+ ; /* invalid */
+ }
+ else {
+ /* NUL */
+
+ if (byte == 0)
+ break;
+
+ /* ASCII character, common case */
+ }
+
+ len++;
+ }
+
+ return len;
+}
+
+
+/* utf8_safe_convert_to_u2s ****************************************************
+
+ Convert the given UTF-8 string to UTF-16 into a pre-allocated buffer.
+ (Invalid UTF-8 will be replaced with the U+fffd replacement character.)
+ Use utf8_safe_number_of_u2s to determine the number of u2s to allocate.
+
+ This function is safe even for invalid UTF-8 strings.
+
+ IN:
+ text..........zero-terminated(!) UTF-8 string (may be invalid)
+ must NOT be NULL
+ nbytes........strlen(text). (This is needed to completely emulate
+ the RI).
+ buffer........a preallocated array of u2s to receive the decoded
+ string. Use utf8_safe_number_of_u2s to get the
+ required number of u2s for allocating this.
+
+*******************************************************************************/
+
+#define UNICODE_REPLACEMENT 0xfffd
+
+void utf8_safe_convert_to_u2s(const char *text, s4 nbytes, u2 *buffer) {
+ register const unsigned char *t;
+ register s4 byte;
+ register const unsigned char *tlimit;
+ s4 byte1;
+ s4 byte2;
+ s4 byte3;
+ s4 value;
+ s4 skip;
+
+ assert(text);
+ assert(nbytes >= 0);
+
+ t = (const unsigned char *) text;
+ tlimit = t + nbytes;
+
+ /* CAUTION: Keep this code in sync with utf8_safe_number_of_u2s! */
+
+ while (1) {
+ byte = *t++;
+
+ if (byte & 0x80) {
+ /* highest bit set, non-ASCII character */
+
+ if ((byte & 0xe0) == 0xc0) {
+ /* 2-byte: should be 110..... 10...... */
+
+ if (((byte1 = *t++) & 0xc0) == 0x80) {
+ /* valid 2-byte UTF-8 */
+ *buffer++ = ((byte & 0x1f) << 6)
+ | ((byte1 & 0x3f) );
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else if ((byte & 0xf0) == 0xe0) {
+ /* 3-byte: should be 1110.... 10...... 10...... */
+
+ if (t + 2 > tlimit) {
+ *buffer++ = UNICODE_REPLACEMENT;
+ return;
+ }
+
+ if (((byte1 = *t++) & 0xc0) == 0x80) {
+ if (((byte2 = *t++) & 0xc0) == 0x80) {
+ /* valid 3-byte UTF-8 */
+ *buffer++ = ((byte & 0x0f) << 12)
+ | ((byte1 & 0x3f) << 6)
+ | ((byte2 & 0x3f) );
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else if ((byte & 0xf8) == 0xf0) {
+ /* 4-byte: should be 11110... 10...... 10...... 10...... */
+
+ if (t + 3 > tlimit) {
+ *buffer++ = UNICODE_REPLACEMENT;
+ return;
+ }
+
+ if (((byte1 = *t++) & 0xc0) == 0x80) {
+ if (((byte2 = *t++) & 0xc0) == 0x80) {
+ if (((byte3 = *t++) & 0xc0) == 0x80) {
+ /* valid 4-byte UTF-8? */
+ value = ((byte & 0x07) << 18)
+ | ((byte1 & 0x3f) << 12)
+ | ((byte2 & 0x3f) << 6)
+ | ((byte3 & 0x3f) );
+
+ if (value > 0x10FFFF) {
+ *buffer++ = UNICODE_REPLACEMENT;
+ }
+ else if (value > 0xFFFF) {
+ /* we need surrogates */
+ *buffer++ = 0xd800 | ((value >> 10) - 0x40);
+ *buffer++ = 0xdc00 | (value & 0x03ff);
+ }
+ else
+ *buffer++ = value; /* 16bit suffice */
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else {
+ *buffer++ = UNICODE_REPLACEMENT;
+ t--;
+ }
+ }
+ else if ((byte & 0xfc) == 0xf8) {
+ if (t + 4 > tlimit) {
+ *buffer++ = UNICODE_REPLACEMENT;
+ return;
+ }
+
+ skip = 4;
+ for (; skip && ((*t & 0xc0) == 0x80); --skip)
+ t++;
+ *buffer++ = UNICODE_REPLACEMENT;
+ }
+ else if ((byte & 0xfe) == 0xfc) {
+ if (t + 5 > tlimit) {
+ *buffer++ = UNICODE_REPLACEMENT;
+ return;
+ }
+
+ skip = 5;
+ for (; skip && ((*t & 0xc0) == 0x80); --skip)
+ t++;
+ *buffer++ = UNICODE_REPLACEMENT;
+ }
+ else
+ *buffer++ = UNICODE_REPLACEMENT;
+ }
+ else {
+ /* NUL */
+
+ if (byte == 0)
+ break;
+
+ /* ASCII character, common case */
+
+ *buffer++ = byte;
+ }
+ }
+}
+
+