1 // Copyright (C) 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ****************************************************************************** 5 * 6 * Copyright (C) 1999-2012, International Business Machines 7 * Corporation and others. All Rights Reserved. 8 * 9 ****************************************************************************** 10 * file name: utf_impl.c 11 * encoding: US-ASCII 12 * tab size: 8 (not used) 13 * indentation:4 14 * 15 * created on: 1999sep13 16 * created by: Markus W. Scherer 17 * 18 * This file provides implementation functions for macros in the utfXX.h 19 * that would otherwise be too long as macros. 20 */ 21 22 /* set import/export definitions */ 23 #ifndef U_UTF8_IMPL 24 # define U_UTF8_IMPL 25 #endif 26 27 #include "unicode/utypes.h" 28 #include "unicode/utf.h" 29 #include "unicode/utf8.h" 30 #include "unicode/utf_old.h" 31 #include "uassert.h" 32 33 /* 34 * This table could be replaced on many machines by 35 * a few lines of assembler code using an 36 * "index of first 0-bit from msb" instruction and 37 * one or two more integer instructions. 38 * 39 * For example, on an i386, do something like 40 * - MOV AL, leadByte 41 * - NOT AL (8-bit, leave b15..b8==0..0, reverse only b7..b0) 42 * - MOV AH, 0 43 * - BSR BX, AX (16-bit) 44 * - MOV AX, 6 (result) 45 * - JZ finish (ZF==1 if leadByte==0xff) 46 * - SUB AX, BX (result) 47 * -finish: 48 * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB) 49 * 50 * In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal; 51 * lead bytes above 0xf4 are illegal. 52 * We keep them in this table for skipping long ISO 10646-UTF-8 sequences. 53 */ 54 U_EXPORT const uint8_t 55 utf8_countTrailBytes[256]={ 56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60 61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70 71 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 72 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 73 74 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 75 3, 3, 3, 3, 3, 76 3, 3, 3, /* illegal in Unicode */ 77 4, 4, 4, 4, /* illegal in Unicode */ 78 5, 5, /* illegal in Unicode */ 79 0, 0 /* illegal bytes 0xfe and 0xff */ 80 }; 81 82 static const UChar32 83 utf8_minLegal[4]={ 0, 0x80, 0x800, 0x10000 }; 84 85 static const UChar32 86 utf8_errorValue[6]={ 87 UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_2, UTF_ERROR_VALUE, 0x10ffff, 88 0x3ffffff, 0x7fffffff 89 }; 90 91 static UChar32 92 errorValue(int32_t count, int8_t strict) { 93 if(strict>=0) { 94 return utf8_errorValue[count]; 95 } else if(strict==-3) { 96 return 0xfffd; 97 } else { 98 return U_SENTINEL; 99 } 100 } 101 102 /* 103 * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros 104 * and their obsolete sibling UTF8_NEXT_CHAR_SAFE(). 105 * 106 * U8_NEXT() supports NUL-terminated strings indicated via length<0. 107 * 108 * The "strict" parameter controls the error behavior: 109 * <0 "Safe" behavior of U8_NEXT(): 110 * -1: All illegal byte sequences yield U_SENTINEL=-1. 111 * -2: Same as -1, except for lenient treatment of surrogate code points as legal. 112 * Some implementations use this for roundtripping of 113 * Unicode 16-bit strings that are not well-formed UTF-16, that is, they 114 * contain unpaired surrogates. 115 * -3: All illegal byte sequences yield U+FFFD. 116 * 0 Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE): 117 * All illegal byte sequences yield a positive code point such that this 118 * result code point would be encoded with the same number of bytes as 119 * the illegal sequence. 120 * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., TRUE): 121 * Same as the obsolete "safe" behavior, but non-characters are also treated 122 * like illegal sequences. 123 * 124 * Note that a UBool is the same as an int8_t. 125 */ 126 U_CAPI UChar32 U_EXPORT2 127 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) { 128 int32_t i=*pi; 129 uint8_t count=U8_COUNT_TRAIL_BYTES(c); 130 U_ASSERT(count <= 5); /* U8_COUNT_TRAIL_BYTES returns value 0...5 */ 131 if(i+count<=length || length<0) { 132 uint8_t trail; 133 134 U8_MASK_LEAD_BYTE(c, count); 135 /* support NUL-terminated strings: do not read beyond the first non-trail byte */ 136 switch(count) { 137 /* each branch falls through to the next one */ 138 case 0: 139 /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */ 140 case 5: 141 case 4: 142 /* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */ 143 break; 144 case 3: 145 trail=s[i++]-0x80; 146 c=(c<<6)|trail; 147 /* c>=0x110 would result in code point>0x10ffff, outside Unicode */ 148 if(c>=0x110 || trail>0x3f) { break; } 149 case 2: 150 trail=s[i++]-0x80; 151 c=(c<<6)|trail; 152 /* 153 * test for a surrogate d800..dfff unless we are lenient: 154 * before the last (c<<6), a surrogate is c=360..37f 155 */ 156 if(((c&0xffe0)==0x360 && strict!=-2) || trail>0x3f) { break; } 157 case 1: 158 trail=s[i++]-0x80; 159 c=(c<<6)|trail; 160 if(trail>0x3f) { break; } 161 /* correct sequence - all trail bytes have (b7..b6)==(10) */ 162 if(c>=utf8_minLegal[count] && 163 /* strict: forbid non-characters like U+fffe */ 164 (strict<=0 || !U_IS_UNICODE_NONCHAR(c))) { 165 *pi=i; 166 return c; 167 } 168 /* no default branch to optimize switch() - all values are covered */ 169 } 170 } else { 171 /* too few bytes left */ 172 count=length-i; 173 } 174 175 /* error handling */ 176 i=*pi; 177 while(count>0 && U8_IS_TRAIL(s[i])) { 178 ++i; 179 --count; 180 } 181 c=errorValue(i-*pi, strict); 182 *pi=i; 183 return c; 184 } 185 186 U_CAPI int32_t U_EXPORT2 187 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError) { 188 if((uint32_t)(c)<=0x7ff) { 189 if((i)+1<(length)) { 190 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); 191 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); 192 return i; 193 } 194 } else if((uint32_t)(c)<=0xffff) { 195 /* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8. */ 196 if((i)+2<(length) && !U_IS_SURROGATE(c)) { 197 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); 198 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); 199 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); 200 return i; 201 } 202 } else if((uint32_t)(c)<=0x10ffff) { 203 if((i)+3<(length)) { 204 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); 205 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); 206 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); 207 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); 208 return i; 209 } 210 } 211 /* c>0x10ffff or not enough space, write an error value */ 212 if(pIsError!=NULL) { 213 *pIsError=TRUE; 214 } else { 215 length-=i; 216 if(length>0) { 217 int32_t offset; 218 if(length>3) { 219 length=3; 220 } 221 s+=i; 222 offset=0; 223 c=utf8_errorValue[length-1]; 224 UTF8_APPEND_CHAR_UNSAFE(s, offset, c); 225 i=i+offset; 226 } 227 } 228 return i; 229 } 230 231 U_CAPI UChar32 U_EXPORT2 232 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) { 233 int32_t i=*pi; 234 uint8_t b, count=1, shift=6; 235 236 if(!U8_IS_TRAIL(c)) { return errorValue(0, strict); } 237 238 /* extract value bits from the last trail byte */ 239 c&=0x3f; 240 241 for(;;) { 242 if(i<=start) { 243 /* no lead byte at all */ 244 return errorValue(0, strict); 245 } 246 247 /* read another previous byte */ 248 b=s[--i]; 249 if((uint8_t)(b-0x80)<0x7e) { /* 0x80<=b<0xfe */ 250 if(b&0x40) { 251 /* lead byte, this will always end the loop */ 252 uint8_t shouldCount=U8_COUNT_TRAIL_BYTES(b); 253 254 if(count==shouldCount) { 255 /* set the new position */ 256 *pi=i; 257 U8_MASK_LEAD_BYTE(b, count); 258 c|=(UChar32)b<<shift; 259 if(count>=4 || c>0x10ffff || c<utf8_minLegal[count] || (U_IS_SURROGATE(c) && strict!=-2) || (strict>0 && U_IS_UNICODE_NONCHAR(c))) { 260 /* illegal sequence or (strict and non-character) */ 261 if(count>=4) { 262 count=3; 263 } 264 c=errorValue(count, strict); 265 } else { 266 /* exit with correct c */ 267 } 268 } else { 269 /* the lead byte does not match the number of trail bytes */ 270 /* only set the position to the lead byte if it would 271 include the trail byte that we started with */ 272 if(count<shouldCount) { 273 *pi=i; 274 c=errorValue(count, strict); 275 } else { 276 c=errorValue(0, strict); 277 } 278 } 279 break; 280 } else if(count<5) { 281 /* trail byte */ 282 c|=(UChar32)(b&0x3f)<<shift; 283 ++count; 284 shift+=6; 285 } else { 286 /* more than 5 trail bytes is illegal */ 287 c=errorValue(0, strict); 288 break; 289 } 290 } else { 291 /* single-byte character precedes trailing bytes */ 292 c=errorValue(0, strict); 293 break; 294 } 295 } 296 return c; 297 } 298 299 U_CAPI int32_t U_EXPORT2 300 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i) { 301 /* i had been decremented once before the function call */ 302 int32_t I=i, Z; 303 uint8_t b; 304 305 /* read at most the 6 bytes s[Z] to s[i], inclusively */ 306 if(I-5>start) { 307 Z=I-5; 308 } else { 309 Z=start; 310 } 311 312 /* return I if the sequence starting there is long enough to include i */ 313 do { 314 b=s[I]; 315 if((uint8_t)(b-0x80)>=0x7e) { /* not 0x80<=b<0xfe */ 316 break; 317 } else if(b>=0xc0) { 318 if(U8_COUNT_TRAIL_BYTES(b)>=(i-I)) { 319 return I; 320 } else { 321 break; 322 } 323 } 324 } while(Z<=--I); 325 326 /* return i itself to be consistent with the FWD_1 macro */ 327 return i; 328 } 329