xref: /sqlite-3.40.0/src/util.c (revision 5665b3ea)
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.205 2007/05/16 17:50:46 danielk1977 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 ** 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 */
313 int sqlite3Atoi64(const char *zNum, i64 *pNum){
314   i64 v = 0;
315   int neg;
316   int i, c;
317   while( isspace(*(u8*)zNum) ) zNum++;
318   if( *zNum=='-' ){
319     neg = 1;
320     zNum++;
321   }else if( *zNum=='+' ){
322     neg = 0;
323     zNum++;
324   }else{
325     neg = 0;
326   }
327   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
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 /*
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
342 ** that number will in fact fit in a 32-bit integer.  But positive
343 ** 2147483648 will not fit in 32 bits.  So it seems safer to return
344 ** false.
345 */
346 static int sqlite3FitsIn32Bits(const char *zNum){
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 
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 
383 
384 /*
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.
392 */
393 int sqlite3SafetyCheck(sqlite3 *db){
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;
400   return 0;
401 }
402 
403 /*
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 /*
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
429 ** bit clear.  Except, if we get to the 9th byte, it stores the full
430 ** 8 bits and is the last byte.
431 */
432 int sqlite3PutVarint(unsigned char *p, u64 v){
433   int i, j, n;
434   u8 buf[10];
435   if( v & (((u64)0xff000000)<<32) ){
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   }
444   n = 0;
445   do{
446     buf[n++] = (v & 0x7f) | 0x80;
447     v >>= 7;
448   }while( v!=0 );
449   buf[0] &= 0x7f;
450   assert( n<=9 );
451   for(i=0, j=n-1; j>=0; j--, i++){
452     p[i] = buf[j];
453   }
454   return n;
455 }
456 
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 */
461 int sqlite3GetVarint(const unsigned char *p, u64 *v){
462   u32 x;
463   u64 x64;
464   int n;
465   unsigned char c;
466   if( ((c = p[0]) & 0x80)==0 ){
467     *v = c;
468     return 1;
469   }
470   x = c & 0x7f;
471   if( ((c = p[1]) & 0x80)==0 ){
472     *v = (x<<7) | c;
473     return 2;
474   }
475   x = (x<<7) | (c&0x7f);
476   if( ((c = p[2]) & 0x80)==0 ){
477     *v = (x<<7) | c;
478     return 3;
479   }
480   x = (x<<7) | (c&0x7f);
481   if( ((c = p[3]) & 0x80)==0 ){
482     *v = (x<<7) | c;
483     return 4;
484   }
485   x64 = (x<<7) | (c&0x7f);
486   n = 4;
487   do{
488     c = p[n++];
489     if( n==9 ){
490       x64 = (x64<<8) | c;
491       break;
492     }
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){
504   u32 x;
505   int n;
506   unsigned char c;
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);
517   n = 2;
518   do{
519     x = (x<<7) | ((c = p[n++])&0x7f);
520   }while( (c & 0x80)!=0 && n<9 );
521   *v = x;
522   return n;
523 }
524 
525 /*
526 ** Return the number of bytes that will be needed to store the given
527 ** 64-bit integer.
528 */
529 int sqlite3VarintLen(u64 v){
530   int i = 0;
531   do{
532     i++;
533     v >>= 7;
534   }while( v!=0 && i<9 );
535   return i;
536 }
537 
538 
539 /*
540 ** Read or write a four-byte big-endian integer value.
541 */
542 u32 sqlite3Get4byte(const u8 *p){
543   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
544 }
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 
554 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
555     || defined(SQLITE_TEST)
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;
564   }else{
565     assert( h>='A' && h<='F' );
566     return h - 'A' + 10;
567   }
568 }
569 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
570 
571 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
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 */
578 void *sqlite3HexToBlob(const char *z){
579   char *zBlob;
580   int i;
581   int n = strlen(z);
582   if( n%2 ) return 0;
583 
584   zBlob = (char *)sqliteMalloc(n/2);
585   if( zBlob ){
586     for(i=0; i<n; i+=2){
587       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
588     }
589   }
590   return zBlob;
591 }
592 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
593 
594 
595 /*
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.
619 */
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;
627   }
628   return 1;
629 }
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 }
646 
647 /*
648 ** Return a pointer to the ThreadData associated with the calling thread.
649 */
650 ThreadData *sqlite3ThreadData(){
651   ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
652   if( !p ){
653     sqlite3FailedMalloc();
654   }
655   return p;
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(){
664   static const ThreadData zeroData = {0};  /* Initializer to silence warnings
665                                            ** from broken compilers */
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(){
675   sqlite3OsThreadSpecificData(-1);
676 }
677