interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / utf.c

592 lines · 18341 bytes Raw Blame History
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**
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**
26** Word-0 Word-1 Value
27** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
28** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
29**
30**
31** BOM or Byte Order Mark:
32** 0xff 0xfe little-endian utf-16 follows
33** 0xfe 0xff big-endian utf-16 follows
34**
35*/
36#include "sqliteInt.h"
37#include <assert.h>
38#include "vdbeInt.h"
39
40#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
41/*
42** The following constant value is used by the SQLITE_BIGENDIAN and
43** SQLITE_LITTLEENDIAN macros.
44*/
45const int sqlite3one = 1;
46#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
47
48/*
49** This lookup table is used to help decode the first byte of
50** a multi-byte UTF8 character.
51*/
52static const unsigned char sqlite3Utf8Trans1[] = {
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,
61};
62
63
64#define WRITE_UTF8(zOut, c) { \
65 if( c<0x00080 ){ \
66 *zOut++ = (u8)(c&0xFF); \
67 } \
68 else if( c<0x00800 ){ \
69 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
70 *zOut++ = 0x80 + (u8)(c & 0x3F); \
71 } \
72 else if( c<0x10000 ){ \
73 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
74 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
75 *zOut++ = 0x80 + (u8)(c & 0x3F); \
76 }else{ \
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); \
81 } \
82}
83
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 } \
94}
95
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 } \
106}
107
108/*
109** Write a single UTF8 character whose value is v into the
110** buffer starting at zOut. zOut must be sized to hold at
111** least four bytes. Return the number of bytes needed
112** to encode the new character.
113*/
114int 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/*
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**
160** * This routine accepts over-length UTF8 encodings
161** for unicode values 0x80 and greater. It does not change over-length
162** encodings to 0xfffd as some systems recommend.
163*/
164#define READ_UTF8(zIn, zTerm, c) \
165 c = *(zIn++); \
166 if( c>=0xc0 ){ \
167 c = sqlite3Utf8Trans1[c-0xc0]; \
168 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
169 c = (c<<6) + (0x3f & *(zIn++)); \
170 } \
171 if( c<0x80 || (c&0xFFFFF800)==0xD800 ){ c = 0xFFFD; } \
172 }
173u32 sqlite3Utf8Read(
174 const unsigned char **pz /* Pointer to string from which to read char */
175){
176 unsigned int c;
177
178 /* Same as READ_UTF8() above but without the zTerm parameter.
179 ** For this routine, we assume the UTF8 string is always zero-terminated.
180 */
181 c = *((*pz)++);
182 if( c>=0xc0 ){
183 c = sqlite3Utf8Trans1[c-0xc0];
184 while( (*(*pz) & 0xc0)==0x80 ){
185 c = (c<<6) + (0x3f & *((*pz)++));
186 }
187 if( c<0x80 || (c&0xFFFFF800)==0xD800 ){ c = 0xFFFD; }
188 }
189 return c;
190}
191
192/*
193** Read a single UTF8 character out of buffer z[], but reading no
194** more than n characters from the buffer. z[] is not zero-terminated.
195**
196** Return the number of bytes used to construct the character.
197**
198** Invalid UTF8 might generate a strange result. No effort is made
199** to detect invalid UTF8.
200**
201** At most 4 bytes will be read out of z[]. The return value will always
202** be between 1 and 4.
203*/
204int sqlite3Utf8ReadLimited(
205 const u8 *z,
206 int n,
207 u32 *piOut
208){
209 u32 c;
210 int i = 1;
211 assert( n>0 );
212 c = z[0];
213 if( c>=0xc0 ){
214 c = sqlite3Utf8Trans1[c-0xc0];
215 if( n>4 ) n = 4;
216 while( i<n && (z[i] & 0xc0)==0x80 ){
217 c = (c<<6) + (0x3f & z[i]);
218 i++;
219 }
220 }
221 *piOut = c;
222 return i;
223}
224
225
226/*
227** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
228** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
229*/
230/* #define TRANSLATE_TRACE 1 */
231
232#ifndef SQLITE_OMIT_UTF16
233/*
234** This routine transforms the internal text encoding used by pMem to
235** desiredEnc. It is an error if the string is already of the desired
236** encoding, or if *pMem does not contain a string value.
237*/
238SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
239 sqlite3_int64 len; /* Maximum length of output string in bytes */
240 unsigned char *zOut; /* Output buffer */
241 unsigned char *zIn; /* Input iterator */
242 unsigned char *zTerm; /* End of input */
243 unsigned char *z; /* Output iterator */
244 unsigned int c;
245
246 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
247 assert( pMem->flags&MEM_Str );
248 assert( pMem->enc!=desiredEnc );
249 assert( pMem->enc!=0 );
250 assert( pMem->n>=0 );
251
252#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
253 {
254 StrAccum acc;
255 char zBuf[1000];
256 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
257 sqlite3VdbeMemPrettyPrint(pMem, &acc);
258 fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
259 }
260#endif
261
262 /* If the translation is between UTF-16 little and big endian, then
263 ** all that is required is to swap the byte order. This case is handled
264 ** differently from the others.
265 */
266 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
267 u8 temp;
268 int rc;
269 rc = sqlite3VdbeMemMakeWriteable(pMem);
270 if( rc!=SQLITE_OK ){
271 assert( rc==SQLITE_NOMEM );
272 return SQLITE_NOMEM_BKPT;
273 }
274 zIn = (u8*)pMem->z;
275 zTerm = &zIn[pMem->n&~1];
276 while( zIn<zTerm ){
277 temp = *zIn;
278 *zIn = *(zIn+1);
279 zIn++;
280 *zIn++ = temp;
281 }
282 pMem->enc = desiredEnc;
283 goto translate_out;
284 }
285
286 /* Set len to the maximum number of bytes required in the output buffer. */
287 if( desiredEnc==SQLITE_UTF8 ){
288 /* When converting from UTF-16, the maximum growth results from
289 ** translating a 2-byte character to a 4-byte UTF-8 character.
290 ** A single byte is required for the output string
291 ** nul-terminator.
292 */
293 pMem->n &= ~1;
294 len = 2 * (sqlite3_int64)pMem->n + 1;
295 }else{
296 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
297 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
298 ** character. Two bytes are required in the output buffer for the
299 ** nul-terminator.
300 */
301 len = 2 * (sqlite3_int64)pMem->n + 2;
302 }
303
304 /* Set zIn to point at the start of the input buffer and zTerm to point 1
305 ** byte past the end.
306 **
307 ** Variable zOut is set to point at the output buffer, space obtained
308 ** from sqlite3_malloc().
309 */
310 zIn = (u8*)pMem->z;
311 zTerm = &zIn[pMem->n];
312 zOut = sqlite3DbMallocRaw(pMem->db, len);
313 if( !zOut ){
314 return SQLITE_NOMEM_BKPT;
315 }
316 z = zOut;
317
318 if( pMem->enc==SQLITE_UTF8 ){
319 if( desiredEnc==SQLITE_UTF16LE ){
320 /* UTF-8 -> UTF-16 Little-endian */
321 while( zIn<zTerm ){
322 READ_UTF8(zIn, zTerm, c);
323 WRITE_UTF16LE(z, c);
324 }
325 }else{
326 assert( desiredEnc==SQLITE_UTF16BE );
327 /* UTF-8 -> UTF-16 Big-endian */
328 while( zIn<zTerm ){
329 READ_UTF8(zIn, zTerm, c);
330 WRITE_UTF16BE(z, c);
331 }
332 }
333 pMem->n = (int)(z - zOut);
334 *z++ = 0;
335 }else{
336 assert( desiredEnc==SQLITE_UTF8 );
337 if( pMem->enc==SQLITE_UTF16LE ){
338 /* UTF-16 Little-endian -> UTF-8 */
339 while( zIn<zTerm ){
340 c = *(zIn++);
341 c += (*(zIn++))<<8;
342 if( c>=0xd800 && c<0xe000 ){
343#ifdef SQLITE_REPLACE_INVALID_UTF
344 if( c>=0xdc00 || zIn>=zTerm ){
345 c = 0xfffd;
346 }else{
347 int c2 = *(zIn++);
348 c2 += (*(zIn++))<<8;
349 if( c2<0xdc00 || c2>=0xe000 ){
350 zIn -= 2;
351 c = 0xfffd;
352 }else{
353 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
354 }
355 }
356#else
357 if( zIn<zTerm ){
358 int c2 = (*zIn++);
359 c2 += ((*zIn++)<<8);
360 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
361 }
362#endif
363 }
364 WRITE_UTF8(z, c);
365 }
366 }else{
367 /* UTF-16 Big-endian -> UTF-8 */
368 while( zIn<zTerm ){
369 c = (*(zIn++))<<8;
370 c += *(zIn++);
371 if( c>=0xd800 && c<0xe000 ){
372#ifdef SQLITE_REPLACE_INVALID_UTF
373 if( c>=0xdc00 || zIn>=zTerm ){
374 c = 0xfffd;
375 }else{
376 int c2 = (*(zIn++))<<8;
377 c2 += *(zIn++);
378 if( c2<0xdc00 || c2>=0xe000 ){
379 zIn -= 2;
380 c = 0xfffd;
381 }else{
382 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
383 }
384 }
385#else
386 if( zIn<zTerm ){
387 int c2 = ((*zIn++)<<8);
388 c2 += (*zIn++);
389 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
390 }
391#endif
392 }
393 WRITE_UTF8(z, c);
394 }
395 }
396 pMem->n = (int)(z - zOut);
397 }
398 *z = 0;
399 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
400
401 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
402 sqlite3VdbeMemRelease(pMem);
403 pMem->flags = c;
404 pMem->enc = desiredEnc;
405 pMem->z = (char*)zOut;
406 pMem->zMalloc = pMem->z;
407 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
408
409translate_out:
410#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
411 {
412 StrAccum acc;
413 char zBuf[1000];
414 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
415 sqlite3VdbeMemPrettyPrint(pMem, &acc);
416 fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
417 }
418#endif
419 return SQLITE_OK;
420}
421#endif /* SQLITE_OMIT_UTF16 */
422
423#ifndef SQLITE_OMIT_UTF16
424/*
425** This routine checks for a byte-order mark at the beginning of the
426** UTF-16 string stored in *pMem. If one is present, it is removed and
427** the encoding of the Mem adjusted. This routine does not do any
428** byte-swapping, it just sets Mem.enc appropriately.
429**
430** The allocation (static, dynamic etc.) and encoding of the Mem may be
431** changed by this function.
432*/
433int sqlite3VdbeMemHandleBom(Mem *pMem){
434 int rc = SQLITE_OK;
435 u8 bom = 0;
436
437 assert( pMem->n>=0 );
438 if( pMem->n>1 ){
439 u8 b1 = *(u8 *)pMem->z;
440 u8 b2 = *(((u8 *)pMem->z) + 1);
441 if( b1==0xFE && b2==0xFF ){
442 bom = SQLITE_UTF16BE;
443 }
444 if( b1==0xFF && b2==0xFE ){
445 bom = SQLITE_UTF16LE;
446 }
447 }
448
449 if( bom ){
450 rc = sqlite3VdbeMemMakeWriteable(pMem);
451 if( rc==SQLITE_OK ){
452 pMem->n -= 2;
453 memmove(pMem->z, &pMem->z[2], pMem->n);
454 pMem->z[pMem->n] = '\0';
455 pMem->z[pMem->n+1] = '\0';
456 pMem->flags |= MEM_Term;
457 pMem->enc = bom;
458 }
459 }
460 return rc;
461}
462#endif /* SQLITE_OMIT_UTF16 */
463
464/*
465** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
466** return the number of unicode characters in pZ up to (but not including)
467** the first 0x00 byte. If nByte is not less than zero, return the
468** number of unicode characters in the first nByte of pZ (or up to
469** the first 0x00, whichever comes first).
470*/
471int sqlite3Utf8CharLen(const char *zIn, int nByte){
472 int r = 0;
473 const u8 *z = (const u8*)zIn;
474 const u8 *zTerm;
475 if( nByte>=0 ){
476 zTerm = &z[nByte];
477 }else{
478 zTerm = (const u8*)(-1);
479 }
480 assert( z<=zTerm );
481 while( *z!=0 && z<zTerm ){
482 SQLITE_SKIP_UTF8(z);
483 r++;
484 }
485 return r;
486}
487
488/* This test function is not currently used by the automated test-suite.
489** Hence it is only available in debug builds.
490*/
491#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
492/*
493** Translate UTF-8 to UTF-8.
494**
495** This has the effect of making sure that the string is well-formed
496** UTF-8. Miscoded characters are removed.
497**
498** The translation is done in-place and aborted if the output
499** overruns the input.
500*/
501int sqlite3Utf8To8(unsigned char *zIn){
502 unsigned char *zOut = zIn;
503 unsigned char *zStart = zIn;
504 u32 c;
505
506 while( zIn[0] && zOut<=zIn ){
507 c = sqlite3Utf8Read((const u8**)&zIn);
508 if( c!=0xfffd ){
509 WRITE_UTF8(zOut, c);
510 }
511 }
512 *zOut = 0;
513 return (int)(zOut - zStart);
514}
515#endif
516
517#ifndef SQLITE_OMIT_UTF16
518/*
519** Convert a UTF-16 string in the native encoding into a UTF-8 string.
520** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
521** be freed by the calling function.
522**
523** NULL is returned if there is an allocation error.
524*/
525char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
526 Mem m;
527 memset(&m, 0, sizeof(m));
528 m.db = db;
529 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
530 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
531 if( db->mallocFailed ){
532 sqlite3VdbeMemRelease(&m);
533 m.z = 0;
534 }
535 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
536 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
537 assert( m.z || db->mallocFailed );
538 return m.z;
539}
540
541/*
542** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
543** Return the number of bytes in the first nChar unicode characters
544** in pZ. nChar must be non-negative. Surrogate pairs count as a single
545** character.
546*/
547int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){
548 int c;
549 unsigned char const *z = zIn;
550 unsigned char const *zEnd = &z[nByte-1];
551 int n = 0;
552
553 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
554 while( n<nChar && z<=zEnd ){
555 c = z[0];
556 z += 2;
557 if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
558 n++;
559 }
560 return (int)(z-(unsigned char const *)zIn)
561 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
562}
563
564#if defined(SQLITE_TEST)
565/*
566** This routine is called from the TCL test function "translate_selftest".
567** It checks that the primitives for serializing and deserializing
568** characters in each encoding are inverses of each other.
569*/
570void sqlite3UtfSelfTest(void){
571 unsigned int i, t;
572 unsigned char zBuf[20];
573 unsigned char *z;
574 int n;
575 unsigned int c;
576
577 for(i=0; i<0x00110000; i++){
578 z = zBuf;
579 WRITE_UTF8(z, i);
580 n = (int)(z-zBuf);
581 assert( n>0 && n<=4 );
582 z[0] = 0;
583 z = zBuf;
584 c = sqlite3Utf8Read((const u8**)&z);
585 t = i;
586 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
587 assert( c==t );
588 assert( (z-zBuf)==n );
589 }
590}
591#endif /* SQLITE_TEST */
592#endif /* SQLITE_OMIT_UTF16 */