blob: 87e5f4431c4e287975b398f5b7a2168ea1b298cf [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**
drhda93d232003-03-31 02:12:46 +000017** $Id: util.c,v 1.59 2003/03/31 02:12:48 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 */
drhd94a6692002-08-25 18:29:11 +000042#if MEMORY_DEBUG>1
43static int memcnt = 0;
44#endif
drh8c82b352000-12-10 18:23:50 +000045
46
47/*
drhdcc581c2000-05-30 13:44:19 +000048** Allocate new memory and set it to zero. Return NULL if
49** no memory is available.
50*/
drh8c1238a2003-01-02 14:43:55 +000051void *sqliteMalloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000052 void *p;
53 int *pi;
54 int k;
drh6e142f52000-06-08 13:36:40 +000055 if( sqlite_iMallocFail>=0 ){
56 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000057 if( sqlite_iMallocFail==0 ){
58 sqlite_malloc_failed++;
drh6d4abfb2001-10-22 02:58:08 +000059#if MEMORY_DEBUG>1
60 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
61 n, zFile,line);
62#endif
63 sqlite_iMallocFail--;
drhdaffd0e2001-04-11 14:28:42 +000064 return 0;
65 }
drh6e142f52000-06-08 13:36:40 +000066 }
drhb0729502001-03-14 12:35:57 +000067 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000068 k = (n+sizeof(int)-1)/sizeof(int);
69 pi = malloc( (3+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000070 if( pi==0 ){
71 sqlite_malloc_failed++;
72 return 0;
73 }
drh6d4abfb2001-10-22 02:58:08 +000074 sqlite_nMalloc++;
drhdcc581c2000-05-30 13:44:19 +000075 pi[0] = 0xdead1122;
76 pi[1] = n;
77 pi[k+2] = 0xdead3344;
78 p = &pi[2];
drh8c1238a2003-01-02 14:43:55 +000079 memset(p, bZero==0, n);
drhc3c2fc92000-05-31 22:58:39 +000080#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +000081 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
82 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +000083#endif
drhdcc581c2000-05-30 13:44:19 +000084 return p;
85}
86
87/*
drhed6c8672003-01-12 18:02:16 +000088** Check to see if the given pointer was obtained from sqliteMalloc()
89** and is able to hold at least N bytes. Raise an exception if this
90** is not the case.
91**
92** This routine is used for testing purposes only.
93*/
94void sqliteCheckMemory(void *p, int N){
95 int *pi = p;
96 int n, k;
97 pi -= 2;
98 assert( pi[0]==0xdead1122 );
99 n = pi[1];
100 assert( N>=0 && N<n );
101 k = (n+sizeof(int)-1)/sizeof(int);
102 assert( pi[k+2]==0xdead3344 );
103}
104
105/*
drhdcc581c2000-05-30 13:44:19 +0000106** Free memory previously obtained from sqliteMalloc()
107*/
108void sqliteFree_(void *p, char *zFile, int line){
109 if( p ){
110 int *pi, k, n;
111 pi = p;
112 pi -= 2;
drh6e142f52000-06-08 13:36:40 +0000113 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +0000114 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000115 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000116 return;
117 }
118 n = pi[1];
119 k = (n+sizeof(int)-1)/sizeof(int);
120 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000121 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +0000122 return;
123 }
drhc3c2fc92000-05-31 22:58:39 +0000124 memset(pi, 0xff, (k+3)*sizeof(int));
125#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000126 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
127 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000128#endif
drhdcc581c2000-05-30 13:44:19 +0000129 free(pi);
130 }
131}
132
133/*
134** Resize a prior allocation. If p==0, then this routine
135** works just like sqliteMalloc(). If n==0, then this routine
136** works just like sqliteFree().
137*/
138void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
139 int *oldPi, *pi, k, oldN, oldK;
140 void *p;
141 if( oldP==0 ){
drh8c1238a2003-01-02 14:43:55 +0000142 return sqliteMalloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000143 }
144 if( n==0 ){
145 sqliteFree_(oldP,zFile,line);
146 return 0;
147 }
148 oldPi = oldP;
149 oldPi -= 2;
150 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000151 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000152 return 0;
drhdcc581c2000-05-30 13:44:19 +0000153 }
154 oldN = oldPi[1];
155 oldK = (oldN+sizeof(int)-1)/sizeof(int);
156 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000157 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000158 return 0;
drhdcc581c2000-05-30 13:44:19 +0000159 }
160 k = (n + sizeof(int) - 1)/sizeof(int);
161 pi = malloc( (k+3)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000162 if( pi==0 ){
163 sqlite_malloc_failed++;
164 return 0;
165 }
drhdcc581c2000-05-30 13:44:19 +0000166 pi[0] = 0xdead1122;
167 pi[1] = n;
168 pi[k+2] = 0xdead3344;
169 p = &pi[2];
170 memcpy(p, oldP, n>oldN ? oldN : n);
171 if( n>oldN ){
172 memset(&((char*)p)[oldN], 0, n-oldN);
173 }
drh8c1238a2003-01-02 14:43:55 +0000174 memset(oldPi, 0xab, (oldK+3)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000175 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000176#if MEMORY_DEBUG>1
drhd94a6692002-08-25 18:29:11 +0000177 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
178 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000179#endif
drhdcc581c2000-05-30 13:44:19 +0000180 return p;
181}
drhc3c2fc92000-05-31 22:58:39 +0000182
183/*
184** Make a duplicate of a string into memory obtained from malloc()
185** Free the original string using sqliteFree().
drhdaffd0e2001-04-11 14:28:42 +0000186**
187** This routine is called on all strings that are passed outside of
188** the SQLite library. That way clients can free the string using free()
189** rather than having to call sqliteFree().
drhc3c2fc92000-05-31 22:58:39 +0000190*/
191void sqliteStrRealloc(char **pz){
192 char *zNew;
193 if( pz==0 || *pz==0 ) return;
194 zNew = malloc( strlen(*pz) + 1 );
drhdaffd0e2001-04-11 14:28:42 +0000195 if( zNew==0 ){
196 sqlite_malloc_failed++;
197 sqliteFree(*pz);
198 *pz = 0;
199 }
200 strcpy(zNew, *pz);
drhc3c2fc92000-05-31 22:58:39 +0000201 sqliteFree(*pz);
202 *pz = zNew;
203}
204
drh6e142f52000-06-08 13:36:40 +0000205/*
206** Make a copy of a string in memory obtained from sqliteMalloc()
207*/
208char *sqliteStrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000209 char *zNew;
210 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000211 zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000212 if( zNew ) strcpy(zNew, z);
213 return zNew;
214}
215char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000216 char *zNew;
217 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000218 zNew = sqliteMalloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000219 if( zNew ){
220 memcpy(zNew, z, n);
221 zNew[n] = 0;
222 }
223 return zNew;
224}
drh7c68d602000-10-11 19:28:51 +0000225#endif /* MEMORY_DEBUG */
drh6e142f52000-06-08 13:36:40 +0000226
drh7c68d602000-10-11 19:28:51 +0000227/*
228** The following versions of malloc() and free() are for use in a
229** normal build.
230*/
231#if !defined(MEMORY_DEBUG)
drh6e142f52000-06-08 13:36:40 +0000232
drh75897232000-05-29 14:26:00 +0000233/*
234** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000235** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000236*/
237void *sqliteMalloc(int n){
drh26780582002-10-20 15:46:22 +0000238 void *p;
239 if( n==0 ) return 0;
240 p = malloc(n);
drhdaffd0e2001-04-11 14:28:42 +0000241 if( p==0 ){
242 sqlite_malloc_failed++;
243 return 0;
244 }
drh75897232000-05-29 14:26:00 +0000245 memset(p, 0, n);
246 return p;
247}
248
249/*
drh8c1238a2003-01-02 14:43:55 +0000250** Allocate new memory but do not set it to zero. Return NULL if
251** no memory is available. See also sqliteMalloc().
252*/
253void *sqliteMallocRaw(int n){
254 void *p;
255 if( n==0 ) return 0;
256 p = malloc(n);
257 if( p==0 ){
258 sqlite_malloc_failed++;
259 return 0;
260 }
261 return p;
262}
263
264/*
drh75897232000-05-29 14:26:00 +0000265** Free memory previously obtained from sqliteMalloc()
266*/
267void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000268 if( p ){
drh305cea62000-05-29 17:44:25 +0000269 free(p);
270 }
drh75897232000-05-29 14:26:00 +0000271}
272
273/*
274** Resize a prior allocation. If p==0, then this routine
275** works just like sqliteMalloc(). If n==0, then this routine
276** works just like sqliteFree().
277*/
278void *sqliteRealloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000279 void *p2;
drh75897232000-05-29 14:26:00 +0000280 if( p==0 ){
281 return sqliteMalloc(n);
282 }
283 if( n==0 ){
284 sqliteFree(p);
285 return 0;
286 }
drh6d4abfb2001-10-22 02:58:08 +0000287 p2 = realloc(p, n);
288 if( p2==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000289 sqlite_malloc_failed++;
290 }
drh6d4abfb2001-10-22 02:58:08 +0000291 return p2;
drh75897232000-05-29 14:26:00 +0000292}
drh6e142f52000-06-08 13:36:40 +0000293
294/*
295** Make a copy of a string in memory obtained from sqliteMalloc()
296*/
297char *sqliteStrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000298 char *zNew;
299 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000300 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000301 if( zNew ) strcpy(zNew, z);
302 return zNew;
303}
304char *sqliteStrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000305 char *zNew;
306 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000307 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000308 if( zNew ){
309 memcpy(zNew, z, n);
310 zNew[n] = 0;
311 }
312 return zNew;
313}
drh7c68d602000-10-11 19:28:51 +0000314#endif /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000315
316/*
317** Create a string from the 2nd and subsequent arguments (up to the
318** first NULL argument), store the string in memory obtained from
319** sqliteMalloc() and make the pointer indicated by the 1st argument
320** point to that string.
321*/
322void sqliteSetString(char **pz, const char *zFirst, ...){
323 va_list ap;
324 int nByte;
325 const char *z;
326 char *zResult;
327
328 if( pz==0 ) return;
329 nByte = strlen(zFirst) + 1;
330 va_start(ap, zFirst);
331 while( (z = va_arg(ap, const char*))!=0 ){
332 nByte += strlen(z);
333 }
334 va_end(ap);
335 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000336 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000337 if( zResult==0 ){
338 return;
339 }
drh75897232000-05-29 14:26:00 +0000340 strcpy(zResult, zFirst);
341 zResult += strlen(zResult);
342 va_start(ap, zFirst);
343 while( (z = va_arg(ap, const char*))!=0 ){
344 strcpy(zResult, z);
345 zResult += strlen(zResult);
346 }
347 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000348#ifdef MEMORY_DEBUG
349#if MEMORY_DEBUG>1
350 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
351#endif
352#endif
drh75897232000-05-29 14:26:00 +0000353}
354
355/*
356** Works like sqliteSetString, but each string is now followed by
drhe17a7e32001-04-04 21:10:18 +0000357** a length integer which specifies how much of the source string
358** to copy (in bytes). -1 means use the whole string.
drh75897232000-05-29 14:26:00 +0000359*/
360void sqliteSetNString(char **pz, ...){
361 va_list ap;
362 int nByte;
363 const char *z;
364 char *zResult;
365 int n;
366
367 if( pz==0 ) return;
368 nByte = 0;
369 va_start(ap, pz);
370 while( (z = va_arg(ap, const char*))!=0 ){
371 n = va_arg(ap, int);
372 if( n<=0 ) n = strlen(z);
373 nByte += n;
374 }
375 va_end(ap);
376 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000377 *pz = zResult = sqliteMallocRaw( nByte + 1 );
drh75897232000-05-29 14:26:00 +0000378 if( zResult==0 ) return;
379 va_start(ap, pz);
380 while( (z = va_arg(ap, const char*))!=0 ){
381 n = va_arg(ap, int);
382 if( n<=0 ) n = strlen(z);
383 strncpy(zResult, z, n);
384 zResult += n;
385 }
386 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000387#ifdef MEMORY_DEBUG
388#if MEMORY_DEBUG>1
389 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
390#endif
391#endif
drh75897232000-05-29 14:26:00 +0000392 va_end(ap);
393}
394
drh982cef72000-05-30 16:27:03 +0000395/*
drhda93d232003-03-31 02:12:46 +0000396** Add an error message to pParse->zErrMsg and increment pParse->nErr.
397** The following formatting characters are allowed:
398**
399** %s Insert a string
400** %z A string that should be freed after use
401** %d Insert an integer
402** %T Insert a token
403** %S Insert the first element of a SrcList
404*/
405void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){
406 va_list ap;
407 int nByte;
408 int i, j;
409 char *z;
410 static char zNull[] = "NULL";
411
412 pParse->nErr++;
413 nByte = 1 + strlen(zFormat);
414 va_start(ap, zFormat);
415 for(i=0; zFormat[i]; i++){
416 if( zFormat[i]!='%' && zFormat[i+1] ) continue;
417 i++;
418 switch( zFormat[i] ){
419 case 'd': {
420 (void)va_arg(ap, int);
421 nByte += 20;
422 break;
423 }
424 case 'z':
425 case 's': {
426 char *z2 = va_arg(ap, char*);
427 if( z2==0 ) z2 = zNull;
428 nByte += strlen(z2);
429 break;
430 }
431 case 'T': {
432 Token *p = va_arg(ap, Token*);
433 nByte += p->n;
434 break;
435 }
436 case 'S': {
437 SrcList *p = va_arg(ap, SrcList*);
438 int k = va_arg(ap, int);
439 assert( p->nSrc>k && k>=0 );
440 nByte += strlen(p->a[k].zName);
441 if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
442 nByte += strlen(p->a[k].zDatabase)+1;
443 }
444 break;
445 }
446 default: {
447 nByte++;
448 break;
449 }
450 }
451 }
452 va_end(ap);
453 z = sqliteMalloc( nByte );
454 if( z==0 ) return;
455 sqliteFree(pParse->zErrMsg);
456 pParse->zErrMsg = z;
457 va_start(ap, zFormat);
458 for(i=j=0; zFormat[i]; i++){
459 if( zFormat[i]!='%' ) continue;
460 if( i>j ){
461 memcpy(z, &zFormat[j], i-j);
462 z += i-j;
463 }
464 j = i+2;
465 i++;
466 switch( zFormat[i] ){
467 case 'd': {
468 int x = va_arg(ap, int);
469 sprintf(z, "%d", x);
470 z += strlen(z);
471 break;
472 }
473 case 'z':
474 case 's': {
475 int len;
476 char *z2 = va_arg(ap, char*);
477 if( z2==0 ) z2 = zNull;
478 len = strlen(z2);
479 memcpy(z, z2, len);
480 z += len;
481 if( zFormat[i]=='z' && z2!=zNull ){
482 sqliteFree(z2);
483 }
484 break;
485 }
486 case 'T': {
487 Token *p = va_arg(ap, Token*);
488 memcpy(z, p->z, p->n);
489 z += p->n;
490 break;
491 }
492 case 'S': {
493 int len;
494 SrcList *p = va_arg(ap, SrcList*);
495 int k = va_arg(ap, int);
496 assert( p->nSrc>k && k>=0 );
497 if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
498 len = strlen(p->a[k].zDatabase);
499 memcpy(z, p->a[k].zDatabase, len);
500 z += len;
501 *(z++) = '.';
502 }
503 len = strlen(p->a[k].zName);
504 memcpy(z, p->a[k].zName, len);
505 z += len;
506 break;
507 }
508 default: {
509 *(z++) = zFormat[i];
510 break;
511 }
512 }
513 }
514 va_end(ap);
515 if( i>j ){
516 memcpy(z, &zFormat[j], i-j);
517 z += i-j;
518 }
519 assert( (z - pParse->zErrMsg) < nByte );
520 *z = 0;
521}
522
523/*
drh982cef72000-05-30 16:27:03 +0000524** Convert an SQL-style quoted string into a normal string by removing
525** the quote characters. The conversion is done in-place. If the
526** input does not begin with a quote character, then this routine
527** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000528**
529** 2002-Feb-14: This routine is extended to remove MS-Access style
530** brackets from around identifers. For example: "[a-b-c]" becomes
531** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000532*/
533void sqliteDequote(char *z){
534 int quote;
535 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000536 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000537 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000538 switch( quote ){
539 case '\'': break;
540 case '"': break;
541 case '[': quote = ']'; break;
542 default: return;
543 }
drh982cef72000-05-30 16:27:03 +0000544 for(i=1, j=0; z[i]; i++){
545 if( z[i]==quote ){
546 if( z[i+1]==quote ){
547 z[j++] = quote;
548 i++;
549 }else{
550 z[j++] = 0;
551 break;
552 }
553 }else{
554 z[j++] = z[i];
555 }
556 }
557}
558
drh75897232000-05-29 14:26:00 +0000559/* An array to map all upper-case characters into their corresponding
560** lower-case character.
561*/
562static unsigned char UpperToLower[] = {
563 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
564 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
565 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
566 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
567 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
568 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
569 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
570 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
571 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
572 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
573 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
574 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
575 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
576 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
577 252,253,254,255
578};
579
580/*
581** This function computes a hash on the name of a keyword.
582** Case is not significant.
583*/
584int sqliteHashNoCase(const char *z, int n){
585 int h = 0;
drh75897232000-05-29 14:26:00 +0000586 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000587 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000588 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000589 n--;
drh75897232000-05-29 14:26:00 +0000590 }
591 if( h<0 ) h = -h;
592 return h;
593}
594
595/*
drh967e8b72000-06-21 13:59:10 +0000596** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000597** there is no consistency, we will define our own.
598*/
599int sqliteStrICmp(const char *zLeft, const char *zRight){
600 register unsigned char *a, *b;
601 a = (unsigned char *)zLeft;
602 b = (unsigned char *)zRight;
603 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
604 return *a - *b;
605}
606int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
607 register unsigned char *a, *b;
608 a = (unsigned char *)zLeft;
609 b = (unsigned char *)zRight;
610 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000611 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000612}
613
drha9e99ae2002-08-13 23:02:57 +0000614#if 0 /* NOT USED */
drh7a7c7392001-11-24 00:31:46 +0000615/*
616** The sortStrCmp() function below is used to order elements according
617** to the ORDER BY clause of a SELECT. The sort order is a little different
618** from what one might expect. This note attempts to describe what is
619** going on.
drh75897232000-05-29 14:26:00 +0000620**
621** We want the main string comparision function used for sorting to
622** sort both numbers and alphanumeric words into the correct sequence.
623** The same routine should do both without prior knowledge of which
624** type of text the input represents. It should even work for strings
drh7a7c7392001-11-24 00:31:46 +0000625** which are a mixture of text and numbers. (It does not work for
626** numeric substrings in exponential notation, however.)
drh75897232000-05-29 14:26:00 +0000627**
628** To accomplish this, we keep track of a state number while scanning
629** the two strings. The states are as follows:
630**
631** 1 Beginning of word
632** 2 Arbitrary text
633** 3 Integer
634** 4 Negative integer
635** 5 Real number
636** 6 Negative real
637**
638** The scan begins in state 1, beginning of word. Transitions to other
639** states are determined by characters seen, as shown in the following
640** chart:
641**
642** Current State Character Seen New State
643** -------------------- -------------- -------------------
644** 0 Beginning of word "-" 3 Negative integer
645** digit 2 Integer
646** space 0 Beginning of word
647** otherwise 1 Arbitrary text
648**
649** 1 Arbitrary text space 0 Beginning of word
650** digit 2 Integer
651** otherwise 1 Arbitrary text
652**
653** 2 Integer space 0 Beginning of word
654** "." 4 Real number
655** digit 2 Integer
656** otherwise 1 Arbitrary text
657**
658** 3 Negative integer space 0 Beginning of word
659** "." 5 Negative Real num
660** digit 3 Negative integer
661** otherwise 1 Arbitrary text
662**
663** 4 Real number space 0 Beginning of word
664** digit 4 Real number
665** otherwise 1 Arbitrary text
666**
667** 5 Negative real num space 0 Beginning of word
668** digit 5 Negative real num
669** otherwise 1 Arbitrary text
670**
671** To implement this state machine, we first classify each character
672** into on of the following categories:
673**
674** 0 Text
675** 1 Space
676** 2 Digit
677** 3 "-"
678** 4 "."
679**
680** Given an arbitrary character, the array charClass[] maps that character
681** into one of the atove categories.
682*/
683static const unsigned char charClass[] = {
684 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
685/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
686/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
687/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
688/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
689/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
690/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
691/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
692/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
693/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
694/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
695/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
696/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
697/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
698/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
699/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
700/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
701};
702#define N_CHAR_CLASS 5
703
704/*
705** Given the current state number (0 thru 5), this array figures
706** the new state number given the character class.
707*/
708static const unsigned char stateMachine[] = {
709 /* Text, Space, Digit, "-", "." */
710 1, 0, 2, 3, 1, /* State 0: Beginning of word */
711 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
712 1, 0, 2, 1, 4, /* State 2: Integer */
713 1, 0, 3, 1, 5, /* State 3: Negative integer */
714 1, 0, 4, 1, 1, /* State 4: Real number */
715 1, 0, 5, 1, 1, /* State 5: Negative real num */
716};
717
718/* This routine does a comparison of two strings. Case is used only
drh7a7c7392001-11-24 00:31:46 +0000719** if useCase!=0. Numeric substrings compare in numerical order for the
720** most part but this routine does not understand exponential notation.
drh75897232000-05-29 14:26:00 +0000721*/
drh7a7c7392001-11-24 00:31:46 +0000722static int sortStrCmp(const char *atext, const char *btext, int useCase){
drh75897232000-05-29 14:26:00 +0000723 register unsigned char *a, *b, *map, ca, cb;
724 int result;
725 register int cclass = 0;
726
727 a = (unsigned char *)atext;
728 b = (unsigned char *)btext;
729 if( useCase ){
730 do{
731 if( (ca= *a++)!=(cb= *b++) ) break;
732 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
733 }while( ca!=0 );
734 }else{
735 map = UpperToLower;
736 do{
737 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
738 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
739 }while( ca!=0 );
drh92086432002-01-22 14:11:29 +0000740 if( ca>='[' && ca<='`' ) cb = b[-1];
741 if( cb>='[' && cb<='`' ) ca = a[-1];
drh75897232000-05-29 14:26:00 +0000742 }
743 switch( cclass ){
744 case 0:
745 case 1: {
746 if( isdigit(ca) && isdigit(cb) ){
747 cclass = 2;
748 }
749 break;
750 }
751 default: {
752 break;
753 }
754 }
755 switch( cclass ){
756 case 2:
757 case 3: {
758 if( isdigit(ca) ){
759 if( isdigit(cb) ){
760 int acnt, bcnt;
761 acnt = bcnt = 0;
762 while( isdigit(*a++) ) acnt++;
763 while( isdigit(*b++) ) bcnt++;
764 result = acnt - bcnt;
765 if( result==0 ) result = ca-cb;
766 }else{
767 result = 1;
768 }
769 }else if( isdigit(cb) ){
770 result = -1;
771 }else if( ca=='.' ){
772 result = 1;
773 }else if( cb=='.' ){
774 result = -1;
775 }else{
776 result = ca - cb;
777 cclass = 2;
778 }
779 if( cclass==3 ) result = -result;
780 break;
781 }
782 case 0:
783 case 1:
784 case 4: {
785 result = ca - cb;
786 break;
787 }
788 case 5: {
789 result = cb - ca;
790 };
791 }
792 return result;
793}
drha9e99ae2002-08-13 23:02:57 +0000794#endif /* NOT USED */
drh75897232000-05-29 14:26:00 +0000795
drha5c2ad02000-09-14 01:21:10 +0000796/*
drh7a7c7392001-11-24 00:31:46 +0000797** Return TRUE if z is a pure numeric string. Return FALSE if the
798** string contains any character which is not part of a number.
799**
800** Am empty string is considered numeric.
drha5c2ad02000-09-14 01:21:10 +0000801*/
drhe6840902002-03-06 03:08:25 +0000802static int sqliteIsNumber(const char *z){
drh7a7c7392001-11-24 00:31:46 +0000803 if( *z=='-' || *z=='+' ) z++;
804 if( !isdigit(*z) ){
805 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000806 }
drh7a7c7392001-11-24 00:31:46 +0000807 z++;
808 while( isdigit(*z) ){ z++; }
809 if( *z=='.' ){
810 z++;
811 if( !isdigit(*z) ) return 0;
812 while( isdigit(*z) ){ z++; }
813 if( *z=='e' || *z=='E' ){
814 z++;
815 if( *z=='+' || *z=='-' ) z++;
816 if( !isdigit(*z) ) return 0;
817 while( isdigit(*z) ){ z++; }
818 }
drha5c2ad02000-09-14 01:21:10 +0000819 }
drh7a7c7392001-11-24 00:31:46 +0000820 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000821}
822
drh75897232000-05-29 14:26:00 +0000823/* This comparison routine is what we use for comparison operations
drha9e99ae2002-08-13 23:02:57 +0000824** between numeric values in an SQL expression. "Numeric" is a little
825** bit misleading here. What we mean is that the strings have a
826** type of "numeric" from the point of view of SQL. The strings
827** do not necessarily contain numbers. They could contain text.
drh7a7c7392001-11-24 00:31:46 +0000828**
drha9e99ae2002-08-13 23:02:57 +0000829** If the input strings both look like actual numbers then they
830** compare in numerical order. Numerical strings are always less
831** than non-numeric strings so if one input string looks like a
832** number and the other does not, then the one that looks like
833** a number is the smaller. Non-numeric strings compare in
834** lexigraphical order (the same order as strcmp()).
drh75897232000-05-29 14:26:00 +0000835*/
836int sqliteCompare(const char *atext, const char *btext){
837 int result;
drh0bce8352002-02-28 00:41:10 +0000838 int isNumA, isNumB;
839 if( atext==0 ){
drh8912d102002-05-26 21:34:58 +0000840 return -1;
drh0bce8352002-02-28 00:41:10 +0000841 }else if( btext==0 ){
842 return 1;
843 }
drhe6840902002-03-06 03:08:25 +0000844 isNumA = sqliteIsNumber(atext);
845 isNumB = sqliteIsNumber(btext);
drh7a7c7392001-11-24 00:31:46 +0000846 if( isNumA ){
847 if( !isNumB ){
848 result = -1;
849 }else{
850 double rA, rB;
851 rA = atof(atext);
852 rB = atof(btext);
853 if( rA<rB ){
854 result = -1;
855 }else if( rA>rB ){
856 result = +1;
857 }else{
858 result = 0;
drha5c2ad02000-09-14 01:21:10 +0000859 }
860 }
drh7a7c7392001-11-24 00:31:46 +0000861 }else if( isNumB ){
862 result = +1;
863 }else {
864 result = strcmp(atext, btext);
drha5c2ad02000-09-14 01:21:10 +0000865 }
drh7a7c7392001-11-24 00:31:46 +0000866 return result;
drh75897232000-05-29 14:26:00 +0000867}
drh75897232000-05-29 14:26:00 +0000868
869/*
drh16e59552000-07-31 11:57:37 +0000870** This routine is used for sorting. Each key is a list of one or more
drha9e99ae2002-08-13 23:02:57 +0000871** null-terminated elements. The list is terminated by two nulls in
872** a row. For example, the following text is a key with three elements
drh75897232000-05-29 14:26:00 +0000873**
drha9e99ae2002-08-13 23:02:57 +0000874** Aone\000Dtwo\000Athree\000\000
drh75897232000-05-29 14:26:00 +0000875**
drhda30d362002-08-26 19:55:07 +0000876** All elements begin with one of the characters "+-AD" and end with "\000"
877** with zero or more text elements in between. Except, NULL elements
878** consist of the special two-character sequence "N\000".
879**
drha9e99ae2002-08-13 23:02:57 +0000880** Both arguments will have the same number of elements. This routine
drh75897232000-05-29 14:26:00 +0000881** returns negative, zero, or positive if the first argument is less
882** than, equal to, or greater than the first. (Result is a-b).
883**
drha9e99ae2002-08-13 23:02:57 +0000884** Each element begins with one of the characters "+", "-", "A", "D".
drh38640e12002-07-05 21:42:36 +0000885** This character determines the sort order and collating sequence:
drh7a7c7392001-11-24 00:31:46 +0000886**
drh38640e12002-07-05 21:42:36 +0000887** + Sort numerically in ascending order
888** - Sort numerically in descending order
889** A Sort as strings in ascending order
890** D Sort as strings in descending order.
891**
892** For the "+" and "-" sorting, pure numeric strings (strings for which the
drh7a7c7392001-11-24 00:31:46 +0000893** isNum() function above returns TRUE) always compare less than strings
drha9e99ae2002-08-13 23:02:57 +0000894** that are not pure numerics. Non-numeric strings compare in memcmp()
895** order. This is the same sort order as the sqliteCompare() function
896** above generates.
drh7a7c7392001-11-24 00:31:46 +0000897**
drha9e99ae2002-08-13 23:02:57 +0000898** The last point is a change from version 2.6.3 to version 2.7.0. In
899** version 2.6.3 and earlier, substrings of digits compare in numerical
900** and case was used only to break a tie.
901**
902** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
903** of whether or not they look like a number.
904**
905** Note that the sort order imposed by the rules above is the same
drh7a7c7392001-11-24 00:31:46 +0000906** from the ordering defined by the "<", "<=", ">", and ">=" operators
drha9e99ae2002-08-13 23:02:57 +0000907** of expressions and for indices. This was not the case for version
908** 2.6.3 and earlier.
drh75897232000-05-29 14:26:00 +0000909*/
910int sqliteSortCompare(const char *a, const char *b){
911 int len;
912 int res = 0;
drh7a7c7392001-11-24 00:31:46 +0000913 int isNumA, isNumB;
drh294fb922002-09-30 01:31:21 +0000914 int dir = 0;
drh75897232000-05-29 14:26:00 +0000915
916 while( res==0 && *a && *b ){
drhda30d362002-08-26 19:55:07 +0000917 if( a[0]=='N' || b[0]=='N' ){
918 if( a[0]==b[0] ){
919 a += 2;
920 b += 2;
921 continue;
922 }
923 if( a[0]=='N' ){
924 dir = b[0];
925 res = -1;
926 }else{
927 dir = a[0];
928 res = +1;
929 }
drhf570f012002-05-31 15:51:25 +0000930 break;
931 }
drhda30d362002-08-26 19:55:07 +0000932 assert( a[0]==b[0] );
933 if( (dir=a[0])=='A' || a[0]=='D' ){
drh38640e12002-07-05 21:42:36 +0000934 res = strcmp(&a[1],&b[1]);
935 if( res ) break;
936 }else{
937 isNumA = sqliteIsNumber(&a[1]);
938 isNumB = sqliteIsNumber(&b[1]);
939 if( isNumA ){
940 double rA, rB;
941 if( !isNumB ){
942 res = -1;
943 break;
944 }
945 rA = atof(&a[1]);
946 rB = atof(&b[1]);
947 if( rA<rB ){
948 res = -1;
949 break;
950 }
951 if( rA>rB ){
952 res = +1;
953 break;
954 }
955 }else if( isNumB ){
drh7a7c7392001-11-24 00:31:46 +0000956 res = +1;
957 break;
drh38640e12002-07-05 21:42:36 +0000958 }else{
drha9e99ae2002-08-13 23:02:57 +0000959 res = strcmp(&a[1],&b[1]);
960 if( res ) break;
drh7a7c7392001-11-24 00:31:46 +0000961 }
drh75897232000-05-29 14:26:00 +0000962 }
drh7a7c7392001-11-24 00:31:46 +0000963 len = strlen(&a[1]) + 2;
964 a += len;
965 b += len;
drh75897232000-05-29 14:26:00 +0000966 }
drhda30d362002-08-26 19:55:07 +0000967 if( dir=='-' || dir=='D' ) res = -res;
drh75897232000-05-29 14:26:00 +0000968 return res;
969}
drhdce2cbe2000-05-31 02:27:49 +0000970
drh9bbca4c2001-11-06 04:00:18 +0000971/*
drh7a7c7392001-11-24 00:31:46 +0000972** Some powers of 64. These constants are needed in the
973** sqliteRealToSortable() routine below.
drh9bbca4c2001-11-06 04:00:18 +0000974*/
975#define _64e3 (64.0 * 64.0 * 64.0)
976#define _64e4 (64.0 * 64.0 * 64.0 * 64.0)
977#define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
978#define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
979#define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
980#define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
981
982/*
983** The following procedure converts a double-precision floating point
984** number into a string. The resulting string has the property that
985** two such strings comparied using strcmp() or memcmp() will give the
drh5a2c2c22001-11-21 02:21:11 +0000986** same results as a numeric comparison of the original floating point
987** numbers.
drh9bbca4c2001-11-06 04:00:18 +0000988**
989** This routine is used to generate database keys from floating point
990** numbers such that the keys sort in the same order as the original
991** floating point numbers even though the keys are compared using
992** memcmp().
993**
994** The calling function should have allocated at least 14 characters
995** of space for the buffer z[].
996*/
997void sqliteRealToSortable(double r, char *z){
998 int neg;
999 int exp;
1000 int cnt = 0;
1001
1002 /* This array maps integers between 0 and 63 into base-64 digits.
1003 ** The digits must be chosen such at their ASCII codes are increasing.
1004 ** This means we can not use the traditional base-64 digit set. */
1005 static const char zDigit[] =
1006 "0123456789"
1007 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1008 "abcdefghijklmnopqrstuvwxyz"
1009 "|~";
1010 if( r<0.0 ){
1011 neg = 1;
1012 r = -r;
1013 *z++ = '-';
1014 } else {
1015 neg = 0;
1016 *z++ = '0';
1017 }
1018 exp = 0;
1019
1020 if( r==0.0 ){
1021 exp = -1024;
1022 }else if( r<(0.5/64.0) ){
1023 while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
1024 while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
1025 while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
1026 while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
1027 }else if( r>=0.5 ){
1028 while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
1029 while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
1030 while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
1031 while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
1032 }
1033 if( neg ){
1034 exp = -exp;
1035 r = -r;
1036 }
1037 exp += 1024;
1038 r += 0.5;
1039 if( exp<0 ) return;
1040 if( exp>=2048 || r>=1.0 ){
1041 strcpy(z, "~~~~~~~~~~~~");
1042 return;
1043 }
1044 *z++ = zDigit[(exp>>6)&0x3f];
1045 *z++ = zDigit[exp & 0x3f];
1046 while( r>0.0 && cnt<10 ){
1047 int digit;
1048 r *= 64.0;
drh1ab43002002-01-14 09:28:19 +00001049 digit = (int)r;
drh9bbca4c2001-11-06 04:00:18 +00001050 assert( digit>=0 && digit<64 );
1051 *z++ = zDigit[digit & 0x3f];
1052 r -= digit;
1053 cnt++;
1054 }
1055 *z = 0;
1056}
1057
drh297ecf12001-04-05 15:57:13 +00001058#ifdef SQLITE_UTF8
drhdce2cbe2000-05-31 02:27:49 +00001059/*
drh297ecf12001-04-05 15:57:13 +00001060** X is a pointer to the first byte of a UTF-8 character. Increment
1061** X so that it points to the next character. This only works right
1062** if X points to a well-formed UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +00001063*/
drh297ecf12001-04-05 15:57:13 +00001064#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
1065#define sqliteCharVal(X) sqlite_utf8_to_int(X)
drhe17a7e32001-04-04 21:10:18 +00001066
drh297ecf12001-04-05 15:57:13 +00001067#else /* !defined(SQLITE_UTF8) */
drhe17a7e32001-04-04 21:10:18 +00001068/*
drh297ecf12001-04-05 15:57:13 +00001069** For iso8859 encoding, the next character is just the next byte.
drhe17a7e32001-04-04 21:10:18 +00001070*/
drh297ecf12001-04-05 15:57:13 +00001071#define sqliteNextChar(X) (++(X));
1072#define sqliteCharVal(X) ((int)*(X))
drhe17a7e32001-04-04 21:10:18 +00001073
drh297ecf12001-04-05 15:57:13 +00001074#endif /* defined(SQLITE_UTF8) */
1075
1076
1077#ifdef SQLITE_UTF8
drhe17a7e32001-04-04 21:10:18 +00001078/*
drh297ecf12001-04-05 15:57:13 +00001079** Convert the UTF-8 character to which z points into a 31-bit
1080** UCS character. This only works right if z points to a well-formed
1081** UTF-8 string.
drhe17a7e32001-04-04 21:10:18 +00001082*/
drh297ecf12001-04-05 15:57:13 +00001083static int sqlite_utf8_to_int(const unsigned char *z){
drhe17a7e32001-04-04 21:10:18 +00001084 int c;
drh297ecf12001-04-05 15:57:13 +00001085 static const int initVal[] = {
1086 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1087 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1088 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
1089 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
1090 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
1091 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
1092 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
1093 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
1094 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
1095 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
1096 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
1097 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
1098 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2,
1099 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
1100 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
1101 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1102 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254,
1103 255,
1104 };
1105 c = initVal[*(z++)];
1106 while( (0xc0&*z)==0x80 ){
1107 c = (c<<6) | (0x3f&*(z++));
drhe17a7e32001-04-04 21:10:18 +00001108 }
1109 return c;
1110}
drh297ecf12001-04-05 15:57:13 +00001111#endif
drhe17a7e32001-04-04 21:10:18 +00001112
1113/*
1114** Compare two UTF-8 strings for equality where the first string can
drhdce2cbe2000-05-31 02:27:49 +00001115** potentially be a "glob" expression. Return true (1) if they
1116** are the same and false (0) if they are different.
1117**
1118** Globbing rules:
1119**
1120** '*' Matches any sequence of zero or more characters.
1121**
1122** '?' Matches exactly one character.
1123**
1124** [...] Matches one character from the enclosed list of
1125** characters.
1126**
1127** [^...] Matches one character not in the enclosed list.
1128**
1129** With the [...] and [^...] matching, a ']' character can be included
1130** in the list by making it the first character after '[' or '^'. A
1131** range of characters can be specified using '-'. Example:
1132** "[a-z]" matches any single lower-case letter. To match a '-', make
1133** it the last character in the list.
1134**
1135** This routine is usually quick, but can be N**2 in the worst case.
1136**
1137** Hints: to match '*' or '?', put them in "[]". Like this:
1138**
1139** abc[*]xyz Matches "abc*xyz" only
1140*/
drhe17a7e32001-04-04 21:10:18 +00001141int
1142sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
1143 register int c;
drhdce2cbe2000-05-31 02:27:49 +00001144 int invert;
1145 int seen;
drhe17a7e32001-04-04 21:10:18 +00001146 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001147
1148 while( (c = *zPattern)!=0 ){
1149 switch( c ){
1150 case '*':
drhe17a7e32001-04-04 21:10:18 +00001151 while( (c=zPattern[1]) == '*' || c == '?' ){
1152 if( c=='?' ){
1153 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001154 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001155 }
1156 zPattern++;
1157 }
1158 if( c==0 ) return 1;
drhe17a7e32001-04-04 21:10:18 +00001159 if( c=='[' ){
drhdce2cbe2000-05-31 02:27:49 +00001160 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
drh297ecf12001-04-05 15:57:13 +00001161 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001162 }
1163 return *zString!=0;
1164 }else{
1165 while( (c2 = *zString)!=0 ){
1166 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +00001167 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +00001168 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001169 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001170 }
1171 return 0;
1172 }
drhe17a7e32001-04-04 21:10:18 +00001173 case '?': {
drhdce2cbe2000-05-31 02:27:49 +00001174 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001175 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001176 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001177 break;
drhe17a7e32001-04-04 21:10:18 +00001178 }
1179 case '[': {
1180 int prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001181 seen = 0;
1182 invert = 0;
drh297ecf12001-04-05 15:57:13 +00001183 c = sqliteCharVal(zString);
drhdce2cbe2000-05-31 02:27:49 +00001184 if( c==0 ) return 0;
1185 c2 = *++zPattern;
1186 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1187 if( c2==']' ){
1188 if( c==']' ) seen = 1;
1189 c2 = *++zPattern;
1190 }
drh297ecf12001-04-05 15:57:13 +00001191 while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
drhe17a7e32001-04-04 21:10:18 +00001192 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1193 zPattern++;
drh297ecf12001-04-05 15:57:13 +00001194 c2 = sqliteCharVal(zPattern);
drhe17a7e32001-04-04 21:10:18 +00001195 if( c>=prior_c && c<=c2 ) seen = 1;
1196 prior_c = 0;
drhdce2cbe2000-05-31 02:27:49 +00001197 }else if( c==c2 ){
1198 seen = 1;
drhe17a7e32001-04-04 21:10:18 +00001199 prior_c = c2;
1200 }else{
1201 prior_c = c2;
drhdce2cbe2000-05-31 02:27:49 +00001202 }
drh297ecf12001-04-05 15:57:13 +00001203 sqliteNextChar(zPattern);
drhdce2cbe2000-05-31 02:27:49 +00001204 }
1205 if( c2==0 || (seen ^ invert)==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001206 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001207 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001208 break;
drhe17a7e32001-04-04 21:10:18 +00001209 }
1210 default: {
drhdce2cbe2000-05-31 02:27:49 +00001211 if( c != *zString ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001212 zPattern++;
1213 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001214 break;
drhe17a7e32001-04-04 21:10:18 +00001215 }
drhdce2cbe2000-05-31 02:27:49 +00001216 }
drhdce2cbe2000-05-31 02:27:49 +00001217 }
1218 return *zString==0;
1219}
1220
1221/*
drhe17a7e32001-04-04 21:10:18 +00001222** Compare two UTF-8 strings for equality using the "LIKE" operator of
drhdce2cbe2000-05-31 02:27:49 +00001223** SQL. The '%' character matches any sequence of 0 or more
1224** characters and '_' matches any single character. Case is
1225** not significant.
1226**
1227** This routine is just an adaptation of the sqliteGlobCompare()
1228** routine above.
1229*/
1230int
1231sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
drhe17a7e32001-04-04 21:10:18 +00001232 register int c;
1233 int c2;
drhdce2cbe2000-05-31 02:27:49 +00001234
1235 while( (c = UpperToLower[*zPattern])!=0 ){
1236 switch( c ){
drhe17a7e32001-04-04 21:10:18 +00001237 case '%': {
1238 while( (c=zPattern[1]) == '%' || c == '_' ){
1239 if( c=='_' ){
1240 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001241 sqliteNextChar(zString);
drhdce2cbe2000-05-31 02:27:49 +00001242 }
drhe17a7e32001-04-04 21:10:18 +00001243 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001244 }
drhe17a7e32001-04-04 21:10:18 +00001245 if( c==0 ) return 1;
1246 c = UpperToLower[c];
1247 while( (c2=UpperToLower[*zString])!=0 ){
1248 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1249 if( c2==0 ) return 0;
1250 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
drh297ecf12001-04-05 15:57:13 +00001251 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001252 }
1253 return 0;
1254 }
1255 case '_': {
drhdce2cbe2000-05-31 02:27:49 +00001256 if( *zString==0 ) return 0;
drh297ecf12001-04-05 15:57:13 +00001257 sqliteNextChar(zString);
drhe17a7e32001-04-04 21:10:18 +00001258 zPattern++;
drhdce2cbe2000-05-31 02:27:49 +00001259 break;
drhe17a7e32001-04-04 21:10:18 +00001260 }
1261 default: {
drhdce2cbe2000-05-31 02:27:49 +00001262 if( c != UpperToLower[*zString] ) return 0;
drhe17a7e32001-04-04 21:10:18 +00001263 zPattern++;
1264 zString++;
drhdce2cbe2000-05-31 02:27:49 +00001265 break;
drhe17a7e32001-04-04 21:10:18 +00001266 }
drhdce2cbe2000-05-31 02:27:49 +00001267 }
drhdce2cbe2000-05-31 02:27:49 +00001268 }
1269 return *zString==0;
1270}
drh5e00f6c2001-09-13 13:46:56 +00001271
1272/*
drhc22bd472002-05-10 13:14:07 +00001273** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1274** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1275** when this routine is called.
1276**
1277** This routine is a attempt to detect if two threads use the
1278** same sqlite* pointer at the same time. There is a race
1279** condition so it is possible that the error is not detected.
1280** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +00001281** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +00001282** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +00001283**
1284** Ticket #202: If db->magic is not a valid open value, take care not
1285** to modify the db structure at all. It could be that db is a stale
1286** pointer. In other words, it could be that there has been a prior
1287** call to sqlite_close(db) and db has been deallocated. And we do
1288** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +00001289*/
drhc22bd472002-05-10 13:14:07 +00001290int sqliteSafetyOn(sqlite *db){
1291 if( db->magic==SQLITE_MAGIC_OPEN ){
1292 db->magic = SQLITE_MAGIC_BUSY;
1293 return 0;
drh94e92032003-02-16 22:21:32 +00001294 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1295 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001296 db->magic = SQLITE_MAGIC_ERROR;
1297 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +00001298 }
drhe7e8bc72002-12-17 13:05:25 +00001299 return 1;
drhc22bd472002-05-10 13:14:07 +00001300}
1301
1302/*
1303** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1304** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1305** when this routine is called.
1306*/
1307int sqliteSafetyOff(sqlite *db){
1308 if( db->magic==SQLITE_MAGIC_BUSY ){
1309 db->magic = SQLITE_MAGIC_OPEN;
1310 return 0;
drh94e92032003-02-16 22:21:32 +00001311 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1312 || db->want_to_close ){
drhc22bd472002-05-10 13:14:07 +00001313 db->magic = SQLITE_MAGIC_ERROR;
1314 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +00001315 }
drhe7e8bc72002-12-17 13:05:25 +00001316 return 1;
drhc22bd472002-05-10 13:14:07 +00001317}
1318
1319/*
1320** Check to make sure we are not currently executing an sqlite_exec().
1321** If we are currently in an sqlite_exec(), return true and set
1322** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete
1323** shutdown of the database.
1324**
1325** This routine is used to try to detect when API routines are called
1326** at the wrong time or in the wrong sequence.
1327*/
1328int sqliteSafetyCheck(sqlite *db){
drh326dce72003-01-29 14:06:07 +00001329 if( db->pVdbe!=0 ){
drhc22bd472002-05-10 13:14:07 +00001330 db->magic = SQLITE_MAGIC_ERROR;
1331 return 1;
1332 }
1333 return 0;
drh5e00f6c2001-09-13 13:46:56 +00001334}