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