sl@0: /*
sl@0: ** 2001 September 15
sl@0: **
sl@0: ** The author disclaims copyright to this source code.  In place of
sl@0: ** a legal notice, here is a blessing:
sl@0: **
sl@0: **    May you do good and not evil.
sl@0: **    May you find forgiveness for yourself and forgive others.
sl@0: **    May you share freely, never taking more than you give.
sl@0: **
sl@0: *************************************************************************
sl@0: ** Utility functions used throughout sqlite.
sl@0: **
sl@0: ** This file contains functions for allocating memory, comparing
sl@0: ** strings, and stuff like that.
sl@0: **
sl@0: ** $Id: util.c,v 1.241 2008/07/28 19:34:54 drh Exp $
sl@0: */
sl@0: #include "sqliteInt.h"
sl@0: #include <stdarg.h>
sl@0: #include <ctype.h>
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Return true if the floating point value is Not a Number (NaN).
sl@0: */
sl@0: int sqlite3IsNaN(double x){
sl@0:   /* This NaN test sometimes fails if compiled on GCC with -ffast-math.
sl@0:   ** On the other hand, the use of -ffast-math comes with the following
sl@0:   ** warning:
sl@0:   **
sl@0:   **      This option [-ffast-math] should never be turned on by any
sl@0:   **      -O option since it can result in incorrect output for programs
sl@0:   **      which depend on an exact implementation of IEEE or ISO 
sl@0:   **      rules/specifications for math functions.
sl@0:   **
sl@0:   ** Under MSVC, this NaN test may fail if compiled with a floating-
sl@0:   ** point precision mode other than /fp:precise.  From the MSDN 
sl@0:   ** documentation:
sl@0:   **
sl@0:   **      The compiler [with /fp:precise] will properly handle comparisons 
sl@0:   **      involving NaN. For example, x != x evaluates to true if x is NaN 
sl@0:   **      ...
sl@0:   */
sl@0: #ifdef __FAST_MATH__
sl@0: # error SQLite will not work correctly with the -ffast-math option of GCC.
sl@0: #endif
sl@0:   volatile double y = x;
sl@0:   volatile double z = y;
sl@0:   return y!=z;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the length of a string, except do not allow the string length
sl@0: ** to exceed the SQLITE_LIMIT_LENGTH setting.
sl@0: */
sl@0: int sqlite3Strlen(sqlite3 *db, const char *z){
sl@0:   const char *z2 = z;
sl@0:   int len;
sl@0:   size_t x;
sl@0:   while( *z2 ){ z2++; }
sl@0:   x = z2 - z;
sl@0:   len = 0x7fffffff & x;
sl@0:   if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
sl@0:     return db->aLimit[SQLITE_LIMIT_LENGTH];
sl@0:   }else{
sl@0:     return len;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Set the most recent error code and error string for the sqlite
sl@0: ** handle "db". The error code is set to "err_code".
sl@0: **
sl@0: ** If it is not NULL, string zFormat specifies the format of the
sl@0: ** error string in the style of the printf functions: The following
sl@0: ** format characters are allowed:
sl@0: **
sl@0: **      %s      Insert a string
sl@0: **      %z      A string that should be freed after use
sl@0: **      %d      Insert an integer
sl@0: **      %T      Insert a token
sl@0: **      %S      Insert the first element of a SrcList
sl@0: **
sl@0: ** zFormat and any string tokens that follow it are assumed to be
sl@0: ** encoded in UTF-8.
sl@0: **
sl@0: ** To clear the most recent error for sqlite handle "db", sqlite3Error
sl@0: ** should be called with err_code set to SQLITE_OK and zFormat set
sl@0: ** to NULL.
sl@0: */
sl@0: void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
sl@0:   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
sl@0:     db->errCode = err_code;
sl@0:     if( zFormat ){
sl@0:       char *z;
sl@0:       va_list ap;
sl@0:       va_start(ap, zFormat);
sl@0:       z = sqlite3VMPrintf(db, zFormat, ap);
sl@0:       va_end(ap);
sl@0:       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
sl@0:     }else{
sl@0:       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
sl@0: ** The following formatting characters are allowed:
sl@0: **
sl@0: **      %s      Insert a string
sl@0: **      %z      A string that should be freed after use
sl@0: **      %d      Insert an integer
sl@0: **      %T      Insert a token
sl@0: **      %S      Insert the first element of a SrcList
sl@0: **
sl@0: ** This function should be used to report any error that occurs whilst
sl@0: ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
sl@0: ** last thing the sqlite3_prepare() function does is copy the error
sl@0: ** stored by this function into the database handle using sqlite3Error().
sl@0: ** Function sqlite3Error() should be used during statement execution
sl@0: ** (sqlite3_step() etc.).
sl@0: */
sl@0: void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
sl@0:   va_list ap;
sl@0:   sqlite3 *db = pParse->db;
sl@0:   pParse->nErr++;
sl@0:   sqlite3DbFree(db, pParse->zErrMsg);
sl@0:   va_start(ap, zFormat);
sl@0:   pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
sl@0:   va_end(ap);
sl@0:   if( pParse->rc==SQLITE_OK ){
sl@0:     pParse->rc = SQLITE_ERROR;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Clear the error message in pParse, if any
sl@0: */
sl@0: void sqlite3ErrorClear(Parse *pParse){
sl@0:   sqlite3DbFree(pParse->db, pParse->zErrMsg);
sl@0:   pParse->zErrMsg = 0;
sl@0:   pParse->nErr = 0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Convert an SQL-style quoted string into a normal string by removing
sl@0: ** the quote characters.  The conversion is done in-place.  If the
sl@0: ** input does not begin with a quote character, then this routine
sl@0: ** is a no-op.
sl@0: **
sl@0: ** 2002-Feb-14: This routine is extended to remove MS-Access style
sl@0: ** brackets from around identifers.  For example:  "[a-b-c]" becomes
sl@0: ** "a-b-c".
sl@0: */
sl@0: void sqlite3Dequote(char *z){
sl@0:   int quote;
sl@0:   int i, j;
sl@0:   if( z==0 ) return;
sl@0:   quote = z[0];
sl@0:   switch( quote ){
sl@0:     case '\'':  break;
sl@0:     case '"':   break;
sl@0:     case '`':   break;                /* For MySQL compatibility */
sl@0:     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
sl@0:     default:    return;
sl@0:   }
sl@0:   for(i=1, j=0; z[i]; i++){
sl@0:     if( z[i]==quote ){
sl@0:       if( z[i+1]==quote ){
sl@0:         z[j++] = quote;
sl@0:         i++;
sl@0:       }else{
sl@0:         z[j++] = 0;
sl@0:         break;
sl@0:       }
sl@0:     }else{
sl@0:       z[j++] = z[i];
sl@0:     }
sl@0:   }
sl@0: }
sl@0: 
sl@0: /* Convenient short-hand */
sl@0: #define UpperToLower sqlite3UpperToLower
sl@0: 
sl@0: /*
sl@0: ** Some systems have stricmp().  Others have strcasecmp().  Because
sl@0: ** there is no consistency, we will define our own.
sl@0: */
sl@0: int sqlite3StrICmp(const char *zLeft, const char *zRight){
sl@0:   register unsigned char *a, *b;
sl@0:   a = (unsigned char *)zLeft;
sl@0:   b = (unsigned char *)zRight;
sl@0:   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
sl@0:   return UpperToLower[*a] - UpperToLower[*b];
sl@0: }
sl@0: int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
sl@0:   register unsigned char *a, *b;
sl@0:   a = (unsigned char *)zLeft;
sl@0:   b = (unsigned char *)zRight;
sl@0:   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
sl@0:   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return TRUE if z is a pure numeric string.  Return FALSE if the
sl@0: ** string contains any character which is not part of a number. If
sl@0: ** the string is numeric and contains the '.' character, set *realnum
sl@0: ** to TRUE (otherwise FALSE).
sl@0: **
sl@0: ** An empty string is considered non-numeric.
sl@0: */
sl@0: int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
sl@0:   int incr = (enc==SQLITE_UTF8?1:2);
sl@0:   if( enc==SQLITE_UTF16BE ) z++;
sl@0:   if( *z=='-' || *z=='+' ) z += incr;
sl@0:   if( !isdigit(*(u8*)z) ){
sl@0:     return 0;
sl@0:   }
sl@0:   z += incr;
sl@0:   if( realnum ) *realnum = 0;
sl@0:   while( isdigit(*(u8*)z) ){ z += incr; }
sl@0:   if( *z=='.' ){
sl@0:     z += incr;
sl@0:     if( !isdigit(*(u8*)z) ) return 0;
sl@0:     while( isdigit(*(u8*)z) ){ z += incr; }
sl@0:     if( realnum ) *realnum = 1;
sl@0:   }
sl@0:   if( *z=='e' || *z=='E' ){
sl@0:     z += incr;
sl@0:     if( *z=='+' || *z=='-' ) z += incr;
sl@0:     if( !isdigit(*(u8*)z) ) return 0;
sl@0:     while( isdigit(*(u8*)z) ){ z += incr; }
sl@0:     if( realnum ) *realnum = 1;
sl@0:   }
sl@0:   return *z==0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The string z[] is an ascii representation of a real number.
sl@0: ** Convert this string to a double.
sl@0: **
sl@0: ** This routine assumes that z[] really is a valid number.  If it
sl@0: ** is not, the result is undefined.
sl@0: **
sl@0: ** This routine is used instead of the library atof() function because
sl@0: ** the library atof() might want to use "," as the decimal point instead
sl@0: ** of "." depending on how locale is set.  But that would cause problems
sl@0: ** for SQL.  So this routine always uses "." regardless of locale.
sl@0: */
sl@0: int sqlite3AtoF(const char *z, double *pResult){
sl@0: #ifndef SQLITE_OMIT_FLOATING_POINT
sl@0:   int sign = 1;
sl@0:   const char *zBegin = z;
sl@0:   LONGDOUBLE_TYPE v1 = 0.0;
sl@0:   int nSignificant = 0;
sl@0:   while( isspace(*(u8*)z) ) z++;
sl@0:   if( *z=='-' ){
sl@0:     sign = -1;
sl@0:     z++;
sl@0:   }else if( *z=='+' ){
sl@0:     z++;
sl@0:   }
sl@0:   while( z[0]=='0' ){
sl@0:     z++;
sl@0:   }
sl@0:   while( isdigit(*(u8*)z) ){
sl@0:     v1 = v1*10.0 + (*z - '0');
sl@0:     z++;
sl@0:     nSignificant++;
sl@0:   }
sl@0:   if( *z=='.' ){
sl@0:     LONGDOUBLE_TYPE divisor = 1.0;
sl@0:     z++;
sl@0:     if( nSignificant==0 ){
sl@0:       while( z[0]=='0' ){
sl@0:         divisor *= 10.0;
sl@0:         z++;
sl@0:       }
sl@0:     }
sl@0:     while( isdigit(*(u8*)z) ){
sl@0:       if( nSignificant<18 ){
sl@0:         v1 = v1*10.0 + (*z - '0');
sl@0:         divisor *= 10.0;
sl@0:         nSignificant++;
sl@0:       }
sl@0:       z++;
sl@0:     }
sl@0:     v1 /= divisor;
sl@0:   }
sl@0:   if( *z=='e' || *z=='E' ){
sl@0:     int esign = 1;
sl@0:     int eval = 0;
sl@0:     LONGDOUBLE_TYPE scale = 1.0;
sl@0:     z++;
sl@0:     if( *z=='-' ){
sl@0:       esign = -1;
sl@0:       z++;
sl@0:     }else if( *z=='+' ){
sl@0:       z++;
sl@0:     }
sl@0:     while( isdigit(*(u8*)z) ){
sl@0:       eval = eval*10 + *z - '0';
sl@0:       z++;
sl@0:     }
sl@0:     while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
sl@0:     while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
sl@0:     while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
sl@0:     while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
sl@0:     if( esign<0 ){
sl@0:       v1 /= scale;
sl@0:     }else{
sl@0:       v1 *= scale;
sl@0:     }
sl@0:   }
sl@0:   *pResult = sign<0 ? -v1 : v1;
sl@0:   return z - zBegin;
sl@0: #else
sl@0:   return sqlite3Atoi64(z, pResult);
sl@0: #endif /* SQLITE_OMIT_FLOATING_POINT */
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Compare the 19-character string zNum against the text representation
sl@0: ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
sl@0: ** if zNum is less than, equal to, or greater than the string.
sl@0: **
sl@0: ** Unlike memcmp() this routine is guaranteed to return the difference
sl@0: ** in the values of the last digit if the only difference is in the
sl@0: ** last digit.  So, for example,
sl@0: **
sl@0: **      compare2pow63("9223372036854775800")
sl@0: **
sl@0: ** will return -8.
sl@0: */
sl@0: static int compare2pow63(const char *zNum){
sl@0:   int c;
sl@0:   c = memcmp(zNum,"922337203685477580",18);
sl@0:   if( c==0 ){
sl@0:     c = zNum[18] - '8';
sl@0:   }
sl@0:   return c;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Return TRUE if zNum is a 64-bit signed integer and write
sl@0: ** the value of the integer into *pNum.  If zNum is not an integer
sl@0: ** or is an integer that is too large to be expressed with 64 bits,
sl@0: ** then return false.
sl@0: **
sl@0: ** When this routine was originally written it dealt with only
sl@0: ** 32-bit numbers.  At that time, it was much faster than the
sl@0: ** atoi() library routine in RedHat 7.2.
sl@0: */
sl@0: int sqlite3Atoi64(const char *zNum, i64 *pNum){
sl@0:   i64 v = 0;
sl@0:   int neg;
sl@0:   int i, c;
sl@0:   const char *zStart;
sl@0:   while( isspace(*(u8*)zNum) ) zNum++;
sl@0:   if( *zNum=='-' ){
sl@0:     neg = 1;
sl@0:     zNum++;
sl@0:   }else if( *zNum=='+' ){
sl@0:     neg = 0;
sl@0:     zNum++;
sl@0:   }else{
sl@0:     neg = 0;
sl@0:   }
sl@0:   zStart = zNum;
sl@0:   while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
sl@0:   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
sl@0:     v = v*10 + c - '0';
sl@0:   }
sl@0:   *pNum = neg ? -v : v;
sl@0:   if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
sl@0:     /* zNum is empty or contains non-numeric text or is longer
sl@0:     ** than 19 digits (thus guaranting that it is too large) */
sl@0:     return 0;
sl@0:   }else if( i<19 ){
sl@0:     /* Less than 19 digits, so we know that it fits in 64 bits */
sl@0:     return 1;
sl@0:   }else{
sl@0:     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
sl@0:     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
sl@0:     ** is 2^63. */
sl@0:     return compare2pow63(zNum)<neg;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The string zNum represents an integer.  There might be some other
sl@0: ** information following the integer too, but that part is ignored.
sl@0: ** If the integer that the prefix of zNum represents will fit in a
sl@0: ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
sl@0: **
sl@0: ** This routine returns FALSE for the string -9223372036854775808 even that
sl@0: ** that number will, in theory fit in a 64-bit integer.  Positive
sl@0: ** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
sl@0: ** false.
sl@0: */
sl@0: int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
sl@0:   int i, c;
sl@0:   int neg = 0;
sl@0:   if( *zNum=='-' ){
sl@0:     neg = 1;
sl@0:     zNum++;
sl@0:   }else if( *zNum=='+' ){
sl@0:     zNum++;
sl@0:   }
sl@0:   if( negFlag ) neg = 1-neg;
sl@0:   while( *zNum=='0' ){
sl@0:     zNum++;   /* Skip leading zeros.  Ticket #2454 */
sl@0:   }
sl@0:   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
sl@0:   if( i<19 ){
sl@0:     /* Guaranteed to fit if less than 19 digits */
sl@0:     return 1;
sl@0:   }else if( i>19 ){
sl@0:     /* Guaranteed to be too big if greater than 19 digits */
sl@0:     return 0;
sl@0:   }else{
sl@0:     /* Compare against 2^63. */
sl@0:     return compare2pow63(zNum)<neg;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** If zNum represents an integer that will fit in 32-bits, then set
sl@0: ** *pValue to that integer and return true.  Otherwise return false.
sl@0: **
sl@0: ** Any non-numeric characters that following zNum are ignored.
sl@0: ** This is different from sqlite3Atoi64() which requires the
sl@0: ** input number to be zero-terminated.
sl@0: */
sl@0: int sqlite3GetInt32(const char *zNum, int *pValue){
sl@0:   sqlite_int64 v = 0;
sl@0:   int i, c;
sl@0:   int neg = 0;
sl@0:   if( zNum[0]=='-' ){
sl@0:     neg = 1;
sl@0:     zNum++;
sl@0:   }else if( zNum[0]=='+' ){
sl@0:     zNum++;
sl@0:   }
sl@0:   while( zNum[0]=='0' ) zNum++;
sl@0:   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
sl@0:     v = v*10 + c;
sl@0:   }
sl@0: 
sl@0:   /* The longest decimal representation of a 32 bit integer is 10 digits:
sl@0:   **
sl@0:   **             1234567890
sl@0:   **     2^31 -> 2147483648
sl@0:   */
sl@0:   if( i>10 ){
sl@0:     return 0;
sl@0:   }
sl@0:   if( v-neg>2147483647 ){
sl@0:     return 0;
sl@0:   }
sl@0:   if( neg ){
sl@0:     v = -v;
sl@0:   }
sl@0:   *pValue = (int)v;
sl@0:   return 1;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The variable-length integer encoding is as follows:
sl@0: **
sl@0: ** KEY:
sl@0: **         A = 0xxxxxxx    7 bits of data and one flag bit
sl@0: **         B = 1xxxxxxx    7 bits of data and one flag bit
sl@0: **         C = xxxxxxxx    8 bits of data
sl@0: **
sl@0: **  7 bits - A
sl@0: ** 14 bits - BA
sl@0: ** 21 bits - BBA
sl@0: ** 28 bits - BBBA
sl@0: ** 35 bits - BBBBA
sl@0: ** 42 bits - BBBBBA
sl@0: ** 49 bits - BBBBBBA
sl@0: ** 56 bits - BBBBBBBA
sl@0: ** 64 bits - BBBBBBBBC
sl@0: */
sl@0: 
sl@0: /*
sl@0: ** Write a 64-bit variable-length integer to memory starting at p[0].
sl@0: ** The length of data write will be between 1 and 9 bytes.  The number
sl@0: ** of bytes written is returned.
sl@0: **
sl@0: ** A variable-length integer consists of the lower 7 bits of each byte
sl@0: ** for all bytes that have the 8th bit set and one byte with the 8th
sl@0: ** bit clear.  Except, if we get to the 9th byte, it stores the full
sl@0: ** 8 bits and is the last byte.
sl@0: */
sl@0: int sqlite3PutVarint(unsigned char *p, u64 v){
sl@0:   int i, j, n;
sl@0:   u8 buf[10];
sl@0:   if( v & (((u64)0xff000000)<<32) ){
sl@0:     p[8] = v;
sl@0:     v >>= 8;
sl@0:     for(i=7; i>=0; i--){
sl@0:       p[i] = (v & 0x7f) | 0x80;
sl@0:       v >>= 7;
sl@0:     }
sl@0:     return 9;
sl@0:   }    
sl@0:   n = 0;
sl@0:   do{
sl@0:     buf[n++] = (v & 0x7f) | 0x80;
sl@0:     v >>= 7;
sl@0:   }while( v!=0 );
sl@0:   buf[0] &= 0x7f;
sl@0:   assert( n<=9 );
sl@0:   for(i=0, j=n-1; j>=0; j--, i++){
sl@0:     p[i] = buf[j];
sl@0:   }
sl@0:   return n;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine is a faster version of sqlite3PutVarint() that only
sl@0: ** works for 32-bit positive integers and which is optimized for
sl@0: ** the common case of small integers.  A MACRO version, putVarint32,
sl@0: ** is provided which inlines the single-byte case.  All code should use
sl@0: ** the MACRO version as this function assumes the single-byte case has
sl@0: ** already been handled.
sl@0: */
sl@0: int sqlite3PutVarint32(unsigned char *p, u32 v){
sl@0: #ifndef putVarint32
sl@0:   if( (v & ~0x7f)==0 ){
sl@0:     p[0] = v;
sl@0:     return 1;
sl@0:   }
sl@0: #endif
sl@0:   if( (v & ~0x3fff)==0 ){
sl@0:     p[0] = (v>>7) | 0x80;
sl@0:     p[1] = v & 0x7f;
sl@0:     return 2;
sl@0:   }
sl@0:   return sqlite3PutVarint(p, v);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
sl@0: ** are defined here rather than simply putting the constant expressions
sl@0: ** inline in order to work around bugs in the RVT compiler.
sl@0: **
sl@0: ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
sl@0: **
sl@0: ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
sl@0: */
sl@0: #define SLOT_2_0     0x001fc07f
sl@0: #define SLOT_4_2_0   0xf01fc07f
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Read a 64-bit variable-length integer from memory starting at p[0].
sl@0: ** Return the number of bytes read.  The value is stored in *v.
sl@0: */
sl@0: int sqlite3GetVarint(const unsigned char *p, u64 *v){
sl@0:   u32 a,b,s;
sl@0: 
sl@0:   a = *p;
sl@0:   /* a: p0 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     *v = a;
sl@0:     return 1;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   b = *p;
sl@0:   /* b: p1 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     a &= 0x7f;
sl@0:     a = a<<7;
sl@0:     a |= b;
sl@0:     *v = a;
sl@0:     return 2;
sl@0:   }
sl@0: 
sl@0:   /* Verify that constants are precomputed correctly */
sl@0:   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
sl@0:   assert( SLOT_4_2_0 == ((0xf<<28) | (0x7f<<14) | (0x7f)) );
sl@0: 
sl@0:   p++;
sl@0:   a = a<<14;
sl@0:   a |= *p;
sl@0:   /* a: p0<<14 | p2 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     a &= SLOT_2_0;
sl@0:     b &= 0x7f;
sl@0:     b = b<<7;
sl@0:     a |= b;
sl@0:     *v = a;
sl@0:     return 3;
sl@0:   }
sl@0: 
sl@0:   /* CSE1 from below */
sl@0:   a &= SLOT_2_0;
sl@0:   p++;
sl@0:   b = b<<14;
sl@0:   b |= *p;
sl@0:   /* b: p1<<14 | p3 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     b &= SLOT_2_0;
sl@0:     /* moved CSE1 up */
sl@0:     /* a &= (0x7f<<14)|(0x7f); */
sl@0:     a = a<<7;
sl@0:     a |= b;
sl@0:     *v = a;
sl@0:     return 4;
sl@0:   }
sl@0: 
sl@0:   /* a: p0<<14 | p2 (masked) */
sl@0:   /* b: p1<<14 | p3 (unmasked) */
sl@0:   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
sl@0:   /* moved CSE1 up */
sl@0:   /* a &= (0x7f<<14)|(0x7f); */
sl@0:   b &= SLOT_2_0;
sl@0:   s = a;
sl@0:   /* s: p0<<14 | p2 (masked) */
sl@0: 
sl@0:   p++;
sl@0:   a = a<<14;
sl@0:   a |= *p;
sl@0:   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     /* we can skip these cause they were (effectively) done above in calc'ing s */
sl@0:     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
sl@0:     /* b &= (0x7f<<14)|(0x7f); */
sl@0:     b = b<<7;
sl@0:     a |= b;
sl@0:     s = s>>18;
sl@0:     *v = ((u64)s)<<32 | a;
sl@0:     return 5;
sl@0:   }
sl@0: 
sl@0:   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
sl@0:   s = s<<7;
sl@0:   s |= b;
sl@0:   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
sl@0: 
sl@0:   p++;
sl@0:   b = b<<14;
sl@0:   b |= *p;
sl@0:   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     /* we can skip this cause it was (effectively) done above in calc'ing s */
sl@0:     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
sl@0:     a &= SLOT_2_0;
sl@0:     a = a<<7;
sl@0:     a |= b;
sl@0:     s = s>>18;
sl@0:     *v = ((u64)s)<<32 | a;
sl@0:     return 6;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   a = a<<14;
sl@0:   a |= *p;
sl@0:   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     a &= SLOT_4_2_0;
sl@0:     b &= SLOT_2_0;
sl@0:     b = b<<7;
sl@0:     a |= b;
sl@0:     s = s>>11;
sl@0:     *v = ((u64)s)<<32 | a;
sl@0:     return 7;
sl@0:   }
sl@0: 
sl@0:   /* CSE2 from below */
sl@0:   a &= SLOT_2_0;
sl@0:   p++;
sl@0:   b = b<<14;
sl@0:   b |= *p;
sl@0:   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     b &= SLOT_4_2_0;
sl@0:     /* moved CSE2 up */
sl@0:     /* a &= (0x7f<<14)|(0x7f); */
sl@0:     a = a<<7;
sl@0:     a |= b;
sl@0:     s = s>>4;
sl@0:     *v = ((u64)s)<<32 | a;
sl@0:     return 8;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   a = a<<15;
sl@0:   a |= *p;
sl@0:   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
sl@0: 
sl@0:   /* moved CSE2 up */
sl@0:   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
sl@0:   b &= SLOT_2_0;
sl@0:   b = b<<8;
sl@0:   a |= b;
sl@0: 
sl@0:   s = s<<4;
sl@0:   b = p[-4];
sl@0:   b &= 0x7f;
sl@0:   b = b>>3;
sl@0:   s |= b;
sl@0: 
sl@0:   *v = ((u64)s)<<32 | a;
sl@0: 
sl@0:   return 9;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Read a 32-bit variable-length integer from memory starting at p[0].
sl@0: ** Return the number of bytes read.  The value is stored in *v.
sl@0: ** A MACRO version, getVarint32, is provided which inlines the 
sl@0: ** single-byte case.  All code should use the MACRO version as 
sl@0: ** this function assumes the single-byte case has already been handled.
sl@0: */
sl@0: int sqlite3GetVarint32(const unsigned char *p, u32 *v){
sl@0:   u32 a,b;
sl@0: 
sl@0:   a = *p;
sl@0:   /* a: p0 (unmasked) */
sl@0: #ifndef getVarint32
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     *v = a;
sl@0:     return 1;
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   p++;
sl@0:   b = *p;
sl@0:   /* b: p1 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     a &= 0x7f;
sl@0:     a = a<<7;
sl@0:     *v = a | b;
sl@0:     return 2;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   a = a<<14;
sl@0:   a |= *p;
sl@0:   /* a: p0<<14 | p2 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     a &= (0x7f<<14)|(0x7f);
sl@0:     b &= 0x7f;
sl@0:     b = b<<7;
sl@0:     *v = a | b;
sl@0:     return 3;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   b = b<<14;
sl@0:   b |= *p;
sl@0:   /* b: p1<<14 | p3 (unmasked) */
sl@0:   if (!(b&0x80))
sl@0:   {
sl@0:     b &= (0x7f<<14)|(0x7f);
sl@0:     a &= (0x7f<<14)|(0x7f);
sl@0:     a = a<<7;
sl@0:     *v = a | b;
sl@0:     return 4;
sl@0:   }
sl@0: 
sl@0:   p++;
sl@0:   a = a<<14;
sl@0:   a |= *p;
sl@0:   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
sl@0:   if (!(a&0x80))
sl@0:   {
sl@0:     a &= SLOT_4_2_0;
sl@0:     b &= SLOT_4_2_0;
sl@0:     b = b<<7;
sl@0:     *v = a | b;
sl@0:     return 5;
sl@0:   }
sl@0: 
sl@0:   /* We can only reach this point when reading a corrupt database
sl@0:   ** file.  In that case we are not in any hurry.  Use the (relatively
sl@0:   ** slow) general-purpose sqlite3GetVarint() routine to extract the
sl@0:   ** value. */
sl@0:   {
sl@0:     u64 v64;
sl@0:     int n;
sl@0: 
sl@0:     p -= 4;
sl@0:     n = sqlite3GetVarint(p, &v64);
sl@0:     assert( n>5 && n<=9 );
sl@0:     *v = (u32)v64;
sl@0:     return n;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the number of bytes that will be needed to store the given
sl@0: ** 64-bit integer.
sl@0: */
sl@0: int sqlite3VarintLen(u64 v){
sl@0:   int i = 0;
sl@0:   do{
sl@0:     i++;
sl@0:     v >>= 7;
sl@0:   }while( v!=0 && i<9 );
sl@0:   return i;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Read or write a four-byte big-endian integer value.
sl@0: */
sl@0: u32 sqlite3Get4byte(const u8 *p){
sl@0:   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
sl@0: }
sl@0: void sqlite3Put4byte(unsigned char *p, u32 v){
sl@0:   p[0] = v>>24;
sl@0:   p[1] = v>>16;
sl@0:   p[2] = v>>8;
sl@0:   p[3] = v;
sl@0: }
sl@0: 
sl@0: 
sl@0: 
sl@0: #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
sl@0: /*
sl@0: ** Translate a single byte of Hex into an integer.
sl@0: ** This routinen only works if h really is a valid hexadecimal
sl@0: ** character:  0..9a..fA..F
sl@0: */
sl@0: static int hexToInt(int h){
sl@0:   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
sl@0: #ifdef SQLITE_ASCII
sl@0:   h += 9*(1&(h>>6));
sl@0: #endif
sl@0: #ifdef SQLITE_EBCDIC
sl@0:   h += 9*(1&~(h>>4));
sl@0: #endif
sl@0:   return h & 0xf;
sl@0: }
sl@0: #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
sl@0: 
sl@0: #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
sl@0: /*
sl@0: ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
sl@0: ** value.  Return a pointer to its binary value.  Space to hold the
sl@0: ** binary value has been obtained from malloc and must be freed by
sl@0: ** the calling routine.
sl@0: */
sl@0: void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
sl@0:   char *zBlob;
sl@0:   int i;
sl@0: 
sl@0:   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
sl@0:   n--;
sl@0:   if( zBlob ){
sl@0:     for(i=0; i<n; i+=2){
sl@0:       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
sl@0:     }
sl@0:     zBlob[i/2] = 0;
sl@0:   }
sl@0:   return zBlob;
sl@0: }
sl@0: #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
sl@0: ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
sl@0: ** when this routine is called.
sl@0: **
sl@0: ** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
sl@0: ** value indicates that the database connection passed into the API is
sl@0: ** open and is not being used by another thread.  By changing the value
sl@0: ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
sl@0: ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
sl@0: ** when the API exits. 
sl@0: **
sl@0: ** This routine is a attempt to detect if two threads use the
sl@0: ** same sqlite* pointer at the same time.  There is a race 
sl@0: ** condition so it is possible that the error is not detected.
sl@0: ** But usually the problem will be seen.  The result will be an
sl@0: ** error which can be used to debug the application that is
sl@0: ** using SQLite incorrectly.
sl@0: **
sl@0: ** Ticket #202:  If db->magic is not a valid open value, take care not
sl@0: ** to modify the db structure at all.  It could be that db is a stale
sl@0: ** pointer.  In other words, it could be that there has been a prior
sl@0: ** call to sqlite3_close(db) and db has been deallocated.  And we do
sl@0: ** not want to write into deallocated memory.
sl@0: */
sl@0: #ifdef SQLITE_DEBUG
sl@0: int sqlite3SafetyOn(sqlite3 *db){
sl@0:   if( db->magic==SQLITE_MAGIC_OPEN ){
sl@0:     db->magic = SQLITE_MAGIC_BUSY;
sl@0:     assert( sqlite3_mutex_held(db->mutex) );
sl@0:     return 0;
sl@0:   }else if( db->magic==SQLITE_MAGIC_BUSY ){
sl@0:     db->magic = SQLITE_MAGIC_ERROR;
sl@0:     db->u1.isInterrupted = 1;
sl@0:   }
sl@0:   return 1;
sl@0: }
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
sl@0: ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
sl@0: ** when this routine is called.
sl@0: */
sl@0: #ifdef SQLITE_DEBUG
sl@0: int sqlite3SafetyOff(sqlite3 *db){
sl@0:   if( db->magic==SQLITE_MAGIC_BUSY ){
sl@0:     db->magic = SQLITE_MAGIC_OPEN;
sl@0:     assert( sqlite3_mutex_held(db->mutex) );
sl@0:     return 0;
sl@0:   }else{
sl@0:     db->magic = SQLITE_MAGIC_ERROR;
sl@0:     db->u1.isInterrupted = 1;
sl@0:     return 1;
sl@0:   }
sl@0: }
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Check to make sure we have a valid db pointer.  This test is not
sl@0: ** foolproof but it does provide some measure of protection against
sl@0: ** misuse of the interface such as passing in db pointers that are
sl@0: ** NULL or which have been previously closed.  If this routine returns
sl@0: ** 1 it means that the db pointer is valid and 0 if it should not be
sl@0: ** dereferenced for any reason.  The calling function should invoke
sl@0: ** SQLITE_MISUSE immediately.
sl@0: **
sl@0: ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
sl@0: ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
sl@0: ** open properly and is not fit for general use but which can be
sl@0: ** used as an argument to sqlite3_errmsg() or sqlite3_close().
sl@0: */
sl@0: int sqlite3SafetyCheckOk(sqlite3 *db){
sl@0:   int magic;
sl@0:   if( db==0 ) return 0;
sl@0:   magic = db->magic;
sl@0:   if( magic!=SQLITE_MAGIC_OPEN &&
sl@0:       magic!=SQLITE_MAGIC_BUSY ) return 0;
sl@0:   return 1;
sl@0: }
sl@0: int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
sl@0:   int magic;
sl@0:   if( db==0 ) return 0;
sl@0:   magic = db->magic;
sl@0:   if( magic!=SQLITE_MAGIC_SICK &&
sl@0:       magic!=SQLITE_MAGIC_OPEN &&
sl@0:       magic!=SQLITE_MAGIC_BUSY ) return 0;
sl@0:   return 1;
sl@0: }