blob: c677f61186de8b3a06655d4695d8181f6e6c8fe8 [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**
drhfec19aa2004-05-19 20:41:03 +000017** $Id: util.c,v 1.86 2004/05/19 20:41:03 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*/
danielk19776f8a5032004-05-10 10:34:51 +000027int sqlite3_malloc_failed = 0;
drhdaffd0e2001-04-11 14:28:42 +000028
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*/
danielk19776f8a5032004-05-10 10:34:51 +000039int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */
40int sqlite3_nFree; /* Number of sqliteFree() calls */
41int sqlite3_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*/
danielk19774adee202004-05-08 08:23:19 +000055void *sqlite3Malloc_(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;
danielk19776f8a5032004-05-10 10:34:51 +000059 if( sqlite3_iMallocFail>=0 ){
60 sqlite3_iMallocFail--;
61 if( sqlite3_iMallocFail==0 ){
62 sqlite3_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
danielk19776f8a5032004-05-10 10:34:51 +000067 sqlite3_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 ){
danielk19776f8a5032004-05-10 10:34:51 +000075 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +000076 return 0;
77 }
danielk19776f8a5032004-05-10 10:34:51 +000078 sqlite3_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*/
danielk19774adee202004-05-08 08:23:19 +000098void sqlite3CheckMemory(void *p, int N){
drhed6c8672003-01-12 18:02:16 +000099 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*/
danielk19774adee202004-05-08 08:23:19 +0000116void sqlite3Free_(void *p, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +0000117 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;
danielk19776f8a5032004-05-10 10:34:51 +0000121 sqlite3_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*/
danielk19774adee202004-05-08 08:23:19 +0000150void *sqlite3Realloc_(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 ){
danielk19774adee202004-05-08 08:23:19 +0000154 return sqlite3Malloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000155 }
156 if( n==0 ){
danielk19774adee202004-05-08 08:23:19 +0000157 sqlite3Free_(oldP,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000158 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 ){
danielk19776f8a5032004-05-10 10:34:51 +0000178 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000179 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*/
danielk19774adee202004-05-08 08:23:19 +0000206void sqlite3StrRealloc(char **pz){
drhc3c2fc92000-05-31 22:58:39 +0000207 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 ){
danielk19776f8a5032004-05-10 10:34:51 +0000211 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000212 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*/
danielk19774adee202004-05-08 08:23:19 +0000223char *sqlite3StrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000224 char *zNew;
225 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000226 zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000227 if( zNew ) strcpy(zNew, z);
228 return zNew;
229}
danielk19774adee202004-05-08 08:23:19 +0000230char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000231 char *zNew;
232 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000233 zNew = sqlite3Malloc_(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 ){
danielk19776f8a5032004-05-10 10:34:51 +0000255 if( n>0 ) sqlite3_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 ){
danielk19776f8a5032004-05-10 10:34:51 +0000269 if( n>0 ) sqlite3_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 ){
danielk19776f8a5032004-05-10 10:34:51 +0000299 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000300 }
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*/
danielk19774adee202004-05-08 08:23:19 +0000333void sqlite3SetString(char **pz, const char *zFirst, ...){
drh75897232000-05-29 14:26:00 +0000334 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/*
danielk19774adee202004-05-08 08:23:19 +0000367** Works like sqlite3SetString, 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*/
danielk19774adee202004-05-08 08:23:19 +0000373void sqlite3SetNString(char **pz, ...){
drh75897232000-05-29 14:26:00 +0000374 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*/
danielk19774adee202004-05-08 08:23:19 +0000418void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000419 va_list ap;
drhda93d232003-03-31 02:12:46 +0000420 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +0000421 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000422 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +0000423 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000424 va_end(ap);
drhda93d232003-03-31 02:12:46 +0000425}
426
427/*
drh982cef72000-05-30 16:27:03 +0000428** Convert an SQL-style quoted string into a normal string by removing
429** the quote characters. The conversion is done in-place. If the
430** input does not begin with a quote character, then this routine
431** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000432**
433** 2002-Feb-14: This routine is extended to remove MS-Access style
434** brackets from around identifers. For example: "[a-b-c]" becomes
435** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000436*/
danielk19774adee202004-05-08 08:23:19 +0000437void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000438 int quote;
439 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000440 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000441 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000442 switch( quote ){
443 case '\'': break;
444 case '"': break;
445 case '[': quote = ']'; break;
446 default: return;
447 }
drh982cef72000-05-30 16:27:03 +0000448 for(i=1, j=0; z[i]; i++){
449 if( z[i]==quote ){
450 if( z[i+1]==quote ){
451 z[j++] = quote;
452 i++;
453 }else{
454 z[j++] = 0;
455 break;
456 }
457 }else{
458 z[j++] = z[i];
459 }
460 }
461}
462
drh75897232000-05-29 14:26:00 +0000463/* An array to map all upper-case characters into their corresponding
464** lower-case character.
465*/
466static unsigned char UpperToLower[] = {
467 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
468 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
469 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
470 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
471 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
472 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
473 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
474 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
475 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
476 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
477 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
478 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
479 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
480 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
481 252,253,254,255
482};
483
484/*
485** This function computes a hash on the name of a keyword.
486** Case is not significant.
487*/
danielk19774adee202004-05-08 08:23:19 +0000488int sqlite3HashNoCase(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000489 int h = 0;
drh75897232000-05-29 14:26:00 +0000490 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000491 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000492 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000493 n--;
drh75897232000-05-29 14:26:00 +0000494 }
drh5364f602003-05-12 23:06:52 +0000495 return h & 0x7fffffff;
drh75897232000-05-29 14:26:00 +0000496}
497
498/*
drh967e8b72000-06-21 13:59:10 +0000499** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000500** there is no consistency, we will define our own.
501*/
danielk19774adee202004-05-08 08:23:19 +0000502int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000503 register unsigned char *a, *b;
504 a = (unsigned char *)zLeft;
505 b = (unsigned char *)zRight;
506 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
507 return *a - *b;
508}
danielk19774adee202004-05-08 08:23:19 +0000509int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000510 register unsigned char *a, *b;
511 a = (unsigned char *)zLeft;
512 b = (unsigned char *)zRight;
513 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000514 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000515}
516
drha5c2ad02000-09-14 01:21:10 +0000517/*
drh7a7c7392001-11-24 00:31:46 +0000518** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000519** string contains any character which is not part of a number. If
520** the string is numeric and contains the '.' character, set *realnum
521** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000522**
drhbb07e9a2003-04-16 02:17:35 +0000523** Am empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000524*/
danielk19773d1bfea2004-05-14 11:00:53 +0000525int sqlite3IsNumber(const char *z, int *realnum){
drh7a7c7392001-11-24 00:31:46 +0000526 if( *z=='-' || *z=='+' ) z++;
527 if( !isdigit(*z) ){
drhbb07e9a2003-04-16 02:17:35 +0000528 return 0;
drha5c2ad02000-09-14 01:21:10 +0000529 }
drh7a7c7392001-11-24 00:31:46 +0000530 z++;
danielk19773d1bfea2004-05-14 11:00:53 +0000531 if( realnum ) *realnum = 0;
drh7a7c7392001-11-24 00:31:46 +0000532 while( isdigit(*z) ){ z++; }
533 if( *z=='.' ){
534 z++;
535 if( !isdigit(*z) ) return 0;
536 while( isdigit(*z) ){ z++; }
danielk19773d1bfea2004-05-14 11:00:53 +0000537 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000538 }
539 if( *z=='e' || *z=='E' ){
540 z++;
541 if( *z=='+' || *z=='-' ) z++;
542 if( !isdigit(*z) ) return 0;
543 while( isdigit(*z) ){ z++; }
danielk19773d1bfea2004-05-14 11:00:53 +0000544 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000545 }
drh7a7c7392001-11-24 00:31:46 +0000546 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000547}
548
drh93a5c6b2003-12-23 02:17:35 +0000549/*
550** The string z[] is an ascii representation of a real number.
551** Convert this string to a double.
552**
553** This routine assumes that z[] really is a valid number. If it
554** is not, the result is undefined.
555**
556** This routine is used instead of the library atof() function because
557** the library atof() might want to use "," as the decimal point instead
558** of "." depending on how locale is set. But that would cause problems
559** for SQL. So this routine always uses "." regardless of locale.
560*/
danielk19774adee202004-05-08 08:23:19 +0000561double sqlite3AtoF(const char *z, const char **pzEnd){
drh93a5c6b2003-12-23 02:17:35 +0000562 int sign = 1;
drh384eef32004-01-07 03:04:27 +0000563 LONGDOUBLE_TYPE v1 = 0.0;
drh93a5c6b2003-12-23 02:17:35 +0000564 if( *z=='-' ){
565 sign = -1;
566 z++;
567 }else if( *z=='+' ){
568 z++;
569 }
570 while( isdigit(*z) ){
571 v1 = v1*10.0 + (*z - '0');
572 z++;
573 }
574 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000575 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000576 z++;
577 while( isdigit(*z) ){
578 v1 = v1*10.0 + (*z - '0');
579 divisor *= 10.0;
580 z++;
581 }
582 v1 /= divisor;
583 }
584 if( *z=='e' || *z=='E' ){
585 int esign = 1;
586 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000587 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000588 z++;
589 if( *z=='-' ){
590 esign = -1;
591 z++;
592 }else if( *z=='+' ){
593 z++;
594 }
595 while( isdigit(*z) ){
596 eval = eval*10 + *z - '0';
597 z++;
598 }
599 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
600 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
601 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
602 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
603 if( esign<0 ){
604 v1 /= scale;
605 }else{
606 v1 *= scale;
607 }
608 }
drheb9a9e82004-02-22 17:49:32 +0000609 if( pzEnd ) *pzEnd = z;
drh93a5c6b2003-12-23 02:17:35 +0000610 return sign<0 ? -v1 : v1;
611}
612
drh202b2df2004-01-06 01:13:46 +0000613/*
drhfec19aa2004-05-19 20:41:03 +0000614** Return TRUE if zNum is a 64-bit signed integer and write
615** the value of the integer into *pNum. If zNum is not an integer
616** or is an integer that is too large to be expressed with 64 bits,
617** then return false. If n>0 and the integer is string is not
618** exactly n bytes long, return false.
619**
620** When this routine was originally written it dealt with only
621** 32-bit numbers. At that time, it was much faster than the
622** atoi() library routine in RedHat 7.2.
623*/
624int sqlite3atoi64(const char *zNum, i64 *pNum){
625 i64 v = 0;
626 int neg;
627 int i, c;
628 if( *zNum=='-' ){
629 neg = 1;
630 zNum++;
631 }else if( *zNum=='+' ){
632 neg = 0;
633 zNum++;
634 }else{
635 neg = 0;
636 }
637 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
638 v = v*10 + c - '0';
639 }
640 *pNum = neg ? -v : v;
641 return c==0 && i>0 &&
642 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
643}
644
645/*
drh202b2df2004-01-06 01:13:46 +0000646** The string zNum represents an integer. There might be some other
647** information following the integer too, but that part is ignored.
648** If the integer that the prefix of zNum represents will fit in a
649** 32-bit signed integer, return TRUE. Otherwise return FALSE.
650**
651** This routine returns FALSE for the string -2147483648 even that
652** that number will, in theory fit in a 32-bit integer. But positive
653** 2147483648 will not fit in 32 bits. So it seems safer to return
654** false.
655*/
drhfec19aa2004-05-19 20:41:03 +0000656static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000657 int i, c;
658 if( *zNum=='-' || *zNum=='+' ) zNum++;
659 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
660 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
661}
662
drhfec19aa2004-05-19 20:41:03 +0000663/*
664** If zNum represents an integer that will fit in 32-bits, then set
665** *pValue to that integer and return true. Otherwise return false.
666*/
667int sqlite3GetInt32(const char *zNum, int *pValue){
668 if( sqlite3FitsIn32Bits(zNum) ){
669 *pValue = atoi(zNum);
670 return 1;
671 }
672 return 0;
673}
674
675/*
676** The string zNum represents an integer. There might be some other
677** information following the integer too, but that part is ignored.
678** If the integer that the prefix of zNum represents will fit in a
679** 64-bit signed integer, return TRUE. Otherwise return FALSE.
680**
681** This routine returns FALSE for the string -9223372036854775808 even that
682** that number will, in theory fit in a 64-bit integer. Positive
683** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
684** false.
685*/
686int sqlite3FitsIn64Bits(const char *zNum){
687 int i, c;
688 if( *zNum=='-' || *zNum=='+' ) zNum++;
689 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
690 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
691}
692
693/*
694** If zNum represents an integer that will fit in 64-bits, then set
695** *pValue to that integer and return true. Otherwise return false.
696*/
697int sqlite3GetInt64(const char *zNum, i64 *pValue){
698 if( sqlite3FitsIn64Bits(zNum) ){
699 sqlite3atoi64(zNum, pValue);
700 return 1;
701 }
702 return 0;
703}
704
drh75897232000-05-29 14:26:00 +0000705/* This comparison routine is what we use for comparison operations
drha9e99ae2002-08-13 23:02:57 +0000706** between numeric values in an SQL expression. "Numeric" is a little
707** bit misleading here. What we mean is that the strings have a
708** type of "numeric" from the point of view of SQL. The strings
709** do not necessarily contain numbers. They could contain text.
drh7a7c7392001-11-24 00:31:46 +0000710**
drha9e99ae2002-08-13 23:02:57 +0000711** If the input strings both look like actual numbers then they
712** compare in numerical order. Numerical strings are always less
713** than non-numeric strings so if one input string looks like a
714** number and the other does not, then the one that looks like
715** a number is the smaller. Non-numeric strings compare in
716** lexigraphical order (the same order as strcmp()).
drh75897232000-05-29 14:26:00 +0000717*/
danielk19774adee202004-05-08 08:23:19 +0000718int sqlite3Compare(const char *atext, const char *btext){
drh75897232000-05-29 14:26:00 +0000719 int result;
drh0bce8352002-02-28 00:41:10 +0000720 int isNumA, isNumB;
721 if( atext==0 ){
drh8912d102002-05-26 21:34:58 +0000722 return -1;
drh0bce8352002-02-28 00:41:10 +0000723 }else if( btext==0 ){
724 return 1;
725 }
danielk19773d1bfea2004-05-14 11:00:53 +0000726 isNumA = sqlite3IsNumber(atext, 0);
727 isNumB = sqlite3IsNumber(btext, 0);
drh7a7c7392001-11-24 00:31:46 +0000728 if( isNumA ){
729 if( !isNumB ){
730 result = -1;
731 }else{
732 double rA, rB;
danielk19774adee202004-05-08 08:23:19 +0000733 rA = sqlite3AtoF(atext, 0);
734 rB = sqlite3AtoF(btext, 0);
drh7a7c7392001-11-24 00:31:46 +0000735 if( rA<rB ){
736 result = -1;
737 }else if( rA>rB ){
738 result = +1;
739 }else{
740 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000741 }
742 }
drh7a7c7392001-11-24 00:31:46 +0000743 }else if( isNumB ){
744 result = +1;
745 }else {
746 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000747 }
drh7a7c7392001-11-24 00:31:46 +0000748 return result;
drh75897232000-05-29 14:26:00 +0000749}
drh75897232000-05-29 14:26:00 +0000750
751/*
drh16e59552000-07-31 11:57:37 +0000752** This routine is used for sorting. Each key is a list of one or more
drha9e99ae2002-08-13 23:02:57 +0000753** null-terminated elements. The list is terminated by two nulls in
754** a row. For example, the following text is a key with three elements
drh75897232000-05-29 14:26:00 +0000755**
drha9e99ae2002-08-13 23:02:57 +0000756** Aone\000Dtwo\000Athree\000\000
drh75897232000-05-29 14:26:00 +0000757**
drhda30d362002-08-26 19:55:07 +0000758** All elements begin with one of the characters "+-AD" and end with "\000"
759** with zero or more text elements in between. Except, NULL elements
760** consist of the special two-character sequence "N\000".
761**
drha9e99ae2002-08-13 23:02:57 +0000762** Both arguments will have the same number of elements. This routine
drh75897232000-05-29 14:26:00 +0000763** returns negative, zero, or positive if the first argument is less
764** than, equal to, or greater than the first. (Result is a-b).
765**
drha9e99ae2002-08-13 23:02:57 +0000766** Each element begins with one of the characters "+", "-", "A", "D".
drh38640e12002-07-05 21:42:36 +0000767** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000768**
drh38640e12002-07-05 21:42:36 +0000769** + Sort numerically in ascending order
770** - Sort numerically in descending order
771** A Sort as strings in ascending order
772** D Sort as strings in descending order.
773**
774** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000775** isNum() function above returns TRUE) always compare less than strings
drha9e99ae2002-08-13 23:02:57 +0000776** that are not pure numerics. Non-numeric strings compare in memcmp()
danielk19774adee202004-05-08 08:23:19 +0000777** order. This is the same sort order as the sqlite3Compare() function
drha9e99ae2002-08-13 23:02:57 +0000778** above generates.
drh7a7c7392001-11-24 00:31:46 +0000779**
drha9e99ae2002-08-13 23:02:57 +0000780** The last point is a change from version 2.6.3 to version 2.7.0. In
781** version 2.6.3 and earlier, substrings of digits compare in numerical
782** and case was used only to break a tie.
783**
784** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
785** of whether or not they look like a number.
786**
787** Note that the sort order imposed by the rules above is the same
drh7a7c7392001-11-24 00:31:46 +0000788** from the ordering defined by the "<", "<=", ">", and ">=" operators
drha9e99ae2002-08-13 23:02:57 +0000789** of expressions and for indices. This was not the case for version
790** 2.6.3 and earlier.
drh75897232000-05-29 14:26:00 +0000791*/
danielk19774adee202004-05-08 08:23:19 +0000792int sqlite3SortCompare(const char *a, const char *b){
drh75897232000-05-29 14:26:00 +0000793 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000794 int isNumA, isNumB;
drh294fb922002-09-30 01:31:21 +0000795 int dir = 0;
drh75897232000-05-29 14:26:00 +0000796
797 while( res==0 && *a && *b ){
drhda30d362002-08-26 19:55:07 +0000798 if( a[0]=='N' || b[0]=='N' ){
799 if( a[0]==b[0] ){
800 a += 2;
801 b += 2;
802 continue;
803 }
804 if( a[0]=='N' ){
805 dir = b[0];
806 res = -1;
807 }else{
808 dir = a[0];
809 res = +1;
810 }
drhf570f012002-05-31 15:51:25 +0000811 break;
812 }
drhda30d362002-08-26 19:55:07 +0000813 assert( a[0]==b[0] );
814 if( (dir=a[0])=='A' || a[0]=='D' ){
drh38640e12002-07-05 21:42:36 +0000815 res = strcmp(&a[1],&b[1]);
816 if( res ) break;
817 }else{
danielk19773d1bfea2004-05-14 11:00:53 +0000818 isNumA = sqlite3IsNumber(&a[1], 0);
819 isNumB = sqlite3IsNumber(&b[1], 0);
drh38640e12002-07-05 21:42:36 +0000820 if( isNumA ){
821 double rA, rB;
822 if( !isNumB ){
823 res = -1;
824 break;
825 }
danielk19774adee202004-05-08 08:23:19 +0000826 rA = sqlite3AtoF(&a[1], 0);
827 rB = sqlite3AtoF(&b[1], 0);
drh38640e12002-07-05 21:42:36 +0000828 if( rA<rB ){
829 res = -1;
830 break;
831 }
832 if( rA>rB ){
833 res = +1;
834 break;
835 }
836 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000837 res = +1;
838 break;
drh38640e12002-07-05 21:42:36 +0000839 }else{
drha9e99ae2002-08-13 23:02:57 +0000840 res = strcmp(&a[1],&b[1]);
841 if( res ) break;
drh7a7c7392001-11-24 00:31:46 +0000842 }
drh75897232000-05-29 14:26:00 +0000843 }
drhcab20052003-04-18 17:45:14 +0000844 a += strlen(&a[1]) + 2;
845 b += strlen(&b[1]) + 2;
drh75897232000-05-29 14:26:00 +0000846 }
drhda30d362002-08-26 19:55:07 +0000847 if( dir=='-' || dir=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000848 return res;
849}
drhdce2cbe2000-05-31 02:27:49 +0000850
drh297ecf12001-04-05 15:57:13 +0000851#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000852/*
drh297ecf12001-04-05 15:57:13 +0000853** X is a pointer to the first byte of a UTF-8 character. Increment
854** X so that it points to the next character. This only works right
855** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000856*/
drh297ecf12001-04-05 15:57:13 +0000857#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
858#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000859
drh297ecf12001-04-05 15:57:13 +0000860#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000861/*
drh297ecf12001-04-05 15:57:13 +0000862** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000863*/
drh297ecf12001-04-05 15:57:13 +0000864#define sqliteNextChar(X) (++(X));
865#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000866
drh297ecf12001-04-05 15:57:13 +0000867#endif /* defined(SQLITE_UTF8) */
868
869
870#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000871/*
drh297ecf12001-04-05 15:57:13 +0000872** Convert the UTF-8 character to which z points into a 31-bit
873** UCS character. This only works right if z points to a well-formed
874** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000875*/
drh297ecf12001-04-05 15:57:13 +0000876static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000877 int c;
drh297ecf12001-04-05 15:57:13 +0000878 static const int initVal[] = {
879 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
880 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
881 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
882 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
883 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
884 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
885 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
886 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
887 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
888 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
889 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
890 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
891 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
892 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
893 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
894 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
895 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
896 255,
897 };
898 c = initVal[*(z++)];
899 while( (0xc0&*z)==0x80 ){
900 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000901 }
902 return c;
903}
drh297ecf12001-04-05 15:57:13 +0000904#endif
drhe17a7e32001-04-04 21:10:18 +0000905
906/*
907** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000908** potentially be a "glob" expression. Return true (1) if they
909** are the same and false (0) if they are different.
910**
911** Globbing rules:
912**
913** '*' Matches any sequence of zero or more characters.
914**
915** '?' Matches exactly one character.
916**
917** [...] Matches one character from the enclosed list of
918** characters.
919**
920** [^...] Matches one character not in the enclosed list.
921**
922** With the [...] and [^...] matching, a ']' character can be included
923** in the list by making it the first character after '[' or '^'. A
924** range of characters can be specified using '-'. Example:
925** "[a-z]" matches any single lower-case letter. To match a '-', make
926** it the last character in the list.
927**
928** This routine is usually quick, but can be N**2 in the worst case.
929**
930** Hints: to match '*' or '?', put them in "[]". Like this:
931**
932** abc[*]xyz Matches "abc*xyz" only
933*/
drhe17a7e32001-04-04 21:10:18 +0000934int
danielk19774adee202004-05-08 08:23:19 +0000935sqlite3GlobCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +0000936 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000937 int invert;
938 int seen;
drhe17a7e32001-04-04 21:10:18 +0000939 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000940
941 while( (c = *zPattern)!=0 ){
942 switch( c ){
943 case '*':
drhe17a7e32001-04-04 21:10:18 +0000944 while( (c=zPattern[1]) == '*' || c == '?' ){
945 if( c=='?' ){
946 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000947 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000948 }
949 zPattern++;
950 }
951 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +0000952 if( c=='[' ){
danielk19774adee202004-05-08 08:23:19 +0000953 while( *zString && sqlite3GlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000954 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000955 }
956 return *zString!=0;
957 }else{
958 while( (c2 = *zString)!=0 ){
959 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000960 if( c2==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000961 if( sqlite3GlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000962 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000963 }
964 return 0;
965 }
drhe17a7e32001-04-04 21:10:18 +0000966 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000967 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000968 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000969 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000970 break;
drhe17a7e32001-04-04 21:10:18 +0000971 }
972 case '[': {
973 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000974 seen = 0;
975 invert = 0;
drh297ecf12001-04-05 15:57:13 +0000976 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +0000977 if( c==0 ) return 0;
978 c2 = *++zPattern;
979 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
980 if( c2==']' ){
981 if( c==']' ) seen = 1;
982 c2 = *++zPattern;
983 }
drh297ecf12001-04-05 15:57:13 +0000984 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +0000985 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
986 zPattern++;
drh297ecf12001-04-05 15:57:13 +0000987 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +0000988 if( c>=prior_c && c<=c2 ) seen = 1;
989 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000990 }else if( c==c2 ){
991 seen = 1;
drhe17a7e32001-04-04 21:10:18 +0000992 prior_c = c2;
993 }else{
994 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +0000995 }
drh297ecf12001-04-05 15:57:13 +0000996 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +0000997 }
998 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000999 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001000 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001001 break;
drhe17a7e32001-04-04 21:10:18 +00001002 }
1003 default: {
drhdce2cbe2000-05-31 02:27:49 +00001004 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001005 zPattern++;
1006 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001007 break;
drhe17a7e32001-04-04 21:10:18 +00001008 }
drhdce2cbe2000-05-31 02:27:49 +00001009 }
drhdce2cbe2000-05-31 02:27:49 +00001010 }
1011 return *zString==0;
1012}
1013
1014/*
drhe17a7e32001-04-04 21:10:18 +00001015** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001016** SQL. The '%' character matches any sequence of 0 or more
1017** characters and '_' matches any single character. Case is
1018** not significant.
1019**
danielk19774adee202004-05-08 08:23:19 +00001020** This routine is just an adaptation of the sqlite3GlobCompare()
drhdce2cbe2000-05-31 02:27:49 +00001021** routine above.
1022*/
1023int
danielk19774adee202004-05-08 08:23:19 +00001024sqlite3LikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001025 register int c;
1026 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001027
1028 while( (c = UpperToLower[*zPattern])!=0 ){
1029 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001030 case '%': {
1031 while( (c=zPattern[1]) == '%' || c == '_' ){
1032 if( c=='_' ){
1033 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001034 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001035 }
drhe17a7e32001-04-04 21:10:18 +00001036 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001037 }
drhe17a7e32001-04-04 21:10:18 +00001038 if( c==0 ) return 1;
1039 c = UpperToLower[c];
1040 while( (c2=UpperToLower[*zString])!=0 ){
1041 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1042 if( c2==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001043 if( sqlite3LikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001044 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001045 }
1046 return 0;
1047 }
1048 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001049 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001050 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001051 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001052 break;
drhe17a7e32001-04-04 21:10:18 +00001053 }
1054 default: {
drhdce2cbe2000-05-31 02:27:49 +00001055 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001056 zPattern++;
1057 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001058 break;
drhe17a7e32001-04-04 21:10:18 +00001059 }
drhdce2cbe2000-05-31 02:27:49 +00001060 }
drhdce2cbe2000-05-31 02:27:49 +00001061 }
1062 return *zString==0;
1063}
drh5e00f6c2001-09-13 13:46:56 +00001064
1065/*
drhc22bd472002-05-10 13:14:07 +00001066** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1067** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1068** when this routine is called.
1069**
1070** This routine is a attempt to detect if two threads use the
1071** same sqlite* pointer at the same time. There is a race
1072** condition so it is possible that the error is not detected.
1073** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001074** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001075** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +00001076**
1077** Ticket #202: If db->magic is not a valid open value, take care not
1078** to modify the db structure at all. It could be that db is a stale
1079** pointer. In other words, it could be that there has been a prior
danielk19776f8a5032004-05-10 10:34:51 +00001080** call to sqlite3_close(db) and db has been deallocated. And we do
drhe7e8bc72002-12-17 13:05:25 +00001081** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +00001082*/
danielk19774adee202004-05-08 08:23:19 +00001083int sqlite3SafetyOn(sqlite *db){
drhc22bd472002-05-10 13:14:07 +00001084 if( db->magic==SQLITE_MAGIC_OPEN ){
1085 db->magic = SQLITE_MAGIC_BUSY;
1086 return 0;
drh94e92032003-02-16 22:21:32 +00001087 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1088 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001089 db->magic = SQLITE_MAGIC_ERROR;
1090 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +00001091 }
drhe7e8bc72002-12-17 13:05:25 +00001092 return 1;
drhc22bd472002-05-10 13:14:07 +00001093}
1094
1095/*
1096** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1097** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1098** when this routine is called.
1099*/
danielk19774adee202004-05-08 08:23:19 +00001100int sqlite3SafetyOff(sqlite *db){
drhc22bd472002-05-10 13:14:07 +00001101 if( db->magic==SQLITE_MAGIC_BUSY ){
1102 db->magic = SQLITE_MAGIC_OPEN;
1103 return 0;
drh94e92032003-02-16 22:21:32 +00001104 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1105 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001106 db->magic = SQLITE_MAGIC_ERROR;
1107 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +00001108 }
drhe7e8bc72002-12-17 13:05:25 +00001109 return 1;
drhc22bd472002-05-10 13:14:07 +00001110}
1111
1112/*
danielk19776f8a5032004-05-10 10:34:51 +00001113** Check to make sure we are not currently executing an sqlite3_exec().
1114** If we are currently in an sqlite3_exec(), return true and set
drhc22bd472002-05-10 13:14:07 +00001115** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1116** shutdown of the database.
1117**
1118** This routine is used to try to detect when API routines are called
1119** at the wrong time or in the wrong sequence.
1120*/
danielk19774adee202004-05-08 08:23:19 +00001121int sqlite3SafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +00001122 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +00001123 db->magic = SQLITE_MAGIC_ERROR;
1124 return 1;
1125 }
1126 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001127}
danielk19774adee202004-05-08 08:23:19 +00001128
drh6d2fb152004-05-14 16:50:06 +00001129/*
drhd8820e82004-05-18 15:57:42 +00001130** The variable-length integer encoding is as follows:
1131**
1132** KEY:
1133** A = 0xxxxxxx 7 bits of data and one flag bit
1134** B = 1xxxxxxx 7 bits of data and one flag bit
1135** C = xxxxxxxx 8 bits of data
1136**
1137** 7 bits - A
1138** 14 bits - BA
1139** 21 bits - BBA
1140** 28 bits - BBBA
1141** 35 bits - BBBBA
1142** 42 bits - BBBBBA
1143** 49 bits - BBBBBBA
1144** 56 bits - BBBBBBBA
1145** 64 bits - BBBBBBBBC
1146*/
1147
1148/*
drh6d2fb152004-05-14 16:50:06 +00001149** Write a 64-bit variable-length integer to memory starting at p[0].
1150** The length of data write will be between 1 and 9 bytes. The number
1151** of bytes written is returned.
1152**
1153** A variable-length integer consists of the lower 7 bits of each byte
1154** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +00001155** bit clear. Except, if we get to the 9th byte, it stores the full
1156** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +00001157*/
danielk1977192ac1d2004-05-10 07:17:30 +00001158int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +00001159 int i, j, n;
1160 u8 buf[10];
drhd8820e82004-05-18 15:57:42 +00001161 if( v & 0xff00000000000000 ){
1162 p[8] = v;
1163 v >>= 8;
1164 for(i=7; i>=0; i--){
1165 p[i] = (v & 0x7f) | 0x80;
1166 v >>= 7;
1167 }
1168 return 9;
1169 }
drh6d2fb152004-05-14 16:50:06 +00001170 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001171 do{
drh6d2fb152004-05-14 16:50:06 +00001172 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +00001173 v >>= 7;
1174 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +00001175 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +00001176 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +00001177 for(i=0, j=n-1; j>=0; j--, i++){
1178 p[i] = buf[j];
1179 }
1180 return n;
danielk1977192ac1d2004-05-10 07:17:30 +00001181}
danielk19774adee202004-05-08 08:23:19 +00001182
drh6d2fb152004-05-14 16:50:06 +00001183/*
1184** Read a 64-bit variable-length integer from memory starting at p[0].
1185** Return the number of bytes read. The value is stored in *v.
1186*/
danielk197790e4d952004-05-10 10:05:53 +00001187int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +00001188 u32 x;
1189 u64 x64;
1190 int n;
1191 unsigned char c;
1192 c = p[0];
1193 if( (c & 0x80)==0 ){
1194 *v = c;
1195 return 1;
1196 }
1197 x = c & 0x7f;
1198 c = p[1];
1199 if( (c & 0x80)==0 ){
1200 *v = (x<<7) | c;
1201 return 2;
1202 }
1203 x = (x<<7) | (c&0x7f);
1204 c = p[2];
1205 if( (c & 0x80)==0 ){
1206 *v = (x<<7) | c;
1207 return 3;
1208 }
1209 x = (x<<7) | (c&0x7f);
1210 c = p[3];
1211 if( (c & 0x80)==0 ){
1212 *v = (x<<7) | c;
1213 return 4;
1214 }
1215 x64 = (x<<7) | (c&0x7f);
1216 n = 4;
1217 do{
1218 c = p[n++];
drhd8820e82004-05-18 15:57:42 +00001219 if( n==9 ){
1220 x64 = (x64<<8) | c;
1221 break;
1222 }
drh6d2fb152004-05-14 16:50:06 +00001223 x64 = (x64<<7) | (c&0x7f);
1224 }while( (c & 0x80)!=0 );
1225 *v = x64;
1226 return n;
1227}
1228
1229/*
1230** Read a 32-bit variable-length integer from memory starting at p[0].
1231** Return the number of bytes read. The value is stored in *v.
1232*/
1233int sqlite3GetVarint32(const unsigned char *p, u32 *v){
1234 int n = 1;
1235 unsigned char c = p[0];
1236 u32 x = c & 0x7f;
drhd8820e82004-05-18 15:57:42 +00001237 while( (c & 0x80)!=0 && n<9 ){
drh6d2fb152004-05-14 16:50:06 +00001238 c = p[n++];
1239 x = (x<<7) | (c & 0x7f);
danielk1977192ac1d2004-05-10 07:17:30 +00001240 }
1241 *v = x;
1242 return n;
1243}
1244
drh6d2fb152004-05-14 16:50:06 +00001245/*
1246** Return the number of bytes that will be needed to store the given
1247** 64-bit integer.
1248*/
danielk1977192ac1d2004-05-10 07:17:30 +00001249int sqlite3VarintLen(u64 v){
1250 int i = 0;
1251 do{
1252 i++;
1253 v >>= 7;
drhd8820e82004-05-18 15:57:42 +00001254 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +00001255 return i;
1256}