blob: 9fa2a0fbd86f41f59424b4e560e7f21bd9755e8d [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**
drha256c1a2013-12-01 01:18:29 +0000468** If zNum is exactly 9223372036854775808, return 2. This special
469** case is broken out because while 9223372036854775808 cannot be a
470** signed 64-bit integer, its negative -9223372036854775808 can be.
drh158b9cb2011-03-05 20:59:46 +0000471**
472** If zNum is too big for a 64-bit integer and is not
drha256c1a2013-12-01 01:18:29 +0000473** 9223372036854775808 or if zNum contains any non-numeric text,
drh0e5fba72013-03-20 12:04:29 +0000474** 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 ){
drhde1a8b82013-11-26 15:45:02 +0000515 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
drh158b9cb2011-03-05 20:59:46 +0000516 }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 );
drh158b9cb2011-03-05 20:59:46 +0000546 return neg ? 0 : 2;
547 }
drhc81c11f2009-11-10 01:30:52 +0000548 }
549}
550
551/*
552** If zNum represents an integer that will fit in 32-bits, then set
553** *pValue to that integer and return true. Otherwise return false.
554**
555** Any non-numeric characters that following zNum are ignored.
556** This is different from sqlite3Atoi64() which requires the
557** input number to be zero-terminated.
558*/
559int sqlite3GetInt32(const char *zNum, int *pValue){
560 sqlite_int64 v = 0;
561 int i, c;
562 int neg = 0;
563 if( zNum[0]=='-' ){
564 neg = 1;
565 zNum++;
566 }else if( zNum[0]=='+' ){
567 zNum++;
568 }
569 while( zNum[0]=='0' ) zNum++;
570 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
571 v = v*10 + c;
572 }
573
574 /* The longest decimal representation of a 32 bit integer is 10 digits:
575 **
576 ** 1234567890
577 ** 2^31 -> 2147483648
578 */
drh44dbca82010-01-13 04:22:20 +0000579 testcase( i==10 );
drhc81c11f2009-11-10 01:30:52 +0000580 if( i>10 ){
581 return 0;
582 }
drh44dbca82010-01-13 04:22:20 +0000583 testcase( v-neg==2147483647 );
drhc81c11f2009-11-10 01:30:52 +0000584 if( v-neg>2147483647 ){
585 return 0;
586 }
587 if( neg ){
588 v = -v;
589 }
590 *pValue = (int)v;
591 return 1;
592}
593
594/*
drh60ac3f42010-11-23 18:59:27 +0000595** Return a 32-bit integer value extracted from a string. If the
596** string is not an integer, just return 0.
597*/
598int sqlite3Atoi(const char *z){
599 int x = 0;
600 if( z ) sqlite3GetInt32(z, &x);
601 return x;
602}
603
604/*
drhc81c11f2009-11-10 01:30:52 +0000605** The variable-length integer encoding is as follows:
606**
607** KEY:
608** A = 0xxxxxxx 7 bits of data and one flag bit
609** B = 1xxxxxxx 7 bits of data and one flag bit
610** C = xxxxxxxx 8 bits of data
611**
612** 7 bits - A
613** 14 bits - BA
614** 21 bits - BBA
615** 28 bits - BBBA
616** 35 bits - BBBBA
617** 42 bits - BBBBBA
618** 49 bits - BBBBBBA
619** 56 bits - BBBBBBBA
620** 64 bits - BBBBBBBBC
621*/
622
623/*
624** Write a 64-bit variable-length integer to memory starting at p[0].
625** The length of data write will be between 1 and 9 bytes. The number
626** of bytes written is returned.
627**
628** A variable-length integer consists of the lower 7 bits of each byte
629** for all bytes that have the 8th bit set and one byte with the 8th
630** bit clear. Except, if we get to the 9th byte, it stores the full
631** 8 bits and is the last byte.
632*/
633int sqlite3PutVarint(unsigned char *p, u64 v){
634 int i, j, n;
635 u8 buf[10];
636 if( v & (((u64)0xff000000)<<32) ){
637 p[8] = (u8)v;
638 v >>= 8;
639 for(i=7; i>=0; i--){
640 p[i] = (u8)((v & 0x7f) | 0x80);
641 v >>= 7;
642 }
643 return 9;
644 }
645 n = 0;
646 do{
647 buf[n++] = (u8)((v & 0x7f) | 0x80);
648 v >>= 7;
649 }while( v!=0 );
650 buf[0] &= 0x7f;
651 assert( n<=9 );
652 for(i=0, j=n-1; j>=0; j--, i++){
653 p[i] = buf[j];
654 }
655 return n;
656}
657
658/*
659** This routine is a faster version of sqlite3PutVarint() that only
660** works for 32-bit positive integers and which is optimized for
661** the common case of small integers. A MACRO version, putVarint32,
662** is provided which inlines the single-byte case. All code should use
663** the MACRO version as this function assumes the single-byte case has
664** already been handled.
665*/
666int sqlite3PutVarint32(unsigned char *p, u32 v){
667#ifndef putVarint32
668 if( (v & ~0x7f)==0 ){
669 p[0] = v;
670 return 1;
671 }
672#endif
673 if( (v & ~0x3fff)==0 ){
674 p[0] = (u8)((v>>7) | 0x80);
675 p[1] = (u8)(v & 0x7f);
676 return 2;
677 }
678 return sqlite3PutVarint(p, v);
679}
680
681/*
drh0b2864c2010-03-03 15:18:38 +0000682** Bitmasks used by sqlite3GetVarint(). These precomputed constants
683** are defined here rather than simply putting the constant expressions
684** inline in order to work around bugs in the RVT compiler.
685**
686** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
687**
688** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
689*/
690#define SLOT_2_0 0x001fc07f
691#define SLOT_4_2_0 0xf01fc07f
692
693
694/*
drhc81c11f2009-11-10 01:30:52 +0000695** Read a 64-bit variable-length integer from memory starting at p[0].
696** Return the number of bytes read. The value is stored in *v.
697*/
698u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
699 u32 a,b,s;
700
701 a = *p;
702 /* a: p0 (unmasked) */
703 if (!(a&0x80))
704 {
705 *v = a;
706 return 1;
707 }
708
709 p++;
710 b = *p;
711 /* b: p1 (unmasked) */
712 if (!(b&0x80))
713 {
714 a &= 0x7f;
715 a = a<<7;
716 a |= b;
717 *v = a;
718 return 2;
719 }
720
drh0b2864c2010-03-03 15:18:38 +0000721 /* Verify that constants are precomputed correctly */
722 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
shaneh1da207e2010-03-09 14:41:12 +0000723 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
drh0b2864c2010-03-03 15:18:38 +0000724
drhc81c11f2009-11-10 01:30:52 +0000725 p++;
726 a = a<<14;
727 a |= *p;
728 /* a: p0<<14 | p2 (unmasked) */
729 if (!(a&0x80))
730 {
drh0b2864c2010-03-03 15:18:38 +0000731 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000732 b &= 0x7f;
733 b = b<<7;
734 a |= b;
735 *v = a;
736 return 3;
737 }
738
739 /* CSE1 from below */
drh0b2864c2010-03-03 15:18:38 +0000740 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000741 p++;
742 b = b<<14;
743 b |= *p;
744 /* b: p1<<14 | p3 (unmasked) */
745 if (!(b&0x80))
746 {
drh0b2864c2010-03-03 15:18:38 +0000747 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000748 /* moved CSE1 up */
749 /* a &= (0x7f<<14)|(0x7f); */
750 a = a<<7;
751 a |= b;
752 *v = a;
753 return 4;
754 }
755
756 /* a: p0<<14 | p2 (masked) */
757 /* b: p1<<14 | p3 (unmasked) */
758 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
759 /* moved CSE1 up */
760 /* a &= (0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000761 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000762 s = a;
763 /* s: p0<<14 | p2 (masked) */
764
765 p++;
766 a = a<<14;
767 a |= *p;
768 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
769 if (!(a&0x80))
770 {
771 /* we can skip these cause they were (effectively) done above in calc'ing s */
772 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
773 /* b &= (0x7f<<14)|(0x7f); */
774 b = b<<7;
775 a |= b;
776 s = s>>18;
777 *v = ((u64)s)<<32 | a;
778 return 5;
779 }
780
781 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
782 s = s<<7;
783 s |= b;
784 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
785
786 p++;
787 b = b<<14;
788 b |= *p;
789 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
790 if (!(b&0x80))
791 {
792 /* we can skip this cause it was (effectively) done above in calc'ing s */
793 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000794 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000795 a = a<<7;
796 a |= b;
797 s = s>>18;
798 *v = ((u64)s)<<32 | a;
799 return 6;
800 }
801
802 p++;
803 a = a<<14;
804 a |= *p;
805 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
806 if (!(a&0x80))
807 {
drh0b2864c2010-03-03 15:18:38 +0000808 a &= SLOT_4_2_0;
809 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000810 b = b<<7;
811 a |= b;
812 s = s>>11;
813 *v = ((u64)s)<<32 | a;
814 return 7;
815 }
816
817 /* CSE2 from below */
drh0b2864c2010-03-03 15:18:38 +0000818 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000819 p++;
820 b = b<<14;
821 b |= *p;
822 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
823 if (!(b&0x80))
824 {
drh0b2864c2010-03-03 15:18:38 +0000825 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000826 /* moved CSE2 up */
827 /* a &= (0x7f<<14)|(0x7f); */
828 a = a<<7;
829 a |= b;
830 s = s>>4;
831 *v = ((u64)s)<<32 | a;
832 return 8;
833 }
834
835 p++;
836 a = a<<15;
837 a |= *p;
838 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
839
840 /* moved CSE2 up */
841 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
drh0b2864c2010-03-03 15:18:38 +0000842 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000843 b = b<<8;
844 a |= b;
845
846 s = s<<4;
847 b = p[-4];
848 b &= 0x7f;
849 b = b>>3;
850 s |= b;
851
852 *v = ((u64)s)<<32 | a;
853
854 return 9;
855}
856
857/*
858** Read a 32-bit variable-length integer from memory starting at p[0].
859** Return the number of bytes read. The value is stored in *v.
860**
861** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
862** integer, then set *v to 0xffffffff.
863**
864** A MACRO version, getVarint32, is provided which inlines the
865** single-byte case. All code should use the MACRO version as
866** this function assumes the single-byte case has already been handled.
867*/
868u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
869 u32 a,b;
870
871 /* The 1-byte case. Overwhelmingly the most common. Handled inline
872 ** by the getVarin32() macro */
873 a = *p;
874 /* a: p0 (unmasked) */
875#ifndef getVarint32
876 if (!(a&0x80))
877 {
878 /* Values between 0 and 127 */
879 *v = a;
880 return 1;
881 }
882#endif
883
884 /* The 2-byte case */
885 p++;
886 b = *p;
887 /* b: p1 (unmasked) */
888 if (!(b&0x80))
889 {
890 /* Values between 128 and 16383 */
891 a &= 0x7f;
892 a = a<<7;
893 *v = a | b;
894 return 2;
895 }
896
897 /* The 3-byte case */
898 p++;
899 a = a<<14;
900 a |= *p;
901 /* a: p0<<14 | p2 (unmasked) */
902 if (!(a&0x80))
903 {
904 /* Values between 16384 and 2097151 */
905 a &= (0x7f<<14)|(0x7f);
906 b &= 0x7f;
907 b = b<<7;
908 *v = a | b;
909 return 3;
910 }
911
912 /* A 32-bit varint is used to store size information in btrees.
913 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
914 ** A 3-byte varint is sufficient, for example, to record the size
915 ** of a 1048569-byte BLOB or string.
916 **
917 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
918 ** rare larger cases can be handled by the slower 64-bit varint
919 ** routine.
920 */
921#if 1
922 {
923 u64 v64;
924 u8 n;
925
926 p -= 2;
927 n = sqlite3GetVarint(p, &v64);
928 assert( n>3 && n<=9 );
929 if( (v64 & SQLITE_MAX_U32)!=v64 ){
930 *v = 0xffffffff;
931 }else{
932 *v = (u32)v64;
933 }
934 return n;
935 }
936
937#else
938 /* For following code (kept for historical record only) shows an
939 ** unrolling for the 3- and 4-byte varint cases. This code is
940 ** slightly faster, but it is also larger and much harder to test.
941 */
942 p++;
943 b = b<<14;
944 b |= *p;
945 /* b: p1<<14 | p3 (unmasked) */
946 if (!(b&0x80))
947 {
948 /* Values between 2097152 and 268435455 */
949 b &= (0x7f<<14)|(0x7f);
950 a &= (0x7f<<14)|(0x7f);
951 a = a<<7;
952 *v = a | b;
953 return 4;
954 }
955
956 p++;
957 a = a<<14;
958 a |= *p;
959 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
960 if (!(a&0x80))
961 {
dan3bbe7612010-03-03 16:02:05 +0000962 /* Values between 268435456 and 34359738367 */
963 a &= SLOT_4_2_0;
964 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000965 b = b<<7;
966 *v = a | b;
967 return 5;
968 }
969
970 /* We can only reach this point when reading a corrupt database
971 ** file. In that case we are not in any hurry. Use the (relatively
972 ** slow) general-purpose sqlite3GetVarint() routine to extract the
973 ** value. */
974 {
975 u64 v64;
976 u8 n;
977
978 p -= 4;
979 n = sqlite3GetVarint(p, &v64);
980 assert( n>5 && n<=9 );
981 *v = (u32)v64;
982 return n;
983 }
984#endif
985}
986
987/*
988** Return the number of bytes that will be needed to store the given
989** 64-bit integer.
990*/
991int sqlite3VarintLen(u64 v){
992 int i = 0;
993 do{
994 i++;
995 v >>= 7;
996 }while( v!=0 && ALWAYS(i<9) );
997 return i;
998}
999
1000
1001/*
1002** Read or write a four-byte big-endian integer value.
1003*/
1004u32 sqlite3Get4byte(const u8 *p){
1005 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
1006}
1007void sqlite3Put4byte(unsigned char *p, u32 v){
1008 p[0] = (u8)(v>>24);
1009 p[1] = (u8)(v>>16);
1010 p[2] = (u8)(v>>8);
1011 p[3] = (u8)v;
1012}
1013
1014
1015
drhc81c11f2009-11-10 01:30:52 +00001016/*
1017** Translate a single byte of Hex into an integer.
1018** This routine only works if h really is a valid hexadecimal
1019** character: 0..9a..fA..F
1020*/
dancd74b612011-04-22 19:37:32 +00001021u8 sqlite3HexToInt(int h){
drhc81c11f2009-11-10 01:30:52 +00001022 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
1023#ifdef SQLITE_ASCII
1024 h += 9*(1&(h>>6));
1025#endif
1026#ifdef SQLITE_EBCDIC
1027 h += 9*(1&~(h>>4));
1028#endif
1029 return (u8)(h & 0xf);
1030}
drhc81c11f2009-11-10 01:30:52 +00001031
1032#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1033/*
1034** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1035** value. Return a pointer to its binary value. Space to hold the
1036** binary value has been obtained from malloc and must be freed by
1037** the calling routine.
1038*/
1039void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
1040 char *zBlob;
1041 int i;
1042
1043 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
1044 n--;
1045 if( zBlob ){
1046 for(i=0; i<n; i+=2){
dancd74b612011-04-22 19:37:32 +00001047 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
drhc81c11f2009-11-10 01:30:52 +00001048 }
1049 zBlob[i/2] = 0;
1050 }
1051 return zBlob;
1052}
1053#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1054
drh413c3d32010-02-23 20:11:56 +00001055/*
1056** Log an error that is an API call on a connection pointer that should
1057** not have been used. The "type" of connection pointer is given as the
1058** argument. The zType is a word like "NULL" or "closed" or "invalid".
1059*/
1060static void logBadConnection(const char *zType){
1061 sqlite3_log(SQLITE_MISUSE,
1062 "API call with %s database connection pointer",
1063 zType
1064 );
1065}
drhc81c11f2009-11-10 01:30:52 +00001066
1067/*
drhc81c11f2009-11-10 01:30:52 +00001068** Check to make sure we have a valid db pointer. This test is not
1069** foolproof but it does provide some measure of protection against
1070** misuse of the interface such as passing in db pointers that are
1071** NULL or which have been previously closed. If this routine returns
1072** 1 it means that the db pointer is valid and 0 if it should not be
1073** dereferenced for any reason. The calling function should invoke
1074** SQLITE_MISUSE immediately.
1075**
1076** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1077** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1078** open properly and is not fit for general use but which can be
1079** used as an argument to sqlite3_errmsg() or sqlite3_close().
1080*/
1081int sqlite3SafetyCheckOk(sqlite3 *db){
1082 u32 magic;
drh413c3d32010-02-23 20:11:56 +00001083 if( db==0 ){
1084 logBadConnection("NULL");
1085 return 0;
1086 }
drhc81c11f2009-11-10 01:30:52 +00001087 magic = db->magic;
drh9978c972010-02-23 17:36:32 +00001088 if( magic!=SQLITE_MAGIC_OPEN ){
drhe294da02010-02-25 23:44:15 +00001089 if( sqlite3SafetyCheckSickOrOk(db) ){
1090 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +00001091 logBadConnection("unopened");
1092 }
drhc81c11f2009-11-10 01:30:52 +00001093 return 0;
1094 }else{
1095 return 1;
1096 }
1097}
1098int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1099 u32 magic;
1100 magic = db->magic;
1101 if( magic!=SQLITE_MAGIC_SICK &&
1102 magic!=SQLITE_MAGIC_OPEN &&
drh413c3d32010-02-23 20:11:56 +00001103 magic!=SQLITE_MAGIC_BUSY ){
drhe294da02010-02-25 23:44:15 +00001104 testcase( sqlite3GlobalConfig.xLog!=0 );
drhaf46dc12010-02-24 21:44:07 +00001105 logBadConnection("invalid");
drh413c3d32010-02-23 20:11:56 +00001106 return 0;
1107 }else{
1108 return 1;
1109 }
drhc81c11f2009-11-10 01:30:52 +00001110}
drh158b9cb2011-03-05 20:59:46 +00001111
1112/*
1113** Attempt to add, substract, or multiply the 64-bit signed value iB against
1114** the other 64-bit signed integer at *pA and store the result in *pA.
1115** Return 0 on success. Or if the operation would have resulted in an
1116** overflow, leave *pA unchanged and return 1.
1117*/
1118int sqlite3AddInt64(i64 *pA, i64 iB){
1119 i64 iA = *pA;
1120 testcase( iA==0 ); testcase( iA==1 );
1121 testcase( iB==-1 ); testcase( iB==0 );
1122 if( iB>=0 ){
1123 testcase( iA>0 && LARGEST_INT64 - iA == iB );
1124 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
1125 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
1126 *pA += iB;
1127 }else{
1128 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
1129 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
1130 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
1131 *pA += iB;
1132 }
1133 return 0;
1134}
1135int sqlite3SubInt64(i64 *pA, i64 iB){
1136 testcase( iB==SMALLEST_INT64+1 );
1137 if( iB==SMALLEST_INT64 ){
1138 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
1139 if( (*pA)>=0 ) return 1;
1140 *pA -= iB;
1141 return 0;
1142 }else{
1143 return sqlite3AddInt64(pA, -iB);
1144 }
1145}
1146#define TWOPOWER32 (((i64)1)<<32)
1147#define TWOPOWER31 (((i64)1)<<31)
1148int sqlite3MulInt64(i64 *pA, i64 iB){
1149 i64 iA = *pA;
1150 i64 iA1, iA0, iB1, iB0, r;
1151
drh158b9cb2011-03-05 20:59:46 +00001152 iA1 = iA/TWOPOWER32;
1153 iA0 = iA % TWOPOWER32;
1154 iB1 = iB/TWOPOWER32;
1155 iB0 = iB % TWOPOWER32;
1156 if( iA1*iB1 != 0 ) return 1;
drhd7255a22011-03-05 21:41:34 +00001157 assert( iA1*iB0==0 || iA0*iB1==0 );
1158 r = iA1*iB0 + iA0*iB1;
drh158b9cb2011-03-05 20:59:46 +00001159 testcase( r==(-TWOPOWER31)-1 );
1160 testcase( r==(-TWOPOWER31) );
1161 testcase( r==TWOPOWER31 );
1162 testcase( r==TWOPOWER31-1 );
1163 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
1164 r *= TWOPOWER32;
1165 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
1166 *pA = r;
1167 return 0;
1168}
drhd50ffc42011-03-08 02:38:28 +00001169
1170/*
1171** Compute the absolute value of a 32-bit signed integer, of possible. Or
1172** if the integer has a value of -2147483648, return +2147483647
1173*/
1174int sqlite3AbsInt32(int x){
1175 if( x>=0 ) return x;
drh87e79ae2011-03-08 13:06:41 +00001176 if( x==(int)0x80000000 ) return 0x7fffffff;
drhd50ffc42011-03-08 02:38:28 +00001177 return -x;
1178}
drh81cc5162011-05-17 20:36:21 +00001179
1180#ifdef SQLITE_ENABLE_8_3_NAMES
1181/*
drhb51bf432011-07-21 21:29:35 +00001182** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
drh81cc5162011-05-17 20:36:21 +00001183** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
1184** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
1185** three characters, then shorten the suffix on z[] to be the last three
1186** characters of the original suffix.
1187**
drhb51bf432011-07-21 21:29:35 +00001188** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
1189** do the suffix shortening regardless of URI parameter.
1190**
drh81cc5162011-05-17 20:36:21 +00001191** Examples:
1192**
1193** test.db-journal => test.nal
1194** test.db-wal => test.wal
1195** test.db-shm => test.shm
drhf5808602011-12-16 00:33:04 +00001196** test.db-mj7f3319fa => test.9fa
drh81cc5162011-05-17 20:36:21 +00001197*/
1198void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
drhb51bf432011-07-21 21:29:35 +00001199#if SQLITE_ENABLE_8_3_NAMES<2
drh7d39e172012-01-02 12:41:53 +00001200 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
drhb51bf432011-07-21 21:29:35 +00001201#endif
1202 {
drh81cc5162011-05-17 20:36:21 +00001203 int i, sz;
1204 sz = sqlite3Strlen30(z);
drhc83f2d42011-05-18 02:41:10 +00001205 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
drhc02a43a2012-01-10 23:18:38 +00001206 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
drh81cc5162011-05-17 20:36:21 +00001207 }
1208}
1209#endif
drhbf539c42013-10-05 18:16:02 +00001210
1211/*
1212** Find (an approximate) sum of two LogEst values. This computation is
1213** not a simple "+" operator because LogEst is stored as a logarithmic
1214** value.
1215**
1216*/
1217LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
1218 static const unsigned char x[] = {
1219 10, 10, /* 0,1 */
1220 9, 9, /* 2,3 */
1221 8, 8, /* 4,5 */
1222 7, 7, 7, /* 6,7,8 */
1223 6, 6, 6, /* 9,10,11 */
1224 5, 5, 5, /* 12-14 */
1225 4, 4, 4, 4, /* 15-18 */
1226 3, 3, 3, 3, 3, 3, /* 19-24 */
1227 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
1228 };
1229 if( a>=b ){
1230 if( a>b+49 ) return a;
1231 if( a>b+31 ) return a+1;
1232 return a+x[a-b];
1233 }else{
1234 if( b>a+49 ) return b;
1235 if( b>a+31 ) return b+1;
1236 return b+x[b-a];
1237 }
1238}
1239
1240/*
1241** Convert an integer into a LogEst. In other words, compute a
1242** good approximatation for 10*log2(x).
1243*/
1244LogEst sqlite3LogEst(u64 x){
1245 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
1246 LogEst y = 40;
1247 if( x<8 ){
1248 if( x<2 ) return 0;
1249 while( x<8 ){ y -= 10; x <<= 1; }
1250 }else{
1251 while( x>255 ){ y += 40; x >>= 4; }
1252 while( x>15 ){ y += 10; x >>= 1; }
1253 }
1254 return a[x&7] + y - 10;
1255}
1256
1257#ifndef SQLITE_OMIT_VIRTUALTABLE
1258/*
1259** Convert a double into a LogEst
1260** In other words, compute an approximation for 10*log2(x).
1261*/
1262LogEst sqlite3LogEstFromDouble(double x){
1263 u64 a;
1264 LogEst e;
1265 assert( sizeof(x)==8 && sizeof(a)==8 );
1266 if( x<=1 ) return 0;
1267 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
1268 memcpy(&a, &x, 8);
1269 e = (a>>52) - 1022;
1270 return e*10;
1271}
1272#endif /* SQLITE_OMIT_VIRTUALTABLE */
1273
1274/*
1275** Convert a LogEst into an integer.
1276*/
1277u64 sqlite3LogEstToInt(LogEst x){
1278 u64 n;
1279 if( x<10 ) return 1;
1280 n = x%10;
1281 x /= 10;
1282 if( n>=5 ) n -= 2;
1283 else if( n>=1 ) n -= 1;
drh47676fe2013-12-05 16:41:55 +00001284 if( x>=3 ){
1285 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
1286 }
drhbf539c42013-10-05 18:16:02 +00001287 return (n+8)>>(3-x);
1288}