extensions/sqlite/sqlite-source/utf.c
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sqlite/sqlite-source/utf.c	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,545 +0,0 @@
     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 -static 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 -
    1.67 -#define WRITE_UTF8(zOut, c) {                          \
    1.68 -  if( c<0x00080 ){                                     \
    1.69 -    *zOut++ = (c&0xFF);                                \
    1.70 -  }                                                    \
    1.71 -  else if( c<0x00800 ){                                \
    1.72 -    *zOut++ = 0xC0 + ((c>>6)&0x1F);                    \
    1.73 -    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.74 -  }                                                    \
    1.75 -  else if( c<0x10000 ){                                \
    1.76 -    *zOut++ = 0xE0 + ((c>>12)&0x0F);                   \
    1.77 -    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
    1.78 -    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.79 -  }else{                                               \
    1.80 -    *zOut++ = 0xF0 + ((c>>18) & 0x07);                 \
    1.81 -    *zOut++ = 0x80 + ((c>>12) & 0x3F);                 \
    1.82 -    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
    1.83 -    *zOut++ = 0x80 + (c & 0x3F);                       \
    1.84 -  }                                                    \
    1.85 -}
    1.86 -
    1.87 -#define WRITE_UTF16LE(zOut, c) {                                \
    1.88 -  if( c<=0xFFFF ){                                              \
    1.89 -    *zOut++ = (c&0x00FF);                                       \
    1.90 -    *zOut++ = ((c>>8)&0x00FF);                                  \
    1.91 -  }else{                                                        \
    1.92 -    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
    1.93 -    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
    1.94 -    *zOut++ = (c&0x00FF);                                       \
    1.95 -    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
    1.96 -  }                                                             \
    1.97 -}
    1.98 -
    1.99 -#define WRITE_UTF16BE(zOut, c) {                                \
   1.100 -  if( c<=0xFFFF ){                                              \
   1.101 -    *zOut++ = ((c>>8)&0x00FF);                                  \
   1.102 -    *zOut++ = (c&0x00FF);                                       \
   1.103 -  }else{                                                        \
   1.104 -    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
   1.105 -    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   1.106 -    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
   1.107 -    *zOut++ = (c&0x00FF);                                       \
   1.108 -  }                                                             \
   1.109 -}
   1.110 -
   1.111 -#define READ_UTF16LE(zIn, c){                                         \
   1.112 -  c = (*zIn++);                                                       \
   1.113 -  c += ((*zIn++)<<8);                                                 \
   1.114 -  if( c>=0xD800 && c<0xE000 ){                                       \
   1.115 -    int c2 = (*zIn++);                                                \
   1.116 -    c2 += ((*zIn++)<<8);                                              \
   1.117 -    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   1.118 -    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
   1.119 -  }                                                                   \
   1.120 -}
   1.121 -
   1.122 -#define READ_UTF16BE(zIn, c){                                         \
   1.123 -  c = ((*zIn++)<<8);                                                  \
   1.124 -  c += (*zIn++);                                                      \
   1.125 -  if( c>=0xD800 && c<0xE000 ){                                       \
   1.126 -    int c2 = ((*zIn++)<<8);                                           \
   1.127 -    c2 += (*zIn++);                                                   \
   1.128 -    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   1.129 -    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
   1.130 -  }                                                                   \
   1.131 -}
   1.132 -
   1.133 -/*
   1.134 -** Translate a single UTF-8 character.  Return the unicode value.
   1.135 -**
   1.136 -** During translation, assume that the byte that zTerm points
   1.137 -** is a 0x00.
   1.138 -**
   1.139 -** Write a pointer to the next unread byte back into *pzNext.
   1.140 -**
   1.141 -** Notes On Invalid UTF-8:
   1.142 -**
   1.143 -**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
   1.144 -**     be encoded as a multi-byte character.  Any multi-byte character that
   1.145 -**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
   1.146 -**
   1.147 -**  *  This routine never allows a UTF16 surrogate value to be encoded.
   1.148 -**     If a multi-byte character attempts to encode a value between
   1.149 -**     0xd800 and 0xe000 then it is rendered as 0xfffd.
   1.150 -**
   1.151 -**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
   1.152 -**     byte of a character are interpreted as single-byte characters
   1.153 -**     and rendered as themselves even though they are technically
   1.154 -**     invalid characters.
   1.155 -**
   1.156 -**  *  This routine accepts an infinite number of different UTF8 encodings
   1.157 -**     for unicode values 0x80 and greater.  It do not change over-length
   1.158 -**     encodings to 0xfffd as some systems recommend.
   1.159 -*/
   1.160 -int sqlite3Utf8Read(
   1.161 -  const unsigned char *z,         /* First byte of UTF-8 character */
   1.162 -  const unsigned char *zTerm,     /* Pretend this byte is 0x00 */
   1.163 -  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
   1.164 -){
   1.165 -  int c = *(z++);
   1.166 -  if( c>=0xc0 ){
   1.167 -    c = sqlite3UtfTrans1[c-0xc0];
   1.168 -    while( z!=zTerm && (*z & 0xc0)==0x80 ){
   1.169 -      c = (c<<6) + (0x3f & *(z++));
   1.170 -    }
   1.171 -    if( c<0x80
   1.172 -        || (c&0xFFFFF800)==0xD800
   1.173 -        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
   1.174 -  }
   1.175 -  *pzNext = z;
   1.176 -  return c;
   1.177 -}
   1.178 -
   1.179 -
   1.180 -
   1.181 -/*
   1.182 -** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
   1.183 -** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
   1.184 -*/ 
   1.185 -/* #define TRANSLATE_TRACE 1 */
   1.186 -
   1.187 -#ifndef SQLITE_OMIT_UTF16
   1.188 -/*
   1.189 -** This routine transforms the internal text encoding used by pMem to
   1.190 -** desiredEnc. It is an error if the string is already of the desired
   1.191 -** encoding, or if *pMem does not contain a string value.
   1.192 -*/
   1.193 -int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
   1.194 -  unsigned char zShort[NBFS]; /* Temporary short output buffer */
   1.195 -  int len;                    /* Maximum length of output string in bytes */
   1.196 -  unsigned char *zOut;                  /* Output buffer */
   1.197 -  unsigned char *zIn;                   /* Input iterator */
   1.198 -  unsigned char *zTerm;                 /* End of input */
   1.199 -  unsigned char *z;                     /* Output iterator */
   1.200 -  unsigned int c;
   1.201 -
   1.202 -  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   1.203 -  assert( pMem->flags&MEM_Str );
   1.204 -  assert( pMem->enc!=desiredEnc );
   1.205 -  assert( pMem->enc!=0 );
   1.206 -  assert( pMem->n>=0 );
   1.207 -
   1.208 -#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   1.209 -  {
   1.210 -    char zBuf[100];
   1.211 -    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   1.212 -    fprintf(stderr, "INPUT:  %s\n", zBuf);
   1.213 -  }
   1.214 -#endif
   1.215 -
   1.216 -  /* If the translation is between UTF-16 little and big endian, then 
   1.217 -  ** all that is required is to swap the byte order. This case is handled
   1.218 -  ** differently from the others.
   1.219 -  */
   1.220 -  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
   1.221 -    u8 temp;
   1.222 -    int rc;
   1.223 -    rc = sqlite3VdbeMemMakeWriteable(pMem);
   1.224 -    if( rc!=SQLITE_OK ){
   1.225 -      assert( rc==SQLITE_NOMEM );
   1.226 -      return SQLITE_NOMEM;
   1.227 -    }
   1.228 -    zIn = (u8*)pMem->z;
   1.229 -    zTerm = &zIn[pMem->n];
   1.230 -    while( zIn<zTerm ){
   1.231 -      temp = *zIn;
   1.232 -      *zIn = *(zIn+1);
   1.233 -      zIn++;
   1.234 -      *zIn++ = temp;
   1.235 -    }
   1.236 -    pMem->enc = desiredEnc;
   1.237 -    goto translate_out;
   1.238 -  }
   1.239 -
   1.240 -  /* Set len to the maximum number of bytes required in the output buffer. */
   1.241 -  if( desiredEnc==SQLITE_UTF8 ){
   1.242 -    /* When converting from UTF-16, the maximum growth results from
   1.243 -    ** translating a 2-byte character to a 4-byte UTF-8 character.
   1.244 -    ** A single byte is required for the output string
   1.245 -    ** nul-terminator.
   1.246 -    */
   1.247 -    len = pMem->n * 2 + 1;
   1.248 -  }else{
   1.249 -    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
   1.250 -    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
   1.251 -    ** character. Two bytes are required in the output buffer for the
   1.252 -    ** nul-terminator.
   1.253 -    */
   1.254 -    len = pMem->n * 2 + 2;
   1.255 -  }
   1.256 -
   1.257 -  /* Set zIn to point at the start of the input buffer and zTerm to point 1
   1.258 -  ** byte past the end.
   1.259 -  **
   1.260 -  ** Variable zOut is set to point at the output buffer. This may be space
   1.261 -  ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and
   1.262 -  ** not in use, or the zShort array on the stack (see above).
   1.263 -  */
   1.264 -  zIn = (u8*)pMem->z;
   1.265 -  zTerm = &zIn[pMem->n];
   1.266 -  if( len>NBFS ){
   1.267 -    zOut = sqlite3DbMallocRaw(pMem->db, len);
   1.268 -    if( !zOut ){
   1.269 -      return SQLITE_NOMEM;
   1.270 -    }
   1.271 -  }else{
   1.272 -    zOut = zShort;
   1.273 -  }
   1.274 -  z = zOut;
   1.275 -
   1.276 -  if( pMem->enc==SQLITE_UTF8 ){
   1.277 -    if( desiredEnc==SQLITE_UTF16LE ){
   1.278 -      /* UTF-8 -> UTF-16 Little-endian */
   1.279 -      while( zIn<zTerm ){
   1.280 -        c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
   1.281 -        WRITE_UTF16LE(z, c);
   1.282 -      }
   1.283 -    }else{
   1.284 -      assert( desiredEnc==SQLITE_UTF16BE );
   1.285 -      /* UTF-8 -> UTF-16 Big-endian */
   1.286 -      while( zIn<zTerm ){
   1.287 -        c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
   1.288 -        WRITE_UTF16BE(z, c);
   1.289 -      }
   1.290 -    }
   1.291 -    pMem->n = z - zOut;
   1.292 -    *z++ = 0;
   1.293 -  }else{
   1.294 -    assert( desiredEnc==SQLITE_UTF8 );
   1.295 -    if( pMem->enc==SQLITE_UTF16LE ){
   1.296 -      /* UTF-16 Little-endian -> UTF-8 */
   1.297 -      while( zIn<zTerm ){
   1.298 -        READ_UTF16LE(zIn, c); 
   1.299 -        WRITE_UTF8(z, c);
   1.300 -      }
   1.301 -    }else{
   1.302 -      /* UTF-16 Little-endian -> UTF-8 */
   1.303 -      while( zIn<zTerm ){
   1.304 -        READ_UTF16BE(zIn, c); 
   1.305 -        WRITE_UTF8(z, c);
   1.306 -      }
   1.307 -    }
   1.308 -    pMem->n = z - zOut;
   1.309 -  }
   1.310 -  *z = 0;
   1.311 -  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   1.312 -
   1.313 -  sqlite3VdbeMemRelease(pMem);
   1.314 -  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
   1.315 -  pMem->enc = desiredEnc;
   1.316 -  if( zOut==zShort ){
   1.317 -    memcpy(pMem->zShort, zOut, len);
   1.318 -    zOut = (u8*)pMem->zShort;
   1.319 -    pMem->flags |= (MEM_Term|MEM_Short);
   1.320 -  }else{
   1.321 -    pMem->flags |= (MEM_Term|MEM_Dyn);
   1.322 -  }
   1.323 -  pMem->z = (char*)zOut;
   1.324 -
   1.325 -translate_out:
   1.326 -#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   1.327 -  {
   1.328 -    char zBuf[100];
   1.329 -    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   1.330 -    fprintf(stderr, "OUTPUT: %s\n", zBuf);
   1.331 -  }
   1.332 -#endif
   1.333 -  return SQLITE_OK;
   1.334 -}
   1.335 -
   1.336 -/*
   1.337 -** This routine checks for a byte-order mark at the beginning of the 
   1.338 -** UTF-16 string stored in *pMem. If one is present, it is removed and
   1.339 -** the encoding of the Mem adjusted. This routine does not do any
   1.340 -** byte-swapping, it just sets Mem.enc appropriately.
   1.341 -**
   1.342 -** The allocation (static, dynamic etc.) and encoding of the Mem may be
   1.343 -** changed by this function.
   1.344 -*/
   1.345 -int sqlite3VdbeMemHandleBom(Mem *pMem){
   1.346 -  int rc = SQLITE_OK;
   1.347 -  u8 bom = 0;
   1.348 -
   1.349 -  if( pMem->n<0 || pMem->n>1 ){
   1.350 -    u8 b1 = *(u8 *)pMem->z;
   1.351 -    u8 b2 = *(((u8 *)pMem->z) + 1);
   1.352 -    if( b1==0xFE && b2==0xFF ){
   1.353 -      bom = SQLITE_UTF16BE;
   1.354 -    }
   1.355 -    if( b1==0xFF && b2==0xFE ){
   1.356 -      bom = SQLITE_UTF16LE;
   1.357 -    }
   1.358 -  }
   1.359 -  
   1.360 -  if( bom ){
   1.361 -    /* This function is called as soon as a string is stored in a Mem*,
   1.362 -    ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
   1.363 -    ** for the string to be stored in Mem.zShort, or for it to be stored
   1.364 -    ** in dynamic memory with no destructor.
   1.365 -    */
   1.366 -    assert( !(pMem->flags&MEM_Short) );
   1.367 -    assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
   1.368 -    if( pMem->flags & MEM_Dyn ){
   1.369 -      void (*xDel)(void*) = pMem->xDel;
   1.370 -      char *z = pMem->z;
   1.371 -      pMem->z = 0;
   1.372 -      pMem->xDel = 0;
   1.373 -      rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, 
   1.374 -          SQLITE_TRANSIENT);
   1.375 -      xDel(z);
   1.376 -    }else{
   1.377 -      rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, 
   1.378 -          SQLITE_TRANSIENT);
   1.379 -    }
   1.380 -  }
   1.381 -  return rc;
   1.382 -}
   1.383 -#endif /* SQLITE_OMIT_UTF16 */
   1.384 -
   1.385 -/*
   1.386 -** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
   1.387 -** return the number of unicode characters in pZ up to (but not including)
   1.388 -** the first 0x00 byte. If nByte is not less than zero, return the
   1.389 -** number of unicode characters in the first nByte of pZ (or up to 
   1.390 -** the first 0x00, whichever comes first).
   1.391 -*/
   1.392 -int sqlite3Utf8CharLen(const char *zIn, int nByte){
   1.393 -  int r = 0;
   1.394 -  const u8 *z = (const u8*)zIn;
   1.395 -  const u8 *zTerm;
   1.396 -  if( nByte>=0 ){
   1.397 -    zTerm = &z[nByte];
   1.398 -  }else{
   1.399 -    zTerm = (const u8*)(-1);
   1.400 -  }
   1.401 -  assert( z<=zTerm );
   1.402 -  while( *z!=0 && z<zTerm ){
   1.403 -    SQLITE_SKIP_UTF8(z);
   1.404 -    r++;
   1.405 -  }
   1.406 -  return r;
   1.407 -}
   1.408 -
   1.409 -/* This test function is not currently used by the automated test-suite. 
   1.410 -** Hence it is only available in debug builds.
   1.411 -*/
   1.412 -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   1.413 -/*
   1.414 -** Translate UTF-8 to UTF-8.
   1.415 -**
   1.416 -** This has the effect of making sure that the string is well-formed
   1.417 -** UTF-8.  Miscoded characters are removed.
   1.418 -**
   1.419 -** The translation is done in-place (since it is impossible for the
   1.420 -** correct UTF-8 encoding to be longer than a malformed encoding).
   1.421 -*/
   1.422 -int sqlite3Utf8To8(unsigned char *zIn){
   1.423 -  unsigned char *zOut = zIn;
   1.424 -  unsigned char *zStart = zIn;
   1.425 -  unsigned char *zTerm;
   1.426 -  u32 c;
   1.427 -
   1.428 -  while( zIn[0] ){
   1.429 -    c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
   1.430 -    if( c!=0xfffd ){
   1.431 -      WRITE_UTF8(zOut, c);
   1.432 -    }
   1.433 -  }
   1.434 -  *zOut = 0;
   1.435 -  return zOut - zStart;
   1.436 -}
   1.437 -#endif
   1.438 -
   1.439 -#ifndef SQLITE_OMIT_UTF16
   1.440 -/*
   1.441 -** Convert a UTF-16 string in the native encoding into a UTF-8 string.
   1.442 -** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
   1.443 -** be freed by the calling function.
   1.444 -**
   1.445 -** NULL is returned if there is an allocation error.
   1.446 -*/
   1.447 -char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
   1.448 -  Mem m;
   1.449 -  memset(&m, 0, sizeof(m));
   1.450 -  m.db = db;
   1.451 -  sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   1.452 -  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   1.453 -  if( db->mallocFailed ){
   1.454 -    sqlite3VdbeMemRelease(&m);
   1.455 -    m.z = 0;
   1.456 -  }
   1.457 -  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
   1.458 -  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
   1.459 -  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
   1.460 -}
   1.461 -
   1.462 -/*
   1.463 -** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
   1.464 -** return the number of bytes up to (but not including), the first pair
   1.465 -** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
   1.466 -** then return the number of bytes in the first nChar unicode characters
   1.467 -** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
   1.468 -*/
   1.469 -int sqlite3Utf16ByteLen(const void *zIn, int nChar){
   1.470 -  unsigned int c = 1;
   1.471 -  char const *z = zIn;
   1.472 -  int n = 0;
   1.473 -  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
   1.474 -    /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
   1.475 -    ** and in other parts of this file means that at one branch will
   1.476 -    ** not be covered by coverage testing on any single host. But coverage
   1.477 -    ** will be complete if the tests are run on both a little-endian and 
   1.478 -    ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
   1.479 -    ** macros are constant at compile time the compiler can determine
   1.480 -    ** which branch will be followed. It is therefore assumed that no runtime
   1.481 -    ** penalty is paid for this "if" statement.
   1.482 -    */
   1.483 -    while( c && ((nChar<0) || n<nChar) ){
   1.484 -      READ_UTF16BE(z, c);
   1.485 -      n++;
   1.486 -    }
   1.487 -  }else{
   1.488 -    while( c && ((nChar<0) || n<nChar) ){
   1.489 -      READ_UTF16LE(z, c);
   1.490 -      n++;
   1.491 -    }
   1.492 -  }
   1.493 -  return (z-(char const *)zIn)-((c==0)?2:0);
   1.494 -}
   1.495 -
   1.496 -#if defined(SQLITE_TEST)
   1.497 -/*
   1.498 -** This routine is called from the TCL test function "translate_selftest".
   1.499 -** It checks that the primitives for serializing and deserializing
   1.500 -** characters in each encoding are inverses of each other.
   1.501 -*/
   1.502 -void sqlite3UtfSelfTest(){
   1.503 -  unsigned int i, t;
   1.504 -  unsigned char zBuf[20];
   1.505 -  unsigned char *z;
   1.506 -  unsigned char *zTerm;
   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 -    zTerm = z;
   1.516 -    z = zBuf;
   1.517 -    c = sqlite3Utf8Read(z, zTerm, (const u8**)&z);
   1.518 -    t = i;
   1.519 -    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
   1.520 -    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
   1.521 -    assert( c==t );
   1.522 -    assert( (z-zBuf)==n );
   1.523 -  }
   1.524 -  for(i=0; i<0x00110000; i++){
   1.525 -    if( i>=0xD800 && i<0xE000 ) continue;
   1.526 -    z = zBuf;
   1.527 -    WRITE_UTF16LE(z, i);
   1.528 -    n = z-zBuf;
   1.529 -    z[0] = 0;
   1.530 -    z = zBuf;
   1.531 -    READ_UTF16LE(z, c);
   1.532 -    assert( c==i );
   1.533 -    assert( (z-zBuf)==n );
   1.534 -  }
   1.535 -  for(i=0; i<0x00110000; i++){
   1.536 -    if( i>=0xD800 && i<0xE000 ) continue;
   1.537 -    z = zBuf;
   1.538 -    WRITE_UTF16BE(z, i);
   1.539 -    n = z-zBuf;
   1.540 -    z[0] = 0;
   1.541 -    z = zBuf;
   1.542 -    READ_UTF16BE(z, c);
   1.543 -    assert( c==i );
   1.544 -    assert( (z-zBuf)==n );
   1.545 -  }
   1.546 -}
   1.547 -#endif /* SQLITE_TEST */
   1.548 -#endif /* SQLITE_OMIT_UTF16 */