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