drh | a5d14fe | 2004-05-04 15:00:46 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 April 13 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains routines used to translate between UTF-8, |
| 13 | ** UTF-16, UTF-16BE, and UTF-16LE. |
| 14 | ** |
drh | a5d14fe | 2004-05-04 15:00:46 | [diff] [blame] | 15 | ** Notes on UTF-8: |
| 16 | ** |
| 17 | ** Byte-0 Byte-1 Byte-2 Byte-3 Value |
| 18 | ** 0xxxxxxx 00000000 00000000 0xxxxxxx |
| 19 | ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx |
| 20 | ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx |
| 21 | ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx |
| 22 | ** |
| 23 | ** |
| 24 | ** Notes on UTF-16: (with wwww+1==uuuuu) |
| 25 | ** |
drh | 51846b5 | 2004-05-28 16:00:21 | [diff] [blame] | 26 | ** Word-0 Word-1 Value |
| 27 | ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx |
| 28 | ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx |
drh | a5d14fe | 2004-05-04 15:00:46 | [diff] [blame] | 29 | ** |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 30 | ** |
drh | a5d14fe | 2004-05-04 15:00:46 | [diff] [blame] | 31 | ** BOM or Byte Order Mark: |
| 32 | ** 0xff 0xfe little-endian utf-16 follows |
| 33 | ** 0xfe 0xff big-endian utf-16 follows |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 34 | ** |
drh | a5d14fe | 2004-05-04 15:00:46 | [diff] [blame] | 35 | */ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 36 | #include "sqliteInt.h" |
drh | b659e9b | 2005-01-28 01:29:08 | [diff] [blame] | 37 | #include <assert.h> |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 38 | #include "vdbeInt.h" |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 39 | |
drh | e1462a7 | 2015-12-24 14:53:27 | [diff] [blame] | 40 | #if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0 |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 41 | /* |
drh | 38def05 | 2007-03-31 15:27:59 | [diff] [blame] | 42 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 43 | ** SQLITE_LITTLEENDIAN macros. |
| 44 | */ |
| 45 | const int sqlite3one = 1; |
drh | e1462a7 | 2015-12-24 14:53:27 | [diff] [blame] | 46 | #endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */ |
drh | 38def05 | 2007-03-31 15:27:59 | [diff] [blame] | 47 | |
| 48 | /* |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 49 | ** This lookup table is used to help decode the first byte of |
| 50 | ** a multi-byte UTF8 character. |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 | [diff] [blame] | 51 | */ |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 52 | static const unsigned char sqlite3Utf8Trans1[] = { |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 53 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 54 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 55 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 56 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 57 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 58 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 59 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 60 | 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 61 | }; |
| 62 | |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 63 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 64 | #define WRITE_UTF8(zOut, c) { \ |
| 65 | if( c<0x00080 ){ \ |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 66 | *zOut++ = (u8)(c&0xFF); \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 67 | } \ |
| 68 | else if( c<0x00800 ){ \ |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 69 | *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \ |
| 70 | *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 71 | } \ |
| 72 | else if( c<0x10000 ){ \ |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 73 | *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \ |
| 74 | *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ |
| 75 | *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 76 | }else{ \ |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 77 | *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \ |
| 78 | *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \ |
| 79 | *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ |
| 80 | *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 81 | } \ |
| 82 | } |
| 83 | |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 84 | #define WRITE_UTF16LE(zOut, c) { \ |
| 85 | if( c<=0xFFFF ){ \ |
| 86 | *zOut++ = (u8)(c&0x00FF); \ |
| 87 | *zOut++ = (u8)((c>>8)&0x00FF); \ |
| 88 | }else{ \ |
| 89 | *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ |
| 90 | *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ |
| 91 | *zOut++ = (u8)(c&0x00FF); \ |
| 92 | *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ |
| 93 | } \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 94 | } |
| 95 | |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 96 | #define WRITE_UTF16BE(zOut, c) { \ |
| 97 | if( c<=0xFFFF ){ \ |
| 98 | *zOut++ = (u8)((c>>8)&0x00FF); \ |
| 99 | *zOut++ = (u8)(c&0x00FF); \ |
| 100 | }else{ \ |
| 101 | *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ |
| 102 | *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ |
| 103 | *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ |
| 104 | *zOut++ = (u8)(c&0x00FF); \ |
| 105 | } \ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 106 | } |
| 107 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 108 | /* |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 109 | ** Write a single UTF8 character whose value is v into the |
| 110 | ** buffer starting at zOut. zOut must be sized to hold at |
drh | a58208a | 2025-05-19 12:34:11 | [diff] [blame] | 111 | ** least four bytes. Return the number of bytes needed |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 112 | ** to encode the new character. |
| 113 | */ |
| 114 | int sqlite3AppendOneUtf8Character(char *zOut, u32 v){ |
| 115 | if( v<0x00080 ){ |
| 116 | zOut[0] = (u8)(v & 0xff); |
| 117 | return 1; |
| 118 | } |
| 119 | if( v<0x00800 ){ |
| 120 | zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f); |
| 121 | zOut[1] = 0x80 + (u8)(v & 0x3f); |
| 122 | return 2; |
| 123 | } |
| 124 | if( v<0x10000 ){ |
| 125 | zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f); |
| 126 | zOut[1] = 0x80 + (u8)((v>>6) & 0x3f); |
| 127 | zOut[2] = 0x80 + (u8)(v & 0x3f); |
| 128 | return 3; |
| 129 | } |
| 130 | zOut[0] = 0xf0 + (u8)((v>>18) & 0x07); |
| 131 | zOut[1] = 0x80 + (u8)((v>>12) & 0x3f); |
| 132 | zOut[2] = 0x80 + (u8)((v>>6) & 0x3f); |
| 133 | zOut[3] = 0x80 + (u8)(v & 0x3f); |
| 134 | return 4; |
| 135 | } |
| 136 | |
| 137 | /* |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 138 | ** Translate a single UTF-8 character. Return the unicode value. |
| 139 | ** |
| 140 | ** During translation, assume that the byte that zTerm points |
| 141 | ** is a 0x00. |
| 142 | ** |
| 143 | ** Write a pointer to the next unread byte back into *pzNext. |
| 144 | ** |
| 145 | ** Notes On Invalid UTF-8: |
| 146 | ** |
| 147 | ** * This routine never allows a 7-bit character (0x00 through 0x7f) to |
| 148 | ** be encoded as a multi-byte character. Any multi-byte character that |
| 149 | ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd. |
| 150 | ** |
| 151 | ** * This routine never allows a UTF16 surrogate value to be encoded. |
| 152 | ** If a multi-byte character attempts to encode a value between |
| 153 | ** 0xd800 and 0xe000 then it is rendered as 0xfffd. |
| 154 | ** |
| 155 | ** * Bytes in the range of 0x80 through 0xbf which occur as the first |
| 156 | ** byte of a character are interpreted as single-byte characters |
| 157 | ** and rendered as themselves even though they are technically |
| 158 | ** invalid characters. |
| 159 | ** |
drh | 6c34e58 | 2014-06-18 15:24:40 | [diff] [blame] | 160 | ** * This routine accepts over-length UTF8 encodings |
| 161 | ** for unicode values 0x80 and greater. It does not change over-length |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 162 | ** encodings to 0xfffd as some systems recommend. |
| 163 | */ |
danielk1977 | ad76a81e | 2008-07-29 11:25:14 | [diff] [blame] | 164 | #define READ_UTF8(zIn, zTerm, c) \ |
| 165 | c = *(zIn++); \ |
| 166 | if( c>=0xc0 ){ \ |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 167 | c = sqlite3Utf8Trans1[c-0xc0]; \ |
drh | 4924847 | 2024-10-14 18:43:04 | [diff] [blame] | 168 | while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \ |
danielk1977 | ad76a81e | 2008-07-29 11:25:14 | [diff] [blame] | 169 | c = (c<<6) + (0x3f & *(zIn++)); \ |
| 170 | } \ |
| 171 | if( c<0x80 \ |
| 172 | || (c&0xFFFFF800)==0xD800 \ |
| 173 | || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ |
| 174 | } |
drh | 0a32fa6 | 2011-06-13 12:19:21 | [diff] [blame] | 175 | u32 sqlite3Utf8Read( |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 176 | const unsigned char **pz /* Pointer to string from which to read char */ |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 177 | ){ |
shaneh | dba2cc4 | 2011-03-24 17:43:18 | [diff] [blame] | 178 | unsigned int c; |
drh | 769e97e | 2009-04-01 16:33:37 | [diff] [blame] | 179 | |
| 180 | /* Same as READ_UTF8() above but without the zTerm parameter. |
| 181 | ** For this routine, we assume the UTF8 string is always zero-terminated. |
| 182 | */ |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 183 | c = *((*pz)++); |
drh | 769e97e | 2009-04-01 16:33:37 | [diff] [blame] | 184 | if( c>=0xc0 ){ |
| 185 | c = sqlite3Utf8Trans1[c-0xc0]; |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 186 | while( (*(*pz) & 0xc0)==0x80 ){ |
| 187 | c = (c<<6) + (0x3f & *((*pz)++)); |
drh | 769e97e | 2009-04-01 16:33:37 | [diff] [blame] | 188 | } |
| 189 | if( c<0x80 |
| 190 | || (c&0xFFFFF800)==0xD800 |
| 191 | || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } |
| 192 | } |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 193 | return c; |
| 194 | } |
| 195 | |
drh | 001d1e7 | 2023-12-13 14:31:15 | [diff] [blame] | 196 | /* |
| 197 | ** Read a single UTF8 character out of buffer z[], but reading no |
| 198 | ** more than n characters from the buffer. z[] is not zero-terminated. |
| 199 | ** |
| 200 | ** Return the number of bytes used to construct the character. |
| 201 | ** |
| 202 | ** Invalid UTF8 might generate a strange result. No effort is made |
| 203 | ** to detect invalid UTF8. |
| 204 | ** |
| 205 | ** At most 4 bytes will be read out of z[]. The return value will always |
| 206 | ** be between 1 and 4. |
| 207 | */ |
| 208 | int sqlite3Utf8ReadLimited( |
| 209 | const u8 *z, |
| 210 | int n, |
| 211 | u32 *piOut |
| 212 | ){ |
| 213 | u32 c; |
| 214 | int i = 1; |
| 215 | assert( n>0 ); |
| 216 | c = z[0]; |
| 217 | if( c>=0xc0 ){ |
| 218 | c = sqlite3Utf8Trans1[c-0xc0]; |
| 219 | if( n>4 ) n = 4; |
| 220 | while( i<n && (z[i] & 0xc0)==0x80 ){ |
| 221 | c = (c<<6) + (0x3f & z[i]); |
| 222 | i++; |
| 223 | } |
| 224 | } |
| 225 | *piOut = c; |
| 226 | return i; |
| 227 | } |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 228 | |
danielk1977 | ad76a81e | 2008-07-29 11:25:14 | [diff] [blame] | 229 | |
drh | 6615095 | 2007-07-23 19:12:41 | [diff] [blame] | 230 | /* |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 231 | ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is |
| 232 | ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). |
| 233 | */ |
| 234 | /* #define TRANSLATE_TRACE 1 */ |
| 235 | |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 236 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 237 | /* |
| 238 | ** This routine transforms the internal text encoding used by pMem to |
| 239 | ** desiredEnc. It is an error if the string is already of the desired |
| 240 | ** encoding, or if *pMem does not contain a string value. |
| 241 | */ |
drh | 4274dae | 2014-08-24 02:53:23 | [diff] [blame] | 242 | SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ |
drh | d4de9f7 | 2019-04-14 00:34:20 | [diff] [blame] | 243 | sqlite3_int64 len; /* Maximum length of output string in bytes */ |
| 244 | unsigned char *zOut; /* Output buffer */ |
| 245 | unsigned char *zIn; /* Input iterator */ |
| 246 | unsigned char *zTerm; /* End of input */ |
| 247 | unsigned char *z; /* Output iterator */ |
drh | a39f4c5 | 2006-10-04 15:23:21 | [diff] [blame] | 248 | unsigned int c; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 249 | |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 250 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 251 | assert( pMem->flags&MEM_Str ); |
| 252 | assert( pMem->enc!=desiredEnc ); |
| 253 | assert( pMem->enc!=0 ); |
| 254 | assert( pMem->n>=0 ); |
| 255 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 | [diff] [blame] | 256 | #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 257 | { |
drh | 5ca0632 | 2020-01-06 19:23:41 | [diff] [blame] | 258 | StrAccum acc; |
| 259 | char zBuf[1000]; |
| 260 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 261 | sqlite3VdbeMemPrettyPrint(pMem, &acc); |
| 262 | fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc)); |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 | [diff] [blame] | 263 | } |
| 264 | #endif |
| 265 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 266 | /* If the translation is between UTF-16 little and big endian, then |
| 267 | ** all that is required is to swap the byte order. This case is handled |
| 268 | ** differently from the others. |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 269 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 270 | if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ |
| 271 | u8 temp; |
drh | 71c697e | 2004-08-08 23:39:19 | [diff] [blame] | 272 | int rc; |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 273 | rc = sqlite3VdbeMemMakeWriteable(pMem); |
drh | 71c697e | 2004-08-08 23:39:19 | [diff] [blame] | 274 | if( rc!=SQLITE_OK ){ |
| 275 | assert( rc==SQLITE_NOMEM ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 276 | return SQLITE_NOMEM_BKPT; |
drh | 71c697e | 2004-08-08 23:39:19 | [diff] [blame] | 277 | } |
drh | 2646da7 | 2005-12-09 20:02:05 | [diff] [blame] | 278 | zIn = (u8*)pMem->z; |
drh | bbf695d | 2008-11-07 03:29:33 | [diff] [blame] | 279 | zTerm = &zIn[pMem->n&~1]; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 280 | while( zIn<zTerm ){ |
| 281 | temp = *zIn; |
| 282 | *zIn = *(zIn+1); |
| 283 | zIn++; |
| 284 | *zIn++ = temp; |
| 285 | } |
| 286 | pMem->enc = desiredEnc; |
| 287 | goto translate_out; |
| 288 | } |
| 289 | |
danielk1977 | d7e6964 | 2004-06-23 00:23:49 | [diff] [blame] | 290 | /* Set len to the maximum number of bytes required in the output buffer. */ |
| 291 | if( desiredEnc==SQLITE_UTF8 ){ |
| 292 | /* When converting from UTF-16, the maximum growth results from |
drh | a49b861 | 2006-04-16 12:05:03 | [diff] [blame] | 293 | ** translating a 2-byte character to a 4-byte UTF-8 character. |
| 294 | ** A single byte is required for the output string |
danielk1977 | d7e6964 | 2004-06-23 00:23:49 | [diff] [blame] | 295 | ** nul-terminator. |
| 296 | */ |
drh | bbf695d | 2008-11-07 03:29:33 | [diff] [blame] | 297 | pMem->n &= ~1; |
drh | d4de9f7 | 2019-04-14 00:34:20 | [diff] [blame] | 298 | len = 2 * (sqlite3_int64)pMem->n + 1; |
danielk1977 | d7e6964 | 2004-06-23 00:23:49 | [diff] [blame] | 299 | }else{ |
| 300 | /* When converting from UTF-8 to UTF-16 the maximum growth is caused |
| 301 | ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 |
| 302 | ** character. Two bytes are required in the output buffer for the |
| 303 | ** nul-terminator. |
| 304 | */ |
drh | d4de9f7 | 2019-04-14 00:34:20 | [diff] [blame] | 305 | len = 2 * (sqlite3_int64)pMem->n + 2; |
danielk1977 | d7e6964 | 2004-06-23 00:23:49 | [diff] [blame] | 306 | } |
| 307 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 308 | /* Set zIn to point at the start of the input buffer and zTerm to point 1 |
| 309 | ** byte past the end. |
| 310 | ** |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 | [diff] [blame] | 311 | ** Variable zOut is set to point at the output buffer, space obtained |
| 312 | ** from sqlite3_malloc(). |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 313 | */ |
drh | 2646da7 | 2005-12-09 20:02:05 | [diff] [blame] | 314 | zIn = (u8*)pMem->z; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 315 | zTerm = &zIn[pMem->n]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 | [diff] [blame] | 316 | zOut = sqlite3DbMallocRaw(pMem->db, len); |
| 317 | if( !zOut ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 318 | return SQLITE_NOMEM_BKPT; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 319 | } |
| 320 | z = zOut; |
| 321 | |
| 322 | if( pMem->enc==SQLITE_UTF8 ){ |
| 323 | if( desiredEnc==SQLITE_UTF16LE ){ |
| 324 | /* UTF-8 -> UTF-16 Little-endian */ |
| 325 | while( zIn<zTerm ){ |
danielk1977 | ad76a81e | 2008-07-29 11:25:14 | [diff] [blame] | 326 | READ_UTF8(zIn, zTerm, c); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 327 | WRITE_UTF16LE(z, c); |
| 328 | } |
drh | b8dd315 | 2004-09-24 23:20:51 | [diff] [blame] | 329 | }else{ |
| 330 | assert( desiredEnc==SQLITE_UTF16BE ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 331 | /* UTF-8 -> UTF-16 Big-endian */ |
| 332 | while( zIn<zTerm ){ |
danielk1977 | ad76a81e | 2008-07-29 11:25:14 | [diff] [blame] | 333 | READ_UTF8(zIn, zTerm, c); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 334 | WRITE_UTF16BE(z, c); |
| 335 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 336 | } |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 337 | pMem->n = (int)(z - zOut); |
drh | b8dd315 | 2004-09-24 23:20:51 | [diff] [blame] | 338 | *z++ = 0; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 339 | }else{ |
| 340 | assert( desiredEnc==SQLITE_UTF8 ); |
| 341 | if( pMem->enc==SQLITE_UTF16LE ){ |
| 342 | /* UTF-16 Little-endian -> UTF-8 */ |
| 343 | while( zIn<zTerm ){ |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 344 | c = *(zIn++); |
| 345 | c += (*(zIn++))<<8; |
| 346 | if( c>=0xd800 && c<0xe000 ){ |
drh | 4f1315a | 2020-05-20 15:02:04 | [diff] [blame] | 347 | #ifdef SQLITE_REPLACE_INVALID_UTF |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 348 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 349 | c = 0xfffd; |
| 350 | }else{ |
| 351 | int c2 = *(zIn++); |
| 352 | c2 += (*(zIn++))<<8; |
| 353 | if( c2<0xdc00 || c2>=0xe000 ){ |
| 354 | zIn -= 2; |
| 355 | c = 0xfffd; |
| 356 | }else{ |
| 357 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 358 | } |
| 359 | } |
drh | 4f1315a | 2020-05-20 15:02:04 | [diff] [blame] | 360 | #else |
| 361 | if( zIn<zTerm ){ |
| 362 | int c2 = (*zIn++); |
| 363 | c2 += ((*zIn++)<<8); |
| 364 | c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 365 | } |
| 366 | #endif |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 367 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 368 | WRITE_UTF8(z, c); |
| 369 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 370 | }else{ |
mihailim | 7ffb2b5 | 2008-06-27 18:59:44 | [diff] [blame] | 371 | /* UTF-16 Big-endian -> UTF-8 */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 372 | while( zIn<zTerm ){ |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 373 | c = (*(zIn++))<<8; |
| 374 | c += *(zIn++); |
| 375 | if( c>=0xd800 && c<0xe000 ){ |
drh | 4f1315a | 2020-05-20 15:02:04 | [diff] [blame] | 376 | #ifdef SQLITE_REPLACE_INVALID_UTF |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 377 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 378 | c = 0xfffd; |
| 379 | }else{ |
| 380 | int c2 = (*(zIn++))<<8; |
| 381 | c2 += *(zIn++); |
| 382 | if( c2<0xdc00 || c2>=0xe000 ){ |
| 383 | zIn -= 2; |
| 384 | c = 0xfffd; |
| 385 | }else{ |
| 386 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 387 | } |
| 388 | } |
drh | 4f1315a | 2020-05-20 15:02:04 | [diff] [blame] | 389 | #else |
| 390 | if( zIn<zTerm ){ |
| 391 | int c2 = ((*zIn++)<<8); |
| 392 | c2 += (*zIn++); |
| 393 | c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 394 | } |
| 395 | #endif |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 396 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 397 | WRITE_UTF8(z, c); |
| 398 | } |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 399 | } |
drh | aa78bec | 2008-12-09 03:55:14 | [diff] [blame] | 400 | pMem->n = (int)(z - zOut); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 401 | } |
drh | b8dd315 | 2004-09-24 23:20:51 | [diff] [blame] | 402 | *z = 0; |
danielk1977 | d7e6964 | 2004-06-23 00:23:49 | [diff] [blame] | 403 | assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 404 | |
drh | 21b473d | 2020-06-04 02:50:47 | [diff] [blame] | 405 | c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 406 | sqlite3VdbeMemRelease(pMem); |
drh | 21b473d | 2020-06-04 02:50:47 | [diff] [blame] | 407 | pMem->flags = c; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 408 | pMem->enc = desiredEnc; |
drh | 2646da7 | 2005-12-09 20:02:05 | [diff] [blame] | 409 | pMem->z = (char*)zOut; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 | [diff] [blame] | 410 | pMem->zMalloc = pMem->z; |
drh | 17bcb10 | 2014-09-18 21:25:33 | [diff] [blame] | 411 | pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 412 | |
| 413 | translate_out: |
danielk1977 | b5402fb | 2005-01-12 07:15:04 | [diff] [blame] | 414 | #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 415 | { |
drh | 5ca0632 | 2020-01-06 19:23:41 | [diff] [blame] | 416 | StrAccum acc; |
| 417 | char zBuf[1000]; |
| 418 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 419 | sqlite3VdbeMemPrettyPrint(pMem, &acc); |
| 420 | fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc)); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 421 | } |
| 422 | #endif |
| 423 | return SQLITE_OK; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 424 | } |
drh | f0f44b7 | 2017-07-12 12:19:33 | [diff] [blame] | 425 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 426 | |
drh | f0f44b7 | 2017-07-12 12:19:33 | [diff] [blame] | 427 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 | [diff] [blame] | 428 | /* |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 429 | ** This routine checks for a byte-order mark at the beginning of the |
| 430 | ** UTF-16 string stored in *pMem. If one is present, it is removed and |
| 431 | ** the encoding of the Mem adjusted. This routine does not do any |
| 432 | ** byte-swapping, it just sets Mem.enc appropriately. |
| 433 | ** |
| 434 | ** The allocation (static, dynamic etc.) and encoding of the Mem may be |
| 435 | ** changed by this function. |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 | [diff] [blame] | 436 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 437 | int sqlite3VdbeMemHandleBom(Mem *pMem){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 438 | int rc = SQLITE_OK; |
| 439 | u8 bom = 0; |
| 440 | |
drh | 769e97e | 2009-04-01 16:33:37 | [diff] [blame] | 441 | assert( pMem->n>=0 ); |
| 442 | if( pMem->n>1 ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 443 | u8 b1 = *(u8 *)pMem->z; |
| 444 | u8 b2 = *(((u8 *)pMem->z) + 1); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 | [diff] [blame] | 445 | if( b1==0xFE && b2==0xFF ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 446 | bom = SQLITE_UTF16BE; |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 | [diff] [blame] | 447 | } |
| 448 | if( b1==0xFF && b2==0xFE ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 449 | bom = SQLITE_UTF16LE; |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 | [diff] [blame] | 450 | } |
| 451 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 452 | |
| 453 | if( bom ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 | [diff] [blame] | 454 | rc = sqlite3VdbeMemMakeWriteable(pMem); |
| 455 | if( rc==SQLITE_OK ){ |
| 456 | pMem->n -= 2; |
| 457 | memmove(pMem->z, &pMem->z[2], pMem->n); |
| 458 | pMem->z[pMem->n] = '\0'; |
| 459 | pMem->z[pMem->n+1] = '\0'; |
| 460 | pMem->flags |= MEM_Term; |
| 461 | pMem->enc = bom; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 462 | } |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 463 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 464 | return rc; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 465 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 466 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 467 | |
| 468 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 | [diff] [blame] | 469 | ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, |
| 470 | ** return the number of unicode characters in pZ up to (but not including) |
| 471 | ** the first 0x00 byte. If nByte is not less than zero, return the |
| 472 | ** number of unicode characters in the first nByte of pZ (or up to |
| 473 | ** the first 0x00, whichever comes first). |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 474 | */ |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 475 | int sqlite3Utf8CharLen(const char *zIn, int nByte){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 476 | int r = 0; |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 477 | const u8 *z = (const u8*)zIn; |
| 478 | const u8 *zTerm; |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 | [diff] [blame] | 479 | if( nByte>=0 ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 480 | zTerm = &z[nByte]; |
| 481 | }else{ |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 482 | zTerm = (const u8*)(-1); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 483 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 484 | assert( z<=zTerm ); |
| 485 | while( *z!=0 && z<zTerm ){ |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 486 | SQLITE_SKIP_UTF8(z); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 487 | r++; |
| 488 | } |
| 489 | return r; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 | [diff] [blame] | 490 | } |
| 491 | |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 | [diff] [blame] | 492 | /* This test function is not currently used by the automated test-suite. |
| 493 | ** Hence it is only available in debug builds. |
| 494 | */ |
| 495 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 496 | /* |
| 497 | ** Translate UTF-8 to UTF-8. |
| 498 | ** |
| 499 | ** This has the effect of making sure that the string is well-formed |
| 500 | ** UTF-8. Miscoded characters are removed. |
| 501 | ** |
shaneh | dba2cc4 | 2011-03-24 17:43:18 | [diff] [blame] | 502 | ** The translation is done in-place and aborted if the output |
| 503 | ** overruns the input. |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 | [diff] [blame] | 504 | */ |
| 505 | int sqlite3Utf8To8(unsigned char *zIn){ |
| 506 | unsigned char *zOut = zIn; |
| 507 | unsigned char *zStart = zIn; |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 | [diff] [blame] | 508 | u32 c; |
| 509 | |
shaneh | dba2cc4 | 2011-03-24 17:43:18 | [diff] [blame] | 510 | while( zIn[0] && zOut<=zIn ){ |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 511 | c = sqlite3Utf8Read((const u8**)&zIn); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 | [diff] [blame] | 512 | if( c!=0xfffd ){ |
| 513 | WRITE_UTF8(zOut, c); |
| 514 | } |
| 515 | } |
| 516 | *zOut = 0; |
shane | b08a67a | 2009-03-31 03:41:56 | [diff] [blame] | 517 | return (int)(zOut - zStart); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 | [diff] [blame] | 518 | } |
| 519 | #endif |
| 520 | |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 521 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6622cce | 2004-05-20 11:00:52 | [diff] [blame] | 522 | /* |
drh | af9a7c2 | 2005-12-15 03:04:10 | [diff] [blame] | 523 | ** Convert a UTF-16 string in the native encoding into a UTF-8 string. |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 524 | ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must |
| 525 | ** be freed by the calling function. |
drh | af9a7c2 | 2005-12-15 03:04:10 | [diff] [blame] | 526 | ** |
| 527 | ** NULL is returned if there is an allocation error. |
| 528 | */ |
dan | b7dca7d | 2010-03-05 16:32:12 | [diff] [blame] | 529 | char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){ |
drh | af9a7c2 | 2005-12-15 03:04:10 | [diff] [blame] | 530 | Mem m; |
| 531 | memset(&m, 0, sizeof(m)); |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 532 | m.db = db; |
dan | b7dca7d | 2010-03-05 16:32:12 | [diff] [blame] | 533 | sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC); |
drh | b21c8cd | 2007-08-21 19:33:56 | [diff] [blame] | 534 | sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 | [diff] [blame] | 535 | if( db->mallocFailed ){ |
| 536 | sqlite3VdbeMemRelease(&m); |
| 537 | m.z = 0; |
| 538 | } |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 539 | assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); |
| 540 | assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); |
dan | b7dca7d | 2010-03-05 16:32:12 | [diff] [blame] | 541 | assert( m.z || db->mallocFailed ); |
| 542 | return m.z; |
drh | af9a7c2 | 2005-12-15 03:04:10 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
drh | f8305e4 | 2024-09-19 13:39:06 | [diff] [blame] | 546 | ** zIn is a UTF-16 encoded unicode string at least nByte bytes long. |
drh | aed382f | 2009-04-01 18:40:32 | [diff] [blame] | 547 | ** Return the number of bytes in the first nChar unicode characters |
drh | f8305e4 | 2024-09-19 13:39:06 | [diff] [blame] | 548 | ** in pZ. nChar must be non-negative. Surrogate pairs count as a single |
| 549 | ** character. |
danielk1977 | 6622cce | 2004-05-20 11:00:52 | [diff] [blame] | 550 | */ |
drh | f8305e4 | 2024-09-19 13:39:06 | [diff] [blame] | 551 | int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){ |
drh | aed382f | 2009-04-01 18:40:32 | [diff] [blame] | 552 | int c; |
| 553 | unsigned char const *z = zIn; |
drh | f8305e4 | 2024-09-19 13:39:06 | [diff] [blame] | 554 | unsigned char const *zEnd = &z[nByte-1]; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 555 | int n = 0; |
drh | 6d116ca | 2009-10-24 01:55:14 | [diff] [blame] | 556 | |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 557 | if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++; |
drh | fda6e50 | 2025-04-08 20:00:33 | [diff] [blame] | 558 | while( n<nChar && z<=zEnd ){ |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 559 | c = z[0]; |
| 560 | z += 2; |
drh | f8305e4 | 2024-09-19 13:39:06 | [diff] [blame] | 561 | if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2; |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 562 | n++; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 | [diff] [blame] | 563 | } |
drh | 0184a25 | 2020-02-17 23:08:16 | [diff] [blame] | 564 | return (int)(z-(unsigned char const *)zIn) |
| 565 | - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 | [diff] [blame] | 566 | } |
| 567 | |
drh | 53c1402 | 2007-05-10 17:23:11 | [diff] [blame] | 568 | #if defined(SQLITE_TEST) |
| 569 | /* |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 570 | ** This routine is called from the TCL test function "translate_selftest". |
| 571 | ** It checks that the primitives for serializing and deserializing |
| 572 | ** characters in each encoding are inverses of each other. |
| 573 | */ |
danielk1977 | 44a376f | 2008-08-12 15:04:58 | [diff] [blame] | 574 | void sqlite3UtfSelfTest(void){ |
drh | b3fa0e0 | 2006-10-19 01:58:43 | [diff] [blame] | 575 | unsigned int i, t; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 576 | unsigned char zBuf[20]; |
| 577 | unsigned char *z; |
| 578 | int n; |
drh | a39f4c5 | 2006-10-04 15:23:21 | [diff] [blame] | 579 | unsigned int c; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 580 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 | [diff] [blame] | 581 | for(i=0; i<0x00110000; i++){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 582 | z = zBuf; |
| 583 | WRITE_UTF8(z, i); |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 584 | n = (int)(z-zBuf); |
| 585 | assert( n>0 && n<=4 ); |
drh | 4a91911 | 2007-05-15 11:55:09 | [diff] [blame] | 586 | z[0] = 0; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 587 | z = zBuf; |
drh | 4261096 | 2012-09-17 18:56:32 | [diff] [blame] | 588 | c = sqlite3Utf8Read((const u8**)&z); |
drh | b3fa0e0 | 2006-10-19 01:58:43 | [diff] [blame] | 589 | t = i; |
| 590 | if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD; |
| 591 | if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD; |
| 592 | assert( c==t ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 593 | assert( (z-zBuf)==n ); |
| 594 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 | [diff] [blame] | 595 | } |
drh | 6c62608 | 2004-11-14 21:56:29 | [diff] [blame] | 596 | #endif /* SQLITE_TEST */ |
| 597 | #endif /* SQLITE_OMIT_UTF16 */ |