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