blob: 57ac13994f914deebadb89be8f260709f40bd11c [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**
danielk19770bb8f362005-05-23 13:00:57 +000017** $Id: util.c,v 1.135 2005/05/23 13:00:58 danielk1977 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/*
drhc96d8532005-05-03 12:30:33 +000068** Number of 32-bit guard words. This should probably be a multiple of
69** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
70** to be 8-byte aligned.
drh4305d102003-07-30 12:34:12 +000071*/
drhc96d8532005-05-03 12:30:33 +000072#define N_GUARD 2
drh8c82b352000-12-10 18:23:50 +000073
74/*
drhdcc581c2000-05-30 13:44:19 +000075** Allocate new memory and set it to zero. Return NULL if
76** no memory is available.
77*/
danielk19774adee202004-05-08 08:23:19 +000078void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +000079 void *p;
80 int *pi;
drh4305d102003-07-30 12:34:12 +000081 int i, k;
danielk19776f8a5032004-05-10 10:34:51 +000082 if( sqlite3_iMallocFail>=0 ){
83 sqlite3_iMallocFail--;
84 if( sqlite3_iMallocFail==0 ){
85 sqlite3_malloc_failed++;
danielk19772c336542005-01-13 02:14:23 +000086#if SQLITE_MEMDEBUG>1
drh6d4abfb2001-10-22 02:58:08 +000087 fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
88 n, zFile,line);
89#endif
drh46934232004-11-20 19:18:00 +000090 sqlite3_iMallocFail = sqlite3_iMallocReset;
drhdaffd0e2001-04-11 14:28:42 +000091 return 0;
92 }
drh6e142f52000-06-08 13:36:40 +000093 }
drhb0729502001-03-14 12:35:57 +000094 if( n==0 ) return 0;
drhdcc581c2000-05-30 13:44:19 +000095 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +000096 pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
drhdaffd0e2001-04-11 14:28:42 +000097 if( pi==0 ){
danielk1977a38432d2005-02-01 10:35:06 +000098 if( n>0 ) sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +000099 return 0;
100 }
danielk19776f8a5032004-05-10 10:34:51 +0000101 sqlite3_nMalloc++;
drh4305d102003-07-30 12:34:12 +0000102 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
103 pi[N_GUARD] = n;
104 for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
105 p = &pi[N_GUARD+1];
drh8c1238a2003-01-02 14:43:55 +0000106 memset(p, bZero==0, n);
danielk19772c336542005-01-13 02:14:23 +0000107#if SQLITE_MEMDEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000108 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000109 fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
110 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000111#endif
drhdcc581c2000-05-30 13:44:19 +0000112 return p;
113}
114
115/*
drh132d8d62005-05-22 20:12:37 +0000116** This version of malloc is always a real function, never a macro
117*/
118void *sqlite3MallocX(int n){
119 return sqlite3Malloc_(n, 0, __FILE__, __LINE__);
120}
121
122/*
drhed6c8672003-01-12 18:02:16 +0000123** Check to see if the given pointer was obtained from sqliteMalloc()
124** and is able to hold at least N bytes. Raise an exception if this
125** is not the case.
126**
127** This routine is used for testing purposes only.
128*/
danielk19774adee202004-05-08 08:23:19 +0000129void sqlite3CheckMemory(void *p, int N){
drhed6c8672003-01-12 18:02:16 +0000130 int *pi = p;
drh4305d102003-07-30 12:34:12 +0000131 int n, i, k;
132 pi -= N_GUARD+1;
133 for(i=0; i<N_GUARD; i++){
134 assert( pi[i]==0xdead1122 );
135 }
136 n = pi[N_GUARD];
drhed6c8672003-01-12 18:02:16 +0000137 assert( N>=0 && N<n );
138 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000139 for(i=0; i<N_GUARD; i++){
140 assert( pi[k+N_GUARD+1+i]==0xdead3344 );
141 }
drhed6c8672003-01-12 18:02:16 +0000142}
143
144/*
drhdcc581c2000-05-30 13:44:19 +0000145** Free memory previously obtained from sqliteMalloc()
146*/
danielk19774adee202004-05-08 08:23:19 +0000147void sqlite3Free_(void *p, char *zFile, int line){
drhdcc581c2000-05-30 13:44:19 +0000148 if( p ){
drh4305d102003-07-30 12:34:12 +0000149 int *pi, i, k, n;
drhdcc581c2000-05-30 13:44:19 +0000150 pi = p;
drh4305d102003-07-30 12:34:12 +0000151 pi -= N_GUARD+1;
danielk19776f8a5032004-05-10 10:34:51 +0000152 sqlite3_nFree++;
drh4305d102003-07-30 12:34:12 +0000153 for(i=0; i<N_GUARD; i++){
154 if( pi[i]!=0xdead1122 ){
155 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
156 return;
157 }
drhdcc581c2000-05-30 13:44:19 +0000158 }
drh4305d102003-07-30 12:34:12 +0000159 n = pi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000160 k = (n+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000161 for(i=0; i<N_GUARD; i++){
162 if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
163 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
164 return;
165 }
drhdcc581c2000-05-30 13:44:19 +0000166 }
drh4305d102003-07-30 12:34:12 +0000167 memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
danielk19772c336542005-01-13 02:14:23 +0000168#if SQLITE_MEMDEBUG>1
drhd94a6692002-08-25 18:29:11 +0000169 fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
170 ++memcnt, n, (int)p, zFile,line);
drhc3c2fc92000-05-31 22:58:39 +0000171#endif
drhdcc581c2000-05-30 13:44:19 +0000172 free(pi);
173 }
174}
175
176/*
177** Resize a prior allocation. If p==0, then this routine
178** works just like sqliteMalloc(). If n==0, then this routine
179** works just like sqliteFree().
180*/
danielk19774adee202004-05-08 08:23:19 +0000181void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){
drh4305d102003-07-30 12:34:12 +0000182 int *oldPi, *pi, i, k, oldN, oldK;
drhdcc581c2000-05-30 13:44:19 +0000183 void *p;
184 if( oldP==0 ){
danielk19774adee202004-05-08 08:23:19 +0000185 return sqlite3Malloc_(n,1,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000186 }
187 if( n==0 ){
danielk19774adee202004-05-08 08:23:19 +0000188 sqlite3Free_(oldP,zFile,line);
drhdcc581c2000-05-30 13:44:19 +0000189 return 0;
190 }
191 oldPi = oldP;
drh4305d102003-07-30 12:34:12 +0000192 oldPi -= N_GUARD+1;
drhdcc581c2000-05-30 13:44:19 +0000193 if( oldPi[0]!=0xdead1122 ){
drh03ab7332003-08-26 11:29:07 +0000194 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
drh9bb61fe2000-06-05 16:01:39 +0000195 return 0;
drhdcc581c2000-05-30 13:44:19 +0000196 }
drh4305d102003-07-30 12:34:12 +0000197 oldN = oldPi[N_GUARD];
drhdcc581c2000-05-30 13:44:19 +0000198 oldK = (oldN+sizeof(int)-1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000199 for(i=0; i<N_GUARD; i++){
200 if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
drh03ab7332003-08-26 11:29:07 +0000201 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
202 (int)oldP);
drh4305d102003-07-30 12:34:12 +0000203 return 0;
204 }
drhdcc581c2000-05-30 13:44:19 +0000205 }
206 k = (n + sizeof(int) - 1)/sizeof(int);
drh4305d102003-07-30 12:34:12 +0000207 pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
drhdaffd0e2001-04-11 14:28:42 +0000208 if( pi==0 ){
danielk1977a38432d2005-02-01 10:35:06 +0000209 if( n>0 ) sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000210 return 0;
211 }
drh4305d102003-07-30 12:34:12 +0000212 for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
213 pi[N_GUARD] = n;
214 for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
215 p = &pi[N_GUARD+1];
drhdcc581c2000-05-30 13:44:19 +0000216 memcpy(p, oldP, n>oldN ? oldN : n);
217 if( n>oldN ){
danielk197725b33632004-06-30 12:49:46 +0000218 memset(&((char*)p)[oldN], 0x55, n-oldN);
drhdcc581c2000-05-30 13:44:19 +0000219 }
drh4305d102003-07-30 12:34:12 +0000220 memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
drhdcc581c2000-05-30 13:44:19 +0000221 free(oldPi);
danielk19772c336542005-01-13 02:14:23 +0000222#if SQLITE_MEMDEBUG>1
danielk1977eac7a362004-06-16 07:45:24 +0000223 print_stack_trace();
drhd94a6692002-08-25 18:29:11 +0000224 fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
225 ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000226#endif
drhdcc581c2000-05-30 13:44:19 +0000227 return p;
228}
drhc3c2fc92000-05-31 22:58:39 +0000229
230/*
drh6e142f52000-06-08 13:36:40 +0000231** Make a copy of a string in memory obtained from sqliteMalloc()
232*/
danielk19774adee202004-05-08 08:23:19 +0000233char *sqlite3StrDup_(const char *z, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000234 char *zNew;
235 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000236 zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000237 if( zNew ) strcpy(zNew, z);
238 return zNew;
239}
danielk19774adee202004-05-08 08:23:19 +0000240char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){
drhff78bd22002-02-27 01:47:11 +0000241 char *zNew;
242 if( z==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +0000243 zNew = sqlite3Malloc_(n+1, 0, zFile, line);
drh6e142f52000-06-08 13:36:40 +0000244 if( zNew ){
245 memcpy(zNew, z, n);
246 zNew[n] = 0;
247 }
248 return zNew;
249}
danielk1977bfd6cce2004-06-18 04:24:54 +0000250
251/*
252** A version of sqliteFree that is always a function, not a macro.
253*/
254void sqlite3FreeX(void *p){
255 sqliteFree(p);
256}
danielk19772c336542005-01-13 02:14:23 +0000257#endif /* SQLITE_MEMDEBUG */
drh6e142f52000-06-08 13:36:40 +0000258
drh7c68d602000-10-11 19:28:51 +0000259/*
260** The following versions of malloc() and free() are for use in a
261** normal build.
262*/
danielk19772c336542005-01-13 02:14:23 +0000263#if !defined(SQLITE_MEMDEBUG)
drh6e142f52000-06-08 13:36:40 +0000264
drh75897232000-05-29 14:26:00 +0000265/*
266** Allocate new memory and set it to zero. Return NULL if
drh8c1238a2003-01-02 14:43:55 +0000267** no memory is available. See also sqliteMallocRaw().
drh75897232000-05-29 14:26:00 +0000268*/
drh38f82712004-06-18 17:10:16 +0000269void *sqlite3Malloc(int n){
drh26780582002-10-20 15:46:22 +0000270 void *p;
drh8548a052003-10-22 22:15:27 +0000271 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000272 if( n>0 ) sqlite3_malloc_failed++;
drh8548a052003-10-22 22:15:27 +0000273 }else{
274 memset(p, 0, n);
drhdaffd0e2001-04-11 14:28:42 +0000275 }
drh75897232000-05-29 14:26:00 +0000276 return p;
277}
278
279/*
drh8c1238a2003-01-02 14:43:55 +0000280** Allocate new memory but do not set it to zero. Return NULL if
281** no memory is available. See also sqliteMalloc().
282*/
drh38f82712004-06-18 17:10:16 +0000283void *sqlite3MallocRaw(int n){
drh8c1238a2003-01-02 14:43:55 +0000284 void *p;
drh8548a052003-10-22 22:15:27 +0000285 if( (p = malloc(n))==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000286 if( n>0 ) sqlite3_malloc_failed++;
drh8c1238a2003-01-02 14:43:55 +0000287 }
288 return p;
289}
290
291/*
drh75897232000-05-29 14:26:00 +0000292** Free memory previously obtained from sqliteMalloc()
293*/
drh38f82712004-06-18 17:10:16 +0000294void sqlite3FreeX(void *p){
drh305cea62000-05-29 17:44:25 +0000295 if( p ){
drh305cea62000-05-29 17:44:25 +0000296 free(p);
297 }
drh75897232000-05-29 14:26:00 +0000298}
299
300/*
301** Resize a prior allocation. If p==0, then this routine
302** works just like sqliteMalloc(). If n==0, then this routine
303** works just like sqliteFree().
304*/
drh38f82712004-06-18 17:10:16 +0000305void *sqlite3Realloc(void *p, int n){
drh6d4abfb2001-10-22 02:58:08 +0000306 void *p2;
drh75897232000-05-29 14:26:00 +0000307 if( p==0 ){
308 return sqliteMalloc(n);
309 }
310 if( n==0 ){
311 sqliteFree(p);
312 return 0;
313 }
drh6d4abfb2001-10-22 02:58:08 +0000314 p2 = realloc(p, n);
315 if( p2==0 ){
danielk1977a38432d2005-02-01 10:35:06 +0000316 if( n>0 ) sqlite3_malloc_failed++;
drhdaffd0e2001-04-11 14:28:42 +0000317 }
drh6d4abfb2001-10-22 02:58:08 +0000318 return p2;
drh75897232000-05-29 14:26:00 +0000319}
drh6e142f52000-06-08 13:36:40 +0000320
321/*
322** Make a copy of a string in memory obtained from sqliteMalloc()
323*/
drh38f82712004-06-18 17:10:16 +0000324char *sqlite3StrDup(const char *z){
drh567c6042002-02-28 04:10:29 +0000325 char *zNew;
326 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000327 zNew = sqliteMallocRaw(strlen(z)+1);
drh6e142f52000-06-08 13:36:40 +0000328 if( zNew ) strcpy(zNew, z);
329 return zNew;
330}
drh38f82712004-06-18 17:10:16 +0000331char *sqlite3StrNDup(const char *z, int n){
drh567c6042002-02-28 04:10:29 +0000332 char *zNew;
333 if( z==0 ) return 0;
drh8c1238a2003-01-02 14:43:55 +0000334 zNew = sqliteMallocRaw(n+1);
drh6e142f52000-06-08 13:36:40 +0000335 if( zNew ){
336 memcpy(zNew, z, n);
337 zNew[n] = 0;
338 }
339 return zNew;
340}
danielk19772c336542005-01-13 02:14:23 +0000341#endif /* !defined(SQLITE_MEMDEBUG) */
drh75897232000-05-29 14:26:00 +0000342
343/*
344** Create a string from the 2nd and subsequent arguments (up to the
345** first NULL argument), store the string in memory obtained from
346** sqliteMalloc() and make the pointer indicated by the 1st argument
jplyon02be20d2003-06-02 06:17:10 +0000347** point to that string. The 1st argument must either be NULL or
348** point to memory obtained from sqliteMalloc().
drh75897232000-05-29 14:26:00 +0000349*/
danielk19774adee202004-05-08 08:23:19 +0000350void sqlite3SetString(char **pz, const char *zFirst, ...){
drh75897232000-05-29 14:26:00 +0000351 va_list ap;
352 int nByte;
353 const char *z;
354 char *zResult;
355
356 if( pz==0 ) return;
357 nByte = strlen(zFirst) + 1;
358 va_start(ap, zFirst);
359 while( (z = va_arg(ap, const char*))!=0 ){
360 nByte += strlen(z);
361 }
362 va_end(ap);
363 sqliteFree(*pz);
drh8c1238a2003-01-02 14:43:55 +0000364 *pz = zResult = sqliteMallocRaw( nByte );
drh6d4abfb2001-10-22 02:58:08 +0000365 if( zResult==0 ){
366 return;
367 }
drh75897232000-05-29 14:26:00 +0000368 strcpy(zResult, zFirst);
369 zResult += strlen(zResult);
370 va_start(ap, zFirst);
371 while( (z = va_arg(ap, const char*))!=0 ){
372 strcpy(zResult, z);
373 zResult += strlen(zResult);
374 }
375 va_end(ap);
drhfaa57ac2004-06-09 14:01:51 +0000376#ifdef SQLITE_DEBUG
377#if SQLITE_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000378 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
379#endif
380#endif
drh75897232000-05-29 14:26:00 +0000381}
382
383/*
danielk19776622cce2004-05-20 11:00:52 +0000384** Set the most recent error code and error string for the sqlite
385** handle "db". The error code is set to "err_code".
386**
387** If it is not NULL, string zFormat specifies the format of the
388** error string in the style of the printf functions: The following
389** format characters are allowed:
390**
391** %s Insert a string
392** %z A string that should be freed after use
393** %d Insert an integer
394** %T Insert a token
395** %S Insert the first element of a SrcList
396**
397** zFormat and any string tokens that follow it are assumed to be
398** encoded in UTF-8.
399**
danielk19770bb8f362005-05-23 13:00:57 +0000400** To clear the most recent error for sqlite handle "db", sqlite3Error
danielk19776622cce2004-05-20 11:00:52 +0000401** should be called with err_code set to SQLITE_OK and zFormat set
402** to NULL.
403*/
drh9bb575f2004-09-06 17:24:11 +0000404void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
danielk1977bfd6cce2004-06-18 04:24:54 +0000405 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){
406 db->errCode = err_code;
407 if( zFormat ){
408 char *z;
409 va_list ap;
410 va_start(ap, zFormat);
411 z = sqlite3VMPrintf(zFormat, ap);
412 va_end(ap);
413 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
414 }else{
415 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
416 }
danielk19776622cce2004-05-20 11:00:52 +0000417 }
418}
419
420/*
drhda93d232003-03-31 02:12:46 +0000421** Add an error message to pParse->zErrMsg and increment pParse->nErr.
422** The following formatting characters are allowed:
423**
424** %s Insert a string
425** %z A string that should be freed after use
426** %d Insert an integer
427** %T Insert a token
428** %S Insert the first element of a SrcList
danielk19779e6db7d2004-06-21 08:18:51 +0000429**
430** This function should be used to report any error that occurs whilst
431** compiling an SQL statement (i.e. within sqlite3_prepare()). The
432** last thing the sqlite3_prepare() function does is copy the error
433** stored by this function into the database handle using sqlite3Error().
434** Function sqlite3Error() should be used during statement execution
435** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +0000436*/
danielk19774adee202004-05-08 08:23:19 +0000437void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000438 va_list ap;
drhda93d232003-03-31 02:12:46 +0000439 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +0000440 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000441 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +0000442 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000443 va_end(ap);
drhda93d232003-03-31 02:12:46 +0000444}
445
446/*
drh982cef72000-05-30 16:27:03 +0000447** Convert an SQL-style quoted string into a normal string by removing
448** the quote characters. The conversion is done in-place. If the
449** input does not begin with a quote character, then this routine
450** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000451**
452** 2002-Feb-14: This routine is extended to remove MS-Access style
453** brackets from around identifers. For example: "[a-b-c]" becomes
454** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000455*/
danielk19774adee202004-05-08 08:23:19 +0000456void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000457 int quote;
458 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000459 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000460 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000461 switch( quote ){
462 case '\'': break;
463 case '"': break;
464 case '[': quote = ']'; break;
465 default: return;
466 }
drh982cef72000-05-30 16:27:03 +0000467 for(i=1, j=0; z[i]; i++){
468 if( z[i]==quote ){
469 if( z[i+1]==quote ){
470 z[j++] = quote;
471 i++;
472 }else{
473 z[j++] = 0;
474 break;
475 }
476 }else{
477 z[j++] = z[i];
478 }
479 }
480}
481
drh75897232000-05-29 14:26:00 +0000482/* An array to map all upper-case characters into their corresponding
483** lower-case character.
484*/
drh4e5ffc52004-08-31 00:52:37 +0000485const unsigned char sqlite3UpperToLower[] = {
drh75897232000-05-29 14:26:00 +0000486 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
487 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
488 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
489 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
490 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
491 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
492 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
493 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
494 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
495 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
496 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
497 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
498 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
499 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
500 252,253,254,255
501};
drh4e5ffc52004-08-31 00:52:37 +0000502#define UpperToLower sqlite3UpperToLower
drh75897232000-05-29 14:26:00 +0000503
504/*
drh967e8b72000-06-21 13:59:10 +0000505** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000506** there is no consistency, we will define our own.
507*/
danielk19774adee202004-05-08 08:23:19 +0000508int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000509 register unsigned char *a, *b;
510 a = (unsigned char *)zLeft;
511 b = (unsigned char *)zRight;
512 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drh2b735012004-07-15 13:23:21 +0000513 return UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000514}
danielk19774adee202004-05-08 08:23:19 +0000515int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000516 register unsigned char *a, *b;
517 a = (unsigned char *)zLeft;
518 b = (unsigned char *)zRight;
519 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000520 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000521}
522
drha5c2ad02000-09-14 01:21:10 +0000523/*
drh7a7c7392001-11-24 00:31:46 +0000524** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000525** string contains any character which is not part of a number. If
526** the string is numeric and contains the '.' character, set *realnum
527** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000528**
drh873cdcb2004-09-05 23:23:41 +0000529** An empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000530*/
danielk19778a6b5412004-05-24 07:04:25 +0000531int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000532 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000533 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000534 if( *z=='-' || *z=='+' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000535 if( !isdigit(*(u8*)z) ){
drhbb07e9a2003-04-16 02:17:35 +0000536 return 0;
drha5c2ad02000-09-14 01:21:10 +0000537 }
danielk19778a6b5412004-05-24 07:04:25 +0000538 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000539 if( realnum ) *realnum = 0;
drh4c755c02004-08-08 20:22:17 +0000540 while( isdigit(*(u8*)z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000541 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000542 z += incr;
drh4c755c02004-08-08 20:22:17 +0000543 if( !isdigit(*(u8*)z) ) return 0;
544 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000545 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000546 }
547 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000548 z += incr;
549 if( *z=='+' || *z=='-' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000550 if( !isdigit(*(u8*)z) ) return 0;
551 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000552 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000553 }
drh7a7c7392001-11-24 00:31:46 +0000554 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000555}
556
drh93a5c6b2003-12-23 02:17:35 +0000557/*
558** The string z[] is an ascii representation of a real number.
559** Convert this string to a double.
560**
561** This routine assumes that z[] really is a valid number. If it
562** is not, the result is undefined.
563**
564** This routine is used instead of the library atof() function because
565** the library atof() might want to use "," as the decimal point instead
566** of "." depending on how locale is set. But that would cause problems
567** for SQL. So this routine always uses "." regardless of locale.
568*/
danielk19774adee202004-05-08 08:23:19 +0000569double sqlite3AtoF(const char *z, const char **pzEnd){
drh93a5c6b2003-12-23 02:17:35 +0000570 int sign = 1;
drh384eef32004-01-07 03:04:27 +0000571 LONGDOUBLE_TYPE v1 = 0.0;
drh93a5c6b2003-12-23 02:17:35 +0000572 if( *z=='-' ){
573 sign = -1;
574 z++;
575 }else if( *z=='+' ){
576 z++;
577 }
drh4c755c02004-08-08 20:22:17 +0000578 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000579 v1 = v1*10.0 + (*z - '0');
580 z++;
581 }
582 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000583 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000584 z++;
drh4c755c02004-08-08 20:22:17 +0000585 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000586 v1 = v1*10.0 + (*z - '0');
587 divisor *= 10.0;
588 z++;
589 }
590 v1 /= divisor;
591 }
592 if( *z=='e' || *z=='E' ){
593 int esign = 1;
594 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000595 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000596 z++;
597 if( *z=='-' ){
598 esign = -1;
599 z++;
600 }else if( *z=='+' ){
601 z++;
602 }
drh4c755c02004-08-08 20:22:17 +0000603 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000604 eval = eval*10 + *z - '0';
605 z++;
606 }
607 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
608 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
609 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
610 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
611 if( esign<0 ){
612 v1 /= scale;
613 }else{
614 v1 *= scale;
615 }
616 }
drheb9a9e82004-02-22 17:49:32 +0000617 if( pzEnd ) *pzEnd = z;
drh93a5c6b2003-12-23 02:17:35 +0000618 return sign<0 ? -v1 : v1;
619}
620
drh202b2df2004-01-06 01:13:46 +0000621/*
drhfec19aa2004-05-19 20:41:03 +0000622** Return TRUE if zNum is a 64-bit signed integer and write
623** the value of the integer into *pNum. If zNum is not an integer
624** or is an integer that is too large to be expressed with 64 bits,
625** then return false. If n>0 and the integer is string is not
626** exactly n bytes long, return false.
627**
628** When this routine was originally written it dealt with only
629** 32-bit numbers. At that time, it was much faster than the
630** atoi() library routine in RedHat 7.2.
631*/
drheb2e1762004-05-27 01:53:56 +0000632int sqlite3atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000633 i64 v = 0;
634 int neg;
635 int i, c;
636 if( *zNum=='-' ){
637 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000638 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000639 }else if( *zNum=='+' ){
640 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000641 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000642 }else{
643 neg = 0;
644 }
drheb2e1762004-05-27 01:53:56 +0000645 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000646 v = v*10 + c - '0';
647 }
648 *pNum = neg ? -v : v;
649 return c==0 && i>0 &&
650 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
651}
652
653/*
drh202b2df2004-01-06 01:13:46 +0000654** The string zNum represents an integer. There might be some other
655** information following the integer too, but that part is ignored.
656** If the integer that the prefix of zNum represents will fit in a
657** 32-bit signed integer, return TRUE. Otherwise return FALSE.
658**
659** This routine returns FALSE for the string -2147483648 even that
drh873cdcb2004-09-05 23:23:41 +0000660** that number will in fact fit in a 32-bit integer. But positive
drh202b2df2004-01-06 01:13:46 +0000661** 2147483648 will not fit in 32 bits. So it seems safer to return
662** false.
663*/
drhfec19aa2004-05-19 20:41:03 +0000664static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000665 int i, c;
666 if( *zNum=='-' || *zNum=='+' ) zNum++;
667 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
668 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
669}
670
drhfec19aa2004-05-19 20:41:03 +0000671/*
672** If zNum represents an integer that will fit in 32-bits, then set
673** *pValue to that integer and return true. Otherwise return false.
674*/
675int sqlite3GetInt32(const char *zNum, int *pValue){
676 if( sqlite3FitsIn32Bits(zNum) ){
677 *pValue = atoi(zNum);
678 return 1;
679 }
680 return 0;
681}
682
683/*
684** The string zNum represents an integer. There might be some other
685** information following the integer too, but that part is ignored.
686** If the integer that the prefix of zNum represents will fit in a
687** 64-bit signed integer, return TRUE. Otherwise return FALSE.
688**
689** This routine returns FALSE for the string -9223372036854775808 even that
690** that number will, in theory fit in a 64-bit integer. Positive
691** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
692** false.
693*/
694int sqlite3FitsIn64Bits(const char *zNum){
695 int i, c;
696 if( *zNum=='-' || *zNum=='+' ) zNum++;
697 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
698 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
699}
700
drhdce2cbe2000-05-31 02:27:49 +0000701
702/*
drhc22bd472002-05-10 13:14:07 +0000703** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
704** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
705** when this routine is called.
706**
707** This routine is a attempt to detect if two threads use the
708** same sqlite* pointer at the same time. There is a race
709** condition so it is possible that the error is not detected.
710** But usually the problem will be seen. The result will be an
drhc27a1ce2002-06-14 20:58:45 +0000711** error which can be used to debug the application that is
drhc22bd472002-05-10 13:14:07 +0000712** using SQLite incorrectly.
drhe7e8bc72002-12-17 13:05:25 +0000713**
714** Ticket #202: If db->magic is not a valid open value, take care not
715** to modify the db structure at all. It could be that db is a stale
716** pointer. In other words, it could be that there has been a prior
danielk19776f8a5032004-05-10 10:34:51 +0000717** call to sqlite3_close(db) and db has been deallocated. And we do
drhe7e8bc72002-12-17 13:05:25 +0000718** not want to write into deallocated memory.
drh5e00f6c2001-09-13 13:46:56 +0000719*/
drh9bb575f2004-09-06 17:24:11 +0000720int sqlite3SafetyOn(sqlite3 *db){
drhc22bd472002-05-10 13:14:07 +0000721 if( db->magic==SQLITE_MAGIC_OPEN ){
722 db->magic = SQLITE_MAGIC_BUSY;
723 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000724 }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000725 db->magic = SQLITE_MAGIC_ERROR;
726 db->flags |= SQLITE_Interrupt;
drh5e00f6c2001-09-13 13:46:56 +0000727 }
drhe7e8bc72002-12-17 13:05:25 +0000728 return 1;
drhc22bd472002-05-10 13:14:07 +0000729}
730
731/*
732** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
733** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
734** when this routine is called.
735*/
drh9bb575f2004-09-06 17:24:11 +0000736int sqlite3SafetyOff(sqlite3 *db){
drhc22bd472002-05-10 13:14:07 +0000737 if( db->magic==SQLITE_MAGIC_BUSY ){
738 db->magic = SQLITE_MAGIC_OPEN;
739 return 0;
danielk197796d81f92004-06-19 03:33:57 +0000740 }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){
drhc22bd472002-05-10 13:14:07 +0000741 db->magic = SQLITE_MAGIC_ERROR;
742 db->flags |= SQLITE_Interrupt;
drhc22bd472002-05-10 13:14:07 +0000743 }
drhe7e8bc72002-12-17 13:05:25 +0000744 return 1;
drhc22bd472002-05-10 13:14:07 +0000745}
746
747/*
drhc60d0442004-09-30 13:43:13 +0000748** Check to make sure we have a valid db pointer. This test is not
749** foolproof but it does provide some measure of protection against
750** misuse of the interface such as passing in db pointers that are
751** NULL or which have been previously closed. If this routine returns
752** TRUE it means that the db pointer is invalid and should not be
753** dereferenced for any reason. The calling function should invoke
754** SQLITE_MISUSE immediately.
drhc22bd472002-05-10 13:14:07 +0000755*/
drh9bb575f2004-09-06 17:24:11 +0000756int sqlite3SafetyCheck(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000757 int magic;
758 if( db==0 ) return 1;
759 magic = db->magic;
760 if( magic!=SQLITE_MAGIC_CLOSED &&
761 magic!=SQLITE_MAGIC_OPEN &&
762 magic!=SQLITE_MAGIC_BUSY ) return 1;
drhc22bd472002-05-10 13:14:07 +0000763 return 0;
drh5e00f6c2001-09-13 13:46:56 +0000764}
danielk19774adee202004-05-08 08:23:19 +0000765
drh6d2fb152004-05-14 16:50:06 +0000766/*
drhd8820e82004-05-18 15:57:42 +0000767** The variable-length integer encoding is as follows:
768**
769** KEY:
770** A = 0xxxxxxx 7 bits of data and one flag bit
771** B = 1xxxxxxx 7 bits of data and one flag bit
772** C = xxxxxxxx 8 bits of data
773**
774** 7 bits - A
775** 14 bits - BA
776** 21 bits - BBA
777** 28 bits - BBBA
778** 35 bits - BBBBA
779** 42 bits - BBBBBA
780** 49 bits - BBBBBBA
781** 56 bits - BBBBBBBA
782** 64 bits - BBBBBBBBC
783*/
784
785/*
drh6d2fb152004-05-14 16:50:06 +0000786** Write a 64-bit variable-length integer to memory starting at p[0].
787** The length of data write will be between 1 and 9 bytes. The number
788** of bytes written is returned.
789**
790** A variable-length integer consists of the lower 7 bits of each byte
791** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +0000792** bit clear. Except, if we get to the 9th byte, it stores the full
793** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +0000794*/
danielk1977192ac1d2004-05-10 07:17:30 +0000795int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +0000796 int i, j, n;
797 u8 buf[10];
drhfe2093d2005-01-20 22:48:47 +0000798 if( v & (((u64)0xff000000)<<32) ){
drhd8820e82004-05-18 15:57:42 +0000799 p[8] = v;
800 v >>= 8;
801 for(i=7; i>=0; i--){
802 p[i] = (v & 0x7f) | 0x80;
803 v >>= 7;
804 }
805 return 9;
806 }
drh6d2fb152004-05-14 16:50:06 +0000807 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +0000808 do{
drh6d2fb152004-05-14 16:50:06 +0000809 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +0000810 v >>= 7;
811 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +0000812 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +0000813 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +0000814 for(i=0, j=n-1; j>=0; j--, i++){
815 p[i] = buf[j];
816 }
817 return n;
danielk1977192ac1d2004-05-10 07:17:30 +0000818}
danielk19774adee202004-05-08 08:23:19 +0000819
drh6d2fb152004-05-14 16:50:06 +0000820/*
821** Read a 64-bit variable-length integer from memory starting at p[0].
822** Return the number of bytes read. The value is stored in *v.
823*/
danielk197790e4d952004-05-10 10:05:53 +0000824int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +0000825 u32 x;
826 u64 x64;
827 int n;
828 unsigned char c;
drhe51c44f2004-05-30 20:46:09 +0000829 if( ((c = p[0]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000830 *v = c;
831 return 1;
832 }
833 x = c & 0x7f;
drhe51c44f2004-05-30 20:46:09 +0000834 if( ((c = p[1]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000835 *v = (x<<7) | c;
836 return 2;
837 }
838 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000839 if( ((c = p[2]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000840 *v = (x<<7) | c;
841 return 3;
842 }
843 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000844 if( ((c = p[3]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000845 *v = (x<<7) | c;
846 return 4;
847 }
848 x64 = (x<<7) | (c&0x7f);
849 n = 4;
850 do{
851 c = p[n++];
drhd8820e82004-05-18 15:57:42 +0000852 if( n==9 ){
853 x64 = (x64<<8) | c;
854 break;
855 }
drh6d2fb152004-05-14 16:50:06 +0000856 x64 = (x64<<7) | (c&0x7f);
857 }while( (c & 0x80)!=0 );
858 *v = x64;
859 return n;
860}
861
862/*
863** Read a 32-bit variable-length integer from memory starting at p[0].
864** Return the number of bytes read. The value is stored in *v.
865*/
866int sqlite3GetVarint32(const unsigned char *p, u32 *v){
drhe51c44f2004-05-30 20:46:09 +0000867 u32 x;
868 int n;
869 unsigned char c;
drhe6f85e72004-12-25 01:03:13 +0000870 if( ((signed char*)p)[0]>=0 ){
871 *v = p[0];
872 return 1;
873 }
874 x = p[0] & 0x7f;
875 if( ((signed char*)p)[1]>=0 ){
876 *v = (x<<7) | p[1];
877 return 2;
878 }
879 x = (x<<7) | (p[1] & 0x7f);
drh9d213ef2004-06-30 04:02:11 +0000880 n = 2;
drhe51c44f2004-05-30 20:46:09 +0000881 do{
882 x = (x<<7) | ((c = p[n++])&0x7f);
883 }while( (c & 0x80)!=0 && n<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000884 *v = x;
885 return n;
886}
887
drh6d2fb152004-05-14 16:50:06 +0000888/*
889** Return the number of bytes that will be needed to store the given
890** 64-bit integer.
891*/
danielk1977192ac1d2004-05-10 07:17:30 +0000892int sqlite3VarintLen(u64 v){
893 int i = 0;
894 do{
895 i++;
896 v >>= 7;
drhd8820e82004-05-18 15:57:42 +0000897 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000898 return i;
899}
danielk1977c572ef72004-05-27 09:28:41 +0000900
drh33fa5352005-03-10 12:35:45 +0000901#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
drha71aa002004-11-03 13:59:04 +0000902 || defined(SQLITE_TEST)
drh9d213ef2004-06-30 04:02:11 +0000903/*
904** Translate a single byte of Hex into an integer.
905*/
906static int hexToInt(int h){
907 if( h>='0' && h<='9' ){
908 return h - '0';
909 }else if( h>='a' && h<='f' ){
910 return h - 'a' + 10;
drh9d213ef2004-06-30 04:02:11 +0000911 }else{
drhcacb2082005-01-11 15:28:33 +0000912 assert( h>='A' && h<='F' );
913 return h - 'A' + 10;
drh9d213ef2004-06-30 04:02:11 +0000914 }
915}
drh33fa5352005-03-10 12:35:45 +0000916#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
drh9d213ef2004-06-30 04:02:11 +0000917
drhbf8aa332004-11-04 04:34:14 +0000918#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000919/*
920** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
921** value. Return a pointer to its binary value. Space to hold the
922** binary value has been obtained from malloc and must be freed by
923** the calling routine.
924*/
danielk1977cbb18d22004-05-28 11:37:27 +0000925void *sqlite3HexToBlob(const char *z){
danielk1977c572ef72004-05-27 09:28:41 +0000926 char *zBlob;
927 int i;
928 int n = strlen(z);
929 if( n%2 ) return 0;
930
931 zBlob = (char *)sqliteMalloc(n/2);
drh9d213ef2004-06-30 04:02:11 +0000932 for(i=0; i<n; i+=2){
933 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
danielk1977c572ef72004-05-27 09:28:41 +0000934 }
danielk19773fd0a732004-05-27 13:35:19 +0000935 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +0000936}
drhbf8aa332004-11-04 04:34:14 +0000937#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drhfe63d1c2004-09-08 20:13:04 +0000938
939#if defined(SQLITE_TEST)
940/*
941** Convert text generated by the "%p" conversion format back into
942** a pointer.
943*/
944void *sqlite3TextToPtr(const char *z){
945 void *p;
946 u64 v;
947 u32 v2;
948 if( z[0]=='0' && z[1]=='x' ){
949 z += 2;
950 }
951 v = 0;
952 while( *z ){
953 v = (v<<4) + hexToInt(*z);
954 z++;
955 }
956 if( sizeof(p)==sizeof(v) ){
957 p = *(void**)&v;
958 }else{
959 assert( sizeof(p)==sizeof(v2) );
960 v2 = (u32)v;
961 p = *(void**)&v2;
962 }
963 return p;
964}
965#endif