| /* |
| ** 2001 September 15 |
| ** |
| ** The author disclaims copyright to this source code. In place of |
| ** a legal notice, here is a blessing: |
| ** |
| ** May you do good and not evil. |
| ** May you find forgiveness for yourself and forgive others. |
| ** May you share freely, never taking more than you give. |
| ** |
| ************************************************************************* |
| ** Utility functions used throughout sqlite. |
| ** |
| ** This file contains functions for allocating memory, comparing |
| ** strings, and stuff like that. |
| ** |
| */ |
| #include "sqliteInt.h" |
| #include <stdarg.h> |
| #ifndef SQLITE_OMIT_FLOATING_POINT |
| #include <math.h> |
| #endif |
| |
| /* |
| ** Calls to sqlite3FaultSim() are used to simulate a failure during testing, |
| ** or to bypass normal error detection during testing in order to let |
| ** execute proceed further downstream. |
| ** |
| ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The |
| ** sqlite3FaultSim() function only returns non-zero during testing. |
| ** |
| ** During testing, if the test harness has set a fault-sim callback using |
| ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then |
| ** each call to sqlite3FaultSim() is relayed to that application-supplied |
| ** callback and the integer return value form the application-supplied |
| ** callback is returned by sqlite3FaultSim(). |
| ** |
| ** The integer argument to sqlite3FaultSim() is a code to identify which |
| ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() |
| ** should have a unique code. To prevent legacy testing applications from |
| ** breaking, the codes should not be changed or reused. |
| */ |
| #ifndef SQLITE_UNTESTABLE |
| int sqlite3FaultSim(int iTest){ |
| int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
| return xCallback ? xCallback(iTest) : SQLITE_OK; |
| } |
| #endif |
| |
| #ifndef SQLITE_OMIT_FLOATING_POINT |
| /* |
| ** Return true if the floating point value is Not a Number (NaN). |
| ** |
| ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
| ** Otherwise, we have our own implementation that works on most systems. |
| */ |
| int sqlite3IsNaN(double x){ |
| int rc; /* The value return */ |
| #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN |
| u64 y; |
| memcpy(&y,&x,sizeof(y)); |
| rc = IsNaN(y); |
| #else |
| rc = isnan(x); |
| #endif /* HAVE_ISNAN */ |
| testcase( rc ); |
| return rc; |
| } |
| #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| |
| #ifndef SQLITE_OMIT_FLOATING_POINT |
| /* |
| ** Return true if the floating point value is NaN or +Inf or -Inf. |
| */ |
| int sqlite3IsOverflow(double x){ |
| int rc; /* The value return */ |
| u64 y; |
| memcpy(&y,&x,sizeof(y)); |
| rc = IsOvfl(y); |
| return rc; |
| } |
| #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| |
| /* |
| ** Compute a string length that is limited to what can be stored in |
| ** lower 30 bits of a 32-bit signed integer. |
| ** |
| ** The value returned will never be negative. Nor will it ever be greater |
| ** than the actual length of the string. For very long strings (greater |
| ** than 1GiB) the value returned might be less than the true string length. |
| */ |
| int sqlite3Strlen30(const char *z){ |
| if( z==0 ) return 0; |
| return 0x3fffffff & (int)strlen(z); |
| } |
| |
| /* |
| ** Return the declared type of a column. Or return zDflt if the column |
| ** has no declared type. |
| ** |
| ** The column type is an extra string stored after the zero-terminator on |
| ** the column name if and only if the COLFLAG_HASTYPE flag is set. |
| */ |
| 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; |
| } |
| } |
| |
| /* |
| ** Helper function for sqlite3Error() - called rarely. Broken out into |
| ** a separate routine to avoid unnecessary register saves on entry to |
| ** sqlite3Error(). |
| */ |
| static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ |
| if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| sqlite3SystemError(db, err_code); |
| } |
| |
| /* |
| ** Set the current error code to err_code and clear any prior error message. |
| ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates |
| ** that would be appropriate. |
| */ |
| 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; |
| } |
| } |
| |
| /* |
| ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state |
| ** and error message. |
| */ |
| void sqlite3ErrorClear(sqlite3 *db){ |
| assert( db!=0 ); |
| db->errCode = SQLITE_OK; |
| db->errByteOffset = -1; |
| if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| } |
| |
| /* |
| ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| ** to do based on the SQLite error code in rc. |
| */ |
| void sqlite3SystemError(sqlite3 *db, int rc){ |
| if( rc==SQLITE_IOERR_NOMEM ) return; |
| #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) |
| if( rc==SQLITE_IOERR_IN_PAGE ){ |
| int ii; |
| int iErr; |
| sqlite3BtreeEnterAll(db); |
| for(ii=0; ii<db->nDb; ii++){ |
| if( db->aDb[ii].pBt ){ |
| iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); |
| if( iErr ){ |
| db->iSysErrno = iErr; |
| } |
| } |
| } |
| sqlite3BtreeLeaveAll(db); |
| return; |
| } |
| #endif |
| rc &= 0xff; |
| if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ |
| db->iSysErrno = sqlite3OsGetLastError(db->pVfs); |
| } |
| } |
| |
| /* |
| ** Set the most recent error code and error string for the sqlite |
| ** handle "db". The error code is set to "err_code". |
| ** |
| ** If it is not NULL, string zFormat specifies the format of the |
| ** error string. zFormat and any string tokens that follow it are |
| ** assumed to be encoded in UTF-8. |
| ** |
| ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| ** should be called with err_code set to SQLITE_OK and zFormat set |
| ** to NULL. |
| */ |
| 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); |
| } |
| } |
| |
| /* |
| ** Check for interrupts and invoke progress callback. |
| */ |
| 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 ){ |
| if( p->rc==SQLITE_INTERRUPT ){ |
| p->nProgressSteps = 0; |
| }else if( (++p->nProgressSteps)>=db->nProgressOps ){ |
| if( db->xProgress(db->pProgressArg) ){ |
| p->nErr++; |
| p->rc = SQLITE_INTERRUPT; |
| } |
| p->nProgressSteps = 0; |
| } |
| } |
| #endif |
| } |
| |
| /* |
| ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| ** |
| ** This function should be used to report any error that occurs while |
| ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| ** last thing the sqlite3_prepare() function does is copy the error |
| ** stored by this function into the database handle using sqlite3Error(). |
| ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used |
| ** during statement execution (sqlite3_step() etc.). |
| */ |
| 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; |
| } |
| } |
| |
| /* |
| ** If database connection db is currently parsing SQL, then transfer |
| ** error code errCode to that parser if the parser has not already |
| ** encountered some other kind of error. |
| */ |
| int sqlite3ErrorToParser(sqlite3 *db, int errCode){ |
| Parse *pParse; |
| if( db==0 || (pParse = db->pParse)==0 ) return errCode; |
| pParse->rc = errCode; |
| pParse->nErr++; |
| return errCode; |
| } |
| |
| /* |
| ** Convert an SQL-style quoted string into a normal string by removing |
| ** the quote characters. The conversion is done in-place. If the |
| ** input does not begin with a quote character, then this routine |
| ** is a no-op. |
| ** |
| ** The input string must be zero-terminated. A new zero-terminator |
| ** is added to the dequoted string. |
| ** |
| ** The return value is -1 if no dequoting occurs or the length of the |
| ** dequoted string, exclusive of the zero terminator, if dequoting does |
| ** occur. |
| ** |
| ** 2002-02-14: This routine is extended to remove MS-Access style |
| ** brackets from around identifiers. For example: "[a-b-c]" becomes |
| ** "a-b-c". |
| */ |
| 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); |
| } |
| |
| /* |
| ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken |
| ** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those |
| ** that contain '_' characters that must be removed before further processing. |
| */ |
| void sqlite3DequoteNumber(Parse *pParse, Expr *p){ |
| assert( p!=0 || pParse->db->mallocFailed ); |
| if( p ){ |
| const char *pIn = p->u.zToken; |
| char *pOut = p->u.zToken; |
| int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X')); |
| int iValue; |
| assert( p->op==TK_QNUMBER ); |
| p->op = TK_INTEGER; |
| do { |
| if( *pIn!=SQLITE_DIGIT_SEPARATOR ){ |
| *pOut++ = *pIn; |
| if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; |
| }else{ |
| if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) |
| || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) |
| ){ |
| sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); |
| } |
| } |
| }while( *pIn++ ); |
| if( bHex ) p->op = TK_INTEGER; |
| |
| /* tag-20240227-a: If after dequoting, the number is an integer that |
| ** fits in 32 bits, then it must be converted into EP_IntValue. Other |
| ** parts of the code expect this. See also tag-20240227-b. */ |
| if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ |
| p->u.iValue = iValue; |
| p->flags |= EP_IntValue; |
| } |
| } |
| } |
| |
| /* |
| ** If the input token p is quoted, try to adjust the token to remove |
| ** the quotes. This is not always possible: |
| ** |
| ** "abc" -> abc |
| ** "ab""cd" -> (not possible because of the interior "") |
| ** |
| ** Remove the quotes if possible. This is a optimization. The overall |
| ** system should still return the correct answer even if this routine |
| ** is always a no-op. |
| */ |
| 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++; |
| } |
| |
| /* |
| ** Generate a Token object from a string |
| */ |
| void sqlite3TokenInit(Token *p, char *z){ |
| p->z = z; |
| p->n = sqlite3Strlen30(z); |
| } |
| |
| /* Convenient short-hand */ |
| #define UpperToLower sqlite3UpperToLower |
| |
| /* |
| ** Some systems have stricmp(). Others have strcasecmp(). Because |
| ** there is no consistency, we will define our own. |
| ** |
| ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
| ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
| ** the contents of two buffers containing UTF-8 strings in a |
| ** case-independent fashion, using the same definition of "case |
| ** independence" that SQLite uses internally when comparing identifiers. |
| */ |
| 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]; |
| } |
| |
| /* |
| ** Compute an 8-bit hash on a string that is insensitive to case differences |
| */ |
| 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; |
| } |
| |
| /* |
| ** Two inputs are multiplied to get a 128-bit result. Write the |
| ** lower 64-bits of the result into *pLo, and return the high-order |
| ** 64 bits. |
| */ |
| static u64 sqlite3Multiply128(u64 a, u64 b, u64 *pLo){ |
| #if (defined(__GNUC__) || defined(__clang__)) \ |
| && (defined(__x86_64__) || defined(__aarch64__) || defined(__riscv)) |
| __uint128_t r = (__uint128_t)a * b; |
| *pLo = (u64)r; |
| return (u64)(r>>64); |
| #elif defined(_MSC_VER) && defined(_M_X64) |
| *pLo = a*b; |
| return __umulh(a, b); |
| #else |
| u64 a0 = (u32)a; |
| u64 a1 = a >> 32; |
| u64 b0 = (u32)b; |
| u64 b1 = b >> 32; |
| u64 a0b0 = a0 * b0; |
| u64 a1b1 = a1 * b1; |
| u64 a0b1 = a0 * b1; |
| u64 a1b0 = a1 * b0; |
| u64 t = (a0b0 >> 32) + (u32)a0b1 + (u32)a1b0; |
| *pLo = (a0b0 & UINT64_C(0xffffffff)) | (t << 32); |
| return a1b1 + (a0b1>>32) + (a1b0>>32) + (t>>32); |
| #endif |
| } |
| |
| /* |
| ** A is an unsigned 96-bit integer formed by (a<<32)+aLo. |
| ** B is an unsigned 64-bit integer. |
| ** |
| ** Compute the upper 96 bits of 160-bit result of A*B. |
| ** |
| ** Write ((A*B)>>64 & 0xffffffff) (the middle 32 bits of A*B) |
| ** into *pLo. Return the upper 64 bits of A*B. |
| ** |
| ** The lower 64 bits of A*B are discarded. |
| */ |
| static u64 sqlite3Multiply160(u64 a, u32 aLo, u64 b, u32 *pLo){ |
| #if (defined(__GNUC__) || defined(__clang__)) \ |
| && (defined(__x86_64__) || defined(__aarch64__) || defined(__riscv)) |
| __uint128_t r = (__uint128_t)a * b; |
| r += ((__uint128_t)aLo * b) >> 32; |
| *pLo = (r>>32)&0xffffffff; |
| return r>>64; |
| #elif defined(_MSC_VER) && defined(_M_X64) |
| u64 r1_hi = __umulh(a,b); |
| u64 r1_lo = a*b; |
| u64 r2 = (__umulh((u64)aLo,b)<<32) + ((aLo*b)>>32); |
| r1_hi += _addcarry_u64(0, r1_lo, r2, &r1_lo); |
| *pLo = r1_lo>>32; |
| return r1_hi; |
| #else |
| u64 x2 = a>>32; |
| u64 x1 = a&0xffffffff; |
| u64 x0 = aLo; |
| u64 y1 = b>>32; |
| u64 y0 = b&0xffffffff; |
| u64 x2y1 = x2*y1; |
| u64 r4 = x2y1>>32; |
| u64 x2y0 = x2*y0; |
| u64 x1y1 = x1*y1; |
| u64 r3 = (x2y1 & 0xffffffff) + (x2y0 >>32) + (x1y1 >>32); |
| u64 x1y0 = x1*y0; |
| u64 x0y1 = x0*y1; |
| u64 r2 = (x2y0 & 0xffffffff) + (x1y1 & 0xffffffff) + |
| (x1y0 >>32) + (x0y1>>32); |
| u64 x0y0 = x0*y0; |
| u64 r1 = (x1y0 & 0xffffffff) + (x0y1 & 0xffffffff) + |
| (x0y0 >>32); |
| r2 += r1>>32; |
| r3 += r2>>32; |
| *pLo = r2&0xffffffff; |
| return (r4<<32) + r3; |
| #endif |
| } |
| |
| /* |
| ** Return a u64 with the N-th bit set. |
| */ |
| #define U64_BIT(N) (((u64)1)<<(N)) |
| |
| /* |
| ** Range of powers of 10 that we need to deal with when converting |
| ** IEEE754 doubles to and from decimal. |
| */ |
| #define POWERSOF10_FIRST (-348) |
| #define POWERSOF10_LAST (+347) |
| |
| /* |
| ** For any p between -348 and +347, return the integer part of |
| ** |
| ** pow(10,p) * pow(2,63-pow10to2(p)) |
| ** |
| ** Or, in other words, for any p in range, return the most significant |
| ** 64 bits of pow(10,p). The pow(10,p) value is shifted left or right, |
| ** as appropriate so the most significant 64 bits fit exactly into a |
| ** 64-bit unsigned integer. |
| ** |
| ** Write into *pLo the next 32 significant bits of the answer after |
| ** the first 64. |
| ** |
| ** Algorithm: |
| ** |
| ** (1) For p between 0 and 26, return the value directly from the aBase[] |
| ** lookup table. |
| ** |
| ** (2) For p outside the range 0 to 26, use aScale[] for the initial value |
| ** then refine that result (if necessary) by a single multiplication |
| ** against aBase[]. |
| ** |
| ** The constant tables aBase[], aScale[], and aScaleLo[] are generated |
| ** by the C program at ../tool/mkfptab.c run with the --round option. |
| */ |
| static u64 powerOfTen(int p, u32 *pLo){ |
| static const u64 aBase[] = { |
| UINT64_C(0x8000000000000000), /* 0: 1.0e+0 << 63 */ |
| UINT64_C(0xa000000000000000), /* 1: 1.0e+1 << 60 */ |
| UINT64_C(0xc800000000000000), /* 2: 1.0e+2 << 57 */ |
| UINT64_C(0xfa00000000000000), /* 3: 1.0e+3 << 54 */ |
| UINT64_C(0x9c40000000000000), /* 4: 1.0e+4 << 50 */ |
| UINT64_C(0xc350000000000000), /* 5: 1.0e+5 << 47 */ |
| UINT64_C(0xf424000000000000), /* 6: 1.0e+6 << 44 */ |
| UINT64_C(0x9896800000000000), /* 7: 1.0e+7 << 40 */ |
| UINT64_C(0xbebc200000000000), /* 8: 1.0e+8 << 37 */ |
| UINT64_C(0xee6b280000000000), /* 9: 1.0e+9 << 34 */ |
| UINT64_C(0x9502f90000000000), /* 10: 1.0e+10 << 30 */ |
| UINT64_C(0xba43b74000000000), /* 11: 1.0e+11 << 27 */ |
| UINT64_C(0xe8d4a51000000000), /* 12: 1.0e+12 << 24 */ |
| UINT64_C(0x9184e72a00000000), /* 13: 1.0e+13 << 20 */ |
| UINT64_C(0xb5e620f480000000), /* 14: 1.0e+14 << 17 */ |
| UINT64_C(0xe35fa931a0000000), /* 15: 1.0e+15 << 14 */ |
| UINT64_C(0x8e1bc9bf04000000), /* 16: 1.0e+16 << 10 */ |
| UINT64_C(0xb1a2bc2ec5000000), /* 17: 1.0e+17 << 7 */ |
| UINT64_C(0xde0b6b3a76400000), /* 18: 1.0e+18 << 4 */ |
| UINT64_C(0x8ac7230489e80000), /* 19: 1.0e+19 >> 0 */ |
| UINT64_C(0xad78ebc5ac620000), /* 20: 1.0e+20 >> 3 */ |
| UINT64_C(0xd8d726b7177a8000), /* 21: 1.0e+21 >> 6 */ |
| UINT64_C(0x878678326eac9000), /* 22: 1.0e+22 >> 10 */ |
| UINT64_C(0xa968163f0a57b400), /* 23: 1.0e+23 >> 13 */ |
| UINT64_C(0xd3c21bcecceda100), /* 24: 1.0e+24 >> 16 */ |
| UINT64_C(0x84595161401484a0), /* 25: 1.0e+25 >> 20 */ |
| UINT64_C(0xa56fa5b99019a5c8), /* 26: 1.0e+26 >> 23 */ |
| }; |
| static const u64 aScale[] = { |
| UINT64_C(0x8049a4ac0c5811ae), /* 0: 1.0e-351 << 1229 */ |
| UINT64_C(0xcf42894a5dce35ea), /* 1: 1.0e-324 << 1140 */ |
| UINT64_C(0xa76c582338ed2621), /* 2: 1.0e-297 << 1050 */ |
| UINT64_C(0x873e4f75e2224e68), /* 3: 1.0e-270 << 960 */ |
| UINT64_C(0xda7f5bf590966848), /* 4: 1.0e-243 << 871 */ |
| UINT64_C(0xb080392cc4349dec), /* 5: 1.0e-216 << 781 */ |
| UINT64_C(0x8e938662882af53e), /* 6: 1.0e-189 << 691 */ |
| UINT64_C(0xe65829b3046b0afa), /* 7: 1.0e-162 << 602 */ |
| UINT64_C(0xba121a4650e4ddeb), /* 8: 1.0e-135 << 512 */ |
| UINT64_C(0x964e858c91ba2655), /* 9: 1.0e-108 << 422 */ |
| UINT64_C(0xf2d56790ab41c2a2), /* 10: 1.0e-81 << 333 */ |
| UINT64_C(0xc428d05aa4751e4c), /* 11: 1.0e-54 << 243 */ |
| UINT64_C(0x9e74d1b791e07e48), /* 12: 1.0e-27 << 153 */ |
| UINT64_C(0xcccccccccccccccc), /* 13: 1.0e-1 << 67 (special case) */ |
| UINT64_C(0xcecb8f27f4200f3a), /* 14: 1.0e+27 >> 26 */ |
| UINT64_C(0xa70c3c40a64e6c51), /* 15: 1.0e+54 >> 116 */ |
| UINT64_C(0x86f0ac99b4e8dafd), /* 16: 1.0e+81 >> 206 */ |
| UINT64_C(0xda01ee641a708de9), /* 17: 1.0e+108 >> 295 */ |
| UINT64_C(0xb01ae745b101e9e4), /* 18: 1.0e+135 >> 385 */ |
| UINT64_C(0x8e41ade9fbebc27d), /* 19: 1.0e+162 >> 475 */ |
| UINT64_C(0xe5d3ef282a242e81), /* 20: 1.0e+189 >> 564 */ |
| UINT64_C(0xb9a74a0637ce2ee1), /* 21: 1.0e+216 >> 654 */ |
| UINT64_C(0x95f83d0a1fb69cd9), /* 22: 1.0e+243 >> 744 */ |
| UINT64_C(0xf24a01a73cf2dccf), /* 23: 1.0e+270 >> 833 */ |
| UINT64_C(0xc3b8358109e84f07), /* 24: 1.0e+297 >> 923 */ |
| UINT64_C(0x9e19db92b4e31ba9), /* 25: 1.0e+324 >> 1013 */ |
| }; |
| static const unsigned int aScaleLo[] = { |
| 0x205b896d, /* 0: 1.0e-351 << 1229 */ |
| 0x52064cad, /* 1: 1.0e-324 << 1140 */ |
| 0xaf2af2b8, /* 2: 1.0e-297 << 1050 */ |
| 0x5a7744a7, /* 3: 1.0e-270 << 960 */ |
| 0xaf39a475, /* 4: 1.0e-243 << 871 */ |
| 0xbd8d794e, /* 5: 1.0e-216 << 781 */ |
| 0x547eb47b, /* 6: 1.0e-189 << 691 */ |
| 0x0cb4a5a3, /* 7: 1.0e-162 << 602 */ |
| 0x92f34d62, /* 8: 1.0e-135 << 512 */ |
| 0x3a6a07f9, /* 9: 1.0e-108 << 422 */ |
| 0xfae27299, /* 10: 1.0e-81 << 333 */ |
| 0xaa97e14c, /* 11: 1.0e-54 << 243 */ |
| 0x775ea265, /* 12: 1.0e-27 << 153 */ |
| 0xcccccccc, /* 13: 1.0e-1 << 67 (special case) */ |
| 0x00000000, /* 14: 1.0e+27 >> 26 */ |
| 0x999090b6, /* 15: 1.0e+54 >> 116 */ |
| 0x69a028bb, /* 16: 1.0e+81 >> 206 */ |
| 0xe80e6f48, /* 17: 1.0e+108 >> 295 */ |
| 0x5ec05dd0, /* 18: 1.0e+135 >> 385 */ |
| 0x14588f14, /* 19: 1.0e+162 >> 475 */ |
| 0x8f1668c9, /* 20: 1.0e+189 >> 564 */ |
| 0x6d953e2c, /* 21: 1.0e+216 >> 654 */ |
| 0x4abdaf10, /* 22: 1.0e+243 >> 744 */ |
| 0xbc633b39, /* 23: 1.0e+270 >> 833 */ |
| 0x0a862f81, /* 24: 1.0e+297 >> 923 */ |
| 0x6c07a2c2, /* 25: 1.0e+324 >> 1013 */ |
| }; |
| int g, n; |
| u64 s, x; |
| u32 lo; |
| |
| assert( p>=POWERSOF10_FIRST && p<=POWERSOF10_LAST ); |
| if( p<0 ){ |
| if( p==(-1) ){ |
| *pLo = aScaleLo[13]; |
| return aScale[13]; |
| } |
| g = p/27; |
| n = p%27; |
| if( n ){ |
| g--; |
| n += 27; |
| } |
| }else if( p<27 ){ |
| *pLo = 0; |
| return aBase[p]; |
| }else{ |
| g = p/27; |
| n = p%27; |
| } |
| s = aScale[g+13]; |
| if( n==0 ){ |
| *pLo = aScaleLo[g+13]; |
| return s; |
| } |
| x = sqlite3Multiply160(s,aScaleLo[g+13],aBase[n],&lo); |
| if( (U64_BIT(63) & x)==0 ){ |
| x = x<<1 | ((lo>>31)&1); |
| lo = (lo<<1) | 1; |
| } |
| *pLo = lo; |
| return x; |
| } |
| |
| /* |
| ** pow10to2(x) computes floor(log2(pow(10,x))). |
| ** pow2to10(y) computes floor(log10(pow(2,y))). |
| ** |
| ** Conceptually, pow10to2(p) converts a base-10 exponent p into |
| ** a corresponding base-2 exponent, and pow2to10(e) converts a base-2 |
| ** exponent into a base-10 exponent. |
| ** |
| ** The conversions are based on the observation that: |
| ** |
| ** ln(10.0)/ln(2.0) == 108853/32768 (approximately) |
| ** ln(2.0)/ln(10.0) == 78913/262144 (approximately) |
| ** |
| ** These ratios are approximate, but they are accurate to 5 digits, |
| ** which is close enough for the usage here. Right-shift is used |
| ** for division so that rounding of negative numbers happens in the |
| ** right direction. |
| */ |
| static int pwr10to2(int p){ return (p*108853) >> 15; } |
| static int pwr2to10(int p){ return (p*78913) >> 18; } |
| |
| /* |
| ** Count leading zeros for a 64-bit unsigned integer. |
| */ |
| static int countLeadingZeros(u64 m){ |
| #if defined(__GNUC__) || defined(__clang__) |
| return __builtin_clzll(m); |
| #else |
| int n = 0; |
| if( m <= 0x00000000ffffffffULL) { n += 32; m <<= 32; } |
| if( m <= 0x0000ffffffffffffULL) { n += 16; m <<= 16; } |
| if( m <= 0x00ffffffffffffffULL) { n += 8; m <<= 8; } |
| if( m <= 0x0fffffffffffffffULL) { n += 4; m <<= 4; } |
| if( m <= 0x3fffffffffffffffULL) { n += 2; m <<= 2; } |
| if( m <= 0x7fffffffffffffffULL) { n += 1; } |
| return n; |
| #endif |
| } |
| |
| /* |
| ** Given m and e, which represent a quantity r == m*pow(2,e), |
| ** return values *pD and *pP such that r == (*pD)*pow(10,*pP), |
| ** approximately. *pD should contain at least n significant digits. |
| ** |
| ** The input m is required to have its highest bit set. In other words, |
| ** m should be left-shifted, and e decremented, to maximize the value of m. |
| */ |
| static void sqlite3Fp2Convert10(u64 m, int e, int n, u64 *pD, int *pP){ |
| int p; |
| u64 h, d1; |
| u32 d2; |
| assert( n>=1 && n<=18 ); |
| p = n - 1 - pwr2to10(e+63); |
| h = sqlite3Multiply128(m, powerOfTen(p,&d2), &d1); |
| assert( -(e + pwr10to2(p) + 2) >= 0 ); |
| assert( -(e + pwr10to2(p) + 1) <= 63 ); |
| if( n==18 ){ |
| h >>= -(e + pwr10to2(p) + 2); |
| *pD = (h + ((h<<1)&2))>>1; |
| }else{ |
| *pD = h >> -(e + pwr10to2(p) + 1); |
| } |
| *pP = -p; |
| } |
| |
| /* |
| ** Return an IEEE754 floating point value that approximates d*pow(10,p). |
| */ |
| static double sqlite3Fp10Convert2(u64 d, int p){ |
| int b, lp, e, adj, s; |
| u32 pwr10l, mid1; |
| u64 pwr10h, x, hi, lo, sticky, u, m; |
| double r; |
| if( p<POWERSOF10_FIRST ) return 0.0; |
| if( p>POWERSOF10_LAST ) return INFINITY; |
| b = 64 - countLeadingZeros(d); |
| lp = pwr10to2(p); |
| e = 53 - b - lp; |
| if( e > 1074 ){ |
| if( e>=1130 ) return 0.0; |
| e = 1074; |
| } |
| s = -(e-(64-b) + lp + 3); |
| pwr10h = powerOfTen(p, &pwr10l); |
| if( pwr10l!=0 ){ |
| pwr10h++; |
| pwr10l = ~pwr10l; |
| } |
| x = d<<(64-b); |
| hi = sqlite3Multiply128(x,pwr10h,&lo); |
| mid1 = lo>>32; |
| sticky = 1; |
| if( (hi & (U64_BIT(s)-1))==0 ) { |
| u32 mid2 = sqlite3Multiply128(x,((u64)pwr10l)<<32,&lo)>>32; |
| sticky = (mid1-mid2 > 1); |
| hi -= mid1 < mid2; |
| } |
| u = (hi>>s) | sticky; |
| adj = (u >= U64_BIT(55)-2); |
| if( adj ){ |
| u = (u>>adj) | (u&1); |
| e -= adj; |
| } |
| m = (u + 1 + ((u>>2)&1)) >> 2; |
| if( e<=(-972) ) return INFINITY; |
| if((m & U64_BIT(52)) != 0){ |
| m = (m & ~U64_BIT(52)) | ((u64)(1075-e)<<52); |
| } |
| memcpy(&r,&m,8); |
| return r; |
| } |
| |
| /* |
| ** The string z[] is an text representation of a real number. |
| ** Convert this string to a double and write it into *pResult. |
| ** |
| ** z[] must be UTF-8 and zero-terminated. |
| ** |
| ** Return TRUE if the result is a valid real number (or integer) and FALSE |
| ** if the string is empty or contains extraneous text. More specifically |
| ** return |
| ** 1 => The input string is a pure integer |
| ** 2 or more => The input has a decimal point or eNNN clause |
| ** 0 or less => The input string is not a valid number |
| ** -1 => Not a valid number, but has a valid prefix which |
| ** includes a decimal point and/or an eNNN clause |
| ** |
| ** Valid numbers are in one of these formats: |
| ** |
| ** [+-]digits[E[+-]digits] |
| ** [+-]digits.[digits][E[+-]digits] |
| ** [+-].digits[E[+-]digits] |
| ** |
| ** Leading and trailing whitespace is ignored for the purpose of determining |
| ** validity. |
| ** |
| ** If some prefix of the input string is a valid number, this routine |
| ** returns FALSE but it still converts the prefix and writes the result |
| ** into *pResult. |
| */ |
| #if defined(_MSC_VER) |
| #pragma warning(disable : 4756) |
| #endif |
| int sqlite3AtoF(const char *z, double *pResult){ |
| #ifndef SQLITE_OMIT_FLOATING_POINT |
| /* sign * significand * (10 ^ (esign * exponent)) */ |
| int neg = 0; /* True for a negative value */ |
| u64 s = 0; /* mantissa */ |
| int d = 0; /* Value is s * pow(10,d) */ |
| int nDigit = 0; /* Number of digits processed */ |
| int eType = 1; /* 1: pure integer, 2+: fractional */ |
| |
| *pResult = 0.0; /* Default return value, in case of an error */ |
| |
| /* skip leading spaces */ |
| while( sqlite3Isspace(*z) ) z++; |
| |
| /* get sign of significand */ |
| if( *z=='-' ){ |
| neg = 1; |
| z++; |
| }else if( *z=='+' ){ |
| z++; |
| } |
| |
| /* copy max significant digits to significand */ |
| while( sqlite3Isdigit(*z) ){ |
| s = s*10 + (*z - '0'); |
| z++; nDigit++; |
| if( s>=((LARGEST_INT64-9)/10) ){ |
| /* skip non-significant significand digits |
| ** (increase exponent by d to shift decimal left) */ |
| while( sqlite3Isdigit(*z) ){ z++; d++; } |
| } |
| } |
| |
| /* if decimal point is present */ |
| if( *z=='.' ){ |
| z++; |
| eType++; |
| /* copy digits from after decimal to significand |
| ** (decrease exponent by d to shift decimal right) */ |
| while( sqlite3Isdigit(*z) ){ |
| if( s<((LARGEST_INT64-9)/10) ){ |
| s = s*10 + (*z - '0'); |
| d--; |
| nDigit++; |
| } |
| z++; |
| } |
| } |
| |
| /* if exponent is present */ |
| if( *z=='e' || *z=='E' ){ |
| int esign = 1; /* sign of exponent */ |
| z++; |
| eType++; |
| |
| /* get sign of exponent */ |
| if( *z=='-' ){ |
| esign = -1; |
| z++; |
| }else if( *z=='+' ){ |
| z++; |
| } |
| /* copy digits to exponent */ |
| if( sqlite3Isdigit(*z) ){ |
| int exp = *z - '0'; |
| z++; |
| while( sqlite3Isdigit(*z) ){ |
| exp = exp<10000 ? (exp*10 + (*z - '0')) : 10000; |
| z++; |
| } |
| d += esign*exp; |
| }else{ |
| eType = -1; |
| } |
| } |
| |
| /* skip trailing spaces */ |
| while( sqlite3Isspace(*z) ) z++; |
| |
| /* Zero is a special case */ |
| if( s==0 ){ |
| *pResult = neg ? -0.0 : +0.0; |
| }else{ |
| *pResult = sqlite3Fp10Convert2(s,d); |
| if( neg ) *pResult = -*pResult; |
| assert( !sqlite3IsNaN(*pResult) ); |
| } |
| |
| /* return true if number and no extra non-whitespace characters after */ |
| if( z[0]==0 && nDigit>0 ){ |
| return eType; |
| }else if( eType>=2 && nDigit>0 ){ |
| return -1; |
| }else{ |
| return 0; |
| } |
| #else |
| return !sqlite3Atoi64(z, pResult, strlen(z), SQLITE_UTF8); |
| #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| } |
| #if defined(_MSC_VER) |
| #pragma warning(default : 4756) |
| #endif |
| |
| /* |
| ** Digit pairs used to convert a U64 or I64 into text, two digits |
| ** at a time. |
| */ |
| static const union { |
| char a[201]; |
| short int forceAlignment; |
| } sqlite3DigitPairs = { |
| "00010203040506070809" |
| "10111213141516171819" |
| "20212223242526272829" |
| "30313233343536373839" |
| "40414243444546474849" |
| "50515253545556575859" |
| "60616263646566676869" |
| "70717273747576777879" |
| "80818283848586878889" |
| "90919293949596979899" |
| }; |
| |
| |
| /* |
| ** Render an signed 64-bit integer as text. Store the result in zOut[] and |
| ** return the length of the string that was stored, in bytes. The value |
| ** returned does not include the zero terminator at the end of the output |
| ** string. |
| ** |
| ** The caller must ensure that zOut[] is at least 21 bytes in size. |
| */ |
| int sqlite3Int64ToText(i64 v, char *zOut){ |
| int i; |
| u64 x; |
| union { |
| char a[23]; |
| u16 forceAlignment; |
| } u; |
| if( v>0 ){ |
| x = v; |
| }else if( v==0 ){ |
| zOut[0] = '0'; |
| zOut[1] = 0; |
| return 1; |
| }else{ |
| x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; |
| } |
| i = sizeof(u.a)-1; |
| u.a[i] = 0; |
| while( x>=10 ){ |
| int kk = (x%100)*2; |
| assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| assert( TWO_BYTE_ALIGNMENT(&u.a[i-2]) ); |
| *(u16*)(&u.a[i-2]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| i -= 2; |
| x /= 100; |
| } |
| if( x ){ |
| u.a[--i] = x + '0'; |
| } |
| if( v<0 ) u.a[--i] = '-'; |
| memcpy(zOut, &u.a[i], sizeof(u.a)-i); |
| return sizeof(u.a)-1-i; |
| } |
| |
| /* |
| ** Compare the 19-character string zNum against the text representation |
| ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| ** if zNum is less than, equal to, or greater than the string. |
| ** Note that zNum must contain exactly 19 characters. |
| ** |
| ** Unlike memcmp() this routine is guaranteed to return the difference |
| ** in the values of the last digit if the only difference is in the |
| ** last digit. So, for example, |
| ** |
| ** compare2pow63("9223372036854775800", 1) |
| ** |
| ** will return -8. |
| */ |
| static int compare2pow63(const char *zNum, int incr){ |
| int c = 0; |
| int i; |
| /* 012345678901234567 */ |
| 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; |
| } |
| |
| /* |
| ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This |
| ** routine does *not* accept hexadecimal notation. |
| ** |
| ** Returns: |
| ** |
| ** -1 Not even a prefix of the input text looks like an integer |
| ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| ** 1 Excess non-space text after the integer value |
| ** 2 Integer too large for a 64-bit signed integer or is malformed |
| ** 3 Special case of 9223372036854775808 |
| ** |
| ** length is the number of bytes in the string (bytes, not characters). |
| ** The string is not necessarily zero-terminated. The encoding is |
| ** given by enc. |
| */ |
| int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
| int incr; |
| u64 u = 0; |
| int neg = 0; /* assume positive */ |
| int i; |
| int c = 0; |
| int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ |
| int rc; /* Baseline return code */ |
| 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; } /* Skip leading zeros. */ |
| 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 ){ |
| /* This test and assignment is needed only to suppress UB warnings |
| ** from clang and -fsanitize=undefined. This test and assignment make |
| ** the code a little larger and slower, and no harm comes from omitting |
| ** them, but we must appease the undefined-behavior pharisees. */ |
| *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| }else if( neg ){ |
| *pNum = -(i64)u; |
| }else{ |
| *pNum = (i64)u; |
| } |
| rc = 0; |
| if( i==0 && zStart==zNum ){ /* No digits */ |
| rc = -1; |
| }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ |
| rc = 1; |
| }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */ |
| int jj = i; |
| do{ |
| if( !sqlite3Isspace(zNum[jj]) ){ |
| rc = 1; /* Extra non-space text after the integer */ |
| break; |
| } |
| jj += incr; |
| }while( &zNum[jj]<zEnd ); |
| } |
| if( i<19*incr ){ |
| /* Less than 19 digits, so we know that it fits in 64 bits */ |
| assert( u<=LARGEST_INT64 ); |
| return rc; |
| }else{ |
| /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
| c = i>19*incr ? 1 : compare2pow63(zNum, incr); |
| if( c<0 ){ |
| /* zNum is less than 9223372036854775808 so it fits */ |
| assert( u<=LARGEST_INT64 ); |
| return rc; |
| }else{ |
| *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| if( c>0 ){ |
| /* zNum is greater than 9223372036854775808 so it overflows */ |
| return 2; |
| }else{ |
| /* zNum is exactly 9223372036854775808. Fits if negative. The |
| ** special case 2 overflow if positive */ |
| assert( u-1==LARGEST_INT64 ); |
| return neg ? rc : 3; |
| } |
| } |
| } |
| } |
| |
| /* |
| ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, |
| ** into a 64-bit signed integer. This routine accepts hexadecimal literals, |
| ** whereas sqlite3Atoi64() does not. |
| ** |
| ** Returns: |
| ** |
| ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| ** 1 Excess text after the integer value |
| ** 2 Integer too large for a 64-bit signed integer or is malformed |
| ** 3 Special case of 9223372036854775808 |
| */ |
| 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 /* SQLITE_OMIT_HEX_INTEGER */ |
| { |
| int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); |
| if( z[n] ) n++; |
| return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8); |
| } |
| } |
| |
| /* |
| ** If zNum represents an integer that will fit in 32-bits, then set |
| ** *pValue to that integer and return true. Otherwise return false. |
| ** |
| ** This routine accepts both decimal and hexadecimal notation for integers. |
| ** |
| ** Any non-numeric characters that following zNum are ignored. |
| ** This is different from sqlite3Atoi64() which requires the |
| ** input number to be zero-terminated. |
| */ |
| 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; |
| } |
| |
| /* The longest decimal representation of a 32 bit integer is 10 digits: |
| ** |
| ** 1234567890 |
| ** 2^31 -> 2147483648 |
| */ |
| 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; |
| } |
| |
| /* |
| ** Return a 32-bit integer value extracted from a string. If the |
| ** string is not an integer, just return 0. |
| */ |
| int sqlite3Atoi(const char *z){ |
| int x = 0; |
| sqlite3GetInt32(z, &x); |
| return x; |
| } |
| |
| /* |
| ** Decode a floating-point value into an approximate decimal |
| ** representation. |
| ** |
| ** If iRound<=0 then round to -iRound significant digits to the |
| ** the right of the decimal point, or to a maximum of mxRound total |
| ** significant digits. |
| ** |
| ** If iRound>0 round to min(iRound,mxRound) significant digits total. |
| ** |
| ** mxRound must be positive. |
| ** |
| ** The significant digits of the decimal representation are |
| ** stored in p->z[] which is a often (but not always) a pointer |
| ** into the middle of p->zBuf[]. There are p->n significant digits. |
| ** The p->z[] array is *not* zero-terminated. |
| */ |
| void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ |
| int i; /* Index into zBuf[] where to put next character */ |
| int n; /* Number of digits */ |
| u64 v; /* mantissa */ |
| int e, exp = 0; /* Base-2 and base-10 exponent */ |
| char *zBuf; /* Local alias for p->zBuf */ |
| char *z; /* Local alias for p->z */ |
| |
| p->isSpecial = 0; |
| assert( mxRound>0 ); |
| |
| /* Convert negative numbers to positive. Deal with Infinity, 0.0, and |
| ** NaN. */ |
| if( r<0.0 ){ |
| p->sign = '-'; |
| r = -r; |
| }else if( r==0.0 ){ |
| p->sign = '+'; |
| p->n = 1; |
| p->iDP = 1; |
| p->z = "0"; |
| return; |
| }else{ |
| p->sign = '+'; |
| } |
| memcpy(&v,&r,8); |
| e = (v>>52)&0x7ff; |
| if( e==0x7ff ){ |
| p->isSpecial = 1 + (v!=0x7ff0000000000000LL); |
| p->n = 0; |
| p->iDP = 0; |
| p->z = p->zBuf; |
| return; |
| } |
| v &= 0x000fffffffffffffULL; |
| if( e==0 ){ |
| int nn = countLeadingZeros(v); |
| v <<= nn; |
| e = -1074 - nn; |
| }else{ |
| v = (v<<11) | U64_BIT(63); |
| e -= 1086; |
| } |
| sqlite3Fp2Convert10(v, e, (iRound<=0||iRound>=18)?18:iRound+1, &v, &exp); |
| |
| /* Extract significant digits, start at the right-most slot in p->zBuf |
| ** and working back to the right. "i" keeps track of the next slot in |
| ** which to store a digit. */ |
| i = sizeof(p->zBuf)-1; |
| zBuf = p->zBuf; |
| assert( v>0 ); |
| while( v>=10 ){ |
| int kk = (v%100)*2; |
| assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| assert( TWO_BYTE_ALIGNMENT(&zBuf[i-1]) ); |
| *(u16*)(&zBuf[i-1]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| i -= 2; |
| v /= 100; |
| } |
| if( v ){ |
| assert( v<10 ); |
| zBuf[i--] = v + '0'; |
| } |
| assert( i>=0 && i<sizeof(p->zBuf)-1 ); |
| n = sizeof(p->zBuf) - 1 - i; /* Total number of digits extracted */ |
| assert( n>0 ); |
| assert( n<sizeof(p->zBuf) ); |
| testcase( n==sizeof(p->zBuf)-1 ); |
| p->iDP = n + exp; |
| if( iRound<=0 ){ |
| iRound = p->iDP - iRound; |
| if( iRound==0 && zBuf[i+1]>='5' ){ |
| iRound = 1; |
| zBuf[i--] = '0'; |
| n++; |
| p->iDP++; |
| } |
| } |
| z = &zBuf[i+1]; /* z points to the first digit */ |
| if( iRound>0 && (iRound<n || n>mxRound) ){ |
| if( iRound>mxRound ) iRound = mxRound; |
| if( iRound==17 ){ |
| /* If the precision is exactly 17, which only happens with the "!" |
| ** flag (ex: "%!.17g") then try to reduce the precision if that |
| ** yields text that will round-trip to the original floating-point. |
| ** value. Thus, for exaple, 49.47 will render as 49.47, rather than |
| ** as 49.469999999999999. */ |
| if( z[15]=='9' && z[14]=='9' ){ |
| int jj, kk; |
| u64 v2; |
| for(jj=14; jj>0 && z[jj-1]=='9'; jj--){} |
| if( jj==0 ){ |
| v2 = 1; |
| }else{ |
| v2 = z[0] - '0'; |
| for(kk=1; kk<jj; kk++) v2 = (v2*10) + z[kk] - '0'; |
| v2++; |
| } |
| if( r==sqlite3Fp10Convert2(v2, exp + n - jj) ){ |
| iRound = jj+1; |
| } |
| }else if( p->iDP>=n || (z[15]=='0' && z[14]=='0' && z[13]=='0') ){ |
| int jj, kk; |
| u64 v2; |
| assert( z[0]!='0' ); |
| for(jj=14; z[jj-1]=='0'; jj--){} |
| v2 = z[0] - '0'; |
| for(kk=1; kk<jj; kk++) v2 = (v2*10) + z[kk] - '0'; |
| if( r==sqlite3Fp10Convert2(v2, exp + n - jj) ){ |
| iRound = jj+1; |
| } |
| } |
| } |
| n = iRound; |
| if( z[iRound]>='5' ){ |
| int j = iRound-1; |
| while( 1 /*exit-by-break*/ ){ |
| z[j]++; |
| if( z[j]<='9' ) break; |
| z[j] = '0'; |
| if( j==0 ){ |
| z--; |
| z[0] = '1'; |
| n++; |
| p->iDP++; |
| break; |
| }else{ |
| j--; |
| } |
| } |
| } |
| } |
| assert( n>0 ); |
| while( z[n-1]=='0' ){ |
| n--; |
| assert( n>0 ); |
| } |
| p->n = n; |
| p->z = z; |
| } |
| |
| /* |
| ** Try to convert z into an unsigned 32-bit integer. Return true on |
| ** success and false if there is an error. |
| ** |
| ** Only decimal notation is accepted. |
| */ |
| 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; |
| } |
| |
| /* |
| ** The variable-length integer encoding is as follows: |
| ** |
| ** KEY: |
| ** A = 0xxxxxxx 7 bits of data and one flag bit |
| ** B = 1xxxxxxx 7 bits of data and one flag bit |
| ** C = xxxxxxxx 8 bits of data |
| ** |
| ** 7 bits - A |
| ** 14 bits - BA |
| ** 21 bits - BBA |
| ** 28 bits - BBBA |
| ** 35 bits - BBBBA |
| ** 42 bits - BBBBBA |
| ** 49 bits - BBBBBBA |
| ** 56 bits - BBBBBBBA |
| ** 64 bits - BBBBBBBBC |
| */ |
| |
| /* |
| ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| ** The length of data write will be between 1 and 9 bytes. The number |
| ** of bytes written is returned. |
| ** |
| ** A variable-length integer consists of the lower 7 bits of each byte |
| ** for all bytes that have the 8th bit set and one byte with the 8th |
| ** bit clear. Except, if we get to the 9th byte, it stores the full |
| ** 8 bits and is the last byte. |
| */ |
| 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); |
| } |
| |
| /* |
| ** Bitmasks used by sqlite3GetVarint(). These precomputed constants |
| ** are defined here rather than simply putting the constant expressions |
| ** inline in order to work around bugs in the RVT compiler. |
| ** |
| ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f |
| ** |
| ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 |
| */ |
| #define SLOT_2_0 0x001fc07f |
| #define SLOT_4_2_0 0xf01fc07f |
| |
| |
| /* |
| ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| ** Return the number of bytes read. The value is stored in *v. |
| */ |
| 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; |
| } |
| |
| /* Verify that constants are precomputed correctly */ |
| 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; |
| /* a: p0<<14 | p2 (unmasked) */ |
| if (!(a&0x80)) |
| { |
| a &= SLOT_2_0; |
| b &= 0x7f; |
| b = b<<7; |
| a |= b; |
| *v = a; |
| return 3; |
| } |
| |
| /* CSE1 from below */ |
| a &= SLOT_2_0; |
| p++; |
| b = b<<14; |
| b |= *p; |
| /* b: p1<<14 | p3 (unmasked) */ |
| if (!(b&0x80)) |
| { |
| b &= SLOT_2_0; |
| /* moved CSE1 up */ |
| /* a &= (0x7f<<14)|(0x7f); */ |
| a = a<<7; |
| a |= b; |
| *v = a; |
| return 4; |
| } |
| |
| /* a: p0<<14 | p2 (masked) */ |
| /* b: p1<<14 | p3 (unmasked) */ |
| /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| /* moved CSE1 up */ |
| /* a &= (0x7f<<14)|(0x7f); */ |
| b &= SLOT_2_0; |
| s = a; |
| /* s: p0<<14 | p2 (masked) */ |
| |
| p++; |
| a = a<<14; |
| a |= *p; |
| /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| if (!(a&0x80)) |
| { |
| /* we can skip these cause they were (effectively) done above |
| ** while calculating s */ |
| /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| /* b &= (0x7f<<14)|(0x7f); */ |
| b = b<<7; |
| a |= b; |
| s = s>>18; |
| *v = ((u64)s)<<32 | a; |
| return 5; |
| } |
| |
| /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| s = s<<7; |
| s |= b; |
| /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| |
| p++; |
| b = b<<14; |
| b |= *p; |
| /* b: p1<<28 | p3<<14 | p5 (unmasked) */ |
| if (!(b&0x80)) |
| { |
| /* we can skip this cause it was (effectively) done above in calc'ing s */ |
| /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 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; |
| /* a: p2<<28 | p4<<14 | p6 (unmasked) */ |
| 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; |
| } |
| |
| /* CSE2 from below */ |
| a &= SLOT_2_0; |
| p++; |
| b = b<<14; |
| b |= *p; |
| /* b: p3<<28 | p5<<14 | p7 (unmasked) */ |
| if (!(b&0x80)) |
| { |
| b &= SLOT_4_2_0; |
| /* moved CSE2 up */ |
| /* a &= (0x7f<<14)|(0x7f); */ |
| a = a<<7; |
| a |= b; |
| s = s>>4; |
| *v = ((u64)s)<<32 | a; |
| return 8; |
| } |
| |
| p++; |
| a = a<<15; |
| a |= *p; |
| /* a: p4<<29 | p6<<15 | p8 (unmasked) */ |
| |
| /* moved CSE2 up */ |
| /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ |
| 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; |
| } |
| |
| /* |
| ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| ** Return the number of bytes read. The value is stored in *v. |
| ** |
| ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned |
| ** integer, then set *v to 0xffffffff. |
| ** |
| ** A MACRO version, getVarint32, is provided which inlines the |
| ** single-byte case. All code should use the MACRO version as |
| ** this function assumes the single-byte case has already been handled. |
| */ |
| u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
| u64 v64; |
| u8 n; |
| |
| /* Assume that the single-byte case has already been handled by |
| ** the getVarint32() macro */ |
| assert( (p[0] & 0x80)!=0 ); |
| |
| if( (p[1] & 0x80)==0 ){ |
| /* This is the two-byte case */ |
| *v = ((p[0]&0x7f)<<7) | p[1]; |
| return 2; |
| } |
| if( (p[2] & 0x80)==0 ){ |
| /* This is the three-byte case */ |
| *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2]; |
| return 3; |
| } |
| /* four or more bytes */ |
| n = sqlite3GetVarint(p, &v64); |
| assert( n>3 && n<=9 ); |
| if( (v64 & SQLITE_MAX_U32)!=v64 ){ |
| *v = 0xffffffff; |
| }else{ |
| *v = (u32)v64; |
| } |
| return n; |
| } |
| |
| /* |
| ** Return the number of bytes that will be needed to store the given |
| ** 64-bit integer. |
| */ |
| int sqlite3VarintLen(u64 v){ |
| int i; |
| for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } |
| return i; |
| } |
| |
| |
| /* |
| ** Read or write a four-byte big-endian integer value. |
| */ |
| 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 |
| } |
| |
| |
| |
| /* |
| ** Translate a single byte of Hex into an integer. |
| ** This routine only works if h really is a valid hexadecimal |
| ** character: 0..9a..fA..F |
| */ |
| 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) |
| /* |
| ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| ** value. Return a pointer to its binary value. Space to hold the |
| ** binary value has been obtained from malloc and must be freed by |
| ** the calling routine. |
| */ |
| 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 /* !SQLITE_OMIT_BLOB_LITERAL */ |
| |
| /* |
| ** Log an error that is an API call on a connection pointer that should |
| ** not have been used. The "type" of connection pointer is given as the |
| ** argument. The zType is a word like "NULL" or "closed" or "invalid". |
| */ |
| static void logBadConnection(const char *zType){ |
| sqlite3_log(SQLITE_MISUSE, |
| "API call with %s database connection pointer", |
| zType |
| ); |
| } |
| |
| /* |
| ** Check to make sure we have a valid db pointer. This test is not |
| ** foolproof but it does provide some measure of protection against |
| ** misuse of the interface such as passing in db pointers that are |
| ** NULL or which have been previously closed. If this routine returns |
| ** 1 it means that the db pointer is valid and 0 if it should not be |
| ** dereferenced for any reason. The calling function should invoke |
| ** SQLITE_MISUSE immediately. |
| ** |
| ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| ** open properly and is not fit for general use but which can be |
| ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| */ |
| 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; |
| } |
| } |
| |
| /* |
| ** Attempt to add, subtract, or multiply the 64-bit signed value iB against |
| ** the other 64-bit signed integer at *pA and store the result in *pA. |
| ** Return 0 on success. Or if the operation would have resulted in an |
| ** overflow, leave *pA unchanged and 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 |
| } |
| |
| /* |
| ** Compute the absolute value of a 32-bit signed integer, if possible. Or |
| ** if the integer has a value of -2147483648, return +2147483647 |
| */ |
| int sqlite3AbsInt32(int x){ |
| if( x>=0 ) return x; |
| if( x==(int)0x80000000 ) return 0x7fffffff; |
| return -x; |
| } |
| |
| #ifdef SQLITE_ENABLE_8_3_NAMES |
| /* |
| ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database |
| ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and |
| ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than |
| ** three characters, then shorten the suffix on z[] to be the last three |
| ** characters of the original suffix. |
| ** |
| ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always |
| ** do the suffix shortening regardless of URI parameter. |
| ** |
| ** Examples: |
| ** |
| ** test.db-journal => test.nal |
| ** test.db-wal => test.wal |
| ** test.db-shm => test.shm |
| ** test.db-mj7f3319fa => test.9fa |
| */ |
| 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 |
| |
| /* |
| ** Find (an approximate) sum of two LogEst values. This computation is |
| ** not a simple "+" operator because LogEst is stored as a logarithmic |
| ** value. |
| ** |
| */ |
| LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ |
| static const unsigned char x[] = { |
| 10, 10, /* 0,1 */ |
| 9, 9, /* 2,3 */ |
| 8, 8, /* 4,5 */ |
| 7, 7, 7, /* 6,7,8 */ |
| 6, 6, 6, /* 9,10,11 */ |
| 5, 5, 5, /* 12-14 */ |
| 4, 4, 4, 4, /* 15-18 */ |
| 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| }; |
| 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]; |
| } |
| } |
| |
| /* |
| ** Convert an integer into a LogEst. In other words, compute an |
| ** approximation for 10*log2(x). |
| */ |
| 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; } /*OPTIMIZATION-IF-TRUE*/ |
| while( x>15 ){ y += 10; x >>= 1; } |
| #endif |
| } |
| return a[x&7] + y - 10; |
| } |
| |
| /* |
| ** Convert a double into a LogEst |
| ** In other words, compute an approximation for 10*log2(x). |
| */ |
| 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; |
| } |
| |
| /* |
| ** Convert a LogEst into an integer. |
| */ |
| 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); |
| } |
| |
| /* |
| ** Add a new name/number pair to a VList. This might require that the |
| ** VList object be reallocated, so return the new VList. If an OOM |
| ** error occurs, the original VList returned and the |
| ** db->mallocFailed flag is set. |
| ** |
| ** A VList is really just an array of integers. To destroy a VList, |
| ** simply pass it to sqlite3DbFree(). |
| ** |
| ** The first integer is the number of integers allocated for the whole |
| ** VList. The second integer is the number of integers actually used. |
| ** Each name/number pair is encoded by subsequent groups of 3 or more |
| ** integers. |
| ** |
| ** Each name/number pair starts with two integers which are the numeric |
| ** value for the pair and the size of the name/number pair, respectively. |
| ** The text name overlays one or more following integers. The text name |
| ** is always zero-terminated. |
| ** |
| ** Conceptually: |
| ** |
| ** struct VList { |
| ** int nAlloc; // Number of allocated slots |
| ** int nUsed; // Number of used slots |
| ** struct VListEntry { |
| ** int iValue; // Value for this entry |
| ** int nSlot; // Slots used by this entry |
| ** // ... variable name goes here |
| ** } a[0]; |
| ** } |
| ** |
| ** During code generation, pointers to the variable names within the |
| ** VList are taken. When that happens, nAlloc is set to zero as an |
| ** indication that the VList may never again be enlarged, since the |
| ** accompanying realloc() would invalidate the pointers. |
| */ |
| VList *sqlite3VListAdd( |
| sqlite3 *db, /* The database connection used for malloc() */ |
| VList *pIn, /* The input VList. Might be NULL */ |
| const char *zName, /* Name of symbol to add */ |
| int nName, /* Bytes of text in zName */ |
| int iVal /* Value to associate with zName */ |
| ){ |
| int nInt; /* number of sizeof(int) objects needed for zName */ |
| char *z; /* Pointer to where zName will be stored */ |
| int i; /* Index in pIn[] where zName is stored */ |
| |
| nInt = nName/4 + 3; |
| assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ |
| if( pIn==0 || pIn[1]+nInt > pIn[0] ){ |
| /* Enlarge the allocation */ |
| 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; |
| } |
| |
| /* |
| ** Return a pointer to the name of a variable in the given VList that |
| ** has the value iVal. Or return a NULL if there is no such variable in |
| ** the list |
| */ |
| 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; |
| } |
| |
| /* |
| ** Return the number of the variable named zName, if it is in VList. |
| ** or return 0 if there is no such variable. |
| */ |
| 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; |
| } |