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