extensions/sqlite/sqlite-source/util.c
changeset 1203 633c25ce6405
child 1645 27307df4d711
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/util.c	Sun Jul 29 01:15:31 2007 +0000
     1.3 @@ -0,0 +1,732 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** Utility functions used throughout sqlite.
    1.16 +**
    1.17 +** This file contains functions for allocating memory, comparing
    1.18 +** strings, and stuff like that.
    1.19 +**
    1.20 +** $Id$
    1.21 +*/
    1.22 +#include "sqliteInt.h"
    1.23 +#include "os.h"
    1.24 +#include <stdarg.h>
    1.25 +#include <ctype.h>
    1.26 +
    1.27 +
    1.28 +/*
    1.29 +** Set the most recent error code and error string for the sqlite
    1.30 +** handle "db". The error code is set to "err_code".
    1.31 +**
    1.32 +** If it is not NULL, string zFormat specifies the format of the
    1.33 +** error string in the style of the printf functions: The following
    1.34 +** format characters are allowed:
    1.35 +**
    1.36 +**      %s      Insert a string
    1.37 +**      %z      A string that should be freed after use
    1.38 +**      %d      Insert an integer
    1.39 +**      %T      Insert a token
    1.40 +**      %S      Insert the first element of a SrcList
    1.41 +**
    1.42 +** zFormat and any string tokens that follow it are assumed to be
    1.43 +** encoded in UTF-8.
    1.44 +**
    1.45 +** To clear the most recent error for sqlite handle "db", sqlite3Error
    1.46 +** should be called with err_code set to SQLITE_OK and zFormat set
    1.47 +** to NULL.
    1.48 +*/
    1.49 +void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
    1.50 +  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
    1.51 +    db->errCode = err_code;
    1.52 +    if( zFormat ){
    1.53 +      char *z;
    1.54 +      va_list ap;
    1.55 +      va_start(ap, zFormat);
    1.56 +      z = sqlite3VMPrintf(zFormat, ap);
    1.57 +      va_end(ap);
    1.58 +      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
    1.59 +    }else{
    1.60 +      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
    1.61 +    }
    1.62 +  }
    1.63 +}
    1.64 +
    1.65 +/*
    1.66 +** Add an error message to pParse->zErrMsg and increment pParse->nErr.
    1.67 +** The following formatting characters are allowed:
    1.68 +**
    1.69 +**      %s      Insert a string
    1.70 +**      %z      A string that should be freed after use
    1.71 +**      %d      Insert an integer
    1.72 +**      %T      Insert a token
    1.73 +**      %S      Insert the first element of a SrcList
    1.74 +**
    1.75 +** This function should be used to report any error that occurs whilst
    1.76 +** compiling an SQL statement (i.e. within sqlite3_prepare()). The
    1.77 +** last thing the sqlite3_prepare() function does is copy the error
    1.78 +** stored by this function into the database handle using sqlite3Error().
    1.79 +** Function sqlite3Error() should be used during statement execution
    1.80 +** (sqlite3_step() etc.).
    1.81 +*/
    1.82 +void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
    1.83 +  va_list ap;
    1.84 +  pParse->nErr++;
    1.85 +  sqliteFree(pParse->zErrMsg);
    1.86 +  va_start(ap, zFormat);
    1.87 +  pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
    1.88 +  va_end(ap);
    1.89 +  if( pParse->rc==SQLITE_OK ){
    1.90 +    pParse->rc = SQLITE_ERROR;
    1.91 +  }
    1.92 +}
    1.93 +
    1.94 +/*
    1.95 +** Clear the error message in pParse, if any
    1.96 +*/
    1.97 +void sqlite3ErrorClear(Parse *pParse){
    1.98 +  sqliteFree(pParse->zErrMsg);
    1.99 +  pParse->zErrMsg = 0;
   1.100 +  pParse->nErr = 0;
   1.101 +}
   1.102 +
   1.103 +/*
   1.104 +** Convert an SQL-style quoted string into a normal string by removing
   1.105 +** the quote characters.  The conversion is done in-place.  If the
   1.106 +** input does not begin with a quote character, then this routine
   1.107 +** is a no-op.
   1.108 +**
   1.109 +** 2002-Feb-14: This routine is extended to remove MS-Access style
   1.110 +** brackets from around identifers.  For example:  "[a-b-c]" becomes
   1.111 +** "a-b-c".
   1.112 +*/
   1.113 +void sqlite3Dequote(char *z){
   1.114 +  int quote;
   1.115 +  int i, j;
   1.116 +  if( z==0 ) return;
   1.117 +  quote = z[0];
   1.118 +  switch( quote ){
   1.119 +    case '\'':  break;
   1.120 +    case '"':   break;
   1.121 +    case '`':   break;                /* For MySQL compatibility */
   1.122 +    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
   1.123 +    default:    return;
   1.124 +  }
   1.125 +  for(i=1, j=0; z[i]; i++){
   1.126 +    if( z[i]==quote ){
   1.127 +      if( z[i+1]==quote ){
   1.128 +        z[j++] = quote;
   1.129 +        i++;
   1.130 +      }else{
   1.131 +        z[j++] = 0;
   1.132 +        break;
   1.133 +      }
   1.134 +    }else{
   1.135 +      z[j++] = z[i];
   1.136 +    }
   1.137 +  }
   1.138 +}
   1.139 +
   1.140 +/* An array to map all upper-case characters into their corresponding
   1.141 +** lower-case character. 
   1.142 +*/
   1.143 +const unsigned char sqlite3UpperToLower[] = {
   1.144 +#ifdef SQLITE_ASCII
   1.145 +      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
   1.146 +     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
   1.147 +     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
   1.148 +     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
   1.149 +    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
   1.150 +    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
   1.151 +    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
   1.152 +    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
   1.153 +    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
   1.154 +    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
   1.155 +    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
   1.156 +    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
   1.157 +    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
   1.158 +    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
   1.159 +    252,253,254,255
   1.160 +#endif
   1.161 +#ifdef SQLITE_EBCDIC
   1.162 +      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
   1.163 +     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
   1.164 +     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
   1.165 +     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
   1.166 +     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
   1.167 +     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
   1.168 +     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
   1.169 +    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
   1.170 +    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
   1.171 +    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
   1.172 +    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
   1.173 +    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
   1.174 +    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
   1.175 +    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
   1.176 +    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
   1.177 +    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
   1.178 +#endif
   1.179 +};
   1.180 +#define UpperToLower sqlite3UpperToLower
   1.181 +
   1.182 +/*
   1.183 +** Some systems have stricmp().  Others have strcasecmp().  Because
   1.184 +** there is no consistency, we will define our own.
   1.185 +*/
   1.186 +int sqlite3StrICmp(const char *zLeft, const char *zRight){
   1.187 +  register unsigned char *a, *b;
   1.188 +  a = (unsigned char *)zLeft;
   1.189 +  b = (unsigned char *)zRight;
   1.190 +  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   1.191 +  return UpperToLower[*a] - UpperToLower[*b];
   1.192 +}
   1.193 +int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
   1.194 +  register unsigned char *a, *b;
   1.195 +  a = (unsigned char *)zLeft;
   1.196 +  b = (unsigned char *)zRight;
   1.197 +  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   1.198 +  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
   1.199 +}
   1.200 +
   1.201 +/*
   1.202 +** Return TRUE if z is a pure numeric string.  Return FALSE if the
   1.203 +** string contains any character which is not part of a number. If
   1.204 +** the string is numeric and contains the '.' character, set *realnum
   1.205 +** to TRUE (otherwise FALSE).
   1.206 +**
   1.207 +** An empty string is considered non-numeric.
   1.208 +*/
   1.209 +int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
   1.210 +  int incr = (enc==SQLITE_UTF8?1:2);
   1.211 +  if( enc==SQLITE_UTF16BE ) z++;
   1.212 +  if( *z=='-' || *z=='+' ) z += incr;
   1.213 +  if( !isdigit(*(u8*)z) ){
   1.214 +    return 0;
   1.215 +  }
   1.216 +  z += incr;
   1.217 +  if( realnum ) *realnum = 0;
   1.218 +  while( isdigit(*(u8*)z) ){ z += incr; }
   1.219 +  if( *z=='.' ){
   1.220 +    z += incr;
   1.221 +    if( !isdigit(*(u8*)z) ) return 0;
   1.222 +    while( isdigit(*(u8*)z) ){ z += incr; }
   1.223 +    if( realnum ) *realnum = 1;
   1.224 +  }
   1.225 +  if( *z=='e' || *z=='E' ){
   1.226 +    z += incr;
   1.227 +    if( *z=='+' || *z=='-' ) z += incr;
   1.228 +    if( !isdigit(*(u8*)z) ) return 0;
   1.229 +    while( isdigit(*(u8*)z) ){ z += incr; }
   1.230 +    if( realnum ) *realnum = 1;
   1.231 +  }
   1.232 +  return *z==0;
   1.233 +}
   1.234 +
   1.235 +/*
   1.236 +** The string z[] is an ascii representation of a real number.
   1.237 +** Convert this string to a double.
   1.238 +**
   1.239 +** This routine assumes that z[] really is a valid number.  If it
   1.240 +** is not, the result is undefined.
   1.241 +**
   1.242 +** This routine is used instead of the library atof() function because
   1.243 +** the library atof() might want to use "," as the decimal point instead
   1.244 +** of "." depending on how locale is set.  But that would cause problems
   1.245 +** for SQL.  So this routine always uses "." regardless of locale.
   1.246 +*/
   1.247 +int sqlite3AtoF(const char *z, double *pResult){
   1.248 +#ifndef SQLITE_OMIT_FLOATING_POINT
   1.249 +  int sign = 1;
   1.250 +  const char *zBegin = z;
   1.251 +  LONGDOUBLE_TYPE v1 = 0.0;
   1.252 +  while( isspace(*(u8*)z) ) z++;
   1.253 +  if( *z=='-' ){
   1.254 +    sign = -1;
   1.255 +    z++;
   1.256 +  }else if( *z=='+' ){
   1.257 +    z++;
   1.258 +  }
   1.259 +  while( isdigit(*(u8*)z) ){
   1.260 +    v1 = v1*10.0 + (*z - '0');
   1.261 +    z++;
   1.262 +  }
   1.263 +  if( *z=='.' ){
   1.264 +    LONGDOUBLE_TYPE divisor = 1.0;
   1.265 +    z++;
   1.266 +    while( isdigit(*(u8*)z) ){
   1.267 +      v1 = v1*10.0 + (*z - '0');
   1.268 +      divisor *= 10.0;
   1.269 +      z++;
   1.270 +    }
   1.271 +    v1 /= divisor;
   1.272 +  }
   1.273 +  if( *z=='e' || *z=='E' ){
   1.274 +    int esign = 1;
   1.275 +    int eval = 0;
   1.276 +    LONGDOUBLE_TYPE scale = 1.0;
   1.277 +    z++;
   1.278 +    if( *z=='-' ){
   1.279 +      esign = -1;
   1.280 +      z++;
   1.281 +    }else if( *z=='+' ){
   1.282 +      z++;
   1.283 +    }
   1.284 +    while( isdigit(*(u8*)z) ){
   1.285 +      eval = eval*10 + *z - '0';
   1.286 +      z++;
   1.287 +    }
   1.288 +    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
   1.289 +    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
   1.290 +    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
   1.291 +    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
   1.292 +    if( esign<0 ){
   1.293 +      v1 /= scale;
   1.294 +    }else{
   1.295 +      v1 *= scale;
   1.296 +    }
   1.297 +  }
   1.298 +  *pResult = sign<0 ? -v1 : v1;
   1.299 +  return z - zBegin;
   1.300 +#else
   1.301 +  return sqlite3Atoi64(z, pResult);
   1.302 +#endif /* SQLITE_OMIT_FLOATING_POINT */
   1.303 +}
   1.304 +
   1.305 +/*
   1.306 +** Compare the 19-character string zNum against the text representation
   1.307 +** value 2^63:  9223372036854775808.  Return negative, zero, or positive
   1.308 +** if zNum is less than, equal to, or greater than the string.
   1.309 +**
   1.310 +** Unlike memcmp() this routine is guaranteed to return the difference
   1.311 +** in the values of the last digit if the only difference is in the
   1.312 +** last digit.  So, for example,
   1.313 +**
   1.314 +**      compare2pow63("9223372036854775800")
   1.315 +**
   1.316 +** will return -8.
   1.317 +*/
   1.318 +static int compare2pow63(const char *zNum){
   1.319 +  int c;
   1.320 +  c = memcmp(zNum,"922337203685477580",18);
   1.321 +  if( c==0 ){
   1.322 +    c = zNum[18] - '8';
   1.323 +  }
   1.324 +  return c;
   1.325 +}
   1.326 +
   1.327 +
   1.328 +/*
   1.329 +** Return TRUE if zNum is a 64-bit signed integer and write
   1.330 +** the value of the integer into *pNum.  If zNum is not an integer
   1.331 +** or is an integer that is too large to be expressed with 64 bits,
   1.332 +** then return false.
   1.333 +**
   1.334 +** When this routine was originally written it dealt with only
   1.335 +** 32-bit numbers.  At that time, it was much faster than the
   1.336 +** atoi() library routine in RedHat 7.2.
   1.337 +*/
   1.338 +int sqlite3Atoi64(const char *zNum, i64 *pNum){
   1.339 +  i64 v = 0;
   1.340 +  int neg;
   1.341 +  int i, c;
   1.342 +  while( isspace(*(u8*)zNum) ) zNum++;
   1.343 +  if( *zNum=='-' ){
   1.344 +    neg = 1;
   1.345 +    zNum++;
   1.346 +  }else if( *zNum=='+' ){
   1.347 +    neg = 0;
   1.348 +    zNum++;
   1.349 +  }else{
   1.350 +    neg = 0;
   1.351 +  }
   1.352 +  while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
   1.353 +  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
   1.354 +    v = v*10 + c - '0';
   1.355 +  }
   1.356 +  *pNum = neg ? -v : v;
   1.357 +  if( c!=0 || i==0 || i>19 ){
   1.358 +    /* zNum is empty or contains non-numeric text or is longer
   1.359 +    ** than 19 digits (thus guaranting that it is too large) */
   1.360 +    return 0;
   1.361 +  }else if( i<19 ){
   1.362 +    /* Less than 19 digits, so we know that it fits in 64 bits */
   1.363 +    return 1;
   1.364 +  }else{
   1.365 +    /* 19-digit numbers must be no larger than 9223372036854775807 if positive
   1.366 +    ** or 9223372036854775808 if negative.  Note that 9223372036854665808
   1.367 +    ** is 2^63. */
   1.368 +    return compare2pow63(zNum)<neg;
   1.369 +  }
   1.370 +}
   1.371 +
   1.372 +/*
   1.373 +** The string zNum represents an integer.  There might be some other
   1.374 +** information following the integer too, but that part is ignored.
   1.375 +** If the integer that the prefix of zNum represents will fit in a
   1.376 +** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
   1.377 +**
   1.378 +** This routine returns FALSE for the string -9223372036854775808 even that
   1.379 +** that number will, in theory fit in a 64-bit integer.  Positive
   1.380 +** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
   1.381 +** false.
   1.382 +*/
   1.383 +int sqlite3FitsIn64Bits(const char *zNum){
   1.384 +  int i, c;
   1.385 +  int neg = 0;
   1.386 +  if( *zNum=='-' ){
   1.387 +    neg = 1;
   1.388 +    zNum++;
   1.389 +  }else if( *zNum=='+' ){
   1.390 +    zNum++;
   1.391 +  }
   1.392 +  while( *zNum=='0' ){
   1.393 +    zNum++;   /* Skip leading zeros.  Ticket #2454 */
   1.394 +  }
   1.395 +  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
   1.396 +  if( i<19 ){
   1.397 +    /* Guaranteed to fit if less than 19 digits */
   1.398 +    return 1;
   1.399 +  }else if( i>19 ){
   1.400 +    /* Guaranteed to be too big if greater than 19 digits */
   1.401 +    return 0;
   1.402 +  }else{
   1.403 +    /* Compare against 2^63. */
   1.404 +    return compare2pow63(zNum)<neg;
   1.405 +  }
   1.406 +}
   1.407 +
   1.408 +/*
   1.409 +** If zNum represents an integer that will fit in 32-bits, then set
   1.410 +** *pValue to that integer and return true.  Otherwise return false.
   1.411 +**
   1.412 +** Any non-numeric characters that following zNum are ignored.
   1.413 +** This is different from sqlite3Atoi64() which requires the
   1.414 +** input number to be zero-terminated.
   1.415 +*/
   1.416 +int sqlite3GetInt32(const char *zNum, int *pValue){
   1.417 +  sqlite_int64 v = 0;
   1.418 +  int i, c;
   1.419 +  int neg = 0;
   1.420 +  if( zNum[0]=='-' ){
   1.421 +    neg = 1;
   1.422 +    zNum++;
   1.423 +  }else if( zNum[0]=='+' ){
   1.424 +    zNum++;
   1.425 +  }
   1.426 +  while( zNum[0]=='0' ) zNum++;
   1.427 +  for(i=0; i<10 && (c = zNum[i] - '0')>=0 && c<=9; i++){
   1.428 +    v = v*10 + c;
   1.429 +  }
   1.430 +  if( i>9 ){
   1.431 +    return 0;
   1.432 +  }
   1.433 +  if( v-neg>2147483647 ){
   1.434 +    return 0;
   1.435 +  }
   1.436 +  if( neg ){
   1.437 +    v = -v;
   1.438 +  }
   1.439 +  *pValue = (int)v;
   1.440 +  return 1;
   1.441 +}
   1.442 +
   1.443 +/*
   1.444 +** Check to make sure we have a valid db pointer.  This test is not
   1.445 +** foolproof but it does provide some measure of protection against
   1.446 +** misuse of the interface such as passing in db pointers that are
   1.447 +** NULL or which have been previously closed.  If this routine returns
   1.448 +** TRUE it means that the db pointer is invalid and should not be
   1.449 +** dereferenced for any reason.  The calling function should invoke
   1.450 +** SQLITE_MISUSE immediately.
   1.451 +*/
   1.452 +int sqlite3SafetyCheck(sqlite3 *db){
   1.453 +  int magic;
   1.454 +  if( db==0 ) return 1;
   1.455 +  magic = db->magic;
   1.456 +  if( magic!=SQLITE_MAGIC_CLOSED &&
   1.457 +         magic!=SQLITE_MAGIC_OPEN &&
   1.458 +         magic!=SQLITE_MAGIC_BUSY ) return 1;
   1.459 +  return 0;
   1.460 +}
   1.461 +
   1.462 +/*
   1.463 +** The variable-length integer encoding is as follows:
   1.464 +**
   1.465 +** KEY:
   1.466 +**         A = 0xxxxxxx    7 bits of data and one flag bit
   1.467 +**         B = 1xxxxxxx    7 bits of data and one flag bit
   1.468 +**         C = xxxxxxxx    8 bits of data
   1.469 +**
   1.470 +**  7 bits - A
   1.471 +** 14 bits - BA
   1.472 +** 21 bits - BBA
   1.473 +** 28 bits - BBBA
   1.474 +** 35 bits - BBBBA
   1.475 +** 42 bits - BBBBBA
   1.476 +** 49 bits - BBBBBBA
   1.477 +** 56 bits - BBBBBBBA
   1.478 +** 64 bits - BBBBBBBBC
   1.479 +*/
   1.480 +
   1.481 +/*
   1.482 +** Write a 64-bit variable-length integer to memory starting at p[0].
   1.483 +** The length of data write will be between 1 and 9 bytes.  The number
   1.484 +** of bytes written is returned.
   1.485 +**
   1.486 +** A variable-length integer consists of the lower 7 bits of each byte
   1.487 +** for all bytes that have the 8th bit set and one byte with the 8th
   1.488 +** bit clear.  Except, if we get to the 9th byte, it stores the full
   1.489 +** 8 bits and is the last byte.
   1.490 +*/
   1.491 +int sqlite3PutVarint(unsigned char *p, u64 v){
   1.492 +  int i, j, n;
   1.493 +  u8 buf[10];
   1.494 +  if( v & (((u64)0xff000000)<<32) ){
   1.495 +    p[8] = v;
   1.496 +    v >>= 8;
   1.497 +    for(i=7; i>=0; i--){
   1.498 +      p[i] = (v & 0x7f) | 0x80;
   1.499 +      v >>= 7;
   1.500 +    }
   1.501 +    return 9;
   1.502 +  }    
   1.503 +  n = 0;
   1.504 +  do{
   1.505 +    buf[n++] = (v & 0x7f) | 0x80;
   1.506 +    v >>= 7;
   1.507 +  }while( v!=0 );
   1.508 +  buf[0] &= 0x7f;
   1.509 +  assert( n<=9 );
   1.510 +  for(i=0, j=n-1; j>=0; j--, i++){
   1.511 +    p[i] = buf[j];
   1.512 +  }
   1.513 +  return n;
   1.514 +}
   1.515 +
   1.516 +/*
   1.517 +** Read a 64-bit variable-length integer from memory starting at p[0].
   1.518 +** Return the number of bytes read.  The value is stored in *v.
   1.519 +*/
   1.520 +int sqlite3GetVarint(const unsigned char *p, u64 *v){
   1.521 +  u32 x;
   1.522 +  u64 x64;
   1.523 +  int n;
   1.524 +  unsigned char c;
   1.525 +  if( ((c = p[0]) & 0x80)==0 ){
   1.526 +    *v = c;
   1.527 +    return 1;
   1.528 +  }
   1.529 +  x = c & 0x7f;
   1.530 +  if( ((c = p[1]) & 0x80)==0 ){
   1.531 +    *v = (x<<7) | c;
   1.532 +    return 2;
   1.533 +  }
   1.534 +  x = (x<<7) | (c&0x7f);
   1.535 +  if( ((c = p[2]) & 0x80)==0 ){
   1.536 +    *v = (x<<7) | c;
   1.537 +    return 3;
   1.538 +  }
   1.539 +  x = (x<<7) | (c&0x7f);
   1.540 +  if( ((c = p[3]) & 0x80)==0 ){
   1.541 +    *v = (x<<7) | c;
   1.542 +    return 4;
   1.543 +  }
   1.544 +  x64 = (x<<7) | (c&0x7f);
   1.545 +  n = 4;
   1.546 +  do{
   1.547 +    c = p[n++];
   1.548 +    if( n==9 ){
   1.549 +      x64 = (x64<<8) | c;
   1.550 +      break;
   1.551 +    }
   1.552 +    x64 = (x64<<7) | (c&0x7f);
   1.553 +  }while( (c & 0x80)!=0 );
   1.554 +  *v = x64;
   1.555 +  return n;
   1.556 +}
   1.557 +
   1.558 +/*
   1.559 +** Read a 32-bit variable-length integer from memory starting at p[0].
   1.560 +** Return the number of bytes read.  The value is stored in *v.
   1.561 +*/
   1.562 +int sqlite3GetVarint32(const unsigned char *p, u32 *v){
   1.563 +  u32 x;
   1.564 +  int n;
   1.565 +  unsigned char c;
   1.566 +  if( ((signed char*)p)[0]>=0 ){
   1.567 +    *v = p[0];
   1.568 +    return 1;
   1.569 +  }
   1.570 +  x = p[0] & 0x7f;
   1.571 +  if( ((signed char*)p)[1]>=0 ){
   1.572 +    *v = (x<<7) | p[1];
   1.573 +    return 2;
   1.574 +  }
   1.575 +  x = (x<<7) | (p[1] & 0x7f);
   1.576 +  n = 2;
   1.577 +  do{
   1.578 +    x = (x<<7) | ((c = p[n++])&0x7f);
   1.579 +  }while( (c & 0x80)!=0 && n<9 );
   1.580 +  *v = x;
   1.581 +  return n;
   1.582 +}
   1.583 +
   1.584 +/*
   1.585 +** Return the number of bytes that will be needed to store the given
   1.586 +** 64-bit integer.
   1.587 +*/
   1.588 +int sqlite3VarintLen(u64 v){
   1.589 +  int i = 0;
   1.590 +  do{
   1.591 +    i++;
   1.592 +    v >>= 7;
   1.593 +  }while( v!=0 && i<9 );
   1.594 +  return i;
   1.595 +}
   1.596 +
   1.597 +
   1.598 +/*
   1.599 +** Read or write a four-byte big-endian integer value.
   1.600 +*/
   1.601 +u32 sqlite3Get4byte(const u8 *p){
   1.602 +  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
   1.603 +}
   1.604 +void sqlite3Put4byte(unsigned char *p, u32 v){
   1.605 +  p[0] = v>>24;
   1.606 +  p[1] = v>>16;
   1.607 +  p[2] = v>>8;
   1.608 +  p[3] = v;
   1.609 +}
   1.610 +
   1.611 +
   1.612 +
   1.613 +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
   1.614 +    || defined(SQLITE_TEST)
   1.615 +/*
   1.616 +** Translate a single byte of Hex into an integer.
   1.617 +*/
   1.618 +static int hexToInt(int h){
   1.619 +  if( h>='0' && h<='9' ){
   1.620 +    return h - '0';
   1.621 +  }else if( h>='a' && h<='f' ){
   1.622 +    return h - 'a' + 10;
   1.623 +  }else{
   1.624 +    assert( h>='A' && h<='F' );
   1.625 +    return h - 'A' + 10;
   1.626 +  }
   1.627 +}
   1.628 +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
   1.629 +
   1.630 +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
   1.631 +/*
   1.632 +** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
   1.633 +** value.  Return a pointer to its binary value.  Space to hold the
   1.634 +** binary value has been obtained from malloc and must be freed by
   1.635 +** the calling routine.
   1.636 +*/
   1.637 +void *sqlite3HexToBlob(const char *z){
   1.638 +  char *zBlob;
   1.639 +  int i;
   1.640 +  int n = strlen(z);
   1.641 +  if( n%2 ) return 0;
   1.642 +
   1.643 +  zBlob = (char *)sqliteMalloc(n/2);
   1.644 +  if( zBlob ){
   1.645 +    for(i=0; i<n; i+=2){
   1.646 +      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
   1.647 +    }
   1.648 +  }
   1.649 +  return zBlob;
   1.650 +}
   1.651 +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
   1.652 +
   1.653 +
   1.654 +/*
   1.655 +** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
   1.656 +** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
   1.657 +** when this routine is called.
   1.658 +**
   1.659 +** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
   1.660 +** value indicates that the database connection passed into the API is
   1.661 +** open and is not being used by another thread.  By changing the value
   1.662 +** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
   1.663 +** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
   1.664 +** when the API exits. 
   1.665 +**
   1.666 +** This routine is a attempt to detect if two threads use the
   1.667 +** same sqlite* pointer at the same time.  There is a race 
   1.668 +** condition so it is possible that the error is not detected.
   1.669 +** But usually the problem will be seen.  The result will be an
   1.670 +** error which can be used to debug the application that is
   1.671 +** using SQLite incorrectly.
   1.672 +**
   1.673 +** Ticket #202:  If db->magic is not a valid open value, take care not
   1.674 +** to modify the db structure at all.  It could be that db is a stale
   1.675 +** pointer.  In other words, it could be that there has been a prior
   1.676 +** call to sqlite3_close(db) and db has been deallocated.  And we do
   1.677 +** not want to write into deallocated memory.
   1.678 +*/
   1.679 +int sqlite3SafetyOn(sqlite3 *db){
   1.680 +  if( db->magic==SQLITE_MAGIC_OPEN ){
   1.681 +    db->magic = SQLITE_MAGIC_BUSY;
   1.682 +    return 0;
   1.683 +  }else if( db->magic==SQLITE_MAGIC_BUSY ){
   1.684 +    db->magic = SQLITE_MAGIC_ERROR;
   1.685 +    db->u1.isInterrupted = 1;
   1.686 +  }
   1.687 +  return 1;
   1.688 +}
   1.689 +
   1.690 +/*
   1.691 +** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
   1.692 +** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
   1.693 +** when this routine is called.
   1.694 +*/
   1.695 +int sqlite3SafetyOff(sqlite3 *db){
   1.696 +  if( db->magic==SQLITE_MAGIC_BUSY ){
   1.697 +    db->magic = SQLITE_MAGIC_OPEN;
   1.698 +    return 0;
   1.699 +  }else {
   1.700 +    db->magic = SQLITE_MAGIC_ERROR;
   1.701 +    db->u1.isInterrupted = 1;
   1.702 +    return 1;
   1.703 +  }
   1.704 +}
   1.705 +
   1.706 +/*
   1.707 +** Return a pointer to the ThreadData associated with the calling thread.
   1.708 +*/
   1.709 +ThreadData *sqlite3ThreadData(){
   1.710 +  ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
   1.711 +  if( !p ){
   1.712 +    sqlite3FailedMalloc();
   1.713 +  }
   1.714 +  return p;
   1.715 +}
   1.716 +
   1.717 +/*
   1.718 +** Return a pointer to the ThreadData associated with the calling thread.
   1.719 +** If no ThreadData has been allocated to this thread yet, return a pointer
   1.720 +** to a substitute ThreadData structure that is all zeros. 
   1.721 +*/
   1.722 +const ThreadData *sqlite3ThreadDataReadOnly(){
   1.723 +  static const ThreadData zeroData = {0};  /* Initializer to silence warnings
   1.724 +                                           ** from broken compilers */
   1.725 +  const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
   1.726 +  return pTd ? pTd : &zeroData;
   1.727 +}
   1.728 +
   1.729 +/*
   1.730 +** Check to see if the ThreadData for this thread is all zero.  If it
   1.731 +** is, then deallocate it. 
   1.732 +*/
   1.733 +void sqlite3ReleaseThreadData(){
   1.734 +  sqlite3OsThreadSpecificData(-1);
   1.735 +}