michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 1999-2012, 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: /* set import/export definitions */ michael@0: #ifndef U_UTF8_IMPL michael@0: # define U_UTF8_IMPL michael@0: #endif michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf8.h" michael@0: #include "unicode/utf_old.h" michael@0: #include "uassert.h" 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: U_EXPORT const uint8_t 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: UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_2, UTF_ERROR_VALUE, 0x10ffff, michael@0: 0x3ffffff, 0x7fffffff michael@0: }; michael@0: michael@0: static UChar32 michael@0: errorValue(int32_t count, int8_t strict) { michael@0: if(strict>=0) { michael@0: return utf8_errorValue[count]; michael@0: } else if(strict==-3) { michael@0: return 0xfffd; michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros michael@0: * and their obsolete sibling UTF8_NEXT_CHAR_SAFE(). michael@0: * michael@0: * U8_NEXT() supports NUL-terminated strings indicated via length<0. michael@0: * michael@0: * The "strict" parameter controls the error behavior: michael@0: * <0 "Safe" behavior of U8_NEXT(): michael@0: * -1: All illegal byte sequences yield U_SENTINEL=-1. michael@0: * -2: Same as -1, except for lenient treatment of surrogate code points as legal. michael@0: * 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: * -3: All illegal byte sequences yield U+FFFD. 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: * Note that a UBool is the same as an int8_t. michael@0: */ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) { michael@0: int32_t i=*pi; michael@0: uint8_t count=U8_COUNT_TRAIL_BYTES(c); michael@0: U_ASSERT(count <= 5); /* U8_COUNT_TRAIL_BYTES returns value 0...5 */ michael@0: if(i+count<=length || length<0) { michael@0: uint8_t trail; michael@0: michael@0: U8_MASK_LEAD_BYTE(c, count); michael@0: /* support NUL-terminated strings: do not read beyond the first non-trail byte */ michael@0: switch(count) { michael@0: /* each branch falls through to the next one */ michael@0: case 0: michael@0: /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */ 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: break; michael@0: case 3: michael@0: trail=s[i++]-0x80; michael@0: c=(c<<6)|trail; michael@0: /* c>=0x110 would result in code point>0x10ffff, outside Unicode */ michael@0: if(c>=0x110 || trail>0x3f) { break; } michael@0: case 2: michael@0: trail=s[i++]-0x80; michael@0: c=(c<<6)|trail; michael@0: /* michael@0: * test for a surrogate d800..dfff unless we are lenient: michael@0: * before the last (c<<6), a surrogate is c=360..37f michael@0: */ michael@0: if(((c&0xffe0)==0x360 && strict!=-2) || trail>0x3f) { break; } michael@0: case 1: michael@0: trail=s[i++]-0x80; michael@0: c=(c<<6)|trail; michael@0: if(trail>0x3f) { break; } michael@0: /* correct sequence - all trail bytes have (b7..b6)==(10) */ michael@0: if(c>=utf8_minLegal[count] && michael@0: /* strict: forbid non-characters like U+fffe */ michael@0: (strict<=0 || !U_IS_UNICODE_NONCHAR(c))) { michael@0: *pi=i; michael@0: return c; michael@0: } michael@0: /* no default branch to optimize switch() - all values are covered */ michael@0: } michael@0: } else { michael@0: /* too few bytes left */ michael@0: count=length-i; michael@0: } michael@0: michael@0: /* error handling */ michael@0: i=*pi; michael@0: while(count>0 && U8_IS_TRAIL(s[i])) { michael@0: ++i; michael@0: --count; michael@0: } michael@0: c=errorValue(i-*pi, strict); michael@0: *pi=i; michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError) { michael@0: if((uint32_t)(c)<=0x7ff) { michael@0: if((i)+1<(length)) { michael@0: (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); michael@0: return i; michael@0: } michael@0: } else if((uint32_t)(c)<=0xffff) { michael@0: /* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8. */ michael@0: if((i)+2<(length) && !U_IS_SURROGATE(c)) { michael@0: (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); michael@0: (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); michael@0: return i; michael@0: } michael@0: } else if((uint32_t)(c)<=0x10ffff) { michael@0: if((i)+3<(length)) { michael@0: (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); michael@0: (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); michael@0: (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); michael@0: return i; michael@0: } michael@0: } michael@0: /* c>0x10ffff or not enough space, write an error value */ michael@0: if(pIsError!=NULL) { michael@0: *pIsError=TRUE; michael@0: } else { michael@0: length-=i; michael@0: if(length>0) { michael@0: int32_t offset; michael@0: if(length>3) { michael@0: length=3; michael@0: } michael@0: s+=i; michael@0: offset=0; michael@0: c=utf8_errorValue[length-1]; michael@0: UTF8_APPEND_CHAR_UNSAFE(s, offset, c); michael@0: i=i+offset; michael@0: } michael@0: } michael@0: return i; michael@0: } michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) { michael@0: int32_t i=*pi; michael@0: uint8_t b, count=1, shift=6; michael@0: michael@0: if(!U8_IS_TRAIL(c)) { return errorValue(0, strict); } michael@0: michael@0: /* extract value bits from the last trail byte */ michael@0: c&=0x3f; michael@0: michael@0: for(;;) { michael@0: if(i<=start) { michael@0: /* no lead byte at all */ michael@0: return errorValue(0, strict); michael@0: } michael@0: michael@0: /* read another previous byte */ michael@0: b=s[--i]; michael@0: if((uint8_t)(b-0x80)<0x7e) { /* 0x80<=b<0xfe */ michael@0: if(b&0x40) { michael@0: /* lead byte, this will always end the loop */ michael@0: uint8_t shouldCount=U8_COUNT_TRAIL_BYTES(b); michael@0: michael@0: if(count==shouldCount) { michael@0: /* set the new position */ michael@0: *pi=i; michael@0: U8_MASK_LEAD_BYTE(b, count); michael@0: c|=(UChar32)b<=4 || c>0x10ffff || c0 && U_IS_UNICODE_NONCHAR(c))) { michael@0: /* illegal sequence or (strict and non-character) */ michael@0: if(count>=4) { michael@0: count=3; michael@0: } michael@0: c=errorValue(count, strict); michael@0: } else { michael@0: /* exit with correct c */ michael@0: } michael@0: } else { michael@0: /* the lead byte does not match the number of trail bytes */ michael@0: /* only set the position to the lead byte if it would michael@0: include the trail byte that we started with */ michael@0: if(countstart) { michael@0: Z=I-5; michael@0: } else { michael@0: Z=start; michael@0: } michael@0: michael@0: /* return I if the sequence starting there is long enough to include i */ michael@0: do { michael@0: b=s[I]; michael@0: if((uint8_t)(b-0x80)>=0x7e) { /* not 0x80<=b<0xfe */ michael@0: break; michael@0: } else if(b>=0xc0) { michael@0: if(U8_COUNT_TRAIL_BYTES(b)>=(i-I)) { michael@0: return I; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: } while(Z<=--I); michael@0: michael@0: /* return i itself to be consistent with the FWD_1 macro */ michael@0: return i; michael@0: }