blob: a13b92af596ce9b6598549f19cd5fe514d66d419 [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**
drhc22bd472002-05-10 13:14:07 +000017** $Id: util.c,v 1.43 2002/05/10 13:14:07 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
drh7c68d602000-10-11 19:28:51 +000023/*
drhdaffd0e2001-04-11 14:28:42 +000024** If malloc() ever fails, this global variable gets set to 1.
25** This causes the library to abort and never again function.
26*/
27int sqlite_malloc_failed = 0;
28
29/*
drh7c68d602000-10-11 19:28:51 +000030** If MEMORY_DEBUG is defined, then use versions of malloc() and
31** free() that track memory usage and check for buffer overruns.
32*/
drhdcc581c2000-05-30 13:44:19 +000033#ifdef MEMORY_DEBUG
34
drhdcc581c2000-05-30 13:44:19 +000035/*
drh8c82b352000-12-10 18:23:50 +000036** For keeping track of the number of mallocs and frees. This
37** is used to check for memory leaks.
38*/
39int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
40int sqlite_nFree; /* Number of sqliteFree() calls */
41int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
42
43
44/*
drhdcc581c2000-05-30 13:44:19 +000045** Allocate new memory and set it to zero. Return NULL if
46** no memory is available.
47*/
48void *sqliteMalloc_(int n, char *zFile, int line){
49 void *p;
50 int *pi;
51 int k;
drh6e142f52000-06-08 13:36:40 +000052 if( sqlite_iMallocFail>=0 ){
53 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000054 if( sqlite_iMallocFail==0 ){
55 sqlite_malloc_failed++;
drh6d4abfb2001-10-22 02:58:08 +000056#if MEMORY_DEBUG>1
57 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
58 n, zFile,line);
59#endif
60 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000061 return 0;
62 }
drh6e142f52000-06-08 13:36:40 +000063 }
drhb0729502001-03-14 12:35:57 +000064 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000065 k = (n+sizeof(int)-1)/sizeof(int);
66 pi = malloc( (3+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000067 if( pi==0 ){
68 sqlite_malloc_failed++;
69 return 0;
70 }
drh6d4abfb2001-10-22 02:58:08 +000071 sqlite_nMalloc++;
drhdcc581c2000-05-30 13:44:19 +000072 pi[0] = 0xdead1122;
73 pi[1] = n;
74 pi[k+2] = 0xdead3344;
75 p = &pi[2];
76 memset(p, 0, n);
drhc3c2fc92000-05-31 22:58:39 +000077#if MEMORY_DEBUG>1
78 fprintf(stderr,"malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
79#endif
drhdcc581c2000-05-30 13:44:19 +000080 return p;
81}
82
83/*
84** Free memory previously obtained from sqliteMalloc()
85*/
86void sqliteFree_(void *p, char *zFile, int line){
87 if( p ){
88 int *pi, k, n;
89 pi = p;
90 pi -= 2;
drh6e142f52000-06-08 13:36:40 +000091 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +000092 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +000093 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +000094 return;
95 }
96 n = pi[1];
97 k = (n+sizeof(int)-1)/sizeof(int);
98 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +000099 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000100 return;
101 }
drhc3c2fc92000-05-31 22:58:39 +0000102 memset(pi, 0xff, (k+3)*sizeof(int));
103#if MEMORY_DEBUG>1
104 fprintf(stderr,"free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
105#endif
drhdcc581c2000-05-30 13:44:19 +0000106 free(pi);
107 }
108}
109
110/*
111** Resize a prior allocation. If p==0, then this routine
112** works just like sqliteMalloc(). If n==0, then this routine
113** works just like sqliteFree().
114*/
115void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
116 int *oldPi, *pi, k, oldN, oldK;
117 void *p;
118 if( oldP==0 ){
119 return sqliteMalloc_(n,zFile,line);
120 }
121 if( n==0 ){
122 sqliteFree_(oldP,zFile,line);
123 return 0;
124 }
125 oldPi = oldP;
126 oldPi -= 2;
127 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000128 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000129 return 0;
drhdcc581c2000-05-30 13:44:19 +0000130 }
131 oldN = oldPi[1];
132 oldK = (oldN+sizeof(int)-1)/sizeof(int);
133 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000134 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000135 return 0;
drhdcc581c2000-05-30 13:44:19 +0000136 }
137 k = (n + sizeof(int) - 1)/sizeof(int);
138 pi = malloc( (k+3)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000139 if( pi==0 ){
140 sqlite_malloc_failed++;
141 return 0;
142 }
drhdcc581c2000-05-30 13:44:19 +0000143 pi[0] = 0xdead1122;
144 pi[1] = n;
145 pi[k+2] = 0xdead3344;
146 p = &pi[2];
147 memcpy(p, oldP, n>oldN ? oldN : n);
148 if( n>oldN ){
149 memset(&((char*)p)[oldN], 0, n-oldN);
150 }
151 memset(oldPi, 0, (oldK+3)*sizeof(int));
152 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000153#if MEMORY_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000154 fprintf(stderr,"realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n", oldN, n,
drhdcc581c2000-05-30 13:44:19 +0000155 (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000156#endif
drhdcc581c2000-05-30 13:44:19 +0000157 return p;
158}
drhc3c2fc92000-05-31 22:58:39 +0000159
160/*
161** Make a duplicate of a string into memory obtained from malloc()
162** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000163**
164** This routine is called on all strings that are passed outside of
165** the SQLite library. That way clients can free the string using free()
166** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000167*/
168void sqliteStrRealloc(char **pz){
169 char *zNew;
170 if( pz==0 || *pz==0 ) return;
171 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000172 if( zNew==0 ){
173 sqlite_malloc_failed++;
174 sqliteFree(*pz);
175 *pz = 0;
176 }
177 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000178 sqliteFree(*pz);
179 *pz = zNew;
180}
181
drh6e142f52000-06-08 13:36:40 +0000182/*
183** Make a copy of a string in memory obtained from sqliteMalloc()
184*/
185char *sqliteStrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000186 char *zNew;
187 if( z==0 ) return 0;
188 zNew = sqliteMalloc_(strlen(z)+1, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000189 if( zNew ) strcpy(zNew, z);
190 return zNew;
191}
192char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000193 char *zNew;
194 if( z==0 ) return 0;
195 zNew = sqliteMalloc_(n+1, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000196 if( zNew ){
197 memcpy(zNew, z, n);
198 zNew[n] = 0;
199 }
200 return zNew;
201}
drh7c68d602000-10-11 19:28:51 +0000202#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000203
drh7c68d602000-10-11 19:28:51 +0000204/*
205** The following versions of malloc() and free() are for use in a
206** normal build.
207*/
208#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000209
drh75897232000-05-29 14:26:00 +0000210/*
211** Allocate new memory and set it to zero. Return NULL if
212** no memory is available.
213*/
214void *sqliteMalloc(int n){
215 void *p = malloc(n);
drhdaffd0e2001-04-11 14:28:42 +0000216 if( p==0 ){
217 sqlite_malloc_failed++;
218 return 0;
219 }
drh75897232000-05-29 14:26:00 +0000220 memset(p, 0, n);
221 return p;
222}
223
224/*
225** Free memory previously obtained from sqliteMalloc()
226*/
227void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000228 if( p ){
drh305cea62000-05-29 17:44:25 +0000229 free(p);
230 }
drh75897232000-05-29 14:26:00 +0000231}
232
233/*
234** Resize a prior allocation. If p==0, then this routine
235** works just like sqliteMalloc(). If n==0, then this routine
236** works just like sqliteFree().
237*/
238void *sqliteRealloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000239 void *p2;
drh75897232000-05-29 14:26:00 +0000240 if( p==0 ){
241 return sqliteMalloc(n);
242 }
243 if( n==0 ){
244 sqliteFree(p);
245 return 0;
246 }
drh6d4abfb2001-10-22 02:58:08 +0000247 p2 = realloc(p, n);
248 if( p2==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000249 sqlite_malloc_failed++;
250 }
drh6d4abfb2001-10-22 02:58:08 +0000251 return p2;
drh75897232000-05-29 14:26:00 +0000252}
drh6e142f52000-06-08 13:36:40 +0000253
254/*
255** Make a copy of a string in memory obtained from sqliteMalloc()
256*/
257char *sqliteStrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000258 char *zNew;
259 if( z==0 ) return 0;
260 zNew = sqliteMalloc(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000261 if( zNew ) strcpy(zNew, z);
262 return zNew;
263}
264char *sqliteStrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000265 char *zNew;
266 if( z==0 ) return 0;
267 zNew = sqliteMalloc(n+1);
drh6e142f52000-06-08 13:36:40 +0000268 if( zNew ){
269 memcpy(zNew, z, n);
270 zNew[n] = 0;
271 }
272 return zNew;
273}
drh7c68d602000-10-11 19:28:51 +0000274#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000275
276/*
277** Create a string from the 2nd and subsequent arguments (up to the
278** first NULL argument), store the string in memory obtained from
279** sqliteMalloc() and make the pointer indicated by the 1st argument
280** point to that string.
281*/
282void sqliteSetString(char **pz, const char *zFirst, ...){
283 va_list ap;
284 int nByte;
285 const char *z;
286 char *zResult;
287
288 if( pz==0 ) return;
289 nByte = strlen(zFirst) + 1;
290 va_start(ap, zFirst);
291 while( (z = va_arg(ap, const char*))!=0 ){
292 nByte += strlen(z);
293 }
294 va_end(ap);
295 sqliteFree(*pz);
296 *pz = zResult = sqliteMalloc( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000297 if( zResult==0 ){
298 return;
299 }
drh75897232000-05-29 14:26:00 +0000300 strcpy(zResult, zFirst);
301 zResult += strlen(zResult);
302 va_start(ap, zFirst);
303 while( (z = va_arg(ap, const char*))!=0 ){
304 strcpy(zResult, z);
305 zResult += strlen(zResult);
306 }
307 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000308#ifdef MEMORY_DEBUG
309#if MEMORY_DEBUG>1
310 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
311#endif
312#endif
drh75897232000-05-29 14:26:00 +0000313}
314
315/*
316** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000317** a length integer which specifies how much of the source string
318** to copy (in bytes). -1 means use the whole string.
drh75897232000-05-29 14:26:00 +0000319*/
320void sqliteSetNString(char **pz, ...){
321 va_list ap;
322 int nByte;
323 const char *z;
324 char *zResult;
325 int n;
326
327 if( pz==0 ) return;
328 nByte = 0;
329 va_start(ap, pz);
330 while( (z = va_arg(ap, const char*))!=0 ){
331 n = va_arg(ap, int);
332 if( n<=0 ) n = strlen(z);
333 nByte += n;
334 }
335 va_end(ap);
336 sqliteFree(*pz);
337 *pz = zResult = sqliteMalloc( nByte + 1 );
338 if( zResult==0 ) return;
339 va_start(ap, pz);
340 while( (z = va_arg(ap, const char*))!=0 ){
341 n = va_arg(ap, int);
342 if( n<=0 ) n = strlen(z);
343 strncpy(zResult, z, n);
344 zResult += n;
345 }
346 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000347#ifdef MEMORY_DEBUG
348#if MEMORY_DEBUG>1
349 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
350#endif
351#endif
drh75897232000-05-29 14:26:00 +0000352 va_end(ap);
353}
354
drh982cef72000-05-30 16:27:03 +0000355/*
356** Convert an SQL-style quoted string into a normal string by removing
357** the quote characters. The conversion is done in-place. If the
358** input does not begin with a quote character, then this routine
359** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000360**
361** 2002-Feb-14: This routine is extended to remove MS-Access style
362** brackets from around identifers. For example: "[a-b-c]" becomes
363** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000364*/
365void sqliteDequote(char *z){
366 int quote;
367 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000368 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000369 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000370 switch( quote ){
371 case '\'': break;
372 case '"': break;
373 case '[': quote = ']'; break;
374 default: return;
375 }
drh982cef72000-05-30 16:27:03 +0000376 for(i=1, j=0; z[i]; i++){
377 if( z[i]==quote ){
378 if( z[i+1]==quote ){
379 z[j++] = quote;
380 i++;
381 }else{
382 z[j++] = 0;
383 break;
384 }
385 }else{
386 z[j++] = z[i];
387 }
388 }
389}
390
drh75897232000-05-29 14:26:00 +0000391/* An array to map all upper-case characters into their corresponding
392** lower-case character.
393*/
394static unsigned char UpperToLower[] = {
395 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
396 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
397 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
398 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
399 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
400 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
401 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
402 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
403 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
404 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
405 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
406 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
407 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
408 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
409 252,253,254,255
410};
411
412/*
413** This function computes a hash on the name of a keyword.
414** Case is not significant.
415*/
416int sqliteHashNoCase(const char *z, int n){
417 int h = 0;
drh75897232000-05-29 14:26:00 +0000418 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000419 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000420 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000421 n--;
drh75897232000-05-29 14:26:00 +0000422 }
423 if( h<0 ) h = -h;
424 return h;
425}
426
427/*
drh967e8b72000-06-21 13:59:10 +0000428** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000429** there is no consistency, we will define our own.
430*/
431int sqliteStrICmp(const char *zLeft, const char *zRight){
432 register unsigned char *a, *b;
433 a = (unsigned char *)zLeft;
434 b = (unsigned char *)zRight;
435 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
436 return *a - *b;
437}
438int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
439 register unsigned char *a, *b;
440 a = (unsigned char *)zLeft;
441 b = (unsigned char *)zRight;
442 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000443 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000444}
445
drh7a7c7392001-11-24 00:31:46 +0000446/*
447** The sortStrCmp() function below is used to order elements according
448** to the ORDER BY clause of a SELECT. The sort order is a little different
449** from what one might expect. This note attempts to describe what is
450** going on.
drh75897232000-05-29 14:26:00 +0000451**
452** We want the main string comparision function used for sorting to
453** sort both numbers and alphanumeric words into the correct sequence.
454** The same routine should do both without prior knowledge of which
455** type of text the input represents. It should even work for strings
drh7a7c7392001-11-24 00:31:46 +0000456** which are a mixture of text and numbers. (It does not work for
457** numeric substrings in exponential notation, however.)
drh75897232000-05-29 14:26:00 +0000458**
459** To accomplish this, we keep track of a state number while scanning
460** the two strings. The states are as follows:
461**
462** 1 Beginning of word
463** 2 Arbitrary text
464** 3 Integer
465** 4 Negative integer
466** 5 Real number
467** 6 Negative real
468**
469** The scan begins in state 1, beginning of word. Transitions to other
470** states are determined by characters seen, as shown in the following
471** chart:
472**
473** Current State Character Seen New State
474** -------------------- -------------- -------------------
475** 0 Beginning of word "-" 3 Negative integer
476** digit 2 Integer
477** space 0 Beginning of word
478** otherwise 1 Arbitrary text
479**
480** 1 Arbitrary text space 0 Beginning of word
481** digit 2 Integer
482** otherwise 1 Arbitrary text
483**
484** 2 Integer space 0 Beginning of word
485** "." 4 Real number
486** digit 2 Integer
487** otherwise 1 Arbitrary text
488**
489** 3 Negative integer space 0 Beginning of word
490** "." 5 Negative Real num
491** digit 3 Negative integer
492** otherwise 1 Arbitrary text
493**
494** 4 Real number space 0 Beginning of word
495** digit 4 Real number
496** otherwise 1 Arbitrary text
497**
498** 5 Negative real num space 0 Beginning of word
499** digit 5 Negative real num
500** otherwise 1 Arbitrary text
501**
502** To implement this state machine, we first classify each character
503** into on of the following categories:
504**
505** 0 Text
506** 1 Space
507** 2 Digit
508** 3 "-"
509** 4 "."
510**
511** Given an arbitrary character, the array charClass[] maps that character
512** into one of the atove categories.
513*/
514static const unsigned char charClass[] = {
515 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
516/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
517/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
518/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
519/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
520/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
521/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
522/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
523/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
524/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
525/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
526/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
527/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
528/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
529/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
530/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
531/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
532};
533#define N_CHAR_CLASS 5
534
535/*
536** Given the current state number (0 thru 5), this array figures
537** the new state number given the character class.
538*/
539static const unsigned char stateMachine[] = {
540 /* Text, Space, Digit, "-", "." */
541 1, 0, 2, 3, 1, /* State 0: Beginning of word */
542 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
543 1, 0, 2, 1, 4, /* State 2: Integer */
544 1, 0, 3, 1, 5, /* State 3: Negative integer */
545 1, 0, 4, 1, 1, /* State 4: Real number */
546 1, 0, 5, 1, 1, /* State 5: Negative real num */
547};
548
549/* This routine does a comparison of two strings. Case is used only
drh7a7c7392001-11-24 00:31:46 +0000550** if useCase!=0. Numeric substrings compare in numerical order for the
551** most part but this routine does not understand exponential notation.
drh75897232000-05-29 14:26:00 +0000552*/
drh7a7c7392001-11-24 00:31:46 +0000553static int sortStrCmp(const char *atext, const char *btext, int useCase){
drh75897232000-05-29 14:26:00 +0000554 register unsigned char *a, *b, *map, ca, cb;
555 int result;
556 register int cclass = 0;
557
558 a = (unsigned char *)atext;
559 b = (unsigned char *)btext;
560 if( useCase ){
561 do{
562 if( (ca= *a++)!=(cb= *b++) ) break;
563 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
564 }while( ca!=0 );
565 }else{
566 map = UpperToLower;
567 do{
568 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
569 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
570 }while( ca!=0 );
drh92086432002-01-22 14:11:29 +0000571 if( ca>='[' && ca<='`' ) cb = b[-1];
572 if( cb>='[' && cb<='`' ) ca = a[-1];
drh75897232000-05-29 14:26:00 +0000573 }
574 switch( cclass ){
575 case 0:
576 case 1: {
577 if( isdigit(ca) && isdigit(cb) ){
578 cclass = 2;
579 }
580 break;
581 }
582 default: {
583 break;
584 }
585 }
586 switch( cclass ){
587 case 2:
588 case 3: {
589 if( isdigit(ca) ){
590 if( isdigit(cb) ){
591 int acnt, bcnt;
592 acnt = bcnt = 0;
593 while( isdigit(*a++) ) acnt++;
594 while( isdigit(*b++) ) bcnt++;
595 result = acnt - bcnt;
596 if( result==0 ) result = ca-cb;
597 }else{
598 result = 1;
599 }
600 }else if( isdigit(cb) ){
601 result = -1;
602 }else if( ca=='.' ){
603 result = 1;
604 }else if( cb=='.' ){
605 result = -1;
606 }else{
607 result = ca - cb;
608 cclass = 2;
609 }
610 if( cclass==3 ) result = -result;
611 break;
612 }
613 case 0:
614 case 1:
615 case 4: {
616 result = ca - cb;
617 break;
618 }
619 case 5: {
620 result = cb - ca;
621 };
622 }
623 return result;
624}
625
drha5c2ad02000-09-14 01:21:10 +0000626/*
drh7a7c7392001-11-24 00:31:46 +0000627** Return TRUE if z is a pure numeric string. Return FALSE if the
628** string contains any character which is not part of a number.
629**
630** Am empty string is considered numeric.
drha5c2ad02000-09-14 01:21:10 +0000631*/
drhe6840902002-03-06 03:08:25 +0000632static int sqliteIsNumber(const char *z){
drh7a7c7392001-11-24 00:31:46 +0000633 if( *z=='-' || *z=='+' ) z++;
634 if( !isdigit(*z) ){
635 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000636 }
drh7a7c7392001-11-24 00:31:46 +0000637 z++;
638 while( isdigit(*z) ){ z++; }
639 if( *z=='.' ){
640 z++;
641 if( !isdigit(*z) ) return 0;
642 while( isdigit(*z) ){ z++; }
643 if( *z=='e' || *z=='E' ){
644 z++;
645 if( *z=='+' || *z=='-' ) z++;
646 if( !isdigit(*z) ) return 0;
647 while( isdigit(*z) ){ z++; }
648 }
drha5c2ad02000-09-14 01:21:10 +0000649 }
drh7a7c7392001-11-24 00:31:46 +0000650 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000651}
652
drh75897232000-05-29 14:26:00 +0000653/* This comparison routine is what we use for comparison operations
drh7a7c7392001-11-24 00:31:46 +0000654** in an SQL expression. (Ex: name<'Hello' or value<5).
655**
656** Numerical strings compare in numerical order. Numerical strings
657** are always less than non-numeric strings. Non-numeric strings
658** compare in lexigraphical order (the same order as strcmp()).
659**
660** This is NOT the comparison function used for sorting. The sort
661** order is a little bit different. See sqliteSortCompare below
662** for additional information.
drh75897232000-05-29 14:26:00 +0000663*/
664int sqliteCompare(const char *atext, const char *btext){
665 int result;
drh0bce8352002-02-28 00:41:10 +0000666 int isNumA, isNumB;
667 if( atext==0 ){
668 return -(btext!=0);
669 }else if( btext==0 ){
670 return 1;
671 }
drhe6840902002-03-06 03:08:25 +0000672 isNumA = sqliteIsNumber(atext);
673 isNumB = sqliteIsNumber(btext);
drh7a7c7392001-11-24 00:31:46 +0000674 if( isNumA ){
675 if( !isNumB ){
676 result = -1;
677 }else{
678 double rA, rB;
679 rA = atof(atext);
680 rB = atof(btext);
681 if( rA<rB ){
682 result = -1;
683 }else if( rA>rB ){
684 result = +1;
685 }else{
686 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000687 }
688 }
drh7a7c7392001-11-24 00:31:46 +0000689 }else if( isNumB ){
690 result = +1;
691 }else {
692 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000693 }
drh7a7c7392001-11-24 00:31:46 +0000694 return result;
drh75897232000-05-29 14:26:00 +0000695}
drh75897232000-05-29 14:26:00 +0000696
697/*
drh16e59552000-07-31 11:57:37 +0000698** This routine is used for sorting. Each key is a list of one or more
699** null-terminated strings. The list is terminated by two nulls in
700** a row. For example, the following text is key with three strings:
drh75897232000-05-29 14:26:00 +0000701**
702** +one\000-two\000+three\000\000
703**
704** Both arguments will have the same number of strings. This routine
705** returns negative, zero, or positive if the first argument is less
706** than, equal to, or greater than the first. (Result is a-b).
707**
708** Every string begins with either a "+" or "-" character. If the
709** character is "-" then the return value is negated. This is done
710** to implement a sort in descending order.
drh7a7c7392001-11-24 00:31:46 +0000711**
712** For sorting purposes, pur numeric strings (strings for which the
713** isNum() function above returns TRUE) always compare less than strings
714** that are not pure numerics. Within non-numeric strings, substrings
715** of digits compare in numerical order. Finally, case is used only
716** to break a tie.
717**
718** Note that the sort order imposed by the rules above is different
719** from the ordering defined by the "<", "<=", ">", and ">=" operators
720** of expressions. The operators compare non-numeric strings in
721** lexigraphical order. This routine does the additional processing
722** to sort substrings of digits into numerical order and to use case
723** only as a tie-breaker.
drh75897232000-05-29 14:26:00 +0000724*/
725int sqliteSortCompare(const char *a, const char *b){
726 int len;
727 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000728 int isNumA, isNumB;
drh75897232000-05-29 14:26:00 +0000729
730 while( res==0 && *a && *b ){
drhe6840902002-03-06 03:08:25 +0000731 isNumA = sqliteIsNumber(&a[1]);
732 isNumB = sqliteIsNumber(&b[1]);
drh7a7c7392001-11-24 00:31:46 +0000733 if( isNumA ){
734 double rA, rB;
735 if( !isNumB ){
736 res = -1;
737 break;
738 }
739 rA = atof(&a[1]);
740 rB = atof(&b[1]);
741 if( rA<rB ){
742 res = -1;
743 break;
744 }
745 if( rA>rB ){
746 res = +1;
747 break;
748 }
749 }else if( isNumB ){
750 res = +1;
751 break;
752 }else{
753 res = sortStrCmp(&a[1],&b[1],0);
754 if( res==0 ){
755 res = sortStrCmp(&a[1],&b[1],1);
756 }
757 if( res!=0 ){
758 break;
759 }
drh75897232000-05-29 14:26:00 +0000760 }
drh7a7c7392001-11-24 00:31:46 +0000761 len = strlen(&a[1]) + 2;
762 a += len;
763 b += len;
drh75897232000-05-29 14:26:00 +0000764 }
765 if( *a=='-' ) res = -res;
766 return res;
767}
drhdce2cbe2000-05-31 02:27:49 +0000768
drh9bbca4c2001-11-06 04:00:18 +0000769/*
drh7a7c7392001-11-24 00:31:46 +0000770** Some powers of 64. These constants are needed in the
771** sqliteRealToSortable() routine below.
drh9bbca4c2001-11-06 04:00:18 +0000772*/
773#define _64e3 (64.0 * 64.0 * 64.0)
774#define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
775#define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
776#define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
777#define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
778#define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
779
780/*
781** The following procedure converts a double-precision floating point
782** number into a string. The resulting string has the property that
783** two such strings comparied using strcmp() or memcmp() will give the
drh5a2c2c22001-11-21 02:21:11 +0000784** same results as a numeric comparison of the original floating point
785** numbers.
drh9bbca4c2001-11-06 04:00:18 +0000786**
787** This routine is used to generate database keys from floating point
788** numbers such that the keys sort in the same order as the original
789** floating point numbers even though the keys are compared using
790** memcmp().
791**
792** The calling function should have allocated at least 14 characters
793** of space for the buffer z[].
794*/
795void sqliteRealToSortable(double r, char *z){
796 int neg;
797 int exp;
798 int cnt = 0;
799
800 /* This array maps integers between 0 and 63 into base-64 digits.
801 ** The digits must be chosen such at their ASCII codes are increasing.
802 ** This means we can not use the traditional base-64 digit set. */
803 static const char zDigit[] =
804 "0123456789"
805 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
806 "abcdefghijklmnopqrstuvwxyz"
807 "|~";
808 if( r<0.0 ){
809 neg = 1;
810 r = -r;
811 *z++ = '-';
812 } else {
813 neg = 0;
814 *z++ = '0';
815 }
816 exp = 0;
817
818 if( r==0.0 ){
819 exp = -1024;
820 }else if( r<(0.5/64.0) ){
821 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
822 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
823 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
824 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
825 }else if( r>=0.5 ){
826 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
827 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
828 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
829 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
830 }
831 if( neg ){
832 exp = -exp;
833 r = -r;
834 }
835 exp += 1024;
836 r += 0.5;
837 if( exp<0 ) return;
838 if( exp>=2048 || r>=1.0 ){
839 strcpy(z, "~~~~~~~~~~~~");
840 return;
841 }
842 *z++ = zDigit[(exp>>6)&0x3f];
843 *z++ = zDigit[exp & 0x3f];
844 while( r>0.0 && cnt<10 ){
845 int digit;
846 r *= 64.0;
drh1ab43002002-01-14 09:28:19 +0000847 digit = (int)r;
drh9bbca4c2001-11-06 04:00:18 +0000848 assert( digit>=0 && digit<64 );
849 *z++ = zDigit[digit & 0x3f];
850 r -= digit;
851 cnt++;
852 }
853 *z = 0;
854}
855
drh297ecf12001-04-05 15:57:13 +0000856#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000857/*
drh297ecf12001-04-05 15:57:13 +0000858** X is a pointer to the first byte of a UTF-8 character. Increment
859** X so that it points to the next character. This only works right
860** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000861*/
drh297ecf12001-04-05 15:57:13 +0000862#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
863#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000864
drh297ecf12001-04-05 15:57:13 +0000865#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000866/*
drh297ecf12001-04-05 15:57:13 +0000867** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000868*/
drh297ecf12001-04-05 15:57:13 +0000869#define sqliteNextChar(X) (++(X));
870#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000871
drh297ecf12001-04-05 15:57:13 +0000872#endif /* defined(SQLITE_UTF8) */
873
874
875#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000876/*
drh297ecf12001-04-05 15:57:13 +0000877** Convert the UTF-8 character to which z points into a 31-bit
878** UCS character. This only works right if z points to a well-formed
879** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000880*/
drh297ecf12001-04-05 15:57:13 +0000881static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000882 int c;
drh297ecf12001-04-05 15:57:13 +0000883 static const int initVal[] = {
884 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
885 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
886 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
887 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
888 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
889 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
890 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
891 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
892 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
893 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
894 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
895 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
896 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
897 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
898 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
899 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
900 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
901 255,
902 };
903 c = initVal[*(z++)];
904 while( (0xc0&*z)==0x80 ){
905 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000906 }
907 return c;
908}
drh297ecf12001-04-05 15:57:13 +0000909#endif
drhe17a7e32001-04-04 21:10:18 +0000910
911/*
912** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000913** potentially be a "glob" expression. Return true (1) if they
914** are the same and false (0) if they are different.
915**
916** Globbing rules:
917**
918** '*' Matches any sequence of zero or more characters.
919**
920** '?' Matches exactly one character.
921**
922** [...] Matches one character from the enclosed list of
923** characters.
924**
925** [^...] Matches one character not in the enclosed list.
926**
927** With the [...] and [^...] matching, a ']' character can be included
928** in the list by making it the first character after '[' or '^'. A
929** range of characters can be specified using '-'. Example:
930** "[a-z]" matches any single lower-case letter. To match a '-', make
931** it the last character in the list.
932**
933** This routine is usually quick, but can be N**2 in the worst case.
934**
935** Hints: to match '*' or '?', put them in "[]". Like this:
936**
937** abc[*]xyz Matches "abc*xyz" only
938*/
drhe17a7e32001-04-04 21:10:18 +0000939int
940sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
941 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000942 int invert;
943 int seen;
drhe17a7e32001-04-04 21:10:18 +0000944 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000945
946 while( (c = *zPattern)!=0 ){
947 switch( c ){
948 case '*':
drhe17a7e32001-04-04 21:10:18 +0000949 while( (c=zPattern[1]) == '*' || c == '?' ){
950 if( c=='?' ){
951 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000952 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000953 }
954 zPattern++;
955 }
956 if( c==0 ) return 1;
957 c = UpperToLower[c];
958 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +0000959 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000960 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000961 }
962 return *zString!=0;
963 }else{
964 while( (c2 = *zString)!=0 ){
965 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000966 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000967 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000968 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000969 }
970 return 0;
971 }
drhe17a7e32001-04-04 21:10:18 +0000972 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000973 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000974 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000975 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000976 break;
drhe17a7e32001-04-04 21:10:18 +0000977 }
978 case '[': {
979 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000980 seen = 0;
981 invert = 0;
drh297ecf12001-04-05 15:57:13 +0000982 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +0000983 if( c==0 ) return 0;
984 c2 = *++zPattern;
985 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
986 if( c2==']' ){
987 if( c==']' ) seen = 1;
988 c2 = *++zPattern;
989 }
drh297ecf12001-04-05 15:57:13 +0000990 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +0000991 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
992 zPattern++;
drh297ecf12001-04-05 15:57:13 +0000993 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +0000994 if( c>=prior_c && c<=c2 ) seen = 1;
995 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000996 }else if( c==c2 ){
997 seen = 1;
drhe17a7e32001-04-04 21:10:18 +0000998 prior_c = c2;
999 }else{
1000 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001001 }
drh297ecf12001-04-05 15:57:13 +00001002 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001003 }
1004 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001005 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001006 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001007 break;
drhe17a7e32001-04-04 21:10:18 +00001008 }
1009 default: {
drhdce2cbe2000-05-31 02:27:49 +00001010 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001011 zPattern++;
1012 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001013 break;
drhe17a7e32001-04-04 21:10:18 +00001014 }
drhdce2cbe2000-05-31 02:27:49 +00001015 }
drhdce2cbe2000-05-31 02:27:49 +00001016 }
1017 return *zString==0;
1018}
1019
1020/*
drhe17a7e32001-04-04 21:10:18 +00001021** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001022** SQL. The '%' character matches any sequence of 0 or more
1023** characters and '_' matches any single character. Case is
1024** not significant.
1025**
1026** This routine is just an adaptation of the sqliteGlobCompare()
1027** routine above.
1028*/
1029int
1030sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001031 register int c;
1032 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001033
1034 while( (c = UpperToLower[*zPattern])!=0 ){
1035 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001036 case '%': {
1037 while( (c=zPattern[1]) == '%' || c == '_' ){
1038 if( c=='_' ){
1039 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001040 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001041 }
drhe17a7e32001-04-04 21:10:18 +00001042 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001043 }
drhe17a7e32001-04-04 21:10:18 +00001044 if( c==0 ) return 1;
1045 c = UpperToLower[c];
1046 while( (c2=UpperToLower[*zString])!=0 ){
1047 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1048 if( c2==0 ) return 0;
1049 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001050 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001051 }
1052 return 0;
1053 }
1054 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001055 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001056 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001057 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001058 break;
drhe17a7e32001-04-04 21:10:18 +00001059 }
1060 default: {
drhdce2cbe2000-05-31 02:27:49 +00001061 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001062 zPattern++;
1063 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001064 break;
drhe17a7e32001-04-04 21:10:18 +00001065 }
drhdce2cbe2000-05-31 02:27:49 +00001066 }
drhdce2cbe2000-05-31 02:27:49 +00001067 }
1068 return *zString==0;
1069}
drh5e00f6c2001-09-13 13:46:56 +00001070
1071/*
drhc22bd472002-05-10 13:14:07 +00001072** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1073** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1074** when this routine is called.
1075**
1076** This routine is a attempt to detect if two threads use the
1077** same sqlite* pointer at the same time. There is a race
1078** condition so it is possible that the error is not detected.
1079** But usually the problem will be seen. The result will be an
1080** error which can be used to debugging the application that is
1081** using SQLite incorrectly.
drh5e00f6c2001-09-13 13:46:56 +00001082*/
drhc22bd472002-05-10 13:14:07 +00001083int sqliteSafetyOn(sqlite *db){
1084 if( db->magic==SQLITE_MAGIC_OPEN ){
1085 db->magic = SQLITE_MAGIC_BUSY;
1086 return 0;
1087 }else{
1088 db->magic = SQLITE_MAGIC_ERROR;
1089 db->flags |= SQLITE_Interrupt;
1090 return 1;
drh5e00f6c2001-09-13 13:46:56 +00001091 }
drhc22bd472002-05-10 13:14:07 +00001092}
1093
1094/*
1095** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1096** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1097** when this routine is called.
1098*/
1099int sqliteSafetyOff(sqlite *db){
1100 if( db->magic==SQLITE_MAGIC_BUSY ){
1101 db->magic = SQLITE_MAGIC_OPEN;
1102 return 0;
1103 }else{
1104 db->magic = SQLITE_MAGIC_ERROR;
1105 db->flags |= SQLITE_Interrupt;
1106 return 1;
1107 }
1108}
1109
1110/*
1111** Check to make sure we are not currently executing an sqlite_exec().
1112** If we are currently in an sqlite_exec(), return true and set
1113** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1114** shutdown of the database.
1115**
1116** This routine is used to try to detect when API routines are called
1117** at the wrong time or in the wrong sequence.
1118*/
1119int sqliteSafetyCheck(sqlite *db){
1120 if( db->recursionDepth ){
1121 db->magic = SQLITE_MAGIC_ERROR;
1122 return 1;
1123 }
1124 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001125}