blob: fb587930b3a8b60b3fbc3e616b1b2a9479916665 [file] [log] [blame]
drha5d14fe2004-05-04 15:00:46 +00001/*
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**
drhb3fa0e02006-10-19 01:58:43 +000015** $Id: utf.c,v 1.43 2006/10/19 01:58:44 drh Exp $
drha5d14fe2004-05-04 15:00:46 +000016**
17** Notes on UTF-8:
18**
19** Byte-0 Byte-1 Byte-2 Byte-3 Value
20** 0xxxxxxx 00000000 00000000 0xxxxxxx
21** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
22** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
23** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
24**
25**
26** Notes on UTF-16: (with wwww+1==uuuuu)
27**
drh51846b52004-05-28 16:00:21 +000028** Word-0 Word-1 Value
29** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
30** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
drha5d14fe2004-05-04 15:00:46 +000031**
danielk1977998b56c2004-05-06 23:37:52 +000032**
drha5d14fe2004-05-04 15:00:46 +000033** BOM or Byte Order Mark:
34** 0xff 0xfe little-endian utf-16 follows
35** 0xfe 0xff big-endian utf-16 follows
danielk1977998b56c2004-05-06 23:37:52 +000036**
37**
38** Handling of malformed strings:
39**
40** SQLite accepts and processes malformed strings without an error wherever
41** possible. However this is not possible when converting between UTF-8 and
42** UTF-16.
43**
44** When converting malformed UTF-8 strings to UTF-16, one instance of the
45** replacement character U+FFFD for each byte that cannot be interpeted as
46** part of a valid unicode character.
47**
48** When converting malformed UTF-16 strings to UTF-8, one instance of the
49** replacement character U+FFFD for each pair of bytes that cannot be
50** interpeted as part of a valid unicode character.
danielk1977bfd6cce2004-06-18 04:24:54 +000051**
52** This file contains the following public routines:
53**
54** sqlite3VdbeMemTranslate() - Translate the encoding used by a Mem* string.
55** sqlite3VdbeMemHandleBom() - Handle byte-order-marks in UTF16 Mem* strings.
56** sqlite3utf16ByteLen() - Calculate byte-length of a void* UTF16 string.
57** sqlite3utf8CharLen() - Calculate char-length of a char* UTF8 string.
58** sqlite3utf8LikeCompare() - Do a LIKE match given two UTF8 char* strings.
59**
drha5d14fe2004-05-04 15:00:46 +000060*/
danielk1977998b56c2004-05-06 23:37:52 +000061#include "sqliteInt.h"
drhb659e9b2005-01-28 01:29:08 +000062#include <assert.h>
danielk1977bfd6cce2004-06-18 04:24:54 +000063#include "vdbeInt.h"
danielk1977998b56c2004-05-06 23:37:52 +000064
65/*
danielk1977bfd6cce2004-06-18 04:24:54 +000066** This table maps from the first byte of a UTF-8 character to the number
drhb3fa0e02006-10-19 01:58:43 +000067** of trailing bytes expected. A value '4' indicates that the table key
danielk1977bfd6cce2004-06-18 04:24:54 +000068** is not a legal first byte for a UTF-8 character.
danielk1977d02eb1f2004-06-06 09:44:03 +000069*/
danielk1977bfd6cce2004-06-18 04:24:54 +000070static const u8 xtra_utf8_bytes[256] = {
71/* 0xxxxxxx */
720, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
730, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
740, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
750, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
760, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
770, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
780, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
790, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
danielk1977d02eb1f2004-06-06 09:44:03 +000080
danielk1977bfd6cce2004-06-18 04:24:54 +000081/* 10wwwwww */
drhb3fa0e02006-10-19 01:58:43 +0000824, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
834, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
844, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
854, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
danielk1977ad7dd422004-06-06 12:41:49 +000086
danielk1977bfd6cce2004-06-18 04:24:54 +000087/* 110yyyyy */
881, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
891, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
90
91/* 1110zzzz */
922, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
93
94/* 11110yyy */
drhb3fa0e02006-10-19 01:58:43 +0000953, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
danielk1977bfd6cce2004-06-18 04:24:54 +000096};
97
98/*
99** This table maps from the number of trailing bytes in a UTF-8 character
100** to an integer constant that is effectively calculated for each character
101** read by a naive implementation of a UTF-8 character reader. The code
102** in the READ_UTF8 macro explains things best.
103*/
drhb3fa0e02006-10-19 01:58:43 +0000104static const int xtra_utf8_bits[] = {
105 0,
106 12416, /* (0xC0 << 6) + (0x80) */
107 925824, /* (0xE0 << 12) + (0x80 << 6) + (0x80) */
108 63447168 /* (0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
109};
110
111/*
112** If a UTF-8 character contains N bytes extra bytes (N bytes follow
113** the initial byte so that the total character length is N+1) then
114** masking the character with utf8_mask[N] must produce a non-zero
115** result. Otherwise, we have an (illegal) overlong encoding.
116*/
117static const int utf_mask[] = {
118 0x00000000,
119 0xffffff80,
120 0xfffff800,
121 0xffff0000,
danielk1977bfd6cce2004-06-18 04:24:54 +0000122};
123
124#define READ_UTF8(zIn, c) { \
125 int xtra; \
126 c = *(zIn)++; \
127 xtra = xtra_utf8_bytes[c]; \
128 switch( xtra ){ \
drhb3fa0e02006-10-19 01:58:43 +0000129 case 4: c = (int)0xFFFD; break; \
danielk1977bfd6cce2004-06-18 04:24:54 +0000130 case 3: c = (c<<6) + *(zIn)++; \
131 case 2: c = (c<<6) + *(zIn)++; \
132 case 1: c = (c<<6) + *(zIn)++; \
133 c -= xtra_utf8_bits[xtra]; \
drhb3fa0e02006-10-19 01:58:43 +0000134 if( (utf_mask[xtra]&c)==0 \
135 || (c&0xFFFFF800)==0xD800 \
136 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
danielk1977bfd6cce2004-06-18 04:24:54 +0000137 } \
138}
drh4e5ffc52004-08-31 00:52:37 +0000139int sqlite3ReadUtf8(const unsigned char *z){
140 int c;
141 READ_UTF8(z, c);
142 return c;
143}
danielk1977bfd6cce2004-06-18 04:24:54 +0000144
145#define SKIP_UTF8(zIn) { \
146 zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \
147}
148
149#define WRITE_UTF8(zOut, c) { \
150 if( c<0x00080 ){ \
151 *zOut++ = (c&0xFF); \
152 } \
153 else if( c<0x00800 ){ \
154 *zOut++ = 0xC0 + ((c>>6)&0x1F); \
155 *zOut++ = 0x80 + (c & 0x3F); \
156 } \
157 else if( c<0x10000 ){ \
158 *zOut++ = 0xE0 + ((c>>12)&0x0F); \
159 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
160 *zOut++ = 0x80 + (c & 0x3F); \
161 }else{ \
162 *zOut++ = 0xF0 + ((c>>18) & 0x07); \
163 *zOut++ = 0x80 + ((c>>12) & 0x3F); \
164 *zOut++ = 0x80 + ((c>>6) & 0x3F); \
165 *zOut++ = 0x80 + (c & 0x3F); \
166 } \
167}
168
169#define WRITE_UTF16LE(zOut, c) { \
170 if( c<=0xFFFF ){ \
171 *zOut++ = (c&0x00FF); \
172 *zOut++ = ((c>>8)&0x00FF); \
173 }else{ \
174 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
175 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \
176 *zOut++ = (c&0x00FF); \
177 *zOut++ = (0x00DC + ((c>>8)&0x03)); \
178 } \
179}
180
181#define WRITE_UTF16BE(zOut, c) { \
182 if( c<=0xFFFF ){ \
183 *zOut++ = ((c>>8)&0x00FF); \
184 *zOut++ = (c&0x00FF); \
185 }else{ \
186 *zOut++ = (0x00D8 + (((c-0x10000)>>18)&0x03)); \
187 *zOut++ = (((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
188 *zOut++ = (0x00DC + ((c>>8)&0x03)); \
189 *zOut++ = (c&0x00FF); \
190 } \
191}
192
193#define READ_UTF16LE(zIn, c){ \
194 c = (*zIn++); \
195 c += ((*zIn++)<<8); \
196 if( c>=0xD800 && c<=0xE000 ){ \
197 int c2 = (*zIn++); \
198 c2 += ((*zIn++)<<8); \
199 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
drhb3fa0e02006-10-19 01:58:43 +0000200 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
danielk1977bfd6cce2004-06-18 04:24:54 +0000201 } \
202}
203
204#define READ_UTF16BE(zIn, c){ \
205 c = ((*zIn++)<<8); \
206 c += (*zIn++); \
207 if( c>=0xD800 && c<=0xE000 ){ \
208 int c2 = ((*zIn++)<<8); \
209 c2 += (*zIn++); \
210 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
drhb3fa0e02006-10-19 01:58:43 +0000211 if( (c & 0xFFFF0000)==0 ) c = 0xFFFD; \
danielk1977bfd6cce2004-06-18 04:24:54 +0000212 } \
213}
214
danielk1977f4618892004-06-28 13:09:11 +0000215#define SKIP_UTF16BE(zIn){ \
216 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn+1)==0x00)) ){ \
217 zIn += 4; \
218 }else{ \
219 zIn += 2; \
220 } \
221}
222#define SKIP_UTF16LE(zIn){ \
223 zIn++; \
224 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn-1)==0x00)) ){ \
225 zIn += 3; \
226 }else{ \
227 zIn += 1; \
228 } \
229}
230
231#define RSKIP_UTF16LE(zIn){ \
232 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn-1)==0x00)) ){ \
233 zIn -= 4; \
234 }else{ \
235 zIn -= 2; \
236 } \
237}
238#define RSKIP_UTF16BE(zIn){ \
239 zIn--; \
240 if( *zIn>=0xD8 && (*zIn<0xE0 || (*zIn==0xE0 && *(zIn+1)==0x00)) ){ \
241 zIn -= 3; \
242 }else{ \
243 zIn -= 1; \
244 } \
245}
246
danielk1977bfd6cce2004-06-18 04:24:54 +0000247/*
248** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
249** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
250*/
251/* #define TRANSLATE_TRACE 1 */
252
drh6c626082004-11-14 21:56:29 +0000253#ifndef SQLITE_OMIT_UTF16
danielk1977bfd6cce2004-06-18 04:24:54 +0000254/*
255** This routine transforms the internal text encoding used by pMem to
256** desiredEnc. It is an error if the string is already of the desired
257** encoding, or if *pMem does not contain a string value.
258*/
259int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
260 unsigned char zShort[NBFS]; /* Temporary short output buffer */
261 int len; /* Maximum length of output string in bytes */
262 unsigned char *zOut; /* Output buffer */
263 unsigned char *zIn; /* Input iterator */
264 unsigned char *zTerm; /* End of input */
265 unsigned char *z; /* Output iterator */
drha39f4c52006-10-04 15:23:21 +0000266 unsigned int c;
danielk1977bfd6cce2004-06-18 04:24:54 +0000267
268 assert( pMem->flags&MEM_Str );
269 assert( pMem->enc!=desiredEnc );
270 assert( pMem->enc!=0 );
271 assert( pMem->n>=0 );
272
danielk1977b5402fb2005-01-12 07:15:04 +0000273#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
danielk1977bfd6cce2004-06-18 04:24:54 +0000274 {
275 char zBuf[100];
drh74161702006-02-24 02:53:49 +0000276 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
danielk1977bfd6cce2004-06-18 04:24:54 +0000277 fprintf(stderr, "INPUT: %s\n", zBuf);
danielk1977ad7dd422004-06-06 12:41:49 +0000278 }
279#endif
280
danielk1977bfd6cce2004-06-18 04:24:54 +0000281 /* If the translation is between UTF-16 little and big endian, then
282 ** all that is required is to swap the byte order. This case is handled
283 ** differently from the others.
danielk1977998b56c2004-05-06 23:37:52 +0000284 */
danielk1977bfd6cce2004-06-18 04:24:54 +0000285 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
286 u8 temp;
drh71c697e2004-08-08 23:39:19 +0000287 int rc;
288 rc = sqlite3VdbeMemMakeWriteable(pMem);
289 if( rc!=SQLITE_OK ){
290 assert( rc==SQLITE_NOMEM );
291 return SQLITE_NOMEM;
292 }
drh2646da72005-12-09 20:02:05 +0000293 zIn = (u8*)pMem->z;
danielk1977bfd6cce2004-06-18 04:24:54 +0000294 zTerm = &zIn[pMem->n];
295 while( zIn<zTerm ){
296 temp = *zIn;
297 *zIn = *(zIn+1);
298 zIn++;
299 *zIn++ = temp;
300 }
301 pMem->enc = desiredEnc;
302 goto translate_out;
303 }
304
danielk1977d7e69642004-06-23 00:23:49 +0000305 /* Set len to the maximum number of bytes required in the output buffer. */
306 if( desiredEnc==SQLITE_UTF8 ){
307 /* When converting from UTF-16, the maximum growth results from
drha49b8612006-04-16 12:05:03 +0000308 ** translating a 2-byte character to a 4-byte UTF-8 character.
309 ** A single byte is required for the output string
danielk1977d7e69642004-06-23 00:23:49 +0000310 ** nul-terminator.
311 */
drha49b8612006-04-16 12:05:03 +0000312 len = pMem->n * 2 + 1;
danielk1977d7e69642004-06-23 00:23:49 +0000313 }else{
314 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
315 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
316 ** character. Two bytes are required in the output buffer for the
317 ** nul-terminator.
318 */
319 len = pMem->n * 2 + 2;
320 }
321
danielk1977bfd6cce2004-06-18 04:24:54 +0000322 /* Set zIn to point at the start of the input buffer and zTerm to point 1
323 ** byte past the end.
324 **
325 ** Variable zOut is set to point at the output buffer. This may be space
326 ** obtained from malloc(), or Mem.zShort, if it large enough and not in
327 ** use, or the zShort array on the stack (see above).
328 */
drh2646da72005-12-09 20:02:05 +0000329 zIn = (u8*)pMem->z;
danielk1977bfd6cce2004-06-18 04:24:54 +0000330 zTerm = &zIn[pMem->n];
danielk1977bfd6cce2004-06-18 04:24:54 +0000331 if( len>NBFS ){
332 zOut = sqliteMallocRaw(len);
333 if( !zOut ) return SQLITE_NOMEM;
334 }else{
danielk19771ba1b552004-06-23 13:46:32 +0000335 zOut = zShort;
danielk1977bfd6cce2004-06-18 04:24:54 +0000336 }
337 z = zOut;
338
339 if( pMem->enc==SQLITE_UTF8 ){
340 if( desiredEnc==SQLITE_UTF16LE ){
341 /* UTF-8 -> UTF-16 Little-endian */
342 while( zIn<zTerm ){
343 READ_UTF8(zIn, c);
344 WRITE_UTF16LE(z, c);
345 }
drhb8dd3152004-09-24 23:20:51 +0000346 }else{
347 assert( desiredEnc==SQLITE_UTF16BE );
danielk1977bfd6cce2004-06-18 04:24:54 +0000348 /* UTF-8 -> UTF-16 Big-endian */
349 while( zIn<zTerm ){
350 READ_UTF8(zIn, c);
351 WRITE_UTF16BE(z, c);
352 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000353 }
drhb8dd3152004-09-24 23:20:51 +0000354 pMem->n = z - zOut;
355 *z++ = 0;
danielk1977bfd6cce2004-06-18 04:24:54 +0000356 }else{
357 assert( desiredEnc==SQLITE_UTF8 );
358 if( pMem->enc==SQLITE_UTF16LE ){
359 /* UTF-16 Little-endian -> UTF-8 */
360 while( zIn<zTerm ){
361 READ_UTF16LE(zIn, c);
362 WRITE_UTF8(z, c);
363 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000364 }else{
365 /* UTF-16 Little-endian -> UTF-8 */
366 while( zIn<zTerm ){
367 READ_UTF16BE(zIn, c);
368 WRITE_UTF8(z, c);
369 }
danielk1977998b56c2004-05-06 23:37:52 +0000370 }
drhb8dd3152004-09-24 23:20:51 +0000371 pMem->n = z - zOut;
danielk1977998b56c2004-05-06 23:37:52 +0000372 }
drhb8dd3152004-09-24 23:20:51 +0000373 *z = 0;
danielk1977d7e69642004-06-23 00:23:49 +0000374 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
danielk1977998b56c2004-05-06 23:37:52 +0000375
danielk1977bfd6cce2004-06-18 04:24:54 +0000376 sqlite3VdbeMemRelease(pMem);
377 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
378 pMem->enc = desiredEnc;
danielk19771ba1b552004-06-23 13:46:32 +0000379 if( zOut==zShort ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000380 memcpy(pMem->zShort, zOut, len);
drh2646da72005-12-09 20:02:05 +0000381 zOut = (u8*)pMem->zShort;
danielk1977bfd6cce2004-06-18 04:24:54 +0000382 pMem->flags |= (MEM_Term|MEM_Short);
383 }else{
384 pMem->flags |= (MEM_Term|MEM_Dyn);
385 }
drh2646da72005-12-09 20:02:05 +0000386 pMem->z = (char*)zOut;
danielk1977bfd6cce2004-06-18 04:24:54 +0000387
388translate_out:
danielk1977b5402fb2005-01-12 07:15:04 +0000389#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
danielk1977bfd6cce2004-06-18 04:24:54 +0000390 {
391 char zBuf[100];
drh74161702006-02-24 02:53:49 +0000392 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
danielk1977bfd6cce2004-06-18 04:24:54 +0000393 fprintf(stderr, "OUTPUT: %s\n", zBuf);
394 }
395#endif
396 return SQLITE_OK;
danielk1977998b56c2004-05-06 23:37:52 +0000397}
398
danielk197793d46752004-05-23 13:30:58 +0000399/*
danielk1977bfd6cce2004-06-18 04:24:54 +0000400** This routine checks for a byte-order mark at the beginning of the
401** UTF-16 string stored in *pMem. If one is present, it is removed and
402** the encoding of the Mem adjusted. This routine does not do any
403** byte-swapping, it just sets Mem.enc appropriately.
404**
405** The allocation (static, dynamic etc.) and encoding of the Mem may be
406** changed by this function.
danielk197793d46752004-05-23 13:30:58 +0000407*/
danielk1977bfd6cce2004-06-18 04:24:54 +0000408int sqlite3VdbeMemHandleBom(Mem *pMem){
409 int rc = SQLITE_OK;
410 u8 bom = 0;
411
412 if( pMem->n<0 || pMem->n>1 ){
413 u8 b1 = *(u8 *)pMem->z;
414 u8 b2 = *(((u8 *)pMem->z) + 1);
danielk197793d46752004-05-23 13:30:58 +0000415 if( b1==0xFE && b2==0xFF ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000416 bom = SQLITE_UTF16BE;
danielk197793d46752004-05-23 13:30:58 +0000417 }
418 if( b1==0xFF && b2==0xFE ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000419 bom = SQLITE_UTF16LE;
danielk197793d46752004-05-23 13:30:58 +0000420 }
421 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000422
423 if( bom ){
danielk19771ba1b552004-06-23 13:46:32 +0000424 /* This function is called as soon as a string is stored in a Mem*,
425 ** from within sqlite3VdbeMemSetStr(). At that point it is not possible
426 ** for the string to be stored in Mem.zShort, or for it to be stored
427 ** in dynamic memory with no destructor.
428 */
429 assert( !(pMem->flags&MEM_Short) );
430 assert( !(pMem->flags&MEM_Dyn) || pMem->xDel );
431 if( pMem->flags & MEM_Dyn ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000432 void (*xDel)(void*) = pMem->xDel;
433 char *z = pMem->z;
434 pMem->z = 0;
435 pMem->xDel = 0;
436 rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, SQLITE_TRANSIENT);
danielk19771ba1b552004-06-23 13:46:32 +0000437 xDel(z);
danielk1977bfd6cce2004-06-18 04:24:54 +0000438 }else{
439 rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom,
440 SQLITE_TRANSIENT);
441 }
danielk1977998b56c2004-05-06 23:37:52 +0000442 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000443 return rc;
danielk1977998b56c2004-05-06 23:37:52 +0000444}
drh6c626082004-11-14 21:56:29 +0000445#endif /* SQLITE_OMIT_UTF16 */
danielk1977998b56c2004-05-06 23:37:52 +0000446
447/*
danielk19776622cce2004-05-20 11:00:52 +0000448** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
449** return the number of unicode characters in pZ up to (but not including)
450** the first 0x00 byte. If nByte is not less than zero, return the
451** number of unicode characters in the first nByte of pZ (or up to
452** the first 0x00, whichever comes first).
danielk1977998b56c2004-05-06 23:37:52 +0000453*/
danielk1977bfd6cce2004-06-18 04:24:54 +0000454int sqlite3utf8CharLen(const char *z, int nByte){
455 int r = 0;
456 const char *zTerm;
danielk19771ba1b552004-06-23 13:46:32 +0000457 if( nByte>=0 ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000458 zTerm = &z[nByte];
459 }else{
460 zTerm = (const char *)(-1);
danielk1977998b56c2004-05-06 23:37:52 +0000461 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000462 assert( z<=zTerm );
463 while( *z!=0 && z<zTerm ){
464 SKIP_UTF8(z);
465 r++;
466 }
467 return r;
danielk19776622cce2004-05-20 11:00:52 +0000468}
469
drh6c626082004-11-14 21:56:29 +0000470#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +0000471/*
drhaf9a7c22005-12-15 03:04:10 +0000472** Convert a UTF-16 string in the native encoding into a UTF-8 string.
473** Memory to hold the UTF-8 string is obtained from malloc and must be
474** freed by the calling function.
475**
476** NULL is returned if there is an allocation error.
477*/
478char *sqlite3utf16to8(const void *z, int nByte){
479 Mem m;
480 memset(&m, 0, sizeof(m));
481 sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
482 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
drh66f4a062006-07-26 14:57:30 +0000483 assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() );
484 assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() );
danielk1977e7259292006-01-13 06:33:23 +0000485 return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z);
drhaf9a7c22005-12-15 03:04:10 +0000486}
487
488/*
danielk19776622cce2004-05-20 11:00:52 +0000489** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
490** return the number of bytes up to (but not including), the first pair
491** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
492** then return the number of bytes in the first nChar unicode characters
493** in pZ (or up until the first pair of 0x00 bytes, whichever comes first).
494*/
danielk1977bfd6cce2004-06-18 04:24:54 +0000495int sqlite3utf16ByteLen(const void *zIn, int nChar){
drha39f4c52006-10-04 15:23:21 +0000496 unsigned int c = 1;
danielk1977bfd6cce2004-06-18 04:24:54 +0000497 char const *z = zIn;
498 int n = 0;
499 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
danielk1977161fb792006-01-24 10:58:21 +0000500 /* Using an "if (SQLITE_UTF16NATIVE==SQLITE_UTF16BE)" construct here
501 ** and in other parts of this file means that at one branch will
502 ** not be covered by coverage testing on any single host. But coverage
503 ** will be complete if the tests are run on both a little-endian and
504 ** big-endian host. Because both the UTF16NATIVE and SQLITE_UTF16BE
505 ** macros are constant at compile time the compiler can determine
506 ** which branch will be followed. It is therefore assumed that no runtime
507 ** penalty is paid for this "if" statement.
508 */
danielk1977bfd6cce2004-06-18 04:24:54 +0000509 while( c && ((nChar<0) || n<nChar) ){
510 READ_UTF16BE(z, c);
511 n++;
danielk19776622cce2004-05-20 11:00:52 +0000512 }
danielk19776622cce2004-05-20 11:00:52 +0000513 }else{
danielk1977bfd6cce2004-06-18 04:24:54 +0000514 while( c && ((nChar<0) || n<nChar) ){
515 READ_UTF16LE(z, c);
516 n++;
danielk19776622cce2004-05-20 11:00:52 +0000517 }
danielk19776622cce2004-05-20 11:00:52 +0000518 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000519 return (z-(char const *)zIn)-((c==0)?2:0);
danielk1977998b56c2004-05-06 23:37:52 +0000520}
521
drha5d14fe2004-05-04 15:00:46 +0000522/*
danielk1977f4618892004-06-28 13:09:11 +0000523** UTF-16 implementation of the substr()
524*/
525void sqlite3utf16Substr(
526 sqlite3_context *context,
527 int argc,
528 sqlite3_value **argv
529){
530 int y, z;
531 unsigned char const *zStr;
532 unsigned char const *zStrEnd;
533 unsigned char const *zStart;
534 unsigned char const *zEnd;
535 int i;
536
537 zStr = (unsigned char const *)sqlite3_value_text16(argv[0]);
538 zStrEnd = &zStr[sqlite3_value_bytes16(argv[0])];
539 y = sqlite3_value_int(argv[1]);
540 z = sqlite3_value_int(argv[2]);
541
542 if( y>0 ){
543 y = y-1;
544 zStart = zStr;
545 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){
546 for(i=0; i<y && zStart<zStrEnd; i++) SKIP_UTF16BE(zStart);
547 }else{
548 for(i=0; i<y && zStart<zStrEnd; i++) SKIP_UTF16LE(zStart);
549 }
550 }else{
551 zStart = zStrEnd;
552 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){
553 for(i=y; i<0 && zStart>zStr; i++) RSKIP_UTF16BE(zStart);
554 }else{
555 for(i=y; i<0 && zStart>zStr; i++) RSKIP_UTF16LE(zStart);
556 }
557 for(; i<0; i++) z -= 1;
558 }
559
560 zEnd = zStart;
561 if( SQLITE_UTF16BE==SQLITE_UTF16NATIVE ){
562 for(i=0; i<z && zEnd<zStrEnd; i++) SKIP_UTF16BE(zEnd);
563 }else{
564 for(i=0; i<z && zEnd<zStrEnd; i++) SKIP_UTF16LE(zEnd);
565 }
566
567 sqlite3_result_text16(context, zStart, zEnd-zStart, SQLITE_TRANSIENT);
568}
569
drh38f82712004-06-18 17:10:16 +0000570#if defined(SQLITE_TEST)
danielk1977bfd6cce2004-06-18 04:24:54 +0000571/*
572** This routine is called from the TCL test function "translate_selftest".
573** It checks that the primitives for serializing and deserializing
574** characters in each encoding are inverses of each other.
575*/
576void sqlite3utfSelfTest(){
drhb3fa0e02006-10-19 01:58:43 +0000577 unsigned int i, t;
danielk1977bfd6cce2004-06-18 04:24:54 +0000578 unsigned char zBuf[20];
579 unsigned char *z;
580 int n;
drha39f4c52006-10-04 15:23:21 +0000581 unsigned int c;
danielk1977bfd6cce2004-06-18 04:24:54 +0000582
danielk19771ba1b552004-06-23 13:46:32 +0000583 for(i=0; i<0x00110000; i++){
danielk1977bfd6cce2004-06-18 04:24:54 +0000584 z = zBuf;
585 WRITE_UTF8(z, i);
586 n = z-zBuf;
587 z = zBuf;
588 READ_UTF8(z, c);
drhb3fa0e02006-10-19 01:58:43 +0000589 t = i;
590 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
591 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
592 assert( c==t );
danielk1977bfd6cce2004-06-18 04:24:54 +0000593 assert( (z-zBuf)==n );
594 }
595 for(i=0; i<0x00110000; i++){
596 if( i>=0xD800 && i<=0xE000 ) continue;
597 z = zBuf;
598 WRITE_UTF16LE(z, i);
599 n = z-zBuf;
600 z = zBuf;
601 READ_UTF16LE(z, c);
602 assert( c==i );
603 assert( (z-zBuf)==n );
604 }
605 for(i=0; i<0x00110000; i++){
606 if( i>=0xD800 && i<=0xE000 ) continue;
607 z = zBuf;
608 WRITE_UTF16BE(z, i);
609 n = z-zBuf;
610 z = zBuf;
611 READ_UTF16BE(z, c);
612 assert( c==i );
613 assert( (z-zBuf)==n );
614 }
615}
drh6c626082004-11-14 21:56:29 +0000616#endif /* SQLITE_TEST */
617#endif /* SQLITE_OMIT_UTF16 */