extensions/sqlite/sqlite-source/utf.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2004 April 13
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains routines used to translate between UTF-8, 
ds@1952
    13
** UTF-16, UTF-16BE, and UTF-16LE.
ds@1952
    14
**
ds@1952
    15
** $Id$
ds@1952
    16
**
ds@1952
    17
** Notes on UTF-8:
ds@1952
    18
**
ds@1952
    19
**   Byte-0    Byte-1    Byte-2    Byte-3    Value
ds@1952
    20
**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
ds@1952
    21
**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
ds@1952
    22
**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
ds@1952
    23
**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
ds@1952
    24
**
ds@1952
    25
**
ds@1952
    26
** Notes on UTF-16:  (with wwww+1==uuuuu)
ds@1952
    27
**
ds@1952
    28
**      Word-0               Word-1          Value
ds@1952
    29
**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
ds@1952
    30
**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
ds@1952
    31
**
ds@1952
    32
**
ds@1952
    33
** BOM or Byte Order Mark:
ds@1952
    34
**     0xff 0xfe   little-endian utf-16 follows
ds@1952
    35
**     0xfe 0xff   big-endian utf-16 follows
ds@1952
    36
**
ds@1952
    37
*/
ds@1952
    38
#include "sqliteInt.h"
ds@1952
    39
#include <assert.h>
ds@1952
    40
#include "vdbeInt.h"
ds@1952
    41
ds@1952
    42
/*
ds@1952
    43
** The following constant value is used by the SQLITE_BIGENDIAN and
ds@1952
    44
** SQLITE_LITTLEENDIAN macros.
ds@1952
    45
*/
ds@1952
    46
const int sqlite3one = 1;
ds@1952
    47
ds@1952
    48
/*
ds@1952
    49
** This lookup table is used to help decode the first byte of
ds@1952
    50
** a multi-byte UTF8 character.
ds@1952
    51
*/
ds@1952
    52
static const unsigned char sqlite3UtfTrans1[] = {
ds@1952
    53
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ds@1952
    54
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
ds@1952
    55
  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
ds@1952
    56
  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
ds@1952
    57
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ds@1952
    58
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
ds@1952
    59
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ds@1952
    60
  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
ds@1952
    61
};
ds@1952
    62
ds@1952
    63
ds@1952
    64
#define WRITE_UTF8(zOut, c) {                          \
ds@1952
    65
  if( c<0x00080 ){                                     \
ds@1952
    66
    *zOut++ = (c&0xFF);                                \
ds@1952
    67
  }                                                    \
ds@1952
    68
  else if( c<0x00800 ){                                \
ds@1952
    69
    *zOut++ = 0xC0 + ((c>>6)&0x1F);                    \
ds@1952
    70
    *zOut++ = 0x80 + (c & 0x3F);                       \
ds@1952
    71
  }                                                    \
ds@1952
    72
  else if( c<0x10000 ){                                \
ds@1952
    73
    *zOut++ = 0xE0 + ((c>>12)&0x0F);                   \
ds@1952
    74
    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
ds@1952
    75
    *zOut++ = 0x80 + (c & 0x3F);                       \
ds@1952
    76
  }else{                                               \
ds@1952
    77
    *zOut++ = 0xF0 + ((c>>18) & 0x07);                 \
ds@1952
    78
    *zOut++ = 0x80 + ((c>>12) & 0x3F);                 \
ds@1952
    79
    *zOut++ = 0x80 + ((c>>6) & 0x3F);                  \
ds@1952
    80
    *zOut++ = 0x80 + (c & 0x3F);                       \
ds@1952
    81
  }                                                    \
ds@1952
    82
}
ds@1952
    83
ds@1952
    84
#define WRITE_UTF16LE(zOut, c) {                                \
ds@1952
    85
  if( c<=0xFFFF ){                                              \
ds@1952
    86
    *zOut++ = (c&0x00FF);                                       \
ds@1952
    87
    *zOut++ = ((c>>8)&0x00FF);                                  \
ds@1952
    88
  }else{                                                        \
ds@1952
    89
    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
ds@1952
    90
    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
ds@1952
    91
    *zOut++ = (c&0x00FF);                                       \
ds@1952
    92
    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
ds@1952
    93
  }                                                             \
