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.43 2006/10/19 01:58:44 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 ** Handling of malformed strings: 39 ** 40 ** SQLite accepts and processes malformed strings without an error wherever 41 ** possible. However this is not possible when converting between UTF-8 and 42 ** UTF-16. 43 ** 44 ** When converting malformed UTF-8 strings to UTF-16, one instance of the 45 ** replacement character U+FFFD for each byte that cannot be interpeted as 46 ** part of a valid unicode character. 47 ** 48 ** When converting malformed UTF-16 strings to UTF-8, one instance of the 49 ** replacement character U+FFFD for each pair of bytes that cannot be 50 ** interpeted as part of a valid unicode character. 51 ** 52 ** This file contains the following public routines: 53 ** 54 ** sqlite3VdbeMemTranslate() - Translate the encoding used by a Mem* string. 55 ** sqlite3VdbeMemHandleBom() - Handle byte-order-marks in UTF16 Mem* strings. 56 ** sqlite3utf16ByteLen() - Calculate byte-length of a void* UTF16 string. 57 ** sqlite3utf8CharLen() - Calculate char-length of a char* UTF8 string. 58 ** sqlite3utf8LikeCompare() - Do a LIKE match given two UTF8 char* strings. 59 ** 60 */ 61 #include "sqliteInt.h" 62 #include <assert.h> 63 #include "vdbeInt.h" 64 65 /* 66 ** This table maps from the first byte of a UTF-8 character to the number 67 ** of trailing bytes expected. A value '4' indicates that the table key 68 ** is not a legal first byte for a UTF-8 character. 69 */ 70 static const u8 xtra_utf8_bytes[256] = { 71 /* 0xxxxxxx */ 72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80 81 /* 10wwwwww */ 82 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 83 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 84 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 85 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 86 87 /* 110yyyyy */ 88 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 89 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 90 91 /* 1110zzzz */ 92 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 93 94 /* 11110yyy */ 95 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 96 }; 97 98 /* 99 ** This table maps from the number of trailing bytes in a UTF-8 character 100 ** to an integer constant that is effectively calculated for each character 101 ** read by a naive implementation of a UTF-8 character reader. The code 102 ** in the READ_UTF8 macro explains things best. 103 */ 104 static const int xtra_utf8_bits[] = { 105 0, 106 12416, /* (0xC0 << 6) + (0x80) */ 107 925824, /* (0xE0 << 12) + (0x80 << 6) + (0x80) */ 108 63447168 /* (0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */ 109 }; 110 111 /* 112 ** If a UTF-8 character contains N bytes extra bytes (N bytes follow 113 ** the initial byte so that the total character length is N+1) then 114 ** masking the character with utf8_mask[N] must produce a non-zero 115 ** result. Otherwise, we have an (illegal) overlong encoding. 116 */ 117 static const int utf_mask[] = { 118 0x00000000, 119 0xffffff80, 120 0xfffff800, 121 0xffff0000, 122 }; 123 124 #define READ_UTF8(zIn, c) { \ 125 int xtra; \ 126 c = *(zIn)++; \ 127 xtra = xtra_utf8_bytes[c]; \ 128 switch( xtra ){ \ 129 case 4: c = (int)0xFFFD; break; \ 130 case 3: c = (c<<6) + *(zIn)++; \ 131 case 2: c = (c<<6) + *(zIn)++; \ 132 case 1: c = (c<<6) + *(zIn)++; \ 133 c -= xtra_utf8_bits[xtra]; \ 134 if( (utf_mask[xtra]&c)==0 \ 135 || (c&0xFFFFF800)==0xD800 \ 136 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ 137 } \ 138 } 139 int sqlite3ReadUtf8(const unsigned char *z){ 140 int c; 141 READ_UTF8(z, c); 142 return c; 143 } 144 145 #define SKIP_UTF8(zIn) { \ 146 zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \ 147 } 148 149 #define WRITE_UTF8(zOut, c) { \ 150 if( c<0x00080 ){ \ 151 *zOut++ = (c&0xFF); \ 152 } \ 153 else if( c<0x00800 ){ \ 154 *zOut++ = 0xC0 + ((c>>6)&0x1F); \ 155 *zOut++ = 0x80 + (c & 0x3F); \ 156 } \ 157 else if( c<0x10000 ){ \ 158 *zOut++ = 0xE0 + ((c>>12)&0x0F); \ 159 *zOut++ = 0x80 + ((c>>6) & 0x3F); \ 160 *zOut++ = 0x80 + (c & 0x3F); \ 161 }else{ \ 162 *zOut++ = 0xF0 + ((c>>18) & 0x07); \ 163 *zOut++ = 0x80 + ((c>>12) & 0x3F); \ 164 *zOut++ = 0x80 + ((c>>6) & 0x3F); \ 165 *zOut++ = 0x80 + (c & 0x3F); \ 166 } \ 167 } 168 169 #define WRITE_UTF16LE(zOut, c) { \ 170 if( c<=0xFFFF ){ \ 171 *zOut++ = (c&0x00FF); \ 172 *zOut++ = ((c>>8)&0x00FF); \ 173 }else{ \ 174 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ 175 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \ 176 *zOut++ = (c&0x00FF); \ 177 *zOut++ = (0x00DC + ((c>>8)&0x03)); \ 178 } \ 179 } 180 181 #define WRITE_UTF16BE(zOut, c) { \ 182 if( c<=0xFFFF ){ \ 183 *zOut++ = ((c>>8)&0x00FF); \ 184 *zOut++ = (c&0x00FF); \ 185 }else{ \ 186 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \ 187 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ 188 *zOut++ = (0x00DC + ((c>>8)&0x03)); \ 189 *zOut++ = (c&0x00FF); \ 190 } \ 191 } 192 193 #define READ_UTF16LE(zIn, c){ \ 194 c = (*zIn++); \ 195 c += ((*zIn++)<<8); \ 196 if( c>=0xD800 && c<=0xE000 ){ \ 197 int c2 = (*zIn++); \ 198 c2 += ((*zIn++)<<8); \ 199 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ 200 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \ 201 } \ 202 } 203 204 #define READ_UTF16BE(zIn, c){ \ 205 c = ((*zIn++)<<8); \ 206 c += (*zIn++); \ 207 if( c>=0xD800 && c<=0xE000 ){ \ 208 int c2 = ((*zIn++)<<8); \ 209 c2 += (*zIn++); \ 210 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ 211 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \ 212 } \ 213 } 214 215 #define SKIP_UTF16BE(zIn){ \ 216 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn+1)==0x00)) ){ \ 217 zIn += 4; \ 218 }else{ \ 219 zIn += 2; \ 220 } \ 221 } 222 #define SKIP_UTF16LE(zIn){ \ 223 zIn++; \ 224 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn-1)==0x00)) ){ \ 225 zIn += 3; \ 226 }else{ \ 227 zIn += 1; \ 228 } \ 229 } 230 231 #define RSKIP_UTF16LE(zIn){ \ 232 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn-1)==0x00)) ){ \ 233 zIn -= 4; \ 234 }else{ \ 235 zIn -= 2; \ 236 } \ 237 } 238 #define RSKIP_UTF16BE(zIn){ \ 239 zIn--; \ 240 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn+1)==0x00)) ){ \ 241 zIn -= 3; \ 242 }else{ \ 243 zIn -= 1; \ 244 } \ 245 } 246 247 /* 248 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is 249 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). 250 */ 251 /* #define TRANSLATE_TRACE 1 */ 252 253 #ifndef SQLITE_OMIT_UTF16 254 /* 255 ** This routine transforms the internal text encoding used by pMem to 256 ** desiredEnc. It is an error if the string is already of the desired 257 ** encoding, or if *pMem does not contain a string value. 258 */ 259 int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ 260 unsigned char zShort[NBFS]; /* Temporary short output buffer */ 261 int len; /* Maximum length of output string in bytes */ 262 unsigned char *zOut; /* Output buffer */ 263 unsigned char *zIn; /* Input iterator */ 264 unsigned char *zTerm; /* End of input */ 265 unsigned char *z; /* Output iterator */ 266 unsigned int c; 267 268 assert( pMem->flags&MEM_Str ); 269 assert( pMem->enc!=desiredEnc ); 270 assert( pMem->enc!=0 ); 271 assert( pMem->n>=0 ); 272 273 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) 274 { 275 char zBuf[100]; 276 sqlite3VdbeMemPrettyPrint(pMem, zBuf); 277 fprintf(stderr, "INPUT: %s\n", zBuf); 278 } 279 #endif 280 281 /* If the translation is between UTF-16 little and big endian, then 282 ** all that is required is to swap the byte order. This case is handled 283 ** differently from the others. 284 */ 285 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ 286 u8 temp; 287 int rc; 288 rc = sqlite3VdbeMemMakeWriteable(pMem); 289 if( rc!=SQLITE_OK ){ 290 assert( rc==SQLITE_NOMEM ); 291 return SQLITE_NOMEM; 292 } 293 zIn = (u8*)pMem->z; 294 zTerm = &zIn[pMem->n]; 295 while( zIn<zTerm ){ 296 temp = *zIn; 297 *zIn = *(zIn+1); 298 zIn++; 299 *zIn++ = temp; 300 } 301 pMem->enc = desiredEnc; 302 goto translate_out; 303 } 304 305 /* Set len to the maximum number of bytes required in the output buffer. */ 306 if( desiredEnc==SQLITE_UTF8 ){ 307 /* When converting from UTF-16, the maximum growth results from 308 ** translating a 2-byte character to a 4-byte UTF-8 character. 309 ** A single byte is required for the output string 310 ** nul-terminator. 311 */ 312 len = pMem->n * 2 + 1; 313 }else{ 314 /* When converting from UTF-8 to UTF-16 the maximum growth is caused 315 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 316 ** character. Two bytes are required in the output buffer for the 317 ** nul-terminator. 318 */ 319 len = pMem->n * 2 + 2; 320 } 321 322 /* Set zIn to point at the start of the input buffer and zTerm to point 1 323 ** byte past the end. 324 ** 325 ** Variable zOut is set to point at the output buffer. This may be space 326 ** obtained from malloc(), or Mem.zShort, if it large enough and not in 327 ** use, or the zShort array on the stack (see above). 328 */ 329 zIn = (u8*)pMem->z; 330 zTerm = &zIn[pMem->n]; 331 if( len>NBFS ){ 332 zOut = sqliteMallocRaw(len); 333 if( !zOut ) return SQLITE_NOMEM; 334 }else{ 335 zOut = zShort; 336 } 337 z = zOut; 338 339 if( pMem->enc==SQLITE_UTF8 ){ 340 if( desiredEnc==SQLITE_UTF16LE ){ 341 /* UTF-8 -> UTF-16 Little-endian */ 342 while( zIn<zTerm ){ 343 READ_UTF8(zIn, c); 344 WRITE_UTF16LE(z, c); 345 } 346 }else{ 347 assert( desiredEnc==SQLITE_UTF16BE ); 348 /* UTF-8 -> UTF-16 Big-endian */ 349 while( zIn<zTerm ){ 350 READ_UTF8(zIn, c); 351 WRITE_UTF16BE(z, c); 352 } 353 } 354 pMem->n = z - zOut; 355 *z++ = 0; 356 }else{ 357 assert( desiredEnc==SQLITE_UTF8 ); 358 if( pMem->enc==SQLITE_UTF16LE ){ 359 /* UTF-16 Little-endian -> UTF-8 */ 360 while( zIn<zTerm ){ 361 READ_UTF16LE(zIn, c); 362 WRITE_UTF8(z, c); 363 } 364 }else{ 365 /* UTF-16 Little-endian -> UTF-8 */ 366 while( zIn<zTerm ){ 367 READ_UTF16BE(zIn, c); 368 WRITE_UTF8(z, c); 369 } 370 } 371 pMem->n = z - zOut; 372 } 373 *z = 0; 374 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); 375 376 sqlite3VdbeMemRelease(pMem); 377 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); 378 pMem->enc = desiredEnc; 379 if( zOut==zShort ){ 380 memcpy(pMem->zShort, zOut, len); 381 zOut = (u8*)pMem->zShort; 382 pMem->flags |= (MEM_Term|MEM_Short); 383 }else{ 384 pMem->flags |= (MEM_Term|MEM_Dyn); 385 } 386 pMem->z = (char*)zOut; 387 388 translate_out: 389 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) 390 { 391 char zBuf[100]; 392 sqlite3VdbeMemPrettyPrint(pMem, zBuf); 393 fprintf(stderr, "OUTPUT: %s\n", zBuf); 394 } 395 #endif 396 return SQLITE_OK; 397 } 398 399 /* 400 ** This routine checks for a byte-order mark at the beginning of the 401 ** UTF-16 string stored in *pMem. If one is present, it is removed and 402 ** the encoding of the Mem adjusted. This routine does not do any 403 ** byte-swapping, it just sets Mem.enc appropriately. 404 ** 405 ** The allocation (static, dynamic etc.) and encoding of the Mem may be 406 ** changed by this function. 407 */ 408 int sqlite3VdbeMemHandleBom(Mem *pMem){ 409 int rc = SQLITE_OK; 410 u8 bom = 0; 411 412 if( pMem->n<0 || pMem->n>1 ){ 413 u8 b1 = *(u8 *)pMem->z; 414 u8 b2 = *(((u8 *)pMem->z) + 1); 415 if( b1==0xFE && b2==0xFF ){ 416 bom = SQLITE_UTF16BE; 417 } 418 if( b1==0xFF && b2==0xFE ){ 419 bom = SQLITE_UTF16LE; 420 } 421 } 422 423 if( bom ){ 424 /* This function is called as soon as a string is stored in a Mem*, 425 ** from within sqlite3VdbeMemSetStr(). At that point it is not possible 426 ** for the string to be stored in Mem.zShort, or for it to be stored 427 ** in dynamic memory with no destructor. 428 */ 429 assert( !(pMem->flags&MEM_Short) ); 430 assert( !(pMem->flags&MEM_Dyn) || pMem->xDel ); 431 if( pMem->flags & MEM_Dyn ){ 432 void (*xDel)(void*) = pMem->xDel; 433 char *z = pMem->z; 434 pMem->z = 0; 435 pMem->xDel = 0; 436 rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, SQLITE_TRANSIENT); 437 xDel(z); 438 }else{ 439 rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, 440 SQLITE_TRANSIENT); 441 } 442 } 443 return rc; 444 } 445 #endif /* SQLITE_OMIT_UTF16 */ 446 447 /* 448 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, 449 ** return the number of unicode characters in pZ up to (but not including) 450 ** the first 0x00 byte. If nByte is not less than zero, return the 451 ** number of unicode characters in the first nByte of pZ (or up to 452 ** the first 0x00, whichever comes first). 453 */ 454 int sqlite3utf8CharLen(const char *z, int nByte){ 455 int r = 0; 456 const char *zTerm; 457 if( nByte>=0 ){ 458 zTerm = &z[nByte]; 459 }else{ 460 zTerm = (const char *)(-1); 461 } 462 assert( z<=zTerm ); 463 while( *z!=0 && z<zTerm ){ 464 SKIP_UTF8(z); 465 r++; 466 } 467 return r; 468 } 469 470 #ifndef SQLITE_OMIT_UTF16 471 /* 472 ** Convert a UTF-16 string in the native encoding into a UTF-8 string. 473 ** Memory to hold the UTF-8 string is obtained from malloc and must be 474 ** freed by the calling function. 475 ** 476 ** NULL is returned if there is an allocation error. 477 */ 478 char *sqlite3utf16to8(const void *z, int nByte){ 479 Mem m; 480 memset(&m, 0, sizeof(m)); 481 sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC); 482 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8); 483 assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() ); 484 assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() ); 485 return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z); 486 } 487 488 /* 489 ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero, 490 ** return the number of bytes up to (but not including), the first pair 491 ** of consecutive 0x00 bytes in pZ. If nChar is not less than zero, 492 ** then return the number of bytes in the first nChar unicode characters 493 ** in pZ (or up until the first pair of 0x00 bytes, whichever comes first). 494 */ 495 int sqlite3utf16ByteLen(const void *zIn, int nChar){ 496 unsigned int c = 1; 497 char const *z = zIn; 498 int n = 0; 499 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){ 500 /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here 501 ** and in other parts of this file means that at one branch will 502 ** not be covered by coverage testing on any single host. But coverage 503 ** will be complete if the tests are run on both a little-endian and 504 ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE 505 ** macros are constant at compile time the compiler can determine 506 ** which branch will be followed. It is therefore assumed that no runtime 507 ** penalty is paid for this "if" statement. 508 */ 509 while( c && ((nChar<0) || n<nChar) ){ 510 READ_UTF16BE(z, c); 511 n++; 512 } 513 }else{ 514 while( c && ((nChar<0) || n<nChar) ){ 515 READ_UTF16LE(z, c); 516 n++; 517 } 518 } 519 return (z-(char const *)zIn)-((c==0)?2:0); 520 } 521 522 /* 523 ** UTF-16 implementation of the substr() 524 */ 525 void sqlite3utf16Substr( 526 sqlite3_context *context, 527 int argc, 528 sqlite3_value **argv 529 ){ 530 int y, z; 531 unsigned char const *zStr; 532 unsigned char const *zStrEnd; 533 unsigned char const *zStart; 534 unsigned char const *zEnd; 535 int i; 536 537 zStr = (unsigned char const *)sqlite3_value_text16(argv[0]); 538 zStrEnd = &zStr[sqlite3_value_bytes16(argv[0])]; 539 y = sqlite3_value_int(argv[1]); 540 z = sqlite3_value_int(argv[2]); 541 542 if( y>0 ){ 543 y = y-1; 544 zStart = zStr; 545 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){ 546 for(i=0; i<y && zStart<zStrEnd; i++) SKIP_UTF16BE(zStart); 547 }else{ 548 for(i=0; i<y && zStart<zStrEnd; i++) SKIP_UTF16LE(zStart); 549 } 550 }else{ 551 zStart = zStrEnd; 552 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){ 553 for(i=y; i<0 && zStart>zStr; i++) RSKIP_UTF16BE(zStart); 554 }else{ 555 for(i=y; i<0 && zStart>zStr; i++) RSKIP_UTF16LE(zStart); 556 } 557 for(; i<0; i++) z -= 1; 558 } 559 560 zEnd = zStart; 561 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){ 562 for(i=0; i<z && zEnd<zStrEnd; i++) SKIP_UTF16BE(zEnd); 563 }else{ 564 for(i=0; i<z && zEnd<zStrEnd; i++) SKIP_UTF16LE(zEnd); 565 } 566 567 sqlite3_result_text16(context, zStart, zEnd-zStart, SQLITE_TRANSIENT); 568 } 569 570 #if defined(SQLITE_TEST) 571 /* 572 ** This routine is called from the TCL test function "translate_selftest". 573 ** It checks that the primitives for serializing and deserializing 574 ** characters in each encoding are inverses of each other. 575 */ 576 void sqlite3utfSelfTest(){ 577 unsigned int i, t; 578 unsigned char zBuf[20]; 579 unsigned char *z; 580 int n; 581 unsigned int c; 582 583 for(i=0; i<0x00110000; i++){ 584 z = zBuf; 585 WRITE_UTF8(z, i); 586 n = z-zBuf; 587 z = zBuf; 588 READ_UTF8(z, c); 589 t = i; 590 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD; 591 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD; 592 assert( c==t ); 593 assert( (z-zBuf)==n ); 594 } 595 for(i=0; i<0x00110000; i++){ 596 if( i>=0xD800 && i<=0xE000 ) continue; 597 z = zBuf; 598 WRITE_UTF16LE(z, i); 599 n = z-zBuf; 600 z = zBuf; 601 READ_UTF16LE(z, c); 602 assert( c==i ); 603 assert( (z-zBuf)==n ); 604 } 605 for(i=0; i<0x00110000; i++){ 606 if( i>=0xD800 && i<=0xE000 ) continue; 607 z = zBuf; 608 WRITE_UTF16BE(z, i); 609 n = z-zBuf; 610 z = zBuf; 611 READ_UTF16BE(z, c); 612 assert( c==i ); 613 assert( (z-zBuf)==n ); 614 } 615 } 616 #endif /* SQLITE_TEST */ 617 #endif /* SQLITE_OMIT_UTF16 */ 618