blob: be4b13a54f73177d9c9b0a3820f08fb31402da5f [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**
drhfe2093d2005-01-20 22:48:47 +000017** $Id: util.c,v 1.128 2005/01/20 22:48:48 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
danielk19772c336542005-01-13 02:14:23 +000023#if SQLITE_MEMDEBUG>2 && defined(__GLIBC__)
danielk1977eac7a362004-06-16 07:45:24 +000024#include <execinfo.h>
25void print_stack_trace(){
26 void *bt[30];
27 int i;
28 int n = backtrace(bt, 30);
29
danielk19775558a8a2005-01-17 07:53:44 +000030 fprintf(stderr, "STACK: ");
danielk1977eac7a362004-06-16 07:45:24 +000031 for(i=0; i<n;i++){
danielk19775558a8a2005-01-17 07:53:44 +000032 fprintf(stderr, "%p ", bt[i]);
danielk1977eac7a362004-06-16 07:45:24 +000033 }
danielk19775558a8a2005-01-17 07:53:44 +000034 fprintf(stderr, "\n");
danielk1977eac7a362004-06-16 07:45:24 +000035}
36#else
37#define print_stack_trace()
38#endif
39
drh7c68d602000-10-11 19:28:51 +000040/*
drhdaffd0e2001-04-11 14:28:42 +000041** If malloc() ever fails, this global variable gets set to 1.
42** This causes the library to abort and never again function.
43*/
danielk19776f8a5032004-05-10 10:34:51 +000044int sqlite3_malloc_failed = 0;
drhdaffd0e2001-04-11 14:28:42 +000045
46/*
danielk19772c336542005-01-13 02:14:23 +000047** If SQLITE_MEMDEBUG is defined, then use versions of malloc() and
drh7c68d602000-10-11 19:28:51 +000048** free() that track memory usage and check for buffer overruns.
49*/
danielk19772c336542005-01-13 02:14:23 +000050#ifdef SQLITE_MEMDEBUG
drhdcc581c2000-05-30 13:44:19 +000051
drhdcc581c2000-05-30 13:44:19 +000052/*
drh8c82b352000-12-10 18:23:50 +000053** For keeping track of the number of mallocs and frees. This
drh46934232004-11-20 19:18:00 +000054** is used to check for memory leaks. The iMallocFail and iMallocReset
55** values are used to simulate malloc() failures during testing in
56** order to verify that the library correctly handles an out-of-memory
57** condition.
drh8c82b352000-12-10 18:23:50 +000058*/
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */
60int sqlite3_nFree; /* Number of sqliteFree() calls */
61int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */
drh46934232004-11-20 19:18:00 +000062int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
danielk19772c336542005-01-13 02:14:23 +000063#if SQLITE_MEMDEBUG>1
drhd94a6692002-08-25 18:29:11 +000064static int memcnt = 0;
65#endif
drh8c82b352000-12-10 18:23:50 +000066
drh4305d102003-07-30 12:34:12 +000067/*
68** Number of 32-bit guard words
69*/
70#define N_GUARD 1
drh8c82b352000-12-10 18:23:50 +000071
72/*
drhdcc581c2000-05-30 13:44:19 +000073** Allocate new memory and set it to zero. Return NULL if
74** no memory is available.
75*/
danielk19774adee202004-05-08 08:23:19 +000076void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000077 void *p;
78 int *pi;
drh4305d102003-07-30 12:34:12 +000079 int i, k;
danielk19776f8a5032004-05-10 10:34:51 +000080 if( sqlite3_iMallocFail>=0 ){
81 sqlite3_iMallocFail--;
82 if( sqlite3_iMallocFail==0 ){
83 sqlite3_malloc_failed++;
danielk19772c336542005-01-13 02:14:23 +000084#if SQLITE_MEMDEBUG>1
drh6d4abfb2001-10-22 02:58:08 +000085 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
86 n, zFile,line);
87#endif
drh46934232004-11-20 19:18:00 +000088 sqlite3_iMallocFail = sqlite3_iMallocReset;
drhdaffd0e2001-04-11 14:28:42 +000089 return 0;
90 }
drh6e142f52000-06-08 13:36:40 +000091 }
drhb0729502001-03-14 12:35:57 +000092 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000093 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +000094 pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000095 if( pi==0 ){
danielk19776f8a5032004-05-10 10:34:51 +000096 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +000097 return 0;
98 }
danielk19776f8a5032004-05-10 10:34:51 +000099 sqlite3_nMalloc++;
drh4305d102003-07-30 12:34:12 +0000100 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
101 pi[N_GUARD] = n;
102 for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
103 p = &pi[N_GUARD+1];
drh8c1238a2003-01-02 14:43:55 +0000104 memset(p, bZero==0, n);
danielk19772c336542005-01-13 02:14:23 +0000105#if SQLITE_MEMDEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000106 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000107 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
108 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000109#endif
drhdcc581c2000-05-30 13:44:19 +0000110 return p;
111}
112
113/*
drhed6c8672003-01-12 18:02:16 +0000114** Check to see if the given pointer was obtained from sqliteMalloc()
115** and is able to hold at least N bytes. Raise an exception if this
116** is not the case.
117**
118** This routine is used for testing purposes only.
119*/
danielk19774adee202004-05-08 08:23:19 +0000120void sqlite3CheckMemory(void *p, int N){
drhed6c8672003-01-12 18:02:16 +0000121 int *pi = p;
drh4305d102003-07-30 12:34:12 +0000122 int n, i, k;
123 pi -= N_GUARD+1;
124 for(i=0; i<N_GUARD; i++){
125 assert( pi[i]==0xdead1122 );
126 }
127 n = pi[N_GUARD];
drhed6c8672003-01-12 18:02:16 +0000128 assert( N>=0 && N<n );
129 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000130 for(i=0; i<N_GUARD; i++){
131 assert( pi[k+N_GUARD+1+i]==0xdead3344 );
132 }
drhed6c8672003-01-12 18:02:16 +0000133}
134
135/*
drhdcc581c2000-05-30 13:44:19 +0000136** Free memory previously obtained from sqliteMalloc()
137*/
danielk19774adee202004-05-08 08:23:19 +0000138void sqlite3Free_(void *p, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +0000139 if( p ){
drh4305d102003-07-30 12:34:12 +0000140 int *pi, i, k, n;
drhdcc581c2000-05-30 13:44:19 +0000141 pi = p;
drh4305d102003-07-30 12:34:12 +0000142 pi -= N_GUARD+1;
danielk19776f8a5032004-05-10 10:34:51 +0000143 sqlite3_nFree++;
drh4305d102003-07-30 12:34:12 +0000144 for(i=0; i<N_GUARD; i++){
145 if( pi[i]!=0xdead1122 ){
146 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
147 return;
148 }
drhdcc581c2000-05-30 13:44:19 +0000149 }
drh4305d102003-07-30 12:34:12 +0000150 n = pi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000151 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000152 for(i=0; i<N_GUARD; i++){
153 if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
154 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
155 return;
156 }
drhdcc581c2000-05-30 13:44:19 +0000157 }
drh4305d102003-07-30 12:34:12 +0000158 memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
danielk19772c336542005-01-13 02:14:23 +0000159#if SQLITE_MEMDEBUG>1
drhd94a6692002-08-25 18:29:11 +0000160 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
161 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000162#endif
drhdcc581c2000-05-30 13:44:19 +0000163 free(pi);
164 }
165}
166
167/*
168** Resize a prior allocation. If p==0, then this routine
169** works just like sqliteMalloc(). If n==0, then this routine
170** works just like sqliteFree().
171*/
danielk19774adee202004-05-08 08:23:19 +0000172void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){
drh4305d102003-07-30 12:34:12 +0000173 int *oldPi, *pi, i, k, oldN, oldK;
drhdcc581c2000-05-30 13:44:19 +0000174 void *p;
175 if( oldP==0 ){
danielk19774adee202004-05-08 08:23:19 +0000176 return sqlite3Malloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000177 }
178 if( n==0 ){
danielk19774adee202004-05-08 08:23:19 +0000179 sqlite3Free_(oldP,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000180 return 0;
181 }
182 oldPi = oldP;
drh4305d102003-07-30 12:34:12 +0000183 oldPi -= N_GUARD+1;
drhdcc581c2000-05-30 13:44:19 +0000184 if( oldPi[0]!=0xdead1122 ){
drh03ab7332003-08-26 11:29:07 +0000185 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
drh9bb61fe2000-06-05 16:01:39 +0000186 return 0;
drhdcc581c2000-05-30 13:44:19 +0000187 }
drh4305d102003-07-30 12:34:12 +0000188 oldN = oldPi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000189 oldK = (oldN+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000190 for(i=0; i<N_GUARD; i++){
191 if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
drh03ab7332003-08-26 11:29:07 +0000192 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
193 (int)oldP);
drh4305d102003-07-30 12:34:12 +0000194 return 0;
195 }
drhdcc581c2000-05-30 13:44:19 +0000196 }
197 k = (n + sizeof(int) - 1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000198 pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000199 if( pi==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000200 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000201 return 0;
202 }
drh4305d102003-07-30 12:34:12 +0000203 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
204 pi[N_GUARD] = n;
205 for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
206 p = &pi[N_GUARD+1];
drhdcc581c2000-05-30 13:44:19 +0000207 memcpy(p, oldP, n>oldN ? oldN : n);
208 if( n>oldN ){
danielk197725b33632004-06-30 12:49:46 +0000209 memset(&((char*)p)[oldN], 0x55, n-oldN);
drhdcc581c2000-05-30 13:44:19 +0000210 }
drh4305d102003-07-30 12:34:12 +0000211 memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000212 free(oldPi);
danielk19772c336542005-01-13 02:14:23 +0000213#if SQLITE_MEMDEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000214 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000215 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
216 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000217#endif
drhdcc581c2000-05-30 13:44:19 +0000218 return p;
219}
drhc3c2fc92000-05-31 22:58:39 +0000220
221/*
drh6e142f52000-06-08 13:36:40 +0000222** Make a copy of a string in memory obtained from sqliteMalloc()
223*/
danielk19774adee202004-05-08 08:23:19 +0000224char *sqlite3StrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000225 char *zNew;
226 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000227 zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000228 if( zNew ) strcpy(zNew, z);
229 return zNew;
230}
danielk19774adee202004-05-08 08:23:19 +0000231char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000232 char *zNew;
233 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000234 zNew = sqlite3Malloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000235 if( zNew ){
236 memcpy(zNew, z, n);
237 zNew[n] = 0;
238 }
239 return zNew;
240}
danielk1977bfd6cce2004-06-18 04:24:54 +0000241
242/*
243** A version of sqliteFree that is always a function, not a macro.
244*/
245void sqlite3FreeX(void *p){
246 sqliteFree(p);
247}
danielk19772c336542005-01-13 02:14:23 +0000248#endif /* SQLITE_MEMDEBUG */
drh6e142f52000-06-08 13:36:40 +0000249
drh7c68d602000-10-11 19:28:51 +0000250/*
251** The following versions of malloc() and free() are for use in a
252** normal build.
253*/
danielk19772c336542005-01-13 02:14:23 +0000254#if !defined(SQLITE_MEMDEBUG)
drh6e142f52000-06-08 13:36:40 +0000255
drh75897232000-05-29 14:26:00 +0000256/*
257** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000258** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000259*/
drh38f82712004-06-18 17:10:16 +0000260void *sqlite3Malloc(int n){
drh26780582002-10-20 15:46:22 +0000261 void *p;
drh8548a052003-10-22 22:15:27 +0000262 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000263 if( n>0 ) sqlite3_malloc_failed++;
drh8548a052003-10-22 22:15:27 +0000264 }else{
265 memset(p, 0, n);
drhdaffd0e2001-04-11 14:28:42 +0000266 }
drh75897232000-05-29 14:26:00 +0000267 return p;
268}
269
270/*
drh8c1238a2003-01-02 14:43:55 +0000271** Allocate new memory but do not set it to zero. Return NULL if
272** no memory is available. See also sqliteMalloc().
273*/
drh38f82712004-06-18 17:10:16 +0000274void *sqlite3MallocRaw(int n){
drh8c1238a2003-01-02 14:43:55 +0000275 void *p;
drh8548a052003-10-22 22:15:27 +0000276 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000277 if( n>0 ) sqlite3_malloc_failed++;
drh8c1238a2003-01-02 14:43:55 +0000278 }
279 return p;
280}
281
282/*
drh75897232000-05-29 14:26:00 +0000283** Free memory previously obtained from sqliteMalloc()
284*/
drh38f82712004-06-18 17:10:16 +0000285void sqlite3FreeX(void *p){
drh305cea62000-05-29 17:44:25 +0000286 if( p ){
drh305cea62000-05-29 17:44:25 +0000287 free(p);
288 }
drh75897232000-05-29 14:26:00 +0000289}
290
291/*
292** Resize a prior allocation. If p==0, then this routine
293** works just like sqliteMalloc(). If n==0, then this routine
294** works just like sqliteFree().
295*/
drh38f82712004-06-18 17:10:16 +0000296void *sqlite3Realloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000297 void *p2;
drh75897232000-05-29 14:26:00 +0000298 if( p==0 ){
299 return sqliteMalloc(n);
300 }
301 if( n==0 ){
302 sqliteFree(p);
303 return 0;
304 }
drh6d4abfb2001-10-22 02:58:08 +0000305 p2 = realloc(p, n);
306 if( p2==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000307 sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000308 }
drh6d4abfb2001-10-22 02:58:08 +0000309 return p2;
drh75897232000-05-29 14:26:00 +0000310}
drh6e142f52000-06-08 13:36:40 +0000311
312/*
313** Make a copy of a string in memory obtained from sqliteMalloc()
314*/
drh38f82712004-06-18 17:10:16 +0000315char *sqlite3StrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000316 char *zNew;
317 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000318 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000319 if( zNew ) strcpy(zNew, z);
320 return zNew;
321}
drh38f82712004-06-18 17:10:16 +0000322char *sqlite3StrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000323 char *zNew;
324 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000325 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000326 if( zNew ){
327 memcpy(zNew, z, n);
328 zNew[n] = 0;
329 }
330 return zNew;
331}
danielk19772c336542005-01-13 02:14:23 +0000332#endif /* !defined(SQLITE_MEMDEBUG) */
drh75897232000-05-29 14:26:00 +0000333
334/*
335** Create a string from the 2nd and subsequent arguments (up to the
336** first NULL argument), store the string in memory obtained from
337** sqliteMalloc() and make the pointer indicated by the 1st argument
jplyon02be20d2003-06-02 06:17:10 +0000338** point to that string. The 1st argument must either be NULL or
339** point to memory obtained from sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000340*/
danielk19774adee202004-05-08 08:23:19 +0000341void sqlite3SetString(char **pz, const char *zFirst, ...){
drh75897232000-05-29 14:26:00 +0000342 va_list ap;
343 int nByte;
344 const char *z;
345 char *zResult;
346
347 if( pz==0 ) return;
348 nByte = strlen(zFirst) + 1;
349 va_start(ap, zFirst);
350 while( (z = va_arg(ap, const char*))!=0 ){
351 nByte += strlen(z);
352 }
353 va_end(ap);
354 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000355 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000356 if( zResult==0 ){
357 return;
358 }
drh75897232000-05-29 14:26:00 +0000359 strcpy(zResult, zFirst);
360 zResult += strlen(zResult);
361 va_start(ap, zFirst);
362 while( (z = va_arg(ap, const char*))!=0 ){
363 strcpy(zResult, z);
364 zResult += strlen(zResult);
365 }
366 va_end(ap);
drhfaa57ac2004-06-09 14:01:51 +0000367#ifdef SQLITE_DEBUG
368#if SQLITE_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000369 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
370#endif
371#endif
drh75897232000-05-29 14:26:00 +0000372}
373
374/*
danielk19776622cce2004-05-20 11:00:52 +0000375** Set the most recent error code and error string for the sqlite
376** handle "db". The error code is set to "err_code".
377**
378** If it is not NULL, string zFormat specifies the format of the
379** error string in the style of the printf functions: The following
380** format characters are allowed:
381**
382** %s Insert a string
383** %z A string that should be freed after use
384** %d Insert an integer
385** %T Insert a token
386** %S Insert the first element of a SrcList
387**
388** zFormat and any string tokens that follow it are assumed to be
389** encoded in UTF-8.
390**
391** To clear the most recent error for slqite handle "db", sqlite3Error
392** should be called with err_code set to SQLITE_OK and zFormat set
393** to NULL.
394*/
drh9bb575f2004-09-06 17:24:11 +0000395void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
danielk1977bfd6cce2004-06-18 04:24:54 +0000396 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){
397 db->errCode = err_code;
398 if( zFormat ){
399 char *z;
400 va_list ap;
401 va_start(ap, zFormat);
402 z = sqlite3VMPrintf(zFormat, ap);
403 va_end(ap);
404 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
405 }else{
406 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
407 }
danielk19776622cce2004-05-20 11:00:52 +0000408 }
409}
410
411/*
drhda93d232003-03-31 02:12:46 +0000412** Add an error message to pParse->zErrMsg and increment pParse->nErr.
413** The following formatting characters are allowed:
414**
415** %s Insert a string
416** %z A string that should be freed after use
417** %d Insert an integer
418** %T Insert a token
419** %S Insert the first element of a SrcList
danielk19779e6db7d2004-06-21 08:18:51 +0000420**
421** This function should be used to report any error that occurs whilst
422** compiling an SQL statement (i.e. within sqlite3_prepare()). The
423** last thing the sqlite3_prepare() function does is copy the error
424** stored by this function into the database handle using sqlite3Error().
425** Function sqlite3Error() should be used during statement execution
426** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +0000427*/
danielk19774adee202004-05-08 08:23:19 +0000428void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000429 va_list ap;
drhda93d232003-03-31 02:12:46 +0000430 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +0000431 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000432 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +0000433 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000434 va_end(ap);
drhda93d232003-03-31 02:12:46 +0000435}
436
437/*
drh982cef72000-05-30 16:27:03 +0000438** Convert an SQL-style quoted string into a normal string by removing
439** the quote characters. The conversion is done in-place. If the
440** input does not begin with a quote character, then this routine
441** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000442**
443** 2002-Feb-14: This routine is extended to remove MS-Access style
444** brackets from around identifers. For example: "[a-b-c]" becomes
445** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000446*/
danielk19774adee202004-05-08 08:23:19 +0000447void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000448 int quote;
449 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000450 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000451 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000452 switch( quote ){
453 case '\'': break;
454 case '"': break;
455 case '[': quote = ']'; break;
456 default: return;
457 }
drh982cef72000-05-30 16:27:03 +0000458 for(i=1, j=0; z[i]; i++){
459 if( z[i]==quote ){
460 if( z[i+1]==quote ){
461 z[j++] = quote;
462 i++;
463 }else{
464 z[j++] = 0;
465 break;
466 }
467 }else{
468 z[j++] = z[i];
469 }
470 }
471}
472
drh75897232000-05-29 14:26:00 +0000473/* An array to map all upper-case characters into their corresponding
474** lower-case character.
475*/
drh4e5ffc52004-08-31 00:52:37 +0000476const unsigned char sqlite3UpperToLower[] = {
drh75897232000-05-29 14:26:00 +0000477 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
478 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
479 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
480 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
481 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
482 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
483 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
484 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
485 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
486 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
487 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
488 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
489 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
490 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
491 252,253,254,255
492};
drh4e5ffc52004-08-31 00:52:37 +0000493#define UpperToLower sqlite3UpperToLower
drh75897232000-05-29 14:26:00 +0000494
495/*
496** This function computes a hash on the name of a keyword.
497** Case is not significant.
498*/
danielk19774adee202004-05-08 08:23:19 +0000499int sqlite3HashNoCase(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000500 int h = 0;
drh75897232000-05-29 14:26:00 +0000501 if( n<=0 ) n = strlen(z);
drhdb5ed6d2001-09-18 22:17:44 +0000502 while( n > 0 ){
drh8cfbf082001-09-19 13:22:39 +0000503 h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
drhdb5ed6d2001-09-18 22:17:44 +0000504 n--;
drh75897232000-05-29 14:26:00 +0000505 }
drh5364f602003-05-12 23:06:52 +0000506 return h & 0x7fffffff;
drh75897232000-05-29 14:26:00 +0000507}
508
509/*
drh967e8b72000-06-21 13:59:10 +0000510** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000511** there is no consistency, we will define our own.
512*/
danielk19774adee202004-05-08 08:23:19 +0000513int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000514 register unsigned char *a, *b;
515 a = (unsigned char *)zLeft;
516 b = (unsigned char *)zRight;
517 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drh2b735012004-07-15 13:23:21 +0000518 return UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000519}
danielk19774adee202004-05-08 08:23:19 +0000520int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000521 register unsigned char *a, *b;
522 a = (unsigned char *)zLeft;
523 b = (unsigned char *)zRight;
524 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000525 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000526}
527
drha5c2ad02000-09-14 01:21:10 +0000528/*
drh7a7c7392001-11-24 00:31:46 +0000529** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000530** string contains any character which is not part of a number. If
531** the string is numeric and contains the '.' character, set *realnum
532** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000533**
drh873cdcb2004-09-05 23:23:41 +0000534** An empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000535*/
danielk19778a6b5412004-05-24 07:04:25 +0000536int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000537 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000538 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000539 if( *z=='-' || *z=='+' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000540 if( !isdigit(*(u8*)z) ){
drhbb07e9a2003-04-16 02:17:35 +0000541 return 0;
drha5c2ad02000-09-14 01:21:10 +0000542 }
danielk19778a6b5412004-05-24 07:04:25 +0000543 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000544 if( realnum ) *realnum = 0;
drh4c755c02004-08-08 20:22:17 +0000545 while( isdigit(*(u8*)z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000546 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000547 z += incr;
drh4c755c02004-08-08 20:22:17 +0000548 if( !isdigit(*(u8*)z) ) return 0;
549 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000550 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000551 }
552 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000553 z += incr;
554 if( *z=='+' || *z=='-' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000555 if( !isdigit(*(u8*)z) ) return 0;
556 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000557 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000558 }
drh7a7c7392001-11-24 00:31:46 +0000559 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000560}
561
drh93a5c6b2003-12-23 02:17:35 +0000562/*
563** The string z[] is an ascii representation of a real number.
564** Convert this string to a double.
565**
566** This routine assumes that z[] really is a valid number. If it
567** is not, the result is undefined.
568**
569** This routine is used instead of the library atof() function because
570** the library atof() might want to use "," as the decimal point instead
571** of "." depending on how locale is set. But that would cause problems
572** for SQL. So this routine always uses "." regardless of locale.
573*/
danielk19774adee202004-05-08 08:23:19 +0000574double sqlite3AtoF(const char *z, const char **pzEnd){
drh93a5c6b2003-12-23 02:17:35 +0000575 int sign = 1;
drh384eef32004-01-07 03:04:27 +0000576 LONGDOUBLE_TYPE v1 = 0.0;
drh93a5c6b2003-12-23 02:17:35 +0000577 if( *z=='-' ){
578 sign = -1;
579 z++;
580 }else if( *z=='+' ){
581 z++;
582 }
drh4c755c02004-08-08 20:22:17 +0000583 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000584 v1 = v1*10.0 + (*z - '0');
585 z++;
586 }
587 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000588 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000589 z++;
drh4c755c02004-08-08 20:22:17 +0000590 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000591 v1 = v1*10.0 + (*z - '0');
592 divisor *= 10.0;
593 z++;
594 }
595 v1 /= divisor;
596 }
597 if( *z=='e' || *z=='E' ){
598 int esign = 1;
599 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000600 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000601 z++;
602 if( *z=='-' ){
603 esign = -1;
604 z++;
605 }else if( *z=='+' ){
606 z++;
607 }
drh4c755c02004-08-08 20:22:17 +0000608 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000609 eval = eval*10 + *z - '0';
610 z++;
611 }
612 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
613 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
614 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
615 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
616 if( esign<0 ){
617 v1 /= scale;
618 }else{
619 v1 *= scale;
620 }
621 }
drheb9a9e82004-02-22 17:49:32 +0000622 if( pzEnd ) *pzEnd = z;
drh93a5c6b2003-12-23 02:17:35 +0000623 return sign<0 ? -v1 : v1;
624}
625
drh202b2df2004-01-06 01:13:46 +0000626/*
drhfec19aa2004-05-19 20:41:03 +0000627** Return TRUE if zNum is a 64-bit signed integer and write
628** the value of the integer into *pNum. If zNum is not an integer
629** or is an integer that is too large to be expressed with 64 bits,
630** then return false. If n>0 and the integer is string is not
631** exactly n bytes long, return false.
632**
633** When this routine was originally written it dealt with only
634** 32-bit numbers. At that time, it was much faster than the
635** atoi() library routine in RedHat 7.2.
636*/
drheb2e1762004-05-27 01:53:56 +0000637int sqlite3atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000638 i64 v = 0;
639 int neg;
640 int i, c;
641 if( *zNum=='-' ){
642 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000643 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000644 }else if( *zNum=='+' ){
645 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000646 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000647 }else{
648 neg = 0;
649 }
drheb2e1762004-05-27 01:53:56 +0000650 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000651 v = v*10 + c - '0';
652 }
653 *pNum = neg ? -v : v;
654 return c==0 && i>0 &&
655 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
656}
657
658/*
drh202b2df2004-01-06 01:13:46 +0000659** The string zNum represents an integer. There might be some other
660** information following the integer too, but that part is ignored.
661** If the integer that the prefix of zNum represents will fit in a
662** 32-bit signed integer, return TRUE. Otherwise return FALSE.
663**
664** This routine returns FALSE for the string -2147483648 even that
drh873cdcb2004-09-05 23:23:41 +0000665** that number will in fact fit in a 32-bit integer. But positive
drh202b2df2004-01-06 01:13:46 +0000666** 2147483648 will not fit in 32 bits. So it seems safer to return
667** false.
668*/
drhfec19aa2004-05-19 20:41:03 +0000669static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000670 int i, c;
671 if( *zNum=='-' || *zNum=='+' ) zNum++;
672 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
673 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
674}
675
drhfec19aa2004-05-19 20:41:03 +0000676/*
677** If zNum represents an integer that will fit in 32-bits, then set
678** *pValue to that integer and return true. Otherwise return false.
679*/
680int sqlite3GetInt32(const char *zNum, int *pValue){
681 if( sqlite3FitsIn32Bits(zNum) ){
682 *pValue = atoi(zNum);
683 return 1;
684 }
685 return 0;
686}
687
688/*
689** The string zNum represents an integer. There might be some other
690** information following the integer too, but that part is ignored.
691** If the integer that the prefix of zNum represents will fit in a
692** 64-bit signed integer, return TRUE. Otherwise return FALSE.
693**
694** This routine returns FALSE for the string -9223372036854775808 even that
695** that number will, in theory fit in a 64-bit integer. Positive
696** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
697** false.
698*/
699int sqlite3FitsIn64Bits(const char *zNum){
700 int i, c;
701 if( *zNum=='-' || *zNum=='+' ) zNum++;
702 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
703 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
704}
705
drhdce2cbe2000-05-31 02:27:49 +0000706
707/*
drhc22bd472002-05-10 13:14:07 +0000708** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
709** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
710** when this routine is called.
711**
712** This routine is a attempt to detect if two threads use the
713** same sqlite* pointer at the same time. There is a race
714** condition so it is possible that the error is not detected.
715** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +0000716** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +0000717** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +0000718**
719** Ticket #202: If db->magic is not a valid open value, take care not
720** to modify the db structure at all. It could be that db is a stale
721** pointer. In other words, it could be that there has been a prior
danielk19776f8a5032004-05-10 10:34:51 +0000722** call to sqlite3_close(db) and db has been deallocated. And we do
drhe7e8bc72002-12-17 13:05:25 +0000723** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +0000724*/
drh9bb575f2004-09-06 17:24:11 +0000725int sqlite3SafetyOn(sqlite3 *db){
drhc22bd472002-05-10 13:14:07 +0000726 if( db->magic==SQLITE_MAGIC_OPEN ){
727 db->magic = SQLITE_MAGIC_BUSY;
728 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000729 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000730 db->magic = SQLITE_MAGIC_ERROR;
731 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +0000732 }
drhe7e8bc72002-12-17 13:05:25 +0000733 return 1;
drhc22bd472002-05-10 13:14:07 +0000734}
735
736/*
737** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
738** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
739** when this routine is called.
740*/
drh9bb575f2004-09-06 17:24:11 +0000741int sqlite3SafetyOff(sqlite3 *db){
drhc22bd472002-05-10 13:14:07 +0000742 if( db->magic==SQLITE_MAGIC_BUSY ){
743 db->magic = SQLITE_MAGIC_OPEN;
744 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000745 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000746 db->magic = SQLITE_MAGIC_ERROR;
747 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +0000748 }
drhe7e8bc72002-12-17 13:05:25 +0000749 return 1;
drhc22bd472002-05-10 13:14:07 +0000750}
751
752/*
drhc60d0442004-09-30 13:43:13 +0000753** Check to make sure we have a valid db pointer. This test is not
754** foolproof but it does provide some measure of protection against
755** misuse of the interface such as passing in db pointers that are
756** NULL or which have been previously closed. If this routine returns
757** TRUE it means that the db pointer is invalid and should not be
758** dereferenced for any reason. The calling function should invoke
759** SQLITE_MISUSE immediately.
drhc22bd472002-05-10 13:14:07 +0000760*/
drh9bb575f2004-09-06 17:24:11 +0000761int sqlite3SafetyCheck(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000762 int magic;
763 if( db==0 ) return 1;
764 magic = db->magic;
765 if( magic!=SQLITE_MAGIC_CLOSED &&
766 magic!=SQLITE_MAGIC_OPEN &&
767 magic!=SQLITE_MAGIC_BUSY ) return 1;
drhc22bd472002-05-10 13:14:07 +0000768 return 0;
drh5e00f6c2001-09-13 13:46:56 +0000769}
danielk19774adee202004-05-08 08:23:19 +0000770
drh6d2fb152004-05-14 16:50:06 +0000771/*
drhd8820e82004-05-18 15:57:42 +0000772** The variable-length integer encoding is as follows:
773**
774** KEY:
775** A = 0xxxxxxx 7 bits of data and one flag bit
776** B = 1xxxxxxx 7 bits of data and one flag bit
777** C = xxxxxxxx 8 bits of data
778**
779** 7 bits - A
780** 14 bits - BA
781** 21 bits - BBA
782** 28 bits - BBBA
783** 35 bits - BBBBA
784** 42 bits - BBBBBA
785** 49 bits - BBBBBBA
786** 56 bits - BBBBBBBA
787** 64 bits - BBBBBBBBC
788*/
789
790/*
drh6d2fb152004-05-14 16:50:06 +0000791** Write a 64-bit variable-length integer to memory starting at p[0].
792** The length of data write will be between 1 and 9 bytes. The number
793** of bytes written is returned.
794**
795** A variable-length integer consists of the lower 7 bits of each byte
796** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +0000797** bit clear. Except, if we get to the 9th byte, it stores the full
798** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +0000799*/
danielk1977192ac1d2004-05-10 07:17:30 +0000800int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +0000801 int i, j, n;
802 u8 buf[10];
drhfe2093d2005-01-20 22:48:47 +0000803 if( v & (((u64)0xff000000)<<32) ){
drhd8820e82004-05-18 15:57:42 +0000804 p[8] = v;
805 v >>= 8;
806 for(i=7; i>=0; i--){
807 p[i] = (v & 0x7f) | 0x80;
808 v >>= 7;
809 }
810 return 9;
811 }
drh6d2fb152004-05-14 16:50:06 +0000812 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +0000813 do{
drh6d2fb152004-05-14 16:50:06 +0000814 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +0000815 v >>= 7;
816 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +0000817 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +0000818 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +0000819 for(i=0, j=n-1; j>=0; j--, i++){
820 p[i] = buf[j];
821 }
822 return n;
danielk1977192ac1d2004-05-10 07:17:30 +0000823}
danielk19774adee202004-05-08 08:23:19 +0000824
drh6d2fb152004-05-14 16:50:06 +0000825/*
826** Read a 64-bit variable-length integer from memory starting at p[0].
827** Return the number of bytes read. The value is stored in *v.
828*/
danielk197790e4d952004-05-10 10:05:53 +0000829int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +0000830 u32 x;
831 u64 x64;
832 int n;
833 unsigned char c;
drhe51c44f2004-05-30 20:46:09 +0000834 if( ((c = p[0]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000835 *v = c;
836 return 1;
837 }
838 x = c & 0x7f;
drhe51c44f2004-05-30 20:46:09 +0000839 if( ((c = p[1]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000840 *v = (x<<7) | c;
841 return 2;
842 }
843 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000844 if( ((c = p[2]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000845 *v = (x<<7) | c;
846 return 3;
847 }
848 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000849 if( ((c = p[3]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000850 *v = (x<<7) | c;
851 return 4;
852 }
853 x64 = (x<<7) | (c&0x7f);
854 n = 4;
855 do{
856 c = p[n++];
drhd8820e82004-05-18 15:57:42 +0000857 if( n==9 ){
858 x64 = (x64<<8) | c;
859 break;
860 }
drh6d2fb152004-05-14 16:50:06 +0000861 x64 = (x64<<7) | (c&0x7f);
862 }while( (c & 0x80)!=0 );
863 *v = x64;
864 return n;
865}
866
867/*
868** Read a 32-bit variable-length integer from memory starting at p[0].
869** Return the number of bytes read. The value is stored in *v.
870*/
871int sqlite3GetVarint32(const unsigned char *p, u32 *v){
drhe51c44f2004-05-30 20:46:09 +0000872 u32 x;
873 int n;
874 unsigned char c;
drhe6f85e72004-12-25 01:03:13 +0000875#if 0
drhe51c44f2004-05-30 20:46:09 +0000876 if( ((c = p[0]) & 0x80)==0 ){
877 *v = c;
878 return 1;
danielk1977192ac1d2004-05-10 07:17:30 +0000879 }
drhe51c44f2004-05-30 20:46:09 +0000880 x = c & 0x7f;
881 if( ((c = p[1]) & 0x80)==0 ){
882 *v = (x<<7) | c;
883 return 2;
884 }
drh9d213ef2004-06-30 04:02:11 +0000885 x = (x<<7) | (c & 0x7f);
drhe6f85e72004-12-25 01:03:13 +0000886#else
887 if( ((signed char*)p)[0]>=0 ){
888 *v = p[0];
889 return 1;
890 }
891 x = p[0] & 0x7f;
892 if( ((signed char*)p)[1]>=0 ){
893 *v = (x<<7) | p[1];
894 return 2;
895 }
896 x = (x<<7) | (p[1] & 0x7f);
897#endif
drh9d213ef2004-06-30 04:02:11 +0000898 n = 2;
drhe51c44f2004-05-30 20:46:09 +0000899 do{
900 x = (x<<7) | ((c = p[n++])&0x7f);
901 }while( (c & 0x80)!=0 && n<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000902 *v = x;
903 return n;
904}
905
drh6d2fb152004-05-14 16:50:06 +0000906/*
907** Return the number of bytes that will be needed to store the given
908** 64-bit integer.
909*/
danielk1977192ac1d2004-05-10 07:17:30 +0000910int sqlite3VarintLen(u64 v){
911 int i = 0;
912 do{
913 i++;
914 v >>= 7;
drhd8820e82004-05-18 15:57:42 +0000915 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000916 return i;
917}
danielk1977c572ef72004-05-27 09:28:41 +0000918
drha71aa002004-11-03 13:59:04 +0000919#if (!defined(SQLITE_OMIT_BLOB_LITERAL) && !defined(SQLITE_HAS_CODEC)) \
920 || defined(SQLITE_TEST)
drh9d213ef2004-06-30 04:02:11 +0000921/*
922** Translate a single byte of Hex into an integer.
923*/
924static int hexToInt(int h){
925 if( h>='0' && h<='9' ){
926 return h - '0';
927 }else if( h>='a' && h<='f' ){
928 return h - 'a' + 10;
drh9d213ef2004-06-30 04:02:11 +0000929 }else{
drhcacb2082005-01-11 15:28:33 +0000930 assert( h>='A' && h<='F' );
931 return h - 'A' + 10;
drh9d213ef2004-06-30 04:02:11 +0000932 }
933}
drha71aa002004-11-03 13:59:04 +0000934#endif /* (!SQLITE_OMIT_BLOB_LITERAL && !SQLITE_HAS_CODEC) || SQLITE_TEST */
drh9d213ef2004-06-30 04:02:11 +0000935
drhbf8aa332004-11-04 04:34:14 +0000936#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000937/*
938** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
939** value. Return a pointer to its binary value. Space to hold the
940** binary value has been obtained from malloc and must be freed by
941** the calling routine.
942*/
danielk1977cbb18d22004-05-28 11:37:27 +0000943void *sqlite3HexToBlob(const char *z){
danielk1977c572ef72004-05-27 09:28:41 +0000944 char *zBlob;
945 int i;
946 int n = strlen(z);
947 if( n%2 ) return 0;
948
949 zBlob = (char *)sqliteMalloc(n/2);
drh9d213ef2004-06-30 04:02:11 +0000950 for(i=0; i<n; i+=2){
951 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
danielk1977c572ef72004-05-27 09:28:41 +0000952 }
danielk19773fd0a732004-05-27 13:35:19 +0000953 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +0000954}
drhbf8aa332004-11-04 04:34:14 +0000955#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drhfe63d1c2004-09-08 20:13:04 +0000956
957#if defined(SQLITE_TEST)
958/*
959** Convert text generated by the "%p" conversion format back into
960** a pointer.
961*/
962void *sqlite3TextToPtr(const char *z){
963 void *p;
964 u64 v;
965 u32 v2;
966 if( z[0]=='0' && z[1]=='x' ){
967 z += 2;
968 }
969 v = 0;
970 while( *z ){
971 v = (v<<4) + hexToInt(*z);
972 z++;
973 }
974 if( sizeof(p)==sizeof(v) ){
975 p = *(void**)&v;
976 }else{
977 assert( sizeof(p)==sizeof(v2) );
978 v2 = (u32)v;
979 p = *(void**)&v2;
980 }
981 return p;
982}
983#endif