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> |
| 20 | #ifdef SQLITE_HAVE_ISNAN |
| 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 | 85c8f29 | 2010-01-13 17:39:53 +0000 | [diff] [blame] | 34 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 35 | /* |
| 36 | ** Return true if the floating point value is Not a Number (NaN). |
| 37 | ** |
| 38 | ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
| 39 | ** Otherwise, we have our own implementation that works on most systems. |
| 40 | */ |
| 41 | int sqlite3IsNaN(double x){ |
| 42 | int rc; /* The value return */ |
| 43 | #if !defined(SQLITE_HAVE_ISNAN) |
| 44 | /* |
| 45 | ** Systems that support the isnan() library function should probably |
| 46 | ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have |
| 47 | ** found that many systems do not have a working isnan() function so |
| 48 | ** this implementation is provided as an alternative. |
| 49 | ** |
| 50 | ** This NaN test sometimes fails if compiled on GCC with -ffast-math. |
| 51 | ** On the other hand, the use of -ffast-math comes with the following |
| 52 | ** warning: |
| 53 | ** |
| 54 | ** This option [-ffast-math] should never be turned on by any |
| 55 | ** -O option since it can result in incorrect output for programs |
| 56 | ** which depend on an exact implementation of IEEE or ISO |
| 57 | ** rules/specifications for math functions. |
| 58 | ** |
| 59 | ** Under MSVC, this NaN test may fail if compiled with a floating- |
| 60 | ** point precision mode other than /fp:precise. From the MSDN |
| 61 | ** documentation: |
| 62 | ** |
| 63 | ** The compiler [with /fp:precise] will properly handle comparisons |
| 64 | ** involving NaN. For example, x != x evaluates to true if x is NaN |
| 65 | ** ... |
| 66 | */ |
| 67 | #ifdef __FAST_MATH__ |
| 68 | # error SQLite will not work correctly with the -ffast-math option of GCC. |
| 69 | #endif |
| 70 | volatile double y = x; |
| 71 | volatile double z = y; |
| 72 | rc = (y!=z); |
| 73 | #else /* if defined(SQLITE_HAVE_ISNAN) */ |
| 74 | rc = isnan(x); |
| 75 | #endif /* SQLITE_HAVE_ISNAN */ |
| 76 | testcase( rc ); |
| 77 | return rc; |
| 78 | } |
drh | 85c8f29 | 2010-01-13 17:39:53 +0000 | [diff] [blame] | 79 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | ** Compute a string length that is limited to what can be stored in |
| 83 | ** lower 30 bits of a 32-bit signed integer. |
| 84 | ** |
| 85 | ** The value returned will never be negative. Nor will it ever be greater |
| 86 | ** than the actual length of the string. For very long strings (greater |
| 87 | ** than 1GiB) the value returned might be less than the true string length. |
| 88 | */ |
| 89 | int sqlite3Strlen30(const char *z){ |
| 90 | const char *z2 = z; |
| 91 | if( z==0 ) return 0; |
| 92 | while( *z2 ){ z2++; } |
| 93 | return 0x3fffffff & (int)(z2 - z); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | ** Set the most recent error code and error string for the sqlite |
| 98 | ** handle "db". The error code is set to "err_code". |
| 99 | ** |
| 100 | ** If it is not NULL, string zFormat specifies the format of the |
| 101 | ** error string in the style of the printf functions: The following |
| 102 | ** format characters are allowed: |
| 103 | ** |
| 104 | ** %s Insert a string |
| 105 | ** %z A string that should be freed after use |
| 106 | ** %d Insert an integer |
| 107 | ** %T Insert a token |
| 108 | ** %S Insert the first element of a SrcList |
| 109 | ** |
| 110 | ** zFormat and any string tokens that follow it are assumed to be |
| 111 | ** encoded in UTF-8. |
| 112 | ** |
| 113 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| 114 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 115 | ** to NULL. |
| 116 | */ |
| 117 | void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ |
| 118 | if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){ |
| 119 | db->errCode = err_code; |
| 120 | if( zFormat ){ |
| 121 | char *z; |
| 122 | va_list ap; |
| 123 | va_start(ap, zFormat); |
| 124 | z = sqlite3VMPrintf(db, zFormat, ap); |
| 125 | va_end(ap); |
| 126 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 127 | }else{ |
| 128 | sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 135 | ** The following formatting characters are allowed: |
| 136 | ** |
| 137 | ** %s Insert a string |
| 138 | ** %z A string that should be freed after use |
| 139 | ** %d Insert an integer |
| 140 | ** %T Insert a token |
| 141 | ** %S Insert the first element of a SrcList |
| 142 | ** |
| 143 | ** This function should be used to report any error that occurs whilst |
| 144 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 145 | ** last thing the sqlite3_prepare() function does is copy the error |
| 146 | ** stored by this function into the database handle using sqlite3Error(). |
| 147 | ** Function sqlite3Error() should be used during statement execution |
| 148 | ** (sqlite3_step() etc.). |
| 149 | */ |
| 150 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 151 | char *zMsg; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 152 | va_list ap; |
| 153 | sqlite3 *db = pParse->db; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 154 | va_start(ap, zFormat); |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 155 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 156 | va_end(ap); |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 157 | if( db->suppressErr ){ |
| 158 | sqlite3DbFree(db, zMsg); |
| 159 | }else{ |
| 160 | pParse->nErr++; |
| 161 | sqlite3DbFree(db, pParse->zErrMsg); |
| 162 | pParse->zErrMsg = zMsg; |
| 163 | pParse->rc = SQLITE_ERROR; |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 164 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | ** Convert an SQL-style quoted string into a normal string by removing |
| 169 | ** the quote characters. The conversion is done in-place. If the |
| 170 | ** input does not begin with a quote character, then this routine |
| 171 | ** is a no-op. |
| 172 | ** |
| 173 | ** The input string must be zero-terminated. A new zero-terminator |
| 174 | ** is added to the dequoted string. |
| 175 | ** |
| 176 | ** The return value is -1 if no dequoting occurs or the length of the |
| 177 | ** dequoted string, exclusive of the zero terminator, if dequoting does |
| 178 | ** occur. |
| 179 | ** |
| 180 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 181 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 182 | ** "a-b-c". |
| 183 | */ |
| 184 | int sqlite3Dequote(char *z){ |
| 185 | char quote; |
| 186 | int i, j; |
| 187 | if( z==0 ) return -1; |
| 188 | quote = z[0]; |
| 189 | switch( quote ){ |
| 190 | case '\'': break; |
| 191 | case '"': break; |
| 192 | case '`': break; /* For MySQL compatibility */ |
| 193 | case '[': quote = ']'; break; /* For MS SqlServer compatibility */ |
| 194 | default: return -1; |
| 195 | } |
| 196 | for(i=1, j=0; ALWAYS(z[i]); i++){ |
| 197 | if( z[i]==quote ){ |
| 198 | if( z[i+1]==quote ){ |
| 199 | z[j++] = quote; |
| 200 | i++; |
| 201 | }else{ |
| 202 | break; |
| 203 | } |
| 204 | }else{ |
| 205 | z[j++] = z[i]; |
| 206 | } |
| 207 | } |
| 208 | z[j] = 0; |
| 209 | return j; |
| 210 | } |
| 211 | |
| 212 | /* Convenient short-hand */ |
| 213 | #define UpperToLower sqlite3UpperToLower |
| 214 | |
| 215 | /* |
| 216 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 217 | ** there is no consistency, we will define our own. |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 218 | ** |
| 219 | ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows |
| 220 | ** applications and extensions to compare the contents of two buffers |
| 221 | ** containing UTF-8 strings in a case-independent fashion, using the same |
| 222 | ** definition of case independence that SQLite uses internally when |
| 223 | ** comparing identifiers. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 224 | */ |
| 225 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 226 | register unsigned char *a, *b; |
| 227 | a = (unsigned char *)zLeft; |
| 228 | b = (unsigned char *)zRight; |
| 229 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 230 | return UpperToLower[*a] - UpperToLower[*b]; |
| 231 | } |
| 232 | int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 233 | register unsigned char *a, *b; |
| 234 | a = (unsigned char *)zLeft; |
| 235 | b = (unsigned char *)zRight; |
| 236 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 237 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 238 | } |
| 239 | |
| 240 | /* |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 241 | ** The string z[] is an text representation of a real number. |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 242 | ** Convert this string to a double and write it into *pResult. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 243 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 244 | ** The string z[] is length bytes in length (bytes, not characters) and |
| 245 | ** uses the encoding enc. The string is not necessarily zero-terminated. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 246 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 247 | ** 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] | 248 | ** if the string is empty or contains extraneous text. Valid numbers |
| 249 | ** are in one of these formats: |
| 250 | ** |
| 251 | ** [+-]digits[E[+-]digits] |
| 252 | ** [+-]digits.[digits][E[+-]digits] |
| 253 | ** [+-].digits[E[+-]digits] |
| 254 | ** |
| 255 | ** Leading and trailing whitespace is ignored for the purpose of determining |
| 256 | ** validity. |
| 257 | ** |
| 258 | ** If some prefix of the input string is a valid number, this routine |
| 259 | ** returns FALSE but it still converts the prefix and writes the result |
| 260 | ** into *pResult. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 261 | */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 262 | int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 263 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 264 | int incr = (enc==SQLITE_UTF8?1:2); |
| 265 | const char *zEnd = z + length; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 266 | /* sign * significand * (10 ^ (esign * exponent)) */ |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 267 | int sign = 1; /* sign of significand */ |
| 268 | i64 s = 0; /* significand */ |
| 269 | int d = 0; /* adjust exponent for shifting decimal point */ |
| 270 | int esign = 1; /* sign of exponent */ |
| 271 | int e = 0; /* exponent */ |
| 272 | int eValid = 1; /* True exponent is either not used or is well-formed */ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 273 | double result; |
| 274 | int nDigits = 0; |
| 275 | |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 276 | *pResult = 0.0; /* Default return value, in case of an error */ |
| 277 | |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 278 | if( enc==SQLITE_UTF16BE ) z++; |
| 279 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 280 | /* skip leading spaces */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 281 | while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 282 | if( z>=zEnd ) return 0; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 283 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 284 | /* get sign of significand */ |
| 285 | if( *z=='-' ){ |
| 286 | sign = -1; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 287 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 288 | }else if( *z=='+' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 289 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 290 | } |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 291 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 292 | /* skip leading zeroes */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 293 | while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 294 | |
| 295 | /* copy max significant digits to significand */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 296 | while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 297 | s = s*10 + (*z - '0'); |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 298 | z+=incr, nDigits++; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 299 | } |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 300 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 301 | /* skip non-significant significand digits |
| 302 | ** (increase exponent by d to shift decimal left) */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 303 | while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; |
| 304 | if( z>=zEnd ) goto do_atof_calc; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 305 | |
| 306 | /* if decimal point is present */ |
| 307 | if( *z=='.' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 308 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 309 | /* copy digits from after decimal to significand |
| 310 | ** (decrease exponent by d to shift decimal right) */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 311 | while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 312 | s = s*10 + (*z - '0'); |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 313 | z+=incr, nDigits++, d--; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 314 | } |
| 315 | /* skip non-significant digits */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 316 | while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 317 | } |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 318 | if( z>=zEnd ) goto do_atof_calc; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 319 | |
| 320 | /* if exponent is present */ |
| 321 | if( *z=='e' || *z=='E' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 322 | z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 323 | eValid = 0; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 324 | if( z>=zEnd ) goto do_atof_calc; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 325 | /* get sign of exponent */ |
| 326 | if( *z=='-' ){ |
| 327 | esign = -1; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 328 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 329 | }else if( *z=='+' ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 330 | z+=incr; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 331 | } |
| 332 | /* copy digits to exponent */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 333 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 334 | e = e*10 + (*z - '0'); |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 335 | z+=incr; |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 336 | eValid = 1; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 340 | /* skip trailing spaces */ |
| 341 | if( nDigits && eValid ){ |
| 342 | while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 343 | } |
| 344 | |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 345 | do_atof_calc: |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 346 | /* adjust exponent by d, and update sign */ |
| 347 | e = (e*esign) + d; |
| 348 | if( e<0 ) { |
| 349 | esign = -1; |
| 350 | e *= -1; |
| 351 | } else { |
| 352 | esign = 1; |
| 353 | } |
| 354 | |
| 355 | /* if 0 significand */ |
| 356 | if( !s ) { |
| 357 | /* In the IEEE 754 standard, zero is signed. |
| 358 | ** Add the sign if we've seen at least one digit */ |
| 359 | result = (sign<0 && nDigits) ? -(double)0 : (double)0; |
| 360 | } else { |
| 361 | /* attempt to reduce exponent */ |
| 362 | if( esign>0 ){ |
| 363 | while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; |
| 364 | }else{ |
| 365 | while( !(s%10) && e>0 ) e--,s/=10; |
| 366 | } |
| 367 | |
| 368 | /* adjust the sign of significand */ |
| 369 | s = sign<0 ? -s : s; |
| 370 | |
| 371 | /* if exponent, scale significand as appropriate |
| 372 | ** and store in result. */ |
| 373 | if( e ){ |
| 374 | double scale = 1.0; |
| 375 | /* attempt to handle extremely small/large numbers better */ |
| 376 | if( e>307 && e<342 ){ |
| 377 | while( e%308 ) { scale *= 1.0e+1; e -= 1; } |
| 378 | if( esign<0 ){ |
| 379 | result = s / scale; |
| 380 | result /= 1.0e+308; |
| 381 | }else{ |
| 382 | result = s * scale; |
| 383 | result *= 1.0e+308; |
| 384 | } |
| 385 | }else{ |
| 386 | /* 1.0e+22 is the largest power of 10 than can be |
| 387 | ** represented exactly. */ |
| 388 | while( e%22 ) { scale *= 1.0e+1; e -= 1; } |
| 389 | while( e>0 ) { scale *= 1.0e+22; e -= 22; } |
| 390 | if( esign<0 ){ |
| 391 | result = s / scale; |
| 392 | }else{ |
| 393 | result = s * scale; |
| 394 | } |
| 395 | } |
| 396 | } else { |
| 397 | result = (double)s; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* store the result */ |
| 402 | *pResult = result; |
| 403 | |
drh | 025586a | 2010-09-30 17:33:11 +0000 | [diff] [blame] | 404 | /* return true if number and no extra non-whitespace chracters after */ |
| 405 | return z>=zEnd && nDigits>0 && eValid; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 406 | #else |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 407 | return !sqlite3Atoi64(z, pResult, length, enc); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 408 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | ** Compare the 19-character string zNum against the text representation |
| 413 | ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| 414 | ** if zNum is less than, equal to, or greater than the string. |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 415 | ** Note that zNum must contain exactly 19 characters. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 416 | ** |
| 417 | ** Unlike memcmp() this routine is guaranteed to return the difference |
| 418 | ** in the values of the last digit if the only difference is in the |
| 419 | ** last digit. So, for example, |
| 420 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 421 | ** compare2pow63("9223372036854775800", 1) |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 422 | ** |
| 423 | ** will return -8. |
| 424 | */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 425 | static int compare2pow63(const char *zNum, int incr){ |
| 426 | int c = 0; |
| 427 | int i; |
| 428 | /* 012345678901234567 */ |
| 429 | const char *pow63 = "922337203685477580"; |
| 430 | for(i=0; c==0 && i<18; i++){ |
| 431 | c = (zNum[i*incr]-pow63[i])*10; |
| 432 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 433 | if( c==0 ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 434 | c = zNum[18*incr] - '8'; |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 435 | testcase( c==(-1) ); |
| 436 | testcase( c==0 ); |
| 437 | testcase( c==(+1) ); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 438 | } |
| 439 | return c; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /* |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 444 | ** Convert zNum to a 64-bit signed integer. |
| 445 | ** |
| 446 | ** If the zNum value is representable as a 64-bit twos-complement |
| 447 | ** integer, then write that value into *pNum and return 0. |
| 448 | ** |
| 449 | ** If zNum is exactly 9223372036854665808, return 2. This special |
| 450 | ** case is broken out because while 9223372036854665808 cannot be a |
| 451 | ** signed 64-bit integer, its negative -9223372036854665808 can be. |
| 452 | ** |
| 453 | ** If zNum is too big for a 64-bit integer and is not |
| 454 | ** 9223372036854665808 then return 1. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 455 | ** |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 456 | ** length is the number of bytes in the string (bytes, not characters). |
| 457 | ** The string is not necessarily zero-terminated. The encoding is |
| 458 | ** given by enc. |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 459 | */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 460 | int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
| 461 | int incr = (enc==SQLITE_UTF8?1:2); |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 462 | u64 u = 0; |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 463 | int neg = 0; /* assume positive */ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 464 | int i; |
| 465 | int c = 0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 466 | const char *zStart; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 467 | const char *zEnd = zNum + length; |
| 468 | if( enc==SQLITE_UTF16BE ) zNum++; |
| 469 | while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 470 | if( zNum<zEnd ){ |
| 471 | if( *zNum=='-' ){ |
| 472 | neg = 1; |
| 473 | zNum+=incr; |
| 474 | }else if( *zNum=='+' ){ |
| 475 | zNum+=incr; |
| 476 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 477 | } |
| 478 | zStart = zNum; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 479 | while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
| 480 | 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] | 481 | u = u*10 + c - '0'; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 482 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 483 | if( u>LARGEST_INT64 ){ |
| 484 | *pNum = SMALLEST_INT64; |
| 485 | }else if( neg ){ |
| 486 | *pNum = -(i64)u; |
| 487 | }else{ |
| 488 | *pNum = (i64)u; |
| 489 | } |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 490 | testcase( i==18 ); |
| 491 | testcase( i==19 ); |
| 492 | testcase( i==20 ); |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 493 | if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 494 | /* zNum is empty or contains non-numeric text or is longer |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 495 | ** than 19 digits (thus guaranteeing that it is too large) */ |
| 496 | return 1; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 497 | }else if( i<19*incr ){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 498 | /* Less than 19 digits, so we know that it fits in 64 bits */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 499 | assert( u<=LARGEST_INT64 ); |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 500 | return 0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 501 | }else{ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 502 | /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
| 503 | c = compare2pow63(zNum, incr); |
| 504 | if( c<0 ){ |
| 505 | /* zNum is less than 9223372036854775808 so it fits */ |
| 506 | assert( u<=LARGEST_INT64 ); |
| 507 | return 0; |
| 508 | }else if( c>0 ){ |
| 509 | /* zNum is greater than 9223372036854775808 so it overflows */ |
| 510 | return 1; |
| 511 | }else{ |
| 512 | /* zNum is exactly 9223372036854775808. Fits if negative. The |
| 513 | ** special case 2 overflow if positive */ |
| 514 | assert( u-1==LARGEST_INT64 ); |
| 515 | assert( (*pNum)==SMALLEST_INT64 ); |
| 516 | return neg ? 0 : 2; |
| 517 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 523 | ** *pValue to that integer and return true. Otherwise return false. |
| 524 | ** |
| 525 | ** Any non-numeric characters that following zNum are ignored. |
| 526 | ** This is different from sqlite3Atoi64() which requires the |
| 527 | ** input number to be zero-terminated. |
| 528 | */ |
| 529 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 530 | sqlite_int64 v = 0; |
| 531 | int i, c; |
| 532 | int neg = 0; |
| 533 | if( zNum[0]=='-' ){ |
| 534 | neg = 1; |
| 535 | zNum++; |
| 536 | }else if( zNum[0]=='+' ){ |
| 537 | zNum++; |
| 538 | } |
| 539 | while( zNum[0]=='0' ) zNum++; |
| 540 | for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
| 541 | v = v*10 + c; |
| 542 | } |
| 543 | |
| 544 | /* The longest decimal representation of a 32 bit integer is 10 digits: |
| 545 | ** |
| 546 | ** 1234567890 |
| 547 | ** 2^31 -> 2147483648 |
| 548 | */ |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 549 | testcase( i==10 ); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 550 | if( i>10 ){ |
| 551 | return 0; |
| 552 | } |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 553 | testcase( v-neg==2147483647 ); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 554 | if( v-neg>2147483647 ){ |
| 555 | return 0; |
| 556 | } |
| 557 | if( neg ){ |
| 558 | v = -v; |
| 559 | } |
| 560 | *pValue = (int)v; |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | /* |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 565 | ** Return a 32-bit integer value extracted from a string. If the |
| 566 | ** string is not an integer, just return 0. |
| 567 | */ |
| 568 | int sqlite3Atoi(const char *z){ |
| 569 | int x = 0; |
| 570 | if( z ) sqlite3GetInt32(z, &x); |
| 571 | return x; |
| 572 | } |
| 573 | |
| 574 | /* |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 575 | ** The variable-length integer encoding is as follows: |
| 576 | ** |
| 577 | ** KEY: |
| 578 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 579 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 580 | ** C = xxxxxxxx 8 bits of data |
| 581 | ** |
| 582 | ** 7 bits - A |
| 583 | ** 14 bits - BA |
| 584 | ** 21 bits - BBA |
| 585 | ** 28 bits - BBBA |
| 586 | ** 35 bits - BBBBA |
| 587 | ** 42 bits - BBBBBA |
| 588 | ** 49 bits - BBBBBBA |
| 589 | ** 56 bits - BBBBBBBA |
| 590 | ** 64 bits - BBBBBBBBC |
| 591 | */ |
| 592 | |
| 593 | /* |
| 594 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 595 | ** The length of data write will be between 1 and 9 bytes. The number |
| 596 | ** of bytes written is returned. |
| 597 | ** |
| 598 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 599 | ** for all bytes that have the 8th bit set and one byte with the 8th |
| 600 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 601 | ** 8 bits and is the last byte. |
| 602 | */ |
| 603 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
| 604 | int i, j, n; |
| 605 | u8 buf[10]; |
| 606 | if( v & (((u64)0xff000000)<<32) ){ |
| 607 | p[8] = (u8)v; |
| 608 | v >>= 8; |
| 609 | for(i=7; i>=0; i--){ |
| 610 | p[i] = (u8)((v & 0x7f) | 0x80); |
| 611 | v >>= 7; |
| 612 | } |
| 613 | return 9; |
| 614 | } |
| 615 | n = 0; |
| 616 | do{ |
| 617 | buf[n++] = (u8)((v & 0x7f) | 0x80); |
| 618 | v >>= 7; |
| 619 | }while( v!=0 ); |
| 620 | buf[0] &= 0x7f; |
| 621 | assert( n<=9 ); |
| 622 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 623 | p[i] = buf[j]; |
| 624 | } |
| 625 | return n; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | ** This routine is a faster version of sqlite3PutVarint() that only |
| 630 | ** works for 32-bit positive integers and which is optimized for |
| 631 | ** the common case of small integers. A MACRO version, putVarint32, |
| 632 | ** is provided which inlines the single-byte case. All code should use |
| 633 | ** the MACRO version as this function assumes the single-byte case has |
| 634 | ** already been handled. |
| 635 | */ |
| 636 | int sqlite3PutVarint32(unsigned char *p, u32 v){ |
| 637 | #ifndef putVarint32 |
| 638 | if( (v & ~0x7f)==0 ){ |
| 639 | p[0] = v; |
| 640 | return 1; |
| 641 | } |
| 642 | #endif |
| 643 | if( (v & ~0x3fff)==0 ){ |
| 644 | p[0] = (u8)((v>>7) | 0x80); |
| 645 | p[1] = (u8)(v & 0x7f); |
| 646 | return 2; |
| 647 | } |
| 648 | return sqlite3PutVarint(p, v); |
| 649 | } |
| 650 | |
| 651 | /* |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 652 | ** Bitmasks used by sqlite3GetVarint(). These precomputed constants |
| 653 | ** are defined here rather than simply putting the constant expressions |
| 654 | ** inline in order to work around bugs in the RVT compiler. |
| 655 | ** |
| 656 | ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f |
| 657 | ** |
| 658 | ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 |
| 659 | */ |
| 660 | #define SLOT_2_0 0x001fc07f |
| 661 | #define SLOT_4_2_0 0xf01fc07f |
| 662 | |
| 663 | |
| 664 | /* |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 665 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 666 | ** Return the number of bytes read. The value is stored in *v. |
| 667 | */ |
| 668 | u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ |
| 669 | u32 a,b,s; |
| 670 | |
| 671 | a = *p; |
| 672 | /* a: p0 (unmasked) */ |
| 673 | if (!(a&0x80)) |
| 674 | { |
| 675 | *v = a; |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | p++; |
| 680 | b = *p; |
| 681 | /* b: p1 (unmasked) */ |
| 682 | if (!(b&0x80)) |
| 683 | { |
| 684 | a &= 0x7f; |
| 685 | a = a<<7; |
| 686 | a |= b; |
| 687 | *v = a; |
| 688 | return 2; |
| 689 | } |
| 690 | |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 691 | /* Verify that constants are precomputed correctly */ |
| 692 | assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); |
shaneh | 1da207e | 2010-03-09 14:41:12 +0000 | [diff] [blame] | 693 | assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 694 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 695 | p++; |
| 696 | a = a<<14; |
| 697 | a |= *p; |
| 698 | /* a: p0<<14 | p2 (unmasked) */ |
| 699 | if (!(a&0x80)) |
| 700 | { |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 701 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 702 | b &= 0x7f; |
| 703 | b = b<<7; |
| 704 | a |= b; |
| 705 | *v = a; |
| 706 | return 3; |
| 707 | } |
| 708 | |
| 709 | /* CSE1 from below */ |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 710 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 711 | p++; |
| 712 | b = b<<14; |
| 713 | b |= *p; |
| 714 | /* b: p1<<14 | p3 (unmasked) */ |
| 715 | if (!(b&0x80)) |
| 716 | { |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 717 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 718 | /* moved CSE1 up */ |
| 719 | /* a &= (0x7f<<14)|(0x7f); */ |
| 720 | a = a<<7; |
| 721 | a |= b; |
| 722 | *v = a; |
| 723 | return 4; |
| 724 | } |
| 725 | |
| 726 | /* a: p0<<14 | p2 (masked) */ |
| 727 | /* b: p1<<14 | p3 (unmasked) */ |
| 728 | /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 729 | /* moved CSE1 up */ |
| 730 | /* a &= (0x7f<<14)|(0x7f); */ |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 731 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 732 | s = a; |
| 733 | /* s: p0<<14 | p2 (masked) */ |
| 734 | |
| 735 | p++; |
| 736 | a = a<<14; |
| 737 | a |= *p; |
| 738 | /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| 739 | if (!(a&0x80)) |
| 740 | { |
| 741 | /* we can skip these cause they were (effectively) done above in calc'ing s */ |
| 742 | /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 743 | /* b &= (0x7f<<14)|(0x7f); */ |
| 744 | b = b<<7; |
| 745 | a |= b; |
| 746 | s = s>>18; |
| 747 | *v = ((u64)s)<<32 | a; |
| 748 | return 5; |
| 749 | } |
| 750 | |
| 751 | /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 752 | s = s<<7; |
| 753 | s |= b; |
| 754 | /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 755 | |
| 756 | p++; |
| 757 | b = b<<14; |
| 758 | b |= *p; |
| 759 | /* b: p1<<28 | p3<<14 | p5 (unmasked) */ |
| 760 | if (!(b&0x80)) |
| 761 | { |
| 762 | /* we can skip this cause it was (effectively) done above in calc'ing s */ |
| 763 | /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 764 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 765 | a = a<<7; |
| 766 | a |= b; |
| 767 | s = s>>18; |
| 768 | *v = ((u64)s)<<32 | a; |
| 769 | return 6; |
| 770 | } |
| 771 | |
| 772 | p++; |
| 773 | a = a<<14; |
| 774 | a |= *p; |
| 775 | /* a: p2<<28 | p4<<14 | p6 (unmasked) */ |
| 776 | if (!(a&0x80)) |
| 777 | { |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 778 | a &= SLOT_4_2_0; |
| 779 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 780 | b = b<<7; |
| 781 | a |= b; |
| 782 | s = s>>11; |
| 783 | *v = ((u64)s)<<32 | a; |
| 784 | return 7; |
| 785 | } |
| 786 | |
| 787 | /* CSE2 from below */ |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 788 | a &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 789 | p++; |
| 790 | b = b<<14; |
| 791 | b |= *p; |
| 792 | /* b: p3<<28 | p5<<14 | p7 (unmasked) */ |
| 793 | if (!(b&0x80)) |
| 794 | { |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 795 | b &= SLOT_4_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 796 | /* moved CSE2 up */ |
| 797 | /* a &= (0x7f<<14)|(0x7f); */ |
| 798 | a = a<<7; |
| 799 | a |= b; |
| 800 | s = s>>4; |
| 801 | *v = ((u64)s)<<32 | a; |
| 802 | return 8; |
| 803 | } |
| 804 | |
| 805 | p++; |
| 806 | a = a<<15; |
| 807 | a |= *p; |
| 808 | /* a: p4<<29 | p6<<15 | p8 (unmasked) */ |
| 809 | |
| 810 | /* moved CSE2 up */ |
| 811 | /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ |
drh | 0b2864c | 2010-03-03 15:18:38 +0000 | [diff] [blame] | 812 | b &= SLOT_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 813 | b = b<<8; |
| 814 | a |= b; |
| 815 | |
| 816 | s = s<<4; |
| 817 | b = p[-4]; |
| 818 | b &= 0x7f; |
| 819 | b = b>>3; |
| 820 | s |= b; |
| 821 | |
| 822 | *v = ((u64)s)<<32 | a; |
| 823 | |
| 824 | return 9; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 829 | ** Return the number of bytes read. The value is stored in *v. |
| 830 | ** |
| 831 | ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned |
| 832 | ** integer, then set *v to 0xffffffff. |
| 833 | ** |
| 834 | ** A MACRO version, getVarint32, is provided which inlines the |
| 835 | ** single-byte case. All code should use the MACRO version as |
| 836 | ** this function assumes the single-byte case has already been handled. |
| 837 | */ |
| 838 | u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
| 839 | u32 a,b; |
| 840 | |
| 841 | /* The 1-byte case. Overwhelmingly the most common. Handled inline |
| 842 | ** by the getVarin32() macro */ |
| 843 | a = *p; |
| 844 | /* a: p0 (unmasked) */ |
| 845 | #ifndef getVarint32 |
| 846 | if (!(a&0x80)) |
| 847 | { |
| 848 | /* Values between 0 and 127 */ |
| 849 | *v = a; |
| 850 | return 1; |
| 851 | } |
| 852 | #endif |
| 853 | |
| 854 | /* The 2-byte case */ |
| 855 | p++; |
| 856 | b = *p; |
| 857 | /* b: p1 (unmasked) */ |
| 858 | if (!(b&0x80)) |
| 859 | { |
| 860 | /* Values between 128 and 16383 */ |
| 861 | a &= 0x7f; |
| 862 | a = a<<7; |
| 863 | *v = a | b; |
| 864 | return 2; |
| 865 | } |
| 866 | |
| 867 | /* The 3-byte case */ |
| 868 | p++; |
| 869 | a = a<<14; |
| 870 | a |= *p; |
| 871 | /* a: p0<<14 | p2 (unmasked) */ |
| 872 | if (!(a&0x80)) |
| 873 | { |
| 874 | /* Values between 16384 and 2097151 */ |
| 875 | a &= (0x7f<<14)|(0x7f); |
| 876 | b &= 0x7f; |
| 877 | b = b<<7; |
| 878 | *v = a | b; |
| 879 | return 3; |
| 880 | } |
| 881 | |
| 882 | /* A 32-bit varint is used to store size information in btrees. |
| 883 | ** Objects are rarely larger than 2MiB limit of a 3-byte varint. |
| 884 | ** A 3-byte varint is sufficient, for example, to record the size |
| 885 | ** of a 1048569-byte BLOB or string. |
| 886 | ** |
| 887 | ** We only unroll the first 1-, 2-, and 3- byte cases. The very |
| 888 | ** rare larger cases can be handled by the slower 64-bit varint |
| 889 | ** routine. |
| 890 | */ |
| 891 | #if 1 |
| 892 | { |
| 893 | u64 v64; |
| 894 | u8 n; |
| 895 | |
| 896 | p -= 2; |
| 897 | n = sqlite3GetVarint(p, &v64); |
| 898 | assert( n>3 && n<=9 ); |
| 899 | if( (v64 & SQLITE_MAX_U32)!=v64 ){ |
| 900 | *v = 0xffffffff; |
| 901 | }else{ |
| 902 | *v = (u32)v64; |
| 903 | } |
| 904 | return n; |
| 905 | } |
| 906 | |
| 907 | #else |
| 908 | /* For following code (kept for historical record only) shows an |
| 909 | ** unrolling for the 3- and 4-byte varint cases. This code is |
| 910 | ** slightly faster, but it is also larger and much harder to test. |
| 911 | */ |
| 912 | p++; |
| 913 | b = b<<14; |
| 914 | b |= *p; |
| 915 | /* b: p1<<14 | p3 (unmasked) */ |
| 916 | if (!(b&0x80)) |
| 917 | { |
| 918 | /* Values between 2097152 and 268435455 */ |
| 919 | b &= (0x7f<<14)|(0x7f); |
| 920 | a &= (0x7f<<14)|(0x7f); |
| 921 | a = a<<7; |
| 922 | *v = a | b; |
| 923 | return 4; |
| 924 | } |
| 925 | |
| 926 | p++; |
| 927 | a = a<<14; |
| 928 | a |= *p; |
| 929 | /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| 930 | if (!(a&0x80)) |
| 931 | { |
dan | 3bbe761 | 2010-03-03 16:02:05 +0000 | [diff] [blame] | 932 | /* Values between 268435456 and 34359738367 */ |
| 933 | a &= SLOT_4_2_0; |
| 934 | b &= SLOT_4_2_0; |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 935 | b = b<<7; |
| 936 | *v = a | b; |
| 937 | return 5; |
| 938 | } |
| 939 | |
| 940 | /* We can only reach this point when reading a corrupt database |
| 941 | ** file. In that case we are not in any hurry. Use the (relatively |
| 942 | ** slow) general-purpose sqlite3GetVarint() routine to extract the |
| 943 | ** value. */ |
| 944 | { |
| 945 | u64 v64; |
| 946 | u8 n; |
| 947 | |
| 948 | p -= 4; |
| 949 | n = sqlite3GetVarint(p, &v64); |
| 950 | assert( n>5 && n<=9 ); |
| 951 | *v = (u32)v64; |
| 952 | return n; |
| 953 | } |
| 954 | #endif |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | ** Return the number of bytes that will be needed to store the given |
| 959 | ** 64-bit integer. |
| 960 | */ |
| 961 | int sqlite3VarintLen(u64 v){ |
| 962 | int i = 0; |
| 963 | do{ |
| 964 | i++; |
| 965 | v >>= 7; |
| 966 | }while( v!=0 && ALWAYS(i<9) ); |
| 967 | return i; |
| 968 | } |
| 969 | |
| 970 | |
| 971 | /* |
| 972 | ** Read or write a four-byte big-endian integer value. |
| 973 | */ |
| 974 | u32 sqlite3Get4byte(const u8 *p){ |
| 975 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 976 | } |
| 977 | void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 978 | p[0] = (u8)(v>>24); |
| 979 | p[1] = (u8)(v>>16); |
| 980 | p[2] = (u8)(v>>8); |
| 981 | p[3] = (u8)v; |
| 982 | } |
| 983 | |
| 984 | |
| 985 | |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 986 | /* |
| 987 | ** Translate a single byte of Hex into an integer. |
| 988 | ** This routine only works if h really is a valid hexadecimal |
| 989 | ** character: 0..9a..fA..F |
| 990 | */ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame^] | 991 | u8 sqlite3HexToInt(int h){ |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 992 | assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 993 | #ifdef SQLITE_ASCII |
| 994 | h += 9*(1&(h>>6)); |
| 995 | #endif |
| 996 | #ifdef SQLITE_EBCDIC |
| 997 | h += 9*(1&~(h>>4)); |
| 998 | #endif |
| 999 | return (u8)(h & 0xf); |
| 1000 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1001 | |
| 1002 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
| 1003 | /* |
| 1004 | ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 1005 | ** value. Return a pointer to its binary value. Space to hold the |
| 1006 | ** binary value has been obtained from malloc and must be freed by |
| 1007 | ** the calling routine. |
| 1008 | */ |
| 1009 | void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
| 1010 | char *zBlob; |
| 1011 | int i; |
| 1012 | |
| 1013 | zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); |
| 1014 | n--; |
| 1015 | if( zBlob ){ |
| 1016 | for(i=0; i<n; i+=2){ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame^] | 1017 | zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1018 | } |
| 1019 | zBlob[i/2] = 0; |
| 1020 | } |
| 1021 | return zBlob; |
| 1022 | } |
| 1023 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
| 1024 | |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | ** Log an error that is an API call on a connection pointer that should |
| 1027 | ** not have been used. The "type" of connection pointer is given as the |
| 1028 | ** argument. The zType is a word like "NULL" or "closed" or "invalid". |
| 1029 | */ |
| 1030 | static void logBadConnection(const char *zType){ |
| 1031 | sqlite3_log(SQLITE_MISUSE, |
| 1032 | "API call with %s database connection pointer", |
| 1033 | zType |
| 1034 | ); |
| 1035 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1038 | ** Check to make sure we have a valid db pointer. This test is not |
| 1039 | ** foolproof but it does provide some measure of protection against |
| 1040 | ** misuse of the interface such as passing in db pointers that are |
| 1041 | ** NULL or which have been previously closed. If this routine returns |
| 1042 | ** 1 it means that the db pointer is valid and 0 if it should not be |
| 1043 | ** dereferenced for any reason. The calling function should invoke |
| 1044 | ** SQLITE_MISUSE immediately. |
| 1045 | ** |
| 1046 | ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| 1047 | ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 1048 | ** open properly and is not fit for general use but which can be |
| 1049 | ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| 1050 | */ |
| 1051 | int sqlite3SafetyCheckOk(sqlite3 *db){ |
| 1052 | u32 magic; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1053 | if( db==0 ){ |
| 1054 | logBadConnection("NULL"); |
| 1055 | return 0; |
| 1056 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1057 | magic = db->magic; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 1058 | if( magic!=SQLITE_MAGIC_OPEN ){ |
drh | e294da0 | 2010-02-25 23:44:15 +0000 | [diff] [blame] | 1059 | if( sqlite3SafetyCheckSickOrOk(db) ){ |
| 1060 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1061 | logBadConnection("unopened"); |
| 1062 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1063 | return 0; |
| 1064 | }else{ |
| 1065 | return 1; |
| 1066 | } |
| 1067 | } |
| 1068 | int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
| 1069 | u32 magic; |
| 1070 | magic = db->magic; |
| 1071 | if( magic!=SQLITE_MAGIC_SICK && |
| 1072 | magic!=SQLITE_MAGIC_OPEN && |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1073 | magic!=SQLITE_MAGIC_BUSY ){ |
drh | e294da0 | 2010-02-25 23:44:15 +0000 | [diff] [blame] | 1074 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 1075 | logBadConnection("invalid"); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1076 | return 0; |
| 1077 | }else{ |
| 1078 | return 1; |
| 1079 | } |
drh | c81c11f | 2009-11-10 01:30:52 +0000 | [diff] [blame] | 1080 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* |
| 1083 | ** Attempt to add, substract, or multiply the 64-bit signed value iB against |
| 1084 | ** the other 64-bit signed integer at *pA and store the result in *pA. |
| 1085 | ** Return 0 on success. Or if the operation would have resulted in an |
| 1086 | ** overflow, leave *pA unchanged and return 1. |
| 1087 | */ |
| 1088 | int sqlite3AddInt64(i64 *pA, i64 iB){ |
| 1089 | i64 iA = *pA; |
| 1090 | testcase( iA==0 ); testcase( iA==1 ); |
| 1091 | testcase( iB==-1 ); testcase( iB==0 ); |
| 1092 | if( iB>=0 ){ |
| 1093 | testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
| 1094 | testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
| 1095 | if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
| 1096 | *pA += iB; |
| 1097 | }else{ |
| 1098 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
| 1099 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
| 1100 | if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
| 1101 | *pA += iB; |
| 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | int sqlite3SubInt64(i64 *pA, i64 iB){ |
| 1106 | testcase( iB==SMALLEST_INT64+1 ); |
| 1107 | if( iB==SMALLEST_INT64 ){ |
| 1108 | testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
| 1109 | if( (*pA)>=0 ) return 1; |
| 1110 | *pA -= iB; |
| 1111 | return 0; |
| 1112 | }else{ |
| 1113 | return sqlite3AddInt64(pA, -iB); |
| 1114 | } |
| 1115 | } |
| 1116 | #define TWOPOWER32 (((i64)1)<<32) |
| 1117 | #define TWOPOWER31 (((i64)1)<<31) |
| 1118 | int sqlite3MulInt64(i64 *pA, i64 iB){ |
| 1119 | i64 iA = *pA; |
| 1120 | i64 iA1, iA0, iB1, iB0, r; |
| 1121 | |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1122 | iA1 = iA/TWOPOWER32; |
| 1123 | iA0 = iA % TWOPOWER32; |
| 1124 | iB1 = iB/TWOPOWER32; |
| 1125 | iB0 = iB % TWOPOWER32; |
| 1126 | if( iA1*iB1 != 0 ) return 1; |
drh | d7255a2 | 2011-03-05 21:41:34 +0000 | [diff] [blame] | 1127 | assert( iA1*iB0==0 || iA0*iB1==0 ); |
| 1128 | r = iA1*iB0 + iA0*iB1; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1129 | testcase( r==(-TWOPOWER31)-1 ); |
| 1130 | testcase( r==(-TWOPOWER31) ); |
| 1131 | testcase( r==TWOPOWER31 ); |
| 1132 | testcase( r==TWOPOWER31-1 ); |
| 1133 | if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; |
| 1134 | r *= TWOPOWER32; |
| 1135 | if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; |
| 1136 | *pA = r; |
| 1137 | return 0; |
| 1138 | } |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* |
| 1141 | ** Compute the absolute value of a 32-bit signed integer, of possible. Or |
| 1142 | ** if the integer has a value of -2147483648, return +2147483647 |
| 1143 | */ |
| 1144 | int sqlite3AbsInt32(int x){ |
| 1145 | if( x>=0 ) return x; |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1146 | if( x==(int)0x80000000 ) return 0x7fffffff; |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 1147 | return -x; |
| 1148 | } |