File Coverage

util.c
Criterion Covered Total %
statement 217 381 56.9
branch 131 382 34.2
condition n/a
subroutine n/a
pod n/a
total 348 763 45.6


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