blob: ea0946dcb0bb08b20c3fa87709542b62d80a107d [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**
danielk197793cd0392004-06-30 02:35:51 +000017** $Id: util.c,v 1.108 2004/06/30 02:35:51 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
danielk1977eac7a362004-06-16 07:45:24 +000023#if SQLITE_DEBUG>2 && defined(__GLIBC__)
24#include <execinfo.h>
25void print_stack_trace(){
26 void *bt[30];
27 int i;
28 int n = backtrace(bt, 30);
29
30 fprintf(stderr, "STACK: ");
31 for(i=0; i<n;i++){
32 fprintf(stderr, "%p ", bt[i]);
33 }
34 fprintf(stderr, "\n");
35}
36#else
37#define print_stack_trace()
38#endif
39
drh7c68d602000-10-11 19:28:51 +000040/*
drhdaffd0e2001-04-11 14:28:42 +000041** If malloc() ever fails, this global variable gets set to 1.
42** This causes the library to abort and never again function.
43*/
danielk19776f8a5032004-05-10 10:34:51 +000044int sqlite3_malloc_failed = 0;
drhdaffd0e2001-04-11 14:28:42 +000045
46/*
drhfaa57ac2004-06-09 14:01:51 +000047** If SQLITE_DEBUG is defined, then use versions of malloc() and
drh7c68d602000-10-11 19:28:51 +000048** free() that track memory usage and check for buffer overruns.
49*/
drhfaa57ac2004-06-09 14:01:51 +000050#ifdef SQLITE_DEBUG
drhdcc581c2000-05-30 13:44:19 +000051
drhdcc581c2000-05-30 13:44:19 +000052/*
drh8c82b352000-12-10 18:23:50 +000053** For keeping track of the number of mallocs and frees. This
54** is used to check for memory leaks.
55*/
danielk19776f8a5032004-05-10 10:34:51 +000056int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */
57int sqlite3_nFree; /* Number of sqliteFree() calls */
58int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */
drhfaa57ac2004-06-09 14:01:51 +000059#if SQLITE_DEBUG>1
drhd94a6692002-08-25 18:29:11 +000060static int memcnt = 0;
61#endif
drh8c82b352000-12-10 18:23:50 +000062
drh4305d102003-07-30 12:34:12 +000063/*
64** Number of 32-bit guard words
65*/
66#define N_GUARD 1
drh8c82b352000-12-10 18:23:50 +000067
68/*
drhdcc581c2000-05-30 13:44:19 +000069** Allocate new memory and set it to zero. Return NULL if
70** no memory is available.
71*/
danielk19774adee202004-05-08 08:23:19 +000072void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000073 void *p;
74 int *pi;
drh4305d102003-07-30 12:34:12 +000075 int i, k;
danielk19776f8a5032004-05-10 10:34:51 +000076 if( sqlite3_iMallocFail>=0 ){
77 sqlite3_iMallocFail--;
78 if( sqlite3_iMallocFail==0 ){
79 sqlite3_malloc_failed++;
drhfaa57ac2004-06-09 14:01:51 +000080#if SQLITE_DEBUG>1
drh6d4abfb2001-10-22 02:58:08 +000081 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
82 n, zFile,line);
83#endif
danielk19776f8a5032004-05-10 10:34:51 +000084 sqlite3_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000085 return 0;
86 }
drh6e142f52000-06-08 13:36:40 +000087 }
drhb0729502001-03-14 12:35:57 +000088 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000089 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +000090 pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000091 if( pi==0 ){
danielk19776f8a5032004-05-10 10:34:51 +000092 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +000093 return 0;
94 }
danielk19776f8a5032004-05-10 10:34:51 +000095 sqlite3_nMalloc++;
drh4305d102003-07-30 12:34:12 +000096 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
97 pi[N_GUARD] = n;
98 for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
99 p = &pi[N_GUARD+1];
drh8c1238a2003-01-02 14:43:55 +0000100 memset(p, bZero==0, n);
drhfaa57ac2004-06-09 14:01:51 +0000101#if SQLITE_DEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000102 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000103 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
104 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000105#endif
drhdcc581c2000-05-30 13:44:19 +0000106 return p;
107}
108
109/*
drhed6c8672003-01-12 18:02:16 +0000110** Check to see if the given pointer was obtained from sqliteMalloc()
111** and is able to hold at least N bytes. Raise an exception if this
112** is not the case.
113**
114** This routine is used for testing purposes only.
115*/
danielk19774adee202004-05-08 08:23:19 +0000116void sqlite3CheckMemory(void *p, int N){
drhed6c8672003-01-12 18:02:16 +0000117 int *pi = p;
drh4305d102003-07-30 12:34:12 +0000118 int n, i, k;
119 pi -= N_GUARD+1;
120 for(i=0; i<N_GUARD; i++){
121 assert( pi[i]==0xdead1122 );
122 }
123 n = pi[N_GUARD];
drhed6c8672003-01-12 18:02:16 +0000124 assert( N>=0 && N<n );
125 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000126 for(i=0; i<N_GUARD; i++){
127 assert( pi[k+N_GUARD+1+i]==0xdead3344 );
128 }
drhed6c8672003-01-12 18:02:16 +0000129}
130
131/*
drhdcc581c2000-05-30 13:44:19 +0000132** Free memory previously obtained from sqliteMalloc()
133*/
danielk19774adee202004-05-08 08:23:19 +0000134void sqlite3Free_(void *p, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +0000135 if( p ){
drh4305d102003-07-30 12:34:12 +0000136 int *pi, i, k, n;
drhdcc581c2000-05-30 13:44:19 +0000137 pi = p;
drh4305d102003-07-30 12:34:12 +0000138 pi -= N_GUARD+1;
danielk19776f8a5032004-05-10 10:34:51 +0000139 sqlite3_nFree++;
drh4305d102003-07-30 12:34:12 +0000140 for(i=0; i<N_GUARD; i++){
141 if( pi[i]!=0xdead1122 ){
142 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
143 return;
144 }
drhdcc581c2000-05-30 13:44:19 +0000145 }
drh4305d102003-07-30 12:34:12 +0000146 n = pi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000147 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000148 for(i=0; i<N_GUARD; i++){
149 if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
150 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
151 return;
152 }
drhdcc581c2000-05-30 13:44:19 +0000153 }
drh4305d102003-07-30 12:34:12 +0000154 memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
drhfaa57ac2004-06-09 14:01:51 +0000155#if SQLITE_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000156 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
157 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000158#endif
drhdcc581c2000-05-30 13:44:19 +0000159 free(pi);
160 }
161}
162
163/*
164** Resize a prior allocation. If p==0, then this routine
165** works just like sqliteMalloc(). If n==0, then this routine
166** works just like sqliteFree().
167*/
danielk19774adee202004-05-08 08:23:19 +0000168void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){
drh4305d102003-07-30 12:34:12 +0000169 int *oldPi, *pi, i, k, oldN, oldK;
drhdcc581c2000-05-30 13:44:19 +0000170 void *p;
171 if( oldP==0 ){
danielk19774adee202004-05-08 08:23:19 +0000172 return sqlite3Malloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000173 }
174 if( n==0 ){
danielk19774adee202004-05-08 08:23:19 +0000175 sqlite3Free_(oldP,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000176 return 0;
177 }
178 oldPi = oldP;
drh4305d102003-07-30 12:34:12 +0000179 oldPi -= N_GUARD+1;
drhdcc581c2000-05-30 13:44:19 +0000180 if( oldPi[0]!=0xdead1122 ){
drh03ab7332003-08-26 11:29:07 +0000181 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
drh9bb61fe2000-06-05 16:01:39 +0000182 return 0;
drhdcc581c2000-05-30 13:44:19 +0000183 }
drh4305d102003-07-30 12:34:12 +0000184 oldN = oldPi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000185 oldK = (oldN+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000186 for(i=0; i<N_GUARD; i++){
187 if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
drh03ab7332003-08-26 11:29:07 +0000188 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
189 (int)oldP);
drh4305d102003-07-30 12:34:12 +0000190 return 0;
191 }
drhdcc581c2000-05-30 13:44:19 +0000192 }
193 k = (n + sizeof(int) - 1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000194 pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000195 if( pi==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000196 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000197 return 0;
198 }
drh4305d102003-07-30 12:34:12 +0000199 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
200 pi[N_GUARD] = n;
201 for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
202 p = &pi[N_GUARD+1];
drhdcc581c2000-05-30 13:44:19 +0000203 memcpy(p, oldP, n>oldN ? oldN : n);
204 if( n>oldN ){
205 memset(&((char*)p)[oldN], 0, n-oldN);
206 }
drh4305d102003-07-30 12:34:12 +0000207 memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000208 free(oldPi);
drhfaa57ac2004-06-09 14:01:51 +0000209#if SQLITE_DEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000210 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000211 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
212 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000213#endif
drhdcc581c2000-05-30 13:44:19 +0000214 return p;
215}
drhc3c2fc92000-05-31 22:58:39 +0000216
217/*
218** Make a duplicate of a string into memory obtained from malloc()
219** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000220**
221** This routine is called on all strings that are passed outside of
222** the SQLite library. That way clients can free the string using free()
223** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000224*/
danielk19774adee202004-05-08 08:23:19 +0000225void sqlite3StrRealloc(char **pz){
drhc3c2fc92000-05-31 22:58:39 +0000226 char *zNew;
227 if( pz==0 || *pz==0 ) return;
228 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000229 if( zNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000230 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000231 sqliteFree(*pz);
232 *pz = 0;
233 }
234 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000235 sqliteFree(*pz);
236 *pz = zNew;
237}
238
drh6e142f52000-06-08 13:36:40 +0000239/*
240** Make a copy of a string in memory obtained from sqliteMalloc()
241*/
danielk19774adee202004-05-08 08:23:19 +0000242char *sqlite3StrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000243 char *zNew;
244 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000245 zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000246 if( zNew ) strcpy(zNew, z);
247 return zNew;
248}
danielk19774adee202004-05-08 08:23:19 +0000249char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000250 char *zNew;
251 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000252 zNew = sqlite3Malloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000253 if( zNew ){
254 memcpy(zNew, z, n);
255 zNew[n] = 0;
256 }
257 return zNew;
258}
danielk1977bfd6cce2004-06-18 04:24:54 +0000259
260/*
261** A version of sqliteFree that is always a function, not a macro.
262*/
263void sqlite3FreeX(void *p){
264 sqliteFree(p);
265}
drhfaa57ac2004-06-09 14:01:51 +0000266#endif /* SQLITE_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000267
drh7c68d602000-10-11 19:28:51 +0000268/*
269** The following versions of malloc() and free() are for use in a
270** normal build.
271*/
drhfaa57ac2004-06-09 14:01:51 +0000272#if !defined(SQLITE_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000273
drh75897232000-05-29 14:26:00 +0000274/*
275** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000276** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000277*/
drh38f82712004-06-18 17:10:16 +0000278void *sqlite3Malloc(int n){
drh26780582002-10-20 15:46:22 +0000279 void *p;
drh8548a052003-10-22 22:15:27 +0000280 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000281 if( n>0 ) sqlite3_malloc_failed++;
drh8548a052003-10-22 22:15:27 +0000282 }else{
283 memset(p, 0, n);
drhdaffd0e2001-04-11 14:28:42 +0000284 }
drh75897232000-05-29 14:26:00 +0000285 return p;
286}
287
288/*
drh8c1238a2003-01-02 14:43:55 +0000289** Allocate new memory but do not set it to zero. Return NULL if
290** no memory is available. See also sqliteMalloc().
291*/
drh38f82712004-06-18 17:10:16 +0000292void *sqlite3MallocRaw(int n){
drh8c1238a2003-01-02 14:43:55 +0000293 void *p;
drh8548a052003-10-22 22:15:27 +0000294 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000295 if( n>0 ) sqlite3_malloc_failed++;
drh8c1238a2003-01-02 14:43:55 +0000296 }
297 return p;
298}
299
300/*
drh75897232000-05-29 14:26:00 +0000301** Free memory previously obtained from sqliteMalloc()
302*/
drh38f82712004-06-18 17:10:16 +0000303void sqlite3FreeX(void *p){
drh305cea62000-05-29 17:44:25 +0000304 if( p ){
drh305cea62000-05-29 17:44:25 +0000305 free(p);
306 }
drh75897232000-05-29 14:26:00 +0000307}
308
309/*
310** Resize a prior allocation. If p==0, then this routine
311** works just like sqliteMalloc(). If n==0, then this routine
312** works just like sqliteFree().
313*/
drh38f82712004-06-18 17:10:16 +0000314void *sqlite3Realloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000315 void *p2;
drh75897232000-05-29 14:26:00 +0000316 if( p==0 ){
317 return sqliteMalloc(n);
318 }
319 if( n==0 ){
320 sqliteFree(p);
321 return 0;
322 }
drh6d4abfb2001-10-22 02:58:08 +0000323 p2 = realloc(p, n);
324 if( p2==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000325 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000326 }
drh6d4abfb2001-10-22 02:58:08 +0000327 return p2;
drh75897232000-05-29 14:26:00 +0000328}
drh6e142f52000-06-08 13:36:40 +0000329
330/*
331** Make a copy of a string in memory obtained from sqliteMalloc()
332*/
drh38f82712004-06-18 17:10:16 +0000333char *sqlite3StrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000334 char *zNew;
335 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000336 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000337 if( zNew ) strcpy(zNew, z);
338 return zNew;
339}
drh38f82712004-06-18 17:10:16 +0000340char *sqlite3StrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000341 char *zNew;
342 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000343 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000344 if( zNew ){
345 memcpy(zNew, z, n);
346 zNew[n] = 0;
347 }
348 return zNew;
349}
drhfaa57ac2004-06-09 14:01:51 +0000350#endif /* !defined(SQLITE_DEBUG) */
drh75897232000-05-29 14:26:00 +0000351
352/*
353** Create a string from the 2nd and subsequent arguments (up to the
354** first NULL argument), store the string in memory obtained from
355** sqliteMalloc() and make the pointer indicated by the 1st argument
jplyon02be20d2003-06-02 06:17:10 +0000356** point to that string. The 1st argument must either be NULL or
357** point to memory obtained from sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000358*/
danielk19774adee202004-05-08 08:23:19 +0000359void sqlite3SetString(char **pz, const char *zFirst, ...){
drh75897232000-05-29 14:26:00 +0000360 va_list ap;
361 int nByte;
362 const char *z;
363 char *zResult;
364
365 if( pz==0 ) return;
366 nByte = strlen(zFirst) + 1;
367 va_start(ap, zFirst);
368 while( (z = va_arg(ap, const char*))!=0 ){
369 nByte += strlen(z);
370 }
371 va_end(ap);
372 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000373 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000374 if( zResult==0 ){
375 return;
376 }
drh75897232000-05-29 14:26:00 +0000377 strcpy(zResult, zFirst);
378 zResult += strlen(zResult);
379 va_start(ap, zFirst);
380 while( (z = va_arg(ap, const char*))!=0 ){
381 strcpy(zResult, z);
382 zResult += strlen(zResult);
383 }
384 va_end(ap);
drhfaa57ac2004-06-09 14:01:51 +0000385#ifdef SQLITE_DEBUG
386#if SQLITE_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000387 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
388#endif
389#endif
drh75897232000-05-29 14:26:00 +0000390}
391
392/*
danielk19774adee202004-05-08 08:23:19 +0000393** Works like sqlite3SetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000394** a length integer which specifies how much of the source string
jplyon02be20d2003-06-02 06:17:10 +0000395** to copy (in bytes). -1 means use the whole string. The 1st
396** argument must either be NULL or point to memory obtained from
397** sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000398*/
danielk19774adee202004-05-08 08:23:19 +0000399void sqlite3SetNString(char **pz, ...){
drh75897232000-05-29 14:26:00 +0000400 va_list ap;
401 int nByte;
402 const char *z;
403 char *zResult;
404 int n;
405
406 if( pz==0 ) return;
407 nByte = 0;
408 va_start(ap, pz);
409 while( (z = va_arg(ap, const char*))!=0 ){
410 n = va_arg(ap, int);
411 if( n<=0 ) n = strlen(z);
412 nByte += n;
413 }
414 va_end(ap);
415 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000416 *pz = zResult = sqliteMallocRaw( nByte + 1 );
drh75897232000-05-29 14:26:00 +0000417 if( zResult==0 ) return;
418 va_start(ap, pz);
419 while( (z = va_arg(ap, const char*))!=0 ){
420 n = va_arg(ap, int);
421 if( n<=0 ) n = strlen(z);
drh51846b52004-05-28 16:00:21 +0000422 memcpy(zResult, z, n);
drh75897232000-05-29 14:26:00 +0000423 zResult += n;
424 }
425 *zResult = 0;
drhfaa57ac2004-06-09 14:01:51 +0000426#ifdef SQLITE_DEBUG
427#if SQLITE_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000428 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
429#endif
430#endif
drh75897232000-05-29 14:26:00 +0000431 va_end(ap);
432}
433
drh982cef72000-05-30 16:27:03 +0000434/*
danielk19776622cce2004-05-20 11:00:52 +0000435** Set the most recent error code and error string for the sqlite
436** handle "db". The error code is set to "err_code".
437**
438** If it is not NULL, string zFormat specifies the format of the
439** error string in the style of the printf functions: The following
440** format characters are allowed:
441**
442** %s Insert a string
443** %z A string that should be freed after use
444** %d Insert an integer
445** %T Insert a token
446** %S Insert the first element of a SrcList
447**
448** zFormat and any string tokens that follow it are assumed to be
449** encoded in UTF-8.
450**
451** To clear the most recent error for slqite handle "db", sqlite3Error
452** should be called with err_code set to SQLITE_OK and zFormat set
453** to NULL.
454*/
455void sqlite3Error(sqlite *db, int err_code, const char *zFormat, ...){
danielk1977bfd6cce2004-06-18 04:24:54 +0000456 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){
457 db->errCode = err_code;
458 if( zFormat ){
459 char *z;
460 va_list ap;
461 va_start(ap, zFormat);
462 z = sqlite3VMPrintf(zFormat, ap);
463 va_end(ap);
464 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
465 }else{
466 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
467 }
danielk19776622cce2004-05-20 11:00:52 +0000468 }
469}
470
471/*
drhda93d232003-03-31 02:12:46 +0000472** Add an error message to pParse->zErrMsg and increment pParse->nErr.
473** The following formatting characters are allowed:
474**
475** %s Insert a string
476** %z A string that should be freed after use
477** %d Insert an integer
478** %T Insert a token
479** %S Insert the first element of a SrcList
danielk19779e6db7d2004-06-21 08:18:51 +0000480**
481** This function should be used to report any error that occurs whilst
482** compiling an SQL statement (i.e. within sqlite3_prepare()). The
483** last thing the sqlite3_prepare() function does is copy the error
484** stored by this function into the database handle using sqlite3Error().
485** Function sqlite3Error() should be used during statement execution
486** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +0000487*/
danielk19774adee202004-05-08 08:23:19 +0000488void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000489 va_list ap;
drhda93d232003-03-31 02:12:46 +0000490 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +0000491 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000492 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +0000493 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000494 va_end(ap);
drhda93d232003-03-31 02:12:46 +0000495}
496
497/*
drh982cef72000-05-30 16:27:03 +0000498** Convert an SQL-style quoted string into a normal string by removing
499** the quote characters. The conversion is done in-place. If the
500** input does not begin with a quote character, then this routine
501** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000502**
503** 2002-Feb-14: This routine is extended to remove MS-Access style
504** brackets from around identifers. For example: "[a-b-c]" becomes
505** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000506*/
danielk19774adee202004-05-08 08:23:19 +0000507void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000508 int quote;
509 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000510 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000511 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000512 switch( quote ){
513 case '\'': break;
514 case '"': break;
515 case '[': quote = ']'; break;
516 default: return;
517 }
drh982cef72000-05-30 16:27:03 +0000518 for(i=1, j=0; z[i]; i++){
519 if( z[i]==quote ){
520 if( z[i+1]==quote ){
521 z[j++] = quote;
522 i++;
523 }else{
524 z[j++] = 0;
525 break;
526 }
527 }else{
528 z[j++] = z[i];
529 }
530 }
531}
532
drh75897232000-05-29 14:26:00 +0000533/* An array to map all upper-case characters into their corresponding
534** lower-case character.
535*/
536static unsigned char UpperToLower[] = {
537 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
538 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
539 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
540 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
541 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
542 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
543 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
544 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
545 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
546 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
547 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
548 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
549 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
550 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
551 252,253,254,255
552};
553
554/*
555** This function computes a hash on the name of a keyword.
556** Case is not significant.
557*/
danielk19774adee202004-05-08 08:23:19 +0000558int sqlite3HashNoCase(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000559 int h = 0;
drh75897232000-05-29 14:26:00 +0000560 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000561 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000562 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000563 n--;
drh75897232000-05-29 14:26:00 +0000564 }
drh5364f602003-05-12 23:06:52 +0000565 return h & 0x7fffffff;
drh75897232000-05-29 14:26:00 +0000566}
567
568/*
drh967e8b72000-06-21 13:59:10 +0000569** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000570** there is no consistency, we will define our own.
571*/
danielk19774adee202004-05-08 08:23:19 +0000572int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000573 register unsigned char *a, *b;
574 a = (unsigned char *)zLeft;
575 b = (unsigned char *)zRight;
576 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
577 return *a - *b;
578}
danielk19774adee202004-05-08 08:23:19 +0000579int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000580 register unsigned char *a, *b;
581 a = (unsigned char *)zLeft;
582 b = (unsigned char *)zRight;
583 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000584 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000585}
586
drha5c2ad02000-09-14 01:21:10 +0000587/*
drh7a7c7392001-11-24 00:31:46 +0000588** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000589** string contains any character which is not part of a number. If
590** the string is numeric and contains the '.' character, set *realnum
591** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000592**
drhbb07e9a2003-04-16 02:17:35 +0000593** Am empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000594*/
danielk19778a6b5412004-05-24 07:04:25 +0000595int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000596 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000597 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000598 if( *z=='-' || *z=='+' ) z += incr;
drh7a7c7392001-11-24 00:31:46 +0000599 if( !isdigit(*z) ){
drhbb07e9a2003-04-16 02:17:35 +0000600 return 0;
drha5c2ad02000-09-14 01:21:10 +0000601 }
danielk19778a6b5412004-05-24 07:04:25 +0000602 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000603 if( realnum ) *realnum = 0;
danielk19778a6b5412004-05-24 07:04:25 +0000604 while( isdigit(*z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000605 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000606 z += incr;
drh7a7c7392001-11-24 00:31:46 +0000607 if( !isdigit(*z) ) return 0;
danielk19778a6b5412004-05-24 07:04:25 +0000608 while( isdigit(*z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000609 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000610 }
611 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000612 z += incr;
613 if( *z=='+' || *z=='-' ) z += incr;
drhbb07e9a2003-04-16 02:17:35 +0000614 if( !isdigit(*z) ) return 0;
danielk19778a6b5412004-05-24 07:04:25 +0000615 while( isdigit(*z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000616 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000617 }
drh7a7c7392001-11-24 00:31:46 +0000618 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000619}
620
drh93a5c6b2003-12-23 02:17:35 +0000621/*
622** The string z[] is an ascii representation of a real number.
623** Convert this string to a double.
624**
625** This routine assumes that z[] really is a valid number. If it
626** is not, the result is undefined.
627**
628** This routine is used instead of the library atof() function because
629** the library atof() might want to use "," as the decimal point instead
630** of "." depending on how locale is set. But that would cause problems
631** for SQL. So this routine always uses "." regardless of locale.
632*/
danielk19774adee202004-05-08 08:23:19 +0000633double sqlite3AtoF(const char *z, const char **pzEnd){
drh93a5c6b2003-12-23 02:17:35 +0000634 int sign = 1;
drh384eef32004-01-07 03:04:27 +0000635 LONGDOUBLE_TYPE v1 = 0.0;
drh93a5c6b2003-12-23 02:17:35 +0000636 if( *z=='-' ){
637 sign = -1;
638 z++;
639 }else if( *z=='+' ){
640 z++;
641 }
642 while( isdigit(*z) ){
643 v1 = v1*10.0 + (*z - '0');
644 z++;
645 }
646 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000647 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000648 z++;
649 while( isdigit(*z) ){
650 v1 = v1*10.0 + (*z - '0');
651 divisor *= 10.0;
652 z++;
653 }
654 v1 /= divisor;
655 }
656 if( *z=='e' || *z=='E' ){
657 int esign = 1;
658 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000659 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000660 z++;
661 if( *z=='-' ){
662 esign = -1;
663 z++;
664 }else if( *z=='+' ){
665 z++;
666 }
667 while( isdigit(*z) ){
668 eval = eval*10 + *z - '0';
669 z++;
670 }
671 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
672 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
673 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
674 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
675 if( esign<0 ){
676 v1 /= scale;
677 }else{
678 v1 *= scale;
679 }
680 }
drheb9a9e82004-02-22 17:49:32 +0000681 if( pzEnd ) *pzEnd = z;
drh93a5c6b2003-12-23 02:17:35 +0000682 return sign<0 ? -v1 : v1;
683}
684
drh202b2df2004-01-06 01:13:46 +0000685/*
drhfec19aa2004-05-19 20:41:03 +0000686** Return TRUE if zNum is a 64-bit signed integer and write
687** the value of the integer into *pNum. If zNum is not an integer
688** or is an integer that is too large to be expressed with 64 bits,
689** then return false. If n>0 and the integer is string is not
690** exactly n bytes long, return false.
691**
692** When this routine was originally written it dealt with only
693** 32-bit numbers. At that time, it was much faster than the
694** atoi() library routine in RedHat 7.2.
695*/
drheb2e1762004-05-27 01:53:56 +0000696int sqlite3atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000697 i64 v = 0;
698 int neg;
699 int i, c;
700 if( *zNum=='-' ){
701 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000702 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000703 }else if( *zNum=='+' ){
704 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000705 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000706 }else{
707 neg = 0;
708 }
drheb2e1762004-05-27 01:53:56 +0000709 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000710 v = v*10 + c - '0';
711 }
712 *pNum = neg ? -v : v;
713 return c==0 && i>0 &&
714 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
715}
716
717/*
drh202b2df2004-01-06 01:13:46 +0000718** The string zNum represents an integer. There might be some other
719** information following the integer too, but that part is ignored.
720** If the integer that the prefix of zNum represents will fit in a
721** 32-bit signed integer, return TRUE. Otherwise return FALSE.
722**
723** This routine returns FALSE for the string -2147483648 even that
724** that number will, in theory fit in a 32-bit integer. But positive
725** 2147483648 will not fit in 32 bits. So it seems safer to return
726** false.
727*/
drhfec19aa2004-05-19 20:41:03 +0000728static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000729 int i, c;
730 if( *zNum=='-' || *zNum=='+' ) zNum++;
731 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
732 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
733}
734
drhfec19aa2004-05-19 20:41:03 +0000735/*
736** If zNum represents an integer that will fit in 32-bits, then set
737** *pValue to that integer and return true. Otherwise return false.
738*/
739int sqlite3GetInt32(const char *zNum, int *pValue){
740 if( sqlite3FitsIn32Bits(zNum) ){
741 *pValue = atoi(zNum);
742 return 1;
743 }
744 return 0;
745}
746
747/*
748** The string zNum represents an integer. There might be some other
749** information following the integer too, but that part is ignored.
750** If the integer that the prefix of zNum represents will fit in a
751** 64-bit signed integer, return TRUE. Otherwise return FALSE.
752**
753** This routine returns FALSE for the string -9223372036854775808 even that
754** that number will, in theory fit in a 64-bit integer. Positive
755** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
756** false.
757*/
758int sqlite3FitsIn64Bits(const char *zNum){
759 int i, c;
760 if( *zNum=='-' || *zNum=='+' ) zNum++;
761 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
762 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
763}
764
765/*
766** If zNum represents an integer that will fit in 64-bits, then set
767** *pValue to that integer and return true. Otherwise return false.
768*/
769int sqlite3GetInt64(const char *zNum, i64 *pValue){
770 if( sqlite3FitsIn64Bits(zNum) ){
drhf4479502004-05-27 03:12:53 +0000771 sqlite3atoi64(zNum, pValue);
drhfec19aa2004-05-19 20:41:03 +0000772 return 1;
773 }
774 return 0;
775}
776
drhdf014892004-06-02 00:41:09 +0000777#if 1 /* We are now always UTF-8 */
drhdce2cbe2000-05-31 02:27:49 +0000778/*
drh297ecf12001-04-05 15:57:13 +0000779** X is a pointer to the first byte of a UTF-8 character. Increment
780** X so that it points to the next character. This only works right
781** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000782*/
drh297ecf12001-04-05 15:57:13 +0000783#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
drhdf014892004-06-02 00:41:09 +0000784#define sqliteCharVal(X) sqlite3ReadUtf8(X)
drhe17a7e32001-04-04 21:10:18 +0000785
drh297ecf12001-04-05 15:57:13 +0000786#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000787/*
drh297ecf12001-04-05 15:57:13 +0000788** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000789*/
drh297ecf12001-04-05 15:57:13 +0000790#define sqliteNextChar(X) (++(X));
791#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000792
drh297ecf12001-04-05 15:57:13 +0000793#endif /* defined(SQLITE_UTF8) */
794
795
drhdf014892004-06-02 00:41:09 +0000796#if 1 /* We are now always UTF-8 */
drhe17a7e32001-04-04 21:10:18 +0000797/*
drh297ecf12001-04-05 15:57:13 +0000798** Convert the UTF-8 character to which z points into a 31-bit
799** UCS character. This only works right if z points to a well-formed
800** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000801*/
danielk1977ad7dd422004-06-06 12:41:49 +0000802int sqlite3ReadUtf8(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000803 int c;
drh297ecf12001-04-05 15:57:13 +0000804 static const int initVal[] = {
805 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
806 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
807 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
808 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
809 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
810 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
811 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
812 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
813 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
814 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
815 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
816 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
817 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
818 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
819 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
820 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
821 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
822 255,
823 };
824 c = initVal[*(z++)];
825 while( (0xc0&*z)==0x80 ){
826 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000827 }
828 return c;
829}
drh297ecf12001-04-05 15:57:13 +0000830#endif
drhe17a7e32001-04-04 21:10:18 +0000831
832/*
833** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000834** potentially be a "glob" expression. Return true (1) if they
835** are the same and false (0) if they are different.
836**
837** Globbing rules:
838**
839** '*' Matches any sequence of zero or more characters.
840**
841** '?' Matches exactly one character.
842**
843** [...] Matches one character from the enclosed list of
844** characters.
845**
846** [^...] Matches one character not in the enclosed list.
847**
848** With the [...] and [^...] matching, a ']' character can be included
849** in the list by making it the first character after '[' or '^'. A
850** range of characters can be specified using '-'. Example:
851** "[a-z]" matches any single lower-case letter. To match a '-', make
852** it the last character in the list.
853**
854** This routine is usually quick, but can be N**2 in the worst case.
855**
856** Hints: to match '*' or '?', put them in "[]". Like this:
857**
858** abc[*]xyz Matches "abc*xyz" only
859*/
drhe17a7e32001-04-04 21:10:18 +0000860int
danielk19774adee202004-05-08 08:23:19 +0000861sqlite3GlobCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +0000862 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000863 int invert;
864 int seen;
drhe17a7e32001-04-04 21:10:18 +0000865 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000866
867 while( (c = *zPattern)!=0 ){
868 switch( c ){
869 case '*':
drhe17a7e32001-04-04 21:10:18 +0000870 while( (c=zPattern[1]) == '*' || c == '?' ){
871 if( c=='?' ){
872 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000873 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000874 }
875 zPattern++;
876 }
877 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +0000878 if( c=='[' ){
danielk19774adee202004-05-08 08:23:19 +0000879 while( *zString && sqlite3GlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000880 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000881 }
882 return *zString!=0;
883 }else{
884 while( (c2 = *zString)!=0 ){
885 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000886 if( c2==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000887 if( sqlite3GlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000888 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000889 }
890 return 0;
891 }
drhe17a7e32001-04-04 21:10:18 +0000892 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000893 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000894 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000895 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000896 break;
drhe17a7e32001-04-04 21:10:18 +0000897 }
898 case '[': {
899 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000900 seen = 0;
901 invert = 0;
drh297ecf12001-04-05 15:57:13 +0000902 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +0000903 if( c==0 ) return 0;
904 c2 = *++zPattern;
905 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
906 if( c2==']' ){
907 if( c==']' ) seen = 1;
908 c2 = *++zPattern;
909 }
drh297ecf12001-04-05 15:57:13 +0000910 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +0000911 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
912 zPattern++;
drh297ecf12001-04-05 15:57:13 +0000913 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +0000914 if( c>=prior_c && c<=c2 ) seen = 1;
915 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000916 }else if( c==c2 ){
917 seen = 1;
drhe17a7e32001-04-04 21:10:18 +0000918 prior_c = c2;
919 }else{
920 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +0000921 }
drh297ecf12001-04-05 15:57:13 +0000922 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +0000923 }
924 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000925 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000926 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000927 break;
drhe17a7e32001-04-04 21:10:18 +0000928 }
929 default: {
drhdce2cbe2000-05-31 02:27:49 +0000930 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +0000931 zPattern++;
932 zString++;
drhdce2cbe2000-05-31 02:27:49 +0000933 break;
drhe17a7e32001-04-04 21:10:18 +0000934 }
drhdce2cbe2000-05-31 02:27:49 +0000935 }
drhdce2cbe2000-05-31 02:27:49 +0000936 }
937 return *zString==0;
938}
939
940/*
drhc22bd472002-05-10 13:14:07 +0000941** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
942** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
943** when this routine is called.
944**
945** This routine is a attempt to detect if two threads use the
946** same sqlite* pointer at the same time. There is a race
947** condition so it is possible that the error is not detected.
948** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +0000949** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +0000950** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +0000951**
952** Ticket #202: If db->magic is not a valid open value, take care not
953** to modify the db structure at all. It could be that db is a stale
954** pointer. In other words, it could be that there has been a prior
danielk19776f8a5032004-05-10 10:34:51 +0000955** call to sqlite3_close(db) and db has been deallocated. And we do
drhe7e8bc72002-12-17 13:05:25 +0000956** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +0000957*/
danielk19774adee202004-05-08 08:23:19 +0000958int sqlite3SafetyOn(sqlite *db){
drhc22bd472002-05-10 13:14:07 +0000959 if( db->magic==SQLITE_MAGIC_OPEN ){
960 db->magic = SQLITE_MAGIC_BUSY;
961 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000962 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000963 db->magic = SQLITE_MAGIC_ERROR;
964 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +0000965 }
drhe7e8bc72002-12-17 13:05:25 +0000966 return 1;
drhc22bd472002-05-10 13:14:07 +0000967}
968
969/*
970** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
971** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
972** when this routine is called.
973*/
danielk19774adee202004-05-08 08:23:19 +0000974int sqlite3SafetyOff(sqlite *db){
drhc22bd472002-05-10 13:14:07 +0000975 if( db->magic==SQLITE_MAGIC_BUSY ){
976 db->magic = SQLITE_MAGIC_OPEN;
977 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000978 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000979 db->magic = SQLITE_MAGIC_ERROR;
980 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +0000981 }
drhe7e8bc72002-12-17 13:05:25 +0000982 return 1;
drhc22bd472002-05-10 13:14:07 +0000983}
984
985/*
danielk19776f8a5032004-05-10 10:34:51 +0000986** Check to make sure we are not currently executing an sqlite3_exec().
987** If we are currently in an sqlite3_exec(), return true and set
drhc22bd472002-05-10 13:14:07 +0000988** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
989** shutdown of the database.
990**
991** This routine is used to try to detect when API routines are called
992** at the wrong time or in the wrong sequence.
993*/
danielk19774adee202004-05-08 08:23:19 +0000994int sqlite3SafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +0000995 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +0000996 db->magic = SQLITE_MAGIC_ERROR;
997 return 1;
998 }
999 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001000}
danielk19774adee202004-05-08 08:23:19 +00001001
drh6d2fb152004-05-14 16:50:06 +00001002/*
drhd8820e82004-05-18 15:57:42 +00001003** The variable-length integer encoding is as follows:
1004**
1005** KEY:
1006** A = 0xxxxxxx 7 bits of data and one flag bit
1007** B = 1xxxxxxx 7 bits of data and one flag bit
1008** C = xxxxxxxx 8 bits of data
1009**
1010** 7 bits - A
1011** 14 bits - BA
1012** 21 bits - BBA
1013** 28 bits - BBBA
1014** 35 bits - BBBBA
1015** 42 bits - BBBBBA
1016** 49 bits - BBBBBBA
1017** 56 bits - BBBBBBBA
1018** 64 bits - BBBBBBBBC
1019*/
1020
1021/*
drh6d2fb152004-05-14 16:50:06 +00001022** Write a 64-bit variable-length integer to memory starting at p[0].
1023** The length of data write will be between 1 and 9 bytes. The number
1024** of bytes written is returned.
1025**
1026** A variable-length integer consists of the lower 7 bits of each byte
1027** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +00001028** bit clear. Except, if we get to the 9th byte, it stores the full
1029** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +00001030*/
danielk1977192ac1d2004-05-10 07:17:30 +00001031int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +00001032 int i, j, n;
1033 u8 buf[10];
drhd8820e82004-05-18 15:57:42 +00001034 if( v & 0xff00000000000000 ){
1035 p[8] = v;
1036 v >>= 8;
1037 for(i=7; i>=0; i--){
1038 p[i] = (v & 0x7f) | 0x80;
1039 v >>= 7;
1040 }
1041 return 9;
1042 }
drh6d2fb152004-05-14 16:50:06 +00001043 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001044 do{
drh6d2fb152004-05-14 16:50:06 +00001045 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +00001046 v >>= 7;
1047 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +00001048 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +00001049 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +00001050 for(i=0, j=n-1; j>=0; j--, i++){
1051 p[i] = buf[j];
1052 }
1053 return n;
danielk1977192ac1d2004-05-10 07:17:30 +00001054}
danielk19774adee202004-05-08 08:23:19 +00001055
drh6d2fb152004-05-14 16:50:06 +00001056/*
1057** Read a 64-bit variable-length integer from memory starting at p[0].
1058** Return the number of bytes read. The value is stored in *v.
1059*/
danielk197790e4d952004-05-10 10:05:53 +00001060int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +00001061 u32 x;
1062 u64 x64;
1063 int n;
1064 unsigned char c;
drhe51c44f2004-05-30 20:46:09 +00001065 if( ((c = p[0]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001066 *v = c;
1067 return 1;
1068 }
1069 x = c & 0x7f;
drhe51c44f2004-05-30 20:46:09 +00001070 if( ((c = p[1]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001071 *v = (x<<7) | c;
1072 return 2;
1073 }
1074 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +00001075 if( ((c = p[2]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001076 *v = (x<<7) | c;
1077 return 3;
1078 }
1079 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +00001080 if( ((c = p[3]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001081 *v = (x<<7) | c;
1082 return 4;
1083 }
1084 x64 = (x<<7) | (c&0x7f);
1085 n = 4;
1086 do{
1087 c = p[n++];
drhd8820e82004-05-18 15:57:42 +00001088 if( n==9 ){
1089 x64 = (x64<<8) | c;
1090 break;
1091 }
drh6d2fb152004-05-14 16:50:06 +00001092 x64 = (x64<<7) | (c&0x7f);
1093 }while( (c & 0x80)!=0 );
1094 *v = x64;
1095 return n;
1096}
1097
1098/*
1099** Read a 32-bit variable-length integer from memory starting at p[0].
1100** Return the number of bytes read. The value is stored in *v.
1101*/
1102int sqlite3GetVarint32(const unsigned char *p, u32 *v){
drhe51c44f2004-05-30 20:46:09 +00001103 u32 x;
1104 int n;
1105 unsigned char c;
1106 if( ((c = p[0]) & 0x80)==0 ){
1107 *v = c;
1108 return 1;
danielk1977192ac1d2004-05-10 07:17:30 +00001109 }
drhe51c44f2004-05-30 20:46:09 +00001110 x = c & 0x7f;
1111 if( ((c = p[1]) & 0x80)==0 ){
1112 *v = (x<<7) | c;
1113 return 2;
1114 }
1115 x = (x<<7) | (c&0x7f);
1116 if( ((c = p[2]) & 0x80)==0 ){
1117 *v = (x<<7) | c;
1118 return 3;
1119 }
1120 x = (x<<7) | (c&0x7f);
1121 if( ((c = p[3]) & 0x80)==0 ){
1122 *v = (x<<7) | c;
1123 return 4;
1124 }
1125 n = 4;
1126 do{
1127 x = (x<<7) | ((c = p[n++])&0x7f);
1128 }while( (c & 0x80)!=0 && n<9 );
danielk1977192ac1d2004-05-10 07:17:30 +00001129 *v = x;
1130 return n;
1131}
1132
drh6d2fb152004-05-14 16:50:06 +00001133/*
1134** Return the number of bytes that will be needed to store the given
1135** 64-bit integer.
1136*/
danielk1977192ac1d2004-05-10 07:17:30 +00001137int sqlite3VarintLen(u64 v){
1138 int i = 0;
1139 do{
1140 i++;
1141 v >>= 7;
drhd8820e82004-05-18 15:57:42 +00001142 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +00001143 return i;
1144}
danielk1977c572ef72004-05-27 09:28:41 +00001145
danielk1977cbb18d22004-05-28 11:37:27 +00001146void *sqlite3HexToBlob(const char *z){
danielk1977c572ef72004-05-27 09:28:41 +00001147 char *zBlob;
1148 int i;
1149 int n = strlen(z);
1150 if( n%2 ) return 0;
1151
1152 zBlob = (char *)sqliteMalloc(n/2);
1153
danielk19773fd0a732004-05-27 13:35:19 +00001154 for(i=0; i<n; i++){
danielk1977c572ef72004-05-27 09:28:41 +00001155 u8 c;
1156
1157 if ( z[i]>47 && z[i]<58 ) c = (z[i]-48)<<4;
1158 else if( z[i]>64 && z[i]<71 ) c = (z[i]-55)<<4;
1159 else if( z[i]>96 && z[i]<103 ) c = (z[i]-87)<<4;
1160 else {
1161 sqliteFree(zBlob);
1162 return 0;
1163 }
danielk19773fd0a732004-05-27 13:35:19 +00001164 i++;
danielk1977c572ef72004-05-27 09:28:41 +00001165 if ( z[i]>47 && z[i]<58 ) c += (z[i]-48);
1166 else if( z[i]>64 && z[i]<71 ) c += (z[i]-55);
1167 else if( z[i]>96 && z[i]<103 ) c += (z[i]-87);
1168 else {
1169 sqliteFree(zBlob);
1170 return 0;
1171 }
1172
1173 zBlob[i/2] = c;
1174 }
danielk19773fd0a732004-05-27 13:35:19 +00001175 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +00001176}