blob: 26e7afcb274f1de095ba0380a0723611042f40cc [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Utility functions used throughout sqlite.
13**
14** This file contains functions for allocating memory, comparing
15** strings, and stuff like that.
16**
drh326dce72003-01-29 14:06:07 +000017** $Id: util.c,v 1.57 2003/01/29 14:06:09 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
drh7c68d602000-10-11 19:28:51 +000023/*
drhdaffd0e2001-04-11 14:28:42 +000024** If malloc() ever fails, this global variable gets set to 1.
25** This causes the library to abort and never again function.
26*/
27int sqlite_malloc_failed = 0;
28
29/*
drh7c68d602000-10-11 19:28:51 +000030** If MEMORY_DEBUG is defined, then use versions of malloc() and
31** free() that track memory usage and check for buffer overruns.
32*/
drhdcc581c2000-05-30 13:44:19 +000033#ifdef MEMORY_DEBUG
34
drhdcc581c2000-05-30 13:44:19 +000035/*
drh8c82b352000-12-10 18:23:50 +000036** For keeping track of the number of mallocs and frees. This
37** is used to check for memory leaks.
38*/
39int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
40int sqlite_nFree; /* Number of sqliteFree() calls */
41int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
drhd94a6692002-08-25 18:29:11 +000042#if MEMORY_DEBUG>1
43static int memcnt = 0;
44#endif
drh8c82b352000-12-10 18:23:50 +000045
46
47/*
drhdcc581c2000-05-30 13:44:19 +000048** Allocate new memory and set it to zero. Return NULL if
49** no memory is available.
50*/
drh8c1238a2003-01-02 14:43:55 +000051void *sqliteMalloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000052 void *p;
53 int *pi;
54 int k;
drh6e142f52000-06-08 13:36:40 +000055 if( sqlite_iMallocFail>=0 ){
56 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000057 if( sqlite_iMallocFail==0 ){
58 sqlite_malloc_failed++;
drh6d4abfb2001-10-22 02:58:08 +000059#if MEMORY_DEBUG>1
60 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
61 n, zFile,line);
62#endif
63 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000064 return 0;
65 }
drh6e142f52000-06-08 13:36:40 +000066 }
drhb0729502001-03-14 12:35:57 +000067 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000068 k = (n+sizeof(int)-1)/sizeof(int);
69 pi = malloc( (3+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000070 if( pi==0 ){
71 sqlite_malloc_failed++;
72 return 0;
73 }
drh6d4abfb2001-10-22 02:58:08 +000074 sqlite_nMalloc++;
drhdcc581c2000-05-30 13:44:19 +000075 pi[0] = 0xdead1122;
76 pi[1] = n;
77 pi[k+2] = 0xdead3344;
78 p = &pi[2];
drh8c1238a2003-01-02 14:43:55 +000079 memset(p, bZero==0, n);
drhc3c2fc92000-05-31 22:58:39 +000080#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +000081 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
82 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +000083#endif
drhdcc581c2000-05-30 13:44:19 +000084 return p;
85}
86
87/*
drhed6c8672003-01-12 18:02:16 +000088** Check to see if the given pointer was obtained from sqliteMalloc()
89** and is able to hold at least N bytes. Raise an exception if this
90** is not the case.
91**
92** This routine is used for testing purposes only.
93*/
94void sqliteCheckMemory(void *p, int N){
95 int *pi = p;
96 int n, k;
97 pi -= 2;
98 assert( pi[0]==0xdead1122 );
99 n = pi[1];
100 assert( N>=0 && N<n );
101 k = (n+sizeof(int)-1)/sizeof(int);
102 assert( pi[k+2]==0xdead3344 );
103}
104
105/*
drhdcc581c2000-05-30 13:44:19 +0000106** Free memory previously obtained from sqliteMalloc()
107*/
108void sqliteFree_(void *p, char *zFile, int line){
109 if( p ){
110 int *pi, k, n;
111 pi = p;
112 pi -= 2;
drh6e142f52000-06-08 13:36:40 +0000113 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +0000114 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000115 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000116 return;
117 }
118 n = pi[1];
119 k = (n+sizeof(int)-1)/sizeof(int);
120 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000121 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000122 return;
123 }
drhc3c2fc92000-05-31 22:58:39 +0000124 memset(pi, 0xff, (k+3)*sizeof(int));
125#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000126 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
127 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000128#endif
drhdcc581c2000-05-30 13:44:19 +0000129 free(pi);
130 }
131}
132
133/*
134** Resize a prior allocation. If p==0, then this routine
135** works just like sqliteMalloc(). If n==0, then this routine
136** works just like sqliteFree().
137*/
138void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
139 int *oldPi, *pi, k, oldN, oldK;
140 void *p;
141 if( oldP==0 ){
drh8c1238a2003-01-02 14:43:55 +0000142 return sqliteMalloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000143 }
144 if( n==0 ){
145 sqliteFree_(oldP,zFile,line);
146 return 0;
147 }
148 oldPi = oldP;
149 oldPi -= 2;
150 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000151 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000152 return 0;
drhdcc581c2000-05-30 13:44:19 +0000153 }
154 oldN = oldPi[1];
155 oldK = (oldN+sizeof(int)-1)/sizeof(int);
156 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000157 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000158 return 0;
drhdcc581c2000-05-30 13:44:19 +0000159 }
160 k = (n + sizeof(int) - 1)/sizeof(int);
161 pi = malloc( (k+3)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000162 if( pi==0 ){
163 sqlite_malloc_failed++;
164 return 0;
165 }
drhdcc581c2000-05-30 13:44:19 +0000166 pi[0] = 0xdead1122;
167 pi[1] = n;
168 pi[k+2] = 0xdead3344;
169 p = &pi[2];
170 memcpy(p, oldP, n>oldN ? oldN : n);
171 if( n>oldN ){
172 memset(&((char*)p)[oldN], 0, n-oldN);
173 }
drh8c1238a2003-01-02 14:43:55 +0000174 memset(oldPi, 0xab, (oldK+3)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000175 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000176#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000177 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
178 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000179#endif
drhdcc581c2000-05-30 13:44:19 +0000180 return p;
181}
drhc3c2fc92000-05-31 22:58:39 +0000182
183/*
184** Make a duplicate of a string into memory obtained from malloc()
185** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000186**
187** This routine is called on all strings that are passed outside of
188** the SQLite library. That way clients can free the string using free()
189** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000190*/
191void sqliteStrRealloc(char **pz){
192 char *zNew;
193 if( pz==0 || *pz==0 ) return;
194 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000195 if( zNew==0 ){
196 sqlite_malloc_failed++;
197 sqliteFree(*pz);
198 *pz = 0;
199 }
200 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000201 sqliteFree(*pz);
202 *pz = zNew;
203}
204
drh6e142f52000-06-08 13:36:40 +0000205/*
206** Make a copy of a string in memory obtained from sqliteMalloc()
207*/
208char *sqliteStrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000209 char *zNew;
210 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000211 zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000212 if( zNew ) strcpy(zNew, z);
213 return zNew;
214}
215char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000216 char *zNew;
217 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000218 zNew = sqliteMalloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000219 if( zNew ){
220 memcpy(zNew, z, n);
221 zNew[n] = 0;
222 }
223 return zNew;
224}
drh7c68d602000-10-11 19:28:51 +0000225#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000226
drh7c68d602000-10-11 19:28:51 +0000227/*
228** The following versions of malloc() and free() are for use in a
229** normal build.
230*/
231#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000232
drh75897232000-05-29 14:26:00 +0000233/*
234** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000235** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000236*/
237void *sqliteMalloc(int n){
drh26780582002-10-20 15:46:22 +0000238 void *p;
239 if( n==0 ) return 0;
240 p = malloc(n);
drhdaffd0e2001-04-11 14:28:42 +0000241 if( p==0 ){
242 sqlite_malloc_failed++;
243 return 0;
244 }
drh75897232000-05-29 14:26:00 +0000245 memset(p, 0, n);
246 return p;
247}
248
249/*
drh8c1238a2003-01-02 14:43:55 +0000250** Allocate new memory but do not set it to zero. Return NULL if
251** no memory is available. See also sqliteMalloc().
252*/
253void *sqliteMallocRaw(int n){
254 void *p;
255 if( n==0 ) return 0;
256 p = malloc(n);
257 if( p==0 ){
258 sqlite_malloc_failed++;
259 return 0;
260 }
261 return p;
262}
263
264/*
drh75897232000-05-29 14:26:00 +0000265** Free memory previously obtained from sqliteMalloc()
266*/
267void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000268 if( p ){
drh305cea62000-05-29 17:44:25 +0000269 free(p);
270 }
drh75897232000-05-29 14:26:00 +0000271}
272
273/*
274** Resize a prior allocation. If p==0, then this routine
275** works just like sqliteMalloc(). If n==0, then this routine
276** works just like sqliteFree().
277*/
278void *sqliteRealloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000279 void *p2;
drh75897232000-05-29 14:26:00 +0000280 if( p==0 ){
281 return sqliteMalloc(n);
282 }
283 if( n==0 ){
284 sqliteFree(p);
285 return 0;
286 }
drh6d4abfb2001-10-22 02:58:08 +0000287 p2 = realloc(p, n);
288 if( p2==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000289 sqlite_malloc_failed++;
290 }
drh6d4abfb2001-10-22 02:58:08 +0000291 return p2;
drh75897232000-05-29 14:26:00 +0000292}
drh6e142f52000-06-08 13:36:40 +0000293
294/*
295** Make a copy of a string in memory obtained from sqliteMalloc()
296*/
297char *sqliteStrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000298 char *zNew;
299 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000300 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000301 if( zNew ) strcpy(zNew, z);
302 return zNew;
303}
304char *sqliteStrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000305 char *zNew;
306 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000307 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000308 if( zNew ){
309 memcpy(zNew, z, n);
310 zNew[n] = 0;
311 }
312 return zNew;
313}
drh7c68d602000-10-11 19:28:51 +0000314#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000315
316/*
317** Create a string from the 2nd and subsequent arguments (up to the
318** first NULL argument), store the string in memory obtained from
319** sqliteMalloc() and make the pointer indicated by the 1st argument
320** point to that string.
321*/
322void sqliteSetString(char **pz, const char *zFirst, ...){
323 va_list ap;
324 int nByte;
325 const char *z;
326 char *zResult;
327
328 if( pz==0 ) return;
329 nByte = strlen(zFirst) + 1;
330 va_start(ap, zFirst);
331 while( (z = va_arg(ap, const char*))!=0 ){
332 nByte += strlen(z);
333 }
334 va_end(ap);
335 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000336 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000337 if( zResult==0 ){
338 return;
339 }
drh75897232000-05-29 14:26:00 +0000340 strcpy(zResult, zFirst);
341 zResult += strlen(zResult);
342 va_start(ap, zFirst);
343 while( (z = va_arg(ap, const char*))!=0 ){
344 strcpy(zResult, z);
345 zResult += strlen(zResult);
346 }
347 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000348#ifdef MEMORY_DEBUG
349#if MEMORY_DEBUG>1
350 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
351#endif
352#endif
drh75897232000-05-29 14:26:00 +0000353}
354
355/*
356** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000357** a length integer which specifies how much of the source string
358** to copy (in bytes). -1 means use the whole string.
drh75897232000-05-29 14:26:00 +0000359*/
360void sqliteSetNString(char **pz, ...){
361 va_list ap;
362 int nByte;
363 const char *z;
364 char *zResult;
365 int n;
366
367 if( pz==0 ) return;
368 nByte = 0;
369 va_start(ap, pz);
370 while( (z = va_arg(ap, const char*))!=0 ){
371 n = va_arg(ap, int);
372 if( n<=0 ) n = strlen(z);
373 nByte += n;
374 }
375 va_end(ap);
376 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000377 *pz = zResult = sqliteMallocRaw( nByte + 1 );
drh75897232000-05-29 14:26:00 +0000378 if( zResult==0 ) return;
379 va_start(ap, pz);
380 while( (z = va_arg(ap, const char*))!=0 ){
381 n = va_arg(ap, int);
382 if( n<=0 ) n = strlen(z);
383 strncpy(zResult, z, n);
384 zResult += n;
385 }
386 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000387#ifdef MEMORY_DEBUG
388#if MEMORY_DEBUG>1
389 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
390#endif
391#endif
drh75897232000-05-29 14:26:00 +0000392 va_end(ap);
393}
394
drh982cef72000-05-30 16:27:03 +0000395/*
396** Convert an SQL-style quoted string into a normal string by removing
397** the quote characters. The conversion is done in-place. If the
398** input does not begin with a quote character, then this routine
399** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000400**
401** 2002-Feb-14: This routine is extended to remove MS-Access style
402** brackets from around identifers. For example: "[a-b-c]" becomes
403** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000404*/
405void sqliteDequote(char *z){
406 int quote;
407 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000408 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000409 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000410 switch( quote ){
411 case '\'': break;
412 case '"': break;
413 case '[': quote = ']'; break;
414 default: return;
415 }
drh982cef72000-05-30 16:27:03 +0000416 for(i=1, j=0; z[i]; i++){
417 if( z[i]==quote ){
418 if( z[i+1]==quote ){
419 z[j++] = quote;
420 i++;
421 }else{
422 z[j++] = 0;
423 break;
424 }
425 }else{
426 z[j++] = z[i];
427 }
428 }
429}
430
drh75897232000-05-29 14:26:00 +0000431/* An array to map all upper-case characters into their corresponding
432** lower-case character.
433*/
434static unsigned char UpperToLower[] = {
435 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
436 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
437 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
438 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
439 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
440 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
441 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
442 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
443 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
444 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
445 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
446 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
447 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
448 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
449 252,253,254,255
450};
451
452/*
453** This function computes a hash on the name of a keyword.
454** Case is not significant.
455*/
456int sqliteHashNoCase(const char *z, int n){
457 int h = 0;
drh75897232000-05-29 14:26:00 +0000458 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000459 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000460 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000461 n--;
drh75897232000-05-29 14:26:00 +0000462 }
463 if( h<0 ) h = -h;
464 return h;
465}
466
467/*
drh967e8b72000-06-21 13:59:10 +0000468** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000469** there is no consistency, we will define our own.
470*/
471int sqliteStrICmp(const char *zLeft, const char *zRight){
472 register unsigned char *a, *b;
473 a = (unsigned char *)zLeft;
474 b = (unsigned char *)zRight;
475 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
476 return *a - *b;
477}
478int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
479 register unsigned char *a, *b;
480 a = (unsigned char *)zLeft;
481 b = (unsigned char *)zRight;
482 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000483 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000484}
485
drha9e99ae2002-08-13 23:02:57 +0000486#if 0 /* NOT USED */
drh7a7c7392001-11-24 00:31:46 +0000487/*
488** The sortStrCmp() function below is used to order elements according
489** to the ORDER BY clause of a SELECT. The sort order is a little different
490** from what one might expect. This note attempts to describe what is
491** going on.
drh75897232000-05-29 14:26:00 +0000492**
493** We want the main string comparision function used for sorting to
494** sort both numbers and alphanumeric words into the correct sequence.
495** The same routine should do both without prior knowledge of which
496** type of text the input represents. It should even work for strings
drh7a7c7392001-11-24 00:31:46 +0000497** which are a mixture of text and numbers. (It does not work for
498** numeric substrings in exponential notation, however.)
drh75897232000-05-29 14:26:00 +0000499**
500** To accomplish this, we keep track of a state number while scanning
501** the two strings. The states are as follows:
502**
503** 1 Beginning of word
504** 2 Arbitrary text
505** 3 Integer
506** 4 Negative integer
507** 5 Real number
508** 6 Negative real
509**
510** The scan begins in state 1, beginning of word. Transitions to other
511** states are determined by characters seen, as shown in the following
512** chart:
513**
514** Current State Character Seen New State
515** -------------------- -------------- -------------------
516** 0 Beginning of word "-" 3 Negative integer
517** digit 2 Integer
518** space 0 Beginning of word
519** otherwise 1 Arbitrary text
520**
521** 1 Arbitrary text space 0 Beginning of word
522** digit 2 Integer
523** otherwise 1 Arbitrary text
524**
525** 2 Integer space 0 Beginning of word
526** "." 4 Real number
527** digit 2 Integer
528** otherwise 1 Arbitrary text
529**
530** 3 Negative integer space 0 Beginning of word
531** "." 5 Negative Real num
532** digit 3 Negative integer
533** otherwise 1 Arbitrary text
534**
535** 4 Real number space 0 Beginning of word
536** digit 4 Real number
537** otherwise 1 Arbitrary text
538**
539** 5 Negative real num space 0 Beginning of word
540** digit 5 Negative real num
541** otherwise 1 Arbitrary text
542**
543** To implement this state machine, we first classify each character
544** into on of the following categories:
545**
546** 0 Text
547** 1 Space
548** 2 Digit
549** 3 "-"
550** 4 "."
551**
552** Given an arbitrary character, the array charClass[] maps that character
553** into one of the atove categories.
554*/
555static const unsigned char charClass[] = {
556 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
557/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
558/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
559/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
560/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
561/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
562/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
563/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
564/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
565/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
566/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
567/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
568/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
569/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
570/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
571/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
572/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
573};
574#define N_CHAR_CLASS 5
575
576/*
577** Given the current state number (0 thru 5), this array figures
578** the new state number given the character class.
579*/
580static const unsigned char stateMachine[] = {
581 /* Text, Space, Digit, "-", "." */
582 1, 0, 2, 3, 1, /* State 0: Beginning of word */
583 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
584 1, 0, 2, 1, 4, /* State 2: Integer */
585 1, 0, 3, 1, 5, /* State 3: Negative integer */
586 1, 0, 4, 1, 1, /* State 4: Real number */
587 1, 0, 5, 1, 1, /* State 5: Negative real num */
588};
589
590/* This routine does a comparison of two strings. Case is used only
drh7a7c7392001-11-24 00:31:46 +0000591** if useCase!=0. Numeric substrings compare in numerical order for the
592** most part but this routine does not understand exponential notation.
drh75897232000-05-29 14:26:00 +0000593*/
drh7a7c7392001-11-24 00:31:46 +0000594static int sortStrCmp(const char *atext, const char *btext, int useCase){
drh75897232000-05-29 14:26:00 +0000595 register unsigned char *a, *b, *map, ca, cb;
596 int result;
597 register int cclass = 0;
598
599 a = (unsigned char *)atext;
600 b = (unsigned char *)btext;
601 if( useCase ){
602 do{
603 if( (ca= *a++)!=(cb= *b++) ) break;
604 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
605 }while( ca!=0 );
606 }else{
607 map = UpperToLower;
608 do{
609 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
610 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
611 }while( ca!=0 );
drh92086432002-01-22 14:11:29 +0000612 if( ca>='[' && ca<='`' ) cb = b[-1];
613 if( cb>='[' && cb<='`' ) ca = a[-1];
drh75897232000-05-29 14:26:00 +0000614 }
615 switch( cclass ){
616 case 0:
617 case 1: {
618 if( isdigit(ca) && isdigit(cb) ){
619 cclass = 2;
620 }
621 break;
622 }
623 default: {
624 break;
625 }
626 }
627 switch( cclass ){
628 case 2:
629 case 3: {
630 if( isdigit(ca) ){
631 if( isdigit(cb) ){
632 int acnt, bcnt;
633 acnt = bcnt = 0;
634 while( isdigit(*a++) ) acnt++;
635 while( isdigit(*b++) ) bcnt++;
636 result = acnt - bcnt;
637 if( result==0 ) result = ca-cb;
638 }else{
639 result = 1;
640 }
641 }else if( isdigit(cb) ){
642 result = -1;
643 }else if( ca=='.' ){
644 result = 1;
645 }else if( cb=='.' ){
646 result = -1;
647 }else{
648 result = ca - cb;
649 cclass = 2;
650 }
651 if( cclass==3 ) result = -result;
652 break;
653 }
654 case 0:
655 case 1:
656 case 4: {
657 result = ca - cb;
658 break;
659 }
660 case 5: {
661 result = cb - ca;
662 };
663 }
664 return result;
665}
drha9e99ae2002-08-13 23:02:57 +0000666#endif /* NOT USED */
drh75897232000-05-29 14:26:00 +0000667
drha5c2ad02000-09-14 01:21:10 +0000668/*
drh7a7c7392001-11-24 00:31:46 +0000669** Return TRUE if z is a pure numeric string. Return FALSE if the
670** string contains any character which is not part of a number.
671**
672** Am empty string is considered numeric.
drha5c2ad02000-09-14 01:21:10 +0000673*/
drhe6840902002-03-06 03:08:25 +0000674static int sqliteIsNumber(const char *z){
drh7a7c7392001-11-24 00:31:46 +0000675 if( *z=='-' || *z=='+' ) z++;
676 if( !isdigit(*z) ){
677 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000678 }
drh7a7c7392001-11-24 00:31:46 +0000679 z++;
680 while( isdigit(*z) ){ z++; }
681 if( *z=='.' ){
682 z++;
683 if( !isdigit(*z) ) return 0;
684 while( isdigit(*z) ){ z++; }
685 if( *z=='e' || *z=='E' ){
686 z++;
687 if( *z=='+' || *z=='-' ) z++;
688 if( !isdigit(*z) ) return 0;
689 while( isdigit(*z) ){ z++; }
690 }
drha5c2ad02000-09-14 01:21:10 +0000691 }
drh7a7c7392001-11-24 00:31:46 +0000692 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000693}
694
drh75897232000-05-29 14:26:00 +0000695/* This comparison routine is what we use for comparison operations
drha9e99ae2002-08-13 23:02:57 +0000696** between numeric values in an SQL expression. "Numeric" is a little
697** bit misleading here. What we mean is that the strings have a
698** type of "numeric" from the point of view of SQL. The strings
699** do not necessarily contain numbers. They could contain text.
drh7a7c7392001-11-24 00:31:46 +0000700**
drha9e99ae2002-08-13 23:02:57 +0000701** If the input strings both look like actual numbers then they
702** compare in numerical order. Numerical strings are always less
703** than non-numeric strings so if one input string looks like a
704** number and the other does not, then the one that looks like
705** a number is the smaller. Non-numeric strings compare in
706** lexigraphical order (the same order as strcmp()).
drh75897232000-05-29 14:26:00 +0000707*/
708int sqliteCompare(const char *atext, const char *btext){
709 int result;
drh0bce8352002-02-28 00:41:10 +0000710 int isNumA, isNumB;
711 if( atext==0 ){
drh8912d102002-05-26 21:34:58 +0000712 return -1;
drh0bce8352002-02-28 00:41:10 +0000713 }else if( btext==0 ){
714 return 1;
715 }
drhe6840902002-03-06 03:08:25 +0000716 isNumA = sqliteIsNumber(atext);
717 isNumB = sqliteIsNumber(btext);
drh7a7c7392001-11-24 00:31:46 +0000718 if( isNumA ){
719 if( !isNumB ){
720 result = -1;
721 }else{
722 double rA, rB;
723 rA = atof(atext);
724 rB = atof(btext);
725 if( rA<rB ){
726 result = -1;
727 }else if( rA>rB ){
728 result = +1;
729 }else{
730 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000731 }
732 }
drh7a7c7392001-11-24 00:31:46 +0000733 }else if( isNumB ){
734 result = +1;
735 }else {
736 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000737 }
drh7a7c7392001-11-24 00:31:46 +0000738 return result;
drh75897232000-05-29 14:26:00 +0000739}
drh75897232000-05-29 14:26:00 +0000740
741/*
drh16e59552000-07-31 11:57:37 +0000742** This routine is used for sorting. Each key is a list of one or more
drha9e99ae2002-08-13 23:02:57 +0000743** null-terminated elements. The list is terminated by two nulls in
744** a row. For example, the following text is a key with three elements
drh75897232000-05-29 14:26:00 +0000745**
drha9e99ae2002-08-13 23:02:57 +0000746** Aone\000Dtwo\000Athree\000\000
drh75897232000-05-29 14:26:00 +0000747**
drhda30d362002-08-26 19:55:07 +0000748** All elements begin with one of the characters "+-AD" and end with "\000"
749** with zero or more text elements in between. Except, NULL elements
750** consist of the special two-character sequence "N\000".
751**
drha9e99ae2002-08-13 23:02:57 +0000752** Both arguments will have the same number of elements. This routine
drh75897232000-05-29 14:26:00 +0000753** returns negative, zero, or positive if the first argument is less
754** than, equal to, or greater than the first. (Result is a-b).
755**
drha9e99ae2002-08-13 23:02:57 +0000756** Each element begins with one of the characters "+", "-", "A", "D".
drh38640e12002-07-05 21:42:36 +0000757** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000758**
drh38640e12002-07-05 21:42:36 +0000759** + Sort numerically in ascending order
760** - Sort numerically in descending order
761** A Sort as strings in ascending order
762** D Sort as strings in descending order.
763**
764** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000765** isNum() function above returns TRUE) always compare less than strings
drha9e99ae2002-08-13 23:02:57 +0000766** that are not pure numerics. Non-numeric strings compare in memcmp()
767** order. This is the same sort order as the sqliteCompare() function
768** above generates.
drh7a7c7392001-11-24 00:31:46 +0000769**
drha9e99ae2002-08-13 23:02:57 +0000770** The last point is a change from version 2.6.3 to version 2.7.0. In
771** version 2.6.3 and earlier, substrings of digits compare in numerical
772** and case was used only to break a tie.
773**
774** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
775** of whether or not they look like a number.
776**
777** Note that the sort order imposed by the rules above is the same
drh7a7c7392001-11-24 00:31:46 +0000778** from the ordering defined by the "<", "<=", ">", and ">=" operators
drha9e99ae2002-08-13 23:02:57 +0000779** of expressions and for indices. This was not the case for version
780** 2.6.3 and earlier.
drh75897232000-05-29 14:26:00 +0000781*/
782int sqliteSortCompare(const char *a, const char *b){
783 int len;
784 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000785 int isNumA, isNumB;
drh294fb922002-09-30 01:31:21 +0000786 int dir = 0;
drh75897232000-05-29 14:26:00 +0000787
788 while( res==0 && *a && *b ){
drhda30d362002-08-26 19:55:07 +0000789 if( a[0]=='N' || b[0]=='N' ){
790 if( a[0]==b[0] ){
791 a += 2;
792 b += 2;
793 continue;
794 }
795 if( a[0]=='N' ){
796 dir = b[0];
797 res = -1;
798 }else{
799 dir = a[0];
800 res = +1;
801 }
drhf570f012002-05-31 15:51:25 +0000802 break;
803 }
drhda30d362002-08-26 19:55:07 +0000804 assert( a[0]==b[0] );
805 if( (dir=a[0])=='A' || a[0]=='D' ){
drh38640e12002-07-05 21:42:36 +0000806 res = strcmp(&a[1],&b[1]);
807 if( res ) break;
808 }else{
809 isNumA = sqliteIsNumber(&a[1]);
810 isNumB = sqliteIsNumber(&b[1]);
811 if( isNumA ){
812 double rA, rB;
813 if( !isNumB ){
814 res = -1;
815 break;
816 }
817 rA = atof(&a[1]);
818 rB = atof(&b[1]);
819 if( rA<rB ){
820 res = -1;
821 break;
822 }
823 if( rA>rB ){
824 res = +1;
825 break;
826 }
827 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000828 res = +1;
829 break;
drh38640e12002-07-05 21:42:36 +0000830 }else{
drha9e99ae2002-08-13 23:02:57 +0000831 res = strcmp(&a[1],&b[1]);
832 if( res ) break;
drh7a7c7392001-11-24 00:31:46 +0000833 }
drh75897232000-05-29 14:26:00 +0000834 }
drh7a7c7392001-11-24 00:31:46 +0000835 len = strlen(&a[1]) + 2;
836 a += len;
837 b += len;
drh75897232000-05-29 14:26:00 +0000838 }
drhda30d362002-08-26 19:55:07 +0000839 if( dir=='-' || dir=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000840 return res;
841}
drhdce2cbe2000-05-31 02:27:49 +0000842
drh9bbca4c2001-11-06 04:00:18 +0000843/*
drh7a7c7392001-11-24 00:31:46 +0000844** Some powers of 64. These constants are needed in the
845** sqliteRealToSortable() routine below.
drh9bbca4c2001-11-06 04:00:18 +0000846*/
847#define _64e3 (64.0 * 64.0 * 64.0)
848#define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
849#define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
850#define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
851#define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
852#define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
853
854/*
855** The following procedure converts a double-precision floating point
856** number into a string. The resulting string has the property that
857** two such strings comparied using strcmp() or memcmp() will give the
drh5a2c2c22001-11-21 02:21:11 +0000858** same results as a numeric comparison of the original floating point
859** numbers.
drh9bbca4c2001-11-06 04:00:18 +0000860**
861** This routine is used to generate database keys from floating point
862** numbers such that the keys sort in the same order as the original
863** floating point numbers even though the keys are compared using
864** memcmp().
865**
866** The calling function should have allocated at least 14 characters
867** of space for the buffer z[].
868*/
869void sqliteRealToSortable(double r, char *z){
870 int neg;
871 int exp;
872 int cnt = 0;
873
874 /* This array maps integers between 0 and 63 into base-64 digits.
875 ** The digits must be chosen such at their ASCII codes are increasing.
876 ** This means we can not use the traditional base-64 digit set. */
877 static const char zDigit[] =
878 "0123456789"
879 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
880 "abcdefghijklmnopqrstuvwxyz"
881 "|~";
882 if( r<0.0 ){
883 neg = 1;
884 r = -r;
885 *z++ = '-';
886 } else {
887 neg = 0;
888 *z++ = '0';
889 }
890 exp = 0;
891
892 if( r==0.0 ){
893 exp = -1024;
894 }else if( r<(0.5/64.0) ){
895 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
896 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
897 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
898 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
899 }else if( r>=0.5 ){
900 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
901 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
902 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
903 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
904 }
905 if( neg ){
906 exp = -exp;
907 r = -r;
908 }
909 exp += 1024;
910 r += 0.5;
911 if( exp<0 ) return;
912 if( exp>=2048 || r>=1.0 ){
913 strcpy(z, "~~~~~~~~~~~~");
914 return;
915 }
916 *z++ = zDigit[(exp>>6)&0x3f];
917 *z++ = zDigit[exp & 0x3f];
918 while( r>0.0 && cnt<10 ){
919 int digit;
920 r *= 64.0;
drh1ab43002002-01-14 09:28:19 +0000921 digit = (int)r;
drh9bbca4c2001-11-06 04:00:18 +0000922 assert( digit>=0 && digit<64 );
923 *z++ = zDigit[digit & 0x3f];
924 r -= digit;
925 cnt++;
926 }
927 *z = 0;
928}
929
drh297ecf12001-04-05 15:57:13 +0000930#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000931/*
drh297ecf12001-04-05 15:57:13 +0000932** X is a pointer to the first byte of a UTF-8 character. Increment
933** X so that it points to the next character. This only works right
934** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000935*/
drh297ecf12001-04-05 15:57:13 +0000936#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
937#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000938
drh297ecf12001-04-05 15:57:13 +0000939#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000940/*
drh297ecf12001-04-05 15:57:13 +0000941** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000942*/
drh297ecf12001-04-05 15:57:13 +0000943#define sqliteNextChar(X) (++(X));
944#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000945
drh297ecf12001-04-05 15:57:13 +0000946#endif /* defined(SQLITE_UTF8) */
947
948
949#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000950/*
drh297ecf12001-04-05 15:57:13 +0000951** Convert the UTF-8 character to which z points into a 31-bit
952** UCS character. This only works right if z points to a well-formed
953** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000954*/
drh297ecf12001-04-05 15:57:13 +0000955static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000956 int c;
drh297ecf12001-04-05 15:57:13 +0000957 static const int initVal[] = {
958 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
959 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
960 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
961 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
962 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
963 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
964 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
965 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
966 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
967 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
968 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
969 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
970 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
971 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
972 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
973 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
974 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
975 255,
976 };
977 c = initVal[*(z++)];
978 while( (0xc0&*z)==0x80 ){
979 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000980 }
981 return c;
982}
drh297ecf12001-04-05 15:57:13 +0000983#endif
drhe17a7e32001-04-04 21:10:18 +0000984
985/*
986** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000987** potentially be a "glob" expression. Return true (1) if they
988** are the same and false (0) if they are different.
989**
990** Globbing rules:
991**
992** '*' Matches any sequence of zero or more characters.
993**
994** '?' Matches exactly one character.
995**
996** [...] Matches one character from the enclosed list of
997** characters.
998**
999** [^...] Matches one character not in the enclosed list.
1000**
1001** With the [...] and [^...] matching, a ']' character can be included
1002** in the list by making it the first character after '[' or '^'. A
1003** range of characters can be specified using '-'. Example:
1004** "[a-z]" matches any single lower-case letter. To match a '-', make
1005** it the last character in the list.
1006**
1007** This routine is usually quick, but can be N**2 in the worst case.
1008**
1009** Hints: to match '*' or '?', put them in "[]". Like this:
1010**
1011** abc[*]xyz Matches "abc*xyz" only
1012*/
drhe17a7e32001-04-04 21:10:18 +00001013int
1014sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
1015 register int c;
drhdce2cbe2000-05-31 02:27:49 +00001016 int invert;
1017 int seen;
drhe17a7e32001-04-04 21:10:18 +00001018 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001019
1020 while( (c = *zPattern)!=0 ){
1021 switch( c ){
1022 case '*':
drhe17a7e32001-04-04 21:10:18 +00001023 while( (c=zPattern[1]) == '*' || c == '?' ){
1024 if( c=='?' ){
1025 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001026 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001027 }
1028 zPattern++;
1029 }
1030 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +00001031 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +00001032 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +00001033 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001034 }
1035 return *zString!=0;
1036 }else{
1037 while( (c2 = *zString)!=0 ){
1038 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +00001039 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +00001040 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001041 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001042 }
1043 return 0;
1044 }
drhe17a7e32001-04-04 21:10:18 +00001045 case '?': {
drhdce2cbe2000-05-31 02:27:49 +00001046 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001047 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001048 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001049 break;
drhe17a7e32001-04-04 21:10:18 +00001050 }
1051 case '[': {
1052 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001053 seen = 0;
1054 invert = 0;
drh297ecf12001-04-05 15:57:13 +00001055 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +00001056 if( c==0 ) return 0;
1057 c2 = *++zPattern;
1058 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1059 if( c2==']' ){
1060 if( c==']' ) seen = 1;
1061 c2 = *++zPattern;
1062 }
drh297ecf12001-04-05 15:57:13 +00001063 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +00001064 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1065 zPattern++;
drh297ecf12001-04-05 15:57:13 +00001066 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +00001067 if( c>=prior_c && c<=c2 ) seen = 1;
1068 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001069 }else if( c==c2 ){
1070 seen = 1;
drhe17a7e32001-04-04 21:10:18 +00001071 prior_c = c2;
1072 }else{
1073 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001074 }
drh297ecf12001-04-05 15:57:13 +00001075 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001076 }
1077 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001078 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001079 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001080 break;
drhe17a7e32001-04-04 21:10:18 +00001081 }
1082 default: {
drhdce2cbe2000-05-31 02:27:49 +00001083 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001084 zPattern++;
1085 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001086 break;
drhe17a7e32001-04-04 21:10:18 +00001087 }
drhdce2cbe2000-05-31 02:27:49 +00001088 }
drhdce2cbe2000-05-31 02:27:49 +00001089 }
1090 return *zString==0;
1091}
1092
1093/*
drhe17a7e32001-04-04 21:10:18 +00001094** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001095** SQL. The '%' character matches any sequence of 0 or more
1096** characters and '_' matches any single character. Case is
1097** not significant.
1098**
1099** This routine is just an adaptation of the sqliteGlobCompare()
1100** routine above.
1101*/
1102int
1103sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001104 register int c;
1105 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001106
1107 while( (c = UpperToLower[*zPattern])!=0 ){
1108 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001109 case '%': {
1110 while( (c=zPattern[1]) == '%' || c == '_' ){
1111 if( c=='_' ){
1112 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001113 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001114 }
drhe17a7e32001-04-04 21:10:18 +00001115 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001116 }
drhe17a7e32001-04-04 21:10:18 +00001117 if( c==0 ) return 1;
1118 c = UpperToLower[c];
1119 while( (c2=UpperToLower[*zString])!=0 ){
1120 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1121 if( c2==0 ) return 0;
1122 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001123 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001124 }
1125 return 0;
1126 }
1127 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001128 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001129 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001130 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001131 break;
drhe17a7e32001-04-04 21:10:18 +00001132 }
1133 default: {
drhdce2cbe2000-05-31 02:27:49 +00001134 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001135 zPattern++;
1136 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001137 break;
drhe17a7e32001-04-04 21:10:18 +00001138 }
drhdce2cbe2000-05-31 02:27:49 +00001139 }
drhdce2cbe2000-05-31 02:27:49 +00001140 }
1141 return *zString==0;
1142}
drh5e00f6c2001-09-13 13:46:56 +00001143
1144/*
drhc22bd472002-05-10 13:14:07 +00001145** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1146** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1147** when this routine is called.
1148**
1149** This routine is a attempt to detect if two threads use the
1150** same sqlite* pointer at the same time. There is a race
1151** condition so it is possible that the error is not detected.
1152** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001153** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001154** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +00001155**
1156** Ticket #202: If db->magic is not a valid open value, take care not
1157** to modify the db structure at all. It could be that db is a stale
1158** pointer. In other words, it could be that there has been a prior
1159** call to sqlite_close(db) and db has been deallocated. And we do
1160** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +00001161*/
drhc22bd472002-05-10 13:14:07 +00001162int sqliteSafetyOn(sqlite *db){
1163 if( db->magic==SQLITE_MAGIC_OPEN ){
1164 db->magic = SQLITE_MAGIC_BUSY;
1165 return 0;
drhe7e8bc72002-12-17 13:05:25 +00001166 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +00001167 db->magic = SQLITE_MAGIC_ERROR;
1168 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +00001169 }
drhe7e8bc72002-12-17 13:05:25 +00001170 return 1;
drhc22bd472002-05-10 13:14:07 +00001171}
1172
1173/*
1174** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1175** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1176** when this routine is called.
1177*/
1178int sqliteSafetyOff(sqlite *db){
1179 if( db->magic==SQLITE_MAGIC_BUSY ){
1180 db->magic = SQLITE_MAGIC_OPEN;
1181 return 0;
drhe7e8bc72002-12-17 13:05:25 +00001182 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +00001183 db->magic = SQLITE_MAGIC_ERROR;
1184 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +00001185 }
drhe7e8bc72002-12-17 13:05:25 +00001186 return 1;
drhc22bd472002-05-10 13:14:07 +00001187}
1188
1189/*
1190** Check to make sure we are not currently executing an sqlite_exec().
1191** If we are currently in an sqlite_exec(), return true and set
1192** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1193** shutdown of the database.
1194**
1195** This routine is used to try to detect when API routines are called
1196** at the wrong time or in the wrong sequence.
1197*/
1198int sqliteSafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +00001199 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +00001200 db->magic = SQLITE_MAGIC_ERROR;
1201 return 1;
1202 }
1203 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001204}