1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite364/util.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,952 @@
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: util.c,v 1.241 2008/07/28 19:34:54 drh Exp $
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 +** Return true if the floating point value is Not a Number (NaN).
1.29 +*/
1.30 +int sqlite3IsNaN(double x){
1.31 + /* This NaN test sometimes fails if compiled on GCC with -ffast-math.
1.32 + ** On the other hand, the use of -ffast-math comes with the following
1.33 + ** warning:
1.34 + **
1.35 + ** This option [-ffast-math] should never be turned on by any
1.36 + ** -O option since it can result in incorrect output for programs
1.37 + ** which depend on an exact implementation of IEEE or ISO
1.38 + ** rules/specifications for math functions.
1.39 + **
1.40 + ** Under MSVC, this NaN test may fail if compiled with a floating-
1.41 + ** point precision mode other than /fp:precise. From the MSDN
1.42 + ** documentation:
1.43 + **
1.44 + ** The compiler [with /fp:precise] will properly handle comparisons
1.45 + ** involving NaN. For example, x != x evaluates to true if x is NaN
1.46 + ** ...
1.47 + */
1.48 +#ifdef __FAST_MATH__
1.49 +# error SQLite will not work correctly with the -ffast-math option of GCC.
1.50 +#endif
1.51 + volatile double y = x;
1.52 + volatile double z = y;
1.53 + return y!=z;
1.54 +}
1.55 +
1.56 +/*
1.57 +** Return the length of a string, except do not allow the string length
1.58 +** to exceed the SQLITE_LIMIT_LENGTH setting.
1.59 +*/
1.60 +int sqlite3Strlen(sqlite3 *db, const char *z){
1.61 + const char *z2 = z;
1.62 + int len;
1.63 + size_t x;
1.64 + while( *z2 ){ z2++; }
1.65 + x = z2 - z;
1.66 + len = 0x7fffffff & x;
1.67 + if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.68 + return db->aLimit[SQLITE_LIMIT_LENGTH];
1.69 + }else{
1.70 + return len;
1.71 + }
1.72 +}
1.73 +
1.74 +/*
1.75 +** Set the most recent error code and error string for the sqlite
1.76 +** handle "db". The error code is set to "err_code".
1.77 +**
1.78 +** If it is not NULL, string zFormat specifies the format of the
1.79 +** error string in the style of the printf functions: The following
1.80 +** format characters are allowed:
1.81 +**
1.82 +** %s Insert a string
1.83 +** %z A string that should be freed after use
1.84 +** %d Insert an integer
1.85 +** %T Insert a token
1.86 +** %S Insert the first element of a SrcList
1.87 +**
1.88 +** zFormat and any string tokens that follow it are assumed to be
1.89 +** encoded in UTF-8.
1.90 +**
1.91 +** To clear the most recent error for sqlite handle "db", sqlite3Error
1.92 +** should be called with err_code set to SQLITE_OK and zFormat set
1.93 +** to NULL.
1.94 +*/
1.95 +void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
1.96 + if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
1.97 + db->errCode = err_code;
1.98 + if( zFormat ){
1.99 + char *z;
1.100 + va_list ap;
1.101 + va_start(ap, zFormat);
1.102 + z = sqlite3VMPrintf(db, zFormat, ap);
1.103 + va_end(ap);
1.104 + sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
1.105 + }else{
1.106 + sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
1.107 + }
1.108 + }
1.109 +}
1.110 +
1.111 +/*
1.112 +** Add an error message to pParse->zErrMsg and increment pParse->nErr.
1.113 +** The following formatting characters are allowed:
1.114 +**
1.115 +** %s Insert a string
1.116 +** %z A string that should be freed after use
1.117 +** %d Insert an integer
1.118 +** %T Insert a token
1.119 +** %S Insert the first element of a SrcList
1.120 +**
1.121 +** This function should be used to report any error that occurs whilst
1.122 +** compiling an SQL statement (i.e. within sqlite3_prepare()). The
1.123 +** last thing the sqlite3_prepare() function does is copy the error
1.124 +** stored by this function into the database handle using sqlite3Error().
1.125 +** Function sqlite3Error() should be used during statement execution
1.126 +** (sqlite3_step() etc.).
1.127 +*/
1.128 +void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
1.129 + va_list ap;
1.130 + sqlite3 *db = pParse->db;
1.131 + pParse->nErr++;
1.132 + sqlite3DbFree(db, pParse->zErrMsg);
1.133 + va_start(ap, zFormat);
1.134 + pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
1.135 + va_end(ap);
1.136 + if( pParse->rc==SQLITE_OK ){
1.137 + pParse->rc = SQLITE_ERROR;
1.138 + }
1.139 +}
1.140 +
1.141 +/*
1.142 +** Clear the error message in pParse, if any
1.143 +*/
1.144 +void sqlite3ErrorClear(Parse *pParse){
1.145 + sqlite3DbFree(pParse->db, pParse->zErrMsg);
1.146 + pParse->zErrMsg = 0;
1.147 + pParse->nErr = 0;
1.148 +}
1.149 +
1.150 +/*
1.151 +** Convert an SQL-style quoted string into a normal string by removing
1.152 +** the quote characters. The conversion is done in-place. If the
1.153 +** input does not begin with a quote character, then this routine
1.154 +** is a no-op.
1.155 +**
1.156 +** 2002-Feb-14: This routine is extended to remove MS-Access style
1.157 +** brackets from around identifers. For example: "[a-b-c]" becomes
1.158 +** "a-b-c".
1.159 +*/
1.160 +void sqlite3Dequote(char *z){
1.161 + int quote;
1.162 + int i, j;
1.163 + if( z==0 ) return;
1.164 + quote = z[0];
1.165 + switch( quote ){
1.166 + case '\'': break;
1.167 + case '"': break;
1.168 + case '`': break; /* For MySQL compatibility */
1.169 + case '[': quote = ']'; break; /* For MS SqlServer compatibility */
1.170 + default: return;
1.171 + }
1.172 + for(i=1, j=0; z[i]; i++){
1.173 + if( z[i]==quote ){
1.174 + if( z[i+1]==quote ){
1.175 + z[j++] = quote;
1.176 + i++;
1.177 + }else{
1.178 + z[j++] = 0;
1.179 + break;
1.180 + }
1.181 + }else{
1.182 + z[j++] = z[i];
1.183 + }
1.184 + }
1.185 +}
1.186 +
1.187 +/* Convenient short-hand */
1.188 +#define UpperToLower sqlite3UpperToLower
1.189 +
1.190 +/*
1.191 +** Some systems have stricmp(). Others have strcasecmp(). Because
1.192 +** there is no consistency, we will define our own.
1.193 +*/
1.194 +int sqlite3StrICmp(const char *zLeft, const char *zRight){
1.195 + register unsigned char *a, *b;
1.196 + a = (unsigned char *)zLeft;
1.197 + b = (unsigned char *)zRight;
1.198 + while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
1.199 + return UpperToLower[*a] - UpperToLower[*b];
1.200 +}
1.201 +int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
1.202 + register unsigned char *a, *b;
1.203 + a = (unsigned char *)zLeft;
1.204 + b = (unsigned char *)zRight;
1.205 + while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
1.206 + return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
1.207 +}
1.208 +
1.209 +/*
1.210 +** Return TRUE if z is a pure numeric string. Return FALSE if the
1.211 +** string contains any character which is not part of a number. If
1.212 +** the string is numeric and contains the '.' character, set *realnum
1.213 +** to TRUE (otherwise FALSE).
1.214 +**
1.215 +** An empty string is considered non-numeric.
1.216 +*/
1.217 +int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
1.218 + int incr = (enc==SQLITE_UTF8?1:2);
1.219 + if( enc==SQLITE_UTF16BE ) z++;
1.220 + if( *z=='-' || *z=='+' ) z += incr;
1.221 + if( !isdigit(*(u8*)z) ){
1.222 + return 0;
1.223 + }
1.224 + z += incr;
1.225 + if( realnum ) *realnum = 0;
1.226 + while( isdigit(*(u8*)z) ){ z += incr; }
1.227 + if( *z=='.' ){
1.228 + z += incr;
1.229 + if( !isdigit(*(u8*)z) ) return 0;
1.230 + while( isdigit(*(u8*)z) ){ z += incr; }
1.231 + if( realnum ) *realnum = 1;
1.232 + }
1.233 + if( *z=='e' || *z=='E' ){
1.234 + z += incr;
1.235 + if( *z=='+' || *z=='-' ) z += incr;
1.236 + if( !isdigit(*(u8*)z) ) return 0;
1.237 + while( isdigit(*(u8*)z) ){ z += incr; }
1.238 + if( realnum ) *realnum = 1;
1.239 + }
1.240 + return *z==0;
1.241 +}
1.242 +
1.243 +/*
1.244 +** The string z[] is an ascii representation of a real number.
1.245 +** Convert this string to a double.
1.246 +**
1.247 +** This routine assumes that z[] really is a valid number. If it
1.248 +** is not, the result is undefined.
1.249 +**
1.250 +** This routine is used instead of the library atof() function because
1.251 +** the library atof() might want to use "," as the decimal point instead
1.252 +** of "." depending on how locale is set. But that would cause problems
1.253 +** for SQL. So this routine always uses "." regardless of locale.
1.254 +*/
1.255 +int sqlite3AtoF(const char *z, double *pResult){
1.256 +#ifndef SQLITE_OMIT_FLOATING_POINT
1.257 + int sign = 1;
1.258 + const char *zBegin = z;
1.259 + LONGDOUBLE_TYPE v1 = 0.0;
1.260 + int nSignificant = 0;
1.261 + while( isspace(*(u8*)z) ) z++;
1.262 + if( *z=='-' ){
1.263 + sign = -1;
1.264 + z++;
1.265 + }else if( *z=='+' ){
1.266 + z++;
1.267 + }
1.268 + while( z[0]=='0' ){
1.269 + z++;
1.270 + }
1.271 + while( isdigit(*(u8*)z) ){
1.272 + v1 = v1*10.0 + (*z - '0');
1.273 + z++;
1.274 + nSignificant++;
1.275 + }
1.276 + if( *z=='.' ){
1.277 + LONGDOUBLE_TYPE divisor = 1.0;
1.278 + z++;
1.279 + if( nSignificant==0 ){
1.280 + while( z[0]=='0' ){
1.281 + divisor *= 10.0;
1.282 + z++;
1.283 + }
1.284 + }
1.285 + while( isdigit(*(u8*)z) ){
1.286 + if( nSignificant<18 ){
1.287 + v1 = v1*10.0 + (*z - '0');
1.288 + divisor *= 10.0;
1.289 + nSignificant++;
1.290 + }
1.291 + z++;
1.292 + }
1.293 + v1 /= divisor;
1.294 + }
1.295 + if( *z=='e' || *z=='E' ){
1.296 + int esign = 1;
1.297 + int eval = 0;
1.298 + LONGDOUBLE_TYPE scale = 1.0;
1.299 + z++;
1.300 + if( *z=='-' ){
1.301 + esign = -1;
1.302 + z++;
1.303 + }else if( *z=='+' ){
1.304 + z++;
1.305 + }
1.306 + while( isdigit(*(u8*)z) ){
1.307 + eval = eval*10 + *z - '0';
1.308 + z++;
1.309 + }
1.310 + while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
1.311 + while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
1.312 + while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
1.313 + while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
1.314 + if( esign<0 ){
1.315 + v1 /= scale;
1.316 + }else{
1.317 + v1 *= scale;
1.318 + }
1.319 + }
1.320 + *pResult = sign<0 ? -v1 : v1;
1.321 + return z - zBegin;
1.322 +#else
1.323 + return sqlite3Atoi64(z, pResult);
1.324 +#endif /* SQLITE_OMIT_FLOATING_POINT */
1.325 +}
1.326 +
1.327 +/*
1.328 +** Compare the 19-character string zNum against the text representation
1.329 +** value 2^63: 9223372036854775808. Return negative, zero, or positive
1.330 +** if zNum is less than, equal to, or greater than the string.
1.331 +**
1.332 +** Unlike memcmp() this routine is guaranteed to return the difference
1.333 +** in the values of the last digit if the only difference is in the
1.334 +** last digit. So, for example,
1.335 +**
1.336 +** compare2pow63("9223372036854775800")
1.337 +**
1.338 +** will return -8.
1.339 +*/
1.340 +static int compare2pow63(const char *zNum){
1.341 + int c;
1.342 + c = memcmp(zNum,"922337203685477580",18);
1.343 + if( c==0 ){
1.344 + c = zNum[18] - '8';
1.345 + }
1.346 + return c;
1.347 +}
1.348 +
1.349 +
1.350 +/*
1.351 +** Return TRUE if zNum is a 64-bit signed integer and write
1.352 +** the value of the integer into *pNum. If zNum is not an integer
1.353 +** or is an integer that is too large to be expressed with 64 bits,
1.354 +** then return false.
1.355 +**
1.356 +** When this routine was originally written it dealt with only
1.357 +** 32-bit numbers. At that time, it was much faster than the
1.358 +** atoi() library routine in RedHat 7.2.
1.359 +*/
1.360 +int sqlite3Atoi64(const char *zNum, i64 *pNum){
1.361 + i64 v = 0;
1.362 + int neg;
1.363 + int i, c;
1.364 + const char *zStart;
1.365 + while( isspace(*(u8*)zNum) ) zNum++;
1.366 + if( *zNum=='-' ){
1.367 + neg = 1;
1.368 + zNum++;
1.369 + }else if( *zNum=='+' ){
1.370 + neg = 0;
1.371 + zNum++;
1.372 + }else{
1.373 + neg = 0;
1.374 + }
1.375 + zStart = zNum;
1.376 + while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
1.377 + for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
1.378 + v = v*10 + c - '0';
1.379 + }
1.380 + *pNum = neg ? -v : v;
1.381 + if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
1.382 + /* zNum is empty or contains non-numeric text or is longer
1.383 + ** than 19 digits (thus guaranting that it is too large) */
1.384 + return 0;
1.385 + }else if( i<19 ){
1.386 + /* Less than 19 digits, so we know that it fits in 64 bits */
1.387 + return 1;
1.388 + }else{
1.389 + /* 19-digit numbers must be no larger than 9223372036854775807 if positive
1.390 + ** or 9223372036854775808 if negative. Note that 9223372036854665808
1.391 + ** is 2^63. */
1.392 + return compare2pow63(zNum)<neg;
1.393 + }
1.394 +}
1.395 +
1.396 +/*
1.397 +** The string zNum represents an integer. There might be some other
1.398 +** information following the integer too, but that part is ignored.
1.399 +** If the integer that the prefix of zNum represents will fit in a
1.400 +** 64-bit signed integer, return TRUE. Otherwise return FALSE.
1.401 +**
1.402 +** This routine returns FALSE for the string -9223372036854775808 even that
1.403 +** that number will, in theory fit in a 64-bit integer. Positive
1.404 +** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
1.405 +** false.
1.406 +*/
1.407 +int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
1.408 + int i, c;
1.409 + int neg = 0;
1.410 + if( *zNum=='-' ){
1.411 + neg = 1;
1.412 + zNum++;
1.413 + }else if( *zNum=='+' ){
1.414 + zNum++;
1.415 + }
1.416 + if( negFlag ) neg = 1-neg;
1.417 + while( *zNum=='0' ){
1.418 + zNum++; /* Skip leading zeros. Ticket #2454 */
1.419 + }
1.420 + for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
1.421 + if( i<19 ){
1.422 + /* Guaranteed to fit if less than 19 digits */
1.423 + return 1;
1.424 + }else if( i>19 ){
1.425 + /* Guaranteed to be too big if greater than 19 digits */
1.426 + return 0;
1.427 + }else{
1.428 + /* Compare against 2^63. */
1.429 + return compare2pow63(zNum)<neg;
1.430 + }
1.431 +}
1.432 +
1.433 +/*
1.434 +** If zNum represents an integer that will fit in 32-bits, then set
1.435 +** *pValue to that integer and return true. Otherwise return false.
1.436 +**
1.437 +** Any non-numeric characters that following zNum are ignored.
1.438 +** This is different from sqlite3Atoi64() which requires the
1.439 +** input number to be zero-terminated.
1.440 +*/
1.441 +int sqlite3GetInt32(const char *zNum, int *pValue){
1.442 + sqlite_int64 v = 0;
1.443 + int i, c;
1.444 + int neg = 0;
1.445 + if( zNum[0]=='-' ){
1.446 + neg = 1;
1.447 + zNum++;
1.448 + }else if( zNum[0]=='+' ){
1.449 + zNum++;
1.450 + }
1.451 + while( zNum[0]=='0' ) zNum++;
1.452 + for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
1.453 + v = v*10 + c;
1.454 + }
1.455 +
1.456 + /* The longest decimal representation of a 32 bit integer is 10 digits:
1.457 + **
1.458 + ** 1234567890
1.459 + ** 2^31 -> 2147483648
1.460 + */
1.461 + if( i>10 ){
1.462 + return 0;
1.463 + }
1.464 + if( v-neg>2147483647 ){
1.465 + return 0;
1.466 + }
1.467 + if( neg ){
1.468 + v = -v;
1.469 + }
1.470 + *pValue = (int)v;
1.471 + return 1;
1.472 +}
1.473 +
1.474 +/*
1.475 +** The variable-length integer encoding is as follows:
1.476 +**
1.477 +** KEY:
1.478 +** A = 0xxxxxxx 7 bits of data and one flag bit
1.479 +** B = 1xxxxxxx 7 bits of data and one flag bit
1.480 +** C = xxxxxxxx 8 bits of data
1.481 +**
1.482 +** 7 bits - A
1.483 +** 14 bits - BA
1.484 +** 21 bits - BBA
1.485 +** 28 bits - BBBA
1.486 +** 35 bits - BBBBA
1.487 +** 42 bits - BBBBBA
1.488 +** 49 bits - BBBBBBA
1.489 +** 56 bits - BBBBBBBA
1.490 +** 64 bits - BBBBBBBBC
1.491 +*/
1.492 +
1.493 +/*
1.494 +** Write a 64-bit variable-length integer to memory starting at p[0].
1.495 +** The length of data write will be between 1 and 9 bytes. The number
1.496 +** of bytes written is returned.
1.497 +**
1.498 +** A variable-length integer consists of the lower 7 bits of each byte
1.499 +** for all bytes that have the 8th bit set and one byte with the 8th
1.500 +** bit clear. Except, if we get to the 9th byte, it stores the full
1.501 +** 8 bits and is the last byte.
1.502 +*/
1.503 +int sqlite3PutVarint(unsigned char *p, u64 v){
1.504 + int i, j, n;
1.505 + u8 buf[10];
1.506 + if( v & (((u64)0xff000000)<<32) ){
1.507 + p[8] = v;
1.508 + v >>= 8;
1.509 + for(i=7; i>=0; i--){
1.510 + p[i] = (v & 0x7f) | 0x80;
1.511 + v >>= 7;
1.512 + }
1.513 + return 9;
1.514 + }
1.515 + n = 0;
1.516 + do{
1.517 + buf[n++] = (v & 0x7f) | 0x80;
1.518 + v >>= 7;
1.519 + }while( v!=0 );
1.520 + buf[0] &= 0x7f;
1.521 + assert( n<=9 );
1.522 + for(i=0, j=n-1; j>=0; j--, i++){
1.523 + p[i] = buf[j];
1.524 + }
1.525 + return n;
1.526 +}
1.527 +
1.528 +/*
1.529 +** This routine is a faster version of sqlite3PutVarint() that only
1.530 +** works for 32-bit positive integers and which is optimized for
1.531 +** the common case of small integers. A MACRO version, putVarint32,
1.532 +** is provided which inlines the single-byte case. All code should use
1.533 +** the MACRO version as this function assumes the single-byte case has
1.534 +** already been handled.
1.535 +*/
1.536 +int sqlite3PutVarint32(unsigned char *p, u32 v){
1.537 +#ifndef putVarint32
1.538 + if( (v & ~0x7f)==0 ){
1.539 + p[0] = v;
1.540 + return 1;
1.541 + }
1.542 +#endif
1.543 + if( (v & ~0x3fff)==0 ){
1.544 + p[0] = (v>>7) | 0x80;
1.545 + p[1] = v & 0x7f;
1.546 + return 2;
1.547 + }
1.548 + return sqlite3PutVarint(p, v);
1.549 +}
1.550 +
1.551 +/*
1.552 +** Read a 64-bit variable-length integer from memory starting at p[0].
1.553 +** Return the number of bytes read. The value is stored in *v.
1.554 +*/
1.555 +int sqlite3GetVarint(const unsigned char *p, u64 *v){
1.556 + u32 a,b,s;
1.557 +
1.558 + a = *p;
1.559 + /* a: p0 (unmasked) */
1.560 + if (!(a&0x80))
1.561 + {
1.562 + *v = a;
1.563 + return 1;
1.564 + }
1.565 +
1.566 + p++;
1.567 + b = *p;
1.568 + /* b: p1 (unmasked) */
1.569 + if (!(b&0x80))
1.570 + {
1.571 + a &= 0x7f;
1.572 + a = a<<7;
1.573 + a |= b;
1.574 + *v = a;
1.575 + return 2;
1.576 + }
1.577 +
1.578 + p++;
1.579 + a = a<<14;
1.580 + a |= *p;
1.581 + /* a: p0<<14 | p2 (unmasked) */
1.582 + if (!(a&0x80))
1.583 + {
1.584 + a &= (0x7f<<14)|(0x7f);
1.585 + b &= 0x7f;
1.586 + b = b<<7;
1.587 + a |= b;
1.588 + *v = a;
1.589 + return 3;
1.590 + }
1.591 +
1.592 + /* CSE1 from below */
1.593 + a &= (0x7f<<14)|(0x7f);
1.594 + p++;
1.595 + b = b<<14;
1.596 + b |= *p;
1.597 + /* b: p1<<14 | p3 (unmasked) */
1.598 + if (!(b&0x80))
1.599 + {
1.600 + b &= (0x7f<<14)|(0x7f);
1.601 + /* moved CSE1 up */
1.602 + /* a &= (0x7f<<14)|(0x7f); */
1.603 + a = a<<7;
1.604 + a |= b;
1.605 + *v = a;
1.606 + return 4;
1.607 + }
1.608 +
1.609 + /* a: p0<<14 | p2 (masked) */
1.610 + /* b: p1<<14 | p3 (unmasked) */
1.611 + /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
1.612 + /* moved CSE1 up */
1.613 + /* a &= (0x7f<<14)|(0x7f); */
1.614 + b &= (0x7f<<14)|(0x7f);
1.615 + s = a;
1.616 + /* s: p0<<14 | p2 (masked) */
1.617 +
1.618 + p++;
1.619 + a = a<<14;
1.620 + a |= *p;
1.621 + /* a: p0<<28 | p2<<14 | p4 (unmasked) */
1.622 + if (!(a&0x80))
1.623 + {
1.624 + /* we can skip these cause they were (effectively) done above in calc'ing s */
1.625 + /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
1.626 + /* b &= (0x7f<<14)|(0x7f); */
1.627 + b = b<<7;
1.628 + a |= b;
1.629 + s = s>>18;
1.630 + *v = ((u64)s)<<32 | a;
1.631 + return 5;
1.632 + }
1.633 +
1.634 + /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
1.635 + s = s<<7;
1.636 + s |= b;
1.637 + /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
1.638 +
1.639 + p++;
1.640 + b = b<<14;
1.641 + b |= *p;
1.642 + /* b: p1<<28 | p3<<14 | p5 (unmasked) */
1.643 + if (!(b&0x80))
1.644 + {
1.645 + /* we can skip this cause it was (effectively) done above in calc'ing s */
1.646 + /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
1.647 + a &= (0x7f<<14)|(0x7f);
1.648 + a = a<<7;
1.649 + a |= b;
1.650 + s = s>>18;
1.651 + *v = ((u64)s)<<32 | a;
1.652 + return 6;
1.653 + }
1.654 +
1.655 + p++;
1.656 + a = a<<14;
1.657 + a |= *p;
1.658 + /* a: p2<<28 | p4<<14 | p6 (unmasked) */
1.659 + if (!(a&0x80))
1.660 + {
1.661 + a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
1.662 + b &= (0x7f<<14)|(0x7f);
1.663 + b = b<<7;
1.664 + a |= b;
1.665 + s = s>>11;
1.666 + *v = ((u64)s)<<32 | a;
1.667 + return 7;
1.668 + }
1.669 +
1.670 + /* CSE2 from below */
1.671 + a &= (0x7f<<14)|(0x7f);
1.672 + p++;
1.673 + b = b<<14;
1.674 + b |= *p;
1.675 + /* b: p3<<28 | p5<<14 | p7 (unmasked) */
1.676 + if (!(b&0x80))
1.677 + {
1.678 + b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
1.679 + /* moved CSE2 up */
1.680 + /* a &= (0x7f<<14)|(0x7f); */
1.681 + a = a<<7;
1.682 + a |= b;
1.683 + s = s>>4;
1.684 + *v = ((u64)s)<<32 | a;
1.685 + return 8;
1.686 + }
1.687 +
1.688 + p++;
1.689 + a = a<<15;
1.690 + a |= *p;
1.691 + /* a: p4<<29 | p6<<15 | p8 (unmasked) */
1.692 +
1.693 + /* moved CSE2 up */
1.694 + /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
1.695 + b &= (0x7f<<14)|(0x7f);
1.696 + b = b<<8;
1.697 + a |= b;
1.698 +
1.699 + s = s<<4;
1.700 + b = p[-4];
1.701 + b &= 0x7f;
1.702 + b = b>>3;
1.703 + s |= b;
1.704 +
1.705 + *v = ((u64)s)<<32 | a;
1.706 +
1.707 + return 9;
1.708 +}
1.709 +
1.710 +/*
1.711 +** Read a 32-bit variable-length integer from memory starting at p[0].
1.712 +** Return the number of bytes read. The value is stored in *v.
1.713 +** A MACRO version, getVarint32, is provided which inlines the
1.714 +** single-byte case. All code should use the MACRO version as
1.715 +** this function assumes the single-byte case has already been handled.
1.716 +*/
1.717 +int sqlite3GetVarint32(const unsigned char *p, u32 *v){
1.718 + u32 a,b;
1.719 +
1.720 + a = *p;
1.721 + /* a: p0 (unmasked) */
1.722 +#ifndef getVarint32
1.723 + if (!(a&0x80))
1.724 + {
1.725 + *v = a;
1.726 + return 1;
1.727 + }
1.728 +#endif
1.729 +
1.730 + p++;
1.731 + b = *p;
1.732 + /* b: p1 (unmasked) */
1.733 + if (!(b&0x80))
1.734 + {
1.735 + a &= 0x7f;
1.736 + a = a<<7;
1.737 + *v = a | b;
1.738 + return 2;
1.739 + }
1.740 +
1.741 + p++;
1.742 + a = a<<14;
1.743 + a |= *p;
1.744 + /* a: p0<<14 | p2 (unmasked) */
1.745 + if (!(a&0x80))
1.746 + {
1.747 + a &= (0x7f<<14)|(0x7f);
1.748 + b &= 0x7f;
1.749 + b = b<<7;
1.750 + *v = a | b;
1.751 + return 3;
1.752 + }
1.753 +
1.754 + p++;
1.755 + b = b<<14;
1.756 + b |= *p;
1.757 + /* b: p1<<14 | p3 (unmasked) */
1.758 + if (!(b&0x80))
1.759 + {
1.760 + b &= (0x7f<<14)|(0x7f);
1.761 + a &= (0x7f<<14)|(0x7f);
1.762 + a = a<<7;
1.763 + *v = a | b;
1.764 + return 4;
1.765 + }
1.766 +
1.767 + p++;
1.768 + a = a<<14;
1.769 + a |= *p;
1.770 + /* a: p0<<28 | p2<<14 | p4 (unmasked) */
1.771 + if (!(a&0x80))
1.772 + {
1.773 + a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
1.774 + b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
1.775 + b = b<<7;
1.776 + *v = a | b;
1.777 + return 5;
1.778 + }
1.779 +
1.780 + /* We can only reach this point when reading a corrupt database
1.781 + ** file. In that case we are not in any hurry. Use the (relatively
1.782 + ** slow) general-purpose sqlite3GetVarint() routine to extract the
1.783 + ** value. */
1.784 + {
1.785 + u64 v64;
1.786 + int n;
1.787 +
1.788 + p -= 4;
1.789 + n = sqlite3GetVarint(p, &v64);
1.790 + assert( n>5 && n<=9 );
1.791 + *v = (u32)v64;
1.792 + return n;
1.793 + }
1.794 +}
1.795 +
1.796 +/*
1.797 +** Return the number of bytes that will be needed to store the given
1.798 +** 64-bit integer.
1.799 +*/
1.800 +int sqlite3VarintLen(u64 v){
1.801 + int i = 0;
1.802 + do{
1.803 + i++;
1.804 + v >>= 7;
1.805 + }while( v!=0 && i<9 );
1.806 + return i;
1.807 +}
1.808 +
1.809 +
1.810 +/*
1.811 +** Read or write a four-byte big-endian integer value.
1.812 +*/
1.813 +u32 sqlite3Get4byte(const u8 *p){
1.814 + return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
1.815 +}
1.816 +void sqlite3Put4byte(unsigned char *p, u32 v){
1.817 + p[0] = v>>24;
1.818 + p[1] = v>>16;
1.819 + p[2] = v>>8;
1.820 + p[3] = v;
1.821 +}
1.822 +
1.823 +
1.824 +
1.825 +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1.826 +/*
1.827 +** Translate a single byte of Hex into an integer.
1.828 +** This routinen only works if h really is a valid hexadecimal
1.829 +** character: 0..9a..fA..F
1.830 +*/
1.831 +static int hexToInt(int h){
1.832 + assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
1.833 +#ifdef SQLITE_ASCII
1.834 + h += 9*(1&(h>>6));
1.835 +#endif
1.836 +#ifdef SQLITE_EBCDIC
1.837 + h += 9*(1&~(h>>4));
1.838 +#endif
1.839 + return h & 0xf;
1.840 +}
1.841 +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1.842 +
1.843 +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1.844 +/*
1.845 +** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1.846 +** value. Return a pointer to its binary value. Space to hold the
1.847 +** binary value has been obtained from malloc and must be freed by
1.848 +** the calling routine.
1.849 +*/
1.850 +void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
1.851 + char *zBlob;
1.852 + int i;
1.853 +
1.854 + zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
1.855 + n--;
1.856 + if( zBlob ){
1.857 + for(i=0; i<n; i+=2){
1.858 + zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
1.859 + }
1.860 + zBlob[i/2] = 0;
1.861 + }
1.862 + return zBlob;
1.863 +}
1.864 +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1.865 +
1.866 +
1.867 +/*
1.868 +** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1.869 +** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1.870 +** when this routine is called.
1.871 +**
1.872 +** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
1.873 +** value indicates that the database connection passed into the API is
1.874 +** open and is not being used by another thread. By changing the value
1.875 +** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
1.876 +** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
1.877 +** when the API exits.
1.878 +**
1.879 +** This routine is a attempt to detect if two threads use the
1.880 +** same sqlite* pointer at the same time. There is a race
1.881 +** condition so it is possible that the error is not detected.
1.882 +** But usually the problem will be seen. The result will be an
1.883 +** error which can be used to debug the application that is
1.884 +** using SQLite incorrectly.
1.885 +**
1.886 +** Ticket #202: If db->magic is not a valid open value, take care not
1.887 +** to modify the db structure at all. It could be that db is a stale
1.888 +** pointer. In other words, it could be that there has been a prior
1.889 +** call to sqlite3_close(db) and db has been deallocated. And we do
1.890 +** not want to write into deallocated memory.
1.891 +*/
1.892 +#ifdef SQLITE_DEBUG
1.893 +int sqlite3SafetyOn(sqlite3 *db){
1.894 + if( db->magic==SQLITE_MAGIC_OPEN ){
1.895 + db->magic = SQLITE_MAGIC_BUSY;
1.896 + assert( sqlite3_mutex_held(db->mutex) );
1.897 + return 0;
1.898 + }else if( db->magic==SQLITE_MAGIC_BUSY ){
1.899 + db->magic = SQLITE_MAGIC_ERROR;
1.900 + db->u1.isInterrupted = 1;
1.901 + }
1.902 + return 1;
1.903 +}
1.904 +#endif
1.905 +
1.906 +/*
1.907 +** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1.908 +** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1.909 +** when this routine is called.
1.910 +*/
1.911 +#ifdef SQLITE_DEBUG
1.912 +int sqlite3SafetyOff(sqlite3 *db){
1.913 + if( db->magic==SQLITE_MAGIC_BUSY ){
1.914 + db->magic = SQLITE_MAGIC_OPEN;
1.915 + assert( sqlite3_mutex_held(db->mutex) );
1.916 + return 0;
1.917 + }else{
1.918 + db->magic = SQLITE_MAGIC_ERROR;
1.919 + db->u1.isInterrupted = 1;
1.920 + return 1;
1.921 + }
1.922 +}
1.923 +#endif
1.924 +
1.925 +/*
1.926 +** Check to make sure we have a valid db pointer. This test is not
1.927 +** foolproof but it does provide some measure of protection against
1.928 +** misuse of the interface such as passing in db pointers that are
1.929 +** NULL or which have been previously closed. If this routine returns
1.930 +** 1 it means that the db pointer is valid and 0 if it should not be
1.931 +** dereferenced for any reason. The calling function should invoke
1.932 +** SQLITE_MISUSE immediately.
1.933 +**
1.934 +** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1.935 +** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1.936 +** open properly and is not fit for general use but which can be
1.937 +** used as an argument to sqlite3_errmsg() or sqlite3_close().
1.938 +*/
1.939 +int sqlite3SafetyCheckOk(sqlite3 *db){
1.940 + int magic;
1.941 + if( db==0 ) return 0;
1.942 + magic = db->magic;
1.943 + if( magic!=SQLITE_MAGIC_OPEN &&
1.944 + magic!=SQLITE_MAGIC_BUSY ) return 0;
1.945 + return 1;
1.946 +}
1.947 +int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1.948 + int magic;
1.949 + if( db==0 ) return 0;
1.950 + magic = db->magic;
1.951 + if( magic!=SQLITE_MAGIC_SICK &&
1.952 + magic!=SQLITE_MAGIC_OPEN &&
1.953 + magic!=SQLITE_MAGIC_BUSY ) return 0;
1.954 + return 1;
1.955 +}