1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** Utility functions used throughout sqlite. 13 ** 14 ** This file contains functions for allocating memory, comparing 15 ** strings, and stuff like that. 16 ** 17 */ 18 #include "sqliteInt.h" 19 #include <stdarg.h> 20 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN 21 # include <math.h> 22 #endif 23 24 /* 25 ** Routine needed to support the testcase() macro. 26 */ 27 #ifdef SQLITE_COVERAGE_TEST 28 void sqlite3Coverage(int x){ 29 static unsigned dummy = 0; 30 dummy += (unsigned)x; 31 } 32 #endif 33 34 /* 35 ** Give a callback to the test harness that can be used to simulate faults 36 ** in places where it is difficult or expensive to do so purely by means 37 ** of inputs. 38 ** 39 ** The intent of the integer argument is to let the fault simulator know 40 ** which of multiple sqlite3FaultSim() calls has been hit. 41 ** 42 ** Return whatever integer value the test callback returns, or return 43 ** SQLITE_OK if no test callback is installed. 44 */ 45 #ifndef SQLITE_OMIT_BUILTIN_TEST 46 int sqlite3FaultSim(int iTest){ 47 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; 48 return xCallback ? xCallback(iTest) : SQLITE_OK; 49 } 50 #endif 51 52 #ifndef SQLITE_OMIT_FLOATING_POINT 53 /* 54 ** Return true if the floating point value is Not a Number (NaN). 55 ** 56 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. 57 ** Otherwise, we have our own implementation that works on most systems. 58 */ 59 int sqlite3IsNaN(double x){ 60 int rc; /* The value return */ 61 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN 62 /* 63 ** Systems that support the isnan() library function should probably 64 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have 65 ** found that many systems do not have a working isnan() function so 66 ** this implementation is provided as an alternative. 67 ** 68 ** This NaN test sometimes fails if compiled on GCC with -ffast-math. 69 ** On the other hand, the use of -ffast-math comes with the following 70 ** warning: 71 ** 72 ** This option [-ffast-math] should never be turned on by any 73 ** -O option since it can result in incorrect output for programs 74 ** which depend on an exact implementation of IEEE or ISO 75 ** rules/specifications for math functions. 76 ** 77 ** Under MSVC, this NaN test may fail if compiled with a floating- 78 ** point precision mode other than /fp:precise. From the MSDN 79 ** documentation: 80 ** 81 ** The compiler [with /fp:precise] will properly handle comparisons 82 ** involving NaN. For example, x != x evaluates to true if x is NaN 83 ** ... 84 */ 85 #ifdef __FAST_MATH__ 86 # error SQLite will not work correctly with the -ffast-math option of GCC. 87 #endif 88 volatile double y = x; 89 volatile double z = y; 90 rc = (y!=z); 91 #else /* if HAVE_ISNAN */ 92 rc = isnan(x); 93 #endif /* HAVE_ISNAN */ 94 testcase( rc ); 95 return rc; 96 } 97 #endif /* SQLITE_OMIT_FLOATING_POINT */ 98 99 /* 100 ** Compute a string length that is limited to what can be stored in 101 ** lower 30 bits of a 32-bit signed integer. 102 ** 103 ** The value returned will never be negative. Nor will it ever be greater 104 ** than the actual length of the string. For very long strings (greater 105 ** than 1GiB) the value returned might be less than the true string length. 106 */ 107 int sqlite3Strlen30(const char *z){ 108 if( z==0 ) return 0; 109 return 0x3fffffff & (int)strlen(z); 110 } 111 112 /* 113 ** Set the current error code to err_code and clear any prior error message. 114 */ 115 void sqlite3Error(sqlite3 *db, int err_code){ 116 assert( db!=0 ); 117 db->errCode = err_code; 118 if( db->pErr ) sqlite3ValueSetNull(db->pErr); 119 } 120 121 /* 122 ** Set the most recent error code and error string for the sqlite 123 ** handle "db". The error code is set to "err_code". 124 ** 125 ** If it is not NULL, string zFormat specifies the format of the 126 ** error string in the style of the printf functions: The following 127 ** format characters are allowed: 128 ** 129 ** %s Insert a string 130 ** %z A string that should be freed after use 131 ** %d Insert an integer 132 ** %T Insert a token 133 ** %S Insert the first element of a SrcList 134 ** 135 ** zFormat and any string tokens that follow it are assumed to be 136 ** encoded in UTF-8. 137 ** 138 ** To clear the most recent error for sqlite handle "db", sqlite3Error 139 ** should be called with err_code set to SQLITE_OK and zFormat set 140 ** to NULL. 141 */ 142 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ 143 assert( db!=0 ); 144 db->errCode = err_code; 145 if( zFormat==0 ){ 146 sqlite3Error(db, err_code); 147 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ 148 char *z; 149 va_list ap; 150 va_start(ap, zFormat); 151 z = sqlite3VMPrintf(db, zFormat, ap); 152 va_end(ap); 153 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); 154 } 155 } 156 157 /* 158 ** Add an error message to pParse->zErrMsg and increment pParse->nErr. 159 ** The following formatting characters are allowed: 160 ** 161 ** %s Insert a string 162 ** %z A string that should be freed after use 163 ** %d Insert an integer 164 ** %T Insert a token 165 ** %S Insert the first element of a SrcList 166 ** 167 ** This function should be used to report any error that occurs while 168 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The 169 ** last thing the sqlite3_prepare() function does is copy the error 170 ** stored by this function into the database handle using sqlite3Error(). 171 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used 172 ** during statement execution (sqlite3_step() etc.). 173 */ 174 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ 175 char *zMsg; 176 va_list ap; 177 sqlite3 *db = pParse->db; 178 va_start(ap, zFormat); 179 zMsg = sqlite3VMPrintf(db, zFormat, ap); 180 va_end(ap); 181 if( db->suppressErr ){ 182 sqlite3DbFree(db, zMsg); 183 }else{ 184 pParse->nErr++; 185 sqlite3DbFree(db, pParse->zErrMsg); 186 pParse->zErrMsg = zMsg; 187 pParse->rc = SQLITE_ERROR; 188 } 189 } 190 191 /* 192 ** Convert an SQL-style quoted string into a normal string by removing 193 ** the quote characters. The conversion is done in-place. If the 194 ** input does not begin with a quote character, then this routine 195 ** is a no-op. 196 ** 197 ** The input string must be zero-terminated. A new zero-terminator 198 ** is added to the dequoted string. 199 ** 200 ** The return value is -1 if no dequoting occurs or the length of the 201 ** dequoted string, exclusive of the zero terminator, if dequoting does 202 ** occur. 203 ** 204 ** 2002-Feb-14: This routine is extended to remove MS-Access style 205 ** brackets from around identifiers. For example: "[a-b-c]" becomes 206 ** "a-b-c". 207 */ 208 int sqlite3Dequote(char *z){ 209 char quote; 210 int i, j; 211 if( z==0 ) return -1; 212 quote = z[0]; 213 switch( quote ){ 214 case '\'': break; 215 case '"': break; 216 case '`': break; /* For MySQL compatibility */ 217 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ 218 default: return -1; 219 } 220 for(i=1, j=0;; i++){ 221 assert( z[i] ); 222 if( z[i]==quote ){ 223 if( z[i+1]==quote ){ 224 z[j++] = quote; 225 i++; 226 }else{ 227 break; 228 } 229 }else{ 230 z[j++] = z[i]; 231 } 232 } 233 z[j] = 0; 234 return j; 235 } 236 237 /* Convenient short-hand */ 238 #define UpperToLower sqlite3UpperToLower 239 240 /* 241 ** Some systems have stricmp(). Others have strcasecmp(). Because 242 ** there is no consistency, we will define our own. 243 ** 244 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and 245 ** sqlite3_strnicmp() APIs allow applications and extensions to compare 246 ** the contents of two buffers containing UTF-8 strings in a 247 ** case-independent fashion, using the same definition of "case 248 ** independence" that SQLite uses internally when comparing identifiers. 249 */ 250 int sqlite3_stricmp(const char *zLeft, const char *zRight){ 251 register unsigned char *a, *b; 252 if( zLeft==0 ){ 253 return zRight ? -1 : 0; 254 }else if( zRight==0 ){ 255 return 1; 256 } 257 a = (unsigned char *)zLeft; 258 b = (unsigned char *)zRight; 259 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } 260 return UpperToLower[*a] - UpperToLower[*b]; 261 } 262 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ 263 register unsigned char *a, *b; 264 if( zLeft==0 ){ 265 return zRight ? -1 : 0; 266 }else if( zRight==0 ){ 267 return 1; 268 } 269 a = (unsigned char *)zLeft; 270 b = (unsigned char *)zRight; 271 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } 272 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; 273 } 274 275 /* 276 ** The string z[] is an text representation of a real number. 277 ** Convert this string to a double and write it into *pResult. 278 ** 279 ** The string z[] is length bytes in length (bytes, not characters) and 280 ** uses the encoding enc. The string is not necessarily zero-terminated. 281 ** 282 ** Return TRUE if the result is a valid real number (or integer) and FALSE 283 ** if the string is empty or contains extraneous text. Valid numbers 284 ** are in one of these formats: 285 ** 286 ** [+-]digits[E[+-]digits] 287 ** [+-]digits.[digits][E[+-]digits] 288 ** [+-].digits[E[+-]digits] 289 ** 290 ** Leading and trailing whitespace is ignored for the purpose of determining 291 ** validity. 292 ** 293 ** If some prefix of the input string is a valid number, this routine 294 ** returns FALSE but it still converts the prefix and writes the result 295 ** into *pResult. 296 */ 297 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ 298 #ifndef SQLITE_OMIT_FLOATING_POINT 299 int incr; 300 const char *zEnd = z + length; 301 /* sign * significand * (10 ^ (esign * exponent)) */ 302 int sign = 1; /* sign of significand */ 303 i64 s = 0; /* significand */ 304 int d = 0; /* adjust exponent for shifting decimal point */ 305 int esign = 1; /* sign of exponent */ 306 int e = 0; /* exponent */ 307 int eValid = 1; /* True exponent is either not used or is well-formed */ 308 double result; 309 int nDigits = 0; 310 int nonNum = 0; 311 312 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 313 *pResult = 0.0; /* Default return value, in case of an error */ 314 315 if( enc==SQLITE_UTF8 ){ 316 incr = 1; 317 }else{ 318 int i; 319 incr = 2; 320 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); 321 for(i=3-enc; i<length && z[i]==0; i+=2){} 322 nonNum = i<length; 323 zEnd = z+i+enc-3; 324 z += (enc&1); 325 } 326 327 /* skip leading spaces */ 328 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; 329 if( z>=zEnd ) return 0; 330 331 /* get sign of significand */ 332 if( *z=='-' ){ 333 sign = -1; 334 z+=incr; 335 }else if( *z=='+' ){ 336 z+=incr; 337 } 338 339 /* skip leading zeroes */ 340 while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++; 341 342 /* copy max significant digits to significand */ 343 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ 344 s = s*10 + (*z - '0'); 345 z+=incr, nDigits++; 346 } 347 348 /* skip non-significant significand digits 349 ** (increase exponent by d to shift decimal left) */ 350 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; 351 if( z>=zEnd ) goto do_atof_calc; 352 353 /* if decimal point is present */ 354 if( *z=='.' ){ 355 z+=incr; 356 /* copy digits from after decimal to significand 357 ** (decrease exponent by d to shift decimal right) */ 358 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ 359 s = s*10 + (*z - '0'); 360 z+=incr, nDigits++, d--; 361 } 362 /* skip non-significant digits */ 363 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++; 364 } 365 if( z>=zEnd ) goto do_atof_calc; 366 367 /* if exponent is present */ 368 if( *z=='e' || *z=='E' ){ 369 z+=incr; 370 eValid = 0; 371 if( z>=zEnd ) goto do_atof_calc; 372 /* get sign of exponent */ 373 if( *z=='-' ){ 374 esign = -1; 375 z+=incr; 376 }else if( *z=='+' ){ 377 z+=incr; 378 } 379 /* copy digits to exponent */ 380 while( z<zEnd && sqlite3Isdigit(*z) ){ 381 e = e<10000 ? (e*10 + (*z - '0')) : 10000; 382 z+=incr; 383 eValid = 1; 384 } 385 } 386 387 /* skip trailing spaces */ 388 if( nDigits && eValid ){ 389 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; 390 } 391 392 do_atof_calc: 393 /* adjust exponent by d, and update sign */ 394 e = (e*esign) + d; 395 if( e<0 ) { 396 esign = -1; 397 e *= -1; 398 } else { 399 esign = 1; 400 } 401 402 /* if 0 significand */ 403 if( !s ) { 404 /* In the IEEE 754 standard, zero is signed. 405 ** Add the sign if we've seen at least one digit */ 406 result = (sign<0 && nDigits) ? -(double)0 : (double)0; 407 } else { 408 /* attempt to reduce exponent */ 409 if( esign>0 ){ 410 while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; 411 }else{ 412 while( !(s%10) && e>0 ) e--,s/=10; 413 } 414 415 /* adjust the sign of significand */ 416 s = sign<0 ? -s : s; 417 418 /* if exponent, scale significand as appropriate 419 ** and store in result. */ 420 if( e ){ 421 LONGDOUBLE_TYPE scale = 1.0; 422 /* attempt to handle extremely small/large numbers better */ 423 if( e>307 && e<342 ){ 424 while( e%308 ) { scale *= 1.0e+1; e -= 1; } 425 if( esign<0 ){ 426 result = s / scale; 427 result /= 1.0e+308; 428 }else{ 429 result = s * scale; 430 result *= 1.0e+308; 431 } 432 }else if( e>=342 ){ 433 if( esign<0 ){ 434 result = 0.0*s; 435 }else{ 436 result = 1e308*1e308*s; /* Infinity */ 437 } 438 }else{ 439 /* 1.0e+22 is the largest power of 10 than can be 440 ** represented exactly. */ 441 while( e%22 ) { scale *= 1.0e+1; e -= 1; } 442 while( e>0 ) { scale *= 1.0e+22; e -= 22; } 443 if( esign<0 ){ 444 result = s / scale; 445 }else{ 446 result = s * scale; 447 } 448 } 449 } else { 450 result = (double)s; 451 } 452 } 453 454 /* store the result */ 455 *pResult = result; 456 457 /* return true if number and no extra non-whitespace chracters after */ 458 return z>=zEnd && nDigits>0 && eValid && nonNum==0; 459 #else 460 return !sqlite3Atoi64(z, pResult, length, enc); 461 #endif /* SQLITE_OMIT_FLOATING_POINT */ 462 } 463 464 /* 465 ** Compare the 19-character string zNum against the text representation 466 ** value 2^63: 9223372036854775808. Return negative, zero, or positive 467 ** if zNum is less than, equal to, or greater than the string. 468 ** Note that zNum must contain exactly 19 characters. 469 ** 470 ** Unlike memcmp() this routine is guaranteed to return the difference 471 ** in the values of the last digit if the only difference is in the 472 ** last digit. So, for example, 473 ** 474 ** compare2pow63("9223372036854775800", 1) 475 ** 476 ** will return -8. 477 */ 478 static int compare2pow63(const char *zNum, int incr){ 479 int c = 0; 480 int i; 481 /* 012345678901234567 */ 482 const char *pow63 = "922337203685477580"; 483 for(i=0; c==0 && i<18; i++){ 484 c = (zNum[i*incr]-pow63[i])*10; 485 } 486 if( c==0 ){ 487 c = zNum[18*incr] - '8'; 488 testcase( c==(-1) ); 489 testcase( c==0 ); 490 testcase( c==(+1) ); 491 } 492 return c; 493 } 494 495 /* 496 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This 497 ** routine does *not* accept hexadecimal notation. 498 ** 499 ** If the zNum value is representable as a 64-bit twos-complement 500 ** integer, then write that value into *pNum and return 0. 501 ** 502 ** If zNum is exactly 9223372036854775808, return 2. This special 503 ** case is broken out because while 9223372036854775808 cannot be a 504 ** signed 64-bit integer, its negative -9223372036854775808 can be. 505 ** 506 ** If zNum is too big for a 64-bit integer and is not 507 ** 9223372036854775808 or if zNum contains any non-numeric text, 508 ** then return 1. 509 ** 510 ** length is the number of bytes in the string (bytes, not characters). 511 ** The string is not necessarily zero-terminated. The encoding is 512 ** given by enc. 513 */ 514 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ 515 int incr; 516 u64 u = 0; 517 int neg = 0; /* assume positive */ 518 int i; 519 int c = 0; 520 int nonNum = 0; 521 const char *zStart; 522 const char *zEnd = zNum + length; 523 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 524 if( enc==SQLITE_UTF8 ){ 525 incr = 1; 526 }else{ 527 incr = 2; 528 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); 529 for(i=3-enc; i<length && zNum[i]==0; i+=2){} 530 nonNum = i<length; 531 zEnd = zNum+i+enc-3; 532 zNum += (enc&1); 533 } 534 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; 535 if( zNum<zEnd ){ 536 if( *zNum=='-' ){ 537 neg = 1; 538 zNum+=incr; 539 }else if( *zNum=='+' ){ 540 zNum+=incr; 541 } 542 } 543 zStart = zNum; 544 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ 545 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ 546 u = u*10 + c - '0'; 547 } 548 if( u>LARGEST_INT64 ){ 549 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; 550 }else if( neg ){ 551 *pNum = -(i64)u; 552 }else{ 553 *pNum = (i64)u; 554 } 555 testcase( i==18 ); 556 testcase( i==19 ); 557 testcase( i==20 ); 558 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) 559 || i>19*incr || nonNum ){ 560 /* zNum is empty or contains non-numeric text or is longer 561 ** than 19 digits (thus guaranteeing that it is too large) */ 562 return 1; 563 }else if( i<19*incr ){ 564 /* Less than 19 digits, so we know that it fits in 64 bits */ 565 assert( u<=LARGEST_INT64 ); 566 return 0; 567 }else{ 568 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ 569 c = compare2pow63(zNum, incr); 570 if( c<0 ){ 571 /* zNum is less than 9223372036854775808 so it fits */ 572 assert( u<=LARGEST_INT64 ); 573 return 0; 574 }else if( c>0 ){ 575 /* zNum is greater than 9223372036854775808 so it overflows */ 576 return 1; 577 }else{ 578 /* zNum is exactly 9223372036854775808. Fits if negative. The 579 ** special case 2 overflow if positive */ 580 assert( u-1==LARGEST_INT64 ); 581 return neg ? 0 : 2; 582 } 583 } 584 } 585 586 /* 587 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, 588 ** into a 64-bit signed integer. This routine accepts hexadecimal literals, 589 ** whereas sqlite3Atoi64() does not. 590 ** 591 ** Returns: 592 ** 593 ** 0 Successful transformation. Fits in a 64-bit signed integer. 594 ** 1 Integer too large for a 64-bit signed integer or is malformed 595 ** 2 Special case of 9223372036854775808 596 */ 597 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ 598 #ifndef SQLITE_OMIT_HEX_INTEGER 599 if( z[0]=='0' 600 && (z[1]=='x' || z[1]=='X') 601 && sqlite3Isxdigit(z[2]) 602 ){ 603 u64 u = 0; 604 int i, k; 605 for(i=2; z[i]=='0'; i++){} 606 for(k=i; sqlite3Isxdigit(z[k]); k++){ 607 u = u*16 + sqlite3HexToInt(z[k]); 608 } 609 memcpy(pOut, &u, 8); 610 return (z[k]==0 && k-i<=16) ? 0 : 1; 611 }else 612 #endif /* SQLITE_OMIT_HEX_INTEGER */ 613 { 614 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); 615 } 616 } 617 618 /* 619 ** If zNum represents an integer that will fit in 32-bits, then set 620 ** *pValue to that integer and return true. Otherwise return false. 621 ** 622 ** This routine accepts both decimal and hexadecimal notation for integers. 623 ** 624 ** Any non-numeric characters that following zNum are ignored. 625 ** This is different from sqlite3Atoi64() which requires the 626 ** input number to be zero-terminated. 627 */ 628 int sqlite3GetInt32(const char *zNum, int *pValue){ 629 sqlite_int64 v = 0; 630 int i, c; 631 int neg = 0; 632 if( zNum[0]=='-' ){ 633 neg = 1; 634 zNum++; 635 }else if( zNum[0]=='+' ){ 636 zNum++; 637 } 638 #ifndef SQLITE_OMIT_HEX_INTEGER 639 else if( zNum[0]=='0' 640 && (zNum[1]=='x' || zNum[1]=='X') 641 && sqlite3Isxdigit(zNum[2]) 642 ){ 643 u32 u = 0; 644 zNum += 2; 645 while( zNum[0]=='0' ) zNum++; 646 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ 647 u = u*16 + sqlite3HexToInt(zNum[i]); 648 } 649 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ 650 memcpy(pValue, &u, 4); 651 return 1; 652 }else{ 653 return 0; 654 } 655 } 656 #endif 657 while( zNum[0]=='0' ) zNum++; 658 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ 659 v = v*10 + c; 660 } 661 662 /* The longest decimal representation of a 32 bit integer is 10 digits: 663 ** 664 ** 1234567890 665 ** 2^31 -> 2147483648 666 */ 667 testcase( i==10 ); 668 if( i>10 ){ 669 return 0; 670 } 671 testcase( v-neg==2147483647 ); 672 if( v-neg>2147483647 ){ 673 return 0; 674 } 675 if( neg ){ 676 v = -v; 677 } 678 *pValue = (int)v; 679 return 1; 680 } 681 682 /* 683 ** Return a 32-bit integer value extracted from a string. If the 684 ** string is not an integer, just return 0. 685 */ 686 int sqlite3Atoi(const char *z){ 687 int x = 0; 688 if( z ) sqlite3GetInt32(z, &x); 689 return x; 690 } 691 692 /* 693 ** The variable-length integer encoding is as follows: 694 ** 695 ** KEY: 696 ** A = 0xxxxxxx 7 bits of data and one flag bit 697 ** B = 1xxxxxxx 7 bits of data and one flag bit 698 ** C = xxxxxxxx 8 bits of data 699 ** 700 ** 7 bits - A 701 ** 14 bits - BA 702 ** 21 bits - BBA 703 ** 28 bits - BBBA 704 ** 35 bits - BBBBA 705 ** 42 bits - BBBBBA 706 ** 49 bits - BBBBBBA 707 ** 56 bits - BBBBBBBA 708 ** 64 bits - BBBBBBBBC 709 */ 710 711 /* 712 ** Write a 64-bit variable-length integer to memory starting at p[0]. 713 ** The length of data write will be between 1 and 9 bytes. The number 714 ** of bytes written is returned. 715 ** 716 ** A variable-length integer consists of the lower 7 bits of each byte 717 ** for all bytes that have the 8th bit set and one byte with the 8th 718 ** bit clear. Except, if we get to the 9th byte, it stores the full 719 ** 8 bits and is the last byte. 720 */ 721 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ 722 int i, j, n; 723 u8 buf[10]; 724 if( v & (((u64)0xff000000)<<32) ){ 725 p[8] = (u8)v; 726 v >>= 8; 727 for(i=7; i>=0; i--){ 728 p[i] = (u8)((v & 0x7f) | 0x80); 729 v >>= 7; 730 } 731 return 9; 732 } 733 n = 0; 734 do{ 735 buf[n++] = (u8)((v & 0x7f) | 0x80); 736 v >>= 7; 737 }while( v!=0 ); 738 buf[0] &= 0x7f; 739 assert( n<=9 ); 740 for(i=0, j=n-1; j>=0; j--, i++){ 741 p[i] = buf[j]; 742 } 743 return n; 744 } 745 int sqlite3PutVarint(unsigned char *p, u64 v){ 746 if( v<=0x7f ){ 747 p[0] = v&0x7f; 748 return 1; 749 } 750 if( v<=0x3fff ){ 751 p[0] = ((v>>7)&0x7f)|0x80; 752 p[1] = v&0x7f; 753 return 2; 754 } 755 return putVarint64(p,v); 756 } 757 758 /* 759 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants 760 ** are defined here rather than simply putting the constant expressions 761 ** inline in order to work around bugs in the RVT compiler. 762 ** 763 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f 764 ** 765 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 766 */ 767 #define SLOT_2_0 0x001fc07f 768 #define SLOT_4_2_0 0xf01fc07f 769 770 771 /* 772 ** Read a 64-bit variable-length integer from memory starting at p[0]. 773 ** Return the number of bytes read. The value is stored in *v. 774 */ 775 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ 776 u32 a,b,s; 777 778 a = *p; 779 /* a: p0 (unmasked) */ 780 if (!(a&0x80)) 781 { 782 *v = a; 783 return 1; 784 } 785 786 p++; 787 b = *p; 788 /* b: p1 (unmasked) */ 789 if (!(b&0x80)) 790 { 791 a &= 0x7f; 792 a = a<<7; 793 a |= b; 794 *v = a; 795 return 2; 796 } 797 798 /* Verify that constants are precomputed correctly */ 799 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); 800 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); 801 802 p++; 803 a = a<<14; 804 a |= *p; 805 /* a: p0<<14 | p2 (unmasked) */ 806 if (!(a&0x80)) 807 { 808 a &= SLOT_2_0; 809 b &= 0x7f; 810 b = b<<7; 811 a |= b; 812 *v = a; 813 return 3; 814 } 815 816 /* CSE1 from below */ 817 a &= SLOT_2_0; 818 p++; 819 b = b<<14; 820 b |= *p; 821 /* b: p1<<14 | p3 (unmasked) */ 822 if (!(b&0x80)) 823 { 824 b &= SLOT_2_0; 825 /* moved CSE1 up */ 826 /* a &= (0x7f<<14)|(0x7f); */ 827 a = a<<7; 828 a |= b; 829 *v = a; 830 return 4; 831 } 832 833 /* a: p0<<14 | p2 (masked) */ 834 /* b: p1<<14 | p3 (unmasked) */ 835 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 836 /* moved CSE1 up */ 837 /* a &= (0x7f<<14)|(0x7f); */ 838 b &= SLOT_2_0; 839 s = a; 840 /* s: p0<<14 | p2 (masked) */ 841 842 p++; 843 a = a<<14; 844 a |= *p; 845 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ 846 if (!(a&0x80)) 847 { 848 /* we can skip these cause they were (effectively) done above 849 ** while calculating s */ 850 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ 851 /* b &= (0x7f<<14)|(0x7f); */ 852 b = b<<7; 853 a |= b; 854 s = s>>18; 855 *v = ((u64)s)<<32 | a; 856 return 5; 857 } 858 859 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 860 s = s<<7; 861 s |= b; 862 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ 863 864 p++; 865 b = b<<14; 866 b |= *p; 867 /* b: p1<<28 | p3<<14 | p5 (unmasked) */ 868 if (!(b&0x80)) 869 { 870 /* we can skip this cause it was (effectively) done above in calc'ing s */ 871 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ 872 a &= SLOT_2_0; 873 a = a<<7; 874 a |= b; 875 s = s>>18; 876 *v = ((u64)s)<<32 | a; 877 return 6; 878 } 879 880 p++; 881 a = a<<14; 882 a |= *p; 883 /* a: p2<<28 | p4<<14 | p6 (unmasked) */ 884 if (!(a&0x80)) 885 { 886 a &= SLOT_4_2_0; 887 b &= SLOT_2_0; 888 b = b<<7; 889 a |= b; 890 s = s>>11; 891 *v = ((u64)s)<<32 | a; 892 return 7; 893 } 894 895 /* CSE2 from below */ 896 a &= SLOT_2_0; 897 p++; 898 b = b<<14; 899 b |= *p; 900 /* b: p3<<28 | p5<<14 | p7 (unmasked) */ 901 if (!(b&0x80)) 902 { 903 b &= SLOT_4_2_0; 904 /* moved CSE2 up */ 905 /* a &= (0x7f<<14)|(0x7f); */ 906 a = a<<7; 907 a |= b; 908 s = s>>4; 909 *v = ((u64)s)<<32 | a; 910 return 8; 911 } 912 913 p++; 914 a = a<<15; 915 a |= *p; 916 /* a: p4<<29 | p6<<15 | p8 (unmasked) */ 917 918 /* moved CSE2 up */ 919 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ 920 b &= SLOT_2_0; 921 b = b<<8; 922 a |= b; 923 924 s = s<<4; 925 b = p[-4]; 926 b &= 0x7f; 927 b = b>>3; 928 s |= b; 929 930 *v = ((u64)s)<<32 | a; 931 932 return 9; 933 } 934 935 /* 936 ** Read a 32-bit variable-length integer from memory starting at p[0]. 937 ** Return the number of bytes read. The value is stored in *v. 938 ** 939 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned 940 ** integer, then set *v to 0xffffffff. 941 ** 942 ** A MACRO version, getVarint32, is provided which inlines the 943 ** single-byte case. All code should use the MACRO version as 944 ** this function assumes the single-byte case has already been handled. 945 */ 946 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ 947 u32 a,b; 948 949 /* The 1-byte case. Overwhelmingly the most common. Handled inline 950 ** by the getVarin32() macro */ 951 a = *p; 952 /* a: p0 (unmasked) */ 953 #ifndef getVarint32 954 if (!(a&0x80)) 955 { 956 /* Values between 0 and 127 */ 957 *v = a; 958 return 1; 959 } 960 #endif 961 962 /* The 2-byte case */ 963 p++; 964 b = *p; 965 /* b: p1 (unmasked) */ 966 if (!(b&0x80)) 967 { 968 /* Values between 128 and 16383 */ 969 a &= 0x7f; 970 a = a<<7; 971 *v = a | b; 972 return 2; 973 } 974 975 /* The 3-byte case */ 976 p++; 977 a = a<<14; 978 a |= *p; 979 /* a: p0<<14 | p2 (unmasked) */ 980 if (!(a&0x80)) 981 { 982 /* Values between 16384 and 2097151 */ 983 a &= (0x7f<<14)|(0x7f); 984 b &= 0x7f; 985 b = b<<7; 986 *v = a | b; 987 return 3; 988 } 989 990 /* A 32-bit varint is used to store size information in btrees. 991 ** Objects are rarely larger than 2MiB limit of a 3-byte varint. 992 ** A 3-byte varint is sufficient, for example, to record the size 993 ** of a 1048569-byte BLOB or string. 994 ** 995 ** We only unroll the first 1-, 2-, and 3- byte cases. The very 996 ** rare larger cases can be handled by the slower 64-bit varint 997 ** routine. 998 */ 999 #if 1 1000 { 1001 u64 v64; 1002 u8 n; 1003 1004 p -= 2; 1005 n = sqlite3GetVarint(p, &v64); 1006 assert( n>3 && n<=9 ); 1007 if( (v64 & SQLITE_MAX_U32)!=v64 ){ 1008 *v = 0xffffffff; 1009 }else{ 1010 *v = (u32)v64; 1011 } 1012 return n; 1013 } 1014 1015 #else 1016 /* For following code (kept for historical record only) shows an 1017 ** unrolling for the 3- and 4-byte varint cases. This code is 1018 ** slightly faster, but it is also larger and much harder to test. 1019 */ 1020 p++; 1021 b = b<<14; 1022 b |= *p; 1023 /* b: p1<<14 | p3 (unmasked) */ 1024 if (!(b&0x80)) 1025 { 1026 /* Values between 2097152 and 268435455 */ 1027 b &= (0x7f<<14)|(0x7f); 1028 a &= (0x7f<<14)|(0x7f); 1029 a = a<<7; 1030 *v = a | b; 1031 return 4; 1032 } 1033 1034 p++; 1035 a = a<<14; 1036 a |= *p; 1037 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ 1038 if (!(a&0x80)) 1039 { 1040 /* Values between 268435456 and 34359738367 */ 1041 a &= SLOT_4_2_0; 1042 b &= SLOT_4_2_0; 1043 b = b<<7; 1044 *v = a | b; 1045 return 5; 1046 } 1047 1048 /* We can only reach this point when reading a corrupt database 1049 ** file. In that case we are not in any hurry. Use the (relatively 1050 ** slow) general-purpose sqlite3GetVarint() routine to extract the 1051 ** value. */ 1052 { 1053 u64 v64; 1054 u8 n; 1055 1056 p -= 4; 1057 n = sqlite3GetVarint(p, &v64); 1058 assert( n>5 && n<=9 ); 1059 *v = (u32)v64; 1060 return n; 1061 } 1062 #endif 1063 } 1064 1065 /* 1066 ** Return the number of bytes that will be needed to store the given 1067 ** 64-bit integer. 1068 */ 1069 int sqlite3VarintLen(u64 v){ 1070 int i; 1071 for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); } 1072 return i; 1073 } 1074 1075 1076 /* 1077 ** Read or write a four-byte big-endian integer value. 1078 */ 1079 u32 sqlite3Get4byte(const u8 *p){ 1080 #if SQLITE_BYTEORDER==4321 1081 u32 x; 1082 memcpy(&x,p,4); 1083 return x; 1084 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 1085 && defined(__GNUC__) && GCC_VERSION>=4003000 1086 u32 x; 1087 memcpy(&x,p,4); 1088 return __builtin_bswap32(x); 1089 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ 1090 && defined(_MSC_VER) && _MSC_VER>=1300 1091 u32 x; 1092 memcpy(&x,p,4); 1093 return _byteswap_ulong(x); 1094 #else 1095 testcase( p[0]&0x80 ); 1096 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; 1097 #endif 1098 } 1099 void sqlite3Put4byte(unsigned char *p, u32 v){ 1100 #if SQLITE_BYTEORDER==4321 1101 memcpy(p,&v,4); 1102 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 1103 u32 x = __builtin_bswap32(v); 1104 memcpy(p,&x,4); 1105 #elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 1106 u32 x = _byteswap_ulong(v); 1107 memcpy(p,&x,4); 1108 #else 1109 p[0] = (u8)(v>>24); 1110 p[1] = (u8)(v>>16); 1111 p[2] = (u8)(v>>8); 1112 p[3] = (u8)v; 1113 #endif 1114 } 1115 1116 1117 1118 /* 1119 ** Translate a single byte of Hex into an integer. 1120 ** This routine only works if h really is a valid hexadecimal 1121 ** character: 0..9a..fA..F 1122 */ 1123 u8 sqlite3HexToInt(int h){ 1124 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); 1125 #ifdef SQLITE_ASCII 1126 h += 9*(1&(h>>6)); 1127 #endif 1128 #ifdef SQLITE_EBCDIC 1129 h += 9*(1&~(h>>4)); 1130 #endif 1131 return (u8)(h & 0xf); 1132 } 1133 1134 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) 1135 /* 1136 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary 1137 ** value. Return a pointer to its binary value. Space to hold the 1138 ** binary value has been obtained from malloc and must be freed by 1139 ** the calling routine. 1140 */ 1141 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ 1142 char *zBlob; 1143 int i; 1144 1145 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); 1146 n--; 1147 if( zBlob ){ 1148 for(i=0; i<n; i+=2){ 1149 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); 1150 } 1151 zBlob[i/2] = 0; 1152 } 1153 return zBlob; 1154 } 1155 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ 1156 1157 /* 1158 ** Log an error that is an API call on a connection pointer that should 1159 ** not have been used. The "type" of connection pointer is given as the 1160 ** argument. The zType is a word like "NULL" or "closed" or "invalid". 1161 */ 1162 static void logBadConnection(const char *zType){ 1163 sqlite3_log(SQLITE_MISUSE, 1164 "API call with %s database connection pointer", 1165 zType 1166 ); 1167 } 1168 1169 /* 1170 ** Check to make sure we have a valid db pointer. This test is not 1171 ** foolproof but it does provide some measure of protection against 1172 ** misuse of the interface such as passing in db pointers that are 1173 ** NULL or which have been previously closed. If this routine returns 1174 ** 1 it means that the db pointer is valid and 0 if it should not be 1175 ** dereferenced for any reason. The calling function should invoke 1176 ** SQLITE_MISUSE immediately. 1177 ** 1178 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for 1179 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to 1180 ** open properly and is not fit for general use but which can be 1181 ** used as an argument to sqlite3_errmsg() or sqlite3_close(). 1182 */ 1183 int sqlite3SafetyCheckOk(sqlite3 *db){ 1184 u32 magic; 1185 if( db==0 ){ 1186 logBadConnection("NULL"); 1187 return 0; 1188 } 1189 magic = db->magic; 1190 if( magic!=SQLITE_MAGIC_OPEN ){ 1191 if( sqlite3SafetyCheckSickOrOk(db) ){ 1192 testcase( sqlite3GlobalConfig.xLog!=0 ); 1193 logBadConnection("unopened"); 1194 } 1195 return 0; 1196 }else{ 1197 return 1; 1198 } 1199 } 1200 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ 1201 u32 magic; 1202 magic = db->magic; 1203 if( magic!=SQLITE_MAGIC_SICK && 1204 magic!=SQLITE_MAGIC_OPEN && 1205 magic!=SQLITE_MAGIC_BUSY ){ 1206 testcase( sqlite3GlobalConfig.xLog!=0 ); 1207 logBadConnection("invalid"); 1208 return 0; 1209 }else{ 1210 return 1; 1211 } 1212 } 1213 1214 /* 1215 ** Attempt to add, substract, or multiply the 64-bit signed value iB against 1216 ** the other 64-bit signed integer at *pA and store the result in *pA. 1217 ** Return 0 on success. Or if the operation would have resulted in an 1218 ** overflow, leave *pA unchanged and return 1. 1219 */ 1220 int sqlite3AddInt64(i64 *pA, i64 iB){ 1221 i64 iA = *pA; 1222 testcase( iA==0 ); testcase( iA==1 ); 1223 testcase( iB==-1 ); testcase( iB==0 ); 1224 if( iB>=0 ){ 1225 testcase( iA>0 && LARGEST_INT64 - iA == iB ); 1226 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); 1227 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; 1228 }else{ 1229 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); 1230 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); 1231 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; 1232 } 1233 *pA += iB; 1234 return 0; 1235 } 1236 int sqlite3SubInt64(i64 *pA, i64 iB){ 1237 testcase( iB==SMALLEST_INT64+1 ); 1238 if( iB==SMALLEST_INT64 ){ 1239 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); 1240 if( (*pA)>=0 ) return 1; 1241 *pA -= iB; 1242 return 0; 1243 }else{ 1244 return sqlite3AddInt64(pA, -iB); 1245 } 1246 } 1247 #define TWOPOWER32 (((i64)1)<<32) 1248 #define TWOPOWER31 (((i64)1)<<31) 1249 int sqlite3MulInt64(i64 *pA, i64 iB){ 1250 i64 iA = *pA; 1251 i64 iA1, iA0, iB1, iB0, r; 1252 1253 iA1 = iA/TWOPOWER32; 1254 iA0 = iA % TWOPOWER32; 1255 iB1 = iB/TWOPOWER32; 1256 iB0 = iB % TWOPOWER32; 1257 if( iA1==0 ){ 1258 if( iB1==0 ){ 1259 *pA *= iB; 1260 return 0; 1261 } 1262 r = iA0*iB1; 1263 }else if( iB1==0 ){ 1264 r = iA1*iB0; 1265 }else{ 1266 /* If both iA1 and iB1 are non-zero, overflow will result */ 1267 return 1; 1268 } 1269 testcase( r==(-TWOPOWER31)-1 ); 1270 testcase( r==(-TWOPOWER31) ); 1271 testcase( r==TWOPOWER31 ); 1272 testcase( r==TWOPOWER31-1 ); 1273 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; 1274 r *= TWOPOWER32; 1275 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; 1276 *pA = r; 1277 return 0; 1278 } 1279 1280 /* 1281 ** Compute the absolute value of a 32-bit signed integer, of possible. Or 1282 ** if the integer has a value of -2147483648, return +2147483647 1283 */ 1284 int sqlite3AbsInt32(int x){ 1285 if( x>=0 ) return x; 1286 if( x==(int)0x80000000 ) return 0x7fffffff; 1287 return -x; 1288 } 1289 1290 #ifdef SQLITE_ENABLE_8_3_NAMES 1291 /* 1292 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database 1293 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and 1294 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than 1295 ** three characters, then shorten the suffix on z[] to be the last three 1296 ** characters of the original suffix. 1297 ** 1298 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always 1299 ** do the suffix shortening regardless of URI parameter. 1300 ** 1301 ** Examples: 1302 ** 1303 ** test.db-journal => test.nal 1304 ** test.db-wal => test.wal 1305 ** test.db-shm => test.shm 1306 ** test.db-mj7f3319fa => test.9fa 1307 */ 1308 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ 1309 #if SQLITE_ENABLE_8_3_NAMES<2 1310 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) 1311 #endif 1312 { 1313 int i, sz; 1314 sz = sqlite3Strlen30(z); 1315 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} 1316 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); 1317 } 1318 } 1319 #endif 1320 1321 /* 1322 ** Find (an approximate) sum of two LogEst values. This computation is 1323 ** not a simple "+" operator because LogEst is stored as a logarithmic 1324 ** value. 1325 ** 1326 */ 1327 LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ 1328 static const unsigned char x[] = { 1329 10, 10, /* 0,1 */ 1330 9, 9, /* 2,3 */ 1331 8, 8, /* 4,5 */ 1332 7, 7, 7, /* 6,7,8 */ 1333 6, 6, 6, /* 9,10,11 */ 1334 5, 5, 5, /* 12-14 */ 1335 4, 4, 4, 4, /* 15-18 */ 1336 3, 3, 3, 3, 3, 3, /* 19-24 */ 1337 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ 1338 }; 1339 if( a>=b ){ 1340 if( a>b+49 ) return a; 1341 if( a>b+31 ) return a+1; 1342 return a+x[a-b]; 1343 }else{ 1344 if( b>a+49 ) return b; 1345 if( b>a+31 ) return b+1; 1346 return b+x[b-a]; 1347 } 1348 } 1349 1350 /* 1351 ** Convert an integer into a LogEst. In other words, compute an 1352 ** approximation for 10*log2(x). 1353 */ 1354 LogEst sqlite3LogEst(u64 x){ 1355 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; 1356 LogEst y = 40; 1357 if( x<8 ){ 1358 if( x<2 ) return 0; 1359 while( x<8 ){ y -= 10; x <<= 1; } 1360 }else{ 1361 while( x>255 ){ y += 40; x >>= 4; } 1362 while( x>15 ){ y += 10; x >>= 1; } 1363 } 1364 return a[x&7] + y - 10; 1365 } 1366 1367 #ifndef SQLITE_OMIT_VIRTUALTABLE 1368 /* 1369 ** Convert a double into a LogEst 1370 ** In other words, compute an approximation for 10*log2(x). 1371 */ 1372 LogEst sqlite3LogEstFromDouble(double x){ 1373 u64 a; 1374 LogEst e; 1375 assert( sizeof(x)==8 && sizeof(a)==8 ); 1376 if( x<=1 ) return 0; 1377 if( x<=2000000000 ) return sqlite3LogEst((u64)x); 1378 memcpy(&a, &x, 8); 1379 e = (a>>52) - 1022; 1380 return e*10; 1381 } 1382 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 1383 1384 /* 1385 ** Convert a LogEst into an integer. 1386 */ 1387 u64 sqlite3LogEstToInt(LogEst x){ 1388 u64 n; 1389 if( x<10 ) return 1; 1390 n = x%10; 1391 x /= 10; 1392 if( n>=5 ) n -= 2; 1393 else if( n>=1 ) n -= 1; 1394 if( x>=3 ){ 1395 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); 1396 } 1397 return (n+8)>>(3-x); 1398 } 1399