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