xref: /sqlite-3.40.0/src/util.c (revision 5368f29a)
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.261 2009/06/24 10:26:33 drh Exp $
18 */
19 #include "sqliteInt.h"
20 #include <stdarg.h>
21 #ifdef SQLITE_HAVE_ISNAN
22 # include <math.h>
23 #endif
24 
25 /*
26 ** Routine needed to support the testcase() macro.
27 */
28 #ifdef SQLITE_COVERAGE_TEST
29 void sqlite3Coverage(int x){
30   static int dummy = 0;
31   dummy += x;
32 }
33 #endif
34 
35 /*
36 ** Return true if the floating point value is Not a Number (NaN).
37 **
38 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
39 ** Otherwise, we have our own implementation that works on most systems.
40 */
41 int sqlite3IsNaN(double x){
42   int rc;   /* The value return */
43 #if !defined(SQLITE_HAVE_ISNAN)
44   /*
45   ** Systems that support the isnan() library function should probably
46   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
47   ** found that many systems do not have a working isnan() function so
48   ** this implementation is provided as an alternative.
49   **
50   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
51   ** On the other hand, the use of -ffast-math comes with the following
52   ** warning:
53   **
54   **      This option [-ffast-math] should never be turned on by any
55   **      -O option since it can result in incorrect output for programs
56   **      which depend on an exact implementation of IEEE or ISO
57   **      rules/specifications for math functions.
58   **
59   ** Under MSVC, this NaN test may fail if compiled with a floating-
60   ** point precision mode other than /fp:precise.  From the MSDN
61   ** documentation:
62   **
63   **      The compiler [with /fp:precise] will properly handle comparisons
64   **      involving NaN. For example, x != x evaluates to true if x is NaN
65   **      ...
66   */
67 #ifdef __FAST_MATH__
68 # error SQLite will not work correctly with the -ffast-math option of GCC.
69 #endif
70   volatile double y = x;
71   volatile double z = y;
72   rc = (y!=z);
73 #else  /* if defined(SQLITE_HAVE_ISNAN) */
74   rc = isnan(x);
75 #endif /* SQLITE_HAVE_ISNAN */
76   testcase( rc );
77   return rc;
78 }
79 
80 /*
81 ** Compute a string length that is limited to what can be stored in
82 ** lower 30 bits of a 32-bit signed integer.
83 **
84 ** The value returned will never be negative.  Nor will it ever be greater
85 ** than the actual length of the string.  For very long strings (greater
86 ** than 1GiB) the value returned might be less than the true string length.
87 */
88 int sqlite3Strlen30(const char *z){
89   const char *z2 = z;
90   if( z==0 ) return 0;
91   while( *z2 ){ z2++; }
92   return 0x3fffffff & (int)(z2 - z);
93 }
94 
95 /*
96 ** Set the most recent error code and error string for the sqlite
97 ** handle "db". The error code is set to "err_code".
98 **
99 ** If it is not NULL, string zFormat specifies the format of the
100 ** error string in the style of the printf functions: The following
101 ** format characters are allowed:
102 **
103 **      %s      Insert a string
104 **      %z      A string that should be freed after use
105 **      %d      Insert an integer
106 **      %T      Insert a token
107 **      %S      Insert the first element of a SrcList
108 **
109 ** zFormat and any string tokens that follow it are assumed to be
110 ** encoded in UTF-8.
111 **
112 ** To clear the most recent error for sqlite handle "db", sqlite3Error
113 ** should be called with err_code set to SQLITE_OK and zFormat set
114 ** to NULL.
115 */
116 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
117   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
118     db->errCode = err_code;
119     if( zFormat ){
120       char *z;
121       va_list ap;
122       va_start(ap, zFormat);
123       z = sqlite3VMPrintf(db, zFormat, ap);
124       va_end(ap);
125       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
126     }else{
127       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
128     }
129   }
130 }
131 
132 /*
133 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
134 ** The following formatting characters are allowed:
135 **
136 **      %s      Insert a string
137 **      %z      A string that should be freed after use
138 **      %d      Insert an integer
139 **      %T      Insert a token
140 **      %S      Insert the first element of a SrcList
141 **
142 ** This function should be used to report any error that occurs whilst
143 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
144 ** last thing the sqlite3_prepare() function does is copy the error
145 ** stored by this function into the database handle using sqlite3Error().
146 ** Function sqlite3Error() should be used during statement execution
147 ** (sqlite3_step() etc.).
148 */
149 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
150   va_list ap;
151   sqlite3 *db = pParse->db;
152   pParse->nErr++;
153   sqlite3DbFree(db, pParse->zErrMsg);
154   va_start(ap, zFormat);
155   pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
156   va_end(ap);
157   pParse->rc = SQLITE_ERROR;
158 }
159 
160 /*
161 ** Clear the error message in pParse, if any
162 */
163 void sqlite3ErrorClear(Parse *pParse){
164   sqlite3DbFree(pParse->db, pParse->zErrMsg);
165   pParse->zErrMsg = 0;
166   pParse->nErr = 0;
167 }
168 
169 /*
170 ** Convert an SQL-style quoted string into a normal string by removing
171 ** the quote characters.  The conversion is done in-place.  If the
172 ** input does not begin with a quote character, then this routine
173 ** is a no-op.
174 **
175 ** The input string must be zero-terminated.  A new zero-terminator
176 ** is added to the dequoted string.
177 **
178 ** The return value is -1 if no dequoting occurs or the length of the
179 ** dequoted string, exclusive of the zero terminator, if dequoting does
180 ** occur.
181 **
182 ** 2002-Feb-14: This routine is extended to remove MS-Access style
183 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
184 ** "a-b-c".
185 */
186 int sqlite3Dequote(char *z){
187   char quote;
188   int i, j;
189   if( z==0 ) return -1;
190   quote = z[0];
191   switch( quote ){
192     case '\'':  break;
193     case '"':   break;
194     case '`':   break;                /* For MySQL compatibility */
195     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
196     default:    return -1;
197   }
198   for(i=1, j=0; ALWAYS(z[i]); i++){
199     if( z[i]==quote ){
200       if( z[i+1]==quote ){
201         z[j++] = quote;
202         i++;
203       }else{
204         break;
205       }
206     }else{
207       z[j++] = z[i];
208     }
209   }
210   z[j] = 0;
211   return j;
212 }
213 
214 /* Convenient short-hand */
215 #define UpperToLower sqlite3UpperToLower
216 
217 /*
218 ** Some systems have stricmp().  Others have strcasecmp().  Because
219 ** there is no consistency, we will define our own.
220 */
221 int sqlite3StrICmp(const char *zLeft, const char *zRight){
222   register unsigned char *a, *b;
223   a = (unsigned char *)zLeft;
224   b = (unsigned char *)zRight;
225   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
226   return UpperToLower[*a] - UpperToLower[*b];
227 }
228 int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
229   register unsigned char *a, *b;
230   a = (unsigned char *)zLeft;
231   b = (unsigned char *)zRight;
232   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
233   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
234 }
235 
236 /*
237 ** Return TRUE if z is a pure numeric string.  Return FALSE and leave
238 ** *realnum unchanged if the string contains any character which is not
239 ** part of a number.
240 **
241 ** If the string is pure numeric, set *realnum to TRUE if the string
242 ** contains the '.' character or an "E+000" style exponentiation suffix.
243 ** Otherwise set *realnum to FALSE.  Note that just becaue *realnum is
244 ** false does not mean that the number can be successfully converted into
245 ** an integer - it might be too big.
246 **
247 ** An empty string is considered non-numeric.
248 */
249 int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
250   int incr = (enc==SQLITE_UTF8?1:2);
251   if( enc==SQLITE_UTF16BE ) z++;
252   if( *z=='-' || *z=='+' ) z += incr;
253   if( !sqlite3Isdigit(*z) ){
254     return 0;
255   }
256   z += incr;
257   *realnum = 0;
258   while( sqlite3Isdigit(*z) ){ z += incr; }
259   if( *z=='.' ){
260     z += incr;
261     if( !sqlite3Isdigit(*z) ) return 0;
262     while( sqlite3Isdigit(*z) ){ z += incr; }
263     *realnum = 1;
264   }
265   if( *z=='e' || *z=='E' ){
266     z += incr;
267     if( *z=='+' || *z=='-' ) z += incr;
268     if( !sqlite3Isdigit(*z) ) return 0;
269     while( sqlite3Isdigit(*z) ){ z += incr; }
270     *realnum = 1;
271   }
272   return *z==0;
273 }
274 
275 /*
276 ** The string z[] is an ascii representation of a real number.
277 ** Convert this string to a double.
278 **
279 ** This routine assumes that z[] really is a valid number.  If it
280 ** is not, the result is undefined.
281 **
282 ** This routine is used instead of the library atof() function because
283 ** the library atof() might want to use "," as the decimal point instead
284 ** of "." depending on how locale is set.  But that would cause problems
285 ** for SQL.  So this routine always uses "." regardless of locale.
286 */
287 int sqlite3AtoF(const char *z, double *pResult){
288 #ifndef SQLITE_OMIT_FLOATING_POINT
289   int sign = 1;
290   const char *zBegin = z;
291   LONGDOUBLE_TYPE v1 = 0.0;
292   int nSignificant = 0;
293   while( sqlite3Isspace(*z) ) z++;
294   if( *z=='-' ){
295     sign = -1;
296     z++;
297   }else if( *z=='+' ){
298     z++;
299   }
300   while( z[0]=='0' ){
301     z++;
302   }
303   while( sqlite3Isdigit(*z) ){
304     v1 = v1*10.0 + (*z - '0');
305     z++;
306     nSignificant++;
307   }
308   if( *z=='.' ){
309     LONGDOUBLE_TYPE divisor = 1.0;
310     z++;
311     if( nSignificant==0 ){
312       while( z[0]=='0' ){
313         divisor *= 10.0;
314         z++;
315       }
316     }
317     while( sqlite3Isdigit(*z) ){
318       if( nSignificant<18 ){
319         v1 = v1*10.0 + (*z - '0');
320         divisor *= 10.0;
321         nSignificant++;
322       }
323       z++;
324     }
325     v1 /= divisor;
326   }
327   if( *z=='e' || *z=='E' ){
328     int esign = 1;
329     int eval = 0;
330     LONGDOUBLE_TYPE scale = 1.0;
331     z++;
332     if( *z=='-' ){
333       esign = -1;
334       z++;
335     }else if( *z=='+' ){
336       z++;
337     }
338     while( sqlite3Isdigit(*z) ){
339       eval = eval*10 + *z - '0';
340       z++;
341     }
342     while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
343     while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
344     while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
345     while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
346     if( esign<0 ){
347       v1 /= scale;
348     }else{
349       v1 *= scale;
350     }
351   }
352   *pResult = (double)(sign<0 ? -v1 : v1);
353   return (int)(z - zBegin);
354 #else
355   return sqlite3Atoi64(z, pResult);
356 #endif /* SQLITE_OMIT_FLOATING_POINT */
357 }
358 
359 /*
360 ** Compare the 19-character string zNum against the text representation
361 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
362 ** if zNum is less than, equal to, or greater than the string.
363 **
364 ** Unlike memcmp() this routine is guaranteed to return the difference
365 ** in the values of the last digit if the only difference is in the
366 ** last digit.  So, for example,
367 **
368 **      compare2pow63("9223372036854775800")
369 **
370 ** will return -8.
371 */
372 static int compare2pow63(const char *zNum){
373   int c;
374   c = memcmp(zNum,"922337203685477580",18)*10;
375   if( c==0 ){
376     c = zNum[18] - '8';
377   }
378   return c;
379 }
380 
381 
382 /*
383 ** Return TRUE if zNum is a 64-bit signed integer and write
384 ** the value of the integer into *pNum.  If zNum is not an integer
385 ** or is an integer that is too large to be expressed with 64 bits,
386 ** then return false.
387 **
388 ** When this routine was originally written it dealt with only
389 ** 32-bit numbers.  At that time, it was much faster than the
390 ** atoi() library routine in RedHat 7.2.
391 */
392 int sqlite3Atoi64(const char *zNum, i64 *pNum){
393   i64 v = 0;
394   int neg;
395   int i, c;
396   const char *zStart;
397   while( sqlite3Isspace(*zNum) ) zNum++;
398   if( *zNum=='-' ){
399     neg = 1;
400     zNum++;
401   }else if( *zNum=='+' ){
402     neg = 0;
403     zNum++;
404   }else{
405     neg = 0;
406   }
407   zStart = zNum;
408   while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
409   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
410     v = v*10 + c - '0';
411   }
412   *pNum = neg ? -v : v;
413   if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
414     /* zNum is empty or contains non-numeric text or is longer
415     ** than 19 digits (thus guaranting that it is too large) */
416     return 0;
417   }else if( i<19 ){
418     /* Less than 19 digits, so we know that it fits in 64 bits */
419     return 1;
420   }else{
421     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
422     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
423     ** is 2^63. */
424     return compare2pow63(zNum)<neg;
425   }
426 }
427 
428 /*
429 ** The string zNum represents an unsigned integer.  The zNum string
430 ** consists of one or more digit characters and is terminated by
431 ** a zero character.  Any stray characters in zNum result in undefined
432 ** behavior.
433 **
434 ** If the unsigned integer that zNum represents will fit in a
435 ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
436 **
437 ** If the negFlag parameter is true, that means that zNum really represents
438 ** a negative number.  (The leading "-" is omitted from zNum.)  This
439 ** parameter is needed to determine a boundary case.  A string
440 ** of "9223373036854775808" returns false if negFlag is false or true
441 ** if negFlag is true.
442 **
443 ** Leading zeros are ignored.
444 */
445 int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
446   int i;
447   int neg = 0;
448 
449   assert( zNum[0]>='0' && zNum[0]<='9' ); /* zNum is an unsigned number */
450 
451   if( negFlag ) neg = 1-neg;
452   while( *zNum=='0' ){
453     zNum++;   /* Skip leading zeros.  Ticket #2454 */
454   }
455   for(i=0; zNum[i]; i++){ assert( zNum[i]>='0' && zNum[i]<='9' ); }
456   if( i<19 ){
457     /* Guaranteed to fit if less than 19 digits */
458     return 1;
459   }else if( i>19 ){
460     /* Guaranteed to be too big if greater than 19 digits */
461     return 0;
462   }else{
463     /* Compare against 2^63. */
464     return compare2pow63(zNum)<neg;
465   }
466 }
467 
468 /*
469 ** If zNum represents an integer that will fit in 32-bits, then set
470 ** *pValue to that integer and return true.  Otherwise return false.
471 **
472 ** Any non-numeric characters that following zNum are ignored.
473 ** This is different from sqlite3Atoi64() which requires the
474 ** input number to be zero-terminated.
475 */
476 int sqlite3GetInt32(const char *zNum, int *pValue){
477   sqlite_int64 v = 0;
478   int i, c;
479   int neg = 0;
480   if( zNum[0]=='-' ){
481     neg = 1;
482     zNum++;
483   }else if( zNum[0]=='+' ){
484     zNum++;
485   }
486   while( zNum[0]=='0' ) zNum++;
487   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
488     v = v*10 + c;
489   }
490 
491   /* The longest decimal representation of a 32 bit integer is 10 digits:
492   **
493   **             1234567890
494   **     2^31 -> 2147483648
495   */
496   if( i>10 ){
497     return 0;
498   }
499   if( v-neg>2147483647 ){
500     return 0;
501   }
502   if( neg ){
503     v = -v;
504   }
505   *pValue = (int)v;
506   return 1;
507 }
508 
509 /*
510 ** The variable-length integer encoding is as follows:
511 **
512 ** KEY:
513 **         A = 0xxxxxxx    7 bits of data and one flag bit
514 **         B = 1xxxxxxx    7 bits of data and one flag bit
515 **         C = xxxxxxxx    8 bits of data
516 **
517 **  7 bits - A
518 ** 14 bits - BA
519 ** 21 bits - BBA
520 ** 28 bits - BBBA
521 ** 35 bits - BBBBA
522 ** 42 bits - BBBBBA
523 ** 49 bits - BBBBBBA
524 ** 56 bits - BBBBBBBA
525 ** 64 bits - BBBBBBBBC
526 */
527 
528 /*
529 ** Write a 64-bit variable-length integer to memory starting at p[0].
530 ** The length of data write will be between 1 and 9 bytes.  The number
531 ** of bytes written is returned.
532 **
533 ** A variable-length integer consists of the lower 7 bits of each byte
534 ** for all bytes that have the 8th bit set and one byte with the 8th
535 ** bit clear.  Except, if we get to the 9th byte, it stores the full
536 ** 8 bits and is the last byte.
537 */
538 int sqlite3PutVarint(unsigned char *p, u64 v){
539   int i, j, n;
540   u8 buf[10];
541   if( v & (((u64)0xff000000)<<32) ){
542     p[8] = (u8)v;
543     v >>= 8;
544     for(i=7; i>=0; i--){
545       p[i] = (u8)((v & 0x7f) | 0x80);
546       v >>= 7;
547     }
548     return 9;
549   }
550   n = 0;
551   do{
552     buf[n++] = (u8)((v & 0x7f) | 0x80);
553     v >>= 7;
554   }while( v!=0 );
555   buf[0] &= 0x7f;
556   assert( n<=9 );
557   for(i=0, j=n-1; j>=0; j--, i++){
558     p[i] = buf[j];
559   }
560   return n;
561 }
562 
563 /*
564 ** This routine is a faster version of sqlite3PutVarint() that only
565 ** works for 32-bit positive integers and which is optimized for
566 ** the common case of small integers.  A MACRO version, putVarint32,
567 ** is provided which inlines the single-byte case.  All code should use
568 ** the MACRO version as this function assumes the single-byte case has
569 ** already been handled.
570 */
571 int sqlite3PutVarint32(unsigned char *p, u32 v){
572 #ifndef putVarint32
573   if( (v & ~0x7f)==0 ){
574     p[0] = v;
575     return 1;
576   }
577 #endif
578   if( (v & ~0x3fff)==0 ){
579     p[0] = (u8)((v>>7) | 0x80);
580     p[1] = (u8)(v & 0x7f);
581     return 2;
582   }
583   return sqlite3PutVarint(p, v);
584 }
585 
586 /*
587 ** Read a 64-bit variable-length integer from memory starting at p[0].
588 ** Return the number of bytes read.  The value is stored in *v.
589 */
590 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
591   u32 a,b,s;
592 
593   a = *p;
594   /* a: p0 (unmasked) */
595   if (!(a&0x80))
596   {
597     *v = a;
598     return 1;
599   }
600 
601   p++;
602   b = *p;
603   /* b: p1 (unmasked) */
604   if (!(b&0x80))
605   {
606     a &= 0x7f;
607     a = a<<7;
608     a |= b;
609     *v = a;
610     return 2;
611   }
612 
613   p++;
614   a = a<<14;
615   a |= *p;
616   /* a: p0<<14 | p2 (unmasked) */
617   if (!(a&0x80))
618   {
619     a &= (0x7f<<14)|(0x7f);
620     b &= 0x7f;
621     b = b<<7;
622     a |= b;
623     *v = a;
624     return 3;
625   }
626 
627   /* CSE1 from below */
628   a &= (0x7f<<14)|(0x7f);
629   p++;
630   b = b<<14;
631   b |= *p;
632   /* b: p1<<14 | p3 (unmasked) */
633   if (!(b&0x80))
634   {
635     b &= (0x7f<<14)|(0x7f);
636     /* moved CSE1 up */
637     /* a &= (0x7f<<14)|(0x7f); */
638     a = a<<7;
639     a |= b;
640     *v = a;
641     return 4;
642   }
643 
644   /* a: p0<<14 | p2 (masked) */
645   /* b: p1<<14 | p3 (unmasked) */
646   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
647   /* moved CSE1 up */
648   /* a &= (0x7f<<14)|(0x7f); */
649   b &= (0x7f<<14)|(0x7f);
650   s = a;
651   /* s: p0<<14 | p2 (masked) */
652 
653   p++;
654   a = a<<14;
655   a |= *p;
656   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
657   if (!(a&0x80))
658   {
659     /* we can skip these cause they were (effectively) done above in calc'ing s */
660     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
661     /* b &= (0x7f<<14)|(0x7f); */
662     b = b<<7;
663     a |= b;
664     s = s>>18;
665     *v = ((u64)s)<<32 | a;
666     return 5;
667   }
668 
669   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
670   s = s<<7;
671   s |= b;
672   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
673 
674   p++;
675   b = b<<14;
676   b |= *p;
677   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
678   if (!(b&0x80))
679   {
680     /* we can skip this cause it was (effectively) done above in calc'ing s */
681     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
682     a &= (0x7f<<14)|(0x7f);
683     a = a<<7;
684     a |= b;
685     s = s>>18;
686     *v = ((u64)s)<<32 | a;
687     return 6;
688   }
689 
690   p++;
691   a = a<<14;
692   a |= *p;
693   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
694   if (!(a&0x80))
695   {
696     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
697     b &= (0x7f<<14)|(0x7f);
698     b = b<<7;
699     a |= b;
700     s = s>>11;
701     *v = ((u64)s)<<32 | a;
702     return 7;
703   }
704 
705   /* CSE2 from below */
706   a &= (0x7f<<14)|(0x7f);
707   p++;
708   b = b<<14;
709   b |= *p;
710   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
711   if (!(b&0x80))
712   {
713     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
714     /* moved CSE2 up */
715     /* a &= (0x7f<<14)|(0x7f); */
716     a = a<<7;
717     a |= b;
718     s = s>>4;
719     *v = ((u64)s)<<32 | a;
720     return 8;
721   }
722 
723   p++;
724   a = a<<15;
725   a |= *p;
726   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
727 
728   /* moved CSE2 up */
729   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
730   b &= (0x7f<<14)|(0x7f);
731   b = b<<8;
732   a |= b;
733 
734   s = s<<4;
735   b = p[-4];
736   b &= 0x7f;
737   b = b>>3;
738   s |= b;
739 
740   *v = ((u64)s)<<32 | a;
741 
742   return 9;
743 }
744 
745 /*
746 ** Read a 32-bit variable-length integer from memory starting at p[0].
747 ** Return the number of bytes read.  The value is stored in *v.
748 **
749 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
750 ** integer, then set *v to 0xffffffff.
751 **
752 ** A MACRO version, getVarint32, is provided which inlines the
753 ** single-byte case.  All code should use the MACRO version as
754 ** this function assumes the single-byte case has already been handled.
755 */
756 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
757   u32 a,b;
758 
759   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
760   ** by the getVarin32() macro */
761   a = *p;
762   /* a: p0 (unmasked) */
763 #ifndef getVarint32
764   if (!(a&0x80))
765   {
766     /* Values between 0 and 127 */
767     *v = a;
768     return 1;
769   }
770 #endif
771 
772   /* The 2-byte case */
773   p++;
774   b = *p;
775   /* b: p1 (unmasked) */
776   if (!(b&0x80))
777   {
778     /* Values between 128 and 16383 */
779     a &= 0x7f;
780     a = a<<7;
781     *v = a | b;
782     return 2;
783   }
784 
785   /* The 3-byte case */
786   p++;
787   a = a<<14;
788   a |= *p;
789   /* a: p0<<14 | p2 (unmasked) */
790   if (!(a&0x80))
791   {
792     /* Values between 16384 and 2097151 */
793     a &= (0x7f<<14)|(0x7f);
794     b &= 0x7f;
795     b = b<<7;
796     *v = a | b;
797     return 3;
798   }
799 
800   /* A 32-bit varint is used to store size information in btrees.
801   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
802   ** A 3-byte varint is sufficient, for example, to record the size
803   ** of a 1048569-byte BLOB or string.
804   **
805   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
806   ** rare larger cases can be handled by the slower 64-bit varint
807   ** routine.
808   */
809 #if 1
810   {
811     u64 v64;
812     u8 n;
813 
814     p -= 2;
815     n = sqlite3GetVarint(p, &v64);
816     assert( n>3 && n<=9 );
817     if( (v64 & SQLITE_MAX_U32)!=v64 ){
818       *v = 0xffffffff;
819     }else{
820       *v = (u32)v64;
821     }
822     return n;
823   }
824 
825 #else
826   /* For following code (kept for historical record only) shows an
827   ** unrolling for the 3- and 4-byte varint cases.  This code is
828   ** slightly faster, but it is also larger and much harder to test.
829   */
830   p++;
831   b = b<<14;
832   b |= *p;
833   /* b: p1<<14 | p3 (unmasked) */
834   if (!(b&0x80))
835   {
836     /* Values between 2097152 and 268435455 */
837     b &= (0x7f<<14)|(0x7f);
838     a &= (0x7f<<14)|(0x7f);
839     a = a<<7;
840     *v = a | b;
841     return 4;
842   }
843 
844   p++;
845   a = a<<14;
846   a |= *p;
847   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
848   if (!(a&0x80))
849   {
850     /* Walues  between 268435456 and 34359738367 */
851     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
852     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
853     b = b<<7;
854     *v = a | b;
855     return 5;
856   }
857 
858   /* We can only reach this point when reading a corrupt database
859   ** file.  In that case we are not in any hurry.  Use the (relatively
860   ** slow) general-purpose sqlite3GetVarint() routine to extract the
861   ** value. */
862   {
863     u64 v64;
864     u8 n;
865 
866     p -= 4;
867     n = sqlite3GetVarint(p, &v64);
868     assert( n>5 && n<=9 );
869     *v = (u32)v64;
870     return n;
871   }
872 #endif
873 }
874 
875 /*
876 ** Return the number of bytes that will be needed to store the given
877 ** 64-bit integer.
878 */
879 int sqlite3VarintLen(u64 v){
880   int i = 0;
881   do{
882     i++;
883     v >>= 7;
884   }while( v!=0 && ALWAYS(i<9) );
885   return i;
886 }
887 
888 
889 /*
890 ** Read or write a four-byte big-endian integer value.
891 */
892 u32 sqlite3Get4byte(const u8 *p){
893   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
894 }
895 void sqlite3Put4byte(unsigned char *p, u32 v){
896   p[0] = (u8)(v>>24);
897   p[1] = (u8)(v>>16);
898   p[2] = (u8)(v>>8);
899   p[3] = (u8)v;
900 }
901 
902 
903 
904 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
905 /*
906 ** Translate a single byte of Hex into an integer.
907 ** This routinen only works if h really is a valid hexadecimal
908 ** character:  0..9a..fA..F
909 */
910 static u8 hexToInt(int h){
911   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
912 #ifdef SQLITE_ASCII
913   h += 9*(1&(h>>6));
914 #endif
915 #ifdef SQLITE_EBCDIC
916   h += 9*(1&~(h>>4));
917 #endif
918   return (u8)(h & 0xf);
919 }
920 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
921 
922 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
923 /*
924 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
925 ** value.  Return a pointer to its binary value.  Space to hold the
926 ** binary value has been obtained from malloc and must be freed by
927 ** the calling routine.
928 */
929 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
930   char *zBlob;
931   int i;
932 
933   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
934   n--;
935   if( zBlob ){
936     for(i=0; i<n; i+=2){
937       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
938     }
939     zBlob[i/2] = 0;
940   }
941   return zBlob;
942 }
943 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
944 
945 
946 /*
947 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
948 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
949 ** when this routine is called.
950 **
951 ** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
952 ** value indicates that the database connection passed into the API is
953 ** open and is not being used by another thread.  By changing the value
954 ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
955 ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
956 ** when the API exits.
957 **
958 ** This routine is a attempt to detect if two threads use the
959 ** same sqlite* pointer at the same time.  There is a race
960 ** condition so it is possible that the error is not detected.
961 ** But usually the problem will be seen.  The result will be an
962 ** error which can be used to debug the application that is
963 ** using SQLite incorrectly.
964 **
965 ** Ticket #202:  If db->magic is not a valid open value, take care not
966 ** to modify the db structure at all.  It could be that db is a stale
967 ** pointer.  In other words, it could be that there has been a prior
968 ** call to sqlite3_close(db) and db has been deallocated.  And we do
969 ** not want to write into deallocated memory.
970 */
971 #ifdef SQLITE_DEBUG
972 int sqlite3SafetyOn(sqlite3 *db){
973   if( db->magic==SQLITE_MAGIC_OPEN ){
974     db->magic = SQLITE_MAGIC_BUSY;
975     assert( sqlite3_mutex_held(db->mutex) );
976     return 0;
977   }else if( db->magic==SQLITE_MAGIC_BUSY ){
978     db->magic = SQLITE_MAGIC_ERROR;
979     db->u1.isInterrupted = 1;
980   }
981   return 1;
982 }
983 #endif
984 
985 /*
986 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
987 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
988 ** when this routine is called.
989 */
990 #ifdef SQLITE_DEBUG
991 int sqlite3SafetyOff(sqlite3 *db){
992   if( db->magic==SQLITE_MAGIC_BUSY ){
993     db->magic = SQLITE_MAGIC_OPEN;
994     assert( sqlite3_mutex_held(db->mutex) );
995     return 0;
996   }else{
997     db->magic = SQLITE_MAGIC_ERROR;
998     db->u1.isInterrupted = 1;
999     return 1;
1000   }
1001 }
1002 #endif
1003 
1004 /*
1005 ** Check to make sure we have a valid db pointer.  This test is not
1006 ** foolproof but it does provide some measure of protection against
1007 ** misuse of the interface such as passing in db pointers that are
1008 ** NULL or which have been previously closed.  If this routine returns
1009 ** 1 it means that the db pointer is valid and 0 if it should not be
1010 ** dereferenced for any reason.  The calling function should invoke
1011 ** SQLITE_MISUSE immediately.
1012 **
1013 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1014 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1015 ** open properly and is not fit for general use but which can be
1016 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
1017 */
1018 int sqlite3SafetyCheckOk(sqlite3 *db){
1019   u32 magic;
1020   if( db==0 ) return 0;
1021   magic = db->magic;
1022   if( magic!=SQLITE_MAGIC_OPEN
1023 #ifdef SQLITE_DEBUG
1024      && magic!=SQLITE_MAGIC_BUSY
1025 #endif
1026   ){
1027     return 0;
1028   }else{
1029     return 1;
1030   }
1031 }
1032 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1033   u32 magic;
1034   magic = db->magic;
1035   if( magic!=SQLITE_MAGIC_SICK &&
1036       magic!=SQLITE_MAGIC_OPEN &&
1037       magic!=SQLITE_MAGIC_BUSY ) return 0;
1038   return 1;
1039 }
1040