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