blob: 325c75aae171d5fa955f11b66d062589987d9041 [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 }
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.
drh9f129f42010-08-31 15:27:32 +0000218**
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.
drhc81c11f2009-11-10 01:30:52 +0000224*/
drh3fa97302012-02-22 16:58:36 +0000225int sqlite3_stricmp(const char *zLeft, const char *zRight){
drhc81c11f2009-11-10 01:30:52 +0000226 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}
232int 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/*
drh9339da12010-09-30 00:50:49 +0000241** The string z[] is an text representation of a real number.
drh025586a2010-09-30 17:33:11 +0000242** Convert this string to a double and write it into *pResult.
drhc81c11f2009-11-10 01:30:52 +0000243**
drh9339da12010-09-30 00:50:49 +0000244** The string z[] is length bytes in length (bytes, not characters) and
245** uses the encoding enc. The string is not necessarily zero-terminated.
drhc81c11f2009-11-10 01:30:52 +0000246**
drh9339da12010-09-30 00:50:49 +0000247** Return TRUE if the result is a valid real number (or integer) and FALSE
drh025586a2010-09-30 17:33:11 +0000248** 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.
drhc81c11f2009-11-10 01:30:52 +0000261*/
drh9339da12010-09-30 00:50:49 +0000262int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
drhc81c11f2009-11-10 01:30:52 +0000263#ifndef SQLITE_OMIT_FLOATING_POINT
drh9339da12010-09-30 00:50:49 +0000264 int incr = (enc==SQLITE_UTF8?1:2);
265 const char *zEnd = z + length;
drhc81c11f2009-11-10 01:30:52 +0000266 /* sign * significand * (10 ^ (esign * exponent)) */
drh025586a2010-09-30 17:33:11 +0000267 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 */
drhc81c11f2009-11-10 01:30:52 +0000273 double result;
274 int nDigits = 0;
275
drh025586a2010-09-30 17:33:11 +0000276 *pResult = 0.0; /* Default return value, in case of an error */
277
drh9339da12010-09-30 00:50:49 +0000278 if( enc==SQLITE_UTF16BE ) z++;
279
drhc81c11f2009-11-10 01:30:52 +0000280 /* skip leading spaces */
drh9339da12010-09-30 00:50:49 +0000281 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
drh025586a2010-09-30 17:33:11 +0000282 if( z>=zEnd ) return 0;
drh9339da12010-09-30 00:50:49 +0000283
drhc81c11f2009-11-10 01:30:52 +0000284 /* get sign of significand */
285 if( *z=='-' ){
286 sign = -1;
drh9339da12010-09-30 00:50:49 +0000287 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000288 }else if( *z=='+' ){
drh9339da12010-09-30 00:50:49 +0000289 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000290 }
drh9339da12010-09-30 00:50:49 +0000291
drhc81c11f2009-11-10 01:30:52 +0000292 /* skip leading zeroes */
drh9339da12010-09-30 00:50:49 +0000293 while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000294
295 /* copy max significant digits to significand */
drh9339da12010-09-30 00:50:49 +0000296 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
drhc81c11f2009-11-10 01:30:52 +0000297 s = s*10 + (*z - '0');
drh9339da12010-09-30 00:50:49 +0000298 z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000299 }
drh9339da12010-09-30 00:50:49 +0000300
drhc81c11f2009-11-10 01:30:52 +0000301 /* skip non-significant significand digits
302 ** (increase exponent by d to shift decimal left) */
drh9339da12010-09-30 00:50:49 +0000303 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
304 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000305
306 /* if decimal point is present */
307 if( *z=='.' ){
drh9339da12010-09-30 00:50:49 +0000308 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000309 /* copy digits from after decimal to significand
310 ** (decrease exponent by d to shift decimal right) */
drh9339da12010-09-30 00:50:49 +0000311 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
drhc81c11f2009-11-10 01:30:52 +0000312 s = s*10 + (*z - '0');
drh9339da12010-09-30 00:50:49 +0000313 z+=incr, nDigits++, d--;
drhc81c11f2009-11-10 01:30:52 +0000314 }
315 /* skip non-significant digits */
drh9339da12010-09-30 00:50:49 +0000316 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
drhc81c11f2009-11-10 01:30:52 +0000317 }
drh9339da12010-09-30 00:50:49 +0000318 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000319
320 /* if exponent is present */
321 if( *z=='e' || *z=='E' ){
drh9339da12010-09-30 00:50:49 +0000322 z+=incr;
drh025586a2010-09-30 17:33:11 +0000323 eValid = 0;
drh9339da12010-09-30 00:50:49 +0000324 if( z>=zEnd ) goto do_atof_calc;
drhc81c11f2009-11-10 01:30:52 +0000325 /* get sign of exponent */
326 if( *z=='-' ){
327 esign = -1;
drh9339da12010-09-30 00:50:49 +0000328 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000329 }else if( *z=='+' ){
drh9339da12010-09-30 00:50:49 +0000330 z+=incr;
drhc81c11f2009-11-10 01:30:52 +0000331 }
332 /* copy digits to exponent */
drh9339da12010-09-30 00:50:49 +0000333 while( z<zEnd && sqlite3Isdigit(*z) ){
drh57db4a72011-10-17 20:41:46 +0000334 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
drh9339da12010-09-30 00:50:49 +0000335 z+=incr;
drh025586a2010-09-30 17:33:11 +0000336 eValid = 1;
drhc81c11f2009-11-10 01:30:52 +0000337 }
338 }
339
drh025586a2010-09-30 17:33:11 +0000340 /* skip trailing spaces */
341 if( nDigits && eValid ){
342 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
343 }
344
drh9339da12010-09-30 00:50:49 +0000345do_atof_calc:
drhc81c11f2009-11-10 01:30:52 +0000346 /* 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 }
drh2458a2e2011-10-17 12:14:26 +0000385 }else if( e>=342 ){
386 if( esign<0 ){
387 result = 0.0*s;
388 }else{
389 result = 1e308*1e308*s; /* Infinity */
390 }
drhc81c11f2009-11-10 01:30:52 +0000391 }else{
392 /* 1.0e+22 is the largest power of 10 than can be
393 ** represented exactly. */
394 while( e%22 ) { scale *= 1.0e+1; e -= 1; }
395 while( e>0 ) { scale *= 1.0e+22; e -= 22; }
396 if( esign<0 ){
397 result = s / scale;
398 }else{
399 result = s * scale;
400 }
401 }
402 } else {
403 result = (double)s;
404 }
405 }
406
407 /* store the result */
408 *pResult = result;
409
drh025586a2010-09-30 17:33:11 +0000410 /* return true if number and no extra non-whitespace chracters after */
411 return z>=zEnd && nDigits>0 && eValid;
drhc81c11f2009-11-10 01:30:52 +0000412#else
shaneh5f1d6b62010-09-30 16:51:25 +0000413 return !sqlite3Atoi64(z, pResult, length, enc);
drhc81c11f2009-11-10 01:30:52 +0000414#endif /* SQLITE_OMIT_FLOATING_POINT */
415}
416
417/*
418** Compare the 19-character string zNum against the text representation
419** value 2^63: 9223372036854775808. Return negative, zero, or positive
420** if zNum is less than, equal to, or greater than the string.
shaneh5f1d6b62010-09-30 16:51:25 +0000421** Note that zNum must contain exactly 19 characters.
drhc81c11f2009-11-10 01:30:52 +0000422**
423** Unlike memcmp() this routine is guaranteed to return the difference
424** in the values of the last digit if the only difference is in the
425** last digit. So, for example,
426**
drh9339da12010-09-30 00:50:49 +0000427** compare2pow63("9223372036854775800", 1)
drhc81c11f2009-11-10 01:30:52 +0000428**
429** will return -8.
430*/
drh9339da12010-09-30 00:50:49 +0000431static int compare2pow63(const char *zNum, int incr){
432 int c = 0;
433 int i;
434 /* 012345678901234567 */
435 const char *pow63 = "922337203685477580";
436 for(i=0; c==0 && i<18; i++){
437 c = (zNum[i*incr]-pow63[i])*10;
438 }
drhc81c11f2009-11-10 01:30:52 +0000439 if( c==0 ){
drh9339da12010-09-30 00:50:49 +0000440 c = zNum[18*incr] - '8';
drh44dbca82010-01-13 04:22:20 +0000441 testcase( c==(-1) );
442 testcase( c==0 );
443 testcase( c==(+1) );
drhc81c11f2009-11-10 01:30:52 +0000444 }
445 return c;
446}
447
448
449/*
drh158b9cb2011-03-05 20:59:46 +0000450** Convert zNum to a 64-bit signed integer.
451**
452** If the zNum value is representable as a 64-bit twos-complement
453** integer, then write that value into *pNum and return 0.
454**
455** If zNum is exactly 9223372036854665808, return 2. This special
456** case is broken out because while 9223372036854665808 cannot be a
457** signed 64-bit integer, its negative -9223372036854665808 can be.
458**
459** If zNum is too big for a 64-bit integer and is not
460** 9223372036854665808 then return 1.
drhc81c11f2009-11-10 01:30:52 +0000461**
drh9339da12010-09-30 00:50:49 +0000462** length is the number of bytes in the string (bytes, not characters).
463** The string is not necessarily zero-terminated. The encoding is
464** given by enc.
drhc81c11f2009-11-10 01:30:52 +0000465*/
drh9339da12010-09-30 00:50:49 +0000466int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
467 int incr = (enc==SQLITE_UTF8?1:2);
drh158b9cb2011-03-05 20:59:46 +0000468 u64 u = 0;
shaneh5f1d6b62010-09-30 16:51:25 +0000469 int neg = 0; /* assume positive */
drh9339da12010-09-30 00:50:49 +0000470 int i;
471 int c = 0;
drhc81c11f2009-11-10 01:30:52 +0000472 const char *zStart;
drh9339da12010-09-30 00:50:49 +0000473 const char *zEnd = zNum + length;
474 if( enc==SQLITE_UTF16BE ) zNum++;
475 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
drh158b9cb2011-03-05 20:59:46 +0000476 if( zNum<zEnd ){
477 if( *zNum=='-' ){
478 neg = 1;
479 zNum+=incr;
480 }else if( *zNum=='+' ){
481 zNum+=incr;
482 }
drhc81c11f2009-11-10 01:30:52 +0000483 }
484 zStart = zNum;
drh9339da12010-09-30 00:50:49 +0000485 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
486 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
drh158b9cb2011-03-05 20:59:46 +0000487 u = u*10 + c - '0';
drhc81c11f2009-11-10 01:30:52 +0000488 }
drh158b9cb2011-03-05 20:59:46 +0000489 if( u>LARGEST_INT64 ){
490 *pNum = SMALLEST_INT64;
491 }else if( neg ){
492 *pNum = -(i64)u;
493 }else{
494 *pNum = (i64)u;
495 }
drh44dbca82010-01-13 04:22:20 +0000496 testcase( i==18 );
497 testcase( i==19 );
498 testcase( i==20 );
drh9339da12010-09-30 00:50:49 +0000499 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
drhc81c11f2009-11-10 01:30:52 +0000500 /* zNum is empty or contains non-numeric text or is longer
shaneh5f1d6b62010-09-30 16:51:25 +0000501 ** than 19 digits (thus guaranteeing that it is too large) */
502 return 1;
drh9339da12010-09-30 00:50:49 +0000503 }else if( i<19*incr ){
drhc81c11f2009-11-10 01:30:52 +0000504 /* Less than 19 digits, so we know that it fits in 64 bits */
drh158b9cb2011-03-05 20:59:46 +0000505 assert( u<=LARGEST_INT64 );
shaneh5f1d6b62010-09-30 16:51:25 +0000506 return 0;
drhc81c11f2009-11-10 01:30:52 +0000507 }else{
drh158b9cb2011-03-05 20:59:46 +0000508 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
509 c = compare2pow63(zNum, incr);
510 if( c<0 ){
511 /* zNum is less than 9223372036854775808 so it fits */
512 assert( u<=LARGEST_INT64 );
513 return 0;
514 }else if( c>0 ){
515 /* zNum is greater than 9223372036854775808 so it overflows */
516 return 1;
517 }else{
518 /* zNum is exactly 9223372036854775808. Fits if negative. The
519 ** special case 2 overflow if positive */
520 assert( u-1==LARGEST_INT64 );
521 assert( (*pNum)==SMALLEST_INT64 );
522 return neg ? 0 : 2;
523 }
drhc81c11f2009-11-10 01:30:52 +0000524 }
525}
526
527/*
528** If zNum represents an integer that will fit in 32-bits, then set
529** *pValue to that integer and return true. Otherwise return false.
530**
531** Any non-numeric characters that following zNum are ignored.
532** This is different from sqlite3Atoi64() which requires the
533** input number to be zero-terminated.
534*/
535int sqlite3GetInt32(const char *zNum, int *pValue){
536 sqlite_int64 v = 0;
537 int i, c;
538 int neg = 0;
539 if( zNum[0]=='-' ){
540 neg = 1;
541 zNum++;
542 }else if( zNum[0]=='+' ){
543 zNum++;
544 }
545 while( zNum[0]=='0' ) zNum++;
546 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
547 v = v*10 + c;
548 }
549
550 /* The longest decimal representation of a 32 bit integer is 10 digits:
551 **
552 ** 1234567890
553 ** 2^31 -> 2147483648
554 */
drh44dbca82010-01-13 04:22:20 +0000555 testcase( i==10 );
drhc81c11f2009-11-10 01:30:52 +0000556 if( i>10 ){
557 return 0;
558 }
drh44dbca82010-01-13 04:22:20 +0000559 testcase( v-neg==2147483647 );
drhc81c11f2009-11-10 01:30:52 +0000560 if( v-neg>2147483647 ){
561 return 0;
562 }
563 if( neg ){
564 v = -v;
565 }
566 *pValue = (int)v;
567 return 1;
568}
569
570/*
drh60ac3f42010-11-23 18:59:27 +0000571** Return a 32-bit integer value extracted from a string. If the
572** string is not an integer, just return 0.
573*/
574int sqlite3Atoi(const char *z){
575 int x = 0;
576 if( z ) sqlite3GetInt32(z, &x);
577 return x;
578}
579
580/*
drhc81c11f2009-11-10 01:30:52 +0000581** The variable-length integer encoding is as follows:
582**
583** KEY:
584** A = 0xxxxxxx 7 bits of data and one flag bit
585** B = 1xxxxxxx 7 bits of data and one flag bit
586** C = xxxxxxxx 8 bits of data
587**
588** 7 bits - A
589** 14 bits - BA
590** 21 bits - BBA
591** 28 bits - BBBA
592** 35 bits - BBBBA
593** 42 bits - BBBBBA
594** 49 bits - BBBBBBA
595** 56 bits - BBBBBBBA
596** 64 bits - BBBBBBBBC
597*/
598
599/*
600** Write a 64-bit variable-length integer to memory starting at p[0].
601** The length of data write will be between 1 and 9 bytes. The number
602** of bytes written is returned.
603**
604** A variable-length integer consists of the lower 7 bits of each byte
605** for all bytes that have the 8th bit set and one byte with the 8th
606** bit clear. Except, if we get to the 9th byte, it stores the full
607** 8 bits and is the last byte.
608*/
609int sqlite3PutVarint(unsigned char *p, u64 v){
610 int i, j, n;
611 u8 buf[10];
612 if( v & (((u64)0xff000000)<<32) ){
613 p[8] = (u8)v;
614 v >>= 8;
615 for(i=7; i>=0; i--){
616 p[i] = (u8)((v & 0x7f) | 0x80);
617 v >>= 7;
618 }
619 return 9;
620 }
621 n = 0;
622 do{
623 buf[n++] = (u8)((v & 0x7f) | 0x80);
624 v >>= 7;
625 }while( v!=0 );
626 buf[0] &= 0x7f;
627 assert( n<=9 );
628 for(i=0, j=n-1; j>=0; j--, i++){
629 p[i] = buf[j];
630 }
631 return n;
632}
633
634/*
635** This routine is a faster version of sqlite3PutVarint() that only
636** works for 32-bit positive integers and which is optimized for
637** the common case of small integers. A MACRO version, putVarint32,
638** is provided which inlines the single-byte case. All code should use
639** the MACRO version as this function assumes the single-byte case has
640** already been handled.
641*/
642int sqlite3PutVarint32(unsigned char *p, u32 v){
643#ifndef putVarint32
644 if( (v & ~0x7f)==0 ){
645 p[0] = v;
646 return 1;
647 }
648#endif
649 if( (v & ~0x3fff)==0 ){
650 p[0] = (u8)((v>>7) | 0x80);
651 p[1] = (u8)(v & 0x7f);
652 return 2;
653 }
654 return sqlite3PutVarint(p, v);
655}
656
657/*
drh0b2864c2010-03-03 15:18:38 +0000658** Bitmasks used by sqlite3GetVarint(). These precomputed constants
659** are defined here rather than simply putting the constant expressions
660** inline in order to work around bugs in the RVT compiler.
661**
662** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
663**
664** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
665*/
666#define SLOT_2_0 0x001fc07f
667#define SLOT_4_2_0 0xf01fc07f
668
669
670/*
drhc81c11f2009-11-10 01:30:52 +0000671** Read a 64-bit variable-length integer from memory starting at p[0].
672** Return the number of bytes read. The value is stored in *v.
673*/
674u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
675 u32 a,b,s;
676
677 a = *p;
678 /* a: p0 (unmasked) */
679 if (!(a&0x80))
680 {
681 *v = a;
682 return 1;
683 }
684
685 p++;
686 b = *p;
687 /* b: p1 (unmasked) */
688 if (!(b&0x80))
689 {
690 a &= 0x7f;
691 a = a<<7;
692 a |= b;
693 *v = a;
694 return 2;
695 }
696
drh0b2864c2010-03-03 15:18:38 +0000697 /* Verify that constants are precomputed correctly */
698 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
shaneh1da207e2010-03-09 14:41:12 +0000699 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
drh0b2864c2010-03-03 15:18:38 +0000700
drhc81c11f2009-11-10 01:30:52 +0000701 p++;
702 a = a<<14;
703 a |= *p;
704 /* a: p0<<14 | p2 (unmasked) */
705 if (!(a&0x80))
706 {
drh0b2864c2010-03-03 15:18:38 +0000707 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000708 b &= 0x7f;
709 b = b<<7;
710 a |= b;
711 *v = a;
712 return 3;
713 }
714
715 /* CSE1 from below */
drh0b2864c2010-03-03 15:18:38 +0000716 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000717 p++;
718 b = b<<14;
719 b |= *p;
720 /* b: p1<<14 | p3 (unmasked) */
721 if (!(b&0x80))
722 {
drh0b2864c2010-03-03 15:18:38 +0000723 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000724 /* moved CSE1 up */
725 /* a &= (0x7f<<14)|(0x7f); */
726 a = a<<7;
727 a |= b;
728 *v = a;
729 return 4;
730 }
731
732 /* a: p0<<14 | p2 (masked) */
733 /* b: p1<<14 | p3 (unmasked) */
734 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
735 /* moved CSE1 up */
736 /* a &= (0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000737 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000738 s = a;
739 /* s: p0<<14 | p2 (masked) */
740
741 p++;
742 a = a<<14;
743 a |= *p;
744 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
745 if (!(a&0x80))
746 {
747 /* we can skip these cause they were (effectively) done above in calc'ing s */
748 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
749 /* b &= (0x7f<<14)|(0x7f); */
750 b = b<<7;
751 a |= b;
752 s = s>>18;
753 *v = ((u64)s)<<32 | a;
754 return 5;
755 }
756
757 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
758 s = s<<7;
759 s |= b;
760 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
761
762 p++;
763 b = b<<14;
764 b |= *p;
765 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
766 if (!(b&0x80))
767 {
768 /* we can skip this cause it was (effectively) done above in calc'ing s */
769 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
drh0b2864c2010-03-03 15:18:38 +0000770 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000771 a = a<<7;
772 a |= b;
773 s = s>>18;
774 *v = ((u64)s)<<32 | a;
775 return 6;
776 }
777
778 p++;
779 a = a<<14;
780 a |= *p;
781 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
782 if (!(a&0x80))
783 {
drh0b2864c2010-03-03 15:18:38 +0000784 a &= SLOT_4_2_0;
785 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000786 b = b<<7;
787 a |= b;
788 s = s>>11;
789 *v = ((u64)s)<<32 | a;
790 return 7;
791 }
792
793 /* CSE2 from below */
drh0b2864c2010-03-03 15:18:38 +0000794 a &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000795 p++;
796 b = b<<14;
797 b |= *p;
798 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
799 if (!(b&0x80))
800 {
drh0b2864c2010-03-03 15:18:38 +0000801 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000802 /* moved CSE2 up */
803 /* a &= (0x7f<<14)|(0x7f); */
804 a = a<<7;
805 a |= b;
806 s = s>>4;
807 *v = ((u64)s)<<32 | a;
808 return 8;
809 }
810
811 p++;
812 a = a<<15;
813 a |= *p;
814 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
815
816 /* moved CSE2 up */
817 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
drh0b2864c2010-03-03 15:18:38 +0000818 b &= SLOT_2_0;
drhc81c11f2009-11-10 01:30:52 +0000819 b = b<<8;
820 a |= b;
821
822 s = s<<4;
823 b = p[-4];
824 b &= 0x7f;
825 b = b>>3;
826 s |= b;
827
828 *v = ((u64)s)<<32 | a;
829
830 return 9;
831}
832
833/*
834** Read a 32-bit variable-length integer from memory starting at p[0].
835** Return the number of bytes read. The value is stored in *v.
836**
837** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
838** integer, then set *v to 0xffffffff.
839**
840** A MACRO version, getVarint32, is provided which inlines the
841** single-byte case. All code should use the MACRO version as
842** this function assumes the single-byte case has already been handled.
843*/
844u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
845 u32 a,b;
846
847 /* The 1-byte case. Overwhelmingly the most common. Handled inline
848 ** by the getVarin32() macro */
849 a = *p;
850 /* a: p0 (unmasked) */
851#ifndef getVarint32
852 if (!(a&0x80))
853 {
854 /* Values between 0 and 127 */
855 *v = a;
856 return 1;
857 }
858#endif
859
860 /* The 2-byte case */
861 p++;
862 b = *p;
863 /* b: p1 (unmasked) */
864 if (!(b&0x80))
865 {
866 /* Values between 128 and 16383 */
867 a &= 0x7f;
868 a = a<<7;
869 *v = a | b;
870 return 2;
871 }
872
873 /* The 3-byte case */
874 p++;
875 a = a<<14;
876 a |= *p;
877 /* a: p0<<14 | p2 (unmasked) */
878 if (!(a&0x80))
879 {
880 /* Values between 16384 and 2097151 */
881 a &= (0x7f<<14)|(0x7f);
882 b &= 0x7f;
883 b = b<<7;
884 *v = a | b;
885 return 3;
886 }
887
888 /* A 32-bit varint is used to store size information in btrees.
889 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
890 ** A 3-byte varint is sufficient, for example, to record the size
891 ** of a 1048569-byte BLOB or string.
892 **
893 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
894 ** rare larger cases can be handled by the slower 64-bit varint
895 ** routine.
896 */
897#if 1
898 {
899 u64 v64;
900 u8 n;
901
902 p -= 2;
903 n = sqlite3GetVarint(p, &v64);
904 assert( n>3 && n<=9 );
905 if( (v64 & SQLITE_MAX_U32)!=v64 ){
906 *v = 0xffffffff;
907 }else{
908 *v = (u32)v64;
909 }
910 return n;
911 }
912
913#else
914 /* For following code (kept for historical record only) shows an
915 ** unrolling for the 3- and 4-byte varint cases. This code is
916 ** slightly faster, but it is also larger and much harder to test.
917 */
918 p++;
919 b = b<<14;
920 b |= *p;
921 /* b: p1<<14 | p3 (unmasked) */
922 if (!(b&0x80))
923 {
924 /* Values between 2097152 and 268435455 */
925 b &= (0x7f<<14)|(0x7f);
926 a &= (0x7f<<14)|(0x7f);
927 a = a<<7;
928 *v = a | b;
929 return 4;
930 }
931
932 p++;
933 a = a<<14;
934 a |= *p;
935 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
936 if (!(a&0x80))
937 {
dan3bbe7612010-03-03 16:02:05 +0000938 /* Values between 268435456 and 34359738367 */
939 a &= SLOT_4_2_0;
940 b &= SLOT_4_2_0;
drhc81c11f2009-11-10 01:30:52 +0000941 b = b<<7;
942 *v = a | b;
943 return 5;
944 }
945
946 /* We can only reach this point when reading a corrupt database
947 ** file. In that case we are not in any hurry. Use the (relatively
948 ** slow) general-purpose sqlite3GetVarint() routine to extract the
949 ** value. */
950 {
951 u64 v64;
952 u8 n;
953
954 p -= 4;
955 n = sqlite3GetVarint(p, &v64);
956 assert( n>5 && n<=9 );
957 *v = (u32)v64;
958 return n;
959 }
960#endif
961}
962
963/*
964** Return the number of bytes that will be needed to store the given
965** 64-bit integer.
966*/
967int sqlite3VarintLen(u64 v){
968 int i = 0;
969 do{
970 i++;
971 v >>= 7;
972 }while( v!=0 && ALWAYS(i<9) );
973 return i;
974}
975
976
977/*
978** Read or write a four-byte big-endian integer value.
979*/
980u32 sqlite3Get4byte(const u8 *p){
981 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
982}
983void sqlite3Put4byte(unsigned char *p, u32 v){
984 p[0] = (u8)(v>>24);
985 p[1] = (u8)(v>>16);
986 p[2] = (u8)(v>>8);
987 p[3] = (u8)v;
988}
989
990
991
drhc81c11f2009-11-10 01:30:52 +0000992/*
993** Translate a single byte of Hex into an integer.
994** This routine only works if h really is a valid hexadecimal
995** character: 0..9a..fA..F
996*/
dancd74b612011-04-22 19:37:32 +0000997u8 sqlite3HexToInt(int h){
drhc81c11f2009-11-10 01:30:52 +0000998 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
999#ifdef SQLITE_ASCII
1000 h += 9*(1&(h>>6));
1001#endif
1002#ifdef SQLITE_EBCDIC
1003 h += 9*(1&~(h>>4));
1004#endif
1005 return (u8)(h & 0xf);
1006}
drhc81c11f2009-11-10 01:30:52 +00001007
1008#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1009/*
1010** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1011** value. Return a pointer to its binary value. Space to hold the
1012** binary value has been obtained from malloc and must be freed by
1013** the calling routine.
1014*/
1015void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
1016 char *zBlob;
1017 int i;
1018
1019 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
1020 n--;
1021 if( zBlob ){
1022 for(i=0; i<n; i+=2){
dancd74b612011-04-22 19:37:32 +00001023 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
drhc81c11f2009-11-10 01:30:52 +00001024 }
1025 zBlob[i/2] = 0;
1026 }
1027 return zBlob;
1028}
1029#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1030
drh413c3d32010-02-23 20:11:56 +00001031/*
1032** Log an error that is an API call on a connection pointer that should
1033** not have been used. The "type" of connection pointer is given as the
1034** argument. The zType is a word like "NULL" or "closed" or "invalid".
1035*/
1036static void logBadConnection(const char *zType){
1037 sqlite3_log(SQLITE_MISUSE,
1038 "API call with %s database connection pointer",
1039 zType
1040 );
1041}
drhc81c11f2009-11-10 01:30:52 +00001042
1043/*
drhc81c11f2009-11-10 01:30:52 +00001044** Check to make sure we have a valid db pointer. This test is not
1045** foolproof but it does provide some measure of protection against
1046** misuse of the interface such as passing in db pointers that are
1047** NULL or which have been previously closed. If this routine returns
1048** 1 it means that the db pointer is valid and 0 if it should not be
1049** dereferenced for any reason. The calling function should invoke
1050** SQLITE_MISUSE immediately.
1051**
1052** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1053** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1054** open properly and is not fit for general use but which can be
1055** used as an argument to sqlite3_errmsg() or sqlite3_close().
1056*/
1057int sqlite3SafetyCheckOk(sqlite3 *db){
1058 u32 magic;
drh413c3d32010-02-23 20:11:56 +00001059 if( db==0 ){
1060 logBadConnection("NULL");
1061 return 0;
1062 }
drhc81c11f2009-11-10 01:30:52 +00001063 magic = db->magic;
drh9978c972010-02-23 17:36:32 +00001064 if( magic!=SQLITE_MAGIC_OPEN ){
drhe294da02010-02-25 23:44:15 +00001065 if( sqlite3SafetyCheckSickOrOk(db) ){
1066 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +00001067 logBadConnection("unopened");
1068 }
drhc81c11f2009-11-10 01:30:52 +00001069 return 0;
1070 }else{
1071 return 1;
1072 }
1073}
1074int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1075 u32 magic;
1076 magic = db->magic;
1077 if( magic!=SQLITE_MAGIC_SICK &&
1078 magic!=SQLITE_MAGIC_OPEN &&
drh413c3d32010-02-23 20:11:56 +00001079 magic!=SQLITE_MAGIC_BUSY ){
drhe294da02010-02-25 23:44:15 +00001080 testcase( sqlite3GlobalConfig.xLog!=0 );
drhaf46dc12010-02-24 21:44:07 +00001081 logBadConnection("invalid");
drh413c3d32010-02-23 20:11:56 +00001082 return 0;
1083 }else{
1084 return 1;
1085 }
drhc81c11f2009-11-10 01:30:52 +00001086}
drh158b9cb2011-03-05 20:59:46 +00001087
1088/*
1089** Attempt to add, substract, or multiply the 64-bit signed value iB against
1090** the other 64-bit signed integer at *pA and store the result in *pA.
1091** Return 0 on success. Or if the operation would have resulted in an
1092** overflow, leave *pA unchanged and return 1.
1093*/
1094int sqlite3AddInt64(i64 *pA, i64 iB){
1095 i64 iA = *pA;
1096 testcase( iA==0 ); testcase( iA==1 );
1097 testcase( iB==-1 ); testcase( iB==0 );
1098 if( iB>=0 ){
1099 testcase( iA>0 && LARGEST_INT64 - iA == iB );
1100 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
1101 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
1102 *pA += iB;
1103 }else{
1104 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
1105 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
1106 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
1107 *pA += iB;
1108 }
1109 return 0;
1110}
1111int sqlite3SubInt64(i64 *pA, i64 iB){
1112 testcase( iB==SMALLEST_INT64+1 );
1113 if( iB==SMALLEST_INT64 ){
1114 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
1115 if( (*pA)>=0 ) return 1;
1116 *pA -= iB;
1117 return 0;
1118 }else{
1119 return sqlite3AddInt64(pA, -iB);
1120 }
1121}
1122#define TWOPOWER32 (((i64)1)<<32)
1123#define TWOPOWER31 (((i64)1)<<31)
1124int sqlite3MulInt64(i64 *pA, i64 iB){
1125 i64 iA = *pA;
1126 i64 iA1, iA0, iB1, iB0, r;
1127
drh158b9cb2011-03-05 20:59:46 +00001128 iA1 = iA/TWOPOWER32;
1129 iA0 = iA % TWOPOWER32;
1130 iB1 = iB/TWOPOWER32;
1131 iB0 = iB % TWOPOWER32;
1132 if( iA1*iB1 != 0 ) return 1;
drhd7255a22011-03-05 21:41:34 +00001133 assert( iA1*iB0==0 || iA0*iB1==0 );
1134 r = iA1*iB0 + iA0*iB1;
drh158b9cb2011-03-05 20:59:46 +00001135 testcase( r==(-TWOPOWER31)-1 );
1136 testcase( r==(-TWOPOWER31) );
1137 testcase( r==TWOPOWER31 );
1138 testcase( r==TWOPOWER31-1 );
1139 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
1140 r *= TWOPOWER32;
1141 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
1142 *pA = r;
1143 return 0;
1144}
drhd50ffc42011-03-08 02:38:28 +00001145
1146/*
1147** Compute the absolute value of a 32-bit signed integer, of possible. Or
1148** if the integer has a value of -2147483648, return +2147483647
1149*/
1150int sqlite3AbsInt32(int x){
1151 if( x>=0 ) return x;
drh87e79ae2011-03-08 13:06:41 +00001152 if( x==(int)0x80000000 ) return 0x7fffffff;
drhd50ffc42011-03-08 02:38:28 +00001153 return -x;
1154}
drh81cc5162011-05-17 20:36:21 +00001155
1156#ifdef SQLITE_ENABLE_8_3_NAMES
1157/*
drhb51bf432011-07-21 21:29:35 +00001158** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
drh81cc5162011-05-17 20:36:21 +00001159** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
1160** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
1161** three characters, then shorten the suffix on z[] to be the last three
1162** characters of the original suffix.
1163**
drhb51bf432011-07-21 21:29:35 +00001164** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
1165** do the suffix shortening regardless of URI parameter.
1166**
drh81cc5162011-05-17 20:36:21 +00001167** Examples:
1168**
1169** test.db-journal => test.nal
1170** test.db-wal => test.wal
1171** test.db-shm => test.shm
drhf5808602011-12-16 00:33:04 +00001172** test.db-mj7f3319fa => test.9fa
drh81cc5162011-05-17 20:36:21 +00001173*/
1174void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
drhb51bf432011-07-21 21:29:35 +00001175#if SQLITE_ENABLE_8_3_NAMES<2
drh7d39e172012-01-02 12:41:53 +00001176 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
drhb51bf432011-07-21 21:29:35 +00001177#endif
1178 {
drh81cc5162011-05-17 20:36:21 +00001179 int i, sz;
1180 sz = sqlite3Strlen30(z);
drhc83f2d42011-05-18 02:41:10 +00001181 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
drhc02a43a2012-01-10 23:18:38 +00001182 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
drh81cc5162011-05-17 20:36:21 +00001183 }
1184}
1185#endif