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