xref: /sqlite-3.40.0/src/util.c (revision 6695f47e)
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     }else{
368       while( !(s%10) && e>0 ) e--,s/=10;
369     }
370 
371     /* adjust the sign of significand */
372     s = sign<0 ? -s : s;
373 
374     /* if exponent, scale significand as appropriate
375     ** and store in result. */
376     if( e ){
377       double scale = 1.0;
378       /* attempt to handle extremely small/large numbers better */
379       if( e>307 && e<342 ){
380         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
381         if( esign<0 ){
382           result = s / scale;
383           result /= 1.0e+308;
384         }else{
385           result = s * scale;
386           result *= 1.0e+308;
387         }
388       }else{
389         /* 1.0e+22 is the largest power of 10 than can be
390         ** represented exactly. */
391         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
392         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
393         if( esign<0 ){
394           result = s / scale;
395         }else{
396           result = s * scale;
397         }
398       }
399     } else {
400       result = (double)s;
401     }
402   }
403 
404   /* store the result */
405   *pResult = result;
406 
407   /* return number of characters used */
408   return (int)(z - zBegin);
409 #else
410   return sqlite3Atoi64(z, pResult);
411 #endif /* SQLITE_OMIT_FLOATING_POINT */
412 }
413 
414 /*
415 ** Compare the 19-character string zNum against the text representation
416 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
417 ** if zNum is less than, equal to, or greater than the string.
418 **
419 ** Unlike memcmp() this routine is guaranteed to return the difference
420 ** in the values of the last digit if the only difference is in the
421 ** last digit.  So, for example,
422 **
423 **      compare2pow63("9223372036854775800")
424 **
425 ** will return -8.
426 */
427 static int compare2pow63(const char *zNum){
428   int c;
429   c = memcmp(zNum,"922337203685477580",18)*10;
430   if( c==0 ){
431     c = zNum[18] - '8';
432   }
433   return c;
434 }
435 
436 
437 /*
438 ** Return TRUE if zNum is a 64-bit signed integer and write
439 ** the value of the integer into *pNum.  If zNum is not an integer
440 ** or is an integer that is too large to be expressed with 64 bits,
441 ** then return false.
442 **
443 ** When this routine was originally written it dealt with only
444 ** 32-bit numbers.  At that time, it was much faster than the
445 ** atoi() library routine in RedHat 7.2.
446 */
447 int sqlite3Atoi64(const char *zNum, i64 *pNum){
448   i64 v = 0;
449   int neg;
450   int i, c;
451   const char *zStart;
452   while( sqlite3Isspace(*zNum) ) zNum++;
453   if( *zNum=='-' ){
454     neg = 1;
455     zNum++;
456   }else if( *zNum=='+' ){
457     neg = 0;
458     zNum++;
459   }else{
460     neg = 0;
461   }
462   zStart = zNum;
463   while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
464   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
465     v = v*10 + c - '0';
466   }
467   *pNum = neg ? -v : v;
468   if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
469     /* zNum is empty or contains non-numeric text or is longer
470     ** than 19 digits (thus guaranting that it is too large) */
471     return 0;
472   }else if( i<19 ){
473     /* Less than 19 digits, so we know that it fits in 64 bits */
474     return 1;
475   }else{
476     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
477     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
478     ** is 2^63. */
479     return compare2pow63(zNum)<neg;
480   }
481 }
482 
483 /*
484 ** The string zNum represents an unsigned integer.  The zNum string
485 ** consists of one or more digit characters and is terminated by
486 ** a zero character.  Any stray characters in zNum result in undefined
487 ** behavior.
488 **
489 ** If the unsigned integer that zNum represents will fit in a
490 ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
491 **
492 ** If the negFlag parameter is true, that means that zNum really represents
493 ** a negative number.  (The leading "-" is omitted from zNum.)  This
494 ** parameter is needed to determine a boundary case.  A string
495 ** of "9223373036854775808" returns false if negFlag is false or true
496 ** if negFlag is true.
497 **
498 ** Leading zeros are ignored.
499 */
500 int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
501   int i;
502   int neg = 0;
503 
504   assert( zNum[0]>='0' && zNum[0]<='9' ); /* zNum is an unsigned number */
505 
506   if( negFlag ) neg = 1-neg;
507   while( *zNum=='0' ){
508     zNum++;   /* Skip leading zeros.  Ticket #2454 */
509   }
510   for(i=0; zNum[i]; i++){ assert( zNum[i]>='0' && zNum[i]<='9' ); }
511   if( i<19 ){
512     /* Guaranteed to fit if less than 19 digits */
513     return 1;
514   }else if( i>19 ){
515     /* Guaranteed to be too big if greater than 19 digits */
516     return 0;
517   }else{
518     /* Compare against 2^63. */
519     return compare2pow63(zNum)<neg;
520   }
521 }
522 
523 /*
524 ** If zNum represents an integer that will fit in 32-bits, then set
525 ** *pValue to that integer and return true.  Otherwise return false.
526 **
527 ** Any non-numeric characters that following zNum are ignored.
528 ** This is different from sqlite3Atoi64() which requires the
529 ** input number to be zero-terminated.
530 */
531 int sqlite3GetInt32(const char *zNum, int *pValue){
532   sqlite_int64 v = 0;
533   int i, c;
534   int neg = 0;
535   if( zNum[0]=='-' ){
536     neg = 1;
537     zNum++;
538   }else if( zNum[0]=='+' ){
539     zNum++;
540   }
541   while( zNum[0]=='0' ) zNum++;
542   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
543     v = v*10 + c;
544   }
545 
546   /* The longest decimal representation of a 32 bit integer is 10 digits:
547   **
548   **             1234567890
549   **     2^31 -> 2147483648
550   */
551   if( i>10 ){
552     return 0;
553   }
554   if( v-neg>2147483647 ){
555     return 0;
556   }
557   if( neg ){
558     v = -v;
559   }
560   *pValue = (int)v;
561   return 1;
562 }
563 
564 /*
565 ** The variable-length integer encoding is as follows:
566 **
567 ** KEY:
568 **         A = 0xxxxxxx    7 bits of data and one flag bit
569 **         B = 1xxxxxxx    7 bits of data and one flag bit
570 **         C = xxxxxxxx    8 bits of data
571 **
572 **  7 bits - A
573 ** 14 bits - BA
574 ** 21 bits - BBA
575 ** 28 bits - BBBA
576 ** 35 bits - BBBBA
577 ** 42 bits - BBBBBA
578 ** 49 bits - BBBBBBA
579 ** 56 bits - BBBBBBBA
580 ** 64 bits - BBBBBBBBC
581 */
582 
583 /*
584 ** Write a 64-bit variable-length integer to memory starting at p[0].
585 ** The length of data write will be between 1 and 9 bytes.  The number
586 ** of bytes written is returned.
587 **
588 ** A variable-length integer consists of the lower 7 bits of each byte
589 ** for all bytes that have the 8th bit set and one byte with the 8th
590 ** bit clear.  Except, if we get to the 9th byte, it stores the full
591 ** 8 bits and is the last byte.
592 */
593 int sqlite3PutVarint(unsigned char *p, u64 v){
594   int i, j, n;
595   u8 buf[10];
596   if( v & (((u64)0xff000000)<<32) ){
597     p[8] = (u8)v;
598     v >>= 8;
599     for(i=7; i>=0; i--){
600       p[i] = (u8)((v & 0x7f) | 0x80);
601       v >>= 7;
602     }
603     return 9;
604   }
605   n = 0;
606   do{
607     buf[n++] = (u8)((v & 0x7f) | 0x80);
608     v >>= 7;
609   }while( v!=0 );
610   buf[0] &= 0x7f;
611   assert( n<=9 );
612   for(i=0, j=n-1; j>=0; j--, i++){
613     p[i] = buf[j];
614   }
615   return n;
616 }
617 
618 /*
619 ** This routine is a faster version of sqlite3PutVarint() that only
620 ** works for 32-bit positive integers and which is optimized for
621 ** the common case of small integers.  A MACRO version, putVarint32,
622 ** is provided which inlines the single-byte case.  All code should use
623 ** the MACRO version as this function assumes the single-byte case has
624 ** already been handled.
625 */
626 int sqlite3PutVarint32(unsigned char *p, u32 v){
627 #ifndef putVarint32
628   if( (v & ~0x7f)==0 ){
629     p[0] = v;
630     return 1;
631   }
632 #endif
633   if( (v & ~0x3fff)==0 ){
634     p[0] = (u8)((v>>7) | 0x80);
635     p[1] = (u8)(v & 0x7f);
636     return 2;
637   }
638   return sqlite3PutVarint(p, v);
639 }
640 
641 /*
642 ** Read a 64-bit variable-length integer from memory starting at p[0].
643 ** Return the number of bytes read.  The value is stored in *v.
644 */
645 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
646   u32 a,b,s;
647 
648   a = *p;
649   /* a: p0 (unmasked) */
650   if (!(a&0x80))
651   {
652     *v = a;
653     return 1;
654   }
655 
656   p++;
657   b = *p;
658   /* b: p1 (unmasked) */
659   if (!(b&0x80))
660   {
661     a &= 0x7f;
662     a = a<<7;
663     a |= b;
664     *v = a;
665     return 2;
666   }
667 
668   p++;
669   a = a<<14;
670   a |= *p;
671   /* a: p0<<14 | p2 (unmasked) */
672   if (!(a&0x80))
673   {
674     a &= (0x7f<<14)|(0x7f);
675     b &= 0x7f;
676     b = b<<7;
677     a |= b;
678     *v = a;
679     return 3;
680   }
681 
682   /* CSE1 from below */
683   a &= (0x7f<<14)|(0x7f);
684   p++;
685   b = b<<14;
686   b |= *p;
687   /* b: p1<<14 | p3 (unmasked) */
688   if (!(b&0x80))
689   {
690     b &= (0x7f<<14)|(0x7f);
691     /* moved CSE1 up */
692     /* a &= (0x7f<<14)|(0x7f); */
693     a = a<<7;
694     a |= b;
695     *v = a;
696     return 4;
697   }
698 
699   /* a: p0<<14 | p2 (masked) */
700   /* b: p1<<14 | p3 (unmasked) */
701   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
702   /* moved CSE1 up */
703   /* a &= (0x7f<<14)|(0x7f); */
704   b &= (0x7f<<14)|(0x7f);
705   s = a;
706   /* s: p0<<14 | p2 (masked) */
707 
708   p++;
709   a = a<<14;
710   a |= *p;
711   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
712   if (!(a&0x80))
713   {
714     /* we can skip these cause they were (effectively) done above in calc'ing s */
715     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
716     /* b &= (0x7f<<14)|(0x7f); */
717     b = b<<7;
718     a |= b;
719     s = s>>18;
720     *v = ((u64)s)<<32 | a;
721     return 5;
722   }
723 
724   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
725   s = s<<7;
726   s |= b;
727   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
728 
729   p++;
730   b = b<<14;
731   b |= *p;
732   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
733   if (!(b&0x80))
734   {
735     /* we can skip this cause it was (effectively) done above in calc'ing s */
736     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
737     a &= (0x7f<<14)|(0x7f);
738     a = a<<7;
739     a |= b;
740     s = s>>18;
741     *v = ((u64)s)<<32 | a;
742     return 6;
743   }
744 
745   p++;
746   a = a<<14;
747   a |= *p;
748   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
749   if (!(a&0x80))
750   {
751     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
752     b &= (0x7f<<14)|(0x7f);
753     b = b<<7;
754     a |= b;
755     s = s>>11;
756     *v = ((u64)s)<<32 | a;
757     return 7;
758   }
759 
760   /* CSE2 from below */
761   a &= (0x7f<<14)|(0x7f);
762   p++;
763   b = b<<14;
764   b |= *p;
765   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
766   if (!(b&0x80))
767   {
768     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
769     /* moved CSE2 up */
770     /* a &= (0x7f<<14)|(0x7f); */
771     a = a<<7;
772     a |= b;
773     s = s>>4;
774     *v = ((u64)s)<<32 | a;
775     return 8;
776   }
777 
778   p++;
779   a = a<<15;
780   a |= *p;
781   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
782 
783   /* moved CSE2 up */
784   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
785   b &= (0x7f<<14)|(0x7f);
786   b = b<<8;
787   a |= b;
788 
789   s = s<<4;
790   b = p[-4];
791   b &= 0x7f;
792   b = b>>3;
793   s |= b;
794 
795   *v = ((u64)s)<<32 | a;
796 
797   return 9;
798 }
799 
800 /*
801 ** Read a 32-bit variable-length integer from memory starting at p[0].
802 ** Return the number of bytes read.  The value is stored in *v.
803 **
804 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
805 ** integer, then set *v to 0xffffffff.
806 **
807 ** A MACRO version, getVarint32, is provided which inlines the
808 ** single-byte case.  All code should use the MACRO version as
809 ** this function assumes the single-byte case has already been handled.
810 */
811 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
812   u32 a,b;
813 
814   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
815   ** by the getVarin32() macro */
816   a = *p;
817   /* a: p0 (unmasked) */
818 #ifndef getVarint32
819   if (!(a&0x80))
820   {
821     /* Values between 0 and 127 */
822     *v = a;
823     return 1;
824   }
825 #endif
826 
827   /* The 2-byte case */
828   p++;
829   b = *p;
830   /* b: p1 (unmasked) */
831   if (!(b&0x80))
832   {
833     /* Values between 128 and 16383 */
834     a &= 0x7f;
835     a = a<<7;
836     *v = a | b;
837     return 2;
838   }
839 
840   /* The 3-byte case */
841   p++;
842   a = a<<14;
843   a |= *p;
844   /* a: p0<<14 | p2 (unmasked) */
845   if (!(a&0x80))
846   {
847     /* Values between 16384 and 2097151 */
848     a &= (0x7f<<14)|(0x7f);
849     b &= 0x7f;
850     b = b<<7;
851     *v = a | b;
852     return 3;
853   }
854 
855   /* A 32-bit varint is used to store size information in btrees.
856   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
857   ** A 3-byte varint is sufficient, for example, to record the size
858   ** of a 1048569-byte BLOB or string.
859   **
860   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
861   ** rare larger cases can be handled by the slower 64-bit varint
862   ** routine.
863   */
864 #if 1
865   {
866     u64 v64;
867     u8 n;
868 
869     p -= 2;
870     n = sqlite3GetVarint(p, &v64);
871     assert( n>3 && n<=9 );
872     if( (v64 & SQLITE_MAX_U32)!=v64 ){
873       *v = 0xffffffff;
874     }else{
875       *v = (u32)v64;
876     }
877     return n;
878   }
879 
880 #else
881   /* For following code (kept for historical record only) shows an
882   ** unrolling for the 3- and 4-byte varint cases.  This code is
883   ** slightly faster, but it is also larger and much harder to test.
884   */
885   p++;
886   b = b<<14;
887   b |= *p;
888   /* b: p1<<14 | p3 (unmasked) */
889   if (!(b&0x80))
890   {
891     /* Values between 2097152 and 268435455 */
892     b &= (0x7f<<14)|(0x7f);
893     a &= (0x7f<<14)|(0x7f);
894     a = a<<7;
895     *v = a | b;
896     return 4;
897   }
898 
899   p++;
900   a = a<<14;
901   a |= *p;
902   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
903   if (!(a&0x80))
904   {
905     /* Walues  between 268435456 and 34359738367 */
906     a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
907     b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
908     b = b<<7;
909     *v = a | b;
910     return 5;
911   }
912 
913   /* We can only reach this point when reading a corrupt database
914   ** file.  In that case we are not in any hurry.  Use the (relatively
915   ** slow) general-purpose sqlite3GetVarint() routine to extract the
916   ** value. */
917   {
918     u64 v64;
919     u8 n;
920 
921     p -= 4;
922     n = sqlite3GetVarint(p, &v64);
923     assert( n>5 && n<=9 );
924     *v = (u32)v64;
925     return n;
926   }
927 #endif
928 }
929 
930 /*
931 ** Return the number of bytes that will be needed to store the given
932 ** 64-bit integer.
933 */
934 int sqlite3VarintLen(u64 v){
935   int i = 0;
936   do{
937     i++;
938     v >>= 7;
939   }while( v!=0 && ALWAYS(i<9) );
940   return i;
941 }
942 
943 
944 /*
945 ** Read or write a four-byte big-endian integer value.
946 */
947 u32 sqlite3Get4byte(const u8 *p){
948   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
949 }
950 void sqlite3Put4byte(unsigned char *p, u32 v){
951   p[0] = (u8)(v>>24);
952   p[1] = (u8)(v>>16);
953   p[2] = (u8)(v>>8);
954   p[3] = (u8)v;
955 }
956 
957 
958 
959 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
960 /*
961 ** Translate a single byte of Hex into an integer.
962 ** This routine only works if h really is a valid hexadecimal
963 ** character:  0..9a..fA..F
964 */
965 static u8 hexToInt(int h){
966   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
967 #ifdef SQLITE_ASCII
968   h += 9*(1&(h>>6));
969 #endif
970 #ifdef SQLITE_EBCDIC
971   h += 9*(1&~(h>>4));
972 #endif
973   return (u8)(h & 0xf);
974 }
975 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
976 
977 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
978 /*
979 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
980 ** value.  Return a pointer to its binary value.  Space to hold the
981 ** binary value has been obtained from malloc and must be freed by
982 ** the calling routine.
983 */
984 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
985   char *zBlob;
986   int i;
987 
988   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
989   n--;
990   if( zBlob ){
991     for(i=0; i<n; i+=2){
992       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
993     }
994     zBlob[i/2] = 0;
995   }
996   return zBlob;
997 }
998 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
999 
1000 
1001 /*
1002 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1003 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1004 ** when this routine is called.
1005 **
1006 ** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
1007 ** value indicates that the database connection passed into the API is
1008 ** open and is not being used by another thread.  By changing the value
1009 ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
1010 ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
1011 ** when the API exits.
1012 **
1013 ** This routine is a attempt to detect if two threads use the
1014 ** same sqlite* pointer at the same time.  There is a race
1015 ** condition so it is possible that the error is not detected.
1016 ** But usually the problem will be seen.  The result will be an
1017 ** error which can be used to debug the application that is
1018 ** using SQLite incorrectly.
1019 **
1020 ** Ticket #202:  If db->magic is not a valid open value, take care not
1021 ** to modify the db structure at all.  It could be that db is a stale
1022 ** pointer.  In other words, it could be that there has been a prior
1023 ** call to sqlite3_close(db) and db has been deallocated.  And we do
1024 ** not want to write into deallocated memory.
1025 */
1026 #ifdef SQLITE_DEBUG
1027 int sqlite3SafetyOn(sqlite3 *db){
1028   if( db->magic==SQLITE_MAGIC_OPEN ){
1029     db->magic = SQLITE_MAGIC_BUSY;
1030     assert( sqlite3_mutex_held(db->mutex) );
1031     return 0;
1032   }else if( db->magic==SQLITE_MAGIC_BUSY ){
1033     db->magic = SQLITE_MAGIC_ERROR;
1034     db->u1.isInterrupted = 1;
1035   }
1036   return 1;
1037 }
1038 #endif
1039 
1040 /*
1041 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1042 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1043 ** when this routine is called.
1044 */
1045 #ifdef SQLITE_DEBUG
1046 int sqlite3SafetyOff(sqlite3 *db){
1047   if( db->magic==SQLITE_MAGIC_BUSY ){
1048     db->magic = SQLITE_MAGIC_OPEN;
1049     assert( sqlite3_mutex_held(db->mutex) );
1050     return 0;
1051   }else{
1052     db->magic = SQLITE_MAGIC_ERROR;
1053     db->u1.isInterrupted = 1;
1054     return 1;
1055   }
1056 }
1057 #endif
1058 
1059 /*
1060 ** Check to make sure we have a valid db pointer.  This test is not
1061 ** foolproof but it does provide some measure of protection against
1062 ** misuse of the interface such as passing in db pointers that are
1063 ** NULL or which have been previously closed.  If this routine returns
1064 ** 1 it means that the db pointer is valid and 0 if it should not be
1065 ** dereferenced for any reason.  The calling function should invoke
1066 ** SQLITE_MISUSE immediately.
1067 **
1068 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1069 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1070 ** open properly and is not fit for general use but which can be
1071 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
1072 */
1073 int sqlite3SafetyCheckOk(sqlite3 *db){
1074   u32 magic;
1075   if( db==0 ) return 0;
1076   magic = db->magic;
1077   if( magic!=SQLITE_MAGIC_OPEN
1078 #ifdef SQLITE_DEBUG
1079      && magic!=SQLITE_MAGIC_BUSY
1080 #endif
1081   ){
1082     return 0;
1083   }else{
1084     return 1;
1085   }
1086 }
1087 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1088   u32 magic;
1089   magic = db->magic;
1090   if( magic!=SQLITE_MAGIC_SICK &&
1091       magic!=SQLITE_MAGIC_OPEN &&
1092       magic!=SQLITE_MAGIC_BUSY ) return 0;
1093   return 1;
1094 }
1095