xref: /sqlite-3.40.0/src/util.c (revision 7a420e22)
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 ** The string z[] is an text representation of a real number.
242 ** Convert this string to a double and write it into *pResult.
243 **
244 ** The string z[] is length bytes in length (bytes, not characters) and
245 ** uses the encoding enc.  The string is not necessarily zero-terminated.
246 **
247 ** Return TRUE if the result is a valid real number (or integer) and FALSE
248 ** if the string is empty or contains extraneous text.  Valid numbers
249 ** are in one of these formats:
250 **
251 **    [+-]digits[E[+-]digits]
252 **    [+-]digits.[digits][E[+-]digits]
253 **    [+-].digits[E[+-]digits]
254 **
255 ** Leading and trailing whitespace is ignored for the purpose of determining
256 ** validity.
257 **
258 ** If some prefix of the input string is a valid number, this routine
259 ** returns FALSE but it still converts the prefix and writes the result
260 ** into *pResult.
261 */
262 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
263 #ifndef SQLITE_OMIT_FLOATING_POINT
264   int incr = (enc==SQLITE_UTF8?1:2);
265   const char *zEnd = z + length;
266   /* sign * significand * (10 ^ (esign * exponent)) */
267   int sign = 1;    /* sign of significand */
268   i64 s = 0;       /* significand */
269   int d = 0;       /* adjust exponent for shifting decimal point */
270   int esign = 1;   /* sign of exponent */
271   int e = 0;       /* exponent */
272   int eValid = 1;  /* True exponent is either not used or is well-formed */
273   double result;
274   int nDigits = 0;
275 
276   *pResult = 0.0;   /* Default return value, in case of an error */
277 
278   if( enc==SQLITE_UTF16BE ) z++;
279 
280   /* skip leading spaces */
281   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
282   if( z>=zEnd ) return 0;
283 
284   /* get sign of significand */
285   if( *z=='-' ){
286     sign = -1;
287     z+=incr;
288   }else if( *z=='+' ){
289     z+=incr;
290   }
291 
292   /* skip leading zeroes */
293   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
294 
295   /* copy max significant digits to significand */
296   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
297     s = s*10 + (*z - '0');
298     z+=incr, nDigits++;
299   }
300 
301   /* skip non-significant significand digits
302   ** (increase exponent by d to shift decimal left) */
303   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
304   if( z>=zEnd ) goto do_atof_calc;
305 
306   /* if decimal point is present */
307   if( *z=='.' ){
308     z+=incr;
309     /* copy digits from after decimal to significand
310     ** (decrease exponent by d to shift decimal right) */
311     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
312       s = s*10 + (*z - '0');
313       z+=incr, nDigits++, d--;
314     }
315     /* skip non-significant digits */
316     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
317   }
318   if( z>=zEnd ) goto do_atof_calc;
319 
320   /* if exponent is present */
321   if( *z=='e' || *z=='E' ){
322     z+=incr;
323     eValid = 0;
324     if( z>=zEnd ) goto do_atof_calc;
325     /* get sign of exponent */
326     if( *z=='-' ){
327       esign = -1;
328       z+=incr;
329     }else if( *z=='+' ){
330       z+=incr;
331     }
332     /* copy digits to exponent */
333     while( z<zEnd && sqlite3Isdigit(*z) ){
334       e = e*10 + (*z - '0');
335       z+=incr;
336       eValid = 1;
337     }
338   }
339 
340   /* skip trailing spaces */
341   if( nDigits && eValid ){
342     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
343   }
344 
345 do_atof_calc:
346   /* adjust exponent by d, and update sign */
347   e = (e*esign) + d;
348   if( e<0 ) {
349     esign = -1;
350     e *= -1;
351   } else {
352     esign = 1;
353   }
354 
355   /* if 0 significand */
356   if( !s ) {
357     /* In the IEEE 754 standard, zero is signed.
358     ** Add the sign if we've seen at least one digit */
359     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
360   } else {
361     /* attempt to reduce exponent */
362     if( esign>0 ){
363       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
364     }else{
365       while( !(s%10) && e>0 ) e--,s/=10;
366     }
367 
368     /* adjust the sign of significand */
369     s = sign<0 ? -s : s;
370 
371     /* if exponent, scale significand as appropriate
372     ** and store in result. */
373     if( e ){
374       double scale = 1.0;
375       /* attempt to handle extremely small/large numbers better */
376       if( e>307 && e<342 ){
377         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
378         if( esign<0 ){
379           result = s / scale;
380           result /= 1.0e+308;
381         }else{
382           result = s * scale;
383           result *= 1.0e+308;
384         }
385       }else{
386         /* 1.0e+22 is the largest power of 10 than can be
387         ** represented exactly. */
388         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
389         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
390         if( esign<0 ){
391           result = s / scale;
392         }else{
393           result = s * scale;
394         }
395       }
396     } else {
397       result = (double)s;
398     }
399   }
400 
401   /* store the result */
402   *pResult = result;
403 
404   /* return true if number and no extra non-whitespace chracters after */
405   return z>=zEnd && nDigits>0 && eValid;
406 #else
407   return !sqlite3Atoi64(z, pResult, length, enc);
408 #endif /* SQLITE_OMIT_FLOATING_POINT */
409 }
410 
411 /*
412 ** Compare the 19-character string zNum against the text representation
413 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
414 ** if zNum is less than, equal to, or greater than the string.
415 ** Note that zNum must contain exactly 19 characters.
416 **
417 ** Unlike memcmp() this routine is guaranteed to return the difference
418 ** in the values of the last digit if the only difference is in the
419 ** last digit.  So, for example,
420 **
421 **      compare2pow63("9223372036854775800", 1)
422 **
423 ** will return -8.
424 */
425 static int compare2pow63(const char *zNum, int incr){
426   int c = 0;
427   int i;
428                     /* 012345678901234567 */
429   const char *pow63 = "922337203685477580";
430   for(i=0; c==0 && i<18; i++){
431     c = (zNum[i*incr]-pow63[i])*10;
432   }
433   if( c==0 ){
434     c = zNum[18*incr] - '8';
435     testcase( c==(-1) );
436     testcase( c==0 );
437     testcase( c==(+1) );
438   }
439   return c;
440 }
441 
442 
443 /*
444 ** Convert zNum to a 64-bit signed integer and write
445 ** the value of the integer into *pNum.
446 ** If zNum is exactly 9223372036854665808, return 2.
447 ** This is a special case as the context will determine
448 ** if it is too big (used as a negative).
449 ** If zNum is not an integer or is an integer that
450 ** is too large to be expressed with 64 bits,
451 ** then return 1.  Otherwise return 0.
452 **
453 ** length is the number of bytes in the string (bytes, not characters).
454 ** The string is not necessarily zero-terminated.  The encoding is
455 ** given by enc.
456 */
457 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
458   int incr = (enc==SQLITE_UTF8?1:2);
459   i64 v = 0;
460   int neg = 0; /* assume positive */
461   int i;
462   int c = 0;
463   const char *zStart;
464   const char *zEnd = zNum + length;
465   if( enc==SQLITE_UTF16BE ) zNum++;
466   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
467   if( zNum>=zEnd ) goto do_atoi_calc;
468   if( *zNum=='-' ){
469     neg = 1;
470     zNum+=incr;
471   }else if( *zNum=='+' ){
472     zNum+=incr;
473   }
474 do_atoi_calc:
475   zStart = zNum;
476   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
477   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
478     v = v*10 + c - '0';
479   }
480   *pNum = neg ? -v : v;
481   testcase( i==18 );
482   testcase( i==19 );
483   testcase( i==20 );
484   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
485     /* zNum is empty or contains non-numeric text or is longer
486     ** than 19 digits (thus guaranteeing that it is too large) */
487     return 1;
488   }else if( i<19*incr ){
489     /* Less than 19 digits, so we know that it fits in 64 bits */
490     return 0;
491   }else{
492     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
493     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
494     ** is 2^63. Return 1 if to large */
495     c=compare2pow63(zNum, incr);
496     if( c==0 && neg==0 ) return 2; /* too big, exactly 9223372036854665808 */
497     return c<neg ? 0 : 1;
498   }
499 }
500 
501 /*
502 ** If zNum represents an integer that will fit in 32-bits, then set
503 ** *pValue to that integer and return true.  Otherwise return false.
504 **
505 ** Any non-numeric characters that following zNum are ignored.
506 ** This is different from sqlite3Atoi64() which requires the
507 ** input number to be zero-terminated.
508 */
509 int sqlite3GetInt32(const char *zNum, int *pValue){
510   sqlite_int64 v = 0;
511   int i, c;
512   int neg = 0;
513   if( zNum[0]=='-' ){
514     neg = 1;
515     zNum++;
516   }else if( zNum[0]=='+' ){
517     zNum++;
518   }
519   while( zNum[0]=='0' ) zNum++;
520   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
521     v = v*10 + c;
522   }
523 
524   /* The longest decimal representation of a 32 bit integer is 10 digits:
525   **
526   **             1234567890
527   **     2^31 -> 2147483648
528   */
529   testcase( i==10 );
530   if( i>10 ){
531     return 0;
532   }
533   testcase( v-neg==2147483647 );
534   if( v-neg>2147483647 ){
535     return 0;
536   }
537   if( neg ){
538     v = -v;
539   }
540   *pValue = (int)v;
541   return 1;
542 }
543 
544 /*
545 ** The variable-length integer encoding is as follows:
546 **
547 ** KEY:
548 **         A = 0xxxxxxx    7 bits of data and one flag bit
549 **         B = 1xxxxxxx    7 bits of data and one flag bit
550 **         C = xxxxxxxx    8 bits of data
551 **
552 **  7 bits - A
553 ** 14 bits - BA
554 ** 21 bits - BBA
555 ** 28 bits - BBBA
556 ** 35 bits - BBBBA
557 ** 42 bits - BBBBBA
558 ** 49 bits - BBBBBBA
559 ** 56 bits - BBBBBBBA
560 ** 64 bits - BBBBBBBBC
561 */
562 
563 /*
564 ** Write a 64-bit variable-length integer to memory starting at p[0].
565 ** The length of data write will be between 1 and 9 bytes.  The number
566 ** of bytes written is returned.
567 **
568 ** A variable-length integer consists of the lower 7 bits of each byte
569 ** for all bytes that have the 8th bit set and one byte with the 8th
570 ** bit clear.  Except, if we get to the 9th byte, it stores the full
571 ** 8 bits and is the last byte.
572 */
573 int sqlite3PutVarint(unsigned char *p, u64 v){
574   int i, j, n;
575   u8 buf[10];
576   if( v & (((u64)0xff000000)<<32) ){
577     p[8] = (u8)v;
578     v >>= 8;
579     for(i=7; i>=0; i--){
580       p[i] = (u8)((v & 0x7f) | 0x80);
581       v >>= 7;
582     }
583     return 9;
584   }
585   n = 0;
586   do{
587     buf[n++] = (u8)((v & 0x7f) | 0x80);
588     v >>= 7;
589   }while( v!=0 );
590   buf[0] &= 0x7f;
591   assert( n<=9 );
592   for(i=0, j=n-1; j>=0; j--, i++){
593     p[i] = buf[j];
594   }
595   return n;
596 }
597 
598 /*
599 ** This routine is a faster version of sqlite3PutVarint() that only
600 ** works for 32-bit positive integers and which is optimized for
601 ** the common case of small integers.  A MACRO version, putVarint32,
602 ** is provided which inlines the single-byte case.  All code should use
603 ** the MACRO version as this function assumes the single-byte case has
604 ** already been handled.
605 */
606 int sqlite3PutVarint32(unsigned char *p, u32 v){
607 #ifndef putVarint32
608   if( (v & ~0x7f)==0 ){
609     p[0] = v;
610     return 1;
611   }
612 #endif
613   if( (v & ~0x3fff)==0 ){
614     p[0] = (u8)((v>>7) | 0x80);
615     p[1] = (u8)(v & 0x7f);
616     return 2;
617   }
618   return sqlite3PutVarint(p, v);
619 }
620 
621 /*
622 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
623 ** are defined here rather than simply putting the constant expressions
624 ** inline in order to work around bugs in the RVT compiler.
625 **
626 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
627 **
628 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
629 */
630 #define SLOT_2_0     0x001fc07f
631 #define SLOT_4_2_0   0xf01fc07f
632 
633 
634 /*
635 ** Read a 64-bit variable-length integer from memory starting at p[0].
636 ** Return the number of bytes read.  The value is stored in *v.
637 */
638 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
639   u32 a,b,s;
640 
641   a = *p;
642   /* a: p0 (unmasked) */
643   if (!(a&0x80))
644   {
645     *v = a;
646     return 1;
647   }
648 
649   p++;
650   b = *p;
651   /* b: p1 (unmasked) */
652   if (!(b&0x80))
653   {
654     a &= 0x7f;
655     a = a<<7;
656     a |= b;
657     *v = a;
658     return 2;
659   }
660 
661   /* Verify that constants are precomputed correctly */
662   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
663   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
664 
665   p++;
666   a = a<<14;
667   a |= *p;
668   /* a: p0<<14 | p2 (unmasked) */
669   if (!(a&0x80))
670   {
671     a &= SLOT_2_0;
672     b &= 0x7f;
673     b = b<<7;
674     a |= b;
675     *v = a;
676     return 3;
677   }
678 
679   /* CSE1 from below */
680   a &= SLOT_2_0;
681   p++;
682   b = b<<14;
683   b |= *p;
684   /* b: p1<<14 | p3 (unmasked) */
685   if (!(b&0x80))
686   {
687     b &= SLOT_2_0;
688     /* moved CSE1 up */
689     /* a &= (0x7f<<14)|(0x7f); */
690     a = a<<7;
691     a |= b;
692     *v = a;
693     return 4;
694   }
695 
696   /* a: p0<<14 | p2 (masked) */
697   /* b: p1<<14 | p3 (unmasked) */
698   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
699   /* moved CSE1 up */
700   /* a &= (0x7f<<14)|(0x7f); */
701   b &= SLOT_2_0;
702   s = a;
703   /* s: p0<<14 | p2 (masked) */
704 
705   p++;
706   a = a<<14;
707   a |= *p;
708   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
709   if (!(a&0x80))
710   {
711     /* we can skip these cause they were (effectively) done above in calc'ing s */
712     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
713     /* b &= (0x7f<<14)|(0x7f); */
714     b = b<<7;
715     a |= b;
716     s = s>>18;
717     *v = ((u64)s)<<32 | a;
718     return 5;
719   }
720 
721   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
722   s = s<<7;
723   s |= b;
724   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
725 
726   p++;
727   b = b<<14;
728   b |= *p;
729   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
730   if (!(b&0x80))
731   {
732     /* we can skip this cause it was (effectively) done above in calc'ing s */
733     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
734     a &= SLOT_2_0;
735     a = a<<7;
736     a |= b;
737     s = s>>18;
738     *v = ((u64)s)<<32 | a;
739     return 6;
740   }
741 
742   p++;
743   a = a<<14;
744   a |= *p;
745   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
746   if (!(a&0x80))
747   {
748     a &= SLOT_4_2_0;
749     b &= SLOT_2_0;
750     b = b<<7;
751     a |= b;
752     s = s>>11;
753     *v = ((u64)s)<<32 | a;
754     return 7;
755   }
756 
757   /* CSE2 from below */
758   a &= SLOT_2_0;
759   p++;
760   b = b<<14;
761   b |= *p;
762   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
763   if (!(b&0x80))
764   {
765     b &= SLOT_4_2_0;
766     /* moved CSE2 up */
767     /* a &= (0x7f<<14)|(0x7f); */
768     a = a<<7;
769     a |= b;
770     s = s>>4;
771     *v = ((u64)s)<<32 | a;
772     return 8;
773   }
774 
775   p++;
776   a = a<<15;
777   a |= *p;
778   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
779 
780   /* moved CSE2 up */
781   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
782   b &= SLOT_2_0;
783   b = b<<8;
784   a |= b;
785 
786   s = s<<4;
787   b = p[-4];
788   b &= 0x7f;
789   b = b>>3;
790   s |= b;
791 
792   *v = ((u64)s)<<32 | a;
793 
794   return 9;
795 }
796 
797 /*
798 ** Read a 32-bit variable-length integer from memory starting at p[0].
799 ** Return the number of bytes read.  The value is stored in *v.
800 **
801 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
802 ** integer, then set *v to 0xffffffff.
803 **
804 ** A MACRO version, getVarint32, is provided which inlines the
805 ** single-byte case.  All code should use the MACRO version as
806 ** this function assumes the single-byte case has already been handled.
807 */
808 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
809   u32 a,b;
810 
811   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
812   ** by the getVarin32() macro */
813   a = *p;
814   /* a: p0 (unmasked) */
815 #ifndef getVarint32
816   if (!(a&0x80))
817   {
818     /* Values between 0 and 127 */
819     *v = a;
820     return 1;
821   }
822 #endif
823 
824   /* The 2-byte case */
825   p++;
826   b = *p;
827   /* b: p1 (unmasked) */
828   if (!(b&0x80))
829   {
830     /* Values between 128 and 16383 */
831     a &= 0x7f;
832     a = a<<7;
833     *v = a | b;
834     return 2;
835   }
836 
837   /* The 3-byte case */
838   p++;
839   a = a<<14;
840   a |= *p;
841   /* a: p0<<14 | p2 (unmasked) */
842   if (!(a&0x80))
843   {
844     /* Values between 16384 and 2097151 */
845     a &= (0x7f<<14)|(0x7f);
846     b &= 0x7f;
847     b = b<<7;
848     *v = a | b;
849     return 3;
850   }
851 
852   /* A 32-bit varint is used to store size information in btrees.
853   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
854   ** A 3-byte varint is sufficient, for example, to record the size
855   ** of a 1048569-byte BLOB or string.
856   **
857   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
858   ** rare larger cases can be handled by the slower 64-bit varint
859   ** routine.
860   */
861 #if 1
862   {
863     u64 v64;
864     u8 n;
865 
866     p -= 2;
867     n = sqlite3GetVarint(p, &v64);
868     assert( n>3 && n<=9 );
869     if( (v64 & SQLITE_MAX_U32)!=v64 ){
870       *v = 0xffffffff;
871     }else{
872       *v = (u32)v64;
873     }
874     return n;
875   }
876 
877 #else
878   /* For following code (kept for historical record only) shows an
879   ** unrolling for the 3- and 4-byte varint cases.  This code is
880   ** slightly faster, but it is also larger and much harder to test.
881   */
882   p++;
883   b = b<<14;
884   b |= *p;
885   /* b: p1<<14 | p3 (unmasked) */
886   if (!(b&0x80))
887   {
888     /* Values between 2097152 and 268435455 */
889     b &= (0x7f<<14)|(0x7f);
890     a &= (0x7f<<14)|(0x7f);
891     a = a<<7;
892     *v = a | b;
893     return 4;
894   }
895 
896   p++;
897   a = a<<14;
898   a |= *p;
899   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
900   if (!(a&0x80))
901   {
902     /* Values  between 268435456 and 34359738367 */
903     a &= SLOT_4_2_0;
904     b &= SLOT_4_2_0;
905     b = b<<7;
906     *v = a | b;
907     return 5;
908   }
909 
910   /* We can only reach this point when reading a corrupt database
911   ** file.  In that case we are not in any hurry.  Use the (relatively
912   ** slow) general-purpose sqlite3GetVarint() routine to extract the
913   ** value. */
914   {
915     u64 v64;
916     u8 n;
917 
918     p -= 4;
919     n = sqlite3GetVarint(p, &v64);
920     assert( n>5 && n<=9 );
921     *v = (u32)v64;
922     return n;
923   }
924 #endif
925 }
926 
927 /*
928 ** Return the number of bytes that will be needed to store the given
929 ** 64-bit integer.
930 */
931 int sqlite3VarintLen(u64 v){
932   int i = 0;
933   do{
934     i++;
935     v >>= 7;
936   }while( v!=0 && ALWAYS(i<9) );
937   return i;
938 }
939 
940 
941 /*
942 ** Read or write a four-byte big-endian integer value.
943 */
944 u32 sqlite3Get4byte(const u8 *p){
945   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
946 }
947 void sqlite3Put4byte(unsigned char *p, u32 v){
948   p[0] = (u8)(v>>24);
949   p[1] = (u8)(v>>16);
950   p[2] = (u8)(v>>8);
951   p[3] = (u8)v;
952 }
953 
954 
955 
956 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
957 /*
958 ** Translate a single byte of Hex into an integer.
959 ** This routine only works if h really is a valid hexadecimal
960 ** character:  0..9a..fA..F
961 */
962 static u8 hexToInt(int h){
963   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
964 #ifdef SQLITE_ASCII
965   h += 9*(1&(h>>6));
966 #endif
967 #ifdef SQLITE_EBCDIC
968   h += 9*(1&~(h>>4));
969 #endif
970   return (u8)(h & 0xf);
971 }
972 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
973 
974 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
975 /*
976 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
977 ** value.  Return a pointer to its binary value.  Space to hold the
978 ** binary value has been obtained from malloc and must be freed by
979 ** the calling routine.
980 */
981 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
982   char *zBlob;
983   int i;
984 
985   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
986   n--;
987   if( zBlob ){
988     for(i=0; i<n; i+=2){
989       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
990     }
991     zBlob[i/2] = 0;
992   }
993   return zBlob;
994 }
995 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
996 
997 /*
998 ** Log an error that is an API call on a connection pointer that should
999 ** not have been used.  The "type" of connection pointer is given as the
1000 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
1001 */
1002 static void logBadConnection(const char *zType){
1003   sqlite3_log(SQLITE_MISUSE,
1004      "API call with %s database connection pointer",
1005      zType
1006   );
1007 }
1008 
1009 /*
1010 ** Check to make sure we have a valid db pointer.  This test is not
1011 ** foolproof but it does provide some measure of protection against
1012 ** misuse of the interface such as passing in db pointers that are
1013 ** NULL or which have been previously closed.  If this routine returns
1014 ** 1 it means that the db pointer is valid and 0 if it should not be
1015 ** dereferenced for any reason.  The calling function should invoke
1016 ** SQLITE_MISUSE immediately.
1017 **
1018 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1019 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1020 ** open properly and is not fit for general use but which can be
1021 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
1022 */
1023 int sqlite3SafetyCheckOk(sqlite3 *db){
1024   u32 magic;
1025   if( db==0 ){
1026     logBadConnection("NULL");
1027     return 0;
1028   }
1029   magic = db->magic;
1030   if( magic!=SQLITE_MAGIC_OPEN ){
1031     if( sqlite3SafetyCheckSickOrOk(db) ){
1032       testcase( sqlite3GlobalConfig.xLog!=0 );
1033       logBadConnection("unopened");
1034     }
1035     return 0;
1036   }else{
1037     return 1;
1038   }
1039 }
1040 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1041   u32 magic;
1042   magic = db->magic;
1043   if( magic!=SQLITE_MAGIC_SICK &&
1044       magic!=SQLITE_MAGIC_OPEN &&
1045       magic!=SQLITE_MAGIC_BUSY ){
1046     testcase( sqlite3GlobalConfig.xLog!=0 );
1047     logBadConnection("invalid");
1048     return 0;
1049   }else{
1050     return 1;
1051   }
1052 }
1053