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