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