xref: /sqlite-3.40.0/src/util.c (revision 74e4352a)
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.193 2006/09/15 07:28:51 drh Exp $
18 */
19 #include "sqliteInt.h"
20 #include "os.h"
21 #include <stdarg.h>
22 #include <ctype.h>
23 
24 /*
25 ** MALLOC WRAPPER ARCHITECTURE
26 **
27 ** The sqlite code accesses dynamic memory allocation/deallocation by invoking
28 ** the following six APIs (which may be implemented as macros).
29 **
30 **     sqlite3Malloc()
31 **     sqlite3MallocRaw()
32 **     sqlite3Realloc()
33 **     sqlite3ReallocOrFree()
34 **     sqlite3Free()
35 **     sqlite3AllocSize()
36 **
37 ** The function sqlite3FreeX performs the same task as sqlite3Free and is
38 ** guaranteed to be a real function. The same holds for sqlite3MallocX
39 **
40 ** The above APIs are implemented in terms of the functions provided in the
41 ** operating-system interface. The OS interface is never accessed directly
42 ** by code outside of this file.
43 **
44 **     sqlite3OsMalloc()
45 **     sqlite3OsRealloc()
46 **     sqlite3OsFree()
47 **     sqlite3OsAllocationSize()
48 **
49 ** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke
50 ** sqlite3_release_memory() if a call to sqlite3OsMalloc() or
51 ** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
52 ** exceeded). Function sqlite3Malloc() usually invokes
53 ** sqlite3MallocRaw().
54 **
55 ** MALLOC TEST WRAPPER ARCHITECTURE
56 **
57 ** The test wrapper provides extra test facilities to ensure the library
58 ** does not leak memory and handles the failure of the underlying OS level
59 ** allocation system correctly. It is only present if the library is
60 ** compiled with the SQLITE_MEMDEBUG macro set.
61 **
62 **     * Guardposts to detect overwrites.
63 **     * Ability to cause a specific Malloc() or Realloc() to fail.
64 **     * Audit outstanding memory allocations (i.e check for leaks).
65 */
66 
67 #define MAX(x,y) ((x)>(y)?(x):(y))
68 
69 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
70 /*
71 ** Set the soft heap-size limit for the current thread. Passing a negative
72 ** value indicates no limit.
73 */
74 void sqlite3_soft_heap_limit(int n){
75   ThreadData *pTd = sqlite3ThreadData();
76   if( pTd ){
77     pTd->nSoftHeapLimit = n;
78   }
79   sqlite3ReleaseThreadData();
80 }
81 
82 /*
83 ** Release memory held by SQLite instances created by the current thread.
84 */
85 int sqlite3_release_memory(int n){
86   return sqlite3pager_release_memory(n);
87 }
88 #else
89 /* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
90 ** of sqlite3_release_memory() to be used by other code in this file.
91 ** This is done for no better reason than to reduce the number of
92 ** pre-processor #ifndef statements.
93 */
94 #define sqlite3_release_memory(x) 0    /* 0 == no memory freed */
95 #endif
96 
97 #ifdef SQLITE_MEMDEBUG
98 /*--------------------------------------------------------------------------
99 ** Begin code for memory allocation system test layer.
100 **
101 ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
102 **
103 ** SQLITE_MEMDEBUG==1    -> Fence-posting only (thread safe)
104 ** SQLITE_MEMDEBUG==2    -> Fence-posting + linked list of allocations (not ts)
105 ** SQLITE_MEMDEBUG==3    -> Above + backtraces (not thread safe, req. glibc)
106 */
107 
108 /* Figure out whether or not to store backtrace() information for each malloc.
109 ** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or
110 ** greater and glibc is in use. If we don't want to use backtrace(), then just
111 ** define it as an empty macro and set the amount of space reserved to 0.
112 */
113 #if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
114   extern int backtrace(void **, int);
115   #define TESTALLOC_STACKSIZE 128
116   #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
117 #else
118   #define backtrace(x, y)
119   #define TESTALLOC_STACKSIZE 0
120   #define TESTALLOC_STACKFRAMES 0
121 #endif
122 
123 /*
124 ** Number of 32-bit guard words.  This should probably be a multiple of
125 ** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
126 ** to be 8-byte aligned.
127 */
128 #ifndef TESTALLOC_NGUARD
129 # define TESTALLOC_NGUARD 2
130 #endif
131 
132 /*
133 ** Size reserved for storing file-name along with each malloc()ed blob.
134 */
135 #define TESTALLOC_FILESIZE 64
136 
137 /*
138 ** Size reserved for storing the user string. Each time a Malloc() or Realloc()
139 ** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by
140 ** sqlite3_malloc_id are stored along with the other test system metadata.
141 */
142 #define TESTALLOC_USERSIZE 64
143 const char *sqlite3_malloc_id = 0;
144 
145 /*
146 ** Blocks used by the test layer have the following format:
147 **
148 **        <sizeof(void *) pNext pointer>
149 **        <sizeof(void *) pPrev pointer>
150 **        <TESTALLOC_NGUARD 32-bit guard words>
151 **            <The application level allocation>
152 **        <TESTALLOC_NGUARD 32-bit guard words>
153 **        <32-bit line number>
154 **        <TESTALLOC_FILESIZE bytes containing null-terminated file name>
155 **        <TESTALLOC_STACKSIZE bytes of backtrace() output>
156 */
157 
158 #define TESTALLOC_OFFSET_GUARD1(p)    (sizeof(void *) * 2)
159 #define TESTALLOC_OFFSET_DATA(p) ( \
160   TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
161 )
162 #define TESTALLOC_OFFSET_GUARD2(p) ( \
163   TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
164 )
165 #define TESTALLOC_OFFSET_LINENUMBER(p) ( \
166   TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
167 )
168 #define TESTALLOC_OFFSET_FILENAME(p) ( \
169   TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
170 )
171 #define TESTALLOC_OFFSET_USER(p) ( \
172   TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
173 )
174 #define TESTALLOC_OFFSET_STACK(p) ( \
175   TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
176   (TESTALLOC_OFFSET_USER(p) % 8) \
177 )
178 
179 #define TESTALLOC_OVERHEAD ( \
180   sizeof(void *)*2 +                   /* pPrev and pNext pointers */   \
181   TESTALLOC_NGUARD*sizeof(u32)*2 +              /* Guard words */       \
182   sizeof(u32) + TESTALLOC_FILESIZE +   /* File and line number */       \
183   TESTALLOC_USERSIZE +                 /* User string */                \
184   TESTALLOC_STACKSIZE                  /* backtrace() stack */          \
185 )
186 
187 
188 /*
189 ** For keeping track of the number of mallocs and frees.   This
190 ** is used to check for memory leaks.  The iMallocFail and iMallocReset
191 ** values are used to simulate malloc() failures during testing in
192 ** order to verify that the library correctly handles an out-of-memory
193 ** condition.
194 */
195 int sqlite3_nMalloc;         /* Number of sqliteMalloc() calls */
196 int sqlite3_nFree;           /* Number of sqliteFree() calls */
197 int sqlite3_memUsed;         /* TODO Total memory obtained from malloc */
198 int sqlite3_memMax;          /* TODO Mem usage high-water mark */
199 int sqlite3_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
200 int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
201 
202 void *sqlite3_pFirst = 0;         /* Pointer to linked list of allocations */
203 int sqlite3_nMaxAlloc = 0;        /* High water mark of ThreadData.nAlloc */
204 int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
205 int sqlite3_isFail = 0;           /* True if all malloc calls should fail */
206 const char *sqlite3_zFile = 0;    /* Filename to associate debug info with */
207 int sqlite3_iLine = 0;            /* Line number for debug info */
208 
209 /*
210 ** Check for a simulated memory allocation failure.  Return true if
211 ** the failure should be simulated.  Return false to proceed as normal.
212 */
213 int sqlite3TestMallocFail(){
214   if( sqlite3_isFail ){
215     return 1;
216   }
217   if( sqlite3_iMallocFail>=0 ){
218     sqlite3_iMallocFail--;
219     if( sqlite3_iMallocFail==0 ){
220       sqlite3_iMallocFail = sqlite3_iMallocReset;
221       sqlite3_isFail = 1;
222       return 1;
223     }
224   }
225   return 0;
226 }
227 
228 /*
229 ** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc().
230 ** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
231 ** values set by the applyGuards() function.
232 */
233 static void checkGuards(u32 *p)
234 {
235   int i;
236   char *zAlloc = (char *)p;
237   char *z;
238 
239   /* First set of guard words */
240   z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
241   for(i=0; i<TESTALLOC_NGUARD; i++){
242     assert(((u32 *)z)[i]==0xdead1122);
243   }
244 
245   /* Second set of guard words */
246   z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
247   for(i=0; i<TESTALLOC_NGUARD; i++){
248     u32 guard = 0;
249     memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
250     assert(guard==0xdead3344);
251   }
252 }
253 
254 /*
255 ** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The
256 ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as
257 ** guard-posts.
258 */
259 static void applyGuards(u32 *p)
260 {
261   int i;
262   char *z;
263   char *zAlloc = (char *)p;
264 
265   /* First set of guard words */
266   z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
267   for(i=0; i<TESTALLOC_NGUARD; i++){
268     ((u32 *)z)[i] = 0xdead1122;
269   }
270 
271   /* Second set of guard words */
272   z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
273   for(i=0; i<TESTALLOC_NGUARD; i++){
274     static const int guard = 0xdead3344;
275     memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
276   }
277 
278   /* Line number */
279   z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)];             /* Guard words */
280   z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
281   memcpy(z, &sqlite3_iLine, sizeof(u32));
282 
283   /* File name */
284   z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
285   strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
286   z[TESTALLOC_FILESIZE - 1] = '\0';
287 
288   /* User string */
289   z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
290   z[0] = 0;
291   if( sqlite3_malloc_id ){
292     strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
293     z[TESTALLOC_USERSIZE-1] = 0;
294   }
295 
296   /* backtrace() stack */
297   z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
298   backtrace((void **)z, TESTALLOC_STACKFRAMES);
299 
300   /* Sanity check to make sure checkGuards() is working */
301   checkGuards(p);
302 }
303 
304 /*
305 ** The argument is a malloc()ed pointer as returned by the test-wrapper.
306 ** Return a pointer to the Os level allocation.
307 */
308 static void *getOsPointer(void *p)
309 {
310   char *z = (char *)p;
311   return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
312 }
313 
314 
315 #if SQLITE_MEMDEBUG>1
316 /*
317 ** The argument points to an Os level allocation. Link it into the threads list
318 ** of allocations.
319 */
320 static void linkAlloc(void *p){
321   void **pp = (void **)p;
322   pp[0] = 0;
323   pp[1] = sqlite3_pFirst;
324   if( sqlite3_pFirst ){
325     ((void **)sqlite3_pFirst)[0] = p;
326   }
327   sqlite3_pFirst = p;
328 }
329 
330 /*
331 ** The argument points to an Os level allocation. Unlinke it from the threads
332 ** list of allocations.
333 */
334 static void unlinkAlloc(void *p)
335 {
336   void **pp = (void **)p;
337   if( p==sqlite3_pFirst ){
338     assert(!pp[0]);
339     assert(!pp[1] || ((void **)(pp[1]))[0]==p);
340     sqlite3_pFirst = pp[1];
341     if( sqlite3_pFirst ){
342       ((void **)sqlite3_pFirst)[0] = 0;
343     }
344   }else{
345     void **pprev = pp[0];
346     void **pnext = pp[1];
347     assert(pprev);
348     assert(pprev[1]==p);
349     pprev[1] = (void *)pnext;
350     if( pnext ){
351       assert(pnext[0]==p);
352       pnext[0] = (void *)pprev;
353     }
354   }
355 }
356 
357 /*
358 ** Pointer p is a pointer to an OS level allocation that has just been
359 ** realloc()ed. Set the list pointers that point to this entry to it's new
360 ** location.
361 */
362 static void relinkAlloc(void *p)
363 {
364   void **pp = (void **)p;
365   if( pp[0] ){
366     ((void **)(pp[0]))[1] = p;
367   }else{
368     sqlite3_pFirst = p;
369   }
370   if( pp[1] ){
371     ((void **)(pp[1]))[0] = p;
372   }
373 }
374 #else
375 #define linkAlloc(x)
376 #define relinkAlloc(x)
377 #define unlinkAlloc(x)
378 #endif
379 
380 /*
381 ** This function sets the result of the Tcl interpreter passed as an argument
382 ** to a list containing an entry for each currently outstanding call made to
383 ** sqliteMalloc and friends by the current thread. Each list entry is itself a
384 ** list, consisting of the following (in order):
385 **
386 **     * The number of bytes allocated
387 **     * The __FILE__ macro at the time of the sqliteMalloc() call.
388 **     * The __LINE__ macro ...
389 **     * The value of the sqlite3_malloc_id variable ...
390 **     * The output of backtrace() (if available) ...
391 **
392 ** Todo: We could have a version of this function that outputs to stdout,
393 ** to debug memory leaks when Tcl is not available.
394 */
395 #if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
396 #include <tcl.h>
397 int sqlite3OutstandingMallocs(Tcl_Interp *interp){
398   void *p;
399   Tcl_Obj *pRes = Tcl_NewObj();
400   Tcl_IncrRefCount(pRes);
401 
402 
403   for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
404     Tcl_Obj *pEntry = Tcl_NewObj();
405     Tcl_Obj *pStack = Tcl_NewObj();
406     char *z;
407     u32 iLine;
408     int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
409     char *zAlloc = (char *)p;
410     int i;
411 
412     Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
413 
414     z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
415     Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
416 
417     z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
418     memcpy(&iLine, z, sizeof(u32));
419     Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
420 
421     z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
422     Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
423 
424     z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
425     for(i=0; i<TESTALLOC_STACKFRAMES; i++){
426       char zHex[128];
427       sprintf(zHex, "%p", ((void **)z)[i]);
428       Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
429     }
430 
431     Tcl_ListObjAppendElement(0, pEntry, pStack);
432     Tcl_ListObjAppendElement(0, pRes, pEntry);
433   }
434 
435   Tcl_ResetResult(interp);
436   Tcl_SetObjResult(interp, pRes);
437   Tcl_DecrRefCount(pRes);
438   return TCL_OK;
439 }
440 #endif
441 
442 /*
443 ** This is the test layer's wrapper around sqlite3OsMalloc().
444 */
445 static void * OSMALLOC(int n){
446   sqlite3OsEnterMutex();
447 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
448   sqlite3_nMaxAlloc =
449       MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
450 #endif
451   assert( !sqlite3_mallocDisallowed );
452   if( !sqlite3TestMallocFail() ){
453     u32 *p;
454     p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
455     assert(p);
456     sqlite3_nMalloc++;
457     applyGuards(p);
458     linkAlloc(p);
459     sqlite3OsLeaveMutex();
460     return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
461   }
462   sqlite3OsLeaveMutex();
463   return 0;
464 }
465 
466 static int OSSIZEOF(void *p){
467   if( p ){
468     u32 *pOs = (u32 *)getOsPointer(p);
469     return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
470   }
471   return 0;
472 }
473 
474 /*
475 ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a
476 ** pointer to the space allocated for the application to use.
477 */
478 static void OSFREE(void *pFree){
479   u32 *p;         /* Pointer to the OS-layer allocation */
480   sqlite3OsEnterMutex();
481   p = (u32 *)getOsPointer(pFree);
482   checkGuards(p);
483   unlinkAlloc(p);
484   memset(pFree, 0x55, OSSIZEOF(pFree));
485   sqlite3OsFree(p);
486   sqlite3_nFree++;
487   sqlite3OsLeaveMutex();
488 }
489 
490 /*
491 ** This is the test layer's wrapper around sqlite3OsRealloc().
492 */
493 static void * OSREALLOC(void *pRealloc, int n){
494 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
495   sqlite3_nMaxAlloc =
496       MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
497 #endif
498   assert( !sqlite3_mallocDisallowed );
499   if( !sqlite3TestMallocFail() ){
500     u32 *p = (u32 *)getOsPointer(pRealloc);
501     checkGuards(p);
502     p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
503     applyGuards(p);
504     relinkAlloc(p);
505     return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
506   }
507   return 0;
508 }
509 
510 static void OSMALLOC_FAILED(){
511   sqlite3_isFail = 0;
512 }
513 
514 #else
515 /* Define macros to call the sqlite3OsXXX interface directly if
516 ** the SQLITE_MEMDEBUG macro is not defined.
517 */
518 #define OSMALLOC(x)        sqlite3OsMalloc(x)
519 #define OSREALLOC(x,y)     sqlite3OsRealloc(x,y)
520 #define OSFREE(x)          sqlite3OsFree(x)
521 #define OSSIZEOF(x)        sqlite3OsAllocationSize(x)
522 #define OSMALLOC_FAILED()
523 
524 #endif  /* SQLITE_MEMDEBUG */
525 /*
526 ** End code for memory allocation system test layer.
527 **--------------------------------------------------------------------------*/
528 
529 /*
530 ** This routine is called when we are about to allocate n additional bytes
531 ** of memory.  If the new allocation will put is over the soft allocation
532 ** limit, then invoke sqlite3_release_memory() to try to release some
533 ** memory before continuing with the allocation.
534 **
535 ** This routine also makes sure that the thread-specific-data (TSD) has
536 ** be allocated.  If it has not and can not be allocated, then return
537 ** false.  The updateMemoryUsedCount() routine below will deallocate
538 ** the TSD if it ought to be.
539 **
540 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
541 ** a no-op
542 */
543 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
544 static int enforceSoftLimit(int n){
545   ThreadData *pTsd = sqlite3ThreadData();
546   if( pTsd==0 ){
547     return 0;
548   }
549   assert( pTsd->nAlloc>=0 );
550   if( n>0 && pTsd->nSoftHeapLimit>0 ){
551     while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
552   }
553   return 1;
554 }
555 #else
556 # define enforceSoftLimit(X)  1
557 #endif
558 
559 /*
560 ** Update the count of total outstanding memory that is held in
561 ** thread-specific-data (TSD).  If after this update the TSD is
562 ** no longer being used, then deallocate it.
563 **
564 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
565 ** a no-op
566 */
567 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
568 static void updateMemoryUsedCount(int n){
569   ThreadData *pTsd = sqlite3ThreadData();
570   if( pTsd ){
571     pTsd->nAlloc += n;
572     assert( pTsd->nAlloc>=0 );
573     if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
574       sqlite3ReleaseThreadData();
575     }
576   }
577 }
578 #else
579 #define updateMemoryUsedCount(x)  /* no-op */
580 #endif
581 
582 /*
583 ** Allocate and return N bytes of uninitialised memory by calling
584 ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory
585 ** by calling sqlite3_release_memory().
586 */
587 void *sqlite3MallocRaw(int n, int doMemManage){
588   void *p = 0;
589   if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
590     while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
591     if( !p ){
592       sqlite3FailedMalloc();
593       OSMALLOC_FAILED();
594     }else if( doMemManage ){
595       updateMemoryUsedCount(OSSIZEOF(p));
596     }
597   }
598   return p;
599 }
600 
601 /*
602 ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
603 ** pointer to the new allocation is returned.  If the Realloc() call fails,
604 ** attempt to free memory by calling sqlite3_release_memory().
605 */
606 void *sqlite3Realloc(void *p, int n){
607   if( sqlite3MallocFailed() ){
608     return 0;
609   }
610 
611   if( !p ){
612     return sqlite3Malloc(n, 1);
613   }else{
614     void *np = 0;
615 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
616     int origSize = OSSIZEOF(p);
617 #endif
618     if( enforceSoftLimit(n - origSize) ){
619       while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
620       if( !np ){
621         sqlite3FailedMalloc();
622         OSMALLOC_FAILED();
623       }else{
624         updateMemoryUsedCount(OSSIZEOF(np) - origSize);
625       }
626     }
627     return np;
628   }
629 }
630 
631 /*
632 ** Free the memory pointed to by p. p must be either a NULL pointer or a
633 ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
634 */
635 void sqlite3FreeX(void *p){
636   if( p ){
637     updateMemoryUsedCount(0 - OSSIZEOF(p));
638     OSFREE(p);
639   }
640 }
641 
642 /*
643 ** A version of sqliteMalloc() that is always a function, not a macro.
644 ** Currently, this is used only to alloc to allocate the parser engine.
645 */
646 void *sqlite3MallocX(int n){
647   return sqliteMalloc(n);
648 }
649 
650 /*
651 ** sqlite3Malloc
652 ** sqlite3ReallocOrFree
653 **
654 ** These two are implemented as wrappers around sqlite3MallocRaw(),
655 ** sqlite3Realloc() and sqlite3Free().
656 */
657 void *sqlite3Malloc(int n, int doMemManage){
658   void *p = sqlite3MallocRaw(n, doMemManage);
659   if( p ){
660     memset(p, 0, n);
661   }
662   return p;
663 }
664 void sqlite3ReallocOrFree(void **pp, int n){
665   void *p = sqlite3Realloc(*pp, n);
666   if( !p ){
667     sqlite3FreeX(*pp);
668   }
669   *pp = p;
670 }
671 
672 /*
673 ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
674 ** rare scenarios where sqlite may allocate memory in one thread and free
675 ** it in another. They are exactly the same as sqlite3Malloc() and
676 ** sqlite3Free() except that:
677 **
678 **   * The allocated memory is not included in any calculations with
679 **     respect to the soft-heap-limit, and
680 **
681 **   * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),
682 **     not sqlite3Free(). Calling sqlite3Free() on memory obtained from
683 **     ThreadSafeMalloc() will cause an error somewhere down the line.
684 */
685 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
686 void *sqlite3ThreadSafeMalloc(int n){
687   (void)ENTER_MALLOC;
688   return sqlite3Malloc(n, 0);
689 }
690 void sqlite3ThreadSafeFree(void *p){
691   (void)ENTER_MALLOC;
692   if( p ){
693     OSFREE(p);
694   }
695 }
696 #endif
697 
698 
699 /*
700 ** Return the number of bytes allocated at location p. p must be either
701 ** a NULL pointer (in which case 0 is returned) or a pointer returned by
702 ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
703 **
704 ** The number of bytes allocated does not include any overhead inserted by
705 ** any malloc() wrapper functions that may be called. So the value returned
706 ** is the number of bytes that were available to SQLite using pointer p,
707 ** regardless of how much memory was actually allocated.
708 */
709 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
710 int sqlite3AllocSize(void *p){
711   return OSSIZEOF(p);
712 }
713 #endif
714 
715 /*
716 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
717 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
718 ** is because when memory debugging is turned on, these two functions are
719 ** called via macros that record the current file and line number in the
720 ** ThreadData structure.
721 */
722 char *sqlite3StrDup(const char *z){
723   char *zNew;
724   if( z==0 ) return 0;
725   zNew = sqlite3MallocRaw(strlen(z)+1, 1);
726   if( zNew ) strcpy(zNew, z);
727   return zNew;
728 }
729 char *sqlite3StrNDup(const char *z, int n){
730   char *zNew;
731   if( z==0 ) return 0;
732   zNew = sqlite3MallocRaw(n+1, 1);
733   if( zNew ){
734     memcpy(zNew, z, n);
735     zNew[n] = 0;
736   }
737   return zNew;
738 }
739 
740 /*
741 ** Create a string from the 2nd and subsequent arguments (up to the
742 ** first NULL argument), store the string in memory obtained from
743 ** sqliteMalloc() and make the pointer indicated by the 1st argument
744 ** point to that string.  The 1st argument must either be NULL or
745 ** point to memory obtained from sqliteMalloc().
746 */
747 void sqlite3SetString(char **pz, ...){
748   va_list ap;
749   int nByte;
750   const char *z;
751   char *zResult;
752 
753   if( pz==0 ) return;
754   nByte = 1;
755   va_start(ap, pz);
756   while( (z = va_arg(ap, const char*))!=0 ){
757     nByte += strlen(z);
758   }
759   va_end(ap);
760   sqliteFree(*pz);
761   *pz = zResult = sqliteMallocRaw( nByte );
762   if( zResult==0 ){
763     return;
764   }
765   *zResult = 0;
766   va_start(ap, pz);
767   while( (z = va_arg(ap, const char*))!=0 ){
768     strcpy(zResult, z);
769     zResult += strlen(zResult);
770   }
771   va_end(ap);
772 }
773 
774 /*
775 ** Set the most recent error code and error string for the sqlite
776 ** handle "db". The error code is set to "err_code".
777 **
778 ** If it is not NULL, string zFormat specifies the format of the
779 ** error string in the style of the printf functions: The following
780 ** format characters are allowed:
781 **
782 **      %s      Insert a string
783 **      %z      A string that should be freed after use
784 **      %d      Insert an integer
785 **      %T      Insert a token
786 **      %S      Insert the first element of a SrcList
787 **
788 ** zFormat and any string tokens that follow it are assumed to be
789 ** encoded in UTF-8.
790 **
791 ** To clear the most recent error for sqlite handle "db", sqlite3Error
792 ** should be called with err_code set to SQLITE_OK and zFormat set
793 ** to NULL.
794 */
795 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
796   if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
797     db->errCode = err_code;
798     if( zFormat ){
799       char *z;
800       va_list ap;
801       va_start(ap, zFormat);
802       z = sqlite3VMPrintf(zFormat, ap);
803       va_end(ap);
804       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
805     }else{
806       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
807     }
808   }
809 }
810 
811 /*
812 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
813 ** The following formatting characters are allowed:
814 **
815 **      %s      Insert a string
816 **      %z      A string that should be freed after use
817 **      %d      Insert an integer
818 **      %T      Insert a token
819 **      %S      Insert the first element of a SrcList
820 **
821 ** This function should be used to report any error that occurs whilst
822 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
823 ** last thing the sqlite3_prepare() function does is copy the error
824 ** stored by this function into the database handle using sqlite3Error().
825 ** Function sqlite3Error() should be used during statement execution
826 ** (sqlite3_step() etc.).
827 */
828 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
829   va_list ap;
830   pParse->nErr++;
831   sqliteFree(pParse->zErrMsg);
832   va_start(ap, zFormat);
833   pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
834   va_end(ap);
835 }
836 
837 /*
838 ** Clear the error message in pParse, if any
839 */
840 void sqlite3ErrorClear(Parse *pParse){
841   sqliteFree(pParse->zErrMsg);
842   pParse->zErrMsg = 0;
843   pParse->nErr = 0;
844 }
845 
846 /*
847 ** Convert an SQL-style quoted string into a normal string by removing
848 ** the quote characters.  The conversion is done in-place.  If the
849 ** input does not begin with a quote character, then this routine
850 ** is a no-op.
851 **
852 ** 2002-Feb-14: This routine is extended to remove MS-Access style
853 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
854 ** "a-b-c".
855 */
856 void sqlite3Dequote(char *z){
857   int quote;
858   int i, j;
859   if( z==0 ) return;
860   quote = z[0];
861   switch( quote ){
862     case '\'':  break;
863     case '"':   break;
864     case '`':   break;                /* For MySQL compatibility */
865     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
866     default:    return;
867   }
868   for(i=1, j=0; z[i]; i++){
869     if( z[i]==quote ){
870       if( z[i+1]==quote ){
871         z[j++] = quote;
872         i++;
873       }else{
874         z[j++] = 0;
875         break;
876       }
877     }else{
878       z[j++] = z[i];
879     }
880   }
881 }
882 
883 /* An array to map all upper-case characters into their corresponding
884 ** lower-case character.
885 */
886 const unsigned char sqlite3UpperToLower[] = {
887 #ifdef SQLITE_ASCII
888       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
889      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
890      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
891      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
892     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
893     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
894     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
895     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
896     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
897     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
898     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
899     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
900     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
901     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
902     252,253,254,255
903 #endif
904 #ifdef SQLITE_EBCDIC
905       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
906      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
907      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
908      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
909      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
910      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
911      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
912     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
913     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
914     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
915     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
916     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
917     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
918     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
919     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
920     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
921 #endif
922 };
923 #define UpperToLower sqlite3UpperToLower
924 
925 /*
926 ** Some systems have stricmp().  Others have strcasecmp().  Because
927 ** there is no consistency, we will define our own.
928 */
929 int sqlite3StrICmp(const char *zLeft, const char *zRight){
930   register unsigned char *a, *b;
931   a = (unsigned char *)zLeft;
932   b = (unsigned char *)zRight;
933   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
934   return UpperToLower[*a] - UpperToLower[*b];
935 }
936 int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
937   register unsigned char *a, *b;
938   a = (unsigned char *)zLeft;
939   b = (unsigned char *)zRight;
940   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
941   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
942 }
943 
944 /*
945 ** Return TRUE if z is a pure numeric string.  Return FALSE if the
946 ** string contains any character which is not part of a number. If
947 ** the string is numeric and contains the '.' character, set *realnum
948 ** to TRUE (otherwise FALSE).
949 **
950 ** An empty string is considered non-numeric.
951 */
952 int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
953   int incr = (enc==SQLITE_UTF8?1:2);
954   if( enc==SQLITE_UTF16BE ) z++;
955   if( *z=='-' || *z=='+' ) z += incr;
956   if( !isdigit(*(u8*)z) ){
957     return 0;
958   }
959   z += incr;
960   if( realnum ) *realnum = 0;
961   while( isdigit(*(u8*)z) ){ z += incr; }
962   if( *z=='.' ){
963     z += incr;
964     if( !isdigit(*(u8*)z) ) return 0;
965     while( isdigit(*(u8*)z) ){ z += incr; }
966     if( realnum ) *realnum = 1;
967   }
968   if( *z=='e' || *z=='E' ){
969     z += incr;
970     if( *z=='+' || *z=='-' ) z += incr;
971     if( !isdigit(*(u8*)z) ) return 0;
972     while( isdigit(*(u8*)z) ){ z += incr; }
973     if( realnum ) *realnum = 1;
974   }
975   return *z==0;
976 }
977 
978 /*
979 ** The string z[] is an ascii representation of a real number.
980 ** Convert this string to a double.
981 **
982 ** This routine assumes that z[] really is a valid number.  If it
983 ** is not, the result is undefined.
984 **
985 ** This routine is used instead of the library atof() function because
986 ** the library atof() might want to use "," as the decimal point instead
987 ** of "." depending on how locale is set.  But that would cause problems
988 ** for SQL.  So this routine always uses "." regardless of locale.
989 */
990 int sqlite3AtoF(const char *z, double *pResult){
991 #ifndef SQLITE_OMIT_FLOATING_POINT
992   int sign = 1;
993   const char *zBegin = z;
994   LONGDOUBLE_TYPE v1 = 0.0;
995   while( isspace(*z) ) z++;
996   if( *z=='-' ){
997     sign = -1;
998     z++;
999   }else if( *z=='+' ){
1000     z++;
1001   }
1002   while( isdigit(*(u8*)z) ){
1003     v1 = v1*10.0 + (*z - '0');
1004     z++;
1005   }
1006   if( *z=='.' ){
1007     LONGDOUBLE_TYPE divisor = 1.0;
1008     z++;
1009     while( isdigit(*(u8*)z) ){
1010       v1 = v1*10.0 + (*z - '0');
1011       divisor *= 10.0;
1012       z++;
1013     }
1014     v1 /= divisor;
1015   }
1016   if( *z=='e' || *z=='E' ){
1017     int esign = 1;
1018     int eval = 0;
1019     LONGDOUBLE_TYPE scale = 1.0;
1020     z++;
1021     if( *z=='-' ){
1022       esign = -1;
1023       z++;
1024     }else if( *z=='+' ){
1025       z++;
1026     }
1027     while( isdigit(*(u8*)z) ){
1028       eval = eval*10 + *z - '0';
1029       z++;
1030     }
1031     while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
1032     while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
1033     while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
1034     while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
1035     if( esign<0 ){
1036       v1 /= scale;
1037     }else{
1038       v1 *= scale;
1039     }
1040   }
1041   *pResult = sign<0 ? -v1 : v1;
1042   return z - zBegin;
1043 #else
1044   return sqlite3atoi64(z, pResult);
1045 #endif /* SQLITE_OMIT_FLOATING_POINT */
1046 }
1047 
1048 /*
1049 ** Return TRUE if zNum is a 64-bit signed integer and write
1050 ** the value of the integer into *pNum.  If zNum is not an integer
1051 ** or is an integer that is too large to be expressed with 64 bits,
1052 ** then return false.  If n>0 and the integer is string is not
1053 ** exactly n bytes long, return false.
1054 **
1055 ** When this routine was originally written it dealt with only
1056 ** 32-bit numbers.  At that time, it was much faster than the
1057 ** atoi() library routine in RedHat 7.2.
1058 */
1059 int sqlite3atoi64(const char *zNum, i64 *pNum){
1060   i64 v = 0;
1061   int neg;
1062   int i, c;
1063   while( isspace(*zNum) ) zNum++;
1064   if( *zNum=='-' ){
1065     neg = 1;
1066     zNum++;
1067   }else if( *zNum=='+' ){
1068     neg = 0;
1069     zNum++;
1070   }else{
1071     neg = 0;
1072   }
1073   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
1074     v = v*10 + c - '0';
1075   }
1076   *pNum = neg ? -v : v;
1077   return c==0 && i>0 &&
1078       (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
1079 }
1080 
1081 /*
1082 ** The string zNum represents an integer.  There might be some other
1083 ** information following the integer too, but that part is ignored.
1084 ** If the integer that the prefix of zNum represents will fit in a
1085 ** 32-bit signed integer, return TRUE.  Otherwise return FALSE.
1086 **
1087 ** This routine returns FALSE for the string -2147483648 even that
1088 ** that number will in fact fit in a 32-bit integer.  But positive
1089 ** 2147483648 will not fit in 32 bits.  So it seems safer to return
1090 ** false.
1091 */
1092 static int sqlite3FitsIn32Bits(const char *zNum){
1093   int i, c;
1094   if( *zNum=='-' || *zNum=='+' ) zNum++;
1095   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
1096   return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
1097 }
1098 
1099 /*
1100 ** If zNum represents an integer that will fit in 32-bits, then set
1101 ** *pValue to that integer and return true.  Otherwise return false.
1102 */
1103 int sqlite3GetInt32(const char *zNum, int *pValue){
1104   if( sqlite3FitsIn32Bits(zNum) ){
1105     *pValue = atoi(zNum);
1106     return 1;
1107   }
1108   return 0;
1109 }
1110 
1111 /*
1112 ** The string zNum represents an integer.  There might be some other
1113 ** information following the integer too, but that part is ignored.
1114 ** If the integer that the prefix of zNum represents will fit in a
1115 ** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
1116 **
1117 ** This routine returns FALSE for the string -9223372036854775808 even that
1118 ** that number will, in theory fit in a 64-bit integer.  Positive
1119 ** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
1120 ** false.
1121 */
1122 int sqlite3FitsIn64Bits(const char *zNum){
1123   int i, c;
1124   if( *zNum=='-' || *zNum=='+' ) zNum++;
1125   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
1126   return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
1127 }
1128 
1129 
1130 /*
1131 ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1132 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1133 ** when this routine is called.
1134 **
1135 ** This routine is a attempt to detect if two threads use the
1136 ** same sqlite* pointer at the same time.  There is a race
1137 ** condition so it is possible that the error is not detected.
1138 ** But usually the problem will be seen.  The result will be an
1139 ** error which can be used to debug the application that is
1140 ** using SQLite incorrectly.
1141 **
1142 ** Ticket #202:  If db->magic is not a valid open value, take care not
1143 ** to modify the db structure at all.  It could be that db is a stale
1144 ** pointer.  In other words, it could be that there has been a prior
1145 ** call to sqlite3_close(db) and db has been deallocated.  And we do
1146 ** not want to write into deallocated memory.
1147 */
1148 int sqlite3SafetyOn(sqlite3 *db){
1149   if( db->magic==SQLITE_MAGIC_OPEN ){
1150     db->magic = SQLITE_MAGIC_BUSY;
1151     return 0;
1152   }else if( db->magic==SQLITE_MAGIC_BUSY ){
1153     db->magic = SQLITE_MAGIC_ERROR;
1154     db->u1.isInterrupted = 1;
1155   }
1156   return 1;
1157 }
1158 
1159 /*
1160 ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1161 ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1162 ** when this routine is called.
1163 */
1164 int sqlite3SafetyOff(sqlite3 *db){
1165   if( db->magic==SQLITE_MAGIC_BUSY ){
1166     db->magic = SQLITE_MAGIC_OPEN;
1167     return 0;
1168   }else if( db->magic==SQLITE_MAGIC_OPEN ){
1169     db->magic = SQLITE_MAGIC_ERROR;
1170     db->u1.isInterrupted = 1;
1171   }
1172   return 1;
1173 }
1174 
1175 /*
1176 ** Check to make sure we have a valid db pointer.  This test is not
1177 ** foolproof but it does provide some measure of protection against
1178 ** misuse of the interface such as passing in db pointers that are
1179 ** NULL or which have been previously closed.  If this routine returns
1180 ** TRUE it means that the db pointer is invalid and should not be
1181 ** dereferenced for any reason.  The calling function should invoke
1182 ** SQLITE_MISUSE immediately.
1183 */
1184 int sqlite3SafetyCheck(sqlite3 *db){
1185   int magic;
1186   if( db==0 ) return 1;
1187   magic = db->magic;
1188   if( magic!=SQLITE_MAGIC_CLOSED &&
1189          magic!=SQLITE_MAGIC_OPEN &&
1190          magic!=SQLITE_MAGIC_BUSY ) return 1;
1191   return 0;
1192 }
1193 
1194 /*
1195 ** The variable-length integer encoding is as follows:
1196 **
1197 ** KEY:
1198 **         A = 0xxxxxxx    7 bits of data and one flag bit
1199 **         B = 1xxxxxxx    7 bits of data and one flag bit
1200 **         C = xxxxxxxx    8 bits of data
1201 **
1202 **  7 bits - A
1203 ** 14 bits - BA
1204 ** 21 bits - BBA
1205 ** 28 bits - BBBA
1206 ** 35 bits - BBBBA
1207 ** 42 bits - BBBBBA
1208 ** 49 bits - BBBBBBA
1209 ** 56 bits - BBBBBBBA
1210 ** 64 bits - BBBBBBBBC
1211 */
1212 
1213 /*
1214 ** Write a 64-bit variable-length integer to memory starting at p[0].
1215 ** The length of data write will be between 1 and 9 bytes.  The number
1216 ** of bytes written is returned.
1217 **
1218 ** A variable-length integer consists of the lower 7 bits of each byte
1219 ** for all bytes that have the 8th bit set and one byte with the 8th
1220 ** bit clear.  Except, if we get to the 9th byte, it stores the full
1221 ** 8 bits and is the last byte.
1222 */
1223 int sqlite3PutVarint(unsigned char *p, u64 v){
1224   int i, j, n;
1225   u8 buf[10];
1226   if( v & (((u64)0xff000000)<<32) ){
1227     p[8] = v;
1228     v >>= 8;
1229     for(i=7; i>=0; i--){
1230       p[i] = (v & 0x7f) | 0x80;
1231       v >>= 7;
1232     }
1233     return 9;
1234   }
1235   n = 0;
1236   do{
1237     buf[n++] = (v & 0x7f) | 0x80;
1238     v >>= 7;
1239   }while( v!=0 );
1240   buf[0] &= 0x7f;
1241   assert( n<=9 );
1242   for(i=0, j=n-1; j>=0; j--, i++){
1243     p[i] = buf[j];
1244   }
1245   return n;
1246 }
1247 
1248 /*
1249 ** Read a 64-bit variable-length integer from memory starting at p[0].
1250 ** Return the number of bytes read.  The value is stored in *v.
1251 */
1252 int sqlite3GetVarint(const unsigned char *p, u64 *v){
1253   u32 x;
1254   u64 x64;
1255   int n;
1256   unsigned char c;
1257   if( ((c = p[0]) & 0x80)==0 ){
1258     *v = c;
1259     return 1;
1260   }
1261   x = c & 0x7f;
1262   if( ((c = p[1]) & 0x80)==0 ){
1263     *v = (x<<7) | c;
1264     return 2;
1265   }
1266   x = (x<<7) | (c&0x7f);
1267   if( ((c = p[2]) & 0x80)==0 ){
1268     *v = (x<<7) | c;
1269     return 3;
1270   }
1271   x = (x<<7) | (c&0x7f);
1272   if( ((c = p[3]) & 0x80)==0 ){
1273     *v = (x<<7) | c;
1274     return 4;
1275   }
1276   x64 = (x<<7) | (c&0x7f);
1277   n = 4;
1278   do{
1279     c = p[n++];
1280     if( n==9 ){
1281       x64 = (x64<<8) | c;
1282       break;
1283     }
1284     x64 = (x64<<7) | (c&0x7f);
1285   }while( (c & 0x80)!=0 );
1286   *v = x64;
1287   return n;
1288 }
1289 
1290 /*
1291 ** Read a 32-bit variable-length integer from memory starting at p[0].
1292 ** Return the number of bytes read.  The value is stored in *v.
1293 */
1294 int sqlite3GetVarint32(const unsigned char *p, u32 *v){
1295   u32 x;
1296   int n;
1297   unsigned char c;
1298   if( ((signed char*)p)[0]>=0 ){
1299     *v = p[0];
1300     return 1;
1301   }
1302   x = p[0] & 0x7f;
1303   if( ((signed char*)p)[1]>=0 ){
1304     *v = (x<<7) | p[1];
1305     return 2;
1306   }
1307   x = (x<<7) | (p[1] & 0x7f);
1308   n = 2;
1309   do{
1310     x = (x<<7) | ((c = p[n++])&0x7f);
1311   }while( (c & 0x80)!=0 && n<9 );
1312   *v = x;
1313   return n;
1314 }
1315 
1316 /*
1317 ** Return the number of bytes that will be needed to store the given
1318 ** 64-bit integer.
1319 */
1320 int sqlite3VarintLen(u64 v){
1321   int i = 0;
1322   do{
1323     i++;
1324     v >>= 7;
1325   }while( v!=0 && i<9 );
1326   return i;
1327 }
1328 
1329 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
1330     || defined(SQLITE_TEST)
1331 /*
1332 ** Translate a single byte of Hex into an integer.
1333 */
1334 static int hexToInt(int h){
1335   if( h>='0' && h<='9' ){
1336     return h - '0';
1337   }else if( h>='a' && h<='f' ){
1338     return h - 'a' + 10;
1339   }else{
1340     assert( h>='A' && h<='F' );
1341     return h - 'A' + 10;
1342   }
1343 }
1344 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
1345 
1346 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1347 /*
1348 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1349 ** value.  Return a pointer to its binary value.  Space to hold the
1350 ** binary value has been obtained from malloc and must be freed by
1351 ** the calling routine.
1352 */
1353 void *sqlite3HexToBlob(const char *z){
1354   char *zBlob;
1355   int i;
1356   int n = strlen(z);
1357   if( n%2 ) return 0;
1358 
1359   zBlob = (char *)sqliteMalloc(n/2);
1360   if( zBlob ){
1361     for(i=0; i<n; i+=2){
1362       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
1363     }
1364   }
1365   return zBlob;
1366 }
1367 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1368 
1369 #if defined(SQLITE_TEST)
1370 /*
1371 ** Convert text generated by the "%p" conversion format back into
1372 ** a pointer.
1373 */
1374 void *sqlite3TextToPtr(const char *z){
1375   void *p;
1376   u64 v;
1377   u32 v2;
1378   if( z[0]=='0' && z[1]=='x' ){
1379     z += 2;
1380   }
1381   v = 0;
1382   while( *z ){
1383     v = (v<<4) + hexToInt(*z);
1384     z++;
1385   }
1386   if( sizeof(p)==sizeof(v) ){
1387     p = *(void**)&v;
1388   }else{
1389     assert( sizeof(p)==sizeof(v2) );
1390     v2 = (u32)v;
1391     p = *(void**)&v2;
1392   }
1393   return p;
1394 }
1395 #endif
1396 
1397 /*
1398 ** Return a pointer to the ThreadData associated with the calling thread.
1399 */
1400 ThreadData *sqlite3ThreadData(){
1401   ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
1402   if( !p ){
1403     sqlite3FailedMalloc();
1404   }
1405   return p;
1406 }
1407 
1408 /*
1409 ** Return a pointer to the ThreadData associated with the calling thread.
1410 ** If no ThreadData has been allocated to this thread yet, return a pointer
1411 ** to a substitute ThreadData structure that is all zeros.
1412 */
1413 const ThreadData *sqlite3ThreadDataReadOnly(){
1414   static const ThreadData zeroData = {0};  /* Initializer to silence warnings
1415                                            ** from broken compilers */
1416   const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
1417   return pTd ? pTd : &zeroData;
1418 }
1419 
1420 /*
1421 ** Check to see if the ThreadData for this thread is all zero.  If it
1422 ** is, then deallocate it.
1423 */
1424 void sqlite3ReleaseThreadData(){
1425   sqlite3OsThreadSpecificData(-1);
1426 }
1427 
1428 /*
1429 ** This function must be called before exiting any API function (i.e.
1430 ** returning control to the user) that has called sqlite3Malloc or
1431 ** sqlite3Realloc.
1432 **
1433 ** The returned value is normally a copy of the second argument to this
1434 ** function. However, if a malloc() failure has occured since the previous
1435 ** invocation SQLITE_NOMEM is returned instead.
1436 **
1437 ** If the first argument, db, is not NULL and a malloc() error has occured,
1438 ** then the connection error-code (the value returned by sqlite3_errcode())
1439 ** is set to SQLITE_NOMEM.
1440 */
1441 static int mallocHasFailed = 0;
1442 int sqlite3ApiExit(sqlite3* db, int rc){
1443   if( sqlite3MallocFailed() ){
1444     mallocHasFailed = 0;
1445     sqlite3OsLeaveMutex();
1446     sqlite3Error(db, SQLITE_NOMEM, 0);
1447     rc = SQLITE_NOMEM;
1448   }
1449   return rc & (db ? db->errMask : 0xff);
1450 }
1451 
1452 /*
1453 ** Return true is a malloc has failed in this thread since the last call
1454 ** to sqlite3ApiExit(), or false otherwise.
1455 */
1456 int sqlite3MallocFailed(){
1457   return (mallocHasFailed && sqlite3OsInMutex(1));
1458 }
1459 
1460 /*
1461 ** Set the "malloc has failed" condition to true for this thread.
1462 */
1463 void sqlite3FailedMalloc(){
1464   sqlite3OsEnterMutex();
1465   assert( mallocHasFailed==0 );
1466   mallocHasFailed = 1;
1467 }
1468 
1469 #ifdef SQLITE_MEMDEBUG
1470 /*
1471 ** This function sets a flag in the thread-specific-data structure that will
1472 ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
1473 */
1474 void sqlite3MallocDisallow(){
1475   assert( sqlite3_mallocDisallowed>=0 );
1476   sqlite3_mallocDisallowed++;
1477 }
1478 
1479 /*
1480 ** This function clears the flag set in the thread-specific-data structure set
1481 ** by sqlite3MallocDisallow().
1482 */
1483 void sqlite3MallocAllow(){
1484   assert( sqlite3_mallocDisallowed>0 );
1485   sqlite3_mallocDisallowed--;
1486 }
1487 #endif
1488