blob: af0f31cd1a73061f610b255cdb71b3092aaed0ca [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**
drh202b2df2004-01-06 01:13:46 +000017** $Id: util.c,v 1.70 2004/01/06 01:13:47 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
drh4305d102003-07-30 12:34:12 +000046/*
47** Number of 32-bit guard words
48*/
49#define N_GUARD 1
drh8c82b352000-12-10 18:23:50 +000050
51/*
drhdcc581c2000-05-30 13:44:19 +000052** Allocate new memory and set it to zero. Return NULL if
53** no memory is available.
54*/
drh8c1238a2003-01-02 14:43:55 +000055void *sqliteMalloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000056 void *p;
57 int *pi;
drh4305d102003-07-30 12:34:12 +000058 int i, k;
drh6e142f52000-06-08 13:36:40 +000059 if( sqlite_iMallocFail>=0 ){
60 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000061 if( sqlite_iMallocFail==0 ){
62 sqlite_malloc_failed++;
drh6d4abfb2001-10-22 02:58:08 +000063#if MEMORY_DEBUG>1
64 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
65 n, zFile,line);
66#endif
67 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000068 return 0;
69 }
drh6e142f52000-06-08 13:36:40 +000070 }
drhb0729502001-03-14 12:35:57 +000071 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000072 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +000073 pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000074 if( pi==0 ){
75 sqlite_malloc_failed++;
76 return 0;
77 }
drh6d4abfb2001-10-22 02:58:08 +000078 sqlite_nMalloc++;
drh4305d102003-07-30 12:34:12 +000079 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
80 pi[N_GUARD] = n;
81 for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
82 p = &pi[N_GUARD+1];
drh8c1238a2003-01-02 14:43:55 +000083 memset(p, bZero==0, n);
drhc3c2fc92000-05-31 22:58:39 +000084#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +000085 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
86 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +000087#endif
drhdcc581c2000-05-30 13:44:19 +000088 return p;
89}
90
91/*
drhed6c8672003-01-12 18:02:16 +000092** Check to see if the given pointer was obtained from sqliteMalloc()
93** and is able to hold at least N bytes. Raise an exception if this
94** is not the case.
95**
96** This routine is used for testing purposes only.
97*/
98void sqliteCheckMemory(void *p, int N){
99 int *pi = p;
drh4305d102003-07-30 12:34:12 +0000100 int n, i, k;
101 pi -= N_GUARD+1;
102 for(i=0; i<N_GUARD; i++){
103 assert( pi[i]==0xdead1122 );
104 }
105 n = pi[N_GUARD];
drhed6c8672003-01-12 18:02:16 +0000106 assert( N>=0 && N<n );
107 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000108 for(i=0; i<N_GUARD; i++){
109 assert( pi[k+N_GUARD+1+i]==0xdead3344 );
110 }
drhed6c8672003-01-12 18:02:16 +0000111}
112
113/*
drhdcc581c2000-05-30 13:44:19 +0000114** Free memory previously obtained from sqliteMalloc()
115*/
116void sqliteFree_(void *p, char *zFile, int line){
117 if( p ){
drh4305d102003-07-30 12:34:12 +0000118 int *pi, i, k, n;
drhdcc581c2000-05-30 13:44:19 +0000119 pi = p;
drh4305d102003-07-30 12:34:12 +0000120 pi -= N_GUARD+1;
drh6e142f52000-06-08 13:36:40 +0000121 sqlite_nFree++;
drh4305d102003-07-30 12:34:12 +0000122 for(i=0; i<N_GUARD; i++){
123 if( pi[i]!=0xdead1122 ){
124 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
125 return;
126 }
drhdcc581c2000-05-30 13:44:19 +0000127 }
drh4305d102003-07-30 12:34:12 +0000128 n = pi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000129 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000130 for(i=0; i<N_GUARD; i++){
131 if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
132 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
133 return;
134 }
drhdcc581c2000-05-30 13:44:19 +0000135 }
drh4305d102003-07-30 12:34:12 +0000136 memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
drhc3c2fc92000-05-31 22:58:39 +0000137#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000138 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
139 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000140#endif
drhdcc581c2000-05-30 13:44:19 +0000141 free(pi);
142 }
143}
144
145/*
146** Resize a prior allocation. If p==0, then this routine
147** works just like sqliteMalloc(). If n==0, then this routine
148** works just like sqliteFree().
149*/
150void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
drh4305d102003-07-30 12:34:12 +0000151 int *oldPi, *pi, i, k, oldN, oldK;
drhdcc581c2000-05-30 13:44:19 +0000152 void *p;
153 if( oldP==0 ){
drh8c1238a2003-01-02 14:43:55 +0000154 return sqliteMalloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000155 }
156 if( n==0 ){
157 sqliteFree_(oldP,zFile,line);
158 return 0;
159 }
160 oldPi = oldP;
drh4305d102003-07-30 12:34:12 +0000161 oldPi -= N_GUARD+1;
drhdcc581c2000-05-30 13:44:19 +0000162 if( oldPi[0]!=0xdead1122 ){
drh03ab7332003-08-26 11:29:07 +0000163 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
drh9bb61fe2000-06-05 16:01:39 +0000164 return 0;
drhdcc581c2000-05-30 13:44:19 +0000165 }
drh4305d102003-07-30 12:34:12 +0000166 oldN = oldPi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000167 oldK = (oldN+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000168 for(i=0; i<N_GUARD; i++){
169 if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
drh03ab7332003-08-26 11:29:07 +0000170 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
171 (int)oldP);
drh4305d102003-07-30 12:34:12 +0000172 return 0;
173 }
drhdcc581c2000-05-30 13:44:19 +0000174 }
175 k = (n + sizeof(int) - 1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000176 pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000177 if( pi==0 ){
178 sqlite_malloc_failed++;
179 return 0;
180 }
drh4305d102003-07-30 12:34:12 +0000181 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
182 pi[N_GUARD] = n;
183 for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
184 p = &pi[N_GUARD+1];
drhdcc581c2000-05-30 13:44:19 +0000185 memcpy(p, oldP, n>oldN ? oldN : n);
186 if( n>oldN ){
187 memset(&((char*)p)[oldN], 0, n-oldN);
188 }
drh4305d102003-07-30 12:34:12 +0000189 memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000190 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000191#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000192 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
193 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000194#endif
drhdcc581c2000-05-30 13:44:19 +0000195 return p;
196}
drhc3c2fc92000-05-31 22:58:39 +0000197
198/*
199** Make a duplicate of a string into memory obtained from malloc()
200** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000201**
202** This routine is called on all strings that are passed outside of
203** the SQLite library. That way clients can free the string using free()
204** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000205*/
206void sqliteStrRealloc(char **pz){
207 char *zNew;
208 if( pz==0 || *pz==0 ) return;
209 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000210 if( zNew==0 ){
211 sqlite_malloc_failed++;
212 sqliteFree(*pz);
213 *pz = 0;
214 }
215 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000216 sqliteFree(*pz);
217 *pz = zNew;
218}
219
drh6e142f52000-06-08 13:36:40 +0000220/*
221** Make a copy of a string in memory obtained from sqliteMalloc()
222*/
223char *sqliteStrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000224 char *zNew;
225 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000226 zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000227 if( zNew ) strcpy(zNew, z);
228 return zNew;
229}
230char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000231 char *zNew;
232 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000233 zNew = sqliteMalloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000234 if( zNew ){
235 memcpy(zNew, z, n);
236 zNew[n] = 0;
237 }
238 return zNew;
239}
drh7c68d602000-10-11 19:28:51 +0000240#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000241
drh7c68d602000-10-11 19:28:51 +0000242/*
243** The following versions of malloc() and free() are for use in a
244** normal build.
245*/
246#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000247
drh75897232000-05-29 14:26:00 +0000248/*
249** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000250** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000251*/
252void *sqliteMalloc(int n){
drh26780582002-10-20 15:46:22 +0000253 void *p;
drh8548a052003-10-22 22:15:27 +0000254 if( (p = malloc(n))==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000255 sqlite_malloc_failed++;
drh8548a052003-10-22 22:15:27 +0000256 }else{
257 memset(p, 0, n);
drhdaffd0e2001-04-11 14:28:42 +0000258 }
drh75897232000-05-29 14:26:00 +0000259 return p;
260}
261
262/*
drh8c1238a2003-01-02 14:43:55 +0000263** Allocate new memory but do not set it to zero. Return NULL if
264** no memory is available. See also sqliteMalloc().
265*/
266void *sqliteMallocRaw(int n){
267 void *p;
drh8548a052003-10-22 22:15:27 +0000268 if( (p = malloc(n))==0 ){
drh8c1238a2003-01-02 14:43:55 +0000269 sqlite_malloc_failed++;
drh8c1238a2003-01-02 14:43:55 +0000270 }
271 return p;
272}
273
274/*
drh75897232000-05-29 14:26:00 +0000275** Free memory previously obtained from sqliteMalloc()
276*/
277void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000278 if( p ){
drh305cea62000-05-29 17:44:25 +0000279 free(p);
280 }
drh75897232000-05-29 14:26:00 +0000281}
282
283/*
284** Resize a prior allocation. If p==0, then this routine
285** works just like sqliteMalloc(). If n==0, then this routine
286** works just like sqliteFree().
287*/
288void *sqliteRealloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000289 void *p2;
drh75897232000-05-29 14:26:00 +0000290 if( p==0 ){
291 return sqliteMalloc(n);
292 }
293 if( n==0 ){
294 sqliteFree(p);
295 return 0;
296 }
drh6d4abfb2001-10-22 02:58:08 +0000297 p2 = realloc(p, n);
298 if( p2==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000299 sqlite_malloc_failed++;
300 }
drh6d4abfb2001-10-22 02:58:08 +0000301 return p2;
drh75897232000-05-29 14:26:00 +0000302}
drh6e142f52000-06-08 13:36:40 +0000303
304/*
305** Make a copy of a string in memory obtained from sqliteMalloc()
306*/
307char *sqliteStrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000308 char *zNew;
309 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000310 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000311 if( zNew ) strcpy(zNew, z);
312 return zNew;
313}
314char *sqliteStrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000315 char *zNew;
316 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000317 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000318 if( zNew ){
319 memcpy(zNew, z, n);
320 zNew[n] = 0;
321 }
322 return zNew;
323}
drh7c68d602000-10-11 19:28:51 +0000324#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000325
326/*
327** Create a string from the 2nd and subsequent arguments (up to the
328** first NULL argument), store the string in memory obtained from
329** sqliteMalloc() and make the pointer indicated by the 1st argument
jplyon02be20d2003-06-02 06:17:10 +0000330** point to that string. The 1st argument must either be NULL or
331** point to memory obtained from sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000332*/
333void sqliteSetString(char **pz, const char *zFirst, ...){
334 va_list ap;
335 int nByte;
336 const char *z;
337 char *zResult;
338
339 if( pz==0 ) return;
340 nByte = strlen(zFirst) + 1;
341 va_start(ap, zFirst);
342 while( (z = va_arg(ap, const char*))!=0 ){
343 nByte += strlen(z);
344 }
345 va_end(ap);
346 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000347 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000348 if( zResult==0 ){
349 return;
350 }
drh75897232000-05-29 14:26:00 +0000351 strcpy(zResult, zFirst);
352 zResult += strlen(zResult);
353 va_start(ap, zFirst);
354 while( (z = va_arg(ap, const char*))!=0 ){
355 strcpy(zResult, z);
356 zResult += strlen(zResult);
357 }
358 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000359#ifdef MEMORY_DEBUG
360#if MEMORY_DEBUG>1
361 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
362#endif
363#endif
drh75897232000-05-29 14:26:00 +0000364}
365
366/*
367** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000368** a length integer which specifies how much of the source string
jplyon02be20d2003-06-02 06:17:10 +0000369** to copy (in bytes). -1 means use the whole string. The 1st
370** argument must either be NULL or point to memory obtained from
371** sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000372*/
373void sqliteSetNString(char **pz, ...){
374 va_list ap;
375 int nByte;
376 const char *z;
377 char *zResult;
378 int n;
379
380 if( pz==0 ) return;
381 nByte = 0;
382 va_start(ap, pz);
383 while( (z = va_arg(ap, const char*))!=0 ){
384 n = va_arg(ap, int);
385 if( n<=0 ) n = strlen(z);
386 nByte += n;
387 }
388 va_end(ap);
389 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000390 *pz = zResult = sqliteMallocRaw( nByte + 1 );
drh75897232000-05-29 14:26:00 +0000391 if( zResult==0 ) return;
392 va_start(ap, pz);
393 while( (z = va_arg(ap, const char*))!=0 ){
394 n = va_arg(ap, int);
395 if( n<=0 ) n = strlen(z);
396 strncpy(zResult, z, n);
397 zResult += n;
398 }
399 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000400#ifdef MEMORY_DEBUG
401#if MEMORY_DEBUG>1
402 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
403#endif
404#endif
drh75897232000-05-29 14:26:00 +0000405 va_end(ap);
406}
407
drh982cef72000-05-30 16:27:03 +0000408/*
drhda93d232003-03-31 02:12:46 +0000409** Add an error message to pParse->zErrMsg and increment pParse->nErr.
410** The following formatting characters are allowed:
411**
412** %s Insert a string
413** %z A string that should be freed after use
414** %d Insert an integer
415** %T Insert a token
416** %S Insert the first element of a SrcList
417*/
418void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){
419 va_list ap;
420 int nByte;
421 int i, j;
422 char *z;
423 static char zNull[] = "NULL";
424
425 pParse->nErr++;
426 nByte = 1 + strlen(zFormat);
427 va_start(ap, zFormat);
428 for(i=0; zFormat[i]; i++){
drh665de472003-03-31 13:36:09 +0000429 if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue;
drhda93d232003-03-31 02:12:46 +0000430 i++;
431 switch( zFormat[i] ){
432 case 'd': {
433 (void)va_arg(ap, int);
434 nByte += 20;
435 break;
436 }
437 case 'z':
438 case 's': {
439 char *z2 = va_arg(ap, char*);
440 if( z2==0 ) z2 = zNull;
441 nByte += strlen(z2);
442 break;
443 }
444 case 'T': {
445 Token *p = va_arg(ap, Token*);
446 nByte += p->n;
447 break;
448 }
449 case 'S': {
450 SrcList *p = va_arg(ap, SrcList*);
451 int k = va_arg(ap, int);
452 assert( p->nSrc>k && k>=0 );
453 nByte += strlen(p->a[k].zName);
454 if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
455 nByte += strlen(p->a[k].zDatabase)+1;
456 }
457 break;
458 }
459 default: {
460 nByte++;
461 break;
462 }
463 }
464 }
465 va_end(ap);
466 z = sqliteMalloc( nByte );
467 if( z==0 ) return;
468 sqliteFree(pParse->zErrMsg);
469 pParse->zErrMsg = z;
470 va_start(ap, zFormat);
471 for(i=j=0; zFormat[i]; i++){
drh665de472003-03-31 13:36:09 +0000472 if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue;
drhda93d232003-03-31 02:12:46 +0000473 if( i>j ){
474 memcpy(z, &zFormat[j], i-j);
475 z += i-j;
476 }
477 j = i+2;
478 i++;
479 switch( zFormat[i] ){
480 case 'd': {
481 int x = va_arg(ap, int);
482 sprintf(z, "%d", x);
483 z += strlen(z);
484 break;
485 }
486 case 'z':
487 case 's': {
488 int len;
489 char *z2 = va_arg(ap, char*);
490 if( z2==0 ) z2 = zNull;
491 len = strlen(z2);
492 memcpy(z, z2, len);
493 z += len;
494 if( zFormat[i]=='z' && z2!=zNull ){
495 sqliteFree(z2);
496 }
497 break;
498 }
499 case 'T': {
500 Token *p = va_arg(ap, Token*);
501 memcpy(z, p->z, p->n);
502 z += p->n;
503 break;
504 }
505 case 'S': {
506 int len;
507 SrcList *p = va_arg(ap, SrcList*);
508 int k = va_arg(ap, int);
509 assert( p->nSrc>k && k>=0 );
510 if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
511 len = strlen(p->a[k].zDatabase);
512 memcpy(z, p->a[k].zDatabase, len);
513 z += len;
514 *(z++) = '.';
515 }
516 len = strlen(p->a[k].zName);
517 memcpy(z, p->a[k].zName, len);
518 z += len;
519 break;
520 }
521 default: {
522 *(z++) = zFormat[i];
523 break;
524 }
525 }
526 }
527 va_end(ap);
528 if( i>j ){
529 memcpy(z, &zFormat[j], i-j);
530 z += i-j;
531 }
532 assert( (z - pParse->zErrMsg) < nByte );
533 *z = 0;
534}
535
536/*
drh982cef72000-05-30 16:27:03 +0000537** Convert an SQL-style quoted string into a normal string by removing
538** the quote characters. The conversion is done in-place. If the
539** input does not begin with a quote character, then this routine
540** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000541**
542** 2002-Feb-14: This routine is extended to remove MS-Access style
543** brackets from around identifers. For example: "[a-b-c]" becomes
544** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000545*/
546void sqliteDequote(char *z){
547 int quote;
548 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000549 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000550 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000551 switch( quote ){
552 case '\'': break;
553 case '"': break;
554 case '[': quote = ']'; break;
555 default: return;
556 }
drh982cef72000-05-30 16:27:03 +0000557 for(i=1, j=0; z[i]; i++){
558 if( z[i]==quote ){
559 if( z[i+1]==quote ){
560 z[j++] = quote;
561 i++;
562 }else{
563 z[j++] = 0;
564 break;
565 }
566 }else{
567 z[j++] = z[i];
568 }
569 }
570}
571
drh75897232000-05-29 14:26:00 +0000572/* An array to map all upper-case characters into their corresponding
573** lower-case character.
574*/
575static unsigned char UpperToLower[] = {
576 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
577 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
578 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
579 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
580 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
581 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
582 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
583 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
584 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
585 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
586 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
587 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
588 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
589 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
590 252,253,254,255
591};
592
593/*
594** This function computes a hash on the name of a keyword.
595** Case is not significant.
596*/
597int sqliteHashNoCase(const char *z, int n){
598 int h = 0;
drh75897232000-05-29 14:26:00 +0000599 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000600 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000601 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000602 n--;
drh75897232000-05-29 14:26:00 +0000603 }
drh5364f602003-05-12 23:06:52 +0000604 return h & 0x7fffffff;
drh75897232000-05-29 14:26:00 +0000605}
606
607/*
drh967e8b72000-06-21 13:59:10 +0000608** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000609** there is no consistency, we will define our own.
610*/
611int sqliteStrICmp(const char *zLeft, const char *zRight){
612 register unsigned char *a, *b;
613 a = (unsigned char *)zLeft;
614 b = (unsigned char *)zRight;
615 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
616 return *a - *b;
617}
618int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
619 register unsigned char *a, *b;
620 a = (unsigned char *)zLeft;
621 b = (unsigned char *)zRight;
622 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000623 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000624}
625
drha5c2ad02000-09-14 01:21:10 +0000626/*
drh7a7c7392001-11-24 00:31:46 +0000627** Return TRUE if z is a pure numeric string. Return FALSE if the
628** string contains any character which is not part of a number.
629**
drhbb07e9a2003-04-16 02:17:35 +0000630** Am empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000631*/
drhbb07e9a2003-04-16 02:17:35 +0000632int sqliteIsNumber(const char *z){
drh7a7c7392001-11-24 00:31:46 +0000633 if( *z=='-' || *z=='+' ) z++;
634 if( !isdigit(*z) ){
drhbb07e9a2003-04-16 02:17:35 +0000635 return 0;
drha5c2ad02000-09-14 01:21:10 +0000636 }
drh7a7c7392001-11-24 00:31:46 +0000637 z++;
638 while( isdigit(*z) ){ z++; }
639 if( *z=='.' ){
640 z++;
641 if( !isdigit(*z) ) return 0;
642 while( isdigit(*z) ){ z++; }
drhbb07e9a2003-04-16 02:17:35 +0000643 }
644 if( *z=='e' || *z=='E' ){
645 z++;
646 if( *z=='+' || *z=='-' ) z++;
647 if( !isdigit(*z) ) return 0;
648 while( isdigit(*z) ){ z++; }
drha5c2ad02000-09-14 01:21:10 +0000649 }
drh7a7c7392001-11-24 00:31:46 +0000650 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000651}
652
drh93a5c6b2003-12-23 02:17:35 +0000653/*
654** The string z[] is an ascii representation of a real number.
655** Convert this string to a double.
656**
657** This routine assumes that z[] really is a valid number. If it
658** is not, the result is undefined.
659**
660** This routine is used instead of the library atof() function because
661** the library atof() might want to use "," as the decimal point instead
662** of "." depending on how locale is set. But that would cause problems
663** for SQL. So this routine always uses "." regardless of locale.
664*/
665double sqliteAtoF(const char *z){
666 int sign = 1;
667 double v1 = 0.0;
668 if( *z=='-' ){
669 sign = -1;
670 z++;
671 }else if( *z=='+' ){
672 z++;
673 }
674 while( isdigit(*z) ){
675 v1 = v1*10.0 + (*z - '0');
676 z++;
677 }
678 if( *z=='.' ){
679 double divisor = 1.0;
680 z++;
681 while( isdigit(*z) ){
682 v1 = v1*10.0 + (*z - '0');
683 divisor *= 10.0;
684 z++;
685 }
686 v1 /= divisor;
687 }
688 if( *z=='e' || *z=='E' ){
689 int esign = 1;
690 int eval = 0;
691 double scale = 1.0;
692 z++;
693 if( *z=='-' ){
694 esign = -1;
695 z++;
696 }else if( *z=='+' ){
697 z++;
698 }
699 while( isdigit(*z) ){
700 eval = eval*10 + *z - '0';
701 z++;
702 }
703 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
704 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
705 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
706 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
707 if( esign<0 ){
708 v1 /= scale;
709 }else{
710 v1 *= scale;
711 }
712 }
713 return sign<0 ? -v1 : v1;
714}
715
drh202b2df2004-01-06 01:13:46 +0000716/*
717** The string zNum represents an integer. There might be some other
718** information following the integer too, but that part is ignored.
719** If the integer that the prefix of zNum represents will fit in a
720** 32-bit signed integer, return TRUE. Otherwise return FALSE.
721**
722** This routine returns FALSE for the string -2147483648 even that
723** that number will, in theory fit in a 32-bit integer. But positive
724** 2147483648 will not fit in 32 bits. So it seems safer to return
725** false.
726*/
727int sqliteFitsIn32Bits(const char *zNum){
728 int i, c;
729 if( *zNum=='-' || *zNum=='+' ) zNum++;
730 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
731 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
732}
733
drh75897232000-05-29 14:26:00 +0000734/* This comparison routine is what we use for comparison operations
drha9e99ae2002-08-13 23:02:57 +0000735** between numeric values in an SQL expression. "Numeric" is a little
736** bit misleading here. What we mean is that the strings have a
737** type of "numeric" from the point of view of SQL. The strings
738** do not necessarily contain numbers. They could contain text.
drh7a7c7392001-11-24 00:31:46 +0000739**
drha9e99ae2002-08-13 23:02:57 +0000740** If the input strings both look like actual numbers then they
741** compare in numerical order. Numerical strings are always less
742** than non-numeric strings so if one input string looks like a
743** number and the other does not, then the one that looks like
744** a number is the smaller. Non-numeric strings compare in
745** lexigraphical order (the same order as strcmp()).
drh75897232000-05-29 14:26:00 +0000746*/
747int sqliteCompare(const char *atext, const char *btext){
748 int result;
drh0bce8352002-02-28 00:41:10 +0000749 int isNumA, isNumB;
750 if( atext==0 ){
drh8912d102002-05-26 21:34:58 +0000751 return -1;
drh0bce8352002-02-28 00:41:10 +0000752 }else if( btext==0 ){
753 return 1;
754 }
drhe6840902002-03-06 03:08:25 +0000755 isNumA = sqliteIsNumber(atext);
756 isNumB = sqliteIsNumber(btext);
drh7a7c7392001-11-24 00:31:46 +0000757 if( isNumA ){
758 if( !isNumB ){
759 result = -1;
760 }else{
761 double rA, rB;
drh93a5c6b2003-12-23 02:17:35 +0000762 rA = sqliteAtoF(atext);
763 rB = sqliteAtoF(btext);
drh7a7c7392001-11-24 00:31:46 +0000764 if( rA<rB ){
765 result = -1;
766 }else if( rA>rB ){
767 result = +1;
768 }else{
769 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000770 }
771 }
drh7a7c7392001-11-24 00:31:46 +0000772 }else if( isNumB ){
773 result = +1;
774 }else {
775 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000776 }
drh7a7c7392001-11-24 00:31:46 +0000777 return result;
drh75897232000-05-29 14:26:00 +0000778}
drh75897232000-05-29 14:26:00 +0000779
780/*
drh16e59552000-07-31 11:57:37 +0000781** This routine is used for sorting. Each key is a list of one or more
drha9e99ae2002-08-13 23:02:57 +0000782** null-terminated elements. The list is terminated by two nulls in
783** a row. For example, the following text is a key with three elements
drh75897232000-05-29 14:26:00 +0000784**
drha9e99ae2002-08-13 23:02:57 +0000785** Aone\000Dtwo\000Athree\000\000
drh75897232000-05-29 14:26:00 +0000786**
drhda30d362002-08-26 19:55:07 +0000787** All elements begin with one of the characters "+-AD" and end with "\000"
788** with zero or more text elements in between. Except, NULL elements
789** consist of the special two-character sequence "N\000".
790**
drha9e99ae2002-08-13 23:02:57 +0000791** Both arguments will have the same number of elements. This routine
drh75897232000-05-29 14:26:00 +0000792** returns negative, zero, or positive if the first argument is less
793** than, equal to, or greater than the first. (Result is a-b).
794**
drha9e99ae2002-08-13 23:02:57 +0000795** Each element begins with one of the characters "+", "-", "A", "D".
drh38640e12002-07-05 21:42:36 +0000796** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000797**
drh38640e12002-07-05 21:42:36 +0000798** + Sort numerically in ascending order
799** - Sort numerically in descending order
800** A Sort as strings in ascending order
801** D Sort as strings in descending order.
802**
803** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000804** isNum() function above returns TRUE) always compare less than strings
drha9e99ae2002-08-13 23:02:57 +0000805** that are not pure numerics. Non-numeric strings compare in memcmp()
806** order. This is the same sort order as the sqliteCompare() function
807** above generates.
drh7a7c7392001-11-24 00:31:46 +0000808**
drha9e99ae2002-08-13 23:02:57 +0000809** The last point is a change from version 2.6.3 to version 2.7.0. In
810** version 2.6.3 and earlier, substrings of digits compare in numerical
811** and case was used only to break a tie.
812**
813** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
814** of whether or not they look like a number.
815**
816** Note that the sort order imposed by the rules above is the same
drh7a7c7392001-11-24 00:31:46 +0000817** from the ordering defined by the "<", "<=", ">", and ">=" operators
drha9e99ae2002-08-13 23:02:57 +0000818** of expressions and for indices. This was not the case for version
819** 2.6.3 and earlier.
drh75897232000-05-29 14:26:00 +0000820*/
821int sqliteSortCompare(const char *a, const char *b){
drh75897232000-05-29 14:26:00 +0000822 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000823 int isNumA, isNumB;
drh294fb922002-09-30 01:31:21 +0000824 int dir = 0;
drh75897232000-05-29 14:26:00 +0000825
826 while( res==0 && *a && *b ){
drhda30d362002-08-26 19:55:07 +0000827 if( a[0]=='N' || b[0]=='N' ){
828 if( a[0]==b[0] ){
829 a += 2;
830 b += 2;
831 continue;
832 }
833 if( a[0]=='N' ){
834 dir = b[0];
835 res = -1;
836 }else{
837 dir = a[0];
838 res = +1;
839 }
drhf570f012002-05-31 15:51:25 +0000840 break;
841 }
drhda30d362002-08-26 19:55:07 +0000842 assert( a[0]==b[0] );
843 if( (dir=a[0])=='A' || a[0]=='D' ){
drh38640e12002-07-05 21:42:36 +0000844 res = strcmp(&a[1],&b[1]);
845 if( res ) break;
846 }else{
847 isNumA = sqliteIsNumber(&a[1]);
848 isNumB = sqliteIsNumber(&b[1]);
849 if( isNumA ){
850 double rA, rB;
851 if( !isNumB ){
852 res = -1;
853 break;
854 }
drh93a5c6b2003-12-23 02:17:35 +0000855 rA = sqliteAtoF(&a[1]);
856 rB = sqliteAtoF(&b[1]);
drh38640e12002-07-05 21:42:36 +0000857 if( rA<rB ){
858 res = -1;
859 break;
860 }
861 if( rA>rB ){
862 res = +1;
863 break;
864 }
865 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000866 res = +1;
867 break;
drh38640e12002-07-05 21:42:36 +0000868 }else{
drha9e99ae2002-08-13 23:02:57 +0000869 res = strcmp(&a[1],&b[1]);
870 if( res ) break;
drh7a7c7392001-11-24 00:31:46 +0000871 }
drh75897232000-05-29 14:26:00 +0000872 }
drhcab20052003-04-18 17:45:14 +0000873 a += strlen(&a[1]) + 2;
874 b += strlen(&b[1]) + 2;
drh75897232000-05-29 14:26:00 +0000875 }
drhda30d362002-08-26 19:55:07 +0000876 if( dir=='-' || dir=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000877 return res;
878}
drhdce2cbe2000-05-31 02:27:49 +0000879
drh9bbca4c2001-11-06 04:00:18 +0000880/*
drh7a7c7392001-11-24 00:31:46 +0000881** Some powers of 64. These constants are needed in the
882** sqliteRealToSortable() routine below.
drh9bbca4c2001-11-06 04:00:18 +0000883*/
884#define _64e3 (64.0 * 64.0 * 64.0)
885#define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
886#define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
887#define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
888#define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
889#define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
890
891/*
892** The following procedure converts a double-precision floating point
893** number into a string. The resulting string has the property that
894** two such strings comparied using strcmp() or memcmp() will give the
drh5a2c2c22001-11-21 02:21:11 +0000895** same results as a numeric comparison of the original floating point
896** numbers.
drh9bbca4c2001-11-06 04:00:18 +0000897**
898** This routine is used to generate database keys from floating point
899** numbers such that the keys sort in the same order as the original
900** floating point numbers even though the keys are compared using
901** memcmp().
902**
903** The calling function should have allocated at least 14 characters
904** of space for the buffer z[].
905*/
906void sqliteRealToSortable(double r, char *z){
907 int neg;
908 int exp;
909 int cnt = 0;
910
911 /* This array maps integers between 0 and 63 into base-64 digits.
912 ** The digits must be chosen such at their ASCII codes are increasing.
913 ** This means we can not use the traditional base-64 digit set. */
914 static const char zDigit[] =
915 "0123456789"
916 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
917 "abcdefghijklmnopqrstuvwxyz"
918 "|~";
919 if( r<0.0 ){
920 neg = 1;
921 r = -r;
922 *z++ = '-';
923 } else {
924 neg = 0;
925 *z++ = '0';
926 }
927 exp = 0;
928
929 if( r==0.0 ){
930 exp = -1024;
931 }else if( r<(0.5/64.0) ){
932 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
933 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
934 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
935 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
936 }else if( r>=0.5 ){
937 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
938 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
939 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
940 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
941 }
942 if( neg ){
943 exp = -exp;
944 r = -r;
945 }
946 exp += 1024;
947 r += 0.5;
948 if( exp<0 ) return;
949 if( exp>=2048 || r>=1.0 ){
950 strcpy(z, "~~~~~~~~~~~~");
951 return;
952 }
953 *z++ = zDigit[(exp>>6)&0x3f];
954 *z++ = zDigit[exp & 0x3f];
955 while( r>0.0 && cnt<10 ){
956 int digit;
957 r *= 64.0;
drh1ab43002002-01-14 09:28:19 +0000958 digit = (int)r;
drh9bbca4c2001-11-06 04:00:18 +0000959 assert( digit>=0 && digit<64 );
960 *z++ = zDigit[digit & 0x3f];
961 r -= digit;
962 cnt++;
963 }
964 *z = 0;
965}
966
drh297ecf12001-04-05 15:57:13 +0000967#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000968/*
drh297ecf12001-04-05 15:57:13 +0000969** X is a pointer to the first byte of a UTF-8 character. Increment
970** X so that it points to the next character. This only works right
971** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000972*/
drh297ecf12001-04-05 15:57:13 +0000973#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
974#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000975
drh297ecf12001-04-05 15:57:13 +0000976#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000977/*
drh297ecf12001-04-05 15:57:13 +0000978** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000979*/
drh297ecf12001-04-05 15:57:13 +0000980#define sqliteNextChar(X) (++(X));
981#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000982
drh297ecf12001-04-05 15:57:13 +0000983#endif /* defined(SQLITE_UTF8) */
984
985
986#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000987/*
drh297ecf12001-04-05 15:57:13 +0000988** Convert the UTF-8 character to which z points into a 31-bit
989** UCS character. This only works right if z points to a well-formed
990** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000991*/
drh297ecf12001-04-05 15:57:13 +0000992static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000993 int c;
drh297ecf12001-04-05 15:57:13 +0000994 static const int initVal[] = {
995 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
996 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
997 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
998 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
999 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
1000 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
1001 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
1002 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
1003 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
1004 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
1005 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
1006 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
1007 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
1008 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
1009 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
1010 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1011 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
1012 255,
1013 };
1014 c = initVal[*(z++)];
1015 while( (0xc0&*z)==0x80 ){
1016 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +00001017 }
1018 return c;
1019}
drh297ecf12001-04-05 15:57:13 +00001020#endif
drhe17a7e32001-04-04 21:10:18 +00001021
1022/*
1023** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +00001024** potentially be a "glob" expression. Return true (1) if they
1025** are the same and false (0) if they are different.
1026**
1027** Globbing rules:
1028**
1029** '*' Matches any sequence of zero or more characters.
1030**
1031** '?' Matches exactly one character.
1032**
1033** [...] Matches one character from the enclosed list of
1034** characters.
1035**
1036** [^...] Matches one character not in the enclosed list.
1037**
1038** With the [...] and [^...] matching, a ']' character can be included
1039** in the list by making it the first character after '[' or '^'. A
1040** range of characters can be specified using '-'. Example:
1041** "[a-z]" matches any single lower-case letter. To match a '-', make
1042** it the last character in the list.
1043**
1044** This routine is usually quick, but can be N**2 in the worst case.
1045**
1046** Hints: to match '*' or '?', put them in "[]". Like this:
1047**
1048** abc[*]xyz Matches "abc*xyz" only
1049*/
drhe17a7e32001-04-04 21:10:18 +00001050int
1051sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
1052 register int c;
drhdce2cbe2000-05-31 02:27:49 +00001053 int invert;
1054 int seen;
drhe17a7e32001-04-04 21:10:18 +00001055 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001056
1057 while( (c = *zPattern)!=0 ){
1058 switch( c ){
1059 case '*':
drhe17a7e32001-04-04 21:10:18 +00001060 while( (c=zPattern[1]) == '*' || c == '?' ){
1061 if( c=='?' ){
1062 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001063 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001064 }
1065 zPattern++;
1066 }
1067 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +00001068 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +00001069 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +00001070 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001071 }
1072 return *zString!=0;
1073 }else{
1074 while( (c2 = *zString)!=0 ){
1075 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +00001076 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +00001077 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001078 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001079 }
1080 return 0;
1081 }
drhe17a7e32001-04-04 21:10:18 +00001082 case '?': {
drhdce2cbe2000-05-31 02:27:49 +00001083 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001084 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001085 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001086 break;
drhe17a7e32001-04-04 21:10:18 +00001087 }
1088 case '[': {
1089 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001090 seen = 0;
1091 invert = 0;
drh297ecf12001-04-05 15:57:13 +00001092 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +00001093 if( c==0 ) return 0;
1094 c2 = *++zPattern;
1095 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1096 if( c2==']' ){
1097 if( c==']' ) seen = 1;
1098 c2 = *++zPattern;
1099 }
drh297ecf12001-04-05 15:57:13 +00001100 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +00001101 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1102 zPattern++;
drh297ecf12001-04-05 15:57:13 +00001103 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +00001104 if( c>=prior_c && c<=c2 ) seen = 1;
1105 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001106 }else if( c==c2 ){
1107 seen = 1;
drhe17a7e32001-04-04 21:10:18 +00001108 prior_c = c2;
1109 }else{
1110 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001111 }
drh297ecf12001-04-05 15:57:13 +00001112 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001113 }
1114 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001115 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001116 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001117 break;
drhe17a7e32001-04-04 21:10:18 +00001118 }
1119 default: {
drhdce2cbe2000-05-31 02:27:49 +00001120 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001121 zPattern++;
1122 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001123 break;
drhe17a7e32001-04-04 21:10:18 +00001124 }
drhdce2cbe2000-05-31 02:27:49 +00001125 }
drhdce2cbe2000-05-31 02:27:49 +00001126 }
1127 return *zString==0;
1128}
1129
1130/*
drhe17a7e32001-04-04 21:10:18 +00001131** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001132** SQL. The '%' character matches any sequence of 0 or more
1133** characters and '_' matches any single character. Case is
1134** not significant.
1135**
1136** This routine is just an adaptation of the sqliteGlobCompare()
1137** routine above.
1138*/
1139int
1140sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001141 register int c;
1142 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001143
1144 while( (c = UpperToLower[*zPattern])!=0 ){
1145 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001146 case '%': {
1147 while( (c=zPattern[1]) == '%' || c == '_' ){
1148 if( c=='_' ){
1149 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001150 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001151 }
drhe17a7e32001-04-04 21:10:18 +00001152 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001153 }
drhe17a7e32001-04-04 21:10:18 +00001154 if( c==0 ) return 1;
1155 c = UpperToLower[c];
1156 while( (c2=UpperToLower[*zString])!=0 ){
1157 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1158 if( c2==0 ) return 0;
1159 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001160 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001161 }
1162 return 0;
1163 }
1164 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001165 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001166 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001167 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001168 break;
drhe17a7e32001-04-04 21:10:18 +00001169 }
1170 default: {
drhdce2cbe2000-05-31 02:27:49 +00001171 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001172 zPattern++;
1173 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001174 break;
drhe17a7e32001-04-04 21:10:18 +00001175 }
drhdce2cbe2000-05-31 02:27:49 +00001176 }
drhdce2cbe2000-05-31 02:27:49 +00001177 }
1178 return *zString==0;
1179}
drh5e00f6c2001-09-13 13:46:56 +00001180
1181/*
drhc22bd472002-05-10 13:14:07 +00001182** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1183** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1184** when this routine is called.
1185**
1186** This routine is a attempt to detect if two threads use the
1187** same sqlite* pointer at the same time. There is a race
1188** condition so it is possible that the error is not detected.
1189** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001190** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001191** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +00001192**
1193** Ticket #202: If db->magic is not a valid open value, take care not
1194** to modify the db structure at all. It could be that db is a stale
1195** pointer. In other words, it could be that there has been a prior
1196** call to sqlite_close(db) and db has been deallocated. And we do
1197** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +00001198*/
drhc22bd472002-05-10 13:14:07 +00001199int sqliteSafetyOn(sqlite *db){
1200 if( db->magic==SQLITE_MAGIC_OPEN ){
1201 db->magic = SQLITE_MAGIC_BUSY;
1202 return 0;
drh94e92032003-02-16 22:21:32 +00001203 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1204 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001205 db->magic = SQLITE_MAGIC_ERROR;
1206 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +00001207 }
drhe7e8bc72002-12-17 13:05:25 +00001208 return 1;
drhc22bd472002-05-10 13:14:07 +00001209}
1210
1211/*
1212** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1213** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1214** when this routine is called.
1215*/
1216int sqliteSafetyOff(sqlite *db){
1217 if( db->magic==SQLITE_MAGIC_BUSY ){
1218 db->magic = SQLITE_MAGIC_OPEN;
1219 return 0;
drh94e92032003-02-16 22:21:32 +00001220 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1221 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001222 db->magic = SQLITE_MAGIC_ERROR;
1223 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +00001224 }
drhe7e8bc72002-12-17 13:05:25 +00001225 return 1;
drhc22bd472002-05-10 13:14:07 +00001226}
1227
1228/*
1229** Check to make sure we are not currently executing an sqlite_exec().
1230** If we are currently in an sqlite_exec(), return true and set
1231** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1232** shutdown of the database.
1233**
1234** This routine is used to try to detect when API routines are called
1235** at the wrong time or in the wrong sequence.
1236*/
1237int sqliteSafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +00001238 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +00001239 db->magic = SQLITE_MAGIC_ERROR;
1240 return 1;
1241 }
1242 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001243}