drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 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> |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 20 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 7e6dc5d | 2019-05-10 12:14:51 | [diff] [blame] | 21 | #include <math.h> |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 22 | #endif |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 23 | |
| 24 | /* |
drh | ce059e5 | 2019-04-05 17:22:50 | [diff] [blame] | 25 | ** Calls to sqlite3FaultSim() are used to simulate a failure during testing, |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 26 | ** or to bypass normal error detection during testing in order to let |
| 27 | ** execute proceed further downstream. |
drh | c007f61 | 2014-05-16 14:17:01 | [diff] [blame] | 28 | ** |
drh | ce059e5 | 2019-04-05 17:22:50 | [diff] [blame] | 29 | ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The |
| 30 | ** sqlite3FaultSim() function only returns non-zero during testing. |
drh | c007f61 | 2014-05-16 14:17:01 | [diff] [blame] | 31 | ** |
drh | ce059e5 | 2019-04-05 17:22:50 | [diff] [blame] | 32 | ** During testing, if the test harness has set a fault-sim callback using |
| 33 | ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then |
| 34 | ** each call to sqlite3FaultSim() is relayed to that application-supplied |
| 35 | ** callback and the integer return value form the application-supplied |
| 36 | ** callback is returned by sqlite3FaultSim(). |
| 37 | ** |
| 38 | ** The integer argument to sqlite3FaultSim() is a code to identify which |
| 39 | ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() |
| 40 | ** should have a unique code. To prevent legacy testing applications from |
| 41 | ** breaking, the codes should not be changed or reused. |
drh | c007f61 | 2014-05-16 14:17:01 | [diff] [blame] | 42 | */ |
drh | d12602a | 2016-12-07 15:49:02 | [diff] [blame] | 43 | #ifndef SQLITE_UNTESTABLE |
drh | c007f61 | 2014-05-16 14:17:01 | [diff] [blame] | 44 | int sqlite3FaultSim(int iTest){ |
| 45 | int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
| 46 | return xCallback ? xCallback(iTest) : SQLITE_OK; |
| 47 | } |
| 48 | #endif |
| 49 | |
drh | 85c8f29 | 2010-01-13 17:39:53 | [diff] [blame] | 50 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 51 | /* |
| 52 | ** Return true if the floating point value is Not a Number (NaN). |
drh | e534c7b | 2021-09-06 11:44:19 | [diff] [blame] | 53 | ** |
| 54 | ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
| 55 | ** Otherwise, we have our own implementation that works on most systems. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 56 | */ |
| 57 | int sqlite3IsNaN(double x){ |
drh | e534c7b | 2021-09-06 11:44:19 | [diff] [blame] | 58 | int rc; /* The value return */ |
| 59 | #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN |
drh | 0592122 | 2019-05-30 00:46:37 | [diff] [blame] | 60 | u64 y; |
| 61 | memcpy(&y,&x,sizeof(y)); |
drh | e534c7b | 2021-09-06 11:44:19 | [diff] [blame] | 62 | rc = IsNaN(y); |
| 63 | #else |
| 64 | rc = isnan(x); |
| 65 | #endif /* HAVE_ISNAN */ |
| 66 | testcase( rc ); |
| 67 | return rc; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 68 | } |
drh | 85c8f29 | 2010-01-13 17:39:53 | [diff] [blame] | 69 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 70 | |
drh | 5ed044e | 2024-03-19 10:16:17 | [diff] [blame] | 71 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 72 | /* |
| 73 | ** Return true if the floating point value is NaN or +Inf or -Inf. |
| 74 | */ |
| 75 | int sqlite3IsOverflow(double x){ |
| 76 | int rc; /* The value return */ |
| 77 | u64 y; |
| 78 | memcpy(&y,&x,sizeof(y)); |
| 79 | rc = IsOvfl(y); |
| 80 | return rc; |
| 81 | } |
| 82 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 83 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 84 | /* |
| 85 | ** Compute a string length that is limited to what can be stored in |
| 86 | ** lower 30 bits of a 32-bit signed integer. |
| 87 | ** |
| 88 | ** The value returned will never be negative. Nor will it ever be greater |
| 89 | ** than the actual length of the string. For very long strings (greater |
| 90 | ** than 1GiB) the value returned might be less than the true string length. |
| 91 | */ |
| 92 | int sqlite3Strlen30(const char *z){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 93 | if( z==0 ) return 0; |
drh | 1116bf1 | 2015-06-30 03:18:33 | [diff] [blame] | 94 | return 0x3fffffff & (int)strlen(z); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 98 | ** Return the declared type of a column. Or return zDflt if the column |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 99 | ** has no declared type. |
| 100 | ** |
| 101 | ** The column type is an extra string stored after the zero-terminator on |
| 102 | ** the column name if and only if the COLFLAG_HASTYPE flag is set. |
drh | 94eaafa | 2016-02-29 15:53:11 | [diff] [blame] | 103 | */ |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 104 | char *sqlite3ColumnType(Column *pCol, char *zDflt){ |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 105 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 106 | return pCol->zCnName + strlen(pCol->zCnName) + 1; |
drh | b70f2ea | 2021-08-18 12:05:22 | [diff] [blame] | 107 | }else if( pCol->eCType ){ |
| 108 | assert( pCol->eCType<=SQLITE_N_STDTYPE ); |
| 109 | return (char*)sqlite3StdType[pCol->eCType-1]; |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 110 | }else{ |
| 111 | return zDflt; |
| 112 | } |
drh | 94eaafa | 2016-02-29 15:53:11 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
drh | 80fbee0 | 2016-03-21 11:57:13 | [diff] [blame] | 116 | ** Helper function for sqlite3Error() - called rarely. Broken out into |
| 117 | ** a separate routine to avoid unnecessary register saves on entry to |
| 118 | ** sqlite3Error(). |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 119 | */ |
drh | 8d2f41c | 2016-03-21 11:38:01 | [diff] [blame] | 120 | static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ |
| 121 | if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 122 | sqlite3SystemError(db, err_code); |
| 123 | } |
drh | 80fbee0 | 2016-03-21 11:57:13 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | ** Set the current error code to err_code and clear any prior error message. |
| 127 | ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates |
| 128 | ** that would be appropriate. |
| 129 | */ |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 130 | void sqlite3Error(sqlite3 *db, int err_code){ |
| 131 | assert( db!=0 ); |
| 132 | db->errCode = err_code; |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 133 | if( err_code || db->pErr ){ |
| 134 | sqlite3ErrorFinish(db, err_code); |
| 135 | }else{ |
| 136 | db->errByteOffset = -1; |
| 137 | } |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
drh | 88efc79 | 2021-01-01 18:23:56 | [diff] [blame] | 141 | ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state |
| 142 | ** and error message. |
| 143 | */ |
| 144 | void sqlite3ErrorClear(sqlite3 *db){ |
| 145 | assert( db!=0 ); |
| 146 | db->errCode = SQLITE_OK; |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 147 | db->errByteOffset = -1; |
drh | 88efc79 | 2021-01-01 18:23:56 | [diff] [blame] | 148 | if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 149 | } |
| 150 | |
| 151 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 | [diff] [blame] | 152 | ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| 153 | ** to do based on the SQLite error code in rc. |
| 154 | */ |
| 155 | void sqlite3SystemError(sqlite3 *db, int rc){ |
| 156 | if( rc==SQLITE_IOERR_NOMEM ) return; |
stephan | 09e6c82 | 2023-12-22 15:41:13 | [diff] [blame] | 157 | #if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) |
dan | 9023444 | 2021-09-10 21:28:56 | [diff] [blame] | 158 | if( rc==SQLITE_IOERR_IN_PAGE ){ |
| 159 | int ii; |
| 160 | int iErr; |
| 161 | sqlite3BtreeEnterAll(db); |
| 162 | for(ii=0; ii<db->nDb; ii++){ |
| 163 | if( db->aDb[ii].pBt ){ |
| 164 | iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); |
| 165 | if( iErr ){ |
| 166 | db->iSysErrno = iErr; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | sqlite3BtreeLeaveAll(db); |
| 171 | return; |
| 172 | } |
| 173 | #endif |
drh | 1b9f214 | 2016-03-17 16:01:23 | [diff] [blame] | 174 | rc &= 0xff; |
| 175 | if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ |
| 176 | db->iSysErrno = sqlite3OsGetLastError(db->pVfs); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 181 | ** Set the most recent error code and error string for the sqlite |
| 182 | ** handle "db". The error code is set to "err_code". |
| 183 | ** |
| 184 | ** If it is not NULL, string zFormat specifies the format of the |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 185 | ** error string. zFormat and any string tokens that follow it are |
| 186 | ** assumed to be encoded in UTF-8. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 187 | ** |
| 188 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| 189 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 190 | ** to NULL. |
| 191 | */ |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 192 | void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ |
drh | a3cc007 | 2013-12-13 16:23:55 | [diff] [blame] | 193 | assert( db!=0 ); |
| 194 | db->errCode = err_code; |
drh | 8d2f41c | 2016-03-21 11:38:01 | [diff] [blame] | 195 | sqlite3SystemError(db, err_code); |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 196 | if( zFormat==0 ){ |
| 197 | sqlite3Error(db, err_code); |
| 198 | }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ |
drh | a3cc007 | 2013-12-13 16:23:55 | [diff] [blame] | 199 | char *z; |
| 200 | va_list ap; |
| 201 | va_start(ap, zFormat); |
| 202 | z = sqlite3VMPrintf(db, zFormat, ap); |
| 203 | va_end(ap); |
| 204 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
drh | f84cbd1 | 2023-01-12 13:25:48 | [diff] [blame] | 209 | ** Check for interrupts and invoke progress callback. |
| 210 | */ |
| 211 | void sqlite3ProgressCheck(Parse *p){ |
| 212 | sqlite3 *db = p->db; |
| 213 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
| 214 | p->nErr++; |
| 215 | p->rc = SQLITE_INTERRUPT; |
| 216 | } |
| 217 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
stephan | 0d066bc | 2023-08-28 12:06:38 | [diff] [blame] | 218 | if( db->xProgress ){ |
| 219 | if( p->rc==SQLITE_INTERRUPT ){ |
| 220 | p->nProgressSteps = 0; |
| 221 | }else if( (++p->nProgressSteps)>=db->nProgressOps ){ |
| 222 | if( db->xProgress(db->pProgressArg) ){ |
| 223 | p->nErr++; |
| 224 | p->rc = SQLITE_INTERRUPT; |
| 225 | } |
| 226 | p->nProgressSteps = 0; |
drh | 2fc9dc9 | 2023-01-12 19:51:49 | [diff] [blame] | 227 | } |
drh | f84cbd1 | 2023-01-12 13:25:48 | [diff] [blame] | 228 | } |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 233 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 234 | ** |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 235 | ** This function should be used to report any error that occurs while |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 236 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 237 | ** last thing the sqlite3_prepare() function does is copy the error |
| 238 | ** stored by this function into the database handle using sqlite3Error(). |
drh | 13f40da | 2014-08-22 18:00:11 | [diff] [blame] | 239 | ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used |
| 240 | ** during statement execution (sqlite3_step() etc.). |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 241 | */ |
| 242 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 243 | char *zMsg; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 244 | va_list ap; |
| 245 | sqlite3 *db = pParse->db; |
drh | c692df2 | 2022-01-24 15:34:55 | [diff] [blame] | 246 | assert( db!=0 ); |
drh | 173b418 | 2022-09-02 17:25:25 | [diff] [blame] | 247 | assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 248 | db->errByteOffset = -2; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 249 | va_start(ap, zFormat); |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 250 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 251 | va_end(ap); |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 252 | if( db->errByteOffset<-1 ) db->errByteOffset = -1; |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 253 | if( db->suppressErr ){ |
| 254 | sqlite3DbFree(db, zMsg); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 255 | if( db->mallocFailed ){ |
| 256 | pParse->nErr++; |
| 257 | pParse->rc = SQLITE_NOMEM; |
| 258 | } |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 259 | }else{ |
| 260 | pParse->nErr++; |
| 261 | sqlite3DbFree(db, pParse->zErrMsg); |
| 262 | pParse->zErrMsg = zMsg; |
| 263 | pParse->rc = SQLITE_ERROR; |
drh | 46a31cd | 2019-11-09 14:38:58 | [diff] [blame] | 264 | pParse->pWith = 0; |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 265 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
drh | c3dcdba | 2019-04-09 21:32:46 | [diff] [blame] | 269 | ** If database connection db is currently parsing SQL, then transfer |
| 270 | ** error code errCode to that parser if the parser has not already |
| 271 | ** encountered some other kind of error. |
| 272 | */ |
| 273 | int sqlite3ErrorToParser(sqlite3 *db, int errCode){ |
| 274 | Parse *pParse; |
| 275 | if( db==0 || (pParse = db->pParse)==0 ) return errCode; |
| 276 | pParse->rc = errCode; |
| 277 | pParse->nErr++; |
| 278 | return errCode; |
| 279 | } |
| 280 | |
| 281 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 282 | ** Convert an SQL-style quoted string into a normal string by removing |
| 283 | ** the quote characters. The conversion is done in-place. If the |
| 284 | ** input does not begin with a quote character, then this routine |
| 285 | ** is a no-op. |
| 286 | ** |
| 287 | ** The input string must be zero-terminated. A new zero-terminator |
| 288 | ** is added to the dequoted string. |
| 289 | ** |
| 290 | ** The return value is -1 if no dequoting occurs or the length of the |
| 291 | ** dequoted string, exclusive of the zero terminator, if dequoting does |
| 292 | ** occur. |
| 293 | ** |
drh | 51d35b0 | 2019-01-11 13:32:23 | [diff] [blame] | 294 | ** 2002-02-14: This routine is extended to remove MS-Access style |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 295 | ** brackets from around identifiers. For example: "[a-b-c]" becomes |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 296 | ** "a-b-c". |
| 297 | */ |
drh | 244b9d6 | 2016-04-11 19:01:08 | [diff] [blame] | 298 | void sqlite3Dequote(char *z){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 299 | char quote; |
| 300 | int i, j; |
drh | 244b9d6 | 2016-04-11 19:01:08 | [diff] [blame] | 301 | if( z==0 ) return; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 302 | quote = z[0]; |
drh | 244b9d6 | 2016-04-11 19:01:08 | [diff] [blame] | 303 | if( !sqlite3Isquote(quote) ) return; |
| 304 | if( quote=='[' ) quote = ']'; |
drh | 9ccd865 | 2013-09-13 16:36:46 | [diff] [blame] | 305 | for(i=1, j=0;; i++){ |
| 306 | assert( z[i] ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 307 | if( z[i]==quote ){ |
| 308 | if( z[i+1]==quote ){ |
| 309 | z[j++] = quote; |
| 310 | i++; |
| 311 | }else{ |
| 312 | break; |
| 313 | } |
| 314 | }else{ |
| 315 | z[j++] = z[i]; |
| 316 | } |
| 317 | } |
| 318 | z[j] = 0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 319 | } |
drh | 51d35b0 | 2019-01-11 13:32:23 | [diff] [blame] | 320 | void sqlite3DequoteExpr(Expr *p){ |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 321 | assert( !ExprHasProperty(p, EP_IntValue) ); |
drh | 51d35b0 | 2019-01-11 13:32:23 | [diff] [blame] | 322 | assert( sqlite3Isquote(p->u.zToken[0]) ); |
| 323 | p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; |
| 324 | sqlite3Dequote(p->u.zToken); |
| 325 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 326 | |
drh | 40aced5 | 2016-01-22 17:48:09 | [diff] [blame] | 327 | /* |
dan | 8374f7d | 2024-01-22 19:38:55 | [diff] [blame] | 328 | ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken |
| 329 | ** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those |
| 330 | ** that contain '_' characters that must be removed before further processing. |
dan | 3eae666 | 2024-01-20 16:18:04 | [diff] [blame] | 331 | */ |
dan | 406eb5a | 2024-01-23 11:20:58 | [diff] [blame] | 332 | void sqlite3DequoteNumber(Parse *pParse, Expr *p){ |
drh | 8597eee | 2024-02-28 01:12:21 | [diff] [blame] | 333 | assert( p!=0 || pParse->db->mallocFailed ); |
dan | 3eae666 | 2024-01-20 16:18:04 | [diff] [blame] | 334 | if( p ){ |
| 335 | const char *pIn = p->u.zToken; |
| 336 | char *pOut = p->u.zToken; |
dan | 406eb5a | 2024-01-23 11:20:58 | [diff] [blame] | 337 | int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X')); |
drh | 8597eee | 2024-02-28 01:12:21 | [diff] [blame] | 338 | int iValue; |
dan | 3eae666 | 2024-01-20 16:18:04 | [diff] [blame] | 339 | assert( p->op==TK_QNUMBER ); |
| 340 | p->op = TK_INTEGER; |
| 341 | do { |
| 342 | if( *pIn!=SQLITE_DIGIT_SEPARATOR ){ |
| 343 | *pOut++ = *pIn; |
| 344 | if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; |
dan | 406eb5a | 2024-01-23 11:20:58 | [diff] [blame] | 345 | }else{ |
| 346 | if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) |
| 347 | || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) |
| 348 | ){ |
| 349 | sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); |
| 350 | } |
dan | 3eae666 | 2024-01-20 16:18:04 | [diff] [blame] | 351 | } |
| 352 | }while( *pIn++ ); |
dan | 406eb5a | 2024-01-23 11:20:58 | [diff] [blame] | 353 | if( bHex ) p->op = TK_INTEGER; |
drh | 8597eee | 2024-02-28 01:12:21 | [diff] [blame] | 354 | |
| 355 | /* tag-20240227-a: If after dequoting, the number is an integer that |
| 356 | ** fits in 32 bits, then it must be converted into EP_IntValue. Other |
| 357 | ** parts of the code expect this. See also tag-20240227-b. */ |
| 358 | if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ |
| 359 | p->u.iValue = iValue; |
| 360 | p->flags |= EP_IntValue; |
| 361 | } |
dan | 3eae666 | 2024-01-20 16:18:04 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /* |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 366 | ** If the input token p is quoted, try to adjust the token to remove |
| 367 | ** the quotes. This is not always possible: |
| 368 | ** |
| 369 | ** "abc" -> abc |
| 370 | ** "ab""cd" -> (not possible because of the interior "") |
| 371 | ** |
| 372 | ** Remove the quotes if possible. This is a optimization. The overall |
| 373 | ** system should still return the correct answer even if this routine |
| 374 | ** is always a no-op. |
| 375 | */ |
| 376 | void sqlite3DequoteToken(Token *p){ |
drh | 15482bc | 2021-08-06 15:26:01 | [diff] [blame] | 377 | unsigned int i; |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 378 | if( p->n<2 ) return; |
| 379 | if( !sqlite3Isquote(p->z[0]) ) return; |
| 380 | for(i=1; i<p->n-1; i++){ |
| 381 | if( sqlite3Isquote(p->z[i]) ) return; |
| 382 | } |
| 383 | p->n -= 2; |
| 384 | p->z++; |
| 385 | } |
| 386 | |
| 387 | /* |
drh | 40aced5 | 2016-01-22 17:48:09 | [diff] [blame] | 388 | ** Generate a Token object from a string |
| 389 | */ |
| 390 | void sqlite3TokenInit(Token *p, char *z){ |
| 391 | p->z = z; |
| 392 | p->n = sqlite3Strlen30(z); |
| 393 | } |
| 394 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 395 | /* Convenient short-hand */ |
| 396 | #define UpperToLower sqlite3UpperToLower |
| 397 | |
| 398 | /* |
| 399 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 400 | ** there is no consistency, we will define our own. |
drh | 9f129f4 | 2010-08-31 15:27:32 | [diff] [blame] | 401 | ** |
drh | 0299b40 | 2012-03-19 17:42:46 | [diff] [blame] | 402 | ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
| 403 | ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
| 404 | ** the contents of two buffers containing UTF-8 strings in a |
| 405 | ** case-independent fashion, using the same definition of "case |
| 406 | ** independence" that SQLite uses internally when comparing identifiers. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 407 | */ |
drh | 3fa9730 | 2012-02-22 16:58:36 | [diff] [blame] | 408 | int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 409 | if( zLeft==0 ){ |
| 410 | return zRight ? -1 : 0; |
| 411 | }else if( zRight==0 ){ |
| 412 | return 1; |
| 413 | } |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 414 | return sqlite3StrICmp(zLeft, zRight); |
| 415 | } |
| 416 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 417 | unsigned char *a, *b; |
drh | 7e42733 | 2019-04-17 11:34:44 | [diff] [blame] | 418 | int c, x; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 419 | a = (unsigned char *)zLeft; |
| 420 | b = (unsigned char *)zRight; |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 421 | for(;;){ |
drh | 7e42733 | 2019-04-17 11:34:44 | [diff] [blame] | 422 | c = *a; |
| 423 | x = *b; |
| 424 | if( c==x ){ |
| 425 | if( c==0 ) break; |
| 426 | }else{ |
| 427 | c = (int)UpperToLower[c] - (int)UpperToLower[x]; |
| 428 | if( c ) break; |
| 429 | } |
drh | 80738d9 | 2016-02-15 00:34:16 | [diff] [blame] | 430 | a++; |
| 431 | b++; |
| 432 | } |
| 433 | return c; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 434 | } |
| 435 | int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 436 | register unsigned char *a, *b; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 437 | if( zLeft==0 ){ |
| 438 | return zRight ? -1 : 0; |
| 439 | }else if( zRight==0 ){ |
| 440 | return 1; |
| 441 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 442 | a = (unsigned char *)zLeft; |
| 443 | b = (unsigned char *)zRight; |
| 444 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 445 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 446 | } |
| 447 | |
| 448 | /* |
drh | d44390c | 2020-04-06 18:16:31 | [diff] [blame] | 449 | ** Compute an 8-bit hash on a string that is insensitive to case differences |
| 450 | */ |
| 451 | u8 sqlite3StrIHash(const char *z){ |
| 452 | u8 h = 0; |
| 453 | if( z==0 ) return 0; |
| 454 | while( z[0] ){ |
| 455 | h += UpperToLower[(unsigned char)z[0]]; |
| 456 | z++; |
| 457 | } |
| 458 | return h; |
| 459 | } |
| 460 | |
drh | 1790ccb | 2023-07-05 14:42:50 | [diff] [blame] | 461 | /* Double-Double multiplication. (x[0],x[1]) *= (y,yy) |
drh | 02a43f6 | 2017-12-26 14:46:20 | [diff] [blame] | 462 | ** |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 463 | ** Reference: |
| 464 | ** T. J. Dekker, "A Floating-Point Technique for Extending the |
| 465 | ** Available Precision". 1971-07-26. |
drh | 02a43f6 | 2017-12-26 14:46:20 | [diff] [blame] | 466 | */ |
drh | efd0cf8 | 2023-07-05 22:05:18 | [diff] [blame] | 467 | static void dekkerMul2(volatile double *x, double y, double yy){ |
| 468 | /* |
| 469 | ** The "volatile" keywords on parameter x[] and on local variables |
| 470 | ** below are needed force intermediate results to be truncated to |
| 471 | ** binary64 rather than be carried around in an extended-precision |
| 472 | ** format. The truncation is necessary for the Dekker algorithm to |
| 473 | ** work. Intel x86 floating point might omit the truncation without |
| 474 | ** the use of volatile. |
| 475 | */ |
| 476 | volatile double tx, ty, p, q, c, cc; |
| 477 | double hx, hy; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 478 | u64 m; |
drh | efd0cf8 | 2023-07-05 22:05:18 | [diff] [blame] | 479 | memcpy(&m, (void*)&x[0], 8); |
drh | 4c40b7b | 2023-07-08 17:42:24 | [diff] [blame] | 480 | m &= 0xfffffffffc000000LL; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 481 | memcpy(&hx, &m, 8); |
drh | 1790ccb | 2023-07-05 14:42:50 | [diff] [blame] | 482 | tx = x[0] - hx; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 483 | memcpy(&m, &y, 8); |
drh | 4c40b7b | 2023-07-08 17:42:24 | [diff] [blame] | 484 | m &= 0xfffffffffc000000LL; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 485 | memcpy(&hy, &m, 8); |
| 486 | ty = y - hy; |
| 487 | p = hx*hy; |
| 488 | q = hx*ty + tx*hy; |
| 489 | c = p+q; |
| 490 | cc = p - c + q + tx*ty; |
drh | 1790ccb | 2023-07-05 14:42:50 | [diff] [blame] | 491 | cc = x[0]*yy + x[1]*y + cc; |
| 492 | x[0] = c + cc; |
drh | 85ca6d7 | 2023-07-05 15:34:30 | [diff] [blame] | 493 | x[1] = c - x[0]; |
| 494 | x[1] += cc; |
drh | 02a43f6 | 2017-12-26 14:46:20 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 498 | ** The string z[] is an text representation of a real number. |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 499 | ** Convert this string to a double and write it into *pResult. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 500 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 501 | ** The string z[] is length bytes in length (bytes, not characters) and |
| 502 | ** uses the encoding enc. The string is not necessarily zero-terminated. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 503 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 504 | ** Return TRUE if the result is a valid real number (or integer) and FALSE |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 505 | ** if the string is empty or contains extraneous text. More specifically |
| 506 | ** return |
| 507 | ** 1 => The input string is a pure integer |
| 508 | ** 2 or more => The input has a decimal point or eNNN clause |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 509 | ** 0 or less => The input string is not a valid number |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 510 | ** -1 => Not a valid number, but has a valid prefix which |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 511 | ** includes a decimal point and/or an eNNN clause |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 512 | ** |
| 513 | ** Valid numbers are in one of these formats: |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 514 | ** |
| 515 | ** [+-]digits[E[+-]digits] |
| 516 | ** [+-]digits.[digits][E[+-]digits] |
| 517 | ** [+-].digits[E[+-]digits] |
| 518 | ** |
| 519 | ** Leading and trailing whitespace is ignored for the purpose of determining |
| 520 | ** validity. |
| 521 | ** |
| 522 | ** If some prefix of the input string is a valid number, this routine |
| 523 | ** returns FALSE but it still converts the prefix and writes the result |
| 524 | ** into *pResult. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 525 | */ |
mistachkin | 6dcf9a4 | 2019-10-10 23:58:16 | [diff] [blame] | 526 | #if defined(_MSC_VER) |
| 527 | #pragma warning(disable : 4756) |
| 528 | #endif |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 529 | int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 530 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 531 | int incr; |
drh | e3a4f2c | 2019-12-13 23:38:57 | [diff] [blame] | 532 | const char *zEnd; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 533 | /* sign * significand * (10 ^ (esign * exponent)) */ |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 534 | int sign = 1; /* sign of significand */ |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 535 | u64 s = 0; /* significand */ |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 536 | int d = 0; /* adjust exponent for shifting decimal point */ |
| 537 | int esign = 1; /* sign of exponent */ |
| 538 | int e = 0; /* exponent */ |
| 539 | int eValid = 1; /* True exponent is either not used or is well-formed */ |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 540 | int nDigit = 0; /* Number of digits processed */ |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 541 | int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ |
drh | 8703642 | 2024-12-07 19:06:25 | [diff] [blame] | 542 | u64 s2; /* round-tripped significand */ |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 543 | double rr[2]; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 544 | |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 545 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 546 | *pResult = 0.0; /* Default return value, in case of an error */ |
drh | e3a4f2c | 2019-12-13 23:38:57 | [diff] [blame] | 547 | if( length==0 ) return 0; |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 548 | |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 549 | if( enc==SQLITE_UTF8 ){ |
| 550 | incr = 1; |
drh | e3a4f2c | 2019-12-13 23:38:57 | [diff] [blame] | 551 | zEnd = z + length; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 552 | }else{ |
| 553 | int i; |
| 554 | incr = 2; |
drh | 87969b2 | 2020-01-08 12:17:46 | [diff] [blame] | 555 | length &= ~1; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 556 | assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
drh | 84422db | 2019-05-30 13:47:10 | [diff] [blame] | 557 | testcase( enc==SQLITE_UTF16LE ); |
| 558 | testcase( enc==SQLITE_UTF16BE ); |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 559 | for(i=3-enc; i<length && z[i]==0; i+=2){} |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 560 | if( i<length ) eType = -100; |
drh | ad975d5 | 2016-04-27 15:24:13 | [diff] [blame] | 561 | zEnd = &z[i^1]; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 562 | z += (enc&1); |
| 563 | } |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 564 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 565 | /* skip leading spaces */ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 566 | while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 567 | if( z>=zEnd ) return 0; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 568 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 569 | /* get sign of significand */ |
| 570 | if( *z=='-' ){ |
| 571 | sign = -1; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 572 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 573 | }else if( *z=='+' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 574 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 575 | } |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 576 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 577 | /* copy max significant digits to significand */ |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 578 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 579 | s = s*10 + (*z - '0'); |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 580 | z+=incr; nDigit++; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 581 | if( s>=((LARGEST_UINT64-9)/10) ){ |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 582 | /* skip non-significant significand digits |
| 583 | ** (increase exponent by d to shift decimal left) */ |
| 584 | while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; } |
| 585 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 586 | } |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 587 | if( z>=zEnd ) goto do_atof_calc; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 588 | |
| 589 | /* if decimal point is present */ |
| 590 | if( *z=='.' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 591 | z+=incr; |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 592 | eType++; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 593 | /* copy digits from after decimal to significand |
| 594 | ** (decrease exponent by d to shift decimal right) */ |
drh | 15af62a | 2016-04-26 23:14:45 | [diff] [blame] | 595 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 596 | if( s<((LARGEST_UINT64-9)/10) ){ |
drh | 15af62a | 2016-04-26 23:14:45 | [diff] [blame] | 597 | s = s*10 + (*z - '0'); |
| 598 | d--; |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 599 | nDigit++; |
drh | 15af62a | 2016-04-26 23:14:45 | [diff] [blame] | 600 | } |
drh | c2b893a | 2019-05-25 18:17:53 | [diff] [blame] | 601 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 602 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 603 | } |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 604 | if( z>=zEnd ) goto do_atof_calc; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 605 | |
| 606 | /* if exponent is present */ |
| 607 | if( *z=='e' || *z=='E' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 608 | z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 609 | eValid = 0; |
drh | 8a3884e | 2019-05-29 21:18:27 | [diff] [blame] | 610 | eType++; |
drh | ad975d5 | 2016-04-27 15:24:13 | [diff] [blame] | 611 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 612 | /* This branch is needed to avoid a (harmless) buffer overread. The |
drh | ad975d5 | 2016-04-27 15:24:13 | [diff] [blame] | 613 | ** special comment alerts the mutation tester that the correct answer |
| 614 | ** is obtained even if the branch is omitted */ |
| 615 | if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ |
| 616 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 617 | /* get sign of exponent */ |
| 618 | if( *z=='-' ){ |
| 619 | esign = -1; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 620 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 621 | }else if( *z=='+' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 622 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 623 | } |
| 624 | /* copy digits to exponent */ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 625 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
drh | 57db4a7 | 2011-10-17 20:41:46 | [diff] [blame] | 626 | e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 627 | z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 628 | eValid = 1; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 632 | /* skip trailing spaces */ |
drh | c6daa01 | 2016-04-27 02:35:03 | [diff] [blame] | 633 | while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 | [diff] [blame] | 634 | |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 635 | do_atof_calc: |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 636 | /* Zero is a special case */ |
| 637 | if( s==0 ){ |
| 638 | *pResult = sign<0 ? -0.0 : +0.0; |
| 639 | goto atof_return; |
| 640 | } |
| 641 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 642 | /* adjust exponent by d, and update sign */ |
| 643 | e = (e*esign) + d; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 644 | |
| 645 | /* Try to adjust the exponent to make it smaller */ |
drh | 1a4b211 | 2024-12-07 14:48:55 | [diff] [blame] | 646 | while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){ |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 647 | s *= 10; |
| 648 | e--; |
| 649 | } |
| 650 | while( e<0 && (s%10)==0 ){ |
| 651 | s /= 10; |
| 652 | e++; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 653 | } |
| 654 | |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 655 | rr[0] = (double)s; |
drh | 9f53d0c | 2024-12-07 19:57:30 | [diff] [blame] | 656 | assert( sizeof(s2)==sizeof(rr[0]) ); |
drh | 54f96dc | 2024-12-09 11:47:28 | [diff] [blame] | 657 | #ifdef SQLITE_DEBUG |
| 658 | rr[1] = 18446744073709549568.0; |
| 659 | memcpy(&s2, &rr[1], sizeof(s2)); |
| 660 | assert( s2==0x43efffffffffffffLL ); |
| 661 | #endif |
| 662 | /* Largest double that can be safely converted to u64 |
| 663 | ** vvvvvvvvvvvvvvvvvvvvvv */ |
| 664 | if( rr[0]<=18446744073709549568.0 ){ |
drh | 9f53d0c | 2024-12-07 19:57:30 | [diff] [blame] | 665 | s2 = (u64)rr[0]; |
| 666 | rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); |
| 667 | }else{ |
drh | 1a4b211 | 2024-12-07 14:48:55 | [diff] [blame] | 668 | rr[1] = 0.0; |
| 669 | } |
drh | 9f53d0c | 2024-12-07 19:57:30 | [diff] [blame] | 670 | assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */ |
drh | 8703642 | 2024-12-07 19:06:25 | [diff] [blame] | 671 | |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 672 | if( e>0 ){ |
| 673 | while( e>=100 ){ |
| 674 | e -= 100; |
| 675 | dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 676 | } |
| 677 | while( e>=10 ){ |
| 678 | e -= 10; |
| 679 | dekkerMul2(rr, 1.0e+10, 0.0); |
| 680 | } |
| 681 | while( e>=1 ){ |
| 682 | e -= 1; |
| 683 | dekkerMul2(rr, 1.0e+01, 0.0); |
drh | cbaef88 | 2023-08-01 19:10:30 | [diff] [blame] | 684 | } |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 685 | }else{ |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 686 | while( e<=-100 ){ |
| 687 | e += 100; |
| 688 | dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 689 | } |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 690 | while( e<=-10 ){ |
| 691 | e += 10; |
| 692 | dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 693 | } |
| 694 | while( e<=-1 ){ |
| 695 | e += 1; |
| 696 | dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 697 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 698 | } |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 699 | *pResult = rr[0]+rr[1]; |
| 700 | if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300; |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 701 | if( sign<0 ) *pResult = -*pResult; |
| 702 | assert( !sqlite3IsNaN(*pResult) ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 703 | |
drh | aa4356d | 2023-07-03 18:18:35 | [diff] [blame] | 704 | atof_return: |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 705 | /* return true if number and no extra non-whitespace characters after */ |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 706 | if( z==zEnd && nDigit>0 && eValid && eType>0 ){ |
| 707 | return eType; |
drh | 378a7d3 | 2019-06-10 23:45:10 | [diff] [blame] | 708 | }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){ |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 709 | return -1; |
| 710 | }else{ |
| 711 | return 0; |
| 712 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 713 | #else |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 | [diff] [blame] | 714 | return !sqlite3Atoi64(z, pResult, length, enc); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 715 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 716 | } |
mistachkin | 6dcf9a4 | 2019-10-10 23:58:16 | [diff] [blame] | 717 | #if defined(_MSC_VER) |
| 718 | #pragma warning(default : 4756) |
| 719 | #endif |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 720 | |
| 721 | /* |
drh | fbde3f5 | 2023-01-03 18:51:18 | [diff] [blame] | 722 | ** Render an signed 64-bit integer as text. Store the result in zOut[] and |
| 723 | ** return the length of the string that was stored, in bytes. The value |
| 724 | ** returned does not include the zero terminator at the end of the output |
| 725 | ** string. |
drh | 82b0f10 | 2020-07-21 18:25:19 | [diff] [blame] | 726 | ** |
| 727 | ** The caller must ensure that zOut[] is at least 21 bytes in size. |
| 728 | */ |
drh | fbde3f5 | 2023-01-03 18:51:18 | [diff] [blame] | 729 | int sqlite3Int64ToText(i64 v, char *zOut){ |
drh | 82b0f10 | 2020-07-21 18:25:19 | [diff] [blame] | 730 | int i; |
| 731 | u64 x; |
| 732 | char zTemp[22]; |
| 733 | if( v<0 ){ |
drh | 8deae5a | 2020-07-29 12:23:20 | [diff] [blame] | 734 | x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; |
drh | 82b0f10 | 2020-07-21 18:25:19 | [diff] [blame] | 735 | }else{ |
| 736 | x = v; |
| 737 | } |
| 738 | i = sizeof(zTemp)-2; |
| 739 | zTemp[sizeof(zTemp)-1] = 0; |
drh | 6b50742 | 2023-04-12 18:57:50 | [diff] [blame] | 740 | while( 1 /*exit-by-break*/ ){ |
| 741 | zTemp[i] = (x%10) + '0'; |
drh | 82b0f10 | 2020-07-21 18:25:19 | [diff] [blame] | 742 | x = x/10; |
drh | 6b50742 | 2023-04-12 18:57:50 | [diff] [blame] | 743 | if( x==0 ) break; |
| 744 | i--; |
| 745 | }; |
| 746 | if( v<0 ) zTemp[--i] = '-'; |
| 747 | memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); |
| 748 | return sizeof(zTemp)-1-i; |
drh | 82b0f10 | 2020-07-21 18:25:19 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 752 | ** Compare the 19-character string zNum against the text representation |
| 753 | ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| 754 | ** if zNum is less than, equal to, or greater than the string. |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 | [diff] [blame] | 755 | ** Note that zNum must contain exactly 19 characters. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 756 | ** |
| 757 | ** Unlike memcmp() this routine is guaranteed to return the difference |
| 758 | ** in the values of the last digit if the only difference is in the |
| 759 | ** last digit. So, for example, |
| 760 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 761 | ** compare2pow63("9223372036854775800", 1) |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 762 | ** |
| 763 | ** will return -8. |
| 764 | */ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 765 | static int compare2pow63(const char *zNum, int incr){ |
| 766 | int c = 0; |
| 767 | int i; |
| 768 | /* 012345678901234567 */ |
| 769 | const char *pow63 = "922337203685477580"; |
| 770 | for(i=0; c==0 && i<18; i++){ |
| 771 | c = (zNum[i*incr]-pow63[i])*10; |
| 772 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 773 | if( c==0 ){ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 774 | c = zNum[18*incr] - '8'; |
drh | 44dbca8 | 2010-01-13 04:22:20 | [diff] [blame] | 775 | testcase( c==(-1) ); |
| 776 | testcase( c==0 ); |
| 777 | testcase( c==(+1) ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 778 | } |
| 779 | return c; |
| 780 | } |
| 781 | |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 782 | /* |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 783 | ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This |
| 784 | ** routine does *not* accept hexadecimal notation. |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 785 | ** |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 786 | ** Returns: |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 787 | ** |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 788 | ** -1 Not even a prefix of the input text looks like an integer |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 789 | ** 0 Successful transformation. Fits in a 64-bit signed integer. |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 790 | ** 1 Excess non-space text after the integer value |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 791 | ** 2 Integer too large for a 64-bit signed integer or is malformed |
| 792 | ** 3 Special case of 9223372036854775808 |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 793 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 794 | ** length is the number of bytes in the string (bytes, not characters). |
| 795 | ** The string is not necessarily zero-terminated. The encoding is |
| 796 | ** given by enc. |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 797 | */ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 798 | int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 799 | int incr; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 800 | u64 u = 0; |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 | [diff] [blame] | 801 | int neg = 0; /* assume positive */ |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 802 | int i; |
| 803 | int c = 0; |
drh | 609d584 | 2016-04-28 00:32:16 | [diff] [blame] | 804 | int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 805 | int rc; /* Baseline return code */ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 806 | const char *zStart; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 807 | const char *zEnd = zNum + length; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 808 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 809 | if( enc==SQLITE_UTF8 ){ |
| 810 | incr = 1; |
| 811 | }else{ |
| 812 | incr = 2; |
drh | 359941b | 2020-08-27 16:28:30 | [diff] [blame] | 813 | length &= ~1; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 814 | assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 815 | for(i=3-enc; i<length && zNum[i]==0; i+=2){} |
| 816 | nonNum = i<length; |
drh | 609d584 | 2016-04-28 00:32:16 | [diff] [blame] | 817 | zEnd = &zNum[i^1]; |
drh | 0e5fba7 | 2013-03-20 12:04:29 | [diff] [blame] | 818 | zNum += (enc&1); |
| 819 | } |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 820 | while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 821 | if( zNum<zEnd ){ |
| 822 | if( *zNum=='-' ){ |
| 823 | neg = 1; |
| 824 | zNum+=incr; |
| 825 | }else if( *zNum=='+' ){ |
| 826 | zNum+=incr; |
| 827 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 828 | } |
| 829 | zStart = zNum; |
drh | 9339da1 | 2010-09-30 00:50:49 | [diff] [blame] | 830 | while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
| 831 | for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 832 | u = u*10 + c - '0'; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 833 | } |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 834 | testcase( i==18*incr ); |
| 835 | testcase( i==19*incr ); |
| 836 | testcase( i==20*incr ); |
drh | 1822ebf | 2018-01-27 14:25:27 | [diff] [blame] | 837 | if( u>LARGEST_INT64 ){ |
| 838 | /* This test and assignment is needed only to suppress UB warnings |
| 839 | ** from clang and -fsanitize=undefined. This test and assignment make |
| 840 | ** the code a little larger and slower, and no harm comes from omitting |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 841 | ** them, but we must appease the undefined-behavior pharisees. */ |
drh | 1822ebf | 2018-01-27 14:25:27 | [diff] [blame] | 842 | *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| 843 | }else if( neg ){ |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 844 | *pNum = -(i64)u; |
| 845 | }else{ |
| 846 | *pNum = (i64)u; |
| 847 | } |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 848 | rc = 0; |
drh | 9a27822 | 2019-06-07 22:26:08 | [diff] [blame] | 849 | if( i==0 && zStart==zNum ){ /* No digits */ |
| 850 | rc = -1; |
| 851 | }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 852 | rc = 1; |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 853 | }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */ |
| 854 | int jj = i; |
| 855 | do{ |
| 856 | if( !sqlite3Isspace(zNum[jj]) ){ |
| 857 | rc = 1; /* Extra non-space text after the integer */ |
| 858 | break; |
| 859 | } |
| 860 | jj += incr; |
| 861 | }while( &zNum[jj]<zEnd ); |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 862 | } |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 863 | if( i<19*incr ){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 864 | /* Less than 19 digits, so we know that it fits in 64 bits */ |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 865 | assert( u<=LARGEST_INT64 ); |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 866 | return rc; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 867 | }else{ |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 868 | /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 869 | c = i>19*incr ? 1 : compare2pow63(zNum, incr); |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 870 | if( c<0 ){ |
| 871 | /* zNum is less than 9223372036854775808 so it fits */ |
| 872 | assert( u<=LARGEST_INT64 ); |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 873 | return rc; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 874 | }else{ |
drh | 4eb57ce | 2018-01-26 18:37:34 | [diff] [blame] | 875 | *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| 876 | if( c>0 ){ |
| 877 | /* zNum is greater than 9223372036854775808 so it overflows */ |
| 878 | return 2; |
| 879 | }else{ |
| 880 | /* zNum is exactly 9223372036854775808. Fits if negative. The |
| 881 | ** special case 2 overflow if positive */ |
| 882 | assert( u-1==LARGEST_INT64 ); |
| 883 | return neg ? rc : 3; |
| 884 | } |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 885 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | /* |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 890 | ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, |
| 891 | ** into a 64-bit signed integer. This routine accepts hexadecimal literals, |
| 892 | ** whereas sqlite3Atoi64() does not. |
| 893 | ** |
| 894 | ** Returns: |
| 895 | ** |
| 896 | ** 0 Successful transformation. Fits in a 64-bit signed integer. |
drh | 84d4f1a | 2017-09-20 10:47:10 | [diff] [blame] | 897 | ** 1 Excess text after the integer value |
| 898 | ** 2 Integer too large for a 64-bit signed integer or is malformed |
| 899 | ** 3 Special case of 9223372036854775808 |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 900 | */ |
| 901 | int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ |
| 902 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 903 | if( z[0]=='0' |
| 904 | && (z[1]=='x' || z[1]=='X') |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 905 | ){ |
| 906 | u64 u = 0; |
| 907 | int i, k; |
| 908 | for(i=2; z[i]=='0'; i++){} |
| 909 | for(k=i; sqlite3Isxdigit(z[k]); k++){ |
| 910 | u = u*16 + sqlite3HexToInt(z[k]); |
| 911 | } |
| 912 | memcpy(pOut, &u, 8); |
drh | 49d8e0e | 2023-04-27 18:28:10 | [diff] [blame] | 913 | if( k-i>16 ) return 2; |
| 914 | if( z[k]!=0 ) return 1; |
| 915 | return 0; |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 916 | }else |
| 917 | #endif /* SQLITE_OMIT_HEX_INTEGER */ |
| 918 | { |
drh | 028acd9 | 2023-07-21 18:38:59 | [diff] [blame] | 919 | int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); |
| 920 | if( z[n] ) n++; |
| 921 | return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8); |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 926 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 927 | ** *pValue to that integer and return true. Otherwise return false. |
| 928 | ** |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 929 | ** This routine accepts both decimal and hexadecimal notation for integers. |
| 930 | ** |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 931 | ** Any non-numeric characters that following zNum are ignored. |
| 932 | ** This is different from sqlite3Atoi64() which requires the |
| 933 | ** input number to be zero-terminated. |
| 934 | */ |
| 935 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 936 | sqlite_int64 v = 0; |
| 937 | int i, c; |
| 938 | int neg = 0; |
| 939 | if( zNum[0]=='-' ){ |
| 940 | neg = 1; |
| 941 | zNum++; |
| 942 | }else if( zNum[0]=='+' ){ |
| 943 | zNum++; |
| 944 | } |
drh | 28e048c | 2014-07-23 01:26:51 | [diff] [blame] | 945 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 946 | else if( zNum[0]=='0' |
| 947 | && (zNum[1]=='x' || zNum[1]=='X') |
| 948 | && sqlite3Isxdigit(zNum[2]) |
| 949 | ){ |
| 950 | u32 u = 0; |
| 951 | zNum += 2; |
| 952 | while( zNum[0]=='0' ) zNum++; |
drh | e61aa23 | 2023-04-19 12:08:46 | [diff] [blame] | 953 | for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){ |
drh | 28e048c | 2014-07-23 01:26:51 | [diff] [blame] | 954 | u = u*16 + sqlite3HexToInt(zNum[i]); |
| 955 | } |
| 956 | if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ |
| 957 | memcpy(pValue, &u, 4); |
| 958 | return 1; |
| 959 | }else{ |
| 960 | return 0; |
| 961 | } |
| 962 | } |
| 963 | #endif |
drh | 313e6fd | 2017-05-03 17:44:28 | [diff] [blame] | 964 | if( !sqlite3Isdigit(zNum[0]) ) return 0; |
drh | 935f2e7 | 2015-04-18 04:45:00 | [diff] [blame] | 965 | while( zNum[0]=='0' ) zNum++; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 966 | for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
| 967 | v = v*10 + c; |
| 968 | } |
| 969 | |
| 970 | /* The longest decimal representation of a 32 bit integer is 10 digits: |
| 971 | ** |
| 972 | ** 1234567890 |
| 973 | ** 2^31 -> 2147483648 |
| 974 | */ |
drh | 44dbca8 | 2010-01-13 04:22:20 | [diff] [blame] | 975 | testcase( i==10 ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 976 | if( i>10 ){ |
| 977 | return 0; |
| 978 | } |
drh | 44dbca8 | 2010-01-13 04:22:20 | [diff] [blame] | 979 | testcase( v-neg==2147483647 ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 980 | if( v-neg>2147483647 ){ |
| 981 | return 0; |
| 982 | } |
| 983 | if( neg ){ |
| 984 | v = -v; |
| 985 | } |
| 986 | *pValue = (int)v; |
| 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | /* |
drh | 60ac3f4 | 2010-11-23 18:59:27 | [diff] [blame] | 991 | ** Return a 32-bit integer value extracted from a string. If the |
| 992 | ** string is not an integer, just return 0. |
| 993 | */ |
| 994 | int sqlite3Atoi(const char *z){ |
| 995 | int x = 0; |
drh | 48bf2d7 | 2020-07-30 17:14:55 | [diff] [blame] | 996 | sqlite3GetInt32(z, &x); |
drh | 60ac3f4 | 2010-11-23 18:59:27 | [diff] [blame] | 997 | return x; |
| 998 | } |
| 999 | |
| 1000 | /* |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1001 | ** Decode a floating-point value into an approximate decimal |
| 1002 | ** representation. |
drh | 002330d | 2023-06-30 19:41:57 | [diff] [blame] | 1003 | ** |
drh | 34e4c6f | 2024-06-10 12:43:03 | [diff] [blame] | 1004 | ** If iRound<=0 then round to -iRound significant digits to the |
| 1005 | ** the left of the decimal point, or to a maximum of mxRound total |
| 1006 | ** significant digits. |
| 1007 | ** |
| 1008 | ** If iRound>0 round to min(iRound,mxRound) significant digits total. |
| 1009 | ** |
| 1010 | ** mxRound must be positive. |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1011 | ** |
| 1012 | ** The significant digits of the decimal representation are |
| 1013 | ** stored in p->z[] which is a often (but not always) a pointer |
| 1014 | ** into the middle of p->zBuf[]. There are p->n significant digits. |
| 1015 | ** The p->z[] array is *not* zero-terminated. |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1016 | */ |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1017 | void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1018 | int i; |
| 1019 | u64 v; |
| 1020 | int e, exp = 0; |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1021 | double rr[2]; |
| 1022 | |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 1023 | p->isSpecial = 0; |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1024 | p->z = p->zBuf; |
drh | 34e4c6f | 2024-06-10 12:43:03 | [diff] [blame] | 1025 | assert( mxRound>0 ); |
| 1026 | |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1027 | /* Convert negative numbers to positive. Deal with Infinity, 0.0, and |
| 1028 | ** NaN. */ |
| 1029 | if( r<0.0 ){ |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1030 | p->sign = '-'; |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1031 | r = -r; |
| 1032 | }else if( r==0.0 ){ |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1033 | p->sign = '+'; |
| 1034 | p->n = 1; |
| 1035 | p->iDP = 1; |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1036 | p->z = "0"; |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1037 | return; |
| 1038 | }else{ |
| 1039 | p->sign = '+'; |
| 1040 | } |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1041 | memcpy(&v,&r,8); |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1042 | e = v>>52; |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 1043 | if( (e&0x7ff)==0x7ff ){ |
drh | 4c40b7b | 2023-07-08 17:42:24 | [diff] [blame] | 1044 | p->isSpecial = 1 + (v!=0x7ff0000000000000LL); |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 1045 | p->n = 0; |
| 1046 | p->iDP = 0; |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1047 | return; |
| 1048 | } |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 1049 | |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1050 | /* Multiply r by powers of ten until it lands somewhere in between |
| 1051 | ** 1.0e+19 and 1.0e+17. |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1052 | ** |
| 1053 | ** Use Dekker-style double-double computation to increase the |
| 1054 | ** precision. |
| 1055 | ** |
| 1056 | ** The error terms on constants like 1.0e+100 computed using the |
| 1057 | ** decimal extension, for example as follows: |
| 1058 | ** |
| 1059 | ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100))); |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1060 | */ |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1061 | rr[0] = r; |
| 1062 | rr[1] = 0.0; |
| 1063 | if( rr[0]>9.223372036854774784e+18 ){ |
| 1064 | while( rr[0]>9.223372036854774784e+118 ){ |
| 1065 | exp += 100; |
| 1066 | dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1067 | } |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1068 | while( rr[0]>9.223372036854774784e+28 ){ |
| 1069 | exp += 10; |
| 1070 | dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 1071 | } |
| 1072 | while( rr[0]>9.223372036854774784e+18 ){ |
| 1073 | exp += 1; |
| 1074 | dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 1075 | } |
drh | 17c20bb | 2023-07-01 17:56:00 | [diff] [blame] | 1076 | }else{ |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1077 | while( rr[0]<9.223372036854774784e-83 ){ |
| 1078 | exp -= 100; |
| 1079 | dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1080 | } |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1081 | while( rr[0]<9.223372036854774784e+07 ){ |
| 1082 | exp -= 10; |
| 1083 | dekkerMul2(rr, 1.0e+10, 0.0); |
| 1084 | } |
| 1085 | while( rr[0]<9.22337203685477478e+17 ){ |
| 1086 | exp -= 1; |
| 1087 | dekkerMul2(rr, 1.0e+01, 0.0); |
| 1088 | } |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1089 | } |
drh | e8b2c92 | 2024-10-01 20:29:43 | [diff] [blame] | 1090 | v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; |
drh | c27bda0 | 2023-07-03 00:40:37 | [diff] [blame] | 1091 | |
| 1092 | /* Extract significant digits. */ |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1093 | i = sizeof(p->zBuf)-1; |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1094 | assert( v>0 ); |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1095 | while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1096 | assert( i>=0 && i<sizeof(p->zBuf)-1 ); |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1097 | p->n = sizeof(p->zBuf) - 1 - i; |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1098 | assert( p->n>0 ); |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1099 | assert( p->n<sizeof(p->zBuf) ); |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1100 | p->iDP = p->n + exp; |
drh | 6161cdd | 2024-02-17 03:32:31 | [diff] [blame] | 1101 | if( iRound<=0 ){ |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 1102 | iRound = p->iDP - iRound; |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1103 | if( iRound==0 && p->zBuf[i+1]>='5' ){ |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 1104 | iRound = 1; |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1105 | p->zBuf[i--] = '0'; |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 1106 | p->n++; |
| 1107 | p->iDP++; |
| 1108 | } |
| 1109 | } |
drh | 17c20bb | 2023-07-01 17:56:00 | [diff] [blame] | 1110 | if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1111 | char *z = &p->zBuf[i+1]; |
drh | 17c20bb | 2023-07-01 17:56:00 | [diff] [blame] | 1112 | if( iRound>mxRound ) iRound = mxRound; |
drh | 002330d | 2023-06-30 19:41:57 | [diff] [blame] | 1113 | p->n = iRound; |
| 1114 | if( z[iRound]>='5' ){ |
| 1115 | int j = iRound-1; |
| 1116 | while( 1 /*exit-by-break*/ ){ |
| 1117 | z[j]++; |
| 1118 | if( z[j]<='9' ) break; |
| 1119 | z[j] = '0'; |
| 1120 | if( j==0 ){ |
| 1121 | p->z[i--] = '1'; |
| 1122 | p->n++; |
| 1123 | p->iDP++; |
| 1124 | break; |
| 1125 | }else{ |
| 1126 | j--; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
drh | 50ba4e3 | 2023-07-07 18:49:08 | [diff] [blame] | 1131 | p->z = &p->zBuf[i+1]; |
drh | bc532ae | 2023-07-08 14:27:55 | [diff] [blame] | 1132 | assert( i+p->n < sizeof(p->zBuf) ); |
drh | a0d35d4 | 2025-02-10 11:16:37 | [diff] [blame] | 1133 | assert( p->n>0 ); |
| 1134 | while( p->z[p->n-1]=='0' ){ |
| 1135 | p->n--; |
| 1136 | assert( p->n>0 ); |
| 1137 | } |
drh | a1b0ff1 | 2023-06-30 18:35:43 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /* |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 1141 | ** Try to convert z into an unsigned 32-bit integer. Return true on |
| 1142 | ** success and false if there is an error. |
| 1143 | ** |
| 1144 | ** Only decimal notation is accepted. |
| 1145 | */ |
| 1146 | int sqlite3GetUInt32(const char *z, u32 *pI){ |
| 1147 | u64 v = 0; |
| 1148 | int i; |
| 1149 | for(i=0; sqlite3Isdigit(z[i]); i++){ |
| 1150 | v = v*10 + z[i] - '0'; |
drh | 69306bf | 2020-07-22 20:12:10 | [diff] [blame] | 1151 | if( v>4294967296LL ){ *pI = 0; return 0; } |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 1152 | } |
drh | 69306bf | 2020-07-22 20:12:10 | [diff] [blame] | 1153 | if( i==0 || z[i]!=0 ){ *pI = 0; return 0; } |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 1154 | *pI = (u32)v; |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1159 | ** The variable-length integer encoding is as follows: |
| 1160 | ** |
| 1161 | ** KEY: |
| 1162 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 1163 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 1164 | ** C = xxxxxxxx 8 bits of data |
| 1165 | ** |
| 1166 | ** 7 bits - A |
| 1167 | ** 14 bits - BA |
| 1168 | ** 21 bits - BBA |
| 1169 | ** 28 bits - BBBA |
| 1170 | ** 35 bits - BBBBA |
| 1171 | ** 42 bits - BBBBBA |
| 1172 | ** 49 bits - BBBBBBA |
| 1173 | ** 56 bits - BBBBBBBA |
| 1174 | ** 64 bits - BBBBBBBBC |
| 1175 | */ |
| 1176 | |
| 1177 | /* |
| 1178 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 1179 | ** The length of data write will be between 1 and 9 bytes. The number |
| 1180 | ** of bytes written is returned. |
| 1181 | ** |
| 1182 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 1183 | ** for all bytes that have the 8th bit set and one byte with the 8th |
| 1184 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 1185 | ** 8 bits and is the last byte. |
| 1186 | */ |
drh | 2f2b2b8 | 2014-08-22 18:48:25 | [diff] [blame] | 1187 | static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1188 | int i, j, n; |
| 1189 | u8 buf[10]; |
| 1190 | if( v & (((u64)0xff000000)<<32) ){ |
| 1191 | p[8] = (u8)v; |
| 1192 | v >>= 8; |
| 1193 | for(i=7; i>=0; i--){ |
| 1194 | p[i] = (u8)((v & 0x7f) | 0x80); |
| 1195 | v >>= 7; |
| 1196 | } |
| 1197 | return 9; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1198 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1199 | n = 0; |
| 1200 | do{ |
| 1201 | buf[n++] = (u8)((v & 0x7f) | 0x80); |
| 1202 | v >>= 7; |
| 1203 | }while( v!=0 ); |
| 1204 | buf[0] &= 0x7f; |
| 1205 | assert( n<=9 ); |
| 1206 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 1207 | p[i] = buf[j]; |
| 1208 | } |
| 1209 | return n; |
| 1210 | } |
drh | 2f2b2b8 | 2014-08-22 18:48:25 | [diff] [blame] | 1211 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
| 1212 | if( v<=0x7f ){ |
| 1213 | p[0] = v&0x7f; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1214 | return 1; |
| 1215 | } |
drh | 2f2b2b8 | 2014-08-22 18:48:25 | [diff] [blame] | 1216 | if( v<=0x3fff ){ |
| 1217 | p[0] = ((v>>7)&0x7f)|0x80; |
| 1218 | p[1] = v&0x7f; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1219 | return 2; |
| 1220 | } |
drh | 2f2b2b8 | 2014-08-22 18:48:25 | [diff] [blame] | 1221 | return putVarint64(p,v); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /* |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1225 | ** Bitmasks used by sqlite3GetVarint(). These precomputed constants |
| 1226 | ** are defined here rather than simply putting the constant expressions |
| 1227 | ** inline in order to work around bugs in the RVT compiler. |
| 1228 | ** |
| 1229 | ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f |
| 1230 | ** |
| 1231 | ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 |
| 1232 | */ |
| 1233 | #define SLOT_2_0 0x001fc07f |
| 1234 | #define SLOT_4_2_0 0xf01fc07f |
| 1235 | |
| 1236 | |
| 1237 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1238 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 1239 | ** Return the number of bytes read. The value is stored in *v. |
| 1240 | */ |
| 1241 | u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ |
| 1242 | u32 a,b,s; |
| 1243 | |
drh | 698c86f | 2019-04-17 12:07:08 | [diff] [blame] | 1244 | if( ((signed char*)p)[0]>=0 ){ |
| 1245 | *v = *p; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1246 | return 1; |
| 1247 | } |
drh | 698c86f | 2019-04-17 12:07:08 | [diff] [blame] | 1248 | if( ((signed char*)p)[1]>=0 ){ |
| 1249 | *v = ((u32)(p[0]&0x7f)<<7) | p[1]; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1250 | return 2; |
| 1251 | } |
| 1252 | |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1253 | /* Verify that constants are precomputed correctly */ |
| 1254 | assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); |
shaneh | 1da207e | 2010-03-09 14:41:12 | [diff] [blame] | 1255 | assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1256 | |
drh | 698c86f | 2019-04-17 12:07:08 | [diff] [blame] | 1257 | a = ((u32)p[0])<<14; |
| 1258 | b = p[1]; |
| 1259 | p += 2; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1260 | a |= *p; |
| 1261 | /* a: p0<<14 | p2 (unmasked) */ |
| 1262 | if (!(a&0x80)) |
| 1263 | { |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1264 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1265 | b &= 0x7f; |
| 1266 | b = b<<7; |
| 1267 | a |= b; |
| 1268 | *v = a; |
| 1269 | return 3; |
| 1270 | } |
| 1271 | |
| 1272 | /* CSE1 from below */ |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1273 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1274 | p++; |
| 1275 | b = b<<14; |
| 1276 | b |= *p; |
| 1277 | /* b: p1<<14 | p3 (unmasked) */ |
| 1278 | if (!(b&0x80)) |
| 1279 | { |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1280 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1281 | /* moved CSE1 up */ |
| 1282 | /* a &= (0x7f<<14)|(0x7f); */ |
| 1283 | a = a<<7; |
| 1284 | a |= b; |
| 1285 | *v = a; |
| 1286 | return 4; |
| 1287 | } |
| 1288 | |
| 1289 | /* a: p0<<14 | p2 (masked) */ |
| 1290 | /* b: p1<<14 | p3 (unmasked) */ |
| 1291 | /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 1292 | /* moved CSE1 up */ |
| 1293 | /* a &= (0x7f<<14)|(0x7f); */ |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1294 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1295 | s = a; |
| 1296 | /* s: p0<<14 | p2 (masked) */ |
| 1297 | |
| 1298 | p++; |
| 1299 | a = a<<14; |
| 1300 | a |= *p; |
| 1301 | /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| 1302 | if (!(a&0x80)) |
| 1303 | { |
drh | 62aaa6c | 2015-11-21 17:27:42 | [diff] [blame] | 1304 | /* we can skip these cause they were (effectively) done above |
| 1305 | ** while calculating s */ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1306 | /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 1307 | /* b &= (0x7f<<14)|(0x7f); */ |
| 1308 | b = b<<7; |
| 1309 | a |= b; |
| 1310 | s = s>>18; |
| 1311 | *v = ((u64)s)<<32 | a; |
| 1312 | return 5; |
| 1313 | } |
| 1314 | |
| 1315 | /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 1316 | s = s<<7; |
| 1317 | s |= b; |
| 1318 | /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 1319 | |
| 1320 | p++; |
| 1321 | b = b<<14; |
| 1322 | b |= *p; |
| 1323 | /* b: p1<<28 | p3<<14 | p5 (unmasked) */ |
| 1324 | if (!(b&0x80)) |
| 1325 | { |
| 1326 | /* we can skip this cause it was (effectively) done above in calc'ing s */ |
| 1327 | /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1328 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1329 | a = a<<7; |
| 1330 | a |= b; |
| 1331 | s = s>>18; |
| 1332 | *v = ((u64)s)<<32 | a; |
| 1333 | return 6; |
| 1334 | } |
| 1335 | |
| 1336 | p++; |
| 1337 | a = a<<14; |
| 1338 | a |= *p; |
| 1339 | /* a: p2<<28 | p4<<14 | p6 (unmasked) */ |
| 1340 | if (!(a&0x80)) |
| 1341 | { |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1342 | a &= SLOT_4_2_0; |
| 1343 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1344 | b = b<<7; |
| 1345 | a |= b; |
| 1346 | s = s>>11; |
| 1347 | *v = ((u64)s)<<32 | a; |
| 1348 | return 7; |
| 1349 | } |
| 1350 | |
| 1351 | /* CSE2 from below */ |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1352 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1353 | p++; |
| 1354 | b = b<<14; |
| 1355 | b |= *p; |
| 1356 | /* b: p3<<28 | p5<<14 | p7 (unmasked) */ |
| 1357 | if (!(b&0x80)) |
| 1358 | { |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1359 | b &= SLOT_4_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1360 | /* moved CSE2 up */ |
| 1361 | /* a &= (0x7f<<14)|(0x7f); */ |
| 1362 | a = a<<7; |
| 1363 | a |= b; |
| 1364 | s = s>>4; |
| 1365 | *v = ((u64)s)<<32 | a; |
| 1366 | return 8; |
| 1367 | } |
| 1368 | |
| 1369 | p++; |
| 1370 | a = a<<15; |
| 1371 | a |= *p; |
| 1372 | /* a: p4<<29 | p6<<15 | p8 (unmasked) */ |
| 1373 | |
| 1374 | /* moved CSE2 up */ |
| 1375 | /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ |
drh | 0b2864c | 2010-03-03 15:18:38 | [diff] [blame] | 1376 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1377 | b = b<<8; |
| 1378 | a |= b; |
| 1379 | |
| 1380 | s = s<<4; |
| 1381 | b = p[-4]; |
| 1382 | b &= 0x7f; |
| 1383 | b = b>>3; |
| 1384 | s |= b; |
| 1385 | |
| 1386 | *v = ((u64)s)<<32 | a; |
| 1387 | |
| 1388 | return 9; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 1393 | ** Return the number of bytes read. The value is stored in *v. |
| 1394 | ** |
| 1395 | ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned |
| 1396 | ** integer, then set *v to 0xffffffff. |
| 1397 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1398 | ** A MACRO version, getVarint32, is provided which inlines the |
| 1399 | ** single-byte case. All code should use the MACRO version as |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1400 | ** this function assumes the single-byte case has already been handled. |
| 1401 | */ |
| 1402 | u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1403 | u64 v64; |
| 1404 | u8 n; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1405 | |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1406 | /* Assume that the single-byte case has already been handled by |
| 1407 | ** the getVarint32() macro */ |
| 1408 | assert( (p[0] & 0x80)!=0 ); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1409 | |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1410 | if( (p[1] & 0x80)==0 ){ |
| 1411 | /* This is the two-byte case */ |
| 1412 | *v = ((p[0]&0x7f)<<7) | p[1]; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1413 | return 2; |
| 1414 | } |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1415 | if( (p[2] & 0x80)==0 ){ |
| 1416 | /* This is the three-byte case */ |
| 1417 | *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2]; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1418 | return 3; |
| 1419 | } |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1420 | /* four or more bytes */ |
| 1421 | n = sqlite3GetVarint(p, &v64); |
| 1422 | assert( n>3 && n<=9 ); |
| 1423 | if( (v64 & SQLITE_MAX_U32)!=v64 ){ |
| 1424 | *v = 0xffffffff; |
| 1425 | }else{ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1426 | *v = (u32)v64; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1427 | } |
drh | 0588385 | 2023-10-19 13:35:22 | [diff] [blame] | 1428 | return n; |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | ** Return the number of bytes that will be needed to store the given |
| 1433 | ** 64-bit integer. |
| 1434 | */ |
| 1435 | int sqlite3VarintLen(u64 v){ |
drh | 59a5364 | 2015-09-01 22:29:07 | [diff] [blame] | 1436 | int i; |
drh | 6f17c09 | 2016-03-04 21:18:09 | [diff] [blame] | 1437 | for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1438 | return i; |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | /* |
| 1443 | ** Read or write a four-byte big-endian integer value. |
| 1444 | */ |
| 1445 | u32 sqlite3Get4byte(const u8 *p){ |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1446 | #if SQLITE_BYTEORDER==4321 |
| 1447 | u32 x; |
| 1448 | memcpy(&x,p,4); |
| 1449 | return x; |
drh | dc5ece8 | 2017-02-15 15:09:09 | [diff] [blame] | 1450 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1451 | u32 x; |
| 1452 | memcpy(&x,p,4); |
| 1453 | return __builtin_bswap32(x); |
drh | a39284b | 2017-02-09 17:12:22 | [diff] [blame] | 1454 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
mistachkin | 647ca46 | 2015-06-30 17:28:40 | [diff] [blame] | 1455 | u32 x; |
| 1456 | memcpy(&x,p,4); |
| 1457 | return _byteswap_ulong(x); |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1458 | #else |
drh | 693e671 | 2014-01-24 22:58:00 | [diff] [blame] | 1459 | testcase( p[0]&0x80 ); |
| 1460 | return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1461 | #endif |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1462 | } |
| 1463 | void sqlite3Put4byte(unsigned char *p, u32 v){ |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1464 | #if SQLITE_BYTEORDER==4321 |
| 1465 | memcpy(p,&v,4); |
drh | dc5ece8 | 2017-02-15 15:09:09 | [diff] [blame] | 1466 | #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1467 | u32 x = __builtin_bswap32(v); |
| 1468 | memcpy(p,&x,4); |
drh | a39284b | 2017-02-09 17:12:22 | [diff] [blame] | 1469 | #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
mistachkin | 647ca46 | 2015-06-30 17:28:40 | [diff] [blame] | 1470 | u32 x = _byteswap_ulong(v); |
| 1471 | memcpy(p,&x,4); |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1472 | #else |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1473 | p[0] = (u8)(v>>24); |
| 1474 | p[1] = (u8)(v>>16); |
| 1475 | p[2] = (u8)(v>>8); |
| 1476 | p[3] = (u8)v; |
drh | 5372e4d | 2015-06-30 12:47:09 | [diff] [blame] | 1477 | #endif |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1478 | } |
| 1479 | |
drh | 9296c18 | 2014-07-23 13:40:49 | [diff] [blame] | 1480 | |
| 1481 | |
| 1482 | /* |
| 1483 | ** Translate a single byte of Hex into an integer. |
| 1484 | ** This routine only works if h really is a valid hexadecimal |
| 1485 | ** character: 0..9a..fA..F |
| 1486 | */ |
| 1487 | u8 sqlite3HexToInt(int h){ |
| 1488 | assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 1489 | #ifdef SQLITE_ASCII |
| 1490 | h += 9*(1&(h>>6)); |
| 1491 | #endif |
| 1492 | #ifdef SQLITE_EBCDIC |
| 1493 | h += 9*(1&~(h>>4)); |
| 1494 | #endif |
| 1495 | return (u8)(h & 0xf); |
| 1496 | } |
| 1497 | |
drh | b48c0d5 | 2020-02-07 01:12:53 | [diff] [blame] | 1498 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1499 | /* |
| 1500 | ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 1501 | ** value. Return a pointer to its binary value. Space to hold the |
| 1502 | ** binary value has been obtained from malloc and must be freed by |
| 1503 | ** the calling routine. |
| 1504 | */ |
| 1505 | void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
| 1506 | char *zBlob; |
| 1507 | int i; |
| 1508 | |
drh | 575fad6 | 2016-02-05 13:38:36 | [diff] [blame] | 1509 | zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1510 | n--; |
| 1511 | if( zBlob ){ |
| 1512 | for(i=0; i<n; i+=2){ |
dan | cd74b61 | 2011-04-22 19:37:32 | [diff] [blame] | 1513 | zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1514 | } |
| 1515 | zBlob[i/2] = 0; |
| 1516 | } |
| 1517 | return zBlob; |
| 1518 | } |
drh | b48c0d5 | 2020-02-07 01:12:53 | [diff] [blame] | 1519 | #endif /* !SQLITE_OMIT_BLOB_LITERAL */ |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1520 | |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1521 | /* |
| 1522 | ** Log an error that is an API call on a connection pointer that should |
| 1523 | ** not have been used. The "type" of connection pointer is given as the |
| 1524 | ** argument. The zType is a word like "NULL" or "closed" or "invalid". |
| 1525 | */ |
| 1526 | static void logBadConnection(const char *zType){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1527 | sqlite3_log(SQLITE_MISUSE, |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1528 | "API call with %s database connection pointer", |
| 1529 | zType |
| 1530 | ); |
| 1531 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1532 | |
| 1533 | /* |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1534 | ** Check to make sure we have a valid db pointer. This test is not |
| 1535 | ** foolproof but it does provide some measure of protection against |
| 1536 | ** misuse of the interface such as passing in db pointers that are |
| 1537 | ** NULL or which have been previously closed. If this routine returns |
| 1538 | ** 1 it means that the db pointer is valid and 0 if it should not be |
| 1539 | ** dereferenced for any reason. The calling function should invoke |
| 1540 | ** SQLITE_MISUSE immediately. |
| 1541 | ** |
| 1542 | ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| 1543 | ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 1544 | ** open properly and is not fit for general use but which can be |
| 1545 | ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| 1546 | */ |
| 1547 | int sqlite3SafetyCheckOk(sqlite3 *db){ |
drh | 5f9de6e | 2021-08-07 23:16:52 | [diff] [blame] | 1548 | u8 eOpenState; |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1549 | if( db==0 ){ |
| 1550 | logBadConnection("NULL"); |
| 1551 | return 0; |
| 1552 | } |
drh | 5f9de6e | 2021-08-07 23:16:52 | [diff] [blame] | 1553 | eOpenState = db->eOpenState; |
| 1554 | if( eOpenState!=SQLITE_STATE_OPEN ){ |
drh | e294da0 | 2010-02-25 23:44:15 | [diff] [blame] | 1555 | if( sqlite3SafetyCheckSickOrOk(db) ){ |
| 1556 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1557 | logBadConnection("unopened"); |
| 1558 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1559 | return 0; |
| 1560 | }else{ |
| 1561 | return 1; |
| 1562 | } |
| 1563 | } |
| 1564 | int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
drh | 5f9de6e | 2021-08-07 23:16:52 | [diff] [blame] | 1565 | u8 eOpenState; |
| 1566 | eOpenState = db->eOpenState; |
| 1567 | if( eOpenState!=SQLITE_STATE_SICK && |
| 1568 | eOpenState!=SQLITE_STATE_OPEN && |
| 1569 | eOpenState!=SQLITE_STATE_BUSY ){ |
drh | e294da0 | 2010-02-25 23:44:15 | [diff] [blame] | 1570 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | af46dc1 | 2010-02-24 21:44:07 | [diff] [blame] | 1571 | logBadConnection("invalid"); |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 1572 | return 0; |
| 1573 | }else{ |
| 1574 | return 1; |
| 1575 | } |
drh | c81c11f | 2009-11-10 01:30:52 | [diff] [blame] | 1576 | } |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1577 | |
| 1578 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1579 | ** Attempt to add, subtract, or multiply the 64-bit signed value iB against |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1580 | ** the other 64-bit signed integer at *pA and store the result in *pA. |
| 1581 | ** Return 0 on success. Or if the operation would have resulted in an |
| 1582 | ** overflow, leave *pA unchanged and return 1. |
| 1583 | */ |
| 1584 | int sqlite3AddInt64(i64 *pA, i64 iB){ |
drh | b9772e7 | 2017-09-12 13:27:43 | [diff] [blame] | 1585 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1586 | return __builtin_add_overflow(*pA, iB, pA); |
| 1587 | #else |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1588 | i64 iA = *pA; |
| 1589 | testcase( iA==0 ); testcase( iA==1 ); |
| 1590 | testcase( iB==-1 ); testcase( iB==0 ); |
| 1591 | if( iB>=0 ){ |
| 1592 | testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
| 1593 | testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
| 1594 | if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1595 | }else{ |
| 1596 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
| 1597 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
| 1598 | if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1599 | } |
drh | 53a6eb3 | 2014-02-10 12:59:15 | [diff] [blame] | 1600 | *pA += iB; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1601 | return 0; |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1602 | #endif |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1603 | } |
| 1604 | int sqlite3SubInt64(i64 *pA, i64 iB){ |
drh | b9772e7 | 2017-09-12 13:27:43 | [diff] [blame] | 1605 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1606 | return __builtin_sub_overflow(*pA, iB, pA); |
| 1607 | #else |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1608 | testcase( iB==SMALLEST_INT64+1 ); |
| 1609 | if( iB==SMALLEST_INT64 ){ |
| 1610 | testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
| 1611 | if( (*pA)>=0 ) return 1; |
| 1612 | *pA -= iB; |
| 1613 | return 0; |
| 1614 | }else{ |
| 1615 | return sqlite3AddInt64(pA, -iB); |
| 1616 | } |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1617 | #endif |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1618 | } |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1619 | int sqlite3MulInt64(i64 *pA, i64 iB){ |
drh | b9772e7 | 2017-09-12 13:27:43 | [diff] [blame] | 1620 | #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1621 | return __builtin_mul_overflow(*pA, iB, pA); |
| 1622 | #else |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1623 | i64 iA = *pA; |
drh | 09952c6 | 2016-09-20 22:04:05 | [diff] [blame] | 1624 | if( iB>0 ){ |
| 1625 | if( iA>LARGEST_INT64/iB ) return 1; |
| 1626 | if( iA<SMALLEST_INT64/iB ) return 1; |
| 1627 | }else if( iB<0 ){ |
| 1628 | if( iA>0 ){ |
| 1629 | if( iB<SMALLEST_INT64/iA ) return 1; |
| 1630 | }else if( iA<0 ){ |
| 1631 | if( iB==SMALLEST_INT64 ) return 1; |
| 1632 | if( iA==SMALLEST_INT64 ) return 1; |
| 1633 | if( -iA>LARGEST_INT64/-iB ) return 1; |
drh | 53a6eb3 | 2014-02-10 12:59:15 | [diff] [blame] | 1634 | } |
drh | 53a6eb3 | 2014-02-10 12:59:15 | [diff] [blame] | 1635 | } |
drh | 09952c6 | 2016-09-20 22:04:05 | [diff] [blame] | 1636 | *pA = iA*iB; |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1637 | return 0; |
drh | 4a47761 | 2017-01-03 17:33:43 | [diff] [blame] | 1638 | #endif |
drh | 158b9cb | 2011-03-05 20:59:46 | [diff] [blame] | 1639 | } |
drh | d50ffc4 | 2011-03-08 02:38:28 | [diff] [blame] | 1640 | |
| 1641 | /* |
stephan | 129203b | 2025-02-27 03:23:33 | [diff] [blame] | 1642 | ** Compute the absolute value of a 32-bit signed integer, if possible. Or |
drh | d50ffc4 | 2011-03-08 02:38:28 | [diff] [blame] | 1643 | ** if the integer has a value of -2147483648, return +2147483647 |
| 1644 | */ |
| 1645 | int sqlite3AbsInt32(int x){ |
| 1646 | if( x>=0 ) return x; |
drh | 87e79ae | 2011-03-08 13:06:41 | [diff] [blame] | 1647 | if( x==(int)0x80000000 ) return 0x7fffffff; |
drh | d50ffc4 | 2011-03-08 02:38:28 | [diff] [blame] | 1648 | return -x; |
| 1649 | } |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1650 | |
| 1651 | #ifdef SQLITE_ENABLE_8_3_NAMES |
| 1652 | /* |
drh | b51bf43 | 2011-07-21 21:29:35 | [diff] [blame] | 1653 | ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1654 | ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and |
| 1655 | ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than |
| 1656 | ** three characters, then shorten the suffix on z[] to be the last three |
| 1657 | ** characters of the original suffix. |
| 1658 | ** |
drh | b51bf43 | 2011-07-21 21:29:35 | [diff] [blame] | 1659 | ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always |
| 1660 | ** do the suffix shortening regardless of URI parameter. |
| 1661 | ** |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1662 | ** Examples: |
| 1663 | ** |
| 1664 | ** test.db-journal => test.nal |
| 1665 | ** test.db-wal => test.wal |
| 1666 | ** test.db-shm => test.shm |
drh | f580860 | 2011-12-16 00:33:04 | [diff] [blame] | 1667 | ** test.db-mj7f3319fa => test.9fa |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1668 | */ |
| 1669 | void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ |
drh | b51bf43 | 2011-07-21 21:29:35 | [diff] [blame] | 1670 | #if SQLITE_ENABLE_8_3_NAMES<2 |
drh | 7d39e17 | 2012-01-02 12:41:53 | [diff] [blame] | 1671 | if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) |
drh | b51bf43 | 2011-07-21 21:29:35 | [diff] [blame] | 1672 | #endif |
| 1673 | { |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1674 | int i, sz; |
| 1675 | sz = sqlite3Strlen30(z); |
drh | c83f2d4 | 2011-05-18 02:41:10 | [diff] [blame] | 1676 | for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} |
drh | c02a43a | 2012-01-10 23:18:38 | [diff] [blame] | 1677 | if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); |
drh | 81cc516 | 2011-05-17 20:36:21 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | #endif |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1681 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1682 | /* |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1683 | ** Find (an approximate) sum of two LogEst values. This computation is |
| 1684 | ** not a simple "+" operator because LogEst is stored as a logarithmic |
| 1685 | ** value. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1686 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1687 | */ |
| 1688 | LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ |
| 1689 | static const unsigned char x[] = { |
| 1690 | 10, 10, /* 0,1 */ |
| 1691 | 9, 9, /* 2,3 */ |
| 1692 | 8, 8, /* 4,5 */ |
| 1693 | 7, 7, 7, /* 6,7,8 */ |
| 1694 | 6, 6, 6, /* 9,10,11 */ |
| 1695 | 5, 5, 5, /* 12-14 */ |
| 1696 | 4, 4, 4, 4, /* 15-18 */ |
| 1697 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1698 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1699 | }; |
| 1700 | if( a>=b ){ |
| 1701 | if( a>b+49 ) return a; |
| 1702 | if( a>b+31 ) return a+1; |
| 1703 | return a+x[a-b]; |
| 1704 | }else{ |
| 1705 | if( b>a+49 ) return b; |
| 1706 | if( b>a+31 ) return b+1; |
| 1707 | return b+x[b-a]; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | /* |
drh | 224155d | 2014-04-30 13:19:09 | [diff] [blame] | 1712 | ** Convert an integer into a LogEst. In other words, compute an |
| 1713 | ** approximation for 10*log2(x). |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1714 | */ |
| 1715 | LogEst sqlite3LogEst(u64 x){ |
| 1716 | static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1717 | LogEst y = 40; |
| 1718 | if( x<8 ){ |
| 1719 | if( x<2 ) return 0; |
| 1720 | while( x<8 ){ y -= 10; x <<= 1; } |
| 1721 | }else{ |
drh | ceb4b1d | 2017-08-17 20:53:07 | [diff] [blame] | 1722 | #if GCC_VERSION>=5004000 |
| 1723 | int i = 60 - __builtin_clzll(x); |
| 1724 | y += i*10; |
| 1725 | x >>= i; |
| 1726 | #else |
drh | 75ab50c | 2016-04-28 14:15:12 | [diff] [blame] | 1727 | while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1728 | while( x>15 ){ y += 10; x >>= 1; } |
drh | ceb4b1d | 2017-08-17 20:53:07 | [diff] [blame] | 1729 | #endif |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1730 | } |
| 1731 | return a[x&7] + y - 10; |
| 1732 | } |
| 1733 | |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1734 | /* |
| 1735 | ** Convert a double into a LogEst |
| 1736 | ** In other words, compute an approximation for 10*log2(x). |
| 1737 | */ |
| 1738 | LogEst sqlite3LogEstFromDouble(double x){ |
| 1739 | u64 a; |
| 1740 | LogEst e; |
| 1741 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 1742 | if( x<=1 ) return 0; |
| 1743 | if( x<=2000000000 ) return sqlite3LogEst((u64)x); |
| 1744 | memcpy(&a, &x, 8); |
| 1745 | e = (a>>52) - 1022; |
| 1746 | return e*10; |
| 1747 | } |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1748 | |
| 1749 | /* |
| 1750 | ** Convert a LogEst into an integer. |
| 1751 | */ |
| 1752 | u64 sqlite3LogEstToInt(LogEst x){ |
| 1753 | u64 n; |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1754 | n = x%10; |
| 1755 | x /= 10; |
| 1756 | if( n>=5 ) n -= 2; |
| 1757 | else if( n>=1 ) n -= 1; |
drh | ecdf20d | 2016-03-10 14:28:24 | [diff] [blame] | 1758 | if( x>60 ) return (u64)LARGEST_INT64; |
drh | ecdf20d | 2016-03-10 14:28:24 | [diff] [blame] | 1759 | return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); |
drh | bf539c4 | 2013-10-05 18:16:02 | [diff] [blame] | 1760 | } |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1761 | |
| 1762 | /* |
| 1763 | ** Add a new name/number pair to a VList. This might require that the |
| 1764 | ** VList object be reallocated, so return the new VList. If an OOM |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1765 | ** error occurs, the original VList returned and the |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1766 | ** db->mallocFailed flag is set. |
| 1767 | ** |
| 1768 | ** A VList is really just an array of integers. To destroy a VList, |
| 1769 | ** simply pass it to sqlite3DbFree(). |
| 1770 | ** |
| 1771 | ** The first integer is the number of integers allocated for the whole |
| 1772 | ** VList. The second integer is the number of integers actually used. |
| 1773 | ** Each name/number pair is encoded by subsequent groups of 3 or more |
| 1774 | ** integers. |
| 1775 | ** |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1776 | ** Each name/number pair starts with two integers which are the numeric |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1777 | ** value for the pair and the size of the name/number pair, respectively. |
| 1778 | ** The text name overlays one or more following integers. The text name |
| 1779 | ** is always zero-terminated. |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1780 | ** |
| 1781 | ** Conceptually: |
| 1782 | ** |
| 1783 | ** struct VList { |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1784 | ** int nAlloc; // Number of allocated slots |
| 1785 | ** int nUsed; // Number of used slots |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1786 | ** struct VListEntry { |
| 1787 | ** int iValue; // Value for this entry |
| 1788 | ** int nSlot; // Slots used by this entry |
| 1789 | ** // ... variable name goes here |
| 1790 | ** } a[0]; |
| 1791 | ** } |
| 1792 | ** |
| 1793 | ** During code generation, pointers to the variable names within the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1794 | ** VList are taken. When that happens, nAlloc is set to zero as an |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1795 | ** indication that the VList may never again be enlarged, since the |
| 1796 | ** accompanying realloc() would invalidate the pointers. |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1797 | */ |
| 1798 | VList *sqlite3VListAdd( |
| 1799 | sqlite3 *db, /* The database connection used for malloc() */ |
| 1800 | VList *pIn, /* The input VList. Might be NULL */ |
| 1801 | const char *zName, /* Name of symbol to add */ |
| 1802 | int nName, /* Bytes of text in zName */ |
| 1803 | int iVal /* Value to associate with zName */ |
| 1804 | ){ |
| 1805 | int nInt; /* number of sizeof(int) objects needed for zName */ |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1806 | char *z; /* Pointer to where zName will be stored */ |
| 1807 | int i; /* Index in pIn[] where zName is stored */ |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1808 | |
| 1809 | nInt = nName/4 + 3; |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1810 | assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1811 | if( pIn==0 || pIn[1]+nInt > pIn[0] ){ |
| 1812 | /* Enlarge the allocation */ |
drh | 0aa3231 | 2019-04-13 04:01:12 | [diff] [blame] | 1813 | sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt; |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1814 | VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); |
drh | ce1bbe5 | 2016-12-23 13:52:45 | [diff] [blame] | 1815 | if( pOut==0 ) return pIn; |
drh | 9bf755c | 2016-12-23 03:59:31 | [diff] [blame] | 1816 | if( pIn==0 ) pOut[1] = 2; |
| 1817 | pIn = pOut; |
| 1818 | pIn[0] = nAlloc; |
| 1819 | } |
| 1820 | i = pIn[1]; |
| 1821 | pIn[i] = iVal; |
| 1822 | pIn[i+1] = nInt; |
| 1823 | z = (char*)&pIn[i+2]; |
| 1824 | pIn[1] = i+nInt; |
| 1825 | assert( pIn[1]<=pIn[0] ); |
| 1826 | memcpy(z, zName, nName); |
| 1827 | z[nName] = 0; |
| 1828 | return pIn; |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | ** Return a pointer to the name of a variable in the given VList that |
| 1833 | ** has the value iVal. Or return a NULL if there is no such variable in |
| 1834 | ** the list |
| 1835 | */ |
| 1836 | const char *sqlite3VListNumToName(VList *pIn, int iVal){ |
| 1837 | int i, mx; |
| 1838 | if( pIn==0 ) return 0; |
| 1839 | mx = pIn[1]; |
| 1840 | i = 2; |
| 1841 | do{ |
| 1842 | if( pIn[i]==iVal ) return (char*)&pIn[i+2]; |
| 1843 | i += pIn[i+1]; |
| 1844 | }while( i<mx ); |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | ** Return the number of the variable named zName, if it is in VList. |
| 1850 | ** or return 0 if there is no such variable. |
| 1851 | */ |
| 1852 | int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ |
| 1853 | int i, mx; |
| 1854 | if( pIn==0 ) return 0; |
| 1855 | mx = pIn[1]; |
| 1856 | i = 2; |
| 1857 | do{ |
| 1858 | const char *z = (const char*)&pIn[i+2]; |
| 1859 | if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i]; |
| 1860 | i += pIn[i+1]; |
| 1861 | }while( i<mx ); |
| 1862 | return 0; |
| 1863 | } |