drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Utility functions used throughout sqlite. |
| 13 | ** |
| 14 | ** This file contains functions for allocating memory, comparing |
| 15 | ** strings, and stuff like that. |
| 16 | ** |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.234 2008/07/08 14:52:10 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | #include <stdarg.h> |
| 21 | #include <ctype.h> |
| 22 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 25 | ** Return true if the floating point value is Not a Number. |
| 26 | */ |
| 27 | int sqlite3IsNaN(double x){ |
drh | 8a54f9f | 2008-05-09 13:47:58 +0000 | [diff] [blame] | 28 | /* This NaN test sometimes fails if compiled on GCC with -ffast-math. |
| 29 | ** On the other hand, the use of -ffast-math comes with the following |
| 30 | ** warning: |
| 31 | ** |
| 32 | ** This option [-ffast-math] should never be turned on by any |
| 33 | ** -O option since it can result in incorrect output for programs |
| 34 | ** which depend on an exact implementation of IEEE or ISO |
| 35 | ** rules/specifications for math functions. |
| 36 | */ |
drh | 8c4d5b2 | 2008-07-06 00:21:35 +0000 | [diff] [blame] | 37 | #ifdef __FAST_MATH__ |
| 38 | # error SQLite will not work correctly with the -ffast-math option of GCC. |
| 39 | #endif |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 40 | volatile double y = x; |
| 41 | return x!=y; |
| 42 | } |
| 43 | |
| 44 | /* |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame^] | 45 | ** Return the length of a string, except do not allow the string length |
| 46 | ** to exceed the SQLITE_LIMIT_LENGTH setting. |
| 47 | */ |
| 48 | int sqlite3Strlen(sqlite3 *db, const char *z){ |
| 49 | const char *z2 = z; |
| 50 | while( *z2 ){ z2++; } |
| 51 | if( z2 > &z[db->aLimit[SQLITE_LIMIT_LENGTH]] ){ |
| 52 | return db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 53 | }else{ |
| 54 | return (int)(z2 - z); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 59 | ** Set the most recent error code and error string for the sqlite |
| 60 | ** handle "db". The error code is set to "err_code". |
| 61 | ** |
| 62 | ** If it is not NULL, string zFormat specifies the format of the |
| 63 | ** error string in the style of the printf functions: The following |
| 64 | ** format characters are allowed: |
| 65 | ** |
| 66 | ** %s Insert a string |
| 67 | ** %z A string that should be freed after use |
| 68 | ** %d Insert an integer |
| 69 | ** %T Insert a token |
| 70 | ** %S Insert the first element of a SrcList |
| 71 | ** |
| 72 | ** zFormat and any string tokens that follow it are assumed to be |
| 73 | ** encoded in UTF-8. |
| 74 | ** |
danielk1977 | 0bb8f36 | 2005-05-23 13:00:57 +0000 | [diff] [blame] | 75 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 76 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 77 | ** to NULL. |
| 78 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 79 | void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 80 | if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 81 | db->errCode = err_code; |
| 82 | if( zFormat ){ |
| 83 | char *z; |
| 84 | va_list ap; |
| 85 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 86 | z = sqlite3VMPrintf(db, zFormat, ap); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 87 | va_end(ap); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 88 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 89 | }else{ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 90 | sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 91 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 96 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 97 | ** The following formatting characters are allowed: |
| 98 | ** |
| 99 | ** %s Insert a string |
| 100 | ** %z A string that should be freed after use |
| 101 | ** %d Insert an integer |
| 102 | ** %T Insert a token |
| 103 | ** %S Insert the first element of a SrcList |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 104 | ** |
| 105 | ** This function should be used to report any error that occurs whilst |
| 106 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 107 | ** last thing the sqlite3_prepare() function does is copy the error |
| 108 | ** stored by this function into the database handle using sqlite3Error(). |
| 109 | ** Function sqlite3Error() should be used during statement execution |
| 110 | ** (sqlite3_step() etc.). |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 111 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 112 | void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 113 | va_list ap; |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 114 | pParse->nErr++; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 115 | sqlite3_free(pParse->zErrMsg); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 116 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 117 | pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap); |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 118 | va_end(ap); |
drh | 7e326c0 | 2007-05-15 16:51:37 +0000 | [diff] [blame] | 119 | if( pParse->rc==SQLITE_OK ){ |
| 120 | pParse->rc = SQLITE_ERROR; |
| 121 | } |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 125 | ** Clear the error message in pParse, if any |
| 126 | */ |
| 127 | void sqlite3ErrorClear(Parse *pParse){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 128 | sqlite3_free(pParse->zErrMsg); |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 129 | pParse->zErrMsg = 0; |
| 130 | pParse->nErr = 0; |
| 131 | } |
| 132 | |
| 133 | /* |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 134 | ** Convert an SQL-style quoted string into a normal string by removing |
| 135 | ** the quote characters. The conversion is done in-place. If the |
| 136 | ** input does not begin with a quote character, then this routine |
| 137 | ** is a no-op. |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 138 | ** |
| 139 | ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 140 | ** brackets from around identifers. For example: "[a-b-c]" becomes |
| 141 | ** "a-b-c". |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 142 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 143 | void sqlite3Dequote(char *z){ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 144 | int quote; |
| 145 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 146 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 147 | quote = z[0]; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 148 | switch( quote ){ |
| 149 | case '\'': break; |
| 150 | case '"': break; |
drh | 3d94662 | 2005-08-13 18:15:42 +0000 | [diff] [blame] | 151 | case '`': break; /* For MySQL compatibility */ |
| 152 | case '[': quote = ']'; break; /* For MS SqlServer compatibility */ |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 153 | default: return; |
| 154 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 155 | for(i=1, j=0; z[i]; i++){ |
| 156 | if( z[i]==quote ){ |
| 157 | if( z[i+1]==quote ){ |
| 158 | z[j++] = quote; |
| 159 | i++; |
| 160 | }else{ |
| 161 | z[j++] = 0; |
| 162 | break; |
| 163 | } |
| 164 | }else{ |
| 165 | z[j++] = z[i]; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 170 | /* Convenient short-hand */ |
drh | 4e5ffc5 | 2004-08-31 00:52:37 +0000 | [diff] [blame] | 171 | #define UpperToLower sqlite3UpperToLower |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 174 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 175 | ** there is no consistency, we will define our own. |
| 176 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 177 | int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 178 | register unsigned char *a, *b; |
| 179 | a = (unsigned char *)zLeft; |
| 180 | b = (unsigned char *)zRight; |
| 181 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
drh | 2b73501 | 2004-07-15 13:23:21 +0000 | [diff] [blame] | 182 | return UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 183 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 184 | int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 185 | register unsigned char *a, *b; |
| 186 | a = (unsigned char *)zLeft; |
| 187 | b = (unsigned char *)zRight; |
| 188 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 189 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 192 | /* |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 193 | ** Return TRUE if z is a pure numeric string. Return FALSE if the |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 194 | ** string contains any character which is not part of a number. If |
| 195 | ** the string is numeric and contains the '.' character, set *realnum |
| 196 | ** to TRUE (otherwise FALSE). |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 197 | ** |
drh | 873cdcb | 2004-09-05 23:23:41 +0000 | [diff] [blame] | 198 | ** An empty string is considered non-numeric. |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 199 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 200 | int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 201 | int incr = (enc==SQLITE_UTF8?1:2); |
danielk1977 | 93cd039 | 2004-06-30 02:35:51 +0000 | [diff] [blame] | 202 | if( enc==SQLITE_UTF16BE ) z++; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 203 | if( *z=='-' || *z=='+' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 204 | if( !isdigit(*(u8*)z) ){ |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 205 | return 0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 206 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 207 | z += incr; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 208 | if( realnum ) *realnum = 0; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 209 | while( isdigit(*(u8*)z) ){ z += incr; } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 210 | if( *z=='.' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 211 | z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 212 | if( !isdigit(*(u8*)z) ) return 0; |
| 213 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 214 | if( realnum ) *realnum = 1; |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 215 | } |
| 216 | if( *z=='e' || *z=='E' ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 217 | z += incr; |
| 218 | if( *z=='+' || *z=='-' ) z += incr; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 219 | if( !isdigit(*(u8*)z) ) return 0; |
| 220 | while( isdigit(*(u8*)z) ){ z += incr; } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 221 | if( realnum ) *realnum = 1; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 222 | } |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 223 | return *z==0; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 224 | } |
| 225 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 226 | /* |
| 227 | ** The string z[] is an ascii representation of a real number. |
| 228 | ** Convert this string to a double. |
| 229 | ** |
| 230 | ** This routine assumes that z[] really is a valid number. If it |
| 231 | ** is not, the result is undefined. |
| 232 | ** |
| 233 | ** This routine is used instead of the library atof() function because |
| 234 | ** the library atof() might want to use "," as the decimal point instead |
| 235 | ** of "." depending on how locale is set. But that would cause problems |
| 236 | ** for SQL. So this routine always uses "." regardless of locale. |
| 237 | */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 238 | int sqlite3AtoF(const char *z, double *pResult){ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 239 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 240 | int sign = 1; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 241 | const char *zBegin = z; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 242 | LONGDOUBLE_TYPE v1 = 0.0; |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 243 | int nSignificant = 0; |
danielk1977 | 2be2be9 | 2007-05-16 17:50:45 +0000 | [diff] [blame] | 244 | while( isspace(*(u8*)z) ) z++; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 245 | if( *z=='-' ){ |
| 246 | sign = -1; |
| 247 | z++; |
| 248 | }else if( *z=='+' ){ |
| 249 | z++; |
| 250 | } |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 251 | while( z[0]=='0' ){ |
| 252 | z++; |
| 253 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 254 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 255 | v1 = v1*10.0 + (*z - '0'); |
| 256 | z++; |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 257 | nSignificant++; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 258 | } |
| 259 | if( *z=='.' ){ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 260 | LONGDOUBLE_TYPE divisor = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 261 | z++; |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 262 | if( nSignificant==0 ){ |
| 263 | while( z[0]=='0' ){ |
| 264 | divisor *= 10.0; |
| 265 | z++; |
| 266 | } |
| 267 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 268 | while( isdigit(*(u8*)z) ){ |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 269 | if( nSignificant<18 ){ |
| 270 | v1 = v1*10.0 + (*z - '0'); |
| 271 | divisor *= 10.0; |
| 272 | nSignificant++; |
| 273 | } |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 274 | z++; |
| 275 | } |
| 276 | v1 /= divisor; |
| 277 | } |
| 278 | if( *z=='e' || *z=='E' ){ |
| 279 | int esign = 1; |
| 280 | int eval = 0; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 281 | LONGDOUBLE_TYPE scale = 1.0; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 282 | z++; |
| 283 | if( *z=='-' ){ |
| 284 | esign = -1; |
| 285 | z++; |
| 286 | }else if( *z=='+' ){ |
| 287 | z++; |
| 288 | } |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 289 | while( isdigit(*(u8*)z) ){ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 290 | eval = eval*10 + *z - '0'; |
| 291 | z++; |
| 292 | } |
| 293 | while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } |
| 294 | while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } |
| 295 | while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } |
| 296 | while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } |
| 297 | if( esign<0 ){ |
| 298 | v1 /= scale; |
| 299 | }else{ |
| 300 | v1 *= scale; |
| 301 | } |
| 302 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 303 | *pResult = sign<0 ? -v1 : v1; |
| 304 | return z - zBegin; |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 305 | #else |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 306 | return sqlite3Atoi64(z, pResult); |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 307 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 308 | } |
| 309 | |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 310 | /* |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 311 | ** Compare the 19-character string zNum against the text representation |
| 312 | ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| 313 | ** if zNum is less than, equal to, or greater than the string. |
| 314 | ** |
| 315 | ** Unlike memcmp() this routine is guaranteed to return the difference |
| 316 | ** in the values of the last digit if the only difference is in the |
| 317 | ** last digit. So, for example, |
| 318 | ** |
| 319 | ** compare2pow63("9223372036854775800") |
| 320 | ** |
| 321 | ** will return -8. |
| 322 | */ |
| 323 | static int compare2pow63(const char *zNum){ |
| 324 | int c; |
| 325 | c = memcmp(zNum,"922337203685477580",18); |
| 326 | if( c==0 ){ |
| 327 | c = zNum[18] - '8'; |
| 328 | } |
| 329 | return c; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 334 | ** Return TRUE if zNum is a 64-bit signed integer and write |
| 335 | ** the value of the integer into *pNum. If zNum is not an integer |
| 336 | ** or is an integer that is too large to be expressed with 64 bits, |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 337 | ** then return false. |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 338 | ** |
| 339 | ** When this routine was originally written it dealt with only |
| 340 | ** 32-bit numbers. At that time, it was much faster than the |
| 341 | ** atoi() library routine in RedHat 7.2. |
| 342 | */ |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 343 | int sqlite3Atoi64(const char *zNum, i64 *pNum){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 344 | i64 v = 0; |
| 345 | int neg; |
| 346 | int i, c; |
drh | 4b81592 | 2008-05-19 15:54:59 +0000 | [diff] [blame] | 347 | const char *zStart; |
danielk1977 | 2be2be9 | 2007-05-16 17:50:45 +0000 | [diff] [blame] | 348 | while( isspace(*(u8*)zNum) ) zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 349 | if( *zNum=='-' ){ |
| 350 | neg = 1; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 351 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 352 | }else if( *zNum=='+' ){ |
| 353 | neg = 0; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 354 | zNum++; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 355 | }else{ |
| 356 | neg = 0; |
| 357 | } |
drh | 4b81592 | 2008-05-19 15:54:59 +0000 | [diff] [blame] | 358 | zStart = zNum; |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 359 | while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 360 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 361 | v = v*10 + c - '0'; |
| 362 | } |
| 363 | *pNum = neg ? -v : v; |
drh | 4b81592 | 2008-05-19 15:54:59 +0000 | [diff] [blame] | 364 | if( c!=0 || (i==0 && zStart==zNum) || i>19 ){ |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 365 | /* zNum is empty or contains non-numeric text or is longer |
| 366 | ** than 19 digits (thus guaranting that it is too large) */ |
| 367 | return 0; |
| 368 | }else if( i<19 ){ |
| 369 | /* Less than 19 digits, so we know that it fits in 64 bits */ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 370 | return 1; |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 371 | }else{ |
| 372 | /* 19-digit numbers must be no larger than 9223372036854775807 if positive |
| 373 | ** or 9223372036854775808 if negative. Note that 9223372036854665808 |
| 374 | ** is 2^63. */ |
| 375 | return compare2pow63(zNum)<neg; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 376 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
| 380 | ** The string zNum represents an integer. There might be some other |
| 381 | ** information following the integer too, but that part is ignored. |
| 382 | ** If the integer that the prefix of zNum represents will fit in a |
| 383 | ** 64-bit signed integer, return TRUE. Otherwise return FALSE. |
| 384 | ** |
| 385 | ** This routine returns FALSE for the string -9223372036854775808 even that |
| 386 | ** that number will, in theory fit in a 64-bit integer. Positive |
| 387 | ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return |
| 388 | ** false. |
| 389 | */ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 390 | int sqlite3FitsIn64Bits(const char *zNum, int negFlag){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 391 | int i, c; |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 392 | int neg = 0; |
| 393 | if( *zNum=='-' ){ |
| 394 | neg = 1; |
| 395 | zNum++; |
| 396 | }else if( *zNum=='+' ){ |
| 397 | zNum++; |
| 398 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 399 | if( negFlag ) neg = 1-neg; |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 400 | while( *zNum=='0' ){ |
| 401 | zNum++; /* Skip leading zeros. Ticket #2454 */ |
| 402 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 403 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 404 | if( i<19 ){ |
| 405 | /* Guaranteed to fit if less than 19 digits */ |
| 406 | return 1; |
| 407 | }else if( i>19 ){ |
| 408 | /* Guaranteed to be too big if greater than 19 digits */ |
| 409 | return 0; |
| 410 | }else{ |
| 411 | /* Compare against 2^63. */ |
| 412 | return compare2pow63(zNum)<neg; |
| 413 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 414 | } |
| 415 | |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 416 | /* |
| 417 | ** If zNum represents an integer that will fit in 32-bits, then set |
| 418 | ** *pValue to that integer and return true. Otherwise return false. |
| 419 | ** |
| 420 | ** Any non-numeric characters that following zNum are ignored. |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 421 | ** This is different from sqlite3Atoi64() which requires the |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 422 | ** input number to be zero-terminated. |
| 423 | */ |
| 424 | int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 425 | sqlite_int64 v = 0; |
| 426 | int i, c; |
| 427 | int neg = 0; |
| 428 | if( zNum[0]=='-' ){ |
| 429 | neg = 1; |
| 430 | zNum++; |
| 431 | }else if( zNum[0]=='+' ){ |
| 432 | zNum++; |
| 433 | } |
| 434 | while( zNum[0]=='0' ) zNum++; |
danielk1977 | b8cdbec | 2007-09-01 10:01:12 +0000 | [diff] [blame] | 435 | for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 436 | v = v*10 + c; |
| 437 | } |
danielk1977 | b8cdbec | 2007-09-01 10:01:12 +0000 | [diff] [blame] | 438 | |
| 439 | /* The longest decimal representation of a 32 bit integer is 10 digits: |
| 440 | ** |
| 441 | ** 1234567890 |
| 442 | ** 2^31 -> 2147483648 |
| 443 | */ |
| 444 | if( i>10 ){ |
drh | 217f490 | 2007-06-25 17:28:00 +0000 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | if( v-neg>2147483647 ){ |
| 448 | return 0; |
| 449 | } |
| 450 | if( neg ){ |
| 451 | v = -v; |
| 452 | } |
| 453 | *pValue = (int)v; |
| 454 | return 1; |
| 455 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 456 | |
| 457 | /* |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 458 | ** The variable-length integer encoding is as follows: |
| 459 | ** |
| 460 | ** KEY: |
| 461 | ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 462 | ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 463 | ** C = xxxxxxxx 8 bits of data |
| 464 | ** |
| 465 | ** 7 bits - A |
| 466 | ** 14 bits - BA |
| 467 | ** 21 bits - BBA |
| 468 | ** 28 bits - BBBA |
| 469 | ** 35 bits - BBBBA |
| 470 | ** 42 bits - BBBBBA |
| 471 | ** 49 bits - BBBBBBA |
| 472 | ** 56 bits - BBBBBBBA |
| 473 | ** 64 bits - BBBBBBBBC |
| 474 | */ |
| 475 | |
| 476 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 477 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 478 | ** The length of data write will be between 1 and 9 bytes. The number |
| 479 | ** of bytes written is returned. |
| 480 | ** |
| 481 | ** A variable-length integer consists of the lower 7 bits of each byte |
| 482 | ** 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] | 483 | ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 484 | ** 8 bits and is the last byte. |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 485 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 486 | int sqlite3PutVarint(unsigned char *p, u64 v){ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 487 | int i, j, n; |
| 488 | u8 buf[10]; |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 489 | if( v & (((u64)0xff000000)<<32) ){ |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 490 | p[8] = v; |
| 491 | v >>= 8; |
| 492 | for(i=7; i>=0; i--){ |
| 493 | p[i] = (v & 0x7f) | 0x80; |
| 494 | v >>= 7; |
| 495 | } |
| 496 | return 9; |
| 497 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 498 | n = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 499 | do{ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 500 | buf[n++] = (v & 0x7f) | 0x80; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 501 | v >>= 7; |
| 502 | }while( v!=0 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 503 | buf[0] &= 0x7f; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 504 | assert( n<=9 ); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 505 | for(i=0, j=n-1; j>=0; j--, i++){ |
| 506 | p[i] = buf[j]; |
| 507 | } |
| 508 | return n; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 509 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 511 | /* |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 512 | ** This routine is a faster version of sqlite3PutVarint() that only |
| 513 | ** works for 32-bit positive integers and which is optimized for |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 514 | ** the common case of small integers. A MACRO version, putVarint32, |
| 515 | ** is provided which inlines the single-byte case. All code should use |
| 516 | ** the MACRO version as this function assumes the single-byte case has |
| 517 | ** already been handled. |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 518 | */ |
| 519 | int sqlite3PutVarint32(unsigned char *p, u32 v){ |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 520 | #ifndef putVarint32 |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 521 | if( (v & ~0x7f)==0 ){ |
| 522 | p[0] = v; |
| 523 | return 1; |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 524 | } |
| 525 | #endif |
| 526 | if( (v & ~0x3fff)==0 ){ |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 527 | p[0] = (v>>7) | 0x80; |
| 528 | p[1] = v & 0x7f; |
| 529 | return 2; |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 530 | } |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 531 | return sqlite3PutVarint(p, v); |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 535 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 536 | ** Return the number of bytes read. The value is stored in *v. |
| 537 | */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 538 | int sqlite3GetVarint(const unsigned char *p, u64 *v){ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 539 | u32 a,b,s; |
| 540 | |
| 541 | a = *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 542 | /* a: p0 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 543 | if (!(a&0x80)) |
| 544 | { |
| 545 | *v = a; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 546 | return 1; |
| 547 | } |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 548 | |
| 549 | p++; |
| 550 | b = *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 551 | /* b: p1 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 552 | if (!(b&0x80)) |
| 553 | { |
| 554 | a &= 0x7f; |
| 555 | a = a<<7; |
| 556 | a |= b; |
| 557 | *v = a; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 558 | return 2; |
| 559 | } |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 560 | |
| 561 | p++; |
| 562 | a = a<<14; |
| 563 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 564 | /* a: p0<<14 | p2 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 565 | if (!(a&0x80)) |
| 566 | { |
| 567 | a &= (0x7f<<14)|(0x7f); |
| 568 | b &= 0x7f; |
| 569 | b = b<<7; |
| 570 | a |= b; |
| 571 | *v = a; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 572 | return 3; |
| 573 | } |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 574 | |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 575 | /* CSE1 from below */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 576 | a &= (0x7f<<14)|(0x7f); |
| 577 | p++; |
| 578 | b = b<<14; |
| 579 | b |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 580 | /* b: p1<<14 | p3 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 581 | if (!(b&0x80)) |
| 582 | { |
| 583 | b &= (0x7f<<14)|(0x7f); |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 584 | /* moved CSE1 up */ |
| 585 | /* a &= (0x7f<<14)|(0x7f); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 586 | a = a<<7; |
| 587 | a |= b; |
| 588 | *v = a; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 589 | return 4; |
| 590 | } |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 591 | |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 592 | /* a: p0<<14 | p2 (masked) */ |
| 593 | /* b: p1<<14 | p3 (unmasked) */ |
| 594 | /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 595 | /* moved CSE1 up */ |
| 596 | /* a &= (0x7f<<14)|(0x7f); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 597 | b &= (0x7f<<14)|(0x7f); |
| 598 | s = a; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 599 | /* s: p0<<14 | p2 (masked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 600 | |
| 601 | p++; |
| 602 | a = a<<14; |
| 603 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 604 | /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 605 | if (!(a&0x80)) |
| 606 | { |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 607 | /* we can skip these cause they were (effectively) done above in calc'ing s */ |
| 608 | /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 609 | /* b &= (0x7f<<14)|(0x7f); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 610 | b = b<<7; |
| 611 | a |= b; |
| 612 | s = s>>18; |
| 613 | *v = ((u64)s)<<32 | a; |
| 614 | return 5; |
| 615 | } |
| 616 | |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 617 | /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 618 | s = s<<7; |
| 619 | s |= b; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 620 | /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 621 | |
| 622 | p++; |
| 623 | b = b<<14; |
| 624 | b |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 625 | /* b: p1<<28 | p3<<14 | p5 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 626 | if (!(b&0x80)) |
| 627 | { |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 628 | /* we can skip this cause it was (effectively) done above in calc'ing s */ |
| 629 | /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 630 | a &= (0x7f<<14)|(0x7f); |
| 631 | a = a<<7; |
| 632 | a |= b; |
| 633 | s = s>>18; |
| 634 | *v = ((u64)s)<<32 | a; |
| 635 | return 6; |
| 636 | } |
| 637 | |
| 638 | p++; |
| 639 | a = a<<14; |
| 640 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 641 | /* a: p2<<28 | p4<<14 | p6 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 642 | if (!(a&0x80)) |
| 643 | { |
| 644 | a &= (0x7f<<28)|(0x7f<<14)|(0x7f); |
| 645 | b &= (0x7f<<14)|(0x7f); |
| 646 | b = b<<7; |
| 647 | a |= b; |
| 648 | s = s>>11; |
| 649 | *v = ((u64)s)<<32 | a; |
| 650 | return 7; |
| 651 | } |
| 652 | |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 653 | /* CSE2 from below */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 654 | a &= (0x7f<<14)|(0x7f); |
| 655 | p++; |
| 656 | b = b<<14; |
| 657 | b |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 658 | /* b: p3<<28 | p5<<14 | p7 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 659 | if (!(b&0x80)) |
| 660 | { |
| 661 | b &= (0x7f<<28)|(0x7f<<14)|(0x7f); |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 662 | /* moved CSE2 up */ |
| 663 | /* a &= (0x7f<<14)|(0x7f); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 664 | a = a<<7; |
| 665 | a |= b; |
| 666 | s = s>>4; |
| 667 | *v = ((u64)s)<<32 | a; |
| 668 | return 8; |
| 669 | } |
| 670 | |
| 671 | p++; |
| 672 | a = a<<15; |
| 673 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 674 | /* a: p4<<29 | p6<<15 | p8 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 675 | |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 676 | /* moved CSE2 up */ |
| 677 | /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 678 | b &= (0x7f<<14)|(0x7f); |
| 679 | b = b<<8; |
| 680 | a |= b; |
| 681 | |
| 682 | s = s<<4; |
| 683 | b = p[-4]; |
| 684 | b &= 0x7f; |
| 685 | b = b>>3; |
| 686 | s |= b; |
| 687 | |
| 688 | *v = ((u64)s)<<32 | a; |
| 689 | |
| 690 | return 9; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 695 | ** Return the number of bytes read. The value is stored in *v. |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 696 | ** A MACRO version, getVarint32, is provided which inlines the |
| 697 | ** single-byte case. All code should use the MACRO version as |
| 698 | ** this function assumes the single-byte case has already been handled. |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 699 | */ |
| 700 | int sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 701 | u32 a,b; |
| 702 | |
| 703 | a = *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 704 | /* a: p0 (unmasked) */ |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 705 | #ifndef getVarint32 |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 706 | if (!(a&0x80)) |
| 707 | { |
| 708 | *v = a; |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 709 | return 1; |
| 710 | } |
shane | 952856a | 2008-04-28 17:41:30 +0000 | [diff] [blame] | 711 | #endif |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 712 | |
| 713 | p++; |
| 714 | b = *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 715 | /* b: p1 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 716 | if (!(b&0x80)) |
| 717 | { |
| 718 | a &= 0x7f; |
| 719 | a = a<<7; |
| 720 | *v = a | b; |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 721 | return 2; |
| 722 | } |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 723 | |
| 724 | p++; |
| 725 | a = a<<14; |
| 726 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 727 | /* a: p0<<14 | p2 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 728 | if (!(a&0x80)) |
| 729 | { |
| 730 | a &= (0x7f<<14)|(0x7f); |
| 731 | b &= 0x7f; |
| 732 | b = b<<7; |
| 733 | *v = a | b; |
| 734 | return 3; |
| 735 | } |
| 736 | |
| 737 | p++; |
| 738 | b = b<<14; |
| 739 | b |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 740 | /* b: p1<<14 | p3 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 741 | if (!(b&0x80)) |
| 742 | { |
| 743 | b &= (0x7f<<14)|(0x7f); |
| 744 | a &= (0x7f<<14)|(0x7f); |
| 745 | a = a<<7; |
| 746 | *v = a | b; |
| 747 | return 4; |
| 748 | } |
| 749 | |
| 750 | p++; |
| 751 | a = a<<14; |
| 752 | a |= *p; |
shane | 022e8af | 2008-06-12 02:16:44 +0000 | [diff] [blame] | 753 | /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 754 | if (!(a&0x80)) |
| 755 | { |
| 756 | a &= (0x7f<<28)|(0x7f<<14)|(0x7f); |
| 757 | b &= (0x7f<<28)|(0x7f<<14)|(0x7f); |
| 758 | b = b<<7; |
| 759 | *v = a | b; |
| 760 | return 5; |
| 761 | } |
| 762 | |
drh | cec3e3e | 2008-05-13 16:41:50 +0000 | [diff] [blame] | 763 | /* We can only reach this point when reading a corrupt database |
| 764 | ** file. In that case we are not in any hurry. Use the (relatively |
| 765 | ** slow) general-purpose sqlite3GetVarint() routine to extract the |
| 766 | ** value. */ |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 767 | { |
drh | cec3e3e | 2008-05-13 16:41:50 +0000 | [diff] [blame] | 768 | u64 v64; |
| 769 | int n; |
| 770 | |
| 771 | p -= 4; |
| 772 | n = sqlite3GetVarint(p, &v64); |
| 773 | assert( n>5 && n<=9 ); |
| 774 | *v = (u32)v64; |
| 775 | return n; |
shane | 356574e | 2008-05-01 02:47:03 +0000 | [diff] [blame] | 776 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 777 | } |
| 778 | |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 779 | /* |
| 780 | ** Return the number of bytes that will be needed to store the given |
| 781 | ** 64-bit integer. |
| 782 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 783 | int sqlite3VarintLen(u64 v){ |
| 784 | int i = 0; |
| 785 | do{ |
| 786 | i++; |
| 787 | v >>= 7; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 788 | }while( v!=0 && i<9 ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 789 | return i; |
| 790 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 791 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 792 | |
| 793 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 794 | ** Read or write a four-byte big-endian integer value. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 795 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 796 | u32 sqlite3Get4byte(const u8 *p){ |
| 797 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 798 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 799 | void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 800 | p[0] = v>>24; |
| 801 | p[1] = v>>16; |
| 802 | p[2] = v>>8; |
| 803 | p[3] = v; |
| 804 | } |
| 805 | |
| 806 | |
| 807 | |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 808 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 809 | /* |
| 810 | ** Translate a single byte of Hex into an integer. |
drh | 335d29d | 2008-04-04 15:12:21 +0000 | [diff] [blame] | 811 | ** This routinen only works if h really is a valid hexadecimal |
| 812 | ** character: 0..9a..fA..F |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 813 | */ |
| 814 | static int hexToInt(int h){ |
drh | 06fbb73 | 2008-04-11 19:37:55 +0000 | [diff] [blame] | 815 | assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
drh | 04924d8 | 2008-04-14 14:34:44 +0000 | [diff] [blame] | 816 | #ifdef SQLITE_ASCII |
drh | 06fbb73 | 2008-04-11 19:37:55 +0000 | [diff] [blame] | 817 | h += 9*(1&(h>>6)); |
drh | 04924d8 | 2008-04-14 14:34:44 +0000 | [diff] [blame] | 818 | #endif |
| 819 | #ifdef SQLITE_EBCDIC |
drh | 06fbb73 | 2008-04-11 19:37:55 +0000 | [diff] [blame] | 820 | h += 9*(1&~(h>>4)); |
drh | 335d29d | 2008-04-04 15:12:21 +0000 | [diff] [blame] | 821 | #endif |
drh | 06fbb73 | 2008-04-11 19:37:55 +0000 | [diff] [blame] | 822 | return h & 0xf; |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 823 | } |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 824 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 825 | |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 826 | #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 827 | /* |
| 828 | ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 829 | ** value. Return a pointer to its binary value. Space to hold the |
| 830 | ** binary value has been obtained from malloc and must be freed by |
| 831 | ** the calling routine. |
| 832 | */ |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 833 | void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 834 | char *zBlob; |
| 835 | int i; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 836 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 837 | zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); |
| 838 | n--; |
drh | 76f8079 | 2006-07-11 12:40:25 +0000 | [diff] [blame] | 839 | if( zBlob ){ |
| 840 | for(i=0; i<n; i+=2){ |
| 841 | zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); |
| 842 | } |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 843 | zBlob[i/2] = 0; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 844 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 845 | return zBlob; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 846 | } |
drh | bf8aa33 | 2004-11-04 04:34:14 +0000 | [diff] [blame] | 847 | #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 848 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 849 | |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 850 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 851 | ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. |
| 852 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN |
| 853 | ** when this routine is called. |
| 854 | ** |
| 855 | ** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN |
| 856 | ** value indicates that the database connection passed into the API is |
| 857 | ** open and is not being used by another thread. By changing the value |
| 858 | ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use. |
| 859 | ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN |
| 860 | ** when the API exits. |
| 861 | ** |
| 862 | ** This routine is a attempt to detect if two threads use the |
| 863 | ** same sqlite* pointer at the same time. There is a race |
| 864 | ** condition so it is possible that the error is not detected. |
| 865 | ** But usually the problem will be seen. The result will be an |
| 866 | ** error which can be used to debug the application that is |
| 867 | ** using SQLite incorrectly. |
| 868 | ** |
| 869 | ** Ticket #202: If db->magic is not a valid open value, take care not |
| 870 | ** to modify the db structure at all. It could be that db is a stale |
| 871 | ** pointer. In other words, it could be that there has been a prior |
| 872 | ** call to sqlite3_close(db) and db has been deallocated. And we do |
| 873 | ** not want to write into deallocated memory. |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 874 | */ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 875 | #ifdef SQLITE_DEBUG |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 876 | int sqlite3SafetyOn(sqlite3 *db){ |
| 877 | if( db->magic==SQLITE_MAGIC_OPEN ){ |
| 878 | db->magic = SQLITE_MAGIC_BUSY; |
drh | a0af99f | 2008-04-16 00:49:12 +0000 | [diff] [blame] | 879 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 880 | return 0; |
| 881 | }else if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 882 | db->magic = SQLITE_MAGIC_ERROR; |
| 883 | db->u1.isInterrupted = 1; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 884 | } |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 885 | return 1; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 886 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 887 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. |
| 891 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY |
| 892 | ** when this routine is called. |
| 893 | */ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 894 | #ifdef SQLITE_DEBUG |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 895 | int sqlite3SafetyOff(sqlite3 *db){ |
| 896 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
| 897 | db->magic = SQLITE_MAGIC_OPEN; |
drh | a0af99f | 2008-04-16 00:49:12 +0000 | [diff] [blame] | 898 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 899 | return 0; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 900 | }else{ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 901 | db->magic = SQLITE_MAGIC_ERROR; |
| 902 | db->u1.isInterrupted = 1; |
| 903 | return 1; |
| 904 | } |
| 905 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 906 | #endif |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | ** Check to make sure we have a valid db pointer. This test is not |
| 910 | ** foolproof but it does provide some measure of protection against |
| 911 | ** misuse of the interface such as passing in db pointers that are |
| 912 | ** NULL or which have been previously closed. If this routine returns |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 913 | ** 1 it means that the db pointer is valid and 0 if it should not be |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 914 | ** dereferenced for any reason. The calling function should invoke |
| 915 | ** SQLITE_MISUSE immediately. |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 916 | ** |
| 917 | ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| 918 | ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 919 | ** open properly and is not fit for general use but which can be |
| 920 | ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 921 | */ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 922 | int sqlite3SafetyCheckOk(sqlite3 *db){ |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 923 | int magic; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 924 | if( db==0 ) return 0; |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 925 | magic = db->magic; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 926 | if( magic!=SQLITE_MAGIC_OPEN && |
| 927 | magic!=SQLITE_MAGIC_BUSY ) return 0; |
| 928 | return 1; |
| 929 | } |
| 930 | int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
| 931 | int magic; |
| 932 | if( db==0 ) return 0; |
| 933 | magic = db->magic; |
| 934 | if( magic!=SQLITE_MAGIC_SICK && |
| 935 | magic!=SQLITE_MAGIC_OPEN && |
| 936 | magic!=SQLITE_MAGIC_BUSY ) return 0; |
| 937 | return 1; |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 938 | } |