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