AryaWu/sqlite
0
1/*2** 2004 April 133**4** The author disclaims copyright to this source code. In place of5** 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** Notes on UTF-8:16**17** Byte-0 Byte-1 Byte-2 Byte-3 Value18** 0xxxxxxx 00000000 00000000 0xxxxxxx19** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx20** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx21** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx22**23**24** Notes on UTF-16: (with wwww+1==uuuuu)25**26** Word-0 Word-1 Value27** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx28** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx29**30**31** BOM or Byte Order Mark:32** 0xff 0xfe little-endian utf-16 follows33** 0xfe 0xff big-endian utf-16 follows34**35*/36#include "sqliteInt.h"37#include <assert.h>38#include "vdbeInt.h"39 40#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==041/*42** The following constant value is used by the SQLITE_BIGENDIAN and43** SQLITE_LITTLEENDIAN macros.44*/45const int sqlite3one = 1;46#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */47 48/*49** This lookup table is used to help decode the first byte of50** a multi-byte UTF8 character.51*/52static const unsigned char sqlite3Utf8Trans1[] = {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++ = (u8)(c&0xFF); \67 } \68 else if( c<0x00800 ){ \69 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \70 *zOut++ = 0x80 + (u8)(c & 0x3F); \71 } \72 else if( c<0x10000 ){ \73 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \74 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \75 *zOut++ = 0x80 + (u8)(c & 0x3F); \76 }else{ \77 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \78 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \79 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \80 *zOut++ = 0x80 + (u8)(c & 0x3F); \81 } \82}83 84#define WRITE_UTF16LE(zOut, c) { \85 if( c<=0xFFFF ){ \86 *zOut++ = (u8)(c&0x00FF); \87 *zOut++ = (u8)((c>>8)&0x00FF); \88 }else{ \89 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \90 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \91 *zOut++ = (u8)(c&0x00FF); \92 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \93 } \94}95 96#define WRITE_UTF16BE(zOut, c) { \97 if( c<=0xFFFF ){ \98 *zOut++ = (u8)((c>>8)&0x00FF); \99 *zOut++ = (u8)(c&0x00FF); \100 }else{ \101 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \102 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \103 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \104 *zOut++ = (u8)(c&0x00FF); \105 } \106}107 108/*109** Write a single UTF8 character whose value is v into the110** buffer starting at zOut. zOut must be sized to hold at111** least four bytes. Return the number of bytes needed112** to encode the new character.113*/114int sqlite3AppendOneUtf8Character(char *zOut, u32 v){115 if( v<0x00080 ){116 zOut[0] = (u8)(v & 0xff);117 return 1;118 }119 if( v<0x00800 ){120 zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f);121 zOut[1] = 0x80 + (u8)(v & 0x3f);122 return 2;123 }124 if( v<0x10000 ){125 zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f);126 zOut[1] = 0x80 + (u8)((v>>6) & 0x3f);127 zOut[2] = 0x80 + (u8)(v & 0x3f);128 return 3;129 }130 zOut[0] = 0xf0 + (u8)((v>>18) & 0x07);131 zOut[1] = 0x80 + (u8)((v>>12) & 0x3f);132 zOut[2] = 0x80 + (u8)((v>>6) & 0x3f);133 zOut[3] = 0x80 + (u8)(v & 0x3f);134 return 4;135}136 137/*138** Translate a single UTF-8 character. Return the unicode value.139**140** During translation, assume that the byte that zTerm points141** is a 0x00.142**143** Write a pointer to the next unread byte back into *pzNext.144**145** Notes On Invalid UTF-8:146**147** * This routine never allows a 7-bit character (0x00 through 0x7f) to148** be encoded as a multi-byte character. Any multi-byte character that149** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.150**151** * This routine never allows a UTF16 surrogate value to be encoded.152** If a multi-byte character attempts to encode a value between153** 0xd800 and 0xe000 then it is rendered as 0xfffd.154**155** * Bytes in the range of 0x80 through 0xbf which occur as the first156** byte of a character are interpreted as single-byte characters157** and rendered as themselves even though they are technically158** invalid characters.159**160** * This routine accepts over-length UTF8 encodings161** for unicode values 0x80 and greater. It does not change over-length162** encodings to 0xfffd as some systems recommend.163*/164#define READ_UTF8(zIn, zTerm, c) \165 c = *(zIn++); \166 if( c>=0xc0 ){ \167 c = sqlite3Utf8Trans1[c-0xc0]; \168 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \169 c = (c<<6) + (0x3f & *(zIn++)); \170 } \171 if( c<0x80 \172 || (c&0xFFFFF800)==0xD800 \173 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \174 }175u32 sqlite3Utf8Read(176 const unsigned char **pz /* Pointer to string from which to read char */177){178 unsigned int c;179 180 /* Same as READ_UTF8() above but without the zTerm parameter.181 ** For this routine, we assume the UTF8 string is always zero-terminated.182 */183 c = *((*pz)++);184 if( c>=0xc0 ){185 c = sqlite3Utf8Trans1[c-0xc0];186 while( (*(*pz) & 0xc0)==0x80 ){187 c = (c<<6) + (0x3f & *((*pz)++));188 }189 if( c<0x80190 || (c&0xFFFFF800)==0xD800191 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }192 }193 return c;194}195 196/*197** Read a single UTF8 character out of buffer z[], but reading no198** more than n characters from the buffer. z[] is not zero-terminated.199**200** Return the number of bytes used to construct the character.201**202** Invalid UTF8 might generate a strange result. No effort is made203** to detect invalid UTF8.204**205** At most 4 bytes will be read out of z[]. The return value will always206** be between 1 and 4.207*/208int sqlite3Utf8ReadLimited(209 const u8 *z,210 int n,211 u32 *piOut212){213 u32 c;214 int i = 1;215 assert( n>0 );216 c = z[0];217 if( c>=0xc0 ){218 c = sqlite3Utf8Trans1[c-0xc0];219 if( n>4 ) n = 4;220 while( i<n && (z[i] & 0xc0)==0x80 ){221 c = (c<<6) + (0x3f & z[i]);222 i++;223 }224 }225 *piOut = c;226 return i;227}228 229 230/*231** If the TRANSLATE_TRACE macro is defined, the value of each Mem is232** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().233*/ 234/* #define TRANSLATE_TRACE 1 */235 236#ifndef SQLITE_OMIT_UTF16237/*238** This routine transforms the internal text encoding used by pMem to239** desiredEnc. It is an error if the string is already of the desired240** encoding, or if *pMem does not contain a string value.241*/242SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){243 sqlite3_int64 len; /* Maximum length of output string in bytes */244 unsigned char *zOut; /* Output buffer */245 unsigned char *zIn; /* Input iterator */246 unsigned char *zTerm; /* End of input */247 unsigned char *z; /* Output iterator */248 unsigned int c;249 250 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );251 assert( pMem->flags&MEM_Str );252 assert( pMem->enc!=desiredEnc );253 assert( pMem->enc!=0 );254 assert( pMem->n>=0 );255 256#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)257 {258 StrAccum acc;259 char zBuf[1000];260 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 261 sqlite3VdbeMemPrettyPrint(pMem, &acc);262 fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));263 }264#endif265 266 /* If the translation is between UTF-16 little and big endian, then 267 ** all that is required is to swap the byte order. This case is handled268 ** differently from the others.269 */270 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){271 u8 temp;272 int rc;273 rc = sqlite3VdbeMemMakeWriteable(pMem);274 if( rc!=SQLITE_OK ){275 assert( rc==SQLITE_NOMEM );276 return SQLITE_NOMEM_BKPT;277 }278 zIn = (u8*)pMem->z;279 zTerm = &zIn[pMem->n&~1];280 while( zIn<zTerm ){281 temp = *zIn;282 *zIn = *(zIn+1);283 zIn++;284 *zIn++ = temp;285 }286 pMem->enc = desiredEnc;287 goto translate_out;288 }289 290 /* Set len to the maximum number of bytes required in the output buffer. */291 if( desiredEnc==SQLITE_UTF8 ){292 /* When converting from UTF-16, the maximum growth results from293 ** translating a 2-byte character to a 4-byte UTF-8 character.294 ** A single byte is required for the output string295 ** nul-terminator.296 */297 pMem->n &= ~1;298 len = 2 * (sqlite3_int64)pMem->n + 1;299 }else{300 /* When converting from UTF-8 to UTF-16 the maximum growth is caused301 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16302 ** character. Two bytes are required in the output buffer for the303 ** nul-terminator.304 */305 len = 2 * (sqlite3_int64)pMem->n + 2;306 }307 308 /* Set zIn to point at the start of the input buffer and zTerm to point 1309 ** byte past the end.310 **311 ** Variable zOut is set to point at the output buffer, space obtained312 ** from sqlite3_malloc().313 */314 zIn = (u8*)pMem->z;315 zTerm = &zIn[pMem->n];316 zOut = sqlite3DbMallocRaw(pMem->db, len);317 if( !zOut ){318 return SQLITE_NOMEM_BKPT;319 }320 z = zOut;321 322 if( pMem->enc==SQLITE_UTF8 ){323 if( desiredEnc==SQLITE_UTF16LE ){324 /* UTF-8 -> UTF-16 Little-endian */325 while( zIn<zTerm ){326 READ_UTF8(zIn, zTerm, c);327 WRITE_UTF16LE(z, c);328 }329 }else{330 assert( desiredEnc==SQLITE_UTF16BE );331 /* UTF-8 -> UTF-16 Big-endian */332 while( zIn<zTerm ){333 READ_UTF8(zIn, zTerm, c);334 WRITE_UTF16BE(z, c);335 }336 }337 pMem->n = (int)(z - zOut);338 *z++ = 0;339 }else{340 assert( desiredEnc==SQLITE_UTF8 );341 if( pMem->enc==SQLITE_UTF16LE ){342 /* UTF-16 Little-endian -> UTF-8 */343 while( zIn<zTerm ){344 c = *(zIn++);345 c += (*(zIn++))<<8;346 if( c>=0xd800 && c<0xe000 ){347#ifdef SQLITE_REPLACE_INVALID_UTF348 if( c>=0xdc00 || zIn>=zTerm ){349 c = 0xfffd;350 }else{351 int c2 = *(zIn++);352 c2 += (*(zIn++))<<8;353 if( c2<0xdc00 || c2>=0xe000 ){354 zIn -= 2;355 c = 0xfffd;356 }else{357 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;358 }359 }360#else361 if( zIn<zTerm ){362 int c2 = (*zIn++);363 c2 += ((*zIn++)<<8);364 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);365 }366#endif367 }368 WRITE_UTF8(z, c);369 }370 }else{371 /* UTF-16 Big-endian -> UTF-8 */372 while( zIn<zTerm ){373 c = (*(zIn++))<<8;374 c += *(zIn++);375 if( c>=0xd800 && c<0xe000 ){376#ifdef SQLITE_REPLACE_INVALID_UTF377 if( c>=0xdc00 || zIn>=zTerm ){378 c = 0xfffd;379 }else{380 int c2 = (*(zIn++))<<8;381 c2 += *(zIn++);382 if( c2<0xdc00 || c2>=0xe000 ){383 zIn -= 2;384 c = 0xfffd;385 }else{386 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;387 }388 }389#else390 if( zIn<zTerm ){391 int c2 = ((*zIn++)<<8);392 c2 += (*zIn++);393 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);394 }395#endif396 }397 WRITE_UTF8(z, c);398 }399 }400 pMem->n = (int)(z - zOut);401 }402 *z = 0;403 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );404 405 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));406 sqlite3VdbeMemRelease(pMem);407 pMem->flags = c;408 pMem->enc = desiredEnc;409 pMem->z = (char*)zOut;410 pMem->zMalloc = pMem->z;411 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);412 413translate_out:414#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)415 {416 StrAccum acc;417 char zBuf[1000];418 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 419 sqlite3VdbeMemPrettyPrint(pMem, &acc);420 fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));421 }422#endif423 return SQLITE_OK;424}425#endif /* SQLITE_OMIT_UTF16 */426 427#ifndef SQLITE_OMIT_UTF16428/*429** This routine checks for a byte-order mark at the beginning of the 430** UTF-16 string stored in *pMem. If one is present, it is removed and431** the encoding of the Mem adjusted. This routine does not do any432** byte-swapping, it just sets Mem.enc appropriately.433**434** The allocation (static, dynamic etc.) and encoding of the Mem may be435** changed by this function.436*/437int sqlite3VdbeMemHandleBom(Mem *pMem){438 int rc = SQLITE_OK;439 u8 bom = 0;440 441 assert( pMem->n>=0 );442 if( pMem->n>1 ){443 u8 b1 = *(u8 *)pMem->z;444 u8 b2 = *(((u8 *)pMem->z) + 1);445 if( b1==0xFE && b2==0xFF ){446 bom = SQLITE_UTF16BE;447 }448 if( b1==0xFF && b2==0xFE ){449 bom = SQLITE_UTF16LE;450 }451 }452 453 if( bom ){454 rc = sqlite3VdbeMemMakeWriteable(pMem);455 if( rc==SQLITE_OK ){456 pMem->n -= 2;457 memmove(pMem->z, &pMem->z[2], pMem->n);458 pMem->z[pMem->n] = '\0';459 pMem->z[pMem->n+1] = '\0';460 pMem->flags |= MEM_Term;461 pMem->enc = bom;462 }463 }464 return rc;465}466#endif /* SQLITE_OMIT_UTF16 */467 468/*469** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,470** return the number of unicode characters in pZ up to (but not including)471** the first 0x00 byte. If nByte is not less than zero, return the472** number of unicode characters in the first nByte of pZ (or up to 473** the first 0x00, whichever comes first).474*/475int sqlite3Utf8CharLen(const char *zIn, int nByte){476 int r = 0;477 const u8 *z = (const u8*)zIn;478 const u8 *zTerm;479 if( nByte>=0 ){480 zTerm = &z[nByte];481 }else{482 zTerm = (const u8*)(-1);483 }484 assert( z<=zTerm );485 while( *z!=0 && z<zTerm ){486 SQLITE_SKIP_UTF8(z);487 r++;488 }489 return r;490}491 492/* This test function is not currently used by the automated test-suite. 493** Hence it is only available in debug builds.494*/495#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)496/*497** Translate UTF-8 to UTF-8.498**499** This has the effect of making sure that the string is well-formed500** UTF-8. Miscoded characters are removed.501**502** The translation is done in-place and aborted if the output503** overruns the input.504*/505int sqlite3Utf8To8(unsigned char *zIn){506 unsigned char *zOut = zIn;507 unsigned char *zStart = zIn;508 u32 c;509 510 while( zIn[0] && zOut<=zIn ){511 c = sqlite3Utf8Read((const u8**)&zIn);512 if( c!=0xfffd ){513 WRITE_UTF8(zOut, c);514 }515 }516 *zOut = 0;517 return (int)(zOut - zStart);518}519#endif520 521#ifndef SQLITE_OMIT_UTF16522/*523** Convert a UTF-16 string in the native encoding into a UTF-8 string.524** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must525** be freed by the calling function.526**527** NULL is returned if there is an allocation error.528*/529char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){530 Mem m;531 memset(&m, 0, sizeof(m));532 m.db = db;533 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);534 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);535 if( db->mallocFailed ){536 sqlite3VdbeMemRelease(&m);537 m.z = 0;538 }539 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );540 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );541 assert( m.z || db->mallocFailed );542 return m.z;543}544 545/*546** zIn is a UTF-16 encoded unicode string at least nByte bytes long.547** Return the number of bytes in the first nChar unicode characters548** in pZ. nChar must be non-negative. Surrogate pairs count as a single549** character.550*/551int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){552 int c;553 unsigned char const *z = zIn;554 unsigned char const *zEnd = &z[nByte-1];555 int n = 0;556 557 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;558 while( n<nChar && z<=zEnd ){559 c = z[0];560 z += 2;561 if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;562 n++;563 }564 return (int)(z-(unsigned char const *)zIn) 565 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);566}567 568#if defined(SQLITE_TEST)569/*570** This routine is called from the TCL test function "translate_selftest".571** It checks that the primitives for serializing and deserializing572** characters in each encoding are inverses of each other.573*/574void sqlite3UtfSelfTest(void){575 unsigned int i, t;576 unsigned char zBuf[20];577 unsigned char *z;578 int n;579 unsigned int c;580 581 for(i=0; i<0x00110000; i++){582 z = zBuf;583 WRITE_UTF8(z, i);584 n = (int)(z-zBuf);585 assert( n>0 && n<=4 );586 z[0] = 0;587 z = zBuf;588 c = sqlite3Utf8Read((const u8**)&z);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}596#endif /* SQLITE_TEST */597#endif /* SQLITE_OMIT_UTF16 */598 