]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Changes to internal AtoF to improve "accuracy" when measured against built-in GCC...
authorshane <shane@noemail.net>
Mon, 7 Sep 2009 02:46:31 +0000 (02:46 +0000)
committershane <shane@noemail.net>
Mon, 7 Sep 2009 02:46:31 +0000 (02:46 +0000)
FossilOrigin-Name: 438e1577d0f9ceb2851ee0df0df18f0229eca491

manifest
manifest.uuid
src/util.c

index 8abf794db8542c5685a061fbc9ccf53f09a76cdf..d97af3eed7ac7ec9526cad737414578a1c52ba9a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,5 @@
------BEGIN PGP SIGNED MESSAGE-----
-Hash: SHA1
-
-C Add\sthe\s"unix-wfl"\sVFS\sthat\sdoes\swhole-file\slocking\sin\sorder\sto\shelp\sNFS\ndo\sbetter\scache\scoherency.
-D 2009-09-03T16:23:45
+C Changes\sto\sinternal\sAtoF\sto\simprove\s"accuracy"\swhen\smeasured\sagainst\sbuilt-in\sGCC\satof().
+D 2009-09-07T02:46:32
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 73ddeec9dd10b85876c5c2ce1fdce627e1dcc7f8
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -206,7 +203,7 @@ F src/tokenize.c af8a56e6a50c5042fc305bfa796275e9bf26ff2b
 F src/trigger.c 8158237fc5fa4b51a126a294791729c64d27a0a9
 F src/update.c 12ffd68c2f241c730d5e051eb29b23e90682ff8a
 F src/utf.c 3ca2c9461b8e942c68da28bfccd448663f536a6f
-F src/util.c efb5f8e533d4beef545cf765cab5f7920b4c75f9
+F src/util.c 7404ac078125aafc0e274b2d1a918c3f15b50294
 F src/vacuum.c 3fe0eebea6d2311c1c2ab2962887d11f7a4dcfb0
 F src/vdbe.c addf5f6fefe9b9c6ca8819efbf52cfce8bd68089
 F src/vdbe.h 080fe6bc1264438becb8bf9b9f3c84074c336b78
