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 | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.128 2005/01/20 22:48:48 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 | /* |
| 68 | ** Number of 32-bit guard words |
| 69 | */ |
| 70 | #define N_GUARD 1 |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 73 | ** Allocate new memory and set it to zero. Return NULL if |
| 74 | ** no memory is available. |
| 75 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 76 | void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 77 | void *p; |
| 78 | int *pi; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 79 | int i, k; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 80 | if( sqlite3_iMallocFail>=0 ){ |
| 81 | sqlite3_iMallocFail--; |
| 82 | if( sqlite3_iMallocFail==0 ){ |
| 83 | sqlite3_malloc_failed++; |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 84 | #if SQLITE_MEMDEBUG>1 |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 85 | fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n", |
| 86 | n, zFile,line); |
| 87 | #endif |
drh | 4693423 | 2004-11-20 19:18:00 +0000 | [diff] [blame] | 88 | sqlite3_iMallocFail = sqlite3_iMallocReset; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 91 | } |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 92 | if( n==0 ) return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 93 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 94 | pi = malloc( (N_GUARD*2+1+k)*sizeof(int)); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 95 | if( pi==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 96 | sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 99 | sqlite3_nMalloc++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 100 | for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122; |
| 101 | pi[N_GUARD] = n; |
| 102 | for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344; |
| 103 | p = &pi[N_GUARD+1]; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 104 | memset(p, bZero==0, n); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 105 | #if SQLITE_MEMDEBUG>1 |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 106 | print_stack_trace(); |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 107 | fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n", |
| 108 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 109 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 110 | return p; |
| 111 | } |
| 112 | |
| 113 | /* |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 114 | ** Check to see if the given pointer was obtained from sqliteMalloc() |
| 115 | ** and is able to hold at least N bytes. Raise an exception if this |
| 116 | ** is not the case. |
| 117 | ** |
| 118 | ** This routine is used for testing purposes only. |
| 119 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 120 | void sqlite3CheckMemory(void *p, int N){ |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 121 | int *pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 122 | int n, i, k; |
| 123 | pi -= N_GUARD+1; |
| 124 | for(i=0; i<N_GUARD; i++){ |
| 125 | assert( pi[i]==0xdead1122 ); |
| 126 | } |
| 127 | n = pi[N_GUARD]; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 128 | assert( N>=0 && N<n ); |
| 129 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 130 | for(i=0; i<N_GUARD; i++){ |
| 131 | assert( pi[k+N_GUARD+1+i]==0xdead3344 ); |
| 132 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 136 | ** Free memory previously obtained from sqliteMalloc() |
| 137 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 138 | void sqlite3Free_(void *p, char *zFile, int line){ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 139 | if( p ){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 140 | int *pi, i, k, n; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 141 | pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 142 | pi -= N_GUARD+1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 143 | sqlite3_nFree++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 144 | for(i=0; i<N_GUARD; i++){ |
| 145 | if( pi[i]!=0xdead1122 ){ |
| 146 | fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p); |
| 147 | return; |
| 148 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 149 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 150 | n = pi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 151 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 152 | for(i=0; i<N_GUARD; i++){ |
| 153 | if( pi[k+N_GUARD+1+i]!=0xdead3344 ){ |
| 154 | fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p); |
| 155 | return; |
| 156 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 157 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 158 | memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int)); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 159 | #if SQLITE_MEMDEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 160 | fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n", |
| 161 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 162 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 163 | free(pi); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | ** Resize a prior allocation. If p==0, then this routine |
| 169 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 170 | ** works just like sqliteFree(). |
| 171 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 172 | void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 173 | int *oldPi, *pi, i, k, oldN, oldK; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 174 | void *p; |
| 175 | if( oldP==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 176 | return sqlite3Malloc_(n,1,zFile,line); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 177 | } |
| 178 | if( n==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 179 | sqlite3Free_(oldP,zFile,line); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | oldPi = oldP; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 183 | oldPi -= N_GUARD+1; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 184 | if( oldPi[0]!=0xdead1122 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 185 | 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] | 186 | return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 187 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 188 | oldN = oldPi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 189 | oldK = (oldN+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 190 | for(i=0; i<N_GUARD; i++){ |
| 191 | if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 192 | fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", |
| 193 | (int)oldP); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 196 | } |
| 197 | k = (n + sizeof(int) - 1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 198 | pi = malloc( (k+N_GUARD*2+1)*sizeof(int) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 199 | if( pi==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 200 | sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 203 | for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122; |
| 204 | pi[N_GUARD] = n; |
| 205 | for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344; |
| 206 | p = &pi[N_GUARD+1]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 207 | memcpy(p, oldP, n>oldN ? oldN : n); |
| 208 | if( n>oldN ){ |
danielk1977 | 25b3363 | 2004-06-30 12:49:46 +0000 | [diff] [blame] | 209 | memset(&((char*)p)[oldN], 0x55, n-oldN); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 210 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 211 | memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int)); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 212 | free(oldPi); |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 213 | #if SQLITE_MEMDEBUG>1 |
danielk1977 | eac7a36 | 2004-06-16 07:45:24 +0000 | [diff] [blame] | 214 | print_stack_trace(); |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 215 | fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n", |
| 216 | ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 217 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 218 | return p; |
| 219 | } |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 220 | |
| 221 | /* |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 222 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 223 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 224 | char *sqlite3StrDup_(const char *z, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 225 | char *zNew; |
| 226 | if( z==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 227 | zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 228 | if( zNew ) strcpy(zNew, z); |
| 229 | return zNew; |
| 230 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 231 | char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 232 | char *zNew; |
| 233 | if( z==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 234 | zNew = sqlite3Malloc_(n+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 235 | if( zNew ){ |
| 236 | memcpy(zNew, z, n); |
| 237 | zNew[n] = 0; |
| 238 | } |
| 239 | return zNew; |
| 240 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | ** A version of sqliteFree that is always a function, not a macro. |
| 244 | */ |
| 245 | void sqlite3FreeX(void *p){ |
| 246 | sqliteFree(p); |
| 247 | } |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 248 | #endif /* SQLITE_MEMDEBUG */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 249 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 250 | /* |
| 251 | ** The following versions of malloc() and free() are for use in a |
| 252 | ** normal build. |
| 253 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 254 | #if !defined(SQLITE_MEMDEBUG) |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 255 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 256 | /* |
| 257 | ** Allocate new memory and set it to zero. Return NULL if |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 258 | ** no memory is available. See also sqliteMallocRaw(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 260 | void *sqlite3Malloc(int n){ |
drh | 2678058 | 2002-10-20 15:46:22 +0000 | [diff] [blame] | 261 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 262 | if( (p = malloc(n))==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 263 | if( n>0 ) sqlite3_malloc_failed++; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 264 | }else{ |
| 265 | memset(p, 0, n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 266 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 267 | return p; |
| 268 | } |
| 269 | |
| 270 | /* |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 271 | ** Allocate new memory but do not set it to zero. Return NULL if |
| 272 | ** no memory is available. See also sqliteMalloc(). |
| 273 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 274 | void *sqlite3MallocRaw(int n){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 275 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame] | 276 | if( (p = malloc(n))==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 277 | if( n>0 ) sqlite3_malloc_failed++; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 278 | } |
| 279 | return p; |
| 280 | } |
| 281 | |
| 282 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 283 | ** Free memory previously obtained from sqliteMalloc() |
| 284 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 285 | void sqlite3FreeX(void *p){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 286 | if( p ){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 287 | free(p); |
| 288 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | ** Resize a prior allocation. If p==0, then this routine |
| 293 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 294 | ** works just like sqliteFree(). |
| 295 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 296 | void *sqlite3Realloc(void *p, int n){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 297 | void *p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 298 | if( p==0 ){ |
| 299 | return sqliteMalloc(n); |
| 300 | } |
| 301 | if( n==0 ){ |
| 302 | sqliteFree(p); |
| 303 | return 0; |
| 304 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 305 | p2 = realloc(p, n); |
| 306 | if( p2==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 307 | sqlite3_malloc_failed++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 308 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 309 | return p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 310 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 314 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 315 | char *sqlite3StrDup(const char *z){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 316 | char *zNew; |
| 317 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 318 | zNew = sqliteMallocRaw(strlen(z)+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 319 | if( zNew ) strcpy(zNew, z); |
| 320 | return zNew; |
| 321 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 322 | char *sqlite3StrNDup(const char *z, int n){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 323 | char *zNew; |
| 324 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 325 | zNew = sqliteMallocRaw(n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 326 | if( zNew ){ |
| 327 | memcpy(zNew, z, n); |
| 328 | zNew[n] = 0; |
| 329 | } |
| 330 | return zNew; |
| 331 | } |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 332 | #endif /* !defined(SQLITE_MEMDEBUG) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 336 | ** first NULL argument), store the string in memory obtained from |
| 337 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
jplyon | 02be20d | 2003-06-02 06:17:10 +0000 | [diff] [blame] | 338 | ** point to that string. The 1st argument must either be NULL or |
| 339 | ** point to memory obtained from sqliteMalloc(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 341 | void sqlite3SetString(char **pz, const char *zFirst, ...){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | va_list ap; |
| 343 | int nByte; |
| 344 | const char *z; |
| 345 | char *zResult; |
| 346 | |
| 347 | if( pz==0 ) return; |
| 348 | nByte = strlen(zFirst) + 1; |
| 349 | va_start(ap, zFirst); |
| 350 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 351 | nByte += strlen(z); |
| 352 | } |
| 353 | va_end(ap); |
| 354 | sqliteFree(*pz); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 355 | *pz = zResult = sqliteMallocRaw( nByte ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 356 | if( zResult==0 ){ |
| 357 | return; |
| 358 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 359 | strcpy(zResult, zFirst); |
| 360 | zResult += strlen(zResult); |
| 361 | va_start(ap, zFirst); |
| 362 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 363 | strcpy(zResult, z); |
| 364 | zResult += strlen(zResult); |
| 365 | } |
| 366 | va_end(ap); |
drh | faa57ac | 2004-06-09 14:01:51 +0000 | [diff] [blame] | 367 | #ifdef SQLITE_DEBUG |
| 368 | #if SQLITE_DEBUG>1 |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 369 | fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz); |
| 370 | #endif |
| 371 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 375 | ** Set the most recent error code and error string for the sqlite |
| 376 | ** handle "db". The error code is set to "err_code". |
| 377 | ** |
| 378 | ** If it is not NULL, string zFormat specifies the format of the |
| 379 | ** error string in the style of the printf functions: The following |
| 380 | ** format characters are allowed: |
| 381 | ** |
| 382 | ** %s Insert a string |
| 383 | ** %z A string that should be freed after use |
| 384 | ** %d Insert an integer |
| 385 | ** %T Insert a token |
| 386 | ** %S Insert the first element of a SrcList |
| 387 | ** |
| 388 | ** zFormat and any string tokens that follow it are assumed to be |
| 389 | ** encoded in UTF-8. |
| 390 | ** |
| 391 | ** To clear the most recent error for slqite handle "db", sqlite3Error |
| 392 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 393 | ** to NULL. |
| 394 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 395 | void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 396 | if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){ |
| 397 | db->errCode = err_code; |
| 398 | if( zFormat ){ |
| 399 | char *z; |
| 400 | va_list ap; |
| 401 | va_start(ap, zFormat); |
| 402 | z = sqlite3VMPrintf(zFormat, ap); |
| 403 | va_end(ap); |
| 404 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX); |
| 405 | }else{ |
| 406 | sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); |
| 407 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | /* |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 412 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 413 | ** The following formatting characters are allowed: |
| 414 | ** |
| 415 | ** %s Insert a string |
| 416 | ** %z A string that should be freed after use |
| 417 | ** %d Insert an integer |
| 418 | ** %T Insert a token |
| 419 | ** %S Insert the first element of a SrcList |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 420 | ** |
| 421 | ** This function should be used to report any error that occurs whilst |
| 422 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 423 | ** last thing the sqlite3_prepare() function does is copy the error |
| 424 | ** stored by this function into the database handle using sqlite3Error(). |
| 425 | ** Function sqlite3Error() should be used during statement execution |
| 426 | ** (sqlite3_step() etc.). |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 427 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 428 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 429 | va_list ap; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 430 | pParse->nErr++; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 431 | sqliteFree(pParse->zErrMsg); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 432 | va_start(ap, zFormat); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 433 | pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 434 | va_end(ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 438 | ** Convert an SQL-style quoted string into a normal string by removing |
| 439 | ** the quote characters. The conversion is done in-place. If the |
| 440 | ** input does not begin with a quote character, then this routine |
| 441 | ** is a no-op. |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 442 | ** |
| 443 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 444 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 445 | ** "a-b-c". |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 446 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 447 | void sqlite3Dequote(char *z){ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 448 | int quote; |
| 449 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 450 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 451 | quote = z[0]; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 452 | switch( quote ){ |
| 453 | case '\'': break; |
| 454 | case '"': break; |
| 455 | case '[': quote = ']'; break; |
| 456 | default: return; |
| 457 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 458 | for(i=1, j=0; z[i]; i++){ |
| 459 | if( z[i]==quote ){ |
| 460 | if( z[i+1]==quote ){ |
| 461 | z[j++] = quote; |
| 462 | i++; |
| 463 | }else{ |
| 464 | z[j++] = 0; |
| 465 | break; |
| 466 | } |
| 467 | }else{ |
| 468 | z[j++] = z[i]; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 473 | /* An array to map all upper-case characters into their corresponding |
| 474 | ** lower-case character. |
| 475 | */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 476 | const unsigned char sqlite3UpperToLower[] = { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 477 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 478 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 479 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 480 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, |
| 481 | 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, |
| 482 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, |
| 483 | 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 484 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, |
| 485 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, |
| 486 | 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, |
| 487 | 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, |
| 488 | 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, |
| 489 | 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, |
| 490 | 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, |
| 491 | 252,253,254,255 |
| 492 | }; |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 493 | #define UpperToLower sqlite3UpperToLower |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | ** This function computes a hash on the name of a keyword. |
| 497 | ** Case is not significant. |
| 498 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 499 | int sqlite3HashNoCase(const char *z, int n){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | int h = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 501 | if( n<=0 ) n = strlen(z); |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 502 | while( n > 0 ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 503 | h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++]; |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 504 | n--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 505 | } |
drh | 5364f60 | 2003-05-12 23:06:52 +0000 | [diff] [blame] | 506 | return h & 0x7fffffff; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 510 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | ** there is no consistency, we will define our own. |
| 512 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 513 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 514 | register unsigned char *a, *b; |
| 515 | a = (unsigned char *)zLeft; |
| 516 | b = (unsigned char *)zRight; |
| 517 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
drh | 2b73501 | 2004-07-15 13:23:21 +0000 | [diff] [blame] | 518 | return UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 519 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 520 | int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 521 | register unsigned char *a, *b; |
| 522 | a = (unsigned char *)zLeft; |
| 523 | b = (unsigned char *)zRight; |
| 524 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 525 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 526 | } |
| 527 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 528 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 529 | ** Return TRUE if z is a pure numeric string. Return FALSE if the |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 530 | ** string contains any character which is not part of a number. If |
| 531 | ** the string is numeric and contains the '.' character, set *realnum |
| 532 | ** to TRUE (otherwise FALSE). |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 533 | ** |
drh | 873cdcb | 2004-09-05 23:23:41 +0000 | [diff] [blame] | 534 | ** An empty string is considered non-numeric. |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 535 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 536 | int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 537 | int incr = (enc==SQLITE_UTF8?1:2); |
danielk1977 | 93cd039 | 2004-06-30 02:35:51 +0000 | [diff] [blame] | 538 | if( enc==SQLITE_UTF16BE ) z++; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 539 | if( *z=='-' || *z=='+' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 540 | if( !isdigit(*(u8*)z) ){ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 541 | return 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 542 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 543 | z += incr; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 544 | if( realnum ) *realnum = 0; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 545 | while( isdigit(*(u8*)z) ){ z += incr; } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 546 | if( *z=='.' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 547 | z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 548 | if( !isdigit(*(u8*)z) ) return 0; |
| 549 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 550 | if( realnum ) *realnum = 1; |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 551 | } |
| 552 | if( *z=='e' || *z=='E' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 553 | z += incr; |
| 554 | if( *z=='+' || *z=='-' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 555 | if( !isdigit(*(u8*)z) ) return 0; |
| 556 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 557 | if( realnum ) *realnum = 1; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 558 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 559 | return *z==0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 560 | } |
| 561 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 562 | /* |
| 563 | ** The string z[] is an ascii representation of a real number. |
| 564 | ** Convert this string to a double. |
| 565 | ** |
| 566 | ** This routine assumes that z[] really is a valid number. If it |
| 567 | ** is not, the result is undefined. |
| 568 | ** |
| 569 | ** This routine is used instead of the library atof() function because |
| 570 | ** the library atof() might want to use "," as the decimal point instead |
| 571 | ** of "." depending on how locale is set. But that would cause problems |
| 572 | ** for SQL. So this routine always uses "." regardless of locale. |
| 573 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 574 | double sqlite3AtoF(const char *z, const char **pzEnd){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 575 | int sign = 1; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 576 | LONGDOUBLE_TYPE v1 = 0.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 577 | if( *z=='-' ){ |
| 578 | sign = -1; |
| 579 | z++; |
| 580 | }else if( *z=='+' ){ |
| 581 | z++; |
| 582 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 583 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 584 | v1 = v1*10.0 + (*z - '0'); |
| 585 | z++; |
| 586 | } |
| 587 | if( *z=='.' ){ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 588 | LONGDOUBLE_TYPE divisor = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 589 | z++; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 590 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 591 | v1 = v1*10.0 + (*z - '0'); |
| 592 | divisor *= 10.0; |
| 593 | z++; |
| 594 | } |
| 595 | v1 /= divisor; |
| 596 | } |
| 597 | if( *z=='e' || *z=='E' ){ |
| 598 | int esign = 1; |
| 599 | int eval = 0; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 600 | LONGDOUBLE_TYPE scale = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 601 | z++; |
| 602 | if( *z=='-' ){ |
| 603 | esign = -1; |
| 604 | z++; |
| 605 | }else if( *z=='+' ){ |
| 606 | z++; |
| 607 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 608 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 609 | eval = eval*10 + *z - '0'; |
| 610 | z++; |
| 611 | } |
| 612 | while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } |
| 613 | while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } |
| 614 | while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } |
| 615 | while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } |
| 616 | if( esign<0 ){ |
| 617 | v1 /= scale; |
| 618 | }else{ |
| 619 | v1 *= scale; |
| 620 | } |
| 621 | } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 622 | if( pzEnd ) *pzEnd = z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 623 | return sign<0 ? -v1 : v1; |
| 624 | } |
| 625 | |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 626 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 627 | ** Return TRUE if zNum is a 64-bit signed integer and write |
| 628 | ** the value of the integer into *pNum. If zNum is not an integer |
| 629 | ** or is an integer that is too large to be expressed with 64 bits, |
| 630 | ** then return false. If n>0 and the integer is string is not |
| 631 | ** exactly n bytes long, return false. |
| 632 | ** |
| 633 | ** When this routine was originally written it dealt with only |
| 634 | ** 32-bit numbers. At that time, it was much faster than the |
| 635 | ** atoi() library routine in RedHat 7.2. |
| 636 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 637 | int sqlite3atoi64(const char *zNum, i64 *pNum){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 638 | i64 v = 0; |
| 639 | int neg; |
| 640 | int i, c; |
| 641 | if( *zNum=='-' ){ |
| 642 | neg = 1; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 643 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 644 | }else if( *zNum=='+' ){ |
| 645 | neg = 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 646 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 647 | }else{ |
| 648 | neg = 0; |
| 649 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 650 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 651 | v = v*10 + c - '0'; |
| 652 | } |
| 653 | *pNum = neg ? -v : v; |
| 654 | return c==0 && i>0 && |
| 655 | (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0)); |
| 656 | } |
| 657 | |
| 658 | /* |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 659 | ** The string zNum represents an integer. There might be some other |
| 660 | ** information following the integer too, but that part is ignored. |
| 661 | ** If the integer that the prefix of zNum represents will fit in a |
| 662 | ** 32-bit signed integer, return TRUE. Otherwise return FALSE. |
| 663 | ** |
| 664 | ** This routine returns FALSE for the string -2147483648 even that |
drh | 873cdcb | 2004-09-05 23:23:41 +0000 | [diff] [blame] | 665 | ** that number will in fact fit in a 32-bit integer. But positive |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 666 | ** 2147483648 will not fit in 32 bits. So it seems safer to return |
| 667 | ** false. |
| 668 | */ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 669 | static int sqlite3FitsIn32Bits(const char *zNum){ |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 670 | int i, c; |
| 671 | if( *zNum=='-' || *zNum=='+' ) zNum++; |
| 672 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} |
| 673 | return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0); |
| 674 | } |
| 675 | |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 676 | /* |
| 677 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 678 | ** *pValue to that integer and return true. Otherwise return false. |
| 679 | */ |
| 680 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 681 | if( sqlite3FitsIn32Bits(zNum) ){ |
| 682 | *pValue = atoi(zNum); |
| 683 | return 1; |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | ** The string zNum represents an integer. There might be some other |
| 690 | ** information following the integer too, but that part is ignored. |
| 691 | ** If the integer that the prefix of zNum represents will fit in a |
| 692 | ** 64-bit signed integer, return TRUE. Otherwise return FALSE. |
| 693 | ** |
| 694 | ** This routine returns FALSE for the string -9223372036854775808 even that |
| 695 | ** that number will, in theory fit in a 64-bit integer. Positive |
| 696 | ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return |
| 697 | ** false. |
| 698 | */ |
| 699 | int sqlite3FitsIn64Bits(const char *zNum){ |
| 700 | int i, c; |
| 701 | if( *zNum=='-' || *zNum=='+' ) zNum++; |
| 702 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} |
| 703 | return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0); |
| 704 | } |
| 705 | |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 706 | |
| 707 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 708 | ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. |
| 709 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN |
| 710 | ** when this routine is called. |
| 711 | ** |
| 712 | ** This routine is a attempt to detect if two threads use the |
| 713 | ** same sqlite* pointer at the same time. There is a race |
| 714 | ** condition so it is possible that the error is not detected. |
| 715 | ** But usually the problem will be seen. The result will be an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 716 | ** error which can be used to debug the application that is |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 717 | ** using SQLite incorrectly. |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 718 | ** |
| 719 | ** Ticket #202: If db->magic is not a valid open value, take care not |
| 720 | ** to modify the db structure at all. It could be that db is a stale |
| 721 | ** pointer. In other words, it could be that there has been a prior |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 722 | ** call to sqlite3_close(db) and db has been deallocated. And we do |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 723 | ** not want to write into deallocated memory. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 724 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 725 | int sqlite3SafetyOn(sqlite3 *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 726 | if( db->magic==SQLITE_MAGIC_OPEN ){ |
| 727 | db->magic = SQLITE_MAGIC_BUSY; |
| 728 | return 0; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 729 | }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 730 | db->magic = SQLITE_MAGIC_ERROR; |
| 731 | db->flags |= SQLITE_Interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 732 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 733 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
| 737 | ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. |
| 738 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY |
| 739 | ** when this routine is called. |
| 740 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 741 | int sqlite3SafetyOff(sqlite3 *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 742 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 743 | db->magic = SQLITE_MAGIC_OPEN; |
| 744 | return 0; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 745 | }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 746 | db->magic = SQLITE_MAGIC_ERROR; |
| 747 | db->flags |= SQLITE_Interrupt; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 748 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 749 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 753 | ** Check to make sure we have a valid db pointer. This test is not |
| 754 | ** foolproof but it does provide some measure of protection against |
| 755 | ** misuse of the interface such as passing in db pointers that are |
| 756 | ** NULL or which have been previously closed. If this routine returns |
| 757 | ** TRUE it means that the db pointer is invalid and should not be |
| 758 | ** dereferenced for any reason. The calling function should invoke |
| 759 | ** SQLITE_MISUSE immediately. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 760 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 761 | int sqlite3SafetyCheck(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 762 | int magic; |
| 763 | if( db==0 ) return 1; |
| 764 | magic = db->magic; |
| 765 | if( magic!=SQLITE_MAGIC_CLOSED && |
| 766 | magic!=SQLITE_MAGIC_OPEN && |
| 767 | magic!=SQLITE_MAGIC_BUSY ) return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 768 | return 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 769 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 770 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 771 | /* |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 772 | ** The variable-length integer encoding is as follows: |
| 773 | ** |
| 774 | ** KEY: |
| 775 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 776 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 777 | ** C = xxxxxxxx 8 bits of data |
| 778 | ** |
| 779 | ** 7 bits - A |
| 780 | ** 14 bits - BA |
| 781 | ** 21 bits - BBA |
| 782 | ** 28 bits - BBBA |
| 783 | ** 35 bits - BBBBA |
| 784 | ** 42 bits - BBBBBA |
| 785 | ** 49 bits - BBBBBBA |
| 786 | ** 56 bits - BBBBBBBA |
| 787 | ** 64 bits - BBBBBBBBC |
| 788 | */ |
| 789 | |
| 790 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 791 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 792 | ** The length of data write will be between 1 and 9 bytes. The number |
| 793 | ** of bytes written is returned. |
| 794 | ** |
| 795 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 796 | ** for all bytes that have the 8th bit set and one byte with the 8th |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 797 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 798 | ** 8 bits and is the last byte. |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 799 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 800 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 801 | int i, j, n; |
| 802 | u8 buf[10]; |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame^] | 803 | if( v & (((u64)0xff000000)<<32) ){ |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 804 | p[8] = v; |
| 805 | v >>= 8; |
| 806 | for(i=7; i>=0; i--){ |
| 807 | p[i] = (v & 0x7f) | 0x80; |
| 808 | v >>= 7; |
| 809 | } |
| 810 | return 9; |
| 811 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 812 | n = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 813 | do{ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 814 | buf[n++] = (v & 0x7f) | 0x80; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 815 | v >>= 7; |
| 816 | }while( v!=0 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 817 | buf[0] &= 0x7f; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 818 | assert( n<=9 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 819 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 820 | p[i] = buf[j]; |
| 821 | } |
| 822 | return n; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 823 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 824 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 825 | /* |
| 826 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 827 | ** Return the number of bytes read. The value is stored in *v. |
| 828 | */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 829 | int sqlite3GetVarint(const unsigned char *p, u64 *v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 830 | u32 x; |
| 831 | u64 x64; |
| 832 | int n; |
| 833 | unsigned char c; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 834 | if( ((c = p[0]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 835 | *v = c; |
| 836 | return 1; |
| 837 | } |
| 838 | x = c & 0x7f; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 839 | if( ((c = p[1]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 840 | *v = (x<<7) | c; |
| 841 | return 2; |
| 842 | } |
| 843 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 844 | if( ((c = p[2]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 845 | *v = (x<<7) | c; |
| 846 | return 3; |
| 847 | } |
| 848 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 849 | if( ((c = p[3]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 850 | *v = (x<<7) | c; |
| 851 | return 4; |
| 852 | } |
| 853 | x64 = (x<<7) | (c&0x7f); |
| 854 | n = 4; |
| 855 | do{ |
| 856 | c = p[n++]; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 857 | if( n==9 ){ |
| 858 | x64 = (x64<<8) | c; |
| 859 | break; |
| 860 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 861 | x64 = (x64<<7) | (c&0x7f); |
| 862 | }while( (c & 0x80)!=0 ); |
| 863 | *v = x64; |
| 864 | return n; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 869 | ** Return the number of bytes read. The value is stored in *v. |
| 870 | */ |
| 871 | int sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 872 | u32 x; |
| 873 | int n; |
| 874 | unsigned char c; |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 875 | #if 0 |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 876 | if( ((c = p[0]) & 0x80)==0 ){ |
| 877 | *v = c; |
| 878 | return 1; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 879 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 880 | x = c & 0x7f; |
| 881 | if( ((c = p[1]) & 0x80)==0 ){ |
| 882 | *v = (x<<7) | c; |
| 883 | return 2; |
| 884 | } |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 885 | x = (x<<7) | (c & 0x7f); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 886 | #else |
| 887 | if( ((signed char*)p)[0]>=0 ){ |
| 888 | *v = p[0]; |
| 889 | return 1; |
| 890 | } |
| 891 | x = p[0] & 0x7f; |
| 892 | if( ((signed char*)p)[1]>=0 ){ |
| 893 | *v = (x<<7) | p[1]; |
| 894 | return 2; |
| 895 | } |
| 896 | x = (x<<7) | (p[1] & 0x7f); |
| 897 | #endif |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 898 | n = 2; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 899 | do{ |
| 900 | x = (x<<7) | ((c = p[n++])&0x7f); |
| 901 | }while( (c & 0x80)!=0 && n<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 902 | *v = x; |
| 903 | return n; |
| 904 | } |
| 905 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 906 | /* |
| 907 | ** Return the number of bytes that will be needed to store the given |
| 908 | ** 64-bit integer. |
| 909 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 910 | int sqlite3VarintLen(u64 v){ |
| 911 | int i = 0; |
| 912 | do{ |
| 913 | i++; |
| 914 | v >>= 7; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 915 | }while( v!=0 && i<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 916 | return i; |
| 917 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 918 | |
drh | a71aa00 | 2004-11-03 13:59:04 +0000 | [diff] [blame] | 919 | #if (!defined(SQLITE_OMIT_BLOB_LITERAL) && !defined(SQLITE_HAS_CODEC)) \ |
| 920 | || defined(SQLITE_TEST) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 921 | /* |
| 922 | ** Translate a single byte of Hex into an integer. |
| 923 | */ |
| 924 | static int hexToInt(int h){ |
| 925 | if( h>='0' && h<='9' ){ |
| 926 | return h - '0'; |
| 927 | }else if( h>='a' && h<='f' ){ |
| 928 | return h - 'a' + 10; |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 929 | }else{ |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 930 | assert( h>='A' && h<='F' ); |
| 931 | return h - 'A' + 10; |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
drh | a71aa00 | 2004-11-03 13:59:04 +0000 | [diff] [blame] | 934 | #endif /* (!SQLITE_OMIT_BLOB_LITERAL && !SQLITE_HAS_CODEC) || SQLITE_TEST */ |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 935 | |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 936 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 937 | /* |
| 938 | ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 939 | ** value. Return a pointer to its binary value. Space to hold the |
| 940 | ** binary value has been obtained from malloc and must be freed by |
| 941 | ** the calling routine. |
| 942 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 943 | void *sqlite3HexToBlob(const char *z){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 944 | char *zBlob; |
| 945 | int i; |
| 946 | int n = strlen(z); |
| 947 | if( n%2 ) return 0; |
| 948 | |
| 949 | zBlob = (char *)sqliteMalloc(n/2); |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 950 | for(i=0; i<n; i+=2){ |
| 951 | zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 952 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 953 | return zBlob; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 954 | } |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 955 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 956 | |
| 957 | #if defined(SQLITE_TEST) |
| 958 | /* |
| 959 | ** Convert text generated by the "%p" conversion format back into |
| 960 | ** a pointer. |
| 961 | */ |
| 962 | void *sqlite3TextToPtr(const char *z){ |
| 963 | void *p; |
| 964 | u64 v; |
| 965 | u32 v2; |
| 966 | if( z[0]=='0' && z[1]=='x' ){ |
| 967 | z += 2; |
| 968 | } |
| 969 | v = 0; |
| 970 | while( *z ){ |
| 971 | v = (v<<4) + hexToInt(*z); |
| 972 | z++; |
| 973 | } |
| 974 | if( sizeof(p)==sizeof(v) ){ |
| 975 | p = *(void**)&v; |
| 976 | }else{ |
| 977 | assert( sizeof(p)==sizeof(v2) ); |
| 978 | v2 = (u32)v; |
| 979 | p = *(void**)&v2; |
| 980 | } |
| 981 | return p; |
| 982 | } |
| 983 | #endif |