ds@1952
    94
}
ds@1952
    95
ds@1952
    96
#define WRITE_UTF16BE(zOut, c) {                                \
ds@1952
    97
  if( c<=0xFFFF ){                                              \
ds@1952
    98
    *zOut++ = ((c>>8)&0x00FF);                                  \
ds@1952
    99
    *zOut++ = (c&0x00FF);                                       \
ds@1952
   100
  }else{                                                        \
ds@1952
   101
    *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03));              \
ds@1952
   102
    *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
ds@1952
   103
    *zOut++ = (0x00DC + ((c>>8)&0x03));                         \
ds@1952
   104
    *zOut++ = (c&0x00FF);                                       \
ds@1952
   105
  }                                                             \
ds@1952
   106
}
ds@1952
   107
ds@1952
   108
#define READ_UTF16LE(zIn, c){                                         \
ds@1952
   109
  c = (*zIn++);                                                       \
ds@1952
   110
  c += ((*zIn++)<<8);                                                 \
ds@1952
   111
  if( c>=0xD800 && c<0xE000 ){                                       \
ds@1952
   112
    int c2 = (*zIn++);                                                \
ds@1952
   113
    c2 += ((*zIn++)<<8);                                              \
ds@1952
   114
    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
ds@1952
   115
    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
ds@1952
   116
  }                                                                   \
ds@1952
   117
}
ds@1952
   118
ds@1952
   119
#define READ_UTF16BE(zIn, c){                                         \
ds@1952
   120
  c = ((*zIn++)<<8);                                                  \
ds@1952
   121
  c += (*zIn++);                                                      \
ds@1952
   122
  if( c>=0xD800 && c<0xE000 ){                                       \
ds@1952
   123
    int c2 = ((*zIn++)<<8);                                           \
ds@1952
   124
    c2 += (*zIn++);                                                   \
ds@1952
   125
    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
ds@1952
   126
    if( (c & 0xFFFF0000)==0 ) c = 0xFFFD;                             \
ds@1952
   127
  }                                                                   \
ds@1952
   128
}
ds@1952
   129
ds@1952
   130
/*
ds@1952
   131
** Translate a single UTF-8 character.  Return the unicode value.
ds@1952
   132
**
ds@1952
   133
** During translation, assume that the byte that zTerm points
ds@1952
   134
** is a 0x00.
ds@1952
   135
**
ds@1952
   136
** Write a pointer to the next unread byte back into *pzNext.
ds@1952
   137
**
ds@1952
   138
** Notes On Invalid UTF-8:
ds@1952
   139
**
ds@1952
   140
**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
ds@1952
   141
**     be encoded as a multi-byte character.  Any multi-byte character that
ds@1952
   142
**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
ds@1952
   143
**
ds@1952
   144
**  *  This routine never allows a UTF16 surrogate value to be encoded.
ds@1952
   145
**     If a multi-byte character attempts to encode a value between
ds@1952
   146
**     0xd800 and 0xe000 then it is rendered as 0xfffd.
ds@1952
   147
**
ds@1952
   148
**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
ds@1952
   149
**     byte of a character are interpreted as single-byte characters
ds@1952
   150
**     and rendered as themselves even though they are technically
ds@1952
   151
**     invalid characters.
ds@1952
   152
**
ds@1952
   153
**  *  This routine accepts an infinite number of different UTF8 encodings
ds@1952
   154
**     for unicode values 0x80 and greater.  It do not change over-length
ds@1952
   155
**     encodings to 0xfffd as some systems recommend.
ds@1952
   156
*/
ds@1952
   157
