blob: 29cd5611267b61111979a03e82431d0fd9f94896 [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**
danielk1977bfd6cce2004-06-18 04:24:54 +000017** $Id: util.c,v 1.103 2004/06/18 04:24:55 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*/
278void *sqliteMalloc(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*/
292void *sqliteMallocRaw(int n){
293 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*/
303void sqliteFree(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*/
314void *sqliteRealloc(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*/
333char *sqliteStrDup(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}
340char *sqliteStrNDup(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
480*/
danielk19774adee202004-05-08 08:23:19 +0000481void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000482 va_list ap;
drhda93d232003-03-31 02:12:46 +0000483 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +0000484 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000485 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +0000486 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000487 va_end(ap);
drhda93d232003-03-31 02:12:46 +0000488}
489
490/*
drh982cef72000-05-30 16:27:03 +0000491** Convert an SQL-style quoted string into a normal string by removing
492** the quote characters. The conversion is done in-place. If the
493** input does not begin with a quote character, then this routine
494** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000495**
496** 2002-Feb-14: This routine is extended to remove MS-Access style
497** brackets from around identifers. For example: "[a-b-c]" becomes
498** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000499*/
danielk19774adee202004-05-08 08:23:19 +0000500void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000501 int quote;
502 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000503 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000504 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000505 switch( quote ){
506 case '\'': break;
507 case '"': break;
508 case '[': quote = ']'; break;
509 default: return;
510 }
drh982cef72000-05-30 16:27:03 +0000511 for(i=1, j=0; z[i]; i++){
512 if( z[i]==quote ){
513 if( z[i+1]==quote ){
514 z[j++] = quote;
515 i++;
516 }else{
517 z[j++] = 0;
518 break;
519 }
520 }else{
521 z[j++] = z[i];
522 }
523 }
524}
525
drh75897232000-05-29 14:26:00 +0000526/* An array to map all upper-case characters into their corresponding
527** lower-case character.
528*/
529static unsigned char UpperToLower[] = {
530 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
531 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
532 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
533 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
534 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
535 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
536 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
537 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
538 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
539 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
540 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
541 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
542 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
543 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
544 252,253,254,255
545};
546
547/*
548** This function computes a hash on the name of a keyword.
549** Case is not significant.
550*/
danielk19774adee202004-05-08 08:23:19 +0000551int sqlite3HashNoCase(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000552 int h = 0;
drh75897232000-05-29 14:26:00 +0000553 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000554 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000555 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000556 n--;
drh75897232000-05-29 14:26:00 +0000557 }
drh5364f602003-05-12 23:06:52 +0000558 return h & 0x7fffffff;
drh75897232000-05-29 14:26:00 +0000559}
560
561/*
drh967e8b72000-06-21 13:59:10 +0000562** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000563** there is no consistency, we will define our own.
564*/
danielk19774adee202004-05-08 08:23:19 +0000565int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000566 register unsigned char *a, *b;
567 a = (unsigned char *)zLeft;
568 b = (unsigned char *)zRight;
569 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
570 return *a - *b;
571}
danielk19774adee202004-05-08 08:23:19 +0000572int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000573 register unsigned char *a, *b;
574 a = (unsigned char *)zLeft;
575 b = (unsigned char *)zRight;
576 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000577 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000578}
579
drha5c2ad02000-09-14 01:21:10 +0000580/*
drh7a7c7392001-11-24 00:31:46 +0000581** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000582** string contains any character which is not part of a number. If
583** the string is numeric and contains the '.' character, set *realnum
584** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000585**
drhbb07e9a2003-04-16 02:17:35 +0000586** Am empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000587*/
danielk19778a6b5412004-05-24 07:04:25 +0000588int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000589 int incr = (enc==SQLITE_UTF8?1:2);
590 if( enc==SQLITE_UTF16LE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000591 if( *z=='-' || *z=='+' ) z += incr;
drh7a7c7392001-11-24 00:31:46 +0000592 if( !isdigit(*z) ){
drhbb07e9a2003-04-16 02:17:35 +0000593 return 0;
drha5c2ad02000-09-14 01:21:10 +0000594 }
danielk19778a6b5412004-05-24 07:04:25 +0000595 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000596 if( realnum ) *realnum = 0;
danielk19778a6b5412004-05-24 07:04:25 +0000597 while( isdigit(*z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000598 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000599 z += incr;
drh7a7c7392001-11-24 00:31:46 +0000600 if( !isdigit(*z) ) return 0;
danielk19778a6b5412004-05-24 07:04:25 +0000601 while( isdigit(*z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000602 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000603 }
604 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000605 z += incr;
606 if( *z=='+' || *z=='-' ) z += incr;
drhbb07e9a2003-04-16 02:17:35 +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;
drha5c2ad02000-09-14 01:21:10 +0000610 }
drh7a7c7392001-11-24 00:31:46 +0000611 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000612}
613
drh93a5c6b2003-12-23 02:17:35 +0000614/*
615** The string z[] is an ascii representation of a real number.
616** Convert this string to a double.
617**
618** This routine assumes that z[] really is a valid number. If it
619** is not, the result is undefined.
620**
621** This routine is used instead of the library atof() function because
622** the library atof() might want to use "," as the decimal point instead
623** of "." depending on how locale is set. But that would cause problems
624** for SQL. So this routine always uses "." regardless of locale.
625*/
danielk19774adee202004-05-08 08:23:19 +0000626double sqlite3AtoF(const char *z, const char **pzEnd){
drh93a5c6b2003-12-23 02:17:35 +0000627 int sign = 1;
drh384eef32004-01-07 03:04:27 +0000628 LONGDOUBLE_TYPE v1 = 0.0;
drh93a5c6b2003-12-23 02:17:35 +0000629 if( *z=='-' ){
630 sign = -1;
631 z++;
632 }else if( *z=='+' ){
633 z++;
634 }
635 while( isdigit(*z) ){
636 v1 = v1*10.0 + (*z - '0');
637 z++;
638 }
639 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000640 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000641 z++;
642 while( isdigit(*z) ){
643 v1 = v1*10.0 + (*z - '0');
644 divisor *= 10.0;
645 z++;
646 }
647 v1 /= divisor;
648 }
649 if( *z=='e' || *z=='E' ){
650 int esign = 1;
651 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000652 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000653 z++;
654 if( *z=='-' ){
655 esign = -1;
656 z++;
657 }else if( *z=='+' ){
658 z++;
659 }
660 while( isdigit(*z) ){
661 eval = eval*10 + *z - '0';
662 z++;
663 }
664 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
665 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
666 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
667 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
668 if( esign<0 ){
669 v1 /= scale;
670 }else{
671 v1 *= scale;
672 }
673 }
drheb9a9e82004-02-22 17:49:32 +0000674 if( pzEnd ) *pzEnd = z;
drh93a5c6b2003-12-23 02:17:35 +0000675 return sign<0 ? -v1 : v1;
676}
677
drh202b2df2004-01-06 01:13:46 +0000678/*
drhfec19aa2004-05-19 20:41:03 +0000679** Return TRUE if zNum is a 64-bit signed integer and write
680** the value of the integer into *pNum. If zNum is not an integer
681** or is an integer that is too large to be expressed with 64 bits,
682** then return false. If n>0 and the integer is string is not
683** exactly n bytes long, return false.
684**
685** When this routine was originally written it dealt with only
686** 32-bit numbers. At that time, it was much faster than the
687** atoi() library routine in RedHat 7.2.
688*/
drheb2e1762004-05-27 01:53:56 +0000689int sqlite3atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000690 i64 v = 0;
691 int neg;
692 int i, c;
693 if( *zNum=='-' ){
694 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000695 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000696 }else if( *zNum=='+' ){
697 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000698 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000699 }else{
700 neg = 0;
701 }
drheb2e1762004-05-27 01:53:56 +0000702 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000703 v = v*10 + c - '0';
704 }
705 *pNum = neg ? -v : v;
706 return c==0 && i>0 &&
707 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
708}
709
710/*
drh202b2df2004-01-06 01:13:46 +0000711** The string zNum represents an integer. There might be some other
712** information following the integer too, but that part is ignored.
713** If the integer that the prefix of zNum represents will fit in a
714** 32-bit signed integer, return TRUE. Otherwise return FALSE.
715**
716** This routine returns FALSE for the string -2147483648 even that
717** that number will, in theory fit in a 32-bit integer. But positive
718** 2147483648 will not fit in 32 bits. So it seems safer to return
719** false.
720*/
drhfec19aa2004-05-19 20:41:03 +0000721static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000722 int i, c;
723 if( *zNum=='-' || *zNum=='+' ) zNum++;
724 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
725 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
726}
727
drhfec19aa2004-05-19 20:41:03 +0000728/*
729** If zNum represents an integer that will fit in 32-bits, then set
730** *pValue to that integer and return true. Otherwise return false.
731*/
732int sqlite3GetInt32(const char *zNum, int *pValue){
733 if( sqlite3FitsIn32Bits(zNum) ){
734 *pValue = atoi(zNum);
735 return 1;
736 }
737 return 0;
738}
739
740/*
741** The string zNum represents an integer. There might be some other
742** information following the integer too, but that part is ignored.
743** If the integer that the prefix of zNum represents will fit in a
744** 64-bit signed integer, return TRUE. Otherwise return FALSE.
745**
746** This routine returns FALSE for the string -9223372036854775808 even that
747** that number will, in theory fit in a 64-bit integer. Positive
748** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
749** false.
750*/
751int sqlite3FitsIn64Bits(const char *zNum){
752 int i, c;
753 if( *zNum=='-' || *zNum=='+' ) zNum++;
754 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
755 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
756}
757
758/*
759** If zNum represents an integer that will fit in 64-bits, then set
760** *pValue to that integer and return true. Otherwise return false.
761*/
762int sqlite3GetInt64(const char *zNum, i64 *pValue){
763 if( sqlite3FitsIn64Bits(zNum) ){
drhf4479502004-05-27 03:12:53 +0000764 sqlite3atoi64(zNum, pValue);
drhfec19aa2004-05-19 20:41:03 +0000765 return 1;
766 }
767 return 0;
768}
769
drh75897232000-05-29 14:26:00 +0000770/* This comparison routine is what we use for comparison operations
drha9e99ae2002-08-13 23:02:57 +0000771** between numeric values in an SQL expression. "Numeric" is a little
772** bit misleading here. What we mean is that the strings have a
773** type of "numeric" from the point of view of SQL. The strings
774** do not necessarily contain numbers. They could contain text.
drh7a7c7392001-11-24 00:31:46 +0000775**
drha9e99ae2002-08-13 23:02:57 +0000776** If the input strings both look like actual numbers then they
777** compare in numerical order. Numerical strings are always less
778** than non-numeric strings so if one input string looks like a
779** number and the other does not, then the one that looks like
780** a number is the smaller. Non-numeric strings compare in
781** lexigraphical order (the same order as strcmp()).
drh75897232000-05-29 14:26:00 +0000782*/
danielk19774adee202004-05-08 08:23:19 +0000783int sqlite3Compare(const char *atext, const char *btext){
drh75897232000-05-29 14:26:00 +0000784 int result;
drh0bce8352002-02-28 00:41:10 +0000785 int isNumA, isNumB;
786 if( atext==0 ){
drh8912d102002-05-26 21:34:58 +0000787 return -1;
drh0bce8352002-02-28 00:41:10 +0000788 }else if( btext==0 ){
789 return 1;
790 }
danielk1977dc8453f2004-06-12 00:42:34 +0000791 isNumA = sqlite3IsNumber(atext, 0, SQLITE_UTF8);
792 isNumB = sqlite3IsNumber(btext, 0, SQLITE_UTF8);
drh7a7c7392001-11-24 00:31:46 +0000793 if( isNumA ){
794 if( !isNumB ){
795 result = -1;
796 }else{
797 double rA, rB;
danielk19774adee202004-05-08 08:23:19 +0000798 rA = sqlite3AtoF(atext, 0);
799 rB = sqlite3AtoF(btext, 0);
drh7a7c7392001-11-24 00:31:46 +0000800 if( rA<rB ){
801 result = -1;
802 }else if( rA>rB ){
803 result = +1;
804 }else{
805 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000806 }
807 }
drh7a7c7392001-11-24 00:31:46 +0000808 }else if( isNumB ){
809 result = +1;
810 }else {
811 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000812 }
drh7a7c7392001-11-24 00:31:46 +0000813 return result;
drh75897232000-05-29 14:26:00 +0000814}
drh75897232000-05-29 14:26:00 +0000815
816/*
drh16e59552000-07-31 11:57:37 +0000817** This routine is used for sorting. Each key is a list of one or more
drha9e99ae2002-08-13 23:02:57 +0000818** null-terminated elements. The list is terminated by two nulls in
819** a row. For example, the following text is a key with three elements
drh75897232000-05-29 14:26:00 +0000820**
drha9e99ae2002-08-13 23:02:57 +0000821** Aone\000Dtwo\000Athree\000\000
drh75897232000-05-29 14:26:00 +0000822**
drhda30d362002-08-26 19:55:07 +0000823** All elements begin with one of the characters "+-AD" and end with "\000"
824** with zero or more text elements in between. Except, NULL elements
825** consist of the special two-character sequence "N\000".
826**
drha9e99ae2002-08-13 23:02:57 +0000827** Both arguments will have the same number of elements. This routine
drh75897232000-05-29 14:26:00 +0000828** returns negative, zero, or positive if the first argument is less
829** than, equal to, or greater than the first. (Result is a-b).
830**
drha9e99ae2002-08-13 23:02:57 +0000831** Each element begins with one of the characters "+", "-", "A", "D".
drh38640e12002-07-05 21:42:36 +0000832** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000833**
drh38640e12002-07-05 21:42:36 +0000834** + Sort numerically in ascending order
835** - Sort numerically in descending order
836** A Sort as strings in ascending order
837** D Sort as strings in descending order.
838**
839** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000840** isNum() function above returns TRUE) always compare less than strings
drha9e99ae2002-08-13 23:02:57 +0000841** that are not pure numerics. Non-numeric strings compare in memcmp()
danielk19774adee202004-05-08 08:23:19 +0000842** order. This is the same sort order as the sqlite3Compare() function
drha9e99ae2002-08-13 23:02:57 +0000843** above generates.
drh7a7c7392001-11-24 00:31:46 +0000844**
drha9e99ae2002-08-13 23:02:57 +0000845** The last point is a change from version 2.6.3 to version 2.7.0. In
846** version 2.6.3 and earlier, substrings of digits compare in numerical
847** and case was used only to break a tie.
848**
849** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
850** of whether or not they look like a number.
851**
852** Note that the sort order imposed by the rules above is the same
drh7a7c7392001-11-24 00:31:46 +0000853** from the ordering defined by the "<", "<=", ">", and ">=" operators
drha9e99ae2002-08-13 23:02:57 +0000854** of expressions and for indices. This was not the case for version
855** 2.6.3 and earlier.
drh75897232000-05-29 14:26:00 +0000856*/
danielk19774adee202004-05-08 08:23:19 +0000857int sqlite3SortCompare(const char *a, const char *b){
drh75897232000-05-29 14:26:00 +0000858 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000859 int isNumA, isNumB;
drh294fb922002-09-30 01:31:21 +0000860 int dir = 0;
drh75897232000-05-29 14:26:00 +0000861
862 while( res==0 && *a && *b ){
drhda30d362002-08-26 19:55:07 +0000863 if( a[0]=='N' || b[0]=='N' ){
864 if( a[0]==b[0] ){
865 a += 2;
866 b += 2;
867 continue;
868 }
869 if( a[0]=='N' ){
870 dir = b[0];
871 res = -1;
872 }else{
873 dir = a[0];
874 res = +1;
875 }
drhf570f012002-05-31 15:51:25 +0000876 break;
877 }
drhda30d362002-08-26 19:55:07 +0000878 assert( a[0]==b[0] );
879 if( (dir=a[0])=='A' || a[0]=='D' ){
drh38640e12002-07-05 21:42:36 +0000880 res = strcmp(&a[1],&b[1]);
881 if( res ) break;
882 }else{
danielk1977dc8453f2004-06-12 00:42:34 +0000883 isNumA = sqlite3IsNumber(&a[1], 0, SQLITE_UTF8);
884 isNumB = sqlite3IsNumber(&b[1], 0, SQLITE_UTF8);
drh38640e12002-07-05 21:42:36 +0000885 if( isNumA ){
886 double rA, rB;
887 if( !isNumB ){
888 res = -1;
889 break;
890 }
danielk19774adee202004-05-08 08:23:19 +0000891 rA = sqlite3AtoF(&a[1], 0);
892 rB = sqlite3AtoF(&b[1], 0);
drh38640e12002-07-05 21:42:36 +0000893 if( rA<rB ){
894 res = -1;
895 break;
896 }
897 if( rA>rB ){
898 res = +1;
899 break;
900 }
901 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000902 res = +1;
903 break;
drh38640e12002-07-05 21:42:36 +0000904 }else{
drha9e99ae2002-08-13 23:02:57 +0000905 res = strcmp(&a[1],&b[1]);
906 if( res ) break;
drh7a7c7392001-11-24 00:31:46 +0000907 }
drh75897232000-05-29 14:26:00 +0000908 }
drhcab20052003-04-18 17:45:14 +0000909 a += strlen(&a[1]) + 2;
910 b += strlen(&b[1]) + 2;
drh75897232000-05-29 14:26:00 +0000911 }
drhda30d362002-08-26 19:55:07 +0000912 if( dir=='-' || dir=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000913 return res;
914}
drhdce2cbe2000-05-31 02:27:49 +0000915
drhdf014892004-06-02 00:41:09 +0000916#if 1 /* We are now always UTF-8 */
drhdce2cbe2000-05-31 02:27:49 +0000917/*
drh297ecf12001-04-05 15:57:13 +0000918** X is a pointer to the first byte of a UTF-8 character. Increment
919** X so that it points to the next character. This only works right
920** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000921*/
drh297ecf12001-04-05 15:57:13 +0000922#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
drhdf014892004-06-02 00:41:09 +0000923#define sqliteCharVal(X) sqlite3ReadUtf8(X)
drhe17a7e32001-04-04 21:10:18 +0000924
drh297ecf12001-04-05 15:57:13 +0000925#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000926/*
drh297ecf12001-04-05 15:57:13 +0000927** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000928*/
drh297ecf12001-04-05 15:57:13 +0000929#define sqliteNextChar(X) (++(X));
930#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000931
drh297ecf12001-04-05 15:57:13 +0000932#endif /* defined(SQLITE_UTF8) */
933
934
drhdf014892004-06-02 00:41:09 +0000935#if 1 /* We are now always UTF-8 */
drhe17a7e32001-04-04 21:10:18 +0000936/*
drh297ecf12001-04-05 15:57:13 +0000937** Convert the UTF-8 character to which z points into a 31-bit
938** UCS character. This only works right if z points to a well-formed
939** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000940*/
danielk1977ad7dd422004-06-06 12:41:49 +0000941int sqlite3ReadUtf8(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000942 int c;
drh297ecf12001-04-05 15:57:13 +0000943 static const int initVal[] = {
944 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
945 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
946 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
947 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
948 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
949 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
950 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
951 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
952 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
953 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
954 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
955 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
956 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
957 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
958 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
959 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
960 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
961 255,
962 };
963 c = initVal[*(z++)];
964 while( (0xc0&*z)==0x80 ){
965 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000966 }
967 return c;
968}
drh297ecf12001-04-05 15:57:13 +0000969#endif
drhe17a7e32001-04-04 21:10:18 +0000970
971/*
972** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000973** potentially be a "glob" expression. Return true (1) if they
974** are the same and false (0) if they are different.
975**
976** Globbing rules:
977**
978** '*' Matches any sequence of zero or more characters.
979**
980** '?' Matches exactly one character.
981**
982** [...] Matches one character from the enclosed list of
983** characters.
984**
985** [^...] Matches one character not in the enclosed list.
986**
987** With the [...] and [^...] matching, a ']' character can be included
988** in the list by making it the first character after '[' or '^'. A
989** range of characters can be specified using '-'. Example:
990** "[a-z]" matches any single lower-case letter. To match a '-', make
991** it the last character in the list.
992**
993** This routine is usually quick, but can be N**2 in the worst case.
994**
995** Hints: to match '*' or '?', put them in "[]". Like this:
996**
997** abc[*]xyz Matches "abc*xyz" only
998*/
drhe17a7e32001-04-04 21:10:18 +0000999int
danielk19774adee202004-05-08 08:23:19 +00001000sqlite3GlobCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001001 register int c;
drhdce2cbe2000-05-31 02:27:49 +00001002 int invert;
1003 int seen;
drhe17a7e32001-04-04 21:10:18 +00001004 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001005
1006 while( (c = *zPattern)!=0 ){
1007 switch( c ){
1008 case '*':
drhe17a7e32001-04-04 21:10:18 +00001009 while( (c=zPattern[1]) == '*' || c == '?' ){
1010 if( c=='?' ){
1011 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001012 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001013 }
1014 zPattern++;
1015 }
1016 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +00001017 if( c=='[' ){
danielk19774adee202004-05-08 08:23:19 +00001018 while( *zString && sqlite3GlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +00001019 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001020 }
1021 return *zString!=0;
1022 }else{
1023 while( (c2 = *zString)!=0 ){
1024 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +00001025 if( c2==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001026 if( sqlite3GlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001027 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001028 }
1029 return 0;
1030 }
drhe17a7e32001-04-04 21:10:18 +00001031 case '?': {
drhdce2cbe2000-05-31 02:27:49 +00001032 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001033 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001034 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001035 break;
drhe17a7e32001-04-04 21:10:18 +00001036 }
1037 case '[': {
1038 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001039 seen = 0;
1040 invert = 0;
drh297ecf12001-04-05 15:57:13 +00001041 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +00001042 if( c==0 ) return 0;
1043 c2 = *++zPattern;
1044 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1045 if( c2==']' ){
1046 if( c==']' ) seen = 1;
1047 c2 = *++zPattern;
1048 }
drh297ecf12001-04-05 15:57:13 +00001049 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +00001050 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1051 zPattern++;
drh297ecf12001-04-05 15:57:13 +00001052 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +00001053 if( c>=prior_c && c<=c2 ) seen = 1;
1054 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001055 }else if( c==c2 ){
1056 seen = 1;
drhe17a7e32001-04-04 21:10:18 +00001057 prior_c = c2;
1058 }else{
1059 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001060 }
drh297ecf12001-04-05 15:57:13 +00001061 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001062 }
1063 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001064 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001065 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001066 break;
drhe17a7e32001-04-04 21:10:18 +00001067 }
1068 default: {
drhdce2cbe2000-05-31 02:27:49 +00001069 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001070 zPattern++;
1071 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001072 break;
drhe17a7e32001-04-04 21:10:18 +00001073 }
drhdce2cbe2000-05-31 02:27:49 +00001074 }
drhdce2cbe2000-05-31 02:27:49 +00001075 }
1076 return *zString==0;
1077}
1078
1079/*
drhc22bd472002-05-10 13:14:07 +00001080** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1081** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1082** when this routine is called.
1083**
1084** This routine is a attempt to detect if two threads use the
1085** same sqlite* pointer at the same time. There is a race
1086** condition so it is possible that the error is not detected.
1087** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001088** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001089** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +00001090**
1091** Ticket #202: If db->magic is not a valid open value, take care not
1092** to modify the db structure at all. It could be that db is a stale
1093** pointer. In other words, it could be that there has been a prior
danielk19776f8a5032004-05-10 10:34:51 +00001094** call to sqlite3_close(db) and db has been deallocated. And we do
drhe7e8bc72002-12-17 13:05:25 +00001095** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +00001096*/
danielk19774adee202004-05-08 08:23:19 +00001097int sqlite3SafetyOn(sqlite *db){
drhc22bd472002-05-10 13:14:07 +00001098 if( db->magic==SQLITE_MAGIC_OPEN ){
1099 db->magic = SQLITE_MAGIC_BUSY;
1100 return 0;
drh94e92032003-02-16 22:21:32 +00001101 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1102 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001103 db->magic = SQLITE_MAGIC_ERROR;
1104 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +00001105 }
drhe7e8bc72002-12-17 13:05:25 +00001106 return 1;
drhc22bd472002-05-10 13:14:07 +00001107}
1108
1109/*
1110** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1111** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1112** when this routine is called.
1113*/
danielk19774adee202004-05-08 08:23:19 +00001114int sqlite3SafetyOff(sqlite *db){
drhc22bd472002-05-10 13:14:07 +00001115 if( db->magic==SQLITE_MAGIC_BUSY ){
1116 db->magic = SQLITE_MAGIC_OPEN;
1117 return 0;
drh94e92032003-02-16 22:21:32 +00001118 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1119 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001120 db->magic = SQLITE_MAGIC_ERROR;
1121 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +00001122 }
drhe7e8bc72002-12-17 13:05:25 +00001123 return 1;
drhc22bd472002-05-10 13:14:07 +00001124}
1125
1126/*
danielk19776f8a5032004-05-10 10:34:51 +00001127** Check to make sure we are not currently executing an sqlite3_exec().
1128** If we are currently in an sqlite3_exec(), return true and set
drhc22bd472002-05-10 13:14:07 +00001129** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1130** shutdown of the database.
1131**
1132** This routine is used to try to detect when API routines are called
1133** at the wrong time or in the wrong sequence.
1134*/
danielk19774adee202004-05-08 08:23:19 +00001135int sqlite3SafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +00001136 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +00001137 db->magic = SQLITE_MAGIC_ERROR;
1138 return 1;
1139 }
1140 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001141}
danielk19774adee202004-05-08 08:23:19 +00001142
drh6d2fb152004-05-14 16:50:06 +00001143/*
drhd8820e82004-05-18 15:57:42 +00001144** The variable-length integer encoding is as follows:
1145**
1146** KEY:
1147** A = 0xxxxxxx 7 bits of data and one flag bit
1148** B = 1xxxxxxx 7 bits of data and one flag bit
1149** C = xxxxxxxx 8 bits of data
1150**
1151** 7 bits - A
1152** 14 bits - BA
1153** 21 bits - BBA
1154** 28 bits - BBBA
1155** 35 bits - BBBBA
1156** 42 bits - BBBBBA
1157** 49 bits - BBBBBBA
1158** 56 bits - BBBBBBBA
1159** 64 bits - BBBBBBBBC
1160*/
1161
1162/*
drh6d2fb152004-05-14 16:50:06 +00001163** Write a 64-bit variable-length integer to memory starting at p[0].
1164** The length of data write will be between 1 and 9 bytes. The number
1165** of bytes written is returned.
1166**
1167** A variable-length integer consists of the lower 7 bits of each byte
1168** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +00001169** bit clear. Except, if we get to the 9th byte, it stores the full
1170** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +00001171*/
danielk1977192ac1d2004-05-10 07:17:30 +00001172int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +00001173 int i, j, n;
1174 u8 buf[10];
drhd8820e82004-05-18 15:57:42 +00001175 if( v & 0xff00000000000000 ){
1176 p[8] = v;
1177 v >>= 8;
1178 for(i=7; i>=0; i--){
1179 p[i] = (v & 0x7f) | 0x80;
1180 v >>= 7;
1181 }
1182 return 9;
1183 }
drh6d2fb152004-05-14 16:50:06 +00001184 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001185 do{
drh6d2fb152004-05-14 16:50:06 +00001186 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +00001187 v >>= 7;
1188 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +00001189 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +00001190 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +00001191 for(i=0, j=n-1; j>=0; j--, i++){
1192 p[i] = buf[j];
1193 }
1194 return n;
danielk1977192ac1d2004-05-10 07:17:30 +00001195}
danielk19774adee202004-05-08 08:23:19 +00001196
drh6d2fb152004-05-14 16:50:06 +00001197/*
1198** Read a 64-bit variable-length integer from memory starting at p[0].
1199** Return the number of bytes read. The value is stored in *v.
1200*/
danielk197790e4d952004-05-10 10:05:53 +00001201int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +00001202 u32 x;
1203 u64 x64;
1204 int n;
1205 unsigned char c;
drhe51c44f2004-05-30 20:46:09 +00001206 if( ((c = p[0]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001207 *v = c;
1208 return 1;
1209 }
1210 x = c & 0x7f;
drhe51c44f2004-05-30 20:46:09 +00001211 if( ((c = p[1]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001212 *v = (x<<7) | c;
1213 return 2;
1214 }
1215 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +00001216 if( ((c = p[2]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001217 *v = (x<<7) | c;
1218 return 3;
1219 }
1220 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +00001221 if( ((c = p[3]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +00001222 *v = (x<<7) | c;
1223 return 4;
1224 }
1225 x64 = (x<<7) | (c&0x7f);
1226 n = 4;
1227 do{
1228 c = p[n++];
drhd8820e82004-05-18 15:57:42 +00001229 if( n==9 ){
1230 x64 = (x64<<8) | c;
1231 break;
1232 }
drh6d2fb152004-05-14 16:50:06 +00001233 x64 = (x64<<7) | (c&0x7f);
1234 }while( (c & 0x80)!=0 );
1235 *v = x64;
1236 return n;
1237}
1238
1239/*
1240** Read a 32-bit variable-length integer from memory starting at p[0].
1241** Return the number of bytes read. The value is stored in *v.
1242*/
1243int sqlite3GetVarint32(const unsigned char *p, u32 *v){
drhe51c44f2004-05-30 20:46:09 +00001244 u32 x;
1245 int n;
1246 unsigned char c;
1247 if( ((c = p[0]) & 0x80)==0 ){
1248 *v = c;
1249 return 1;
danielk1977192ac1d2004-05-10 07:17:30 +00001250 }
drhe51c44f2004-05-30 20:46:09 +00001251 x = c & 0x7f;
1252 if( ((c = p[1]) & 0x80)==0 ){
1253 *v = (x<<7) | c;
1254 return 2;
1255 }
1256 x = (x<<7) | (c&0x7f);
1257 if( ((c = p[2]) & 0x80)==0 ){
1258 *v = (x<<7) | c;
1259 return 3;
1260 }
1261 x = (x<<7) | (c&0x7f);
1262 if( ((c = p[3]) & 0x80)==0 ){
1263 *v = (x<<7) | c;
1264 return 4;
1265 }
1266 n = 4;
1267 do{
1268 x = (x<<7) | ((c = p[n++])&0x7f);
1269 }while( (c & 0x80)!=0 && n<9 );
danielk1977192ac1d2004-05-10 07:17:30 +00001270 *v = x;
1271 return n;
1272}
1273
drh6d2fb152004-05-14 16:50:06 +00001274/*
1275** Return the number of bytes that will be needed to store the given
1276** 64-bit integer.
1277*/
danielk1977192ac1d2004-05-10 07:17:30 +00001278int sqlite3VarintLen(u64 v){
1279 int i = 0;
1280 do{
1281 i++;
1282 v >>= 7;
drhd8820e82004-05-18 15:57:42 +00001283 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +00001284 return i;
1285}
danielk1977c572ef72004-05-27 09:28:41 +00001286
danielk1977cbb18d22004-05-28 11:37:27 +00001287void *sqlite3HexToBlob(const char *z){
danielk1977c572ef72004-05-27 09:28:41 +00001288 char *zBlob;
1289 int i;
1290 int n = strlen(z);
1291 if( n%2 ) return 0;
1292
1293 zBlob = (char *)sqliteMalloc(n/2);
1294
danielk19773fd0a732004-05-27 13:35:19 +00001295 for(i=0; i<n; i++){
danielk1977c572ef72004-05-27 09:28:41 +00001296 u8 c;
1297
1298 if ( z[i]>47 && z[i]<58 ) c = (z[i]-48)<<4;
1299 else if( z[i]>64 && z[i]<71 ) c = (z[i]-55)<<4;
1300 else if( z[i]>96 && z[i]<103 ) c = (z[i]-87)<<4;
1301 else {
1302 sqliteFree(zBlob);
1303 return 0;
1304 }
danielk19773fd0a732004-05-27 13:35:19 +00001305 i++;
danielk1977c572ef72004-05-27 09:28:41 +00001306 if ( z[i]>47 && z[i]<58 ) c += (z[i]-48);
1307 else if( z[i]>64 && z[i]<71 ) c += (z[i]-55);
1308 else if( z[i]>96 && z[i]<103 ) c += (z[i]-87);
1309 else {
1310 sqliteFree(zBlob);
1311 return 0;
1312 }
1313
1314 zBlob[i/2] = c;
1315 }
danielk19773fd0a732004-05-27 13:35:19 +00001316 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +00001317}