drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Utility functions used throughout sqlite. |
| 13 | ** |
| 14 | ** This file contains functions for allocating memory, comparing |
| 15 | ** strings, and stuff like that. |
| 16 | ** |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.138 2005/06/25 18:42:15 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | #include <stdarg.h> |
| 21 | #include <ctype.h> |
| 22 | |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 23 | #if SQLITE_MEMDEBUG>2 && defined(__GLIBC__) |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 24 | #include <execinfo.h> |
| 25 | void print_stack_trace(){ |
| 26 | void *bt[30]; |
| 27 | int i; |
| 28 | int n = backtrace(bt, 30); |
| 29 | |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 30 | fprintf(stderr, "STACK: "); |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 31 | for(i=0; i<n;i++){ |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 32 | fprintf(stderr, "%p ", bt[i]); |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 33 | } |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 34 | fprintf(stderr, "\n"); |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 35 | } |
| 36 | #else |
| 37 | #define print_stack_trace() |
| 38 | #endif |
| 39 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 40 | /* |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 41 | ** If malloc() ever fails, this global variable gets set to 1. |
| 42 | ** This causes the library to abort and never again function. |
| 43 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 44 | int sqlite3_malloc_failed = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 47 | ** If SQLITE_MEMDEBUG is defined, then use versions of malloc() and |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 48 | ** free() that track memory usage and check for buffer overruns. |
| 49 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 50 | #ifdef SQLITE_MEMDEBUG |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 51 | |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 52 | /* |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 53 | ** For keeping track of the number of mallocs and frees. This |
drh | 4693423 | 2004-11-20 19:18:00 +0000 | [diff] [blame] | 54 | ** 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. |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 58 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 59 | int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ |
| 60 | int sqlite3_nFree; /* Number of sqliteFree() calls */ |
| 61 | int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ |
drh | 4693423 | 2004-11-20 19:18:00 +0000 | [diff] [blame] | 62 | int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 63 | #if SQLITE_MEMDEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 64 | static int memcnt = 0; |
| 65 | #endif |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 66 | |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 67 | /* |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 68 | ** 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. |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 71 | */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 72 | #define N_GUARD 2 |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 75 | ** Allocate new memory and set it to zero. Return NULL if |
| 76 | ** no memory is available. |
| 77 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 78 | void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 79 | void *p; |
| 80 | int *pi; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 81 | int i, k; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 82 | if( sqlite3_iMallocFail>=0 ){ |
| 83 | sqlite3_iMallocFail--; |
| 84 | if( sqlite3_iMallocFail==0 ){ |
| 85 | sqlite3_malloc_failed++; |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 86 | #if SQLITE_MEMDEBUG>1 |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 87 | fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n", |
| 88 | n, zFile,line); |
| 89 | #endif |
drh | 4693423 | 2004-11-20 19:18:00 +0000 | [diff] [blame] | 90 | sqlite3_iMallocFail = sqlite3_iMallocReset; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 91 | return 0; |
| 92 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 93 | } |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 94 | if( n==0 ) return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 95 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 96 | pi = malloc( (N_GUARD*2+1+k)*sizeof(int)); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 97 | if( pi==0 ){ |
danielk1977 | a38432d | 2005-02-01 10:35:06 +0000 | [diff] [blame] | 98 | if( n>0 ) sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 101 | sqlite3_nMalloc++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 102 | 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]; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 106 | memset(p, bZero==0, n); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 107 | #if SQLITE_MEMDEBUG>1 |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 108 | print_stack_trace(); |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 109 | fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n", |
| 110 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 111 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 112 | return p; |
| 113 | } |
| 114 | |
| 115 | /* |
drh | 132d8d6 | 2005-05-22 20:12:37 +0000 | [diff] [blame] | 116 | ** This version of malloc is always a real function, never a macro |
| 117 | */ |
| 118 | void *sqlite3MallocX(int n){ |
| 119 | return sqlite3Malloc_(n, 0, __FILE__, __LINE__); |
| 120 | } |
| 121 | |
| 122 | /* |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 123 | ** 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 129 | void sqlite3CheckMemory(void *p, int N){ |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 130 | int *pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 131 | 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]; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 137 | assert( N>=0 && N<n ); |
| 138 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 139 | for(i=0; i<N_GUARD; i++){ |
| 140 | assert( pi[k+N_GUARD+1+i]==0xdead3344 ); |
| 141 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 145 | ** Free memory previously obtained from sqliteMalloc() |
| 146 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 147 | void sqlite3Free_(void *p, char *zFile, int line){ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 148 | if( p ){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 149 | int *pi, i, k, n; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 150 | pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 151 | pi -= N_GUARD+1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 152 | sqlite3_nFree++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 153 | 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 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 158 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 159 | n = pi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 160 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 161 | 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 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 166 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 167 | memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int)); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 168 | #if SQLITE_MEMDEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 169 | fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n", |
| 170 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 171 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 172 | 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 181 | void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 182 | int *oldPi, *pi, i, k, oldN, oldK; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 183 | void *p; |
| 184 | if( oldP==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 185 | return sqlite3Malloc_(n,1,zFile,line); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 186 | } |
| 187 | if( n==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 188 | sqlite3Free_(oldP,zFile,line); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | oldPi = oldP; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 192 | oldPi -= N_GUARD+1; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 193 | if( oldPi[0]!=0xdead1122 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 194 | fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 195 | return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 196 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 197 | oldN = oldPi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 198 | oldK = (oldN+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 199 | for(i=0; i<N_GUARD; i++){ |
| 200 | if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 201 | fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", |
| 202 | (int)oldP); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 203 | return 0; |
| 204 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 205 | } |
| 206 | k = (n + sizeof(int) - 1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 207 | pi = malloc( (k+N_GUARD*2+1)*sizeof(int) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 208 | if( pi==0 ){ |
danielk1977 | a38432d | 2005-02-01 10:35:06 +0000 | [diff] [blame] | 209 | if( n>0 ) sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 212 | 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]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 216 | memcpy(p, oldP, n>oldN ? oldN : n); |
| 217 | if( n>oldN ){ |
danielk1977 | 25b3363 | 2004-06-30 12:49:46 +0000 | [diff] [blame] | 218 | memset(&((char*)p)[oldN], 0x55, n-oldN); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 219 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 220 | memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int)); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 221 | free(oldPi); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 222 | #if SQLITE_MEMDEBUG>1 |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 223 | print_stack_trace(); |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 224 | 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); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 226 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 227 | return p; |
| 228 | } |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 229 | |
| 230 | /* |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 231 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 232 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 233 | char *sqlite3StrDup_(const char *z, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 234 | char *zNew; |
| 235 | if( z==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 236 | zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 237 | if( zNew ) strcpy(zNew, z); |
| 238 | return zNew; |
| 239 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 240 | char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 241 | char *zNew; |
| 242 | if( z==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 243 | zNew = sqlite3Malloc_(n+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 244 | if( zNew ){ |
| 245 | memcpy(zNew, z, n); |
| 246 | zNew[n] = 0; |
| 247 | } |
| 248 | return zNew; |
| 249 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | ** A version of sqliteFree that is always a function, not a macro. |
| 253 | */ |
| 254 | void sqlite3FreeX(void *p){ |
| 255 | sqliteFree(p); |
| 256 | } |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 257 | #endif /* SQLITE_MEMDEBUG */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 258 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 259 | /* |
| 260 | ** The following versions of malloc() and free() are for use in a |
| 261 | ** normal build. |
| 262 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 263 | #if !defined(SQLITE_MEMDEBUG) |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 264 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 265 | /* |
| 266 | ** Allocate new memory and set it to zero. Return NULL if |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 267 | ** no memory is available. See also sqliteMallocRaw(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 268 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 269 | void *sqlite3Malloc(int n){ |
drh | 2678058 | 2002-10-20 15:46:22 +0000 | [diff] [blame] | 270 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 271 | if( (p = malloc(n))==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 272 | if( n>0 ) sqlite3_malloc_failed++; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 273 | }else{ |
| 274 | memset(p, 0, n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 275 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 276 | return p; |
| 277 | } |
| 278 | |
| 279 | /* |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 280 | ** Allocate new memory but do not set it to zero. Return NULL if |
| 281 | ** no memory is available. See also sqliteMalloc(). |
| 282 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 283 | void *sqlite3MallocRaw(int n){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 284 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 285 | if( (p = malloc(n))==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 286 | if( n>0 ) sqlite3_malloc_failed++; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 287 | } |
| 288 | return p; |
| 289 | } |
| 290 | |
| 291 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | ** Free memory previously obtained from sqliteMalloc() |
| 293 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 294 | void sqlite3FreeX(void *p){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 295 | if( p ){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 296 | free(p); |
| 297 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 298 | } |
| 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 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 305 | void *sqlite3Realloc(void *p, int n){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 306 | void *p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 307 | if( p==0 ){ |
| 308 | return sqliteMalloc(n); |
| 309 | } |
| 310 | if( n==0 ){ |
| 311 | sqliteFree(p); |
| 312 | return 0; |
| 313 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 314 | p2 = realloc(p, n); |
| 315 | if( p2==0 ){ |
danielk1977 | a38432d | 2005-02-01 10:35:06 +0000 | [diff] [blame] | 316 | if( n>0 ) sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 317 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 318 | return p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 319 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 323 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 324 | char *sqlite3StrDup(const char *z){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 325 | char *zNew; |
| 326 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 327 | zNew = sqliteMallocRaw(strlen(z)+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 328 | if( zNew ) strcpy(zNew, z); |
| 329 | return zNew; |
| 330 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 331 | char *sqlite3StrNDup(const char *z, int n){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 332 | char *zNew; |
| 333 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 334 | zNew = sqliteMallocRaw(n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 335 | if( zNew ){ |
| 336 | memcpy(zNew, z, n); |
| 337 | zNew[n] = 0; |
| 338 | } |
| 339 | return zNew; |
| 340 | } |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 341 | #endif /* !defined(SQLITE_MEMDEBUG) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | |
| 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 |
jplyon | 02be20d | 2003-06-02 06:17:10 +0000 | [diff] [blame] | 347 | ** point to that string. The 1st argument must either be NULL or |
| 348 | ** point to memory obtained from sqliteMalloc(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 349 | */ |
drh | 41f5852 | 2005-06-06 15:06:39 +0000 | [diff] [blame] | 350 | void sqlite3SetString(char **pz, ...){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 351 | va_list ap; |
| 352 | int nByte; |
| 353 | const char *z; |
| 354 | char *zResult; |
| 355 | |
| 356 | if( pz==0 ) return; |
drh | 41f5852 | 2005-06-06 15:06:39 +0000 | [diff] [blame] | 357 | nByte = 1; |
| 358 | va_start(ap, pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 359 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 360 | nByte += strlen(z); |
| 361 | } |
| 362 | va_end(ap); |
| 363 | sqliteFree(*pz); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 364 | *pz = zResult = sqliteMallocRaw( nByte ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 365 | if( zResult==0 ){ |
| 366 | return; |
| 367 | } |
drh | 41f5852 | 2005-06-06 15:06:39 +0000 | [diff] [blame] | 368 | *zResult = 0; |
| 369 | va_start(ap, pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 370 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 371 | strcpy(zResult, z); |
| 372 | zResult += strlen(zResult); |
| 373 | } |
| 374 | va_end(ap); |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 375 | #ifdef SQLITE_MEMDEBUG |
| 376 | #if SQLITE_MEMDEBUG>1 |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 377 | fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz); |
| 378 | #endif |
| 379 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 383 | ** Set the most recent error code and error string for the sqlite |
| 384 | ** handle "db". The error code is set to "err_code". |
| 385 | ** |
| 386 | ** If it is not NULL, string zFormat specifies the format of the |
| 387 | ** error string in the style of the printf functions: The following |
| 388 | ** format characters are allowed: |
| 389 | ** |
| 390 | ** %s Insert a string |
| 391 | ** %z A string that should be freed after use |
| 392 | ** %d Insert an integer |
| 393 | ** %T Insert a token |
| 394 | ** %S Insert the first element of a SrcList |
| 395 | ** |
| 396 | ** zFormat and any string tokens that follow it are assumed to be |
| 397 | ** encoded in UTF-8. |
| 398 | ** |
danielk1977 | 0bb8f36 | 2005-05-23 13:00:57 +0000 | [diff] [blame] | 399 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 400 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 401 | ** to NULL. |
| 402 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 403 | void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 404 | if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){ |
| 405 | db->errCode = err_code; |
| 406 | if( zFormat ){ |
| 407 | char *z; |
| 408 | va_list ap; |
| 409 | va_start(ap, zFormat); |
| 410 | z = sqlite3VMPrintf(zFormat, ap); |
| 411 | va_end(ap); |
| 412 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX); |
| 413 | }else{ |
| 414 | sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); |
| 415 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 420 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 421 | ** The following formatting characters are allowed: |
| 422 | ** |
| 423 | ** %s Insert a string |
| 424 | ** %z A string that should be freed after use |
| 425 | ** %d Insert an integer |
| 426 | ** %T Insert a token |
| 427 | ** %S Insert the first element of a SrcList |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 428 | ** |
| 429 | ** This function should be used to report any error that occurs whilst |
| 430 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 431 | ** last thing the sqlite3_prepare() function does is copy the error |
| 432 | ** stored by this function into the database handle using sqlite3Error(). |
| 433 | ** Function sqlite3Error() should be used during statement execution |
| 434 | ** (sqlite3_step() etc.). |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 435 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 436 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 437 | va_list ap; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 438 | pParse->nErr++; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 439 | sqliteFree(pParse->zErrMsg); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 440 | va_start(ap, zFormat); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 441 | pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 442 | va_end(ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 446 | ** Convert an SQL-style quoted string into a normal string by removing |
| 447 | ** the quote characters. The conversion is done in-place. If the |
| 448 | ** input does not begin with a quote character, then this routine |
| 449 | ** is a no-op. |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 450 | ** |
| 451 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 452 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 453 | ** "a-b-c". |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 454 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 455 | void sqlite3Dequote(char *z){ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 456 | int quote; |
| 457 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 458 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 459 | quote = z[0]; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 460 | switch( quote ){ |
| 461 | case '\'': break; |
| 462 | case '"': break; |
| 463 | case '[': quote = ']'; break; |
| 464 | default: return; |
| 465 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 466 | for(i=1, j=0; z[i]; i++){ |
| 467 | if( z[i]==quote ){ |
| 468 | if( z[i+1]==quote ){ |
| 469 | z[j++] = quote; |
| 470 | i++; |
| 471 | }else{ |
| 472 | z[j++] = 0; |
| 473 | break; |
| 474 | } |
| 475 | }else{ |
| 476 | z[j++] = z[i]; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 481 | /* An array to map all upper-case characters into their corresponding |
| 482 | ** lower-case character. |
| 483 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 484 | const unsigned char sqlite3UpperToLower[] = { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 485 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 486 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 487 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 488 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, |
| 489 | 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, |
| 490 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, |
| 491 | 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 492 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, |
| 493 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, |
| 494 | 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, |
| 495 | 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, |
| 496 | 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, |
| 497 | 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, |
| 498 | 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, |
| 499 | 252,253,254,255 |
| 500 | }; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 501 | #define UpperToLower sqlite3UpperToLower |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 502 | |
| 503 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 504 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 505 | ** there is no consistency, we will define our own. |
| 506 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 507 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 508 | register unsigned char *a, *b; |
| 509 | a = (unsigned char *)zLeft; |
| 510 | b = (unsigned char *)zRight; |
| 511 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
drh | 2b73501 | 2004-07-15 13:23:21 +0000 | [diff] [blame] | 512 | return UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 514 | int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 515 | register unsigned char *a, *b; |
| 516 | a = (unsigned char *)zLeft; |
| 517 | b = (unsigned char *)zRight; |
| 518 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 519 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 520 | } |
| 521 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 522 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 523 | ** Return TRUE if z is a pure numeric string. Return FALSE if the |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 524 | ** string contains any character which is not part of a number. If |
| 525 | ** the string is numeric and contains the '.' character, set *realnum |
| 526 | ** to TRUE (otherwise FALSE). |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 527 | ** |
drh | 873cdcb | 2004-09-05 23:23:41 +0000 | [diff] [blame] | 528 | ** An empty string is considered non-numeric. |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 529 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 530 | int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 531 | int incr = (enc==SQLITE_UTF8?1:2); |
danielk1977 | 93cd039 | 2004-06-30 02:35:51 +0000 | [diff] [blame] | 532 | if( enc==SQLITE_UTF16BE ) z++; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 533 | if( *z=='-' || *z=='+' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 534 | if( !isdigit(*(u8*)z) ){ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 535 | return 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 536 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 537 | z += incr; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 538 | if( realnum ) *realnum = 0; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 539 | while( isdigit(*(u8*)z) ){ z += incr; } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 540 | if( *z=='.' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 541 | z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 542 | if( !isdigit(*(u8*)z) ) return 0; |
| 543 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 544 | if( realnum ) *realnum = 1; |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 545 | } |
| 546 | if( *z=='e' || *z=='E' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 547 | z += incr; |
| 548 | if( *z=='+' || *z=='-' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 549 | if( !isdigit(*(u8*)z) ) return 0; |
| 550 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 551 | if( realnum ) *realnum = 1; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 552 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 553 | return *z==0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 554 | } |
| 555 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 556 | /* |
| 557 | ** The string z[] is an ascii representation of a real number. |
| 558 | ** Convert this string to a double. |
| 559 | ** |
| 560 | ** This routine assumes that z[] really is a valid number. If it |
| 561 | ** is not, the result is undefined. |
| 562 | ** |
| 563 | ** This routine is used instead of the library atof() function because |
| 564 | ** the library atof() might want to use "," as the decimal point instead |
| 565 | ** of "." depending on how locale is set. But that would cause problems |
| 566 | ** for SQL. So this routine always uses "." regardless of locale. |
| 567 | */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame^] | 568 | int sqlite3AtoF(const char *z, double *pResult){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 569 | int sign = 1; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame^] | 570 | const char *zBegin = z; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 571 | LONGDOUBLE_TYPE v1 = 0.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 572 | if( *z=='-' ){ |
| 573 | sign = -1; |
| 574 | z++; |
| 575 | }else if( *z=='+' ){ |
| 576 | z++; |
| 577 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 578 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 579 | v1 = v1*10.0 + (*z - '0'); |
| 580 | z++; |
| 581 | } |
| 582 | if( *z=='.' ){ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 583 | LONGDOUBLE_TYPE divisor = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 584 | z++; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 585 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 586 | 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; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 595 | LONGDOUBLE_TYPE scale = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 596 | z++; |
| 597 | if( *z=='-' ){ |
| 598 | esign = -1; |
| 599 | z++; |
| 600 | }else if( *z=='+' ){ |
| 601 | z++; |
| 602 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 603 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 604 | 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 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame^] | 617 | *pResult = sign<0 ? -v1 : v1; |
| 618 | return z - zBegin; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 619 | } |
| 620 | |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 621 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 622 | ** 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 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 632 | int sqlite3atoi64(const char *zNum, i64 *pNum){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 633 | i64 v = 0; |
| 634 | int neg; |
| 635 | int i, c; |
| 636 | if( *zNum=='-' ){ |
| 637 | neg = 1; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 638 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 639 | }else if( *zNum=='+' ){ |
| 640 | neg = 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 641 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 642 | }else{ |
| 643 | neg = 0; |
| 644 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 645 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 646 | 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 | /* |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 654 | ** 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 |
drh | 873cdcb | 2004-09-05 23:23:41 +0000 | [diff] [blame] | 660 | ** that number will in fact fit in a 32-bit integer. But positive |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 661 | ** 2147483648 will not fit in 32 bits. So it seems safer to return |
| 662 | ** false. |
| 663 | */ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 664 | static int sqlite3FitsIn32Bits(const char *zNum){ |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 665 | 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 | |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 671 | /* |
| 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 | */ |
| 675 | int 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 | */ |
| 694 | int 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 | |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 701 | |
| 702 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 703 | ** 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 |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 711 | ** error which can be used to debug the application that is |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 712 | ** using SQLite incorrectly. |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 713 | ** |
| 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 |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 717 | ** call to sqlite3_close(db) and db has been deallocated. And we do |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 718 | ** not want to write into deallocated memory. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 719 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 720 | int sqlite3SafetyOn(sqlite3 *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 721 | if( db->magic==SQLITE_MAGIC_OPEN ){ |
| 722 | db->magic = SQLITE_MAGIC_BUSY; |
| 723 | return 0; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 724 | }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 725 | db->magic = SQLITE_MAGIC_ERROR; |
| 726 | db->flags |= SQLITE_Interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 727 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 728 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 729 | } |
| 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 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 736 | int sqlite3SafetyOff(sqlite3 *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 737 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 738 | db->magic = SQLITE_MAGIC_OPEN; |
| 739 | return 0; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 740 | }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 741 | db->magic = SQLITE_MAGIC_ERROR; |
| 742 | db->flags |= SQLITE_Interrupt; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 743 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 744 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 748 | ** 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. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 755 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 756 | int sqlite3SafetyCheck(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 757 | 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; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 763 | return 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 764 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 765 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 766 | /* |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 767 | ** 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 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 786 | ** 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 |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 792 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 793 | ** 8 bits and is the last byte. |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 794 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 795 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 796 | int i, j, n; |
| 797 | u8 buf[10]; |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 798 | if( v & (((u64)0xff000000)<<32) ){ |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 799 | 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 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 807 | n = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 808 | do{ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 809 | buf[n++] = (v & 0x7f) | 0x80; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 810 | v >>= 7; |
| 811 | }while( v!=0 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 812 | buf[0] &= 0x7f; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 813 | assert( n<=9 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 814 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 815 | p[i] = buf[j]; |
| 816 | } |
| 817 | return n; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 818 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 819 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 820 | /* |
| 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 | */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 824 | int sqlite3GetVarint(const unsigned char *p, u64 *v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 825 | u32 x; |
| 826 | u64 x64; |
| 827 | int n; |
| 828 | unsigned char c; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 829 | if( ((c = p[0]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 830 | *v = c; |
| 831 | return 1; |
| 832 | } |
| 833 | x = c & 0x7f; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 834 | if( ((c = p[1]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 835 | *v = (x<<7) | c; |
| 836 | return 2; |
| 837 | } |
| 838 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 839 | if( ((c = p[2]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 840 | *v = (x<<7) | c; |
| 841 | return 3; |
| 842 | } |
| 843 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 844 | if( ((c = p[3]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 845 | *v = (x<<7) | c; |
| 846 | return 4; |
| 847 | } |
| 848 | x64 = (x<<7) | (c&0x7f); |
| 849 | n = 4; |
| 850 | do{ |
| 851 | c = p[n++]; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 852 | if( n==9 ){ |
| 853 | x64 = (x64<<8) | c; |
| 854 | break; |
| 855 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 856 | 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 | */ |
| 866 | int sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 867 | u32 x; |
| 868 | int n; |
| 869 | unsigned char c; |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 870 | 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); |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 880 | n = 2; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 881 | do{ |
| 882 | x = (x<<7) | ((c = p[n++])&0x7f); |
| 883 | }while( (c & 0x80)!=0 && n<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 884 | *v = x; |
| 885 | return n; |
| 886 | } |
| 887 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 888 | /* |
| 889 | ** Return the number of bytes that will be needed to store the given |
| 890 | ** 64-bit integer. |
| 891 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 892 | int sqlite3VarintLen(u64 v){ |
| 893 | int i = 0; |
| 894 | do{ |
| 895 | i++; |
| 896 | v >>= 7; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 897 | }while( v!=0 && i<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 898 | return i; |
| 899 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 900 | |
drh | 33fa535 | 2005-03-10 12:35:45 +0000 | [diff] [blame] | 901 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \ |
drh | a71aa00 | 2004-11-03 13:59:04 +0000 | [diff] [blame] | 902 | || defined(SQLITE_TEST) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 903 | /* |
| 904 | ** Translate a single byte of Hex into an integer. |
| 905 | */ |
| 906 | static 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; |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 911 | }else{ |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 912 | assert( h>='A' && h<='F' ); |
| 913 | return h - 'A' + 10; |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
drh | 33fa535 | 2005-03-10 12:35:45 +0000 | [diff] [blame] | 916 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */ |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 917 | |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 918 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 919 | /* |
| 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 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 925 | void *sqlite3HexToBlob(const char *z){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 926 | char *zBlob; |
| 927 | int i; |
| 928 | int n = strlen(z); |
| 929 | if( n%2 ) return 0; |
| 930 | |
| 931 | zBlob = (char *)sqliteMalloc(n/2); |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 932 | for(i=0; i<n; i+=2){ |
| 933 | zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 934 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 935 | return zBlob; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 936 | } |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 937 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 938 | |
| 939 | #if defined(SQLITE_TEST) |
| 940 | /* |
| 941 | ** Convert text generated by the "%p" conversion format back into |
| 942 | ** a pointer. |
| 943 | */ |
| 944 | void *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 |