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 | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.68 2003/10/22 22:15:28 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 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 23 | /* |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 24 | ** If malloc() ever fails, this global variable gets set to 1. |
| 25 | ** This causes the library to abort and never again function. |
| 26 | */ |
| 27 | int sqlite_malloc_failed = 0; |
| 28 | |
| 29 | /* |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 30 | ** If MEMORY_DEBUG is defined, then use versions of malloc() and |
| 31 | ** free() that track memory usage and check for buffer overruns. |
| 32 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 33 | #ifdef MEMORY_DEBUG |
| 34 | |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 35 | /* |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 36 | ** For keeping track of the number of mallocs and frees. This |
| 37 | ** is used to check for memory leaks. |
| 38 | */ |
| 39 | int sqlite_nMalloc; /* Number of sqliteMalloc() calls */ |
| 40 | int sqlite_nFree; /* Number of sqliteFree() calls */ |
| 41 | int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */ |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 42 | #if MEMORY_DEBUG>1 |
| 43 | static int memcnt = 0; |
| 44 | #endif |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 45 | |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 46 | /* |
| 47 | ** Number of 32-bit guard words |
| 48 | */ |
| 49 | #define N_GUARD 1 |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 52 | ** Allocate new memory and set it to zero. Return NULL if |
| 53 | ** no memory is available. |
| 54 | */ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 55 | void *sqliteMalloc_(int n, int bZero, char *zFile, int line){ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 56 | void *p; |
| 57 | int *pi; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 58 | int i, k; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 59 | if( sqlite_iMallocFail>=0 ){ |
| 60 | sqlite_iMallocFail--; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 61 | if( sqlite_iMallocFail==0 ){ |
| 62 | sqlite_malloc_failed++; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 63 | #if MEMORY_DEBUG>1 |
| 64 | fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n", |
| 65 | n, zFile,line); |
| 66 | #endif |
| 67 | sqlite_iMallocFail--; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 70 | } |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 71 | if( n==0 ) return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 72 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 73 | pi = malloc( (N_GUARD*2+1+k)*sizeof(int)); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 74 | if( pi==0 ){ |
| 75 | sqlite_malloc_failed++; |
| 76 | return 0; |
| 77 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 78 | sqlite_nMalloc++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 79 | for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122; |
| 80 | pi[N_GUARD] = n; |
| 81 | for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344; |
| 82 | p = &pi[N_GUARD+1]; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 83 | memset(p, bZero==0, n); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 84 | #if MEMORY_DEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 85 | fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n", |
| 86 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 87 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 88 | return p; |
| 89 | } |
| 90 | |
| 91 | /* |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 92 | ** Check to see if the given pointer was obtained from sqliteMalloc() |
| 93 | ** and is able to hold at least N bytes. Raise an exception if this |
| 94 | ** is not the case. |
| 95 | ** |
| 96 | ** This routine is used for testing purposes only. |
| 97 | */ |
| 98 | void sqliteCheckMemory(void *p, int N){ |
| 99 | int *pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 100 | int n, i, k; |
| 101 | pi -= N_GUARD+1; |
| 102 | for(i=0; i<N_GUARD; i++){ |
| 103 | assert( pi[i]==0xdead1122 ); |
| 104 | } |
| 105 | n = pi[N_GUARD]; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 106 | assert( N>=0 && N<n ); |
| 107 | k = (n+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 108 | for(i=0; i<N_GUARD; i++){ |
| 109 | assert( pi[k+N_GUARD+1+i]==0xdead3344 ); |
| 110 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 114 | ** Free memory previously obtained from sqliteMalloc() |
| 115 | */ |
| 116 | void sqliteFree_(void *p, char *zFile, int line){ |
| 117 | if( p ){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 118 | int *pi, i, k, n; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 119 | pi = p; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 120 | pi -= N_GUARD+1; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 121 | sqlite_nFree++; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 122 | for(i=0; i<N_GUARD; i++){ |
| 123 | if( pi[i]!=0xdead1122 ){ |
| 124 | fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p); |
| 125 | return; |
| 126 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 127 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 128 | n = pi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 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 | if( pi[k+N_GUARD+1+i]!=0xdead3344 ){ |
| 132 | fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p); |
| 133 | return; |
| 134 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 135 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 136 | memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int)); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 137 | #if MEMORY_DEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 138 | fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n", |
| 139 | ++memcnt, n, (int)p, zFile,line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 140 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 141 | free(pi); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | ** Resize a prior allocation. If p==0, then this routine |
| 147 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 148 | ** works just like sqliteFree(). |
| 149 | */ |
| 150 | void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){ |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 151 | int *oldPi, *pi, i, k, oldN, oldK; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 152 | void *p; |
| 153 | if( oldP==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 154 | return sqliteMalloc_(n,1,zFile,line); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 155 | } |
| 156 | if( n==0 ){ |
| 157 | sqliteFree_(oldP,zFile,line); |
| 158 | return 0; |
| 159 | } |
| 160 | oldPi = oldP; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 161 | oldPi -= N_GUARD+1; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 162 | if( oldPi[0]!=0xdead1122 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 163 | 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] | 164 | return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 165 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 166 | oldN = oldPi[N_GUARD]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 167 | oldK = (oldN+sizeof(int)-1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 168 | for(i=0; i<N_GUARD; i++){ |
| 169 | if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){ |
drh | 03ab733 | 2003-08-26 11:29:07 +0000 | [diff] [blame] | 170 | fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", |
| 171 | (int)oldP); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 174 | } |
| 175 | k = (n + sizeof(int) - 1)/sizeof(int); |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 176 | pi = malloc( (k+N_GUARD*2+1)*sizeof(int) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 177 | if( pi==0 ){ |
| 178 | sqlite_malloc_failed++; |
| 179 | return 0; |
| 180 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 181 | for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122; |
| 182 | pi[N_GUARD] = n; |
| 183 | for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344; |
| 184 | p = &pi[N_GUARD+1]; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 185 | memcpy(p, oldP, n>oldN ? oldN : n); |
| 186 | if( n>oldN ){ |
| 187 | memset(&((char*)p)[oldN], 0, n-oldN); |
| 188 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 189 | memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int)); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 190 | free(oldPi); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 191 | #if MEMORY_DEBUG>1 |
drh | d94a669 | 2002-08-25 18:29:11 +0000 | [diff] [blame] | 192 | fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n", |
| 193 | ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 194 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 195 | return p; |
| 196 | } |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | ** Make a duplicate of a string into memory obtained from malloc() |
| 200 | ** Free the original string using sqliteFree(). |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 201 | ** |
| 202 | ** This routine is called on all strings that are passed outside of |
| 203 | ** the SQLite library. That way clients can free the string using free() |
| 204 | ** rather than having to call sqliteFree(). |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 205 | */ |
| 206 | void sqliteStrRealloc(char **pz){ |
| 207 | char *zNew; |
| 208 | if( pz==0 || *pz==0 ) return; |
| 209 | zNew = malloc( strlen(*pz) + 1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 210 | if( zNew==0 ){ |
| 211 | sqlite_malloc_failed++; |
| 212 | sqliteFree(*pz); |
| 213 | *pz = 0; |
| 214 | } |
| 215 | strcpy(zNew, *pz); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 216 | sqliteFree(*pz); |
| 217 | *pz = zNew; |
| 218 | } |
| 219 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 220 | /* |
| 221 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 222 | */ |
| 223 | char *sqliteStrDup_(const char *z, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 224 | char *zNew; |
| 225 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 226 | zNew = sqliteMalloc_(strlen(z)+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 227 | if( zNew ) strcpy(zNew, z); |
| 228 | return zNew; |
| 229 | } |
| 230 | char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 231 | char *zNew; |
| 232 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 233 | zNew = sqliteMalloc_(n+1, 0, zFile, line); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 234 | if( zNew ){ |
| 235 | memcpy(zNew, z, n); |
| 236 | zNew[n] = 0; |
| 237 | } |
| 238 | return zNew; |
| 239 | } |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 240 | #endif /* MEMORY_DEBUG */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 241 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 242 | /* |
| 243 | ** The following versions of malloc() and free() are for use in a |
| 244 | ** normal build. |
| 245 | */ |
| 246 | #if !defined(MEMORY_DEBUG) |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 247 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 248 | /* |
| 249 | ** Allocate new memory and set it to zero. Return NULL if |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 250 | ** no memory is available. See also sqliteMallocRaw(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 251 | */ |
| 252 | void *sqliteMalloc(int n){ |
drh | 2678058 | 2002-10-20 15:46:22 +0000 | [diff] [blame] | 253 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame^] | 254 | if( (p = malloc(n))==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 255 | sqlite_malloc_failed++; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame^] | 256 | }else{ |
| 257 | memset(p, 0, n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 258 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | return p; |
| 260 | } |
| 261 | |
| 262 | /* |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 263 | ** Allocate new memory but do not set it to zero. Return NULL if |
| 264 | ** no memory is available. See also sqliteMalloc(). |
| 265 | */ |
| 266 | void *sqliteMallocRaw(int n){ |
| 267 | void *p; |
drh | 8548a05 | 2003-10-22 22:15:27 +0000 | [diff] [blame^] | 268 | if( (p = malloc(n))==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 269 | sqlite_malloc_failed++; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 270 | } |
| 271 | return p; |
| 272 | } |
| 273 | |
| 274 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 275 | ** Free memory previously obtained from sqliteMalloc() |
| 276 | */ |
| 277 | void sqliteFree(void *p){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 278 | if( p ){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 279 | free(p); |
| 280 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
| 284 | ** Resize a prior allocation. If p==0, then this routine |
| 285 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 286 | ** works just like sqliteFree(). |
| 287 | */ |
| 288 | void *sqliteRealloc(void *p, int n){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 289 | void *p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 290 | if( p==0 ){ |
| 291 | return sqliteMalloc(n); |
| 292 | } |
| 293 | if( n==0 ){ |
| 294 | sqliteFree(p); |
| 295 | return 0; |
| 296 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 297 | p2 = realloc(p, n); |
| 298 | if( p2==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 299 | sqlite_malloc_failed++; |
| 300 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 301 | return p2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 306 | */ |
| 307 | char *sqliteStrDup(const char *z){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 308 | char *zNew; |
| 309 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 310 | zNew = sqliteMallocRaw(strlen(z)+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 311 | if( zNew ) strcpy(zNew, z); |
| 312 | return zNew; |
| 313 | } |
| 314 | char *sqliteStrNDup(const char *z, int n){ |
drh | 567c604 | 2002-02-28 04:10:29 +0000 | [diff] [blame] | 315 | char *zNew; |
| 316 | if( z==0 ) return 0; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 317 | zNew = sqliteMallocRaw(n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 318 | if( zNew ){ |
| 319 | memcpy(zNew, z, n); |
| 320 | zNew[n] = 0; |
| 321 | } |
| 322 | return zNew; |
| 323 | } |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 324 | #endif /* !defined(MEMORY_DEBUG) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 328 | ** first NULL argument), store the string in memory obtained from |
| 329 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
jplyon | 02be20d | 2003-06-02 06:17:10 +0000 | [diff] [blame] | 330 | ** point to that string. The 1st argument must either be NULL or |
| 331 | ** point to memory obtained from sqliteMalloc(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 332 | */ |
| 333 | void sqliteSetString(char **pz, const char *zFirst, ...){ |
| 334 | va_list ap; |
| 335 | int nByte; |
| 336 | const char *z; |
| 337 | char *zResult; |
| 338 | |
| 339 | if( pz==0 ) return; |
| 340 | nByte = strlen(zFirst) + 1; |
| 341 | va_start(ap, zFirst); |
| 342 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 343 | nByte += strlen(z); |
| 344 | } |
| 345 | va_end(ap); |
| 346 | sqliteFree(*pz); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 347 | *pz = zResult = sqliteMallocRaw( nByte ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 348 | if( zResult==0 ){ |
| 349 | return; |
| 350 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 351 | strcpy(zResult, zFirst); |
| 352 | zResult += strlen(zResult); |
| 353 | va_start(ap, zFirst); |
| 354 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 355 | strcpy(zResult, z); |
| 356 | zResult += strlen(zResult); |
| 357 | } |
| 358 | va_end(ap); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 359 | #ifdef MEMORY_DEBUG |
| 360 | #if MEMORY_DEBUG>1 |
| 361 | fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz); |
| 362 | #endif |
| 363 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
| 367 | ** Works like sqliteSetString, but each string is now followed by |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 368 | ** a length integer which specifies how much of the source string |
jplyon | 02be20d | 2003-06-02 06:17:10 +0000 | [diff] [blame] | 369 | ** to copy (in bytes). -1 means use the whole string. The 1st |
| 370 | ** argument must either be NULL or point to memory obtained from |
| 371 | ** sqliteMalloc(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 372 | */ |
| 373 | void sqliteSetNString(char **pz, ...){ |
| 374 | va_list ap; |
| 375 | int nByte; |
| 376 | const char *z; |
| 377 | char *zResult; |
| 378 | int n; |
| 379 | |
| 380 | if( pz==0 ) return; |
| 381 | nByte = 0; |
| 382 | va_start(ap, pz); |
| 383 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 384 | n = va_arg(ap, int); |
| 385 | if( n<=0 ) n = strlen(z); |
| 386 | nByte += n; |
| 387 | } |
| 388 | va_end(ap); |
| 389 | sqliteFree(*pz); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 390 | *pz = zResult = sqliteMallocRaw( nByte + 1 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | if( zResult==0 ) return; |
| 392 | va_start(ap, pz); |
| 393 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 394 | n = va_arg(ap, int); |
| 395 | if( n<=0 ) n = strlen(z); |
| 396 | strncpy(zResult, z, n); |
| 397 | zResult += n; |
| 398 | } |
| 399 | *zResult = 0; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 400 | #ifdef MEMORY_DEBUG |
| 401 | #if MEMORY_DEBUG>1 |
| 402 | fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz); |
| 403 | #endif |
| 404 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 405 | va_end(ap); |
| 406 | } |
| 407 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 408 | /* |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 409 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 410 | ** The following formatting characters are allowed: |
| 411 | ** |
| 412 | ** %s Insert a string |
| 413 | ** %z A string that should be freed after use |
| 414 | ** %d Insert an integer |
| 415 | ** %T Insert a token |
| 416 | ** %S Insert the first element of a SrcList |
| 417 | */ |
| 418 | void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 419 | va_list ap; |
| 420 | int nByte; |
| 421 | int i, j; |
| 422 | char *z; |
| 423 | static char zNull[] = "NULL"; |
| 424 | |
| 425 | pParse->nErr++; |
| 426 | nByte = 1 + strlen(zFormat); |
| 427 | va_start(ap, zFormat); |
| 428 | for(i=0; zFormat[i]; i++){ |
drh | 665de47 | 2003-03-31 13:36:09 +0000 | [diff] [blame] | 429 | if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 430 | i++; |
| 431 | switch( zFormat[i] ){ |
| 432 | case 'd': { |
| 433 | (void)va_arg(ap, int); |
| 434 | nByte += 20; |
| 435 | break; |
| 436 | } |
| 437 | case 'z': |
| 438 | case 's': { |
| 439 | char *z2 = va_arg(ap, char*); |
| 440 | if( z2==0 ) z2 = zNull; |
| 441 | nByte += strlen(z2); |
| 442 | break; |
| 443 | } |
| 444 | case 'T': { |
| 445 | Token *p = va_arg(ap, Token*); |
| 446 | nByte += p->n; |
| 447 | break; |
| 448 | } |
| 449 | case 'S': { |
| 450 | SrcList *p = va_arg(ap, SrcList*); |
| 451 | int k = va_arg(ap, int); |
| 452 | assert( p->nSrc>k && k>=0 ); |
| 453 | nByte += strlen(p->a[k].zName); |
| 454 | if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ |
| 455 | nByte += strlen(p->a[k].zDatabase)+1; |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | default: { |
| 460 | nByte++; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | va_end(ap); |
| 466 | z = sqliteMalloc( nByte ); |
| 467 | if( z==0 ) return; |
| 468 | sqliteFree(pParse->zErrMsg); |
| 469 | pParse->zErrMsg = z; |
| 470 | va_start(ap, zFormat); |
| 471 | for(i=j=0; zFormat[i]; i++){ |
drh | 665de47 | 2003-03-31 13:36:09 +0000 | [diff] [blame] | 472 | if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 473 | if( i>j ){ |
| 474 | memcpy(z, &zFormat[j], i-j); |
| 475 | z += i-j; |
| 476 | } |
| 477 | j = i+2; |
| 478 | i++; |
| 479 | switch( zFormat[i] ){ |
| 480 | case 'd': { |
| 481 | int x = va_arg(ap, int); |
| 482 | sprintf(z, "%d", x); |
| 483 | z += strlen(z); |
| 484 | break; |
| 485 | } |
| 486 | case 'z': |
| 487 | case 's': { |
| 488 | int len; |
| 489 | char *z2 = va_arg(ap, char*); |
| 490 | if( z2==0 ) z2 = zNull; |
| 491 | len = strlen(z2); |
| 492 | memcpy(z, z2, len); |
| 493 | z += len; |
| 494 | if( zFormat[i]=='z' && z2!=zNull ){ |
| 495 | sqliteFree(z2); |
| 496 | } |
| 497 | break; |
| 498 | } |
| 499 | case 'T': { |
| 500 | Token *p = va_arg(ap, Token*); |
| 501 | memcpy(z, p->z, p->n); |
| 502 | z += p->n; |
| 503 | break; |
| 504 | } |
| 505 | case 'S': { |
| 506 | int len; |
| 507 | SrcList *p = va_arg(ap, SrcList*); |
| 508 | int k = va_arg(ap, int); |
| 509 | assert( p->nSrc>k && k>=0 ); |
| 510 | if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ |
| 511 | len = strlen(p->a[k].zDatabase); |
| 512 | memcpy(z, p->a[k].zDatabase, len); |
| 513 | z += len; |
| 514 | *(z++) = '.'; |
| 515 | } |
| 516 | len = strlen(p->a[k].zName); |
| 517 | memcpy(z, p->a[k].zName, len); |
| 518 | z += len; |
| 519 | break; |
| 520 | } |
| 521 | default: { |
| 522 | *(z++) = zFormat[i]; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | va_end(ap); |
| 528 | if( i>j ){ |
| 529 | memcpy(z, &zFormat[j], i-j); |
| 530 | z += i-j; |
| 531 | } |
| 532 | assert( (z - pParse->zErrMsg) < nByte ); |
| 533 | *z = 0; |
| 534 | } |
| 535 | |
| 536 | /* |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 537 | ** Convert an SQL-style quoted string into a normal string by removing |
| 538 | ** the quote characters. The conversion is done in-place. If the |
| 539 | ** input does not begin with a quote character, then this routine |
| 540 | ** is a no-op. |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 541 | ** |
| 542 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 543 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 544 | ** "a-b-c". |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 545 | */ |
| 546 | void sqliteDequote(char *z){ |
| 547 | int quote; |
| 548 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 549 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 550 | quote = z[0]; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 551 | switch( quote ){ |
| 552 | case '\'': break; |
| 553 | case '"': break; |
| 554 | case '[': quote = ']'; break; |
| 555 | default: return; |
| 556 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 557 | for(i=1, j=0; z[i]; i++){ |
| 558 | if( z[i]==quote ){ |
| 559 | if( z[i+1]==quote ){ |
| 560 | z[j++] = quote; |
| 561 | i++; |
| 562 | }else{ |
| 563 | z[j++] = 0; |
| 564 | break; |
| 565 | } |
| 566 | }else{ |
| 567 | z[j++] = z[i]; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 572 | /* An array to map all upper-case characters into their corresponding |
| 573 | ** lower-case character. |
| 574 | */ |
| 575 | static unsigned char UpperToLower[] = { |
| 576 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 577 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 578 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 579 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, |
| 580 | 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, |
| 581 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, |
| 582 | 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 583 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, |
| 584 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, |
| 585 | 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, |
| 586 | 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, |
| 587 | 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, |
| 588 | 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, |
| 589 | 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, |
| 590 | 252,253,254,255 |
| 591 | }; |
| 592 | |
| 593 | /* |
| 594 | ** This function computes a hash on the name of a keyword. |
| 595 | ** Case is not significant. |
| 596 | */ |
| 597 | int sqliteHashNoCase(const char *z, int n){ |
| 598 | int h = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 599 | if( n<=0 ) n = strlen(z); |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 600 | while( n > 0 ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 601 | h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++]; |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 602 | n--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 603 | } |
drh | 5364f60 | 2003-05-12 23:06:52 +0000 | [diff] [blame] | 604 | return h & 0x7fffffff; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 608 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | ** there is no consistency, we will define our own. |
| 610 | */ |
| 611 | int sqliteStrICmp(const char *zLeft, const char *zRight){ |
| 612 | register unsigned char *a, *b; |
| 613 | a = (unsigned char *)zLeft; |
| 614 | b = (unsigned char *)zRight; |
| 615 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 616 | return *a - *b; |
| 617 | } |
| 618 | int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){ |
| 619 | register unsigned char *a, *b; |
| 620 | a = (unsigned char *)zLeft; |
| 621 | b = (unsigned char *)zRight; |
| 622 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 623 | return N<0 ? 0 : *a - *b; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 624 | } |
| 625 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 626 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 627 | ** Return TRUE if z is a pure numeric string. Return FALSE if the |
| 628 | ** string contains any character which is not part of a number. |
| 629 | ** |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 630 | ** Am empty string is considered non-numeric. |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 631 | */ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 632 | int sqliteIsNumber(const char *z){ |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 633 | if( *z=='-' || *z=='+' ) z++; |
| 634 | if( !isdigit(*z) ){ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 635 | return 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 636 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 637 | z++; |
| 638 | while( isdigit(*z) ){ z++; } |
| 639 | if( *z=='.' ){ |
| 640 | z++; |
| 641 | if( !isdigit(*z) ) return 0; |
| 642 | while( isdigit(*z) ){ z++; } |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 643 | } |
| 644 | if( *z=='e' || *z=='E' ){ |
| 645 | z++; |
| 646 | if( *z=='+' || *z=='-' ) z++; |
| 647 | if( !isdigit(*z) ) return 0; |
| 648 | while( isdigit(*z) ){ z++; } |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 649 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 650 | return *z==0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 651 | } |
| 652 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | /* This comparison routine is what we use for comparison operations |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 654 | ** between numeric values in an SQL expression. "Numeric" is a little |
| 655 | ** bit misleading here. What we mean is that the strings have a |
| 656 | ** type of "numeric" from the point of view of SQL. The strings |
| 657 | ** do not necessarily contain numbers. They could contain text. |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 658 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 659 | ** If the input strings both look like actual numbers then they |
| 660 | ** compare in numerical order. Numerical strings are always less |
| 661 | ** than non-numeric strings so if one input string looks like a |
| 662 | ** number and the other does not, then the one that looks like |
| 663 | ** a number is the smaller. Non-numeric strings compare in |
| 664 | ** lexigraphical order (the same order as strcmp()). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 665 | */ |
| 666 | int sqliteCompare(const char *atext, const char *btext){ |
| 667 | int result; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 668 | int isNumA, isNumB; |
| 669 | if( atext==0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 670 | return -1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 671 | }else if( btext==0 ){ |
| 672 | return 1; |
| 673 | } |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 674 | isNumA = sqliteIsNumber(atext); |
| 675 | isNumB = sqliteIsNumber(btext); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 676 | if( isNumA ){ |
| 677 | if( !isNumB ){ |
| 678 | result = -1; |
| 679 | }else{ |
| 680 | double rA, rB; |
| 681 | rA = atof(atext); |
| 682 | rB = atof(btext); |
| 683 | if( rA<rB ){ |
| 684 | result = -1; |
| 685 | }else if( rA>rB ){ |
| 686 | result = +1; |
| 687 | }else{ |
| 688 | result = 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 691 | }else if( isNumB ){ |
| 692 | result = +1; |
| 693 | }else { |
| 694 | result = strcmp(atext, btext); |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 695 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 696 | return result; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 697 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 698 | |
| 699 | /* |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 700 | ** This routine is used for sorting. Each key is a list of one or more |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 701 | ** null-terminated elements. The list is terminated by two nulls in |
| 702 | ** a row. For example, the following text is a key with three elements |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 703 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 704 | ** Aone\000Dtwo\000Athree\000\000 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 705 | ** |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 706 | ** All elements begin with one of the characters "+-AD" and end with "\000" |
| 707 | ** with zero or more text elements in between. Except, NULL elements |
| 708 | ** consist of the special two-character sequence "N\000". |
| 709 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 710 | ** Both arguments will have the same number of elements. This routine |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 711 | ** returns negative, zero, or positive if the first argument is less |
| 712 | ** than, equal to, or greater than the first. (Result is a-b). |
| 713 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 714 | ** Each element begins with one of the characters "+", "-", "A", "D". |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 715 | ** This character determines the sort order and collating sequence: |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 716 | ** |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 717 | ** + Sort numerically in ascending order |
| 718 | ** - Sort numerically in descending order |
| 719 | ** A Sort as strings in ascending order |
| 720 | ** D Sort as strings in descending order. |
| 721 | ** |
| 722 | ** For the "+" and "-" sorting, pure numeric strings (strings for which the |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 723 | ** isNum() function above returns TRUE) always compare less than strings |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 724 | ** that are not pure numerics. Non-numeric strings compare in memcmp() |
| 725 | ** order. This is the same sort order as the sqliteCompare() function |
| 726 | ** above generates. |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 727 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 728 | ** The last point is a change from version 2.6.3 to version 2.7.0. In |
| 729 | ** version 2.6.3 and earlier, substrings of digits compare in numerical |
| 730 | ** and case was used only to break a tie. |
| 731 | ** |
| 732 | ** Elements that begin with 'A' or 'D' compare in memcmp() order regardless |
| 733 | ** of whether or not they look like a number. |
| 734 | ** |
| 735 | ** Note that the sort order imposed by the rules above is the same |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 736 | ** from the ordering defined by the "<", "<=", ">", and ">=" operators |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 737 | ** of expressions and for indices. This was not the case for version |
| 738 | ** 2.6.3 and earlier. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 739 | */ |
| 740 | int sqliteSortCompare(const char *a, const char *b){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 741 | int res = 0; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 742 | int isNumA, isNumB; |
drh | 294fb92 | 2002-09-30 01:31:21 +0000 | [diff] [blame] | 743 | int dir = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 744 | |
| 745 | while( res==0 && *a && *b ){ |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 746 | if( a[0]=='N' || b[0]=='N' ){ |
| 747 | if( a[0]==b[0] ){ |
| 748 | a += 2; |
| 749 | b += 2; |
| 750 | continue; |
| 751 | } |
| 752 | if( a[0]=='N' ){ |
| 753 | dir = b[0]; |
| 754 | res = -1; |
| 755 | }else{ |
| 756 | dir = a[0]; |
| 757 | res = +1; |
| 758 | } |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 759 | break; |
| 760 | } |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 761 | assert( a[0]==b[0] ); |
| 762 | if( (dir=a[0])=='A' || a[0]=='D' ){ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 763 | res = strcmp(&a[1],&b[1]); |
| 764 | if( res ) break; |
| 765 | }else{ |
| 766 | isNumA = sqliteIsNumber(&a[1]); |
| 767 | isNumB = sqliteIsNumber(&b[1]); |
| 768 | if( isNumA ){ |
| 769 | double rA, rB; |
| 770 | if( !isNumB ){ |
| 771 | res = -1; |
| 772 | break; |
| 773 | } |
| 774 | rA = atof(&a[1]); |
| 775 | rB = atof(&b[1]); |
| 776 | if( rA<rB ){ |
| 777 | res = -1; |
| 778 | break; |
| 779 | } |
| 780 | if( rA>rB ){ |
| 781 | res = +1; |
| 782 | break; |
| 783 | } |
| 784 | }else if( isNumB ){ |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 785 | res = +1; |
| 786 | break; |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 787 | }else{ |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 788 | res = strcmp(&a[1],&b[1]); |
| 789 | if( res ) break; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 790 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 791 | } |
drh | cab2005 | 2003-04-18 17:45:14 +0000 | [diff] [blame] | 792 | a += strlen(&a[1]) + 2; |
| 793 | b += strlen(&b[1]) + 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 794 | } |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 795 | if( dir=='-' || dir=='D' ) res = -res; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 796 | return res; |
| 797 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 798 | |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 799 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 800 | ** Some powers of 64. These constants are needed in the |
| 801 | ** sqliteRealToSortable() routine below. |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 802 | */ |
| 803 | #define _64e3 (64.0 * 64.0 * 64.0) |
| 804 | #define _64e4 (64.0 * 64.0 * 64.0 * 64.0) |
| 805 | #define _64e15 (_64e3 * _64e4 * _64e4 * _64e4) |
| 806 | #define _64e16 (_64e4 * _64e4 * _64e4 * _64e4) |
| 807 | #define _64e63 (_64e15 * _64e16 * _64e16 * _64e16) |
| 808 | #define _64e64 (_64e16 * _64e16 * _64e16 * _64e16) |
| 809 | |
| 810 | /* |
| 811 | ** The following procedure converts a double-precision floating point |
| 812 | ** number into a string. The resulting string has the property that |
| 813 | ** two such strings comparied using strcmp() or memcmp() will give the |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 814 | ** same results as a numeric comparison of the original floating point |
| 815 | ** numbers. |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 816 | ** |
| 817 | ** This routine is used to generate database keys from floating point |
| 818 | ** numbers such that the keys sort in the same order as the original |
| 819 | ** floating point numbers even though the keys are compared using |
| 820 | ** memcmp(). |
| 821 | ** |
| 822 | ** The calling function should have allocated at least 14 characters |
| 823 | ** of space for the buffer z[]. |
| 824 | */ |
| 825 | void sqliteRealToSortable(double r, char *z){ |
| 826 | int neg; |
| 827 | int exp; |
| 828 | int cnt = 0; |
| 829 | |
| 830 | /* This array maps integers between 0 and 63 into base-64 digits. |
| 831 | ** The digits must be chosen such at their ASCII codes are increasing. |
| 832 | ** This means we can not use the traditional base-64 digit set. */ |
| 833 | static const char zDigit[] = |
| 834 | "0123456789" |
| 835 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 836 | "abcdefghijklmnopqrstuvwxyz" |
| 837 | "|~"; |
| 838 | if( r<0.0 ){ |
| 839 | neg = 1; |
| 840 | r = -r; |
| 841 | *z++ = '-'; |
| 842 | } else { |
| 843 | neg = 0; |
| 844 | *z++ = '0'; |
| 845 | } |
| 846 | exp = 0; |
| 847 | |
| 848 | if( r==0.0 ){ |
| 849 | exp = -1024; |
| 850 | }else if( r<(0.5/64.0) ){ |
| 851 | while( r < 0.5/_64e64 && exp > -961 ){ r *= _64e64; exp -= 64; } |
| 852 | while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16; exp -= 16; } |
| 853 | while( r < 0.5/_64e4 && exp > -1021 ){ r *= _64e4; exp -= 4; } |
| 854 | while( r < 0.5/64.0 && exp > -1024 ){ r *= 64.0; exp -= 1; } |
| 855 | }else if( r>=0.5 ){ |
| 856 | while( r >= 0.5*_64e63 && exp < 960 ){ r *= 1.0/_64e64; exp += 64; } |
| 857 | while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; } |
| 858 | while( r >= 0.5*_64e3 && exp < 1020 ){ r *= 1.0/_64e4; exp += 4; } |
| 859 | while( r >= 0.5 && exp < 1023 ){ r *= 1.0/64.0; exp += 1; } |
| 860 | } |
| 861 | if( neg ){ |
| 862 | exp = -exp; |
| 863 | r = -r; |
| 864 | } |
| 865 | exp += 1024; |
| 866 | r += 0.5; |
| 867 | if( exp<0 ) return; |
| 868 | if( exp>=2048 || r>=1.0 ){ |
| 869 | strcpy(z, "~~~~~~~~~~~~"); |
| 870 | return; |
| 871 | } |
| 872 | *z++ = zDigit[(exp>>6)&0x3f]; |
| 873 | *z++ = zDigit[exp & 0x3f]; |
| 874 | while( r>0.0 && cnt<10 ){ |
| 875 | int digit; |
| 876 | r *= 64.0; |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 877 | digit = (int)r; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 878 | assert( digit>=0 && digit<64 ); |
| 879 | *z++ = zDigit[digit & 0x3f]; |
| 880 | r -= digit; |
| 881 | cnt++; |
| 882 | } |
| 883 | *z = 0; |
| 884 | } |
| 885 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 886 | #ifdef SQLITE_UTF8 |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 887 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 888 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 889 | ** X so that it points to the next character. This only works right |
| 890 | ** if X points to a well-formed UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 891 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 892 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
| 893 | #define sqliteCharVal(X) sqlite_utf8_to_int(X) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 894 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 895 | #else /* !defined(SQLITE_UTF8) */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 896 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 897 | ** For iso8859 encoding, the next character is just the next byte. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 898 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 899 | #define sqliteNextChar(X) (++(X)); |
| 900 | #define sqliteCharVal(X) ((int)*(X)) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 901 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 902 | #endif /* defined(SQLITE_UTF8) */ |
| 903 | |
| 904 | |
| 905 | #ifdef SQLITE_UTF8 |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 906 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 907 | ** Convert the UTF-8 character to which z points into a 31-bit |
| 908 | ** UCS character. This only works right if z points to a well-formed |
| 909 | ** UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 910 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 911 | static int sqlite_utf8_to_int(const unsigned char *z){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 912 | int c; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 913 | static const int initVal[] = { |
| 914 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 915 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 916 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| 917 | 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 918 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, |
| 919 | 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 920 | 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, |
| 921 | 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 922 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, |
| 923 | 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 924 | 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, |
| 925 | 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, |
| 926 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2, |
| 927 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 928 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, |
| 929 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 930 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254, |
| 931 | 255, |
| 932 | }; |
| 933 | c = initVal[*(z++)]; |
| 934 | while( (0xc0&*z)==0x80 ){ |
| 935 | c = (c<<6) | (0x3f&*(z++)); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 936 | } |
| 937 | return c; |
| 938 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 939 | #endif |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 940 | |
| 941 | /* |
| 942 | ** Compare two UTF-8 strings for equality where the first string can |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 943 | ** potentially be a "glob" expression. Return true (1) if they |
| 944 | ** are the same and false (0) if they are different. |
| 945 | ** |
| 946 | ** Globbing rules: |
| 947 | ** |
| 948 | ** '*' Matches any sequence of zero or more characters. |
| 949 | ** |
| 950 | ** '?' Matches exactly one character. |
| 951 | ** |
| 952 | ** [...] Matches one character from the enclosed list of |
| 953 | ** characters. |
| 954 | ** |
| 955 | ** [^...] Matches one character not in the enclosed list. |
| 956 | ** |
| 957 | ** With the [...] and [^...] matching, a ']' character can be included |
| 958 | ** in the list by making it the first character after '[' or '^'. A |
| 959 | ** range of characters can be specified using '-'. Example: |
| 960 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 961 | ** it the last character in the list. |
| 962 | ** |
| 963 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 964 | ** |
| 965 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 966 | ** |
| 967 | ** abc[*]xyz Matches "abc*xyz" only |
| 968 | */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 969 | int |
| 970 | sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){ |
| 971 | register int c; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 972 | int invert; |
| 973 | int seen; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 974 | int c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 975 | |
| 976 | while( (c = *zPattern)!=0 ){ |
| 977 | switch( c ){ |
| 978 | case '*': |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 979 | while( (c=zPattern[1]) == '*' || c == '?' ){ |
| 980 | if( c=='?' ){ |
| 981 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 982 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 983 | } |
| 984 | zPattern++; |
| 985 | } |
| 986 | if( c==0 ) return 1; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 987 | if( c=='[' ){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 988 | while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 989 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 990 | } |
| 991 | return *zString!=0; |
| 992 | }else{ |
| 993 | while( (c2 = *zString)!=0 ){ |
| 994 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 995 | if( c2==0 ) return 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 996 | if( sqliteGlobCompare(&zPattern[1],zString) ) return 1; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 997 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 998 | } |
| 999 | return 0; |
| 1000 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1001 | case '?': { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1002 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1003 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1004 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1005 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1006 | } |
| 1007 | case '[': { |
| 1008 | int prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1009 | seen = 0; |
| 1010 | invert = 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1011 | c = sqliteCharVal(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1012 | if( c==0 ) return 0; |
| 1013 | c2 = *++zPattern; |
| 1014 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 1015 | if( c2==']' ){ |
| 1016 | if( c==']' ) seen = 1; |
| 1017 | c2 = *++zPattern; |
| 1018 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1019 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1020 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 1021 | zPattern++; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1022 | c2 = sqliteCharVal(zPattern); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1023 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 1024 | prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1025 | }else if( c==c2 ){ |
| 1026 | seen = 1; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1027 | prior_c = c2; |
| 1028 | }else{ |
| 1029 | prior_c = c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1030 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1031 | sqliteNextChar(zPattern); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1032 | } |
| 1033 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1034 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1035 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1036 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1037 | } |
| 1038 | default: { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1039 | if( c != *zString ) return 0; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1040 | zPattern++; |
| 1041 | zString++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1042 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1043 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1044 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1045 | } |
| 1046 | return *zString==0; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1050 | ** Compare two UTF-8 strings for equality using the "LIKE" operator of |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1051 | ** SQL. The '%' character matches any sequence of 0 or more |
| 1052 | ** characters and '_' matches any single character. Case is |
| 1053 | ** not significant. |
| 1054 | ** |
| 1055 | ** This routine is just an adaptation of the sqliteGlobCompare() |
| 1056 | ** routine above. |
| 1057 | */ |
| 1058 | int |
| 1059 | sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1060 | register int c; |
| 1061 | int c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1062 | |
| 1063 | while( (c = UpperToLower[*zPattern])!=0 ){ |
| 1064 | switch( c ){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1065 | case '%': { |
| 1066 | while( (c=zPattern[1]) == '%' || c == '_' ){ |
| 1067 | if( c=='_' ){ |
| 1068 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1069 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1070 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1071 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1072 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1073 | if( c==0 ) return 1; |
| 1074 | c = UpperToLower[c]; |
| 1075 | while( (c2=UpperToLower[*zString])!=0 ){ |
| 1076 | while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; } |
| 1077 | if( c2==0 ) return 0; |
| 1078 | if( sqliteLikeCompare(&zPattern[1],zString) ) return 1; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1079 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1080 | } |
| 1081 | return 0; |
| 1082 | } |
| 1083 | case '_': { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1084 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1085 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1086 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1087 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1088 | } |
| 1089 | default: { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1090 | if( c != UpperToLower[*zString] ) return 0; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1091 | zPattern++; |
| 1092 | zString++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1093 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1094 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1095 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1096 | } |
| 1097 | return *zString==0; |
| 1098 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1099 | |
| 1100 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1101 | ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. |
| 1102 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN |
| 1103 | ** when this routine is called. |
| 1104 | ** |
| 1105 | ** This routine is a attempt to detect if two threads use the |
| 1106 | ** same sqlite* pointer at the same time. There is a race |
| 1107 | ** condition so it is possible that the error is not detected. |
| 1108 | ** But usually the problem will be seen. The result will be an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1109 | ** error which can be used to debug the application that is |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1110 | ** using SQLite incorrectly. |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1111 | ** |
| 1112 | ** Ticket #202: If db->magic is not a valid open value, take care not |
| 1113 | ** to modify the db structure at all. It could be that db is a stale |
| 1114 | ** pointer. In other words, it could be that there has been a prior |
| 1115 | ** call to sqlite_close(db) and db has been deallocated. And we do |
| 1116 | ** not want to write into deallocated memory. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1117 | */ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1118 | int sqliteSafetyOn(sqlite *db){ |
| 1119 | if( db->magic==SQLITE_MAGIC_OPEN ){ |
| 1120 | db->magic = SQLITE_MAGIC_BUSY; |
| 1121 | return 0; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1122 | }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR |
| 1123 | || db->want_to_close ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1124 | db->magic = SQLITE_MAGIC_ERROR; |
| 1125 | db->flags |= SQLITE_Interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1126 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1127 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. |
| 1132 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY |
| 1133 | ** when this routine is called. |
| 1134 | */ |
| 1135 | int sqliteSafetyOff(sqlite *db){ |
| 1136 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 1137 | db->magic = SQLITE_MAGIC_OPEN; |
| 1138 | return 0; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1139 | }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR |
| 1140 | || db->want_to_close ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1141 | db->magic = SQLITE_MAGIC_ERROR; |
| 1142 | db->flags |= SQLITE_Interrupt; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1143 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1144 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | ** Check to make sure we are not currently executing an sqlite_exec(). |
| 1149 | ** If we are currently in an sqlite_exec(), return true and set |
| 1150 | ** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete |
| 1151 | ** shutdown of the database. |
| 1152 | ** |
| 1153 | ** This routine is used to try to detect when API routines are called |
| 1154 | ** at the wrong time or in the wrong sequence. |
| 1155 | */ |
| 1156 | int sqliteSafetyCheck(sqlite *db){ |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 1157 | if( db->pVdbe!=0 ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1158 | db->magic = SQLITE_MAGIC_ERROR; |
| 1159 | return 1; |
| 1160 | } |
| 1161 | return 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | } |