extensions/sqlite/sqlite-source/utf.c
changeset 1203 633c25ce6405
child 1645 27307df4d711
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/utf.c	Sun Jul 29 01:15:31 2007 +0000
     1.3 @@ -0,0 +1,544 @@
     1.4 +/*
     1.5 +** 2004 April 13
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** This file contains routines used to translate between UTF-8, 
    1.16 +** UTF-16, UTF-16BE, and UTF-16LE.
    1.17 +**
    1.18 +** $Id$
    1.19 +**
    1.20 +** Notes on UTF-8:
    1.21 +**
    1.22 +**   Byte-0    Byte-1    Byte-2    Byte-3    Value
    1.23 +**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    1.24 +**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    1.25 +**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
    1.26 +**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
    1.27 +**
    1.28 +**
    1.29 +** Notes on UTF-16:  (with wwww+1==uuuuu)
    1.30 +**
    1.31 +**      Word-0               Word-1          Value
    1.32 +**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
    1.33 +**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
    1.34 +**
    1.35 +**
    1.36 +** BOM or Byte Order Mark:
    1.37 +**     0xff 0xfe   little-endian utf-16 follows
    1.38 +**     0xfe 0xff   big-endian utf-16 follows
    1.39 +**
    1.40 +*/
    1.41 +#include "sqliteInt.h"
    1.42 +#include <assert.h>
    1.43 +#include "vdbeInt.h"
    1.44 +
    1.45 +/*
    1.46 +** The following constant value is used by the SQLITE_BIGENDIAN and
    1.47 +** SQLITE_LITTLEENDIAN macros.
    1.48 +*/
    1.49 +const int sqlite3one = 1;
    1.50 +
    1.51 +/*
    1.52 +** This lookup table is used to help decode the first byte of
    1.53 +** a multi-byte UTF8 character.
    1.54 +*/
    1.55 +const unsigned char sqlite3UtfTrans1[] = {
    1.56 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    1.57 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    1.58 +  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    1.59 +  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
    1.60 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    1.61 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    1.62 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    1.63 +  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
    1.64 +};
    1.65 +
    1.66 +#define WRITE_UTF8(zOut, c) {                          \
    1.67 +  if( c<0x00080 ){                                     \
    1.68 +    *zOut++ = (c&0xFF);                                \
    1.69 +  }                                                    \
    1.70 +  else if( c<0x00800 ){                                \
    1.71 +    *zOut++ = 0xC0 + ((c>>6)&0x1F);                    \
    1.72 +    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.73 +  }                                                    \
    1.74 +  else if( c<0x10000 ){                                \
    1.75 +    *zOut++ = 0xE0 + ((c>>12)&0x0F);                   \
    1.76 +    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
    1.77 +    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.78 +  }else{                                               \
    1.79 +    *zOut++ = 0xF0 + ((c>>18) & 0x07);                 \
    1.80 +    *zOut++ = 0x80 + ((c>>12) & 0x3F);                 \
    1.81 +    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
    1.82 +    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.83 +  }                                                    \
    1.84 +}
    1.85 +
    1.86 +#define WRITE_UTF16LE(zOut, c) {                                \
    1.87 +  if( c<=0xFFFF ){                                              \
    1.88 +    *zOut++ = (c&0x00FF);                                       \
    1.89 +    *zOut++ = ((c>>8)&0x00FF);                                  \
    1.90 +  }else{                                                        \
    1.91 +    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
    1.92 +    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
    1.93 +    *zOut++ = (c&0x00FF);                                       \
    1.94 +    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
    1.95 +  }                                                             \
    1.96 +}
    1.97 +
    1.98 +#define WRITE_UTF16BE(zOut, c) {                                \
    1.99 +  if( c<=0xFFFF ){                                              \
   1.100 +    *zOut++ = ((c>>8)&0x00FF);                                  \
   1.101 +    *zOut++ = (c&0x00FF);                                       \
   1.102 +  }else{                                                        \
   1.103 +    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
   1.104 +    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   1.105 +    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
   1.106 +    *zOut++ = (c&0x00FF);                                       \
   1.107 +  }                                                             \
   1.108 +}
   1.109 +
   1.110 +#define READ_UTF16LE(zIn, c){                                         \
   1.111 +  c = (*zIn++);                                                       \
   1.112 +  c += ((*zIn++)<<8);                                                 \
   1.113 +  if( c>=0xD800 && c<0xE000 ){                                       \
   1.114 +    int c2 = (*zIn++);                                                \
   1.115 +    c2 += ((*zIn++)<<8);                                              \
   1.116 +    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   1.117 +    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
   1.118 +  }                                                                   \
   1.119 +}
   1.120 +
   1.121 +#define READ_UTF16BE(zIn, c){                                         \
   1.122 +  c = ((*zIn++)<<8);                                                  \
   1.123 +  c += (*zIn++);                                                      \
   1.124 +  if( c>=0xD800 && c<0xE000 ){                                       \
   1.125 +    int c2 = ((*zIn++)<<8);                                           \
   1.126 +    c2 += (*zIn++);                                                   \
   1.127 +    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   1.128 +    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
   1.129 +  }                                                                   \
   1.130 +}
   1.131 +
   1.132 +/*
   1.133 +** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
   1.134 +** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
   1.135 +*/ 
   1.136 +/* #define TRANSLATE_TRACE 1 */
   1.137 +
   1.138 +#ifndef SQLITE_OMIT_UTF16
   1.139 +/*
   1.140 +** This routine transforms the internal text encoding used by pMem to
   1.141 +** desiredEnc. It is an error if the string is already of the desired
   1.142 +** encoding, or if *pMem does not contain a string value.
   1.143 +*/
   1.144 +int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
   1.145 +  unsigned char zShort[NBFS]; /* Temporary short output buffer */
   1.146 +  int len;                    /* Maximum length of output string in bytes */
   1.147 +  unsigned char *zOut;                  /* Output buffer */
   1.148 +  unsigned char *zIn;                   /* Input iterator */
   1.149 +  unsigned char *zTerm;                 /* End of input */
   1.150 +  unsigned char *z;                     /* Output iterator */
   1.151 +  unsigned int c;
   1.152 +
   1.153 +  assert( pMem->flags&MEM_Str );
   1.154 +  assert( pMem->enc!=desiredEnc );
   1.155 +  assert( pMem->enc!=0 );
   1.156 +  assert( pMem->n>=0 );
   1.157 +
   1.158 +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   1.159 +  {
   1.160 +    char zBuf[100];
   1.161 +    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   1.162 +    fprintf(stderr, "INPUT:  %s\n", zBuf);
   1.163 +  }
   1.164 +#endif
   1.165 +
   1.166 +  /* If the translation is between UTF-16 little and big endian, then 
   1.167 +  ** all that is required is to swap the byte order. This case is handled
   1.168 +  ** differently from the others.
   1.169 +  */
   1.170 +  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
   1.171 +    u8 temp;
   1.172 +    int rc;
   1.173 +    rc = sqlite3VdbeMemMakeWriteable(pMem);
   1.174 +    if( rc!=SQLITE_OK ){
   1.175 +      assert( rc==SQLITE_NOMEM );
   1.176 +      return SQLITE_NOMEM;
   1.177 +    }
   1.178 +    zIn = (u8*)pMem->z;
   1.179 +    zTerm = &zIn[pMem->n];
   1.180 +    while( zIn<zTerm ){
   1.181 +      temp = *zIn;
   1.182 +      *zIn = *(zIn+1);
   1.183 +      zIn++;
   1.184 +      *zIn++ = temp;
   1.185 +    }
   1.186 +    pMem->enc = desiredEnc;
   1.187 +    goto translate_out;
   1.188 +  }
   1.189 +
   1.190 +  /* Set len to the maximum number of bytes required in the output buffer. */
   1.191 +  if( desiredEnc==SQLITE_UTF8 ){
   1.192 +    /* When converting from UTF-16, the maximum growth results from
   1.193 +    ** translating a 2-byte character to a 4-byte UTF-8 character.
   1.194 +    ** A single byte is required for the output string
   1.195 +    ** nul-terminator.
   1.196 +    */
   1.197 +    len = pMem->n * 2 + 1;
   1.198 +  }else{
   1.199 +    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
   1.200 +    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
   1.201 +    ** character. Two bytes are required in the output buffer for the
   1.202 +    ** nul-terminator.
   1.203 +    */
   1.204 +    len = pMem->n * 2 + 2;
   1.205 +  }
   1.206 +
   1.207 +  /* Set zIn to point at the start of the input buffer and zTerm to point 1
   1.208 +  ** byte past the end.
   1.209 +  **
   1.210 +  ** Variable zOut is set to point at the output buffer. This may be space
   1.211 +  ** obtained from malloc(), or Mem.zShort, if it large enough and not in
   1.212 +  ** use, or the zShort array on the stack (see above).
   1.213 +  */
   1.214 +  zIn = (u8*)pMem->z;
   1.215 +  zTerm = &zIn[pMem->n];
   1.216 +  if( len>NBFS ){
   1.217 +    zOut = sqliteMallocRaw(len);
   1.218 +    if( !zOut ) return SQLITE_NOMEM;
   1.219 +  }else{
   1.220 +    zOut = zShort;
   1.221 +  }
   1.222 +  z = zOut;
   1.223 +
   1.224 +  if( pMem->enc==SQLITE_UTF8 ){
   1.225 +    unsigned int iExtra = 0xD800;
   1.226 +
   1.227 +    if( 0==(pMem->flags&MEM_Term) && zTerm>zIn && (zTerm[-1]&0x80) ){
   1.228 +      /* This UTF8 string is not nul-terminated, and the last byte is
   1.229 +      ** not a character in the ascii range (codpoints 0..127). This
   1.230 +      ** means the SQLITE_READ_UTF8() macro might read past the end
   1.231 +      ** of the allocated buffer.
   1.232 +      **
   1.233 +      ** There are four possibilities:
   1.234 +      **
   1.235 +      **   1. The last byte is the first byte of a non-ASCII character,
   1.236 +      **
   1.237 +      **   2. The final N bytes of the input string are continuation bytes
   1.238 +      **      and immediately preceding them is the first byte of a 
   1.239 +      **      non-ASCII character.
   1.240 +      **
   1.241 +      **   3. The final N bytes of the input string are continuation bytes
   1.242 +      **      and immediately preceding them is a byte that encodes a 
   1.243 +      **      character in the ASCII range.
   1.244 +      **
   1.245 +      **   4. The entire string consists of continuation characters.
   1.246 +      **
   1.247 +      ** Cases (3) and (4) require no special handling. The SQLITE_READ_UTF8()
   1.248 +      ** macro will not overread the buffer in these cases.
   1.249 +      */
   1.250 +      unsigned char *zExtra = &zTerm[-1];
   1.251 +      while( zExtra>zIn && (zExtra[0]&0xC0)==0x80 ){
   1.252 +        zExtra--;
   1.253 +      }
   1.254 +
   1.255 +      if( (zExtra[0]&0xC0)==0xC0 ){
   1.256 +        /* Make a copy of the last character encoding in the input string.
   1.257 +        ** Then make sure it is nul-terminated and use SQLITE_READ_UTF8()
   1.258 +        ** to decode the codepoint. Store the codepoint in variable iExtra,
   1.259 +        ** it will be appended to the output string later.
   1.260 +        */
   1.261 +        unsigned char *zFree = 0;
   1.262 +        unsigned char zBuf[16];
   1.263 +        int nExtra = (pMem->n+zIn-zExtra);
   1.264 +        zTerm = zExtra;
   1.265 +        if( nExtra>15 ){
   1.266 +          zExtra = sqliteMallocRaw(nExtra+1);
   1.267 +          if( !zExtra ){
   1.268 +            return SQLITE_NOMEM;
   1.269 +          }
   1.270 +          zFree = zExtra;
   1.271 +        }else{
   1.272 +          zExtra = zBuf;
   1.273 +        }
   1.274 +        memcpy(zExtra, zTerm, nExtra);
   1.275 +        zExtra[nExtra] = '\0';
   1.276 +        SQLITE_READ_UTF8(zExtra, iExtra);
   1.277 +        sqliteFree(zFree);
   1.278 +      }
   1.279 +    }
   1.280 +
   1.281 +    if( desiredEnc==SQLITE_UTF16LE ){
   1.282 +      /* UTF-8 -> UTF-16 Little-endian */
   1.283 +      while( zIn<zTerm ){
   1.284 +        SQLITE_READ_UTF8(zIn, c); 
   1.285 +        WRITE_UTF16LE(z, c);
   1.286 +      }
   1.287 +      if( iExtra!=0xD800 ){
   1.288 +        WRITE_UTF16LE(z, iExtra);
   1.289 +      }
   1.290 +    }else{
   1.291 +      assert( desiredEnc==SQLITE_UTF16BE );
   1.292 +      /* UTF-8 -> UTF-16 Big-endian */
   1.293 +      while( zIn<zTerm ){
   1.294 +        SQLITE_READ_UTF8(zIn, c); 
   1.295 +        WRITE_UTF16BE(z, c);
   1.296 +      }
   1.297 +      if( iExtra!=0xD800 ){
   1.298 +        WRITE_UTF16BE(z, iExtra);
   1.299 +      }
   1.300 +    }
   1.301 +    pMem->n = z - zOut;
   1.302 +    *z++ = 0;
   1.303 +  }else{
   1.304 +    assert( desiredEnc==SQLITE_UTF8 );
   1.305 +    if( pMem->enc==SQLITE_UTF16LE ){
   1.306 +      /* UTF-16 Little-endian -> UTF-8 */
   1.307 +      while( zIn<zTerm ){
   1.308 +        READ_UTF16LE(zIn, c); 
   1.309 +        WRITE_UTF8(z, c);
   1.310 +      }
   1.311 +    }else{
   1.312 +      /* UTF-16 Little-endian -> UTF-8 */
   1.313 +      while( zIn<zTerm ){
   1.314 +        READ_UTF16BE(zIn, c); 
   1.315 +        WRITE_UTF8(z, c);
   1.316 +      }
   1.317 +    }
   1.318 +    pMem->n = z - zOut;
   1.319 +  }
   1.320 +  *z = 0;
   1.321 +  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   1.322 +
   1.323 +  sqlite3VdbeMemRelease(pMem);
   1.324 +  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
   1.325 +  pMem->enc = desiredEnc;
   1.326 +  if( zOut==zShort ){
   1.327 +    memcpy(pMem->zShort, zOut, len);
   1.328 +    zOut = (u8*)pMem->zShort;
   1.329 +    pMem->flags |= (MEM_Term|MEM_Short);
   1.330 +  }else{
   1.331 +    pMem->flags |= (MEM_Term|MEM_Dyn);
   1.332 +  }
   1.333 +  pMem->z = (char*)zOut;
   1.334 +
   1.335 +translate_out:
   1.336 +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   1.337 +  {
   1.338 +    char zBuf[100];
   1.339 +    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   1.340 +    fprintf(stderr, "OUTPUT: %s\n", zBuf);
   1.341 +  }
   1.342 +#endif
   1.343 +  return SQLITE_OK;
   1.344 +}
   1.345 +
   1.346 +/*
   1.347 +** This routine checks for a byte-order mark at the beginning of the 
   1.348 +** UTF-16 string stored in *pMem. If one is present, it is removed and
   1.349 +** the encoding of the Mem adjusted. This routine does not do any
   1.350 +** byte-swapping, it just sets Mem.enc appropriately.
   1.351 +**
   1.352 +** The allocation (static, dynamic etc.) and encoding of the Mem may be
   1.353 +** changed by this function.
   1.354 +*/
   1.355 +int sqlite3VdbeMemHandleBom(Mem *pMem){
   1.356 +  int rc = SQLITE_OK;
   1.357 +  u8 bom = 0;
   1.358 +
   1.359 +  if( pMem->n<0 || pMem->n>1 ){
   1.360 +    u8 b1 = *(u8 *)pMem->z;
   1.361 +    u8 b2 = *(((u8 *)pMem->z) + 1);
   1.362 +    if( b1==0xFE && b2==0xFF ){
   1.363 +      bom = SQLITE_UTF16BE;
   1.364 +    }
   1.365 +    if( b1==0xFF && b2==0xFE ){
   1.366 +      bom = SQLITE_UTF16LE;
   1.367 +    }
   1.368 +  }
   1.369 +  
   1.370 +  if( bom ){
   1.371 +    /* This function is called as soon as a string is stored in a Mem*,
   1.372 +    ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
   1.373 +    ** for the string to be stored in Mem.zShort, or for it to be stored
   1.374 +    ** in dynamic memory with no destructor.
   1.375 +    */
   1.376 +    assert( !(pMem->flags&MEM_Short) );
   1.377 +    assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
   1.378 +    if( pMem->flags & MEM_Dyn ){
   1.379 +      void (*xDel)(void*) = pMem->xDel;
   1.380 +      char *z = pMem->z;
   1.381 +      pMem->z = 0;
   1.382 +      pMem->xDel = 0;
   1.383 +      rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, SQLITE_TRANSIENT);
   1.384 +      xDel(z);
   1.385 +    }else{
   1.386 +      rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, 
   1.387 +          SQLITE_TRANSIENT);
   1.388 +    }
   1.389 +  }
   1.390 +  return rc;
   1.391 +}
   1.392 +#endif /* SQLITE_OMIT_UTF16 */
   1.393 +
   1.394 +/*
   1.395 +** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
   1.396 +** return the number of unicode characters in pZ up to (but not including)
   1.397 +** the first 0x00 byte. If nByte is not less than zero, return the
   1.398 +** number of unicode characters in the first nByte of pZ (or up to 
   1.399 +** the first 0x00, whichever comes first).
   1.400 +*/
   1.401 +int sqlite3Utf8CharLen(const char *zIn, int nByte){
   1.402 +  int r = 0;
   1.403 +  const u8 *z = (const u8*)zIn;
   1.404 +  const u8 *zTerm;
   1.405 +  if( nByte>=0 ){
   1.406 +    zTerm = &z[nByte];
   1.407 +  }else{
   1.408 +    zTerm = (const u8*)(-1);
   1.409 +  }
   1.410 +  assert( z<=zTerm );
   1.411 +  while( *z!=0 && z<zTerm ){
   1.412 +    SQLITE_SKIP_UTF8(z);
   1.413 +    r++;
   1.414 +  }
   1.415 +  return r;
   1.416 +}
   1.417 +
   1.418 +#ifndef SQLITE_OMIT_UTF16
   1.419 +/*
   1.420 +** Convert a UTF-16 string in the native encoding into a UTF-8 string.
   1.421 +** Memory to hold the UTF-8 string is obtained from malloc and must be
   1.422 +** freed by the calling function.
   1.423 +**
   1.424 +** NULL is returned if there is an allocation error.
   1.425 +*/
   1.426 +char *sqlite3Utf16to8(const void *z, int nByte){
   1.427 +  Mem m;
   1.428 +  memset(&m, 0, sizeof(m));
   1.429 +  sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   1.430 +  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   1.431 +  assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() );
   1.432 +  assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() );
   1.433 +  return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z);
   1.434 +}
   1.435 +
   1.436 +/*
   1.437 +** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
   1.438 +** return the number of bytes up to (but not including), the first pair
   1.439 +** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
   1.440 +** then return the number of bytes in the first nChar unicode characters
   1.441 +** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
   1.442 +*/
   1.443 +int sqlite3Utf16ByteLen(const void *zIn, int nChar){
   1.444 +  unsigned int c = 1;
   1.445 +  char const *z = zIn;
   1.446 +  int n = 0;
   1.447 +  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
   1.448 +    /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
   1.449 +    ** and in other parts of this file means that at one branch will
   1.450 +    ** not be covered by coverage testing on any single host. But coverage
   1.451 +    ** will be complete if the tests are run on both a little-endian and 
   1.452 +    ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
   1.453 +    ** macros are constant at compile time the compiler can determine
   1.454 +    ** which branch will be followed. It is therefore assumed that no runtime
   1.455 +    ** penalty is paid for this "if" statement.
   1.456 +    */
   1.457 +    while( c && ((nChar<0) || n<nChar) ){
   1.458 +      READ_UTF16BE(z, c);
   1.459 +      n++;
   1.460 +    }
   1.461 +  }else{
   1.462 +    while( c && ((nChar<0) || n<nChar) ){
   1.463 +      READ_UTF16LE(z, c);
   1.464 +      n++;
   1.465 +    }
   1.466 +  }
   1.467 +  return (z-(char const *)zIn)-((c==0)?2:0);
   1.468 +}
   1.469 +
   1.470 +#if defined(SQLITE_TEST)
   1.471 +/*
   1.472 +** Translate UTF-8 to UTF-8.
   1.473 +**
   1.474 +** This has the effect of making sure that the string is well-formed
   1.475 +** UTF-8.  Miscoded characters are removed.
   1.476 +**
   1.477 +** The translation is done in-place (since it is impossible for the
   1.478 +** correct UTF-8 encoding to be longer than a malformed encoding).
   1.479 +*/
   1.480 +int sqlite3Utf8To8(unsigned char *zIn){
   1.481 +  unsigned char *zOut = zIn;
   1.482 +  unsigned char *zStart = zIn;
   1.483 +  int c;
   1.484 +
   1.485 +  while(1){
   1.486 +    SQLITE_READ_UTF8(zIn, c);
   1.487 +    if( c==0 ) break;
   1.488 +    if( c!=0xfffd ){
   1.489 +      WRITE_UTF8(zOut, c);
   1.490 +    }
   1.491 +  }
   1.492 +  *zOut = 0;
   1.493 +  return zOut - zStart;
   1.494 +}
   1.495 +#endif
   1.496 +
   1.497 +#if defined(SQLITE_TEST)
   1.498 +/*
   1.499 +** This routine is called from the TCL test function "translate_selftest".
   1.500 +** It checks that the primitives for serializing and deserializing
   1.501 +** characters in each encoding are inverses of each other.
   1.502 +*/
   1.503 +void sqlite3UtfSelfTest(){
   1.504 +  unsigned int i, t;
   1.505 +  unsigned char zBuf[20];
   1.506 +  unsigned char *z;
   1.507 +  int n;
   1.508 +  unsigned int c;
   1.509 +
   1.510 +  for(i=0; i<0x00110000; i++){
   1.511 +    z = zBuf;
   1.512 +    WRITE_UTF8(z, i);
   1.513 +    n = z-zBuf;
   1.514 +    z[0] = 0;
   1.515 +    z = zBuf;
   1.516 +    SQLITE_READ_UTF8(z, c);
   1.517 +    t = i;
   1.518 +    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
   1.519 +    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
   1.520 +    assert( c==t );
   1.521 +    assert( (z-zBuf)==n );
   1.522 +  }
   1.523 +  for(i=0; i<0x00110000; i++){
   1.524 +    if( i>=0xD800 && i<0xE000 ) continue;
   1.525 +    z = zBuf;
   1.526 +    WRITE_UTF16LE(z, i);
   1.527 +    n = z-zBuf;
   1.528 +    z[0] = 0;
   1.529 +    z = zBuf;
   1.530 +    READ_UTF16LE(z, c);
   1.531 +    assert( c==i );
   1.532 +    assert( (z-zBuf)==n );
   1.533 +  }
   1.534 +  for(i=0; i<0x00110000; i++){
   1.535 +    if( i>=0xD800 && i<0xE000 ) continue;
   1.536 +    z = zBuf;
   1.537 +    WRITE_UTF16BE(z, i);
   1.538 +    n = z-zBuf;
   1.539 +    z[0] = 0;
   1.540 +    z = zBuf;
   1.541 +    READ_UTF16BE(z, c);
   1.542 +    assert( c==i );
   1.543 +    assert( (z-zBuf)==n );
   1.544 +  }
   1.545 +}
   1.546 +#endif /* SQLITE_TEST */
   1.547 +#endif /* SQLITE_OMIT_UTF16 */