extensions/sqlite/sqlite-source/util.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2001 September 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** Utility functions used throughout sqlite.
ds@1952
    13
**
ds@1952
    14
** This file contains functions for allocating memory, comparing
ds@1952
    15
** strings, and stuff like that.
ds@1952
    16
**
ds@1952
    17
** $Id$
ds@1952
    18
*/
ds@1952
    19
#include "sqliteInt.h"
ds@1952
    20
#include <stdarg.h>
ds@1952
    21
#include <ctype.h>
ds@1952
    22
ds@1952
    23
ds@1952
    24
/*
ds@1952
    25
** Set the most recent error code and error string for the sqlite
ds@1952
    26
** handle "db". The error code is set to "err_code".
ds@1952
    27
**
ds@1952
    28
** If it is not NULL, string zFormat specifies the format of the
ds@1952
    29
** error string in the style of the printf functions: The following
ds@1952
    30
** format characters are allowed:
ds@1952
    31
**
ds@1952
    32
**      %s      Insert a string
ds@1952
    33
**      %z      A string that should be freed after use
ds@1952
    34
**      %d      Insert an integer
ds@1952
    35
**      %T      Insert a token
ds@1952
    36
**      %S      Insert the first element of a SrcList
ds@1952
    37
**
ds@1952
    38
** zFormat and any string tokens that follow it are assumed to be
ds@1952
    39
** encoded in UTF-8.
ds@1952
    40
**
ds@1952
    41
** To clear the most recent error for sqlite handle "db", sqlite3Error
ds@1952
    42
** should be called with err_code set to SQLITE_OK and zFormat set
ds@1952
    43
** to NULL.
ds@1952
    44
*/
ds@1952
    45
void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
ds@1952
    46
  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
ds@1952
    47
    db->errCode = err_code;
ds@1952
    48
    if( zFormat ){
ds@1952
    49
      char *z;
ds@1952
    50
      va_list ap;
ds@1952
    51
      va_start(ap, zFormat);
ds@1952
    52
      z = sqlite3VMPrintf(db, zFormat, ap);
ds@1952
    53
      va_end(ap);
ds@1952
    54
      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free);
ds@1952
    55
    }else{
ds@1952
    56
      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
ds@1952
    57
    }
ds@1952
    58
  }
ds@1952
    59
}
ds@1952
    60
ds@1952
    61
/*
ds@1952
    62
** Add an error message to pParse->zErrMsg and increment pParse->nErr.
ds@1952
    63
** The following formatting characters are allowed:
ds@1952
    64
**
ds@1952
    65
**      %s      Insert a string
ds@1952
    66
**      %z      A string that should be freed after use
ds@1952
    67
**      %d      Insert an integer
ds@1952
    68
**      %T      Insert a token
ds@1952
    69
**      %S      Insert the first element of a SrcList
ds@1952
    70
**
ds@1952
    71
** This function should be used to report any error that occurs whilst
ds@1952
    72
** compiling an SQL statement (i.e. within sqlite3_prepare()). The
ds@1952
    73
** last thing the sqlite3_prepare() function does is copy the error
ds@1952
    74
** stored by this function into the database handle using sqlite3Error().
ds@1952
    75
** Function sqlite3Error() should be used during statement execution
ds@1952
    76
** (sqlite3_step() etc.).
ds@1952
    77
*/
ds@1952
    78
void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
ds@1952
    79
  va_list ap;
ds@1952
    80
  pParse->nErr++;
ds@1952
    81
  sqlite3_free(pParse->zErrMsg);
ds@1952
    82
  va_start(ap, zFormat);
ds@1952
    83
  pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap);
ds@1952
    84
  va_end(ap);
ds@1952
    85
  if( pParse->rc==SQLITE_OK ){
ds@1952
    86
    pParse->rc = SQLITE_ERROR;
ds@1952
    87
  }
ds@1952
    88
}
ds@1952
    89
ds@1952
    90
/*
ds@1952
    91
** Clear the error message in pParse, if any
ds@1952
    92
*/
ds@1952
    93
void sqlite3ErrorClear(Parse *pParse){
ds@1952
    94
  sqlite3_free(pParse->zErrMsg);
ds@1952
    95
  pParse->zErrMsg = 0;
ds@1952
    96
  pParse->nErr = 0;
ds@1952
    97
}
ds@1952
    98
