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