OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 13 matching lines...) Expand all Loading... |
24 /* | 24 /* |
25 ** Routine needed to support the testcase() macro. | 25 ** Routine needed to support the testcase() macro. |
26 */ | 26 */ |
27 #ifdef SQLITE_COVERAGE_TEST | 27 #ifdef SQLITE_COVERAGE_TEST |
28 void sqlite3Coverage(int x){ | 28 void sqlite3Coverage(int x){ |
29 static unsigned dummy = 0; | 29 static unsigned dummy = 0; |
30 dummy += (unsigned)x; | 30 dummy += (unsigned)x; |
31 } | 31 } |
32 #endif | 32 #endif |
33 | 33 |
| 34 /* |
| 35 ** Give a callback to the test harness that can be used to simulate faults |
| 36 ** in places where it is difficult or expensive to do so purely by means |
| 37 ** of inputs. |
| 38 ** |
| 39 ** The intent of the integer argument is to let the fault simulator know |
| 40 ** which of multiple sqlite3FaultSim() calls has been hit. |
| 41 ** |
| 42 ** Return whatever integer value the test callback returns, or return |
| 43 ** SQLITE_OK if no test callback is installed. |
| 44 */ |
| 45 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 46 int sqlite3FaultSim(int iTest){ |
| 47 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
| 48 return xCallback ? xCallback(iTest) : SQLITE_OK; |
| 49 } |
| 50 #endif |
| 51 |
34 #ifndef SQLITE_OMIT_FLOATING_POINT | 52 #ifndef SQLITE_OMIT_FLOATING_POINT |
35 /* | 53 /* |
36 ** Return true if the floating point value is Not a Number (NaN). | 54 ** Return true if the floating point value is Not a Number (NaN). |
37 ** | 55 ** |
38 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. | 56 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
39 ** Otherwise, we have our own implementation that works on most systems. | 57 ** Otherwise, we have our own implementation that works on most systems. |
40 */ | 58 */ |
41 int sqlite3IsNaN(double x){ | 59 int sqlite3IsNaN(double x){ |
42 int rc; /* The value return */ | 60 int rc; /* The value return */ |
43 #if !defined(SQLITE_HAVE_ISNAN) | 61 #if !defined(SQLITE_HAVE_ISNAN) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 ** than 1GiB) the value returned might be less than the true string length. | 105 ** than 1GiB) the value returned might be less than the true string length. |
88 */ | 106 */ |
89 int sqlite3Strlen30(const char *z){ | 107 int sqlite3Strlen30(const char *z){ |
90 const char *z2 = z; | 108 const char *z2 = z; |
91 if( z==0 ) return 0; | 109 if( z==0 ) return 0; |
92 while( *z2 ){ z2++; } | 110 while( *z2 ){ z2++; } |
93 return 0x3fffffff & (int)(z2 - z); | 111 return 0x3fffffff & (int)(z2 - z); |
94 } | 112 } |
95 | 113 |
96 /* | 114 /* |
| 115 ** Set the current error code to err_code and clear any prior error message. |
| 116 */ |
| 117 void sqlite3Error(sqlite3 *db, int err_code){ |
| 118 assert( db!=0 ); |
| 119 db->errCode = err_code; |
| 120 if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 121 } |
| 122 |
| 123 /* |
97 ** Set the most recent error code and error string for the sqlite | 124 ** Set the most recent error code and error string for the sqlite |
98 ** handle "db". The error code is set to "err_code". | 125 ** handle "db". The error code is set to "err_code". |
99 ** | 126 ** |
100 ** If it is not NULL, string zFormat specifies the format of the | 127 ** If it is not NULL, string zFormat specifies the format of the |
101 ** error string in the style of the printf functions: The following | 128 ** error string in the style of the printf functions: The following |
102 ** format characters are allowed: | 129 ** format characters are allowed: |
103 ** | 130 ** |
104 ** %s Insert a string | 131 ** %s Insert a string |
105 ** %z A string that should be freed after use | 132 ** %z A string that should be freed after use |
106 ** %d Insert an integer | 133 ** %d Insert an integer |
107 ** %T Insert a token | 134 ** %T Insert a token |
108 ** %S Insert the first element of a SrcList | 135 ** %S Insert the first element of a SrcList |
109 ** | 136 ** |
110 ** zFormat and any string tokens that follow it are assumed to be | 137 ** zFormat and any string tokens that follow it are assumed to be |
111 ** encoded in UTF-8. | 138 ** encoded in UTF-8. |
112 ** | 139 ** |
113 ** To clear the most recent error for sqlite handle "db", sqlite3Error | 140 ** To clear the most recent error for sqlite handle "db", sqlite3Error |
114 ** should be called with err_code set to SQLITE_OK and zFormat set | 141 ** should be called with err_code set to SQLITE_OK and zFormat set |
115 ** to NULL. | 142 ** to NULL. |
116 */ | 143 */ |
117 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ | 144 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ |
118 if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){ | 145 assert( db!=0 ); |
119 db->errCode = err_code; | 146 db->errCode = err_code; |
120 if( zFormat ){ | 147 if( zFormat==0 ){ |
121 char *z; | 148 sqlite3Error(db, err_code); |
122 va_list ap; | 149 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ |
123 va_start(ap, zFormat); | 150 char *z; |
124 z = sqlite3VMPrintf(db, zFormat, ap); | 151 va_list ap; |
125 va_end(ap); | 152 va_start(ap, zFormat); |
126 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); | 153 z = sqlite3VMPrintf(db, zFormat, ap); |
127 }else{ | 154 va_end(ap); |
128 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); | 155 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
129 } | |
130 } | 156 } |
131 } | 157 } |
132 | 158 |
133 /* | 159 /* |
134 ** Add an error message to pParse->zErrMsg and increment pParse->nErr. | 160 ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
135 ** The following formatting characters are allowed: | 161 ** The following formatting characters are allowed: |
136 ** | 162 ** |
137 ** %s Insert a string | 163 ** %s Insert a string |
138 ** %z A string that should be freed after use | 164 ** %z A string that should be freed after use |
139 ** %d Insert an integer | 165 ** %d Insert an integer |
140 ** %T Insert a token | 166 ** %T Insert a token |
141 ** %S Insert the first element of a SrcList | 167 ** %S Insert the first element of a SrcList |
142 ** | 168 ** |
143 ** This function should be used to report any error that occurs whilst | 169 ** This function should be used to report any error that occurs while |
144 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The | 170 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
145 ** last thing the sqlite3_prepare() function does is copy the error | 171 ** last thing the sqlite3_prepare() function does is copy the error |
146 ** stored by this function into the database handle using sqlite3Error(). | 172 ** stored by this function into the database handle using sqlite3Error(). |
147 ** Function sqlite3Error() should be used during statement execution | 173 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used |
148 ** (sqlite3_step() etc.). | 174 ** during statement execution (sqlite3_step() etc.). |
149 */ | 175 */ |
150 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ | 176 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
151 char *zMsg; | 177 char *zMsg; |
152 va_list ap; | 178 va_list ap; |
153 sqlite3 *db = pParse->db; | 179 sqlite3 *db = pParse->db; |
154 va_start(ap, zFormat); | 180 va_start(ap, zFormat); |
155 zMsg = sqlite3VMPrintf(db, zFormat, ap); | 181 zMsg = sqlite3VMPrintf(db, zFormat, ap); |
156 va_end(ap); | 182 va_end(ap); |
157 if( db->suppressErr ){ | 183 if( db->suppressErr ){ |
158 sqlite3DbFree(db, zMsg); | 184 sqlite3DbFree(db, zMsg); |
(...skipping 12 matching lines...) Expand all Loading... |
171 ** is a no-op. | 197 ** is a no-op. |
172 ** | 198 ** |
173 ** The input string must be zero-terminated. A new zero-terminator | 199 ** The input string must be zero-terminated. A new zero-terminator |
174 ** is added to the dequoted string. | 200 ** is added to the dequoted string. |
175 ** | 201 ** |
176 ** The return value is -1 if no dequoting occurs or the length of the | 202 ** The return value is -1 if no dequoting occurs or the length of the |
177 ** dequoted string, exclusive of the zero terminator, if dequoting does | 203 ** dequoted string, exclusive of the zero terminator, if dequoting does |
178 ** occur. | 204 ** occur. |
179 ** | 205 ** |
180 ** 2002-Feb-14: This routine is extended to remove MS-Access style | 206 ** 2002-Feb-14: This routine is extended to remove MS-Access style |
181 ** brackets from around identifers. For example: "[a-b-c]" becomes | 207 ** brackets from around identifiers. For example: "[a-b-c]" becomes |
182 ** "a-b-c". | 208 ** "a-b-c". |
183 */ | 209 */ |
184 int sqlite3Dequote(char *z){ | 210 int sqlite3Dequote(char *z){ |
185 char quote; | 211 char quote; |
186 int i, j; | 212 int i, j; |
187 if( z==0 ) return -1; | 213 if( z==0 ) return -1; |
188 quote = z[0]; | 214 quote = z[0]; |
189 switch( quote ){ | 215 switch( quote ){ |
190 case '\'': break; | 216 case '\'': break; |
191 case '"': break; | 217 case '"': break; |
192 case '`': break; /* For MySQL compatibility */ | 218 case '`': break; /* For MySQL compatibility */ |
193 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ | 219 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ |
194 default: return -1; | 220 default: return -1; |
195 } | 221 } |
196 for(i=1, j=0; ALWAYS(z[i]); i++){ | 222 for(i=1, j=0;; i++){ |
| 223 assert( z[i] ); |
197 if( z[i]==quote ){ | 224 if( z[i]==quote ){ |
198 if( z[i+1]==quote ){ | 225 if( z[i+1]==quote ){ |
199 z[j++] = quote; | 226 z[j++] = quote; |
200 i++; | 227 i++; |
201 }else{ | 228 }else{ |
202 break; | 229 break; |
203 } | 230 } |
204 }else{ | 231 }else{ |
205 z[j++] = z[i]; | 232 z[j++] = z[i]; |
206 } | 233 } |
207 } | 234 } |
208 z[j] = 0; | 235 z[j] = 0; |
209 return j; | 236 return j; |
210 } | 237 } |
211 | 238 |
212 /* Convenient short-hand */ | 239 /* Convenient short-hand */ |
213 #define UpperToLower sqlite3UpperToLower | 240 #define UpperToLower sqlite3UpperToLower |
214 | 241 |
215 /* | 242 /* |
216 ** Some systems have stricmp(). Others have strcasecmp(). Because | 243 ** Some systems have stricmp(). Others have strcasecmp(). Because |
217 ** there is no consistency, we will define our own. | 244 ** there is no consistency, we will define our own. |
218 ** | 245 ** |
219 ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows | 246 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
220 ** applications and extensions to compare the contents of two buffers | 247 ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
221 ** containing UTF-8 strings in a case-independent fashion, using the same | 248 ** the contents of two buffers containing UTF-8 strings in a |
222 ** definition of case independence that SQLite uses internally when | 249 ** case-independent fashion, using the same definition of "case |
223 ** comparing identifiers. | 250 ** independence" that SQLite uses internally when comparing identifiers. |
224 */ | 251 */ |
225 int sqlite3StrICmp(const char *zLeft, const char *zRight){ | 252 int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
226 register unsigned char *a, *b; | 253 register unsigned char *a, *b; |
227 a = (unsigned char *)zLeft; | 254 a = (unsigned char *)zLeft; |
228 b = (unsigned char *)zRight; | 255 b = (unsigned char *)zRight; |
229 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } | 256 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
230 return UpperToLower[*a] - UpperToLower[*b]; | 257 return UpperToLower[*a] - UpperToLower[*b]; |
231 } | 258 } |
232 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ | 259 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
233 register unsigned char *a, *b; | 260 register unsigned char *a, *b; |
234 a = (unsigned char *)zLeft; | 261 a = (unsigned char *)zLeft; |
235 b = (unsigned char *)zRight; | 262 b = (unsigned char *)zRight; |
(...skipping 18 matching lines...) Expand all Loading... |
254 ** | 281 ** |
255 ** Leading and trailing whitespace is ignored for the purpose of determining | 282 ** Leading and trailing whitespace is ignored for the purpose of determining |
256 ** validity. | 283 ** validity. |
257 ** | 284 ** |
258 ** If some prefix of the input string is a valid number, this routine | 285 ** If some prefix of the input string is a valid number, this routine |
259 ** returns FALSE but it still converts the prefix and writes the result | 286 ** returns FALSE but it still converts the prefix and writes the result |
260 ** into *pResult. | 287 ** into *pResult. |
261 */ | 288 */ |
262 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ | 289 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ |
263 #ifndef SQLITE_OMIT_FLOATING_POINT | 290 #ifndef SQLITE_OMIT_FLOATING_POINT |
264 int incr = (enc==SQLITE_UTF8?1:2); | 291 int incr; |
265 const char *zEnd = z + length; | 292 const char *zEnd = z + length; |
266 /* sign * significand * (10 ^ (esign * exponent)) */ | 293 /* sign * significand * (10 ^ (esign * exponent)) */ |
267 int sign = 1; /* sign of significand */ | 294 int sign = 1; /* sign of significand */ |
268 i64 s = 0; /* significand */ | 295 i64 s = 0; /* significand */ |
269 int d = 0; /* adjust exponent for shifting decimal point */ | 296 int d = 0; /* adjust exponent for shifting decimal point */ |
270 int esign = 1; /* sign of exponent */ | 297 int esign = 1; /* sign of exponent */ |
271 int e = 0; /* exponent */ | 298 int e = 0; /* exponent */ |
272 int eValid = 1; /* True exponent is either not used or is well-formed */ | 299 int eValid = 1; /* True exponent is either not used or is well-formed */ |
273 double result; | 300 double result; |
274 int nDigits = 0; | 301 int nDigits = 0; |
| 302 int nonNum = 0; |
275 | 303 |
| 304 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
276 *pResult = 0.0; /* Default return value, in case of an error */ | 305 *pResult = 0.0; /* Default return value, in case of an error */ |
277 | 306 |
278 if( enc==SQLITE_UTF16BE ) z++; | 307 if( enc==SQLITE_UTF8 ){ |
| 308 incr = 1; |
| 309 }else{ |
| 310 int i; |
| 311 incr = 2; |
| 312 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 313 for(i=3-enc; i<length && z[i]==0; i+=2){} |
| 314 nonNum = i<length; |
| 315 zEnd = z+i+enc-3; |
| 316 z += (enc&1); |
| 317 } |
279 | 318 |
280 /* skip leading spaces */ | 319 /* skip leading spaces */ |
281 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; | 320 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
282 if( z>=zEnd ) return 0; | 321 if( z>=zEnd ) return 0; |
283 | 322 |
284 /* get sign of significand */ | 323 /* get sign of significand */ |
285 if( *z=='-' ){ | 324 if( *z=='-' ){ |
286 sign = -1; | 325 sign = -1; |
287 z+=incr; | 326 z+=incr; |
288 }else if( *z=='+' ){ | 327 }else if( *z=='+' ){ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 if( z>=zEnd ) goto do_atof_calc; | 363 if( z>=zEnd ) goto do_atof_calc; |
325 /* get sign of exponent */ | 364 /* get sign of exponent */ |
326 if( *z=='-' ){ | 365 if( *z=='-' ){ |
327 esign = -1; | 366 esign = -1; |
328 z+=incr; | 367 z+=incr; |
329 }else if( *z=='+' ){ | 368 }else if( *z=='+' ){ |
330 z+=incr; | 369 z+=incr; |
331 } | 370 } |
332 /* copy digits to exponent */ | 371 /* copy digits to exponent */ |
333 while( z<zEnd && sqlite3Isdigit(*z) ){ | 372 while( z<zEnd && sqlite3Isdigit(*z) ){ |
334 e = e*10 + (*z - '0'); | 373 e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
335 z+=incr; | 374 z+=incr; |
336 eValid = 1; | 375 eValid = 1; |
337 } | 376 } |
338 } | 377 } |
339 | 378 |
340 /* skip trailing spaces */ | 379 /* skip trailing spaces */ |
341 if( nDigits && eValid ){ | 380 if( nDigits && eValid ){ |
342 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; | 381 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
343 } | 382 } |
344 | 383 |
(...skipping 19 matching lines...) Expand all Loading... |
364 }else{ | 403 }else{ |
365 while( !(s%10) && e>0 ) e--,s/=10; | 404 while( !(s%10) && e>0 ) e--,s/=10; |
366 } | 405 } |
367 | 406 |
368 /* adjust the sign of significand */ | 407 /* adjust the sign of significand */ |
369 s = sign<0 ? -s : s; | 408 s = sign<0 ? -s : s; |
370 | 409 |
371 /* if exponent, scale significand as appropriate | 410 /* if exponent, scale significand as appropriate |
372 ** and store in result. */ | 411 ** and store in result. */ |
373 if( e ){ | 412 if( e ){ |
374 double scale = 1.0; | 413 LONGDOUBLE_TYPE scale = 1.0; |
375 /* attempt to handle extremely small/large numbers better */ | 414 /* attempt to handle extremely small/large numbers better */ |
376 if( e>307 && e<342 ){ | 415 if( e>307 && e<342 ){ |
377 while( e%308 ) { scale *= 1.0e+1; e -= 1; } | 416 while( e%308 ) { scale *= 1.0e+1; e -= 1; } |
378 if( esign<0 ){ | 417 if( esign<0 ){ |
379 result = s / scale; | 418 result = s / scale; |
380 result /= 1.0e+308; | 419 result /= 1.0e+308; |
381 }else{ | 420 }else{ |
382 result = s * scale; | 421 result = s * scale; |
383 result *= 1.0e+308; | 422 result *= 1.0e+308; |
384 } | 423 } |
| 424 }else if( e>=342 ){ |
| 425 if( esign<0 ){ |
| 426 result = 0.0*s; |
| 427 }else{ |
| 428 result = 1e308*1e308*s; /* Infinity */ |
| 429 } |
385 }else{ | 430 }else{ |
386 /* 1.0e+22 is the largest power of 10 than can be | 431 /* 1.0e+22 is the largest power of 10 than can be |
387 ** represented exactly. */ | 432 ** represented exactly. */ |
388 while( e%22 ) { scale *= 1.0e+1; e -= 1; } | 433 while( e%22 ) { scale *= 1.0e+1; e -= 1; } |
389 while( e>0 ) { scale *= 1.0e+22; e -= 22; } | 434 while( e>0 ) { scale *= 1.0e+22; e -= 22; } |
390 if( esign<0 ){ | 435 if( esign<0 ){ |
391 result = s / scale; | 436 result = s / scale; |
392 }else{ | 437 }else{ |
393 result = s * scale; | 438 result = s * scale; |
394 } | 439 } |
395 } | 440 } |
396 } else { | 441 } else { |
397 result = (double)s; | 442 result = (double)s; |
398 } | 443 } |
399 } | 444 } |
400 | 445 |
401 /* store the result */ | 446 /* store the result */ |
402 *pResult = result; | 447 *pResult = result; |
403 | 448 |
404 /* return true if number and no extra non-whitespace chracters after */ | 449 /* return true if number and no extra non-whitespace chracters after */ |
405 return z>=zEnd && nDigits>0 && eValid; | 450 return z>=zEnd && nDigits>0 && eValid && nonNum==0; |
406 #else | 451 #else |
407 return !sqlite3Atoi64(z, pResult, length, enc); | 452 return !sqlite3Atoi64(z, pResult, length, enc); |
408 #endif /* SQLITE_OMIT_FLOATING_POINT */ | 453 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
409 } | 454 } |
410 | 455 |
411 /* | 456 /* |
412 ** Compare the 19-character string zNum against the text representation | 457 ** Compare the 19-character string zNum against the text representation |
413 ** value 2^63: 9223372036854775808. Return negative, zero, or positive | 458 ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
414 ** if zNum is less than, equal to, or greater than the string. | 459 ** if zNum is less than, equal to, or greater than the string. |
415 ** Note that zNum must contain exactly 19 characters. | 460 ** Note that zNum must contain exactly 19 characters. |
(...skipping 16 matching lines...) Expand all Loading... |
432 } | 477 } |
433 if( c==0 ){ | 478 if( c==0 ){ |
434 c = zNum[18*incr] - '8'; | 479 c = zNum[18*incr] - '8'; |
435 testcase( c==(-1) ); | 480 testcase( c==(-1) ); |
436 testcase( c==0 ); | 481 testcase( c==0 ); |
437 testcase( c==(+1) ); | 482 testcase( c==(+1) ); |
438 } | 483 } |
439 return c; | 484 return c; |
440 } | 485 } |
441 | 486 |
442 | |
443 /* | 487 /* |
444 ** Convert zNum to a 64-bit signed integer. | 488 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This |
| 489 ** routine does *not* accept hexadecimal notation. |
445 ** | 490 ** |
446 ** If the zNum value is representable as a 64-bit twos-complement | 491 ** If the zNum value is representable as a 64-bit twos-complement |
447 ** integer, then write that value into *pNum and return 0. | 492 ** integer, then write that value into *pNum and return 0. |
448 ** | 493 ** |
449 ** If zNum is exactly 9223372036854665808, return 2. This special | 494 ** If zNum is exactly 9223372036854775808, return 2. This special |
450 ** case is broken out because while 9223372036854665808 cannot be a | 495 ** case is broken out because while 9223372036854775808 cannot be a |
451 ** signed 64-bit integer, its negative -9223372036854665808 can be. | 496 ** signed 64-bit integer, its negative -9223372036854775808 can be. |
452 ** | 497 ** |
453 ** If zNum is too big for a 64-bit integer and is not | 498 ** If zNum is too big for a 64-bit integer and is not |
454 ** 9223372036854665808 then return 1. | 499 ** 9223372036854775808 or if zNum contains any non-numeric text, |
| 500 ** then return 1. |
455 ** | 501 ** |
456 ** length is the number of bytes in the string (bytes, not characters). | 502 ** length is the number of bytes in the string (bytes, not characters). |
457 ** The string is not necessarily zero-terminated. The encoding is | 503 ** The string is not necessarily zero-terminated. The encoding is |
458 ** given by enc. | 504 ** given by enc. |
459 */ | 505 */ |
460 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ | 506 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
461 int incr = (enc==SQLITE_UTF8?1:2); | 507 int incr; |
462 u64 u = 0; | 508 u64 u = 0; |
463 int neg = 0; /* assume positive */ | 509 int neg = 0; /* assume positive */ |
464 int i; | 510 int i; |
465 int c = 0; | 511 int c = 0; |
| 512 int nonNum = 0; |
466 const char *zStart; | 513 const char *zStart; |
467 const char *zEnd = zNum + length; | 514 const char *zEnd = zNum + length; |
468 if( enc==SQLITE_UTF16BE ) zNum++; | 515 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 516 if( enc==SQLITE_UTF8 ){ |
| 517 incr = 1; |
| 518 }else{ |
| 519 incr = 2; |
| 520 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 521 for(i=3-enc; i<length && zNum[i]==0; i+=2){} |
| 522 nonNum = i<length; |
| 523 zEnd = zNum+i+enc-3; |
| 524 zNum += (enc&1); |
| 525 } |
469 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; | 526 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
470 if( zNum<zEnd ){ | 527 if( zNum<zEnd ){ |
471 if( *zNum=='-' ){ | 528 if( *zNum=='-' ){ |
472 neg = 1; | 529 neg = 1; |
473 zNum+=incr; | 530 zNum+=incr; |
474 }else if( *zNum=='+' ){ | 531 }else if( *zNum=='+' ){ |
475 zNum+=incr; | 532 zNum+=incr; |
476 } | 533 } |
477 } | 534 } |
478 zStart = zNum; | 535 zStart = zNum; |
479 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ | 536 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
480 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ | 537 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ |
481 u = u*10 + c - '0'; | 538 u = u*10 + c - '0'; |
482 } | 539 } |
483 if( u>LARGEST_INT64 ){ | 540 if( u>LARGEST_INT64 ){ |
484 *pNum = SMALLEST_INT64; | 541 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
485 }else if( neg ){ | 542 }else if( neg ){ |
486 *pNum = -(i64)u; | 543 *pNum = -(i64)u; |
487 }else{ | 544 }else{ |
488 *pNum = (i64)u; | 545 *pNum = (i64)u; |
489 } | 546 } |
490 testcase( i==18 ); | 547 testcase( i==18 ); |
491 testcase( i==19 ); | 548 testcase( i==19 ); |
492 testcase( i==20 ); | 549 testcase( i==20 ); |
493 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){ | 550 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum )
{ |
494 /* zNum is empty or contains non-numeric text or is longer | 551 /* zNum is empty or contains non-numeric text or is longer |
495 ** than 19 digits (thus guaranteeing that it is too large) */ | 552 ** than 19 digits (thus guaranteeing that it is too large) */ |
496 return 1; | 553 return 1; |
497 }else if( i<19*incr ){ | 554 }else if( i<19*incr ){ |
498 /* Less than 19 digits, so we know that it fits in 64 bits */ | 555 /* Less than 19 digits, so we know that it fits in 64 bits */ |
499 assert( u<=LARGEST_INT64 ); | 556 assert( u<=LARGEST_INT64 ); |
500 return 0; | 557 return 0; |
501 }else{ | 558 }else{ |
502 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ | 559 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
503 c = compare2pow63(zNum, incr); | 560 c = compare2pow63(zNum, incr); |
504 if( c<0 ){ | 561 if( c<0 ){ |
505 /* zNum is less than 9223372036854775808 so it fits */ | 562 /* zNum is less than 9223372036854775808 so it fits */ |
506 assert( u<=LARGEST_INT64 ); | 563 assert( u<=LARGEST_INT64 ); |
507 return 0; | 564 return 0; |
508 }else if( c>0 ){ | 565 }else if( c>0 ){ |
509 /* zNum is greater than 9223372036854775808 so it overflows */ | 566 /* zNum is greater than 9223372036854775808 so it overflows */ |
510 return 1; | 567 return 1; |
511 }else{ | 568 }else{ |
512 /* zNum is exactly 9223372036854775808. Fits if negative. The | 569 /* zNum is exactly 9223372036854775808. Fits if negative. The |
513 ** special case 2 overflow if positive */ | 570 ** special case 2 overflow if positive */ |
514 assert( u-1==LARGEST_INT64 ); | 571 assert( u-1==LARGEST_INT64 ); |
515 assert( (*pNum)==SMALLEST_INT64 ); | |
516 return neg ? 0 : 2; | 572 return neg ? 0 : 2; |
517 } | 573 } |
518 } | 574 } |
519 } | 575 } |
520 | 576 |
521 /* | 577 /* |
| 578 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, |
| 579 ** into a 64-bit signed integer. This routine accepts hexadecimal literals, |
| 580 ** whereas sqlite3Atoi64() does not. |
| 581 ** |
| 582 ** Returns: |
| 583 ** |
| 584 ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| 585 ** 1 Integer too large for a 64-bit signed integer or is malformed |
| 586 ** 2 Special case of 9223372036854775808 |
| 587 */ |
| 588 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ |
| 589 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 590 if( z[0]=='0' |
| 591 && (z[1]=='x' || z[1]=='X') |
| 592 && sqlite3Isxdigit(z[2]) |
| 593 ){ |
| 594 u64 u = 0; |
| 595 int i, k; |
| 596 for(i=2; z[i]=='0'; i++){} |
| 597 for(k=i; sqlite3Isxdigit(z[k]); k++){ |
| 598 u = u*16 + sqlite3HexToInt(z[k]); |
| 599 } |
| 600 memcpy(pOut, &u, 8); |
| 601 return (z[k]==0 && k-i<=16) ? 0 : 1; |
| 602 }else |
| 603 #endif /* SQLITE_OMIT_HEX_INTEGER */ |
| 604 { |
| 605 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); |
| 606 } |
| 607 } |
| 608 |
| 609 /* |
522 ** If zNum represents an integer that will fit in 32-bits, then set | 610 ** If zNum represents an integer that will fit in 32-bits, then set |
523 ** *pValue to that integer and return true. Otherwise return false. | 611 ** *pValue to that integer and return true. Otherwise return false. |
524 ** | 612 ** |
| 613 ** This routine accepts both decimal and hexadecimal notation for integers. |
| 614 ** |
525 ** Any non-numeric characters that following zNum are ignored. | 615 ** Any non-numeric characters that following zNum are ignored. |
526 ** This is different from sqlite3Atoi64() which requires the | 616 ** This is different from sqlite3Atoi64() which requires the |
527 ** input number to be zero-terminated. | 617 ** input number to be zero-terminated. |
528 */ | 618 */ |
529 int sqlite3GetInt32(const char *zNum, int *pValue){ | 619 int sqlite3GetInt32(const char *zNum, int *pValue){ |
530 sqlite_int64 v = 0; | 620 sqlite_int64 v = 0; |
531 int i, c; | 621 int i, c; |
532 int neg = 0; | 622 int neg = 0; |
533 if( zNum[0]=='-' ){ | 623 if( zNum[0]=='-' ){ |
534 neg = 1; | 624 neg = 1; |
535 zNum++; | 625 zNum++; |
536 }else if( zNum[0]=='+' ){ | 626 }else if( zNum[0]=='+' ){ |
537 zNum++; | 627 zNum++; |
538 } | 628 } |
539 while( zNum[0]=='0' ) zNum++; | 629 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 630 else if( zNum[0]=='0' |
| 631 && (zNum[1]=='x' || zNum[1]=='X') |
| 632 && sqlite3Isxdigit(zNum[2]) |
| 633 ){ |
| 634 u32 u = 0; |
| 635 zNum += 2; |
| 636 while( zNum[0]=='0' ) zNum++; |
| 637 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ |
| 638 u = u*16 + sqlite3HexToInt(zNum[i]); |
| 639 } |
| 640 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ |
| 641 memcpy(pValue, &u, 4); |
| 642 return 1; |
| 643 }else{ |
| 644 return 0; |
| 645 } |
| 646 } |
| 647 #endif |
540 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ | 648 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
541 v = v*10 + c; | 649 v = v*10 + c; |
542 } | 650 } |
543 | 651 |
544 /* The longest decimal representation of a 32 bit integer is 10 digits: | 652 /* The longest decimal representation of a 32 bit integer is 10 digits: |
545 ** | 653 ** |
546 ** 1234567890 | 654 ** 1234567890 |
547 ** 2^31 -> 2147483648 | 655 ** 2^31 -> 2147483648 |
548 */ | 656 */ |
549 testcase( i==10 ); | 657 testcase( i==10 ); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 /* | 701 /* |
594 ** Write a 64-bit variable-length integer to memory starting at p[0]. | 702 ** Write a 64-bit variable-length integer to memory starting at p[0]. |
595 ** The length of data write will be between 1 and 9 bytes. The number | 703 ** The length of data write will be between 1 and 9 bytes. The number |
596 ** of bytes written is returned. | 704 ** of bytes written is returned. |
597 ** | 705 ** |
598 ** A variable-length integer consists of the lower 7 bits of each byte | 706 ** A variable-length integer consists of the lower 7 bits of each byte |
599 ** for all bytes that have the 8th bit set and one byte with the 8th | 707 ** for all bytes that have the 8th bit set and one byte with the 8th |
600 ** bit clear. Except, if we get to the 9th byte, it stores the full | 708 ** bit clear. Except, if we get to the 9th byte, it stores the full |
601 ** 8 bits and is the last byte. | 709 ** 8 bits and is the last byte. |
602 */ | 710 */ |
603 int sqlite3PutVarint(unsigned char *p, u64 v){ | 711 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ |
604 int i, j, n; | 712 int i, j, n; |
605 u8 buf[10]; | 713 u8 buf[10]; |
606 if( v & (((u64)0xff000000)<<32) ){ | 714 if( v & (((u64)0xff000000)<<32) ){ |
607 p[8] = (u8)v; | 715 p[8] = (u8)v; |
608 v >>= 8; | 716 v >>= 8; |
609 for(i=7; i>=0; i--){ | 717 for(i=7; i>=0; i--){ |
610 p[i] = (u8)((v & 0x7f) | 0x80); | 718 p[i] = (u8)((v & 0x7f) | 0x80); |
611 v >>= 7; | 719 v >>= 7; |
612 } | 720 } |
613 return 9; | 721 return 9; |
614 } | 722 } |
615 n = 0; | 723 n = 0; |
616 do{ | 724 do{ |
617 buf[n++] = (u8)((v & 0x7f) | 0x80); | 725 buf[n++] = (u8)((v & 0x7f) | 0x80); |
618 v >>= 7; | 726 v >>= 7; |
619 }while( v!=0 ); | 727 }while( v!=0 ); |
620 buf[0] &= 0x7f; | 728 buf[0] &= 0x7f; |
621 assert( n<=9 ); | 729 assert( n<=9 ); |
622 for(i=0, j=n-1; j>=0; j--, i++){ | 730 for(i=0, j=n-1; j>=0; j--, i++){ |
623 p[i] = buf[j]; | 731 p[i] = buf[j]; |
624 } | 732 } |
625 return n; | 733 return n; |
626 } | 734 } |
627 | 735 int sqlite3PutVarint(unsigned char *p, u64 v){ |
628 /* | 736 if( v<=0x7f ){ |
629 ** This routine is a faster version of sqlite3PutVarint() that only | 737 p[0] = v&0x7f; |
630 ** works for 32-bit positive integers and which is optimized for | |
631 ** the common case of small integers. A MACRO version, putVarint32, | |
632 ** is provided which inlines the single-byte case. All code should use | |
633 ** the MACRO version as this function assumes the single-byte case has | |
634 ** already been handled. | |
635 */ | |
636 int sqlite3PutVarint32(unsigned char *p, u32 v){ | |
637 #ifndef putVarint32 | |
638 if( (v & ~0x7f)==0 ){ | |
639 p[0] = v; | |
640 return 1; | 738 return 1; |
641 } | 739 } |
642 #endif | 740 if( v<=0x3fff ){ |
643 if( (v & ~0x3fff)==0 ){ | 741 p[0] = ((v>>7)&0x7f)|0x80; |
644 p[0] = (u8)((v>>7) | 0x80); | 742 p[1] = v&0x7f; |
645 p[1] = (u8)(v & 0x7f); | |
646 return 2; | 743 return 2; |
647 } | 744 } |
648 return sqlite3PutVarint(p, v); | 745 return putVarint64(p,v); |
649 } | 746 } |
650 | 747 |
651 /* | 748 /* |
652 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants | 749 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants |
653 ** are defined here rather than simply putting the constant expressions | 750 ** are defined here rather than simply putting the constant expressions |
654 ** inline in order to work around bugs in the RVT compiler. | 751 ** inline in order to work around bugs in the RVT compiler. |
655 ** | 752 ** |
656 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f | 753 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f |
657 ** | 754 ** |
658 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 | 755 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 v >>= 7; | 1062 v >>= 7; |
966 }while( v!=0 && ALWAYS(i<9) ); | 1063 }while( v!=0 && ALWAYS(i<9) ); |
967 return i; | 1064 return i; |
968 } | 1065 } |
969 | 1066 |
970 | 1067 |
971 /* | 1068 /* |
972 ** Read or write a four-byte big-endian integer value. | 1069 ** Read or write a four-byte big-endian integer value. |
973 */ | 1070 */ |
974 u32 sqlite3Get4byte(const u8 *p){ | 1071 u32 sqlite3Get4byte(const u8 *p){ |
975 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; | 1072 testcase( p[0]&0x80 ); |
| 1073 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
976 } | 1074 } |
977 void sqlite3Put4byte(unsigned char *p, u32 v){ | 1075 void sqlite3Put4byte(unsigned char *p, u32 v){ |
978 p[0] = (u8)(v>>24); | 1076 p[0] = (u8)(v>>24); |
979 p[1] = (u8)(v>>16); | 1077 p[1] = (u8)(v>>16); |
980 p[2] = (u8)(v>>8); | 1078 p[2] = (u8)(v>>8); |
981 p[3] = (u8)v; | 1079 p[3] = (u8)v; |
982 } | 1080 } |
983 | 1081 |
984 | 1082 |
985 | 1083 |
986 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) | |
987 /* | 1084 /* |
988 ** Translate a single byte of Hex into an integer. | 1085 ** Translate a single byte of Hex into an integer. |
989 ** This routine only works if h really is a valid hexadecimal | 1086 ** This routine only works if h really is a valid hexadecimal |
990 ** character: 0..9a..fA..F | 1087 ** character: 0..9a..fA..F |
991 */ | 1088 */ |
992 static u8 hexToInt(int h){ | 1089 u8 sqlite3HexToInt(int h){ |
993 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); | 1090 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
994 #ifdef SQLITE_ASCII | 1091 #ifdef SQLITE_ASCII |
995 h += 9*(1&(h>>6)); | 1092 h += 9*(1&(h>>6)); |
996 #endif | 1093 #endif |
997 #ifdef SQLITE_EBCDIC | 1094 #ifdef SQLITE_EBCDIC |
998 h += 9*(1&~(h>>4)); | 1095 h += 9*(1&~(h>>4)); |
999 #endif | 1096 #endif |
1000 return (u8)(h & 0xf); | 1097 return (u8)(h & 0xf); |
1001 } | 1098 } |
1002 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ | |
1003 | 1099 |
1004 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) | 1100 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
1005 /* | 1101 /* |
1006 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary | 1102 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
1007 ** value. Return a pointer to its binary value. Space to hold the | 1103 ** value. Return a pointer to its binary value. Space to hold the |
1008 ** binary value has been obtained from malloc and must be freed by | 1104 ** binary value has been obtained from malloc and must be freed by |
1009 ** the calling routine. | 1105 ** the calling routine. |
1010 */ | 1106 */ |
1011 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ | 1107 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
1012 char *zBlob; | 1108 char *zBlob; |
1013 int i; | 1109 int i; |
1014 | 1110 |
1015 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); | 1111 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); |
1016 n--; | 1112 n--; |
1017 if( zBlob ){ | 1113 if( zBlob ){ |
1018 for(i=0; i<n; i+=2){ | 1114 for(i=0; i<n; i+=2){ |
1019 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); | 1115 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
1020 } | 1116 } |
1021 zBlob[i/2] = 0; | 1117 zBlob[i/2] = 0; |
1022 } | 1118 } |
1023 return zBlob; | 1119 return zBlob; |
1024 } | 1120 } |
1025 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ | 1121 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
1026 | 1122 |
1027 /* | 1123 /* |
1028 ** Log an error that is an API call on a connection pointer that should | 1124 ** Log an error that is an API call on a connection pointer that should |
1029 ** not have been used. The "type" of connection pointer is given as the | 1125 ** not have been used. The "type" of connection pointer is given as the |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1088 ** overflow, leave *pA unchanged and return 1. | 1184 ** overflow, leave *pA unchanged and return 1. |
1089 */ | 1185 */ |
1090 int sqlite3AddInt64(i64 *pA, i64 iB){ | 1186 int sqlite3AddInt64(i64 *pA, i64 iB){ |
1091 i64 iA = *pA; | 1187 i64 iA = *pA; |
1092 testcase( iA==0 ); testcase( iA==1 ); | 1188 testcase( iA==0 ); testcase( iA==1 ); |
1093 testcase( iB==-1 ); testcase( iB==0 ); | 1189 testcase( iB==-1 ); testcase( iB==0 ); |
1094 if( iB>=0 ){ | 1190 if( iB>=0 ){ |
1095 testcase( iA>0 && LARGEST_INT64 - iA == iB ); | 1191 testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
1096 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); | 1192 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
1097 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; | 1193 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
1098 *pA += iB; | |
1099 }else{ | 1194 }else{ |
1100 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); | 1195 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
1101 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); | 1196 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
1102 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; | 1197 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
1103 *pA += iB; | |
1104 } | 1198 } |
| 1199 *pA += iB; |
1105 return 0; | 1200 return 0; |
1106 } | 1201 } |
1107 int sqlite3SubInt64(i64 *pA, i64 iB){ | 1202 int sqlite3SubInt64(i64 *pA, i64 iB){ |
1108 testcase( iB==SMALLEST_INT64+1 ); | 1203 testcase( iB==SMALLEST_INT64+1 ); |
1109 if( iB==SMALLEST_INT64 ){ | 1204 if( iB==SMALLEST_INT64 ){ |
1110 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); | 1205 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
1111 if( (*pA)>=0 ) return 1; | 1206 if( (*pA)>=0 ) return 1; |
1112 *pA -= iB; | 1207 *pA -= iB; |
1113 return 0; | 1208 return 0; |
1114 }else{ | 1209 }else{ |
1115 return sqlite3AddInt64(pA, -iB); | 1210 return sqlite3AddInt64(pA, -iB); |
1116 } | 1211 } |
1117 } | 1212 } |
1118 #define TWOPOWER32 (((i64)1)<<32) | 1213 #define TWOPOWER32 (((i64)1)<<32) |
1119 #define TWOPOWER31 (((i64)1)<<31) | 1214 #define TWOPOWER31 (((i64)1)<<31) |
1120 int sqlite3MulInt64(i64 *pA, i64 iB){ | 1215 int sqlite3MulInt64(i64 *pA, i64 iB){ |
1121 i64 iA = *pA; | 1216 i64 iA = *pA; |
1122 i64 iA1, iA0, iB1, iB0, r; | 1217 i64 iA1, iA0, iB1, iB0, r; |
1123 | 1218 |
1124 iA1 = iA/TWOPOWER32; | 1219 iA1 = iA/TWOPOWER32; |
1125 iA0 = iA % TWOPOWER32; | 1220 iA0 = iA % TWOPOWER32; |
1126 iB1 = iB/TWOPOWER32; | 1221 iB1 = iB/TWOPOWER32; |
1127 iB0 = iB % TWOPOWER32; | 1222 iB0 = iB % TWOPOWER32; |
1128 if( iA1*iB1 != 0 ) return 1; | 1223 if( iA1==0 ){ |
1129 assert( iA1*iB0==0 || iA0*iB1==0 ); | 1224 if( iB1==0 ){ |
1130 r = iA1*iB0 + iA0*iB1; | 1225 *pA *= iB; |
| 1226 return 0; |
| 1227 } |
| 1228 r = iA0*iB1; |
| 1229 }else if( iB1==0 ){ |
| 1230 r = iA1*iB0; |
| 1231 }else{ |
| 1232 /* If both iA1 and iB1 are non-zero, overflow will result */ |
| 1233 return 1; |
| 1234 } |
1131 testcase( r==(-TWOPOWER31)-1 ); | 1235 testcase( r==(-TWOPOWER31)-1 ); |
1132 testcase( r==(-TWOPOWER31) ); | 1236 testcase( r==(-TWOPOWER31) ); |
1133 testcase( r==TWOPOWER31 ); | 1237 testcase( r==TWOPOWER31 ); |
1134 testcase( r==TWOPOWER31-1 ); | 1238 testcase( r==TWOPOWER31-1 ); |
1135 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; | 1239 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; |
1136 r *= TWOPOWER32; | 1240 r *= TWOPOWER32; |
1137 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; | 1241 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; |
1138 *pA = r; | 1242 *pA = r; |
1139 return 0; | 1243 return 0; |
1140 } | 1244 } |
1141 | 1245 |
1142 /* | 1246 /* |
1143 ** Compute the absolute value of a 32-bit signed integer, of possible. Or | 1247 ** Compute the absolute value of a 32-bit signed integer, of possible. Or |
1144 ** if the integer has a value of -2147483648, return +2147483647 | 1248 ** if the integer has a value of -2147483648, return +2147483647 |
1145 */ | 1249 */ |
1146 int sqlite3AbsInt32(int x){ | 1250 int sqlite3AbsInt32(int x){ |
1147 if( x>=0 ) return x; | 1251 if( x>=0 ) return x; |
1148 if( x==(int)0x80000000 ) return 0x7fffffff; | 1252 if( x==(int)0x80000000 ) return 0x7fffffff; |
1149 return -x; | 1253 return -x; |
1150 } | 1254 } |
| 1255 |
| 1256 #ifdef SQLITE_ENABLE_8_3_NAMES |
| 1257 /* |
| 1258 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database |
| 1259 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and |
| 1260 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than |
| 1261 ** three characters, then shorten the suffix on z[] to be the last three |
| 1262 ** characters of the original suffix. |
| 1263 ** |
| 1264 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always |
| 1265 ** do the suffix shortening regardless of URI parameter. |
| 1266 ** |
| 1267 ** Examples: |
| 1268 ** |
| 1269 ** test.db-journal => test.nal |
| 1270 ** test.db-wal => test.wal |
| 1271 ** test.db-shm => test.shm |
| 1272 ** test.db-mj7f3319fa => test.9fa |
| 1273 */ |
| 1274 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ |
| 1275 #if SQLITE_ENABLE_8_3_NAMES<2 |
| 1276 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) |
| 1277 #endif |
| 1278 { |
| 1279 int i, sz; |
| 1280 sz = sqlite3Strlen30(z); |
| 1281 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} |
| 1282 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); |
| 1283 } |
| 1284 } |
| 1285 #endif |
| 1286 |
| 1287 /* |
| 1288 ** Find (an approximate) sum of two LogEst values. This computation is |
| 1289 ** not a simple "+" operator because LogEst is stored as a logarithmic |
| 1290 ** value. |
| 1291 ** |
| 1292 */ |
| 1293 LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ |
| 1294 static const unsigned char x[] = { |
| 1295 10, 10, /* 0,1 */ |
| 1296 9, 9, /* 2,3 */ |
| 1297 8, 8, /* 4,5 */ |
| 1298 7, 7, 7, /* 6,7,8 */ |
| 1299 6, 6, 6, /* 9,10,11 */ |
| 1300 5, 5, 5, /* 12-14 */ |
| 1301 4, 4, 4, 4, /* 15-18 */ |
| 1302 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1303 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1304 }; |
| 1305 if( a>=b ){ |
| 1306 if( a>b+49 ) return a; |
| 1307 if( a>b+31 ) return a+1; |
| 1308 return a+x[a-b]; |
| 1309 }else{ |
| 1310 if( b>a+49 ) return b; |
| 1311 if( b>a+31 ) return b+1; |
| 1312 return b+x[b-a]; |
| 1313 } |
| 1314 } |
| 1315 |
| 1316 /* |
| 1317 ** Convert an integer into a LogEst. In other words, compute an |
| 1318 ** approximation for 10*log2(x). |
| 1319 */ |
| 1320 LogEst sqlite3LogEst(u64 x){ |
| 1321 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1322 LogEst y = 40; |
| 1323 if( x<8 ){ |
| 1324 if( x<2 ) return 0; |
| 1325 while( x<8 ){ y -= 10; x <<= 1; } |
| 1326 }else{ |
| 1327 while( x>255 ){ y += 40; x >>= 4; } |
| 1328 while( x>15 ){ y += 10; x >>= 1; } |
| 1329 } |
| 1330 return a[x&7] + y - 10; |
| 1331 } |
| 1332 |
| 1333 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1334 /* |
| 1335 ** Convert a double into a LogEst |
| 1336 ** In other words, compute an approximation for 10*log2(x). |
| 1337 */ |
| 1338 LogEst sqlite3LogEstFromDouble(double x){ |
| 1339 u64 a; |
| 1340 LogEst e; |
| 1341 assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 1342 if( x<=1 ) return 0; |
| 1343 if( x<=2000000000 ) return sqlite3LogEst((u64)x); |
| 1344 memcpy(&a, &x, 8); |
| 1345 e = (a>>52) - 1022; |
| 1346 return e*10; |
| 1347 } |
| 1348 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1349 |
| 1350 /* |
| 1351 ** Convert a LogEst into an integer. |
| 1352 */ |
| 1353 u64 sqlite3LogEstToInt(LogEst x){ |
| 1354 u64 n; |
| 1355 if( x<10 ) return 1; |
| 1356 n = x%10; |
| 1357 x /= 10; |
| 1358 if( n>=5 ) n -= 2; |
| 1359 else if( n>=1 ) n -= 1; |
| 1360 if( x>=3 ){ |
| 1361 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); |
| 1362 } |
| 1363 return (n+8)>>(3-x); |
| 1364 } |
OLD | NEW |