ds@1952
    99
/*
ds@1952
   100
** Convert an SQL-style quoted string into a normal string by removing
ds@1952
   101
** the quote characters.  The conversion is done in-place.  If the
ds@1952
   102
** input does not begin with a quote character, then this routine
ds@1952
   103
** is a no-op.
ds@1952
   104
**
ds@1952
   105
** 2002-Feb-14: This routine is extended to remove MS-Access style
ds@1952
   106
** brackets from around identifers.  For example:  "[a-b-c]" becomes
ds@1952
   107
** "a-b-c".
ds@1952
   108
*/
ds@1952
   109
void sqlite3Dequote(char *z){
ds@1952
   110
  int quote;
ds@1952
   111
  int i, j;
ds@1952
   112
  if( z==0 ) return;
ds@1952
   113
  quote = z[0];
ds@1952
   114
  switch( quote ){
ds@1952
   115
    case '\'':  break;
ds@1952
   116
    case '"':   break;
ds@1952
   117
    case '`':   break;                /* For MySQL compatibility */
ds@1952
   118
    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
ds@1952
   119
    default:    return;
ds@1952
   120
  }
ds@1952
   121
  for(i=1, j=0; z[i]; i++){
ds@1952
   122
    if( z[i]==quote ){
ds@1952
   123
      if( z[i+1]==quote ){
ds@1952
   124
        z[j++] = quote;
ds@1952
   125
        i++;
ds@1952
   126
      }else{
ds@1952
   127
        z[j++] = 0;
ds@1952
   128
        break;
ds@1952
   129
      }
ds@1952
   130
    }else{
ds@1952
   131
      z[j++] = z[i];
ds@1952
   132
    }
ds@1952
   133
  }
ds@1952
   134
}
ds@1952
   135
ds@1952
   136
/* An array to map all upper-case characters into their corresponding
ds@1952
   137
** lower-case character. 
ds@1952
   138
*/
ds@1952
   139
const unsigned char sqlite3UpperToLower[] = {
ds@1952
   140
#ifdef SQLITE_ASCII
ds@1952
   141
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
ds@1952
   142
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
ds@1952
   143
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
ds@1952
   144
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
ds@1952
   145
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
ds@1952
   146
    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
ds@1952
   147
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
ds@1952
   148
    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
ds@1952
   149
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
ds@1952
   150
    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
ds@1952
   151
    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
ds@1952
   152
    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
ds@1952
   153
    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
ds@1952
   154
    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
ds@1952
   155
    252,253,254,255
ds@1952
   156
#endif
ds@1952
   157
#ifdef SQLITE_EBCDIC
ds@1952
   158
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
ds@1952
   159
     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
ds@1952
   160
     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
ds@1952
   161
     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
ds@1952
   162
     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
ds@1952
   163
     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
ds@1952
   164
     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
ds@1952
   165
    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
ds@1952
   166
    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
ds@1952
   167
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
ds@1952
   168
    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
ds@1952
   169
    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
ds@1952
   170
    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
ds@1952
   171
    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
ds@1952
   172
    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
ds@1952
   173
    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
ds@1952
   174
#endif
ds@1952
   175
};
ds@1952
   176
#define UpperToLower sqlite3UpperToLower
ds@1952
   177
ds@1952
   178
/*
ds@1952
   179
** Some systems have stricmp().  Others have strcasecmp().  Because
ds@1952
   180
** there is no consistency, we will define our own.
ds@1952
   181
*/
ds@1952
   182
int sqlite3StrICmp(const char *zLeft, const char *zRight){
ds@1952
   183
  register unsigned char *a, *b;
ds@1952
   184
  a = (unsigned char *)zLeft;
ds@1952
   185
  b = (unsigned char *)zRight;
ds@1952
   186
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
ds@1952
   187
  return UpperToLower[*a] - UpperToLower[*b];
ds@1952
   188
}
ds@1952
   189
int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
ds@1952
   190
  register unsigned char *a, *b;
ds@1952
   191
  a = (unsigned char *)zLeft;
ds@1952
   192
  b = (unsigned char *)zRight;
ds@1952
   193
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
ds@1952
   194
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
ds@1952
   195
}
ds@1952
   196
ds@1952
   197
