blob: 21736e9ea348e3ee1801a0d420a990e9ea107c76 [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**
drh38640e12002-07-05 21:42:36 +000017** $Id: util.c,v 1.47 2002/07/05 21:42:37 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 ){
drh8912d102002-05-26 21:34:58 +0000668 return -1;
drh0bce8352002-02-28 00:41:10 +0000669 }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**
drh38640e12002-07-05 21:42:36 +0000708** Each string begins with one of the characters "+", "-", "A", "D".
709** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000710**
drh38640e12002-07-05 21:42:36 +0000711** + Sort numerically in ascending order
712** - Sort numerically in descending order
713** A Sort as strings in ascending order
714** D Sort as strings in descending order.
715**
716** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000717** isNum() function above returns TRUE) always compare less than strings
718** that are not pure numerics. Within non-numeric strings, substrings
719** of digits compare in numerical order. Finally, case is used only
720** to break a tie.
721**
722** Note that the sort order imposed by the rules above is different
723** from the ordering defined by the "<", "<=", ">", and ">=" operators
724** of expressions. The operators compare non-numeric strings in
725** lexigraphical order. This routine does the additional processing
726** to sort substrings of digits into numerical order and to use case
727** only as a tie-breaker.
drh38640e12002-07-05 21:42:36 +0000728**
729** The special rules above apply only to numeric sorting, when the
730** prefix is "+" or "-". If the prefix is "A" or "D" then plain old
731** "strcmp()" is used for the comparison.
drh75897232000-05-29 14:26:00 +0000732*/
733int sqliteSortCompare(const char *a, const char *b){
734 int len;
735 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000736 int isNumA, isNumB;
drh75897232000-05-29 14:26:00 +0000737
738 while( res==0 && *a && *b ){
drh38640e12002-07-05 21:42:36 +0000739 assert( a[0]==b[0] );
drhf570f012002-05-31 15:51:25 +0000740 if( a[1]==0 ){
741 res = -1;
742 break;
743 }else if( b[1]==0 ){
744 res = +1;
745 break;
746 }
drh38640e12002-07-05 21:42:36 +0000747 if( a[0]=='A' || a[0]=='D' ){
748 res = strcmp(&a[1],&b[1]);
749 if( res ) break;
750 }else{
751 isNumA = sqliteIsNumber(&a[1]);
752 isNumB = sqliteIsNumber(&b[1]);
753 if( isNumA ){
754 double rA, rB;
755 if( !isNumB ){
756 res = -1;
757 break;
758 }
759 rA = atof(&a[1]);
760 rB = atof(&b[1]);
761 if( rA<rB ){
762 res = -1;
763 break;
764 }
765 if( rA>rB ){
766 res = +1;
767 break;
768 }
769 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000770 res = +1;
771 break;
drh38640e12002-07-05 21:42:36 +0000772 }else{
773 res = sortStrCmp(&a[1],&b[1],0);
774 if( res==0 ){
775 res = sortStrCmp(&a[1],&b[1],1);
776 }
777 if( res!=0 ){
778 break;
779 }
drh7a7c7392001-11-24 00:31:46 +0000780 }
drh75897232000-05-29 14:26:00 +0000781 }
drh7a7c7392001-11-24 00:31:46 +0000782 len = strlen(&a[1]) + 2;
783 a += len;
784 b += len;
drh75897232000-05-29 14:26:00 +0000785 }
drh38640e12002-07-05 21:42:36 +0000786 if( *a=='-' || *a=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000787 return res;
788}
drhdce2cbe2000-05-31 02:27:49 +0000789
drh9bbca4c2001-11-06 04:00:18 +0000790/*
drh7a7c7392001-11-24 00:31:46 +0000791** Some powers of 64. These constants are needed in the
792** sqliteRealToSortable() routine below.
drh9bbca4c2001-11-06 04:00:18 +0000793*/
794#define _64e3 (64.0 * 64.0 * 64.0)
795#define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
796#define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
797#define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
798#define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
799#define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
800
801/*
802** The following procedure converts a double-precision floating point
803** number into a string. The resulting string has the property that
804** two such strings comparied using strcmp() or memcmp() will give the
drh5a2c2c22001-11-21 02:21:11 +0000805** same results as a numeric comparison of the original floating point
806** numbers.
drh9bbca4c2001-11-06 04:00:18 +0000807**
808** This routine is used to generate database keys from floating point
809** numbers such that the keys sort in the same order as the original
810** floating point numbers even though the keys are compared using
811** memcmp().
812**
813** The calling function should have allocated at least 14 characters
814** of space for the buffer z[].
815*/
816void sqliteRealToSortable(double r, char *z){
817 int neg;
818 int exp;
819 int cnt = 0;
820
821 /* This array maps integers between 0 and 63 into base-64 digits.
822 ** The digits must be chosen such at their ASCII codes are increasing.
823 ** This means we can not use the traditional base-64 digit set. */
824 static const char zDigit[] =
825 "0123456789"
826 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
827 "abcdefghijklmnopqrstuvwxyz"
828 "|~";
829 if( r<0.0 ){
830 neg = 1;
831 r = -r;
832 *z++ = '-';
833 } else {
834 neg = 0;
835 *z++ = '0';
836 }
837 exp = 0;
838
839 if( r==0.0 ){
840 exp = -1024;
841 }else if( r<(0.5/64.0) ){
842 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
843 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
844 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
845 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
846 }else if( r>=0.5 ){
847 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
848 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
849 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
850 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
851 }
852 if( neg ){
853 exp = -exp;
854 r = -r;
855 }
856 exp += 1024;
857 r += 0.5;
858 if( exp<0 ) return;
859 if( exp>=2048 || r>=1.0 ){
860 strcpy(z, "~~~~~~~~~~~~");
861 return;
862 }
863 *z++ = zDigit[(exp>>6)&0x3f];
864 *z++ = zDigit[exp & 0x3f];
865 while( r>0.0 && cnt<10 ){
866 int digit;
867 r *= 64.0;
drh1ab43002002-01-14 09:28:19 +0000868 digit = (int)r;
drh9bbca4c2001-11-06 04:00:18 +0000869 assert( digit>=0 && digit<64 );
870 *z++ = zDigit[digit & 0x3f];
871 r -= digit;
872 cnt++;
873 }
874 *z = 0;
875}
876
drh297ecf12001-04-05 15:57:13 +0000877#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000878/*
drh297ecf12001-04-05 15:57:13 +0000879** X is a pointer to the first byte of a UTF-8 character. Increment
880** X so that it points to the next character. This only works right
881** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000882*/
drh297ecf12001-04-05 15:57:13 +0000883#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
884#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000885
drh297ecf12001-04-05 15:57:13 +0000886#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000887/*
drh297ecf12001-04-05 15:57:13 +0000888** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000889*/
drh297ecf12001-04-05 15:57:13 +0000890#define sqliteNextChar(X) (++(X));
891#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000892
drh297ecf12001-04-05 15:57:13 +0000893#endif /* defined(SQLITE_UTF8) */
894
895
896#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000897/*
drh297ecf12001-04-05 15:57:13 +0000898** Convert the UTF-8 character to which z points into a 31-bit
899** UCS character. This only works right if z points to a well-formed
900** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000901*/
drh297ecf12001-04-05 15:57:13 +0000902static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000903 int c;
drh297ecf12001-04-05 15:57:13 +0000904 static const int initVal[] = {
905 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
906 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
907 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
908 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
909 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
910 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
911 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
912 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
913 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
914 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
915 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
916 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
917 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
918 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
919 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
920 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
921 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
922 255,
923 };
924 c = initVal[*(z++)];
925 while( (0xc0&*z)==0x80 ){
926 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000927 }
928 return c;
929}
drh297ecf12001-04-05 15:57:13 +0000930#endif
drhe17a7e32001-04-04 21:10:18 +0000931
932/*
933** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000934** potentially be a "glob" expression. Return true (1) if they
935** are the same and false (0) if they are different.
936**
937** Globbing rules:
938**
939** '*' Matches any sequence of zero or more characters.
940**
941** '?' Matches exactly one character.
942**
943** [...] Matches one character from the enclosed list of
944** characters.
945**
946** [^...] Matches one character not in the enclosed list.
947**
948** With the [...] and [^...] matching, a ']' character can be included
949** in the list by making it the first character after '[' or '^'. A
950** range of characters can be specified using '-'. Example:
951** "[a-z]" matches any single lower-case letter. To match a '-', make
952** it the last character in the list.
953**
954** This routine is usually quick, but can be N**2 in the worst case.
955**
956** Hints: to match '*' or '?', put them in "[]". Like this:
957**
958** abc[*]xyz Matches "abc*xyz" only
959*/
drhe17a7e32001-04-04 21:10:18 +0000960int
961sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
962 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000963 int invert;
964 int seen;
drhe17a7e32001-04-04 21:10:18 +0000965 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000966
967 while( (c = *zPattern)!=0 ){
968 switch( c ){
969 case '*':
drhe17a7e32001-04-04 21:10:18 +0000970 while( (c=zPattern[1]) == '*' || c == '?' ){
971 if( c=='?' ){
972 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000973 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000974 }
975 zPattern++;
976 }
977 if( c==0 ) return 1;
978 c = UpperToLower[c];
979 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +0000980 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000981 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000982 }
983 return *zString!=0;
984 }else{
985 while( (c2 = *zString)!=0 ){
986 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000987 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000988 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000989 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000990 }
991 return 0;
992 }
drhe17a7e32001-04-04 21:10:18 +0000993 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000994 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000995 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000996 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000997 break;
drhe17a7e32001-04-04 21:10:18 +0000998 }
999 case '[': {
1000 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001001 seen = 0;
1002 invert = 0;
drh297ecf12001-04-05 15:57:13 +00001003 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +00001004 if( c==0 ) return 0;
1005 c2 = *++zPattern;
1006 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1007 if( c2==']' ){
1008 if( c==']' ) seen = 1;
1009 c2 = *++zPattern;
1010 }
drh297ecf12001-04-05 15:57:13 +00001011 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +00001012 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1013 zPattern++;
drh297ecf12001-04-05 15:57:13 +00001014 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +00001015 if( c>=prior_c && c<=c2 ) seen = 1;
1016 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001017 }else if( c==c2 ){
1018 seen = 1;
drhe17a7e32001-04-04 21:10:18 +00001019 prior_c = c2;
1020 }else{
1021 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001022 }
drh297ecf12001-04-05 15:57:13 +00001023 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001024 }
1025 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001026 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001027 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001028 break;
drhe17a7e32001-04-04 21:10:18 +00001029 }
1030 default: {
drhdce2cbe2000-05-31 02:27:49 +00001031 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001032 zPattern++;
1033 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001034 break;
drhe17a7e32001-04-04 21:10:18 +00001035 }
drhdce2cbe2000-05-31 02:27:49 +00001036 }
drhdce2cbe2000-05-31 02:27:49 +00001037 }
1038 return *zString==0;
1039}
1040
1041/*
drhe17a7e32001-04-04 21:10:18 +00001042** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001043** SQL. The '%' character matches any sequence of 0 or more
1044** characters and '_' matches any single character. Case is
1045** not significant.
1046**
1047** This routine is just an adaptation of the sqliteGlobCompare()
1048** routine above.
1049*/
1050int
1051sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001052 register int c;
1053 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001054
1055 while( (c = UpperToLower[*zPattern])!=0 ){
1056 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001057 case '%': {
1058 while( (c=zPattern[1]) == '%' || c == '_' ){
1059 if( c=='_' ){
1060 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001061 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001062 }
drhe17a7e32001-04-04 21:10:18 +00001063 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001064 }
drhe17a7e32001-04-04 21:10:18 +00001065 if( c==0 ) return 1;
1066 c = UpperToLower[c];
1067 while( (c2=UpperToLower[*zString])!=0 ){
1068 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1069 if( c2==0 ) return 0;
1070 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001071 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001072 }
1073 return 0;
1074 }
1075 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001076 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001077 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001078 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001079 break;
drhe17a7e32001-04-04 21:10:18 +00001080 }
1081 default: {
drhdce2cbe2000-05-31 02:27:49 +00001082 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001083 zPattern++;
1084 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001085 break;
drhe17a7e32001-04-04 21:10:18 +00001086 }
drhdce2cbe2000-05-31 02:27:49 +00001087 }
drhdce2cbe2000-05-31 02:27:49 +00001088 }
1089 return *zString==0;
1090}
drh5e00f6c2001-09-13 13:46:56 +00001091
1092/*
drhc22bd472002-05-10 13:14:07 +00001093** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1094** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1095** when this routine is called.
1096**
1097** This routine is a attempt to detect if two threads use the
1098** same sqlite* pointer at the same time. There is a race
1099** condition so it is possible that the error is not detected.
1100** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001101** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001102** using SQLite incorrectly.
drh5e00f6c2001-09-13 13:46:56 +00001103*/
drhc22bd472002-05-10 13:14:07 +00001104int sqliteSafetyOn(sqlite *db){
1105 if( db->magic==SQLITE_MAGIC_OPEN ){
1106 db->magic = SQLITE_MAGIC_BUSY;
1107 return 0;
1108 }else{
1109 db->magic = SQLITE_MAGIC_ERROR;
1110 db->flags |= SQLITE_Interrupt;
1111 return 1;
drh5e00f6c2001-09-13 13:46:56 +00001112 }
drhc22bd472002-05-10 13:14:07 +00001113}
1114
1115/*
1116** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1117** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1118** when this routine is called.
1119*/
1120int sqliteSafetyOff(sqlite *db){
1121 if( db->magic==SQLITE_MAGIC_BUSY ){
1122 db->magic = SQLITE_MAGIC_OPEN;
1123 return 0;
1124 }else{
1125 db->magic = SQLITE_MAGIC_ERROR;
1126 db->flags |= SQLITE_Interrupt;
1127 return 1;
1128 }
1129}
1130
1131/*
1132** Check to make sure we are not currently executing an sqlite_exec().
1133** If we are currently in an sqlite_exec(), return true and set
1134** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1135** shutdown of the database.
1136**
1137** This routine is used to try to detect when API routines are called
1138** at the wrong time or in the wrong sequence.
1139*/
1140int sqliteSafetyCheck(sqlite *db){
1141 if( db->recursionDepth ){
1142 db->magic = SQLITE_MAGIC_ERROR;
1143 return 1;
1144 }
1145 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001146}