blob: 50ffd98650bf862985cd453ace8830063847ebf9 [file] [log] [blame]
drhc81c11f2009-11-10 01:30:52 +00001/*
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
28void sqlite3Coverage(int x){
drh68bf0672011-04-11 15:35:24 +000029 static unsigned dummy = 0;
30 dummy += (unsigned)x;
drhc81c11f2009-11-10 01:30:52 +000031}
32#endif
33
drh85c8f292010-01-13 17:39:53 +000034#ifndef SQLITE_OMIT_FLOATING_POINT
drhc81c11f2009-11-10 01:30:52 +000035/*
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*/
41int 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}
drh85c8f292010-01-13 17:39:53 +000079#endif /* SQLITE_OMIT_FLOATING_POINT */
drhc81c11f2009-11-10 01:30:52 +000080
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*/
89int 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*/
117void 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*/
150void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drha7564662010-02-22 19:32:31 +0000151 char *zMsg;
drhc81c11f2009-11-10 01:30:52 +0000152 va_list ap;
153 sqlite3 *db = pParse->db;
drhc81c11f2009-11-10 01:30:52 +0000154 va_start(ap, zFormat);
drha7564662010-02-22 19:32:31 +0000155 zMsg = sqlite3VMPrintf(db, zFormat, ap);
drhc81c11f2009-11-10 01:30:52 +0000156 va_end(ap);
drha7564662010-02-22 19:32:31 +0000157 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;
drha7564662010-02-22 19:32:31 +0000164 }
drhc81c11f2009-11-10 01:30:52 +0000165}
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*/
184int 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 }
drh9ccd8652013-09-13 16:36:46 +0000196 for(i=1, j=0;; i++){
197 assert( z[i] );
drhc81c11f2009-11-10 01:30:52 +0000198 if( z[i]==quote ){
199 if( z[i+1]==quote ){
200 z[j++] = quote;
201 i++;
202 }else{
203 break;
204 }
205 }else{
206 z[j++] = z[i];
207 }
208 }
209 z[j] = 0;
210 return j;
211}
212
213/* Convenient short-hand */
214#define UpperToLower sqlite3UpperToLower
215
216/*
217** Some systems have stricmp(). Others have strcasecmp(). Because
218** there is no consistency, we will define our own.
drh9f129f42010-08-31 15:27:32 +0000219**
drh0299b402012-03-19 17:42:46 +0000220** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
221** sqlite3_strnicmp() APIs allow applications and extensions to compare
222** the contents of two buffers containing UTF-8 strings in a
223** case-independent fashion, using the same definition of "case
224** independence" that SQLite uses internally when comparing identifiers.
drhc81c11f2009-11-10 01:30:52 +0000225*/
drh3fa97302012-02-22 16:58:36 +0000226int sqlite3_stricmp(const char *zLeft, const char *zRight){
drhc81c11f2009-11-10 01:30:52 +0000227 register unsigned char *a, *b;
228 a = (unsigned char *)zLeft;
229 b = (unsigned char *)zRight;
230 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
231 return UpperToLower[*a] - UpperToLower[*b];
232}
233int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
234 register unsigned char *a, *b;
235 a = (unsigned char *)zLeft;
236 b = (unsigned char *)zRight;
237 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
238 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
239}
240
241/*
drh9339da12010-09-30 00:50:49 +0000242** The string z[] is an text representation of a real number.
drh025586a2010-09-30 17:33:11 +0000243** Convert this string to a double and write it into *pResult.
drhc81c11f2009-11-10 01:30:52 +0000244**
drh9339da12010-09-30 00:50:49 +0000245** The string z[] is length bytes in length (bytes, not characters) and
246** uses the encoding enc. The string is not necessarily zero-terminated.
drhc81c11f2009-11-10 01:30:52 +0000247**
drh9339da12010-09-30 00:50:49 +0000248** Return TRUE if the result is a valid real number (or integer) and FALSE
drh025586a2010-09-30 17:33:11 +0000249** if the string is empty or contains extraneous text. Valid numbers
250** are in one of these formats:
251**
252** [+-]digits[E[+-]digits]
253** [+-]digits.[digits][E[+-]digits]
254** [+-].digits[E[+-]digits]
255**
256** Leading and trailing whitespace is ignored for the purpose of determining
257** validity.
258**
259** If some prefix of the input string is a valid number, this routine
260** returns FALSE but it still converts the prefix and writes the result
261** into *pResult.
drhc81c11f2009-11-10 01:30:52 +0000262*/
drh9339da12010-09-30 00:50:49 +0000263int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
drhc81c11f2009-11-10 01:30:52 +0000264#ifndef SQLITE_OMIT_FLOATING_POINT
drh0e5fba72013-03-20 12:04:29 +0000265 int incr;
drh9339da12010-09-30 00:50:49 +0000266 const char *zEnd = z + length;
drhc81c11f2009-11-10 01:30:52 +0000267 /* sign * significand * (10 ^ (esign * exponent)) */
drh025586a2010-09-30 17:33:11 +0000268 int sign = 1; /* sign of significand */
269 i64 s = 0; /* significand */
270 int d = 0; /* adjust exponent for shifting decimal point */
271 int esign = 1; /* sign of exponent */
272 int e = 0; /* exponent */
273 int eValid = 1; /* True exponent is either not used or is well-formed */
drhc81c11f2009-11-10 01:30:52 +0000274 double result;
275 int nDigits = 0;
drh0e5fba72013-03-20 12:04:29 +0000276 int nonNum = 0;
drhc81c11f2009-11-10 01:30:52 +0000277
drh0e5fba72013-03-20 12:04:29 +0000278 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
drh025586a2010-09-30 17:33:11 +0000279 *pResult = 0.0; /* Default return value, in case of an error */
280
drh0e5fba72013-03-20 12:04:29 +0000281 if( enc==SQLITE_UTF8 ){
282 incr = 1;
283 }else{
284 int i;
285 incr = 2;
286 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
287 for(i=3-enc; i<length && z[i]==0; i+=2){}
288 nonNum = i<length;
289 zEnd = z+i+enc-3;
290 z += (enc&1);
291 }
drh9339da12010-09-30 00:50:49 +0000292
drhc81c11f2009-11-10 01:30:52 +0000293 /* skip leading spaces */
drh9339da12010-09-30 00:50:49 +0000294 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
drh025586a2010-09-30 17:33:11 +0000295 if( z>=zEnd ) return 0;
drh9339da12010-09-30 00:50:49 +0000296
drhc81c11f2009-11-10 01:30:52 +0000297 /* get sign of significand */
298 if( *z=='-' ){
299 sign = -1;
drh9339da12010-09-30 00:50:49 +0000300 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000301 }else if( *z=='+' ){
drh9339da12010-09-30 00:50:49 +0000302 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000303 }
drh9339da12010-09-30 00:50:49 +0000304
drhc81c11f2009-11-10 01:30:52 +0000305 /* skip leading zeroes */
drh9339da12010-09-30 00:50:49 +0000306 while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000307
308 /* copy max significant digits to significand */
drh9339da12010-09-30 00:50:49 +0000309 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
drhc81c11f2009-11-10 01:30:52 +0000310 s = s*10 + (*z - '0');
drh9339da12010-09-30 00:50:49 +0000311 z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000312 }
drh9339da12010-09-30 00:50:49 +0000313
drhc81c11f2009-11-10 01:30:52 +0000314 /* skip non-significant significand digits
315 ** (increase exponent by d to shift decimal left) */
drh9339da12010-09-30 00:50:49 +0000316 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
317 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000318
319 /* if decimal point is present */
320 if( *z=='.' ){
drh9339da12010-09-30 00:50:49 +0000321 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000322 /* copy digits from after decimal to significand
323 ** (decrease exponent by d to shift decimal right) */
drh9339da12010-09-30 00:50:49 +0000324 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
drhc81c11f2009-11-10 01:30:52 +0000325 s = s*10 + (*z - '0');
drh9339da12010-09-30 00:50:49 +0000326 z+=incr, nDigits++, d--;
drhc81c11f2009-11-10 01:30:52 +0000327 }
328 /* skip non-significant digits */
drh9339da12010-09-30 00:50:49 +0000329 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000330 }
drh9339da12010-09-30 00:50:49 +0000331 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000332
333 /* if exponent is present */
334 if( *z=='e' || *z=='E' ){
drh9339da12010-09-30 00:50:49 +0000335 z+=incr;
drh025586a2010-09-30 17:33:11 +0000336 eValid = 0;
drh9339da12010-09-30 00:50:49 +0000337 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000338 /* get sign of exponent */
339 if( *z=='-' ){
340 esign = -1;
drh9339da12010-09-30 00:50:49 +0000341 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000342 }else if( *z=='+' ){
drh9339da12010-09-30 00:50:49 +0000343 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000344 }
345 /* copy digits to exponent */
drh9339da12010-09-30 00:50:49 +0000346 while( z<zEnd && sqlite3Isdigit(*z) ){
drh57db4a72011-10-17 20:41:46 +0000347 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
drh9339da12010-09-30 00:50:49 +0000348 z+=incr;
drh025586a2010-09-30 17:33:11 +0000349 eValid = 1;
drhc81c11f2009-11-10 01:30:52 +0000350 }
351 }
352
drh025586a2010-09-30 17:33:11 +0000353 /* skip trailing spaces */
354 if( nDigits && eValid ){
355 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
356 }
357
drh9339da12010-09-30 00:50:49 +0000358do_atof_calc:
drhc81c11f2009-11-10 01:30:52 +0000359 /* adjust exponent by d, and update sign */
360 e = (e*esign) + d;
361 if( e<0 ) {
362 esign = -1;
363 e *= -1;
364 } else {
365 esign = 1;
366 }
367
368 /* if 0 significand */
369 if( !s ) {
370 /* In the IEEE 754 standard, zero is signed.
371 ** Add the sign if we've seen at least one digit */
372 result = (sign<0 && nDigits) ? -(double)0 : (double)0;
373 } else {
374 /* attempt to reduce exponent */
375 if( esign>0 ){
376 while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
377 }else{
378 while( !(s%10) && e>0 ) e--,s/=10;
379 }
380
381 /* adjust the sign of significand */
382 s = sign<0 ? -s : s;
383
384 /* if exponent, scale significand as appropriate
385 ** and store in result. */
386 if( e ){
drh89f15082012-06-19 00:45:16 +0000387 LONGDOUBLE_TYPE scale = 1.0;
drhc81c11f2009-11-10 01:30:52 +0000388 /* attempt to handle extremely small/large numbers better */
389 if( e>307 && e<342 ){
390 while( e%308 ) { scale *= 1.0e+1; e -= 1; }
391 if( esign<0 ){
392 result = s / scale;
393 result /= 1.0e+308;
394 }else{
395 result = s * scale;
396 result *= 1.0e+308;
397 }
drh2458a2e2011-10-17 12:14:26 +0000398 }else if( e>=342 ){
399 if( esign<0 ){
400 result = 0.0*s;
401 }else{
402 result = 1e308*1e308*s; /* Infinity */
403 }
drhc81c11f2009-11-10 01:30:52 +0000404 }else{
405 /* 1.0e+22 is the largest power of 10 than can be
406 ** represented exactly. */
407 while( e%22 ) { scale *= 1.0e+1; e -= 1; }
408 while( e>0 ) { scale *= 1.0e+22; e -= 22; }
409 if( esign<0 ){
410 result = s / scale;
411 }else{
412 result = s * scale;
413 }
414 }
415 } else {
416 result = (double)s;
417 }
418 }
419
420 /* store the result */
421 *pResult = result;
422
drh025586a2010-09-30 17:33:11 +0000423 /* return true if number and no extra non-whitespace chracters after */
drh0e5fba72013-03-20 12:04:29 +0000424 return z>=zEnd && nDigits>0 && eValid && nonNum==0;
drhc81c11f2009-11-10 01:30:52 +0000425#else
shaneh5f1d6b62010-09-30 16:51:25 +0000426 return !sqlite3Atoi64(z, pResult, length, enc);
drhc81c11f2009-11-10 01:30:52 +0000427#endif /* SQLITE_OMIT_FLOATING_POINT */
428}
429
430/*
431** Compare the 19-character string zNum against the text representation
432** value 2^63: 9223372036854775808. Return negative, zero, or positive
433** if zNum is less than, equal to, or greater than the string.
shaneh5f1d6b62010-09-30 16:51:25 +0000434** Note that zNum must contain exactly 19 characters.
drhc81c11f2009-11-10 01:30:52 +0000435**
436** Unlike memcmp() this routine is guaranteed to return the difference
437** in the values of the last digit if the only difference is in the
438** last digit. So, for example,
439**
drh9339da12010-09-30 00:50:49 +0000440** compare2pow63("9223372036854775800", 1)
drhc81c11f2009-11-10 01:30:52 +0000441**
442** will return -8.
443*/
drh9339da12010-09-30 00:50:49 +0000444static int compare2pow63(const char *zNum, int incr){
445 int c = 0;
446 int i;
447 /* 012345678901234567 */
448 const char *pow63 = "922337203685477580";
449 for(i=0; c==0 && i<18; i++){
450 c = (zNum[i*incr]-pow63[i])*10;
451 }
drhc81c11f2009-11-10 01:30:52 +0000452 if( c==0 ){
drh9339da12010-09-30 00:50:49 +0000453 c = zNum[18*incr] - '8';
drh44dbca82010-01-13 04:22:20 +0000454 testcase( c==(-1) );
455 testcase( c==0 );
456 testcase( c==(+1) );
drhc81c11f2009-11-10 01:30:52 +0000457 }
458 return c;
459}
460
461
462/*
drh158b9cb2011-03-05 20:59:46 +0000463** Convert zNum to a 64-bit signed integer.
464**
465** If the zNum value is representable as a 64-bit twos-complement
466** integer, then write that value into *pNum and return 0.
467**
468** If zNum is exactly 9223372036854665808, return 2. This special
469** case is broken out because while 9223372036854665808 cannot be a
470** signed 64-bit integer, its negative -9223372036854665808 can be.
471**
472** If zNum is too big for a 64-bit integer and is not
drh0e5fba72013-03-20 12:04:29 +0000473** 9223372036854665808 or if zNum contains any non-numeric text,
474** then return 1.
drhc81c11f2009-11-10 01:30:52 +0000475**
drh9339da12010-09-30 00:50:49 +0000476** length is the number of bytes in the string (bytes, not characters).
477** The string is not necessarily zero-terminated. The encoding is
478** given by enc.
drhc81c11f2009-11-10 01:30:52 +0000479*/
drh9339da12010-09-30 00:50:49 +0000480int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
drh0e5fba72013-03-20 12:04:29 +0000481 int incr;
drh158b9cb2011-03-05 20:59:46 +0000482 u64 u = 0;
shaneh5f1d6b62010-09-30 16:51:25 +0000483 int neg = 0; /* assume positive */
drh9339da12010-09-30 00:50:49 +0000484 int i;
485 int c = 0;
drh0e5fba72013-03-20 12:04:29 +0000486 int nonNum = 0;
drhc81c11f2009-11-10 01:30:52 +0000487 const char *zStart;
drh9339da12010-09-30 00:50:49 +0000488 const char *zEnd = zNum + length;
drh0e5fba72013-03-20 12:04:29 +0000489 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
490 if( enc==SQLITE_UTF8 ){
491 incr = 1;
492 }else{
493 incr = 2;
494 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
495 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
496 nonNum = i<length;
497 zEnd = zNum+i+enc-3;
498 zNum += (enc&1);
499 }
drh9339da12010-09-30 00:50:49 +0000500 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
drh158b9cb2011-03-05 20:59:46 +0000501 if( zNum<zEnd ){
502 if( *zNum=='-' ){
503 neg = 1;
504 zNum+=incr;
505 }else if( *zNum=='+' ){
506 zNum+=incr;
507 }
drhc81c11f2009-11-10 01:30:52 +0000508 }
509 zStart = zNum;
drh9339da12010-09-30 00:50:49 +0000510 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
511 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
drh158b9cb2011-03-05 20:59:46 +0000512 u = u*10 + c - '0';
drhc81c11f2009-11-10 01:30:52 +0000513 }
drh158b9cb2011-03-05 20:59:46 +0000514 if( u>LARGEST_INT64 ){
515 *pNum = SMALLEST_INT64;
516 }else if( neg ){
517 *pNum = -(i64)u;
518 }else{
519 *pNum = (i64)u;
520 }
drh44dbca82010-01-13 04:22:20 +0000521 testcase( i==18 );
522 testcase( i==19 );
523 testcase( i==20 );
drh12886632013-03-28 11:40:14 +0000524 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
drhc81c11f2009-11-10 01:30:52 +0000525 /* zNum is empty or contains non-numeric text or is longer
shaneh5f1d6b62010-09-30 16:51:25 +0000526 ** than 19 digits (thus guaranteeing that it is too large) */
527 return 1;
drh9339da12010-09-30 00:50:49 +0000528 }else if( i<19*incr ){
drhc81c11f2009-11-10 01:30:52 +0000529 /* Less than 19 digits, so we know that it fits in 64 bits */
drh158b9cb2011-03-05 20:59:46 +0000530 assert( u<=LARGEST_INT64 );
shaneh5f1d6b62010-09-30 16:51:25 +0000531 return 0;
drhc81c11f2009-11-10 01:30:52 +0000532 }else{
drh158b9cb2011-03-05 20:59:46 +0000533 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
534 c = compare2pow63(zNum, incr);
535 if( c<0 ){
536 /* zNum is less than 9223372036854775808 so it fits */
537 assert( u<=LARGEST_INT64 );
538 return 0;
539 }else if( c>0 ){
540 /* zNum is greater than 9223372036854775808 so it overflows */
541 return 1;
542 }else{
543 /* zNum is exactly 9223372036854775808. Fits if negative. The
544 ** special case 2 overflow if positive */
545 assert( u-1==LARGEST_INT64 );
546 assert( (*pNum)==SMALLEST_INT64 );
547 return neg ? 0 : 2;
548 }
drhc81c11f2009-11-10 01:30:52 +0000549 }
550}
551
552/*
553** If zNum represents an integer that will fit in 32-bits, then set
554** *pValue to that integer and return true. Otherwise return false.
555**
556** Any non-numeric characters that following zNum are ignored.
557** This is different from sqlite3Atoi64() which requires the
558** input number to be zero-terminated.
559*/
560int sqlite3GetInt32(const char *zNum, int *pValue){
561 sqlite_int64 v = 0;
562 int i, c;
563 int neg = 0;
564 if( zNum[0]=='-' ){
565 neg = 1;
566 zNum++;
567 }else if( zNum[0]=='+' ){
568 zNum++;
569 }
570 while( zNum[0]=='0' ) zNum++;
571 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
572 v = v*10 + c;
573 }
574
575 /* The longest decimal representation of a 32 bit integer is 10 digits:
576 **
577 ** 1234567890
578 ** 2^31 -> 2147483648
579 */
drh44dbca82010-01-13 04:22:20 +0000580 testcase( i==10 );
drhc81c11f2009-11-10 01:30:52 +0000581 if( i>10 ){
582 return 0;
583 }
drh44dbca82010-01-13 04:22:20 +0000584 testcase( v-neg==2147483647 );
drhc81c11f2009-11-10 01:30:52 +0000585 if( v-neg>2147483647 ){
586 return 0;
587 }
588 if( neg ){
589 v = -v;
590 }
591 *pValue = (int)v;
592 return 1;
593}
594
595/*
drh60ac3f42010-11-23 18:59:27 +0000596** Return a 32-bit integer value extracted from a string. If the
597** string is not an integer, just return 0.
598*/
599int sqlite3Atoi(const char *z){
600 int x = 0;
601 if( z ) sqlite3GetInt32(z, &x);
602 return x;
603}
604
605/*
drhc81c11f2009-11-10 01:30:52 +0000606** The variable-length integer encoding is as follows:
607**
608** KEY:
609** A = 0xxxxxxx 7 bits of data and one flag bit
610** B = 1xxxxxxx 7 bits of data and one flag bit
611** C = xxxxxxxx 8 bits of data
612**
613** 7 bits - A
614** 14 bits - BA
615** 21 bits - BBA
616** 28 bits - BBBA
617** 35 bits - BBBBA
618** 42 bits - BBBBBA
619** 49 bits - BBBBBBA
620** 56 bits - BBBBBBBA
621** 64 bits - BBBBBBBBC
622*/
623
624/*
625** Write a 64-bit variable-length integer to memory starting at p[0].
626** The length of data write will be between 1 and 9 bytes. The number
627** of bytes written is returned.
628**
629** A variable-length integer consists of the lower 7 bits of each byte
630** for all bytes that have the 8th bit set and one byte with the 8th
631** bit clear. Except, if we get to the 9th byte, it stores the full
632** 8 bits and is the last byte.
633*/
634int sqlite3PutVarint(unsigned char *p, u64 v){
635 int i, j, n;
636 u8 buf[10];
637 if( v & (((u64)0xff000000)<<32) ){
638 p[8] = (u8)v;
639 v >>= 8;
640 for(i=7; i>=0; i--){
641 p[i] = (u8)((v & 0x7f) | 0x80);
642 v >>= 7;
643 }
644 return 9;
645 }
646 n = 0;
647 do{
648 buf[n++] = (u8)((v & 0x7f) | 0x80);
649 v >>= 7;
650 }while( v!=0 );
651 buf[0] &= 0x7f;
652 assert( n<=9 );
653 for(i=0, j=n-1; j>=0; j--, i++){
654 p[i] = buf[j];
655 }
656 return n;
657}
658
659/*
660** This routine is a faster version of sqlite3PutVarint() that only
661** works for 32-bit positive integers and which is optimized for
662** the common case of small integers. A MACRO version, putVarint32,
663** is provided which inlines the single-byte case. All code should use
664** the MACRO version as this function assumes the single-byte case has
665** already been handled.
666*/
667int sqlite3PutVarint32(unsigned char *p, u32 v){
668#ifndef putVarint32
669 if( (v & ~0x7f)==0 ){
670 p[0] = v;
671 return 1;
672 }
673#endif
674 if( (v & ~0x3fff)==0 ){
675 p[0] = (u8)((v>>7) | 0x80);
676 p[1] = (u8)(v & 0x7f);
677 return 2;
678 }
679 return sqlite3PutVarint(p, v);
680}
681
682/*
drh0b2864c2010-03-03 15:18:38 +0000683** Bitmasks used by sqlite3GetVarint(). These precomputed constants
684** are defined here rather than simply putting the constant expressions
685** inline in order to work around bugs in the RVT compiler.
686**
687** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
688**
689** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
690*/
691#define SLOT_2_0 0x001fc07f
692#define SLOT_4_2_0 0xf01fc07f
693
694
695/*
drhc81c11f2009-11-10 01:30:52 +0000696** Read a 64-bit variable-length integer from memory starting at p[0].
697** Return the number of bytes read. The value is stored in *v.
698*/
699u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
700 u32 a,b,s;
701
702 a = *p;
703 /* a: p0 (unmasked) */
704 if (!(a&0x80))
705 {
706 *v = a;
707 return 1;
708 }
709
710 p++;
711 b = *p;
712 /* b: p1 (unmasked) */
713 if (!(b&0x80))
714 {
715 a &= 0x7f;
716 a = a<<7;
717 a |= b;
718 *v = a;
719 return 2;
720 }
721
drh0b2864c2010-03-03 15:18:38 +0000722 /* Verify that constants are precomputed correctly */
723 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
shaneh1da207e2010-03-09 14:41:12 +0000724 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
drh0b2864c2010-03-03 15:18:38 +0000725
drhc81c11f2009-11-10 01:30:52 +0000726 p++;
727 a = a<<14;
728 a |= *p;
729 /* a: p0<<14 | p2 (unmasked) */
730 if (!(a&0x80))
731 {
drh0b2864c2010-03-03 15:18:38 +0000732 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000733 b &= 0x7f;
734 b = b<<7;
735 a |= b;
736 *v = a;
737 return 3;
738 }
739
740 /* CSE1 from below */
drh0b2864c2010-03-03 15:18:38 +0000741 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000742 p++;
743 b = b<<14;
744 b |= *p;
745 /* b: p1<<14 | p3 (unmasked) */
746 if (!(b&0x80))
747 {
drh0b2864c2010-03-03 15:18:38 +0000748 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000749 /* moved CSE1 up */
750 /* a &= (0x7f<<14)|(0x7f); */
751 a = a<<7;
752 a |= b;
753 *v = a;
754 return 4;
755 }
756
757 /* a: p0<<14 | p2 (masked) */
758 /* b: p1<<14 | p3 (unmasked) */
759 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
760 /* moved CSE1 up */
761 /* a &= (0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000762 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000763 s = a;
764 /* s: p0<<14 | p2 (masked) */
765
766 p++;
767 a = a<<14;
768 a |= *p;
769 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
770 if (!(a&0x80))
771 {
772 /* we can skip these cause they were (effectively) done above in calc'ing s */
773 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
774 /* b &= (0x7f<<14)|(0x7f); */
775 b = b<<7;
776 a |= b;
777 s = s>>18;
778 *v = ((u64)s)<<32 | a;
779 return 5;
780 }
781
782 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
783 s = s<<7;
784 s |= b;
785 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
786
787 p++;
788 b = b<<14;
789 b |= *p;
790 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
791 if (!(b&0x80))
792 {
793 /* we can skip this cause it was (effectively) done above in calc'ing s */
794 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000795 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000796 a = a<<7;
797 a |= b;
798 s = s>>18;
799 *v = ((u64)s)<<32 | a;
800 return 6;
801 }
802
803 p++;
804 a = a<<14;
805 a |= *p;
806 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
807 if (!(a&0x80))
808 {
drh0b2864c2010-03-03 15:18:38 +0000809 a &= SLOT_4_2_0;
810 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000811 b = b<<7;
812 a |= b;
813 s = s>>11;
814 *v = ((u64)s)<<32 | a;
815 return 7;
816 }
817
818 /* CSE2 from below */
drh0b2864c2010-03-03 15:18:38 +0000819 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000820 p++;
821 b = b<<14;
822 b |= *p;
823 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
824 if (!(b&0x80))
825 {
drh0b2864c2010-03-03 15:18:38 +0000826 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000827 /* moved CSE2 up */
828 /* a &= (0x7f<<14)|(0x7f); */
829 a = a<<7;
830 a |= b;
831 s = s>>4;
832 *v = ((u64)s)<<32 | a;
833 return 8;
834 }
835
836 p++;
837 a = a<<15;
838 a |= *p;
839 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
840
841 /* moved CSE2 up */
842 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
drh0b2864c2010-03-03 15:18:38 +0000843 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000844 b = b<<8;
845 a |= b;
846
847 s = s<<4;
848 b = p[-4];
849 b &= 0x7f;
850 b = b>>3;
851 s |= b;
852
853 *v = ((u64)s)<<32 | a;
854
855 return 9;
856}
857
858/*
859** Read a 32-bit variable-length integer from memory starting at p[0].
860** Return the number of bytes read. The value is stored in *v.
861**
862** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
863** integer, then set *v to 0xffffffff.
864**
865** A MACRO version, getVarint32, is provided which inlines the
866** single-byte case. All code should use the MACRO version as
867** this function assumes the single-byte case has already been handled.
868*/
869u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
870 u32 a,b;
871
872 /* The 1-byte case. Overwhelmingly the most common. Handled inline
873 ** by the getVarin32() macro */
874 a = *p;
875 /* a: p0 (unmasked) */
876#ifndef getVarint32
877 if (!(a&0x80))
878 {
879 /* Values between 0 and 127 */
880 *v = a;
881 return 1;
882 }
883#endif
884
885 /* The 2-byte case */
886 p++;
887 b = *p;
888 /* b: p1 (unmasked) */
889 if (!(b&0x80))
890 {
891 /* Values between 128 and 16383 */
892 a &= 0x7f;
893 a = a<<7;
894 *v = a | b;
895 return 2;
896 }
897
898 /* The 3-byte case */
899 p++;
900 a = a<<14;
901 a |= *p;
902 /* a: p0<<14 | p2 (unmasked) */
903 if (!(a&0x80))
904 {
905 /* Values between 16384 and 2097151 */
906 a &= (0x7f<<14)|(0x7f);
907 b &= 0x7f;
908 b = b<<7;
909 *v = a | b;
910 return 3;
911 }
912
913 /* A 32-bit varint is used to store size information in btrees.
914 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
915 ** A 3-byte varint is sufficient, for example, to record the size
916 ** of a 1048569-byte BLOB or string.
917 **
918 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
919 ** rare larger cases can be handled by the slower 64-bit varint
920 ** routine.
921 */
922#if 1
923 {
924 u64 v64;
925 u8 n;
926
927 p -= 2;
928 n = sqlite3GetVarint(p, &v64);
929 assert( n>3 && n<=9 );
930 if( (v64 & SQLITE_MAX_U32)!=v64 ){
931 *v = 0xffffffff;
932 }else{
933 *v = (u32)v64;
934 }
935 return n;
936 }
937
938#else
939 /* For following code (kept for historical record only) shows an
940 ** unrolling for the 3- and 4-byte varint cases. This code is
941 ** slightly faster, but it is also larger and much harder to test.
942 */
943 p++;
944 b = b<<14;
945 b |= *p;
946 /* b: p1<<14 | p3 (unmasked) */
947 if (!(b&0x80))
948 {
949 /* Values between 2097152 and 268435455 */
950 b &= (0x7f<<14)|(0x7f);
951 a &= (0x7f<<14)|(0x7f);
952 a = a<<7;
953 *v = a | b;
954 return 4;
955 }
956
957 p++;
958 a = a<<14;
959 a |= *p;
960 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
961 if (!(a&0x80))
962 {
dan3bbe7612010-03-03 16:02:05 +0000963 /* Values between 268435456 and 34359738367 */
964 a &= SLOT_4_2_0;
965 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000966 b = b<<7;
967 *v = a | b;
968 return 5;
969 }
970
971 /* We can only reach this point when reading a corrupt database
972 ** file. In that case we are not in any hurry. Use the (relatively
973 ** slow) general-purpose sqlite3GetVarint() routine to extract the
974 ** value. */
975 {
976 u64 v64;
977 u8 n;
978
979 p -= 4;
980 n = sqlite3GetVarint(p, &v64);
981 assert( n>5 && n<=9 );
982 *v = (u32)v64;
983 return n;
984 }
985#endif
986}
987
988/*
989** Return the number of bytes that will be needed to store the given
990** 64-bit integer.
991*/
992int sqlite3VarintLen(u64 v){
993 int i = 0;
994 do{
995 i++;
996 v >>= 7;
997 }while( v!=0 && ALWAYS(i<9) );
998 return i;
999}
1000
1001
1002/*
1003** Read or write a four-byte big-endian integer value.
1004*/
1005u32 sqlite3Get4byte(const u8 *p){
1006 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
1007}
1008void sqlite3Put4byte(unsigned char *p, u32 v){
1009 p[0] = (u8)(v>>24);
1010 p[1] = (u8)(v>>16);
1011 p[2] = (u8)(v>>8);
1012 p[3] = (u8)v;
1013}
1014
1015
1016
drhc81c11f2009-11-10 01:30:52 +00001017/*
1018** Translate a single byte of Hex into an integer.
1019** This routine only works if h really is a valid hexadecimal
1020** character: 0..9a..fA..F
1021*/
dancd74b612011-04-22 19:37:32 +00001022u8 sqlite3HexToInt(int h){
drhc81c11f2009-11-10 01:30:52 +00001023 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
1024#ifdef SQLITE_ASCII
1025 h += 9*(1&(h>>6));
1026#endif
1027#ifdef SQLITE_EBCDIC
1028 h += 9*(1&~(h>>4));
1029#endif
1030 return (u8)(h & 0xf);
1031}
drhc81c11f2009-11-10 01:30:52 +00001032
1033#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1034/*
1035** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1036** value. Return a pointer to its binary value. Space to hold the
1037** binary value has been obtained from malloc and must be freed by
1038** the calling routine.
1039*/
1040void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
1041 char *zBlob;
1042 int i;
1043
1044 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
1045 n--;
1046 if( zBlob ){
1047 for(i=0; i<n; i+=2){
dancd74b612011-04-22 19:37:32 +00001048 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
drhc81c11f2009-11-10 01:30:52 +00001049 }
1050 zBlob[i/2] = 0;
1051 }
1052 return zBlob;
1053}
1054#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1055
drh413c3d32010-02-23 20:11:56 +00001056/*
1057** Log an error that is an API call on a connection pointer that should
1058** not have been used. The "type" of connection pointer is given as the
1059** argument. The zType is a word like "NULL" or "closed" or "invalid".
1060*/
1061static void logBadConnection(const char *zType){
1062 sqlite3_log(SQLITE_MISUSE,
1063 "API call with %s database connection pointer",
1064 zType
1065 );
1066}
drhc81c11f2009-11-10 01:30:52 +00001067
1068/*
drhc81c11f2009-11-10 01:30:52 +00001069** Check to make sure we have a valid db pointer. This test is not
1070** foolproof but it does provide some measure of protection against
1071** misuse of the interface such as passing in db pointers that are
1072** NULL or which have been previously closed. If this routine returns
1073** 1 it means that the db pointer is valid and 0 if it should not be
1074** dereferenced for any reason. The calling function should invoke
1075** SQLITE_MISUSE immediately.
1076**
1077** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1078** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1079** open properly and is not fit for general use but which can be
1080** used as an argument to sqlite3_errmsg() or sqlite3_close().
1081*/
1082int sqlite3SafetyCheckOk(sqlite3 *db){
1083 u32 magic;
drh413c3d32010-02-23 20:11:56 +00001084 if( db==0 ){
1085 logBadConnection("NULL");
1086 return 0;
1087 }
drhc81c11f2009-11-10 01:30:52 +00001088 magic = db->magic;
drh9978c972010-02-23 17:36:32 +00001089 if( magic!=SQLITE_MAGIC_OPEN ){
drhe294da02010-02-25 23:44:15 +00001090 if( sqlite3SafetyCheckSickOrOk(db) ){
1091 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +00001092 logBadConnection("unopened");
1093 }
drhc81c11f2009-11-10 01:30:52 +00001094 return 0;
1095 }else{
1096 return 1;
1097 }
1098}
1099int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1100 u32 magic;
1101 magic = db->magic;
1102 if( magic!=SQLITE_MAGIC_SICK &&
1103 magic!=SQLITE_MAGIC_OPEN &&
drh413c3d32010-02-23 20:11:56 +00001104 magic!=SQLITE_MAGIC_BUSY ){
drhe294da02010-02-25 23:44:15 +00001105 testcase( sqlite3GlobalConfig.xLog!=0 );
drhaf46dc12010-02-24 21:44:07 +00001106 logBadConnection("invalid");
drh413c3d32010-02-23 20:11:56 +00001107 return 0;
1108 }else{
1109 return 1;
1110 }
drhc81c11f2009-11-10 01:30:52 +00001111}
drh158b9cb2011-03-05 20:59:46 +00001112
1113/*
1114** Attempt to add, substract, or multiply the 64-bit signed value iB against
1115** the other 64-bit signed integer at *pA and store the result in *pA.
1116** Return 0 on success. Or if the operation would have resulted in an
1117** overflow, leave *pA unchanged and return 1.
1118*/
1119int sqlite3AddInt64(i64 *pA, i64 iB){
1120 i64 iA = *pA;
1121 testcase( iA==0 ); testcase( iA==1 );
1122 testcase( iB==-1 ); testcase( iB==0 );
1123 if( iB>=0 ){
1124 testcase( iA>0 && LARGEST_INT64 - iA == iB );
1125 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
1126 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
1127 *pA += iB;
1128 }else{
1129 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
1130 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
1131 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
1132 *pA += iB;
1133 }
1134 return 0;
1135}
1136int sqlite3SubInt64(i64 *pA, i64 iB){
1137 testcase( iB==SMALLEST_INT64+1 );
1138 if( iB==SMALLEST_INT64 ){
1139 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
1140 if( (*pA)>=0 ) return 1;
1141 *pA -= iB;
1142 return 0;
1143 }else{
1144 return sqlite3AddInt64(pA, -iB);
1145 }
1146}
1147#define TWOPOWER32 (((i64)1)<<32)
1148#define TWOPOWER31 (((i64)1)<<31)
1149int sqlite3MulInt64(i64 *pA, i64 iB){
1150 i64 iA = *pA;
1151 i64 iA1, iA0, iB1, iB0, r;
1152
drh158b9cb2011-03-05 20:59:46 +00001153 iA1 = iA/TWOPOWER32;
1154 iA0 = iA % TWOPOWER32;
1155 iB1 = iB/TWOPOWER32;
1156 iB0 = iB % TWOPOWER32;
1157 if( iA1*iB1 != 0 ) return 1;
drhd7255a22011-03-05 21:41:34 +00001158 assert( iA1*iB0==0 || iA0*iB1==0 );
1159 r = iA1*iB0 + iA0*iB1;
drh158b9cb2011-03-05 20:59:46 +00001160 testcase( r==(-TWOPOWER31)-1 );
1161 testcase( r==(-TWOPOWER31) );
1162 testcase( r==TWOPOWER31 );
1163 testcase( r==TWOPOWER31-1 );
1164 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
1165 r *= TWOPOWER32;
1166 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
1167 *pA = r;
1168 return 0;
1169}
drhd50ffc42011-03-08 02:38:28 +00001170
1171/*
1172** Compute the absolute value of a 32-bit signed integer, of possible. Or
1173** if the integer has a value of -2147483648, return +2147483647
1174*/
1175int sqlite3AbsInt32(int x){
1176 if( x>=0 ) return x;
drh87e79ae2011-03-08 13:06:41 +00001177 if( x==(int)0x80000000 ) return 0x7fffffff;
drhd50ffc42011-03-08 02:38:28 +00001178 return -x;
1179}
drh81cc5162011-05-17 20:36:21 +00001180
1181#ifdef SQLITE_ENABLE_8_3_NAMES
1182/*
drhb51bf432011-07-21 21:29:35 +00001183** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
drh81cc5162011-05-17 20:36:21 +00001184** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
1185** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
1186** three characters, then shorten the suffix on z[] to be the last three
1187** characters of the original suffix.
1188**
drhb51bf432011-07-21 21:29:35 +00001189** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
1190** do the suffix shortening regardless of URI parameter.
1191**
drh81cc5162011-05-17 20:36:21 +00001192** Examples:
1193**
1194** test.db-journal => test.nal
1195** test.db-wal => test.wal
1196** test.db-shm => test.shm
drhf5808602011-12-16 00:33:04 +00001197** test.db-mj7f3319fa => test.9fa
drh81cc5162011-05-17 20:36:21 +00001198*/
1199void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
drhb51bf432011-07-21 21:29:35 +00001200#if SQLITE_ENABLE_8_3_NAMES<2
drh7d39e172012-01-02 12:41:53 +00001201 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
drhb51bf432011-07-21 21:29:35 +00001202#endif
1203 {
drh81cc5162011-05-17 20:36:21 +00001204 int i, sz;
1205 sz = sqlite3Strlen30(z);
drhc83f2d42011-05-18 02:41:10 +00001206 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
drhc02a43a2012-01-10 23:18:38 +00001207 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
drh81cc5162011-05-17 20:36:21 +00001208 }
1209}
1210#endif
drhbf539c42013-10-05 18:16:02 +00001211
1212/*
1213** Find (an approximate) sum of two LogEst values. This computation is
1214** not a simple "+" operator because LogEst is stored as a logarithmic
1215** value.
1216**
1217*/
1218LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
1219 static const unsigned char x[] = {
1220 10, 10, /* 0,1 */
1221 9, 9, /* 2,3 */
1222 8, 8, /* 4,5 */
1223 7, 7, 7, /* 6,7,8 */
1224 6, 6, 6, /* 9,10,11 */
1225 5, 5, 5, /* 12-14 */
1226 4, 4, 4, 4, /* 15-18 */
1227 3, 3, 3, 3, 3, 3, /* 19-24 */
1228 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
1229 };
1230 if( a>=b ){
1231 if( a>b+49 ) return a;
1232 if( a>b+31 ) return a+1;
1233 return a+x[a-b];
1234 }else{
1235 if( b>a+49 ) return b;
1236 if( b>a+31 ) return b+1;
1237 return b+x[b-a];
1238 }
1239}
1240
1241/*
1242** Convert an integer into a LogEst. In other words, compute a
1243** good approximatation for 10*log2(x).
1244*/
1245LogEst sqlite3LogEst(u64 x){
1246 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
1247 LogEst y = 40;
1248 if( x<8 ){
1249 if( x<2 ) return 0;
1250 while( x<8 ){ y -= 10; x <<= 1; }
1251 }else{
1252 while( x>255 ){ y += 40; x >>= 4; }
1253 while( x>15 ){ y += 10; x >>= 1; }
1254 }
1255 return a[x&7] + y - 10;
1256}
1257
1258#ifndef SQLITE_OMIT_VIRTUALTABLE
1259/*
1260** Convert a double into a LogEst
1261** In other words, compute an approximation for 10*log2(x).
1262*/
1263LogEst sqlite3LogEstFromDouble(double x){
1264 u64 a;
1265 LogEst e;
1266 assert( sizeof(x)==8 && sizeof(a)==8 );
1267 if( x<=1 ) return 0;
1268 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
1269 memcpy(&a, &x, 8);
1270 e = (a>>52) - 1022;
1271 return e*10;
1272}
1273#endif /* SQLITE_OMIT_VIRTUALTABLE */
1274
1275/*
1276** Convert a LogEst into an integer.
1277*/
1278u64 sqlite3LogEstToInt(LogEst x){
1279 u64 n;
1280 if( x<10 ) return 1;
1281 n = x%10;
1282 x /= 10;
1283 if( n>=5 ) n -= 2;
1284 else if( n>=1 ) n -= 1;
1285 if( x>=3 ) return (n+8)<<(x-3);
1286 return (n+8)>>(3-x);
1287}