/*
ds@1952
   198
** Return TRUE if z is a pure numeric string.  Return FALSE if the
ds@1952
   199
** string contains any character which is not part of a number. If
ds@1952
   200
** the string is numeric and contains the '.' character, set *realnum
ds@1952
   201
** to TRUE (otherwise FALSE).
ds@1952
   202
**
ds@1952
   203
** An empty string is considered non-numeric.
ds@1952
   204
*/
ds@1952
   205
int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
ds@1952
   206
  int incr = (enc==SQLITE_UTF8?1:2);
ds@1952
   207
  if( enc==SQLITE_UTF16BE ) z++;
ds@1952
   208
  if( *z=='-' || *z=='+' ) z += incr;
ds@1952
   209
  if( !isdigit(*(u8*)z) ){
ds@1952
   210
    return 0;
ds@1952
   211
  }
ds@1952
   212
  z += incr;
ds@1952
   213
  if( realnum ) *realnum = 0;
ds@1952
   214
  while( isdigit(*(u8*)z) ){ z += incr; }
ds@1952
   215
  if( *z=='.' ){
ds@1952
   216
    z += incr;
ds@1952
   217
    if( !isdigit(*(u8*)z) ) return 0;
ds@1952
   218
    while( isdigit(*(u8*)z) ){ z += incr; }
ds@1952
   219
    if( realnum ) *realnum = 1;
ds@1952
   220
  }
ds@1952
   221
  if( *z=='e' || *z=='E' ){
ds@1952
   222
    z += incr;
ds@1952
   223
    if( *z=='+' || *z=='-' ) z += incr;
ds@1952
   224
    if( !isdigit(*(u8*)z) ) return 0;
ds@1952
   225
    while( isdigit(*(u8*)z) ){ z += incr; }
ds@1952
   226
    if( realnum ) *realnum = 1;
ds@1952
   227
  }
ds@1952
   228
  return *z==0;
ds@1952
   229
}
ds@1952
   230
ds@1952
   231
/*
ds@1952
   232
** The string z[] is an ascii representation of a real number.
ds@1952
   233
** Convert this string to a double.
ds@1952
   234
**
ds@1952
   235
** This routine assumes that z[] really is a valid number.  If it
ds@1952
   236
** is not, the result is undefined.
ds@1952
   237
**
ds@1952
   238
** This routine is used instead of the library atof() function because
ds@1952
   239
** the library atof() might want to use "," as the decimal point instead
ds@1952
   240
** of "." depending on how locale is set.  But that would cause problems
ds@1952
   241
** for SQL.  So this routine always uses "." regardless of locale.
ds@1952
   242
*/
ds@1952
   243
int sqlite3AtoF(const char *z, double *pResult){
ds@1952
   244
#ifndef SQLITE_OMIT_FLOATING_POINT
ds@1952
   245
  int sign = 1;
ds@1952
   246
  const char *zBegin = z;
ds@1952
   247
  LONGDOUBLE_TYPE v1 = 0.0;
ds@1952
   248
  while( isspace(*(u8*)z) ) z++;
ds@1952
   249
  if( *z=='-' ){
ds@1952
   250
    sign = -1;
ds@1952
   251
    z++;
ds@1952
   252
  }else if( *z=='+' ){
ds@1952
   253
    z++;
ds@1952
   254
  }
ds@1952
   255
  while( isdigit(*(u8*)z) ){
ds@1952
   256
    v1 = v1*10.0 + (*z - '0');
ds@1952
   257
    z++;
ds@1952
   258
  }
ds@1952
   259
  if( *z=='.' ){
ds@1952
   260
    LONGDOUBLE_TYPE divisor = 1.0;
ds@1952
   261
    z++;
ds@1952
   262
    while( isdigit(*(u8*)z) ){
ds@1952
   263
      v1 = v1*10.0 + (*z - '0');
ds@1952
   264
      divisor *= 10.0;
ds@1952
   265
      z++;
ds@1952
   266
    }
ds@1952
   267
    v1 /= divisor;
ds@1952
   268
  }
ds@1952
   269
  if( *z=='e' || *z=='E' ){
ds@1952
   270
    int esign = 1;
ds@1952
   271
    int eval = 0;
ds@1952
   272
    LONGDOUBLE_TYPE scale = 1.0;
ds@1952
   273
    z++;
ds@1952
   274
    if( *z=='-' ){
ds@1952
   275
      esign = -1;
ds@1952
   276
      z++;
ds@1952
   277
    }else if( *z=='+' ){
ds@1952
   278
      z++;
ds@1952
   279
    }
ds@1952
   280
    while( isdigit(*(u8*)z) ){
ds@1952
   281
      eval = eval*10 + *z - '0';
ds@1952
   282
      z++;
ds@1952
   283
    }
ds@1952
   284
    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
ds@1952
   285
    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
ds@1952
   286
    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
ds@1952
   287
    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
ds@1952
   288
    if( esign<0 ){
ds@1952
   289
      v1 /= scale;
ds@1952
   290
    }else{
ds@1952
   291
      v1 *= scale;
ds@1952
   292
    }
ds@1952
   293
  }
ds@1952
   294
  *pResult = sign<0 ? -v1 : v1;
ds@1952
   295
  return z - zBegin;
ds@1952
   296
#else
ds@1952
   297
  return sqlite3Atoi64(z, pResult);
ds@1952
   298
#endif /* SQLITE_OMIT_FLOATING_POINT */
ds@1952
   299
}
ds@1952
   300
