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 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.103 2004/06/18 04:24:55 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 | */ |
| 278 | void *sqliteMalloc(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 | */ |
| 292 | void *sqliteMallocRaw(int n){ |
| 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 | */ |
| 303 | void sqliteFree(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 | */ |
| 314 | void *sqliteRealloc(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 | */ |
| 333 | char *sqliteStrDup(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 | } |
| 340 | char *sqliteStrNDup(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 |
| 480 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 481 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 482 | va_list ap; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 483 | pParse->nErr++; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 484 | sqliteFree(pParse->zErrMsg); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 485 | va_start(ap, zFormat); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 486 | pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 487 | va_end(ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 491 | ** Convert an SQL-style quoted string into a normal string by removing |
| 492 | ** the quote characters. The conversion is done in-place. If the |
| 493 | ** input does not begin with a quote character, then this routine |
| 494 | ** is a no-op. |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 495 | ** |
| 496 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 497 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 498 | ** "a-b-c". |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 499 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 500 | void sqlite3Dequote(char *z){ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 501 | int quote; |
| 502 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 503 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 504 | quote = z[0]; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 505 | switch( quote ){ |
| 506 | case '\'': break; |
| 507 | case '"': break; |
| 508 | case '[': quote = ']'; break; |
| 509 | default: return; |
| 510 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 511 | for(i=1, j=0; z[i]; i++){ |
| 512 | if( z[i]==quote ){ |
| 513 | if( z[i+1]==quote ){ |
| 514 | z[j++] = quote; |
| 515 | i++; |
| 516 | }else{ |
| 517 | z[j++] = 0; |
| 518 | break; |
| 519 | } |
| 520 | }else{ |
| 521 | z[j++] = z[i]; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 526 | /* An array to map all upper-case characters into their corresponding |
| 527 | ** lower-case character. |
| 528 | */ |
| 529 | static unsigned char UpperToLower[] = { |
| 530 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 531 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 532 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 533 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, |
| 534 | 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, |
| 535 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, |
| 536 | 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 537 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, |
| 538 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, |
| 539 | 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, |
| 540 | 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, |
| 541 | 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, |
| 542 | 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, |
| 543 | 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, |
| 544 | 252,253,254,255 |
| 545 | }; |
| 546 | |
| 547 | /* |
| 548 | ** This function computes a hash on the name of a keyword. |
| 549 | ** Case is not significant. |
| 550 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 551 | int sqlite3HashNoCase(const char *z, int n){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | int h = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 553 | if( n<=0 ) n = strlen(z); |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 554 | while( n > 0 ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 555 | h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++]; |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 556 | n--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 557 | } |
drh | 5364f60 | 2003-05-12 23:06:52 +0000 | [diff] [blame] | 558 | return h & 0x7fffffff; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 562 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 563 | ** there is no consistency, we will define our own. |
| 564 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 565 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 566 | register unsigned char *a, *b; |
| 567 | a = (unsigned char *)zLeft; |
| 568 | b = (unsigned char *)zRight; |
| 569 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 570 | return *a - *b; |
| 571 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 572 | int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ |
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( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 577 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 578 | } |
| 579 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 580 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 581 | ** Return TRUE if z is a pure numeric string. Return FALSE if the |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 582 | ** string contains any character which is not part of a number. If |
| 583 | ** the string is numeric and contains the '.' character, set *realnum |
| 584 | ** to TRUE (otherwise FALSE). |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 585 | ** |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 586 | ** Am empty string is considered non-numeric. |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 587 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 588 | int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 589 | int incr = (enc==SQLITE_UTF8?1:2); |
| 590 | if( enc==SQLITE_UTF16LE ) z++; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 591 | if( *z=='-' || *z=='+' ) z += incr; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 592 | if( !isdigit(*z) ){ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 593 | return 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 594 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 595 | z += incr; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 596 | if( realnum ) *realnum = 0; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 597 | while( isdigit(*z) ){ z += incr; } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 598 | if( *z=='.' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 599 | z += incr; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 600 | if( !isdigit(*z) ) return 0; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 601 | while( isdigit(*z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 602 | if( realnum ) *realnum = 1; |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 603 | } |
| 604 | if( *z=='e' || *z=='E' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 605 | z += incr; |
| 606 | if( *z=='+' || *z=='-' ) z += incr; |
drh | bb07e9a | 2003-04-16 02:17:35 +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 | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 610 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 611 | return *z==0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 612 | } |
| 613 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 614 | /* |
| 615 | ** The string z[] is an ascii representation of a real number. |
| 616 | ** Convert this string to a double. |
| 617 | ** |
| 618 | ** This routine assumes that z[] really is a valid number. If it |
| 619 | ** is not, the result is undefined. |
| 620 | ** |
| 621 | ** This routine is used instead of the library atof() function because |
| 622 | ** the library atof() might want to use "," as the decimal point instead |
| 623 | ** of "." depending on how locale is set. But that would cause problems |
| 624 | ** for SQL. So this routine always uses "." regardless of locale. |
| 625 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 626 | double sqlite3AtoF(const char *z, const char **pzEnd){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 627 | int sign = 1; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 628 | LONGDOUBLE_TYPE v1 = 0.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 629 | if( *z=='-' ){ |
| 630 | sign = -1; |
| 631 | z++; |
| 632 | }else if( *z=='+' ){ |
| 633 | z++; |
| 634 | } |
| 635 | while( isdigit(*z) ){ |
| 636 | v1 = v1*10.0 + (*z - '0'); |
| 637 | z++; |
| 638 | } |
| 639 | if( *z=='.' ){ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 640 | LONGDOUBLE_TYPE divisor = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 641 | z++; |
| 642 | while( isdigit(*z) ){ |
| 643 | v1 = v1*10.0 + (*z - '0'); |
| 644 | divisor *= 10.0; |
| 645 | z++; |
| 646 | } |
| 647 | v1 /= divisor; |
| 648 | } |
| 649 | if( *z=='e' || *z=='E' ){ |
| 650 | int esign = 1; |
| 651 | int eval = 0; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 652 | LONGDOUBLE_TYPE scale = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 653 | z++; |
| 654 | if( *z=='-' ){ |
| 655 | esign = -1; |
| 656 | z++; |
| 657 | }else if( *z=='+' ){ |
| 658 | z++; |
| 659 | } |
| 660 | while( isdigit(*z) ){ |
| 661 | eval = eval*10 + *z - '0'; |
| 662 | z++; |
| 663 | } |
| 664 | while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } |
| 665 | while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } |
| 666 | while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } |
| 667 | while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } |
| 668 | if( esign<0 ){ |
| 669 | v1 /= scale; |
| 670 | }else{ |
| 671 | v1 *= scale; |
| 672 | } |
| 673 | } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 674 | if( pzEnd ) *pzEnd = z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 675 | return sign<0 ? -v1 : v1; |
| 676 | } |
| 677 | |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 678 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 679 | ** Return TRUE if zNum is a 64-bit signed integer and write |
| 680 | ** the value of the integer into *pNum. If zNum is not an integer |
| 681 | ** or is an integer that is too large to be expressed with 64 bits, |
| 682 | ** then return false. If n>0 and the integer is string is not |
| 683 | ** exactly n bytes long, return false. |
| 684 | ** |
| 685 | ** When this routine was originally written it dealt with only |
| 686 | ** 32-bit numbers. At that time, it was much faster than the |
| 687 | ** atoi() library routine in RedHat 7.2. |
| 688 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 689 | int sqlite3atoi64(const char *zNum, i64 *pNum){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 690 | i64 v = 0; |
| 691 | int neg; |
| 692 | int i, c; |
| 693 | if( *zNum=='-' ){ |
| 694 | neg = 1; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 695 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 696 | }else if( *zNum=='+' ){ |
| 697 | neg = 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 698 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 699 | }else{ |
| 700 | neg = 0; |
| 701 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 702 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 703 | v = v*10 + c - '0'; |
| 704 | } |
| 705 | *pNum = neg ? -v : v; |
| 706 | return c==0 && i>0 && |
| 707 | (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0)); |
| 708 | } |
| 709 | |
| 710 | /* |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 711 | ** The string zNum represents an integer. There might be some other |
| 712 | ** information following the integer too, but that part is ignored. |
| 713 | ** If the integer that the prefix of zNum represents will fit in a |
| 714 | ** 32-bit signed integer, return TRUE. Otherwise return FALSE. |
| 715 | ** |
| 716 | ** This routine returns FALSE for the string -2147483648 even that |
| 717 | ** that number will, in theory fit in a 32-bit integer. But positive |
| 718 | ** 2147483648 will not fit in 32 bits. So it seems safer to return |
| 719 | ** false. |
| 720 | */ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 721 | static int sqlite3FitsIn32Bits(const char *zNum){ |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 722 | int i, c; |
| 723 | if( *zNum=='-' || *zNum=='+' ) zNum++; |
| 724 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} |
| 725 | return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0); |
| 726 | } |
| 727 | |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 728 | /* |
| 729 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 730 | ** *pValue to that integer and return true. Otherwise return false. |
| 731 | */ |
| 732 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 733 | if( sqlite3FitsIn32Bits(zNum) ){ |
| 734 | *pValue = atoi(zNum); |
| 735 | return 1; |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | ** The string zNum represents an integer. There might be some other |
| 742 | ** information following the integer too, but that part is ignored. |
| 743 | ** If the integer that the prefix of zNum represents will fit in a |
| 744 | ** 64-bit signed integer, return TRUE. Otherwise return FALSE. |
| 745 | ** |
| 746 | ** This routine returns FALSE for the string -9223372036854775808 even that |
| 747 | ** that number will, in theory fit in a 64-bit integer. Positive |
| 748 | ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return |
| 749 | ** false. |
| 750 | */ |
| 751 | int sqlite3FitsIn64Bits(const char *zNum){ |
| 752 | int i, c; |
| 753 | if( *zNum=='-' || *zNum=='+' ) zNum++; |
| 754 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} |
| 755 | return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | ** If zNum represents an integer that will fit in 64-bits, then set |
| 760 | ** *pValue to that integer and return true. Otherwise return false. |
| 761 | */ |
| 762 | int sqlite3GetInt64(const char *zNum, i64 *pValue){ |
| 763 | if( sqlite3FitsIn64Bits(zNum) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 764 | sqlite3atoi64(zNum, pValue); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 765 | return 1; |
| 766 | } |
| 767 | return 0; |
| 768 | } |
| 769 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 770 | /* This comparison routine is what we use for comparison operations |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 771 | ** between numeric values in an SQL expression. "Numeric" is a little |
| 772 | ** bit misleading here. What we mean is that the strings have a |
| 773 | ** type of "numeric" from the point of view of SQL. The strings |
| 774 | ** do not necessarily contain numbers. They could contain text. |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 775 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 776 | ** If the input strings both look like actual numbers then they |
| 777 | ** compare in numerical order. Numerical strings are always less |
| 778 | ** than non-numeric strings so if one input string looks like a |
| 779 | ** number and the other does not, then the one that looks like |
| 780 | ** a number is the smaller. Non-numeric strings compare in |
| 781 | ** lexigraphical order (the same order as strcmp()). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 783 | int sqlite3Compare(const char *atext, const char *btext){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 784 | int result; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 785 | int isNumA, isNumB; |
| 786 | if( atext==0 ){ |
drh | 8912d10 | 2002-05-26 21:34:58 +0000 | [diff] [blame] | 787 | return -1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 788 | }else if( btext==0 ){ |
| 789 | return 1; |
| 790 | } |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 791 | isNumA = sqlite3IsNumber(atext, 0, SQLITE_UTF8); |
| 792 | isNumB = sqlite3IsNumber(btext, 0, SQLITE_UTF8); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 793 | if( isNumA ){ |
| 794 | if( !isNumB ){ |
| 795 | result = -1; |
| 796 | }else{ |
| 797 | double rA, rB; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 798 | rA = sqlite3AtoF(atext, 0); |
| 799 | rB = sqlite3AtoF(btext, 0); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 800 | if( rA<rB ){ |
| 801 | result = -1; |
| 802 | }else if( rA>rB ){ |
| 803 | result = +1; |
| 804 | }else{ |
| 805 | result = 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 808 | }else if( isNumB ){ |
| 809 | result = +1; |
| 810 | }else { |
| 811 | result = strcmp(atext, btext); |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 812 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 813 | return result; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 814 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 815 | |
| 816 | /* |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 817 | ** 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] | 818 | ** null-terminated elements. The list is terminated by two nulls in |
| 819 | ** a row. For example, the following text is a key with three elements |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 820 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 821 | ** Aone\000Dtwo\000Athree\000\000 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | ** |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 823 | ** All elements begin with one of the characters "+-AD" and end with "\000" |
| 824 | ** with zero or more text elements in between. Except, NULL elements |
| 825 | ** consist of the special two-character sequence "N\000". |
| 826 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 827 | ** Both arguments will have the same number of elements. This routine |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 828 | ** returns negative, zero, or positive if the first argument is less |
| 829 | ** than, equal to, or greater than the first. (Result is a-b). |
| 830 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 831 | ** Each element begins with one of the characters "+", "-", "A", "D". |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 832 | ** This character determines the sort order and collating sequence: |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 833 | ** |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 834 | ** + Sort numerically in ascending order |
| 835 | ** - Sort numerically in descending order |
| 836 | ** A Sort as strings in ascending order |
| 837 | ** D Sort as strings in descending order. |
| 838 | ** |
| 839 | ** For the "+" and "-" sorting, pure numeric strings (strings for which the |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 840 | ** isNum() function above returns TRUE) always compare less than strings |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 841 | ** that are not pure numerics. Non-numeric strings compare in memcmp() |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 842 | ** order. This is the same sort order as the sqlite3Compare() function |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 843 | ** above generates. |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 844 | ** |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 845 | ** The last point is a change from version 2.6.3 to version 2.7.0. In |
| 846 | ** version 2.6.3 and earlier, substrings of digits compare in numerical |
| 847 | ** and case was used only to break a tie. |
| 848 | ** |
| 849 | ** Elements that begin with 'A' or 'D' compare in memcmp() order regardless |
| 850 | ** of whether or not they look like a number. |
| 851 | ** |
| 852 | ** Note that the sort order imposed by the rules above is the same |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 853 | ** from the ordering defined by the "<", "<=", ">", and ">=" operators |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 854 | ** of expressions and for indices. This was not the case for version |
| 855 | ** 2.6.3 and earlier. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 856 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 857 | int sqlite3SortCompare(const char *a, const char *b){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 858 | int res = 0; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 859 | int isNumA, isNumB; |
drh | 294fb92 | 2002-09-30 01:31:21 +0000 | [diff] [blame] | 860 | int dir = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 861 | |
| 862 | while( res==0 && *a && *b ){ |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 863 | if( a[0]=='N' || b[0]=='N' ){ |
| 864 | if( a[0]==b[0] ){ |
| 865 | a += 2; |
| 866 | b += 2; |
| 867 | continue; |
| 868 | } |
| 869 | if( a[0]=='N' ){ |
| 870 | dir = b[0]; |
| 871 | res = -1; |
| 872 | }else{ |
| 873 | dir = a[0]; |
| 874 | res = +1; |
| 875 | } |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 878 | assert( a[0]==b[0] ); |
| 879 | if( (dir=a[0])=='A' || a[0]=='D' ){ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 880 | res = strcmp(&a[1],&b[1]); |
| 881 | if( res ) break; |
| 882 | }else{ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 883 | isNumA = sqlite3IsNumber(&a[1], 0, SQLITE_UTF8); |
| 884 | isNumB = sqlite3IsNumber(&b[1], 0, SQLITE_UTF8); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 885 | if( isNumA ){ |
| 886 | double rA, rB; |
| 887 | if( !isNumB ){ |
| 888 | res = -1; |
| 889 | break; |
| 890 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 891 | rA = sqlite3AtoF(&a[1], 0); |
| 892 | rB = sqlite3AtoF(&b[1], 0); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 893 | if( rA<rB ){ |
| 894 | res = -1; |
| 895 | break; |
| 896 | } |
| 897 | if( rA>rB ){ |
| 898 | res = +1; |
| 899 | break; |
| 900 | } |
| 901 | }else if( isNumB ){ |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 902 | res = +1; |
| 903 | break; |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 904 | }else{ |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 905 | res = strcmp(&a[1],&b[1]); |
| 906 | if( res ) break; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 907 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 908 | } |
drh | cab2005 | 2003-04-18 17:45:14 +0000 | [diff] [blame] | 909 | a += strlen(&a[1]) + 2; |
| 910 | b += strlen(&b[1]) + 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 911 | } |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 912 | if( dir=='-' || dir=='D' ) res = -res; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 913 | return res; |
| 914 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 915 | |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 916 | #if 1 /* We are now always UTF-8 */ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 917 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 918 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 919 | ** X so that it points to the next character. This only works right |
| 920 | ** if X points to a well-formed UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 921 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 922 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 923 | #define sqliteCharVal(X) sqlite3ReadUtf8(X) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 924 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 925 | #else /* !defined(SQLITE_UTF8) */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 926 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 927 | ** For iso8859 encoding, the next character is just the next byte. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 928 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 929 | #define sqliteNextChar(X) (++(X)); |
| 930 | #define sqliteCharVal(X) ((int)*(X)) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 931 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 932 | #endif /* defined(SQLITE_UTF8) */ |
| 933 | |
| 934 | |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 935 | #if 1 /* We are now always UTF-8 */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 936 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 937 | ** Convert the UTF-8 character to which z points into a 31-bit |
| 938 | ** UCS character. This only works right if z points to a well-formed |
| 939 | ** UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 940 | */ |
danielk1977 | ad7dd42 | 2004-06-06 12:41:49 +0000 | [diff] [blame] | 941 | int sqlite3ReadUtf8(const unsigned char *z){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 942 | int c; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 943 | static const int initVal[] = { |
| 944 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 945 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 946 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| 947 | 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 948 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, |
| 949 | 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 950 | 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, |
| 951 | 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 952 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, |
| 953 | 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 954 | 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, |
| 955 | 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, |
| 956 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2, |
| 957 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 958 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, |
| 959 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 960 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254, |
| 961 | 255, |
| 962 | }; |
| 963 | c = initVal[*(z++)]; |
| 964 | while( (0xc0&*z)==0x80 ){ |
| 965 | c = (c<<6) | (0x3f&*(z++)); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 966 | } |
| 967 | return c; |
| 968 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 969 | #endif |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 970 | |
| 971 | /* |
| 972 | ** Compare two UTF-8 strings for equality where the first string can |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 973 | ** potentially be a "glob" expression. Return true (1) if they |
| 974 | ** are the same and false (0) if they are different. |
| 975 | ** |
| 976 | ** Globbing rules: |
| 977 | ** |
| 978 | ** '*' Matches any sequence of zero or more characters. |
| 979 | ** |
| 980 | ** '?' Matches exactly one character. |
| 981 | ** |
| 982 | ** [...] Matches one character from the enclosed list of |
| 983 | ** characters. |
| 984 | ** |
| 985 | ** [^...] Matches one character not in the enclosed list. |
| 986 | ** |
| 987 | ** With the [...] and [^...] matching, a ']' character can be included |
| 988 | ** in the list by making it the first character after '[' or '^'. A |
| 989 | ** range of characters can be specified using '-'. Example: |
| 990 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 991 | ** it the last character in the list. |
| 992 | ** |
| 993 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 994 | ** |
| 995 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 996 | ** |
| 997 | ** abc[*]xyz Matches "abc*xyz" only |
| 998 | */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 999 | int |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1000 | sqlite3GlobCompare(const unsigned char *zPattern, const unsigned char *zString){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1001 | register int c; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1002 | int invert; |
| 1003 | int seen; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1004 | int c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1005 | |
| 1006 | while( (c = *zPattern)!=0 ){ |
| 1007 | switch( c ){ |
| 1008 | case '*': |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1009 | while( (c=zPattern[1]) == '*' || c == '?' ){ |
| 1010 | if( c=='?' ){ |
| 1011 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1012 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1013 | } |
| 1014 | zPattern++; |
| 1015 | } |
| 1016 | if( c==0 ) return 1; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1017 | if( c=='[' ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1018 | while( *zString && sqlite3GlobCompare(&zPattern[1],zString)==0 ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1019 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1020 | } |
| 1021 | return *zString!=0; |
| 1022 | }else{ |
| 1023 | while( (c2 = *zString)!=0 ){ |
| 1024 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1025 | if( c2==0 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1026 | if( sqlite3GlobCompare(&zPattern[1],zString) ) return 1; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1027 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1028 | } |
| 1029 | return 0; |
| 1030 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1031 | case '?': { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1032 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1033 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1034 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1035 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1036 | } |
| 1037 | case '[': { |
| 1038 | int prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1039 | seen = 0; |
| 1040 | invert = 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1041 | c = sqliteCharVal(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1042 | if( c==0 ) return 0; |
| 1043 | c2 = *++zPattern; |
| 1044 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 1045 | if( c2==']' ){ |
| 1046 | if( c==']' ) seen = 1; |
| 1047 | c2 = *++zPattern; |
| 1048 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1049 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1050 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 1051 | zPattern++; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1052 | c2 = sqliteCharVal(zPattern); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1053 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 1054 | prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1055 | }else if( c==c2 ){ |
| 1056 | seen = 1; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1057 | prior_c = c2; |
| 1058 | }else{ |
| 1059 | prior_c = c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1060 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1061 | sqliteNextChar(zPattern); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1062 | } |
| 1063 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1064 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1065 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1066 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1067 | } |
| 1068 | default: { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1069 | if( c != *zString ) return 0; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1070 | zPattern++; |
| 1071 | zString++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1072 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 1073 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1074 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1075 | } |
| 1076 | return *zString==0; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1080 | ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. |
| 1081 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN |
| 1082 | ** when this routine is called. |
| 1083 | ** |
| 1084 | ** This routine is a attempt to detect if two threads use the |
| 1085 | ** same sqlite* pointer at the same time. There is a race |
| 1086 | ** condition so it is possible that the error is not detected. |
| 1087 | ** But usually the problem will be seen. The result will be an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1088 | ** error which can be used to debug the application that is |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1089 | ** using SQLite incorrectly. |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1090 | ** |
| 1091 | ** Ticket #202: If db->magic is not a valid open value, take care not |
| 1092 | ** to modify the db structure at all. It could be that db is a stale |
| 1093 | ** pointer. In other words, it could be that there has been a prior |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1094 | ** call to sqlite3_close(db) and db has been deallocated. And we do |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1095 | ** not want to write into deallocated memory. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1096 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1097 | int sqlite3SafetyOn(sqlite *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1098 | if( db->magic==SQLITE_MAGIC_OPEN ){ |
| 1099 | db->magic = SQLITE_MAGIC_BUSY; |
| 1100 | return 0; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1101 | }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR |
| 1102 | || db->want_to_close ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1103 | db->magic = SQLITE_MAGIC_ERROR; |
| 1104 | db->flags |= SQLITE_Interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1105 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1106 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. |
| 1111 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY |
| 1112 | ** when this routine is called. |
| 1113 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1114 | int sqlite3SafetyOff(sqlite *db){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1115 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 1116 | db->magic = SQLITE_MAGIC_OPEN; |
| 1117 | return 0; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1118 | }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR |
| 1119 | || db->want_to_close ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1120 | db->magic = SQLITE_MAGIC_ERROR; |
| 1121 | db->flags |= SQLITE_Interrupt; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1122 | } |
drh | e7e8bc7 | 2002-12-17 13:05:25 +0000 | [diff] [blame] | 1123 | return 1; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1127 | ** Check to make sure we are not currently executing an sqlite3_exec(). |
| 1128 | ** If we are currently in an sqlite3_exec(), return true and set |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1129 | ** sqlite.magic to SQLITE_MAGIC_ERROR. This will cause a complete |
| 1130 | ** shutdown of the database. |
| 1131 | ** |
| 1132 | ** This routine is used to try to detect when API routines are called |
| 1133 | ** at the wrong time or in the wrong sequence. |
| 1134 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1135 | int sqlite3SafetyCheck(sqlite *db){ |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 1136 | if( db->pVdbe!=0 ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1137 | db->magic = SQLITE_MAGIC_ERROR; |
| 1138 | return 1; |
| 1139 | } |
| 1140 | return 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1141 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1142 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1143 | /* |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 1144 | ** The variable-length integer encoding is as follows: |
| 1145 | ** |
| 1146 | ** KEY: |
| 1147 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 1148 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 1149 | ** C = xxxxxxxx 8 bits of data |
| 1150 | ** |
| 1151 | ** 7 bits - A |
| 1152 | ** 14 bits - BA |
| 1153 | ** 21 bits - BBA |
| 1154 | ** 28 bits - BBBA |
| 1155 | ** 35 bits - BBBBA |
| 1156 | ** 42 bits - BBBBBA |
| 1157 | ** 49 bits - BBBBBBA |
| 1158 | ** 56 bits - BBBBBBBA |
| 1159 | ** 64 bits - BBBBBBBBC |
| 1160 | */ |
| 1161 | |
| 1162 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1163 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 1164 | ** The length of data write will be between 1 and 9 bytes. The number |
| 1165 | ** of bytes written is returned. |
| 1166 | ** |
| 1167 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 1168 | ** 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] | 1169 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 1170 | ** 8 bits and is the last byte. |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1171 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1172 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1173 | int i, j, n; |
| 1174 | u8 buf[10]; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 1175 | if( v & 0xff00000000000000 ){ |
| 1176 | p[8] = v; |
| 1177 | v >>= 8; |
| 1178 | for(i=7; i>=0; i--){ |
| 1179 | p[i] = (v & 0x7f) | 0x80; |
| 1180 | v >>= 7; |
| 1181 | } |
| 1182 | return 9; |
| 1183 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1184 | n = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1185 | do{ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1186 | buf[n++] = (v & 0x7f) | 0x80; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1187 | v >>= 7; |
| 1188 | }while( v!=0 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1189 | buf[0] &= 0x7f; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 1190 | assert( n<=9 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1191 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 1192 | p[i] = buf[j]; |
| 1193 | } |
| 1194 | return n; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1195 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1196 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 1199 | ** Return the number of bytes read. The value is stored in *v. |
| 1200 | */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1201 | int sqlite3GetVarint(const unsigned char *p, u64 *v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1202 | u32 x; |
| 1203 | u64 x64; |
| 1204 | int n; |
| 1205 | unsigned char c; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1206 | if( ((c = p[0]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1207 | *v = c; |
| 1208 | return 1; |
| 1209 | } |
| 1210 | x = c & 0x7f; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1211 | if( ((c = p[1]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1212 | *v = (x<<7) | c; |
| 1213 | return 2; |
| 1214 | } |
| 1215 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1216 | if( ((c = p[2]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1217 | *v = (x<<7) | c; |
| 1218 | return 3; |
| 1219 | } |
| 1220 | x = (x<<7) | (c&0x7f); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1221 | if( ((c = p[3]) & 0x80)==0 ){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1222 | *v = (x<<7) | c; |
| 1223 | return 4; |
| 1224 | } |
| 1225 | x64 = (x<<7) | (c&0x7f); |
| 1226 | n = 4; |
| 1227 | do{ |
| 1228 | c = p[n++]; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 1229 | if( n==9 ){ |
| 1230 | x64 = (x64<<8) | c; |
| 1231 | break; |
| 1232 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1233 | x64 = (x64<<7) | (c&0x7f); |
| 1234 | }while( (c & 0x80)!=0 ); |
| 1235 | *v = x64; |
| 1236 | return n; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 1241 | ** Return the number of bytes read. The value is stored in *v. |
| 1242 | */ |
| 1243 | int sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1244 | u32 x; |
| 1245 | int n; |
| 1246 | unsigned char c; |
| 1247 | if( ((c = p[0]) & 0x80)==0 ){ |
| 1248 | *v = c; |
| 1249 | return 1; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1250 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1251 | x = c & 0x7f; |
| 1252 | if( ((c = p[1]) & 0x80)==0 ){ |
| 1253 | *v = (x<<7) | c; |
| 1254 | return 2; |
| 1255 | } |
| 1256 | x = (x<<7) | (c&0x7f); |
| 1257 | if( ((c = p[2]) & 0x80)==0 ){ |
| 1258 | *v = (x<<7) | c; |
| 1259 | return 3; |
| 1260 | } |
| 1261 | x = (x<<7) | (c&0x7f); |
| 1262 | if( ((c = p[3]) & 0x80)==0 ){ |
| 1263 | *v = (x<<7) | c; |
| 1264 | return 4; |
| 1265 | } |
| 1266 | n = 4; |
| 1267 | do{ |
| 1268 | x = (x<<7) | ((c = p[n++])&0x7f); |
| 1269 | }while( (c & 0x80)!=0 && n<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1270 | *v = x; |
| 1271 | return n; |
| 1272 | } |
| 1273 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 1274 | /* |
| 1275 | ** Return the number of bytes that will be needed to store the given |
| 1276 | ** 64-bit integer. |
| 1277 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1278 | int sqlite3VarintLen(u64 v){ |
| 1279 | int i = 0; |
| 1280 | do{ |
| 1281 | i++; |
| 1282 | v >>= 7; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 1283 | }while( v!=0 && i<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1284 | return i; |
| 1285 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1286 | |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1287 | void *sqlite3HexToBlob(const char *z){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1288 | char *zBlob; |
| 1289 | int i; |
| 1290 | int n = strlen(z); |
| 1291 | if( n%2 ) return 0; |
| 1292 | |
| 1293 | zBlob = (char *)sqliteMalloc(n/2); |
| 1294 | |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 1295 | for(i=0; i<n; i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1296 | u8 c; |
| 1297 | |
| 1298 | if ( z[i]>47 && z[i]<58 ) c = (z[i]-48)<<4; |
| 1299 | else if( z[i]>64 && z[i]<71 ) c = (z[i]-55)<<4; |
| 1300 | else if( z[i]>96 && z[i]<103 ) c = (z[i]-87)<<4; |
| 1301 | else { |
| 1302 | sqliteFree(zBlob); |
| 1303 | return 0; |
| 1304 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 1305 | i++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1306 | if ( z[i]>47 && z[i]<58 ) c += (z[i]-48); |
| 1307 | else if( z[i]>64 && z[i]<71 ) c += (z[i]-55); |
| 1308 | else if( z[i]>96 && z[i]<103 ) c += (z[i]-87); |
| 1309 | else { |
| 1310 | sqliteFree(zBlob); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | zBlob[i/2] = c; |
| 1315 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 1316 | return zBlob; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1317 | } |