sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 21 | #include <math.h> |
| 22 | |
| 23 | /* Work around a bug in older Microsoft compilers |
| 24 | ** Forum post 2026-04-10T06:33:11z */ |
| 25 | #if !defined(INFINITY) && defined(_MSC_VER) |
| 26 | # define INFINITY HUGE_VAL |
| 27 | #endif |
| 28 | |
| 29 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 30 | |
| 31 | /* |
| 32 | ** Calls to sqlite3FaultSim() are used to simulate a failure during testing, |
| 33 | ** or to bypass normal error detection during testing in order to let |
| 34 | ** execute proceed further downstream. |
| 35 | ** |
| 36 | ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The |
| 37 | ** sqlite3FaultSim() function only returns non-zero during testing. |
| 38 | ** |
| 39 | ** During testing, if the test harness has set a fault-sim callback using |
| 40 | ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then |
| 41 | ** each call to sqlite3FaultSim() is relayed to that application-supplied |
| 42 | ** callback and the integer return value form the application-supplied |
| 43 | ** callback is returned by sqlite3FaultSim(). |
| 44 | ** |
| 45 | ** The integer argument to sqlite3FaultSim() is a code to identify which |
| 46 | ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() |
| 47 | ** should have a unique code. To prevent legacy testing applications from |
| 48 | ** breaking, the codes should not be changed or reused. |
| 49 | */ |
| 50 | #ifndef SQLITE_UNTESTABLE |
| 51 | int sqlite3FaultSim(int iTest){ |
| 52 | int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
| 53 | return xCallback ? xCallback(iTest) : SQLITE_OK; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 58 | /* |
| 59 | ** Return true if the floating point value is Not a Number (NaN). |
| 60 | ** |
| 61 | ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
| 62 | ** Otherwise, we have our own implementation that works on most systems. |
| 63 | */ |
| 64 | int sqlite3IsNaN(double x){ |
| 65 | int rc; /* The value return */ |
| 66 | #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN |
| 67 | u64 y; |
| 68 | memcpy(&y,&x,sizeof(y)); |
| 69 | rc = IsNaN(y); |
| 70 | #else |
| 71 | rc = isnan(x); |
| 72 | #endif /* HAVE_ISNAN */ |
| 73 | testcase( rc ); |
| 74 | return rc; |
| 75 | } |
| 76 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 77 | |
| 78 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 79 | /* |
| 80 | ** Return true if the floating point value is NaN or +Inf or -Inf. |
| 81 | */ |
| 82 | int sqlite3IsOverflow(double x){ |
| 83 | int rc; /* The value return */ |
| 84 | u64 y; |
| 85 | memcpy(&y,&x,sizeof(y)); |
| 86 | rc = IsOvfl(y); |
| 87 | return rc; |
| 88 | } |
| 89 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 90 | |
| 91 | /* |
| 92 | ** Compute a string length that is limited to what can be stored in |
| 93 | ** lower 30 bits of a 32-bit signed integer. |
| 94 | ** |
| 95 | ** The value returned will never be negative. Nor will it ever be greater |
| 96 | ** than the actual length of the string. For very long strings (greater |
| 97 | ** than 1GiB) the value returned might be less than the true string length. |
| 98 | */ |
| 99 | int sqlite3Strlen30(const char *z){ |
| 100 | if( z==0 ) return 0; |
| 101 | return 0x3fffffff & (int)strlen(z); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | ** Return the declared type of a column. Or return zDflt if the column |
| 106 | ** has no declared type. |
| 107 | ** |
| 108 | ** The column type is an extra string stored after the zero-terminator on |
| 109 | ** the column name if and only if the COLFLAG_HASTYPE flag is set. |
| 110 | */ |
| 111 | char *sqlite3ColumnType(Column *pCol, char *zDflt){ |
| 112 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 113 | return pCol->zCnName + strlen(pCol->zCnName) + 1; |
| 114 | }else if( pCol->eCType ){ |
| 115 | assert( pCol->eCType<=SQLITE_N_STDTYPE ); |
| 116 | return (char*)sqlite3StdType[pCol->eCType-1]; |
| 117 | }else{ |
| 118 | return zDflt; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | ** Helper function for sqlite3Error() - called rarely. Broken out into |
| 124 | ** a separate routine to avoid unnecessary register saves on entry to |
| 125 | ** sqlite3Error(). |
| 126 | */ |
| 127 | static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ |
| 128 | if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 129 | sqlite3SystemError(db, err_code); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | ** Set the current error code to err_code and clear any prior error message. |
| 134 | ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates |
| 135 | ** that would be appropriate. |
| 136 | */ |
| 137 | void sqlite3Error(sqlite3 *db, int err_code){ |
| 138 | assert( db!=0 ); |
| 139 | db->errCode = err_code; |
| 140 | if( err_code || db->pErr ){ |
| 141 | sqlite3ErrorFinish(db, err_code); |
| 142 | }else{ |
| 143 | db->errByteOffset = -1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state |
| 149 | ** and error message. |
| 150 | */ |
| 151 | void sqlite3ErrorClear(sqlite3 *db){ |
| 152 | assert( db!=0 ); |
| 153 | db->errCode = SQLITE_OK; |
| 154 | db->errByteOffset = -1; |
| 155 | if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| 160 | ** to do based on the SQLite error code in rc. |
| 161 | */ |
| 162 | void sqlite3SystemError(sqlite3 *db, int rc){ |
| 163 | if( rc==SQLITE_IOERR_NOMEM ) return; |
| 164 | #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) |
| 165 | if( rc==SQLITE_IOERR_IN_PAGE ){ |
| 166 | int ii; |
| 167 | int iErr; |
| 168 | sqlite3BtreeEnterAll(db); |
| 169 | for(ii=0; ii<db->nDb; ii++){ |
| 170 | if( db->aDb[ii].pBt ){ |
| 171 | iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); |
| 172 | if( iErr ){ |
| 173 | db->iSysErrno = iErr; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | sqlite3BtreeLeaveAll(db); |
| 178 | return; |
| 179 | } |
| 180 | #endif |
| 181 | rc &= 0xff; |
| 182 | if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ |
| 183 | db->iSysErrno = sqlite3OsGetLastError(db->pVfs); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | ** Set the most recent error code and error string for the sqlite |
| 189 | ** handle "db". The error code is set to "err_code". |
| 190 | ** |
| 191 | ** If it is not NULL, string zFormat specifies the format of the |
| 192 | ** error string. zFormat and any string tokens that follow it are |
| 193 | ** assumed to be encoded in UTF-8. |
| 194 | ** |
| 195 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| 196 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 197 | ** to NULL. |
| 198 | */ |
| 199 | void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ |
| 200 | assert( db!=0 ); |
| 201 | db->errCode = err_code; |
| 202 | sqlite3SystemError(db, err_code); |
| 203 | if( zFormat==0 ){ |
| 204 | sqlite3Error(db, err_code); |
| 205 | }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ |
| 206 | char *z; |
| 207 | va_list ap; |
| 208 | va_start(ap, zFormat); |
| 209 | z = sqlite3VMPrintf(db, zFormat, ap); |
| 210 | va_end(ap); |
| 211 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | ** Check for interrupts and invoke progress callback. |
| 217 | */ |
| 218 | void sqlite3ProgressCheck(Parse *p){ |
| 219 | sqlite3 *db = p->db; |
| 220 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
| 221 | p->nErr++; |
| 222 | p->rc = SQLITE_INTERRUPT; |
| 223 | } |
| 224 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 225 | if( db->xProgress ){ |
| 226 | if( p->rc==SQLITE_INTERRUPT ){ |
| 227 | p->nProgressSteps = 0; |
| 228 | }else if( (++p->nProgressSteps)>=db->nProgressOps ){ |
| 229 | if( db->xProgress(db->pProgressArg) ){ |
| 230 | p->nErr++; |
| 231 | p->rc = SQLITE_INTERRUPT; |
| 232 | } |
| 233 | p->nProgressSteps = 0; |
| 234 | } |
| 235 | } |
| 236 | #endif |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 241 | ** |
| 242 | ** This function should be used to report any error that occurs while |
| 243 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 244 | ** last thing the sqlite3_prepare() function does is copy the error |
| 245 | ** stored by this function into the database handle using sqlite3Error(). |
| 246 | ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used |
| 247 | ** during statement execution (sqlite3_step() etc.). |
| 248 | */ |
| 249 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 250 | char *zMsg; |
| 251 | va_list ap; |
| 252 | sqlite3 *db = pParse->db; |
| 253 | assert( db!=0 ); |
| 254 | assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); |
| 255 | db->errByteOffset = -2; |
| 256 | va_start(ap, zFormat); |
| 257 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
| 258 | va_end(ap); |
| 259 | if( db->errByteOffset<-1 ) db->errByteOffset = -1; |
| 260 | if( db->suppressErr ){ |
| 261 | sqlite3DbFree(db, zMsg); |
| 262 | if( db->mallocFailed ){ |
| 263 | pParse->nErr++; |
| 264 | pParse->rc = SQLITE_NOMEM; |
| 265 | } |
| 266 | }else{ |
| 267 | pParse->nErr++; |
| 268 | sqlite3DbFree(db, pParse->zErrMsg); |
| 269 | pParse->zErrMsg = zMsg; |
| 270 | pParse->rc = SQLITE_ERROR; |
| 271 | pParse->pWith = 0; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | ** If database connection db is currently parsing SQL, then transfer |
| 277 | ** error code errCode to that parser if the parser has not already |
| 278 | ** encountered some other kind of error. |
| 279 | */ |
| 280 | int sqlite3ErrorToParser(sqlite3 *db, int errCode){ |
| 281 | Parse *pParse; |
| 282 | if( db==0 || (pParse = db->pParse)==0 ) return errCode; |
| 283 | pParse->rc = errCode; |
| 284 | pParse->nErr++; |
| 285 | return errCode; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | ** Convert an SQL-style quoted string into a normal string by removing |
| 290 | ** the quote characters. The conversion is done in-place. If the |
| 291 | ** input does not begin with a quote character, then this routine |
| 292 | ** is a no-op. |
| 293 | ** |
| 294 | ** The input string must be zero-terminated. A new zero-terminator |
| 295 | ** is added to the dequoted string. |
| 296 | ** |
| 297 | ** The return value is -1 if no dequoting occurs or the length of the |
| 298 | ** dequoted string, exclusive of the zero terminator, if dequoting does |
| 299 | ** occur. |
| 300 | ** |
| 301 | ** 2002-02-14: This routine is extended to remove MS-Access style |
| 302 | ** brackets from around identifiers. For example: "[a-b-c]" becomes |
| 303 | ** "a-b-c". |
| 304 | */ |
| 305 | void sqlite3Dequote(char *z){ |
| 306 | char quote; |
| 307 | int i, j; |
| 308 | if( z==0 ) return; |
| 309 | quote = z[0]; |
| 310 | if( !sqlite3Isquote(quote) ) return; |
| 311 | if( quote=='[' ) quote = ']'; |
| 312 | for(i=1, j=0;; i++){ |
| 313 | assert( z[i] ); |
| 314 | if( z[i]==quote ){ |
| 315 | if( z[i+1]==quote ){ |
| 316 | z[j++] = quote; |
| 317 | i++; |
| 318 | }else{ |
| 319 | break; |
| 320 | } |
| 321 | }else{ |
| 322 | z[j++] = z[i]; |
| 323 | } |
| 324 | } |
| 325 | z[j] = 0; |
| 326 | } |
| 327 | void sqlite3DequoteExpr(Expr *p){ |
| 328 | assert( !ExprHasProperty(p, EP_IntValue) ); |
| 329 | assert( sqlite3Isquote(p->u.zToken[0]) ); |
| 330 | p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; |
| 331 | sqlite3Dequote(p->u.zToken); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken |
| 336 | ** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those |
| 337 | ** that contain '_' characters that must be removed before further processing. |
| 338 | */ |
| 339 | void sqlite3DequoteNumber(Parse *pParse, Expr *p){ |
| 340 | assert( p!=0 || pParse->db->mallocFailed ); |
| 341 | if( p ){ |
| 342 | const char *pIn = p->u.zToken; |
| 343 | char *pOut = p->u.zToken; |
| 344 | int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X')); |
| 345 | int iValue; |
| 346 | assert( p->op==TK_QNUMBER ); |
| 347 | p->op = TK_INTEGER; |
| 348 | do { |
| 349 | if( *pIn!=SQLITE_DIGIT_SEPARATOR ){ |
| 350 | *pOut++ = *pIn; |
| 351 | if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; |
| 352 | }else{ |
| 353 | if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) |
| 354 | || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) |
| 355 | ){ |
| 356 | sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); |
| 357 | } |
| 358 | } |
| 359 | }while( *pIn++ ); |
| 360 | if( bHex ) p->op = TK_INTEGER; |
| 361 | |
| 362 | /* tag-20240227-a: If after dequoting, the number is an integer that |
| 363 | ** fits in 32 bits, then it must be converted into EP_IntValue. Other |
| 364 | ** parts of the code expect this. See also tag-20240227-b. */ |
| 365 | if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ |
| 366 | p->u.iValue = iValue; |
| 367 | p->flags |= EP_IntValue; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | ** If the input token p is quoted, try to adjust the token to remove |
| 374 | ** the quotes. This is not always possible: |
| 375 | ** |
| 376 | ** "abc" -> abc |
| 377 | ** "ab""cd" -> (not possible because of the interior "") |
| 378 | ** |
| 379 | ** Remove the quotes if possible. This is a optimization. The overall |
| 380 | ** system should still return the correct answer even if this routine |
| 381 | ** is always a no-op. |
| 382 | */ |
| 383 | void sqlite3DequoteToken(Token *p){ |
| 384 | unsigned int i; |
| 385 | if( p->n<2 ) return; |
| 386 | if( !sqlite3Isquote(p->z[0]) ) return; |
| 387 | for(i=1; i<p->n-1; i++){ |
| 388 | if( sqlite3Isquote(p->z[i]) ) return; |
| 389 | } |
| 390 | p->n -= 2; |
| 391 | p->z++; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | ** Generate a Token object from a string |
| 396 | */ |
| 397 | void sqlite3TokenInit(Token *p, char *z){ |
| 398 | p->z = z; |
| 399 | p->n = sqlite3Strlen30(z); |
| 400 | } |
| 401 | |
| 402 | /* Convenient short-hand */ |
| 403 | #define UpperToLower sqlite3UpperToLower |
| 404 | |
| 405 | /* |
| 406 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 407 | ** there is no consistency, we will define our own. |
| 408 | ** |
| 409 | ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
| 410 | ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
| 411 | ** the contents of two buffers containing UTF-8 strings in a |
| 412 | ** case-independent fashion, using the same definition of "case |
| 413 | ** independence" that SQLite uses internally when comparing identifiers. |
| 414 | */ |
| 415 | int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 416 | if( zLeft==0 ){ |
| 417 | return zRight ? -1 : 0; |
| 418 | }else if( zRight==0 ){ |
| 419 | return 1; |
| 420 | } |
| 421 | return sqlite3StrICmp(zLeft, zRight); |
| 422 | } |
| 423 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 424 | unsigned char *a, *b; |
| 425 | int c, x; |
| 426 | a = (unsigned char *)zLeft; |
| 427 | b = (unsigned char *)zRight; |
| 428 | for(;;){ |
| 429 | c = *a; |
| 430 | x = *b; |
| 431 | if( c==x ){ |
| 432 | if( c==0 ) break; |
| 433 | }else{ |
| 434 | c = (int)UpperToLower[c] - (int)UpperToLower[x]; |
| 435 | if( c ) break; |
| 436 | } |
| 437 | a++; |
| 438 | b++; |
| 439 | } |
| 440 | return c; |
| 441 | } |
| 442 | int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 443 | register unsigned char *a, *b; |
| 444 | if( zLeft==0 ){ |
| 445 | return zRight ? -1 : 0; |
| 446 | }else if( zRight==0 ){ |
| 447 | return 1; |
| 448 | } |
| 449 | a = (unsigned char *)zLeft; |
| 450 | b = (unsigned char *)zRight; |
| 451 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 452 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | ** Compute an 8-bit hash on a string that is insensitive to case differences |
| 457 | */ |
| 458 | u8 sqlite3StrIHash(const char *z){ |
| 459 | u8 h = 0; |
| 460 | if( z==0 ) return 0; |
| 461 | while( z[0] ){ |
| 462 | h += UpperToLower[(unsigned char)z[0]]; |
| 463 | z++; |
| 464 | } |
| 465 | return h; |
| 466 | } |
| 467 | |
| 468 | #if !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 469 | && (defined(__GNUC__) || defined(__clang__)) \ |
| 470 | && (defined(__x86_64__) || defined(__aarch64__) || \ |
| 471 | (defined(__riscv) && defined(__riscv_xlen) && (__riscv_xlen>32))) |
| 472 | #define SQLITE_USE_UINT128 |
| 473 | #endif |
| 474 | |
| 475 | /* |
| 476 | ** Two inputs are multiplied to get a 128-bit result. Write the |
| 477 | ** lower 64-bits of the result into *pLo, and return the high-order |
| 478 | ** 64 bits. |
| 479 | */ |
| 480 | static u64 sqlite3Multiply128(u64 a, u64 b, u64 *pLo){ |
| 481 | #if defined(SQLITE_USE_UINT128) |
| 482 | __uint128_t r = (__uint128_t)a * b; |
| 483 | *pLo = (u64)r; |
| 484 | return (u64)(r>>64); |
| 485 | #elif defined(_WIN64) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 486 | *pLo = a*b; |
| 487 | return __umulh(a, b); |
| 488 | #else |
| 489 | u64 a0 = (u32)a; |
| 490 | u64 a1 = a >> 32; |
| 491 | u64 b0 = (u32)b; |
| 492 | u64 b1 = b >> 32; |
| 493 | u64 a0b0 = a0 * b0; |
| 494 | u64 a1b1 = a1 * b1; |
| 495 | u64 a0b1 = a0 * b1; |
| 496 | u64 a1b0 = a1 * b0; |
| 497 | u64 t = (a0b0 >> 32) + (u32)a0b1 + (u32)a1b0; |
| 498 | *pLo = (a0b0 & UINT64_C(0xffffffff)) | (t << 32); |
| 499 | return a1b1 + (a0b1>>32) + (a1b0>>32) + (t>>32); |
| 500 | #endif |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | ** A is an unsigned 96-bit integer formed by (a<<32)+aLo. |
| 505 | ** B is an unsigned 64-bit integer. |
| 506 | ** |
| 507 | ** Compute the upper 96 bits of 160-bit result of A*B. |
| 508 | ** |
| 509 | ** Write ((A*B)>>64 & 0xffffffff) (the middle 32 bits of A*B) |
| 510 | ** into *pLo. Return the upper 64 bits of A*B. |
| 511 | ** |
| 512 | ** The lower 64 bits of A*B are discarded. |
| 513 | */ |
| 514 | static u64 sqlite3Multiply160(u64 a, u32 aLo, u64 b, u32 *pLo){ |
| 515 | #if defined(SQLITE_USE_UINT128) |
| 516 | __uint128_t r = (__uint128_t)a * b; |
| 517 | r += ((__uint128_t)aLo * b) >> 32; |
| 518 | *pLo = (r>>32)&0xffffffff; |
| 519 | return r>>64; |
| 520 | #elif defined(_WIN64) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 521 | u64 r1_hi = __umulh(a,b); |
| 522 | u64 r1_lo = a*b; |
| 523 | u64 r2 = (__umulh((u64)aLo,b)<<32) + ((aLo*b)>>32); |
| 524 | u64 t = r1_lo + r2; |
| 525 | if( t<r1_lo ) r1_hi++; |
| 526 | *pLo = t>>32; |
| 527 | return r1_hi; |
| 528 | #else |
| 529 | u64 x2 = a>>32; |
| 530 | u64 x1 = a&0xffffffff; |
| 531 | u64 x0 = aLo; |
| 532 | u64 y1 = b>>32; |
| 533 | u64 y0 = b&0xffffffff; |
| 534 | u64 x2y1 = x2*y1; |
| 535 | u64 r4 = x2y1>>32; |
| 536 | u64 x2y0 = x2*y0; |
| 537 | u64 x1y1 = x1*y1; |
| 538 | u64 r3 = (x2y1 & 0xffffffff) + (x2y0 >>32) + (x1y1 >>32); |
| 539 | u64 x1y0 = x1*y0; |
| 540 | u64 x0y1 = x0*y1; |
| 541 | u64 r2 = (x2y0 & 0xffffffff) + (x1y1 & 0xffffffff) + |
| 542 | (x1y0 >>32) + (x0y1>>32); |
| 543 | u64 x0y0 = x0*y0; |
| 544 | u64 r1 = (x1y0 & 0xffffffff) + (x0y1 & 0xffffffff) + |
| 545 | (x0y0 >>32); |
| 546 | r2 += r1>>32; |
| 547 | r3 += r2>>32; |
| 548 | *pLo = r2&0xffffffff; |
| 549 | return (r4<<32) + r3; |
| 550 | #endif |
| 551 | } |
| 552 | |
| 553 | #undef SQLITE_USE_UINT128 |
| 554 | |
| 555 | /* |
| 556 | ** Return a u64 with the N-th bit set. |
| 557 | */ |
| 558 | #define U64_BIT(N) (((u64)1)<<(N)) |
| 559 | |
| 560 | /* |
| 561 | ** Range of powers of 10 that we need to deal with when converting |
| 562 | ** IEEE754 doubles to and from decimal. |
| 563 | */ |
| 564 | #define POWERSOF10_FIRST (-348) |
| 565 | #define POWERSOF10_LAST (+347) |
| 566 | |
| 567 | /* |
| 568 | ** For any p between -348 and +347, return the integer part of |
| 569 | ** |
| 570 | ** pow(10,p) * pow(2,63-pow10to2(p)) |
| 571 | ** |
| 572 | ** Or, in other words, for any p in range, return the most significant |
| 573 | ** 64 bits of pow(10,p). The pow(10,p) value is shifted left or right, |
| 574 | ** as appropriate so the most significant 64 bits fit exactly into a |
| 575 | ** 64-bit unsigned integer. |
| 576 | ** |
| 577 | ** Write into *pLo the next 32 significant bits of the answer after |
| 578 | ** the first 64. |
| 579 | ** |
| 580 | ** Algorithm: |
| 581 | ** |
| 582 | ** (1) For p between 0 and 26, return the value directly from the aBase[] |
| 583 | ** lookup table. |
| 584 | ** |
| 585 | ** (2) For p outside the range 0 to 26, use aScale[] for the initial value |
| 586 | ** then refine that result (if necessary) by a single multiplication |
| 587 | ** against aBase[]. |
| 588 | ** |
| 589 | ** The constant tables aBase[], aScale[], and aScaleLo[] are generated |
| 590 | ** by the C program at ../tool/mkfptab.c run with the --round option. |
| 591 | */ |
| 592 | static u64 powerOfTen(int p, u32 *pLo){ |
| 593 | static const u64 aBase[] = { |
| 594 | UINT64_C(0x8000000000000000), /* 0: 1.0e+0 << 63 */ |
| 595 | UINT64_C(0xa000000000000000), /* 1: 1.0e+1 << 60 */ |
| 596 | UINT64_C(0xc800000000000000), /* 2: 1.0e+2 << 57 */ |
| 597 | UINT64_C(0xfa00000000000000), /* 3: 1.0e+3 << 54 */ |
| 598 | UINT64_C(0x9c40000000000000), /* 4: 1.0e+4 << 50 */ |
| 599 | UINT64_C(0xc350000000000000), /* 5: 1.0e+5 << 47 */ |
| 600 | UINT64_C(0xf424000000000000), /* 6: 1.0e+6 << 44 */ |
| 601 | UINT64_C(0x9896800000000000), /* 7: 1.0e+7 << 40 */ |
| 602 | UINT64_C(0xbebc200000000000), /* 8: 1.0e+8 << 37 */ |
| 603 | UINT64_C(0xee6b280000000000), /* 9: 1.0e+9 << 34 */ |
| 604 | UINT64_C(0x9502f90000000000), /* 10: 1.0e+10 << 30 */ |
| 605 | UINT64_C(0xba43b74000000000), /* 11: 1.0e+11 << 27 */ |
| 606 | UINT64_C(0xe8d4a51000000000), /* 12: 1.0e+12 << 24 */ |
| 607 | UINT64_C(0x9184e72a00000000), /* 13: 1.0e+13 << 20 */ |
| 608 | UINT64_C(0xb5e620f480000000), /* 14: 1.0e+14 << 17 */ |
| 609 | UINT64_C(0xe35fa931a0000000), /* 15: 1.0e+15 << 14 */ |
| 610 | UINT64_C(0x8e1bc9bf04000000), /* 16: 1.0e+16 << 10 */ |
| 611 | UINT64_C(0xb1a2bc2ec5000000), /* 17: 1.0e+17 << 7 */ |
| 612 | UINT64_C(0xde0b6b3a76400000), /* 18: 1.0e+18 << 4 */ |
| 613 | UINT64_C(0x8ac7230489e80000), /* 19: 1.0e+19 >> 0 */ |
| 614 | UINT64_C(0xad78ebc5ac620000), /* 20: 1.0e+20 >> 3 */ |
| 615 | UINT64_C(0xd8d726b7177a8000), /* 21: 1.0e+21 >> 6 */ |
| 616 | UINT64_C(0x878678326eac9000), /* 22: 1.0e+22 >> 10 */ |
| 617 | UINT64_C(0xa968163f0a57b400), /* 23: 1.0e+23 >> 13 */ |
| 618 | UINT64_C(0xd3c21bcecceda100), /* 24: 1.0e+24 >> 16 */ |
| 619 | UINT64_C(0x84595161401484a0), /* 25: 1.0e+25 >> 20 */ |
| 620 | UINT64_C(0xa56fa5b99019a5c8), /* 26: 1.0e+26 >> 23 */ |
| 621 | }; |
| 622 | static const u64 aScale[] = { |
| 623 | UINT64_C(0x8049a4ac0c5811ae), /* 0: 1.0e-351 << 1229 */ |
| 624 | UINT64_C(0xcf42894a5dce35ea), /* 1: 1.0e-324 << 1140 */ |
| 625 | UINT64_C(0xa76c582338ed2621), /* 2: 1.0e-297 << 1050 */ |
| 626 | UINT64_C(0x873e4f75e2224e68), /* 3: 1.0e-270 << 960 */ |
| 627 | UINT64_C(0xda7f5bf590966848), /* 4: 1.0e-243 << 871 */ |
| 628 | UINT64_C(0xb080392cc4349dec), /* 5: 1.0e-216 << 781 */ |
| 629 | UINT64_C(0x8e938662882af53e), /* 6: 1.0e-189 << 691 */ |
| 630 | UINT64_C(0xe65829b3046b0afa), /* 7: 1.0e-162 << 602 */ |
| 631 | UINT64_C(0xba121a4650e4ddeb), /* 8: 1.0e-135 << 512 */ |
| 632 | UINT64_C(0x964e858c91ba2655), /* 9: 1.0e-108 << 422 */ |
| 633 | UINT64_C(0xf2d56790ab41c2a2), /* 10: 1.0e-81 << 333 */ |
| 634 | UINT64_C(0xc428d05aa4751e4c), /* 11: 1.0e-54 << 243 */ |
| 635 | UINT64_C(0x9e74d1b791e07e48), /* 12: 1.0e-27 << 153 */ |
| 636 | UINT64_C(0xcccccccccccccccc), /* 13: 1.0e-1 << 67 (special case) */ |
| 637 | UINT64_C(0xcecb8f27f4200f3a), /* 14: 1.0e+27 >> 26 */ |
| 638 | UINT64_C(0xa70c3c40a64e6c51), /* 15: 1.0e+54 >> 116 */ |
| 639 | UINT64_C(0x86f0ac99b4e8dafd), /* 16: 1.0e+81 >> 206 */ |
| 640 | UINT64_C(0xda01ee641a708de9), /* 17: 1.0e+108 >> 295 */ |
| 641 | UINT64_C(0xb01ae745b101e9e4), /* 18: 1.0e+135 >> 385 */ |
| 642 | UINT64_C(0x8e41ade9fbebc27d), /* 19: 1.0e+162 >> 475 */ |
| 643 | UINT64_C(0xe5d3ef282a242e81), /* 20: 1.0e+189 >> 564 */ |
| 644 | UINT64_C(0xb9a74a0637ce2ee1), /* 21: 1.0e+216 >> 654 */ |
| 645 | UINT64_C(0x95f83d0a1fb69cd9), /* 22: 1.0e+243 >> 744 */ |
| 646 | UINT64_C(0xf24a01a73cf2dccf), /* 23: 1.0e+270 >> 833 */ |
| 647 | UINT64_C(0xc3b8358109e84f07), /* 24: 1.0e+297 >> 923 */ |
| 648 | UINT64_C(0x9e19db92b4e31ba9), /* 25: 1.0e+324 >> 1013 */ |
| 649 | }; |
| 650 | static const unsigned int aScaleLo[] = { |
| 651 | 0x205b896d, /* 0: 1.0e-351 << 1229 */ |
| 652 | 0x52064cad, /* 1: 1.0e-324 << 1140 */ |
| 653 | 0xaf2af2b8, /* 2: 1.0e-297 << 1050 */ |
| 654 | 0x5a7744a7, /* 3: 1.0e-270 << 960 */ |
| 655 | 0xaf39a475, /* 4: 1.0e-243 << 871 */ |
| 656 | 0xbd8d794e, /* 5: 1.0e-216 << 781 */ |
| 657 | 0x547eb47b, /* 6: 1.0e-189 << 691 */ |
| 658 | 0x0cb4a5a3, /* 7: 1.0e-162 << 602 */ |
| 659 | 0x92f34d62, /* 8: 1.0e-135 << 512 */ |
| 660 | 0x3a6a07f9, /* 9: 1.0e-108 << 422 */ |
| 661 | 0xfae27299, /* 10: 1.0e-81 << 333 */ |
| 662 | 0xaa97e14c, /* 11: 1.0e-54 << 243 */ |
| 663 | 0x775ea265, /* 12: 1.0e-27 << 153 */ |
| 664 | 0xcccccccc, /* 13: 1.0e-1 << 67 (special case) */ |
| 665 | 0x00000000, /* 14: 1.0e+27 >> 26 */ |
| 666 | 0x999090b6, /* 15: 1.0e+54 >> 116 */ |
| 667 | 0x69a028bb, /* 16: 1.0e+81 >> 206 */ |
| 668 | 0xe80e6f48, /* 17: 1.0e+108 >> 295 */ |
| 669 | 0x5ec05dd0, /* 18: 1.0e+135 >> 385 */ |
| 670 | 0x14588f14, /* 19: 1.0e+162 >> 475 */ |
| 671 | 0x8f1668c9, /* 20: 1.0e+189 >> 564 */ |
| 672 | 0x6d953e2c, /* 21: 1.0e+216 >> 654 */ |
| 673 | 0x4abdaf10, /* 22: 1.0e+243 >> 744 */ |
| 674 | 0xbc633b39, /* 23: 1.0e+270 >> 833 */ |
| 675 | 0x0a862f81, /* 24: 1.0e+297 >> 923 */ |
| 676 | 0x6c07a2c2, /* 25: 1.0e+324 >> 1013 */ |
| 677 | }; |
| 678 | int g, n; |
| 679 | u64 s, x; |
| 680 | u32 lo; |
| 681 | |
| 682 | assert( p>=POWERSOF10_FIRST && p<=POWERSOF10_LAST ); |
| 683 | if( p<0 ){ |
| 684 | if( p==(-1) ){ |
| 685 | *pLo = aScaleLo[13]; |
| 686 | return aScale[13]; |
| 687 | } |
| 688 | g = p/27; |
| 689 | n = p%27; |
| 690 | if( n ){ |
| 691 | g--; |
| 692 | n += 27; |
| 693 | } |
| 694 | }else if( p<27 ){ |
| 695 | *pLo = 0; |
| 696 | return aBase[p]; |
| 697 | }else{ |
| 698 | g = p/27; |
| 699 | n = p%27; |
| 700 | } |
| 701 | s = aScale[g+13]; |
| 702 | if( n==0 ){ |
| 703 | *pLo = aScaleLo[g+13]; |
| 704 | return s; |
| 705 | } |
| 706 | x = sqlite3Multiply160(s,aScaleLo[g+13],aBase[n],&lo); |
| 707 | if( (U64_BIT(63) & x)==0 ){ |
| 708 | x = x<<1 | ((lo>>31)&1); |
| 709 | lo = (lo<<1) | 1; |
| 710 | } |
| 711 | *pLo = lo; |
| 712 | return x; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | ** pow10to2(x) computes floor(log2(pow(10,x))). |
| 717 | ** pow2to10(y) computes floor(log10(pow(2,y))). |
| 718 | ** |
| 719 | ** Conceptually, pow10to2(p) converts a base-10 exponent p into |
| 720 | ** a corresponding base-2 exponent, and pow2to10(e) converts a base-2 |
| 721 | ** exponent into a base-10 exponent. |
| 722 | ** |
| 723 | ** The conversions are based on the observation that: |
| 724 | ** |
| 725 | ** ln(10.0)/ln(2.0) == 108853/32768 (approximately) |
| 726 | ** ln(2.0)/ln(10.0) == 78913/262144 (approximately) |
| 727 | ** |
| 728 | ** These ratios are approximate, but they are accurate to 5 digits, |
| 729 | ** which is close enough for the usage here. Right-shift is used |
| 730 | ** for division so that rounding of negative numbers happens in the |
| 731 | ** right direction. |
| 732 | */ |
| 733 | static int pwr10to2(int p){ return (p*108853) >> 15; } |
| 734 | static int pwr2to10(int p){ return (p*78913) >> 18; } |
| 735 | |
| 736 | /* |
| 737 | ** Count leading zeros for a 64-bit unsigned integer. |
| 738 | */ |
| 739 | static int countLeadingZeros(u64 m){ |
| 740 | #if (defined(__GNUC__) || defined(__clang__)) \ |
| 741 | && !defined(SQLITE_DISABLE_INTRINSIC) |
| 742 | return __builtin_clzll(m); |
| 743 | #else |
| 744 | int n = 0; |
| 745 | if( m <= 0x00000000ffffffffULL) { n += 32; m <<= 32; } |
| 746 | if( m <= 0x0000ffffffffffffULL) { n += 16; m <<= 16; } |
| 747 | if( m <= 0x00ffffffffffffffULL) { n += 8; m <<= 8; } |
| 748 | if( m <= 0x0fffffffffffffffULL) { n += 4; m <<= 4; } |
| 749 | if( m <= 0x3fffffffffffffffULL) { n += 2; m <<= 2; } |
| 750 | if( m <= 0x7fffffffffffffffULL) { n += 1; } |
| 751 | return n; |
| 752 | #endif |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | ** Given m and e, which represent a quantity r == m*pow(2,e), |
| 757 | ** return values *pD and *pP such that r == (*pD)*pow(10,*pP), |
| 758 | ** approximately. *pD should contain at least n significant digits. |
| 759 | ** |
| 760 | ** The input m is required to have its highest bit set. In other words, |
| 761 | ** m should be left-shifted, and e decremented, to maximize the value of m. |
| 762 | */ |
| 763 | static void sqlite3Fp2Convert10(u64 m, int e, int n, u64 *pD, int *pP){ |
| 764 | int p; |
| 765 | u64 h, d1; |
| 766 | u32 d2; |
| 767 | assert( n>=1 && n<=18 ); |
| 768 | p = n - 1 - pwr2to10(e+63); |
| 769 | h = sqlite3Multiply128(m, powerOfTen(p,&d2), &d1); |
| 770 | assert( -(e + pwr10to2(p) + 2) >= 0 ); |
| 771 | assert( -(e + pwr10to2(p) + 1) <= 63 ); |
| 772 | if( n==18 ){ |
| 773 | h >>= -(e + pwr10to2(p) + 2); |
| 774 | *pD = (h + ((h<<1)&2))>>1; |
| 775 | }else{ |
| 776 | *pD = h >> -(e + pwr10to2(p) + 1); |
| 777 | } |
| 778 | *pP = -p; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | ** Return an IEEE754 floating point value that approximates d*pow(10,p). |
| 783 | ** |
| 784 | ** The (current) algorithm is adapted from the work of Ross Cox at |
| 785 | ** https://github.com/rsc/fpfmt |
| 786 | */ |
| 787 | static double sqlite3Fp10Convert2(u64 d, i64 p){ |
| 788 | int b, lp, e, adj, s; |
| 789 | u32 pwr10l, mid1; |
| 790 | u64 pwr10h, x, hi, lo, sticky, u, m; |
| 791 | double r; |
| 792 | if( p<POWERSOF10_FIRST ) return 0.0; |
| 793 | if( p>POWERSOF10_LAST ) return (double)INFINITY; |
| 794 | b = 64 - countLeadingZeros(d); |
| 795 | lp = pwr10to2(p); |
| 796 | e = 53 - b - lp; |
| 797 | if( e > 1074 ){ |
| 798 | if( e>=1130 ) return 0.0; |
| 799 | e = 1074; |
| 800 | } |
| 801 | s = -(e-(64-b) + lp + 3); |
| 802 | pwr10h = powerOfTen(p, &pwr10l); |
| 803 | if( pwr10l!=0 ){ |
| 804 | pwr10h++; |
| 805 | pwr10l = ~pwr10l; |
| 806 | } |
| 807 | x = d<<(64-b); |
| 808 | hi = sqlite3Multiply128(x,pwr10h,&lo); |
| 809 | mid1 = lo>>32; |
| 810 | sticky = 1; |
| 811 | if( (hi & (U64_BIT(s)-1))==0 ) { |
| 812 | u32 mid2 = sqlite3Multiply128(x,((u64)pwr10l)<<32,&lo)>>32; |
| 813 | sticky = (mid1-mid2 > 1); |
| 814 | hi -= mid1 < mid2; |
| 815 | } |
| 816 | u = (hi>>s) | sticky; |
| 817 | adj = (u >= U64_BIT(55)-2); |
| 818 | if( adj ){ |
| 819 | u = (u>>adj) | (u&1); |
| 820 | e -= adj; |
| 821 | } |
| 822 | m = (u + 1 + ((u>>2)&1)) >> 2; |
| 823 | if( e<=(-972) ) return (double)INFINITY; |
| 824 | if((m & U64_BIT(52)) != 0){ |
| 825 | m = (m & ~U64_BIT(52)) | ((u64)(1075-e)<<52); |
| 826 | } |
| 827 | memcpy(&r,&m,8); |
| 828 | return r; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | ** The string z[] is an text representation of a real number. |
| 833 | ** Convert this string to a double and write it into *pResult. |
| 834 | ** |
| 835 | ** z[] must be UTF-8 and zero-terminated. |
| 836 | ** |
| 837 | ** Return positive if the result is a valid real number (or integer) and |
| 838 | ** zero or negative if the string is empty or contains extraneous text. |
| 839 | ** Lower bits of the return value contain addition information about the |
| 840 | ** parse: |
| 841 | ** |
| 842 | ** bit 0 => Set if any prefix of the input is valid. Clear if |
| 843 | ** there is no prefix of the input that can be seen as |
| 844 | ** a valid floating point number. |
| 845 | ** bit 1 => Set if the input contains a decimal point or eNNN |
| 846 | ** clause or other text that disqualifies it as an |
| 847 | ** integer value. Zero if the input is an integer. |
| 848 | ** bit 2 => The input is exactly 0.0, not an underflow from |
| 849 | ** some value near zero. |
| 850 | ** bit 3 => Set if there are more than about 19 significant |
| 851 | ** digits in the input. |
| 852 | ** bit 4 => The input is a special value like "Inf" or "NaN" |
| 853 | ** |
| 854 | ** If the input contains a syntax error but begins with text that might |
| 855 | ** be a valid number of some kind, then the result is negative. The |
| 856 | ** result is only zero if no prefix of the input could be interpreted as |
| 857 | ** a number. |
| 858 | ** |
| 859 | ** Leading and trailing whitespace is ignored. Valid numbers are in |
| 860 | ** one of the formats below: |
| 861 | ** |
| 862 | ** [+-]digits[E[+-]digits] |
| 863 | ** [+-]digits.[digits][E[+-]digits] |
| 864 | ** [+-].digits[E[+-]digits] |
| 865 | ** |
| 866 | ** Algorithm sketch: Compute an unsigned 64-bit integer s and a base-10 |
| 867 | ** exponent d such that the value encoding by the input is s*pow(10,d). |
| 868 | ** Then invoke sqlite3Fp10Convert2() to calculated the closest possible |
| 869 | ** IEEE754 double. The sign is added back afterwards, if the input string |
| 870 | ** starts with a "-". The use of an unsigned 64-bit s mantissa means that |
| 871 | ** only about the first 19 significant digits of the input can contribute |
| 872 | ** to the result. This can result in suboptimal rounding decisions when |
| 873 | ** correct rounding requires more than 19 input digits. For example, |
| 874 | ** this routine renders "3500000000000000.2500001" as |
| 875 | ** 3500000000000000.0 instead of 3500000000000000.5 because the decision |
| 876 | ** to round up instead of using banker's rounding to round down is determined |
| 877 | ** by the 23rd significant digit, which this routine ignores. It is not |
| 878 | ** possible to do better without some kind of BigNum. |
| 879 | */ |
| 880 | int sqlite3AtoF(const char *zIn, double *pResult){ |
| 881 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 882 | const unsigned char *z = (const unsigned char*)zIn; |
| 883 | int neg = 0; /* True for a negative value */ |
| 884 | u64 s = 0; /* mantissa */ |
| 885 | i64 d = 0; /* Value is s * pow(10,d) */ |
| 886 | int mState = 0; /* 1: digit seen 2: fp 4: hard-zero */ |
| 887 | unsigned v; /* Value of a single digit */ |
| 888 | |
| 889 | start_of_text: |
| 890 | if( (v = (unsigned)z[0] - '0')<10 ){ |
| 891 | parse_integer_part: |
| 892 | mState = 1; |
| 893 | s = v; |
| 894 | z++; |
| 895 | while( (v = (unsigned)z[0] - '0')<10 ){ |
| 896 | s = s*10 + v; |
| 897 | z++; |
| 898 | if( s>=(LARGEST_UINT64-9)/10 ){ |
| 899 | mState = 9; |
| 900 | while( sqlite3Isdigit(z[0]) ){ z++; d++; } |
| 901 | break; |
| 902 | } |
| 903 | } |
| 904 | }else if( z[0]=='-' ){ |
| 905 | neg = 1; |
| 906 | z++; |
| 907 | if( (v = (unsigned)z[0] - '0')<10 ) goto parse_integer_part; |
| 908 | }else if( z[0]=='+' ){ |
| 909 | z++; |
| 910 | if( (v = (unsigned)z[0] - '0')<10 ) goto parse_integer_part; |
| 911 | }else if( sqlite3Isspace(z[0]) ){ |
| 912 | do{ z++; }while( sqlite3Isspace(z[0]) ); |
| 913 | goto start_of_text; |
| 914 | }else{ |
| 915 | s = 0; |
| 916 | } |
| 917 | |
| 918 | /* if decimal point is present */ |
| 919 | if( *z=='.' ){ |
| 920 | z++; |
| 921 | if( sqlite3Isdigit(z[0]) ){ |
| 922 | mState |= 1; |
| 923 | do{ |
| 924 | if( s<(LARGEST_UINT64-9)/10 ){ |
| 925 | s = s*10 + z[0] - '0'; |
| 926 | d--; |
| 927 | }else{ |
| 928 | mState = 11; |
| 929 | } |
| 930 | }while( sqlite3Isdigit(*++z) ); |
| 931 | }else if( mState==0 ){ |
| 932 | *pResult = 0.0; |
| 933 | return 0; |
| 934 | } |
| 935 | mState |= 2; |
| 936 | }else if( mState==0 ){ |
| 937 | if( sqlite3StrICmp((const char*)z,"Inf")==0 ){ |
| 938 | *pResult = neg ? -INFINITY : INFINITY; |
| 939 | return 0x13; |
| 940 | }else{ |
| 941 | *pResult = 0.0; |
| 942 | return 0; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /* if exponent is present */ |
| 947 | if( *z=='e' || *z=='E' ){ |
| 948 | int esign; |
| 949 | z++; |
| 950 | |
| 951 | /* get sign of exponent */ |
| 952 | if( *z=='-' ){ |
| 953 | esign = -1; |
| 954 | z++; |
| 955 | }else{ |
| 956 | esign = +1; |
| 957 | if( *z=='+' ){ |
| 958 | z++; |
| 959 | } |
| 960 | } |
| 961 | /* copy digits to exponent */ |
| 962 | if( (v = (unsigned)z[0] - '0')<10 ){ |
| 963 | u64 exp = v; |
| 964 | z++; |
| 965 | mState |= 2; |
| 966 | while( (v = (unsigned)z[0] - '0')<10 ){ |
| 967 | exp = exp*10 + v + (exp & (0xffULL<<32)); |
| 968 | z++; |
| 969 | } |
| 970 | d += esign*(exp & ((1ULL<<48)-1)); |
| 971 | }else{ |
| 972 | z--; /* Leave z[0] at 'e' or '+' or '-', |
| 973 | ** so that the return is 0 or -1 */ |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /* Convert s*pow(10,d) into real */ |
| 978 | if( s==0 ){ |
| 979 | *pResult = 0.0; |
| 980 | mState |= 4; |
| 981 | }else{ |
| 982 | *pResult = sqlite3Fp10Convert2(s,d); |
| 983 | } |
| 984 | if( neg ) *pResult = -*pResult; |
| 985 | assert( !sqlite3IsNaN(*pResult) ); |
| 986 | |
| 987 | /* return true if number and no extra non-whitespace characters after */ |
| 988 | if( z[0]==0 ){ |
| 989 | return mState; |
| 990 | } |
| 991 | if( sqlite3Isspace(z[0]) ){ |
| 992 | do{ z++; }while( sqlite3Isspace(*z) ); |
| 993 | if( z[0]==0 ){ |
| 994 | return mState; |
| 995 | } |
| 996 | } |
| 997 | return 0xfffffff0 | mState; |
| 998 | #else |
| 999 | return sqlite3Atoi64(zIn, pResult, strlen(zIn), SQLITE_UTF8)==0; |
| 1000 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | ** Digit pairs used to convert a U64 or I64 into text, two digits |
| 1005 | ** at a time. |
| 1006 | */ |
| 1007 | static const union { |
| 1008 | char a[201]; |
| 1009 | short int forceAlignment; |
| 1010 | } sqlite3DigitPairs = { |
| 1011 | "00010203040506070809" |
| 1012 | "10111213141516171819" |
| 1013 | "20212223242526272829" |
| 1014 | "30313233343536373839" |
| 1015 | "40414243444546474849" |
| 1016 | "50515253545556575859" |
| 1017 | "60616263646566676869" |
| 1018 | "70717273747576777879" |
| 1019 | "80818283848586878889" |
| 1020 | "90919293949596979899" |
| 1021 | }; |
| 1022 | |
| 1023 | /* |
| 1024 | ** ARMv6, ARMv7, PPC32 are known to not support hardware u64 division. |
| 1025 | */ |
| 1026 | #if (defined(__arm__) && !defined(__aarch64__)) || \ |
| 1027 | (defined(__ppc__) && !defined(__ppc64__)) |
| 1028 | # define SQLITE_AVOID_U64_DIVIDE 1 |
| 1029 | #endif |
| 1030 | |
| 1031 | #ifdef SQLITE_AVOID_U64_DIVIDE |
| 1032 | /* |
| 1033 | ** Render an unsigned 64-bit integer as text onto the end of a 2-byte |
| 1034 | ** aligned buffer that is SQLITE_U64_DIGIT+1 bytes long. The last byte |
| 1035 | ** of the buffer will be filled with a \000 byte. |
| 1036 | ** |
| 1037 | ** Return the index into the buffer of the first byte. |
| 1038 | ** |
| 1039 | ** This routine is used on platforms where u64-division is slow because |
| 1040 | ** it is not available in hardware and has to be emulated in software. |
| 1041 | ** It seeks to minimize the number of u64 divisions and use u32 divisions |
| 1042 | ** instead. It is slower on platforms that have hardware u64 division, |
| 1043 | ** but much faster on platforms that do not. |
| 1044 | */ |
| 1045 | static int sqlite3UInt64ToText(u64 v, char *zOut){ |
| 1046 | u32 x32, kk; |
| 1047 | int i; |
| 1048 | zOut[SQLITE_U64_DIGITS] = 0; |
| 1049 | i = SQLITE_U64_DIGITS; |
| 1050 | assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[0]) ); |
| 1051 | assert( TWO_BYTE_ALIGNMENT(zOut) ); |
| 1052 | while( (v>>32)!=0 ){ |
| 1053 | u32 y, x0, x1, y0, y1; |
| 1054 | x32 = v % 100000000; |
| 1055 | v = v / 100000000; |
| 1056 | y = x32 % 10000; |
| 1057 | x32 /= 10000; |
| 1058 | x1 = x32 / 100; |
| 1059 | x0 = x32 % 100; |
| 1060 | y1 = y / 100; |
| 1061 | y0 = y % 100; |
| 1062 | assert( i>=8 ); |
| 1063 | i -= 8; |
| 1064 | *(u16*)(&zOut[i]) = *(u16*)&sqlite3DigitPairs.a[x1*2]; |
| 1065 | *(u16*)(&zOut[i+2]) = *(u16*)&sqlite3DigitPairs.a[x0*2]; |
| 1066 | *(u16*)(&zOut[i+4]) = *(u16*)&sqlite3DigitPairs.a[y1*2]; |
| 1067 | *(u16*)(&zOut[i+6]) = *(u16*)&sqlite3DigitPairs.a[y0*2]; |
| 1068 | } |
| 1069 | x32 = v; |
| 1070 | while( x32>=10 ){ |
| 1071 | kk = x32 % 100; |
| 1072 | x32 = x32 / 100; |
| 1073 | assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk*2]) ); |
| 1074 | assert( i>=2 ); |
| 1075 | i -= 2; |
| 1076 | assert( TWO_BYTE_ALIGNMENT(&zOut[i]) ); |
| 1077 | *(u16*)(&zOut[i]) = *(u16*)&sqlite3DigitPairs.a[kk*2]; |
| 1078 | } |
| 1079 | if( x32 ){ |
| 1080 | assert( i>0 ); |
| 1081 | zOut[--i] = x32 + '0'; |
| 1082 | } |
| 1083 | return i; |
| 1084 | } |
| 1085 | #endif /* defined(SQLITE_AVOID_U64_DIVIDE) */ |
| 1086 | |
| 1087 | /* |
| 1088 | ** Render an signed 64-bit integer as text. Store the result in zOut[] and |
| 1089 | ** return the length of the string that was stored, in bytes. The value |
| 1090 | ** returned does not include the zero terminator at the end of the output |
| 1091 | ** string. |
| 1092 | ** |
| 1093 | ** The caller must ensure that zOut[] is at least 21 bytes in size. |
| 1094 | */ |
| 1095 | int sqlite3Int64ToText(i64 v, char *zOut){ |
| 1096 | int i; |
| 1097 | u64 x; |
| 1098 | union { |
| 1099 | char a[SQLITE_U64_DIGITS+1]; |
| 1100 | u16 forceAlignment; |
| 1101 | } u; |
| 1102 | if( v>0 ){ |
| 1103 | x = v; |
| 1104 | }else if( v==0 ){ |
| 1105 | zOut[0] = '0'; |
| 1106 | zOut[1] = 0; |
| 1107 | return 1; |
| 1108 | }else{ |
| 1109 | x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; |
| 1110 | } |
| 1111 | #ifdef SQLITE_AVOID_U64_DIVIDE |
| 1112 | i = sqlite3UInt64ToText(x, u.a); |
| 1113 | #else |
| 1114 | i = sizeof(u.a)-1; |
| 1115 | u.a[i] = 0; |
| 1116 | while( x>=10 ){ |
| 1117 | int kk = (x%100)*2; |
| 1118 | assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| 1119 | assert( TWO_BYTE_ALIGNMENT(&u.a[i-2]) ); |
| 1120 | *(u16*)(&u.a[i-2]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| 1121 | i -= 2; |
| 1122 | x /= 100; |
| 1123 | } |
| 1124 | if( x ){ |
| 1125 | u.a[--i] = x + '0'; |
| 1126 | } |
| 1127 | #endif /* SQLITE_AVOID_U64_DIVIDE */ |
| 1128 | if( v<0 ) u.a[--i] = '-'; |
| 1129 | memcpy(zOut, &u.a[i], sizeof(u.a)-i); |
| 1130 | return sizeof(u.a)-1-i; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | ** Compare the 19-character string zNum against the text representation |
| 1135 | ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| 1136 | ** if zNum is less than, equal to, or greater than the string. |
| 1137 | ** Note that zNum must contain exactly 19 characters. |
| 1138 | ** |
| 1139 | ** Unlike memcmp() this routine is guaranteed to return the difference |
| 1140 | ** in the values of the last digit if the only difference is in the |
| 1141 | ** last digit. So, for example, |
| 1142 | ** |
| 1143 | ** compare2pow63("9223372036854775800", 1) |
| 1144 | ** |
| 1145 | ** will return -8. |
| 1146 | */ |
| 1147 | static int compare2pow63(const char *zNum, int incr){ |
| 1148 | int c = 0; |
| 1149 | int i; |
| 1150 | /* 012345678901234567 */ |
| 1151 | const char *pow63 = "922337203685477580"; |
| 1152 | for(i=0; c==0 && i<18; i++){ |
| 1153 | c = (zNum[i*incr]-pow63[i])*10; |
| 1154 | } |
| 1155 | if( c==0 ){ |
| 1156 | c = zNum[18*incr] - '8'; |
| 1157 | testcase( c==(-1) ); |
| 1158 | testcase( c==0 ); |
| 1159 | testcase( c==(+1) ); |
| 1160 | } |
| 1161 | return c; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This |
| 1166 | ** routine does *not* accept hexadecimal notation. |
| 1167 | ** |
| 1168 | ** Returns: |
| 1169 | ** |
| 1170 | ** -1 Not even a prefix of the input text looks like an integer |
| 1171 | ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| 1172 | ** 1 Excess non-space text after the integer value |
| 1173 | ** 2 Integer too large for a 64-bit signed integer or is malformed |
| 1174 | ** 3 Special case of 9223372036854775808 |
| 1175 | ** |
| 1176 | ** length is the number of bytes in the string (bytes, not characters). |
| 1177 | ** The string is not necessarily zero-terminated. The encoding is |
| 1178 | ** given by enc. |
| 1179 | */ |
| 1180 | int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
| 1181 | int incr; |
| 1182 | u64 u = 0; |
| 1183 | int neg = 0; /* assume positive */ |
| 1184 | int i, j; |
| 1185 | unsigned int c = 0; |
| 1186 | int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ |
| 1187 | int rc; /* Baseline return code */ |
| 1188 | const char *zStart; |
| 1189 | const char *zEnd = zNum + length; |
| 1190 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 1191 | if( enc==SQLITE_UTF8 ){ |
| 1192 | incr = 1; |
| 1193 | }else{ |
| 1194 | incr = 2; |
| 1195 | length &= ~1; |
| 1196 | assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 1197 | for(i=3-enc; i<length && zNum[i]==0; i+=2){} |
| 1198 | nonNum = i<length; |
| 1199 | zEnd = &zNum[i^1]; |
| 1200 | zNum += (enc&1); |
| 1201 | } |
| 1202 | while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
| 1203 | if( zNum<zEnd ){ |
| 1204 | if( *zNum=='-' ){ |
| 1205 | neg = 1; |
| 1206 | zNum+=incr; |
| 1207 | }else if( *zNum=='+' ){ |
| 1208 | zNum+=incr; |
| 1209 | } |
| 1210 | } |
| 1211 | zStart = zNum; |
| 1212 | while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
| 1213 | for(i=0; &zNum[i]<zEnd && (c=(unsigned)zNum[i]-'0')<=9; i+=incr){ |
| 1214 | u = u*10 + c; |
| 1215 | } |
| 1216 | testcase( i==18*incr ); |
| 1217 | testcase( i==19*incr ); |
| 1218 | testcase( i==20*incr ); |
| 1219 | if( u>LARGEST_INT64 ){ |
| 1220 | /* This test and assignment is needed only to suppress UB warnings |
| 1221 | ** from clang and -fsanitize=undefined. This test and assignment make |
| 1222 | ** the code a little larger and slower, and no harm comes from omitting |
| 1223 | ** them, but we must appease the undefined-behavior pharisees. */ |
| 1224 | *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| 1225 | }else if( neg ){ |
| 1226 | *pNum = -(i64)u; |
| 1227 | }else{ |
| 1228 | *pNum = (i64)u; |
| 1229 | } |
| 1230 | rc = 0; |
| 1231 | if( i==0 && zStart==zNum ){ /* No digits */ |
| 1232 | rc = -1; |
| 1233 | }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ |
| 1234 | rc = 1; |
| 1235 | }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */ |
| 1236 | int jj = i; |
| 1237 | do{ |
| 1238 | if( !sqlite3Isspace(zNum[jj]) ){ |
| 1239 | rc = 1; /* Extra non-space text after the integer */ |
| 1240 | break; |
| 1241 | } |
| 1242 | jj += incr; |
| 1243 | }while( &zNum[jj]<zEnd ); |
| 1244 | } |
| 1245 | if( i<19*incr ){ |
| 1246 | /* Less than 19 digits, so we know that it fits in 64 bits */ |
| 1247 | assert( u<=LARGEST_INT64 ); |
| 1248 | return rc; |
| 1249 | }else{ |
| 1250 | /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
| 1251 | j = i>19*incr ? 1 : compare2pow63(zNum, incr); |
| 1252 | if( j<0 ){ |
| 1253 | /* zNum is less than 9223372036854775808 so it fits */ |
| 1254 | assert( u<=LARGEST_INT64 ); |
| 1255 | return rc; |
| 1256 | }else{ |
| 1257 | *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| 1258 | if( j>0 ){ |
| 1259 | /* zNum is greater than 9223372036854775808 so it overflows */ |
| 1260 | return 2; |
| 1261 | }else{ |
| 1262 | /* zNum is exactly 9223372036854775808. Fits if negative. The |
| 1263 | ** special case 2 overflow if positive */ |
| 1264 | assert( u-1==LARGEST_INT64 ); |
| 1265 | return neg ? rc : 3; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, |
| 1273 | ** into a 64-bit signed integer. This routine accepts hexadecimal literals, |
| 1274 | ** whereas sqlite3Atoi64() does not. |
| 1275 | ** |
| 1276 | ** Returns: |
| 1277 | ** |
| 1278 | ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| 1279 | ** 1 Excess text after the integer value |
| 1280 | ** 2 Integer too large for a 64-bit signed integer or is malformed |
| 1281 | ** 3 Special case of 9223372036854775808 |
| 1282 | */ |
| 1283 | int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ |
| 1284 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 1285 | if( z[0]=='0' |
| 1286 | && (z[1]=='x' || z[1]=='X') |
| 1287 | ){ |
| 1288 | u64 u = 0; |
| 1289 | int i, k; |
| 1290 | for(i=2; z[i]=='0'; i++){} |
| 1291 | for(k=i; sqlite3Isxdigit(z[k]); k++){ |
| 1292 | u = u*16 + sqlite3HexToInt(z[k]); |
| 1293 | } |
| 1294 | memcpy(pOut, &u, 8); |
| 1295 | if( k-i>16 ) return 2; |
| 1296 | if( z[k]!=0 ) return 1; |
| 1297 | return 0; |
| 1298 | }else |
| 1299 | #endif /* SQLITE_OMIT_HEX_INTEGER */ |
| 1300 | { |
| 1301 | int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); |
| 1302 | if( z[n] ) n++; |
| 1303 | return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 1309 | ** *pValue to that integer and return true. Otherwise return false. |
| 1310 | ** |
| 1311 | ** This routine accepts both decimal and hexadecimal notation for integers. |
| 1312 | ** |
| 1313 | ** Any non-numeric characters that following zNum are ignored. |
| 1314 | ** This is different from sqlite3Atoi64() which requires the |
| 1315 | ** input number to be zero-terminated. |
| 1316 | */ |
| 1317 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 1318 | sqlite_int64 v = 0; |
| 1319 | int i, c; |
| 1320 | int neg = 0; |
| 1321 | if( zNum[0]=='-' ){ |
| 1322 | neg = 1; |
| 1323 | zNum++; |
| 1324 | }else if( zNum[0]=='+' ){ |
| 1325 | zNum++; |
| 1326 | } |
| 1327 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 1328 | else if( zNum[0]=='0' |
| 1329 | && (zNum[1]=='x' || zNum[1]=='X') |
| 1330 | && sqlite3Isxdigit(zNum[2]) |
| 1331 | ){ |
| 1332 | u32 u = 0; |
| 1333 | zNum += 2; |
| 1334 | while( zNum[0]=='0' ) zNum++; |
| 1335 | for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){ |
| 1336 | u = u*16 + sqlite3HexToInt(zNum[i]); |
| 1337 | } |
| 1338 | if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ |
| 1339 | memcpy(pValue, &u, 4); |
| 1340 | return 1; |
| 1341 | }else{ |
| 1342 | return 0; |
| 1343 | } |
| 1344 | } |
| 1345 | #endif |
| 1346 | if( !sqlite3Isdigit(zNum[0]) ) return 0; |
| 1347 | while( zNum[0]=='0' ) zNum++; |
| 1348 | for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
| 1349 | v = v*10 + c; |
| 1350 | } |
| 1351 | |
| 1352 | /* The longest decimal representation of a 32 bit integer is 10 digits: |
| 1353 | ** |
| 1354 | ** 1234567890 |
| 1355 | ** 2^31 -> 2147483648 |
| 1356 | */ |
| 1357 | testcase( i==10 ); |
| 1358 | if( i>10 ){ |
| 1359 | return 0; |
| 1360 | } |
| 1361 | testcase( v-neg==2147483647 ); |
| 1362 | if( v-neg>2147483647 ){ |
| 1363 | return 0; |
| 1364 | } |
| 1365 | if( neg ){ |
| 1366 | v = -v; |
| 1367 | } |
| 1368 | *pValue = (int)v; |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | ** Return a 32-bit integer value extracted from a string. If the |
| 1374 | ** string is not an integer, just return 0. |
| 1375 | */ |
| 1376 | int sqlite3Atoi(const char *z){ |
| 1377 | int x = 0; |
| 1378 | sqlite3GetInt32(z, &x); |
| 1379 | return x; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | ** Decode a floating-point value into an approximate decimal |
| 1384 | ** representation. |
| 1385 | ** |
| 1386 | ** If iRound<=0 then round to -iRound significant digits to the |
| 1387 | ** the right of the decimal point, or to a maximum of mxRound total |
| 1388 | ** significant digits. |
| 1389 | ** |
| 1390 | ** If iRound>0 round to min(iRound,mxRound) significant digits total. |
| 1391 | ** |
| 1392 | ** mxRound must be positive. iRound must not be less than -100000000 |
| 1393 | ** (the SQLITE_FP_PRECISION_LIMIT maximum in printf.c) so that the |
| 1394 | ** computation of the number of significant digits cannot overflow. |
| 1395 | ** |
| 1396 | ** The significant digits of the decimal representation are |
| 1397 | ** stored in p->z[] which is a often (but not always) a pointer |
| 1398 | ** into the middle of p->zBuf[]. There are p->n significant digits. |
| 1399 | ** The p->z[] array is *not* zero-terminated. |
| 1400 | */ |
| 1401 | void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ |
| 1402 | int i; /* Index into zBuf[] where to put next character */ |
| 1403 | int n; /* Number of digits */ |
| 1404 | u64 v; /* mantissa */ |
| 1405 | int e, exp = 0; /* Base-2 and base-10 exponent */ |
| 1406 | char *zBuf; /* Local alias for p->zBuf */ |
| 1407 | char *z; /* Local alias for p->z */ |
| 1408 | |
| 1409 | p->isSpecial = 0; |
| 1410 | assert( mxRound>0 ); |
| 1411 | assert( iRound>=-100000000 ); |
| 1412 | |
| 1413 | /* Convert negative numbers to positive. Deal with Infinity, 0.0, and |
| 1414 | ** NaN. */ |
| 1415 | if( r<0.0 ){ |
| 1416 | p->sign = '-'; |
| 1417 | r = -r; |
| 1418 | }else if( r==0.0 ){ |
| 1419 | p->sign = '+'; |
| 1420 | p->n = 1; |
| 1421 | p->iDP = 1; |
| 1422 | p->z = "0"; |
| 1423 | return; |
| 1424 | }else{ |
| 1425 | p->sign = '+'; |
| 1426 | } |
| 1427 | memcpy(&v,&r,8); |
| 1428 | e = (v>>52)&0x7ff; |
| 1429 | if( e==0x7ff ){ |
| 1430 | p->isSpecial = 1 + (v!=0x7ff0000000000000LL); |
| 1431 | p->n = 0; |
| 1432 | p->iDP = 0; |
| 1433 | p->z = p->zBuf; |
| 1434 | return; |
| 1435 | } |
| 1436 | v &= 0x000fffffffffffffULL; |
| 1437 | if( e==0 ){ |
| 1438 | int nn = countLeadingZeros(v); |
| 1439 | v <<= nn; |
| 1440 | e = -1074 - nn; |
| 1441 | }else{ |
| 1442 | v = (v<<11) | U64_BIT(63); |
| 1443 | e -= 1086; |
| 1444 | } |
| 1445 | sqlite3Fp2Convert10(v, e, (iRound<=0||iRound>=18)?18:iRound+1, &v, &exp); |
| 1446 | |
| 1447 | /* Extract significant digits, start at the right-most slot in p->zBuf |
| 1448 | ** and working back to the right. "i" keeps track of the next slot in |
| 1449 | ** which to store a digit. */ |
| 1450 | assert( sizeof(p->zBuf)==SQLITE_U64_DIGITS+1 ); |
| 1451 | assert( v>0 ); |
| 1452 | zBuf = p->zBuf; |
| 1453 | #ifdef SQLITE_AVOID_U64_DIVIDE |
| 1454 | i = sqlite3UInt64ToText(v, zBuf); |
| 1455 | #else |
| 1456 | i = SQLITE_U64_DIGITS; |
| 1457 | while( v>=10 ){ |
| 1458 | int kk = (v%100)*2; |
| 1459 | assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| 1460 | assert( TWO_BYTE_ALIGNMENT(&zBuf[i]) ); |
| 1461 | assert( i-2>=0 ); |
| 1462 | *(u16*)(&zBuf[i-2]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| 1463 | i -= 2; |
| 1464 | v /= 100; |
| 1465 | } |
| 1466 | if( v ){ |
| 1467 | assert( v<10 ); |
| 1468 | assert( i>0 ); |
| 1469 | zBuf[--i] = v + '0'; |
| 1470 | } |
| 1471 | #endif /* SQLITE_AVOID_U64_DIVIDE */ |
| 1472 | assert( i>=0 && i<SQLITE_U64_DIGITS ); |
| 1473 | n = SQLITE_U64_DIGITS - i; /* Total number of digits extracted */ |
| 1474 | assert( n>0 ); |
| 1475 | assert( n<=SQLITE_U64_DIGITS ); |
| 1476 | p->iDP = n + exp; |
| 1477 | if( iRound<=0 ){ |
| 1478 | iRound = p->iDP - iRound; |
| 1479 | if( iRound==0 && zBuf[i]>='5' ){ |
| 1480 | iRound = 1; |
| 1481 | zBuf[--i] = '0'; |
| 1482 | n++; |
| 1483 | p->iDP++; |
| 1484 | } |
| 1485 | } |
| 1486 | z = &zBuf[i]; /* z points to the first digit */ |
| 1487 | if( iRound>0 && (iRound<n || n>mxRound) ){ |
| 1488 | if( iRound>mxRound ) iRound = mxRound; |
| 1489 | if( iRound==17 ){ |
| 1490 | /* If the precision is exactly 17, which only happens with the "!" |
| 1491 | ** flag (ex: "%!.17g") then try to reduce the precision if that |
| 1492 | ** yields text that will round-trip to the original floating-point. |
| 1493 | ** value. Thus, for exaple, 49.47 will render as 49.47, rather than |
| 1494 | ** as 49.469999999999999. */ |
| 1495 | if( z[15]=='9' && z[14]=='9' ){ |
| 1496 | int jj, kk; |
| 1497 | u64 v2; |
| 1498 | for(jj=14; jj>0 && z[jj-1]=='9'; jj--){} |
| 1499 | if( jj==0 ){ |
| 1500 | v2 = 1; |
| 1501 | }else{ |
| 1502 | v2 = z[0] - '0'; |
| 1503 | for(kk=1; kk<jj; kk++) v2 = (v2*10) + z[kk] - '0'; |
| 1504 | v2++; |
| 1505 | } |
| 1506 | if( r==sqlite3Fp10Convert2(v2, exp + n - jj) ){ |
| 1507 | iRound = jj+1; |
| 1508 | } |
| 1509 | }else if( p->iDP>=n || (z[15]=='0' && z[14]=='0' && z[13]=='0') ){ |
| 1510 | int jj, kk; |
| 1511 | u64 v2; |
| 1512 | assert( z[0]!='0' ); |
| 1513 | for(jj=13; z[jj-1]=='0'; jj--){} |
| 1514 | v2 = z[0] - '0'; |
| 1515 | for(kk=1; kk<jj; kk++) v2 = (v2*10) + z[kk] - '0'; |
| 1516 | if( r==sqlite3Fp10Convert2(v2, exp + n - jj) ){ |
| 1517 | iRound = jj+1; |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | n = iRound; |
| 1522 | if( z[iRound]>='5' ){ |
| 1523 | int j = iRound-1; |
| 1524 | while( 1 /*exit-by-break*/ ){ |
| 1525 | z[j]++; |
| 1526 | if( z[j]<='9' ) break; |
| 1527 | z[j] = '0'; |
| 1528 | if( j==0 ){ |
| 1529 | z--; |
| 1530 | z[0] = '1'; |
| 1531 | n++; |
| 1532 | p->iDP++; |
| 1533 | break; |
| 1534 | }else{ |
| 1535 | j--; |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | assert( n>0 ); |
| 1541 | while( z[n-1]=='0' ){ |
| 1542 | n--; |
| 1543 | assert( n>0 ); |
| 1544 | } |
| 1545 | p->n = n; |
| 1546 | p->z = z; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | ** Try to convert z into an unsigned 32-bit integer. Return true on |
| 1551 | ** success and false if there is an error. |
| 1552 | ** |
| 1553 | ** Only decimal notation is accepted. |
| 1554 | */ |
| 1555 | int sqlite3GetUInt32(const char *z, u32 *pI){ |
| 1556 | u64 v = 0; |
| 1557 | int i; |
| 1558 | for(i=0; sqlite3Isdigit(z[i]); i++){ |
| 1559 | v = v*10 + z[i] - '0'; |
| 1560 | if( v>4294967295LL ){ *pI = 0; return 0; } |
| 1561 | } |
| 1562 | if( i==0 || z[i]!=0 ){ *pI = 0; return 0; } |
| 1563 | *pI = (u32)v; |
| 1564 | return 1; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | ** The variable-length integer encoding is as follows: |
| 1569 | ** |
| 1570 | ** KEY: |
| 1571 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 1572 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 1573 | ** C = xxxxxxxx 8 bits of data |
| 1574 | ** |
| 1575 | ** 7 bits - A |
| 1576 | ** 14 bits - BA |
| 1577 | ** 21 bits - BBA |
| 1578 | ** 28 bits - BBBA |
| 1579 | ** 35 bits - BBBBA |
| 1580 | ** 42 bits - BBBBBA |
| 1581 | ** 49 bits - BBBBBBA |
| 1582 | ** 56 bits - BBBBBBBA |
| 1583 | ** 64 bits - BBBBBBBBC |
| 1584 | */ |
| 1585 | |
| 1586 | /* |
| 1587 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 1588 | ** The length of data write will be between 1 and 9 bytes. The number |
| 1589 | ** of bytes written is returned. |
| 1590 | ** |
| 1591 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 1592 | ** for all bytes that have the 8th bit set and one byte with the 8th |
| 1593 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 1594 | ** 8 bits and is the last byte. |
| 1595 | */ |
| 1596 | static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ |
| 1597 | int i, j, n; |
| 1598 | u8 buf[10]; |
| 1599 | if( v & (((u64)0xff000000)<<32) ){ |
| 1600 | p[8] = (u8)v; |
| 1601 | v >>= 8; |
| 1602 | for(i=7; i>=0; i--){ |
| 1603 | p[i] = (u8)((v & 0x7f) | 0x80); |
| 1604 | v >>= 7; |
| 1605 | } |
| 1606 | return 9; |
| 1607 | } |
| 1608 | n = 0; |
| 1609 | do{ |
| 1610 | buf[n++] = (u8)((v & 0x7f) | 0x80); |
| 1611 | v >>= 7; |
| 1612 | }while( v!=0 ); |
| 1613 | buf[0] &= 0x7f; |
| 1614 | assert( n<=9 ); |
| 1615 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 1616 | p[i] = buf[j]; |
| 1617 | } |
| 1618 | return n; |
| 1619 | } |
| 1620 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
| 1621 | if( v<=0x7f ){ |
| 1622 | p[0] = v&0x7f; |
| 1623 | return 1; |
| 1624 | } |
| 1625 | if( v<=0x3fff ){ |
| 1626 | p[0] = ((v>>7)&0x7f)|0x80; |
| 1627 | p[1] = v&0x7f; |
| 1628 | return 2; |
| 1629 | } |
| 1630 | return putVarint64(p,v); |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 1635 | ** Return the number of bytes read. The value is stored in *v. |
| 1636 | */ |
| 1637 | u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ |
| 1638 | u64 iKey = p[0]; |
| 1639 | const u8 *pStart = p; |
| 1640 | if( iKey>=0x80 ){ |
| 1641 | u8 x; |
| 1642 | iKey = (iKey<<7) ^ (x = *++p); |
| 1643 | if( x>=0x80 ){ |
| 1644 | iKey = (iKey<<7) ^ (x = *++p); |
| 1645 | if( x>=0x80 ){ |
| 1646 | iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++p); |
| 1647 | if( x>=0x80 ){ |
| 1648 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1649 | if( x>=0x80 ){ |
| 1650 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1651 | if( x>=0x80 ){ |
| 1652 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1653 | if( x>=0x80 ){ |
| 1654 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1655 | if( x>=0x80 ){ |
| 1656 | iKey = (iKey<<8) ^ 0x8000 ^ (*++p); |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | }else{ |
| 1663 | iKey ^= 0x204000; |
| 1664 | } |
| 1665 | }else{ |
| 1666 | iKey ^= 0x4000; |
| 1667 | } |
| 1668 | } |
| 1669 | *v = iKey; |
| 1670 | return (u8)(p - pStart) + 1; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | ** Return the value of a variable-length integer without computing |
| 1675 | ** its length. This is an optimization on sqlite3GetVarint() for the |
| 1676 | ** cases when the return value of sqlite3GetVarint() is not needed. |
| 1677 | */ |
| 1678 | i64 sqlite3VarintValue(const unsigned char *p){ |
| 1679 | u64 iKey = p[0]; |
| 1680 | if( iKey>=0x80 ){ |
| 1681 | u8 x; |
| 1682 | iKey = (iKey<<7) ^ (x = *++p); |
| 1683 | if( x>=0x80 ){ |
| 1684 | iKey = (iKey<<7) ^ (x = *++p); |
| 1685 | if( x>=0x80 ){ |
| 1686 | iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++p); |
| 1687 | if( x>=0x80 ){ |
| 1688 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1689 | if( x>=0x80 ){ |
| 1690 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1691 | if( x>=0x80 ){ |
| 1692 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1693 | if( x>=0x80 ){ |
| 1694 | iKey = (iKey<<7) ^ 0x4000 ^ (x = *++p); |
| 1695 | if( x>=0x80 ){ |
| 1696 | iKey = (iKey<<8) ^ 0x8000 ^ (*++p); |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | }else{ |
| 1703 | iKey ^= 0x204000; |
| 1704 | } |
| 1705 | }else{ |
| 1706 | iKey ^= 0x4000; |
| 1707 | } |
| 1708 | } |
| 1709 | return *(i64*)&iKey; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 1714 | ** Return the number of bytes read. The value is stored in *v. |
| 1715 | ** |
| 1716 | ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned |
| 1717 | ** integer, then set *v to 0xffffffff. |
| 1718 | ** |
| 1719 | ** A MACRO version, getVarint32, is provided which inlines the |
| 1720 | ** single-byte case. All code should use the MACRO version as |
| 1721 | ** this function assumes the single-byte case has already been handled. |
| 1722 | */ |
| 1723 | u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
| 1724 | u64 v64; |
| 1725 | u8 n; |
| 1726 | |
| 1727 | /* Assume that the single-byte case has already been handled by |
| 1728 | ** the getVarint32() macro */ |
| 1729 | assert( (p[0] & 0x80)!=0 ); |
| 1730 | |
| 1731 | if( (p[1] & 0x80)==0 ){ |
| 1732 | /* This is the two-byte case */ |
| 1733 | *v = ((p[0]&0x7f)<<7) | p[1]; |
| 1734 | return 2; |
| 1735 | } |
| 1736 | if( (p[2] & 0x80)==0 ){ |
| 1737 | /* This is the three-byte case */ |
| 1738 | *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2]; |
| 1739 | return 3; |
| 1740 | } |
| 1741 | /* four or more bytes */ |
| 1742 | n = sqlite3GetVarint(p, &v64); |
| 1743 | assert( n>3 && n<=9 ); |
| 1744 | if( (v64 & SQLITE_MAX_U32)!=v64 ){ |
| 1745 | *v = 0xffffffff; |
| 1746 | }else{ |
| 1747 | *v = (u32)v64; |
| 1748 | } |
| 1749 | return n; |
| 1750 | } |
| 1751 | |
| 1752 | /* |
| 1753 | ** Return the number of bytes that will be needed to store the given |
| 1754 | ** 64-bit integer. |
| 1755 | */ |
| 1756 | int sqlite3VarintLen(u64 v){ |
| 1757 | int i; |
| 1758 | for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } |
| 1759 | return i; |
| 1760 | } |
| 1761 | |
| 1762 | |
| 1763 | /* |
| 1764 | ** Read an unsigned 32-bit integer from an unaligned big-endian array of bytes. |
| 1765 | */ |
| 1766 | u32 sqlite3Get4byte(const u8 *p){ |
| 1767 | #if SQLITE_BYTEORDER==4321 |
| 1768 | u32 x; |
| 1769 | memcpy(&x,p,4); |
| 1770 | return x; |
| 1771 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 1772 | u32 x; |
| 1773 | memcpy(&x,p,4); |
| 1774 | return __builtin_bswap32(x); |
| 1775 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 1776 | u32 x; |
| 1777 | memcpy(&x,p,4); |
| 1778 | return _byteswap_ulong(x); |
| 1779 | #else |
| 1780 | /* Test this limb using -DSQLITE_BYTEORDER=0 */ |
| 1781 | testcase( p[0]&0x80 ); |
| 1782 | return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 1783 | #endif |
| 1784 | } |
| 1785 | |
| 1786 | /* Write an unsigned 32-bit integer into an unaligned big-endian array of bytes. |
| 1787 | */ |
| 1788 | void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 1789 | #if SQLITE_BYTEORDER==4321 |
| 1790 | memcpy(p,&v,4); |
| 1791 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 1792 | u32 x = __builtin_bswap32(v); |
| 1793 | memcpy(p,&x,4); |
| 1794 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 1795 | u32 x = _byteswap_ulong(v); |
| 1796 | memcpy(p,&x,4); |
| 1797 | #else |
| 1798 | /* Test this limb using -DSQLITE_BYTEORDER=0 */ |
| 1799 | p[0] = (u8)(v>>24); |
| 1800 | p[1] = (u8)(v>>16); |
| 1801 | p[2] = (u8)(v>>8); |
| 1802 | p[3] = (u8)v; |
| 1803 | #endif |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | ** Read an unsigned 64-bit integer from an unaligned big-endian byte array. |
| 1808 | */ |
| 1809 | SQLITE_OPT_INLINE u64 sqlite3Get8byte(const u8 *p){ |
| 1810 | #if SQLITE_BYTEORDER==4321 |
| 1811 | u64 x; |
| 1812 | memcpy(&x,p,8); |
| 1813 | return x; |
| 1814 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 1815 | u64 x; |
| 1816 | memcpy(&x,p,8); |
| 1817 | return __builtin_bswap64(x); |
| 1818 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 1819 | u64 x; |
| 1820 | memcpy(&x,p,8); |
| 1821 | return _byteswap_uint64(x); |
| 1822 | #else |
| 1823 | /* Test this limb using -DSQLITE_BYTEORDER=0 */ |
| 1824 | testcase( p[0]&0x80 ); |
| 1825 | return (u64)( |
| 1826 | (((u64)p[0]) << 56) + |
| 1827 | (((u64)p[1]) << 48) + |
| 1828 | (((u64)p[2]) << 40) + |
| 1829 | (((u64)p[3]) << 32) + |
| 1830 | (((u64)p[4]) << 24) + |
| 1831 | (((u64)p[5]) << 16) + |
| 1832 | (((u64)p[6]) << 8) + |
| 1833 | (((u64)p[7]) << 0) |
| 1834 | ); |
| 1835 | #endif |
| 1836 | } |
| 1837 | |
| 1838 | #if SQLITE_BYTEORDER!=4321 /* Only used for little-endian machines */ |
| 1839 | /* |
| 1840 | ** Byte-swap a 64-bit unsigned integer. |
| 1841 | */ |
| 1842 | SQLITE_OPT_INLINE u64 sqlite3BSwap64(u64 x){ |
| 1843 | #if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 1844 | return __builtin_bswap64(x); |
| 1845 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 1846 | return _byteswap_uint64(x); |
| 1847 | #else |
| 1848 | /* Test this limb using -DSQLITE_BYTEORDER=0 */ |
| 1849 | x = (x << 32) | (x >> 32); |
| 1850 | x = ((x & UINT64_C(0x0000ffff0000ffff)) << 16) | |
| 1851 | ((x & UINT64_C(0xffff0000ffff0000)) >> 16); |
| 1852 | x = ((x & UINT64_C(0x00ff00ff00ff00ff)) << 8) | |
| 1853 | ((x & UINT64_C(0xff00ff00ff00ff00)) >> 8); |
| 1854 | return x; |
| 1855 | #endif |
| 1856 | } |
| 1857 | #endif /* SQLITE_BYTEORDER!=4321 */ |
| 1858 | |
| 1859 | /* |
| 1860 | ** Translate a single byte of Hex into an integer. |
| 1861 | ** This routine only works if h really is a valid hexadecimal |
| 1862 | ** character: 0..9a..fA..F |
| 1863 | */ |
| 1864 | u8 sqlite3HexToInt(int h){ |
| 1865 | assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 1866 | #ifdef SQLITE_ASCII |
| 1867 | h += 9*(1&(h>>6)); |
| 1868 | #endif |
| 1869 | #ifdef SQLITE_EBCDIC |
| 1870 | h += 9*(1&~(h>>4)); |
| 1871 | #endif |
| 1872 | return (u8)(h & 0xf); |
| 1873 | } |
| 1874 | |
| 1875 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) |
| 1876 | /* |
| 1877 | ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 1878 | ** value. Return a pointer to its binary value. Space to hold the |
| 1879 | ** binary value has been obtained from malloc and must be freed by |
| 1880 | ** the calling routine. |
| 1881 | */ |
| 1882 | void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
| 1883 | char *zBlob; |
| 1884 | int i; |
| 1885 | |
| 1886 | zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); |
| 1887 | n--; |
| 1888 | if( zBlob ){ |
| 1889 | for(i=0; i<n; i+=2){ |
| 1890 | zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
| 1891 | } |
| 1892 | zBlob[i/2] = 0; |
| 1893 | } |
| 1894 | return zBlob; |
| 1895 | } |
| 1896 | #endif /* !SQLITE_OMIT_BLOB_LITERAL */ |
| 1897 | |
| 1898 | /* |
| 1899 | ** Log an error that is an API call on a connection pointer that should |
| 1900 | ** not have been used. The "type" of connection pointer is given as the |
| 1901 | ** argument. The zType is a word like "NULL" or "closed" or "invalid". |
| 1902 | */ |
| 1903 | static void logBadConnection(const char *zType){ |
| 1904 | sqlite3_log(SQLITE_MISUSE, |
| 1905 | "API call with %s database connection pointer", |
| 1906 | zType |
| 1907 | ); |
| 1908 | } |
| 1909 | |
| 1910 | /* |
| 1911 | ** Check to make sure we have a valid db pointer. This test is not |
| 1912 | ** foolproof but it does provide some measure of protection against |
| 1913 | ** misuse of the interface such as passing in db pointers that are |
| 1914 | ** NULL or which have been previously closed. If this routine returns |
| 1915 | ** 1 it means that the db pointer is valid and 0 if it should not be |
| 1916 | ** dereferenced for any reason. The calling function should invoke |
| 1917 | ** SQLITE_MISUSE immediately. |
| 1918 | ** |
| 1919 | ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| 1920 | ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 1921 | ** open properly and is not fit for general use but which can be |
| 1922 | ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| 1923 | */ |
| 1924 | int sqlite3SafetyCheckOk(sqlite3 *db){ |
| 1925 | u8 eOpenState; |
| 1926 | if( db==0 ){ |
| 1927 | logBadConnection("NULL"); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | eOpenState = db->eOpenState; |
| 1931 | if( eOpenState!=SQLITE_STATE_OPEN ){ |
| 1932 | if( sqlite3SafetyCheckSickOrOk(db) ){ |
| 1933 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 1934 | logBadConnection("unopened"); |
| 1935 | } |
| 1936 | return 0; |
| 1937 | }else{ |
| 1938 | return 1; |
| 1939 | } |
| 1940 | } |
| 1941 | int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
| 1942 | u8 eOpenState; |
| 1943 | eOpenState = db->eOpenState; |
| 1944 | if( eOpenState!=SQLITE_STATE_SICK && |
| 1945 | eOpenState!=SQLITE_STATE_OPEN && |
| 1946 | eOpenState!=SQLITE_STATE_BUSY ){ |
| 1947 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 1948 | logBadConnection("invalid"); |
| 1949 | return 0; |
| 1950 | }else{ |
| 1951 | return 1; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | ** Attempt to add, subtract, or multiply the 64-bit signed value iB against |
| 1957 | ** the other 64-bit signed integer at *pA and store the result in *pA. |
| 1958 | ** Return 0 on success. Or if the operation would have resulted in an |
| 1959 | ** overflow, leave *pA unchanged and return 1. |
| 1960 | */ |
| 1961 | int sqlite3AddInt64(i64 *pA, i64 iB){ |
| 1962 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
| 1963 | return __builtin_add_overflow(*pA, iB, pA); |
| 1964 | #else |
| 1965 | i64 iA = *pA; |
| 1966 | testcase( iA==0 ); testcase( iA==1 ); |
| 1967 | testcase( iB==-1 ); testcase( iB==0 ); |
| 1968 | if( iB>=0 ){ |
| 1969 | testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
| 1970 | testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
| 1971 | if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
| 1972 | }else{ |
| 1973 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
| 1974 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
| 1975 | if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
| 1976 | } |
| 1977 | *pA += iB; |
| 1978 | return 0; |
| 1979 | #endif |
| 1980 | } |
| 1981 | int sqlite3SubInt64(i64 *pA, i64 iB){ |
| 1982 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
| 1983 | return __builtin_sub_overflow(*pA, iB, pA); |
| 1984 | #else |
| 1985 | testcase( iB==SMALLEST_INT64+1 ); |
| 1986 | if( iB==SMALLEST_INT64 ){ |
| 1987 | testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
| 1988 | if( (*pA)>=0 ) return 1; |
| 1989 | *pA -= iB; |
| 1990 | return 0; |
| 1991 | }else{ |
| 1992 | return sqlite3AddInt64(pA, -iB); |
| 1993 | } |
| 1994 | #endif |
| 1995 | } |
| 1996 | int sqlite3MulInt64(i64 *pA, i64 iB){ |
| 1997 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
| 1998 | return __builtin_mul_overflow(*pA, iB, pA); |
| 1999 | #else |
| 2000 | i64 iA = *pA; |
| 2001 | if( iB>0 ){ |
| 2002 | if( iA>LARGEST_INT64/iB ) return 1; |
| 2003 | if( iA<SMALLEST_INT64/iB ) return 1; |
| 2004 | }else if( iB<0 ){ |
| 2005 | if( iA>0 ){ |
| 2006 | if( iB<SMALLEST_INT64/iA ) return 1; |
| 2007 | }else if( iA<0 ){ |
| 2008 | if( iB==SMALLEST_INT64 ) return 1; |
| 2009 | if( iA==SMALLEST_INT64 ) return 1; |
| 2010 | if( -iA>LARGEST_INT64/-iB ) return 1; |
| 2011 | } |
| 2012 | } |
| 2013 | *pA = iA*iB; |
| 2014 | return 0; |
| 2015 | #endif |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | ** Compute the absolute value of a 32-bit signed integer, if possible. Or |
| 2020 | ** if the integer has a value of -2147483648, return +2147483647 |
| 2021 | */ |
| 2022 | int sqlite3AbsInt32(int x){ |
| 2023 | if( x>=0 ) return x; |
| 2024 | if( x==(int)0x80000000 ) return 0x7fffffff; |
| 2025 | return -x; |
| 2026 | } |
| 2027 | |
| 2028 | #ifdef SQLITE_ENABLE_8_3_NAMES |
| 2029 | /* |
| 2030 | ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database |
| 2031 | ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and |
| 2032 | ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than |
| 2033 | ** three characters, then shorten the suffix on z[] to be the last three |
| 2034 | ** characters of the original suffix. |
| 2035 | ** |
| 2036 | ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always |
| 2037 | ** do the suffix shortening regardless of URI parameter. |
| 2038 | ** |
| 2039 | ** Examples: |
| 2040 | ** |
| 2041 | ** test.db-journal => test.nal |
| 2042 | ** test.db-wal => test.wal |
| 2043 | ** test.db-shm => test.shm |
| 2044 | ** test.db-mj7f3319fa => test.9fa |
| 2045 | */ |
| 2046 | void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ |
| 2047 | #if SQLITE_ENABLE_8_3_NAMES<2 |
| 2048 | if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) |
| 2049 | #endif |
| 2050 | { |
| 2051 | int i, sz; |
| 2052 | sz = sqlite3Strlen30(z); |
| 2053 | for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} |
| 2054 | if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); |
| 2055 | } |
| 2056 | } |
| 2057 | #endif |
| 2058 | |
| 2059 | /* |
| 2060 | ** Find (an approximate) sum of two LogEst values. This computation is |
| 2061 | ** not a simple "+" operator because LogEst is stored as a logarithmic |
| 2062 | ** value. |
| 2063 | ** |
| 2064 | */ |
| 2065 | LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ |
| 2066 | static const unsigned char x[] = { |
| 2067 | 10, 10, /* 0,1 */ |
| 2068 | 9, 9, /* 2,3 */ |
| 2069 | 8, 8, /* 4,5 */ |
| 2070 | 7, 7, 7, /* 6,7,8 */ |
| 2071 | 6, 6, 6, /* 9,10,11 */ |
| 2072 | 5, 5, 5, /* 12-14 */ |
| 2073 | 4, 4, 4, 4, /* 15-18 */ |
| 2074 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 2075 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 2076 | }; |
| 2077 | if( a>=b ){ |
| 2078 | if( a>b+49 ) return a; |
| 2079 | if( a>b+31 ) return a+1; |
| 2080 | return a+x[a-b]; |
| 2081 | }else{ |
| 2082 | if( b>a+49 ) return b; |
| 2083 | if( b>a+31 ) return b+1; |
| 2084 | return b+x[b-a]; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | ** Convert an integer into a LogEst. In other words, compute an |
| 2090 | ** approximation for 10*log2(x). |
| 2091 | */ |
| 2092 | LogEst sqlite3LogEst(u64 x){ |
| 2093 | static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 2094 | LogEst y = 40; |
| 2095 | if( x<8 ){ |
| 2096 | if( x<2 ) return 0; |
| 2097 | while( x<8 ){ y -= 10; x <<= 1; } |
| 2098 | }else{ |
| 2099 | #if GCC_VERSION>=5004000 |
| 2100 | int i = 60 - __builtin_clzll(x); |
| 2101 | y += i*10; |
| 2102 | x >>= i; |
| 2103 | #else |
| 2104 | while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ |
| 2105 | while( x>15 ){ y += 10; x >>= 1; } |
| 2106 | #endif |
| 2107 | } |
| 2108 | return a[x&7] + y - 10; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | ** Convert a double into a LogEst |
| 2113 | ** In other words, compute an approximation for 10*log2(x). |
| 2114 | */ |
| 2115 | LogEst sqlite3LogEstFromDouble(double x){ |
| 2116 | u64 a; |
| 2117 | LogEst e; |
| 2118 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 2119 | if( x<=1 ) return 0; |
| 2120 | if( x<=2000000000 ) return sqlite3LogEst((u64)x); |
| 2121 | memcpy(&a, &x, 8); |
| 2122 | e = (a>>52) - 1022; |
| 2123 | return e*10; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | ** Convert a LogEst into an integer. |
| 2128 | */ |
| 2129 | u64 sqlite3LogEstToInt(LogEst x){ |
| 2130 | u64 n; |
| 2131 | n = x%10; |
| 2132 | x /= 10; |
| 2133 | if( n>=5 ) n -= 2; |
| 2134 | else if( n>=1 ) n -= 1; |
| 2135 | if( x>60 ) return (u64)LARGEST_INT64; |
| 2136 | return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | ** Add a new name/number pair to a VList. This might require that the |
| 2141 | ** VList object be reallocated, so return the new VList. If an OOM |
| 2142 | ** error occurs, the original VList returned and the |
| 2143 | ** db->mallocFailed flag is set. |
| 2144 | ** |
| 2145 | ** A VList is really just an array of integers. To destroy a VList, |
| 2146 | ** simply pass it to sqlite3DbFree(). |
| 2147 | ** |
| 2148 | ** The first integer is the number of integers allocated for the whole |
| 2149 | ** VList. The second integer is the number of integers actually used. |
| 2150 | ** Each name/number pair is encoded by subsequent groups of 3 or more |
| 2151 | ** integers. |
| 2152 | ** |
| 2153 | ** Each name/number pair starts with two integers which are the numeric |
| 2154 | ** value for the pair and the size of the name/number pair, respectively. |
| 2155 | ** The text name overlays one or more following integers. The text name |
| 2156 | ** is always zero-terminated. |
| 2157 | ** |
| 2158 | ** Conceptually: |
| 2159 | ** |
| 2160 | ** struct VList { |
| 2161 | ** int nAlloc; // Number of allocated slots |
| 2162 | ** int nUsed; // Number of used slots |
| 2163 | ** struct VListEntry { |
| 2164 | ** int iValue; // Value for this entry |
| 2165 | ** int nSlot; // Slots used by this entry |
| 2166 | ** // ... variable name goes here |
| 2167 | ** } a[0]; |
| 2168 | ** } |
| 2169 | ** |
| 2170 | ** During code generation, pointers to the variable names within the |
| 2171 | ** VList are taken. When that happens, nAlloc is set to zero as an |
| 2172 | ** indication that the VList may never again be enlarged, since the |
| 2173 | ** accompanying realloc() would invalidate the pointers. |
| 2174 | */ |
| 2175 | VList *sqlite3VListAdd( |
| 2176 | sqlite3 *db, /* The database connection used for malloc() */ |
| 2177 | VList *pIn, /* The input VList. Might be NULL */ |
| 2178 | const char *zName, /* Name of symbol to add */ |
| 2179 | int nName, /* Bytes of text in zName */ |
| 2180 | int iVal /* Value to associate with zName */ |
| 2181 | ){ |
| 2182 | int nInt; /* number of sizeof(int) objects needed for zName */ |
| 2183 | char *z; /* Pointer to where zName will be stored */ |
| 2184 | int i; /* Index in pIn[] where zName is stored */ |
| 2185 | |
| 2186 | nInt = nName/4 + 3; |
| 2187 | assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ |
| 2188 | if( pIn==0 || pIn[1]+nInt > pIn[0] ){ |
| 2189 | /* Enlarge the allocation */ |
| 2190 | sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt; |
| 2191 | VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); |
| 2192 | if( pOut==0 ) return pIn; |
| 2193 | if( pIn==0 ) pOut[1] = 2; |
| 2194 | pIn = pOut; |
| 2195 | pIn[0] = nAlloc; |
| 2196 | } |
| 2197 | i = pIn[1]; |
| 2198 | pIn[i] = iVal; |
| 2199 | pIn[i+1] = nInt; |
| 2200 | z = (char*)&pIn[i+2]; |
| 2201 | pIn[1] = i+nInt; |
| 2202 | assert( pIn[1]<=pIn[0] ); |
| 2203 | memcpy(z, zName, nName); |
| 2204 | z[nName] = 0; |
| 2205 | return pIn; |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | ** Return a pointer to the name of a variable in the given VList that |
| 2210 | ** has the value iVal. Or return a NULL if there is no such variable in |
| 2211 | ** the list |
| 2212 | */ |
| 2213 | const char *sqlite3VListNumToName(VList *pIn, int iVal){ |
| 2214 | int i, mx; |
| 2215 | if( pIn==0 ) return 0; |
| 2216 | mx = pIn[1]; |
| 2217 | i = 2; |
| 2218 | do{ |
| 2219 | if( pIn[i]==iVal ) return (char*)&pIn[i+2]; |
| 2220 | i += pIn[i+1]; |
| 2221 | }while( i<mx ); |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | ** Return the number of the variable named zName, if it is in VList. |
| 2227 | ** or return 0 if there is no such variable. |
| 2228 | */ |
| 2229 | int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ |
| 2230 | int i, mx; |
| 2231 | if( pIn==0 ) return 0; |
| 2232 | mx = pIn[1]; |
| 2233 | i = 2; |
| 2234 | do{ |
| 2235 | const char *z = (const char*)&pIn[i+2]; |
| 2236 | if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i]; |
| 2237 | i += pIn[i+1]; |
| 2238 | }while( i<mx ); |
| 2239 | return 0; |
| 2240 | } |