ds@1952
   301
/*
ds@1952
   302
** Compare the 19-character string zNum against the text representation
ds@1952
   303
** value 2^63:  9223372036854775808.  Return negative, zero, or positive
ds@1952
   304
** if zNum is less than, equal to, or greater than the string.
ds@1952
   305
**
ds@1952
   306
** Unlike memcmp() this routine is guaranteed to return the difference
ds@1952
   307
** in the values of the last digit if the only difference is in the
ds@1952
   308
** last digit.  So, for example,
ds@1952
   309
**
ds@1952
   310
**      compare2pow63("9223372036854775800")
ds@1952
   311
**
ds@1952
   312
** will return -8.
ds@1952
   313
*/
ds@1952
   314
static int compare2pow63(const char *zNum){
ds@1952
   315
  int c;
ds@1952
   316
  c = memcmp(zNum,"922337203685477580",18);
ds@1952
   317
  if( c==0 ){
ds@1952
   318
    c = zNum[18] - '8';
ds@1952
   319
  }
ds@1952
   320
  return c;
ds@1952
   321
}
ds@1952
   322
ds@1952
   323
ds@1952
   324
/*
ds@1952
   325
** Return TRUE if zNum is a 64-bit signed integer and write
ds@1952
   326
** the value of the integer into *pNum.  If zNum is not an integer
ds@1952
   327
** or is an integer that is too large to be expressed with 64 bits,
ds@1952
   328
** then return false.
ds@1952
   329
**
ds@1952
   330
** When this routine was originally written it dealt with only
ds@1952
   331
** 32-bit numbers.  At that time, it was much faster than the
ds@1952
   332
** atoi() library routine in RedHat 7.2.
ds@1952
   333
*/
ds@1952
   334
int sqlite3Atoi64(const char *zNum, i64 *pNum){
ds@1952
   335
  i64 v = 0;
ds@1952
   336
  int neg;
ds@1952
   337
  int i, c;
ds@1952
   338
  while( isspace(*(u8*)zNum) ) zNum++;
ds@1952
   339
  if( *zNum=='-' ){
ds@1952
   340
    neg = 1;
ds@1952
   341
    zNum++;
ds@1952
   342
  }else if( *zNum=='+' ){
ds@1952
   343
    neg = 0;
ds@1952
   344
    zNum++;
ds@1952
   345
  }else{
ds@1952
   346
    neg = 0;
ds@1952
   347
  }
ds@1952
   348
  while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
ds@1952
   349
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
ds@1952
   350
    v = v*10 + c - '0';
ds@1952
   351
  }
ds@1952
   352
  *pNum = neg ? -v : v;
ds@1952
   353
  if( c!=0 || i==0 || i>19 ){
ds@1952
   354
    /* zNum is empty or contains non-numeric text or is longer
ds@1952
   355
    ** than 19 digits (thus guaranting that it is too large) */
ds@1952
   356
    return 0;
ds@1952
   357
  }else if( i<19 ){
ds@1952
   358
    /* Less than 19 digits, so we know that it fits in 64 bits */
ds@1952
   359
    return 1;
ds@1952
   360
  }else{
ds@1952
   361
    /* 19-digit numbers must be no larger than 9223372036854775807 if positive
ds@1952
   362
    ** or 9223372036854775808 if negative.  Note that 9223372036854665808
ds@1952
   363
    ** is 2^63. */
ds@1952
   364
    return compare2pow63(zNum)<neg;
ds@1952
   365
  }
