1*c5c4113dSnw141292
2*c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292 /*
5*c5c4113dSnw141292 ** 2001 September 15
6*c5c4113dSnw141292 **
7*c5c4113dSnw141292 ** The author disclaims copyright to this source code. In place of
8*c5c4113dSnw141292 ** a legal notice, here is a blessing:
9*c5c4113dSnw141292 **
10*c5c4113dSnw141292 ** May you do good and not evil.
11*c5c4113dSnw141292 ** May you find forgiveness for yourself and forgive others.
12*c5c4113dSnw141292 ** May you share freely, never taking more than you give.
13*c5c4113dSnw141292 **
14*c5c4113dSnw141292 *************************************************************************
15*c5c4113dSnw141292 ** Utility functions used throughout sqlite.
16*c5c4113dSnw141292 **
17*c5c4113dSnw141292 ** This file contains functions for allocating memory, comparing
18*c5c4113dSnw141292 ** strings, and stuff like that.
19*c5c4113dSnw141292 **
20*c5c4113dSnw141292 ** $Id: util.c,v 1.74.2.1 2004/07/15 13:08:41 drh Exp $
21*c5c4113dSnw141292 */
22*c5c4113dSnw141292 #include "sqliteInt.h"
23*c5c4113dSnw141292 #include <stdarg.h>
24*c5c4113dSnw141292 #include <ctype.h>
25*c5c4113dSnw141292
26*c5c4113dSnw141292 /*
27*c5c4113dSnw141292 ** If malloc() ever fails, this global variable gets set to 1.
28*c5c4113dSnw141292 ** This causes the library to abort and never again function.
29*c5c4113dSnw141292 */
30*c5c4113dSnw141292 int sqlite_malloc_failed = 0;
31*c5c4113dSnw141292
32*c5c4113dSnw141292 /*
33*c5c4113dSnw141292 ** If MEMORY_DEBUG is defined, then use versions of malloc() and
34*c5c4113dSnw141292 ** free() that track memory usage and check for buffer overruns.
35*c5c4113dSnw141292 */
36*c5c4113dSnw141292 #ifdef MEMORY_DEBUG
37*c5c4113dSnw141292
38*c5c4113dSnw141292 /*
39*c5c4113dSnw141292 ** For keeping track of the number of mallocs and frees. This
40*c5c4113dSnw141292 ** is used to check for memory leaks.
41*c5c4113dSnw141292 */
42*c5c4113dSnw141292 int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
43*c5c4113dSnw141292 int sqlite_nFree; /* Number of sqliteFree() calls */
44*c5c4113dSnw141292 int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
45*c5c4113dSnw141292 #if MEMORY_DEBUG>1
46*c5c4113dSnw141292 static int memcnt = 0;
47*c5c4113dSnw141292 #endif
48*c5c4113dSnw141292
49*c5c4113dSnw141292 /*
50*c5c4113dSnw141292 ** Number of 32-bit guard words
51*c5c4113dSnw141292 */
52*c5c4113dSnw141292 #define N_GUARD 1
53*c5c4113dSnw141292
54*c5c4113dSnw141292 /*
55*c5c4113dSnw141292 ** Allocate new memory and set it to zero. Return NULL if
56*c5c4113dSnw141292 ** no memory is available.
57*c5c4113dSnw141292 */
sqliteMalloc_(int n,int bZero,char * zFile,int line)58*c5c4113dSnw141292 void *sqliteMalloc_(int n, int bZero, char *zFile, int line){
59*c5c4113dSnw141292 void *p;
60*c5c4113dSnw141292 int *pi;
61*c5c4113dSnw141292 int i, k;
62*c5c4113dSnw141292 if( sqlite_iMallocFail>=0 ){
63*c5c4113dSnw141292 sqlite_iMallocFail--;
64*c5c4113dSnw141292 if( sqlite_iMallocFail==0 ){
65*c5c4113dSnw141292 sqlite_malloc_failed++;
66*c5c4113dSnw141292 #if MEMORY_DEBUG>1
67*c5c4113dSnw141292 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
68*c5c4113dSnw141292 n, zFile,line);
69*c5c4113dSnw141292 #endif
70*c5c4113dSnw141292 sqlite_iMallocFail--;
71*c5c4113dSnw141292 return 0;
72*c5c4113dSnw141292 }
73*c5c4113dSnw141292 }
74*c5c4113dSnw141292 if( n==0 ) return 0;
75*c5c4113dSnw141292 k = (n+sizeof(int)-1)/sizeof(int);
76*c5c4113dSnw141292 pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
77*c5c4113dSnw141292 if( pi==0 ){
78*c5c4113dSnw141292 sqlite_malloc_failed++;
79*c5c4113dSnw141292 return 0;
80*c5c4113dSnw141292 }
81*c5c4113dSnw141292 sqlite_nMalloc++;
82*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
83*c5c4113dSnw141292 pi[N_GUARD] = n;
84*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
85*c5c4113dSnw141292 p = &pi[N_GUARD+1];
86*c5c4113dSnw141292 memset(p, bZero==0, n);
87*c5c4113dSnw141292 #if MEMORY_DEBUG>1
88*c5c4113dSnw141292 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
89*c5c4113dSnw141292 ++memcnt, n, (int)p, zFile,line);
90*c5c4113dSnw141292 #endif
91*c5c4113dSnw141292 return p;
92*c5c4113dSnw141292 }
93*c5c4113dSnw141292
94*c5c4113dSnw141292 /*
95*c5c4113dSnw141292 ** Check to see if the given pointer was obtained from sqliteMalloc()
96*c5c4113dSnw141292 ** and is able to hold at least N bytes. Raise an exception if this
97*c5c4113dSnw141292 ** is not the case.
98*c5c4113dSnw141292 **
99*c5c4113dSnw141292 ** This routine is used for testing purposes only.
100*c5c4113dSnw141292 */
sqliteCheckMemory(void * p,int N)101*c5c4113dSnw141292 void sqliteCheckMemory(void *p, int N){
102*c5c4113dSnw141292 int *pi = p;
103*c5c4113dSnw141292 int n, i, k;
104*c5c4113dSnw141292 pi -= N_GUARD+1;
105*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++){
106*c5c4113dSnw141292 assert( pi[i]==0xdead1122 );
107*c5c4113dSnw141292 }
108*c5c4113dSnw141292 n = pi[N_GUARD];
109*c5c4113dSnw141292 assert( N>=0 && N<n );
110*c5c4113dSnw141292 k = (n+sizeof(int)-1)/sizeof(int);
111*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++){
112*c5c4113dSnw141292 assert( pi[k+N_GUARD+1+i]==0xdead3344 );
113*c5c4113dSnw141292 }
114*c5c4113dSnw141292 }
115*c5c4113dSnw141292
116*c5c4113dSnw141292 /*
117*c5c4113dSnw141292 ** Free memory previously obtained from sqliteMalloc()
118*c5c4113dSnw141292 */
sqliteFree_(void * p,char * zFile,int line)119*c5c4113dSnw141292 void sqliteFree_(void *p, char *zFile, int line){
120*c5c4113dSnw141292 if( p ){
121*c5c4113dSnw141292 int *pi, i, k, n;
122*c5c4113dSnw141292 pi = p;
123*c5c4113dSnw141292 pi -= N_GUARD+1;
124*c5c4113dSnw141292 sqlite_nFree++;
125*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++){
126*c5c4113dSnw141292 if( pi[i]!=0xdead1122 ){
127*c5c4113dSnw141292 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
128*c5c4113dSnw141292 return;
129*c5c4113dSnw141292 }
130*c5c4113dSnw141292 }
131*c5c4113dSnw141292 n = pi[N_GUARD];
132*c5c4113dSnw141292 k = (n+sizeof(int)-1)/sizeof(int);
133*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++){
134*c5c4113dSnw141292 if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
135*c5c4113dSnw141292 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
136*c5c4113dSnw141292 return;
137*c5c4113dSnw141292 }
138*c5c4113dSnw141292 }
139*c5c4113dSnw141292 memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
140*c5c4113dSnw141292 #if MEMORY_DEBUG>1
141*c5c4113dSnw141292 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
142*c5c4113dSnw141292 ++memcnt, n, (int)p, zFile,line);
143*c5c4113dSnw141292 #endif
144*c5c4113dSnw141292 free(pi);
145*c5c4113dSnw141292 }
146*c5c4113dSnw141292 }
147*c5c4113dSnw141292
148*c5c4113dSnw141292 /*
149*c5c4113dSnw141292 ** Resize a prior allocation. If p==0, then this routine
150*c5c4113dSnw141292 ** works just like sqliteMalloc(). If n==0, then this routine
151*c5c4113dSnw141292 ** works just like sqliteFree().
152*c5c4113dSnw141292 */
sqliteRealloc_(void * oldP,int n,char * zFile,int line)153*c5c4113dSnw141292 void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
154*c5c4113dSnw141292 int *oldPi, *pi, i, k, oldN, oldK;
155*c5c4113dSnw141292 void *p;
156*c5c4113dSnw141292 if( oldP==0 ){
157*c5c4113dSnw141292 return sqliteMalloc_(n,1,zFile,line);
158*c5c4113dSnw141292 }
159*c5c4113dSnw141292 if( n==0 ){
160*c5c4113dSnw141292 sqliteFree_(oldP,zFile,line);
161*c5c4113dSnw141292 return 0;
162*c5c4113dSnw141292 }
163*c5c4113dSnw141292 oldPi = oldP;
164*c5c4113dSnw141292 oldPi -= N_GUARD+1;
165*c5c4113dSnw141292 if( oldPi[0]!=0xdead1122 ){
166*c5c4113dSnw141292 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
167*c5c4113dSnw141292 return 0;
168*c5c4113dSnw141292 }
169*c5c4113dSnw141292 oldN = oldPi[N_GUARD];
170*c5c4113dSnw141292 oldK = (oldN+sizeof(int)-1)/sizeof(int);
171*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++){
172*c5c4113dSnw141292 if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
173*c5c4113dSnw141292 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
174*c5c4113dSnw141292 (int)oldP);
175*c5c4113dSnw141292 return 0;
176*c5c4113dSnw141292 }
177*c5c4113dSnw141292 }
178*c5c4113dSnw141292 k = (n + sizeof(int) - 1)/sizeof(int);
179*c5c4113dSnw141292 pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
180*c5c4113dSnw141292 if( pi==0 ){
181*c5c4113dSnw141292 sqlite_malloc_failed++;
182*c5c4113dSnw141292 return 0;
183*c5c4113dSnw141292 }
184*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
185*c5c4113dSnw141292 pi[N_GUARD] = n;
186*c5c4113dSnw141292 for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
187*c5c4113dSnw141292 p = &pi[N_GUARD+1];
188*c5c4113dSnw141292 memcpy(p, oldP, n>oldN ? oldN : n);
189*c5c4113dSnw141292 if( n>oldN ){
190*c5c4113dSnw141292 memset(&((char*)p)[oldN], 0, n-oldN);
191*c5c4113dSnw141292 }
192*c5c4113dSnw141292 memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
193*c5c4113dSnw141292 free(oldPi);
194*c5c4113dSnw141292 #if MEMORY_DEBUG>1
195*c5c4113dSnw141292 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
196*c5c4113dSnw141292 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
197*c5c4113dSnw141292 #endif
198*c5c4113dSnw141292 return p;
199*c5c4113dSnw141292 }
200*c5c4113dSnw141292
201*c5c4113dSnw141292 /*
202*c5c4113dSnw141292 ** Make a duplicate of a string into memory obtained from malloc()
203*c5c4113dSnw141292 ** Free the original string using sqliteFree().
204*c5c4113dSnw141292 **
205*c5c4113dSnw141292 ** This routine is called on all strings that are passed outside of
206*c5c4113dSnw141292 ** the SQLite library. That way clients can free the string using free()
207*c5c4113dSnw141292 ** rather than having to call sqliteFree().
208*c5c4113dSnw141292 */
sqliteStrRealloc(char ** pz)209*c5c4113dSnw141292 void sqliteStrRealloc(char **pz){
210*c5c4113dSnw141292 char *zNew;
211*c5c4113dSnw141292 if( pz==0 || *pz==0 ) return;
212*c5c4113dSnw141292 zNew = malloc( strlen(*pz) + 1 );
213*c5c4113dSnw141292 if( zNew==0 ){
214*c5c4113dSnw141292 sqlite_malloc_failed++;
215*c5c4113dSnw141292 sqliteFree(*pz);
216*c5c4113dSnw141292 *pz = 0;
217*c5c4113dSnw141292 }
218*c5c4113dSnw141292 strcpy(zNew, *pz);
219*c5c4113dSnw141292 sqliteFree(*pz);
220*c5c4113dSnw141292 *pz = zNew;
221*c5c4113dSnw141292 }
222*c5c4113dSnw141292
223*c5c4113dSnw141292 /*
224*c5c4113dSnw141292 ** Make a copy of a string in memory obtained from sqliteMalloc()
225*c5c4113dSnw141292 */
sqliteStrDup_(const char * z,char * zFile,int line)226*c5c4113dSnw141292 char *sqliteStrDup_(const char *z, char *zFile, int line){
227*c5c4113dSnw141292 char *zNew;
228*c5c4113dSnw141292 if( z==0 ) return 0;
229*c5c4113dSnw141292 zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line);
230*c5c4113dSnw141292 if( zNew ) strcpy(zNew, z);
231*c5c4113dSnw141292 return zNew;
232*c5c4113dSnw141292 }
sqliteStrNDup_(const char * z,int n,char * zFile,int line)233*c5c4113dSnw141292 char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
234*c5c4113dSnw141292 char *zNew;
235*c5c4113dSnw141292 if( z==0 ) return 0;
236*c5c4113dSnw141292 zNew = sqliteMalloc_(n+1, 0, zFile, line);
237*c5c4113dSnw141292 if( zNew ){
238*c5c4113dSnw141292 memcpy(zNew, z, n);
239*c5c4113dSnw141292 zNew[n] = 0;
240*c5c4113dSnw141292 }
241*c5c4113dSnw141292 return zNew;
242*c5c4113dSnw141292 }
243*c5c4113dSnw141292 #endif /* MEMORY_DEBUG */
244*c5c4113dSnw141292
245*c5c4113dSnw141292 /*
246*c5c4113dSnw141292 ** The following versions of malloc() and free() are for use in a
247*c5c4113dSnw141292 ** normal build.
248*c5c4113dSnw141292 */
249*c5c4113dSnw141292 #if !defined(MEMORY_DEBUG)
250*c5c4113dSnw141292
251*c5c4113dSnw141292 /*
252*c5c4113dSnw141292 ** Allocate new memory and set it to zero. Return NULL if
253*c5c4113dSnw141292 ** no memory is available. See also sqliteMallocRaw().
254*c5c4113dSnw141292 */
sqliteMalloc(int n)255*c5c4113dSnw141292 void *sqliteMalloc(int n){
256*c5c4113dSnw141292 void *p;
257*c5c4113dSnw141292 if( (p = malloc(n))==0 ){
258*c5c4113dSnw141292 if( n>0 ) sqlite_malloc_failed++;
259*c5c4113dSnw141292 }else{
260*c5c4113dSnw141292 memset(p, 0, n);
261*c5c4113dSnw141292 }
262*c5c4113dSnw141292 return p;
263*c5c4113dSnw141292 }
264*c5c4113dSnw141292
265*c5c4113dSnw141292 /*
266*c5c4113dSnw141292 ** Allocate new memory but do not set it to zero. Return NULL if
267*c5c4113dSnw141292 ** no memory is available. See also sqliteMalloc().
268*c5c4113dSnw141292 */
sqliteMallocRaw(int n)269*c5c4113dSnw141292 void *sqliteMallocRaw(int n){
270*c5c4113dSnw141292 void *p;
271*c5c4113dSnw141292 if( (p = malloc(n))==0 ){
272*c5c4113dSnw141292 if( n>0 ) sqlite_malloc_failed++;
273*c5c4113dSnw141292 }
274*c5c4113dSnw141292 return p;
275*c5c4113dSnw141292 }
276*c5c4113dSnw141292
277*c5c4113dSnw141292 /*
278*c5c4113dSnw141292 ** Free memory previously obtained from sqliteMalloc()
279*c5c4113dSnw141292 */
sqliteFree(void * p)280*c5c4113dSnw141292 void sqliteFree(void *p){
281*c5c4113dSnw141292 if( p ){
282*c5c4113dSnw141292 free(p);
283*c5c4113dSnw141292 }
284*c5c4113dSnw141292 }
285*c5c4113dSnw141292
286*c5c4113dSnw141292 /*
287*c5c4113dSnw141292 ** Resize a prior allocation. If p==0, then this routine
288*c5c4113dSnw141292 ** works just like sqliteMalloc(). If n==0, then this routine
289*c5c4113dSnw141292 ** works just like sqliteFree().
290*c5c4113dSnw141292 */
sqliteRealloc(void * p,int n)291*c5c4113dSnw141292 void *sqliteRealloc(void *p, int n){
292*c5c4113dSnw141292 void *p2;
293*c5c4113dSnw141292 if( p==0 ){
294*c5c4113dSnw141292 return sqliteMalloc(n);
295*c5c4113dSnw141292 }
296*c5c4113dSnw141292 if( n==0 ){
297*c5c4113dSnw141292 sqliteFree(p);
298*c5c4113dSnw141292 return 0;
299*c5c4113dSnw141292 }
300*c5c4113dSnw141292 p2 = realloc(p, n);
301*c5c4113dSnw141292 if( p2==0 ){
302*c5c4113dSnw141292 sqlite_malloc_failed++;
303*c5c4113dSnw141292 }
304*c5c4113dSnw141292 return p2;
305*c5c4113dSnw141292 }
306*c5c4113dSnw141292
307*c5c4113dSnw141292 /*
308*c5c4113dSnw141292 ** Make a copy of a string in memory obtained from sqliteMalloc()
309*c5c4113dSnw141292 */
sqliteStrDup(const char * z)310*c5c4113dSnw141292 char *sqliteStrDup(const char *z){
311*c5c4113dSnw141292 char *zNew;
312*c5c4113dSnw141292 if( z==0 ) return 0;
313*c5c4113dSnw141292 zNew = sqliteMallocRaw(strlen(z)+1);
314*c5c4113dSnw141292 if( zNew ) strcpy(zNew, z);
315*c5c4113dSnw141292 return zNew;
316*c5c4113dSnw141292 }
sqliteStrNDup(const char * z,int n)317*c5c4113dSnw141292 char *sqliteStrNDup(const char *z, int n){
318*c5c4113dSnw141292 char *zNew;
319*c5c4113dSnw141292 if( z==0 ) return 0;
320*c5c4113dSnw141292 zNew = sqliteMallocRaw(n+1);
321*c5c4113dSnw141292 if( zNew ){
322*c5c4113dSnw141292 memcpy(zNew, z, n);
323*c5c4113dSnw141292 zNew[n] = 0;
324*c5c4113dSnw141292 }
325*c5c4113dSnw141292 return zNew;
326*c5c4113dSnw141292 }
327*c5c4113dSnw141292 #endif /* !defined(MEMORY_DEBUG) */
328*c5c4113dSnw141292
329*c5c4113dSnw141292 /*
330*c5c4113dSnw141292 ** Create a string from the 2nd and subsequent arguments (up to the
331*c5c4113dSnw141292 ** first NULL argument), store the string in memory obtained from
332*c5c4113dSnw141292 ** sqliteMalloc() and make the pointer indicated by the 1st argument
333*c5c4113dSnw141292 ** point to that string. The 1st argument must either be NULL or
334*c5c4113dSnw141292 ** point to memory obtained from sqliteMalloc().
335*c5c4113dSnw141292 */
sqliteSetString(char ** pz,const char * zFirst,...)336*c5c4113dSnw141292 void sqliteSetString(char **pz, const char *zFirst, ...){
337*c5c4113dSnw141292 va_list ap;
338*c5c4113dSnw141292 int nByte;
339*c5c4113dSnw141292 const char *z;
340*c5c4113dSnw141292 char *zResult;
341*c5c4113dSnw141292
342*c5c4113dSnw141292 if( pz==0 ) return;
343*c5c4113dSnw141292 nByte = strlen(zFirst) + 1;
344*c5c4113dSnw141292 va_start(ap, zFirst);
345*c5c4113dSnw141292 while( (z = va_arg(ap, const char*))!=0 ){
346*c5c4113dSnw141292 nByte += strlen(z);
347*c5c4113dSnw141292 }
348*c5c4113dSnw141292 va_end(ap);
349*c5c4113dSnw141292 sqliteFree(*pz);
350*c5c4113dSnw141292 *pz = zResult = sqliteMallocRaw( nByte );
351*c5c4113dSnw141292 if( zResult==0 ){
352*c5c4113dSnw141292 return;
353*c5c4113dSnw141292 }
354*c5c4113dSnw141292 strcpy(zResult, zFirst);
355*c5c4113dSnw141292 zResult += strlen(zResult);
356*c5c4113dSnw141292 va_start(ap, zFirst);
357*c5c4113dSnw141292 while( (z = va_arg(ap, const char*))!=0 ){
358*c5c4113dSnw141292 strcpy(zResult, z);
359*c5c4113dSnw141292 zResult += strlen(zResult);
360*c5c4113dSnw141292 }
361*c5c4113dSnw141292 va_end(ap);
362*c5c4113dSnw141292 #ifdef MEMORY_DEBUG
363*c5c4113dSnw141292 #if MEMORY_DEBUG>1
364*c5c4113dSnw141292 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
365*c5c4113dSnw141292 #endif
366*c5c4113dSnw141292 #endif
367*c5c4113dSnw141292 }
368*c5c4113dSnw141292
369*c5c4113dSnw141292 /*
370*c5c4113dSnw141292 ** Works like sqliteSetString, but each string is now followed by
371*c5c4113dSnw141292 ** a length integer which specifies how much of the source string
372*c5c4113dSnw141292 ** to copy (in bytes). -1 means use the whole string. The 1st
373*c5c4113dSnw141292 ** argument must either be NULL or point to memory obtained from
374*c5c4113dSnw141292 ** sqliteMalloc().
375*c5c4113dSnw141292 */
sqliteSetNString(char ** pz,...)376*c5c4113dSnw141292 void sqliteSetNString(char **pz, ...){
377*c5c4113dSnw141292 va_list ap;
378*c5c4113dSnw141292 int nByte;
379*c5c4113dSnw141292 const char *z;
380*c5c4113dSnw141292 char *zResult;
381*c5c4113dSnw141292 int n;
382*c5c4113dSnw141292
383*c5c4113dSnw141292 if( pz==0 ) return;
384*c5c4113dSnw141292 nByte = 0;
385*c5c4113dSnw141292 va_start(ap, pz);
386*c5c4113dSnw141292 while( (z = va_arg(ap, const char*))!=0 ){
387*c5c4113dSnw141292 n = va_arg(ap, int);
388*c5c4113dSnw141292 if( n<=0 ) n = strlen(z);
389*c5c4113dSnw141292 nByte += n;
390*c5c4113dSnw141292 }
391*c5c4113dSnw141292 va_end(ap);
392*c5c4113dSnw141292 sqliteFree(*pz);
393*c5c4113dSnw141292 *pz = zResult = sqliteMallocRaw( nByte + 1 );
394*c5c4113dSnw141292 if( zResult==0 ) return;
395*c5c4113dSnw141292 va_start(ap, pz);
396*c5c4113dSnw141292 while( (z = va_arg(ap, const char*))!=0 ){
397*c5c4113dSnw141292 n = va_arg(ap, int);
398*c5c4113dSnw141292 if( n<=0 ) n = strlen(z);
399*c5c4113dSnw141292 strncpy(zResult, z, n);
400*c5c4113dSnw141292 zResult += n;
401*c5c4113dSnw141292 }
402*c5c4113dSnw141292 *zResult = 0;
403*c5c4113dSnw141292 #ifdef MEMORY_DEBUG
404*c5c4113dSnw141292 #if MEMORY_DEBUG>1
405*c5c4113dSnw141292 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
406*c5c4113dSnw141292 #endif
407*c5c4113dSnw141292 #endif
408*c5c4113dSnw141292 va_end(ap);
409*c5c4113dSnw141292 }
410*c5c4113dSnw141292
411*c5c4113dSnw141292 /*
412*c5c4113dSnw141292 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
413*c5c4113dSnw141292 ** The following formatting characters are allowed:
414*c5c4113dSnw141292 **
415*c5c4113dSnw141292 ** %s Insert a string
416*c5c4113dSnw141292 ** %z A string that should be freed after use
417*c5c4113dSnw141292 ** %d Insert an integer
418*c5c4113dSnw141292 ** %T Insert a token
419*c5c4113dSnw141292 ** %S Insert the first element of a SrcList
420*c5c4113dSnw141292 */
sqliteErrorMsg(Parse * pParse,const char * zFormat,...)421*c5c4113dSnw141292 void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){
422*c5c4113dSnw141292 va_list ap;
423*c5c4113dSnw141292 pParse->nErr++;
424*c5c4113dSnw141292 sqliteFree(pParse->zErrMsg);
425*c5c4113dSnw141292 va_start(ap, zFormat);
426*c5c4113dSnw141292 pParse->zErrMsg = sqliteVMPrintf(zFormat, ap);
427*c5c4113dSnw141292 va_end(ap);
428*c5c4113dSnw141292 }
429*c5c4113dSnw141292
430*c5c4113dSnw141292 /*
431*c5c4113dSnw141292 ** Convert an SQL-style quoted string into a normal string by removing
432*c5c4113dSnw141292 ** the quote characters. The conversion is done in-place. If the
433*c5c4113dSnw141292 ** input does not begin with a quote character, then this routine
434*c5c4113dSnw141292 ** is a no-op.
435*c5c4113dSnw141292 **
436*c5c4113dSnw141292 ** 2002-Feb-14: This routine is extended to remove MS-Access style
437*c5c4113dSnw141292 ** brackets from around identifers. For example: "[a-b-c]" becomes
438*c5c4113dSnw141292 ** "a-b-c".
439*c5c4113dSnw141292 */
sqliteDequote(char * z)440*c5c4113dSnw141292 void sqliteDequote(char *z){
441*c5c4113dSnw141292 int quote;
442*c5c4113dSnw141292 int i, j;
443*c5c4113dSnw141292 if( z==0 ) return;
444*c5c4113dSnw141292 quote = z[0];
445*c5c4113dSnw141292 switch( quote ){
446*c5c4113dSnw141292 case '\'': break;
447*c5c4113dSnw141292 case '"': break;
448*c5c4113dSnw141292 case '[': quote = ']'; break;
449*c5c4113dSnw141292 default: return;
450*c5c4113dSnw141292 }
451*c5c4113dSnw141292 for(i=1, j=0; z[i]; i++){
452*c5c4113dSnw141292 if( z[i]==quote ){
453*c5c4113dSnw141292 if( z[i+1]==quote ){
454*c5c4113dSnw141292 z[j++] = quote;
455*c5c4113dSnw141292 i++;
456*c5c4113dSnw141292 }else{
457*c5c4113dSnw141292 z[j++] = 0;
458*c5c4113dSnw141292 break;
459*c5c4113dSnw141292 }
460*c5c4113dSnw141292 }else{
461*c5c4113dSnw141292 z[j++] = z[i];
462*c5c4113dSnw141292 }
463*c5c4113dSnw141292 }
464*c5c4113dSnw141292 }
465*c5c4113dSnw141292
466*c5c4113dSnw141292 /* An array to map all upper-case characters into their corresponding
467*c5c4113dSnw141292 ** lower-case character.
468*c5c4113dSnw141292 */
469*c5c4113dSnw141292 static unsigned char UpperToLower[] = {
470*c5c4113dSnw141292 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
471*c5c4113dSnw141292 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
472*c5c4113dSnw141292 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
473*c5c4113dSnw141292 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
474*c5c4113dSnw141292 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
475*c5c4113dSnw141292 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
476*c5c4113dSnw141292 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
477*c5c4113dSnw141292 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
478*c5c4113dSnw141292 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
479*c5c4113dSnw141292 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
480*c5c4113dSnw141292 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
481*c5c4113dSnw141292 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
482*c5c4113dSnw141292 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
483*c5c4113dSnw141292 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
484*c5c4113dSnw141292 252,253,254,255
485*c5c4113dSnw141292 };
486*c5c4113dSnw141292
487*c5c4113dSnw141292 /*
488*c5c4113dSnw141292 ** This function computes a hash on the name of a keyword.
489*c5c4113dSnw141292 ** Case is not significant.
490*c5c4113dSnw141292 */
sqliteHashNoCase(const char * z,int n)491*c5c4113dSnw141292 int sqliteHashNoCase(const char *z, int n){
492*c5c4113dSnw141292 int h = 0;
493*c5c4113dSnw141292 if( n<=0 ) n = strlen(z);
494*c5c4113dSnw141292 while( n > 0 ){
495*c5c4113dSnw141292 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
496*c5c4113dSnw141292 n--;
497*c5c4113dSnw141292 }
498*c5c4113dSnw141292 return h & 0x7fffffff;
499*c5c4113dSnw141292 }
500*c5c4113dSnw141292
501*c5c4113dSnw141292 /*
502*c5c4113dSnw141292 ** Some systems have stricmp(). Others have strcasecmp(). Because
503*c5c4113dSnw141292 ** there is no consistency, we will define our own.
504*c5c4113dSnw141292 */
sqliteStrICmp(const char * zLeft,const char * zRight)505*c5c4113dSnw141292 int sqliteStrICmp(const char *zLeft, const char *zRight){
506*c5c4113dSnw141292 register unsigned char *a, *b;
507*c5c4113dSnw141292 a = (unsigned char *)zLeft;
508*c5c4113dSnw141292 b = (unsigned char *)zRight;
509*c5c4113dSnw141292 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
510*c5c4113dSnw141292 return UpperToLower[*a] - UpperToLower[*b];
511*c5c4113dSnw141292 }
sqliteStrNICmp(const char * zLeft,const char * zRight,int N)512*c5c4113dSnw141292 int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
513*c5c4113dSnw141292 register unsigned char *a, *b;
514*c5c4113dSnw141292 a = (unsigned char *)zLeft;
515*c5c4113dSnw141292 b = (unsigned char *)zRight;
516*c5c4113dSnw141292 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
517*c5c4113dSnw141292 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
518*c5c4113dSnw141292 }
519*c5c4113dSnw141292
520*c5c4113dSnw141292 /*
521*c5c4113dSnw141292 ** Return TRUE if z is a pure numeric string. Return FALSE if the
522*c5c4113dSnw141292 ** string contains any character which is not part of a number.
523*c5c4113dSnw141292 **
524*c5c4113dSnw141292 ** Am empty string is considered non-numeric.
525*c5c4113dSnw141292 */
sqliteIsNumber(const char * z)526*c5c4113dSnw141292 int sqliteIsNumber(const char *z){
527*c5c4113dSnw141292 if( *z=='-' || *z=='+' ) z++;
528*c5c4113dSnw141292 if( !isdigit(*z) ){
529*c5c4113dSnw141292 return 0;
530*c5c4113dSnw141292 }
531*c5c4113dSnw141292 z++;
532*c5c4113dSnw141292 while( isdigit(*z) ){ z++; }
533*c5c4113dSnw141292 if( *z=='.' ){
534*c5c4113dSnw141292 z++;
535*c5c4113dSnw141292 if( !isdigit(*z) ) return 0;
536*c5c4113dSnw141292 while( isdigit(*z) ){ z++; }
537*c5c4113dSnw141292 }
538*c5c4113dSnw141292 if( *z=='e' || *z=='E' ){
539*c5c4113dSnw141292 z++;
540*c5c4113dSnw141292 if( *z=='+' || *z=='-' ) z++;
541*c5c4113dSnw141292 if( !isdigit(*z) ) return 0;
542*c5c4113dSnw141292 while( isdigit(*z) ){ z++; }
543*c5c4113dSnw141292 }
544*c5c4113dSnw141292 return *z==0;
545*c5c4113dSnw141292 }
546*c5c4113dSnw141292
547*c5c4113dSnw141292 /*
548*c5c4113dSnw141292 ** The string z[] is an ascii representation of a real number.
549*c5c4113dSnw141292 ** Convert this string to a double.
550*c5c4113dSnw141292 **
551*c5c4113dSnw141292 ** This routine assumes that z[] really is a valid number. If it
552*c5c4113dSnw141292 ** is not, the result is undefined.
553*c5c4113dSnw141292 **
554*c5c4113dSnw141292 ** This routine is used instead of the library atof() function because
555*c5c4113dSnw141292 ** the library atof() might want to use "," as the decimal point instead
556*c5c4113dSnw141292 ** of "." depending on how locale is set. But that would cause problems
557*c5c4113dSnw141292 ** for SQL. So this routine always uses "." regardless of locale.
558*c5c4113dSnw141292 */
sqliteAtoF(const char * z,const char ** pzEnd)559*c5c4113dSnw141292 double sqliteAtoF(const char *z, const char **pzEnd){
560*c5c4113dSnw141292 int sign = 1;
561*c5c4113dSnw141292 LONGDOUBLE_TYPE v1 = 0.0;
562*c5c4113dSnw141292 if( *z=='-' ){
563*c5c4113dSnw141292 sign = -1;
564*c5c4113dSnw141292 z++;
565*c5c4113dSnw141292 }else if( *z=='+' ){
566*c5c4113dSnw141292 z++;
567*c5c4113dSnw141292 }
568*c5c4113dSnw141292 while( isdigit(*z) ){
569*c5c4113dSnw141292 v1 = v1*10.0 + (*z - '0');
570*c5c4113dSnw141292 z++;
571*c5c4113dSnw141292 }
572*c5c4113dSnw141292 if( *z=='.' ){
573*c5c4113dSnw141292 LONGDOUBLE_TYPE divisor = 1.0;
574*c5c4113dSnw141292 z++;
575*c5c4113dSnw141292 while( isdigit(*z) ){
576*c5c4113dSnw141292 v1 = v1*10.0 + (*z - '0');
577*c5c4113dSnw141292 divisor *= 10.0;
578*c5c4113dSnw141292 z++;
579*c5c4113dSnw141292 }
580*c5c4113dSnw141292 v1 /= divisor;
581*c5c4113dSnw141292 }
582*c5c4113dSnw141292 if( *z=='e' || *z=='E' ){
583*c5c4113dSnw141292 int esign = 1;
584*c5c4113dSnw141292 int eval = 0;
585*c5c4113dSnw141292 LONGDOUBLE_TYPE scale = 1.0;
586*c5c4113dSnw141292 z++;
587*c5c4113dSnw141292 if( *z=='-' ){
588*c5c4113dSnw141292 esign = -1;
589*c5c4113dSnw141292 z++;
590*c5c4113dSnw141292 }else if( *z=='+' ){
591*c5c4113dSnw141292 z++;
592*c5c4113dSnw141292 }
593*c5c4113dSnw141292 while( isdigit(*z) ){
594*c5c4113dSnw141292 eval = eval*10 + *z - '0';
595*c5c4113dSnw141292 z++;
596*c5c4113dSnw141292 }
597*c5c4113dSnw141292 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
598*c5c4113dSnw141292 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
599*c5c4113dSnw141292 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
600*c5c4113dSnw141292 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
601*c5c4113dSnw141292 if( esign<0 ){
602*c5c4113dSnw141292 v1 /= scale;
603*c5c4113dSnw141292 }else{
604*c5c4113dSnw141292 v1 *= scale;
605*c5c4113dSnw141292 }
606*c5c4113dSnw141292 }
607*c5c4113dSnw141292 if( pzEnd ) *pzEnd = z;
608*c5c4113dSnw141292 return sign<0 ? -v1 : v1;
609*c5c4113dSnw141292 }
610*c5c4113dSnw141292
611*c5c4113dSnw141292 /*
612*c5c4113dSnw141292 ** The string zNum represents an integer. There might be some other
613*c5c4113dSnw141292 ** information following the integer too, but that part is ignored.
614*c5c4113dSnw141292 ** If the integer that the prefix of zNum represents will fit in a
615*c5c4113dSnw141292 ** 32-bit signed integer, return TRUE. Otherwise return FALSE.
616*c5c4113dSnw141292 **
617*c5c4113dSnw141292 ** This routine returns FALSE for the string -2147483648 even that
618*c5c4113dSnw141292 ** that number will, in theory fit in a 32-bit integer. But positive
619*c5c4113dSnw141292 ** 2147483648 will not fit in 32 bits. So it seems safer to return
620*c5c4113dSnw141292 ** false.
621*c5c4113dSnw141292 */
sqliteFitsIn32Bits(const char * zNum)622*c5c4113dSnw141292 int sqliteFitsIn32Bits(const char *zNum){
623*c5c4113dSnw141292 int i, c;
624*c5c4113dSnw141292 if( *zNum=='-' || *zNum=='+' ) zNum++;
625*c5c4113dSnw141292 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
626*c5c4113dSnw141292 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
627*c5c4113dSnw141292 }
628*c5c4113dSnw141292
629*c5c4113dSnw141292 /* This comparison routine is what we use for comparison operations
630*c5c4113dSnw141292 ** between numeric values in an SQL expression. "Numeric" is a little
631*c5c4113dSnw141292 ** bit misleading here. What we mean is that the strings have a
632*c5c4113dSnw141292 ** type of "numeric" from the point of view of SQL. The strings
633*c5c4113dSnw141292 ** do not necessarily contain numbers. They could contain text.
634*c5c4113dSnw141292 **
635*c5c4113dSnw141292 ** If the input strings both look like actual numbers then they
636*c5c4113dSnw141292 ** compare in numerical order. Numerical strings are always less
637*c5c4113dSnw141292 ** than non-numeric strings so if one input string looks like a
638*c5c4113dSnw141292 ** number and the other does not, then the one that looks like
639*c5c4113dSnw141292 ** a number is the smaller. Non-numeric strings compare in
640*c5c4113dSnw141292 ** lexigraphical order (the same order as strcmp()).
641*c5c4113dSnw141292 */
sqliteCompare(const char * atext,const char * btext)642*c5c4113dSnw141292 int sqliteCompare(const char *atext, const char *btext){
643*c5c4113dSnw141292 int result;
644*c5c4113dSnw141292 int isNumA, isNumB;
645*c5c4113dSnw141292 if( atext==0 ){
646*c5c4113dSnw141292 return -1;
647*c5c4113dSnw141292 }else if( btext==0 ){
648*c5c4113dSnw141292 return 1;
649*c5c4113dSnw141292 }
650*c5c4113dSnw141292 isNumA = sqliteIsNumber(atext);
651*c5c4113dSnw141292 isNumB = sqliteIsNumber(btext);
652*c5c4113dSnw141292 if( isNumA ){
653*c5c4113dSnw141292 if( !isNumB ){
654*c5c4113dSnw141292 result = -1;
655*c5c4113dSnw141292 }else{
656*c5c4113dSnw141292 double rA, rB;
657*c5c4113dSnw141292 rA = sqliteAtoF(atext, 0);
658*c5c4113dSnw141292 rB = sqliteAtoF(btext, 0);
659*c5c4113dSnw141292 if( rA<rB ){
660*c5c4113dSnw141292 result = -1;
661*c5c4113dSnw141292 }else if( rA>rB ){
662*c5c4113dSnw141292 result = +1;
663*c5c4113dSnw141292 }else{
664*c5c4113dSnw141292 result = 0;
665*c5c4113dSnw141292 }
666*c5c4113dSnw141292 }
667*c5c4113dSnw141292 }else if( isNumB ){
668*c5c4113dSnw141292 result = +1;
669*c5c4113dSnw141292 }else {
670*c5c4113dSnw141292 result = strcmp(atext, btext);
671*c5c4113dSnw141292 }
672*c5c4113dSnw141292 return result;
673*c5c4113dSnw141292 }
674*c5c4113dSnw141292
675*c5c4113dSnw141292 /*
676*c5c4113dSnw141292 ** This routine is used for sorting. Each key is a list of one or more
677*c5c4113dSnw141292 ** null-terminated elements. The list is terminated by two nulls in
678*c5c4113dSnw141292 ** a row. For example, the following text is a key with three elements
679*c5c4113dSnw141292 **
680*c5c4113dSnw141292 ** Aone\000Dtwo\000Athree\000\000
681*c5c4113dSnw141292 **
682*c5c4113dSnw141292 ** All elements begin with one of the characters "+-AD" and end with "\000"
683*c5c4113dSnw141292 ** with zero or more text elements in between. Except, NULL elements
684*c5c4113dSnw141292 ** consist of the special two-character sequence "N\000".
685*c5c4113dSnw141292 **
686*c5c4113dSnw141292 ** Both arguments will have the same number of elements. This routine
687*c5c4113dSnw141292 ** returns negative, zero, or positive if the first argument is less
688*c5c4113dSnw141292 ** than, equal to, or greater than the first. (Result is a-b).
689*c5c4113dSnw141292 **
690*c5c4113dSnw141292 ** Each element begins with one of the characters "+", "-", "A", "D".
691*c5c4113dSnw141292 ** This character determines the sort order and collating sequence:
692*c5c4113dSnw141292 **
693*c5c4113dSnw141292 ** + Sort numerically in ascending order
694*c5c4113dSnw141292 ** - Sort numerically in descending order
695*c5c4113dSnw141292 ** A Sort as strings in ascending order
696*c5c4113dSnw141292 ** D Sort as strings in descending order.
697*c5c4113dSnw141292 **
698*c5c4113dSnw141292 ** For the "+" and "-" sorting, pure numeric strings (strings for which the
699*c5c4113dSnw141292 ** isNum() function above returns TRUE) always compare less than strings
700*c5c4113dSnw141292 ** that are not pure numerics. Non-numeric strings compare in memcmp()
701*c5c4113dSnw141292 ** order. This is the same sort order as the sqliteCompare() function
702*c5c4113dSnw141292 ** above generates.
703*c5c4113dSnw141292 **
704*c5c4113dSnw141292 ** The last point is a change from version 2.6.3 to version 2.7.0. In
705*c5c4113dSnw141292 ** version 2.6.3 and earlier, substrings of digits compare in numerical
706*c5c4113dSnw141292 ** and case was used only to break a tie.
707*c5c4113dSnw141292 **
708*c5c4113dSnw141292 ** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
709*c5c4113dSnw141292 ** of whether or not they look like a number.
710*c5c4113dSnw141292 **
711*c5c4113dSnw141292 ** Note that the sort order imposed by the rules above is the same
712*c5c4113dSnw141292 ** from the ordering defined by the "<", "<=", ">", and ">=" operators
713*c5c4113dSnw141292 ** of expressions and for indices. This was not the case for version
714*c5c4113dSnw141292 ** 2.6.3 and earlier.
715*c5c4113dSnw141292 */
sqliteSortCompare(const char * a,const char * b)716*c5c4113dSnw141292 int sqliteSortCompare(const char *a, const char *b){
717*c5c4113dSnw141292 int res = 0;
718*c5c4113dSnw141292 int isNumA, isNumB;
719*c5c4113dSnw141292 int dir = 0;
720*c5c4113dSnw141292
721*c5c4113dSnw141292 while( res==0 && *a && *b ){
722*c5c4113dSnw141292 if( a[0]=='N' || b[0]=='N' ){
723*c5c4113dSnw141292 if( a[0]==b[0] ){
724*c5c4113dSnw141292 a += 2;
725*c5c4113dSnw141292 b += 2;
726*c5c4113dSnw141292 continue;
727*c5c4113dSnw141292 }
728*c5c4113dSnw141292 if( a[0]=='N' ){
729*c5c4113dSnw141292 dir = b[0];
730*c5c4113dSnw141292 res = -1;
731*c5c4113dSnw141292 }else{
732*c5c4113dSnw141292 dir = a[0];
733*c5c4113dSnw141292 res = +1;
734*c5c4113dSnw141292 }
735*c5c4113dSnw141292 break;
736*c5c4113dSnw141292 }
737*c5c4113dSnw141292 assert( a[0]==b[0] );
738*c5c4113dSnw141292 if( (dir=a[0])=='A' || a[0]=='D' ){
739*c5c4113dSnw141292 res = strcmp(&a[1],&b[1]);
740*c5c4113dSnw141292 if( res ) break;
741*c5c4113dSnw141292 }else{
742*c5c4113dSnw141292 isNumA = sqliteIsNumber(&a[1]);
743*c5c4113dSnw141292 isNumB = sqliteIsNumber(&b[1]);
744*c5c4113dSnw141292 if( isNumA ){
745*c5c4113dSnw141292 double rA, rB;
746*c5c4113dSnw141292 if( !isNumB ){
747*c5c4113dSnw141292 res = -1;
748*c5c4113dSnw141292 break;
749*c5c4113dSnw141292 }
750*c5c4113dSnw141292 rA = sqliteAtoF(&a[1], 0);
751*c5c4113dSnw141292 rB = sqliteAtoF(&b[1], 0);
752*c5c4113dSnw141292 if( rA<rB ){
753*c5c4113dSnw141292 res = -1;
754*c5c4113dSnw141292 break;
755*c5c4113dSnw141292 }
756*c5c4113dSnw141292 if( rA>rB ){
757*c5c4113dSnw141292 res = +1;
758*c5c4113dSnw141292 break;
759*c5c4113dSnw141292 }
760*c5c4113dSnw141292 }else if( isNumB ){
761*c5c4113dSnw141292 res = +1;
762*c5c4113dSnw141292 break;
763*c5c4113dSnw141292 }else{
764*c5c4113dSnw141292 res = strcmp(&a[1],&b[1]);
765*c5c4113dSnw141292 if( res ) break;
766*c5c4113dSnw141292 }
767*c5c4113dSnw141292 }
768*c5c4113dSnw141292 a += strlen(&a[1]) + 2;
769*c5c4113dSnw141292 b += strlen(&b[1]) + 2;
770*c5c4113dSnw141292 }
771*c5c4113dSnw141292 if( dir=='-' || dir=='D' ) res = -res;
772*c5c4113dSnw141292 return res;
773*c5c4113dSnw141292 }
774*c5c4113dSnw141292
775*c5c4113dSnw141292 /*
776*c5c4113dSnw141292 ** Some powers of 64. These constants are needed in the
777*c5c4113dSnw141292 ** sqliteRealToSortable() routine below.
778*c5c4113dSnw141292 */
779*c5c4113dSnw141292 #define _64e3 (64.0 * 64.0 * 64.0)
780*c5c4113dSnw141292 #define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
781*c5c4113dSnw141292 #define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
782*c5c4113dSnw141292 #define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
783*c5c4113dSnw141292 #define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
784*c5c4113dSnw141292 #define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
785*c5c4113dSnw141292
786*c5c4113dSnw141292 /*
787*c5c4113dSnw141292 ** The following procedure converts a double-precision floating point
788*c5c4113dSnw141292 ** number into a string. The resulting string has the property that
789*c5c4113dSnw141292 ** two such strings comparied using strcmp() or memcmp() will give the
790*c5c4113dSnw141292 ** same results as a numeric comparison of the original floating point
791*c5c4113dSnw141292 ** numbers.
792*c5c4113dSnw141292 **
793*c5c4113dSnw141292 ** This routine is used to generate database keys from floating point
794*c5c4113dSnw141292 ** numbers such that the keys sort in the same order as the original
795*c5c4113dSnw141292 ** floating point numbers even though the keys are compared using
796*c5c4113dSnw141292 ** memcmp().
797*c5c4113dSnw141292 **
798*c5c4113dSnw141292 ** The calling function should have allocated at least 14 characters
799*c5c4113dSnw141292 ** of space for the buffer z[].
800*c5c4113dSnw141292 */
sqliteRealToSortable(double r,char * z)801*c5c4113dSnw141292 void sqliteRealToSortable(double r, char *z){
802*c5c4113dSnw141292 int neg;
803*c5c4113dSnw141292 int exp;
804*c5c4113dSnw141292 int cnt = 0;
805*c5c4113dSnw141292
806*c5c4113dSnw141292 /* This array maps integers between 0 and 63 into base-64 digits.
807*c5c4113dSnw141292 ** The digits must be chosen such at their ASCII codes are increasing.
808*c5c4113dSnw141292 ** This means we can not use the traditional base-64 digit set. */
809*c5c4113dSnw141292 static const char zDigit[] =
810*c5c4113dSnw141292 "0123456789"
811*c5c4113dSnw141292 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
812*c5c4113dSnw141292 "abcdefghijklmnopqrstuvwxyz"
813*c5c4113dSnw141292 "|~";
814*c5c4113dSnw141292 if( r<0.0 ){
815*c5c4113dSnw141292 neg = 1;
816*c5c4113dSnw141292 r = -r;
817*c5c4113dSnw141292 *z++ = '-';
818*c5c4113dSnw141292 } else {
819*c5c4113dSnw141292 neg = 0;
820*c5c4113dSnw141292 *z++ = '0';
821*c5c4113dSnw141292 }
822*c5c4113dSnw141292 exp = 0;
823*c5c4113dSnw141292
824*c5c4113dSnw141292 if( r==0.0 ){
825*c5c4113dSnw141292 exp = -1024;
826*c5c4113dSnw141292 }else if( r<(0.5/64.0) ){
827*c5c4113dSnw141292 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
828*c5c4113dSnw141292 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
829*c5c4113dSnw141292 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
830*c5c4113dSnw141292 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
831*c5c4113dSnw141292 }else if( r>=0.5 ){
832*c5c4113dSnw141292 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
833*c5c4113dSnw141292 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
834*c5c4113dSnw141292 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
835*c5c4113dSnw141292 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
836*c5c4113dSnw141292 }
837*c5c4113dSnw141292 if( neg ){
838*c5c4113dSnw141292 exp = -exp;
839*c5c4113dSnw141292 r = -r;
840*c5c4113dSnw141292 }
841*c5c4113dSnw141292 exp += 1024;
842*c5c4113dSnw141292 r += 0.5;
843*c5c4113dSnw141292 if( exp<0 ) return;
844*c5c4113dSnw141292 if( exp>=2048 || r>=1.0 ){
845*c5c4113dSnw141292 strcpy(z, "~~~~~~~~~~~~");
846*c5c4113dSnw141292 return;
847*c5c4113dSnw141292 }
848*c5c4113dSnw141292 *z++ = zDigit[(exp>>6)&0x3f];
849*c5c4113dSnw141292 *z++ = zDigit[exp & 0x3f];
850*c5c4113dSnw141292 while( r>0.0 && cnt<10 ){
851*c5c4113dSnw141292 int digit;
852*c5c4113dSnw141292 r *= 64.0;
853*c5c4113dSnw141292 digit = (int)r;
854*c5c4113dSnw141292 assert( digit>=0 && digit<64 );
855*c5c4113dSnw141292 *z++ = zDigit[digit & 0x3f];
856*c5c4113dSnw141292 r -= digit;
857*c5c4113dSnw141292 cnt++;
858*c5c4113dSnw141292 }
859*c5c4113dSnw141292 *z = 0;
860*c5c4113dSnw141292 }
861*c5c4113dSnw141292
862*c5c4113dSnw141292 #ifdef SQLITE_UTF8
863*c5c4113dSnw141292 /*
864*c5c4113dSnw141292 ** X is a pointer to the first byte of a UTF-8 character. Increment
865*c5c4113dSnw141292 ** X so that it points to the next character. This only works right
866*c5c4113dSnw141292 ** if X points to a well-formed UTF-8 string.
867*c5c4113dSnw141292 */
868*c5c4113dSnw141292 #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
869*c5c4113dSnw141292 #define sqliteCharVal(X) sqlite_utf8_to_int(X)
870*c5c4113dSnw141292
871*c5c4113dSnw141292 #else /* !defined(SQLITE_UTF8) */
872*c5c4113dSnw141292 /*
873*c5c4113dSnw141292 ** For iso8859 encoding, the next character is just the next byte.
874*c5c4113dSnw141292 */
875*c5c4113dSnw141292 #define sqliteNextChar(X) (++(X));
876*c5c4113dSnw141292 #define sqliteCharVal(X) ((int)*(X))
877*c5c4113dSnw141292
878*c5c4113dSnw141292 #endif /* defined(SQLITE_UTF8) */
879*c5c4113dSnw141292
880*c5c4113dSnw141292
881*c5c4113dSnw141292 #ifdef SQLITE_UTF8
882*c5c4113dSnw141292 /*
883*c5c4113dSnw141292 ** Convert the UTF-8 character to which z points into a 31-bit
884*c5c4113dSnw141292 ** UCS character. This only works right if z points to a well-formed
885*c5c4113dSnw141292 ** UTF-8 string.
886*c5c4113dSnw141292 */
sqlite_utf8_to_int(const unsigned char * z)887*c5c4113dSnw141292 static int sqlite_utf8_to_int(const unsigned char *z){
888*c5c4113dSnw141292 int c;
889*c5c4113dSnw141292 static const int initVal[] = {
890*c5c4113dSnw141292 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
891*c5c4113dSnw141292 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
892*c5c4113dSnw141292 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
893*c5c4113dSnw141292 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
894*c5c4113dSnw141292 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
895*c5c4113dSnw141292 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
896*c5c4113dSnw141292 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
897*c5c4113dSnw141292 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
898*c5c4113dSnw141292 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
899*c5c4113dSnw141292 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
900*c5c4113dSnw141292 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
901*c5c4113dSnw141292 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
902*c5c4113dSnw141292 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
903*c5c4113dSnw141292 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
904*c5c4113dSnw141292 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
905*c5c4113dSnw141292 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
906*c5c4113dSnw141292 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
907*c5c4113dSnw141292 255,
908*c5c4113dSnw141292 };
909*c5c4113dSnw141292 c = initVal[*(z++)];
910*c5c4113dSnw141292 while( (0xc0&*z)==0x80 ){
911*c5c4113dSnw141292 c = (c<<6) | (0x3f&*(z++));
912*c5c4113dSnw141292 }
913*c5c4113dSnw141292 return c;
914*c5c4113dSnw141292 }
915*c5c4113dSnw141292 #endif
916*c5c4113dSnw141292
917*c5c4113dSnw141292 /*
918*c5c4113dSnw141292 ** Compare two UTF-8 strings for equality where the first string can
919*c5c4113dSnw141292 ** potentially be a "glob" expression. Return true (1) if they
920*c5c4113dSnw141292 ** are the same and false (0) if they are different.
921*c5c4113dSnw141292 **
922*c5c4113dSnw141292 ** Globbing rules:
923*c5c4113dSnw141292 **
924*c5c4113dSnw141292 ** '*' Matches any sequence of zero or more characters.
925*c5c4113dSnw141292 **
926*c5c4113dSnw141292 ** '?' Matches exactly one character.
927*c5c4113dSnw141292 **
928*c5c4113dSnw141292 ** [...] Matches one character from the enclosed list of
929*c5c4113dSnw141292 ** characters.
930*c5c4113dSnw141292 **
931*c5c4113dSnw141292 ** [^...] Matches one character not in the enclosed list.
932*c5c4113dSnw141292 **
933*c5c4113dSnw141292 ** With the [...] and [^...] matching, a ']' character can be included
934*c5c4113dSnw141292 ** in the list by making it the first character after '[' or '^'. A
935*c5c4113dSnw141292 ** range of characters can be specified using '-'. Example:
936*c5c4113dSnw141292 ** "[a-z]" matches any single lower-case letter. To match a '-', make
937*c5c4113dSnw141292 ** it the last character in the list.
938*c5c4113dSnw141292 **
939*c5c4113dSnw141292 ** This routine is usually quick, but can be N**2 in the worst case.
940*c5c4113dSnw141292 **
941*c5c4113dSnw141292 ** Hints: to match '*' or '?', put them in "[]". Like this:
942*c5c4113dSnw141292 **
943*c5c4113dSnw141292 ** abc[*]xyz Matches "abc*xyz" only
944*c5c4113dSnw141292 */
945*c5c4113dSnw141292 int
sqliteGlobCompare(const unsigned char * zPattern,const unsigned char * zString)946*c5c4113dSnw141292 sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
947*c5c4113dSnw141292 register int c;
948*c5c4113dSnw141292 int invert;
949*c5c4113dSnw141292 int seen;
950*c5c4113dSnw141292 int c2;
951*c5c4113dSnw141292
952*c5c4113dSnw141292 while( (c = *zPattern)!=0 ){
953*c5c4113dSnw141292 switch( c ){
954*c5c4113dSnw141292 case '*':
955*c5c4113dSnw141292 while( (c=zPattern[1]) == '*' || c == '?' ){
956*c5c4113dSnw141292 if( c=='?' ){
957*c5c4113dSnw141292 if( *zString==0 ) return 0;
958*c5c4113dSnw141292 sqliteNextChar(zString);
959*c5c4113dSnw141292 }
960*c5c4113dSnw141292 zPattern++;
961*c5c4113dSnw141292 }
962*c5c4113dSnw141292 if( c==0 ) return 1;
963*c5c4113dSnw141292 if( c=='[' ){
964*c5c4113dSnw141292 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
965*c5c4113dSnw141292 sqliteNextChar(zString);
966*c5c4113dSnw141292 }
967*c5c4113dSnw141292 return *zString!=0;
968*c5c4113dSnw141292 }else{
969*c5c4113dSnw141292 while( (c2 = *zString)!=0 ){
970*c5c4113dSnw141292 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
971*c5c4113dSnw141292 if( c2==0 ) return 0;
972*c5c4113dSnw141292 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
973*c5c4113dSnw141292 sqliteNextChar(zString);
974*c5c4113dSnw141292 }
975*c5c4113dSnw141292 return 0;
976*c5c4113dSnw141292 }
977*c5c4113dSnw141292 case '?': {
978*c5c4113dSnw141292 if( *zString==0 ) return 0;
979*c5c4113dSnw141292 sqliteNextChar(zString);
980*c5c4113dSnw141292 zPattern++;
981*c5c4113dSnw141292 break;
982*c5c4113dSnw141292 }
983*c5c4113dSnw141292 case '[': {
984*c5c4113dSnw141292 int prior_c = 0;
985*c5c4113dSnw141292 seen = 0;
986*c5c4113dSnw141292 invert = 0;
987*c5c4113dSnw141292 c = sqliteCharVal(zString);
988*c5c4113dSnw141292 if( c==0 ) return 0;
989*c5c4113dSnw141292 c2 = *++zPattern;
990*c5c4113dSnw141292 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
991*c5c4113dSnw141292 if( c2==']' ){
992*c5c4113dSnw141292 if( c==']' ) seen = 1;
993*c5c4113dSnw141292 c2 = *++zPattern;
994*c5c4113dSnw141292 }
995*c5c4113dSnw141292 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
996*c5c4113dSnw141292 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
997*c5c4113dSnw141292 zPattern++;
998*c5c4113dSnw141292 c2 = sqliteCharVal(zPattern);
999*c5c4113dSnw141292 if( c>=prior_c && c<=c2 ) seen = 1;
1000*c5c4113dSnw141292 prior_c = 0;
1001*c5c4113dSnw141292 }else if( c==c2 ){
1002*c5c4113dSnw141292 seen = 1;
1003*c5c4113dSnw141292 prior_c = c2;
1004*c5c4113dSnw141292 }else{
1005*c5c4113dSnw141292 prior_c = c2;
1006*c5c4113dSnw141292 }
1007*c5c4113dSnw141292 sqliteNextChar(zPattern);
1008*c5c4113dSnw141292 }
1009*c5c4113dSnw141292 if( c2==0 || (seen ^ invert)==0 ) return 0;
1010*c5c4113dSnw141292 sqliteNextChar(zString);
1011*c5c4113dSnw141292 zPattern++;
1012*c5c4113dSnw141292 break;
1013*c5c4113dSnw141292 }
1014*c5c4113dSnw141292 default: {
1015*c5c4113dSnw141292 if( c != *zString ) return 0;
1016*c5c4113dSnw141292 zPattern++;
1017*c5c4113dSnw141292 zString++;
1018*c5c4113dSnw141292 break;
1019*c5c4113dSnw141292 }
1020*c5c4113dSnw141292 }
1021*c5c4113dSnw141292 }
1022*c5c4113dSnw141292 return *zString==0;
1023*c5c4113dSnw141292 }
1024*c5c4113dSnw141292
1025*c5c4113dSnw141292 /*
1026*c5c4113dSnw141292 ** Compare two UTF-8 strings for equality using the "LIKE" operator of
1027*c5c4113dSnw141292 ** SQL. The '%' character matches any sequence of 0 or more
1028*c5c4113dSnw141292 ** characters and '_' matches any single character. Case is
1029*c5c4113dSnw141292 ** not significant.
1030*c5c4113dSnw141292 **
1031*c5c4113dSnw141292 ** This routine is just an adaptation of the sqliteGlobCompare()
1032*c5c4113dSnw141292 ** routine above.
1033*c5c4113dSnw141292 */
1034*c5c4113dSnw141292 int
sqliteLikeCompare(const unsigned char * zPattern,const unsigned char * zString)1035*c5c4113dSnw141292 sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
1036*c5c4113dSnw141292 register int c;
1037*c5c4113dSnw141292 int c2;
1038*c5c4113dSnw141292
1039*c5c4113dSnw141292 while( (c = UpperToLower[*zPattern])!=0 ){
1040*c5c4113dSnw141292 switch( c ){
1041*c5c4113dSnw141292 case '%': {
1042*c5c4113dSnw141292 while( (c=zPattern[1]) == '%' || c == '_' ){
1043*c5c4113dSnw141292 if( c=='_' ){
1044*c5c4113dSnw141292 if( *zString==0 ) return 0;
1045*c5c4113dSnw141292 sqliteNextChar(zString);
1046*c5c4113dSnw141292 }
1047*c5c4113dSnw141292 zPattern++;
1048*c5c4113dSnw141292 }
1049*c5c4113dSnw141292 if( c==0 ) return 1;
1050*c5c4113dSnw141292 c = UpperToLower[c];
1051*c5c4113dSnw141292 while( (c2=UpperToLower[*zString])!=0 ){
1052*c5c4113dSnw141292 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1053*c5c4113dSnw141292 if( c2==0 ) return 0;
1054*c5c4113dSnw141292 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
1055*c5c4113dSnw141292 sqliteNextChar(zString);
1056*c5c4113dSnw141292 }
1057*c5c4113dSnw141292 return 0;
1058*c5c4113dSnw141292 }
1059*c5c4113dSnw141292 case '_': {
1060*c5c4113dSnw141292 if( *zString==0 ) return 0;
1061*c5c4113dSnw141292 sqliteNextChar(zString);
1062*c5c4113dSnw141292 zPattern++;
1063*c5c4113dSnw141292 break;
1064*c5c4113dSnw141292 }
1065*c5c4113dSnw141292 default: {
1066*c5c4113dSnw141292 if( c != UpperToLower[*zString] ) return 0;
1067*c5c4113dSnw141292 zPattern++;
1068*c5c4113dSnw141292 zString++;
1069*c5c4113dSnw141292 break;
1070*c5c4113dSnw141292 }
1071*c5c4113dSnw141292 }
1072*c5c4113dSnw141292 }
1073*c5c4113dSnw141292 return *zString==0;
1074*c5c4113dSnw141292 }
1075*c5c4113dSnw141292
1076*c5c4113dSnw141292 /*
1077*c5c4113dSnw141292 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1078*c5c4113dSnw141292 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1079*c5c4113dSnw141292 ** when this routine is called.
1080*c5c4113dSnw141292 **
1081*c5c4113dSnw141292 ** This routine is a attempt to detect if two threads use the
1082*c5c4113dSnw141292 ** same sqlite* pointer at the same time. There is a race
1083*c5c4113dSnw141292 ** condition so it is possible that the error is not detected.
1084*c5c4113dSnw141292 ** But usually the problem will be seen. The result will be an
1085*c5c4113dSnw141292 ** error which can be used to debug the application that is
1086*c5c4113dSnw141292 ** using SQLite incorrectly.
1087*c5c4113dSnw141292 **
1088*c5c4113dSnw141292 ** Ticket #202: If db->magic is not a valid open value, take care not
1089*c5c4113dSnw141292 ** to modify the db structure at all. It could be that db is a stale
1090*c5c4113dSnw141292 ** pointer. In other words, it could be that there has been a prior
1091*c5c4113dSnw141292 ** call to sqlite_close(db) and db has been deallocated. And we do
1092*c5c4113dSnw141292 ** not want to write into deallocated memory.
1093*c5c4113dSnw141292 */
sqliteSafetyOn(sqlite * db)1094*c5c4113dSnw141292 int sqliteSafetyOn(sqlite *db){
1095*c5c4113dSnw141292 if( db->magic==SQLITE_MAGIC_OPEN ){
1096*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_BUSY;
1097*c5c4113dSnw141292 return 0;
1098*c5c4113dSnw141292 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1099*c5c4113dSnw141292 || db->want_to_close ){
1100*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_ERROR;
1101*c5c4113dSnw141292 db->flags |= SQLITE_Interrupt;
1102*c5c4113dSnw141292 }
1103*c5c4113dSnw141292 return 1;
1104*c5c4113dSnw141292 }
1105*c5c4113dSnw141292
1106*c5c4113dSnw141292 /*
1107*c5c4113dSnw141292 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1108*c5c4113dSnw141292 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1109*c5c4113dSnw141292 ** when this routine is called.
1110*c5c4113dSnw141292 */
sqliteSafetyOff(sqlite * db)1111*c5c4113dSnw141292 int sqliteSafetyOff(sqlite *db){
1112*c5c4113dSnw141292 if( db->magic==SQLITE_MAGIC_BUSY ){
1113*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_OPEN;
1114*c5c4113dSnw141292 return 0;
1115*c5c4113dSnw141292 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1116*c5c4113dSnw141292 || db->want_to_close ){
1117*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_ERROR;
1118*c5c4113dSnw141292 db->flags |= SQLITE_Interrupt;
1119*c5c4113dSnw141292 }
1120*c5c4113dSnw141292 return 1;
1121*c5c4113dSnw141292 }
1122*c5c4113dSnw141292
1123*c5c4113dSnw141292 /*
1124*c5c4113dSnw141292 ** Check to make sure we are not currently executing an sqlite_exec().
1125*c5c4113dSnw141292 ** If we are currently in an sqlite_exec(), return true and set
1126*c5c4113dSnw141292 ** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1127*c5c4113dSnw141292 ** shutdown of the database.
1128*c5c4113dSnw141292 **
1129*c5c4113dSnw141292 ** This routine is used to try to detect when API routines are called
1130*c5c4113dSnw141292 ** at the wrong time or in the wrong sequence.
1131*c5c4113dSnw141292 */
sqliteSafetyCheck(sqlite * db)1132*c5c4113dSnw141292 int sqliteSafetyCheck(sqlite *db){
1133*c5c4113dSnw141292 if( db->pVdbe!=0 ){
1134*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_ERROR;
1135*c5c4113dSnw141292 return 1;
1136*c5c4113dSnw141292 }
1137*c5c4113dSnw141292 return 0;
1138*c5c4113dSnw141292 }
1139