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