000001 /*
000002 ** 2001 September 15
000003 **
000004 ** The author disclaims copyright to this source code. In place of
000005 ** a legal notice, here is a blessing:
000006 **
000007 ** May you do good and not evil.
000008 ** May you find forgiveness for yourself and forgive others.
000009 ** May you share freely, never taking more than you give.
000010 **
000011 *************************************************************************
000012 ** Utility functions used throughout sqlite.
000013 **
000014 ** This file contains functions for allocating memory, comparing
000015 ** strings, and stuff like that.
000016 **
000017 */
000018 #include "sqliteInt.h"
000019 #include <stdarg.h>
000020 #ifndef SQLITE_OMIT_FLOATING_POINT
000021 #include <math.h>
000022 #endif
000023
000024 /*
000025 ** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
000026 ** or to bypass normal error detection during testing in order to let
000027 ** execute proceed further downstream.
000028 **
000029 ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
000030 ** sqlite3FaultSim() function only returns non-zero during testing.
000031 **
000032 ** During testing, if the test harness has set a fault-sim callback using
000033 ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
000034 ** each call to sqlite3FaultSim() is relayed to that application-supplied
000035 ** callback and the integer return value form the application-supplied
000036 ** callback is returned by sqlite3FaultSim().
000037 **
000038 ** The integer argument to sqlite3FaultSim() is a code to identify which
000039 ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
000040 ** should have a unique code. To prevent legacy testing applications from
000041 ** breaking, the codes should not be changed or reused.
000042 */
000043 #ifndef SQLITE_UNTESTABLE
000044 int sqlite3FaultSim(int iTest){
000045 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
000046 return xCallback ? xCallback(iTest) : SQLITE_OK;
000047 }
000048 #endif
000049
000050 #ifndef SQLITE_OMIT_FLOATING_POINT
000051 /*
000052 ** Return true if the floating point value is Not a Number (NaN).
000053 **
000054 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
000055 ** Otherwise, we have our own implementation that works on most systems.
000056 */
000057 int sqlite3IsNaN(double x){
000058 int rc; /* The value return */
000059 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
000060 u64 y;
000061 memcpy(&y,&x,sizeof(y));
000062 rc = IsNaN(y);
000063 #else
000064 rc = isnan(x);
000065 #endif /* HAVE_ISNAN */
000066 testcase( rc );
000067 return rc;
000068 }
000069 #endif /* SQLITE_OMIT_FLOATING_POINT */
000070
000071 #ifndef SQLITE_OMIT_FLOATING_POINT
000072 /*
000073 ** Return true if the floating point value is NaN or +Inf or -Inf.
000074 */
000075 int sqlite3IsOverflow(double x){
000076 int rc; /* The value return */
000077 u64 y;
000078 memcpy(&y,&x,sizeof(y));
000079 rc = IsOvfl(y);
000080 return rc;
000081 }
000082 #endif /* SQLITE_OMIT_FLOATING_POINT */
000083
000084 /*
000085 ** Compute a string length that is limited to what can be stored in
000086 ** lower 30 bits of a 32-bit signed integer.
000087 **
000088 ** The value returned will never be negative. Nor will it ever be greater
000089 ** than the actual length of the string. For very long strings (greater
000090 ** than 1GiB) the value returned might be less than the true string length.
000091 */
000092 int sqlite3Strlen30(const char *z){
000093 if( z==0 ) return 0;
000094 return 0x3fffffff & (int)strlen(z);
000095 }
000096
000097 /*
000098 ** Return the declared type of a column. Or return zDflt if the column
000099 ** has no declared type.
000100 **
000101 ** The column type is an extra string stored after the zero-terminator on
000102 ** the column name if and only if the COLFLAG_HASTYPE flag is set.
000103 */
000104 char *sqlite3ColumnType(Column *pCol, char *zDflt){
000105 if( pCol->colFlags & COLFLAG_HASTYPE ){
000106 return pCol->zCnName + strlen(pCol->zCnName) + 1;
000107 }else if( pCol->eCType ){
000108 assert( pCol->eCType<=SQLITE_N_STDTYPE );
000109 return (char*)sqlite3StdType[pCol->eCType-1];
000110 }else{
000111 return zDflt;
000112 }
000113 }
000114
000115 /*
000116 ** Helper function for sqlite3Error() - called rarely. Broken out into
000117 ** a separate routine to avoid unnecessary register saves on entry to
000118 ** sqlite3Error().
000119 */
000120 static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
000121 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
000122 sqlite3SystemError(db, err_code);
000123 }
000124
000125 /*
000126 ** Set the current error code to err_code and clear any prior error message.
000127 ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
000128 ** that would be appropriate.
000129 */
000130 void sqlite3Error(sqlite3 *db, int err_code){
000131 assert( db!=0 );
000132 db->errCode = err_code;
000133 if( err_code || db->pErr ){
000134 sqlite3ErrorFinish(db, err_code);
000135 }else{
000136 db->errByteOffset = -1;
000137 }
000138 }
000139
000140 /*
000141 ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
000142 ** and error message.
000143 */
000144 void sqlite3ErrorClear(sqlite3 *db){
000145 assert( db!=0 );
000146 db->errCode = SQLITE_OK;
000147 db->errByteOffset = -1;
000148 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
000149 }
000150
000151 /*
000152 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
000153 ** to do based on the SQLite error code in rc.
000154 */
000155 void sqlite3SystemError(sqlite3 *db, int rc){
000156 if( rc==SQLITE_IOERR_NOMEM ) return;
000157 #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
000158 if( rc==SQLITE_IOERR_IN_PAGE ){
000159 int ii;
000160 int iErr;
000161 sqlite3BtreeEnterAll(db);
000162 for(ii=0; ii<db->nDb; ii++){
000163 if( db->aDb[ii].pBt ){
000164 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
000165 if( iErr ){
000166 db->iSysErrno = iErr;
000167 }
000168 }
000169 }
000170 sqlite3BtreeLeaveAll(db);
000171 return;
000172 }
000173 #endif
000174 rc &= 0xff;
000175 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
000176 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
000177 }
000178 }
000179
000180 /*
000181 ** Set the most recent error code and error string for the sqlite
000182 ** handle "db". The error code is set to "err_code".
000183 **
000184 ** If it is not NULL, string zFormat specifies the format of the
000185 ** error string. zFormat and any string tokens that follow it are
000186 ** assumed to be encoded in UTF-8.
000187 **
000188 ** To clear the most recent error for sqlite handle "db", sqlite3Error
000189 ** should be called with err_code set to SQLITE_OK and zFormat set
000190 ** to NULL.
000191 */
000192 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
000193 assert( db!=0 );
000194 db->errCode = err_code;
000195 sqlite3SystemError(db, err_code);
000196 if( zFormat==0 ){
000197 sqlite3Error(db, err_code);
000198 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
000199 char *z;
000200 va_list ap;
000201 va_start(ap, zFormat);
000202 z = sqlite3VMPrintf(db, zFormat, ap);
000203 va_end(ap);
000204 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
000205 }
000206 }
000207
000208 /*
000209 ** Check for interrupts and invoke progress callback.
000210 */
000211 void sqlite3ProgressCheck(Parse *p){
000212 sqlite3 *db = p->db;
000213 if( AtomicLoad(&db->u1.isInterrupted) ){
000214 p->nErr++;
000215 p->rc = SQLITE_INTERRUPT;
000216 }
000217 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
000218 if( db->xProgress ){
000219 if( p->rc==SQLITE_INTERRUPT ){
000220 p->nProgressSteps = 0;
000221 }else if( (++p->nProgressSteps)>=db->nProgressOps ){
000222 if( db->xProgress(db->pProgressArg) ){
000223 p->nErr++;
000224 p->rc = SQLITE_INTERRUPT;
000225 }
000226 p->nProgressSteps = 0;
000227 }
000228 }
000229 #endif
000230 }
000231
000232 /*
000233 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
000234 **
000235 ** This function should be used to report any error that occurs while
000236 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
000237 ** last thing the sqlite3_prepare() function does is copy the error
000238 ** stored by this function into the database handle using sqlite3Error().
000239 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
000240 ** during statement execution (sqlite3_step() etc.).
000241 */
000242 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
000243 char *zMsg;
000244 va_list ap;
000245 sqlite3 *db = pParse->db;
000246 assert( db!=0 );
000247 assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
000248 db->errByteOffset = -2;
000249 va_start(ap, zFormat);
000250 zMsg = sqlite3VMPrintf(db, zFormat, ap);
000251 va_end(ap);
000252 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
000253 if( db->suppressErr ){
000254 sqlite3DbFree(db, zMsg);
000255 if( db->mallocFailed ){
000256 pParse->nErr++;
000257 pParse->rc = SQLITE_NOMEM;
000258 }
000259 }else{
000260 pParse->nErr++;
000261 sqlite3DbFree(db, pParse->zErrMsg);
000262 pParse->zErrMsg = zMsg;
000263 pParse->rc = SQLITE_ERROR;
000264 pParse->pWith = 0;
000265 }
000266 }
000267
000268 /*
000269 ** If database connection db is currently parsing SQL, then transfer
000270 ** error code errCode to that parser if the parser has not already
000271 ** encountered some other kind of error.
000272 */
000273 int sqlite3ErrorToParser(sqlite3 *db, int errCode){
000274 Parse *pParse;
000275 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
000276 pParse->rc = errCode;
000277 pParse->nErr++;
000278 return errCode;
000279 }
000280
000281 /*
000282 ** Convert an SQL-style quoted string into a normal string by removing
000283 ** the quote characters. The conversion is done in-place. If the
000284 ** input does not begin with a quote character, then this routine
000285 ** is a no-op.
000286 **
000287 ** The input string must be zero-terminated. A new zero-terminator
000288 ** is added to the dequoted string.
000289 **
000290 ** The return value is -1 if no dequoting occurs or the length of the
000291 ** dequoted string, exclusive of the zero terminator, if dequoting does
000292 ** occur.
000293 **
000294 ** 2002-02-14: This routine is extended to remove MS-Access style
000295 ** brackets from around identifiers. For example: "[a-b-c]" becomes
000296 ** "a-b-c".
000297 */
000298 void sqlite3Dequote(char *z){
000299 char quote;
000300 int i, j;
000301 if( z==0 ) return;
000302 quote = z[0];
000303 if( !sqlite3Isquote(quote) ) return;
000304 if( quote=='[' ) quote = ']';
000305 for(i=1, j=0;; i++){
000306 assert( z[i] );
000307 if( z[i]==quote ){
000308 if( z[i+1]==quote ){
000309 z[j++] = quote;
000310 i++;
000311 }else{
000312 break;
000313 }
000314 }else{
000315 z[j++] = z[i];
000316 }
000317 }
000318 z[j] = 0;
000319 }
000320 void sqlite3DequoteExpr(Expr *p){
000321 assert( !ExprHasProperty(p, EP_IntValue) );
000322 assert( sqlite3Isquote(p->u.zToken[0]) );
000323 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
000324 sqlite3Dequote(p->u.zToken);
000325 }
000326
000327 /*
000328 ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
000329 ** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those
000330 ** that contain '_' characters that must be removed before further processing.
000331 */
000332 void sqlite3DequoteNumber(Parse *pParse, Expr *p){
000333 assert( p!=0 || pParse->db->mallocFailed );
000334 if( p ){
000335 const char *pIn = p->u.zToken;
000336 char *pOut = p->u.zToken;
000337 int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X'));
000338 int iValue;
000339 assert( p->op==TK_QNUMBER );
000340 p->op = TK_INTEGER;
000341 do {
000342 if( *pIn!=SQLITE_DIGIT_SEPARATOR ){
000343 *pOut++ = *pIn;
000344 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT;
000345 }else{
000346 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1])))
000347 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1])))
000348 ){
000349 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken);
000350 }
000351 }
000352 }while( *pIn++ );
000353 if( bHex ) p->op = TK_INTEGER;
000354
000355 /* tag-20240227-a: If after dequoting, the number is an integer that
000356 ** fits in 32 bits, then it must be converted into EP_IntValue. Other
000357 ** parts of the code expect this. See also tag-20240227-b. */
000358 if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){
000359 p->u.iValue = iValue;
000360 p->flags |= EP_IntValue;
000361 }
000362 }
000363 }
000364
000365 /*
000366 ** If the input token p is quoted, try to adjust the token to remove
000367 ** the quotes. This is not always possible:
000368 **
000369 ** "abc" -> abc
000370 ** "ab""cd" -> (not possible because of the interior "")
000371 **
000372 ** Remove the quotes if possible. This is a optimization. The overall
000373 ** system should still return the correct answer even if this routine
000374 ** is always a no-op.
000375 */
000376 void sqlite3DequoteToken(Token *p){
000377 unsigned int i;
000378 if( p->n<2 ) return;
000379 if( !sqlite3Isquote(p->z[0]) ) return;
000380 for(i=1; i<p->n-1; i++){
000381 if( sqlite3Isquote(p->z[i]) ) return;
000382 }
000383 p->n -= 2;
000384 p->z++;
000385 }
000386
000387 /*
000388 ** Generate a Token object from a string
000389 */
000390 void sqlite3TokenInit(Token *p, char *z){
000391 p->z = z;
000392 p->n = sqlite3Strlen30(z);
000393 }
000394
000395 /* Convenient short-hand */
000396 #define UpperToLower sqlite3UpperToLower
000397
000398 /*
000399 ** Some systems have stricmp(). Others have strcasecmp(). Because
000400 ** there is no consistency, we will define our own.
000401 **
000402 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
000403 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
000404 ** the contents of two buffers containing UTF-8 strings in a
000405 ** case-independent fashion, using the same definition of "case
000406 ** independence" that SQLite uses internally when comparing identifiers.
000407 */
000408 int sqlite3_stricmp(const char *zLeft, const char *zRight){
000409 if( zLeft==0 ){
000410 return zRight ? -1 : 0;
000411 }else if( zRight==0 ){
000412 return 1;
000413 }
000414 return sqlite3StrICmp(zLeft, zRight);
000415 }
000416 int sqlite3StrICmp(const char *zLeft, const char *zRight){
000417 unsigned char *a, *b;
000418 int c, x;
000419 a = (unsigned char *)zLeft;
000420 b = (unsigned char *)zRight;
000421 for(;;){
000422 c = *a;
000423 x = *b;
000424 if( c==x ){
000425 if( c==0 ) break;
000426 }else{
000427 c = (int)UpperToLower[c] - (int)UpperToLower[x];
000428 if( c ) break;
000429 }
000430 a++;
000431 b++;
000432 }
000433 return c;
000434 }
000435 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
000436 register unsigned char *a, *b;
000437 if( zLeft==0 ){
000438 return zRight ? -1 : 0;
000439 }else if( zRight==0 ){
000440 return 1;
000441 }
000442 a = (unsigned char *)zLeft;
000443 b = (unsigned char *)zRight;
000444 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
000445 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
000446 }
000447
000448 /*
000449 ** Compute an 8-bit hash on a string that is insensitive to case differences
000450 */
000451 u8 sqlite3StrIHash(const char *z){
000452 u8 h = 0;
000453 if( z==0 ) return 0;
000454 while( z[0] ){
000455 h += UpperToLower[(unsigned char)z[0]];
000456 z++;
000457 }
000458 return h;
000459 }
000460
000461 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
000462 **
000463 ** Reference:
000464 ** T. J. Dekker, "A Floating-Point Technique for Extending the
000465 ** Available Precision". 1971-07-26.
000466 */
000467 static void dekkerMul2(volatile double *x, double y, double yy){
000468 /*
000469 ** The "volatile" keywords on parameter x[] and on local variables
000470 ** below are needed force intermediate results to be truncated to
000471 ** binary64 rather than be carried around in an extended-precision
000472 ** format. The truncation is necessary for the Dekker algorithm to
000473 ** work. Intel x86 floating point might omit the truncation without
000474 ** the use of volatile.
000475 */
000476 volatile double tx, ty, p, q, c, cc;
000477 double hx, hy;
000478 u64 m;
000479 memcpy(&m, (void*)&x[0], 8);
000480 m &= 0xfffffffffc000000LL;
000481 memcpy(&hx, &m, 8);
000482 tx = x[0] - hx;
000483 memcpy(&m, &y, 8);
000484 m &= 0xfffffffffc000000LL;
000485 memcpy(&hy, &m, 8);
000486 ty = y - hy;
000487 p = hx*hy;
000488 q = hx*ty + tx*hy;
000489 c = p+q;
000490 cc = p - c + q + tx*ty;
000491 cc = x[0]*yy + x[1]*y + cc;
000492 x[0] = c + cc;
000493 x[1] = c - x[0];
000494 x[1] += cc;
000495 }
000496
000497 /*
000498 ** The string z[] is an text representation of a real number.
000499 ** Convert this string to a double and write it into *pResult.
000500 **
000501 ** The string z[] is length bytes in length (bytes, not characters) and
000502 ** uses the encoding enc. The string is not necessarily zero-terminated.
000503 **
000504 ** Return TRUE if the result is a valid real number (or integer) and FALSE
000505 ** if the string is empty or contains extraneous text. More specifically
000506 ** return
000507 ** 1 => The input string is a pure integer
000508 ** 2 or more => The input has a decimal point or eNNN clause
000509 ** 0 or less => The input string is not a valid number
000510 ** -1 => Not a valid number, but has a valid prefix which
000511 ** includes a decimal point and/or an eNNN clause
000512 **
000513 ** Valid numbers are in one of these formats:
000514 **
000515 ** [+-]digits[E[+-]digits]
000516 ** [+-]digits.[digits][E[+-]digits]
000517 ** [+-].digits[E[+-]digits]
000518 **
000519 ** Leading and trailing whitespace is ignored for the purpose of determining
000520 ** validity.
000521 **
000522 ** If some prefix of the input string is a valid number, this routine
000523 ** returns FALSE but it still converts the prefix and writes the result
000524 ** into *pResult.
000525 */
000526 #if defined(_MSC_VER)
000527 #pragma warning(disable : 4756)
000528 #endif
000529 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
000530 #ifndef SQLITE_OMIT_FLOATING_POINT
000531 int incr;
000532 const char *zEnd;
000533 /* sign * significand * (10 ^ (esign * exponent)) */
000534 int sign = 1; /* sign of significand */
000535 u64 s = 0; /* significand */
000536 int d = 0; /* adjust exponent for shifting decimal point */
000537 int esign = 1; /* sign of exponent */
000538 int e = 0; /* exponent */
000539 int eValid = 1; /* True exponent is either not used or is well-formed */
000540 int nDigit = 0; /* Number of digits processed */
000541 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
000542 u64 s2; /* round-tripped significand */
000543 double rr[2];
000544
000545 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
000546 *pResult = 0.0; /* Default return value, in case of an error */
000547 if( length==0 ) return 0;
000548
000549 if( enc==SQLITE_UTF8 ){
000550 incr = 1;
000551 zEnd = z + length;
000552 }else{
000553 int i;
000554 incr = 2;
000555 length &= ~1;
000556 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
000557 testcase( enc==SQLITE_UTF16LE );
000558 testcase( enc==SQLITE_UTF16BE );
000559 for(i=3-enc; i<length && z[i]==0; i+=2){}
000560 if( i<length ) eType = -100;
000561 zEnd = &z[i^1];
000562 z += (enc&1);
000563 }
000564
000565 /* skip leading spaces */
000566 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
000567 if( z>=zEnd ) return 0;
000568
000569 /* get sign of significand */
000570 if( *z=='-' ){
000571 sign = -1;
000572 z+=incr;
000573 }else if( *z=='+' ){
000574 z+=incr;
000575 }
000576
000577 /* copy max significant digits to significand */
000578 while( z<zEnd && sqlite3Isdigit(*z) ){
000579 s = s*10 + (*z - '0');
000580 z+=incr; nDigit++;
000581 if( s>=((LARGEST_UINT64-9)/10) ){
000582 /* skip non-significant significand digits
000583 ** (increase exponent by d to shift decimal left) */
000584 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
000585 }
000586 }
000587 if( z>=zEnd ) goto do_atof_calc;
000588
000589 /* if decimal point is present */
000590 if( *z=='.' ){
000591 z+=incr;
000592 eType++;
000593 /* copy digits from after decimal to significand
000594 ** (decrease exponent by d to shift decimal right) */
000595 while( z<zEnd && sqlite3Isdigit(*z) ){
000596 if( s<((LARGEST_UINT64-9)/10) ){
000597 s = s*10 + (*z - '0');
000598 d--;
000599 nDigit++;
000600 }
000601 z+=incr;
000602 }
000603 }
000604 if( z>=zEnd ) goto do_atof_calc;
000605
000606 /* if exponent is present */
000607 if( *z=='e' || *z=='E' ){
000608 z+=incr;
000609 eValid = 0;
000610 eType++;
000611
000612 /* This branch is needed to avoid a (harmless) buffer overread. The
000613 ** special comment alerts the mutation tester that the correct answer
000614 ** is obtained even if the branch is omitted */
000615 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
000616
000617 /* get sign of exponent */
000618 if( *z=='-' ){
000619 esign = -1;
000620 z+=incr;
000621 }else if( *z=='+' ){
000622 z+=incr;
000623 }
000624 /* copy digits to exponent */
000625 while( z<zEnd && sqlite3Isdigit(*z) ){
000626 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
000627 z+=incr;
000628 eValid = 1;
000629 }
000630 }
000631
000632 /* skip trailing spaces */
000633 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
000634
000635 do_atof_calc:
000636 /* Zero is a special case */
000637 if( s==0 ){
000638 *pResult = sign<0 ? -0.0 : +0.0;
000639 goto atof_return;
000640 }
000641
000642 /* adjust exponent by d, and update sign */
000643 e = (e*esign) + d;
000644
000645 /* Try to adjust the exponent to make it smaller */
000646 while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){
000647 s *= 10;
000648 e--;
000649 }
000650 while( e<0 && (s%10)==0 ){
000651 s /= 10;
000652 e++;
000653 }
000654
000655 rr[0] = (double)s;
000656 assert( sizeof(s2)==sizeof(rr[0]) );
000657 #ifdef SQLITE_DEBUG
000658 rr[1] = 18446744073709549568.0;
000659 memcpy(&s2, &rr[1], sizeof(s2));
000660 assert( s2==0x43efffffffffffffLL );
000661 #endif
000662 /* Largest double that can be safely converted to u64
000663 ** vvvvvvvvvvvvvvvvvvvvvv */
000664 if( rr[0]<=18446744073709549568.0 ){
000665 s2 = (u64)rr[0];
000666 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
000667 }else{
000668 rr[1] = 0.0;
000669 }
000670 assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */
000671
000672 if( e>0 ){
000673 while( e>=100 ){
000674 e -= 100;
000675 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
000676 }
000677 while( e>=10 ){
000678 e -= 10;
000679 dekkerMul2(rr, 1.0e+10, 0.0);
000680 }
000681 while( e>=1 ){
000682 e -= 1;
000683 dekkerMul2(rr, 1.0e+01, 0.0);
000684 }
000685 }else{
000686 while( e<=-100 ){
000687 e += 100;
000688 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
000689 }
000690 while( e<=-10 ){
000691 e += 10;
000692 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
000693 }
000694 while( e<=-1 ){
000695 e += 1;
000696 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
000697 }
000698 }
000699 *pResult = rr[0]+rr[1];
000700 if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300;
000701 if( sign<0 ) *pResult = -*pResult;
000702 assert( !sqlite3IsNaN(*pResult) );
000703
000704 atof_return:
000705 /* return true if number and no extra non-whitespace characters after */
000706 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
000707 return eType;
000708 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
000709 return -1;
000710 }else{
000711 return 0;
000712 }
000713 #else
000714 return !sqlite3Atoi64(z, pResult, length, enc);
000715 #endif /* SQLITE_OMIT_FLOATING_POINT */
000716 }
000717 #if defined(_MSC_VER)
000718 #pragma warning(default : 4756)
000719 #endif
000720
000721 /*
000722 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
000723 ** return the length of the string that was stored, in bytes. The value
000724 ** returned does not include the zero terminator at the end of the output
000725 ** string.
000726 **
000727 ** The caller must ensure that zOut[] is at least 21 bytes in size.
000728 */
000729 int sqlite3Int64ToText(i64 v, char *zOut){
000730 int i;
000731 u64 x;
000732 char zTemp[22];
000733 if( v<0 ){
000734 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
000735 }else{
000736 x = v;
000737 }
000738 i = sizeof(zTemp)-2;
000739 zTemp[sizeof(zTemp)-1] = 0;
000740 while( 1 /*exit-by-break*/ ){
000741 zTemp[i] = (x%10) + '0';
000742 x = x/10;
000743 if( x==0 ) break;
000744 i--;
000745 };
000746 if( v<0 ) zTemp[--i] = '-';
000747 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i);
000748 return sizeof(zTemp)-1-i;
000749 }
000750
000751 /*
000752 ** Compare the 19-character string zNum against the text representation
000753 ** value 2^63: 9223372036854775808. Return negative, zero, or positive
000754 ** if zNum is less than, equal to, or greater than the string.
000755 ** Note that zNum must contain exactly 19 characters.
000756 **
000757 ** Unlike memcmp() this routine is guaranteed to return the difference
000758 ** in the values of the last digit if the only difference is in the
000759 ** last digit. So, for example,
000760 **
000761 ** compare2pow63("9223372036854775800", 1)
000762 **
000763 ** will return -8.
000764 */
000765 static int compare2pow63(const char *zNum, int incr){
000766 int c = 0;
000767 int i;
000768 /* 012345678901234567 */
000769 const char *pow63 = "922337203685477580";
000770 for(i=0; c==0 && i<18; i++){
000771 c = (zNum[i*incr]-pow63[i])*10;
000772 }
000773 if( c==0 ){
000774 c = zNum[18*incr] - '8';
000775 testcase( c==(-1) );
000776 testcase( c==0 );
000777 testcase( c==(+1) );
000778 }
000779 return c;
000780 }
000781
000782 /*
000783 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
000784 ** routine does *not* accept hexadecimal notation.
000785 **
000786 ** Returns:
000787 **
000788 ** -1 Not even a prefix of the input text looks like an integer
000789 ** 0 Successful transformation. Fits in a 64-bit signed integer.
000790 ** 1 Excess non-space text after the integer value
000791 ** 2 Integer too large for a 64-bit signed integer or is malformed
000792 ** 3 Special case of 9223372036854775808
000793 **
000794 ** length is the number of bytes in the string (bytes, not characters).
000795 ** The string is not necessarily zero-terminated. The encoding is
000796 ** given by enc.
000797 */
000798 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
000799 int incr;
000800 u64 u = 0;
000801 int neg = 0; /* assume positive */
000802 int i;
000803 int c = 0;
000804 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
000805 int rc; /* Baseline return code */
000806 const char *zStart;
000807 const char *zEnd = zNum + length;
000808 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
000809 if( enc==SQLITE_UTF8 ){
000810 incr = 1;
000811 }else{
000812 incr = 2;
000813 length &= ~1;
000814 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
000815 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
000816 nonNum = i<length;
000817 zEnd = &zNum[i^1];
000818 zNum += (enc&1);
000819 }
000820 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
000821 if( zNum<zEnd ){
000822 if( *zNum=='-' ){
000823 neg = 1;
000824 zNum+=incr;
000825 }else if( *zNum=='+' ){
000826 zNum+=incr;
000827 }
000828 }
000829 zStart = zNum;
000830 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
000831 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
000832 u = u*10 + c - '0';
000833 }
000834 testcase( i==18*incr );
000835 testcase( i==19*incr );
000836 testcase( i==20*incr );
000837 if( u>LARGEST_INT64 ){
000838 /* This test and assignment is needed only to suppress UB warnings
000839 ** from clang and -fsanitize=undefined. This test and assignment make
000840 ** the code a little larger and slower, and no harm comes from omitting
000841 ** them, but we must appease the undefined-behavior pharisees. */
000842 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
000843 }else if( neg ){
000844 *pNum = -(i64)u;
000845 }else{
000846 *pNum = (i64)u;
000847 }
000848 rc = 0;
000849 if( i==0 && zStart==zNum ){ /* No digits */
000850 rc = -1;
000851 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
000852 rc = 1;
000853 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
000854 int jj = i;
000855 do{
000856 if( !sqlite3Isspace(zNum[jj]) ){
000857 rc = 1; /* Extra non-space text after the integer */
000858 break;
000859 }
000860 jj += incr;
000861 }while( &zNum[jj]<zEnd );
000862 }
000863 if( i<19*incr ){
000864 /* Less than 19 digits, so we know that it fits in 64 bits */
000865 assert( u<=LARGEST_INT64 );
000866 return rc;
000867 }else{
000868 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
000869 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
000870 if( c<0 ){
000871 /* zNum is less than 9223372036854775808 so it fits */
000872 assert( u<=LARGEST_INT64 );
000873 return rc;
000874 }else{
000875 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
000876 if( c>0 ){
000877 /* zNum is greater than 9223372036854775808 so it overflows */
000878 return 2;
000879 }else{
000880 /* zNum is exactly 9223372036854775808. Fits if negative. The
000881 ** special case 2 overflow if positive */
000882 assert( u-1==LARGEST_INT64 );
000883 return neg ? rc : 3;
000884 }
000885 }
000886 }
000887 }
000888
000889 /*
000890 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
000891 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
000892 ** whereas sqlite3Atoi64() does not.
000893 **
000894 ** Returns:
000895 **
000896 ** 0 Successful transformation. Fits in a 64-bit signed integer.
000897 ** 1 Excess text after the integer value
000898 ** 2 Integer too large for a 64-bit signed integer or is malformed
000899 ** 3 Special case of 9223372036854775808
000900 */
000901 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
000902 #ifndef SQLITE_OMIT_HEX_INTEGER
000903 if( z[0]=='0'
000904 && (z[1]=='x' || z[1]=='X')
000905 ){
000906 u64 u = 0;
000907 int i, k;
000908 for(i=2; z[i]=='0'; i++){}
000909 for(k=i; sqlite3Isxdigit(z[k]); k++){
000910 u = u*16 + sqlite3HexToInt(z[k]);
000911 }
000912 memcpy(pOut, &u, 8);
000913 if( k-i>16 ) return 2;
000914 if( z[k]!=0 ) return 1;
000915 return 0;
000916 }else
000917 #endif /* SQLITE_OMIT_HEX_INTEGER */
000918 {
000919 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789"));
000920 if( z[n] ) n++;
000921 return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8);
000922 }
000923 }
000924
000925 /*
000926 ** If zNum represents an integer that will fit in 32-bits, then set
000927 ** *pValue to that integer and return true. Otherwise return false.
000928 **
000929 ** This routine accepts both decimal and hexadecimal notation for integers.
000930 **
000931 ** Any non-numeric characters that following zNum are ignored.
000932 ** This is different from sqlite3Atoi64() which requires the
000933 ** input number to be zero-terminated.
000934 */
000935 int sqlite3GetInt32(const char *zNum, int *pValue){
000936 sqlite_int64 v = 0;
000937 int i, c;
000938 int neg = 0;
000939 if( zNum[0]=='-' ){
000940 neg = 1;
000941 zNum++;
000942 }else if( zNum[0]=='+' ){
000943 zNum++;
000944 }
000945 #ifndef SQLITE_OMIT_HEX_INTEGER
000946 else if( zNum[0]=='0'
000947 && (zNum[1]=='x' || zNum[1]=='X')
000948 && sqlite3Isxdigit(zNum[2])
000949 ){
000950 u32 u = 0;
000951 zNum += 2;
000952 while( zNum[0]=='0' ) zNum++;
000953 for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){
000954 u = u*16 + sqlite3HexToInt(zNum[i]);
000955 }
000956 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
000957 memcpy(pValue, &u, 4);
000958 return 1;
000959 }else{
000960 return 0;
000961 }
000962 }
000963 #endif
000964 if( !sqlite3Isdigit(zNum[0]) ) return 0;
000965 while( zNum[0]=='0' ) zNum++;
000966 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
000967 v = v*10 + c;
000968 }
000969
000970 /* The longest decimal representation of a 32 bit integer is 10 digits:
000971 **
000972 ** 1234567890
000973 ** 2^31 -> 2147483648
000974 */
000975 testcase( i==10 );
000976 if( i>10 ){
000977 return 0;
000978 }
000979 testcase( v-neg==2147483647 );
000980 if( v-neg>2147483647 ){
000981 return 0;
000982 }
000983 if( neg ){
000984 v = -v;
000985 }
000986 *pValue = (int)v;
000987 return 1;
000988 }
000989
000990 /*
000991 ** Return a 32-bit integer value extracted from a string. If the
000992 ** string is not an integer, just return 0.
000993 */
000994 int sqlite3Atoi(const char *z){
000995 int x = 0;
000996 sqlite3GetInt32(z, &x);
000997 return x;
000998 }
000999
001000 /*
001001 ** Decode a floating-point value into an approximate decimal
001002 ** representation.
001003 **
001004 ** If iRound<=0 then round to -iRound significant digits to the
001005 ** the left of the decimal point, or to a maximum of mxRound total
001006 ** significant digits.
001007 **
001008 ** If iRound>0 round to min(iRound,mxRound) significant digits total.
001009 **
001010 ** mxRound must be positive.
001011 **
001012 ** The significant digits of the decimal representation are
001013 ** stored in p->z[] which is a often (but not always) a pointer
001014 ** into the middle of p->zBuf[]. There are p->n significant digits.
001015 ** The p->z[] array is *not* zero-terminated.
001016 */
001017 void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
001018 int i;
001019 u64 v;
001020 int e, exp = 0;
001021 double rr[2];
001022
001023 p->isSpecial = 0;
001024 p->z = p->zBuf;
001025 assert( mxRound>0 );
001026
001027 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
001028 ** NaN. */
001029 if( r<0.0 ){
001030 p->sign = '-';
001031 r = -r;
001032 }else if( r==0.0 ){
001033 p->sign = '+';
001034 p->n = 1;
001035 p->iDP = 1;
001036 p->z = "0";
001037 return;
001038 }else{
001039 p->sign = '+';
001040 }
001041 memcpy(&v,&r,8);
001042 e = v>>52;
001043 if( (e&0x7ff)==0x7ff ){
001044 p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
001045 p->n = 0;
001046 p->iDP = 0;
001047 return;
001048 }
001049
001050 /* Multiply r by powers of ten until it lands somewhere in between
001051 ** 1.0e+19 and 1.0e+17.
001052 **
001053 ** Use Dekker-style double-double computation to increase the
001054 ** precision.
001055 **
001056 ** The error terms on constants like 1.0e+100 computed using the
001057 ** decimal extension, for example as follows:
001058 **
001059 ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
001060 */
001061 rr[0] = r;
001062 rr[1] = 0.0;
001063 if( rr[0]>9.223372036854774784e+18 ){
001064 while( rr[0]>9.223372036854774784e+118 ){
001065 exp += 100;
001066 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
001067 }
001068 while( rr[0]>9.223372036854774784e+28 ){
001069 exp += 10;
001070 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
001071 }
001072 while( rr[0]>9.223372036854774784e+18 ){
001073 exp += 1;
001074 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
001075 }
001076 }else{
001077 while( rr[0]<9.223372036854774784e-83 ){
001078 exp -= 100;
001079 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
001080 }
001081 while( rr[0]<9.223372036854774784e+07 ){
001082 exp -= 10;
001083 dekkerMul2(rr, 1.0e+10, 0.0);
001084 }
001085 while( rr[0]<9.22337203685477478e+17 ){
001086 exp -= 1;
001087 dekkerMul2(rr, 1.0e+01, 0.0);
001088 }
001089 }
001090 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
001091
001092 /* Extract significant digits. */
001093 i = sizeof(p->zBuf)-1;
001094 assert( v>0 );
001095 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; }
001096 assert( i>=0 && i<sizeof(p->zBuf)-1 );
001097 p->n = sizeof(p->zBuf) - 1 - i;
001098 assert( p->n>0 );
001099 assert( p->n<sizeof(p->zBuf) );
001100 p->iDP = p->n + exp;
001101 if( iRound<=0 ){
001102 iRound = p->iDP - iRound;
001103 if( iRound==0 && p->zBuf[i+1]>='5' ){
001104 iRound = 1;
001105 p->zBuf[i--] = '0';
001106 p->n++;
001107 p->iDP++;
001108 }
001109 }
001110 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
001111 char *z = &p->zBuf[i+1];
001112 if( iRound>mxRound ) iRound = mxRound;
001113 p->n = iRound;
001114 if( z[iRound]>='5' ){
001115 int j = iRound-1;
001116 while( 1 /*exit-by-break*/ ){
001117 z[j]++;
001118 if( z[j]<='9' ) break;
001119 z[j] = '0';
001120 if( j==0 ){
001121 p->z[i--] = '1';
001122 p->n++;
001123 p->iDP++;
001124 break;
001125 }else{
001126 j--;
001127 }
001128 }
001129 }
001130 }
001131 p->z = &p->zBuf[i+1];
001132 assert( i+p->n < sizeof(p->zBuf) );
001133 while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
001134 }
001135
001136 /*
001137 ** Try to convert z into an unsigned 32-bit integer. Return true on
001138 ** success and false if there is an error.
001139 **
001140 ** Only decimal notation is accepted.
001141 */
001142 int sqlite3GetUInt32(const char *z, u32 *pI){
001143 u64 v = 0;
001144 int i;
001145 for(i=0; sqlite3Isdigit(z[i]); i++){
001146 v = v*10 + z[i] - '0';
001147 if( v>4294967296LL ){ *pI = 0; return 0; }
001148 }
001149 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
001150 *pI = (u32)v;
001151 return 1;
001152 }
001153
001154 /*
001155 ** The variable-length integer encoding is as follows:
001156 **
001157 ** KEY:
001158 ** A = 0xxxxxxx 7 bits of data and one flag bit
001159 ** B = 1xxxxxxx 7 bits of data and one flag bit
001160 ** C = xxxxxxxx 8 bits of data
001161 **
001162 ** 7 bits - A
001163 ** 14 bits - BA
001164 ** 21 bits - BBA
001165 ** 28 bits - BBBA
001166 ** 35 bits - BBBBA
001167 ** 42 bits - BBBBBA
001168 ** 49 bits - BBBBBBA
001169 ** 56 bits - BBBBBBBA
001170 ** 64 bits - BBBBBBBBC
001171 */
001172
001173 /*
001174 ** Write a 64-bit variable-length integer to memory starting at p[0].
001175 ** The length of data write will be between 1 and 9 bytes. The number
001176 ** of bytes written is returned.
001177 **
001178 ** A variable-length integer consists of the lower 7 bits of each byte
001179 ** for all bytes that have the 8th bit set and one byte with the 8th
001180 ** bit clear. Except, if we get to the 9th byte, it stores the full
001181 ** 8 bits and is the last byte.
001182 */
001183 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
001184 int i, j, n;
001185 u8 buf[10];
001186 if( v & (((u64)0xff000000)<<32) ){
001187 p[8] = (u8)v;
001188 v >>= 8;
001189 for(i=7; i>=0; i--){
001190 p[i] = (u8)((v & 0x7f) | 0x80);
001191 v >>= 7;
001192 }
001193 return 9;
001194 }
001195 n = 0;
001196 do{
001197 buf[n++] = (u8)((v & 0x7f) | 0x80);
001198 v >>= 7;
001199 }while( v!=0 );
001200 buf[0] &= 0x7f;
001201 assert( n<=9 );
001202 for(i=0, j=n-1; j>=0; j--, i++){
001203 p[i] = buf[j];
001204 }
001205 return n;
001206 }
001207 int sqlite3PutVarint(unsigned char *p, u64 v){
001208 if( v<=0x7f ){
001209 p[0] = v&0x7f;
001210 return 1;
001211 }
001212 if( v<=0x3fff ){
001213 p[0] = ((v>>7)&0x7f)|0x80;
001214 p[1] = v&0x7f;
001215 return 2;
001216 }
001217 return putVarint64(p,v);
001218 }
001219
001220 /*
001221 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants
001222 ** are defined here rather than simply putting the constant expressions
001223 ** inline in order to work around bugs in the RVT compiler.
001224 **
001225 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
001226 **
001227 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
001228 */
001229 #define SLOT_2_0 0x001fc07f
001230 #define SLOT_4_2_0 0xf01fc07f
001231
001232
001233 /*
001234 ** Read a 64-bit variable-length integer from memory starting at p[0].
001235 ** Return the number of bytes read. The value is stored in *v.
001236 */
001237 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
001238 u32 a,b,s;
001239
001240 if( ((signed char*)p)[0]>=0 ){
001241 *v = *p;
001242 return 1;
001243 }
001244 if( ((signed char*)p)[1]>=0 ){
001245 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
001246 return 2;
001247 }
001248
001249 /* Verify that constants are precomputed correctly */
001250 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
001251 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
001252
001253 a = ((u32)p[0])<<14;
001254 b = p[1];
001255 p += 2;
001256 a |= *p;
001257 /* a: p0<<14 | p2 (unmasked) */
001258 if (!(a&0x80))
001259 {
001260 a &= SLOT_2_0;
001261 b &= 0x7f;
001262 b = b<<7;
001263 a |= b;
001264 *v = a;
001265 return 3;
001266 }
001267
001268 /* CSE1 from below */
001269 a &= SLOT_2_0;
001270 p++;
001271 b = b<<14;
001272 b |= *p;
001273 /* b: p1<<14 | p3 (unmasked) */
001274 if (!(b&0x80))
001275 {
001276 b &= SLOT_2_0;
001277 /* moved CSE1 up */
001278 /* a &= (0x7f<<14)|(0x7f); */
001279 a = a<<7;
001280 a |= b;
001281 *v = a;
001282 return 4;
001283 }
001284
001285 /* a: p0<<14 | p2 (masked) */
001286 /* b: p1<<14 | p3 (unmasked) */
001287 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001288 /* moved CSE1 up */
001289 /* a &= (0x7f<<14)|(0x7f); */
001290 b &= SLOT_2_0;
001291 s = a;
001292 /* s: p0<<14 | p2 (masked) */
001293
001294 p++;
001295 a = a<<14;
001296 a |= *p;
001297 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
001298 if (!(a&0x80))
001299 {
001300 /* we can skip these cause they were (effectively) done above
001301 ** while calculating s */
001302 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
001303 /* b &= (0x7f<<14)|(0x7f); */
001304 b = b<<7;
001305 a |= b;
001306 s = s>>18;
001307 *v = ((u64)s)<<32 | a;
001308 return 5;
001309 }
001310
001311 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001312 s = s<<7;
001313 s |= b;
001314 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001315
001316 p++;
001317 b = b<<14;
001318 b |= *p;
001319 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
001320 if (!(b&0x80))
001321 {
001322 /* we can skip this cause it was (effectively) done above in calc'ing s */
001323 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
001324 a &= SLOT_2_0;
001325 a = a<<7;
001326 a |= b;
001327 s = s>>18;
001328 *v = ((u64)s)<<32 | a;
001329 return 6;
001330 }
001331
001332 p++;
001333 a = a<<14;
001334 a |= *p;
001335 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
001336 if (!(a&0x80))
001337 {
001338 a &= SLOT_4_2_0;
001339 b &= SLOT_2_0;
001340 b = b<<7;
001341 a |= b;
001342 s = s>>11;
001343 *v = ((u64)s)<<32 | a;
001344 return 7;
001345 }
001346
001347 /* CSE2 from below */
001348 a &= SLOT_2_0;
001349 p++;
001350 b = b<<14;
001351 b |= *p;
001352 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
001353 if (!(b&0x80))
001354 {
001355 b &= SLOT_4_2_0;
001356 /* moved CSE2 up */
001357 /* a &= (0x7f<<14)|(0x7f); */
001358 a = a<<7;
001359 a |= b;
001360 s = s>>4;
001361 *v = ((u64)s)<<32 | a;
001362 return 8;
001363 }
001364
001365 p++;
001366 a = a<<15;
001367 a |= *p;
001368 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
001369
001370 /* moved CSE2 up */
001371 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
001372 b &= SLOT_2_0;
001373 b = b<<8;
001374 a |= b;
001375
001376 s = s<<4;
001377 b = p[-4];
001378 b &= 0x7f;
001379 b = b>>3;
001380 s |= b;
001381
001382 *v = ((u64)s)<<32 | a;
001383
001384 return 9;
001385 }
001386
001387 /*
001388 ** Read a 32-bit variable-length integer from memory starting at p[0].
001389 ** Return the number of bytes read. The value is stored in *v.
001390 **
001391 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
001392 ** integer, then set *v to 0xffffffff.
001393 **
001394 ** A MACRO version, getVarint32, is provided which inlines the
001395 ** single-byte case. All code should use the MACRO version as
001396 ** this function assumes the single-byte case has already been handled.
001397 */
001398 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
001399 u64 v64;
001400 u8 n;
001401
001402 /* Assume that the single-byte case has already been handled by
001403 ** the getVarint32() macro */
001404 assert( (p[0] & 0x80)!=0 );
001405
001406 if( (p[1] & 0x80)==0 ){
001407 /* This is the two-byte case */
001408 *v = ((p[0]&0x7f)<<7) | p[1];
001409 return 2;
001410 }
001411 if( (p[2] & 0x80)==0 ){
001412 /* This is the three-byte case */
001413 *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2];
001414 return 3;
001415 }
001416 /* four or more bytes */
001417 n = sqlite3GetVarint(p, &v64);
001418 assert( n>3 && n<=9 );
001419 if( (v64 & SQLITE_MAX_U32)!=v64 ){
001420 *v = 0xffffffff;
001421 }else{
001422 *v = (u32)v64;
001423 }
001424 return n;
001425 }
001426
001427 /*
001428 ** Return the number of bytes that will be needed to store the given
001429 ** 64-bit integer.
001430 */
001431 int sqlite3VarintLen(u64 v){
001432 int i;
001433 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
001434 return i;
001435 }
001436
001437
001438 /*
001439 ** Read or write a four-byte big-endian integer value.
001440 */
001441 u32 sqlite3Get4byte(const u8 *p){
001442 #if SQLITE_BYTEORDER==4321
001443 u32 x;
001444 memcpy(&x,p,4);
001445 return x;
001446 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
001447 u32 x;
001448 memcpy(&x,p,4);
001449 return __builtin_bswap32(x);
001450 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
001451 u32 x;
001452 memcpy(&x,p,4);
001453 return _byteswap_ulong(x);
001454 #else
001455 testcase( p[0]&0x80 );
001456 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
001457 #endif
001458 }
001459 void sqlite3Put4byte(unsigned char *p, u32 v){
001460 #if SQLITE_BYTEORDER==4321
001461 memcpy(p,&v,4);
001462 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
001463 u32 x = __builtin_bswap32(v);
001464 memcpy(p,&x,4);
001465 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
001466 u32 x = _byteswap_ulong(v);
001467 memcpy(p,&x,4);
001468 #else
001469 p[0] = (u8)(v>>24);
001470 p[1] = (u8)(v>>16);
001471 p[2] = (u8)(v>>8);
001472 p[3] = (u8)v;
001473 #endif
001474 }
001475
001476
001477
001478 /*
001479 ** Translate a single byte of Hex into an integer.
001480 ** This routine only works if h really is a valid hexadecimal
001481 ** character: 0..9a..fA..F
001482 */
001483 u8 sqlite3HexToInt(int h){
001484 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
001485 #ifdef SQLITE_ASCII
001486 h += 9*(1&(h>>6));
001487 #endif
001488 #ifdef SQLITE_EBCDIC
001489 h += 9*(1&~(h>>4));
001490 #endif
001491 return (u8)(h & 0xf);
001492 }
001493
001494 #if !defined(SQLITE_OMIT_BLOB_LITERAL)
001495 /*
001496 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
001497 ** value. Return a pointer to its binary value. Space to hold the
001498 ** binary value has been obtained from malloc and must be freed by
001499 ** the calling routine.
001500 */
001501 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
001502 char *zBlob;
001503 int i;
001504
001505 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
001506 n--;
001507 if( zBlob ){
001508 for(i=0; i<n; i+=2){
001509 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
001510 }
001511 zBlob[i/2] = 0;
001512 }
001513 return zBlob;
001514 }
001515 #endif /* !SQLITE_OMIT_BLOB_LITERAL */
001516
001517 /*
001518 ** Log an error that is an API call on a connection pointer that should
001519 ** not have been used. The "type" of connection pointer is given as the
001520 ** argument. The zType is a word like "NULL" or "closed" or "invalid".
001521 */
001522 static void logBadConnection(const char *zType){
001523 sqlite3_log(SQLITE_MISUSE,
001524 "API call with %s database connection pointer",
001525 zType
001526 );
001527 }
001528
001529 /*
001530 ** Check to make sure we have a valid db pointer. This test is not
001531 ** foolproof but it does provide some measure of protection against
001532 ** misuse of the interface such as passing in db pointers that are
001533 ** NULL or which have been previously closed. If this routine returns
001534 ** 1 it means that the db pointer is valid and 0 if it should not be
001535 ** dereferenced for any reason. The calling function should invoke
001536 ** SQLITE_MISUSE immediately.
001537 **
001538 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
001539 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
001540 ** open properly and is not fit for general use but which can be
001541 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
001542 */
001543 int sqlite3SafetyCheckOk(sqlite3 *db){
001544 u8 eOpenState;
001545 if( db==0 ){
001546 logBadConnection("NULL");
001547 return 0;
001548 }
001549 eOpenState = db->eOpenState;
001550 if( eOpenState!=SQLITE_STATE_OPEN ){
001551 if( sqlite3SafetyCheckSickOrOk(db) ){
001552 testcase( sqlite3GlobalConfig.xLog!=0 );
001553 logBadConnection("unopened");
001554 }
001555 return 0;
001556 }else{
001557 return 1;
001558 }
001559 }
001560 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
001561 u8 eOpenState;
001562 eOpenState = db->eOpenState;
001563 if( eOpenState!=SQLITE_STATE_SICK &&
001564 eOpenState!=SQLITE_STATE_OPEN &&
001565 eOpenState!=SQLITE_STATE_BUSY ){
001566 testcase( sqlite3GlobalConfig.xLog!=0 );
001567 logBadConnection("invalid");
001568 return 0;
001569 }else{
001570 return 1;
001571 }
001572 }
001573
001574 /*
001575 ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
001576 ** the other 64-bit signed integer at *pA and store the result in *pA.
001577 ** Return 0 on success. Or if the operation would have resulted in an
001578 ** overflow, leave *pA unchanged and return 1.
001579 */
001580 int sqlite3AddInt64(i64 *pA, i64 iB){
001581 #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001582 return __builtin_add_overflow(*pA, iB, pA);
001583 #else
001584 i64 iA = *pA;
001585 testcase( iA==0 ); testcase( iA==1 );
001586 testcase( iB==-1 ); testcase( iB==0 );
001587 if( iB>=0 ){
001588 testcase( iA>0 && LARGEST_INT64 - iA == iB );
001589 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
001590 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
001591 }else{
001592 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
001593 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
001594 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
001595 }
001596 *pA += iB;
001597 return 0;
001598 #endif
001599 }
001600 int sqlite3SubInt64(i64 *pA, i64 iB){
001601 #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001602 return __builtin_sub_overflow(*pA, iB, pA);
001603 #else
001604 testcase( iB==SMALLEST_INT64+1 );
001605 if( iB==SMALLEST_INT64 ){
001606 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
001607 if( (*pA)>=0 ) return 1;
001608 *pA -= iB;
001609 return 0;
001610 }else{
001611 return sqlite3AddInt64(pA, -iB);
001612 }
001613 #endif
001614 }
001615 int sqlite3MulInt64(i64 *pA, i64 iB){
001616 #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001617 return __builtin_mul_overflow(*pA, iB, pA);
001618 #else
001619 i64 iA = *pA;
001620 if( iB>0 ){
001621 if( iA>LARGEST_INT64/iB ) return 1;
001622 if( iA<SMALLEST_INT64/iB ) return 1;
001623 }else if( iB<0 ){
001624 if( iA>0 ){
001625 if( iB<SMALLEST_INT64/iA ) return 1;
001626 }else if( iA<0 ){
001627 if( iB==SMALLEST_INT64 ) return 1;
001628 if( iA==SMALLEST_INT64 ) return 1;
001629 if( -iA>LARGEST_INT64/-iB ) return 1;
001630 }
001631 }
001632 *pA = iA*iB;
001633 return 0;
001634 #endif
001635 }
001636
001637 /*
001638 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
001639 ** if the integer has a value of -2147483648, return +2147483647
001640 */
001641 int sqlite3AbsInt32(int x){
001642 if( x>=0 ) return x;
001643 if( x==(int)0x80000000 ) return 0x7fffffff;
001644 return -x;
001645 }
001646
001647 #ifdef SQLITE_ENABLE_8_3_NAMES
001648 /*
001649 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
001650 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
001651 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
001652 ** three characters, then shorten the suffix on z[] to be the last three
001653 ** characters of the original suffix.
001654 **
001655 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
001656 ** do the suffix shortening regardless of URI parameter.
001657 **
001658 ** Examples:
001659 **
001660 ** test.db-journal => test.nal
001661 ** test.db-wal => test.wal
001662 ** test.db-shm => test.shm
001663 ** test.db-mj7f3319fa => test.9fa
001664 */
001665 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
001666 #if SQLITE_ENABLE_8_3_NAMES<2
001667 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
001668 #endif
001669 {
001670 int i, sz;
001671 sz = sqlite3Strlen30(z);
001672 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
001673 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
001674 }
001675 }
001676 #endif
001677
001678 /*
001679 ** Find (an approximate) sum of two LogEst values. This computation is
001680 ** not a simple "+" operator because LogEst is stored as a logarithmic
001681 ** value.
001682 **
001683 */
001684 LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
001685 static const unsigned char x[] = {
001686 10, 10, /* 0,1 */
001687 9, 9, /* 2,3 */
001688 8, 8, /* 4,5 */
001689 7, 7, 7, /* 6,7,8 */
001690 6, 6, 6, /* 9,10,11 */
001691 5, 5, 5, /* 12-14 */
001692 4, 4, 4, 4, /* 15-18 */
001693 3, 3, 3, 3, 3, 3, /* 19-24 */
001694 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
001695 };
001696 if( a>=b ){
001697 if( a>b+49 ) return a;
001698 if( a>b+31 ) return a+1;
001699 return a+x[a-b];
001700 }else{
001701 if( b>a+49 ) return b;
001702 if( b>a+31 ) return b+1;
001703 return b+x[b-a];
001704 }
001705 }
001706
001707 /*
001708 ** Convert an integer into a LogEst. In other words, compute an
001709 ** approximation for 10*log2(x).
001710 */
001711 LogEst sqlite3LogEst(u64 x){
001712 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
001713 LogEst y = 40;
001714 if( x<8 ){
001715 if( x<2 ) return 0;
001716 while( x<8 ){ y -= 10; x <<= 1; }
001717 }else{
001718 #if GCC_VERSION>=5004000
001719 int i = 60 - __builtin_clzll(x);
001720 y += i*10;
001721 x >>= i;
001722 #else
001723 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
001724 while( x>15 ){ y += 10; x >>= 1; }
001725 #endif
001726 }
001727 return a[x&7] + y - 10;
001728 }
001729
001730 /*
001731 ** Convert a double into a LogEst
001732 ** In other words, compute an approximation for 10*log2(x).
001733 */
001734 LogEst sqlite3LogEstFromDouble(double x){
001735 u64 a;
001736 LogEst e;
001737 assert( sizeof(x)==8 && sizeof(a)==8 );
001738 if( x<=1 ) return 0;
001739 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
001740 memcpy(&a, &x, 8);
001741 e = (a>>52) - 1022;
001742 return e*10;
001743 }
001744
001745 /*
001746 ** Convert a LogEst into an integer.
001747 */
001748 u64 sqlite3LogEstToInt(LogEst x){
001749 u64 n;
001750 n = x%10;
001751 x /= 10;
001752 if( n>=5 ) n -= 2;
001753 else if( n>=1 ) n -= 1;
001754 if( x>60 ) return (u64)LARGEST_INT64;
001755 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
001756 }
001757
001758 /*
001759 ** Add a new name/number pair to a VList. This might require that the
001760 ** VList object be reallocated, so return the new VList. If an OOM
001761 ** error occurs, the original VList returned and the
001762 ** db->mallocFailed flag is set.
001763 **
001764 ** A VList is really just an array of integers. To destroy a VList,
001765 ** simply pass it to sqlite3DbFree().
001766 **
001767 ** The first integer is the number of integers allocated for the whole
001768 ** VList. The second integer is the number of integers actually used.
001769 ** Each name/number pair is encoded by subsequent groups of 3 or more
001770 ** integers.
001771 **
001772 ** Each name/number pair starts with two integers which are the numeric
001773 ** value for the pair and the size of the name/number pair, respectively.
001774 ** The text name overlays one or more following integers. The text name
001775 ** is always zero-terminated.
001776 **
001777 ** Conceptually:
001778 **
001779 ** struct VList {
001780 ** int nAlloc; // Number of allocated slots
001781 ** int nUsed; // Number of used slots
001782 ** struct VListEntry {
001783 ** int iValue; // Value for this entry
001784 ** int nSlot; // Slots used by this entry
001785 ** // ... variable name goes here
001786 ** } a[0];
001787 ** }
001788 **
001789 ** During code generation, pointers to the variable names within the
001790 ** VList are taken. When that happens, nAlloc is set to zero as an
001791 ** indication that the VList may never again be enlarged, since the
001792 ** accompanying realloc() would invalidate the pointers.
001793 */
001794 VList *sqlite3VListAdd(
001795 sqlite3 *db, /* The database connection used for malloc() */
001796 VList *pIn, /* The input VList. Might be NULL */
001797 const char *zName, /* Name of symbol to add */
001798 int nName, /* Bytes of text in zName */
001799 int iVal /* Value to associate with zName */
001800 ){
001801 int nInt; /* number of sizeof(int) objects needed for zName */
001802 char *z; /* Pointer to where zName will be stored */
001803 int i; /* Index in pIn[] where zName is stored */
001804
001805 nInt = nName/4 + 3;
001806 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
001807 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
001808 /* Enlarge the allocation */
001809 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
001810 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
001811 if( pOut==0 ) return pIn;
001812 if( pIn==0 ) pOut[1] = 2;
001813 pIn = pOut;
001814 pIn[0] = nAlloc;
001815 }
001816 i = pIn[1];
001817 pIn[i] = iVal;
001818 pIn[i+1] = nInt;
001819 z = (char*)&pIn[i+2];
001820 pIn[1] = i+nInt;
001821 assert( pIn[1]<=pIn[0] );
001822 memcpy(z, zName, nName);
001823 z[nName] = 0;
001824 return pIn;
001825 }
001826
001827 /*
001828 ** Return a pointer to the name of a variable in the given VList that
001829 ** has the value iVal. Or return a NULL if there is no such variable in
001830 ** the list
001831 */
001832 const char *sqlite3VListNumToName(VList *pIn, int iVal){
001833 int i, mx;
001834 if( pIn==0 ) return 0;
001835 mx = pIn[1];
001836 i = 2;
001837 do{
001838 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
001839 i += pIn[i+1];
001840 }while( i<mx );
001841 return 0;
001842 }
001843
001844 /*
001845 ** Return the number of the variable named zName, if it is in VList.
001846 ** or return 0 if there is no such variable.
001847 */
001848 int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
001849 int i, mx;
001850 if( pIn==0 ) return 0;
001851 mx = pIn[1];
001852 i = 2;
001853 do{
001854 const char *z = (const char*)&pIn[i+2];
001855 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
001856 i += pIn[i+1];
001857 }while( i<mx );
001858 return 0;
001859 }