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