int sqlite3Utf8Read(
ds@1952
   158
  const unsigned char *z,         /* First byte of UTF-8 character */
ds@1952
   159
  const unsigned char *zTerm,     /* Pretend this byte is 0x00 */
ds@1952
   160
  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
ds@1952
   161
){
ds@1952
   162
  int c = *(z++);
ds@1952
   163
  if( c>=0xc0 ){
ds@1952
   164
    c = sqlite3UtfTrans1[c-0xc0];
ds@1952
   165
    while( z!=zTerm && (*z & 0xc0)==0x80 ){
ds@1952
   166
      c = (c<<6) + (0x3f & *(z++));
ds@1952
   167
    }
ds@1952
   168
    if( c<0x80
ds@1952
   169
        || (c&0xFFFFF800)==0xD800
ds@1952
   170
        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
ds@1952
   171
  }
ds@1952
   172
  *pzNext = z;
ds@1952
   173
  return c;
ds@1952
   174
}
ds@1952
   175
ds@1952
   176
ds@1952
   177
ds@1952
   178
/*
ds@1952
   179
** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
ds@1952
   180
** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
ds@1952
   181
*/ 
ds@1952
   182
/* #define TRANSLATE_TRACE 1 */
ds@1952
   183
ds@1952
   184
#ifndef SQLITE_OMIT_UTF16
ds@1952
   185
/*
ds@1952
   186
** This routine transforms the internal text encoding used by pMem to
ds@1952
   187
** desiredEnc. It is an error if the string is already of the desired
ds@1952
   188
** encoding, or if *pMem does not contain a string value.
ds@1952
   189
*/
ds@1952
   190
int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
ds@1952
   191
  unsigned char zShort[NBFS]; /* Temporary short output buffer */
ds@1952
   192
  int len;                    /* Maximum length of output string in bytes */
ds@1952
   193
  unsigned char *zOut;                  /* Output buffer */
ds@1952
   194
  unsigned char *zIn;                   /* Input iterator */
ds@1952
   195
  unsigned char *zTerm;                 /* End of input */
ds@1952
   196
  unsigned char *z;                     /* Output iterator */
ds@1952
   197
  unsigned int c;
ds@1952
   198
ds@1952
   199
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
ds@1952
   200
  assert( pMem->flags&MEM_Str );
ds@1952
   201
  assert( pMem->enc!=desiredEnc );
ds@1952
   202
  assert( pMem->enc!=0 );
ds@1952
   203
  assert( pMem->n>=0 );
ds@1952
   204
ds@1952
   205
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
ds@1952
   206
  {
ds@1952
   207
    char zBuf[100];
ds@1952
   208
    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
ds@1952
   209
    fprintf(stderr, "INPUT:  %s\n", zBuf);
ds@1952
   210
  }
ds@1952
   211
#endif
ds@1952
   212
ds@1952
   213
  /* If the translation is between UTF-16 little and big endian, then 
ds@1952
   214
  ** all that is required is to swap the byte order. This case is handled
ds@1952
   215
  ** differently from the others.
ds@1952
   216
  */
ds@1952
   217
  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
ds@1952
   218
    u8 temp;
ds@1952
   219
    int rc;
ds@1952
   220
    rc = sqlite3VdbeMemMakeWriteable(pMem);
ds@1952
   221
    if( rc!=SQLITE_OK ){
ds@1952
   222
      assert( rc==SQLITE_NOMEM );
ds@1952
   223
      return SQLITE_NOMEM;
ds@1952
   224
    }
ds@1952
   225
    zIn = (u8*)pMem->z;
ds@1952
   226
    zTerm = &zIn[pMem->n];
ds@1952
   227
    while( zIn<zTerm ){
ds@1952
   228
      temp = *zIn;
ds@1952
   229
      *zIn = *(zIn+1);
ds@1952
   230
      zIn++;
ds@1952
   231
      *zIn++ = temp;
ds@1952
   232
    }
ds@1952
   233
    pMem->enc = desiredEnc;
ds@1952
   234
    goto translate_out;
ds@1952
   235
  }
ds@1952
   236
ds@1952
   237
  /* Set len to the maximum number of bytes required in the output buffer. */
