xref: /sqlite-3.40.0/src/util.c (revision c023e03e)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    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.
10 **
11 *************************************************************************
12 ** Utility functions used throughout sqlite.
13 **
14 ** This file contains functions for allocating memory, comparing
15 ** strings, and stuff like that.
16 **
17 ** $Id: util.c,v 1.67 2003/08/26 11:29:08 drh Exp $
18 */
19 #include "sqliteInt.h"
20 #include <stdarg.h>
21 #include <ctype.h>
22 
23 /*
24 ** If malloc() ever fails, this global variable gets set to 1.
25 ** This causes the library to abort and never again function.
26 */
27 int sqlite_malloc_failed = 0;
28 
29 /*
30 ** If MEMORY_DEBUG is defined, then use versions of malloc() and
31 ** free() that track memory usage and check for buffer overruns.
32 */
33 #ifdef MEMORY_DEBUG
34 
35 /*
36 ** For keeping track of the number of mallocs and frees.   This
37 ** is used to check for memory leaks.
38 */
39 int sqlite_nMalloc;         /* Number of sqliteMalloc() calls */
40 int sqlite_nFree;           /* Number of sqliteFree() calls */
41 int sqlite_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
42 #if MEMORY_DEBUG>1
43 static int memcnt = 0;
44 #endif
45 
46 /*
47 ** Number of 32-bit guard words
48 */
49 #define N_GUARD 1
50 
51 /*
52 ** Allocate new memory and set it to zero.  Return NULL if
53 ** no memory is available.
54 */
55 void *sqliteMalloc_(int n, int bZero, char *zFile, int line){
56   void *p;
57   int *pi;
58   int i, k;
59   if( sqlite_iMallocFail>=0 ){
60     sqlite_iMallocFail--;
61     if( sqlite_iMallocFail==0 ){
62       sqlite_malloc_failed++;
63 #if MEMORY_DEBUG>1
64       fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
65               n, zFile,line);
66 #endif
67       sqlite_iMallocFail--;
68       return 0;
69     }
70   }
71   if( n==0 ) return 0;
72   k = (n+sizeof(int)-1)/sizeof(int);
73   pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
74   if( pi==0 ){
75     sqlite_malloc_failed++;
76     return 0;
77   }
78   sqlite_nMalloc++;
79   for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
80   pi[N_GUARD] = n;
81   for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
82   p = &pi[N_GUARD+1];
83   memset(p, bZero==0, n);
84 #if MEMORY_DEBUG>1
85   fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
86       ++memcnt, n, (int)p, zFile,line);
87 #endif
88   return p;
89 }
90 
91 /*
92 ** Check to see if the given pointer was obtained from sqliteMalloc()
93 ** and is able to hold at least N bytes.  Raise an exception if this
94 ** is not the case.
95 **
96 ** This routine is used for testing purposes only.
97 */
98 void sqliteCheckMemory(void *p, int N){
99   int *pi = p;
100   int n, i, k;
101   pi -= N_GUARD+1;
102   for(i=0; i<N_GUARD; i++){
103     assert( pi[i]==0xdead1122 );
104   }
105   n = pi[N_GUARD];
106   assert( N>=0 && N<n );
107   k = (n+sizeof(int)-1)/sizeof(int);
108   for(i=0; i<N_GUARD; i++){
109     assert( pi[k+N_GUARD+1+i]==0xdead3344 );
110   }
111 }
112 
113 /*
114 ** Free memory previously obtained from sqliteMalloc()
115 */
116 void sqliteFree_(void *p, char *zFile, int line){
117   if( p ){
118     int *pi, i, k, n;
119     pi = p;
120     pi -= N_GUARD+1;
121     sqlite_nFree++;
122     for(i=0; i<N_GUARD; i++){
123       if( pi[i]!=0xdead1122 ){
124         fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
125         return;
126       }
127     }
128     n = pi[N_GUARD];
129     k = (n+sizeof(int)-1)/sizeof(int);
130     for(i=0; i<N_GUARD; i++){
131       if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
132         fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
133         return;
134       }
135     }
136     memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
137 #if MEMORY_DEBUG>1
138     fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
139          ++memcnt, n, (int)p, zFile,line);
140 #endif
141     free(pi);
142   }
143 }
144 
145 /*
146 ** Resize a prior allocation.  If p==0, then this routine
147 ** works just like sqliteMalloc().  If n==0, then this routine
148 ** works just like sqliteFree().
149 */
150 void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
151   int *oldPi, *pi, i, k, oldN, oldK;
152   void *p;
153   if( oldP==0 ){
154     return sqliteMalloc_(n,1,zFile,line);
155   }
156   if( n==0 ){
157     sqliteFree_(oldP,zFile,line);
158     return 0;
159   }
160   oldPi = oldP;
161   oldPi -= N_GUARD+1;
162   if( oldPi[0]!=0xdead1122 ){
163     fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
164     return 0;
165   }
166   oldN = oldPi[N_GUARD];
167   oldK = (oldN+sizeof(int)-1)/sizeof(int);
168   for(i=0; i<N_GUARD; i++){
169     if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
170       fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
171               (int)oldP);
172       return 0;
173     }
174   }
175   k = (n + sizeof(int) - 1)/sizeof(int);
176   pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
177   if( pi==0 ){
178     sqlite_malloc_failed++;
179     return 0;
180   }
181   for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
182   pi[N_GUARD] = n;
183   for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
184   p = &pi[N_GUARD+1];
185   memcpy(p, oldP, n>oldN ? oldN : n);
186   if( n>oldN ){
187     memset(&((char*)p)[oldN], 0, n-oldN);
188   }
189   memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
190   free(oldPi);
191 #if MEMORY_DEBUG>1
192   fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
193     ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
194 #endif
195   return p;
196 }
197 
198 /*
199 ** Make a duplicate of a string into memory obtained from malloc()
200 ** Free the original string using sqliteFree().
201 **
202 ** This routine is called on all strings that are passed outside of
203 ** the SQLite library.  That way clients can free the string using free()
204 ** rather than having to call sqliteFree().
205 */
206 void sqliteStrRealloc(char **pz){
207   char *zNew;
208   if( pz==0 || *pz==0 ) return;
209   zNew = malloc( strlen(*pz) + 1 );
210   if( zNew==0 ){
211     sqlite_malloc_failed++;
212     sqliteFree(*pz);
213     *pz = 0;
214   }
215   strcpy(zNew, *pz);
216   sqliteFree(*pz);
217   *pz = zNew;
218 }
219 
220 /*
221 ** Make a copy of a string in memory obtained from sqliteMalloc()
222 */
223 char *sqliteStrDup_(const char *z, char *zFile, int line){
224   char *zNew;
225   if( z==0 ) return 0;
226   zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line);
227   if( zNew ) strcpy(zNew, z);
228   return zNew;
229 }
230 char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
231   char *zNew;
232   if( z==0 ) return 0;
233   zNew = sqliteMalloc_(n+1, 0, zFile, line);
234   if( zNew ){
235     memcpy(zNew, z, n);
236     zNew[n] = 0;
237   }
238   return zNew;
239 }
240 #endif /* MEMORY_DEBUG */
241 
242 /*
243 ** The following versions of malloc() and free() are for use in a
244 ** normal build.
245 */
246 #if !defined(MEMORY_DEBUG)
247 
248 /*
249 ** Allocate new memory and set it to zero.  Return NULL if
250 ** no memory is available.  See also sqliteMallocRaw().
251 */
252 void *sqliteMalloc(int n){
253   void *p;
254   if( n==0 ) return 0;
255   p = malloc(n);
256   if( p==0 ){
257     sqlite_malloc_failed++;
258     return 0;
259   }
260   memset(p, 0, n);
261   return p;
262 }
263 
264 /*
265 ** Allocate new memory but do not set it to zero.  Return NULL if
266 ** no memory is available.  See also sqliteMalloc().
267 */
268 void *sqliteMallocRaw(int n){
269   void *p;
270   if( n==0 ) return 0;
271   p = malloc(n);
272   if( p==0 ){
273     sqlite_malloc_failed++;
274     return 0;
275   }
276   return p;
277 }
278 
279 /*
280 ** Free memory previously obtained from sqliteMalloc()
281 */
282 void sqliteFree(void *p){
283   if( p ){
284     free(p);
285   }
286 }
287 
288 /*
289 ** Resize a prior allocation.  If p==0, then this routine
290 ** works just like sqliteMalloc().  If n==0, then this routine
291 ** works just like sqliteFree().
292 */
293 void *sqliteRealloc(void *p, int n){
294   void *p2;
295   if( p==0 ){
296     return sqliteMalloc(n);
297   }
298   if( n==0 ){
299     sqliteFree(p);
300     return 0;
301   }
302   p2 = realloc(p, n);
303   if( p2==0 ){
304     sqlite_malloc_failed++;
305   }
306   return p2;
307 }
308 
309 /*
310 ** Make a copy of a string in memory obtained from sqliteMalloc()
311 */
312 char *sqliteStrDup(const char *z){
313   char *zNew;
314   if( z==0 ) return 0;
315   zNew = sqliteMallocRaw(strlen(z)+1);
316   if( zNew ) strcpy(zNew, z);
317   return zNew;
318 }
319 char *sqliteStrNDup(const char *z, int n){
320   char *zNew;
321   if( z==0 ) return 0;
322   zNew = sqliteMallocRaw(n+1);
323   if( zNew ){
324     memcpy(zNew, z, n);
325     zNew[n] = 0;
326   }
327   return zNew;
328 }
329 #endif /* !defined(MEMORY_DEBUG) */
330 
331 /*
332 ** Create a string from the 2nd and subsequent arguments (up to the
333 ** first NULL argument), store the string in memory obtained from
334 ** sqliteMalloc() and make the pointer indicated by the 1st argument
335 ** point to that string.  The 1st argument must either be NULL or
336 ** point to memory obtained from sqliteMalloc().
337 */
338 void sqliteSetString(char **pz, const char *zFirst, ...){
339   va_list ap;
340   int nByte;
341   const char *z;
342   char *zResult;
343 
344   if( pz==0 ) return;
345   nByte = strlen(zFirst) + 1;
346   va_start(ap, zFirst);
347   while( (z = va_arg(ap, const char*))!=0 ){
348     nByte += strlen(z);
349   }
350   va_end(ap);
351   sqliteFree(*pz);
352   *pz = zResult = sqliteMallocRaw( nByte );
353   if( zResult==0 ){
354     return;
355   }
356   strcpy(zResult, zFirst);
357   zResult += strlen(zResult);
358   va_start(ap, zFirst);
359   while( (z = va_arg(ap, const char*))!=0 ){
360     strcpy(zResult, z);
361     zResult += strlen(zResult);
362   }
363   va_end(ap);
364 #ifdef MEMORY_DEBUG
365 #if MEMORY_DEBUG>1
366   fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
367 #endif
368 #endif
369 }
370 
371 /*
372 ** Works like sqliteSetString, but each string is now followed by
373 ** a length integer which specifies how much of the source string
374 ** to copy (in bytes).  -1 means use the whole string.  The 1st
375 ** argument must either be NULL or point to memory obtained from
376 ** sqliteMalloc().
377 */
378 void sqliteSetNString(char **pz, ...){
379   va_list ap;
380   int nByte;
381   const char *z;
382   char *zResult;
383   int n;
384 
385   if( pz==0 ) return;
386   nByte = 0;
387   va_start(ap, pz);
388   while( (z = va_arg(ap, const char*))!=0 ){
389     n = va_arg(ap, int);
390     if( n<=0 ) n = strlen(z);
391     nByte += n;
392   }
393   va_end(ap);
394   sqliteFree(*pz);
395   *pz = zResult = sqliteMallocRaw( nByte + 1 );
396   if( zResult==0 ) return;
397   va_start(ap, pz);
398   while( (z = va_arg(ap, const char*))!=0 ){
399     n = va_arg(ap, int);
400     if( n<=0 ) n = strlen(z);
401     strncpy(zResult, z, n);
402     zResult += n;
403   }
404   *zResult = 0;
405 #ifdef MEMORY_DEBUG
406 #if MEMORY_DEBUG>1
407   fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
408 #endif
409 #endif
410   va_end(ap);
411 }
412 
413 /*
414 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
415 ** The following formatting characters are allowed:
416 **
417 **      %s      Insert a string
418 **      %z      A string that should be freed after use
419 **      %d      Insert an integer
420 **      %T      Insert a token
421 **      %S      Insert the first element of a SrcList
422 */
423 void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){
424   va_list ap;
425   int nByte;
426   int i, j;
427   char *z;
428   static char zNull[] = "NULL";
429 
430   pParse->nErr++;
431   nByte = 1 + strlen(zFormat);
432   va_start(ap, zFormat);
433   for(i=0; zFormat[i]; i++){
434     if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue;
435     i++;
436     switch( zFormat[i] ){
437       case 'd': {
438         (void)va_arg(ap, int);
439         nByte += 20;
440         break;
441       }
442       case 'z':
443       case 's': {
444         char *z2 = va_arg(ap, char*);
445         if( z2==0 ) z2 = zNull;
446         nByte += strlen(z2);
447         break;
448       }
449       case 'T': {
450         Token *p = va_arg(ap, Token*);
451         nByte += p->n;
452         break;
453       }
454       case 'S': {
455         SrcList *p = va_arg(ap, SrcList*);
456         int k = va_arg(ap, int);
457         assert( p->nSrc>k && k>=0 );
458         nByte += strlen(p->a[k].zName);
459         if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
460           nByte += strlen(p->a[k].zDatabase)+1;
461         }
462         break;
463       }
464       default: {
465         nByte++;
466         break;
467       }
468     }
469   }
470   va_end(ap);
471   z = sqliteMalloc( nByte );
472   if( z==0 ) return;
473   sqliteFree(pParse->zErrMsg);
474   pParse->zErrMsg = z;
475   va_start(ap, zFormat);
476   for(i=j=0; zFormat[i]; i++){
477     if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue;
478     if( i>j ){
479       memcpy(z, &zFormat[j], i-j);
480       z += i-j;
481     }
482     j = i+2;
483     i++;
484     switch( zFormat[i] ){
485       case 'd': {
486         int x = va_arg(ap, int);
487         sprintf(z, "%d", x);
488         z += strlen(z);
489         break;
490       }
491       case 'z':
492       case 's': {
493         int len;
494         char *z2 = va_arg(ap, char*);
495         if( z2==0 ) z2 = zNull;
496         len = strlen(z2);
497         memcpy(z, z2, len);
498         z += len;
499         if( zFormat[i]=='z' && z2!=zNull ){
500           sqliteFree(z2);
501         }
502         break;
503       }
504       case 'T': {
505         Token *p = va_arg(ap, Token*);
506         memcpy(z, p->z, p->n);
507         z += p->n;
508         break;
509       }
510       case 'S': {
511         int len;
512         SrcList *p = va_arg(ap, SrcList*);
513         int k = va_arg(ap, int);
514         assert( p->nSrc>k && k>=0 );
515         if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){
516           len = strlen(p->a[k].zDatabase);
517           memcpy(z, p->a[k].zDatabase, len);
518           z += len;
519           *(z++) = '.';
520         }
521         len = strlen(p->a[k].zName);
522         memcpy(z, p->a[k].zName, len);
523         z += len;
524         break;
525       }
526       default: {
527         *(z++) = zFormat[i];
528         break;
529       }
530     }
531   }
532   va_end(ap);
533   if( i>j ){
534     memcpy(z, &zFormat[j], i-j);
535     z += i-j;
536   }
537   assert( (z - pParse->zErrMsg) < nByte );
538   *z = 0;
539 }
540 
541 /*
542 ** Convert an SQL-style quoted string into a normal string by removing
543 ** the quote characters.  The conversion is done in-place.  If the
544 ** input does not begin with a quote character, then this routine
545 ** is a no-op.
546 **
547 ** 2002-Feb-14: This routine is extended to remove MS-Access style
548 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
549 ** "a-b-c".
550 */
551 void sqliteDequote(char *z){
552   int quote;
553   int i, j;
554   if( z==0 ) return;
555   quote = z[0];
556   switch( quote ){
557     case '\'':  break;
558     case '"':   break;
559     case '[':   quote = ']';  break;
560     default:    return;
561   }
562   for(i=1, j=0; z[i]; i++){
563     if( z[i]==quote ){
564       if( z[i+1]==quote ){
565         z[j++] = quote;
566         i++;
567       }else{
568         z[j++] = 0;
569         break;
570       }
571     }else{
572       z[j++] = z[i];
573     }
574   }
575 }
576 
577 /* An array to map all upper-case characters into their corresponding
578 ** lower-case character.
579 */
580 static unsigned char UpperToLower[] = {
581       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
582      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
583      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
584      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
585     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
586     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
587     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
588     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
589     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
590     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
591     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
592     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
593     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
594     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
595     252,253,254,255
596 };
597 
598 /*
599 ** This function computes a hash on the name of a keyword.
600 ** Case is not significant.
601 */
602 int sqliteHashNoCase(const char *z, int n){
603   int h = 0;
604   if( n<=0 ) n = strlen(z);
605   while( n > 0  ){
606     h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
607     n--;
608   }
609   return h & 0x7fffffff;
610 }
611 
612 /*
613 ** Some systems have stricmp().  Others have strcasecmp().  Because
614 ** there is no consistency, we will define our own.
615 */
616 int sqliteStrICmp(const char *zLeft, const char *zRight){
617   register unsigned char *a, *b;
618   a = (unsigned char *)zLeft;
619   b = (unsigned char *)zRight;
620   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
621   return *a - *b;
622 }
623 int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
624   register unsigned char *a, *b;
625   a = (unsigned char *)zLeft;
626   b = (unsigned char *)zRight;
627   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
628   return N<0 ? 0 : *a - *b;
629 }
630 
631 /*
632 ** Return TRUE if z is a pure numeric string.  Return FALSE if the
633 ** string contains any character which is not part of a number.
634 **
635 ** Am empty string is considered non-numeric.
636 */
637 int sqliteIsNumber(const char *z){
638   if( *z=='-' || *z=='+' ) z++;
639   if( !isdigit(*z) ){
640     return 0;
641   }
642   z++;
643   while( isdigit(*z) ){ z++; }
644   if( *z=='.' ){
645     z++;
646     if( !isdigit(*z) ) return 0;
647     while( isdigit(*z) ){ z++; }
648   }
649   if( *z=='e' || *z=='E' ){
650     z++;
651     if( *z=='+' || *z=='-' ) z++;
652     if( !isdigit(*z) ) return 0;
653     while( isdigit(*z) ){ z++; }
654   }
655   return *z==0;
656 }
657 
658 /* This comparison routine is what we use for comparison operations
659 ** between numeric values in an SQL expression.  "Numeric" is a little
660 ** bit misleading here.  What we mean is that the strings have a
661 ** type of "numeric" from the point of view of SQL.  The strings
662 ** do not necessarily contain numbers.  They could contain text.
663 **
664 ** If the input strings both look like actual numbers then they
665 ** compare in numerical order.  Numerical strings are always less
666 ** than non-numeric strings so if one input string looks like a
667 ** number and the other does not, then the one that looks like
668 ** a number is the smaller.  Non-numeric strings compare in
669 ** lexigraphical order (the same order as strcmp()).
670 */
671 int sqliteCompare(const char *atext, const char *btext){
672   int result;
673   int isNumA, isNumB;
674   if( atext==0 ){
675     return -1;
676   }else if( btext==0 ){
677     return 1;
678   }
679   isNumA = sqliteIsNumber(atext);
680   isNumB = sqliteIsNumber(btext);
681   if( isNumA ){
682     if( !isNumB ){
683       result = -1;
684     }else{
685       double rA, rB;
686       rA = atof(atext);
687       rB = atof(btext);
688       if( rA<rB ){
689         result = -1;
690       }else if( rA>rB ){
691         result = +1;
692       }else{
693         result = 0;
694       }
695     }
696   }else if( isNumB ){
697     result = +1;
698   }else {
699     result = strcmp(atext, btext);
700   }
701   return result;
702 }
703 
704 /*
705 ** This routine is used for sorting.  Each key is a list of one or more
706 ** null-terminated elements.  The list is terminated by two nulls in
707 ** a row.  For example, the following text is a key with three elements
708 **
709 **            Aone\000Dtwo\000Athree\000\000
710 **
711 ** All elements begin with one of the characters "+-AD" and end with "\000"
712 ** with zero or more text elements in between.  Except, NULL elements
713 ** consist of the special two-character sequence "N\000".
714 **
715 ** Both arguments will have the same number of elements.  This routine
716 ** returns negative, zero, or positive if the first argument is less
717 ** than, equal to, or greater than the first.  (Result is a-b).
718 **
719 ** Each element begins with one of the characters "+", "-", "A", "D".
720 ** This character determines the sort order and collating sequence:
721 **
722 **     +      Sort numerically in ascending order
723 **     -      Sort numerically in descending order
724 **     A      Sort as strings in ascending order
725 **     D      Sort as strings in descending order.
726 **
727 ** For the "+" and "-" sorting, pure numeric strings (strings for which the
728 ** isNum() function above returns TRUE) always compare less than strings
729 ** that are not pure numerics.  Non-numeric strings compare in memcmp()
730 ** order.  This is the same sort order as the sqliteCompare() function
731 ** above generates.
732 **
733 ** The last point is a change from version 2.6.3 to version 2.7.0.  In
734 ** version 2.6.3 and earlier, substrings of digits compare in numerical
735 ** and case was used only to break a tie.
736 **
737 ** Elements that begin with 'A' or 'D' compare in memcmp() order regardless
738 ** of whether or not they look like a number.
739 **
740 ** Note that the sort order imposed by the rules above is the same
741 ** from the ordering defined by the "<", "<=", ">", and ">=" operators
742 ** of expressions and for indices.  This was not the case for version
743 ** 2.6.3 and earlier.
744 */
745 int sqliteSortCompare(const char *a, const char *b){
746   int res = 0;
747   int isNumA, isNumB;
748   int dir = 0;
749 
750   while( res==0 && *a && *b ){
751     if( a[0]=='N' || b[0]=='N' ){
752       if( a[0]==b[0] ){
753         a += 2;
754         b += 2;
755         continue;
756       }
757       if( a[0]=='N' ){
758         dir = b[0];
759         res = -1;
760       }else{
761         dir = a[0];
762         res = +1;
763       }
764       break;
765     }
766     assert( a[0]==b[0] );
767     if( (dir=a[0])=='A' || a[0]=='D' ){
768       res = strcmp(&a[1],&b[1]);
769       if( res ) break;
770     }else{
771       isNumA = sqliteIsNumber(&a[1]);
772       isNumB = sqliteIsNumber(&b[1]);
773       if( isNumA ){
774         double rA, rB;
775         if( !isNumB ){
776           res = -1;
777           break;
778         }
779         rA = atof(&a[1]);
780         rB = atof(&b[1]);
781         if( rA<rB ){
782           res = -1;
783           break;
784         }
785         if( rA>rB ){
786           res = +1;
787           break;
788         }
789       }else if( isNumB ){
790         res = +1;
791         break;
792       }else{
793         res = strcmp(&a[1],&b[1]);
794         if( res ) break;
795       }
796     }
797     a += strlen(&a[1]) + 2;
798     b += strlen(&b[1]) + 2;
799   }
800   if( dir=='-' || dir=='D' ) res = -res;
801   return res;
802 }
803 
804 /*
805 ** Some powers of 64.  These constants are needed in the
806 ** sqliteRealToSortable() routine below.
807 */
808 #define _64e3  (64.0 * 64.0 * 64.0)
809 #define _64e4  (64.0 * 64.0 * 64.0 * 64.0)
810 #define _64e15 (_64e3 * _64e4 * _64e4 * _64e4)
811 #define _64e16 (_64e4 * _64e4 * _64e4 * _64e4)
812 #define _64e63 (_64e15 * _64e16 * _64e16 * _64e16)
813 #define _64e64 (_64e16 * _64e16 * _64e16 * _64e16)
814 
815 /*
816 ** The following procedure converts a double-precision floating point
817 ** number into a string.  The resulting string has the property that
818 ** two such strings comparied using strcmp() or memcmp() will give the
819 ** same results as a numeric comparison of the original floating point
820 ** numbers.
821 **
822 ** This routine is used to generate database keys from floating point
823 ** numbers such that the keys sort in the same order as the original
824 ** floating point numbers even though the keys are compared using
825 ** memcmp().
826 **
827 ** The calling function should have allocated at least 14 characters
828 ** of space for the buffer z[].
829 */
830 void sqliteRealToSortable(double r, char *z){
831   int neg;
832   int exp;
833   int cnt = 0;
834 
835   /* This array maps integers between 0 and 63 into base-64 digits.
836   ** The digits must be chosen such at their ASCII codes are increasing.
837   ** This means we can not use the traditional base-64 digit set. */
838   static const char zDigit[] =
839      "0123456789"
840      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
841      "abcdefghijklmnopqrstuvwxyz"
842      "|~";
843   if( r<0.0 ){
844     neg = 1;
845     r = -r;
846     *z++ = '-';
847   } else {
848     neg = 0;
849     *z++ = '0';
850   }
851   exp = 0;
852 
853   if( r==0.0 ){
854     exp = -1024;
855   }else if( r<(0.5/64.0) ){
856     while( r < 0.5/_64e64 && exp > -961  ){ r *= _64e64;  exp -= 64; }
857     while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16;  exp -= 16; }
858     while( r < 0.5/_64e4  && exp > -1021 ){ r *= _64e4;   exp -= 4; }
859     while( r < 0.5/64.0   && exp > -1024 ){ r *= 64.0;    exp -= 1; }
860   }else if( r>=0.5 ){
861     while( r >= 0.5*_64e63 && exp < 960  ){ r *= 1.0/_64e64; exp += 64; }
862     while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
863     while( r >= 0.5*_64e3  && exp < 1020 ){ r *= 1.0/_64e4;  exp += 4; }
864     while( r >= 0.5        && exp < 1023 ){ r *= 1.0/64.0;   exp += 1; }
865   }
866   if( neg ){
867     exp = -exp;
868     r = -r;
869   }
870   exp += 1024;
871   r += 0.5;
872   if( exp<0 ) return;
873   if( exp>=2048 || r>=1.0 ){
874     strcpy(z, "~~~~~~~~~~~~");
875     return;
876   }
877   *z++ = zDigit[(exp>>6)&0x3f];
878   *z++ = zDigit[exp & 0x3f];
879   while( r>0.0 && cnt<10 ){
880     int digit;
881     r *= 64.0;
882     digit = (int)r;
883     assert( digit>=0 && digit<64 );
884     *z++ = zDigit[digit & 0x3f];
885     r -= digit;
886     cnt++;
887   }
888   *z = 0;
889 }
890 
891 #ifdef SQLITE_UTF8
892 /*
893 ** X is a pointer to the first byte of a UTF-8 character.  Increment
894 ** X so that it points to the next character.  This only works right
895 ** if X points to a well-formed UTF-8 string.
896 */
897 #define sqliteNextChar(X)  while( (0xc0&*++(X))==0x80 ){}
898 #define sqliteCharVal(X)   sqlite_utf8_to_int(X)
899 
900 #else /* !defined(SQLITE_UTF8) */
901 /*
902 ** For iso8859 encoding, the next character is just the next byte.
903 */
904 #define sqliteNextChar(X)  (++(X));
905 #define sqliteCharVal(X)   ((int)*(X))
906 
907 #endif /* defined(SQLITE_UTF8) */
908 
909 
910 #ifdef SQLITE_UTF8
911 /*
912 ** Convert the UTF-8 character to which z points into a 31-bit
913 ** UCS character.  This only works right if z points to a well-formed
914 ** UTF-8 string.
915 */
916 static int sqlite_utf8_to_int(const unsigned char *z){
917   int c;
918   static const int initVal[] = {
919       0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
920      15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,
921      30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,
922      45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
923      60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,
924      75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,
925      90,  91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103, 104,
926     105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
927     120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
928     135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
929     150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
930     165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
931     180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,   0,   1,   2,
932       3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,
933      18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,   0,
934       1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
935       0,   1,   2,   3,   4,   5,   6,   7,   0,   1,   2,   3,   0,   1, 254,
936     255,
937   };
938   c = initVal[*(z++)];
939   while( (0xc0&*z)==0x80 ){
940     c = (c<<6) | (0x3f&*(z++));
941   }
942   return c;
943 }
944 #endif
945 
946 /*
947 ** Compare two UTF-8 strings for equality where the first string can
948 ** potentially be a "glob" expression.  Return true (1) if they
949 ** are the same and false (0) if they are different.
950 **
951 ** Globbing rules:
952 **
953 **      '*'       Matches any sequence of zero or more characters.
954 **
955 **      '?'       Matches exactly one character.
956 **
957 **     [...]      Matches one character from the enclosed list of
958 **                characters.
959 **
960 **     [^...]     Matches one character not in the enclosed list.
961 **
962 ** With the [...] and [^...] matching, a ']' character can be included
963 ** in the list by making it the first character after '[' or '^'.  A
964 ** range of characters can be specified using '-'.  Example:
965 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
966 ** it the last character in the list.
967 **
968 ** This routine is usually quick, but can be N**2 in the worst case.
969 **
970 ** Hints: to match '*' or '?', put them in "[]".  Like this:
971 **
972 **         abc[*]xyz        Matches "abc*xyz" only
973 */
974 int
975 sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
976   register int c;
977   int invert;
978   int seen;
979   int c2;
980 
981   while( (c = *zPattern)!=0 ){
982     switch( c ){
983       case '*':
984         while( (c=zPattern[1]) == '*' || c == '?' ){
985           if( c=='?' ){
986             if( *zString==0 ) return 0;
987             sqliteNextChar(zString);
988           }
989           zPattern++;
990         }
991         if( c==0 ) return 1;
992         if( c=='[' ){
993           while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
994             sqliteNextChar(zString);
995           }
996           return *zString!=0;
997         }else{
998           while( (c2 = *zString)!=0 ){
999             while( c2 != 0 && c2 != c ){ c2 = *++zString; }
1000             if( c2==0 ) return 0;
1001             if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
1002             sqliteNextChar(zString);
1003           }
1004           return 0;
1005         }
1006       case '?': {
1007         if( *zString==0 ) return 0;
1008         sqliteNextChar(zString);
1009         zPattern++;
1010         break;
1011       }
1012       case '[': {
1013         int prior_c = 0;
1014         seen = 0;
1015         invert = 0;
1016         c = sqliteCharVal(zString);
1017         if( c==0 ) return 0;
1018         c2 = *++zPattern;
1019         if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
1020         if( c2==']' ){
1021           if( c==']' ) seen = 1;
1022           c2 = *++zPattern;
1023         }
1024         while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
1025           if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
1026             zPattern++;
1027             c2 = sqliteCharVal(zPattern);
1028             if( c>=prior_c && c<=c2 ) seen = 1;
1029             prior_c = 0;
1030           }else if( c==c2 ){
1031             seen = 1;
1032             prior_c = c2;
1033           }else{
1034             prior_c = c2;
1035           }
1036           sqliteNextChar(zPattern);
1037         }
1038         if( c2==0 || (seen ^ invert)==0 ) return 0;
1039         sqliteNextChar(zString);
1040         zPattern++;
1041         break;
1042       }
1043       default: {
1044         if( c != *zString ) return 0;
1045         zPattern++;
1046         zString++;
1047         break;
1048       }
1049     }
1050   }
1051   return *zString==0;
1052 }
1053 
1054 /*
1055 ** Compare two UTF-8 strings for equality using the "LIKE" operator of
1056 ** SQL.  The '%' character matches any sequence of 0 or more
1057 ** characters and '_' matches any single character.  Case is
1058 ** not significant.
1059 **
1060 ** This routine is just an adaptation of the sqliteGlobCompare()
1061 ** routine above.
1062 */
1063 int
1064 sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
1065   register int c;
1066   int c2;
1067 
1068   while( (c = UpperToLower[*zPattern])!=0 ){
1069     switch( c ){
1070       case '%': {
1071         while( (c=zPattern[1]) == '%' || c == '_' ){
1072           if( c=='_' ){
1073             if( *zString==0 ) return 0;
1074             sqliteNextChar(zString);
1075           }
1076           zPattern++;
1077         }
1078         if( c==0 ) return 1;
1079         c = UpperToLower[c];
1080         while( (c2=UpperToLower[*zString])!=0 ){
1081           while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1082           if( c2==0 ) return 0;
1083           if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
1084           sqliteNextChar(zString);
1085         }
1086         return 0;
1087       }
1088       case '_': {
1089         if( *zString==0 ) return 0;
1090         sqliteNextChar(zString);
1091         zPattern++;
1092         break;
1093       }
1094       default: {
1095         if( c != UpperToLower[*zString] ) return 0;
1096         zPattern++;
1097         zString++;
1098         break;
1099       }
1100     }
1101   }
1102   return *zString==0;
1103 }
1104 
1105 /*
1106 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1107 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1108 ** when this routine is called.
1109 **
1110 ** This routine is a attempt to detect if two threads use the
1111 ** same sqlite* pointer at the same time.  There is a race
1112 ** condition so it is possible that the error is not detected.
1113 ** But usually the problem will be seen.  The result will be an
1114 ** error which can be used to debug the application that is
1115 ** using SQLite incorrectly.
1116 **
1117 ** Ticket #202:  If db->magic is not a valid open value, take care not
1118 ** to modify the db structure at all.  It could be that db is a stale
1119 ** pointer.  In other words, it could be that there has been a prior
1120 ** call to sqlite_close(db) and db has been deallocated.  And we do
1121 ** not want to write into deallocated memory.
1122 */
1123 int sqliteSafetyOn(sqlite *db){
1124   if( db->magic==SQLITE_MAGIC_OPEN ){
1125     db->magic = SQLITE_MAGIC_BUSY;
1126     return 0;
1127   }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1128              || db->want_to_close ){
1129     db->magic = SQLITE_MAGIC_ERROR;
1130     db->flags |= SQLITE_Interrupt;
1131   }
1132   return 1;
1133 }
1134 
1135 /*
1136 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1137 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1138 ** when this routine is called.
1139 */
1140 int sqliteSafetyOff(sqlite *db){
1141   if( db->magic==SQLITE_MAGIC_BUSY ){
1142     db->magic = SQLITE_MAGIC_OPEN;
1143     return 0;
1144   }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1145              || db->want_to_close ){
1146     db->magic = SQLITE_MAGIC_ERROR;
1147     db->flags |= SQLITE_Interrupt;
1148   }
1149   return 1;
1150 }
1151 
1152 /*
1153 ** Check to make sure we are not currently executing an sqlite_exec().
1154 ** If we are currently in an sqlite_exec(), return true and set
1155 ** sqlite.magic to SQLITE_MAGIC_ERROR.  This will cause a complete
1156 ** shutdown of the database.
1157 **
1158 ** This routine is used to try to detect when API routines are called
1159 ** at the wrong time or in the wrong sequence.
1160 */
1161 int sqliteSafetyCheck(sqlite *db){
1162   if( db->pVdbe!=0 ){
1163     db->magic = SQLITE_MAGIC_ERROR;
1164     return 1;
1165   }
1166   return 0;
1167 }
1168