@@ -753,14 +750,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P f22e388727f0ba0f187cdee51ff8ba17a5d50b8a
-R 318d6752e52f7f4a033e61119f592643
-U drh
-Z 47965b78e770a8b1304310d0ee191e44
------BEGIN PGP SIGNATURE-----
-Version: GnuPG v1.4.6 (GNU/Linux)
-
-iD8DBQFKn+2UoxKgR168RlERAv/sAJ4sxtNY+4TxErcLxEqGtbpkH3VwnQCgi8kz
-7ozoBqiBdahO12HaBHgOl6c=
-=OZsT
------END PGP SIGNATURE-----
+P 2aeab80e5b84f5e94c5c99b4adeca805601c844b
+R 0b92cbb9074dfe690c0e4c14e6998bde
+U shane
+Z 82d0c356acbda85944f3ae28efe12d94
index d0e52015c57c67ea10183425452adb00e14e6a02..fb0b520154aa3de2e4fc5ab13dd6a751e3fec6be 100644 (file)
@@ -1 +1 @@
-2aeab80e5b84f5e94c5c99b4adeca805601c844b
\ No newline at end of file
+438e1577d0f9ceb2851ee0df0df18f0229eca491
\ No newline at end of file
index ef291b94b35f870ac5b431e8a7e192f0f0955b94..085d7140e4ae2e6cfffa32fc8b5ee2ca268c0d94 100644 (file)
-/*
-** 2001 September 15
-**
-** The author disclaims copyright to this source code.  In place of
-** a legal notice, here is a blessing:
-**
-**    May you do good and not evil.
-**    May you find forgiveness for yourself and forgive others.
-**    May you share freely, never taking more than you give.
-**
-*************************************************************************
-** Utility functions used throughout sqlite.
-**
-** This file contains functions for allocating memory, comparing
-** strings, and stuff like that.
-**
-*/
-#include "sqliteInt.h"
-#include <stdarg.h>
-#ifdef SQLITE_HAVE_ISNAN
-# include <math.h>
-#endif
-
-/*
-** Routine needed to support the testcase() macro.
-*/
-#ifdef SQLITE_COVERAGE_TEST
-void sqlite3Coverage(int x){
-  static int dummy = 0;
-  dummy += x;
-}
-#endif
-
-/*
-** Return true if the floating point value is Not a Number (NaN).
-**
-** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
-** Otherwise, we have our own implementation that works on most systems.
-*/
-int sqlite3IsNaN(double x){
-  int rc;   /* The value return */
-#if !defined(SQLITE_HAVE_ISNAN)
-  /*
-  ** Systems that support the isnan() library function should probably
-  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
-  ** found that many systems do not have a working isnan() function so
-  ** this implementation is provided as an alternative.
-  **
-  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
-  ** On the other hand, the use of -ffast-math comes with the following
-  ** warning:
-  **
-  **      This option [-ffast-math] should never be turned on by any
-  **      -O option since it can result in incorrect output for programs
-  **      which depend on an exact implementation of IEEE or ISO 
-  **      rules/specifications for math functions.
-  **
-  ** Under MSVC, this NaN test may fail if compiled with a floating-
-  ** point precision mode other than /fp:precise.  From the MSDN 
-  ** documentation:
-  **
-  **      The compiler [with /fp:precise] will properly handle comparisons 
-  **      involving NaN. For example, x != x evaluates to true if x is NaN 
-  **      ...
-  */
-#ifdef __FAST_MATH__
-# error SQLite will not work correctly with the -ffast-math option of GCC.
-#endif
-  volatile double y = x;
-  volatile double z = y;
-  rc = (y!=z);
-#else  /* if defined(SQLITE_HAVE_ISNAN) */
-  rc = isnan(x);
-#endif /* SQLITE_HAVE_ISNAN */
-  testcase( rc );
-  return rc;
-}
-
-/*
-** Compute a string length that is limited to what can be stored in
-** lower 30 bits of a 32-bit signed integer.
-**
-** The value returned will never be negative.  Nor will it ever be greater
-** than the actual length of the string.  For very long strings (greater
-** than 1GiB) the value returned might be less than the true string length.
-*/
-int sqlite3Strlen30(const char *z){
-  const char *z2 = z;
-  if( z==0 ) return 0;
-  while( *z2 ){ z2++; }
-  return 0x3fffffff & (int)(z2 - z);
-}
-
-/*
-** Set the most recent error code and error string for the sqlite
-** handle "db". The error code is set to "err_code".
-**
-** If it is not NULL, string zFormat specifies the format of the
-** error string in the style of the printf functions: The following
-** format characters are allowed:
-**
-**      %s      Insert a string
-**      %z      A string that should be freed after use
-**      %d      Insert an integer
-**      %T      Insert a token
-**      %S      Insert the first element of a SrcList
-**
-** zFormat and any string tokens that follow it are assumed to be
-** encoded in UTF-8.
-**
-** To clear the most recent error for sqlite handle "db", sqlite3Error
-** should be called with err_code set to SQLITE_OK and zFormat set
-** to NULL.
-*/
-void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
-  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
-    db->errCode = err_code;
-    if( zFormat ){
-      char *z;
-      va_list ap;
-      va_start(ap, zFormat);
-      z = sqlite3VMPrintf(db, zFormat, ap);
-      va_end(ap);
-      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
-    }else{
-      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
-    }
-  }
-}
-
-/*
-** Add an error message to pParse->zErrMsg and increment pParse->nErr.
-** The following formatting characters are allowed:
-**
-**      %s      Insert a string
-**      %z      A string that should be freed after use
-**      %d      Insert an integer
-**      %T      Insert a token
-**      %S      Insert the first element of a SrcList
-**
-** This function should be used to report any error that occurs whilst
-** compiling an SQL statement (i.e. within sqlite3_prepare()). The
-** last thing the sqlite3_prepare() function does is copy the error
-** stored by this function into the database handle using sqlite3Error().
-** Function sqlite3Error() should be used during statement execution
-** (sqlite3_step() etc.).
-*/
-void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
-  va_list ap;
-  sqlite3 *db = pParse->db;
-  pParse->nErr++;
-  sqlite3DbFree(db, pParse->zErrMsg);
-  va_start(ap, zFormat);
-  pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
-  va_end(ap);
-  pParse->rc = SQLITE_ERROR;
-}
-
-/*
-** Clear the error message in pParse, if any
-*/
-void sqlite3ErrorClear(Parse *pParse){
-  sqlite3DbFree(pParse->db, pParse->zErrMsg);
-  pParse->zErrMsg = 0;
-  pParse->nErr = 0;
-}
-
-/*
-** Convert an SQL-style quoted string into a normal string by removing
-** the quote characters.  The conversion is done in-place.  If the
-** input does not begin with a quote character, then this routine
-** is a no-op.
-**
-** The input string must be zero-terminated.  A new zero-terminator
-** is added to the dequoted string.
-**
-** The return value is -1 if no dequoting occurs or the length of the
-** dequoted string, exclusive of the zero terminator, if dequoting does
-** occur.
-**
-** 2002-Feb-14: This routine is extended to remove MS-Access style
-** brackets from around identifers.  For example:  "[a-b-c]" becomes
-** "a-b-c".
-*/
-int sqlite3Dequote(char *z){
-  char quote;
-  int i, j;
-  if( z==0 ) return -1;
-  quote = z[0];
-  switch( quote ){
-    case '\'':  break;
-    case '"':   break;
-    case '`':   break;                /* For MySQL compatibility */
-    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
-    default:    return -1;
-  }
-  for(i=1, j=0; ALWAYS(z[i]); i++){
-    if( z[i]==quote ){
-      if( z[i+1]==quote ){
-        z[j++] = quote;
-        i++;
-      }else{
-        break;
-      }
-    }else{
-      z[j++] = z[i];
-    }
-  }
-  z[j] = 0;
-  return j;
-}
-
-/* Convenient short-hand */
-#define UpperToLower sqlite3UpperToLower
-
-/*
-** Some systems have stricmp().  Others have strcasecmp().  Because
-** there is no consistency, we will define our own.
-*/
-int sqlite3StrICmp(const char *zLeft, const char *zRight){
-  register unsigned char *a, *b;
-  a = (unsigned char *)zLeft;
-  b = (unsigned char *)zRight;
-  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
-  return UpperToLower[*a] - UpperToLower[*b];
-}
-int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
-  register unsigned char *a, *b;
-  a = (unsigned char *)zLeft;
-  b = (unsigned char *)zRight;
-  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
-  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
-}
-
-/*
-** Return TRUE if z is a pure numeric string.  Return FALSE and leave
-** *realnum unchanged if the string contains any character which is not
-** part of a number.
-**
-** If the string is pure numeric, set *realnum to TRUE if the string
-** contains the '.' character or an "E+000" style exponentiation suffix.
-** Otherwise set *realnum to FALSE.  Note that just becaue *realnum is
-** false does not mean that the number can be successfully converted into
-** an integer - it might be too big.
-**
-** An empty string is considered non-numeric.
-*/
-int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
-  int incr = (enc==SQLITE_UTF8?1:2);
-  if( enc==SQLITE_UTF16BE ) z++;
-  if( *z=='-' || *z=='+' ) z += incr;
-  if( !sqlite3Isdigit(*z) ){
-    return 0;
-  }
-  z += incr;
-  *realnum = 0;
-  while( sqlite3Isdigit(*z) ){ z += incr; }
-  if( *z=='.' ){
-    z += incr;
-    if( !sqlite3Isdigit(*z) ) return 0;
-    while( sqlite3Isdigit(*z) ){ z += incr; }
-    *realnum = 1;
-  }
-  if( *z=='e' || *z=='E' ){
-    z += incr;
-    if( *z=='+' || *z=='-' ) z += incr;
-    if( !sqlite3Isdigit(*z) ) return 0;
-    while( sqlite3Isdigit(*z) ){ z += incr; }
-    *realnum = 1;
-  }
-  return *z==0;
-}
-
-/*
-** The string z[] is an ASCII representation of a real number.
-** Convert this string to a double.
-**
-** This routine assumes that z[] really is a valid number.  If it
-** is not, the result is undefined.
-**
-** This routine is used instead of the library atof() function because
-** the library atof() might want to use "," as the decimal point instead
-** of "." depending on how locale is set.  But that would cause problems
-** for SQL.  So this routine always uses "." regardless of locale.
-*/
-int sqlite3AtoF(const char *z, double *pResult){
-#ifndef SQLITE_OMIT_FLOATING_POINT
-  const char *zBegin = z;
-  /* sign * significand * (10 ^ (esign * exponent)) */
-  int sign = 1;   /* sign of significand */
-  i64 s = 0;      /* significand */
-  int d = 0;      /* adjust exponent for shifting decimal point */
-  int esign = 1;  /* sign of exponent */
-  int e = 0;      /* exponent */
-  double result;
-  int nDigits = 0;
-
-  /* skip leading spaces */
-  while( sqlite3Isspace(*z) ) z++;
-  /* get sign of significand */
-  if( *z=='-' ){
-    sign = -1;
-    z++;
-  }else if( *z=='+' ){
-    z++;
-  }
-  /* skip leading zeroes */
-  while( z[0]=='0' ) z++, nDigits++;
-
-  /* copy max significant digits to significand */
-  while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
-    s = s*10 + (*z - '0');
-    z++, nDigits++;
-  }
-  /* skip non-significant significand digits
-  ** (increase exponent by d to shift decimal left) */
-  while( sqlite3Isdigit(*z) ) z++, nDigits++, d++;
-
-  /* if decimal point is present */
-  if( *z=='.' ){
-    z++;
-    /* copy digits from after decimal to significand
-    ** (decrease exponent by d to shift decimal right) */
-    while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
-      s = s*10 + (*z - '0');
-      z++, nDigits++, d--;
-    }
-    /* skip non-significant digits */
-    while( sqlite3Isdigit(*z) ) z++, nDigits++;
-  }
-
-  /* if exponent is present */
-  if( *z=='e' || *z=='E' ){
-    z++;
-    /* get sign of exponent */
-    if( *z=='-' ){
-      esign = -1;
-      z++;
-    }else if( *z=='+' ){
-      z++;
-    }
-    /* copy digits to exponent */
-    while( sqlite3Isdigit(*z) ){
-      e = e*10 + (*z - '0');
-      z++;
-    }
-  }
-
-  /* adjust exponent by d, and update sign */
-  e = (e*esign) + d;
-  if( e<0 ) {
-    esign = -1;
-    e *= -1;
-  } else {
-    esign = 1;
-  }
-
-  /* if 0 significand */
-  if( !s ) {
-    /* In the IEEE 754 standard, zero is signed.
-    ** Add the sign if we've seen at least one digit */
-    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
-  } else {
-    /* attempt to reduce exponent */
-    if( esign>0 ){
-      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
-    }
-
-    /* adjust the sign of significand */
-    s = sign<0 ? -s : s;
-
-    /* if exponent, scale significand as appropriate
-    ** and store in result. */
-    if( e ){
-      double scale = 1.0;
-      while( e>=16 ){ scale *= 1.0e+16; e -= 16; }
-      while( e>=4 ){ scale *= 1.0e+4; e -= 4; }
-      while( e>=1 ){ scale *= 1.0e+1; e -= 1; }
-      if( esign<0 ){
-        result = s / scale;
-      }else{
-        result = s * scale;
-      }
-    } else {
-      result = (double)s;
-    }
-  }
-
-  /* store the result */
-  *pResult = result;
-
-  /* return number of characters used */
-  return (int)(z - zBegin);
-#else
-  return sqlite3Atoi64(z, pResult);
-#endif /* SQLITE_OMIT_FLOATING_POINT */
-}
-
-/*
-** Compare the 19-character string zNum against the text representation
-** value 2^63:  9223372036854775808.  Return negative, zero, or positive
-** if zNum is less than, equal to, or greater than the string.
-**
-** Unlike memcmp() this routine is guaranteed to return the difference
-** in the values of the last digit if the only difference is in the
-** last digit.  So, for example,
-**
-**      compare2pow63("9223372036854775800")
-**
-** will return -8.
-*/
-static int compare2pow63(const char *zNum){
-  int c;
-  c = memcmp(zNum,"922337203685477580",18)*10;
-  if( c==0 ){
-    c = zNum[18] - '8';
-  }
-  return c;
-}
-
-
-/*
-** Return TRUE if zNum is a 64-bit signed integer and write
-** the value of the integer into *pNum.  If zNum is not an integer
-** or is an integer that is too large to be expressed with 64 bits,
-** then return false.
-**
-** When this routine was originally written it dealt with only
-** 32-bit numbers.  At that time, it was much faster than the
-** atoi() library routine in RedHat 7.2.
-*/
-int sqlite3Atoi64(const char *zNum, i64 *pNum){
-  i64 v = 0;
-  int neg;
-  int i, c;
-  const char *zStart;
-  while( sqlite3Isspace(*zNum) ) zNum++;
-  if( *zNum=='-' ){
-    neg = 1;
-    zNum++;
-  }else if( *zNum=='+' ){
-    neg = 0;
-    zNum++;
-  }else{
-    neg = 0;
-  }
-  zStart = zNum;
-  while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
-  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
-    v = v*10 + c - '0';
-  }
-  *pNum = neg ? -v : v;
-  if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
-    /* zNum is empty or contains non-numeric text or is longer
-    ** than 19 digits (thus guaranting that it is too large) */
-    return 0;
-  }else if( i<19 ){
-    /* Less than 19 digits, so we know that it fits in 64 bits */
-    return 1;
-  }else{
-    /* 19-digit numbers must be no larger than 9223372036854775807 if positive
-    ** or 9223372036854775808 if negative.  Note that 9223372036854665808
-    ** is 2^63. */
-    return compare2pow63(zNum)<neg;
-  }
-}
-
-/*
-** The string zNum represents an unsigned integer.  The zNum string
-** consists of one or more digit characters and is terminated by
-** a zero character.  Any stray characters in zNum result in undefined
-** behavior.
-**
-** If the unsigned integer that zNum represents will fit in a
-** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
-**
-** If the negFlag parameter is true, that means that zNum really represents
-** a negative number.  (The leading "-" is omitted from zNum.)  This
-** parameter is needed to determine a boundary case.  A string
-** of "9223373036854775808" returns false if negFlag is false or true
-** if negFlag is true.
-**
-** Leading zeros are ignored.
-*/
-int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
-  int i;
-  int neg = 0;
-
-  assert( zNum[0]>='0' && zNum[0]<='9' ); /* zNum is an unsigned number */
-
-  if( negFlag ) neg = 1-neg;
-  while( *zNum=='0' ){
-    zNum++;   /* Skip leading zeros.  Ticket #2454 */
-  }
-  for(i=0; zNum[i]; i++){ assert( zNum[i]>='0' && zNum[i]<='9' ); }
-  if( i<19 ){
-    /* Guaranteed to fit if less than 19 digits */
-    return 1;
-  }else if( i>19 ){
-    /* Guaranteed to be too big if greater than 19 digits */
-    return 0;
-  }else{
-    /* Compare against 2^63. */
-    return compare2pow63(zNum)<neg;
-  }
-}
-
-/*
-** If zNum represents an integer that will fit in 32-bits, then set
-** *pValue to that integer and return true.  Otherwise return false.
-**
-** Any non-numeric characters that following zNum are ignored.
-** This is different from sqlite3Atoi64() which requires the
-** input number to be zero-terminated.
-*/
-int sqlite3GetInt32(const char *zNum, int *pValue){
-  sqlite_int64 v = 0;
-  int i, c;
-  int neg = 0;
-  if( zNum[0]=='-' ){
-    neg = 1;
-    zNum++;
-  }else if( zNum[0]=='+' ){
-    zNum++;
-  }
-  while( zNum[0]=='0' ) zNum++;
-  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
-    v = v*10 + c;
-  }
-
-  /* The longest decimal representation of a 32 bit integer is 10 digits:
-  **
-  **             1234567890
-  **     2^31 -> 2147483648
-  */
-  if( i>10 ){
-    return 0;
-  }
-  if( v-neg>2147483647 ){
-    return 0;
-  }
-  if( neg ){
-    v = -v;
-  }
-  *pValue = (int)v;
-  return 1;
-}
-
-/*
-** The variable-length integer encoding is as follows:
-**
-** KEY:
-**         A = 0xxxxxxx    7 bits of data and one flag bit
-**         B = 1xxxxxxx    7 bits of data and one flag bit
-**         C = xxxxxxxx    8 bits of data
-**
-**  7 bits - A
-** 14 bits - BA
-** 21 bits - BBA
-** 28 bits - BBBA
-** 35 bits - BBBBA
-** 42 bits - BBBBBA
-** 49 bits - BBBBBBA
-** 56 bits - BBBBBBBA
-** 64 bits - BBBBBBBBC
-*/
-
-/*
-** Write a 64-bit variable-length integer to memory starting at p[0].
-** The length of data write will be between 1 and 9 bytes.  The number
-** of bytes written is returned.
-**
-** A variable-length integer consists of the lower 7 bits of each byte
-** for all bytes that have the 8th bit set and one byte with the 8th
-** bit clear.  Except, if we get to the 9th byte, it stores the full
-** 8 bits and is the last byte.
-*/
-int sqlite3PutVarint(unsigned char *p, u64 v){
-  int i, j, n;
-  u8 buf[10];
-  if( v & (((u64)0xff000000)<<32) ){
-    p[8] = (u8)v;
-    v >>= 8;
-    for(i=7; i>=0; i--){
-      p[i] = (u8)((v & 0x7f) | 0x80);
-      v >>= 7;
-    }
-    return 9;
-  }    
-  n = 0;
-  do{
-    buf[n++] = (u8)((v & 0x7f) | 0x80);
-    v >>= 7;
-  }while( v!=0 );
-  buf[0] &= 0x7f;
-  assert( n<=9 );
-  for(i=0, j=n-1; j>=0; j--, i++){
-    p[i] = buf[j];
-  }
-  return n;
-}
-
-/*
-** This routine is a faster version of sqlite3PutVarint() that only
-** works for 32-bit positive integers and which is optimized for
-** the common case of small integers.  A MACRO version, putVarint32,
-** is provided which inlines the single-byte case.  All code should use
-** the MACRO version as this function assumes the single-byte case has
-** already been handled.
-*/
-int sqlite3PutVarint32(unsigned char *p, u32 v){
-#ifndef putVarint32
-  if( (v & ~0x7f)==0 ){
-    p[0] = v;
-    return 1;
-  }
-#endif
-  if( (v & ~0x3fff)==0 ){
-    p[0] = (u8)((v>>7) | 0x80);
-    p[1] = (u8)(v & 0x7f);
-    return 2;
-  }
-  return sqlite3PutVarint(p, v);
-}
-
-/*
-** Read a 64-bit variable-length integer from memory starting at p[0].
-** Return the number of bytes read.  The value is stored in *v.
-*/
-u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
-  u32 a,b,s;
-
-  a = *p;
-  /* a: p0 (unmasked) */
-  if (!(a&0x80))
-  {
-    *v = a;
-    return 1;
-  }
-
-  p++;
-  b = *p;
-  /* b: p1 (unmasked) */
-  if (!(b&0x80))
-  {
-    a &= 0x7f;
-    a = a<<7;
-    a |= b;
-    *v = a;
-    return 2;
-  }
-
-  p++;
-  a = a<<14;
-  a |= *p;
-  /* a: p0<<14 | p2 (unmasked) */
-  if (!(a&0x80))
-  {
-    a &= (0x7f<<14)|(0x7f);
-    b &= 0x7f;
-    b = b<<7;
-    a |= b;
-    *v = a;
-    return 3;
-  }
-
-  /* CSE1 from below */
-  a &= (0x7f<<14)|(0x7f);
-  p++;
-  b = b<<14;
-  b |= *p;
-  /* b: p1<<14 | p3 (unmasked) */
-  if (!(b&0x80))
-  {
-    b &= (0x7f<<14)|(0x7f);
-    /* moved CSE1 up */
-    /* a &= (0x7f<<14)|(0x7f); */
-    a = a<<7;
-    a |= b;
-    *v = a;
-    return 4;
-  }
-
-  /* a: p0<<14 | p2 (masked) */
-  /* b: p1<<14 | p3 (unmasked) */
-  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
-  /* moved CSE1 up */
-  /* a &= (0x7f<<14)|(0x7f); */
-  b &= (0x7f<<14)|(0x7f);
-  s = a;
-  /* s: p0<<14 | p2 (masked) */
-
-  p++;
-  a = a<<14;
-  a |= *p;
-  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
-  if (!(a&0x80))
-  {
-    /* we can skip these cause they were (effectively) done above in calc'ing s */
-    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
-    /* b &= (0x7f<<14)|(0x7f); */
-    b = b<<7;
-    a |= b;
-    s = s>>18;
-    *v = ((u64)s)<<32 | a;
-    return 5;
-  }
-
-  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
-  s = s<<7;
-  s |= b;
-  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
-
-  p++;
-  b = b<<14;
-  b |= *p;
-  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
-  if (!(b&0x80))
-  {
-    /* we can skip this cause it was (effectively) done above in calc'ing s */
-    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
-    a &= (0x7f<<14)|(0x7f);
-    a = a<<7;
-    a |= b;
-    s = s>>18;
-    *v = ((u64)s)<<32 | a;
-    return 6;
-  }
-
-  p++;
-  a = a<<14;
-  a |= *p;
-  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
-  if (!(a&0x80))
-  {
-    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
-    b &= (0x7f<<14)|(0x7f);
-    b = b<<7;
-    a |= b;
-    s = s>>11;
-    *v = ((u64)s)<<32 | a;
-    return 7;
-  }
-
-  /* CSE2 from below */
-  a &= (0x7f<<14)|(0x7f);
-  p++;
-  b = b<<14;
-  b |= *p;
-  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
-  if (!(b&0x80))
-  {
-    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
-    /* moved CSE2 up */
-    /* a &= (0x7f<<14)|(0x7f); */
-    a = a<<7;
-    a |= b;
-    s = s>>4;
-    *v = ((u64)s)<<32 | a;
-    return 8;
-  }
-
-  p++;
-  a = a<<15;
-  a |= *p;
-  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
-
-  /* moved CSE2 up */
-  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
-  b &= (0x7f<<14)|(0x7f);
-  b = b<<8;
-  a |= b;
-
-  s = s<<4;
-  b = p[-4];
-  b &= 0x7f;
-  b = b>>3;
-  s |= b;
-
-  *v = ((u64)s)<<32 | a;
-
-  return 9;
-}
-
-/*
-** Read a 32-bit variable-length integer from memory starting at p[0].
-** Return the number of bytes read.  The value is stored in *v.
-**
-** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
-** integer, then set *v to 0xffffffff.
-**
-** A MACRO version, getVarint32, is provided which inlines the 
-** single-byte case.  All code should use the MACRO version as 
-** this function assumes the single-byte case has already been handled.
-*/
-u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
-  u32 a,b;
-
-  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
-  ** by the getVarin32() macro */
-  a = *p;
-  /* a: p0 (unmasked) */
-#ifndef getVarint32
-  if (!(a&0x80))
-  {
-    /* Values between 0 and 127 */
-    *v = a;
-    return 1;
-  }
-#endif
-
-  /* The 2-byte case */
-  p++;
-  b = *p;
-  /* b: p1 (unmasked) */
-  if (!(b&0x80))
-  {
-    /* Values between 128 and 16383 */
-    a &= 0x7f;
-    a = a<<7;
-    *v = a | b;
-    return 2;
-  }
-
-  /* The 3-byte case */
-  p++;
-  a = a<<14;
-  a |= *p;
-  /* a: p0<<14 | p2 (unmasked) */
-  if (!(a&0x80))
-  {
-    /* Values between 16384 and 2097151 */
-    a &= (0x7f<<14)|(0x7f);
-    b &= 0x7f;
-    b = b<<7;
-    *v = a | b;
-    return 3;
-  }
-
-  /* A 32-bit varint is used to store size information in btrees.
-  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
-  ** A 3-byte varint is sufficient, for example, to record the size
-  ** of a 1048569-byte BLOB or string.
-  **
-  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
-  ** rare larger cases can be handled by the slower 64-bit varint
-  ** routine.
-  */
-#if 1
-  {
-    u64 v64;
-    u8 n;
-
-    p -= 2;
-    n = sqlite3GetVarint(p, &v64);
-    assert( n>3 && n<=9 );
-    if( (v64 & SQLITE_MAX_U32)!=v64 ){
-      *v = 0xffffffff;
-    }else{
-      *v = (u32)v64;
-    }
-    return n;
-  }
-
-#else
-  /* For following code (kept for historical record only) shows an
-  ** unrolling for the 3- and 4-byte varint cases.  This code is
-  ** slightly faster, but it is also larger and much harder to test.
-  */
-  p++;
-  b = b<<14;
-  b |= *p;
-  /* b: p1<<14 | p3 (unmasked) */
-  if (!(b&0x80))
-  {
-    /* Values between 2097152 and 268435455 */
-    b &= (0x7f<<14)|(0x7f);
-    a &= (0x7f<<14)|(0x7f);
-    a = a<<7;
-    *v = a | b;
-    return 4;
-  }
-
-  p++;
-  a = a<<14;
-  a |= *p;
-  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
-  if (!(a&0x80))
-  {
-    /* Walues  between 268435456 and 34359738367 */
-    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);
-    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);
-    b = b<<7;
-    *v = a | b;
-    return 5;
-  }
-
-  /* We can only reach this point when reading a corrupt database
-  ** file.  In that case we are not in any hurry.  Use the (relatively
-  ** slow) general-purpose sqlite3GetVarint() routine to extract the
-  ** value. */
-  {
-    u64 v64;
-    u8 n;
-
-    p -= 4;
-    n = sqlite3GetVarint(p, &v64);
-    assert( n>5 && n<=9 );
-    *v = (u32)v64;
-    return n;
-  }
-#endif
-}
-
-/*
-** Return the number of bytes that will be needed to store the given
-** 64-bit integer.
-*/
-int sqlite3VarintLen(u64 v){
-  int i = 0;
-  do{
-    i++;
-    v >>= 7;
-  }while( v!=0 && ALWAYS(i<9) );
-  return i;
-}
-
-
-/*
-** Read or write a four-byte big-endian integer value.
-*/
-u32 sqlite3Get4byte(const u8 *p){
-  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
-}
-void sqlite3Put4byte(unsigned char *p, u32 v){
-  p[0] = (u8)(v>>24);
-  p[1] = (u8)(v>>16);
-  p[2] = (u8)(v>>8);
-  p[3] = (u8)v;
-}
-
-
-
-#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
-/*
-** Translate a single byte of Hex into an integer.
-** This routine only works if h really is a valid hexadecimal
-** character:  0..9a..fA..F
-*/
-static u8 hexToInt(int h){
-  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
-#ifdef SQLITE_ASCII
-  h += 9*(1&(h>>6));
-#endif
-#ifdef SQLITE_EBCDIC
-  h += 9*(1&~(h>>4));
-#endif
-  return (u8)(h & 0xf);
-}
-#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
-
-#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
-/*
-** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
-** value.  Return a pointer to its binary value.  Space to hold the
-** binary value has been obtained from malloc and must be freed by
-** the calling routine.
-*/
-void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
-  char *zBlob;
-  int i;
-
-  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
-  n--;
-  if( zBlob ){
-    for(i=0; i<n; i+=2){
-      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
-    }
-    zBlob[i/2] = 0;
-  }
-  return zBlob;
-}
-#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
-
-
-/*
-** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
-** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
-** when this routine is called.
-**
-** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
-** value indicates that the database connection passed into the API is
-** open and is not being used by another thread.  By changing the value
-** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
-** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
-** when the API exits. 
-**
-** This routine is a attempt to detect if two threads use the
-** same sqlite* pointer at the same time.  There is a race 
-** condition so it is possible that the error is not detected.
-** But usually the problem will be seen.  The result will be an
-** error which can be used to debug the application that is
-** using SQLite incorrectly.
-**
-** Ticket #202:  If db->magic is not a valid open value, take care not
-** to modify the db structure at all.  It could be that db is a stale
-** pointer.  In other words, it could be that there has been a prior
-** call to sqlite3_close(db) and db has been deallocated.  And we do
-** not want to write into deallocated memory.
-*/
-#ifdef SQLITE_DEBUG
-int sqlite3SafetyOn(sqlite3 *db){
-  if( db->magic==SQLITE_MAGIC_OPEN ){
-    db->magic = SQLITE_MAGIC_BUSY;
-    assert( sqlite3_mutex_held(db->mutex) );
-    return 0;
-  }else if( db->magic==SQLITE_MAGIC_BUSY ){
-    db->magic = SQLITE_MAGIC_ERROR;
-    db->u1.isInterrupted = 1;
-  }
-  return 1;
-}
-#endif
-
-/*
-** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
-** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
-** when this routine is called.
-*/
-#ifdef SQLITE_DEBUG
-int sqlite3SafetyOff(sqlite3 *db){
-  if( db->magic==SQLITE_MAGIC_BUSY ){
-    db->magic = SQLITE_MAGIC_OPEN;
-    assert( sqlite3_mutex_held(db->mutex) );
-    return 0;
-  }else{
-    db->magic = SQLITE_MAGIC_ERROR;
-    db->u1.isInterrupted = 1;
-    return 1;
-  }
-}
-#endif
-
-/*
-** Check to make sure we have a valid db pointer.  This test is not
-** foolproof but it does provide some measure of protection against
-** misuse of the interface such as passing in db pointers that are
-** NULL or which have been previously closed.  If this routine returns
-** 1 it means that the db pointer is valid and 0 if it should not be
-** dereferenced for any reason.  The calling function should invoke
-** SQLITE_MISUSE immediately.
-**
-** sqlite3SafetyCheckOk() requires that the db pointer be valid for
-** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
-** open properly and is not fit for general use but which can be
-** used as an argument to sqlite3_errmsg() or sqlite3_close().
-*/
-int sqlite3SafetyCheckOk(sqlite3 *db){
-  u32 magic;
-  if( db==0 ) return 0;
-  magic = db->magic;
-  if( magic!=SQLITE_MAGIC_OPEN 
-#ifdef SQLITE_DEBUG
-     && magic!=SQLITE_MAGIC_BUSY
-#endif
-  ){
-    return 0;
-  }else{
-    return 1;
-  }
-}
-int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
-  u32 magic;
-  magic = db->magic;
-  if( magic!=SQLITE_MAGIC_SICK &&
-      magic!=SQLITE_MAGIC_OPEN &&
-      magic!=SQLITE_MAGIC_BUSY ) return 0;
-  return 1;
-}
+/*\r
+** 2001 September 15\r
+**\r
+** The author disclaims copyright to this source code.  In place of\r
+** a legal notice, here is a blessing:\r
+**\r
+**    May you do good and not evil.\r
+**    May you find forgiveness for yourself and forgive others.\r
+**    May you share freely, never taking more than you give.\r
+**\r
+*************************************************************************\r
+** Utility functions used throughout sqlite.\r
+**\r
+** This file contains functions for allocating memory, comparing\r
+** strings, and stuff like that.\r
+**\r
+*/\r
+#include "sqliteInt.h"\r
+#include <stdarg.h>\r
+#ifdef SQLITE_HAVE_ISNAN\r
+# include <math.h>\r
+#endif\r
+\r
+/*\r
+** Routine needed to support the testcase() macro.\r
+*/\r
+#ifdef SQLITE_COVERAGE_TEST\r
+void sqlite3Coverage(int x){\r
+  static int dummy = 0;\r
+  dummy += x;\r
+}\r
+#endif\r
+\r
+/*\r
+** Return true if the floating point value is Not a Number (NaN).\r
+**\r
+** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.\r
+** Otherwise, we have our own implementation that works on most systems.\r
+*/\r
+int sqlite3IsNaN(double x){\r
+  int rc;   /* The value return */\r
+#if !defined(SQLITE_HAVE_ISNAN)\r
+  /*\r
+  ** Systems that support the isnan() library function should probably\r
+  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have\r
+  ** found that many systems do not have a working isnan() function so\r
+  ** this implementation is provided as an alternative.\r
+  **\r
+  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.\r
+  ** On the other hand, the use of -ffast-math comes with the following\r
+  ** warning:\r
+  **\r
+  **      This option [-ffast-math] should never be turned on by any\r
+  **      -O option since it can result in incorrect output for programs\r
+  **      which depend on an exact implementation of IEEE or ISO \r
+  **      rules/specifications for math functions.\r
+  **\r
+  ** Under MSVC, this NaN test may fail if compiled with a floating-\r
+  ** point precision mode other than /fp:precise.  From the MSDN \r
+  ** documentation:\r
+  **\r
+  **      The compiler [with /fp:precise] will properly handle comparisons \r
+  **      involving NaN. For example, x != x evaluates to true if x is NaN \r
+  **      ...\r
+  */\r
+#ifdef __FAST_MATH__\r
+# error SQLite will not work correctly with the -ffast-math option of GCC.\r
+#endif\r
+  volatile double y = x;\r
+  volatile double z = y;\r
+  rc = (y!=z);\r
+#else  /* if defined(SQLITE_HAVE_ISNAN) */\r
+  rc = isnan(x);\r
+#endif /* SQLITE_HAVE_ISNAN */\r
+  testcase( rc );\r
+  return rc;\r
+}\r
+\r
+/*\r
+** Compute a string length that is limited to what can be stored in\r
+** lower 30 bits of a 32-bit signed integer.\r
+**\r
+** The value returned will never be negative.  Nor will it ever be greater\r
+** than the actual length of the string.  For very long strings (greater\r
+** than 1GiB) the value returned might be less than the true string length.\r
+*/\r
+int sqlite3Strlen30(const char *z){\r
+  const char *z2 = z;\r
+  if( z==0 ) return 0;\r
+  while( *z2 ){ z2++; }\r
+  return 0x3fffffff & (int)(z2 - z);\r
+}\r
+\r
+/*\r
+** Set the most recent error code and error string for the sqlite\r
+** handle "db". The error code is set to "err_code".\r
+**\r
+** If it is not NULL, string zFormat specifies the format of the\r
+** error string in the style of the printf functions: The following\r
+** format characters are allowed:\r
+**\r
+**      %s      Insert a string\r
+**      %z      A string that should be freed after use\r
+**      %d      Insert an integer\r
+**      %T      Insert a token\r
+**      %S      Insert the first element of a SrcList\r
+**\r
+** zFormat and any string tokens that follow it are assumed to be\r
+** encoded in UTF-8.\r
+**\r
+** To clear the most recent error for sqlite handle "db", sqlite3Error\r
+** should be called with err_code set to SQLITE_OK and zFormat set\r
+** to NULL.\r
+*/\r
+void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){\r
+  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){\r
+    db->errCode = err_code;\r
+    if( zFormat ){\r
+      char *z;\r
+      va_list ap;\r
+      va_start(ap, zFormat);\r
+      z = sqlite3VMPrintf(db, zFormat, ap);\r
+      va_end(ap);\r
+      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);\r
+    }else{\r
+      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);\r
+    }\r
+  }\r
+}\r
+\r
+/*\r
+** Add an error message to pParse->zErrMsg and increment pParse->nErr.\r
+** The following formatting characters are allowed:\r
+**\r
+**      %s      Insert a string\r
+**      %z      A string that should be freed after use\r
+**      %d      Insert an integer\r
+**      %T      Insert a token\r
+**      %S      Insert the first element of a SrcList\r
+**\r
+** This function should be used to report any error that occurs whilst\r
+** compiling an SQL statement (i.e. within sqlite3_prepare()). The\r
+** last thing the sqlite3_prepare() function does is copy the error\r
+** stored by this function into the database handle using sqlite3Error().\r
+** Function sqlite3Error() should be used during statement execution\r
+** (sqlite3_step() etc.).\r
+*/\r
+void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){\r
+  va_list ap;\r
+  sqlite3 *db = pParse->db;\r
+  pParse->nErr++;\r
+  sqlite3DbFree(db, pParse->zErrMsg);\r
+  va_start(ap, zFormat);\r
+  pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);\r
+  va_end(ap);\r
+  pParse->rc = SQLITE_ERROR;\r
+}\r
+\r
+/*\r
+** Clear the error message in pParse, if any\r
+*/\r
+void sqlite3ErrorClear(Parse *pParse){\r
+  sqlite3DbFree(pParse->db, pParse->zErrMsg);\r
+  pParse->zErrMsg = 0;\r
+  pParse->nErr = 0;\r
+}\r
+\r
+/*\r
+** Convert an SQL-style quoted string into a normal string by removing\r
+** the quote characters.  The conversion is done in-place.  If the\r
+** input does not begin with a quote character, then this routine\r
+** is a no-op.\r
+**\r
+** The input string must be zero-terminated.  A new zero-terminator\r
+** is added to the dequoted string.\r
+**\r
+** The return value is -1 if no dequoting occurs or the length of the\r
+** dequoted string, exclusive of the zero terminator, if dequoting does\r
+** occur.\r
+**\r
+** 2002-Feb-14: This routine is extended to remove MS-Access style\r
+** brackets from around identifers.  For example:  "[a-b-c]" becomes\r
+** "a-b-c".\r
+*/\r
+int sqlite3Dequote(char *z){\r
+  char quote;\r
+  int i, j;\r
+  if( z==0 ) return -1;\r
+  quote = z[0];\r
+  switch( quote ){\r
+    case '\'':  break;\r
+    case '"':   break;\r
+    case '`':   break;                /* For MySQL compatibility */\r
+    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */\r
+    default:    return -1;\r
+  }\r
+  for(i=1, j=0; ALWAYS(z[i]); i++){\r
+    if( z[i]==quote ){\r
+      if( z[i+1]==quote ){\r
+        z[j++] = quote;\r
+        i++;\r
+      }else{\r
+        break;\r
+      }\r
+    }else{\r
+      z[j++] = z[i];\r
+    }\r
+  }\r
+  z[j] = 0;\r
+  return j;\r
+}\r
+\r
+/* Convenient short-hand */\r
+#define UpperToLower sqlite3UpperToLower\r
+\r
+/*\r
+** Some systems have stricmp().  Others have strcasecmp().  Because\r
+** there is no consistency, we will define our own.\r
+*/\r
+int sqlite3StrICmp(const char *zLeft, const char *zRight){\r
+  register unsigned char *a, *b;\r
+  a = (unsigned char *)zLeft;\r
+  b = (unsigned char *)zRight;\r
+  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }\r
+  return UpperToLower[*a] - UpperToLower[*b];\r
+}\r
+int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){\r
+  register unsigned char *a, *b;\r
+  a = (unsigned char *)zLeft;\r
+  b = (unsigned char *)zRight;\r
+  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }\r
+  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];\r
+}\r
+\r
+/*\r
+** Return TRUE if z is a pure numeric string.  Return FALSE and leave\r
+** *realnum unchanged if the string contains any character which is not\r
+** part of a number.\r
+**\r
+** If the string is pure numeric, set *realnum to TRUE if the string\r
+** contains the '.' character or an "E+000" style exponentiation suffix.\r
+** Otherwise set *realnum to FALSE.  Note that just becaue *realnum is\r
+** false does not mean that the number can be successfully converted into\r
+** an integer - it might be too big.\r
+**\r
+** An empty string is considered non-numeric.\r
+*/\r
+int sqlite3IsNumber(const char *z, int *realnum, u8 enc){\r
+  int incr = (enc==SQLITE_UTF8?1:2);\r
+  if( enc==SQLITE_UTF16BE ) z++;\r
+  if( *z=='-' || *z=='+' ) z += incr;\r
+  if( !sqlite3Isdigit(*z) ){\r
+    return 0;\r
+  }\r
+  z += incr;\r
+  *realnum = 0;\r
+  while( sqlite3Isdigit(*z) ){ z += incr; }\r
+  if( *z=='.' ){\r
+    z += incr;\r
+    if( !sqlite3Isdigit(*z) ) return 0;\r
+    while( sqlite3Isdigit(*z) ){ z += incr; }\r
+    *realnum = 1;\r
+  }\r
+  if( *z=='e' || *z=='E' ){\r
+    z += incr;\r
+    if( *z=='+' || *z=='-' ) z += incr;\r
+    if( !sqlite3Isdigit(*z) ) return 0;\r
+    while( sqlite3Isdigit(*z) ){ z += incr; }\r
+    *realnum = 1;\r
+  }\r
+  return *z==0;\r
+}\r
+\r
+/*\r
+** The string z[] is an ASCII representation of a real number.\r
+** Convert this string to a double.\r
+**\r
+** This routine assumes that z[] really is a valid number.  If it\r
+** is not, the result is undefined.\r
+**\r
+** This routine is used instead of the library atof() function because\r
+** the library atof() might want to use "," as the decimal point instead\r
+** of "." depending on how locale is set.  But that would cause problems\r
+** for SQL.  So this routine always uses "." regardless of locale.\r
+*/\r
+int sqlite3AtoF(const char *z, double *pResult){\r
+#ifndef SQLITE_OMIT_FLOATING_POINT\r
+  const char *zBegin = z;\r
+  /* sign * significand * (10 ^ (esign * exponent)) */\r
+  int sign = 1;   /* sign of significand */\r
+  i64 s = 0;      /* significand */\r
+  int d = 0;      /* adjust exponent for shifting decimal point */\r
+  int esign = 1;  /* sign of exponent */\r
+  int e = 0;      /* exponent */\r
+  double result;\r
+  int nDigits = 0;\r
+\r
+  /* skip leading spaces */\r
+  while( sqlite3Isspace(*z) ) z++;\r
+  /* get sign of significand */\r
+  if( *z=='-' ){\r
+    sign = -1;\r
+    z++;\r
+  }else if( *z=='+' ){\r
+    z++;\r
+  }\r
+  /* skip leading zeroes */\r
+  while( z[0]=='0' ) z++, nDigits++;\r
+\r
+  /* copy max significant digits to significand */\r
+  while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){\r
+    s = s*10 + (*z - '0');\r
+    z++, nDigits++;\r
+  }\r
+  /* skip non-significant significand digits\r
+  ** (increase exponent by d to shift decimal left) */\r
+  while( sqlite3Isdigit(*z) ) z++, nDigits++, d++;\r
+\r
+  /* if decimal point is present */\r
+  if( *z=='.' ){\r
+    z++;\r
+    /* copy digits from after decimal to significand\r
+    ** (decrease exponent by d to shift decimal right) */\r
+    while( sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){\r
+      s = s*10 + (*z - '0');\r
+      z++, nDigits++, d--;\r
+    }\r
+    /* skip non-significant digits */\r
+    while( sqlite3Isdigit(*z) ) z++, nDigits++;\r
+  }\r
+\r
+  /* if exponent is present */\r
+  if( *z=='e' || *z=='E' ){\r
+    z++;\r
+    /* get sign of exponent */\r
+    if( *z=='-' ){\r
+      esign = -1;\r
+      z++;\r
+    }else if( *z=='+' ){\r
+      z++;\r
+    }\r
+    /* copy digits to exponent */\r
+    while( sqlite3Isdigit(*z) ){\r
+      e = e*10 + (*z - '0');\r
+      z++;\r
+    }\r
+  }\r
+\r
+  /* adjust exponent by d, and update sign */\r
+  e = (e*esign) + d;\r
+  if( e<0 ) {\r
+    esign = -1;\r
+    e *= -1;\r
+  } else {\r
+    esign = 1;\r
+  }\r
+\r
+  /* if 0 significand */\r
+  if( !s ) {\r
+    /* In the IEEE 754 standard, zero is signed.\r
+    ** Add the sign if we've seen at least one digit */\r
+    result = (sign<0 && nDigits) ? -(double)0 : (double)0;\r
+  } else {\r
+    /* attempt to reduce exponent */\r
+    if( esign>0 ){\r
+      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;\r
+    }else{\r
+      while( !(s%10) && e>0 ) e--,s/=10;\r
+    }\r
+\r
+    /* adjust the sign of significand */\r
+    s = sign<0 ? -s : s;\r
+\r
+    /* if exponent, scale significand as appropriate\r
+    ** and store in result. */\r
+    if( e ){\r
+      double scale = 1.0;\r
+      /* 1.0e+22 is the largest power of 10 than can be \r
+      ** represented exactly. */\r
+      while( e%22 ) { scale *= 1.0e+1; e -= 1; }\r
+      while( e>0 ) { scale *= 1.0e+22; e -= 22; }\r
+      if( esign<0 ){\r
+        result = s / scale;\r
+      }else{\r
+        result = s * scale;\r
+      }\r
+    } else {\r
+      result = (double)s;\r
+    }\r
+  }\r
+\r
+  /* store the result */\r
+  *pResult = result;\r
+\r
+  /* return number of characters used */\r
+  return (int)(z - zBegin);\r
+#else\r
+  return sqlite3Atoi64(z, pResult);\r
+#endif /* SQLITE_OMIT_FLOATING_POINT */\r
+}\r
+\r
+/*\r
+** Compare the 19-character string zNum against the text representation\r
+** value 2^63:  9223372036854775808.  Return negative, zero, or positive\r
+** if zNum is less than, equal to, or greater than the string.\r
+**\r
+** Unlike memcmp() this routine is guaranteed to return the difference\r
+** in the values of the last digit if the only difference is in the\r
+** last digit.  So, for example,\r
+**\r
+**      compare2pow63("9223372036854775800")\r
+**\r
+** will return -8.\r
+*/\r
+static int compare2pow63(const char *zNum){\r
+  int c;\r
+  c = memcmp(zNum,"922337203685477580",18)*10;\r
+  if( c==0 ){\r
+    c = zNum[18] - '8';\r
+  }\r
+  return c;\r
+}\r
+\r
+\r
+/*\r
+** Return TRUE if zNum is a 64-bit signed integer and write\r
+** the value of the integer into *pNum.  If zNum is not an integer\r
+** or is an integer that is too large to be expressed with 64 bits,\r
+** then return false.\r
+**\r
+** When this routine was originally written it dealt with only\r
+** 32-bit numbers.  At that time, it was much faster than the\r
+** atoi() library routine in RedHat 7.2.\r
+*/\r
+int sqlite3Atoi64(const char *zNum, i64 *pNum){\r
+  i64 v = 0;\r
+  int neg;\r
+  int i, c;\r
+  const char *zStart;\r
+  while( sqlite3Isspace(*zNum) ) zNum++;\r
+  if( *zNum=='-' ){\r
+    neg = 1;\r
+    zNum++;\r
+  }else if( *zNum=='+' ){\r
+    neg = 0;\r
+    zNum++;\r
+  }else{\r
+    neg = 0;\r
+  }\r
+  zStart = zNum;\r
+  while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */\r
+  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){\r
+    v = v*10 + c - '0';\r
+  }\r
+  *pNum = neg ? -v : v;\r
+  if( c!=0 || (i==0 && zStart==zNum) || i>19 ){\r
+    /* zNum is empty or contains non-numeric text or is longer\r
+    ** than 19 digits (thus guaranting that it is too large) */\r
+    return 0;\r
+  }else if( i<19 ){\r
+    /* Less than 19 digits, so we know that it fits in 64 bits */\r
+    return 1;\r
+  }else{\r
+    /* 19-digit numbers must be no larger than 9223372036854775807 if positive\r
+    ** or 9223372036854775808 if negative.  Note that 9223372036854665808\r
+    ** is 2^63. */\r
+    return compare2pow63(zNum)<neg;\r
+  }\r
+}\r
+\r
+/*\r
+** The string zNum represents an unsigned integer.  The zNum string\r
+** consists of one or more digit characters and is terminated by\r
+** a zero character.  Any stray characters in zNum result in undefined\r
+** behavior.\r
+**\r
+** If the unsigned integer that zNum represents will fit in a\r
+** 64-bit signed integer, return TRUE.  Otherwise return FALSE.\r
+**\r
+** If the negFlag parameter is true, that means that zNum really represents\r
+** a negative number.  (The leading "-" is omitted from zNum.)  This\r
+** parameter is needed to determine a boundary case.  A string\r
+** of "9223373036854775808" returns false if negFlag is false or true\r
+** if negFlag is true.\r
+**\r
+** Leading zeros are ignored.\r
+*/\r
+int sqlite3FitsIn64Bits(const char *zNum, int negFlag){\r
+  int i;\r
+  int neg = 0;\r
+\r
+  assert( zNum[0]>='0' && zNum[0]<='9' ); /* zNum is an unsigned number */\r
+\r
+  if( negFlag ) neg = 1-neg;\r
+  while( *zNum=='0' ){\r
+    zNum++;   /* Skip leading zeros.  Ticket #2454 */\r
+  }\r
+  for(i=0; zNum[i]; i++){ assert( zNum[i]>='0' && zNum[i]<='9' ); }\r
+  if( i<19 ){\r
+    /* Guaranteed to fit if less than 19 digits */\r
+    return 1;\r
+  }else if( i>19 ){\r
+    /* Guaranteed to be too big if greater than 19 digits */\r
+    return 0;\r
+  }else{\r
+    /* Compare against 2^63. */\r
+    return compare2pow63(zNum)<neg;\r
+  }\r
+}\r
+\r
+/*\r
+** If zNum represents an integer that will fit in 32-bits, then set\r
+** *pValue to that integer and return true.  Otherwise return false.\r
+**\r
+** Any non-numeric characters that following zNum are ignored.\r
+** This is different from sqlite3Atoi64() which requires the\r
+** input number to be zero-terminated.\r
+*/\r
+int sqlite3GetInt32(const char *zNum, int *pValue){\r
+  sqlite_int64 v = 0;\r
+  int i, c;\r
+  int neg = 0;\r
+  if( zNum[0]=='-' ){\r
+    neg = 1;\r
+    zNum++;\r
+  }else if( zNum[0]=='+' ){\r
+    zNum++;\r
+  }\r
+  while( zNum[0]=='0' ) zNum++;\r
+  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){\r
+    v = v*10 + c;\r
+  }\r
+\r
+  /* The longest decimal representation of a 32 bit integer is 10 digits:\r
+  **\r
+  **             1234567890\r
+  **     2^31 -> 2147483648\r
+  */\r
+  if( i>10 ){\r
+    return 0;\r
+  }\r
+  if( v-neg>2147483647 ){\r
+    return 0;\r
+  }\r
+  if( neg ){\r
+    v = -v;\r
+  }\r
+  *pValue = (int)v;\r
+  return 1;\r
+}\r
+\r
+/*\r
+** The variable-length integer encoding is as follows:\r
+**\r
+** KEY:\r
+**         A = 0xxxxxxx    7 bits of data and one flag bit\r
+**         B = 1xxxxxxx    7 bits of data and one flag bit\r
+**         C = xxxxxxxx    8 bits of data\r
+**\r
+**  7 bits - A\r
+** 14 bits - BA\r
+** 21 bits - BBA\r
+** 28 bits - BBBA\r
+** 35 bits - BBBBA\r
+** 42 bits - BBBBBA\r
+** 49 bits - BBBBBBA\r
+** 56 bits - BBBBBBBA\r
+** 64 bits - BBBBBBBBC\r
+*/\r
+\r
+/*\r
+** Write a 64-bit variable-length integer to memory starting at p[0].\r
+** The length of data write will be between 1 and 9 bytes.  The number\r
+** of bytes written is returned.\r
+**\r
+** A variable-length integer consists of the lower 7 bits of each byte\r
+** for all bytes that have the 8th bit set and one byte with the 8th\r
+** bit clear.  Except, if we get to the 9th byte, it stores the full\r
+** 8 bits and is the last byte.\r
+*/\r
+int sqlite3PutVarint(unsigned char *p, u64 v){\r
+  int i, j, n;\r
+  u8 buf[10];\r
+  if( v & (((u64)0xff000000)<<32) ){\r
+    p[8] = (u8)v;\r
+    v >>= 8;\r
+    for(i=7; i>=0; i--){\r
+      p[i] = (u8)((v & 0x7f) | 0x80);\r
+      v >>= 7;\r
+    }\r
+    return 9;\r
+  }    \r
+  n = 0;\r
+  do{\r
+    buf[n++] = (u8)((v & 0x7f) | 0x80);\r
+    v >>= 7;\r
+  }while( v!=0 );\r
+  buf[0] &= 0x7f;\r
+  assert( n<=9 );\r
+  for(i=0, j=n-1; j>=0; j--, i++){\r
+    p[i] = buf[j];\r
+  }\r
+  return n;\r
+}\r
+\r
+/*\r
+** This routine is a faster version of sqlite3PutVarint() that only\r
+** works for 32-bit positive integers and which is optimized for\r
+** the common case of small integers.  A MACRO version, putVarint32,\r
+** is provided which inlines the single-byte case.  All code should use\r
+** the MACRO version as this function assumes the single-byte case has\r
+** already been handled.\r
+*/\r
+int sqlite3PutVarint32(unsigned char *p, u32 v){\r
+#ifndef putVarint32\r
+  if( (v & ~0x7f)==0 ){\r
+    p[0] = v;\r
+    return 1;\r
+  }\r
+#endif\r
+  if( (v & ~0x3fff)==0 ){\r
+    p[0] = (u8)((v>>7) | 0x80);\r
+    p[1] = (u8)(v & 0x7f);\r
+    return 2;\r
+  }\r
+  return sqlite3PutVarint(p, v);\r
+}\r
+\r
+/*\r
+** Read a 64-bit variable-length integer from memory starting at p[0].\r
+** Return the number of bytes read.  The value is stored in *v.\r
+*/\r
+u8 sqlite3GetVarint(const unsigned char *p, u64 *v){\r
+  u32 a,b,s;\r
+\r
+  a = *p;\r
+  /* a: p0 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    *v = a;\r
+    return 1;\r
+  }\r
+\r
+  p++;\r
+  b = *p;\r
+  /* b: p1 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    a &= 0x7f;\r
+    a = a<<7;\r
+    a |= b;\r
+    *v = a;\r
+    return 2;\r
+  }\r
+\r
+  p++;\r
+  a = a<<14;\r
+  a |= *p;\r
+  /* a: p0<<14 | p2 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    a &= (0x7f<<14)|(0x7f);\r
+    b &= 0x7f;\r
+    b = b<<7;\r
+    a |= b;\r
+    *v = a;\r
+    return 3;\r
+  }\r
+\r
+  /* CSE1 from below */\r
+  a &= (0x7f<<14)|(0x7f);\r
+  p++;\r
+  b = b<<14;\r
+  b |= *p;\r
+  /* b: p1<<14 | p3 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    b &= (0x7f<<14)|(0x7f);\r
+    /* moved CSE1 up */\r
+    /* a &= (0x7f<<14)|(0x7f); */\r
+    a = a<<7;\r
+    a |= b;\r
+    *v = a;\r
+    return 4;\r
+  }\r
+\r
+  /* a: p0<<14 | p2 (masked) */\r
+  /* b: p1<<14 | p3 (unmasked) */\r
+  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */\r
+  /* moved CSE1 up */\r
+  /* a &= (0x7f<<14)|(0x7f); */\r
+  b &= (0x7f<<14)|(0x7f);\r
+  s = a;\r
+  /* s: p0<<14 | p2 (masked) */\r
+\r
+  p++;\r
+  a = a<<14;\r
+  a |= *p;\r
+  /* a: p0<<28 | p2<<14 | p4 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    /* we can skip these cause they were (effectively) done above in calc'ing s */\r
+    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */\r
+    /* b &= (0x7f<<14)|(0x7f); */\r
+    b = b<<7;\r
+    a |= b;\r
+    s = s>>18;\r
+    *v = ((u64)s)<<32 | a;\r
+    return 5;\r
+  }\r
+\r
+  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */\r
+  s = s<<7;\r
+  s |= b;\r
+  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */\r
+\r
+  p++;\r
+  b = b<<14;\r
+  b |= *p;\r
+  /* b: p1<<28 | p3<<14 | p5 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    /* we can skip this cause it was (effectively) done above in calc'ing s */\r
+    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */\r
+    a &= (0x7f<<14)|(0x7f);\r
+    a = a<<7;\r
+    a |= b;\r
+    s = s>>18;\r
+    *v = ((u64)s)<<32 | a;\r
+    return 6;\r
+  }\r
+\r
+  p++;\r
+  a = a<<14;\r
+  a |= *p;\r
+  /* a: p2<<28 | p4<<14 | p6 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);\r
+    b &= (0x7f<<14)|(0x7f);\r
+    b = b<<7;\r
+    a |= b;\r
+    s = s>>11;\r
+    *v = ((u64)s)<<32 | a;\r
+    return 7;\r
+  }\r
+\r
+  /* CSE2 from below */\r
+  a &= (0x7f<<14)|(0x7f);\r
+  p++;\r
+  b = b<<14;\r
+  b |= *p;\r
+  /* b: p3<<28 | p5<<14 | p7 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);\r
+    /* moved CSE2 up */\r
+    /* a &= (0x7f<<14)|(0x7f); */\r
+    a = a<<7;\r
+    a |= b;\r
+    s = s>>4;\r
+    *v = ((u64)s)<<32 | a;\r
+    return 8;\r
+  }\r
+\r
+  p++;\r
+  a = a<<15;\r
+  a |= *p;\r
+  /* a: p4<<29 | p6<<15 | p8 (unmasked) */\r
+\r
+  /* moved CSE2 up */\r
+  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */\r
+  b &= (0x7f<<14)|(0x7f);\r
+  b = b<<8;\r
+  a |= b;\r
+\r
+  s = s<<4;\r
+  b = p[-4];\r
+  b &= 0x7f;\r
+  b = b>>3;\r
+  s |= b;\r
+\r
+  *v = ((u64)s)<<32 | a;\r
+\r
+  return 9;\r
+}\r
+\r
+/*\r
+** Read a 32-bit variable-length integer from memory starting at p[0].\r
+** Return the number of bytes read.  The value is stored in *v.\r
+**\r
+** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned\r
+** integer, then set *v to 0xffffffff.\r
+**\r
+** A MACRO version, getVarint32, is provided which inlines the \r
+** single-byte case.  All code should use the MACRO version as \r
+** this function assumes the single-byte case has already been handled.\r
+*/\r
+u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){\r
+  u32 a,b;\r
+\r
+  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline\r
+  ** by the getVarin32() macro */\r
+  a = *p;\r
+  /* a: p0 (unmasked) */\r
+#ifndef getVarint32\r
+  if (!(a&0x80))\r
+  {\r
+    /* Values between 0 and 127 */\r
+    *v = a;\r
+    return 1;\r
+  }\r
+#endif\r
+\r
+  /* The 2-byte case */\r
+  p++;\r
+  b = *p;\r
+  /* b: p1 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    /* Values between 128 and 16383 */\r
+    a &= 0x7f;\r
+    a = a<<7;\r
+    *v = a | b;\r
+    return 2;\r
+  }\r
+\r
+  /* The 3-byte case */\r
+  p++;\r
+  a = a<<14;\r
+  a |= *p;\r
+  /* a: p0<<14 | p2 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    /* Values between 16384 and 2097151 */\r
+    a &= (0x7f<<14)|(0x7f);\r
+    b &= 0x7f;\r
+    b = b<<7;\r
+    *v = a | b;\r
+    return 3;\r
+  }\r
+\r
+  /* A 32-bit varint is used to store size information in btrees.\r
+  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.\r
+  ** A 3-byte varint is sufficient, for example, to record the size\r
+  ** of a 1048569-byte BLOB or string.\r
+  **\r
+  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very\r
+  ** rare larger cases can be handled by the slower 64-bit varint\r
+  ** routine.\r
+  */\r
+#if 1\r
+  {\r
+    u64 v64;\r
+    u8 n;\r
+\r
+    p -= 2;\r
+    n = sqlite3GetVarint(p, &v64);\r
+    assert( n>3 && n<=9 );\r
+    if( (v64 & SQLITE_MAX_U32)!=v64 ){\r
+      *v = 0xffffffff;\r
+    }else{\r
+      *v = (u32)v64;\r
+    }\r
+    return n;\r
+  }\r
+\r
+#else\r
+  /* For following code (kept for historical record only) shows an\r
+  ** unrolling for the 3- and 4-byte varint cases.  This code is\r
+  ** slightly faster, but it is also larger and much harder to test.\r
+  */\r
+  p++;\r
+  b = b<<14;\r
+  b |= *p;\r
+  /* b: p1<<14 | p3 (unmasked) */\r
+  if (!(b&0x80))\r
+  {\r
+    /* Values between 2097152 and 268435455 */\r
+    b &= (0x7f<<14)|(0x7f);\r
+    a &= (0x7f<<14)|(0x7f);\r
+    a = a<<7;\r
+    *v = a | b;\r
+    return 4;\r
+  }\r
+\r
+  p++;\r
+  a = a<<14;\r
+  a |= *p;\r
+  /* a: p0<<28 | p2<<14 | p4 (unmasked) */\r
+  if (!(a&0x80))\r
+  {\r
+    /* Walues  between 268435456 and 34359738367 */\r
+    a &= (0x1f<<28)|(0x7f<<14)|(0x7f);\r
+    b &= (0x1f<<28)|(0x7f<<14)|(0x7f);\r
+    b = b<<7;\r
+    *v = a | b;\r
+    return 5;\r
+  }\r
+\r
+  /* We can only reach this point when reading a corrupt database\r
+  ** file.  In that case we are not in any hurry.  Use the (relatively\r
+  ** slow) general-purpose sqlite3GetVarint() routine to extract the\r
+  ** value. */\r
+  {\r
+    u64 v64;\r
+    u8 n;\r
+\r
+    p -= 4;\r
+    n = sqlite3GetVarint(p, &v64);\r
+    assert( n>5 && n<=9 );\r
+    *v = (u32)v64;\r
+    return n;\r
+  }\r
+#endif\r
+}\r
+\r
+/*\r
+** Return the number of bytes that will be needed to store the given\r
+** 64-bit integer.\r
+*/\r
+int sqlite3VarintLen(u64 v){\r
+  int i = 0;\r
+  do{\r
+    i++;\r
+    v >>= 7;\r
+  }while( v!=0 && ALWAYS(i<9) );\r
+  return i;\r
+}\r
+\r
+\r
+/*\r
+** Read or write a four-byte big-endian integer value.\r
+*/\r
+u32 sqlite3Get4byte(const u8 *p){\r
+  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];\r
+}\r
+void sqlite3Put4byte(unsigned char *p, u32 v){\r
+  p[0] = (u8)(v>>24);\r
+  p[1] = (u8)(v>>16);\r
+  p[2] = (u8)(v>>8);\r
+  p[3] = (u8)v;\r
+}\r
+\r
+\r
+\r
+#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)\r
+/*\r
+** Translate a single byte of Hex into an integer.\r
+** This routine only works if h really is a valid hexadecimal\r
+** character:  0..9a..fA..F\r
+*/\r
+static u8 hexToInt(int h){\r
+  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );\r
+#ifdef SQLITE_ASCII\r
+  h += 9*(1&(h>>6));\r
+#endif\r
+#ifdef SQLITE_EBCDIC\r
+  h += 9*(1&~(h>>4));\r
+#endif\r
+  return (u8)(h & 0xf);\r
+}\r
+#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */\r
+\r
+#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)\r
+/*\r
+** Convert a BLOB literal of the form "x'hhhhhh'" into its binary\r
+** value.  Return a pointer to its binary value.  Space to hold the\r
+** binary value has been obtained from malloc and must be freed by\r
+** the calling routine.\r
+*/\r
+void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){\r
+  char *zBlob;\r
+  int i;\r
+\r
+  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);\r
+  n--;\r
+  if( zBlob ){\r
+    for(i=0; i<n; i+=2){\r
+      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);\r
+    }\r
+    zBlob[i/2] = 0;\r
+  }\r
+  return zBlob;\r
+}\r
+#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */\r
+\r
+\r
+/*\r
+** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.\r
+** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN\r
+** when this routine is called.\r
+**\r
+** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN\r
+** value indicates that the database connection passed into the API is\r
+** open and is not being used by another thread.  By changing the value\r
+** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.\r
+** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN\r
+** when the API exits. \r
+**\r
+** This routine is a attempt to detect if two threads use the\r
+** same sqlite* pointer at the same time.  There is a race \r
+** condition so it is possible that the error is not detected.\r
+** But usually the problem will be seen.  The result will be an\r
+** error which can be used to debug the application that is\r
+** using SQLite incorrectly.\r
+**\r
+** Ticket #202:  If db->magic is not a valid open value, take care not\r
+** to modify the db structure at all.  It could be that db is a stale\r
+** pointer.  In other words, it could be that there has been a prior\r
+** call to sqlite3_close(db) and db has been deallocated.  And we do\r
+** not want to write into deallocated memory.\r
+*/\r
+#ifdef SQLITE_DEBUG\r
+int sqlite3SafetyOn(sqlite3 *db){\r
+  if( db->magic==SQLITE_MAGIC_OPEN ){\r
+    db->magic = SQLITE_MAGIC_BUSY;\r
+    assert( sqlite3_mutex_held(db->mutex) );\r
+    return 0;\r
+  }else if( db->magic==SQLITE_MAGIC_BUSY ){\r
+    db->magic = SQLITE_MAGIC_ERROR;\r
+    db->u1.isInterrupted = 1;\r
+  }\r
+  return 1;\r
+}\r
+#endif\r
+\r
+/*\r
+** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.\r
+** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY\r
+** when this routine is called.\r
+*/\r
+#ifdef SQLITE_DEBUG\r
+int sqlite3SafetyOff(sqlite3 *db){\r
+  if( db->magic==SQLITE_MAGIC_BUSY ){\r
+    db->magic = SQLITE_MAGIC_OPEN;\r
+    assert( sqlite3_mutex_held(db->mutex) );\r
+    return 0;\r
+  }else{\r
+    db->magic = SQLITE_MAGIC_ERROR;\r
+    db->u1.isInterrupted = 1;\r
+    return 1;\r
+  }\r
+}\r
+#endif\r
+\r
+/*\r
+** Check to make sure we have a valid db pointer.  This test is not\r
+** foolproof but it does provide some measure of protection against\r
+** misuse of the interface such as passing in db pointers that are\r
+** NULL or which have been previously closed.  If this routine returns\r
+** 1 it means that the db pointer is valid and 0 if it should not be\r
+** dereferenced for any reason.  The calling function should invoke\r
+** SQLITE_MISUSE immediately.\r
+**\r
+** sqlite3SafetyCheckOk() requires that the db pointer be valid for\r
+** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to\r
+** open properly and is not fit for general use but which can be\r
+** used as an argument to sqlite3_errmsg() or sqlite3_close().\r
+*/\r
+int sqlite3SafetyCheckOk(sqlite3 *db){\r
+  u32 magic;\r
+  if( db==0 ) return 0;\r
+  magic = db->magic;\r
+  if( magic!=SQLITE_MAGIC_OPEN \r
+#ifdef SQLITE_DEBUG\r
+     && magic!=SQLITE_MAGIC_BUSY\r
+#endif\r
+  ){\r
+    return 0;\r
+  }else{\r
+    return 1;\r
+  }\r
+}\r
+int sqlite3SafetyCheckSickOrOk(sqlite3 *db){\r
+  u32 magic;\r
+  magic = db->magic;\r
+  if( magic!=SQLITE_MAGIC_SICK &&\r
+      magic!=SQLITE_MAGIC_OPEN &&\r
+      magic!=SQLITE_MAGIC_BUSY ) return 0;\r
+  return 1;\r
+}\r