ds@1952
   366
}
ds@1952
   367
ds@1952
   368
/*
ds@1952
   369
** The string zNum represents an integer.  There might be some other
ds@1952
   370
** information following the integer too, but that part is ignored.
ds@1952
   371
** If the integer that the prefix of zNum represents will fit in a
ds@1952
   372
** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
ds@1952
   373
**
ds@1952
   374
** This routine returns FALSE for the string -9223372036854775808 even that
ds@1952
   375
** that number will, in theory fit in a 64-bit integer.  Positive
ds@1952
   376
** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
ds@1952
   377
** false.
ds@1952
   378
*/
ds@1952
   379
int sqlite3FitsIn64Bits(const char *zNum){
ds@1952
   380
  int i, c;
ds@1952
   381
  int neg = 0;
ds@1952
   382
  if( *zNum=='-' ){
ds@1952
   383
    neg = 1;
ds@1952
   384
    zNum++;
ds@1952
   385
  }else if( *zNum=='+' ){
ds@1952
   386
    zNum++;
ds@1952
   387
  }
ds@1952
   388
  while( *zNum=='0' ){
ds@1952
   389
    zNum++;   /* Skip leading zeros.  Ticket #2454 */
ds@1952
   390
  }
ds@1952
   391
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
ds@1952
   392
  if( i<19 ){
ds@1952
   393
    /* Guaranteed to fit if less than 19 digits */
ds@1952
   394
    return 1;
ds@1952
   395
  }else if( i>19 ){
ds@1952
   396
    /* Guaranteed to be too big if greater than 19 digits */
ds@1952
   397
    return 0;
ds@1952
   398
  }else{
ds@1952
   399
    /* Compare against 2^63. */
ds@1952
   400
    return compare2pow63(zNum)<neg;
ds@1952
   401
  }
ds@1952
   402
}
ds@1952
   403
ds@1952
   404
/*
ds@1952
   405
** If zNum represents an integer that will fit in 32-bits, then set
ds@1952
   406
** *pValue to that integer and return true.  Otherwise return false.
ds@1952
   407
**
ds@1952
   408
** Any non-numeric characters that following zNum are ignored.
ds@1952
   409
** This is different from sqlite3Atoi64() which requires the
ds@1952
   410
** input number to be zero-terminated.
ds@1952
   411
*/
ds@1952
   412
int sqlite3GetInt32(const char *zNum, int *pValue){
ds@1952
   413
  sqlite_int64 v = 0;
ds@1952
   414
  int i, c;
ds@1952
   415
  int neg = 0;
ds@1952
   416
  if( zNum[0]=='-' ){
ds@1952
   417
    neg = 1;
ds@1952
   418
    zNum++;
ds@1952
   419
  }else if( zNum[0]=='+' ){
ds@1952
   420
    zNum++;
ds@1952
   421
  }
ds@1952
   422
  while( zNum[0]=='0' ) zNum++;
ds@1952
   423
  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
ds@1952
   424
    v = v*10 + c;
ds@1952
   425
  }
ds@1952
   426
ds@1952
   427
  /* The longest decimal representation of a 32 bit integer is 10 digits:
ds@1952
   428
  **
ds@1952
   429
  **             1234567890
ds@1952
   430
  **     2^31 -> 2147483648
ds@1952
   431
  */
ds@1952
   432
  if( i>10 ){
ds@1952
   433
    return 0;
ds@1952
   434
  }
ds@1952
   435
  if( v-neg>2147483647 ){
ds@1952
   436
    return 0;
ds@1952
   437
  }
ds@1952
   438
  if( neg ){
ds@1952
   439
    v = -v;
ds@1952
   440
  }
ds@1952
   441
  *pValue = (int)v;
ds@1952
   442
  return 1;
ds@1952
   443
}
ds@1952
   444
ds@1952
   445
