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