Fixed extension version reporting.
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains routines used to translate between UTF-8,
13 ** UTF-16, UTF-16BE, and UTF-16LE.
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
26 ** Notes on UTF-16: (with wwww+1==uuuuu)
28 ** Word-0 Word-1 Value
29 ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
30 ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
33 ** BOM or Byte Order Mark:
34 ** 0xff 0xfe little-endian utf-16 follows
35 ** 0xfe 0xff big-endian utf-16 follows
38 #include "sqliteInt.h"
43 ** The following constant value is used by the SQLITE_BIGENDIAN and
44 ** SQLITE_LITTLEENDIAN macros.
46 const int sqlite3one = 1;
49 ** This lookup table is used to help decode the first byte of
50 ** a multi-byte UTF8 character.
52 static 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,
64 #define WRITE_UTF8(zOut, c) { \
68 else if( c<0x00800 ){ \
69 *zOut++ = 0xC0 + ((c>>6)&0x1F); \
70 *zOut++ = 0x80 + (c & 0x3F); \
72 else if( c<0x10000 ){ \
73 *zOut++ = 0xE0 + ((c>>12)&0x0F); \
74 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
75 *zOut++ = 0x80 + (c & 0x3F); \
77 *zOut++ = 0xF0 + ((c>>18) & 0x07); \
78 *zOut++ = 0x80 + ((c>>12) & 0x3F); \
79 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
80 *zOut++ = 0x80 + (c & 0x3F); \
84 #define WRITE_UTF16LE(zOut, c) { \
86 *zOut++ = (c&0x00FF); \
87 *zOut++ = ((c>>8)&0x00FF); \
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)); \
96 #define WRITE_UTF16BE(zOut, c) { \
98 *zOut++ = ((c>>8)&0x00FF); \
99 *zOut++ = (c&0x00FF); \
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); \
108 #define READ_UTF16LE(zIn, c){ \
110 c += ((*zIn++)<<8); \
111 if( c>=0xD800 && c<0xE000 ){ \
113 c2 += ((*zIn++)<<8); \
114 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
115 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
119 #define READ_UTF16BE(zIn, c){ \
122 if( c>=0xD800 && c<0xE000 ){ \
123 int c2 = ((*zIn++)<<8); \
125 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
126 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
131 ** Translate a single UTF-8 character. Return the unicode value.
133 ** During translation, assume that the byte that zTerm points
136 ** Write a pointer to the next unread byte back into *pzNext.
138 ** Notes On Invalid UTF-8:
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.
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.
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.
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.
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 */
164 c = sqlite3UtfTrans1[c-0xc0];
165 while( z!=zTerm && (*z & 0xc0)==0x80 ){
166 c = (c<<6) + (0x3f & *(z++));
169 || (c&0xFFFFF800)==0xD800
170 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
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().
182 /* #define TRANSLATE_TRACE 1 */
184 #ifndef SQLITE_OMIT_UTF16
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.
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 */
199 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
200 assert( pMem->flags&MEM_Str );
201 assert( pMem->enc!=desiredEnc );
202 assert( pMem->enc!=0 );
203 assert( pMem->n>=0 );
205 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
208 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
209 fprintf(stderr, "INPUT: %s\n", zBuf);
213 /* If the translation is between UTF-16 little and big endian, then
214 ** all that is required is to swap the byte order. This case is handled
215 ** differently from the others.
217 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
220 rc = sqlite3VdbeMemMakeWriteable(pMem);
222 assert( rc==SQLITE_NOMEM );
226 zTerm = &zIn[pMem->n];
233 pMem->enc = desiredEnc;
237 /* Set len to the maximum number of bytes required in the output buffer. */
238 if( desiredEnc==SQLITE_UTF8 ){
239 /* When converting from UTF-16, the maximum growth results from
240 ** translating a 2-byte character to a 4-byte UTF-8 character.
241 ** A single byte is required for the output string
244 len = pMem->n * 2 + 1;
246 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
247 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
248 ** character. Two bytes are required in the output buffer for the
251 len = pMem->n * 2 + 2;
254 /* Set zIn to point at the start of the input buffer and zTerm to point 1
255 ** byte past the end.
257 ** Variable zOut is set to point at the output buffer. This may be space
258 ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and
259 ** not in use, or the zShort array on the stack (see above).
262 zTerm = &zIn[pMem->n];
264 zOut = sqlite3DbMallocRaw(pMem->db, len);
273 if( pMem->enc==SQLITE_UTF8 ){
274 if( desiredEnc==SQLITE_UTF16LE ){
275 /* UTF-8 -> UTF-16 Little-endian */
277 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
281 assert( desiredEnc==SQLITE_UTF16BE );
282 /* UTF-8 -> UTF-16 Big-endian */
284 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
291 assert( desiredEnc==SQLITE_UTF8 );
292 if( pMem->enc==SQLITE_UTF16LE ){
293 /* UTF-16 Little-endian -> UTF-8 */
295 READ_UTF16LE(zIn, c);
299 /* UTF-16 Little-endian -> UTF-8 */
301 READ_UTF16BE(zIn, c);
308 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
310 sqlite3VdbeMemRelease(pMem);
311 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
312 pMem->enc = desiredEnc;
314 memcpy(pMem->zShort, zOut, len);
315 zOut = (u8*)pMem->zShort;
316 pMem->flags |= (MEM_Term|MEM_Short);
318 pMem->flags |= (MEM_Term|MEM_Dyn);
320 pMem->z = (char*)zOut;
323 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
326 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
327 fprintf(stderr, "OUTPUT: %s\n", zBuf);
334 ** This routine checks for a byte-order mark at the beginning of the
335 ** UTF-16 string stored in *pMem. If one is present, it is removed and
336 ** the encoding of the Mem adjusted. This routine does not do any
337 ** byte-swapping, it just sets Mem.enc appropriately.
339 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
340 ** changed by this function.
342 int sqlite3VdbeMemHandleBom(Mem *pMem){
346 if( pMem->n<0 || pMem->n>1 ){
347 u8 b1 = *(u8 *)pMem->z;
348 u8 b2 = *(((u8 *)pMem->z) + 1);
349 if( b1==0xFE && b2==0xFF ){
350 bom = SQLITE_UTF16BE;
352 if( b1==0xFF && b2==0xFE ){
353 bom = SQLITE_UTF16LE;
358 /* This function is called as soon as a string is stored in a Mem*,
359 ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
360 ** for the string to be stored in Mem.zShort, or for it to be stored
361 ** in dynamic memory with no destructor.
363 assert( !(pMem->flags&MEM_Short) );
364 assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
365 if( pMem->flags & MEM_Dyn ){
366 void (*xDel)(void*) = pMem->xDel;
370 rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom,
374 rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom,
380 #endif /* SQLITE_OMIT_UTF16 */
383 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
384 ** return the number of unicode characters in pZ up to (but not including)
385 ** the first 0x00 byte. If nByte is not less than zero, return the
386 ** number of unicode characters in the first nByte of pZ (or up to
387 ** the first 0x00, whichever comes first).
389 int sqlite3Utf8CharLen(const char *zIn, int nByte){
391 const u8 *z = (const u8*)zIn;
396 zTerm = (const u8*)(-1);
399 while( *z!=0 && z<zTerm ){
406 /* This test function is not currently used by the automated test-suite.
407 ** Hence it is only available in debug builds.
409 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
411 ** Translate UTF-8 to UTF-8.
413 ** This has the effect of making sure that the string is well-formed
414 ** UTF-8. Miscoded characters are removed.
416 ** The translation is done in-place (since it is impossible for the
417 ** correct UTF-8 encoding to be longer than a malformed encoding).
419 int sqlite3Utf8To8(unsigned char *zIn){
420 unsigned char *zOut = zIn;
421 unsigned char *zStart = zIn;
422 unsigned char *zTerm;
426 c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
432 return zOut - zStart;
436 #ifndef SQLITE_OMIT_UTF16
438 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
439 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
440 ** be freed by the calling function.
442 ** NULL is returned if there is an allocation error.
444 char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
446 memset(&m, 0, sizeof(m));
448 sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
449 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
450 if( db->mallocFailed ){
451 sqlite3VdbeMemRelease(&m);
454 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
455 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
456 return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
460 ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
461 ** return the number of bytes up to (but not including), the first pair
462 ** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
463 ** then return the number of bytes in the first nChar unicode characters
464 ** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
466 int sqlite3Utf16ByteLen(const void *zIn, int nChar){
470 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
471 /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
472 ** and in other parts of this file means that at one branch will
473 ** not be covered by coverage testing on any single host. But coverage
474 ** will be complete if the tests are run on both a little-endian and
475 ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
476 ** macros are constant at compile time the compiler can determine
477 ** which branch will be followed. It is therefore assumed that no runtime
478 ** penalty is paid for this "if" statement.
480 while( c && ((nChar<0) || n<nChar) ){
485 while( c && ((nChar<0) || n<nChar) ){
490 return (z-(char const *)zIn)-((c==0)?2:0);
493 #if defined(SQLITE_TEST)
495 ** This routine is called from the TCL test function "translate_selftest".
496 ** It checks that the primitives for serializing and deserializing
497 ** characters in each encoding are inverses of each other.
499 void sqlite3UtfSelfTest(){
501 unsigned char zBuf[20];
503 unsigned char *zTerm;
507 for(i=0; i<0x00110000; i++){
514 c = sqlite3Utf8Read(z, zTerm, (const u8**)&z);
516 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
517 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
519 assert( (z-zBuf)==n );
521 for(i=0; i<0x00110000; i++){
522 if( i>=0xD800 && i<0xE000 ) continue;
530 assert( (z-zBuf)==n );
532 for(i=0; i<0x00110000; i++){
533 if( i>=0xD800 && i<0xE000 ) continue;
541 assert( (z-zBuf)==n );
544 #endif /* SQLITE_TEST */
545 #endif /* SQLITE_OMIT_UTF16 */