michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 1999-2006, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * file name: utf_impl.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999sep13 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This file provides implementation functions for macros in the utfXX.h michael@0: * that would otherwise be too long as macros. michael@0: */ michael@0: michael@0: #include "base/third_party/icu/icu_utf.h" michael@0: michael@0: namespace base_icu { michael@0: michael@0: /** michael@0: * UTF8_ERROR_VALUE_1 and UTF8_ERROR_VALUE_2 are special error values for UTF-8, michael@0: * which need 1 or 2 bytes in UTF-8: michael@0: * \code michael@0: * U+0015 = NAK = Negative Acknowledge, C0 control character michael@0: * U+009f = highest C1 control character michael@0: * \endcode michael@0: * michael@0: * These are used by UTF8_..._SAFE macros so that they can return an error value michael@0: * that needs the same number of code units (bytes) as were seen by michael@0: * a macro. They should be tested with UTF_IS_ERROR() or UTF_IS_VALID(). michael@0: * michael@0: * @deprecated ICU 2.4. Obsolete, see utf_old.h. michael@0: */ michael@0: #define CBUTF8_ERROR_VALUE_1 0x15 michael@0: michael@0: /** michael@0: * See documentation on UTF8_ERROR_VALUE_1 for details. michael@0: * michael@0: * @deprecated ICU 2.4. Obsolete, see utf_old.h. michael@0: */ michael@0: #define CBUTF8_ERROR_VALUE_2 0x9f michael@0: michael@0: michael@0: /** michael@0: * Error value for all UTFs. This code point value will be set by macros with e> michael@0: * checking if an error is detected. michael@0: * michael@0: * @deprecated ICU 2.4. Obsolete, see utf_old.h. michael@0: */ michael@0: #define CBUTF_ERROR_VALUE 0xffff michael@0: michael@0: /* michael@0: * This table could be replaced on many machines by michael@0: * a few lines of assembler code using an michael@0: * "index of first 0-bit from msb" instruction and michael@0: * one or two more integer instructions. michael@0: * michael@0: * For example, on an i386, do something like michael@0: * - MOV AL, leadByte michael@0: * - NOT AL (8-bit, leave b15..b8==0..0, reverse only b7..b0) michael@0: * - MOV AH, 0 michael@0: * - BSR BX, AX (16-bit) michael@0: * - MOV AX, 6 (result) michael@0: * - JZ finish (ZF==1 if leadByte==0xff) michael@0: * - SUB AX, BX (result) michael@0: * -finish: michael@0: * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB) michael@0: * michael@0: * In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal; michael@0: * lead bytes above 0xf4 are illegal. michael@0: * We keep them in this table for skipping long ISO 10646-UTF-8 sequences. michael@0: */ michael@0: const uint8 michael@0: utf8_countTrailBytes[256]={ michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: michael@0: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, michael@0: 3, 3, 3, 3, 3, michael@0: 3, 3, 3, /* illegal in Unicode */ michael@0: 4, 4, 4, 4, /* illegal in Unicode */ michael@0: 5, 5, /* illegal in Unicode */ michael@0: 0, 0 /* illegal bytes 0xfe and 0xff */ michael@0: }; michael@0: michael@0: static const UChar32 michael@0: utf8_minLegal[4]={ 0, 0x80, 0x800, 0x10000 }; michael@0: michael@0: static const UChar32 michael@0: utf8_errorValue[6]={ michael@0: CBUTF8_ERROR_VALUE_1, CBUTF8_ERROR_VALUE_2, CBUTF_ERROR_VALUE, 0x10ffff, michael@0: 0x3ffffff, 0x7fffffff michael@0: }; michael@0: michael@0: /* michael@0: * Handle the non-inline part of the U8_NEXT() macro and its obsolete sibling michael@0: * UTF8_NEXT_CHAR_SAFE(). michael@0: * michael@0: * The "strict" parameter controls the error behavior: michael@0: * <0 "Safe" behavior of U8_NEXT(): All illegal byte sequences yield a negative michael@0: * code point result. michael@0: * 0 Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE): michael@0: * All illegal byte sequences yield a positive code point such that this michael@0: * result code point would be encoded with the same number of bytes as michael@0: * the illegal sequence. michael@0: * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., TRUE): michael@0: * Same as the obsolete "safe" behavior, but non-characters are also treated michael@0: * like illegal sequences. michael@0: * michael@0: * The special negative (<0) value -2 is used for lenient treatment of surrogate michael@0: * code points as legal. Some implementations use this for roundtripping of michael@0: * Unicode 16-bit strings that are not well-formed UTF-16, that is, they michael@0: * contain unpaired surrogates. michael@0: * michael@0: * Note that a UBool is the same as an int8_t. michael@0: */ michael@0: UChar32 michael@0: utf8_nextCharSafeBody(const uint8 *s, int32 *pi, int32 length, UChar32 c, UBool strict) { michael@0: int32 i=*pi; michael@0: uint8 count=CBU8_COUNT_TRAIL_BYTES(c); michael@0: if((i)+count<=(length)) { michael@0: uint8 trail, illegal=0; michael@0: michael@0: CBU8_MASK_LEAD_BYTE((c), count); michael@0: /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */ michael@0: switch(count) { michael@0: /* each branch falls through to the next one */ michael@0: case 5: michael@0: case 4: michael@0: /* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */ michael@0: illegal=1; michael@0: break; michael@0: case 3: michael@0: trail=s[(i)++]; michael@0: (c)=((c)<<6)|(trail&0x3f); michael@0: if(c<0x110) { michael@0: illegal|=(trail&0xc0)^0x80; michael@0: } else { michael@0: /* code point>0x10ffff, outside Unicode */ michael@0: illegal=1; michael@0: break; michael@0: } michael@0: case 2: michael@0: trail=s[(i)++]; michael@0: (c)=((c)<<6)|(trail&0x3f); michael@0: illegal|=(trail&0xc0)^0x80; michael@0: case 1: michael@0: trail=s[(i)++]; michael@0: (c)=((c)<<6)|(trail&0x3f); michael@0: illegal|=(trail&0xc0)^0x80; michael@0: break; michael@0: case 0: michael@0: if(strict>=0) { michael@0: return CBUTF8_ERROR_VALUE_1; michael@0: } else { michael@0: return CBU_SENTINEL; michael@0: } michael@0: /* no default branch to optimize switch() - all values are covered */ michael@0: } michael@0: michael@0: /* michael@0: * All the error handling should return a value michael@0: * that needs count bytes so that UTF8_GET_CHAR_SAFE() works right. michael@0: * michael@0: * Starting with Unicode 3.0.1, non-shortest forms are illegal. michael@0: * Starting with Unicode 3.2, surrogate code points must not be michael@0: * encoded in UTF-8, and there are no irregular sequences any more. michael@0: * michael@0: * U8_ macros (new in ICU 2.4) return negative values for error conditions. michael@0: */ michael@0: michael@0: /* correct sequence - all trail bytes have (b7..b6)==(10)? */ michael@0: /* illegal is also set if count>=4 */ michael@0: if(illegal || (c)0 && CBU8_IS_TRAIL(s[i])) { michael@0: ++(i); michael@0: --count; michael@0: } michael@0: if(strict>=0) { michael@0: c=utf8_errorValue[errorCount-count]; michael@0: } else { michael@0: c=CBU_SENTINEL; michael@0: } michael@0: } else if((strict)>0 && CBU_IS_UNICODE_NONCHAR(c)) { michael@0: /* strict: forbid non-characters like U+fffe */ michael@0: c=utf8_errorValue[count]; michael@0: } michael@0: } else /* too few bytes left */ { michael@0: /* error handling */ michael@0: int32 i0=i; michael@0: /* don't just set (i)=(length) in case there is an illegal sequence */ michael@0: while((i)<(length) && CBU8_IS_TRAIL(s[i])) { michael@0: ++(i); michael@0: } michael@0: if(strict>=0) { michael@0: c=utf8_errorValue[i-i0]; michael@0: } else { michael@0: c=CBU_SENTINEL; michael@0: } michael@0: } michael@0: *pi=i; michael@0: return c; michael@0: } michael@0: michael@0: } // namespace base_icu