blob: d8a63288623efaa896ce695478a9ca7183d74164 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Utility functions used throughout sqlite.
13**
14** This file contains functions for allocating memory, comparing
15** strings, and stuff like that.
16**
danielk197700e13612008-11-17 19:18:54 +000017** $Id: util.c,v 1.242 2008/11/17 19:18:55 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
drh75897232000-05-29 14:26:00 +000023
24/*
shane791ea932008-07-22 05:15:52 +000025** Return true if the floating point value is Not a Number (NaN).
drh0de3ae92008-04-28 16:55:26 +000026*/
27int sqlite3IsNaN(double x){
drh8a54f9f2008-05-09 13:47:58 +000028 /* This NaN test sometimes fails if compiled on GCC with -ffast-math.
29 ** On the other hand, the use of -ffast-math comes with the following
30 ** warning:
31 **
32 ** This option [-ffast-math] should never be turned on by any
33 ** -O option since it can result in incorrect output for programs
34 ** which depend on an exact implementation of IEEE or ISO
35 ** rules/specifications for math functions.
shane791ea932008-07-22 05:15:52 +000036 **
37 ** Under MSVC, this NaN test may fail if compiled with a floating-
38 ** point precision mode other than /fp:precise. From the MSDN
39 ** documentation:
40 **
41 ** The compiler [with /fp:precise] will properly handle comparisons
42 ** involving NaN. For example, x != x evaluates to true if x is NaN
43 ** ...
drh8a54f9f2008-05-09 13:47:58 +000044 */
drh8c4d5b22008-07-06 00:21:35 +000045#ifdef __FAST_MATH__
46# error SQLite will not work correctly with the -ffast-math option of GCC.
47#endif
drh0de3ae92008-04-28 16:55:26 +000048 volatile double y = x;
drh17a7fa62008-07-11 16:19:09 +000049 volatile double z = y;
50 return y!=z;
drh0de3ae92008-04-28 16:55:26 +000051}
52
53/*
drh0a687d12008-07-08 14:52:07 +000054** Return the length of a string, except do not allow the string length
55** to exceed the SQLITE_LIMIT_LENGTH setting.
56*/
57int sqlite3Strlen(sqlite3 *db, const char *z){
58 const char *z2 = z;
drh513c3312008-07-24 17:06:48 +000059 int len;
danielk197700e13612008-11-17 19:18:54 +000060 int x;
drh0a687d12008-07-08 14:52:07 +000061 while( *z2 ){ z2++; }
drh513c3312008-07-24 17:06:48 +000062 x = z2 - z;
63 len = 0x7fffffff & x;
64 if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh0a687d12008-07-08 14:52:07 +000065 return db->aLimit[SQLITE_LIMIT_LENGTH];
66 }else{
drh513c3312008-07-24 17:06:48 +000067 return len;
drh0a687d12008-07-08 14:52:07 +000068 }
69}
70
71/*
danielk19776622cce2004-05-20 11:00:52 +000072** Set the most recent error code and error string for the sqlite
73** handle "db". The error code is set to "err_code".
74**
75** If it is not NULL, string zFormat specifies the format of the
76** error string in the style of the printf functions: The following
77** format characters are allowed:
78**
79** %s Insert a string
80** %z A string that should be freed after use
81** %d Insert an integer
82** %T Insert a token
83** %S Insert the first element of a SrcList
84**
85** zFormat and any string tokens that follow it are assumed to be
86** encoded in UTF-8.
87**
danielk19770bb8f362005-05-23 13:00:57 +000088** To clear the most recent error for sqlite handle "db", sqlite3Error
danielk19776622cce2004-05-20 11:00:52 +000089** should be called with err_code set to SQLITE_OK and zFormat set
90** to NULL.
91*/
drh9bb575f2004-09-06 17:24:11 +000092void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
danielk19771e536952007-08-16 10:09:01 +000093 if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
danielk1977bfd6cce2004-06-18 04:24:54 +000094 db->errCode = err_code;
95 if( zFormat ){
96 char *z;
97 va_list ap;
98 va_start(ap, zFormat);
danielk19771e536952007-08-16 10:09:01 +000099 z = sqlite3VMPrintf(db, zFormat, ap);
danielk1977bfd6cce2004-06-18 04:24:54 +0000100 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000101 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
danielk1977bfd6cce2004-06-18 04:24:54 +0000102 }else{
drhb21c8cd2007-08-21 19:33:56 +0000103 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
danielk1977bfd6cce2004-06-18 04:24:54 +0000104 }
danielk19776622cce2004-05-20 11:00:52 +0000105 }
106}
107
108/*
drhda93d232003-03-31 02:12:46 +0000109** Add an error message to pParse->zErrMsg and increment pParse->nErr.
110** The following formatting characters are allowed:
111**
112** %s Insert a string
113** %z A string that should be freed after use
114** %d Insert an integer
115** %T Insert a token
116** %S Insert the first element of a SrcList
danielk19779e6db7d2004-06-21 08:18:51 +0000117**
118** This function should be used to report any error that occurs whilst
119** compiling an SQL statement (i.e. within sqlite3_prepare()). The
120** last thing the sqlite3_prepare() function does is copy the error
121** stored by this function into the database handle using sqlite3Error().
122** Function sqlite3Error() should be used during statement execution
123** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +0000124*/
danielk19774adee202004-05-08 08:23:19 +0000125void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000126 va_list ap;
drh633e6d52008-07-28 19:34:53 +0000127 sqlite3 *db = pParse->db;
drhda93d232003-03-31 02:12:46 +0000128 pParse->nErr++;
drh633e6d52008-07-28 19:34:53 +0000129 sqlite3DbFree(db, pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000130 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53 +0000131 pParse->zErrMsg = sqlite3VMPrintf(db, zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000132 va_end(ap);
drh7e326c02007-05-15 16:51:37 +0000133 if( pParse->rc==SQLITE_OK ){
134 pParse->rc = SQLITE_ERROR;
135 }
drhda93d232003-03-31 02:12:46 +0000136}
137
138/*
drha0733842005-12-29 01:11:36 +0000139** Clear the error message in pParse, if any
140*/
141void sqlite3ErrorClear(Parse *pParse){
drh633e6d52008-07-28 19:34:53 +0000142 sqlite3DbFree(pParse->db, pParse->zErrMsg);
drha0733842005-12-29 01:11:36 +0000143 pParse->zErrMsg = 0;
144 pParse->nErr = 0;
145}
146
147/*
drh982cef72000-05-30 16:27:03 +0000148** Convert an SQL-style quoted string into a normal string by removing
149** the quote characters. The conversion is done in-place. If the
150** input does not begin with a quote character, then this routine
151** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000152**
153** 2002-Feb-14: This routine is extended to remove MS-Access style
154** brackets from around identifers. For example: "[a-b-c]" becomes
155** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000156*/
danielk19774adee202004-05-08 08:23:19 +0000157void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000158 int quote;
159 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000160 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000161 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000162 switch( quote ){
163 case '\'': break;
164 case '"': break;
drh3d946622005-08-13 18:15:42 +0000165 case '`': break; /* For MySQL compatibility */
166 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
drh2f4392f2002-02-14 21:42:51 +0000167 default: return;
168 }
drh982cef72000-05-30 16:27:03 +0000169 for(i=1, j=0; z[i]; i++){
170 if( z[i]==quote ){
171 if( z[i+1]==quote ){
172 z[j++] = quote;
173 i++;
174 }else{
175 z[j++] = 0;
176 break;
177 }
178 }else{
179 z[j++] = z[i];
180 }
181 }
182}
183
drh40257ff2008-06-13 18:24:27 +0000184/* Convenient short-hand */
drh4e5ffc52004-08-31 00:52:37 +0000185#define UpperToLower sqlite3UpperToLower
drh75897232000-05-29 14:26:00 +0000186
187/*
drh967e8b72000-06-21 13:59:10 +0000188** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000189** there is no consistency, we will define our own.
190*/
danielk19774adee202004-05-08 08:23:19 +0000191int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000192 register unsigned char *a, *b;
193 a = (unsigned char *)zLeft;
194 b = (unsigned char *)zRight;
195 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drh2b735012004-07-15 13:23:21 +0000196 return UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000197}
danielk19774adee202004-05-08 08:23:19 +0000198int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000199 register unsigned char *a, *b;
200 a = (unsigned char *)zLeft;
201 b = (unsigned char *)zRight;
202 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000203 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000204}
205
drha5c2ad02000-09-14 01:21:10 +0000206/*
drh7a7c7392001-11-24 00:31:46 +0000207** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000208** string contains any character which is not part of a number. If
209** the string is numeric and contains the '.' character, set *realnum
210** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000211**
drh873cdcb2004-09-05 23:23:41 +0000212** An empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000213*/
danielk19778a6b5412004-05-24 07:04:25 +0000214int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000215 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000216 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000217 if( *z=='-' || *z=='+' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000218 if( !isdigit(*(u8*)z) ){
drhbb07e9a2003-04-16 02:17:35 +0000219 return 0;
drha5c2ad02000-09-14 01:21:10 +0000220 }
danielk19778a6b5412004-05-24 07:04:25 +0000221 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000222 if( realnum ) *realnum = 0;
drh4c755c02004-08-08 20:22:17 +0000223 while( isdigit(*(u8*)z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000224 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000225 z += incr;
drh4c755c02004-08-08 20:22:17 +0000226 if( !isdigit(*(u8*)z) ) return 0;
227 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000228 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000229 }
230 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000231 z += incr;
232 if( *z=='+' || *z=='-' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000233 if( !isdigit(*(u8*)z) ) return 0;
234 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000235 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000236 }
drh7a7c7392001-11-24 00:31:46 +0000237 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000238}
239
drh93a5c6b2003-12-23 02:17:35 +0000240/*
241** The string z[] is an ascii representation of a real number.
242** Convert this string to a double.
243**
244** This routine assumes that z[] really is a valid number. If it
245** is not, the result is undefined.
246**
247** This routine is used instead of the library atof() function because
248** the library atof() might want to use "," as the decimal point instead
249** of "." depending on how locale is set. But that would cause problems
250** for SQL. So this routine always uses "." regardless of locale.
251*/
drh487e2622005-06-25 18:42:14 +0000252int sqlite3AtoF(const char *z, double *pResult){
drhb37df7b2005-10-13 02:09:49 +0000253#ifndef SQLITE_OMIT_FLOATING_POINT
drh93a5c6b2003-12-23 02:17:35 +0000254 int sign = 1;
drh487e2622005-06-25 18:42:14 +0000255 const char *zBegin = z;
drh384eef32004-01-07 03:04:27 +0000256 LONGDOUBLE_TYPE v1 = 0.0;
drha06f17f2008-05-11 11:07:06 +0000257 int nSignificant = 0;
danielk19772be2be92007-05-16 17:50:45 +0000258 while( isspace(*(u8*)z) ) z++;
drh93a5c6b2003-12-23 02:17:35 +0000259 if( *z=='-' ){
260 sign = -1;
261 z++;
262 }else if( *z=='+' ){
263 z++;
264 }
drha06f17f2008-05-11 11:07:06 +0000265 while( z[0]=='0' ){
266 z++;
267 }
drh4c755c02004-08-08 20:22:17 +0000268 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000269 v1 = v1*10.0 + (*z - '0');
270 z++;
drha06f17f2008-05-11 11:07:06 +0000271 nSignificant++;
drh93a5c6b2003-12-23 02:17:35 +0000272 }
273 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000274 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000275 z++;
drha06f17f2008-05-11 11:07:06 +0000276 if( nSignificant==0 ){
277 while( z[0]=='0' ){
278 divisor *= 10.0;
279 z++;
280 }
281 }
drh4c755c02004-08-08 20:22:17 +0000282 while( isdigit(*(u8*)z) ){
drha06f17f2008-05-11 11:07:06 +0000283 if( nSignificant<18 ){
284 v1 = v1*10.0 + (*z - '0');
285 divisor *= 10.0;
286 nSignificant++;
287 }
drh93a5c6b2003-12-23 02:17:35 +0000288 z++;
289 }
290 v1 /= divisor;
291 }
292 if( *z=='e' || *z=='E' ){
293 int esign = 1;
294 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000295 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000296 z++;
297 if( *z=='-' ){
298 esign = -1;
299 z++;
300 }else if( *z=='+' ){
301 z++;
302 }
drh4c755c02004-08-08 20:22:17 +0000303 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000304 eval = eval*10 + *z - '0';
305 z++;
306 }
307 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
308 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
309 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
310 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
311 if( esign<0 ){
312 v1 /= scale;
313 }else{
314 v1 *= scale;
315 }
316 }
drh487e2622005-06-25 18:42:14 +0000317 *pResult = sign<0 ? -v1 : v1;
318 return z - zBegin;
drhb37df7b2005-10-13 02:09:49 +0000319#else
drhee858132007-05-08 20:37:38 +0000320 return sqlite3Atoi64(z, pResult);
drhb37df7b2005-10-13 02:09:49 +0000321#endif /* SQLITE_OMIT_FLOATING_POINT */
drh93a5c6b2003-12-23 02:17:35 +0000322}
323
drh202b2df2004-01-06 01:13:46 +0000324/*
drh217f4902007-06-25 17:28:00 +0000325** Compare the 19-character string zNum against the text representation
326** value 2^63: 9223372036854775808. Return negative, zero, or positive
327** if zNum is less than, equal to, or greater than the string.
328**
329** Unlike memcmp() this routine is guaranteed to return the difference
330** in the values of the last digit if the only difference is in the
331** last digit. So, for example,
332**
333** compare2pow63("9223372036854775800")
334**
335** will return -8.
336*/
337static int compare2pow63(const char *zNum){
338 int c;
339 c = memcmp(zNum,"922337203685477580",18);
340 if( c==0 ){
341 c = zNum[18] - '8';
342 }
343 return c;
344}
345
346
347/*
drhfec19aa2004-05-19 20:41:03 +0000348** Return TRUE if zNum is a 64-bit signed integer and write
349** the value of the integer into *pNum. If zNum is not an integer
350** or is an integer that is too large to be expressed with 64 bits,
drh217f4902007-06-25 17:28:00 +0000351** then return false.
drhfec19aa2004-05-19 20:41:03 +0000352**
353** When this routine was originally written it dealt with only
354** 32-bit numbers. At that time, it was much faster than the
355** atoi() library routine in RedHat 7.2.
356*/
drhb6a9ece2007-06-26 00:37:27 +0000357int sqlite3Atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000358 i64 v = 0;
359 int neg;
360 int i, c;
drh4b815922008-05-19 15:54:59 +0000361 const char *zStart;
danielk19772be2be92007-05-16 17:50:45 +0000362 while( isspace(*(u8*)zNum) ) zNum++;
drhfec19aa2004-05-19 20:41:03 +0000363 if( *zNum=='-' ){
364 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000365 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000366 }else if( *zNum=='+' ){
367 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000368 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000369 }else{
370 neg = 0;
371 }
drh4b815922008-05-19 15:54:59 +0000372 zStart = zNum;
drh217f4902007-06-25 17:28:00 +0000373 while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
drheb2e1762004-05-27 01:53:56 +0000374 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000375 v = v*10 + c - '0';
376 }
377 *pNum = neg ? -v : v;
drh4b815922008-05-19 15:54:59 +0000378 if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
drh217f4902007-06-25 17:28:00 +0000379 /* zNum is empty or contains non-numeric text or is longer
380 ** than 19 digits (thus guaranting that it is too large) */
381 return 0;
382 }else if( i<19 ){
383 /* Less than 19 digits, so we know that it fits in 64 bits */
drhfec19aa2004-05-19 20:41:03 +0000384 return 1;
drh217f4902007-06-25 17:28:00 +0000385 }else{
386 /* 19-digit numbers must be no larger than 9223372036854775807 if positive
387 ** or 9223372036854775808 if negative. Note that 9223372036854665808
388 ** is 2^63. */
389 return compare2pow63(zNum)<neg;
drhfec19aa2004-05-19 20:41:03 +0000390 }
drhfec19aa2004-05-19 20:41:03 +0000391}
392
393/*
394** The string zNum represents an integer. There might be some other
395** information following the integer too, but that part is ignored.
396** If the integer that the prefix of zNum represents will fit in a
397** 64-bit signed integer, return TRUE. Otherwise return FALSE.
398**
399** This routine returns FALSE for the string -9223372036854775808 even that
400** that number will, in theory fit in a 64-bit integer. Positive
401** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
402** false.
403*/
drh598f1342007-10-23 15:39:45 +0000404int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
drhfec19aa2004-05-19 20:41:03 +0000405 int i, c;
drh217f4902007-06-25 17:28:00 +0000406 int neg = 0;
407 if( *zNum=='-' ){
408 neg = 1;
409 zNum++;
410 }else if( *zNum=='+' ){
411 zNum++;
412 }
drh598f1342007-10-23 15:39:45 +0000413 if( negFlag ) neg = 1-neg;
drh217f4902007-06-25 17:28:00 +0000414 while( *zNum=='0' ){
415 zNum++; /* Skip leading zeros. Ticket #2454 */
416 }
drhfec19aa2004-05-19 20:41:03 +0000417 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
drh217f4902007-06-25 17:28:00 +0000418 if( i<19 ){
419 /* Guaranteed to fit if less than 19 digits */
420 return 1;
421 }else if( i>19 ){
422 /* Guaranteed to be too big if greater than 19 digits */
423 return 0;
424 }else{
425 /* Compare against 2^63. */
426 return compare2pow63(zNum)<neg;
427 }
drhfec19aa2004-05-19 20:41:03 +0000428}
429
drh217f4902007-06-25 17:28:00 +0000430/*
431** If zNum represents an integer that will fit in 32-bits, then set
432** *pValue to that integer and return true. Otherwise return false.
433**
434** Any non-numeric characters that following zNum are ignored.
drhb6a9ece2007-06-26 00:37:27 +0000435** This is different from sqlite3Atoi64() which requires the
drh217f4902007-06-25 17:28:00 +0000436** input number to be zero-terminated.
437*/
438int sqlite3GetInt32(const char *zNum, int *pValue){
439 sqlite_int64 v = 0;
440 int i, c;
441 int neg = 0;
442 if( zNum[0]=='-' ){
443 neg = 1;
444 zNum++;
445 }else if( zNum[0]=='+' ){
446 zNum++;
447 }
448 while( zNum[0]=='0' ) zNum++;
danielk1977b8cdbec2007-09-01 10:01:12 +0000449 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
drh217f4902007-06-25 17:28:00 +0000450 v = v*10 + c;
451 }
danielk1977b8cdbec2007-09-01 10:01:12 +0000452
453 /* The longest decimal representation of a 32 bit integer is 10 digits:
454 **
455 ** 1234567890
456 ** 2^31 -> 2147483648
457 */
458 if( i>10 ){
drh217f4902007-06-25 17:28:00 +0000459 return 0;
460 }
461 if( v-neg>2147483647 ){
462 return 0;
463 }
464 if( neg ){
465 v = -v;
466 }
467 *pValue = (int)v;
468 return 1;
469}
drhdce2cbe2000-05-31 02:27:49 +0000470
471/*
drhd8820e82004-05-18 15:57:42 +0000472** The variable-length integer encoding is as follows:
473**
474** KEY:
475** A = 0xxxxxxx 7 bits of data and one flag bit
476** B = 1xxxxxxx 7 bits of data and one flag bit
477** C = xxxxxxxx 8 bits of data
478**
479** 7 bits - A
480** 14 bits - BA
481** 21 bits - BBA
482** 28 bits - BBBA
483** 35 bits - BBBBA
484** 42 bits - BBBBBA
485** 49 bits - BBBBBBA
486** 56 bits - BBBBBBBA
487** 64 bits - BBBBBBBBC
488*/
489
490/*
drh6d2fb152004-05-14 16:50:06 +0000491** Write a 64-bit variable-length integer to memory starting at p[0].
492** The length of data write will be between 1 and 9 bytes. The number
493** of bytes written is returned.
494**
495** A variable-length integer consists of the lower 7 bits of each byte
496** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +0000497** bit clear. Except, if we get to the 9th byte, it stores the full
498** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +0000499*/
danielk1977192ac1d2004-05-10 07:17:30 +0000500int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +0000501 int i, j, n;
502 u8 buf[10];
drhfe2093d2005-01-20 22:48:47 +0000503 if( v & (((u64)0xff000000)<<32) ){
drhd8820e82004-05-18 15:57:42 +0000504 p[8] = v;
505 v >>= 8;
506 for(i=7; i>=0; i--){
507 p[i] = (v & 0x7f) | 0x80;
508 v >>= 7;
509 }
510 return 9;
511 }
drh6d2fb152004-05-14 16:50:06 +0000512 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +0000513 do{
drh6d2fb152004-05-14 16:50:06 +0000514 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +0000515 v >>= 7;
516 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +0000517 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +0000518 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +0000519 for(i=0, j=n-1; j>=0; j--, i++){
520 p[i] = buf[j];
521 }
522 return n;
danielk1977192ac1d2004-05-10 07:17:30 +0000523}
danielk19774adee202004-05-08 08:23:19 +0000524
drh6d2fb152004-05-14 16:50:06 +0000525/*
drh35b5a332008-04-05 18:41:42 +0000526** This routine is a faster version of sqlite3PutVarint() that only
527** works for 32-bit positive integers and which is optimized for
shane952856a2008-04-28 17:41:30 +0000528** the common case of small integers. A MACRO version, putVarint32,
529** is provided which inlines the single-byte case. All code should use
530** the MACRO version as this function assumes the single-byte case has
531** already been handled.
drh35b5a332008-04-05 18:41:42 +0000532*/
533int sqlite3PutVarint32(unsigned char *p, u32 v){
shane952856a2008-04-28 17:41:30 +0000534#ifndef putVarint32
drh35b5a332008-04-05 18:41:42 +0000535 if( (v & ~0x7f)==0 ){
536 p[0] = v;
537 return 1;
shane952856a2008-04-28 17:41:30 +0000538 }
539#endif
540 if( (v & ~0x3fff)==0 ){
drh35b5a332008-04-05 18:41:42 +0000541 p[0] = (v>>7) | 0x80;
542 p[1] = v & 0x7f;
543 return 2;
drh35b5a332008-04-05 18:41:42 +0000544 }
shane952856a2008-04-28 17:41:30 +0000545 return sqlite3PutVarint(p, v);
drh35b5a332008-04-05 18:41:42 +0000546}
547
548/*
drh6d2fb152004-05-14 16:50:06 +0000549** Read a 64-bit variable-length integer from memory starting at p[0].
550** Return the number of bytes read. The value is stored in *v.
551*/
danielk197790e4d952004-05-10 10:05:53 +0000552int sqlite3GetVarint(const unsigned char *p, u64 *v){
shane356574e2008-05-01 02:47:03 +0000553 u32 a,b,s;
554
555 a = *p;
shane022e8af2008-06-12 02:16:44 +0000556 /* a: p0 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000557 if (!(a&0x80))
558 {
559 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000560 return 1;
561 }
shane356574e2008-05-01 02:47:03 +0000562
563 p++;
564 b = *p;
shane022e8af2008-06-12 02:16:44 +0000565 /* b: p1 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000566 if (!(b&0x80))
567 {
568 a &= 0x7f;
569 a = a<<7;
570 a |= b;
571 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000572 return 2;
573 }
shane356574e2008-05-01 02:47:03 +0000574
575 p++;
576 a = a<<14;
577 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000578 /* a: p0<<14 | p2 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000579 if (!(a&0x80))
580 {
581 a &= (0x7f<<14)|(0x7f);
582 b &= 0x7f;
583 b = b<<7;
584 a |= b;
585 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000586 return 3;
587 }
shane356574e2008-05-01 02:47:03 +0000588
shane022e8af2008-06-12 02:16:44 +0000589 /* CSE1 from below */
shane356574e2008-05-01 02:47:03 +0000590 a &= (0x7f<<14)|(0x7f);
591 p++;
592 b = b<<14;
593 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000594 /* b: p1<<14 | p3 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000595 if (!(b&0x80))
596 {
597 b &= (0x7f<<14)|(0x7f);
shane022e8af2008-06-12 02:16:44 +0000598 /* moved CSE1 up */
599 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000600 a = a<<7;
601 a |= b;
602 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000603 return 4;
604 }
shane356574e2008-05-01 02:47:03 +0000605
shane022e8af2008-06-12 02:16:44 +0000606 /* a: p0<<14 | p2 (masked) */
607 /* b: p1<<14 | p3 (unmasked) */
608 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
609 /* moved CSE1 up */
610 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000611 b &= (0x7f<<14)|(0x7f);
612 s = a;
shane022e8af2008-06-12 02:16:44 +0000613 /* s: p0<<14 | p2 (masked) */
shane356574e2008-05-01 02:47:03 +0000614
615 p++;
616 a = a<<14;
617 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000618 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000619 if (!(a&0x80))
620 {
shane022e8af2008-06-12 02:16:44 +0000621 /* we can skip these cause they were (effectively) done above in calc'ing s */
622 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
623 /* b &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000624 b = b<<7;
625 a |= b;
626 s = s>>18;
627 *v = ((u64)s)<<32 | a;
628 return 5;
629 }
630
shane022e8af2008-06-12 02:16:44 +0000631 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
shane356574e2008-05-01 02:47:03 +0000632 s = s<<7;
633 s |= b;
shane022e8af2008-06-12 02:16:44 +0000634 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
shane356574e2008-05-01 02:47:03 +0000635
636 p++;
637 b = b<<14;
638 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000639 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000640 if (!(b&0x80))
641 {
shane022e8af2008-06-12 02:16:44 +0000642 /* we can skip this cause it was (effectively) done above in calc'ing s */
643 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000644 a &= (0x7f<<14)|(0x7f);
645 a = a<<7;
646 a |= b;
647 s = s>>18;
648 *v = ((u64)s)<<32 | a;
649 return 6;
650 }
651
652 p++;
653 a = a<<14;
654 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000655 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000656 if (!(a&0x80))
657 {
658 a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
659 b &= (0x7f<<14)|(0x7f);
660 b = b<<7;
661 a |= b;
662 s = s>>11;
663 *v = ((u64)s)<<32 | a;
664 return 7;
665 }
666
shane022e8af2008-06-12 02:16:44 +0000667 /* CSE2 from below */
shane356574e2008-05-01 02:47:03 +0000668 a &= (0x7f<<14)|(0x7f);
669 p++;
670 b = b<<14;
671 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000672 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000673 if (!(b&0x80))
674 {
675 b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
shane022e8af2008-06-12 02:16:44 +0000676 /* moved CSE2 up */
677 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000678 a = a<<7;
679 a |= b;
680 s = s>>4;
681 *v = ((u64)s)<<32 | a;
682 return 8;
683 }
684
685 p++;
686 a = a<<15;
687 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000688 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000689
shane022e8af2008-06-12 02:16:44 +0000690 /* moved CSE2 up */
691 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
shane356574e2008-05-01 02:47:03 +0000692 b &= (0x7f<<14)|(0x7f);
693 b = b<<8;
694 a |= b;
695
696 s = s<<4;
697 b = p[-4];
698 b &= 0x7f;
699 b = b>>3;
700 s |= b;
701
702 *v = ((u64)s)<<32 | a;
703
704 return 9;
drh6d2fb152004-05-14 16:50:06 +0000705}
706
707/*
708** Read a 32-bit variable-length integer from memory starting at p[0].
709** Return the number of bytes read. The value is stored in *v.
shane952856a2008-04-28 17:41:30 +0000710** A MACRO version, getVarint32, is provided which inlines the
711** single-byte case. All code should use the MACRO version as
712** this function assumes the single-byte case has already been handled.
drh6d2fb152004-05-14 16:50:06 +0000713*/
714int sqlite3GetVarint32(const unsigned char *p, u32 *v){
shane356574e2008-05-01 02:47:03 +0000715 u32 a,b;
716
717 a = *p;
shane022e8af2008-06-12 02:16:44 +0000718 /* a: p0 (unmasked) */
shane952856a2008-04-28 17:41:30 +0000719#ifndef getVarint32
shane356574e2008-05-01 02:47:03 +0000720 if (!(a&0x80))
721 {
722 *v = a;
drhe6f85e72004-12-25 01:03:13 +0000723 return 1;
724 }
shane952856a2008-04-28 17:41:30 +0000725#endif
shane356574e2008-05-01 02:47:03 +0000726
727 p++;
728 b = *p;
shane022e8af2008-06-12 02:16:44 +0000729 /* b: p1 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000730 if (!(b&0x80))
731 {
732 a &= 0x7f;
733 a = a<<7;
734 *v = a | b;
drhe6f85e72004-12-25 01:03:13 +0000735 return 2;
736 }
shane356574e2008-05-01 02:47:03 +0000737
738 p++;
739 a = a<<14;
740 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000741 /* a: p0<<14 | p2 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000742 if (!(a&0x80))
743 {
744 a &= (0x7f<<14)|(0x7f);
745 b &= 0x7f;
746 b = b<<7;
747 *v = a | b;
748 return 3;
749 }
750
751 p++;
752 b = b<<14;
753 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000754 /* b: p1<<14 | p3 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000755 if (!(b&0x80))
756 {
757 b &= (0x7f<<14)|(0x7f);
758 a &= (0x7f<<14)|(0x7f);
759 a = a<<7;
760 *v = a | b;
761 return 4;
762 }
763
764 p++;
765 a = a<<14;
766 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000767 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000768 if (!(a&0x80))
769 {
770 a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
771 b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
772 b = b<<7;
773 *v = a | b;
774 return 5;
775 }
776
drhcec3e3e2008-05-13 16:41:50 +0000777 /* We can only reach this point when reading a corrupt database
778 ** file. In that case we are not in any hurry. Use the (relatively
779 ** slow) general-purpose sqlite3GetVarint() routine to extract the
780 ** value. */
shane356574e2008-05-01 02:47:03 +0000781 {
drhcec3e3e2008-05-13 16:41:50 +0000782 u64 v64;
783 int n;
784
785 p -= 4;
786 n = sqlite3GetVarint(p, &v64);
787 assert( n>5 && n<=9 );
788 *v = (u32)v64;
789 return n;
shane356574e2008-05-01 02:47:03 +0000790 }
danielk1977192ac1d2004-05-10 07:17:30 +0000791}
792
drh6d2fb152004-05-14 16:50:06 +0000793/*
794** Return the number of bytes that will be needed to store the given
795** 64-bit integer.
796*/
danielk1977192ac1d2004-05-10 07:17:30 +0000797int sqlite3VarintLen(u64 v){
798 int i = 0;
799 do{
800 i++;
801 v >>= 7;
drhd8820e82004-05-18 15:57:42 +0000802 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000803 return i;
804}
danielk1977c572ef72004-05-27 09:28:41 +0000805
drha3152892007-05-05 11:48:52 +0000806
807/*
danielk19771cc5ed82007-05-16 17:28:43 +0000808** Read or write a four-byte big-endian integer value.
drha3152892007-05-05 11:48:52 +0000809*/
drha3152892007-05-05 11:48:52 +0000810u32 sqlite3Get4byte(const u8 *p){
811 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
812}
drha3152892007-05-05 11:48:52 +0000813void sqlite3Put4byte(unsigned char *p, u32 v){
814 p[0] = v>>24;
815 p[1] = v>>16;
816 p[2] = v>>8;
817 p[3] = v;
818}
819
820
821
drh34533152008-03-19 13:03:33 +0000822#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000823/*
824** Translate a single byte of Hex into an integer.
drh335d29d2008-04-04 15:12:21 +0000825** This routinen only works if h really is a valid hexadecimal
826** character: 0..9a..fA..F
drh9d213ef2004-06-30 04:02:11 +0000827*/
828static int hexToInt(int h){
drh06fbb732008-04-11 19:37:55 +0000829 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
drh04924d82008-04-14 14:34:44 +0000830#ifdef SQLITE_ASCII
drh06fbb732008-04-11 19:37:55 +0000831 h += 9*(1&(h>>6));
drh04924d82008-04-14 14:34:44 +0000832#endif
833#ifdef SQLITE_EBCDIC
drh06fbb732008-04-11 19:37:55 +0000834 h += 9*(1&~(h>>4));
drh335d29d2008-04-04 15:12:21 +0000835#endif
drh06fbb732008-04-11 19:37:55 +0000836 return h & 0xf;
drh9d213ef2004-06-30 04:02:11 +0000837}
drh34533152008-03-19 13:03:33 +0000838#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drh9d213ef2004-06-30 04:02:11 +0000839
drhbf8aa332004-11-04 04:34:14 +0000840#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000841/*
842** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
843** value. Return a pointer to its binary value. Space to hold the
844** binary value has been obtained from malloc and must be freed by
845** the calling routine.
846*/
drhca48c902008-01-18 14:08:24 +0000847void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
danielk1977c572ef72004-05-27 09:28:41 +0000848 char *zBlob;
849 int i;
danielk1977c572ef72004-05-27 09:28:41 +0000850
drhca48c902008-01-18 14:08:24 +0000851 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
852 n--;
drh76f80792006-07-11 12:40:25 +0000853 if( zBlob ){
854 for(i=0; i<n; i+=2){
855 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
856 }
drhca48c902008-01-18 14:08:24 +0000857 zBlob[i/2] = 0;
danielk1977c572ef72004-05-27 09:28:41 +0000858 }
danielk19773fd0a732004-05-27 13:35:19 +0000859 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +0000860}
drhbf8aa332004-11-04 04:34:14 +0000861#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drhfe63d1c2004-09-08 20:13:04 +0000862
drha3152892007-05-05 11:48:52 +0000863
drhfe63d1c2004-09-08 20:13:04 +0000864/*
drha3152892007-05-05 11:48:52 +0000865** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
866** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
867** when this routine is called.
868**
869** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
870** value indicates that the database connection passed into the API is
871** open and is not being used by another thread. By changing the value
872** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
873** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
874** when the API exits.
875**
876** This routine is a attempt to detect if two threads use the
877** same sqlite* pointer at the same time. There is a race
878** condition so it is possible that the error is not detected.
879** But usually the problem will be seen. The result will be an
880** error which can be used to debug the application that is
881** using SQLite incorrectly.
882**
883** Ticket #202: If db->magic is not a valid open value, take care not
884** to modify the db structure at all. It could be that db is a stale
885** pointer. In other words, it could be that there has been a prior
886** call to sqlite3_close(db) and db has been deallocated. And we do
887** not want to write into deallocated memory.
drhfe63d1c2004-09-08 20:13:04 +0000888*/
drh7e8b8482008-01-23 03:03:05 +0000889#ifdef SQLITE_DEBUG
drha3152892007-05-05 11:48:52 +0000890int sqlite3SafetyOn(sqlite3 *db){
891 if( db->magic==SQLITE_MAGIC_OPEN ){
892 db->magic = SQLITE_MAGIC_BUSY;
drha0af99f2008-04-16 00:49:12 +0000893 assert( sqlite3_mutex_held(db->mutex) );
drha3152892007-05-05 11:48:52 +0000894 return 0;
895 }else if( db->magic==SQLITE_MAGIC_BUSY ){
896 db->magic = SQLITE_MAGIC_ERROR;
897 db->u1.isInterrupted = 1;
drhfe63d1c2004-09-08 20:13:04 +0000898 }
drha3152892007-05-05 11:48:52 +0000899 return 1;
drhfe63d1c2004-09-08 20:13:04 +0000900}
drh7e8b8482008-01-23 03:03:05 +0000901#endif
drha3152892007-05-05 11:48:52 +0000902
903/*
904** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
905** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
906** when this routine is called.
907*/
drh7e8b8482008-01-23 03:03:05 +0000908#ifdef SQLITE_DEBUG
drha3152892007-05-05 11:48:52 +0000909int sqlite3SafetyOff(sqlite3 *db){
910 if( db->magic==SQLITE_MAGIC_BUSY ){
911 db->magic = SQLITE_MAGIC_OPEN;
drha0af99f2008-04-16 00:49:12 +0000912 assert( sqlite3_mutex_held(db->mutex) );
drha3152892007-05-05 11:48:52 +0000913 return 0;
drh7e8b8482008-01-23 03:03:05 +0000914 }else{
drha3152892007-05-05 11:48:52 +0000915 db->magic = SQLITE_MAGIC_ERROR;
916 db->u1.isInterrupted = 1;
917 return 1;
918 }
919}
drh7e8b8482008-01-23 03:03:05 +0000920#endif
drh55176252008-01-22 14:50:16 +0000921
922/*
923** Check to make sure we have a valid db pointer. This test is not
924** foolproof but it does provide some measure of protection against
925** misuse of the interface such as passing in db pointers that are
926** NULL or which have been previously closed. If this routine returns
drh7e8b8482008-01-23 03:03:05 +0000927** 1 it means that the db pointer is valid and 0 if it should not be
drh55176252008-01-22 14:50:16 +0000928** dereferenced for any reason. The calling function should invoke
929** SQLITE_MISUSE immediately.
drh7e8b8482008-01-23 03:03:05 +0000930**
931** sqlite3SafetyCheckOk() requires that the db pointer be valid for
932** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
933** open properly and is not fit for general use but which can be
934** used as an argument to sqlite3_errmsg() or sqlite3_close().
drh55176252008-01-22 14:50:16 +0000935*/
drh7e8b8482008-01-23 03:03:05 +0000936int sqlite3SafetyCheckOk(sqlite3 *db){
danielk197700e13612008-11-17 19:18:54 +0000937 u32 magic;
drh7e8b8482008-01-23 03:03:05 +0000938 if( db==0 ) return 0;
drh55176252008-01-22 14:50:16 +0000939 magic = db->magic;
drh7e8b8482008-01-23 03:03:05 +0000940 if( magic!=SQLITE_MAGIC_OPEN &&
941 magic!=SQLITE_MAGIC_BUSY ) return 0;
942 return 1;
943}
944int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
danielk197700e13612008-11-17 19:18:54 +0000945 u32 magic;
drh7e8b8482008-01-23 03:03:05 +0000946 if( db==0 ) return 0;
947 magic = db->magic;
948 if( magic!=SQLITE_MAGIC_SICK &&
949 magic!=SQLITE_MAGIC_OPEN &&
950 magic!=SQLITE_MAGIC_BUSY ) return 0;
951 return 1;
drh55176252008-01-22 14:50:16 +0000952}