1 /* 2 ** 2004 April 13 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains routines used to translate between UTF-8, 13 ** UTF-16, UTF-16BE, and UTF-16LE. 14 ** 15 ** $Id: utf.c,v 1.52 2007/07/23 19:12:42 drh Exp $ 16 ** 17 ** Notes on UTF-8: 18 ** 19 ** Byte-0 Byte-1 Byte-2 Byte-3 Value 20 ** 0xxxxxxx 00000000 00000000 0xxxxxxx 21 ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx 22 ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx 23 ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx 24 ** 25 ** 26 ** Notes on UTF-16: (with wwww+1==uuuuu) 27 ** 28 ** Word-0 Word-1 Value 29 ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx 30 ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx 31 ** 32 ** 33 ** BOM or Byte Order Mark: 34 ** 0xff 0xfe little-endian utf-16 follows 35 ** 0xfe 0xff big-endian utf-16 follows 36 ** 37 */ 38 #include "sqliteInt.h" 39 #include <assert.h> 40 #include "vdbeInt.h" 41 42 /* 43 ** The following constant value is used by the SQLITE_BIGENDIAN and 44 ** SQLITE_LITTLEENDIAN macros. 45 */ 46 const int sqlite3one = 1; 47 48 /* 49 ** This lookup table is used to help decode the first byte of 50 ** a multi-byte UTF8 character. 51 */ 52 const unsigned char sqlite3UtfTrans1[] = { 53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 54 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 55 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 56 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 57 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 58 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 59 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 60 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, 61 }; 62 63 64 #define WRITE_UTF8(zOut, c) { \ 65 if( c<0x00080 ){ \ 66 *zOut++ = (c&0xFF); \ 67 } \ 68 else if( c<0x00800 ){ \ 69 *zOut++ = 0xC0 + ((c>>6)&0x1F); \ 70 *zOut++ = 0x80 + (c & 0x3F); \ 71 } \ 72 else if( c<0x10000 ){ \ 73 *zOut++ = 0xE0 + ((c>>12)&0x0F); \ 74 *zOut++ = 0x80 + ((c>>6) & 0x3F); \ 75 *zOut++ = 0x80 + (c & 0x3F); \ 76 }else{ \ 77 *zOut++ = 0xF0 + ((c>>18) & 0x07); \ 78 *zOut++ = 0x80 + ((c>>12) & 0x3F); \ 79 *zOut++ = 0x80 + ((c>>6) & 0x3F); \ 80 *zOut++ = 0x80 + (c & 0x3F); \ 81 } \ 82 } 83 84 #define WRITE_UTF16LE(zOut, c) { \ 85 if( c<=0xFFFF ){ \ 86 *zOut++ = (c&0x00FF); \ 87 *zOut++ = ((c>>8)&0x00FF); \ 88 }else{ \ 89 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ 90 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \ 91 *zOut++ = (c&0x00FF); \ 92 *zOut++ = (0x00DC + ((c>>8)&0x03)); \ 93 } \ 94 } 95 96 #define WRITE_UTF16BE(zOut, c) { \ 97 if( c<=0xFFFF ){ \ 98 *zOut++ = ((c>>8)&0x00FF); \ 99 *zOut++ = (c&0x00FF); \ 100 }else{ \ 101 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \ 102 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ 103 *zOut++ = (0x00DC + ((c>>8)&0x03)); \ 104 *zOut++ = (c&0x00FF); \ 105 } \ 106 } 107 108 #define READ_UTF16LE(zIn, c){ \ 109 c = (*zIn++); \ 110 c += ((*zIn++)<<8); \ 111 if( c>=0xD800 && c<0xE000 ){ \ 112 int c2 = (*zIn++); \ 113 c2 += ((*zIn++)<<8); \ 114 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ 115 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \ 116 } \ 117 } 118 119 #define READ_UTF16BE(zIn, c){ \ 120 c = ((*zIn++)<<8); \ 121 c += (*zIn++); \ 122 if( c>=0xD800 && c<0xE000 ){ \ 123 int c2 = ((*zIn++)<<8); \ 124 c2 += (*zIn++); \ 125 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ 126 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \ 127 } \ 128 } 129 130 /* 131 ** Translate a single UTF-8 character. Return the unicode value. 132 ** 133 ** During translation, assume that the byte that zTerm points 134 ** is a 0x00. 135 ** 136 ** Write a pointer to the next unread byte back into *pzNext. 137 ** 138 ** Notes On Invalid UTF-8: 139 ** 140 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to 141 ** be encoded as a multi-byte character. Any multi-byte character that 142 ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd. 143 ** 144 ** * This routine never allows a UTF16 surrogate value to be encoded. 145 ** If a multi-byte character attempts to encode a value between 146 ** 0xd800 and 0xe000 then it is rendered as 0xfffd. 147 ** 148 ** * Bytes in the range of 0x80 through 0xbf which occur as the first 149 ** byte of a character are interpreted as single-byte characters 150 ** and rendered as themselves even though they are technically 151 ** invalid characters. 152 ** 153 ** * This routine accepts an infinite number of different UTF8 encodings 154 ** for unicode values 0x80 and greater. It do not change over-length 155 ** encodings to 0xfffd as some systems recommend. 156 */ 157 int sqlite3Utf8Read( 158 const unsigned char *z, /* First byte of UTF-8 character */ 159 const unsigned char *zTerm, /* Pretend this byte is 0x00 */ 160 const unsigned char **pzNext /* Write first byte past UTF-8 char here */ 161 ){ 162 int c = *(z++); 163 if( c>=0xc0 ){ 164 c = sqlite3UtfTrans1[c-0xc0]; 165 while( z!=zTerm && (*z & 0xc0)==0x80 ){ 166 c = (c<<6) + (0x3f & *(z++)); 167 } 168 if( c<0x80 169 || (c&0xFFFFF800)==0xD800 170 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } 171 } 172 *pzNext = z; 173 return c; 174 } 175 176 177 178 /* 179 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is 180 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). 181 */ 182 /* #define TRANSLATE_TRACE 1 */ 183 184 #ifndef SQLITE_OMIT_UTF16 185 /* 186 ** This routine transforms the internal text encoding used by pMem to 187 ** desiredEnc. It is an error if the string is already of the desired 188 ** encoding, or if *pMem does not contain a string value. 189 */ 190 int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ 191 unsigned char zShort[NBFS]; /* Temporary short output buffer */ 192 int len; /* Maximum length of output string in bytes */ 193 unsigned char *zOut; /* Output buffer */ 194 unsigned char *zIn; /* Input iterator */ 195 unsigned char *zTerm; /* End of input */ 196 unsigned char *z; /* Output iterator */ 197 unsigned int c; 198 199 assert( pMem->flags&MEM_Str ); 200 assert( pMem->enc!=desiredEnc ); 201 assert( pMem->enc!=0 ); 202 assert( pMem->n>=0 ); 203 204 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) 205 { 206 char zBuf[100]; 207 sqlite3VdbeMemPrettyPrint(pMem, zBuf); 208 fprintf(stderr, "INPUT: %s\n", zBuf); 209 } 210 #endif 211 212 /* If the translation is between UTF-16 little and big endian, then 213 ** all that is required is to swap the byte order. This case is handled 214 ** differently from the others. 215 */ 216 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ 217 u8 temp; 218 int rc; 219 rc = sqlite3VdbeMemMakeWriteable(pMem); 220 if( rc!=SQLITE_OK ){ 221 assert( rc==SQLITE_NOMEM ); 222 return SQLITE_NOMEM; 223 } 224 zIn = (u8*)pMem->z; 225 zTerm = &zIn[pMem->n]; 226 while( zIn<zTerm ){ 227 temp = *zIn; 228 *zIn = *(zIn+1); 229 zIn++; 230 *zIn++ = temp; 231 } 232 pMem->enc = desiredEnc; 233 goto translate_out; 234 } 235 236 /* Set len to the maximum number of bytes required in the output buffer. */ 237 if( desiredEnc==SQLITE_UTF8 ){ 238 /* When converting from UTF-16, the maximum growth results from 239 ** translating a 2-byte character to a 4-byte UTF-8 character. 240 ** A single byte is required for the output string 241 ** nul-terminator. 242 */ 243 len = pMem->n * 2 + 1; 244 }else{ 245 /* When converting from UTF-8 to UTF-16 the maximum growth is caused 246 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 247 ** character. Two bytes are required in the output buffer for the 248 ** nul-terminator. 249 */ 250 len = pMem->n * 2 + 2; 251 } 252 253 /* Set zIn to point at the start of the input buffer and zTerm to point 1 254 ** byte past the end. 255 ** 256 ** Variable zOut is set to point at the output buffer. This may be space 257 ** obtained from malloc(), or Mem.zShort, if it large enough and not in 258 ** use, or the zShort array on the stack (see above). 259 */ 260 zIn = (u8*)pMem->z; 261 zTerm = &zIn[pMem->n]; 262 if( len>NBFS ){ 263 zOut = sqliteMallocRaw(len); 264 if( !zOut ) return SQLITE_NOMEM; 265 }else{ 266 zOut = zShort; 267 } 268 z = zOut; 269 270 if( pMem->enc==SQLITE_UTF8 ){ 271 if( desiredEnc==SQLITE_UTF16LE ){ 272 /* UTF-8 -> UTF-16 Little-endian */ 273 while( zIn<zTerm ){ 274 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); 275 WRITE_UTF16LE(z, c); 276 } 277 }else{ 278 assert( desiredEnc==SQLITE_UTF16BE ); 279 /* UTF-8 -> UTF-16 Big-endian */ 280 while( zIn<zTerm ){ 281 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); 282 WRITE_UTF16BE(z, c); 283 } 284 } 285 pMem->n = z - zOut; 286 *z++ = 0; 287 }else{ 288 assert( desiredEnc==SQLITE_UTF8 ); 289 if( pMem->enc==SQLITE_UTF16LE ){ 290 /* UTF-16 Little-endian -> UTF-8 */ 291 while( zIn<zTerm ){ 292 READ_UTF16LE(zIn, c); 293 WRITE_UTF8(z, c); 294 } 295 }else{ 296 /* UTF-16 Little-endian -> UTF-8 */ 297 while( zIn<zTerm ){ 298 READ_UTF16BE(zIn, c); 299 WRITE_UTF8(z, c); 300 } 301 } 302 pMem->n = z - zOut; 303 } 304 *z = 0; 305 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); 306 307 sqlite3VdbeMemRelease(pMem); 308 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); 309 pMem->enc = desiredEnc; 310 if( zOut==zShort ){ 311 memcpy(pMem->zShort, zOut, len); 312 zOut = (u8*)pMem->zShort; 313 pMem->flags |= (MEM_Term|MEM_Short); 314 }else{ 315 pMem->flags |= (MEM_Term|MEM_Dyn); 316 } 317 pMem->z = (char*)zOut; 318 319 translate_out: 320 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) 321 { 322 char zBuf[100]; 323 sqlite3VdbeMemPrettyPrint(pMem, zBuf); 324 fprintf(stderr, "OUTPUT: %s\n", zBuf); 325 } 326 #endif 327 return SQLITE_OK; 328 } 329 330 /* 331 ** This routine checks for a byte-order mark at the beginning of the 332 ** UTF-16 string stored in *pMem. If one is present, it is removed and 333 ** the encoding of the Mem adjusted. This routine does not do any 334 ** byte-swapping, it just sets Mem.enc appropriately. 335 ** 336 ** The allocation (static, dynamic etc.) and encoding of the Mem may be 337 ** changed by this function. 338 */ 339 int sqlite3VdbeMemHandleBom(Mem *pMem){ 340 int rc = SQLITE_OK; 341 u8 bom = 0; 342 343 if( pMem->n<0 || pMem->n>1 ){ 344 u8 b1 = *(u8 *)pMem->z; 345 u8 b2 = *(((u8 *)pMem->z) + 1); 346 if( b1==0xFE && b2==0xFF ){ 347 bom = SQLITE_UTF16BE; 348 } 349 if( b1==0xFF && b2==0xFE ){ 350 bom = SQLITE_UTF16LE; 351 } 352 } 353 354 if( bom ){ 355 /* This function is called as soon as a string is stored in a Mem*, 356 ** from within sqlite3VdbeMemSetStr(). At that point it is not possible 357 ** for the string to be stored in Mem.zShort, or for it to be stored 358 ** in dynamic memory with no destructor. 359 */ 360 assert( !(pMem->flags&MEM_Short) ); 361 assert( !(pMem->flags&MEM_Dyn) || pMem->xDel ); 362 if( pMem->flags & MEM_Dyn ){ 363 void (*xDel)(void*) = pMem->xDel; 364 char *z = pMem->z; 365 pMem->z = 0; 366 pMem->xDel = 0; 367 rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, SQLITE_TRANSIENT); 368 xDel(z); 369 }else{ 370 rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, 371 SQLITE_TRANSIENT); 372 } 373 } 374 return rc; 375 } 376 #endif /* SQLITE_OMIT_UTF16 */ 377 378 /* 379 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, 380 ** return the number of unicode characters in pZ up to (but not including) 381 ** the first 0x00 byte. If nByte is not less than zero, return the 382 ** number of unicode characters in the first nByte of pZ (or up to 383 ** the first 0x00, whichever comes first). 384 */ 385 int sqlite3Utf8CharLen(const char *zIn, int nByte){ 386 int r = 0; 387 const u8 *z = (const u8*)zIn; 388 const u8 *zTerm; 389 if( nByte>=0 ){ 390 zTerm = &z[nByte]; 391 }else{ 392 zTerm = (const u8*)(-1); 393 } 394 assert( z<=zTerm ); 395 while( *z!=0 && z<zTerm ){ 396 SQLITE_SKIP_UTF8(z); 397 r++; 398 } 399 return r; 400 } 401 402 #ifndef SQLITE_OMIT_UTF16 403 /* 404 ** Convert a UTF-16 string in the native encoding into a UTF-8 string. 405 ** Memory to hold the UTF-8 string is obtained from malloc and must be 406 ** freed by the calling function. 407 ** 408 ** NULL is returned if there is an allocation error. 409 */ 410 char *sqlite3Utf16to8(const void *z, int nByte){ 411 Mem m; 412 memset(&m, 0, sizeof(m)); 413 sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC); 414 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8); 415 assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() ); 416 assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() ); 417 return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z); 418 } 419 420 /* 421 ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero, 422 ** return the number of bytes up to (but not including), the first pair 423 ** of consecutive 0x00 bytes in pZ. If nChar is not less than zero, 424 ** then return the number of bytes in the first nChar unicode characters 425 ** in pZ (or up until the first pair of 0x00 bytes, whichever comes first). 426 */ 427 int sqlite3Utf16ByteLen(const void *zIn, int nChar){ 428 unsigned int c = 1; 429 char const *z = zIn; 430 int n = 0; 431 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){ 432 /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here 433 ** and in other parts of this file means that at one branch will 434 ** not be covered by coverage testing on any single host. But coverage 435 ** will be complete if the tests are run on both a little-endian and 436 ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE 437 ** macros are constant at compile time the compiler can determine 438 ** which branch will be followed. It is therefore assumed that no runtime 439 ** penalty is paid for this "if" statement. 440 */ 441 while( c && ((nChar<0) || n<nChar) ){ 442 READ_UTF16BE(z, c); 443 n++; 444 } 445 }else{ 446 while( c && ((nChar<0) || n<nChar) ){ 447 READ_UTF16LE(z, c); 448 n++; 449 } 450 } 451 return (z-(char const *)zIn)-((c==0)?2:0); 452 } 453 454 #if defined(SQLITE_TEST) 455 /* 456 ** Translate UTF-8 to UTF-8. 457 ** 458 ** This has the effect of making sure that the string is well-formed 459 ** UTF-8. Miscoded characters are removed. 460 ** 461 ** The translation is done in-place (since it is impossible for the 462 ** correct UTF-8 encoding to be longer than a malformed encoding). 463 */ 464 int sqlite3Utf8To8(unsigned char *zIn){ 465 unsigned char *zOut = zIn; 466 unsigned char *zStart = zIn; 467 unsigned char *zTerm; 468 u32 c; 469 470 while( zIn[0] ){ 471 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); 472 if( c!=0xfffd ){ 473 WRITE_UTF8(zOut, c); 474 } 475 } 476 *zOut = 0; 477 return zOut - zStart; 478 } 479 #endif 480 481 #if defined(SQLITE_TEST) 482 /* 483 ** This routine is called from the TCL test function "translate_selftest". 484 ** It checks that the primitives for serializing and deserializing 485 ** characters in each encoding are inverses of each other. 486 */ 487 void sqlite3UtfSelfTest(){ 488 unsigned int i, t; 489 unsigned char zBuf[20]; 490 unsigned char *z; 491 unsigned char *zTerm; 492 int n; 493 unsigned int c; 494 495 for(i=0; i<0x00110000; i++){ 496 z = zBuf; 497 WRITE_UTF8(z, i); 498 n = z-zBuf; 499 z[0] = 0; 500 zTerm = z; 501 z = zBuf; 502 c = sqlite3Utf8Read(z, zTerm, (const u8**)&z); 503 t = i; 504 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD; 505 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD; 506 assert( c==t ); 507 assert( (z-zBuf)==n ); 508 } 509 for(i=0; i<0x00110000; i++){ 510 if( i>=0xD800 && i<0xE000 ) continue; 511 z = zBuf; 512 WRITE_UTF16LE(z, i); 513 n = z-zBuf; 514 z[0] = 0; 515 z = zBuf; 516 READ_UTF16LE(z, c); 517 assert( c==i ); 518 assert( (z-zBuf)==n ); 519 } 520 for(i=0; i<0x00110000; i++){ 521 if( i>=0xD800 && i<0xE000 ) continue; 522 z = zBuf; 523 WRITE_UTF16BE(z, i); 524 n = z-zBuf; 525 z[0] = 0; 526 z = zBuf; 527 READ_UTF16BE(z, c); 528 assert( c==i ); 529 assert( (z-zBuf)==n ); 530 } 531 } 532 #endif /* SQLITE_TEST */ 533 #endif /* SQLITE_OMIT_UTF16 */ 534