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