/*
ds@1952
   446
** Check to make sure we have a valid db pointer.  This test is not
ds@1952
   447
** foolproof but it does provide some measure of protection against
ds@1952
   448
** misuse of the interface such as passing in db pointers that are
ds@1952
   449
** NULL or which have been previously closed.  If this routine returns
ds@1952
   450
** TRUE it means that the db pointer is invalid and should not be
ds@1952
   451
** dereferenced for any reason.  The calling function should invoke
ds@1952
   452
** SQLITE_MISUSE immediately.
ds@1952
   453
*/
ds@1952
   454
int sqlite3SafetyCheck(sqlite3 *db){
ds@1952
   455
  int magic;
ds@1952
   456
  if( db==0 ) return 1;
ds@1952
   457
  magic = db->magic;
ds@1952
   458
  if( magic!=SQLITE_MAGIC_CLOSED &&
ds@1952
   459
         magic!=SQLITE_MAGIC_OPEN &&
ds@1952
   460
         magic!=SQLITE_MAGIC_BUSY ) return 1;
ds@1952
   461
  return 0;
ds@1952
   462
}
ds@1952
   463
ds@1952
   464
/*
ds@1952
   465
** The variable-length integer encoding is as follows:
ds@1952
   466
**
ds@1952
   467
** KEY:
ds@1952
   468
**         A = 0xxxxxxx    7 bits of data and one flag bit
ds@1952
   469
**         B = 1xxxxxxx    7 bits of data and one flag bit
ds@1952
   470
**         C = xxxxxxxx    8 bits of data
ds@1952
   471
**
ds@1952
   472
**  7 bits - A
ds@1952
   473
** 14 bits - BA
ds@1952
   474
** 21 bits - BBA
ds@1952
   475
** 28 bits - BBBA
ds@1952
   476
** 35 bits - BBBBA
ds@1952
   477
** 42 bits - BBBBBA
ds@1952
   478
** 49 bits - BBBBBBA
ds@1952
   479
** 56 bits - BBBBBBBA
ds@1952
   480
** 64 bits - BBBBBBBBC
ds@1952
   481
*/
ds@1952
   482
ds@1952
   483
/*
ds@1952
   484
** Write a 64-bit variable-length integer to memory starting at p[0].
ds@1952
   485
** The length of data write will be between 1 and 9 bytes.  The number
ds@1952
   486
** of bytes written is returned.
ds@1952
   487
**
ds@1952
   488
** A variable-length integer consists of the lower 7 bits of each byte
ds@1952
   489
** for all bytes that have the 8th bit set and one byte with the 8th
ds@1952
   490
** bit clear.  Except, if we get to the 9th byte, it stores the full
ds@1952
   491
** 8 bits and is the last byte.
ds@1952
   492
*/
ds@1952
   493
int sqlite3PutVarint(unsigned char *p, u64 v){
ds@1952
   494
  int i, j, n;
ds@1952
   495
  u8 buf[10];
ds@1952
   496
  if( v & (((u64)0xff000000)<<32) ){
ds@1952
   497
    p[8] = v;
ds@1952
   498
    v >>= 8;
ds@1952
   499
    for(i=7; i>=0; i--){
ds@1952
   500
      p[i] = (v & 0x7f) | 0x80;
ds@1952
   501
      v >>= 7;
ds@1952
   502
    }
ds@1952
   503
    return 9;
ds@1952
   504
  }    
ds@1952
   505
  n = 0;
ds@1952
   506
  do{
ds@1952
   507
    buf[n++] = (v & 0x7f) | 0x80;
ds@1952
   508
    v >>= 7;
ds@1952
   509
  }while( v!=0 );
ds@1952
   510
  buf[0] &= 0x7f;
ds@1952
   511
  assert( n<=9 );
ds@1952
   512
  for(i=0, j=n-1; j>=0; j--, i++){
ds@1952
   513
    p[i] = buf[j];
ds@1952
   514
  }
ds@1952
   515
  return n;
ds@1952
   516
}
ds@1952
   517
ds@1952
   518
/*
ds@1952
   519
** Read a 64-bit variable-length integer from memory starting at p[0].
ds@1952
   520
** Return the number of bytes read.  The value is stored in *v.
ds@1952
   521
*/
ds@1952
   522
