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