#include "sqliteInt.h"
#include <stdarg.h>
#ifndef SQLITE_OMIT_FLOATING_POINT
#include <math.h>
#endif
#ifndef SQLITE_UNTESTABLE
int sqlite3FaultSim(int iTest){
int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
return xCallback ? xCallback(iTest) : SQLITE_OK;
}
#endif
#ifndef SQLITE_OMIT_FLOATING_POINT
int sqlite3IsNaN(double x){
int rc;
#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
u64 y;
memcpy(&y,&x,sizeof(y));
rc = IsNaN(y);
#else
rc = isnan(x);
#endif
testcase( rc );
return rc;
}
#endif
int sqlite3Strlen30(const char *z){
if( z==0 ) return 0;
return 0x3fffffff & (int)strlen(z);
}
char *sqlite3ColumnType(Column *pCol, char *zDflt){
if( pCol->colFlags & COLFLAG_HASTYPE ){
return pCol->zCnName + strlen(pCol->zCnName) + 1;
}else if( pCol->eCType ){
assert( pCol->eCType<=SQLITE_N_STDTYPE );
return (char*)sqlite3StdType[pCol->eCType-1];
}else{
return zDflt;
}
}
static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
if( db->pErr ) sqlite3ValueSetNull(db->pErr);
sqlite3SystemError(db, err_code);
}
void sqlite3Error(sqlite3 *db, int err_code){
assert( db!=0 );
db->errCode = err_code;
if( err_code || db->pErr ){
sqlite3ErrorFinish(db, err_code);
}else{
db->errByteOffset = -1;
}
}
void sqlite3ErrorClear(sqlite3 *db){
assert( db!=0 );
db->errCode = SQLITE_OK;
db->errByteOffset = -1;
if( db->pErr ) sqlite3ValueSetNull(db->pErr);
}
void sqlite3SystemError(sqlite3 *db, int rc){
if( rc==SQLITE_IOERR_NOMEM ) return;
rc &= 0xff;
if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
}
}
void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
assert( db!=0 );
db->errCode = err_code;
sqlite3SystemError(db, err_code);
if( zFormat==0 ){
sqlite3Error(db, err_code);
}else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
char *z;
va_list ap;
va_start(ap, zFormat);
z = sqlite3VMPrintf(db, zFormat, ap);
va_end(ap);
sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
}
}
void sqlite3ProgressCheck(Parse *p){
sqlite3 *db = p->db;
if( AtomicLoad(&db->u1.isInterrupted) ){
p->nErr++;
p->rc = SQLITE_INTERRUPT;
}
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
if( db->xProgress && (++p->nProgressSteps)>=db->nProgressOps ){
if( db->xProgress(db->pProgressArg) ){
p->nErr++;
p->rc = SQLITE_INTERRUPT;
}
p->nProgressSteps = 0;
}
#endif
}
void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
char *zMsg;
va_list ap;
sqlite3 *db = pParse->db;
assert( db!=0 );
assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
db->errByteOffset = -2;
va_start(ap, zFormat);
zMsg = sqlite3VMPrintf(db, zFormat, ap);
va_end(ap);
if( db->errByteOffset<-1 ) db->errByteOffset = -1;
if( db->suppressErr ){
sqlite3DbFree(db, zMsg);
if( db->mallocFailed ){
pParse->nErr++;
pParse->rc = SQLITE_NOMEM;
}
}else{
pParse->nErr++;
sqlite3DbFree(db, pParse->zErrMsg);
pParse->zErrMsg = zMsg;
pParse->rc = SQLITE_ERROR;
pParse->pWith = 0;
}
}
int sqlite3ErrorToParser(sqlite3 *db, int errCode){
Parse *pParse;
if( db==0 || (pParse = db->pParse)==0 ) return errCode;
pParse->rc = errCode;
pParse->nErr++;
return errCode;
}
void sqlite3Dequote(char *z){
char quote;
int i, j;
if( z==0 ) return;
quote = z[0];
if( !sqlite3Isquote(quote) ) return;
if( quote=='[' ) quote = ']';
for(i=1, j=0;; i++){
assert( z[i] );
if( z[i]==quote ){
if( z[i+1]==quote ){
z[j++] = quote;
i++;
}else{
break;
}
}else{
z[j++] = z[i];
}
}
z[j] = 0;
}
void sqlite3DequoteExpr(Expr *p){
assert( !ExprHasProperty(p, EP_IntValue) );
assert( sqlite3Isquote(p->u.zToken[0]) );
p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
sqlite3Dequote(p->u.zToken);
}
void sqlite3DequoteToken(Token *p){
unsigned int i;
if( p->n<2 ) return;
if( !sqlite3Isquote(p->z[0]) ) return;
for(i=1; i<p->n-1; i++){
if( sqlite3Isquote(p->z[i]) ) return;
}
p->n -= 2;
p->z++;
}
void sqlite3TokenInit(Token *p, char *z){
p->z = z;
p->n = sqlite3Strlen30(z);
}
#define UpperToLower sqlite3UpperToLower
int sqlite3_stricmp(const char *zLeft, const char *zRight){
if( zLeft==0 ){
return zRight ? -1 : 0;
}else if( zRight==0 ){
return 1;
}
return sqlite3StrICmp(zLeft, zRight);
}
int sqlite3StrICmp(const char *zLeft, const char *zRight){
unsigned char *a, *b;
int c, x;
a = (unsigned char *)zLeft;
b = (unsigned char *)zRight;
for(;;){
c = *a;
x = *b;
if( c==x ){
if( c==0 ) break;
}else{
c = (int)UpperToLower[c] - (int)UpperToLower[x];
if( c ) break;
}
a++;
b++;
}
return c;
}
int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
register unsigned char *a, *b;
if( zLeft==0 ){
return zRight ? -1 : 0;
}else if( zRight==0 ){
return 1;
}
a = (unsigned char *)zLeft;
b = (unsigned char *)zRight;
while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
}
u8 sqlite3StrIHash(const char *z){
u8 h = 0;
if( z==0 ) return 0;
while( z[0] ){
h += UpperToLower[(unsigned char)z[0]];
z++;
}
return h;
}
static LONGDOUBLE_TYPE sqlite3Pow10(int E){
#if defined(_MSC_VER)
static const LONGDOUBLE_TYPE x[] = {
1.0e+001L,
1.0e+002L,
1.0e+004L,
1.0e+008L,
1.0e+016L,
1.0e+032L,
1.0e+064L,
1.0e+128L,
1.0e+256L
};
LONGDOUBLE_TYPE r = 1.0;
int i;
assert( E>=0 && E<=307 );
for(i=0; E!=0; i++, E >>=1){
if( E & 1 ) r *= x[i];
}
return r;
#else
LONGDOUBLE_TYPE x = 10.0;
LONGDOUBLE_TYPE r = 1.0;
while(1){
if( E & 1 ) r *= x;
E >>= 1;
if( E==0 ) break;
x *= x;
}
return r;
#endif
}
#if defined(_MSC_VER)
#pragma warning(disable : 4756)
#endif
int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
#ifndef SQLITE_OMIT_FLOATING_POINT
int incr;
const char *zEnd;
int sign = 1;
i64 s = 0;
int d = 0;
int esign = 1;
int e = 0;
int eValid = 1;
double result;
int nDigit = 0;
int eType = 1;
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
*pResult = 0.0;
if( length==0 ) return 0;
if( enc==SQLITE_UTF8 ){
incr = 1;
zEnd = z + length;
}else{
int i;
incr = 2;
length &= ~1;
assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
testcase( enc==SQLITE_UTF16LE );
testcase( enc==SQLITE_UTF16BE );
for(i=3-enc; i<length && z[i]==0; i+=2){}
if( i<length ) eType = -100;
zEnd = &z[i^1];
z += (enc&1);
}
while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
if( z>=zEnd ) return 0;
if( *z=='-' ){
sign = -1;
z+=incr;
}else if( *z=='+' ){
z+=incr;
}
while( z<zEnd && sqlite3Isdigit(*z) ){
s = s*10 + (*z - '0');
z+=incr; nDigit++;
if( s>=((LARGEST_INT64-9)/10) ){
while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
}
}
if( z>=zEnd ) goto do_atof_calc;
if( *z=='.' ){
z+=incr;
eType++;
while( z<zEnd && sqlite3Isdigit(*z) ){
if( s<((LARGEST_INT64-9)/10) ){
s = s*10 + (*z - '0');
d--;
nDigit++;
}
z+=incr;
}
}
if( z>=zEnd ) goto do_atof_calc;
if( *z=='e' || *z=='E' ){
z+=incr;
eValid = 0;
eType++;
if( z>=zEnd ) goto do_atof_calc;
if( *z=='-' ){
esign = -1;
z+=incr;
}else if( *z=='+' ){
z+=incr;
}
while( z<zEnd && sqlite3Isdigit(*z) ){
e = e<10000 ? (e*10 + (*z - '0')) : 10000;
z+=incr;
eValid = 1;
}
}
while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
do_atof_calc:
e = (e*esign) + d;
if( e<0 ) {
esign = -1;
e *= -1;
} else {
esign = 1;
}
if( s==0 ) {
result = sign<0 ? -(double)0 : (double)0;
} else {
while( e>0 ){
if( esign>0 ){
if( s>=(LARGEST_INT64/10) ) break;
s *= 10;
}else{
if( s%10!=0 ) break;
s /= 10;
}
e--;
}
s = sign<0 ? -s : s;
if( e==0 ){
result = (double)s;
}else{
if( e>307 ){
if( e<342 ){
LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
if( esign<0 ){
result = s / scale;
result /= 1.0e+308;
}else{
result = s * scale;
result *= 1.0e+308;
}
}else{ assert( e>=342 );
if( esign<0 ){
result = 0.0*s;
}else{
#ifdef INFINITY
result = INFINITY*s;
#else
result = 1e308*1e308*s;
#endif
}
}
}else{
LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
if( esign<0 ){
result = s / scale;
}else{
result = s * scale;
}
}
}
}
*pResult = result;
if( z==zEnd && nDigit>0 && eValid && eType>0 ){
return eType;
}else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
return -1;
}else{
return 0;
}
#else
return !sqlite3Atoi64(z, pResult, length, enc);
#endif
}
#if defined(_MSC_VER)
#pragma warning(default : 4756)
#endif
int sqlite3Int64ToText(i64 v, char *zOut){
int i;
u64 x;
char zTemp[22];
if( v<0 ){
x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
}else{
x = v;
}
i = sizeof(zTemp)-2;
zTemp[sizeof(zTemp)-1] = 0;
while( 1 ){
zTemp[i] = (x%10) + '0';
x = x/10;
if( x==0 ) break;
i--;
};
if( v<0 ) zTemp[--i] = '-';
memcpy(zOut, &zTemp[i], sizeof(zTemp)-i);
return sizeof(zTemp)-1-i;
}
static int compare2pow63(const char *zNum, int incr){
int c = 0;
int i;
const char *pow63 = "922337203685477580";
for(i=0; c==0 && i<18; i++){
c = (zNum[i*incr]-pow63[i])*10;
}
if( c==0 ){
c = zNum[18*incr] - '8';
testcase( c==(-1) );
testcase( c==0 );
testcase( c==(+1) );
}
return c;
}
int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
int incr;
u64 u = 0;
int neg = 0;
int i;
int c = 0;
int nonNum = 0;
int rc;
const char *zStart;
const char *zEnd = zNum + length;
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
if( enc==SQLITE_UTF8 ){
incr = 1;
}else{
incr = 2;
length &= ~1;
assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
for(i=3-enc; i<length && zNum[i]==0; i+=2){}
nonNum = i<length;
zEnd = &zNum[i^1];
zNum += (enc&1);
}
while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
if( zNum<zEnd ){
if( *zNum=='-' ){
neg = 1;
zNum+=incr;
}else if( *zNum=='+' ){
zNum+=incr;
}
}
zStart = zNum;
while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; }
for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
u = u*10 + c - '0';
}
testcase( i==18*incr );
testcase( i==19*incr );
testcase( i==20*incr );
if( u>LARGEST_INT64 ){
*pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
}else if( neg ){
*pNum = -(i64)u;
}else{
*pNum = (i64)u;
}
rc = 0;
if( i==0 && zStart==zNum ){
rc = -1;
}else if( nonNum ){
rc = 1;
}else if( &zNum[i]<zEnd ){
int jj = i;
do{
if( !sqlite3Isspace(zNum[jj]) ){
rc = 1;
break;
}
jj += incr;
}while( &zNum[jj]<zEnd );
}
if( i<19*incr ){
assert( u<=LARGEST_INT64 );
return rc;
}else{
c = i>19*incr ? 1 : compare2pow63(zNum, incr);
if( c<0 ){
assert( u<=LARGEST_INT64 );
return rc;
}else{
*pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
if( c>0 ){
return 2;
}else{
assert( u-1==LARGEST_INT64 );
return neg ? rc : 3;
}
}
}
}
int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
#ifndef SQLITE_OMIT_HEX_INTEGER
if( z[0]=='0'
&& (z[1]=='x' || z[1]=='X')
){
u64 u = 0;
int i, k;
for(i=2; z[i]=='0'; i++){}
for(k=i; sqlite3Isxdigit(z[k]); k++){
u = u*16 + sqlite3HexToInt(z[k]);
}
memcpy(pOut, &u, 8);
if( k-i>16 ) return 2;
if( z[k]!=0 ) return 1;
return 0;
}else
#endif
{
return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
}
}
int sqlite3GetInt32(const char *zNum, int *pValue){
sqlite_int64 v = 0;
int i, c;
int neg = 0;
if( zNum[0]=='-' ){
neg = 1;
zNum++;
}else if( zNum[0]=='+' ){
zNum++;
}
#ifndef SQLITE_OMIT_HEX_INTEGER
else if( zNum[0]=='0'
&& (zNum[1]=='x' || zNum[1]=='X')
&& sqlite3Isxdigit(zNum[2])
){
u32 u = 0;
zNum += 2;
while( zNum[0]=='0' ) zNum++;
for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){
u = u*16 + sqlite3HexToInt(zNum[i]);
}
if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
memcpy(pValue, &u, 4);
return 1;
}else{
return 0;
}
}
#endif
if( !sqlite3Isdigit(zNum[0]) ) return 0;
while( zNum[0]=='0' ) zNum++;
for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
v = v*10 + c;
}
testcase( i==10 );
if( i>10 ){
return 0;
}
testcase( v-neg==2147483647 );
if( v-neg>2147483647 ){
return 0;
}
if( neg ){
v = -v;
}
*pValue = (int)v;
return 1;
}
int sqlite3Atoi(const char *z){
int x = 0;
sqlite3GetInt32(z, &x);
return x;
}
int sqlite3GetUInt32(const char *z, u32 *pI){
u64 v = 0;
int i;
for(i=0; sqlite3Isdigit(z[i]); i++){
v = v*10 + z[i] - '0';
if( v>4294967296LL ){ *pI = 0; return 0; }
}
if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
*pI = (u32)v;
return 1;
}
static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
int i, j, n;
u8 buf[10];
if( v & (((u64)0xff000000)<<32) ){
p[8] = (u8)v;
v >>= 8;
for(i=7; i>=0; i--){
p[i] = (u8)((v & 0x7f) | 0x80);
v >>= 7;
}
return 9;
}
n = 0;
do{
buf[n++] = (u8)((v & 0x7f) | 0x80);
v >>= 7;
}while( v!=0 );
buf[0] &= 0x7f;
assert( n<=9 );
for(i=0, j=n-1; j>=0; j--, i++){
p[i] = buf[j];
}
return n;
}
int sqlite3PutVarint(unsigned char *p, u64 v){
if( v<=0x7f ){
p[0] = v&0x7f;
return 1;
}
if( v<=0x3fff ){
p[0] = ((v>>7)&0x7f)|0x80;
p[1] = v&0x7f;
return 2;
}
return putVarint64(p,v);
}
#define SLOT_2_0 0x001fc07f
#define SLOT_4_2_0 0xf01fc07f
u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
u32 a,b,s;
if( ((signed char*)p)[0]>=0 ){
*v = *p;
return 1;
}
if( ((signed char*)p)[1]>=0 ){
*v = ((u32)(p[0]&0x7f)<<7) | p[1];
return 2;
}
assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
a = ((u32)p[0])<<14;
b = p[1];
p += 2;
a |= *p;
if (!(a&0x80))
{
a &= SLOT_2_0;
b &= 0x7f;
b = b<<7;
a |= b;
*v = a;
return 3;
}
a &= SLOT_2_0;
p++;
b = b<<14;
b |= *p;
if (!(b&0x80))
{
b &= SLOT_2_0;
a = a<<7;
a |= b;
*v = a;
return 4;
}
b &= SLOT_2_0;
s = a;
p++;
a = a<<14;
a |= *p;
if (!(a&0x80))
{
b = b<<7;
a |= b;
s = s>>18;
*v = ((u64)s)<<32 | a;
return 5;
}
s = s<<7;
s |= b;
p++;
b = b<<14;
b |= *p;
if (!(b&0x80))
{
a &= SLOT_2_0;
a = a<<7;
a |= b;
s = s>>18;
*v = ((u64)s)<<32 | a;
return 6;
}
p++;
a = a<<14;
a |= *p;
if (!(a&0x80))
{
a &= SLOT_4_2_0;
b &= SLOT_2_0;
b = b<<7;
a |= b;
s = s>>11;
*v = ((u64)s)<<32 | a;
return 7;
}
a &= SLOT_2_0;
p++;
b = b<<14;
b |= *p;
if (!(b&0x80))
{
b &= SLOT_4_2_0;
a = a<<7;
a |= b;
s = s>>4;
*v = ((u64)s)<<32 | a;
return 8;
}
p++;
a = a<<15;
a |= *p;
b &= SLOT_2_0;
b = b<<8;
a |= b;
s = s<<4;
b = p[-4];
b &= 0x7f;
b = b>>3;
s |= b;
*v = ((u64)s)<<32 | a;
return 9;
}
u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
u32 a,b;
a = *p;
#ifndef getVarint32
if (!(a&0x80))
{
*v = a;
return 1;
}
#endif
p++;
b = *p;
if (!(b&0x80))
{
a &= 0x7f;
a = a<<7;
*v = a | b;
return 2;
}
p++;
a = a<<14;
a |= *p;
if (!(a&0x80))
{
a &= (0x7f<<14)|(0x7f);
b &= 0x7f;
b = b<<7;
*v = a | b;
return 3;
}
#if 1
{
u64 v64;
u8 n;
n = sqlite3GetVarint(p-2, &v64);
assert( n>3 && n<=9 );
if( (v64 & SQLITE_MAX_U32)!=v64 ){
*v = 0xffffffff;
}else{
*v = (u32)v64;
}
return n;
}
#else#endif
}
int sqlite3VarintLen(u64 v){
int i;
for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
return i;
}
u32 sqlite3Get4byte(const u8 *p){
#if SQLITE_BYTEORDER==4321
u32 x;
memcpy(&x,p,4);
return x;
#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
u32 x;
memcpy(&x,p,4);
return __builtin_bswap32(x);
#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
u32 x;
memcpy(&x,p,4);
return _byteswap_ulong(x);
#else
testcase( p[0]&0x80 );
return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
#endif
}
void sqlite3Put4byte(unsigned char *p, u32 v){
#if SQLITE_BYTEORDER==4321
memcpy(p,&v,4);
#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
u32 x = __builtin_bswap32(v);
memcpy(p,&x,4);
#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
u32 x = _byteswap_ulong(v);
memcpy(p,&x,4);
#else
p[0] = (u8)(v>>24);
p[1] = (u8)(v>>16);
p[2] = (u8)(v>>8);
p[3] = (u8)v;
#endif
}
u8 sqlite3HexToInt(int h){
assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
#ifdef SQLITE_ASCII
h += 9*(1&(h>>6));
#endif
#ifdef SQLITE_EBCDIC
h += 9*(1&~(h>>4));
#endif
return (u8)(h & 0xf);
}
#if !defined(SQLITE_OMIT_BLOB_LITERAL)
void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
char *zBlob;
int i;
zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
n--;
if( zBlob ){
for(i=0; i<n; i+=2){
zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
}
zBlob[i/2] = 0;
}
return zBlob;
}
#endif
static void logBadConnection(const char *zType){
sqlite3_log(SQLITE_MISUSE,
"API call with %s database connection pointer",
zType
);
}
int sqlite3SafetyCheckOk(sqlite3 *db){
u8 eOpenState;
if( db==0 ){
logBadConnection("NULL");
return 0;
}
eOpenState = db->eOpenState;
if( eOpenState!=SQLITE_STATE_OPEN ){
if( sqlite3SafetyCheckSickOrOk(db) ){
testcase( sqlite3GlobalConfig.xLog!=0 );
logBadConnection("unopened");
}
return 0;
}else{
return 1;
}
}
int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
u8 eOpenState;
eOpenState = db->eOpenState;
if( eOpenState!=SQLITE_STATE_SICK &&
eOpenState!=SQLITE_STATE_OPEN &&
eOpenState!=SQLITE_STATE_BUSY ){
testcase( sqlite3GlobalConfig.xLog!=0 );
logBadConnection("invalid");
return 0;
}else{
return 1;
}
}
int sqlite3AddInt64(i64 *pA, i64 iB){
#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
return __builtin_add_overflow(*pA, iB, pA);
#else
i64 iA = *pA;
testcase( iA==0 ); testcase( iA==1 );
testcase( iB==-1 ); testcase( iB==0 );
if( iB>=0 ){
testcase( iA>0 && LARGEST_INT64 - iA == iB );
testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
}else{
testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
}
*pA += iB;
return 0;
#endif
}
int sqlite3SubInt64(i64 *pA, i64 iB){
#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
return __builtin_sub_overflow(*pA, iB, pA);
#else
testcase( iB==SMALLEST_INT64+1 );
if( iB==SMALLEST_INT64 ){
testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
if( (*pA)>=0 ) return 1;
*pA -= iB;
return 0;
}else{
return sqlite3AddInt64(pA, -iB);
}
#endif
}
int sqlite3MulInt64(i64 *pA, i64 iB){
#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
return __builtin_mul_overflow(*pA, iB, pA);
#else
i64 iA = *pA;
if( iB>0 ){
if( iA>LARGEST_INT64/iB ) return 1;
if( iA<SMALLEST_INT64/iB ) return 1;
}else if( iB<0 ){
if( iA>0 ){
if( iB<SMALLEST_INT64/iA ) return 1;
}else if( iA<0 ){
if( iB==SMALLEST_INT64 ) return 1;
if( iA==SMALLEST_INT64 ) return 1;
if( -iA>LARGEST_INT64/-iB ) return 1;
}
}
*pA = iA*iB;
return 0;
#endif
}
int sqlite3AbsInt32(int x){
if( x>=0 ) return x;
if( x==(int)0x80000000 ) return 0x7fffffff;
return -x;
}
#ifdef SQLITE_ENABLE_8_3_NAMES
void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
#if SQLITE_ENABLE_8_3_NAMES<2
if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
#endif
{
int i, sz;
sz = sqlite3Strlen30(z);
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
}
}
#endif
LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
static const unsigned char x[] = {
10, 10,
9, 9,
8, 8,
7, 7, 7,
6, 6, 6,
5, 5, 5,
4, 4, 4, 4,
3, 3, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2,
};
if( a>=b ){
if( a>b+49 ) return a;
if( a>b+31 ) return a+1;
return a+x[a-b];
}else{
if( b>a+49 ) return b;
if( b>a+31 ) return b+1;
return b+x[b-a];
}
}
LogEst sqlite3LogEst(u64 x){
static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
LogEst y = 40;
if( x<8 ){
if( x<2 ) return 0;
while( x<8 ){ y -= 10; x <<= 1; }
}else{
#if GCC_VERSION>=5004000
int i = 60 - __builtin_clzll(x);
y += i*10;
x >>= i;
#else
while( x>255 ){ y += 40; x >>= 4; }
while( x>15 ){ y += 10; x >>= 1; }
#endif
}
return a[x&7] + y - 10;
}
LogEst sqlite3LogEstFromDouble(double x){
u64 a;
LogEst e;
assert( sizeof(x)==8 && sizeof(a)==8 );
if( x<=1 ) return 0;
if( x<=2000000000 ) return sqlite3LogEst((u64)x);
memcpy(&a, &x, 8);
e = (a>>52) - 1022;
return e*10;
}
u64 sqlite3LogEstToInt(LogEst x){
u64 n;
n = x%10;
x /= 10;
if( n>=5 ) n -= 2;
else if( n>=1 ) n -= 1;
if( x>60 ) return (u64)LARGEST_INT64;
return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
}
VList *sqlite3VListAdd(
sqlite3 *db,
VList *pIn,
const char *zName,
int nName,
int iVal
){
int nInt;
char *z;
int i;
nInt = nName/4 + 3;
assert( pIn==0 || pIn[0]>=3 );
if( pIn==0 || pIn[1]+nInt > pIn[0] ){
sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
if( pOut==0 ) return pIn;
if( pIn==0 ) pOut[1] = 2;
pIn = pOut;
pIn[0] = nAlloc;
}
i = pIn[1];
pIn[i] = iVal;
pIn[i+1] = nInt;
z = (char*)&pIn[i+2];
pIn[1] = i+nInt;
assert( pIn[1]<=pIn[0] );
memcpy(z, zName, nName);
z[nName] = 0;
return pIn;
}
const char *sqlite3VListNumToName(VList *pIn, int iVal){
int i, mx;
if( pIn==0 ) return 0;
mx = pIn[1];
i = 2;
do{
if( pIn[i]==iVal ) return (char*)&pIn[i+2];
i += pIn[i+1];
}while( i<mx );
return 0;
}
int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
int i, mx;
if( pIn==0 ) return 0;
mx = pIn[1];
i = 2;
do{
const char *z = (const char*)&pIn[i+2];
if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
i += pIn[i+1];
}while( i<mx );
return 0;
}
#if defined(VDBE_PROFILE) \
|| defined(SQLITE_PERFORMANCE_TRACE) \
|| defined(SQLITE_ENABLE_STMT_SCANSTATUS)
# include "hwtime.h"
#endif