int sqlite3GetVarint(const unsigned char *p, u64 *v){
ds@1952
   523
  u32 x;
ds@1952
   524
  u64 x64;
ds@1952
   525
  int n;
ds@1952
   526
  unsigned char c;
ds@1952
   527
  if( ((c = p[0]) & 0x80)==0 ){
ds@1952
   528
    *v = c;
ds@1952
   529
    return 1;
ds@1952
   530
  }
ds@1952
   531
  x = c & 0x7f;
ds@1952
   532
  if( ((c = p[1]) & 0x80)==0 ){
ds@1952
   533
    *v = (x<<7) | c;
ds@1952
   534
    return 2;
ds@1952
   535
  }
ds@1952
   536
  x = (x<<7) | (c&0x7f);
ds@1952
   537
  if( ((c = p[2]) & 0x80)==0 ){
ds@1952
   538
    *v = (x<<7) | c;
ds@1952
   539
    return 3;
ds@1952
   540
  }
ds@1952
   541
  x = (x<<7) | (c&0x7f);
ds@1952
   542
  if( ((c = p[3]) & 0x80)==0 ){
ds@1952
   543
    *v = (x<<7) | c;
ds@1952
   544
    return 4;
ds@1952
   545
  }
ds@1952
   546
  x64 = (x<<7) | (c&0x7f);
ds@1952
   547
  n = 4;
ds@1952
   548
  do{
ds@1952
   549
    c = p[n++];
ds@1952
   550
    if( n==9 ){
ds@1952
   551
      x64 = (x64<<8) | c;
ds@1952
   552
      break;
ds@1952
   553
    }
ds@1952
   554
    x64 = (x64<<7) | (c&0x7f);
ds@1952
   555
  }while( (c & 0x80)!=0 );
ds@1952
   556
  *v = x64;
ds@1952
   557
  return n;
ds@1952
   558
}
ds@1952
   559
ds@1952
   560
/*
ds@1952
   561
** Read a 32-bit variable-length integer from memory starting at p[0].
ds@1952
   562
** Return the number of bytes read.  The value is stored in *v.
ds@1952
   563
*/
ds@1952
   564
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
ds@1952
   565
  u32 x;
ds@1952
   566
  int n;
ds@1952
   567
  unsigned char c;
ds@1952
   568
  if( ((signed char*)p)[0]>=0 ){
ds@1952
   569
    *v = p[0];
ds@1952
   570
    return 1;
ds@1952
   571
  }
ds@1952
   572
  x = p[0] & 0x7f;
ds@1952
   573
  if( ((signed char*)p)[1]>=0 ){
ds@1952
   574
    *v = (x<<7) | p[1];
ds@1952
   575
    return 2;
ds@1952
   576
  }
ds@1952
   577
  x = (x<<7) | (p[1] & 0x7f);
ds@1952
   578
  n = 2;
ds@1952
   579
  do{
ds@1952
   580
    x = (x<<7) | ((c = p[n++])&0x7f);
ds@1952
   581
  }while( (c & 0x80)!=0 && n<9 );
ds@1952
   582
  *v = x;
ds@1952
   583
  return n;
ds@1952
   584
}
ds@1952
   585
ds@1952
   586
/*
ds@1952
   587
** Return the number of bytes that will be needed to store the given
ds@1952
   588
** 64-bit integer.
ds@1952
   589
*/
ds@1952
   590
int sqlite3VarintLen(u64 v){
ds@1952
   591
  int i = 0;
ds@1952
   592
  do{
ds@1952
   593
    i++;
ds@1952
   594
    v >>= 7;
ds@1952
   595
  }while( v!=0 && i<9 );
ds@1952
   596
  return i;
ds@1952
   597
}
ds@1952
   598
ds@1952
   599
ds@1952
   600
/*
ds@1952
   601
** Read or write a four-byte big-endian integer value.
ds@1952
   602
*/
ds@1952
   603
u32 sqlite3Get4byte(const u8 *p){
ds@1952
   604
  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
ds@1952
   605
}
ds@1952
   606
void sqlite3Put4byte(unsigned char *p, u32 v){
ds@1952
   607
  p[0] = v>>24;
ds@1952
   608
  p[1] = v>>16;
ds@1952
   609
  p[2] = v>>8;
ds@1952
   610
  p[3] = v;
ds@1952
   611
}
ds@1952
   612
ds@1952
   613
ds@1952
   614
ds@1952
   615
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
ds@1952
   616
    || defined(SQLITE_TEST)
ds@1952
   617
/*
ds@1952
   618
** Translate a single byte of Hex into an integer.
ds@1952
   619
*/
ds@1952
   620
