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 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN 000021 # include <math.h> 000022 #endif 000023 000024 /* 000025 ** Routine needed to support the testcase() macro. 000026 */ 000027 #ifdef SQLITE_COVERAGE_TEST 000028 void sqlite3Coverage(int x){ 000029 static unsigned dummy = 0; 000030 dummy += (unsigned)x; 000031 } 000032 #endif 000033 000034 /* 000035 ** Give a callback to the test harness that can be used to simulate faults 000036 ** in places where it is difficult or expensive to do so purely by means 000037 ** of inputs. 000038 ** 000039 ** The intent of the integer argument is to let the fault simulator know 000040 ** which of multiple sqlite3FaultSim() calls has been hit. 000041 ** 000042 ** Return whatever integer value the test callback returns, or return 000043 ** SQLITE_OK if no test callback is installed. 000044 */ 000045 #ifndef SQLITE_UNTESTABLE 000046 int sqlite3FaultSim(int iTest){ 000047 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; 000048 return xCallback ? xCallback(iTest) : SQLITE_OK; 000049 } 000050 #endif 000051 000052 #ifndef SQLITE_OMIT_FLOATING_POINT 000053 /* 000054 ** Return true if the floating point value is Not a Number (NaN). 000055 ** 000056 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. 000057 ** Otherwise, we have our own implementation that works on most systems. 000058 */ 000059 int sqlite3IsNaN(double x){ 000060 int rc; /* The value return */ 000061 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN 000062 /* 000063 ** Systems that support the isnan() library function should probably 000064 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have 000065 ** found that many systems do not have a working isnan() function so 000066 ** this implementation is provided as an alternative. 000067 ** 000068 ** This NaN test sometimes fails if compiled on GCC with -ffast-math. 000069 ** On the other hand, the use of -ffast-math comes with the following 000070 ** warning: 000071 ** 000072 ** This option [-ffast-math] should never be turned on by any 000073 ** -O option since it can result in incorrect output for programs 000074 ** which depend on an exact implementation of IEEE or ISO 000075 ** rules/specifications for math functions. 000076 ** 000077 ** Under MSVC, this NaN test may fail if compiled with a floating- 000078 ** point precision mode other than /fp:precise. From the MSDN 000079 ** documentation: 000080 ** 000081 ** The compiler [with /fp:precise] will properly handle comparisons 000082 ** involving NaN. For example, x != x evaluates to true if x is NaN 000083 ** ... 000084 */ 000085 #ifdef __FAST_MATH__ 000086 # error SQLite will not work correctly with the -ffast-math option of GCC. 000087 #endif 000088 volatile double y = x; 000089 volatile double z = y; 000090 rc = (y!=z); 000091 #else /* if HAVE_ISNAN */ 000092 rc = isnan(x); 000093 #endif /* HAVE_ISNAN */ 000094 testcase( rc ); 000095 return rc; 000096 } 000097 #endif /* SQLITE_OMIT_FLOATING_POINT */ 000098 000099 /* 000100 ** Compute a string length that is limited to what can be stored in 000101 ** lower 30 bits of a 32-bit signed integer. 000102 ** 000103 ** The value returned will never be negative. Nor will it ever be greater 000104 ** than the actual length of the string. For very long strings (greater 000105 ** than 1GiB) the value returned might be less than the true string length. 000106 */ 000107 int sqlite3Strlen30(const char *z){ 000108 if( z==0 ) return 0; 000109 return 0x3fffffff & (int)strlen(z); 000110 } 000111 000112 /* 000113 ** Return the declared type of a column. Or return zDflt if the column 000114 ** has no declared type. 000115 ** 000116 ** The column type is an extra string stored after the zero-terminator on 000117 ** the column name if and only if the COLFLAG_HASTYPE flag is set. 000118 */ 000119 char *sqlite3ColumnType(Column *pCol, char *zDflt){ 000120 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt; 000121 return pCol->zName + strlen(pCol->zName) + 1; 000122 } 000123 000124 /* 000125 ** Helper function for sqlite3Error() - called rarely. Broken out into 000126 ** a separate routine to avoid unnecessary register saves on entry to 000127 ** sqlite3Error(). 000128 */ 000129 static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ 000130 if( db->pErr ) sqlite3ValueSetNull(db->pErr); 000131 sqlite3SystemError(db, err_code); 000132 } 000133 000134 /* 000135 ** Set the current error code to err_code and clear any prior error message. 000136 ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates 000137 ** that would be appropriate. 000138 */ 000139 void sqlite3Error(sqlite3 *db, int err_code){ 000140 assert( db!=0 ); 000141 db->errCode = err_code; 000142 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code); 000143 } 000144 000145 /* 000146 ** Load the sqlite3.iSysErrno field if that is an appropriate thing 000147 ** to do based on the SQLite error code in rc. 000148 */ 000149 void sqlite3SystemError(sqlite3 *db, int rc){ 000150 if( rc==SQLITE_IOERR_NOMEM ) return; 000151 rc &= 0xff; 000152 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ 000153 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); 000154 } 000155 } 000156 000157 /* 000158 ** Set the most recent error code and error string for the sqlite 000159 ** handle "db". The error code is set to "err_code". 000160 ** 000161 ** If it is not NULL, string zFormat specifies the format of the 000162 ** error string in the style of the printf functions: The following 000163 ** format characters are allowed: 000164 ** 000165 ** %s Insert a string 000166 ** %z A string that should be freed after use 000167 ** %d Insert an integer 000168 ** %T Insert a token 000169 ** %S Insert the first element of a SrcList 000170 ** 000171 ** zFormat and any string tokens that follow it are assumed to be 000172 ** encoded in UTF-8. 000173 ** 000174 ** To clear the most recent error for sqlite handle "db", sqlite3Error 000175 ** should be called with err_code set to SQLITE_OK and zFormat set 000176 ** to NULL. 000177 */ 000178 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ 000179 assert( db!=0 ); 000180 db->errCode = err_code; 000181 sqlite3SystemError(db, err_code); 000182 if( zFormat==0 ){ 000183 sqlite3Error(db, err_code); 000184 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ 000185 char *z; 000186 va_list ap; 000187 va_start(ap, zFormat); 000188 z = sqlite3VMPrintf(db, zFormat, ap); 000189 va_end(ap); 000190 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); 000191 } 000192 } 000193 000194 /* 000195 ** Add an error message to pParse->zErrMsg and increment pParse->nErr. 000196 ** The following formatting characters are allowed: 000197 ** 000198 ** %s Insert a string 000199 ** %z A string that should be freed after use 000200 ** %d Insert an integer 000201 ** %T Insert a token 000202 ** %S Insert the first element of a SrcList 000203 ** 000204 ** This function should be used to report any error that occurs while 000205 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The 000206 ** last thing the sqlite3_prepare() function does is copy the error 000207 ** stored by this function into the database handle using sqlite3Error(). 000208 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used 000209 ** during statement execution (sqlite3_step() etc.). 000210 */ 000211 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ 000212 char *zMsg; 000213 va_list ap; 000214 sqlite3 *db = pParse->db; 000215 va_start(ap, zFormat); 000216 zMsg = sqlite3VMPrintf(db, zFormat, ap); 000217 va_end(ap); 000218 if( db->suppressErr ){ 000219 sqlite3DbFree(db, zMsg); 000220 }else{ 000221 pParse->nErr++; 000222 sqlite3DbFree(db, pParse->zErrMsg); 000223 pParse->zErrMsg = zMsg; 000224 pParse->rc = SQLITE_ERROR; 000225 } 000226 } 000227 000228 /* 000229 ** Convert an SQL-style quoted string into a normal string by removing 000230 ** the quote characters. The conversion is done in-place. If the 000231 ** input does not begin with a quote character, then this routine 000232 ** is a no-op. 000233 ** 000234 ** The input string must be zero-terminated. A new zero-terminator 000235 ** is added to the dequoted string. 000236 ** 000237 ** The return value is -1 if no dequoting occurs or the length of the 000238 ** dequoted string, exclusive of the zero terminator, if dequoting does 000239 ** occur. 000240 ** 000241 ** 2002-Feb-14: This routine is extended to remove MS-Access style 000242 ** brackets from around identifiers. For example: "[a-b-c]" becomes 000243 ** "a-b-c". 000244 */ 000245 void sqlite3Dequote(char *z){ 000246 char quote; 000247 int i, j; 000248 if( z==0 ) return; 000249 quote = z[0]; 000250 if( !sqlite3Isquote(quote) ) return; 000251 if( quote=='[' ) quote = ']'; 000252 for(i=1, j=0;; i++){ 000253 assert( z[i] ); 000254 if( z[i]==quote ){ 000255 if( z[i+1]==quote ){ 000256 z[j++] = quote; 000257 i++; 000258 }else{ 000259 break; 000260 } 000261 }else{ 000262 z[j++] = z[i]; 000263 } 000264 } 000265 z[j] = 0; 000266 } 000267 000268 /* 000269 ** Generate a Token object from a string 000270 */ 000271 void sqlite3TokenInit(Token *p, char *z){ 000272 p->z = z; 000273 p->n = sqlite3Strlen30(z); 000274 } 000275 000276 /* Convenient short-hand */ 000277 #define UpperToLower sqlite3UpperToLower 000278 000279 /* 000280 ** Some systems have stricmp(). Others have strcasecmp(). Because 000281 ** there is no consistency, we will define our own. 000282 ** 000283 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and 000284 ** sqlite3_strnicmp() APIs allow applications and extensions to compare 000285 ** the contents of two buffers containing UTF-8 strings in a 000286 ** case-independent fashion, using the same definition of "case 000287 ** independence" that SQLite uses internally when comparing identifiers. 000288 */ 000289 int sqlite3_stricmp(const char *zLeft, const char *zRight){ 000290 if( zLeft==0 ){ 000291 return zRight ? -1 : 0; 000292 }else if( zRight==0 ){ 000293 return 1; 000294 } 000295 return sqlite3StrICmp(zLeft, zRight); 000296 } 000297 int sqlite3StrICmp(const char *zLeft, const char *zRight){ 000298 unsigned char *a, *b; 000299 int c; 000300 a = (unsigned char *)zLeft; 000301 b = (unsigned char *)zRight; 000302 for(;;){ 000303 c = (int)UpperToLower[*a] - (int)UpperToLower[*b]; 000304 if( c || *a==0 ) break; 000305 a++; 000306 b++; 000307 } 000308 return c; 000309 } 000310 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ 000311 register unsigned char *a, *b; 000312 if( zLeft==0 ){ 000313 return zRight ? -1 : 0; 000314 }else if( zRight==0 ){ 000315 return 1; 000316 } 000317 a = (unsigned char *)zLeft; 000318 b = (unsigned char *)zRight; 000319 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } 000320 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; 000321 } 000322 000323 /* 000324 ** The string z[] is an text representation of a real number. 000325 ** Convert this string to a double and write it into *pResult. 000326 ** 000327 ** The string z[] is length bytes in length (bytes, not characters) and 000328 ** uses the encoding enc. The string is not necessarily zero-terminated. 000329 ** 000330 ** Return TRUE if the result is a valid real number (or integer) and FALSE 000331 ** if the string is empty or contains extraneous text. Valid numbers 000332 ** are in one of these formats: 000333 ** 000334 ** [+-]digits[E[+-]digits] 000335 ** [+-]digits.[digits][E[+-]digits] 000336 ** [+-].digits[E[+-]digits] 000337 ** 000338 ** Leading and trailing whitespace is ignored for the purpose of determining 000339 ** validity. 000340 ** 000341 ** If some prefix of the input string is a valid number, this routine 000342 ** returns FALSE but it still converts the prefix and writes the result 000343 ** into *pResult. 000344 */ 000345 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ 000346 #ifndef SQLITE_OMIT_FLOATING_POINT 000347 int incr; 000348 const char *zEnd = z + length; 000349 /* sign * significand * (10 ^ (esign * exponent)) */ 000350 int sign = 1; /* sign of significand */ 000351 i64 s = 0; /* significand */ 000352 int d = 0; /* adjust exponent for shifting decimal point */ 000353 int esign = 1; /* sign of exponent */ 000354 int e = 0; /* exponent */ 000355 int eValid = 1; /* True exponent is either not used or is well-formed */ 000356 double result; 000357 int nDigits = 0; 000358 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ 000359 000360 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 000361 *pResult = 0.0; /* Default return value, in case of an error */ 000362 000363 if( enc==SQLITE_UTF8 ){ 000364 incr = 1; 000365 }else{ 000366 int i; 000367 incr = 2; 000368 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); 000369 for(i=3-enc; i<length && z[i]==0; i+=2){} 000370 nonNum = i<length; 000371 zEnd = &z[i^1]; 000372 z += (enc&1); 000373 } 000374 000375 /* skip leading spaces */ 000376 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; 000377 if( z>=zEnd ) return 0; 000378 000379 /* get sign of significand */ 000380 if( *z=='-' ){ 000381 sign = -1; 000382 z+=incr; 000383 }else if( *z=='+' ){ 000384 z+=incr; 000385 } 000386 000387 /* copy max significant digits to significand */ 000388 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ 000389 s = s*10 + (*z - '0'); 000390 z+=incr, nDigits++; 000391 } 000392 000393 /* skip non-significant significand digits 000394 ** (increase exponent by d to shift decimal left) */ 000395 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; 000396 if( z>=zEnd ) goto do_atof_calc; 000397 000398 /* if decimal point is present */ 000399 if( *z=='.' ){ 000400 z+=incr; 000401 /* copy digits from after decimal to significand 000402 ** (decrease exponent by d to shift decimal right) */ 000403 while( z<zEnd && sqlite3Isdigit(*z) ){ 000404 if( s<((LARGEST_INT64-9)/10) ){ 000405 s = s*10 + (*z - '0'); 000406 d--; 000407 } 000408 z+=incr, nDigits++; 000409 } 000410 } 000411 if( z>=zEnd ) goto do_atof_calc; 000412 000413 /* if exponent is present */ 000414 if( *z=='e' || *z=='E' ){ 000415 z+=incr; 000416 eValid = 0; 000417 000418 /* This branch is needed to avoid a (harmless) buffer overread. The 000419 ** special comment alerts the mutation tester that the correct answer 000420 ** is obtained even if the branch is omitted */ 000421 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ 000422 000423 /* get sign of exponent */ 000424 if( *z=='-' ){ 000425 esign = -1; 000426 z+=incr; 000427 }else if( *z=='+' ){ 000428 z+=incr; 000429 } 000430 /* copy digits to exponent */ 000431 while( z<zEnd && sqlite3Isdigit(*z) ){ 000432 e = e<10000 ? (e*10 + (*z - '0')) : 10000; 000433 z+=incr; 000434 eValid = 1; 000435 } 000436 } 000437 000438 /* skip trailing spaces */ 000439 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; 000440 000441 do_atof_calc: 000442 /* adjust exponent by d, and update sign */ 000443 e = (e*esign) + d; 000444 if( e<0 ) { 000445 esign = -1; 000446 e *= -1; 000447 } else { 000448 esign = 1; 000449 } 000450 000451 if( s==0 ) { 000452 /* In the IEEE 754 standard, zero is signed. */ 000453 result = sign<0 ? -(double)0 : (double)0; 000454 } else { 000455 /* Attempt to reduce exponent. 000456 ** 000457 ** Branches that are not required for the correct answer but which only 000458 ** help to obtain the correct answer faster are marked with special 000459 ** comments, as a hint to the mutation tester. 000460 */ 000461 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/ 000462 if( esign>0 ){ 000463 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ 000464 s *= 10; 000465 }else{ 000466 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ 000467 s /= 10; 000468 } 000469 e--; 000470 } 000471 000472 /* adjust the sign of significand */ 000473 s = sign<0 ? -s : s; 000474 000475 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ 000476 result = (double)s; 000477 }else{ 000478 LONGDOUBLE_TYPE scale = 1.0; 000479 /* attempt to handle extremely small/large numbers better */ 000480 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ 000481 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ 000482 while( e%308 ) { scale *= 1.0e+1; e -= 1; } 000483 if( esign<0 ){ 000484 result = s / scale; 000485 result /= 1.0e+308; 000486 }else{ 000487 result = s * scale; 000488 result *= 1.0e+308; 000489 } 000490 }else{ assert( e>=342 ); 000491 if( esign<0 ){ 000492 result = 0.0*s; 000493 }else{ 000494 result = 1e308*1e308*s; /* Infinity */ 000495 } 000496 } 000497 }else{ 000498 /* 1.0e+22 is the largest power of 10 than can be 000499 ** represented exactly. */ 000500 while( e%22 ) { scale *= 1.0e+1; e -= 1; } 000501 while( e>0 ) { scale *= 1.0e+22; e -= 22; } 000502 if( esign<0 ){ 000503 result = s / scale; 000504 }else{ 000505 result = s * scale; 000506 } 000507 } 000508 } 000509 } 000510 000511 /* store the result */ 000512 *pResult = result; 000513 000514 /* return true if number and no extra non-whitespace chracters after */ 000515 return z==zEnd && nDigits>0 && eValid && nonNum==0; 000516 #else 000517 return !sqlite3Atoi64(z, pResult, length, enc); 000518 #endif /* SQLITE_OMIT_FLOATING_POINT */ 000519 } 000520 000521 /* 000522 ** Compare the 19-character string zNum against the text representation 000523 ** value 2^63: 9223372036854775808. Return negative, zero, or positive 000524 ** if zNum is less than, equal to, or greater than the string. 000525 ** Note that zNum must contain exactly 19 characters. 000526 ** 000527 ** Unlike memcmp() this routine is guaranteed to return the difference 000528 ** in the values of the last digit if the only difference is in the 000529 ** last digit. So, for example, 000530 ** 000531 ** compare2pow63("9223372036854775800", 1) 000532 ** 000533 ** will return -8. 000534 */ 000535 static int compare2pow63(const char *zNum, int incr){ 000536 int c = 0; 000537 int i; 000538 /* 012345678901234567 */ 000539 const char *pow63 = "922337203685477580"; 000540 for(i=0; c==0 && i<18; i++){ 000541 c = (zNum[i*incr]-pow63[i])*10; 000542 } 000543 if( c==0 ){ 000544 c = zNum[18*incr] - '8'; 000545 testcase( c==(-1) ); 000546 testcase( c==0 ); 000547 testcase( c==(+1) ); 000548 } 000549 return c; 000550 } 000551 000552 /* 000553 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This 000554 ** routine does *not* accept hexadecimal notation. 000555 ** 000556 ** If the zNum value is representable as a 64-bit twos-complement 000557 ** integer, then write that value into *pNum and return 0. 000558 ** 000559 ** If zNum is exactly 9223372036854775808, return 2. This special 000560 ** case is broken out because while 9223372036854775808 cannot be a 000561 ** signed 64-bit integer, its negative -9223372036854775808 can be. 000562 ** 000563 ** If zNum is too big for a 64-bit integer and is not 000564 ** 9223372036854775808 or if zNum contains any non-numeric text, 000565 ** then return 1. 000566 ** 000567 ** length is the number of bytes in the string (bytes, not characters). 000568 ** The string is not necessarily zero-terminated. The encoding is 000569 ** given by enc. 000570 */ 000571 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ 000572 int incr; 000573 u64 u = 0; 000574 int neg = 0; /* assume positive */ 000575 int i; 000576 int c = 0; 000577 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ 000578 const char *zStart; 000579 const char *zEnd = zNum + length; 000580 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 000581 if( enc==SQLITE_UTF8 ){ 000582 incr = 1; 000583 }else{ 000584 incr = 2; 000585 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); 000586 for(i=3-enc; i<length && zNum[i]==0; i+=2){} 000587 nonNum = i<length; 000588 zEnd = &zNum[i^1]; 000589 zNum += (enc&1); 000590 } 000591 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; 000592 if( zNum<zEnd ){ 000593 if( *zNum=='-' ){ 000594 neg = 1; 000595 zNum+=incr; 000596 }else if( *zNum=='+' ){ 000597 zNum+=incr; 000598 } 000599 } 000600 zStart = zNum; 000601 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ 000602 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ 000603 u = u*10 + c - '0'; 000604 } 000605 if( u>LARGEST_INT64 ){ 000606 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; 000607 }else if( neg ){ 000608 *pNum = -(i64)u; 000609 }else{ 000610 *pNum = (i64)u; 000611 } 000612 testcase( i==18 ); 000613 testcase( i==19 ); 000614 testcase( i==20 ); 000615 if( &zNum[i]<zEnd /* Extra bytes at the end */ 000616 || (i==0 && zStart==zNum) /* No digits */ 000617 || i>19*incr /* Too many digits */ 000618 || nonNum /* UTF16 with high-order bytes non-zero */ 000619 ){ 000620 /* zNum is empty or contains non-numeric text or is longer 000621 ** than 19 digits (thus guaranteeing that it is too large) */ 000622 return 1; 000623 }else if( i<19*incr ){ 000624 /* Less than 19 digits, so we know that it fits in 64 bits */ 000625 assert( u<=LARGEST_INT64 ); 000626 return 0; 000627 }else{ 000628 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ 000629 c = compare2pow63(zNum, incr); 000630 if( c<0 ){ 000631 /* zNum is less than 9223372036854775808 so it fits */ 000632 assert( u<=LARGEST_INT64 ); 000633 return 0; 000634 }else if( c>0 ){ 000635 /* zNum is greater than 9223372036854775808 so it overflows */ 000636 return 1; 000637 }else{ 000638 /* zNum is exactly 9223372036854775808. Fits if negative. The 000639 ** special case 2 overflow if positive */ 000640 assert( u-1==LARGEST_INT64 ); 000641 return neg ? 0 : 2; 000642 } 000643 } 000644 } 000645 000646 /* 000647 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, 000648 ** into a 64-bit signed integer. This routine accepts hexadecimal literals, 000649 ** whereas sqlite3Atoi64() does not. 000650 ** 000651 ** Returns: 000652 ** 000653 ** 0 Successful transformation. Fits in a 64-bit signed integer. 000654 ** 1 Integer too large for a 64-bit signed integer or is malformed 000655 ** 2 Special case of 9223372036854775808 000656 */ 000657 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ 000658 #ifndef SQLITE_OMIT_HEX_INTEGER 000659 if( z[0]=='0' 000660 && (z[1]=='x' || z[1]=='X') 000661 ){ 000662 u64 u = 0; 000663 int i, k; 000664 for(i=2; z[i]=='0'; i++){} 000665 for(k=i; sqlite3Isxdigit(z[k]); k++){ 000666 u = u*16 + sqlite3HexToInt(z[k]); 000667 } 000668 memcpy(pOut, &u, 8); 000669 return (z[k]==0 && k-i<=16) ? 0 : 1; 000670 }else 000671 #endif /* SQLITE_OMIT_HEX_INTEGER */ 000672 { 000673 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); 000674 } 000675 } 000676 000677 /* 000678 ** If zNum represents an integer that will fit in 32-bits, then set 000679 ** *pValue to that integer and return true. Otherwise return false. 000680 ** 000681 ** This routine accepts both decimal and hexadecimal notation for integers. 000682 ** 000683 ** Any non-numeric characters that following zNum are ignored. 000684 ** This is different from sqlite3Atoi64() which requires the 000685 ** input number to be zero-terminated. 000686 */ 000687 int sqlite3GetInt32(const char *zNum, int *pValue){ 000688 sqlite_int64 v = 0; 000689 int i, c; 000690 int neg = 0; 000691 if( zNum[0]=='-' ){ 000692 neg = 1; 000693 zNum++; 000694 }else if( zNum[0]=='+' ){ 000695 zNum++; 000696 } 000697 #ifndef SQLITE_OMIT_HEX_INTEGER 000698 else if( zNum[0]=='0' 000699 && (zNum[1]=='x' || zNum[1]=='X') 000700 && sqlite3Isxdigit(zNum[2]) 000701 ){ 000702 u32 u = 0; 000703 zNum += 2; 000704 while( zNum[0]=='0' ) zNum++; 000705 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ 000706 u = u*16 + sqlite3HexToInt(zNum[i]); 000707 } 000708 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ 000709 memcpy(pValue, &u, 4); 000710 return 1; 000711 }else{ 000712 return 0; 000713 } 000714 } 000715 #endif 000716 while( zNum[0]=='0' ) zNum++; 000717 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ 000718 v = v*10 + c; 000719 } 000720 000721 /* The longest decimal representation of a 32 bit integer is 10 digits: 000722 ** 000723 ** 1234567890 000724 ** 2^31 -> 2147483648 000725 */ 000726 testcase( i==10 ); 000727 if( i>10 ){ 000728 return 0; 000729 } 000730 testcase( v-neg==2147483647 ); 000731 if( v-neg>2147483647 ){ 000732 return 0; 000733 } 000734 if( neg ){ 000735 v = -v; 000736 } 000737 *pValue = (int)v; 000738 return 1; 000739 } 000740 000741 /* 000742 ** Return a 32-bit integer value extracted from a string. If the 000743 ** string is not an integer, just return 0. 000744 */ 000745 int sqlite3Atoi(const char *z){ 000746 int x = 0; 000747 if( z ) sqlite3GetInt32(z, &x); 000748 return x; 000749 } 000750 000751 /* 000752 ** The variable-length integer encoding is as follows: 000753 ** 000754 ** KEY: 000755 ** A = 0xxxxxxx 7 bits of data and one flag bit 000756 ** B = 1xxxxxxx 7 bits of data and one flag bit 000757 ** C = xxxxxxxx 8 bits of data 000758 ** 000759 ** 7 bits - A 000760 ** 14 bits - BA 000761 ** 21 bits - BBA 000762 ** 28 bits - BBBA 000763 ** 35 bits - BBBBA 000764 ** 42 bits - BBBBBA 000765 ** 49 bits - BBBBBBA 000766 ** 56 bits - BBBBBBBA 000767 ** 64 bits - BBBBBBBBC 000768 */ 000769 000770 /* 000771 ** Write a 64-bit variable-length integer to memory starting at p[0]. 000772 ** The length of data write will be between 1 and 9 bytes. The number 000773 ** of bytes written is returned. 000774 ** 000775 ** A variable-length integer consists of the lower 7 bits of each byte 000776 ** for all bytes that have the 8th bit set and one byte with the 8th 000777 ** bit clear. Except, if we get to the 9th byte, it stores the full 000778 ** 8 bits and is the last byte. 000779 */ 000780 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ 000781 int i, j, n; 000782 u8 buf[10]; 000783 if( v & (((u64)0xff000000)<<32) ){ 000784 p[8] = (u8)v; 000785 v >>= 8; 000786 for(i=7; i>=0; i--){ 000787 p[i] = (u8)((v & 0x7f) | 0x80); 000788 v >>= 7; 000789 } 000790 return 9; 000791 } 000792 n = 0; 000793 do{ 000794 buf[n++] = (u8)((v & 0x7f) | 0x80); 000795 v >>= 7; 000796 }while( v!=0 ); 000797 buf[0] &= 0x7f; 000798 assert( n<=9 ); 000799 for(i=0, j=n-1; j>=0; j--, i++){ 000800 p[i] = buf[j]; 000801 } 000802 return n; 000803 } 000804 int sqlite3PutVarint(unsigned char *p, u64 v){ 000805 if( v<=0x7f ){ 000806 p[0] = v&0x7f; 000807 return 1; 000808 } 000809 if( v<=0x3fff ){ 000810 p[0] = ((v>>7)&0x7f)|0x80; 000811 p[1] = v&0x7f; 000812 return 2; 000813 } 000814 return putVarint64(p,v); 000815 } 000816 000817 /* 000818 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants 000819 ** are defined here rather than simply putting the constant expressions 000820 ** inline in order to work around bugs in the RVT compiler. 000821 ** 000822 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f 000823 ** 000824 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 000825 */ 000826 #define SLOT_2_0 0x001fc07f 000827 #define SLOT_4_2_0 0xf01fc07f 000828 000829 000830 /* 000831 ** Read a 64-bit variable-length integer from memory starting at p[0]. 000832 ** Return the number of bytes read. The value is stored in *v. 000833 */ 000834 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ 000835 u32 a,b,s; 000836 000837 a = *p; 000838 /* a: p0 (unmasked) */ 000839 if (!(a&0x80)) 000840 { 000841 *v = a; 000842 return 1; 000843 } 000844 000845 p++; 000846 b = *p; 000847 /* b: p1 (unmasked) */ 000848 if (!(b&0x80)) 000849 { 000850 a &= 0x7f; 000851 a = a<<7; 000852 a |= b; 000853 *v = a; 000854 return 2; 000855 } 000856 000857 /* Verify that constants are precomputed correctly */ 000858 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); 000859 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); 000860 000861 p++; 000862 a = a<<14; 000863 a |= *p; 000864 /* a: p0<<14 | p2 (unmasked) */ 000865 if (!(a&0x80)) 000866 { 000867 a &= SLOT_2_0; 000868 b &= 0x7f; 000869 b = b<<7; 000870 a |= b; 000871 *v = a; 000872 return 3; 000873 } 000874 000875 /* CSE1 from below */ 000876 a &= SLOT_2_0; 000877 p++; 000878 b = b<<14; 000879 b |= *p; 000880 /* b: p1<<14 | p3 (unmasked) */ 000881 if (!(b&0x80)) 000882 { 000883 b &= SLOT_2_0; 000884 /* moved CSE1 up */ 000885 /* a &= (0x7f<<14)|(0x7f); */ 000886 a = a<<7; 000887 a |= b; 000888 *v = a; 000889 return 4; 000890 } 000891 000892 /* a: p0<<14 | p2 (masked) */ 000893 /* b: p1<<14 | p3 (unmasked) */ 000894 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 000895 /* moved CSE1 up */ 000896 /* a &= (0x7f<<14)|(0x7f); */ 000897 b &= SLOT_2_0; 000898 s = a; 000899 /* s: p0<<14 | p2 (masked) */ 000900 000901 p++; 000902 a = a<<14; 000903 a |= *p; 000904 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ 000905 if (!(a&0x80)) 000906 { 000907 /* we can skip these cause they were (effectively) done above 000908 ** while calculating s */ 000909 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ 000910 /* b &= (0x7f<<14)|(0x7f); */ 000911 b = b<<7; 000912 a |= b; 000913 s = s>>18; 000914 *v = ((u64)s)<<32 | a; 000915 return 5; 000916 } 000917 000918 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 000919 s = s<<7; 000920 s |= b; 000921 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 000922 000923 p++; 000924 b = b<<14; 000925 b |= *p; 000926 /* b: p1<<28 | p3<<14 | p5 (unmasked) */ 000927 if (!(b&0x80)) 000928 { 000929 /* we can skip this cause it was (effectively) done above in calc'ing s */ 000930 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ 000931 a &= SLOT_2_0; 000932 a = a<<7; 000933 a |= b; 000934 s = s>>18; 000935 *v = ((u64)s)<<32 | a; 000936 return 6; 000937 } 000938 000939 p++; 000940 a = a<<14; 000941 a |= *p; 000942 /* a: p2<<28 | p4<<14 | p6 (unmasked) */ 000943 if (!(a&0x80)) 000944 { 000945 a &= SLOT_4_2_0; 000946 b &= SLOT_2_0; 000947 b = b<<7; 000948 a |= b; 000949 s = s>>11; 000950 *v = ((u64)s)<<32 | a; 000951 return 7; 000952 } 000953 000954 /* CSE2 from below */ 000955 a &= SLOT_2_0; 000956 p++; 000957 b = b<<14; 000958 b |= *p; 000959 /* b: p3<<28 | p5<<14 | p7 (unmasked) */ 000960 if (!(b&0x80)) 000961 { 000962 b &= SLOT_4_2_0; 000963 /* moved CSE2 up */ 000964 /* a &= (0x7f<<14)|(0x7f); */ 000965 a = a<<7; 000966 a |= b; 000967 s = s>>4; 000968 *v = ((u64)s)<<32 | a; 000969 return 8; 000970 } 000971 000972 p++; 000973 a = a<<15; 000974 a |= *p; 000975 /* a: p4<<29 | p6<<15 | p8 (unmasked) */ 000976 000977 /* moved CSE2 up */ 000978 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ 000979 b &= SLOT_2_0; 000980 b = b<<8; 000981 a |= b; 000982 000983 s = s<<4; 000984 b = p[-4]; 000985 b &= 0x7f; 000986 b = b>>3; 000987 s |= b; 000988 000989 *v = ((u64)s)<<32 | a; 000990 000991 return 9; 000992 } 000993 000994 /* 000995 ** Read a 32-bit variable-length integer from memory starting at p[0]. 000996 ** Return the number of bytes read. The value is stored in *v. 000997 ** 000998 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned 000999 ** integer, then set *v to 0xffffffff. 001000 ** 001001 ** A MACRO version, getVarint32, is provided which inlines the 001002 ** single-byte case. All code should use the MACRO version as 001003 ** this function assumes the single-byte case has already been handled. 001004 */ 001005 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ 001006 u32 a,b; 001007 001008 /* The 1-byte case. Overwhelmingly the most common. Handled inline 001009 ** by the getVarin32() macro */ 001010 a = *p; 001011 /* a: p0 (unmasked) */ 001012 #ifndef getVarint32 001013 if (!(a&0x80)) 001014 { 001015 /* Values between 0 and 127 */ 001016 *v = a; 001017 return 1; 001018 } 001019 #endif 001020 001021 /* The 2-byte case */ 001022 p++; 001023 b = *p; 001024 /* b: p1 (unmasked) */ 001025 if (!(b&0x80)) 001026 { 001027 /* Values between 128 and 16383 */ 001028 a &= 0x7f; 001029 a = a<<7; 001030 *v = a | b; 001031 return 2; 001032 } 001033 001034 /* The 3-byte case */ 001035 p++; 001036 a = a<<14; 001037 a |= *p; 001038 /* a: p0<<14 | p2 (unmasked) */ 001039 if (!(a&0x80)) 001040 { 001041 /* Values between 16384 and 2097151 */ 001042 a &= (0x7f<<14)|(0x7f); 001043 b &= 0x7f; 001044 b = b<<7; 001045 *v = a | b; 001046 return 3; 001047 } 001048 001049 /* A 32-bit varint is used to store size information in btrees. 001050 ** Objects are rarely larger than 2MiB limit of a 3-byte varint. 001051 ** A 3-byte varint is sufficient, for example, to record the size 001052 ** of a 1048569-byte BLOB or string. 001053 ** 001054 ** We only unroll the first 1-, 2-, and 3- byte cases. The very 001055 ** rare larger cases can be handled by the slower 64-bit varint 001056 ** routine. 001057 */ 001058 #if 1 001059 { 001060 u64 v64; 001061 u8 n; 001062 001063 p -= 2; 001064 n = sqlite3GetVarint(p, &v64); 001065 assert( n>3 && n<=9 ); 001066 if( (v64 & SQLITE_MAX_U32)!=v64 ){ 001067 *v = 0xffffffff; 001068 }else{ 001069 *v = (u32)v64; 001070 } 001071 return n; 001072 } 001073 001074 #else 001075 /* For following code (kept for historical record only) shows an 001076 ** unrolling for the 3- and 4-byte varint cases. This code is 001077 ** slightly faster, but it is also larger and much harder to test. 001078 */ 001079 p++; 001080 b = b<<14; 001081 b |= *p; 001082 /* b: p1<<14 | p3 (unmasked) */ 001083 if (!(b&0x80)) 001084 { 001085 /* Values between 2097152 and 268435455 */ 001086 b &= (0x7f<<14)|(0x7f); 001087 a &= (0x7f<<14)|(0x7f); 001088 a = a<<7; 001089 *v = a | b; 001090 return 4; 001091 } 001092 001093 p++; 001094 a = a<<14; 001095 a |= *p; 001096 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ 001097 if (!(a&0x80)) 001098 { 001099 /* Values between 268435456 and 34359738367 */ 001100 a &= SLOT_4_2_0; 001101 b &= SLOT_4_2_0; 001102 b = b<<7; 001103 *v = a | b; 001104 return 5; 001105 } 001106 001107 /* We can only reach this point when reading a corrupt database 001108 ** file. In that case we are not in any hurry. Use the (relatively 001109 ** slow) general-purpose sqlite3GetVarint() routine to extract the 001110 ** value. */ 001111 { 001112 u64 v64; 001113 u8 n; 001114 001115 p -= 4; 001116 n = sqlite3GetVarint(p, &v64); 001117 assert( n>5 && n<=9 ); 001118 *v = (u32)v64; 001119 return n; 001120 } 001121 #endif 001122 } 001123 001124 /* 001125 ** Return the number of bytes that will be needed to store the given 001126 ** 64-bit integer. 001127 */ 001128 int sqlite3VarintLen(u64 v){ 001129 int i; 001130 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } 001131 return i; 001132 } 001133 001134 001135 /* 001136 ** Read or write a four-byte big-endian integer value. 001137 */ 001138 u32 sqlite3Get4byte(const u8 *p){ 001139 #if SQLITE_BYTEORDER==4321 001140 u32 x; 001141 memcpy(&x,p,4); 001142 return x; 001143 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 001144 && defined(__GNUC__) && GCC_VERSION>=4003000 001145 u32 x; 001146 memcpy(&x,p,4); 001147 return __builtin_bswap32(x); 001148 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 001149 && defined(_MSC_VER) && _MSC_VER>=1300 001150 u32 x; 001151 memcpy(&x,p,4); 001152 return _byteswap_ulong(x); 001153 #else 001154 testcase( p[0]&0x80 ); 001155 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; 001156 #endif 001157 } 001158 void sqlite3Put4byte(unsigned char *p, u32 v){ 001159 #if SQLITE_BYTEORDER==4321 001160 memcpy(p,&v,4); 001161 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 001162 && defined(__GNUC__) && GCC_VERSION>=4003000 001163 u32 x = __builtin_bswap32(v); 001164 memcpy(p,&x,4); 001165 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 001166 && defined(_MSC_VER) && _MSC_VER>=1300 001167 u32 x = _byteswap_ulong(v); 001168 memcpy(p,&x,4); 001169 #else 001170 p[0] = (u8)(v>>24); 001171 p[1] = (u8)(v>>16); 001172 p[2] = (u8)(v>>8); 001173 p[3] = (u8)v; 001174 #endif 001175 } 001176 001177 001178 001179 /* 001180 ** Translate a single byte of Hex into an integer. 001181 ** This routine only works if h really is a valid hexadecimal 001182 ** character: 0..9a..fA..F 001183 */ 001184 u8 sqlite3HexToInt(int h){ 001185 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); 001186 #ifdef SQLITE_ASCII 001187 h += 9*(1&(h>>6)); 001188 #endif 001189 #ifdef SQLITE_EBCDIC 001190 h += 9*(1&~(h>>4)); 001191 #endif 001192 return (u8)(h & 0xf); 001193 } 001194 001195 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) 001196 /* 001197 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary 001198 ** value. Return a pointer to its binary value. Space to hold the 001199 ** binary value has been obtained from malloc and must be freed by 001200 ** the calling routine. 001201 */ 001202 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ 001203 char *zBlob; 001204 int i; 001205 001206 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); 001207 n--; 001208 if( zBlob ){ 001209 for(i=0; i<n; i+=2){ 001210 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); 001211 } 001212 zBlob[i/2] = 0; 001213 } 001214 return zBlob; 001215 } 001216 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ 001217 001218 /* 001219 ** Log an error that is an API call on a connection pointer that should 001220 ** not have been used. The "type" of connection pointer is given as the 001221 ** argument. The zType is a word like "NULL" or "closed" or "invalid". 001222 */ 001223 static void logBadConnection(const char *zType){ 001224 sqlite3_log(SQLITE_MISUSE, 001225 "API call with %s database connection pointer", 001226 zType 001227 ); 001228 } 001229 001230 /* 001231 ** Check to make sure we have a valid db pointer. This test is not 001232 ** foolproof but it does provide some measure of protection against 001233 ** misuse of the interface such as passing in db pointers that are 001234 ** NULL or which have been previously closed. If this routine returns 001235 ** 1 it means that the db pointer is valid and 0 if it should not be 001236 ** dereferenced for any reason. The calling function should invoke 001237 ** SQLITE_MISUSE immediately. 001238 ** 001239 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for 001240 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to 001241 ** open properly and is not fit for general use but which can be 001242 ** used as an argument to sqlite3_errmsg() or sqlite3_close(). 001243 */ 001244 int sqlite3SafetyCheckOk(sqlite3 *db){ 001245 u32 magic; 001246 if( db==0 ){ 001247 logBadConnection("NULL"); 001248 return 0; 001249 } 001250 magic = db->magic; 001251 if( magic!=SQLITE_MAGIC_OPEN ){ 001252 if( sqlite3SafetyCheckSickOrOk(db) ){ 001253 testcase( sqlite3GlobalConfig.xLog!=0 ); 001254 logBadConnection("unopened"); 001255 } 001256 return 0; 001257 }else{ 001258 return 1; 001259 } 001260 } 001261 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ 001262 u32 magic; 001263 magic = db->magic; 001264 if( magic!=SQLITE_MAGIC_SICK && 001265 magic!=SQLITE_MAGIC_OPEN && 001266 magic!=SQLITE_MAGIC_BUSY ){ 001267 testcase( sqlite3GlobalConfig.xLog!=0 ); 001268 logBadConnection("invalid"); 001269 return 0; 001270 }else{ 001271 return 1; 001272 } 001273 } 001274 001275 /* 001276 ** Attempt to add, substract, or multiply the 64-bit signed value iB against 001277 ** the other 64-bit signed integer at *pA and store the result in *pA. 001278 ** Return 0 on success. Or if the operation would have resulted in an 001279 ** overflow, leave *pA unchanged and return 1. 001280 */ 001281 int sqlite3AddInt64(i64 *pA, i64 iB){ 001282 i64 iA = *pA; 001283 testcase( iA==0 ); testcase( iA==1 ); 001284 testcase( iB==-1 ); testcase( iB==0 ); 001285 if( iB>=0 ){ 001286 testcase( iA>0 && LARGEST_INT64 - iA == iB ); 001287 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); 001288 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; 001289 }else{ 001290 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); 001291 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); 001292 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; 001293 } 001294 *pA += iB; 001295 return 0; 001296 } 001297 int sqlite3SubInt64(i64 *pA, i64 iB){ 001298 testcase( iB==SMALLEST_INT64+1 ); 001299 if( iB==SMALLEST_INT64 ){ 001300 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); 001301 if( (*pA)>=0 ) return 1; 001302 *pA -= iB; 001303 return 0; 001304 }else{ 001305 return sqlite3AddInt64(pA, -iB); 001306 } 001307 } 001308 int sqlite3MulInt64(i64 *pA, i64 iB){ 001309 i64 iA = *pA; 001310 if( iB>0 ){ 001311 if( iA>LARGEST_INT64/iB ) return 1; 001312 if( iA<SMALLEST_INT64/iB ) return 1; 001313 }else if( iB<0 ){ 001314 if( iA>0 ){ 001315 if( iB<SMALLEST_INT64/iA ) return 1; 001316 }else if( iA<0 ){ 001317 if( iB==SMALLEST_INT64 ) return 1; 001318 if( iA==SMALLEST_INT64 ) return 1; 001319 if( -iA>LARGEST_INT64/-iB ) return 1; 001320 } 001321 } 001322 *pA = iA*iB; 001323 return 0; 001324 } 001325 001326 /* 001327 ** Compute the absolute value of a 32-bit signed integer, of possible. Or 001328 ** if the integer has a value of -2147483648, return +2147483647 001329 */ 001330 int sqlite3AbsInt32(int x){ 001331 if( x>=0 ) return x; 001332 if( x==(int)0x80000000 ) return 0x7fffffff; 001333 return -x; 001334 } 001335 001336 #ifdef SQLITE_ENABLE_8_3_NAMES 001337 /* 001338 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database 001339 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and 001340 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than 001341 ** three characters, then shorten the suffix on z[] to be the last three 001342 ** characters of the original suffix. 001343 ** 001344 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always 001345 ** do the suffix shortening regardless of URI parameter. 001346 ** 001347 ** Examples: 001348 ** 001349 ** test.db-journal => test.nal 001350 ** test.db-wal => test.wal 001351 ** test.db-shm => test.shm 001352 ** test.db-mj7f3319fa => test.9fa 001353 */ 001354 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ 001355 #if SQLITE_ENABLE_8_3_NAMES<2 001356 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) 001357 #endif 001358 { 001359 int i, sz; 001360 sz = sqlite3Strlen30(z); 001361 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} 001362 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); 001363 } 001364 } 001365 #endif 001366 001367 /* 001368 ** Find (an approximate) sum of two LogEst values. This computation is 001369 ** not a simple "+" operator because LogEst is stored as a logarithmic 001370 ** value. 001371 ** 001372 */ 001373 LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ 001374 static const unsigned char x[] = { 001375 10, 10, /* 0,1 */ 001376 9, 9, /* 2,3 */ 001377 8, 8, /* 4,5 */ 001378 7, 7, 7, /* 6,7,8 */ 001379 6, 6, 6, /* 9,10,11 */ 001380 5, 5, 5, /* 12-14 */ 001381 4, 4, 4, 4, /* 15-18 */ 001382 3, 3, 3, 3, 3, 3, /* 19-24 */ 001383 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ 001384 }; 001385 if( a>=b ){ 001386 if( a>b+49 ) return a; 001387 if( a>b+31 ) return a+1; 001388 return a+x[a-b]; 001389 }else{ 001390 if( b>a+49 ) return b; 001391 if( b>a+31 ) return b+1; 001392 return b+x[b-a]; 001393 } 001394 } 001395 001396 /* 001397 ** Convert an integer into a LogEst. In other words, compute an 001398 ** approximation for 10*log2(x). 001399 */ 001400 LogEst sqlite3LogEst(u64 x){ 001401 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; 001402 LogEst y = 40; 001403 if( x<8 ){ 001404 if( x<2 ) return 0; 001405 while( x<8 ){ y -= 10; x <<= 1; } 001406 }else{ 001407 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ 001408 while( x>15 ){ y += 10; x >>= 1; } 001409 } 001410 return a[x&7] + y - 10; 001411 } 001412 001413 #ifndef SQLITE_OMIT_VIRTUALTABLE 001414 /* 001415 ** Convert a double into a LogEst 001416 ** In other words, compute an approximation for 10*log2(x). 001417 */ 001418 LogEst sqlite3LogEstFromDouble(double x){ 001419 u64 a; 001420 LogEst e; 001421 assert( sizeof(x)==8 && sizeof(a)==8 ); 001422 if( x<=1 ) return 0; 001423 if( x<=2000000000 ) return sqlite3LogEst((u64)x); 001424 memcpy(&a, &x, 8); 001425 e = (a>>52) - 1022; 001426 return e*10; 001427 } 001428 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 001429 001430 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ 001431 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ 001432 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) 001433 /* 001434 ** Convert a LogEst into an integer. 001435 ** 001436 ** Note that this routine is only used when one or more of various 001437 ** non-standard compile-time options is enabled. 001438 */ 001439 u64 sqlite3LogEstToInt(LogEst x){ 001440 u64 n; 001441 n = x%10; 001442 x /= 10; 001443 if( n>=5 ) n -= 2; 001444 else if( n>=1 ) n -= 1; 001445 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ 001446 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) 001447 if( x>60 ) return (u64)LARGEST_INT64; 001448 #else 001449 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input 001450 ** possible to this routine is 310, resulting in a maximum x of 31 */ 001451 assert( x<=60 ); 001452 #endif 001453 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); 001454 } 001455 #endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */ 001456 001457 /* 001458 ** Add a new name/number pair to a VList. This might require that the 001459 ** VList object be reallocated, so return the new VList. If an OOM 001460 ** error occurs, the original VList returned and the 001461 ** db->mallocFailed flag is set. 001462 ** 001463 ** A VList is really just an array of integers. To destroy a VList, 001464 ** simply pass it to sqlite3DbFree(). 001465 ** 001466 ** The first integer is the number of integers allocated for the whole 001467 ** VList. The second integer is the number of integers actually used. 001468 ** Each name/number pair is encoded by subsequent groups of 3 or more 001469 ** integers. 001470 ** 001471 ** Each name/number pair starts with two integers which are the numeric 001472 ** value for the pair and the size of the name/number pair, respectively. 001473 ** The text name overlays one or more following integers. The text name 001474 ** is always zero-terminated. 001475 ** 001476 ** Conceptually: 001477 ** 001478 ** struct VList { 001479 ** int nAlloc; // Number of allocated slots 001480 ** int nUsed; // Number of used slots 001481 ** struct VListEntry { 001482 ** int iValue; // Value for this entry 001483 ** int nSlot; // Slots used by this entry 001484 ** // ... variable name goes here 001485 ** } a[0]; 001486 ** } 001487 ** 001488 ** During code generation, pointers to the variable names within the 001489 ** VList are taken. When that happens, nAlloc is set to zero as an 001490 ** indication that the VList may never again be enlarged, since the 001491 ** accompanying realloc() would invalidate the pointers. 001492 */ 001493 VList *sqlite3VListAdd( 001494 sqlite3 *db, /* The database connection used for malloc() */ 001495 VList *pIn, /* The input VList. Might be NULL */ 001496 const char *zName, /* Name of symbol to add */ 001497 int nName, /* Bytes of text in zName */ 001498 int iVal /* Value to associate with zName */ 001499 ){ 001500 int nInt; /* number of sizeof(int) objects needed for zName */ 001501 char *z; /* Pointer to where zName will be stored */ 001502 int i; /* Index in pIn[] where zName is stored */ 001503 001504 nInt = nName/4 + 3; 001505 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ 001506 if( pIn==0 || pIn[1]+nInt > pIn[0] ){ 001507 /* Enlarge the allocation */ 001508 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt; 001509 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); 001510 if( pOut==0 ) return pIn; 001511 if( pIn==0 ) pOut[1] = 2; 001512 pIn = pOut; 001513 pIn[0] = nAlloc; 001514 } 001515 i = pIn[1]; 001516 pIn[i] = iVal; 001517 pIn[i+1] = nInt; 001518 z = (char*)&pIn[i+2]; 001519 pIn[1] = i+nInt; 001520 assert( pIn[1]<=pIn[0] ); 001521 memcpy(z, zName, nName); 001522 z[nName] = 0; 001523 return pIn; 001524 } 001525 001526 /* 001527 ** Return a pointer to the name of a variable in the given VList that 001528 ** has the value iVal. Or return a NULL if there is no such variable in 001529 ** the list 001530 */ 001531 const char *sqlite3VListNumToName(VList *pIn, int iVal){ 001532 int i, mx; 001533 if( pIn==0 ) return 0; 001534 mx = pIn[1]; 001535 i = 2; 001536 do{ 001537 if( pIn[i]==iVal ) return (char*)&pIn[i+2]; 001538 i += pIn[i+1]; 001539 }while( i<mx ); 001540 return 0; 001541 } 001542 001543 /* 001544 ** Return the number of the variable named zName, if it is in VList. 001545 ** or return 0 if there is no such variable. 001546 */ 001547 int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ 001548 int i, mx; 001549 if( pIn==0 ) return 0; 001550 mx = pIn[1]; 001551 i = 2; 001552 do{ 001553 const char *z = (const char*)&pIn[i+2]; 001554 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i]; 001555 i += pIn[i+1]; 001556 }while( i<mx ); 001557 return 0; 001558 }