ds@1952
   238
  if( desiredEnc==SQLITE_UTF8 ){
ds@1952
   239
    /* When converting from UTF-16, the maximum growth results from
ds@1952
   240
    ** translating a 2-byte character to a 4-byte UTF-8 character.
ds@1952
   241
    ** A single byte is required for the output string
ds@1952
   242
    ** nul-terminator.
ds@1952
   243
    */
ds@1952
   244
    len = pMem->n * 2 + 1;
ds@1952
   245
  }else{
ds@1952
   246
    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
ds@1952
   247
    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
ds@1952
   248
    ** character. Two bytes are required in the output buffer for the
ds@1952
   249
    ** nul-terminator.
ds@1952
   250
    */
ds@1952
   251
    len = pMem->n * 2 + 2;
ds@1952
   252
  }
ds@1952
   253
ds@1952
   254
  /* Set zIn to point at the start of the input buffer and zTerm to point 1
ds@1952
   255
  ** byte past the end.
ds@1952
   256
  **
ds@1952
   257
  ** Variable zOut is set to point at the output buffer. This may be space
ds@1952
   258
  ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and
ds@1952
   259
  ** not in use, or the zShort array on the stack (see above).
ds@1952
   260
  */
ds@1952
   261
  zIn = (u8*)pMem->z;
ds@1952
   262
  zTerm = &zIn[pMem->n];
ds@1952
   263
  if( len>NBFS ){
ds@1952
   264
    zOut = sqlite3DbMallocRaw(pMem->db, len);
ds@1952
   265
    if( !zOut ){
ds@1952
   266
      return SQLITE_NOMEM;
ds@1952
   267
    }
ds@1952
   268
  }else{
ds@1952
   269
    zOut = zShort;
ds@1952
   270
  }
ds@1952
   271
  z = zOut;
ds@1952
   272
ds@1952
   273
  if( pMem->enc==SQLITE_UTF8 ){
ds@1952
   274
    if( desiredEnc==SQLITE_UTF16LE ){
ds@1952
   275
      /* UTF-8 -> UTF-16 Little-endian */
ds@1952
   276
      while( zIn<zTerm ){
ds@1952
   277
        c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
ds@1952
   278
        WRITE_UTF16LE(z, c);
ds@1952
   279
      }
ds@1952
   280
    }else{
ds@1952
   281
      assert( desiredEnc==SQLITE_UTF16BE );
ds@1952
   282
      /* UTF-8 -> UTF-16 Big-endian */
ds@1952
   283
      while( zIn<zTerm ){
ds@1952
   284
        c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
ds@1952
   285
        WRITE_UTF16BE(z, c);
ds@1952
   286
      }
ds@1952
   287
    }
ds@1952
   288
    pMem->n = z - zOut;
ds@1952
   289
    *z++ = 0;
ds@1952
   290
  }else{
ds@1952
   291
    assert( desiredEnc==SQLITE_UTF8 );
ds@1952
   292
    if( pMem->enc==SQLITE_UTF16LE ){
ds@1952
   293
      /* UTF-16 Little-endian -> UTF-8 */
ds@1952
   294
      while( zIn<zTerm ){
ds@1952
   295
        READ_UTF16LE(zIn, c); 
ds@1952
   296
        WRITE_UTF8(z, c);
ds@1952
   297
      }
ds@1952
   298
    }else{
ds@1952
   299
      /* UTF-16 Little-endian -> UTF-8 */
ds@1952
   300
      while( zIn<zTerm ){
ds@1952
   301
        READ_UTF16BE(zIn, c); 
ds@1952
   302
        WRITE_UTF8(z, c);
ds@1952
   303
      }
ds@1952
   304
    }
ds@1952
   305
    pMem->n = z - zOut;
ds@1952
   306
  }
ds@1952
   307
  *z = 0;
ds@1952
   308
  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
ds@1952
   309
ds@1952
   310
  sqlite3VdbeMemRelease(pMem);
ds@1952
   311
  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
