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