blob: e23561ee283ea74ba074599962b469727df26166 [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**
drhb19a2bc2001-09-16 00:13:26 +000017** $Id: util.c,v 1.25 2001/09/16 00:13:27 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 sqlite_nMalloc++;
53 if( sqlite_iMallocFail>=0 ){
54 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000055 if( sqlite_iMallocFail==0 ){
56 sqlite_malloc_failed++;
57 return 0;
58 }
drh6e142f52000-06-08 13:36:40 +000059 }
drhb0729502001-03-14 12:35:57 +000060 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000061 k = (n+sizeof(int)-1)/sizeof(int);
62 pi = malloc( (3+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000063 if( pi==0 ){
64 sqlite_malloc_failed++;
65 return 0;
66 }
drhdcc581c2000-05-30 13:44:19 +000067 pi[0] = 0xdead1122;
68 pi[1] = n;
69 pi[k+2] = 0xdead3344;
70 p = &pi[2];
71 memset(p, 0, n);
drhc3c2fc92000-05-31 22:58:39 +000072#if MEMORY_DEBUG>1
73 fprintf(stderr,"malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
74#endif
drhdcc581c2000-05-30 13:44:19 +000075 return p;
76}
77
78/*
79** Free memory previously obtained from sqliteMalloc()
80*/
81void sqliteFree_(void *p, char *zFile, int line){
82 if( p ){
83 int *pi, k, n;
84 pi = p;
85 pi -= 2;
drh6e142f52000-06-08 13:36:40 +000086 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +000087 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +000088 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +000089 return;
90 }
91 n = pi[1];
92 k = (n+sizeof(int)-1)/sizeof(int);
93 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +000094 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +000095 return;
96 }
drhc3c2fc92000-05-31 22:58:39 +000097 memset(pi, 0xff, (k+3)*sizeof(int));
98#if MEMORY_DEBUG>1
99 fprintf(stderr,"free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
100#endif
drhdcc581c2000-05-30 13:44:19 +0000101 free(pi);
102 }
103}
104
105/*
106** Resize a prior allocation. If p==0, then this routine
107** works just like sqliteMalloc(). If n==0, then this routine
108** works just like sqliteFree().
109*/
110void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
111 int *oldPi, *pi, k, oldN, oldK;
112 void *p;
113 if( oldP==0 ){
114 return sqliteMalloc_(n,zFile,line);
115 }
116 if( n==0 ){
117 sqliteFree_(oldP,zFile,line);
118 return 0;
119 }
120 oldPi = oldP;
121 oldPi -= 2;
122 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000123 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000124 return 0;
drhdcc581c2000-05-30 13:44:19 +0000125 }
126 oldN = oldPi[1];
127 oldK = (oldN+sizeof(int)-1)/sizeof(int);
128 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000129 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000130 return 0;
drhdcc581c2000-05-30 13:44:19 +0000131 }
132 k = (n + sizeof(int) - 1)/sizeof(int);
133 pi = malloc( (k+3)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000134 if( pi==0 ){
135 sqlite_malloc_failed++;
136 return 0;
137 }
drhdcc581c2000-05-30 13:44:19 +0000138 pi[0] = 0xdead1122;
139 pi[1] = n;
140 pi[k+2] = 0xdead3344;
141 p = &pi[2];
142 memcpy(p, oldP, n>oldN ? oldN : n);
143 if( n>oldN ){
144 memset(&((char*)p)[oldN], 0, n-oldN);
145 }
146 memset(oldPi, 0, (oldK+3)*sizeof(int));
147 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000148#if MEMORY_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000149 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 +0000150 (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000151#endif
drhdcc581c2000-05-30 13:44:19 +0000152 return p;
153}
drhc3c2fc92000-05-31 22:58:39 +0000154
155/*
156** Make a duplicate of a string into memory obtained from malloc()
157** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000158**
159** This routine is called on all strings that are passed outside of
160** the SQLite library. That way clients can free the string using free()
161** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000162*/
163void sqliteStrRealloc(char **pz){
164 char *zNew;
165 if( pz==0 || *pz==0 ) return;
166 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000167 if( zNew==0 ){
168 sqlite_malloc_failed++;
169 sqliteFree(*pz);
170 *pz = 0;
171 }
172 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000173 sqliteFree(*pz);
174 *pz = zNew;
175}
176
drh6e142f52000-06-08 13:36:40 +0000177/*
178** Make a copy of a string in memory obtained from sqliteMalloc()
179*/
180char *sqliteStrDup_(const char *z, char *zFile, int line){
181 char *zNew = sqliteMalloc_(strlen(z)+1, zFile, line);
182 if( zNew ) strcpy(zNew, z);
183 return zNew;
184}
185char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
186 char *zNew = sqliteMalloc_(n+1, zFile, line);
187 if( zNew ){
188 memcpy(zNew, z, n);
189 zNew[n] = 0;
190 }
191 return zNew;
192}
drh7c68d602000-10-11 19:28:51 +0000193#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000194
drh7c68d602000-10-11 19:28:51 +0000195/*
196** The following versions of malloc() and free() are for use in a
197** normal build.
198*/
199#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000200
drh75897232000-05-29 14:26:00 +0000201/*
202** Allocate new memory and set it to zero. Return NULL if
203** no memory is available.
204*/
205void *sqliteMalloc(int n){
206 void *p = malloc(n);
drhdaffd0e2001-04-11 14:28:42 +0000207 if( p==0 ){
208 sqlite_malloc_failed++;
209 return 0;
210 }
drh75897232000-05-29 14:26:00 +0000211 memset(p, 0, n);
212 return p;
213}
214
215/*
216** Free memory previously obtained from sqliteMalloc()
217*/
218void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000219 if( p ){
drh305cea62000-05-29 17:44:25 +0000220 free(p);
221 }
drh75897232000-05-29 14:26:00 +0000222}
223
224/*
225** Resize a prior allocation. If p==0, then this routine
226** works just like sqliteMalloc(). If n==0, then this routine
227** works just like sqliteFree().
228*/
229void *sqliteRealloc(void *p, int n){
230 if( p==0 ){
231 return sqliteMalloc(n);
232 }
233 if( n==0 ){
234 sqliteFree(p);
235 return 0;
236 }
drhdaffd0e2001-04-11 14:28:42 +0000237 p = realloc(p, n);
238 if( p==0 ){
239 sqlite_malloc_failed++;
240 }
241 return p;
drh75897232000-05-29 14:26:00 +0000242}
drh6e142f52000-06-08 13:36:40 +0000243
244/*
245** Make a copy of a string in memory obtained from sqliteMalloc()
246*/
247char *sqliteStrDup(const char *z){
248 char *zNew = sqliteMalloc(strlen(z)+1);
249 if( zNew ) strcpy(zNew, z);
250 return zNew;
251}
252char *sqliteStrNDup(const char *z, int n){
253 char *zNew = sqliteMalloc(n+1);
254 if( zNew ){
255 memcpy(zNew, z, n);
256 zNew[n] = 0;
257 }
258 return zNew;
259}
drh7c68d602000-10-11 19:28:51 +0000260#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000261
262/*
263** Create a string from the 2nd and subsequent arguments (up to the
264** first NULL argument), store the string in memory obtained from
265** sqliteMalloc() and make the pointer indicated by the 1st argument
266** point to that string.
267*/
268void sqliteSetString(char **pz, const char *zFirst, ...){
269 va_list ap;
270 int nByte;
271 const char *z;
272 char *zResult;
273
274 if( pz==0 ) return;
275 nByte = strlen(zFirst) + 1;
276 va_start(ap, zFirst);
277 while( (z = va_arg(ap, const char*))!=0 ){
278 nByte += strlen(z);
279 }
280 va_end(ap);
281 sqliteFree(*pz);
282 *pz = zResult = sqliteMalloc( nByte );
283 if( zResult==0 ) return;
284 strcpy(zResult, zFirst);
285 zResult += strlen(zResult);
286 va_start(ap, zFirst);
287 while( (z = va_arg(ap, const char*))!=0 ){
288 strcpy(zResult, z);
289 zResult += strlen(zResult);
290 }
291 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000292#ifdef MEMORY_DEBUG
293#if MEMORY_DEBUG>1
294 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
295#endif
296#endif
drh75897232000-05-29 14:26:00 +0000297}
298
299/*
300** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000301** a length integer which specifies how much of the source string
302** to copy (in bytes). -1 means use the whole string.
drh75897232000-05-29 14:26:00 +0000303*/
304void sqliteSetNString(char **pz, ...){
305 va_list ap;
306 int nByte;
307 const char *z;
308 char *zResult;
309 int n;
310
311 if( pz==0 ) return;
312 nByte = 0;
313 va_start(ap, pz);
314 while( (z = va_arg(ap, const char*))!=0 ){
315 n = va_arg(ap, int);
316 if( n<=0 ) n = strlen(z);
317 nByte += n;
318 }
319 va_end(ap);
320 sqliteFree(*pz);
321 *pz = zResult = sqliteMalloc( nByte + 1 );
322 if( zResult==0 ) return;
323 va_start(ap, pz);
324 while( (z = va_arg(ap, const char*))!=0 ){
325 n = va_arg(ap, int);
326 if( n<=0 ) n = strlen(z);
327 strncpy(zResult, z, n);
328 zResult += n;
329 }
330 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000331#ifdef MEMORY_DEBUG
332#if MEMORY_DEBUG>1
333 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
334#endif
335#endif
drh75897232000-05-29 14:26:00 +0000336 va_end(ap);
337}
338
drh982cef72000-05-30 16:27:03 +0000339/*
340** Convert an SQL-style quoted string into a normal string by removing
341** the quote characters. The conversion is done in-place. If the
342** input does not begin with a quote character, then this routine
343** is a no-op.
344*/
345void sqliteDequote(char *z){
346 int quote;
347 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000348 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000349 quote = z[0];
350 if( quote!='\'' && quote!='"' ) return;
351 for(i=1, j=0; z[i]; i++){
352 if( z[i]==quote ){
353 if( z[i+1]==quote ){
354 z[j++] = quote;
355 i++;
356 }else{
357 z[j++] = 0;
358 break;
359 }
360 }else{
361 z[j++] = z[i];
362 }
363 }
364}
365
drh75897232000-05-29 14:26:00 +0000366/* An array to map all upper-case characters into their corresponding
367** lower-case character.
368*/
369static unsigned char UpperToLower[] = {
370 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
371 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
372 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
373 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
374 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
375 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
376 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
377 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
378 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
379 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
380 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
381 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
382 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
383 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
384 252,253,254,255
385};
386
387/*
388** This function computes a hash on the name of a keyword.
389** Case is not significant.
390*/
391int sqliteHashNoCase(const char *z, int n){
392 int h = 0;
393 int c;
394 if( n<=0 ) n = strlen(z);
395 while( n-- > 0 && (c = *z++)!=0 ){
drh8c82b352000-12-10 18:23:50 +0000396 h = (h<<3) ^ h ^ UpperToLower[c];
drh75897232000-05-29 14:26:00 +0000397 }
398 if( h<0 ) h = -h;
399 return h;
400}
401
402/*
drh967e8b72000-06-21 13:59:10 +0000403** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000404** there is no consistency, we will define our own.
405*/
406int sqliteStrICmp(const char *zLeft, const char *zRight){
407 register unsigned char *a, *b;
408 a = (unsigned char *)zLeft;
409 b = (unsigned char *)zRight;
410 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
411 return *a - *b;
412}
413int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
414 register unsigned char *a, *b;
415 a = (unsigned char *)zLeft;
416 b = (unsigned char *)zRight;
417 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000418 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000419}
420
421/* Notes on string comparisions.
422**
423** We want the main string comparision function used for sorting to
424** sort both numbers and alphanumeric words into the correct sequence.
425** The same routine should do both without prior knowledge of which
426** type of text the input represents. It should even work for strings
427** which are a mixture of text and numbers.
428**
429** To accomplish this, we keep track of a state number while scanning
430** the two strings. The states are as follows:
431**
432** 1 Beginning of word
433** 2 Arbitrary text
434** 3 Integer
435** 4 Negative integer
436** 5 Real number
437** 6 Negative real
438**
439** The scan begins in state 1, beginning of word. Transitions to other
440** states are determined by characters seen, as shown in the following
441** chart:
442**
443** Current State Character Seen New State
444** -------------------- -------------- -------------------
445** 0 Beginning of word "-" 3 Negative integer
446** digit 2 Integer
447** space 0 Beginning of word
448** otherwise 1 Arbitrary text
449**
450** 1 Arbitrary text space 0 Beginning of word
451** digit 2 Integer
452** otherwise 1 Arbitrary text
453**
454** 2 Integer space 0 Beginning of word
455** "." 4 Real number
456** digit 2 Integer
457** otherwise 1 Arbitrary text
458**
459** 3 Negative integer space 0 Beginning of word
460** "." 5 Negative Real num
461** digit 3 Negative integer
462** otherwise 1 Arbitrary text
463**
464** 4 Real number space 0 Beginning of word
465** digit 4 Real number
466** otherwise 1 Arbitrary text
467**
468** 5 Negative real num space 0 Beginning of word
469** digit 5 Negative real num
470** otherwise 1 Arbitrary text
471**
472** To implement this state machine, we first classify each character
473** into on of the following categories:
474**
475** 0 Text
476** 1 Space
477** 2 Digit
478** 3 "-"
479** 4 "."
480**
481** Given an arbitrary character, the array charClass[] maps that character
482** into one of the atove categories.
483*/
484static const unsigned char charClass[] = {
485 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
486/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
487/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
488/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
489/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
490/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
491/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
492/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
494/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
495/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
496/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
497/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
498/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
499/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
500/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
501/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
502};
503#define N_CHAR_CLASS 5
504
505/*
506** Given the current state number (0 thru 5), this array figures
507** the new state number given the character class.
508*/
509static const unsigned char stateMachine[] = {
510 /* Text, Space, Digit, "-", "." */
511 1, 0, 2, 3, 1, /* State 0: Beginning of word */
512 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
513 1, 0, 2, 1, 4, /* State 2: Integer */
514 1, 0, 3, 1, 5, /* State 3: Negative integer */
515 1, 0, 4, 1, 1, /* State 4: Real number */
516 1, 0, 5, 1, 1, /* State 5: Negative real num */
517};
518
519/* This routine does a comparison of two strings. Case is used only
520** if useCase!=0. Numbers compare in numerical order.
521*/
522static int privateStrCmp(const char *atext, const char *btext, int useCase){
523 register unsigned char *a, *b, *map, ca, cb;
524 int result;
525 register int cclass = 0;
526
527 a = (unsigned char *)atext;
528 b = (unsigned char *)btext;
529 if( useCase ){
530 do{
531 if( (ca= *a++)!=(cb= *b++) ) break;
532 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
533 }while( ca!=0 );
534 }else{
535 map = UpperToLower;
536 do{
537 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
538 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
539 }while( ca!=0 );
540 }
541 switch( cclass ){
542 case 0:
543 case 1: {
544 if( isdigit(ca) && isdigit(cb) ){
545 cclass = 2;
546 }
547 break;
548 }
549 default: {
550 break;
551 }
552 }
553 switch( cclass ){
554 case 2:
555 case 3: {
556 if( isdigit(ca) ){
557 if( isdigit(cb) ){
558 int acnt, bcnt;
559 acnt = bcnt = 0;
560 while( isdigit(*a++) ) acnt++;
561 while( isdigit(*b++) ) bcnt++;
562 result = acnt - bcnt;
563 if( result==0 ) result = ca-cb;
564 }else{
565 result = 1;
566 }
567 }else if( isdigit(cb) ){
568 result = -1;
569 }else if( ca=='.' ){
570 result = 1;
571 }else if( cb=='.' ){
572 result = -1;
573 }else{
574 result = ca - cb;
575 cclass = 2;
576 }
577 if( cclass==3 ) result = -result;
578 break;
579 }
580 case 0:
581 case 1:
582 case 4: {
583 result = ca - cb;
584 break;
585 }
586 case 5: {
587 result = cb - ca;
588 };
589 }
590 return result;
591}
592
drha5c2ad02000-09-14 01:21:10 +0000593/*
594** Do a comparison of pure numerics. If either string is not a pure
595** numeric, then return 0. Otherwise return 1 and set *pResult to be
596** negative, zero or positive if the first string are numerially less than
597** equal to, or greater than the second.
598*/
599static int privateCompareNum(const char *a, const char *b, int *pResult){
600 char *endPtr;
601 double rA, rB;
602 int isNumA, isNumB;
603 if( isdigit(*a) || ((*a=='-' || *a=='+') && isdigit(a[1])) ){
604 rA = strtod(a, &endPtr);
605 isNumA = *endPtr==0;
606 }else{
607 isNumA = 0;
608 }
609 if( isdigit(*b) || ((*b=='-' || *b=='+') && isdigit(b[1])) ){
610 rB = strtod(b, &endPtr);
611 isNumB = *endPtr==0;
612 }else{
613 isNumB = 0;
614 }
615 if( isNumB==0 && isNumA==0 ) return 0;
616 if( isNumA!=isNumB ){
617 *pResult = isNumA - isNumB;
618 }else if( rA<rB ){
619 *pResult = -1;
620 }else if( rA>rB ){
621 *pResult = 1;
622 }else{
623 *pResult = 0;
624 }
625 return 1;
626}
627
drh75897232000-05-29 14:26:00 +0000628/* This comparison routine is what we use for comparison operations
629** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two
630** strings. Use case only as a tie-breaker. Numbers compare in
631** numerical order.
632*/
633int sqliteCompare(const char *atext, const char *btext){
634 int result;
drha5c2ad02000-09-14 01:21:10 +0000635 if( !privateCompareNum(atext, btext, &result) || result==0 ){
636 result = privateStrCmp(atext, btext, 0);
637 if( result==0 ) result = privateStrCmp(atext, btext, 1);
638 }
drh75897232000-05-29 14:26:00 +0000639 return result;
640}
641
642/*
643** If you compile just this one file with the -DTEST_COMPARE=1 option,
644** it generates a program to test the comparisons routines.
645*/
646#ifdef TEST_COMPARE
647#include <stdlib.h>
648#include <stdio.h>
649int sortCmp(const char **a, const char **b){
650 return sqliteCompare(*a, *b);
651}
652int main(int argc, char **argv){
drha5c2ad02000-09-14 01:21:10 +0000653 int i, j, k, n, cnt;
drh75897232000-05-29 14:26:00 +0000654 static char *azStr[] = {
655 "abc", "aBc", "abcd", "aBcd",
drha5c2ad02000-09-14 01:21:10 +0000656 "123", "124", "1234", "-123", "-124", "-1234", "+124",
drh75897232000-05-29 14:26:00 +0000657 "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456",
658 "x9", "x10", "x-9", "x-10", "X9", "X10",
drha5c2ad02000-09-14 01:21:10 +0000659 "1.234e+02", "+123", "1.23E2", "1.2345e+2", "-1.2345e2", "+w"
drh75897232000-05-29 14:26:00 +0000660 };
661 n = sizeof(azStr)/sizeof(azStr[0]);
662 qsort(azStr, n, sizeof(azStr[0]), sortCmp);
663 for(i=0; i<n; i++){
664 printf("%s\n", azStr[i]);
665 }
666 printf("Sanity1...");
667 fflush(stdout);
drha5c2ad02000-09-14 01:21:10 +0000668 cnt = 0;
drh75897232000-05-29 14:26:00 +0000669 for(i=0; i<n-1; i++){
670 char *a = azStr[i];
671 for(j=i+1; j<n; j++){
672 char *b = azStr[j];
673 if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){
674 printf("Failed! \"%s\" vs \"%s\"\n", a, b);
675 i = j = n;
676 }
drha5c2ad02000-09-14 01:21:10 +0000677 cnt++;
drh75897232000-05-29 14:26:00 +0000678 }
679 }
680 if( i<n ){
drha5c2ad02000-09-14 01:21:10 +0000681 printf(" OK (%d)\n", cnt);
682 }
683 printf("Sanity2...");
684 fflush(stdout);
685 cnt = 0;
686 for(i=0; i<n; i++){
687 char *a = azStr[i];
688 for(j=0; j<n; j++){
689 char *b = azStr[j];
690 for(k=0; k<n; k++){
691 char *c = azStr[k];
692 int x1, x2, x3, success;
693 x1 = sqliteCompare(a,b);
694 x2 = sqliteCompare(b,c);
695 x3 = sqliteCompare(a,c);
696 if( x1==0 ){
697 success = x2==x3;
698 }else if( x1<0 ){
699 success = (x2<=0 && x3<=0) || x2>0;
700 }else{
701 success = (x2>=0 && x3>=0) || x2<0;
702 }
703 if( !success ){
704 printf("Failed! \"%s\" vs \"%s\" vs \"%s\"\n", a, b, c);
705 i = j = k = n+1;
706 }
707 cnt++;
708 }
709 }
710 }
711 if( i<n+1 ){
712 printf(" OK (%d)\n", cnt);
drh75897232000-05-29 14:26:00 +0000713 }
714 return 0;
715}
716#endif
717
718/*
drh16e59552000-07-31 11:57:37 +0000719** This routine is used for sorting. Each key is a list of one or more
720** null-terminated strings. The list is terminated by two nulls in
721** a row. For example, the following text is key with three strings:
drh75897232000-05-29 14:26:00 +0000722**
723** +one\000-two\000+three\000\000
724**
725** Both arguments will have the same number of strings. This routine
726** returns negative, zero, or positive if the first argument is less
727** than, equal to, or greater than the first. (Result is a-b).
728**
729** Every string begins with either a "+" or "-" character. If the
730** character is "-" then the return value is negated. This is done
731** to implement a sort in descending order.
732*/
733int sqliteSortCompare(const char *a, const char *b){
734 int len;
735 int res = 0;
736
737 while( res==0 && *a && *b ){
738 res = sqliteCompare(&a[1], &b[1]);
739 if( res==0 ){
740 len = strlen(a) + 1;
741 a += len;
742 b += len;
743 }
744 }
745 if( *a=='-' ) res = -res;
746 return res;
747}
drhdce2cbe2000-05-31 02:27:49 +0000748
drh297ecf12001-04-05 15:57:13 +0000749#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000750/*
drh297ecf12001-04-05 15:57:13 +0000751** X is a pointer to the first byte of a UTF-8 character. Increment
752** X so that it points to the next character. This only works right
753** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000754*/
drh297ecf12001-04-05 15:57:13 +0000755#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
756#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000757
drh297ecf12001-04-05 15:57:13 +0000758#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000759/*
drh297ecf12001-04-05 15:57:13 +0000760** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000761*/
drh297ecf12001-04-05 15:57:13 +0000762#define sqliteNextChar(X) (++(X));
763#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000764
drh297ecf12001-04-05 15:57:13 +0000765#endif /* defined(SQLITE_UTF8) */
766
767
768#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000769/*
drh297ecf12001-04-05 15:57:13 +0000770** Convert the UTF-8 character to which z points into a 31-bit
771** UCS character. This only works right if z points to a well-formed
772** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000773*/
drh297ecf12001-04-05 15:57:13 +0000774static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000775 int c;
drh297ecf12001-04-05 15:57:13 +0000776 static const int initVal[] = {
777 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
778 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
779 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
780 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
781 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
782 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
783 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
784 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
785 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
786 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
787 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
788 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
789 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
790 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
791 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
792 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
793 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
794 255,
795 };
796 c = initVal[*(z++)];
797 while( (0xc0&*z)==0x80 ){
798 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000799 }
800 return c;
801}
drh297ecf12001-04-05 15:57:13 +0000802#endif
drhe17a7e32001-04-04 21:10:18 +0000803
804/*
805** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000806** potentially be a "glob" expression. Return true (1) if they
807** are the same and false (0) if they are different.
808**
809** Globbing rules:
810**
811** '*' Matches any sequence of zero or more characters.
812**
813** '?' Matches exactly one character.
814**
815** [...] Matches one character from the enclosed list of
816** characters.
817**
818** [^...] Matches one character not in the enclosed list.
819**
820** With the [...] and [^...] matching, a ']' character can be included
821** in the list by making it the first character after '[' or '^'. A
822** range of characters can be specified using '-'. Example:
823** "[a-z]" matches any single lower-case letter. To match a '-', make
824** it the last character in the list.
825**
826** This routine is usually quick, but can be N**2 in the worst case.
827**
828** Hints: to match '*' or '?', put them in "[]". Like this:
829**
830** abc[*]xyz Matches "abc*xyz" only
831*/
drhe17a7e32001-04-04 21:10:18 +0000832int
833sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
834 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000835 int invert;
836 int seen;
drhe17a7e32001-04-04 21:10:18 +0000837 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000838
839 while( (c = *zPattern)!=0 ){
840 switch( c ){
841 case '*':
drhe17a7e32001-04-04 21:10:18 +0000842 while( (c=zPattern[1]) == '*' || c == '?' ){
843 if( c=='?' ){
844 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000845 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000846 }
847 zPattern++;
848 }
849 if( c==0 ) return 1;
850 c = UpperToLower[c];
851 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +0000852 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000853 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000854 }
855 return *zString!=0;
856 }else{
857 while( (c2 = *zString)!=0 ){
858 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000859 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000860 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000861 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000862 }
863 return 0;
864 }
drhe17a7e32001-04-04 21:10:18 +0000865 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000866 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000867 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000868 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000869 break;
drhe17a7e32001-04-04 21:10:18 +0000870 }
871 case '[': {
872 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000873 seen = 0;
874 invert = 0;
drh297ecf12001-04-05 15:57:13 +0000875 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +0000876 if( c==0 ) return 0;
877 c2 = *++zPattern;
878 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
879 if( c2==']' ){
880 if( c==']' ) seen = 1;
881 c2 = *++zPattern;
882 }
drh297ecf12001-04-05 15:57:13 +0000883 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +0000884 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
885 zPattern++;
drh297ecf12001-04-05 15:57:13 +0000886 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +0000887 if( c>=prior_c && c<=c2 ) seen = 1;
888 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000889 }else if( c==c2 ){
890 seen = 1;
drhe17a7e32001-04-04 21:10:18 +0000891 prior_c = c2;
892 }else{
893 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +0000894 }
drh297ecf12001-04-05 15:57:13 +0000895 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +0000896 }
897 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000898 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000899 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000900 break;
drhe17a7e32001-04-04 21:10:18 +0000901 }
902 default: {
drhdce2cbe2000-05-31 02:27:49 +0000903 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +0000904 zPattern++;
905 zString++;
drhdce2cbe2000-05-31 02:27:49 +0000906 break;
drhe17a7e32001-04-04 21:10:18 +0000907 }
drhdce2cbe2000-05-31 02:27:49 +0000908 }
drhdce2cbe2000-05-31 02:27:49 +0000909 }
910 return *zString==0;
911}
912
913/*
drhe17a7e32001-04-04 21:10:18 +0000914** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +0000915** SQL. The '%' character matches any sequence of 0 or more
916** characters and '_' matches any single character. Case is
917** not significant.
918**
919** This routine is just an adaptation of the sqliteGlobCompare()
920** routine above.
921*/
922int
923sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +0000924 register int c;
925 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000926
927 while( (c = UpperToLower[*zPattern])!=0 ){
928 switch( c ){
drhe17a7e32001-04-04 21:10:18 +0000929 case '%': {
930 while( (c=zPattern[1]) == '%' || c == '_' ){
931 if( c=='_' ){
932 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000933 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000934 }
drhe17a7e32001-04-04 21:10:18 +0000935 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000936 }
drhe17a7e32001-04-04 21:10:18 +0000937 if( c==0 ) return 1;
938 c = UpperToLower[c];
939 while( (c2=UpperToLower[*zString])!=0 ){
940 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
941 if( c2==0 ) return 0;
942 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000943 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000944 }
945 return 0;
946 }
947 case '_': {
drhdce2cbe2000-05-31 02:27:49 +0000948 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000949 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000950 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000951 break;
drhe17a7e32001-04-04 21:10:18 +0000952 }
953 default: {
drhdce2cbe2000-05-31 02:27:49 +0000954 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +0000955 zPattern++;
956 zString++;
drhdce2cbe2000-05-31 02:27:49 +0000957 break;
drhe17a7e32001-04-04 21:10:18 +0000958 }
drhdce2cbe2000-05-31 02:27:49 +0000959 }
drhdce2cbe2000-05-31 02:27:49 +0000960 }
961 return *zString==0;
962}
drh5e00f6c2001-09-13 13:46:56 +0000963
964/*
965** Return a static string that describes the kind of error specified in the
966** argument.
967*/
968const char *sqliteErrStr(int rc){
drh50e5dad2001-09-15 00:57:28 +0000969 const char *z;
drh5e00f6c2001-09-13 13:46:56 +0000970 switch( rc ){
drh50e5dad2001-09-15 00:57:28 +0000971 case SQLITE_OK: z = "not an error"; break;
972 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
973 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
974 case SQLITE_PERM: z = "access permission denied"; break;
975 case SQLITE_ABORT: z = "callback requested query abort"; break;
976 case SQLITE_BUSY: z = "database in use by another process"; break;
977 case SQLITE_NOMEM: z = "out of memory"; break;
978 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
979 case SQLITE_INTERRUPT: z = "interrupted"; break;
980 case SQLITE_IOERR: z = "disk I/O error"; break;
981 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
982 case SQLITE_NOTFOUND: z = "table or record not found"; break;
983 case SQLITE_FULL: z = "database is full"; break;
984 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
985 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
986 case SQLITE_EMPTY: z = "table contains no data"; break;
987 case SQLITE_SCHEMA: z = "database schema has changed"; break;
drh092d0352001-09-15 13:15:12 +0000988 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
drh50e5dad2001-09-15 00:57:28 +0000989 default: z = "unknown error"; break;
drh5e00f6c2001-09-13 13:46:56 +0000990 }
991 return z;
992}