ds@1952
   312
  pMem->enc = desiredEnc;
ds@1952
   313
  if( zOut==zShort ){
ds@1952
   314
    memcpy(pMem->zShort, zOut, len);
ds@1952
   315
    zOut = (u8*)pMem->zShort;
ds@1952
   316
    pMem->flags |= (MEM_Term|MEM_Short);
ds@1952
   317
  }else{
ds@1952
   318
    pMem->flags |= (MEM_Term|MEM_Dyn);
ds@1952
   319
  }
ds@1952
   320
  pMem->z = (char*)zOut;
ds@1952
   321
ds@1952
   322
translate_out:
ds@1952
   323
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
ds@1952
   324
  {
ds@1952
   325
    char zBuf[100];
ds@1952
   326
    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
ds@1952
   327
    fprintf(stderr, "OUTPUT: %s\n", zBuf);
ds@1952
   328
  }
ds@1952
   329
#endif
ds@1952
   330
  return SQLITE_OK;
ds@1952
   331
}
ds@1952
   332
ds@1952
   333
/*
ds@1952
   334
** This routine checks for a byte-order mark at the beginning of the 
ds@1952
   335
** UTF-16 string stored in *pMem. If one is present, it is removed and
ds@1952
   336
** the encoding of the Mem adjusted. This routine does not do any
ds@1952
   337
** byte-swapping, it just sets Mem.enc appropriately.
ds@1952
   338
**
ds@1952
   339
** The allocation (static, dynamic etc.) and encoding of the Mem may be
ds@1952
   340
** changed by this function.
ds@1952
   341
*/
ds@1952
   342
int sqlite3VdbeMemHandleBom(Mem *pMem){
ds@1952
   343
  int rc = SQLITE_OK;
ds@1952
   344
  u8 bom = 0;
ds@1952
   345
ds@1952
   346
  if( pMem->n<0 || pMem->n>1 ){
ds@1952
   347
    u8 b1 = *(u8 *)pMem->z;
ds@1952
   348
    u8 b2 = *(((u8 *)pMem->z) + 1);
ds@1952
   349
    if( b1==0xFE && b2==0xFF ){
ds@1952
   350
      bom = SQLITE_UTF16BE;
ds@1952
   351
    }
ds@1952
   352
    if( b1==0xFF && b2==0xFE ){
ds@1952
   353
      bom = SQLITE_UTF16LE;
ds@1952
   354
    }
ds@1952
   355
  }
ds@1952
   356
  
ds@1952
   357
  if( bom ){
ds@1952
   358
    /* This function is called as soon as a string is stored in a Mem*,
ds@1952
   359
    ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
ds@1952
   360
    ** for the string to be stored in Mem.zShort, or for it to be stored
ds@1952
   361
    ** in dynamic memory with no destructor.
ds@1952
   362
    */
ds@1952
   363
    assert( !(pMem->flags&MEM_Short) );
ds@1952
   364
    assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
ds@1952
   365
    if( pMem->flags & MEM_Dyn ){
ds@1952
   366
      void (*xDel)(void*) = pMem->xDel;
ds@1952
   367
      char *z = pMem->z;
ds@1952
   368
      pMem->z = 0;
ds@1952
   369
      pMem->xDel = 0;
ds@1952
   370
      rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, 
ds@1952
   371
          SQLITE_TRANSIENT);
ds@1952
   372
      xDel(z);
ds@1952
   373
    }else{
ds@1952
   374
      rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, 
ds@1952
   375
          SQLITE_TRANSIENT);
ds@1952
   376
    }
ds@1952
   377
  }
ds@1952
   378
  return rc;
ds@1952
   379
}
ds@1952
   380
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
   381
ds@1952
   382
/*
ds@1952
   383
** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
ds@1952
   384
** return the number of unicode characters in pZ up to (but not including)
ds@1952
   385
** the first 0x00 byte. If nByte is not less than zero, return the
ds@1952
   386
** number of unicode characters in the first nByte of pZ (or up to 
ds@1952
   387
** the first 0x00, whichever comes first).
ds@1952
   388
*/
ds@1952
   389
