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