blob: 58103ba8dc0a7ee585362b6c97b42f7b3286b04e [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** Utility functions used throughout sqlite.
25**
26** This file contains functions for allocating memory, comparing
27** strings, and stuff like that.
28**
drh092d0352001-09-15 13:15:12 +000029** $Id: util.c,v 1.24 2001/09/15 13:15:13 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
32#include <stdarg.h>
33#include <ctype.h>
34
drh7c68d602000-10-11 19:28:51 +000035/*
drhdaffd0e2001-04-11 14:28:42 +000036** If malloc() ever fails, this global variable gets set to 1.
37** This causes the library to abort and never again function.
38*/
39int sqlite_malloc_failed = 0;
40
41/*
drh7c68d602000-10-11 19:28:51 +000042** If MEMORY_DEBUG is defined, then use versions of malloc() and
43** free() that track memory usage and check for buffer overruns.
44*/
drhdcc581c2000-05-30 13:44:19 +000045#ifdef MEMORY_DEBUG
46
drhdcc581c2000-05-30 13:44:19 +000047/*
drh8c82b352000-12-10 18:23:50 +000048** For keeping track of the number of mallocs and frees. This
49** is used to check for memory leaks.
50*/
51int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
52int sqlite_nFree; /* Number of sqliteFree() calls */
53int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
54
55
56/*
drhdcc581c2000-05-30 13:44:19 +000057** Allocate new memory and set it to zero. Return NULL if
58** no memory is available.
59*/
60void *sqliteMalloc_(int n, char *zFile, int line){
61 void *p;
62 int *pi;
63 int k;
drh6e142f52000-06-08 13:36:40 +000064 sqlite_nMalloc++;
65 if( sqlite_iMallocFail>=0 ){
66 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000067 if( sqlite_iMallocFail==0 ){
68 sqlite_malloc_failed++;
69 return 0;
70 }
drh6e142f52000-06-08 13:36:40 +000071 }
drhb0729502001-03-14 12:35:57 +000072 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000073 k = (n+sizeof(int)-1)/sizeof(int);
74 pi = malloc( (3+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000075 if( pi==0 ){
76 sqlite_malloc_failed++;
77 return 0;
78 }
drhdcc581c2000-05-30 13:44:19 +000079 pi[0] = 0xdead1122;
80 pi[1] = n;
81 pi[k+2] = 0xdead3344;
82 p = &pi[2];
83 memset(p, 0, n);
drhc3c2fc92000-05-31 22:58:39 +000084#if MEMORY_DEBUG>1
85 fprintf(stderr,"malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
86#endif
drhdcc581c2000-05-30 13:44:19 +000087 return p;
88}
89
90/*
91** Free memory previously obtained from sqliteMalloc()
92*/
93void sqliteFree_(void *p, char *zFile, int line){
94 if( p ){
95 int *pi, k, n;
96 pi = p;
97 pi -= 2;
drh6e142f52000-06-08 13:36:40 +000098 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +000099 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000100 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000101 return;
102 }
103 n = pi[1];
104 k = (n+sizeof(int)-1)/sizeof(int);
105 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000106 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000107 return;
108 }
drhc3c2fc92000-05-31 22:58:39 +0000109 memset(pi, 0xff, (k+3)*sizeof(int));
110#if MEMORY_DEBUG>1
111 fprintf(stderr,"free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
112#endif
drhdcc581c2000-05-30 13:44:19 +0000113 free(pi);
114 }
115}
116
117/*
118** Resize a prior allocation. If p==0, then this routine
119** works just like sqliteMalloc(). If n==0, then this routine
120** works just like sqliteFree().
121*/
122void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
123 int *oldPi, *pi, k, oldN, oldK;
124 void *p;
125 if( oldP==0 ){
126 return sqliteMalloc_(n,zFile,line);
127 }
128 if( n==0 ){
129 sqliteFree_(oldP,zFile,line);
130 return 0;
131 }
132 oldPi = oldP;
133 oldPi -= 2;
134 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000135 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000136 return 0;
drhdcc581c2000-05-30 13:44:19 +0000137 }
138 oldN = oldPi[1];
139 oldK = (oldN+sizeof(int)-1)/sizeof(int);
140 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000141 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000142 return 0;
drhdcc581c2000-05-30 13:44:19 +0000143 }
144 k = (n + sizeof(int) - 1)/sizeof(int);
145 pi = malloc( (k+3)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000146 if( pi==0 ){
147 sqlite_malloc_failed++;
148 return 0;
149 }
drhdcc581c2000-05-30 13:44:19 +0000150 pi[0] = 0xdead1122;
151 pi[1] = n;
152 pi[k+2] = 0xdead3344;
153 p = &pi[2];
154 memcpy(p, oldP, n>oldN ? oldN : n);
155 if( n>oldN ){
156 memset(&((char*)p)[oldN], 0, n-oldN);
157 }
158 memset(oldPi, 0, (oldK+3)*sizeof(int));
159 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000160#if MEMORY_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000161 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 +0000162 (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000163#endif
drhdcc581c2000-05-30 13:44:19 +0000164 return p;
165}
drhc3c2fc92000-05-31 22:58:39 +0000166
167/*
168** Make a duplicate of a string into memory obtained from malloc()
169** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000170**
171** This routine is called on all strings that are passed outside of
172** the SQLite library. That way clients can free the string using free()
173** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000174*/
175void sqliteStrRealloc(char **pz){
176 char *zNew;
177 if( pz==0 || *pz==0 ) return;
178 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000179 if( zNew==0 ){
180 sqlite_malloc_failed++;
181 sqliteFree(*pz);
182 *pz = 0;
183 }
184 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000185 sqliteFree(*pz);
186 *pz = zNew;
187}
188
drh6e142f52000-06-08 13:36:40 +0000189/*
190** Make a copy of a string in memory obtained from sqliteMalloc()
191*/
192char *sqliteStrDup_(const char *z, char *zFile, int line){
193 char *zNew = sqliteMalloc_(strlen(z)+1, zFile, line);
194 if( zNew ) strcpy(zNew, z);
195 return zNew;
196}
197char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
198 char *zNew = sqliteMalloc_(n+1, zFile, line);
199 if( zNew ){
200 memcpy(zNew, z, n);
201 zNew[n] = 0;
202 }
203 return zNew;
204}
drh7c68d602000-10-11 19:28:51 +0000205#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000206
drh7c68d602000-10-11 19:28:51 +0000207/*
208** The following versions of malloc() and free() are for use in a
209** normal build.
210*/
211#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000212
drh75897232000-05-29 14:26:00 +0000213/*
214** Allocate new memory and set it to zero. Return NULL if
215** no memory is available.
216*/
217void *sqliteMalloc(int n){
218 void *p = malloc(n);
drhdaffd0e2001-04-11 14:28:42 +0000219 if( p==0 ){
220 sqlite_malloc_failed++;
221 return 0;
222 }
drh75897232000-05-29 14:26:00 +0000223 memset(p, 0, n);
224 return p;
225}
226
227/*
228** Free memory previously obtained from sqliteMalloc()
229*/
230void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000231 if( p ){
drh305cea62000-05-29 17:44:25 +0000232 free(p);
233 }
drh75897232000-05-29 14:26:00 +0000234}
235
236/*
237** Resize a prior allocation. If p==0, then this routine
238** works just like sqliteMalloc(). If n==0, then this routine
239** works just like sqliteFree().
240*/
241void *sqliteRealloc(void *p, int n){
242 if( p==0 ){
243 return sqliteMalloc(n);
244 }
245 if( n==0 ){
246 sqliteFree(p);
247 return 0;
248 }
drhdaffd0e2001-04-11 14:28:42 +0000249 p = realloc(p, n);
250 if( p==0 ){
251 sqlite_malloc_failed++;
252 }
253 return p;
drh75897232000-05-29 14:26:00 +0000254}
drh6e142f52000-06-08 13:36:40 +0000255
256/*
257** Make a copy of a string in memory obtained from sqliteMalloc()
258*/
259char *sqliteStrDup(const char *z){
260 char *zNew = sqliteMalloc(strlen(z)+1);
261 if( zNew ) strcpy(zNew, z);
262 return zNew;
263}
264char *sqliteStrNDup(const char *z, int n){
265 char *zNew = sqliteMalloc(n+1);
266 if( zNew ){
267 memcpy(zNew, z, n);
268 zNew[n] = 0;
269 }
270 return zNew;
271}
drh7c68d602000-10-11 19:28:51 +0000272#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000273
274/*
275** Create a string from the 2nd and subsequent arguments (up to the
276** first NULL argument), store the string in memory obtained from
277** sqliteMalloc() and make the pointer indicated by the 1st argument
278** point to that string.
279*/
280void sqliteSetString(char **pz, const char *zFirst, ...){
281 va_list ap;
282 int nByte;
283 const char *z;
284 char *zResult;
285
286 if( pz==0 ) return;
287 nByte = strlen(zFirst) + 1;
288 va_start(ap, zFirst);
289 while( (z = va_arg(ap, const char*))!=0 ){
290 nByte += strlen(z);
291 }
292 va_end(ap);
293 sqliteFree(*pz);
294 *pz = zResult = sqliteMalloc( nByte );
295 if( zResult==0 ) return;
296 strcpy(zResult, zFirst);
297 zResult += strlen(zResult);
298 va_start(ap, zFirst);
299 while( (z = va_arg(ap, const char*))!=0 ){
300 strcpy(zResult, z);
301 zResult += strlen(zResult);
302 }
303 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000304#ifdef MEMORY_DEBUG
305#if MEMORY_DEBUG>1
306 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
307#endif
308#endif
drh75897232000-05-29 14:26:00 +0000309}
310
311/*
312** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000313** a length integer which specifies how much of the source string
314** to copy (in bytes). -1 means use the whole string.
drh75897232000-05-29 14:26:00 +0000315*/
316void sqliteSetNString(char **pz, ...){
317 va_list ap;
318 int nByte;
319 const char *z;
320 char *zResult;
321 int n;
322
323 if( pz==0 ) return;
324 nByte = 0;
325 va_start(ap, pz);
326 while( (z = va_arg(ap, const char*))!=0 ){
327 n = va_arg(ap, int);
328 if( n<=0 ) n = strlen(z);
329 nByte += n;
330 }
331 va_end(ap);
332 sqliteFree(*pz);
333 *pz = zResult = sqliteMalloc( nByte + 1 );
334 if( zResult==0 ) return;
335 va_start(ap, pz);
336 while( (z = va_arg(ap, const char*))!=0 ){
337 n = va_arg(ap, int);
338 if( n<=0 ) n = strlen(z);
339 strncpy(zResult, z, n);
340 zResult += n;
341 }
342 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000343#ifdef MEMORY_DEBUG
344#if MEMORY_DEBUG>1
345 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
346#endif
347#endif
drh75897232000-05-29 14:26:00 +0000348 va_end(ap);
349}
350
drh982cef72000-05-30 16:27:03 +0000351/*
352** Convert an SQL-style quoted string into a normal string by removing
353** the quote characters. The conversion is done in-place. If the
354** input does not begin with a quote character, then this routine
355** is a no-op.
356*/
357void sqliteDequote(char *z){
358 int quote;
359 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000360 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000361 quote = z[0];
362 if( quote!='\'' && quote!='"' ) return;
363 for(i=1, j=0; z[i]; i++){
364 if( z[i]==quote ){
365 if( z[i+1]==quote ){
366 z[j++] = quote;
367 i++;
368 }else{
369 z[j++] = 0;
370 break;
371 }
372 }else{
373 z[j++] = z[i];
374 }
375 }
376}
377
drh75897232000-05-29 14:26:00 +0000378/* An array to map all upper-case characters into their corresponding
379** lower-case character.
380*/
381static unsigned char UpperToLower[] = {
382 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
383 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
384 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
385 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
386 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
387 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
388 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
389 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
390 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
391 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
392 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
393 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
394 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
395 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
396 252,253,254,255
397};
398
399/*
400** This function computes a hash on the name of a keyword.
401** Case is not significant.
402*/
403int sqliteHashNoCase(const char *z, int n){
404 int h = 0;
405 int c;
406 if( n<=0 ) n = strlen(z);
407 while( n-- > 0 && (c = *z++)!=0 ){
drh8c82b352000-12-10 18:23:50 +0000408 h = (h<<3) ^ h ^ UpperToLower[c];
drh75897232000-05-29 14:26:00 +0000409 }
410 if( h<0 ) h = -h;
411 return h;
412}
413
414/*
drh967e8b72000-06-21 13:59:10 +0000415** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000416** there is no consistency, we will define our own.
417*/
418int sqliteStrICmp(const char *zLeft, const char *zRight){
419 register unsigned char *a, *b;
420 a = (unsigned char *)zLeft;
421 b = (unsigned char *)zRight;
422 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
423 return *a - *b;
424}
425int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
426 register unsigned char *a, *b;
427 a = (unsigned char *)zLeft;
428 b = (unsigned char *)zRight;
429 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000430 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000431}
432
433/* Notes on string comparisions.
434**
435** We want the main string comparision function used for sorting to
436** sort both numbers and alphanumeric words into the correct sequence.
437** The same routine should do both without prior knowledge of which
438** type of text the input represents. It should even work for strings
439** which are a mixture of text and numbers.
440**
441** To accomplish this, we keep track of a state number while scanning
442** the two strings. The states are as follows:
443**
444** 1 Beginning of word
445** 2 Arbitrary text
446** 3 Integer
447** 4 Negative integer
448** 5 Real number
449** 6 Negative real
450**
451** The scan begins in state 1, beginning of word. Transitions to other
452** states are determined by characters seen, as shown in the following
453** chart:
454**
455** Current State Character Seen New State
456** -------------------- -------------- -------------------
457** 0 Beginning of word "-" 3 Negative integer
458** digit 2 Integer
459** space 0 Beginning of word
460** otherwise 1 Arbitrary text
461**
462** 1 Arbitrary text space 0 Beginning of word
463** digit 2 Integer
464** otherwise 1 Arbitrary text
465**
466** 2 Integer space 0 Beginning of word
467** "." 4 Real number
468** digit 2 Integer
469** otherwise 1 Arbitrary text
470**
471** 3 Negative integer space 0 Beginning of word
472** "." 5 Negative Real num
473** digit 3 Negative integer
474** otherwise 1 Arbitrary text
475**
476** 4 Real number space 0 Beginning of word
477** digit 4 Real number
478** otherwise 1 Arbitrary text
479**
480** 5 Negative real num space 0 Beginning of word
481** digit 5 Negative real num
482** otherwise 1 Arbitrary text
483**
484** To implement this state machine, we first classify each character
485** into on of the following categories:
486**
487** 0 Text
488** 1 Space
489** 2 Digit
490** 3 "-"
491** 4 "."
492**
493** Given an arbitrary character, the array charClass[] maps that character
494** into one of the atove categories.
495*/
496static const unsigned char charClass[] = {
497 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
498/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
499/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
500/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
501/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
502/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
503/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
504/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
505/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
506/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
507/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
508/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
509/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
510/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
511/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
512/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
513/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
514};
515#define N_CHAR_CLASS 5
516
517/*
518** Given the current state number (0 thru 5), this array figures
519** the new state number given the character class.
520*/
521static const unsigned char stateMachine[] = {
522 /* Text, Space, Digit, "-", "." */
523 1, 0, 2, 3, 1, /* State 0: Beginning of word */
524 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
525 1, 0, 2, 1, 4, /* State 2: Integer */
526 1, 0, 3, 1, 5, /* State 3: Negative integer */
527 1, 0, 4, 1, 1, /* State 4: Real number */
528 1, 0, 5, 1, 1, /* State 5: Negative real num */
529};
530
531/* This routine does a comparison of two strings. Case is used only
532** if useCase!=0. Numbers compare in numerical order.
533*/
534static int privateStrCmp(const char *atext, const char *btext, int useCase){
535 register unsigned char *a, *b, *map, ca, cb;
536 int result;
537 register int cclass = 0;
538
539 a = (unsigned char *)atext;
540 b = (unsigned char *)btext;
541 if( useCase ){
542 do{
543 if( (ca= *a++)!=(cb= *b++) ) break;
544 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
545 }while( ca!=0 );
546 }else{
547 map = UpperToLower;
548 do{
549 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
550 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
551 }while( ca!=0 );
552 }
553 switch( cclass ){
554 case 0:
555 case 1: {
556 if( isdigit(ca) && isdigit(cb) ){
557 cclass = 2;
558 }
559 break;
560 }
561 default: {
562 break;
563 }
564 }
565 switch( cclass ){
566 case 2:
567 case 3: {
568 if( isdigit(ca) ){
569 if( isdigit(cb) ){
570 int acnt, bcnt;
571 acnt = bcnt = 0;
572 while( isdigit(*a++) ) acnt++;
573 while( isdigit(*b++) ) bcnt++;
574 result = acnt - bcnt;
575 if( result==0 ) result = ca-cb;
576 }else{
577 result = 1;
578 }
579 }else if( isdigit(cb) ){
580 result = -1;
581 }else if( ca=='.' ){
582 result = 1;
583 }else if( cb=='.' ){
584 result = -1;
585 }else{
586 result = ca - cb;
587 cclass = 2;
588 }
589 if( cclass==3 ) result = -result;
590 break;
591 }
592 case 0:
593 case 1:
594 case 4: {
595 result = ca - cb;
596 break;
597 }
598 case 5: {
599 result = cb - ca;
600 };
601 }
602 return result;
603}
604
drha5c2ad02000-09-14 01:21:10 +0000605/*
606** Do a comparison of pure numerics. If either string is not a pure
607** numeric, then return 0. Otherwise return 1 and set *pResult to be
608** negative, zero or positive if the first string are numerially less than
609** equal to, or greater than the second.
610*/
611static int privateCompareNum(const char *a, const char *b, int *pResult){
612 char *endPtr;
613 double rA, rB;
614 int isNumA, isNumB;
615 if( isdigit(*a) || ((*a=='-' || *a=='+') && isdigit(a[1])) ){
616 rA = strtod(a, &endPtr);
617 isNumA = *endPtr==0;
618 }else{
619 isNumA = 0;
620 }
621 if( isdigit(*b) || ((*b=='-' || *b=='+') && isdigit(b[1])) ){
622 rB = strtod(b, &endPtr);
623 isNumB = *endPtr==0;
624 }else{
625 isNumB = 0;
626 }
627 if( isNumB==0 && isNumA==0 ) return 0;
628 if( isNumA!=isNumB ){
629 *pResult = isNumA - isNumB;
630 }else if( rA<rB ){
631 *pResult = -1;
632 }else if( rA>rB ){
633 *pResult = 1;
634 }else{
635 *pResult = 0;
636 }
637 return 1;
638}
639
drh75897232000-05-29 14:26:00 +0000640/* This comparison routine is what we use for comparison operations
641** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two
642** strings. Use case only as a tie-breaker. Numbers compare in
643** numerical order.
644*/
645int sqliteCompare(const char *atext, const char *btext){
646 int result;
drha5c2ad02000-09-14 01:21:10 +0000647 if( !privateCompareNum(atext, btext, &result) || result==0 ){
648 result = privateStrCmp(atext, btext, 0);
649 if( result==0 ) result = privateStrCmp(atext, btext, 1);
650 }
drh75897232000-05-29 14:26:00 +0000651 return result;
652}
653
654/*
655** If you compile just this one file with the -DTEST_COMPARE=1 option,
656** it generates a program to test the comparisons routines.
657*/
658#ifdef TEST_COMPARE
659#include <stdlib.h>
660#include <stdio.h>
661int sortCmp(const char **a, const char **b){
662 return sqliteCompare(*a, *b);
663}
664int main(int argc, char **argv){
drha5c2ad02000-09-14 01:21:10 +0000665 int i, j, k, n, cnt;
drh75897232000-05-29 14:26:00 +0000666 static char *azStr[] = {
667 "abc", "aBc", "abcd", "aBcd",
drha5c2ad02000-09-14 01:21:10 +0000668 "123", "124", "1234", "-123", "-124", "-1234", "+124",
drh75897232000-05-29 14:26:00 +0000669 "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456",
670 "x9", "x10", "x-9", "x-10", "X9", "X10",
drha5c2ad02000-09-14 01:21:10 +0000671 "1.234e+02", "+123", "1.23E2", "1.2345e+2", "-1.2345e2", "+w"
drh75897232000-05-29 14:26:00 +0000672 };
673 n = sizeof(azStr)/sizeof(azStr[0]);
674 qsort(azStr, n, sizeof(azStr[0]), sortCmp);
675 for(i=0; i<n; i++){
676 printf("%s\n", azStr[i]);
677 }
678 printf("Sanity1...");
679 fflush(stdout);
drha5c2ad02000-09-14 01:21:10 +0000680 cnt = 0;
drh75897232000-05-29 14:26:00 +0000681 for(i=0; i<n-1; i++){
682 char *a = azStr[i];
683 for(j=i+1; j<n; j++){
684 char *b = azStr[j];
685 if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){
686 printf("Failed! \"%s\" vs \"%s\"\n", a, b);
687 i = j = n;
688 }
drha5c2ad02000-09-14 01:21:10 +0000689 cnt++;
drh75897232000-05-29 14:26:00 +0000690 }
691 }
692 if( i<n ){
drha5c2ad02000-09-14 01:21:10 +0000693 printf(" OK (%d)\n", cnt);
694 }
695 printf("Sanity2...");
696 fflush(stdout);
697 cnt = 0;
698 for(i=0; i<n; i++){
699 char *a = azStr[i];
700 for(j=0; j<n; j++){
701 char *b = azStr[j];
702 for(k=0; k<n; k++){
703 char *c = azStr[k];
704 int x1, x2, x3, success;
705 x1 = sqliteCompare(a,b);
706 x2 = sqliteCompare(b,c);
707 x3 = sqliteCompare(a,c);
708 if( x1==0 ){
709 success = x2==x3;
710 }else if( x1<0 ){
711 success = (x2<=0 && x3<=0) || x2>0;
712 }else{
713 success = (x2>=0 && x3>=0) || x2<0;
714 }
715 if( !success ){
716 printf("Failed! \"%s\" vs \"%s\" vs \"%s\"\n", a, b, c);
717 i = j = k = n+1;
718 }
719 cnt++;
720 }
721 }
722 }
723 if( i<n+1 ){
724 printf(" OK (%d)\n", cnt);
drh75897232000-05-29 14:26:00 +0000725 }
726 return 0;
727}
728#endif
729
730/*
drh16e59552000-07-31 11:57:37 +0000731** This routine is used for sorting. Each key is a list of one or more
732** null-terminated strings. The list is terminated by two nulls in
733** a row. For example, the following text is key with three strings:
drh75897232000-05-29 14:26:00 +0000734**
735** +one\000-two\000+three\000\000
736**
737** Both arguments will have the same number of strings. This routine
738** returns negative, zero, or positive if the first argument is less
739** than, equal to, or greater than the first. (Result is a-b).
740**
741** Every string begins with either a "+" or "-" character. If the
742** character is "-" then the return value is negated. This is done
743** to implement a sort in descending order.
744*/
745int sqliteSortCompare(const char *a, const char *b){
746 int len;
747 int res = 0;
748
749 while( res==0 && *a && *b ){
750 res = sqliteCompare(&a[1], &b[1]);
751 if( res==0 ){
752 len = strlen(a) + 1;
753 a += len;
754 b += len;
755 }
756 }
757 if( *a=='-' ) res = -res;
758 return res;
759}
drhdce2cbe2000-05-31 02:27:49 +0000760
drh297ecf12001-04-05 15:57:13 +0000761#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +0000762/*
drh297ecf12001-04-05 15:57:13 +0000763** X is a pointer to the first byte of a UTF-8 character. Increment
764** X so that it points to the next character. This only works right
765** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000766*/
drh297ecf12001-04-05 15:57:13 +0000767#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
768#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +0000769
drh297ecf12001-04-05 15:57:13 +0000770#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +0000771/*
drh297ecf12001-04-05 15:57:13 +0000772** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +0000773*/
drh297ecf12001-04-05 15:57:13 +0000774#define sqliteNextChar(X) (++(X));
775#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +0000776
drh297ecf12001-04-05 15:57:13 +0000777#endif /* defined(SQLITE_UTF8) */
778
779
780#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +0000781/*
drh297ecf12001-04-05 15:57:13 +0000782** Convert the UTF-8 character to which z points into a 31-bit
783** UCS character. This only works right if z points to a well-formed
784** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +0000785*/
drh297ecf12001-04-05 15:57:13 +0000786static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +0000787 int c;
drh297ecf12001-04-05 15:57:13 +0000788 static const int initVal[] = {
789 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
790 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
791 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
792 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
793 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
794 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
795 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
796 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
797 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
798 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
799 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
800 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
801 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
802 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
803 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
804 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
805 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
806 255,
807 };
808 c = initVal[*(z++)];
809 while( (0xc0&*z)==0x80 ){
810 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +0000811 }
812 return c;
813}
drh297ecf12001-04-05 15:57:13 +0000814#endif
drhe17a7e32001-04-04 21:10:18 +0000815
816/*
817** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +0000818** potentially be a "glob" expression. Return true (1) if they
819** are the same and false (0) if they are different.
820**
821** Globbing rules:
822**
823** '*' Matches any sequence of zero or more characters.
824**
825** '?' Matches exactly one character.
826**
827** [...] Matches one character from the enclosed list of
828** characters.
829**
830** [^...] Matches one character not in the enclosed list.
831**
832** With the [...] and [^...] matching, a ']' character can be included
833** in the list by making it the first character after '[' or '^'. A
834** range of characters can be specified using '-'. Example:
835** "[a-z]" matches any single lower-case letter. To match a '-', make
836** it the last character in the list.
837**
838** This routine is usually quick, but can be N**2 in the worst case.
839**
840** Hints: to match '*' or '?', put them in "[]". Like this:
841**
842** abc[*]xyz Matches "abc*xyz" only
843*/
drhe17a7e32001-04-04 21:10:18 +0000844int
845sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
846 register int c;
drhdce2cbe2000-05-31 02:27:49 +0000847 int invert;
848 int seen;
drhe17a7e32001-04-04 21:10:18 +0000849 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000850
851 while( (c = *zPattern)!=0 ){
852 switch( c ){
853 case '*':
drhe17a7e32001-04-04 21:10:18 +0000854 while( (c=zPattern[1]) == '*' || c == '?' ){
855 if( c=='?' ){
856 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000857 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000858 }
859 zPattern++;
860 }
861 if( c==0 ) return 1;
862 c = UpperToLower[c];
863 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +0000864 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +0000865 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000866 }
867 return *zString!=0;
868 }else{
869 while( (c2 = *zString)!=0 ){
870 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000871 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000872 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000873 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000874 }
875 return 0;
876 }
drhe17a7e32001-04-04 21:10:18 +0000877 case '?': {
drhdce2cbe2000-05-31 02:27:49 +0000878 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000879 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000880 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000881 break;
drhe17a7e32001-04-04 21:10:18 +0000882 }
883 case '[': {
884 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000885 seen = 0;
886 invert = 0;
drh297ecf12001-04-05 15:57:13 +0000887 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +0000888 if( c==0 ) return 0;
889 c2 = *++zPattern;
890 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
891 if( c2==']' ){
892 if( c==']' ) seen = 1;
893 c2 = *++zPattern;
894 }
drh297ecf12001-04-05 15:57:13 +0000895 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +0000896 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
897 zPattern++;
drh297ecf12001-04-05 15:57:13 +0000898 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +0000899 if( c>=prior_c && c<=c2 ) seen = 1;
900 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +0000901 }else if( c==c2 ){
902 seen = 1;
drhe17a7e32001-04-04 21:10:18 +0000903 prior_c = c2;
904 }else{
905 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +0000906 }
drh297ecf12001-04-05 15:57:13 +0000907 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +0000908 }
909 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000910 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000911 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000912 break;
drhe17a7e32001-04-04 21:10:18 +0000913 }
914 default: {
drhdce2cbe2000-05-31 02:27:49 +0000915 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +0000916 zPattern++;
917 zString++;
drhdce2cbe2000-05-31 02:27:49 +0000918 break;
drhe17a7e32001-04-04 21:10:18 +0000919 }
drhdce2cbe2000-05-31 02:27:49 +0000920 }
drhdce2cbe2000-05-31 02:27:49 +0000921 }
922 return *zString==0;
923}
924
925/*
drhe17a7e32001-04-04 21:10:18 +0000926** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +0000927** SQL. The '%' character matches any sequence of 0 or more
928** characters and '_' matches any single character. Case is
929** not significant.
930**
931** This routine is just an adaptation of the sqliteGlobCompare()
932** routine above.
933*/
934int
935sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +0000936 register int c;
937 int c2;
drhdce2cbe2000-05-31 02:27:49 +0000938
939 while( (c = UpperToLower[*zPattern])!=0 ){
940 switch( c ){
drhe17a7e32001-04-04 21:10:18 +0000941 case '%': {
942 while( (c=zPattern[1]) == '%' || c == '_' ){
943 if( c=='_' ){
944 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000945 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +0000946 }
drhe17a7e32001-04-04 21:10:18 +0000947 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000948 }
drhe17a7e32001-04-04 21:10:18 +0000949 if( c==0 ) return 1;
950 c = UpperToLower[c];
951 while( (c2=UpperToLower[*zString])!=0 ){
952 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
953 if( c2==0 ) return 0;
954 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +0000955 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000956 }
957 return 0;
958 }
959 case '_': {
drhdce2cbe2000-05-31 02:27:49 +0000960 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +0000961 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +0000962 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +0000963 break;
drhe17a7e32001-04-04 21:10:18 +0000964 }
965 default: {
drhdce2cbe2000-05-31 02:27:49 +0000966 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +0000967 zPattern++;
968 zString++;
drhdce2cbe2000-05-31 02:27:49 +0000969 break;
drhe17a7e32001-04-04 21:10:18 +0000970 }
drhdce2cbe2000-05-31 02:27:49 +0000971 }
drhdce2cbe2000-05-31 02:27:49 +0000972 }
973 return *zString==0;
974}
drh5e00f6c2001-09-13 13:46:56 +0000975
976/*
977** Return a static string that describes the kind of error specified in the
978** argument.
979*/
980const char *sqliteErrStr(int rc){
drh50e5dad2001-09-15 00:57:28 +0000981 const char *z;
drh5e00f6c2001-09-13 13:46:56 +0000982 switch( rc ){
drh50e5dad2001-09-15 00:57:28 +0000983 case SQLITE_OK: z = "not an error"; break;
984 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
985 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
986 case SQLITE_PERM: z = "access permission denied"; break;
987 case SQLITE_ABORT: z = "callback requested query abort"; break;
988 case SQLITE_BUSY: z = "database in use by another process"; break;
989 case SQLITE_NOMEM: z = "out of memory"; break;
990 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
991 case SQLITE_INTERRUPT: z = "interrupted"; break;
992 case SQLITE_IOERR: z = "disk I/O error"; break;
993 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
994 case SQLITE_NOTFOUND: z = "table or record not found"; break;
995 case SQLITE_FULL: z = "database is full"; break;
996 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
997 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
998 case SQLITE_EMPTY: z = "table contains no data"; break;
999 case SQLITE_SCHEMA: z = "database schema has changed"; break;
drh092d0352001-09-15 13:15:12 +00001000 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
drh50e5dad2001-09-15 00:57:28 +00001001 default: z = "unknown error"; break;
drh5e00f6c2001-09-13 13:46:56 +00001002 }
1003 return z;
1004}