int sqlite3Utf8CharLen(const char *zIn, int nByte){
ds@1952
   390
  int r = 0;
ds@1952
   391
  const u8 *z = (const u8*)zIn;
ds@1952
   392
  const u8 *zTerm;
ds@1952
   393
  if( nByte>=0 ){
ds@1952
   394
    zTerm = &z[nByte];
ds@1952
   395
  }else{
ds@1952
   396
    zTerm = (const u8*)(-1);
ds@1952
   397
  }
ds@1952
   398
  assert( z<=zTerm );
ds@1952
   399
  while( *z!=0 && z<zTerm ){
ds@1952
   400
    SQLITE_SKIP_UTF8(z);
ds@1952
   401
    r++;
ds@1952
   402
  }
ds@1952
   403
  return r;
ds@1952
   404
}
ds@1952
   405
ds@1952
   406
/* This test function is not currently used by the automated test-suite. 
ds@1952
   407
** Hence it is only available in debug builds.
ds@1952
   408
*/
ds@1952
   409
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
ds@1952
   410
/*
ds@1952
   411
** Translate UTF-8 to UTF-8.
ds@1952
   412
**
ds@1952
   413
** This has the effect of making sure that the string is well-formed
ds@1952
   414
** UTF-8.  Miscoded characters are removed.
ds@1952
   415
**
ds@1952
   416
** The translation is done in-place (since it is impossible for the
ds@1952
   417
** correct UTF-8 encoding to be longer than a malformed encoding).
ds@1952
   418
*/
ds@1952
   419
int sqlite3Utf8To8(unsigned char *zIn){
ds@1952
   420
  unsigned char *zOut = zIn;
ds@1952
   421
  unsigned char *zStart = zIn;
ds@1952
   422
  unsigned char *zTerm;
ds@1952
   423
  u32 c;
ds@1952
   424
ds@1952
   425
  while( zIn[0] ){
ds@1952
   426
    c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
ds@1952
   427
    if( c!=0xfffd ){
ds@1952
   428
      WRITE_UTF8(zOut, c);
ds@1952
   429
    }
ds@1952
   430
  }
ds@1952
   431
  *zOut = 0;
ds@1952
   432
  return zOut - zStart;
ds@1952
   433
}
ds@1952
   434
#endif
ds@1952
   435
ds@1952
   436
#ifndef SQLITE_OMIT_UTF16
ds@1952
   437
/*
ds@1952
   438
** Convert a UTF-16 string in the native encoding into a UTF-8 string.
ds@1952
   439
** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
ds@1952
   440
** be freed by the calling function.
ds@1952
   441
**
ds@1952
   442
** NULL is returned if there is an allocation error.
ds@1952
   443
*/
ds@1952
   444
char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
ds@1952
   445
  Mem m;
ds@1952
   446
  memset(&m, 0, sizeof(m));
ds@1952
   447
  m.db = db;
ds@1952
   448
  sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
ds@1952
   449
  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
ds@1952
   450
  if( db->mallocFailed ){
ds@1952
   451
    sqlite3VdbeMemRelease(&m);
ds@1952
   452
    m.z = 0;
ds@1952
   453
  }
ds@1952
   454
  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
ds@1952
   455
  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
ds@1952
   456
  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
ds@1952
   457
}
ds@1952
   458
ds@1952
   459
/*
ds@1952
   460
** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
ds@1952
   461
** return the number of bytes up to (but not including), the first pair
ds@1952
   462
** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
ds@1952
   463
** then return the number of bytes in the first nChar unicode characters
ds@1952
   464
** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
ds@1952
   465
*/
ds@1952
   466
