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 203289 2005-12-20 15:26:26Z iliaa $
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 29042 : void *sqliteMalloc(int n){
253 : void *p;
254 29042 : if( (p = malloc(n))==0 ){
255 0 : if( n>0 ) sqlite_malloc_failed++;
256 : }else{
257 29042 : memset(p, 0, n);
258 : }
259 29042 : 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 19525 : void *sqliteMallocRaw(int n){
267 : void *p;
268 19525 : if( (p = malloc(n))==0 ){
269 0 : if( n>0 ) sqlite_malloc_failed++;
270 : }
271 19525 : return p;
272 : }
273 :
274 : /*
275 : ** Free memory previously obtained from sqliteMalloc()
276 : */
277 85852 : void sqliteFree(void *p){
278 85852 : if( p ){
279 44256 : free(p);
280 : }
281 85852 : }
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 5393 : void *sqliteRealloc(void *p, int n){
289 : void *p2;
290 5393 : if( p==0 ){
291 4876 : return sqliteMalloc(n);
292 : }
293 517 : if( n==0 ){
294 0 : sqliteFree(p);
295 0 : return 0;
296 : }
297 517 : p2 = realloc(p, n);
298 517 : if( p2==0 ){
299 0 : sqlite_malloc_failed++;
300 : }
301 517 : return p2;
302 : }
303 :
304 : /*
305 : ** Make a copy of a string in memory obtained from sqliteMalloc()
306 : */
307 0 : char *sqliteStrDup(const char *z){
308 : char *zNew;
309 0 : if( z==0 ) return 0;
310 0 : zNew = sqliteMallocRaw(strlen(z)+1);
311 0 : if( zNew ) strcpy(zNew, z);
312 0 : return zNew;
313 : }
314 2448 : char *sqliteStrNDup(const char *z, int n){
315 : char *zNew;
316 2448 : if( z==0 ) return 0;
317 2448 : zNew = sqliteMallocRaw(n+1);
318 2448 : if( zNew ){
319 2448 : memcpy(zNew, z, n);
320 2448 : zNew[n] = 0;
321 : }
322 2448 : 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 483 : void sqliteSetString(char **pz, ...){
334 : va_list ap;
335 : int nByte;
336 : const char *z;
337 : char *zResult;
338 :
339 483 : if( pz==0 ) return;
340 483 : nByte = 1;
341 483 : va_start(ap, pz);
342 3481 : while( (z = va_arg(ap, const char*))!=0 ){
343 2515 : nByte += strlen(z);
344 : }
345 483 : va_end(ap);
346 483 : sqliteFree(*pz);
347 483 : *pz = zResult = sqliteMallocRaw( nByte );
348 483 : if( zResult==0 ){
349 0 : return;
350 : }
351 483 : *zResult = 0;
352 483 : va_start(ap, pz);
353 3481 : while( (z = va_arg(ap, const char*))!=0 ){
354 2515 : strcpy(zResult, z);
355 2515 : zResult += strlen(zResult);
356 : }
357 483 : 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 : */
372 10131 : void sqliteSetNString(char **pz, ...){
373 : va_list ap;
374 : int nByte;
375 : const char *z;
376 : char *zResult;
377 : int n;
378 :
379 10131 : if( pz==0 ) return;
380 10131 : nByte = 0;
381 10131 : va_start(ap, pz);
382 30393 : while( (z = va_arg(ap, const char*))!=0 ){
383 10131 : n = va_arg(ap, int);
384 10131 : if( n<=0 ) n = strlen(z);
385 10131 : nByte += n;
386 : }
387 10131 : va_end(ap);
388 10131 : sqliteFree(*pz);
389 10131 : *pz = zResult = sqliteMallocRaw( nByte + 1 );
390 10131 : if( zResult==0 ) return;
391 10131 : va_start(ap, pz);
392 30393 : while( (z = va_arg(ap, const char*))!=0 ){
393 10131 : n = va_arg(ap, int);
394 10131 : if( n<=0 ) n = strlen(z);
395 10131 : strncpy(zResult, z, n);
396 10131 : zResult += n;
397 : }
398 10131 : *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 10131 : 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 : */
417 303 : void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){
418 : va_list ap;
419 303 : pParse->nErr++;
420 303 : sqliteFree(pParse->zErrMsg);
421 303 : va_start(ap, zFormat);
422 303 : pParse->zErrMsg = sqliteVMPrintf(zFormat, ap);
423 303 : va_end(ap);
424 303 : }
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 : */
436 6307 : void sqliteDequote(char *z){
437 : int quote;
438 : int i, j;
439 6307 : if( z==0 ) return;
440 6307 : quote = z[0];
441 6307 : switch( quote ){
442 437 : case '\'': break;
443 306 : case '"': break;
444 0 : case '[': quote = ']'; break;
445 5564 : default: return;
446 : }
447 3959 : for(i=1, j=0; z[i]; i++){
448 3959 : if( z[i]==quote ){
449 744 : if( z[i+1]==quote ){
450 1 : z[j++] = quote;
451 1 : i++;
452 : }else{
453 743 : z[j++] = 0;
454 743 : break;
455 : }
456 : }else{
457 3215 : 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 : */
487 47771 : int sqliteHashNoCase(const char *z, int n){
488 47771 : int h = 0;
489 47771 : if( n<=0 ) n = strlen(z);
490 387939 : while( n > 0 ){
491 292397 : h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
492 292397 : n--;
493 : }
494 47771 : 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 : */
501 8892 : int sqliteStrICmp(const char *zLeft, const char *zRight){
502 : register unsigned char *a, *b;
503 8892 : a = (unsigned char *)zLeft;
504 8892 : b = (unsigned char *)zRight;
505 8892 : while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
506 8892 : return UpperToLower[*a] - UpperToLower[*b];
507 : }
508 16540 : int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
509 : register unsigned char *a, *b;
510 16540 : a = (unsigned char *)zLeft;
511 16540 : b = (unsigned char *)zRight;
512 16540 : while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
513 16540 : 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 : */
522 61 : int sqliteIsNumber(const char *z){
523 61 : if( *z=='-' || *z=='+' ) z++;
524 61 : if( !isdigit(*z) ){
525 2 : return 0;
526 : }
527 59 : z++;
528 59 : while( isdigit(*z) ){ z++; }
529 59 : if( *z=='.' ){
530 0 : z++;
531 0 : if( !isdigit(*z) ) return 0;
532 0 : while( isdigit(*z) ){ z++; }
533 : }
534 59 : if( *z=='e' || *z=='E' ){
535 0 : z++;
536 0 : if( *z=='+' || *z=='-' ) z++;
537 0 : if( !isdigit(*z) ) return 0;
538 0 : while( isdigit(*z) ){ z++; }
539 : }
540 59 : 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 : */
555 59 : double sqliteAtoF(const char *z, const char **pzEnd){
556 59 : int sign = 1;
557 59 : LONGDOUBLE_TYPE v1 = 0.0;
558 59 : if( *z=='-' ){
559 0 : sign = -1;
560 0 : z++;
561 59 : }else if( *z=='+' ){
562 0 : z++;
563 : }
564 207 : while( isdigit(*z) ){
565 89 : v1 = v1*10.0 + (*z - '0');
566 89 : z++;
567 : }
568 59 : if( *z=='.' ){
569 0 : LONGDOUBLE_TYPE divisor = 1.0;
570 0 : z++;
571 0 : while( isdigit(*z) ){
572 0 : v1 = v1*10.0 + (*z - '0');
573 0 : divisor *= 10.0;
574 0 : z++;
575 : }
576 0 : v1 /= divisor;
577 : }
578 59 : if( *z=='e' || *z=='E' ){
579 0 : int esign = 1;
580 0 : int eval = 0;
581 0 : LONGDOUBLE_TYPE scale = 1.0;
582 0 : z++;
583 0 : if( *z=='-' ){
584 0 : esign = -1;
585 0 : z++;
586 0 : }else if( *z=='+' ){
587 0 : z++;
588 : }
589 0 : while( isdigit(*z) ){
590 0 : eval = eval*10 + *z - '0';
591 0 : z++;
592 : }
593 0 : while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
594 0 : while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
595 0 : while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
596 0 : while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
597 0 : if( esign<0 ){
598 0 : v1 /= scale;
599 : }else{
600 0 : v1 *= scale;
601 : }
602 : }
603 59 : if( pzEnd ) *pzEnd = z;
604 59 : 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 : */
618 469 : int sqliteFitsIn32Bits(const char *zNum){
619 : int i, c;
620 469 : if( *zNum=='-' || *zNum=='+' ) zNum++;
621 469 : for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
622 469 : 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 : */
638 2 : int sqliteCompare(const char *atext, const char *btext){
639 : int result;
640 : int isNumA, isNumB;
641 2 : if( atext==0 ){
642 0 : return -1;
643 2 : }else if( btext==0 ){
644 0 : return 1;
645 : }
646 2 : isNumA = sqliteIsNumber(atext);
647 2 : isNumB = sqliteIsNumber(btext);
648 2 : if( isNumA ){
649 1 : if( !isNumB ){
650 0 : result = -1;
651 : }else{
652 : double rA, rB;
653 1 : rA = sqliteAtoF(atext, 0);
654 1 : rB = sqliteAtoF(btext, 0);
655 1 : if( rA<rB ){
656 0 : result = -1;
657 1 : }else if( rA>rB ){
658 0 : result = +1;
659 : }else{
660 1 : result = 0;
661 : }
662 : }
663 1 : }else if( isNumB ){
664 0 : result = +1;
665 : }else {
666 1 : result = strcmp(atext, btext);
667 : }
668 2 : 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 : */
712 0 : int sqliteSortCompare(const char *a, const char *b){
713 0 : int res = 0;
714 : int isNumA, isNumB;
715 0 : int dir = 0;
716 :
717 0 : while( res==0 && *a && *b ){
718 0 : if( a[0]=='N' || b[0]=='N' ){
719 0 : if( a[0]==b[0] ){
720 0 : a += 2;
721 0 : b += 2;
722 0 : continue;
723 : }
724 0 : if( a[0]=='N' ){
725 0 : dir = b[0];
726 0 : res = -1;
727 : }else{
728 0 : dir = a[0];
729 0 : res = +1;
730 : }
731 0 : break;
732 : }
733 : assert( a[0]==b[0] );
734 0 : if( (dir=a[0])=='A' || a[0]=='D' ){
735 0 : res = strcmp(&a[1],&b[1]);
736 0 : if( res ) break;
737 : }else{
738 0 : isNumA = sqliteIsNumber(&a[1]);
739 0 : isNumB = sqliteIsNumber(&b[1]);
740 0 : if( isNumA ){
741 : double rA, rB;
742 0 : if( !isNumB ){
743 0 : res = -1;
744 0 : break;
745 : }
746 0 : rA = sqliteAtoF(&a[1], 0);
747 0 : rB = sqliteAtoF(&b[1], 0);
748 0 : if( rA<rB ){
749 0 : res = -1;
750 0 : break;
751 : }
752 0 : if( rA>rB ){
753 0 : res = +1;
754 0 : break;
755 : }
756 0 : }else if( isNumB ){
757 0 : res = +1;
758 0 : break;
759 : }else{
760 0 : res = strcmp(&a[1],&b[1]);
761 0 : if( res ) break;
762 : }
763 : }
764 0 : a += strlen(&a[1]) + 2;
765 0 : b += strlen(&b[1]) + 2;
766 : }
767 0 : if( dir=='-' || dir=='D' ) res = -res;
768 0 : 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 : */
797 183 : void sqliteRealToSortable(double r, char *z){
798 : int neg;
799 : int exp;
800 183 : 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 183 : if( r<0.0 ){
811 0 : neg = 1;
812 0 : r = -r;
813 0 : *z++ = '-';
814 : } else {
815 183 : neg = 0;
816 183 : *z++ = '0';
817 : }
818 183 : exp = 0;
819 :
820 183 : if( r==0.0 ){
821 20 : exp = -1024;
822 163 : }else if( r<(0.5/64.0) ){
823 0 : while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; }
824 0 : while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; }
825 0 : while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; }
826 0 : while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; }
827 163 : }else if( r>=0.5 ){
828 163 : while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; }
829 163 : while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
830 163 : while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; }
831 163 : while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; }
832 : }
833 183 : if( neg ){
834 0 : exp = -exp;
835 0 : r = -r;
836 : }
837 183 : exp += 1024;
838 183 : r += 0.5;
839 183 : if( exp<0 ) return;
840 183 : if( exp>=2048 || r>=1.0 ){
841 0 : strcpy(z, "~~~~~~~~~~~~");
842 0 : return;
843 : }
844 183 : *z++ = zDigit[(exp>>6)&0x3f];
845 183 : *z++ = zDigit[exp & 0x3f];
846 559 : while( r>0.0 && cnt<10 ){
847 : int digit;
848 193 : r *= 64.0;
849 193 : digit = (int)r;
850 : assert( digit>=0 && digit<64 );
851 193 : *z++ = zDigit[digit & 0x3f];
852 193 : r -= digit;
853 193 : cnt++;
854 : }
855 183 : *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 : */
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
942 0 : sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){
943 : register int c;
944 : int invert;
945 : int seen;
946 : int c2;
947 :
948 0 : while( (c = *zPattern)!=0 ){
949 0 : switch( c ){
950 : case '*':
951 0 : while( (c=zPattern[1]) == '*' || c == '?' ){
952 0 : if( c=='?' ){
953 0 : if( *zString==0 ) return 0;
954 0 : sqliteNextChar(zString);
955 : }
956 0 : zPattern++;
957 : }
958 0 : if( c==0 ) return 1;
959 0 : if( c=='[' ){
960 0 : while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
961 0 : sqliteNextChar(zString);
962 : }
963 0 : return *zString!=0;
964 : }else{
965 0 : while( (c2 = *zString)!=0 ){
966 0 : while( c2 != 0 && c2 != c ){ c2 = *++zString; }
967 0 : if( c2==0 ) return 0;
968 0 : if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
969 0 : sqliteNextChar(zString);
970 : }
971 0 : return 0;
972 : }
973 : case '?': {
974 0 : if( *zString==0 ) return 0;
975 0 : sqliteNextChar(zString);
976 0 : zPattern++;
977 0 : break;
978 : }
979 : case '[': {
980 0 : int prior_c = 0;
981 0 : seen = 0;
982 0 : invert = 0;
983 0 : c = sqliteCharVal(zString);
984 0 : if( c==0 ) return 0;
985 0 : c2 = *++zPattern;
986 0 : if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
987 0 : if( c2==']' ){
988 0 : if( c==']' ) seen = 1;
989 0 : c2 = *++zPattern;
990 : }
991 0 : while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
992 0 : if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
993 0 : zPattern++;
994 0 : c2 = sqliteCharVal(zPattern);
995 0 : if( c>=prior_c && c<=c2 ) seen = 1;
996 0 : prior_c = 0;
997 0 : }else if( c==c2 ){
998 0 : seen = 1;
999 0 : prior_c = c2;
1000 : }else{
1001 0 : prior_c = c2;
1002 : }
1003 0 : sqliteNextChar(zPattern);
1004 : }
1005 0 : if( c2==0 || (seen ^ invert)==0 ) return 0;
1006 0 : sqliteNextChar(zString);
1007 0 : zPattern++;
1008 0 : break;
1009 : }
1010 : default: {
1011 0 : if( c != *zString ) return 0;
1012 0 : zPattern++;
1013 0 : zString++;
1014 : break;
1015 : }
1016 : }
1017 : }
1018 0 : 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
1031 0 : sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
1032 : register int c;
1033 : int c2;
1034 :
1035 0 : while( (c = UpperToLower[*zPattern])!=0 ){
1036 0 : switch( c ){
1037 : case '%': {
1038 0 : while( (c=zPattern[1]) == '%' || c == '_' ){
1039 0 : if( c=='_' ){
1040 0 : if( *zString==0 ) return 0;
1041 0 : sqliteNextChar(zString);
1042 : }
1043 0 : zPattern++;
1044 : }
1045 0 : if( c==0 ) return 1;
1046 0 : c = UpperToLower[c];
1047 0 : while( (c2=UpperToLower[*zString])!=0 ){
1048 0 : while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
1049 0 : if( c2==0 ) return 0;
1050 0 : if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
1051 0 : sqliteNextChar(zString);
1052 : }
1053 0 : return 0;
1054 : }
1055 : case '_': {
1056 0 : if( *zString==0 ) return 0;
1057 0 : sqliteNextChar(zString);
1058 0 : zPattern++;
1059 0 : break;
1060 : }
1061 : default: {
1062 0 : if( c != UpperToLower[*zString] ) return 0;
1063 0 : zPattern++;
1064 0 : zString++;
1065 : break;
1066 : }
1067 : }
1068 : }
1069 0 : 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 : */
1090 3840 : int sqliteSafetyOn(sqlite *db){
1091 3840 : if( db->magic==SQLITE_MAGIC_OPEN ){
1092 3840 : db->magic = SQLITE_MAGIC_BUSY;
1093 3840 : return 0;
1094 0 : }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
1095 : || db->want_to_close ){
1096 0 : db->magic = SQLITE_MAGIC_ERROR;
1097 0 : db->flags |= SQLITE_Interrupt;
1098 : }
1099 0 : 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 : */
1107 3708 : int sqliteSafetyOff(sqlite *db){
1108 3708 : if( db->magic==SQLITE_MAGIC_BUSY ){
1109 3708 : db->magic = SQLITE_MAGIC_OPEN;
1110 3708 : return 0;
1111 0 : }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
1112 : || db->want_to_close ){
1113 0 : db->magic = SQLITE_MAGIC_ERROR;
1114 0 : db->flags |= SQLITE_Interrupt;
1115 : }
1116 0 : 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 : */
1128 5603 : int sqliteSafetyCheck(sqlite *db){
1129 5603 : if( db->pVdbe!=0 ){
1130 17 : db->magic = SQLITE_MAGIC_ERROR;
1131 17 : return 1;
1132 : }
1133 5586 : return 0;
1134 : }
|