util.c

Go to the documentation of this file.
00001 /*
00002 ** 2001 September 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** Utility functions used throughout sqlite.
00013 **
00014 ** This file contains functions for allocating memory, comparing
00015 ** strings, and stuff like that.
00016 **
00017 ** $Id: util.c,v 1.241 2008/07/28 19:34:54 drh Exp $
00018 */
00019 #include "sqliteInt.h"
00020 #include <stdarg.h>
00021 #include <ctype.h>
00022 
00023 
00024 /*
00025 ** Return true if the floating point value is Not a Number (NaN).
00026 */
00027 int sqlite3IsNaN(double x){
00028   /* This NaN test sometimes fails if compiled on GCC with -ffast-math.
00029   ** On the other hand, the use of -ffast-math comes with the following
00030   ** warning:
00031   **
00032   **      This option [-ffast-math] should never be turned on by any
00033   **      -O option since it can result in incorrect output for programs
00034   **      which depend on an exact implementation of IEEE or ISO 
00035   **      rules/specifications for math functions.
00036   **
00037   ** Under MSVC, this NaN test may fail if compiled with a floating-
00038   ** point precision mode other than /fp:precise.  From the MSDN 
00039   ** documentation:
00040   **
00041   **      The compiler [with /fp:precise] will properly handle comparisons 
00042   **      involving NaN. For example, x != x evaluates to true if x is NaN 
00043   **      ...
00044   */
00045 #ifdef __FAST_MATH__
00046 # error SQLite will not work correctly with the -ffast-math option of GCC.
00047 #endif
00048   volatile double y = x;
00049   volatile double z = y;
00050   return y!=z;
00051 }
00052 
00053 /*
00054 ** Return the length of a string, except do not allow the string length
00055 ** to exceed the SQLITE_LIMIT_LENGTH setting.
00056 */
00057 int sqlite3Strlen(sqlite3 *db, const char *z){
00058   const char *z2 = z;
00059   int len;
00060   size_t x;
00061   while( *z2 ){ z2++; }
00062   x = z2 - z;
00063   len = 0x7fffffff & x;
00064   if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
00065     return db->aLimit[SQLITE_LIMIT_LENGTH];
00066   }else{
00067     return len;
00068   }
00069 }
00070 
00071 /*
00072 ** Set the most recent error code and error string for the sqlite
00073 ** handle "db". The error code is set to "err_code".
00074 **
00075 ** If it is not NULL, string zFormat specifies the format of the
00076 ** error string in the style of the printf functions: The following
00077 ** format characters are allowed:
00078 **
00079 **      %s      Insert a string
00080 **      %z      A string that should be freed after use
00081 **      %d      Insert an integer
00082 **      %T      Insert a token
00083 **      %S      Insert the first element of a SrcList
00084 **
00085 ** zFormat and any string tokens that follow it are assumed to be
00086 ** encoded in UTF-8.
00087 **
00088 ** To clear the most recent error for sqlite handle "db", sqlite3Error
00089 ** should be called with err_code set to SQLITE_OK and zFormat set
00090 ** to NULL.
00091 */
00092 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
00093   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
00094     db->errCode = err_code;
00095     if( zFormat ){
00096       char *z;
00097       va_list ap;
00098       va_start(ap, zFormat);
00099       z = sqlite3VMPrintf(db, zFormat, ap);
00100       va_end(ap);
00101       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
00102     }else{
00103       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
00104     }
00105   }
00106 }
00107 
00108 /*
00109 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
00110 ** The following formatting characters are allowed:
00111 **
00112 **      %s      Insert a string
00113 **      %z      A string that should be freed after use
00114 **      %d      Insert an integer
00115 **      %T      Insert a token
00116 **      %S      Insert the first element of a SrcList
00117 **
00118 ** This function should be used to report any error that occurs whilst
00119 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
00120 ** last thing the sqlite3_prepare() function does is copy the error
00121 ** stored by this function into the database handle using sqlite3Error().
00122 ** Function sqlite3Error() should be used during statement execution
00123 ** (sqlite3_step() etc.).
00124 */
00125 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
00126   va_list ap;
00127   sqlite3 *db = pParse->db;
00128   pParse->nErr++;
00129   sqlite3DbFree(db, pParse->zErrMsg);
00130   va_start(ap, zFormat);
00131   pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
00132   va_end(ap);
00133   if( pParse->rc==SQLITE_OK ){
00134     pParse->rc = SQLITE_ERROR;
00135   }
00136 }
00137 
00138 /*
00139 ** Clear the error message in pParse, if any
00140 */
00141 void sqlite3ErrorClear(Parse *pParse){
00142   sqlite3DbFree(pParse->db, pParse->zErrMsg);
00143   pParse->zErrMsg = 0;
00144   pParse->nErr = 0;
00145 }
00146 
00147 /*
00148 ** Convert an SQL-style quoted string into a normal string by removing
00149 ** the quote characters.  The conversion is done in-place.  If the
00150 ** input does not begin with a quote character, then this routine
00151 ** is a no-op.
00152 **
00153 ** 2002-Feb-14: This routine is extended to remove MS-Access style
00154 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
00155 ** "a-b-c".
00156 */
00157 void sqlite3Dequote(char *z){
00158   int quote;
00159   int i, j;
00160   if( z==0 ) return;
00161   quote = z[0];
00162   switch( quote ){
00163     case '\'':  break;
00164     case '"':   break;
00165     case '`':   break;                /* For MySQL compatibility */
00166     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
00167     default:    return;
00168   }
00169   for(i=1, j=0; z[i]; i++){
00170     if( z[i]==quote ){
00171       if( z[i+1]==quote ){
00172         z[j++] = quote;
00173         i++;
00174       }else{
00175         z[j++] = 0;
00176         break;
00177       }
00178     }else{
00179       z[j++] = z[i];
00180     }
00181   }
00182 }
00183 
00184 /* Convenient short-hand */
00185 #define UpperToLower sqlite3UpperToLower
00186 
00187 /*
00188 ** Some systems have stricmp().  Others have strcasecmp().  Because
00189 ** there is no consistency, we will define our own.
00190 */
00191 int sqlite3StrICmp(const char *zLeft, const char *zRight){
00192   register unsigned char *a, *b;
00193   a = (unsigned char *)zLeft;
00194   b = (unsigned char *)zRight;
00195   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
00196   return UpperToLower[*a] - UpperToLower[*b];
00197 }
00198 int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
00199   register unsigned char *a, *b;
00200   a = (unsigned char *)zLeft;
00201   b = (unsigned char *)zRight;
00202   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
00203   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
00204 }
00205 
00206 /*
00207 ** Return TRUE if z is a pure numeric string.  Return FALSE if the
00208 ** string contains any character which is not part of a number. If
00209 ** the string is numeric and contains the '.' character, set *realnum
00210 ** to TRUE (otherwise FALSE).
00211 **
00212 ** An empty string is considered non-numeric.
00213 */
00214 int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
00215   int incr = (enc==SQLITE_UTF8?1:2);
00216   if( enc==SQLITE_UTF16BE ) z++;
00217   if( *z=='-' || *z=='+' ) z += incr;
00218   if( !isdigit(*(u8*)z) ){
00219     return 0;
00220   }
00221   z += incr;
00222   if( realnum ) *realnum = 0;
00223   while( isdigit(*(u8*)z) ){ z += incr; }
00224   if( *z=='.' ){
00225     z += incr;
00226     if( !isdigit(*(u8*)z) ) return 0;
00227     while( isdigit(*(u8*)z) ){ z += incr; }
00228     if( realnum ) *realnum = 1;
00229   }
00230   if( *z=='e' || *z=='E' ){
00231     z += incr;
00232     if( *z=='+' || *z=='-' ) z += incr;
00233     if( !isdigit(*(u8*)z) ) return 0;
00234     while( isdigit(*(u8*)z) ){ z += incr; }
00235     if( realnum ) *realnum = 1;
00236   }
00237   return *z==0;
00238 }
00239 
00240 /*
00241 ** The string z[] is an ascii representation of a real number.
00242 ** Convert this string to a double.
00243 **
00244 ** This routine assumes that z[] really is a valid number.  If it
00245 ** is not, the result is undefined.
00246 **
00247 ** This routine is used instead of the library atof() function because
00248 ** the library atof() might want to use "," as the decimal point instead
00249 ** of "." depending on how locale is set.  But that would cause problems
00250 ** for SQL.  So this routine always uses "." regardless of locale.
00251 */
00252 int sqlite3AtoF(const char *z, double *pResult){
00253 #ifndef SQLITE_OMIT_FLOATING_POINT
00254   int sign = 1;
00255   const char *zBegin = z;
00256   LONGDOUBLE_TYPE v1 = 0.0;
00257   int nSignificant = 0;
00258   while( isspace(*(u8*)z) ) z++;
00259   if( *z=='-' ){
00260     sign = -1;
00261     z++;
00262   }else if( *z=='+' ){
00263     z++;
00264   }
00265   while( z[0]=='0' ){
00266     z++;
00267   }
00268   while( isdigit(*(u8*)z) ){
00269     v1 = v1*10.0 + (*z - '0');
00270     z++;
00271     nSignificant++;
00272   }
00273   if( *z=='.' ){
00274     LONGDOUBLE_TYPE divisor = 1.0;
00275     z++;
00276     if( nSignificant==0 ){
00277       while( z[0]=='0' ){
00278         divisor *= 10.0;
00279         z++;
00280       }
00281     }
00282     while( isdigit(*(u8*)z) ){
00283       if( nSignificant<18 ){
00284         v1 = v1*10.0 + (*z - '0');
00285         divisor *= 10.0;
00286         nSignificant++;
00287       }
00288       z++;
00289     }
00290     v1 /= divisor;
00291   }
00292   if( *z=='e' || *z=='E' ){
00293     int esign = 1;
00294     int eval = 0;
00295     LONGDOUBLE_TYPE scale = 1.0;
00296     z++;
00297     if( *z=='-' ){
00298       esign = -1;
00299       z++;
00300     }else if( *z=='+' ){
00301       z++;
00302     }
00303     while( isdigit(*(u8*)z) ){
00304       eval = eval*10 + *z - '0';
00305       z++;
00306     }
00307     while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
00308     while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
00309     while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
00310     while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
00311     if( esign<0 ){
00312       v1 /= scale;
00313     }else{
00314       v1 *= scale;
00315     }
00316   }
00317   *pResult = sign<0 ? -v1 : v1;
00318   return z - zBegin;
00319 #else
00320   return sqlite3Atoi64(z, pResult);
00321 #endif /* SQLITE_OMIT_FLOATING_POINT */
00322 }
00323 
00324 /*
00325 ** Compare the 19-character string zNum against the text representation
00326 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
00327 ** if zNum is less than, equal to, or greater than the string.
00328 **
00329 ** Unlike memcmp() this routine is guaranteed to return the difference
00330 ** in the values of the last digit if the only difference is in the
00331 ** last digit.  So, for example,
00332 **
00333 **      compare2pow63("9223372036854775800")
00334 **
00335 ** will return -8.
00336 */
00337 static int compare2pow63(const char *zNum){
00338   int c;
00339   c = memcmp(zNum,"922337203685477580",18);
00340   if( c==0 ){
00341     c = zNum[18] - '8';
00342   }
00343   return c;
00344 }
00345 
00346 
00347 /*
00348 ** Return TRUE if zNum is a 64-bit signed integer and write
00349 ** the value of the integer into *pNum.  If zNum is not an integer
00350 ** or is an integer that is too large to be expressed with 64 bits,
00351 ** then return false.
00352 **
00353 ** When this routine was originally written it dealt with only
00354 ** 32-bit numbers.  At that time, it was much faster than the
00355 ** atoi() library routine in RedHat 7.2.
00356 */
00357 int sqlite3Atoi64(const char *zNum, i64 *pNum){
00358   i64 v = 0;
00359   int neg;
00360   int i, c;
00361   const char *zStart;
00362   while( isspace(*(u8*)zNum) ) zNum++;
00363   if( *zNum=='-' ){
00364     neg = 1;
00365     zNum++;
00366   }else if( *zNum=='+' ){
00367     neg = 0;
00368     zNum++;
00369   }else{
00370     neg = 0;
00371   }
00372   zStart = zNum;
00373   while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
00374   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
00375     v = v*10 + c - '0';
00376   }
00377   *pNum = neg ? -v : v;
00378   if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
00379     /* zNum is empty or contains non-numeric text or is longer
00380     ** than 19 digits (thus guaranting that it is too large) */
00381     return 0;
00382   }else if( i<19 ){
00383     /* Less than 19 digits, so we know that it fits in 64 bits */
00384     return 1;
00385   }else{
00386     /* 19-digit numbers must be no larger than 9223372036854775807 if positive
00387     ** or 9223372036854775808 if negative.  Note that 9223372036854665808
00388     ** is 2^63. */
00389     return compare2pow63(zNum)<neg;
00390   }
00391 }
00392 
00393 /*
00394 ** The string zNum represents an integer.  There might be some other
00395 ** information following the integer too, but that part is ignored.
00396 ** If the integer that the prefix of zNum represents will fit in a
00397 ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
00398 **
00399 ** This routine returns FALSE for the string -9223372036854775808 even that
00400 ** that number will, in theory fit in a 64-bit integer.  Positive
00401 ** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
00402 ** false.
00403 */
00404 int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
00405   int i, c;
00406   int neg = 0;
00407   if( *zNum=='-' ){
00408     neg = 1;
00409     zNum++;
00410   }else if( *zNum=='+' ){
00411     zNum++;
00412   }
00413   if( negFlag ) neg = 1-neg;
00414   while( *zNum=='0' ){
00415     zNum++;   /* Skip leading zeros.  Ticket #2454 */
00416   }
00417   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
00418   if( i<19 ){
00419     /* Guaranteed to fit if less than 19 digits */
00420     return 1;
00421   }else if( i>19 ){
00422     /* Guaranteed to be too big if greater than 19 digits */
00423     return 0;
00424   }else{
00425     /* Compare against 2^63. */
00426     return compare2pow63(zNum)<neg;
00427   }
00428 }
00429 
00430 /*
00431 ** If zNum represents an integer that will fit in 32-bits, then set
00432 ** *pValue to that integer and return true.  Otherwise return false.
00433 **
00434 ** Any non-numeric characters that following zNum are ignored.
00435 ** This is different from sqlite3Atoi64() which requires the
00436 ** input number to be zero-terminated.
00437 */
00438 int sqlite3GetInt32(const char *zNum, int *pValue){
00439   sqlite_int64 v = 0;
00440   int i, c;
00441   int neg = 0;
00442   if( zNum[0]=='-' ){
00443     neg = 1;
00444     zNum++;
00445   }else if( zNum[0]=='+' ){
00446     zNum++;
00447   }
00448   while( zNum[0]=='0' ) zNum++;
00449   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
00450     v = v*10 + c;
00451   }
00452 
00453   /* The longest decimal representation of a 32 bit integer is 10 digits:
00454   **
00455   **             1234567890
00456   **     2^31 -> 2147483648
00457   */
00458   if( i>10 ){
00459     return 0;
00460   }
00461   if( v-neg>2147483647 ){
00462     return 0;
00463   }
00464   if( neg ){
00465     v = -v;
00466   }
00467   *pValue = (int)v;
00468   return 1;
00469 }
00470 
00471 /*
00472 ** The variable-length integer encoding is as follows:
00473 **
00474 ** KEY:
00475 **         A = 0xxxxxxx    7 bits of data and one flag bit
00476 **         B = 1xxxxxxx    7 bits of data and one flag bit
00477 **         C = xxxxxxxx    8 bits of data
00478 **
00479 **  7 bits - A
00480 ** 14 bits - BA
00481 ** 21 bits - BBA
00482 ** 28 bits - BBBA
00483 ** 35 bits - BBBBA
00484 ** 42 bits - BBBBBA
00485 ** 49 bits - BBBBBBA
00486 ** 56 bits - BBBBBBBA
00487 ** 64 bits - BBBBBBBBC
00488 */
00489 
00490 /*
00491 ** Write a 64-bit variable-length integer to memory starting at p[0].
00492 ** The length of data write will be between 1 and 9 bytes.  The number
00493 ** of bytes written is returned.
00494 **
00495 ** A variable-length integer consists of the lower 7 bits of each byte
00496 ** for all bytes that have the 8th bit set and one byte with the 8th
00497 ** bit clear.  Except, if we get to the 9th byte, it stores the full
00498 ** 8 bits and is the last byte.
00499 */
00500 int sqlite3PutVarint(unsigned char *p, u64 v){
00501   int i, j, n;
00502   u8 buf[10];
00503   if( v & (((u64)0xff000000)<<32) ){
00504     p[8] = v;
00505     v >>= 8;
00506     for(i=7; i>=0; i--){
00507       p[i] = (v & 0x7f) | 0x80;
00508       v >>= 7;
00509     }
00510     return 9;
00511   }    
00512   n = 0;
00513   do{
00514     buf[n++] = (v & 0x7f) | 0x80;
00515     v >>= 7;
00516   }while( v!=0 );
00517   buf[0] &= 0x7f;
00518   assert( n<=9 );
00519   for(i=0, j=n-1; j>=0; j--, i++){
00520     p[i] = buf[j];
00521   }
00522   return n;
00523 }
00524 
00525 /*
00526 ** This routine is a faster version of sqlite3PutVarint() that only
00527 ** works for 32-bit positive integers and which is optimized for
00528 ** the common case of small integers.  A MACRO version, putVarint32,
00529 ** is provided which inlines the single-byte case.  All code should use
00530 ** the MACRO version as this function assumes the single-byte case has
00531 ** already been handled.
00532 */
00533 int sqlite3PutVarint32(unsigned char *p, u32 v){
00534 #ifndef putVarint32
00535   if( (v & ~0x7f)==0 ){
00536     p[0] = v;
00537     return 1;
00538   }
00539 #endif
00540   if( (v & ~0x3fff)==0 ){
00541     p[0] = (v>>7) | 0x80;
00542     p[1] = v & 0x7f;
00543     return 2;
00544   }
00545   return sqlite3PutVarint(p, v);
00546 }
00547 
00548 /*
00549 ** Read a 64-bit variable-length integer from memory starting at p[0].
00550 ** Return the number of bytes read.  The value is stored in *v.
00551 */
00552 int sqlite3GetVarint(const unsigned char *p, u64 *v){
00553   u32 a,b,s;
00554 
00555   a = *p;
00556   /* a: p0 (unmasked) */
00557   if (!(a&0x80))
00558   {
00559     *v = a;
00560     return 1;
00561   }
00562 
00563   p++;
00564   b = *p;
00565   /* b: p1 (unmasked) */
00566   if (!(b&0x80))
00567   {
00568     a &= 0x7f;
00569     a = a<<7;
00570     a |= b;
00571     *v = a;
00572     return 2;
00573   }
00574 
00575   p++;
00576   a = a<<14;
00577   a |= *p;
00578   /* a: p0<<14 | p2 (unmasked) */
00579   if (!(a&0x80))
00580   {
00581     a &= (0x7f<<14)|(0x7f);
00582     b &= 0x7f;
00583     b = b<<7;
00584     a |= b;
00585     *v = a;
00586     return 3;
00587   }
00588 
00589   /* CSE1 from below */
00590   a &= (0x7f<<14)|(0x7f);
00591   p++;
00592   b = b<<14;
00593   b |= *p;
00594   /* b: p1<<14 | p3 (unmasked) */
00595   if (!(b&0x80))
00596   {
00597     b &= (0x7f<<14)|(0x7f);
00598     /* moved CSE1 up */
00599     /* a &= (0x7f<<14)|(0x7f); */
00600     a = a<<7;
00601     a |= b;
00602     *v = a;
00603     return 4;
00604   }
00605 
00606   /* a: p0<<14 | p2 (masked) */
00607   /* b: p1<<14 | p3 (unmasked) */
00608   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
00609   /* moved CSE1 up */
00610   /* a &= (0x7f<<14)|(0x7f); */
00611   b &= (0x7f<<14)|(0x7f);
00612   s = a;
00613   /* s: p0<<14 | p2 (masked) */
00614 
00615   p++;
00616   a = a<<14;
00617   a |= *p;
00618   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
00619   if (!(a&0x80))
00620   {
00621     /* we can skip these cause they were (effectively) done above in calc'ing s */
00622     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
00623     /* b &= (0x7f<<14)|(0x7f); */
00624     b = b<<7;
00625     a |= b;
00626     s = s>>18;
00627     *v = ((u64)s)<<32 | a;
00628     return 5;
00629   }
00630 
00631   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
00632   s = s<<7;
00633   s |= b;
00634   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
00635 
00636   p++;
00637   b = b<<14;
00638   b |= *p;
00639   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
00640   if (!(b&0x80))
00641   {
00642     /* we can skip this cause it was (effectively) done above in calc'ing s */
00643     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
00644     a &= (0x7f<<14)|(0x7f);
00645     a = a<<7;
00646     a |= b;
00647     s = s>>18;
00648     *v = ((u64)s)<<32 | a;
00649     return 6;
00650   }
00651 
00652   p++;
00653   a = a<<14;
00654   a |= *p;
00655   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
00656   if (!(a&0x80))
00657   {
00658     a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
00659     b &= (0x7f<<14)|(0x7f);
00660     b = b<<7;
00661     a |= b;
00662     s = s>>11;
00663     *v = ((u64)s)<<32 | a;
00664     return 7;
00665   }
00666 
00667   /* CSE2 from below */
00668   a &= (0x7f<<14)|(0x7f);
00669   p++;
00670   b = b<<14;
00671   b |= *p;
00672   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
00673   if (!(b&0x80))
00674   {
00675     b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
00676     /* moved CSE2 up */
00677     /* a &= (0x7f<<14)|(0x7f); */
00678     a = a<<7;
00679     a |= b;
00680     s = s>>4;
00681     *v = ((u64)s)<<32 | a;
00682     return 8;
00683   }
00684 
00685   p++;
00686   a = a<<15;
00687   a |= *p;
00688   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
00689 
00690   /* moved CSE2 up */
00691   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
00692   b &= (0x7f<<14)|(0x7f);
00693   b = b<<8;
00694   a |= b;
00695 
00696   s = s<<4;
00697   b = p[-4];
00698   b &= 0x7f;
00699   b = b>>3;
00700   s |= b;
00701 
00702   *v = ((u64)s)<<32 | a;
00703 
00704   return 9;
00705 }
00706 
00707 /*
00708 ** Read a 32-bit variable-length integer from memory starting at p[0].
00709 ** Return the number of bytes read.  The value is stored in *v.
00710 ** A MACRO version, getVarint32, is provided which inlines the 
00711 ** single-byte case.  All code should use the MACRO version as 
00712 ** this function assumes the single-byte case has already been handled.
00713 */
00714 int sqlite3GetVarint32(const unsigned char *p, u32 *v){
00715   u32 a,b;
00716 
00717   a = *p;
00718   /* a: p0 (unmasked) */
00719 #ifndef getVarint32
00720   if (!(a&0x80))
00721   {
00722     *v = a;
00723     return 1;
00724   }
00725 #endif
00726 
00727   p++;
00728   b = *p;
00729   /* b: p1 (unmasked) */
00730   if (!(b&0x80))
00731   {
00732     a &= 0x7f;
00733     a = a<<7;
00734     *v = a | b;
00735     return 2;
00736   }
00737 
00738   p++;
00739   a = a<<14;
00740   a |= *p;
00741   /* a: p0<<14 | p2 (unmasked) */
00742   if (!(a&0x80))
00743   {
00744     a &= (0x7f<<14)|(0x7f);
00745     b &= 0x7f;
00746     b = b<<7;
00747     *v = a | b;
00748     return 3;
00749   }
00750 
00751   p++;
00752   b = b<<14;
00753   b |= *p;
00754   /* b: p1<<14 | p3 (unmasked) */
00755   if (!(b&0x80))
00756   {
00757     b &= (0x7f<<14)|(0x7f);
00758     a &= (0x7f<<14)|(0x7f);
00759     a = a<<7;
00760     *v = a | b;
00761     return 4;
00762   }
00763 
00764   p++;
00765   a = a<<14;
00766   a |= *p;
00767   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
00768   if (!(a&0x80))
00769   {
00770     a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
00771     b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
00772     b = b<<7;
00773     *v = a | b;
00774     return 5;
00775   }
00776 
00777   /* We can only reach this point when reading a corrupt database
00778   ** file.  In that case we are not in any hurry.  Use the (relatively
00779   ** slow) general-purpose sqlite3GetVarint() routine to extract the
00780   ** value. */
00781   {
00782     u64 v64;
00783     int n;
00784 
00785     p -= 4;
00786     n = sqlite3GetVarint(p, &v64);
00787     assert( n>5 && n<=9 );
00788     *v = (u32)v64;
00789     return n;
00790   }
00791 }
00792 
00793 /*
00794 ** Return the number of bytes that will be needed to store the given
00795 ** 64-bit integer.
00796 */
00797 int sqlite3VarintLen(u64 v){
00798   int i = 0;
00799   do{
00800     i++;
00801     v >>= 7;
00802   }while( v!=0 && i<9 );
00803   return i;
00804 }
00805 
00806 
00807 /*
00808 ** Read or write a four-byte big-endian integer value.
00809 */
00810 u32 sqlite3Get4byte(const u8 *p){
00811   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
00812 }
00813 void sqlite3Put4byte(unsigned char *p, u32 v){
00814   p[0] = v>>24;
00815   p[1] = v>>16;
00816   p[2] = v>>8;
00817   p[3] = v;
00818 }
00819 
00820 
00821 
00822 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
00823 /*
00824 ** Translate a single byte of Hex into an integer.
00825 ** This routinen only works if h really is a valid hexadecimal
00826 ** character:  0..9a..fA..F
00827 */
00828 static int hexToInt(int h){
00829   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
00830 #ifdef SQLITE_ASCII
00831   h += 9*(1&(h>>6));
00832 #endif
00833 #ifdef SQLITE_EBCDIC
00834   h += 9*(1&~(h>>4));
00835 #endif
00836   return h & 0xf;
00837 }
00838 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
00839 
00840 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
00841 /*
00842 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
00843 ** value.  Return a pointer to its binary value.  Space to hold the
00844 ** binary value has been obtained from malloc and must be freed by
00845 ** the calling routine.
00846 */
00847 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
00848   char *zBlob;
00849   int i;
00850 
00851   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
00852   n--;
00853   if( zBlob ){
00854     for(i=0; i<n; i+=2){
00855       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
00856     }
00857     zBlob[i/2] = 0;
00858   }
00859   return zBlob;
00860 }
00861 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
00862 
00863 
00864 /*
00865 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
00866 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
00867 ** when this routine is called.
00868 **
00869 ** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
00870 ** value indicates that the database connection passed into the API is
00871 ** open and is not being used by another thread.  By changing the value
00872 ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
00873 ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
00874 ** when the API exits. 
00875 **
00876 ** This routine is a attempt to detect if two threads use the
00877 ** same sqlite* pointer at the same time.  There is a race 
00878 ** condition so it is possible that the error is not detected.
00879 ** But usually the problem will be seen.  The result will be an
00880 ** error which can be used to debug the application that is
00881 ** using SQLite incorrectly.
00882 **
00883 ** Ticket #202:  If db->magic is not a valid open value, take care not
00884 ** to modify the db structure at all.  It could be that db is a stale
00885 ** pointer.  In other words, it could be that there has been a prior
00886 ** call to sqlite3_close(db) and db has been deallocated.  And we do
00887 ** not want to write into deallocated memory.
00888 */
00889 #ifdef SQLITE_DEBUG
00890 int sqlite3SafetyOn(sqlite3 *db){
00891   if( db->magic==SQLITE_MAGIC_OPEN ){
00892     db->magic = SQLITE_MAGIC_BUSY;
00893     assert( sqlite3_mutex_held(db->mutex) );
00894     return 0;
00895   }else if( db->magic==SQLITE_MAGIC_BUSY ){
00896     db->magic = SQLITE_MAGIC_ERROR;
00897     db->u1.isInterrupted = 1;
00898   }
00899   return 1;
00900 }
00901 #endif
00902 
00903 /*
00904 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
00905 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
00906 ** when this routine is called.
00907 */
00908 #ifdef SQLITE_DEBUG
00909 int sqlite3SafetyOff(sqlite3 *db){
00910   if( db->magic==SQLITE_MAGIC_BUSY ){
00911     db->magic = SQLITE_MAGIC_OPEN;
00912     assert( sqlite3_mutex_held(db->mutex) );
00913     return 0;
00914   }else{
00915     db->magic = SQLITE_MAGIC_ERROR;
00916     db->u1.isInterrupted = 1;
00917     return 1;
00918   }
00919 }
00920 #endif
00921 
00922 /*
00923 ** Check to make sure we have a valid db pointer.  This test is not
00924 ** foolproof but it does provide some measure of protection against
00925 ** misuse of the interface such as passing in db pointers that are
00926 ** NULL or which have been previously closed.  If this routine returns
00927 ** 1 it means that the db pointer is valid and 0 if it should not be
00928 ** dereferenced for any reason.  The calling function should invoke
00929 ** SQLITE_MISUSE immediately.
00930 **
00931 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
00932 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
00933 ** open properly and is not fit for general use but which can be
00934 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
00935 */
00936 int sqlite3SafetyCheckOk(sqlite3 *db){
00937   int magic;
00938   if( db==0 ) return 0;
00939   magic = db->magic;
00940   if( magic!=SQLITE_MAGIC_OPEN &&
00941       magic!=SQLITE_MAGIC_BUSY ) return 0;
00942   return 1;
00943 }
00944 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
00945   int magic;
00946   if( db==0 ) return 0;
00947   magic = db->magic;
00948   if( magic!=SQLITE_MAGIC_SICK &&
00949       magic!=SQLITE_MAGIC_OPEN &&
00950       magic!=SQLITE_MAGIC_BUSY ) return 0;
00951   return 1;
00952 }

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1