static int hexToInt(int h){
ds@1952
   621
  if( h>='0' && h<='9' ){
ds@1952
   622
    return h - '0';
ds@1952
   623
  }else if( h>='a' && h<='f' ){
ds@1952
   624
    return h - 'a' + 10;
ds@1952
   625
  }else{
ds@1952
   626
    assert( h>='A' && h<='F' );
ds@1952
   627
    return h - 'A' + 10;
ds@1952
   628
  }
ds@1952
   629
}
ds@1952
   630
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
ds@1952
   631
ds@1952
   632
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
ds@1952
   633
/*
ds@1952
   634
** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
ds@1952
   635
** value.  Return a pointer to its binary value.  Space to hold the
ds@1952
   636
** binary value has been obtained from malloc and must be freed by
ds@1952
   637
** the calling routine.
ds@1952
   638
*/
ds@1952
   639
void *sqlite3HexToBlob(sqlite3 *db, const char *z){
ds@1952
   640
  char *zBlob;
ds@1952
   641
  int i;
ds@1952
   642
  int n = strlen(z);
ds@1952
   643
  if( n%2 ) return 0;
ds@1952
   644
ds@1952
   645
  zBlob = (char *)sqlite3DbMallocRaw(db, n/2);
ds@1952
   646
  if( zBlob ){
ds@1952
   647
    for(i=0; i<n; i+=2){
ds@1952
   648
      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
ds@1952
   649
    }
ds@1952
   650
  }
ds@1952
   651
  return zBlob;
ds@1952
   652
}
ds@1952
   653
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
ds@1952
   654
ds@1952
   655
ds@1952
   656
/*
ds@1952
   657
** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
ds@1952
   658
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
ds@1952
   659
** when this routine is called.
ds@1952
   660
**
ds@1952
   661
** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
ds@1952
   662
** value indicates that the database connection passed into the API is
ds@1952
   663
** open and is not being used by another thread.  By changing the value
ds@1952
   664
** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
ds@1952
   665
** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
ds@1952
   666
** when the API exits. 
ds@1952
   667
**
ds@1952
   668
** This routine is a attempt to detect if two threads use the
ds@1952
   669
** same sqlite* pointer at the same time.  There is a race 
ds@1952
   670
** condition so it is possible that the error is not detected.
ds@1952
   671
** But usually the problem will be seen.  The result will be an
ds@1952
   672
** error which can be used to debug the application that is
ds@1952
   673
** using SQLite incorrectly.
ds@1952
   674
**
ds@1952
   675
** Ticket #202:  If db->magic is not a valid open value, take care not
ds@1952
   676
** to modify the db structure at all.  It could be that db is a stale
ds@1952
   677
** pointer.  In other words, it could be that there has been a prior
ds@1952
   678
** call to sqlite3_close(db) and db has been deallocated.  And we do
ds@1952
   679
** not want to write into deallocated memory.
ds@1952
   680
*/
ds@1952
   681
int sqlite3SafetyOn(sqlite3 *db){
ds@1952
   682
  if( db->magic==SQLITE_MAGIC_OPEN ){
ds@1952
   683
    db->magic = SQLITE_MAGIC_BUSY;
ds@1952
   684
    return 0;
ds@1952
   685
  }else if( db->magic==SQLITE_MAGIC_BUSY ){
ds@1952
   686
    db->magic = SQLITE_MAGIC_ERROR;
ds@1952
   687
    db->u1.isInterrupted = 1;
ds@1952
   688
  }
ds@1952
   689
  return 1;
ds@1952
   690
}
ds@1952
   691
ds@1952
   692
/*
ds@1952
   693
** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
ds@1952
   694
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
ds@1952
   695
** when this routine is called.
ds@1952
   696
*/
ds@1952
   697
int sqlite3SafetyOff(sqlite3 *db){
ds@1952
   698
  if( db->magic==SQLITE_MAGIC_BUSY ){
ds@1952
   699
    db->magic = SQLITE_MAGIC_OPEN;
ds@1952
   700
    return 0;
ds@1952
   701
  }else {
ds@1952
   702
    db->magic = SQLITE_MAGIC_ERROR;
ds@1952
   703
    db->u1.isInterrupted = 1;
ds@1952
   704
    return 1;
ds@1952
   705
  }
ds@1952
   706
}