int sqlite3Utf16ByteLen(const void *zIn, int nChar){
ds@1952
   467
  unsigned int c = 1;
ds@1952
   468
  char const *z = zIn;
ds@1952
   469
  int n = 0;
ds@1952
   470
  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
ds@1952
   471
    /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
ds@1952
   472
    ** and in other parts of this file means that at one branch will
ds@1952
   473
    ** not be covered by coverage testing on any single host. But coverage
ds@1952
   474
    ** will be complete if the tests are run on both a little-endian and 
ds@1952
   475
    ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
ds@1952
   476
    ** macros are constant at compile time the compiler can determine
ds@1952
   477
    ** which branch will be followed. It is therefore assumed that no runtime
ds@1952
   478
    ** penalty is paid for this "if" statement.
ds@1952
   479
    */
ds@1952
   480
    while( c && ((nChar<0) || n<nChar) ){
ds@1952
   481
      READ_UTF16BE(z, c);
ds@1952
   482
      n++;
ds@1952
   483
    }
ds@1952
   484
  }else{
ds@1952
   485
    while( c && ((nChar<0) || n<nChar) ){
ds@1952
   486
      READ_UTF16LE(z, c);
ds@1952
   487
      n++;
ds@1952
   488
    }
ds@1952
   489
  }
ds@1952
   490
  return (z-(char const *)zIn)-((c==0)?2:0);
ds@1952
   491
}
ds@1952
   492
ds@1952
   493
#if defined(SQLITE_TEST)
ds@1952
   494
/*
ds@1952
   495
** This routine is called from the TCL test function "translate_selftest".
ds@1952
   496
** It checks that the primitives for serializing and deserializing
ds@1952
   497
** characters in each encoding are inverses of each other.
ds@1952
   498
*/
ds@1952
   499
void sqlite3UtfSelfTest(){
ds@1952
   500
  unsigned int i, t;
ds@1952
   501
  unsigned char zBuf[20];
ds@1952
   502
  unsigned char *z;
ds@1952
   503
  unsigned char *zTerm;
ds@1952
   504
  int n;
ds@1952
   505
  unsigned int c;
ds@1952
   506
ds@1952
   507
  for(i=0; i<0x00110000; i++){
ds@1952
   508
    z = zBuf;
ds@1952
   509
    WRITE_UTF8(z, i);
ds@1952
   510
    n = z-zBuf;
ds@1952
   511
    z[0] = 0;
ds@1952
   512
    zTerm = z;
ds@1952
   513
    z = zBuf;
ds@1952
   514
    c = sqlite3Utf8Read(z, zTerm, (const u8**)&z);
ds@1952
   515
    t = i;
ds@1952
   516
    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
ds@1952
   517
    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
ds@1952
   518
    assert( c==t );
ds@1952
   519
    assert( (z-zBuf)==n );
ds@1952
   520
  }
ds@1952
   521
  for(i=0; i<0x00110000; i++){
ds@1952
   522
    if( i>=0xD800 && i<0xE000 ) continue;
ds@1952
   523
    z = zBuf;
ds@1952
   524
    WRITE_UTF16LE(z, i);
ds@1952
   525
    n = z-zBuf;
ds@1952
   526
    z[0] = 0;
ds@1952
   527
    z = zBuf;
ds@1952
   528
    READ_UTF16LE(z, c);
ds@1952
   529
    assert( c==i );
ds@1952
   530
    assert( (z-zBuf)==n );
ds@1952
   531
  }
ds@1952
   532
  for(i=0; i<0x00110000; i++){
ds@1952
   533
    if( i>=0xD800 && i<0xE000 ) continue;
ds@1952
   534
    z = zBuf;
ds@1952
   535
    WRITE_UTF16BE(z, i);
ds@1952
   536
    n = z-zBuf;
ds@1952
   537
    z[0] = 0;
ds@1952
   538
    z = zBuf;
ds@1952
   539
    READ_UTF16BE(z, c);
ds@1952
   540
    assert( c==i );
ds@1952
   541
    assert( (z-zBuf)==n );
ds@1952
   542
  }
ds@1952
   543
}
ds@1952
   544
#endif /* SQLITE_TEST */
ds@1952
   545
#endif /* SQLITE_OMIT_UTF16 */