1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 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. 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 ** 17 ** $Id: util.c,v 1.207 2007/06/26 00:37:28 drh Exp $ 18 */ 19 #include "sqliteInt.h" 20 #include "os.h" 21 #include <stdarg.h> 22 #include <ctype.h> 23 24 25 /* 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 ** 42 ** To clear the most recent error for sqlite handle "db", sqlite3Error 43 ** should be called with err_code set to SQLITE_OK and zFormat set 44 ** to NULL. 45 */ 46 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ 47 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){ 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 } 59 } 60 } 61 62 /* 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 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.). 78 */ 79 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ 80 va_list ap; 81 pParse->nErr++; 82 sqliteFree(pParse->zErrMsg); 83 va_start(ap, zFormat); 84 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap); 85 va_end(ap); 86 if( pParse->rc==SQLITE_OK ){ 87 pParse->rc = SQLITE_ERROR; 88 } 89 } 90 91 /* 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 /* 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. 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". 109 */ 110 void sqlite3Dequote(char *z){ 111 int quote; 112 int i, j; 113 if( z==0 ) return; 114 quote = z[0]; 115 switch( quote ){ 116 case '\'': break; 117 case '"': break; 118 case '`': break; /* For MySQL compatibility */ 119 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ 120 default: return; 121 } 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 137 /* An array to map all upper-case characters into their corresponding 138 ** lower-case character. 139 */ 140 const unsigned char sqlite3UpperToLower[] = { 141 #ifdef SQLITE_ASCII 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 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 176 }; 177 #define UpperToLower sqlite3UpperToLower 178 179 /* 180 ** Some systems have stricmp(). Others have strcasecmp(). Because 181 ** there is no consistency, we will define our own. 182 */ 183 int sqlite3StrICmp(const char *zLeft, const char *zRight){ 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++; } 188 return UpperToLower[*a] - UpperToLower[*b]; 189 } 190 int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ 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++; } 195 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; 196 } 197 198 /* 199 ** Return TRUE if z is a pure numeric string. Return FALSE if the 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). 203 ** 204 ** An empty string is considered non-numeric. 205 */ 206 int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ 207 int incr = (enc==SQLITE_UTF8?1:2); 208 if( enc==SQLITE_UTF16BE ) z++; 209 if( *z=='-' || *z=='+' ) z += incr; 210 if( !isdigit(*(u8*)z) ){ 211 return 0; 212 } 213 z += incr; 214 if( realnum ) *realnum = 0; 215 while( isdigit(*(u8*)z) ){ z += incr; } 216 if( *z=='.' ){ 217 z += incr; 218 if( !isdigit(*(u8*)z) ) return 0; 219 while( isdigit(*(u8*)z) ){ z += incr; } 220 if( realnum ) *realnum = 1; 221 } 222 if( *z=='e' || *z=='E' ){ 223 z += incr; 224 if( *z=='+' || *z=='-' ) z += incr; 225 if( !isdigit(*(u8*)z) ) return 0; 226 while( isdigit(*(u8*)z) ){ z += incr; } 227 if( realnum ) *realnum = 1; 228 } 229 return *z==0; 230 } 231 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 */ 244 int sqlite3AtoF(const char *z, double *pResult){ 245 #ifndef SQLITE_OMIT_FLOATING_POINT 246 int sign = 1; 247 const char *zBegin = z; 248 LONGDOUBLE_TYPE v1 = 0.0; 249 while( isspace(*(u8*)z) ) z++; 250 if( *z=='-' ){ 251 sign = -1; 252 z++; 253 }else if( *z=='+' ){ 254 z++; 255 } 256 while( isdigit(*(u8*)z) ){ 257 v1 = v1*10.0 + (*z - '0'); 258 z++; 259 } 260 if( *z=='.' ){ 261 LONGDOUBLE_TYPE divisor = 1.0; 262 z++; 263 while( isdigit(*(u8*)z) ){ 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; 273 LONGDOUBLE_TYPE scale = 1.0; 274 z++; 275 if( *z=='-' ){ 276 esign = -1; 277 z++; 278 }else if( *z=='+' ){ 279 z++; 280 } 281 while( isdigit(*(u8*)z) ){ 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 } 295 *pResult = sign<0 ? -v1 : v1; 296 return z - zBegin; 297 #else 298 return sqlite3Atoi64(z, pResult); 299 #endif /* SQLITE_OMIT_FLOATING_POINT */ 300 } 301 302 /* 303 ** Compare the 19-character string zNum against the text representation 304 ** value 2^63: 9223372036854775808. Return negative, zero, or positive 305 ** if zNum is less than, equal to, or greater than the string. 306 ** 307 ** Unlike memcmp() this routine is guaranteed to return the difference 308 ** in the values of the last digit if the only difference is in the 309 ** last digit. So, for example, 310 ** 311 ** compare2pow63("9223372036854775800") 312 ** 313 ** will return -8. 314 */ 315 static int compare2pow63(const char *zNum){ 316 int c; 317 c = memcmp(zNum,"922337203685477580",18); 318 if( c==0 ){ 319 c = zNum[18] - '8'; 320 } 321 return c; 322 } 323 324 325 /* 326 ** Return TRUE if zNum is a 64-bit signed integer and write 327 ** the value of the integer into *pNum. If zNum is not an integer 328 ** or is an integer that is too large to be expressed with 64 bits, 329 ** then return false. 330 ** 331 ** When this routine was originally written it dealt with only 332 ** 32-bit numbers. At that time, it was much faster than the 333 ** atoi() library routine in RedHat 7.2. 334 */ 335 int sqlite3Atoi64(const char *zNum, i64 *pNum){ 336 i64 v = 0; 337 int neg; 338 int i, c; 339 while( isspace(*(u8*)zNum) ) zNum++; 340 if( *zNum=='-' ){ 341 neg = 1; 342 zNum++; 343 }else if( *zNum=='+' ){ 344 neg = 0; 345 zNum++; 346 }else{ 347 neg = 0; 348 } 349 while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */ 350 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ 351 v = v*10 + c - '0'; 352 } 353 *pNum = neg ? -v : v; 354 if( c!=0 || i==0 || i>19 ){ 355 /* zNum is empty or contains non-numeric text or is longer 356 ** than 19 digits (thus guaranting that it is too large) */ 357 return 0; 358 }else if( i<19 ){ 359 /* Less than 19 digits, so we know that it fits in 64 bits */ 360 return 1; 361 }else{ 362 /* 19-digit numbers must be no larger than 9223372036854775807 if positive 363 ** or 9223372036854775808 if negative. Note that 9223372036854665808 364 ** is 2^63. */ 365 return compare2pow63(zNum)<neg; 366 } 367 } 368 369 /* 370 ** The string zNum represents an integer. There might be some other 371 ** information following the integer too, but that part is ignored. 372 ** If the integer that the prefix of zNum represents will fit in a 373 ** 64-bit signed integer, return TRUE. Otherwise return FALSE. 374 ** 375 ** This routine returns FALSE for the string -9223372036854775808 even that 376 ** that number will, in theory fit in a 64-bit integer. Positive 377 ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return 378 ** false. 379 */ 380 int sqlite3FitsIn64Bits(const char *zNum){ 381 int i, c; 382 int neg = 0; 383 if( *zNum=='-' ){ 384 neg = 1; 385 zNum++; 386 }else if( *zNum=='+' ){ 387 zNum++; 388 } 389 while( *zNum=='0' ){ 390 zNum++; /* Skip leading zeros. Ticket #2454 */ 391 } 392 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} 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 } 403 } 404 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. 410 ** This is different from sqlite3Atoi64() which requires the 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++; 424 for(i=0; i<10 && (c = zNum[i] - '0')>=0 && c<=9; i++){ 425 v = v*10 + c; 426 } 427 if( i>9 ){ 428 return 0; 429 } 430 if( v-neg>2147483647 ){ 431 return 0; 432 } 433 if( neg ){ 434 v = -v; 435 } 436 *pValue = (int)v; 437 return 1; 438 } 439 440 /* 441 ** Check to make sure we have a valid db pointer. This test is not 442 ** foolproof but it does provide some measure of protection against 443 ** misuse of the interface such as passing in db pointers that are 444 ** NULL or which have been previously closed. If this routine returns 445 ** TRUE it means that the db pointer is invalid and should not be 446 ** dereferenced for any reason. The calling function should invoke 447 ** SQLITE_MISUSE immediately. 448 */ 449 int sqlite3SafetyCheck(sqlite3 *db){ 450 int magic; 451 if( db==0 ) return 1; 452 magic = db->magic; 453 if( magic!=SQLITE_MAGIC_CLOSED && 454 magic!=SQLITE_MAGIC_OPEN && 455 magic!=SQLITE_MAGIC_BUSY ) return 1; 456 return 0; 457 } 458 459 /* 460 ** The variable-length integer encoding is as follows: 461 ** 462 ** KEY: 463 ** A = 0xxxxxxx 7 bits of data and one flag bit 464 ** B = 1xxxxxxx 7 bits of data and one flag bit 465 ** C = xxxxxxxx 8 bits of data 466 ** 467 ** 7 bits - A 468 ** 14 bits - BA 469 ** 21 bits - BBA 470 ** 28 bits - BBBA 471 ** 35 bits - BBBBA 472 ** 42 bits - BBBBBA 473 ** 49 bits - BBBBBBA 474 ** 56 bits - BBBBBBBA 475 ** 64 bits - BBBBBBBBC 476 */ 477 478 /* 479 ** Write a 64-bit variable-length integer to memory starting at p[0]. 480 ** The length of data write will be between 1 and 9 bytes. The number 481 ** of bytes written is returned. 482 ** 483 ** A variable-length integer consists of the lower 7 bits of each byte 484 ** for all bytes that have the 8th bit set and one byte with the 8th 485 ** bit clear. Except, if we get to the 9th byte, it stores the full 486 ** 8 bits and is the last byte. 487 */ 488 int sqlite3PutVarint(unsigned char *p, u64 v){ 489 int i, j, n; 490 u8 buf[10]; 491 if( v & (((u64)0xff000000)<<32) ){ 492 p[8] = v; 493 v >>= 8; 494 for(i=7; i>=0; i--){ 495 p[i] = (v & 0x7f) | 0x80; 496 v >>= 7; 497 } 498 return 9; 499 } 500 n = 0; 501 do{ 502 buf[n++] = (v & 0x7f) | 0x80; 503 v >>= 7; 504 }while( v!=0 ); 505 buf[0] &= 0x7f; 506 assert( n<=9 ); 507 for(i=0, j=n-1; j>=0; j--, i++){ 508 p[i] = buf[j]; 509 } 510 return n; 511 } 512 513 /* 514 ** Read a 64-bit variable-length integer from memory starting at p[0]. 515 ** Return the number of bytes read. The value is stored in *v. 516 */ 517 int sqlite3GetVarint(const unsigned char *p, u64 *v){ 518 u32 x; 519 u64 x64; 520 int n; 521 unsigned char c; 522 if( ((c = p[0]) & 0x80)==0 ){ 523 *v = c; 524 return 1; 525 } 526 x = c & 0x7f; 527 if( ((c = p[1]) & 0x80)==0 ){ 528 *v = (x<<7) | c; 529 return 2; 530 } 531 x = (x<<7) | (c&0x7f); 532 if( ((c = p[2]) & 0x80)==0 ){ 533 *v = (x<<7) | c; 534 return 3; 535 } 536 x = (x<<7) | (c&0x7f); 537 if( ((c = p[3]) & 0x80)==0 ){ 538 *v = (x<<7) | c; 539 return 4; 540 } 541 x64 = (x<<7) | (c&0x7f); 542 n = 4; 543 do{ 544 c = p[n++]; 545 if( n==9 ){ 546 x64 = (x64<<8) | c; 547 break; 548 } 549 x64 = (x64<<7) | (c&0x7f); 550 }while( (c & 0x80)!=0 ); 551 *v = x64; 552 return n; 553 } 554 555 /* 556 ** Read a 32-bit variable-length integer from memory starting at p[0]. 557 ** Return the number of bytes read. The value is stored in *v. 558 */ 559 int sqlite3GetVarint32(const unsigned char *p, u32 *v){ 560 u32 x; 561 int n; 562 unsigned char c; 563 if( ((signed char*)p)[0]>=0 ){ 564 *v = p[0]; 565 return 1; 566 } 567 x = p[0] & 0x7f; 568 if( ((signed char*)p)[1]>=0 ){ 569 *v = (x<<7) | p[1]; 570 return 2; 571 } 572 x = (x<<7) | (p[1] & 0x7f); 573 n = 2; 574 do{ 575 x = (x<<7) | ((c = p[n++])&0x7f); 576 }while( (c & 0x80)!=0 && n<9 ); 577 *v = x; 578 return n; 579 } 580 581 /* 582 ** Return the number of bytes that will be needed to store the given 583 ** 64-bit integer. 584 */ 585 int sqlite3VarintLen(u64 v){ 586 int i = 0; 587 do{ 588 i++; 589 v >>= 7; 590 }while( v!=0 && i<9 ); 591 return i; 592 } 593 594 595 /* 596 ** Read or write a four-byte big-endian integer value. 597 */ 598 u32 sqlite3Get4byte(const u8 *p){ 599 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; 600 } 601 void sqlite3Put4byte(unsigned char *p, u32 v){ 602 p[0] = v>>24; 603 p[1] = v>>16; 604 p[2] = v>>8; 605 p[3] = v; 606 } 607 608 609 610 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \ 611 || defined(SQLITE_TEST) 612 /* 613 ** Translate a single byte of Hex into an integer. 614 */ 615 static int hexToInt(int h){ 616 if( h>='0' && h<='9' ){ 617 return h - '0'; 618 }else if( h>='a' && h<='f' ){ 619 return h - 'a' + 10; 620 }else{ 621 assert( h>='A' && h<='F' ); 622 return h - 'A' + 10; 623 } 624 } 625 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */ 626 627 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) 628 /* 629 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary 630 ** value. Return a pointer to its binary value. Space to hold the 631 ** binary value has been obtained from malloc and must be freed by 632 ** the calling routine. 633 */ 634 void *sqlite3HexToBlob(const char *z){ 635 char *zBlob; 636 int i; 637 int n = strlen(z); 638 if( n%2 ) return 0; 639 640 zBlob = (char *)sqliteMalloc(n/2); 641 if( zBlob ){ 642 for(i=0; i<n; i+=2){ 643 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); 644 } 645 } 646 return zBlob; 647 } 648 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ 649 650 651 /* 652 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. 653 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN 654 ** when this routine is called. 655 ** 656 ** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN 657 ** value indicates that the database connection passed into the API is 658 ** open and is not being used by another thread. By changing the value 659 ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use. 660 ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN 661 ** when the API exits. 662 ** 663 ** This routine is a attempt to detect if two threads use the 664 ** same sqlite* pointer at the same time. There is a race 665 ** condition so it is possible that the error is not detected. 666 ** But usually the problem will be seen. The result will be an 667 ** error which can be used to debug the application that is 668 ** using SQLite incorrectly. 669 ** 670 ** Ticket #202: If db->magic is not a valid open value, take care not 671 ** to modify the db structure at all. It could be that db is a stale 672 ** pointer. In other words, it could be that there has been a prior 673 ** call to sqlite3_close(db) and db has been deallocated. And we do 674 ** not want to write into deallocated memory. 675 */ 676 int sqlite3SafetyOn(sqlite3 *db){ 677 if( db->magic==SQLITE_MAGIC_OPEN ){ 678 db->magic = SQLITE_MAGIC_BUSY; 679 return 0; 680 }else if( db->magic==SQLITE_MAGIC_BUSY ){ 681 db->magic = SQLITE_MAGIC_ERROR; 682 db->u1.isInterrupted = 1; 683 } 684 return 1; 685 } 686 687 /* 688 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. 689 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY 690 ** when this routine is called. 691 */ 692 int sqlite3SafetyOff(sqlite3 *db){ 693 if( db->magic==SQLITE_MAGIC_BUSY ){ 694 db->magic = SQLITE_MAGIC_OPEN; 695 return 0; 696 }else { 697 db->magic = SQLITE_MAGIC_ERROR; 698 db->u1.isInterrupted = 1; 699 return 1; 700 } 701 } 702 703 /* 704 ** Return a pointer to the ThreadData associated with the calling thread. 705 */ 706 ThreadData *sqlite3ThreadData(){ 707 ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1); 708 if( !p ){ 709 sqlite3FailedMalloc(); 710 } 711 return p; 712 } 713 714 /* 715 ** Return a pointer to the ThreadData associated with the calling thread. 716 ** If no ThreadData has been allocated to this thread yet, return a pointer 717 ** to a substitute ThreadData structure that is all zeros. 718 */ 719 const ThreadData *sqlite3ThreadDataReadOnly(){ 720 static const ThreadData zeroData = {0}; /* Initializer to silence warnings 721 ** from broken compilers */ 722 const ThreadData *pTd = sqlite3OsThreadSpecificData(0); 723 return pTd ? pTd : &zeroData; 724 } 725 726 /* 727 ** Check to see if the ThreadData for this thread is all zero. If it 728 ** is, then deallocate it. 729 */ 730 void sqlite3ReleaseThreadData(){ 731 sqlite3OsThreadSpecificData(-1); 732 } 733