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