Home | History | Annotate | Download | only in amalgamation
      1 /******************************************************************************
      2 ** This file is an amalgamation of many separate C source files from SQLite
      3 ** version 3.7.6.3.  By combining all the individual C code files into this
      4 ** single large file, the entire code can be compiled as a single translation
      5 ** unit.  This allows many compilers to do optimizations that would not be
      6 ** possible if the files were compiled separately.  Performance improvements
      7 ** of 5% or more are commonly seen when SQLite is compiled as a single
      8 ** translation unit.
      9 **
     10 ** This file is all you need to compile SQLite.  To use SQLite in other
     11 ** programs, you need this file and the "sqlite3.h" header file that defines
     12 ** the programming interface to the SQLite library.  (If you do not have
     13 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
     14 ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
     15 ** of the embedded sqlite3.h header file.) Additional code files may be needed
     16 ** if you want a wrapper to interface SQLite with your choice of programming
     17 ** language. The code for the "sqlite3" command-line shell is also in a
     18 ** separate file. This file contains only code for the core SQLite library.
     19 */
     20 #define SQLITE_CORE 1
     21 #define SQLITE_AMALGAMATION 1
     22 #ifndef SQLITE_PRIVATE
     23 # define SQLITE_PRIVATE static
     24 #endif
     25 #ifndef SQLITE_API
     26 # define SQLITE_API
     27 #endif
     28 /************** Begin file sqliteInt.h ***************************************/
     29 /*
     30 ** 2001 September 15
     31 **
     32 ** The author disclaims copyright to this source code.  In place of
     33 ** a legal notice, here is a blessing:
     34 **
     35 **    May you do good and not evil.
     36 **    May you find forgiveness for yourself and forgive others.
     37 **    May you share freely, never taking more than you give.
     38 **
     39 *************************************************************************
     40 ** Internal interface definitions for SQLite.
     41 **
     42 */
     43 #ifndef _SQLITEINT_H_
     44 #define _SQLITEINT_H_
     45 
     46 /*
     47 ** These #defines should enable >2GB file support on POSIX if the
     48 ** underlying operating system supports it.  If the OS lacks
     49 ** large file support, or if the OS is windows, these should be no-ops.
     50 **
     51 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
     52 ** system #includes.  Hence, this block of code must be the very first
     53 ** code in all source files.
     54 **
     55 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
     56 ** on the compiler command line.  This is necessary if you are compiling
     57 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
     58 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
     59 ** without this option, LFS is enable.  But LFS does not exist in the kernel
     60 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
     61 ** portability you should omit LFS.
     62 **
     63 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
     64 */
     65 #ifndef SQLITE_DISABLE_LFS
     66 # define _LARGE_FILE       1
     67 # ifndef _FILE_OFFSET_BITS
     68 #   define _FILE_OFFSET_BITS 64
     69 # endif
     70 # define _LARGEFILE_SOURCE 1
     71 #endif
     72 
     73 /*
     74 ** Include the configuration header output by 'configure' if we're using the
     75 ** autoconf-based build
     76 */
     77 #ifdef _HAVE_SQLITE_CONFIG_H
     78 #include "config.h"
     79 #endif
     80 
     81 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
     82 /************** Begin file sqliteLimit.h *************************************/
     83 /*
     84 ** 2007 May 7
     85 **
     86 ** The author disclaims copyright to this source code.  In place of
     87 ** a legal notice, here is a blessing:
     88 **
     89 **    May you do good and not evil.
     90 **    May you find forgiveness for yourself and forgive others.
     91 **    May you share freely, never taking more than you give.
     92 **
     93 *************************************************************************
     94 **
     95 ** This file defines various limits of what SQLite can process.
     96 */
     97 
     98 /*
     99 ** The maximum length of a TEXT or BLOB in bytes.   This also
    100 ** limits the size of a row in a table or index.
    101 **
    102 ** The hard limit is the ability of a 32-bit signed integer
    103 ** to count the size: 2^31-1 or 2147483647.
    104 */
    105 #ifndef SQLITE_MAX_LENGTH
    106 # define SQLITE_MAX_LENGTH 1000000000
    107 #endif
    108 
    109 /*
    110 ** This is the maximum number of
    111 **
    112 **    * Columns in a table
    113 **    * Columns in an index
    114 **    * Columns in a view
    115 **    * Terms in the SET clause of an UPDATE statement
    116 **    * Terms in the result set of a SELECT statement
    117 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
    118 **    * Terms in the VALUES clause of an INSERT statement
    119 **
    120 ** The hard upper limit here is 32676.  Most database people will
    121 ** tell you that in a well-normalized database, you usually should
    122 ** not have more than a dozen or so columns in any table.  And if
    123 ** that is the case, there is no point in having more than a few
    124 ** dozen values in any of the other situations described above.
    125 */
    126 #ifndef SQLITE_MAX_COLUMN
    127 # define SQLITE_MAX_COLUMN 2000
    128 #endif
    129 
    130 /*
    131 ** The maximum length of a single SQL statement in bytes.
    132 **
    133 ** It used to be the case that setting this value to zero would
    134 ** turn the limit off.  That is no longer true.  It is not possible
    135 ** to turn this limit off.
    136 */
    137 #ifndef SQLITE_MAX_SQL_LENGTH
    138 # define SQLITE_MAX_SQL_LENGTH 1000000000
    139 #endif
    140 
    141 /*
    142 ** The maximum depth of an expression tree. This is limited to
    143 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
    144 ** want to place more severe limits on the complexity of an
    145 ** expression.
    146 **
    147 ** A value of 0 used to mean that the limit was not enforced.
    148 ** But that is no longer true.  The limit is now strictly enforced
    149 ** at all times.
    150 */
    151 #ifndef SQLITE_MAX_EXPR_DEPTH
    152 # define SQLITE_MAX_EXPR_DEPTH 1000
    153 #endif
    154 
    155 /*
    156 ** The maximum number of terms in a compound SELECT statement.
    157 ** The code generator for compound SELECT statements does one
    158 ** level of recursion for each term.  A stack overflow can result
    159 ** if the number of terms is too large.  In practice, most SQL
    160 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
    161 ** any limit on the number of terms in a compount SELECT.
    162 */
    163 #ifndef SQLITE_MAX_COMPOUND_SELECT
    164 # define SQLITE_MAX_COMPOUND_SELECT 500
    165 #endif
    166 
    167 /*
    168 ** The maximum number of opcodes in a VDBE program.
    169 ** Not currently enforced.
    170 */
    171 #ifndef SQLITE_MAX_VDBE_OP
    172 # define SQLITE_MAX_VDBE_OP 25000
    173 #endif
    174 
    175 /*
    176 ** The maximum number of arguments to an SQL function.
    177 */
    178 #ifndef SQLITE_MAX_FUNCTION_ARG
    179 # define SQLITE_MAX_FUNCTION_ARG 127
    180 #endif
    181 
    182 /*
    183 ** The maximum number of in-memory pages to use for the main database
    184 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
    185 */
    186 #ifndef SQLITE_DEFAULT_CACHE_SIZE
    187 # define SQLITE_DEFAULT_CACHE_SIZE  2000
    188 #endif
    189 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
    190 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
    191 #endif
    192 
    193 /*
    194 ** The default number of frames to accumulate in the log file before
    195 ** checkpointing the database in WAL mode.
    196 */
    197 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
    198 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
    199 #endif
    200 
    201 /*
    202 ** The maximum number of attached databases.  This must be between 0
    203 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
    204 ** is used internally to track attached databases.
    205 */
    206 #ifndef SQLITE_MAX_ATTACHED
    207 # define SQLITE_MAX_ATTACHED 10
    208 #endif
    209 
    210 
    211 /*
    212 ** The maximum value of a ?nnn wildcard that the parser will accept.
    213 */
    214 #ifndef SQLITE_MAX_VARIABLE_NUMBER
    215 # define SQLITE_MAX_VARIABLE_NUMBER 999
    216 #endif
    217 
    218 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
    219 ** imposed by the use of 16-bit offsets within each page.
    220 **
    221 ** Earlier versions of SQLite allowed the user to change this value at
    222 ** compile time. This is no longer permitted, on the grounds that it creates
    223 ** a library that is technically incompatible with an SQLite library
    224 ** compiled with a different limit. If a process operating on a database
    225 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
    226 ** compiled with the default page-size limit will not be able to rollback
    227 ** the aborted transaction. This could lead to database corruption.
    228 */
    229 #ifdef SQLITE_MAX_PAGE_SIZE
    230 # undef SQLITE_MAX_PAGE_SIZE
    231 #endif
    232 #define SQLITE_MAX_PAGE_SIZE 65536
    233 
    234 
    235 /*
    236 ** The default size of a database page.
    237 */
    238 #ifndef SQLITE_DEFAULT_PAGE_SIZE
    239 # define SQLITE_DEFAULT_PAGE_SIZE 1024
    240 #endif
    241 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
    242 # undef SQLITE_DEFAULT_PAGE_SIZE
    243 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
    244 #endif
    245 
    246 /*
    247 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
    248 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
    249 ** device characteristics (sector-size and atomic write() support),
    250 ** SQLite may choose a larger value. This constant is the maximum value
    251 ** SQLite will choose on its own.
    252 */
    253 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
    254 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
    255 #endif
    256 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
    257 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
    258 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
    259 #endif
    260 
    261 
    262 /*
    263 ** Maximum number of pages in one database file.
    264 **
    265 ** This is really just the default value for the max_page_count pragma.
    266 ** This value can be lowered (or raised) at run-time using that the
    267 ** max_page_count macro.
    268 */
    269 #ifndef SQLITE_MAX_PAGE_COUNT
    270 # define SQLITE_MAX_PAGE_COUNT 1073741823
    271 #endif
    272 
    273 /*
    274 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
    275 ** operator.
    276 */
    277 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
    278 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
    279 #endif
    280 
    281 /*
    282 ** Maximum depth of recursion for triggers.
    283 **
    284 ** A value of 1 means that a trigger program will not be able to itself
    285 ** fire any triggers. A value of 0 means that no trigger programs at all
    286 ** may be executed.
    287 */
    288 #ifndef SQLITE_MAX_TRIGGER_DEPTH
    289 # define SQLITE_MAX_TRIGGER_DEPTH 1000
    290 #endif
    291 
    292 /************** End of sqliteLimit.h *****************************************/
    293 /************** Continuing where we left off in sqliteInt.h ******************/
    294 
    295 /* Disable nuisance warnings on Borland compilers */
    296 #if defined(__BORLANDC__)
    297 #pragma warn -rch /* unreachable code */
    298 #pragma warn -ccc /* Condition is always true or false */
    299 #pragma warn -aus /* Assigned value is never used */
    300 #pragma warn -csu /* Comparing signed and unsigned */
    301 #pragma warn -spa /* Suspicious pointer arithmetic */
    302 #endif
    303 
    304 /* Needed for various definitions... */
    305 #ifndef _GNU_SOURCE
    306 # define _GNU_SOURCE
    307 #endif
    308 
    309 /*
    310 ** Include standard header files as necessary
    311 */
    312 #ifdef HAVE_STDINT_H
    313 #include <stdint.h>
    314 #endif
    315 #ifdef HAVE_INTTYPES_H
    316 #include <inttypes.h>
    317 #endif
    318 
    319 /*
    320 ** The number of samples of an index that SQLite takes in order to
    321 ** construct a histogram of the table content when running ANALYZE
    322 ** and with SQLITE_ENABLE_STAT2
    323 */
    324 #define SQLITE_INDEX_SAMPLES 10
    325 
    326 /*
    327 ** The following macros are used to cast pointers to integers and
    328 ** integers to pointers.  The way you do this varies from one compiler
    329 ** to the next, so we have developed the following set of #if statements
    330 ** to generate appropriate macros for a wide range of compilers.
    331 **
    332 ** The correct "ANSI" way to do this is to use the intptr_t type.
    333 ** Unfortunately, that typedef is not available on all compilers, or
    334 ** if it is available, it requires an #include of specific headers
    335 ** that vary from one machine to the next.
    336 **
    337 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
    338 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
    339 ** So we have to define the macros in different ways depending on the
    340 ** compiler.
    341 */
    342 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
    343 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
    344 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
    345 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
    346 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
    347 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
    348 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
    349 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
    350 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
    351 #else                          /* Generates a warning - but it always works */
    352 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
    353 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
    354 #endif
    355 
    356 /*
    357 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
    358 ** 0 means mutexes are permanently disable and the library is never
    359 ** threadsafe.  1 means the library is serialized which is the highest
    360 ** level of threadsafety.  2 means the libary is multithreaded - multiple
    361 ** threads can use SQLite as long as no two threads try to use the same
    362 ** database connection at the same time.
    363 **
    364 ** Older versions of SQLite used an optional THREADSAFE macro.
    365 ** We support that for legacy.
    366 */
    367 #if !defined(SQLITE_THREADSAFE)
    368 #if defined(THREADSAFE)
    369 # define SQLITE_THREADSAFE THREADSAFE
    370 #else
    371 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
    372 #endif
    373 #endif
    374 
    375 /*
    376 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
    377 ** It determines whether or not the features related to
    378 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
    379 ** be overridden at runtime using the sqlite3_config() API.
    380 */
    381 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
    382 # define SQLITE_DEFAULT_MEMSTATUS 1
    383 #endif
    384 
    385 /*
    386 ** Exactly one of the following macros must be defined in order to
    387 ** specify which memory allocation subsystem to use.
    388 **
    389 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
    390 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
    391 **
    392 ** (Historical note:  There used to be several other options, but we've
    393 ** pared it down to just these two.)
    394 **
    395 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
    396 ** the default.
    397 */
    398 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
    399 # error "At most one of the following compile-time configuration options\
    400  is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
    401 #endif
    402 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
    403 # define SQLITE_SYSTEM_MALLOC 1
    404 #endif
    405 
    406 /*
    407 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
    408 ** sizes of memory allocations below this value where possible.
    409 */
    410 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
    411 # define SQLITE_MALLOC_SOFT_LIMIT 1024
    412 #endif
    413 
    414 /*
    415 ** We need to define _XOPEN_SOURCE as follows in order to enable
    416 ** recursive mutexes on most Unix systems.  But Mac OS X is different.
    417 ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
    418 ** so it is omitted there.  See ticket #2673.
    419 **
    420 ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
    421 ** implemented on some systems.  So we avoid defining it at all
    422 ** if it is already defined or if it is unneeded because we are
    423 ** not doing a threadsafe build.  Ticket #2681.
    424 **
    425 ** See also ticket #2741.
    426 */
    427 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
    428 #  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
    429 #endif
    430 
    431 /*
    432 ** The TCL headers are only needed when compiling the TCL bindings.
    433 */
    434 #if defined(SQLITE_TCL) || defined(TCLSH)
    435 # include <tcl.h>
    436 #endif
    437 
    438 /*
    439 ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
    440 ** Setting NDEBUG makes the code smaller and run faster.  So the following
    441 ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
    442 ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
    443 ** feature.
    444 */
    445 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
    446 # define NDEBUG 1
    447 #endif
    448 
    449 /*
    450 ** The testcase() macro is used to aid in coverage testing.  When
    451 ** doing coverage testing, the condition inside the argument to
    452 ** testcase() must be evaluated both true and false in order to
    453 ** get full branch coverage.  The testcase() macro is inserted
    454 ** to help ensure adequate test coverage in places where simple
    455 ** condition/decision coverage is inadequate.  For example, testcase()
    456 ** can be used to make sure boundary values are tested.  For
    457 ** bitmask tests, testcase() can be used to make sure each bit
    458 ** is significant and used at least once.  On switch statements
    459 ** where multiple cases go to the same block of code, testcase()
    460 ** can insure that all cases are evaluated.
    461 **
    462 */
    463 #ifdef SQLITE_COVERAGE_TEST
    464 SQLITE_PRIVATE   void sqlite3Coverage(int);
    465 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
    466 #else
    467 # define testcase(X)
    468 #endif
    469 
    470 /*
    471 ** The TESTONLY macro is used to enclose variable declarations or
    472 ** other bits of code that are needed to support the arguments
    473 ** within testcase() and assert() macros.
    474 */
    475 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
    476 # define TESTONLY(X)  X
    477 #else
    478 # define TESTONLY(X)
    479 #endif
    480 
    481 /*
    482 ** Sometimes we need a small amount of code such as a variable initialization
    483 ** to setup for a later assert() statement.  We do not want this code to
    484 ** appear when assert() is disabled.  The following macro is therefore
    485 ** used to contain that setup code.  The "VVA" acronym stands for
    486 ** "Verification, Validation, and Accreditation".  In other words, the
    487 ** code within VVA_ONLY() will only run during verification processes.
    488 */
    489 #ifndef NDEBUG
    490 # define VVA_ONLY(X)  X
    491 #else
    492 # define VVA_ONLY(X)
    493 #endif
    494 
    495 /*
    496 ** The ALWAYS and NEVER macros surround boolean expressions which
    497 ** are intended to always be true or false, respectively.  Such
    498 ** expressions could be omitted from the code completely.  But they
    499 ** are included in a few cases in order to enhance the resilience
    500 ** of SQLite to unexpected behavior - to make the code "self-healing"
    501 ** or "ductile" rather than being "brittle" and crashing at the first
    502 ** hint of unplanned behavior.
    503 **
    504 ** In other words, ALWAYS and NEVER are added for defensive code.
    505 **
    506 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
    507 ** be true and false so that the unreachable code then specify will
    508 ** not be counted as untested code.
    509 */
    510 #if defined(SQLITE_COVERAGE_TEST)
    511 # define ALWAYS(X)      (1)
    512 # define NEVER(X)       (0)
    513 #elif !defined(NDEBUG)
    514 # define ALWAYS(X)      ((X)?1:(assert(0),0))
    515 # define NEVER(X)       ((X)?(assert(0),1):0)
    516 #else
    517 # define ALWAYS(X)      (X)
    518 # define NEVER(X)       (X)
    519 #endif
    520 
    521 /*
    522 ** Return true (non-zero) if the input is a integer that is too large
    523 ** to fit in 32-bits.  This macro is used inside of various testcase()
    524 ** macros to verify that we have tested SQLite for large-file support.
    525 */
    526 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
    527 
    528 /*
    529 ** The macro unlikely() is a hint that surrounds a boolean
    530 ** expression that is usually false.  Macro likely() surrounds
    531 ** a boolean expression that is usually true.  GCC is able to
    532 ** use these hints to generate better code, sometimes.
    533 */
    534 #if defined(__GNUC__) && 0
    535 # define likely(X)    __builtin_expect((X),1)
    536 # define unlikely(X)  __builtin_expect((X),0)
    537 #else
    538 # define likely(X)    !!(X)
    539 # define unlikely(X)  !!(X)
    540 #endif
    541 
    542 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
    543 /************** Begin file sqlite3.h *****************************************/
    544 /*
    545 ** 2001 September 15
    546 **
    547 ** The author disclaims copyright to this source code.  In place of
    548 ** a legal notice, here is a blessing:
    549 **
    550 **    May you do good and not evil.
    551 **    May you find forgiveness for yourself and forgive others.
    552 **    May you share freely, never taking more than you give.
    553 **
    554 *************************************************************************
    555 ** This header file defines the interface that the SQLite library
    556 ** presents to client programs.  If a C-function, structure, datatype,
    557 ** or constant definition does not appear in this file, then it is
    558 ** not a published API of SQLite, is subject to change without
    559 ** notice, and should not be referenced by programs that use SQLite.
    560 **
    561 ** Some of the definitions that are in this file are marked as
    562 ** "experimental".  Experimental interfaces are normally new
    563 ** features recently added to SQLite.  We do not anticipate changes
    564 ** to experimental interfaces but reserve the right to make minor changes
    565 ** if experience from use "in the wild" suggest such changes are prudent.
    566 **
    567 ** The official C-language API documentation for SQLite is derived
    568 ** from comments in this file.  This file is the authoritative source
    569 ** on how SQLite interfaces are suppose to operate.
    570 **
    571 ** The name of this file under configuration management is "sqlite.h.in".
    572 ** The makefile makes some minor changes to this file (such as inserting
    573 ** the version number) and changes its name to "sqlite3.h" as
    574 ** part of the build process.
    575 */
    576 #ifndef _SQLITE3_H_
    577 #define _SQLITE3_H_
    578 #include <stdarg.h>     /* Needed for the definition of va_list */
    579 
    580 /*
    581 ** Make sure we can call this stuff from C++.
    582 */
    583 #if 0
    584 extern "C" {
    585 #endif
    586 
    587 
    588 /*
    589 ** Add the ability to override 'extern'
    590 */
    591 #ifndef SQLITE_EXTERN
    592 # define SQLITE_EXTERN extern
    593 #endif
    594 
    595 #ifndef SQLITE_API
    596 # define SQLITE_API
    597 #endif
    598 
    599 
    600 /*
    601 ** These no-op macros are used in front of interfaces to mark those
    602 ** interfaces as either deprecated or experimental.  New applications
    603 ** should not use deprecated interfaces - they are support for backwards
    604 ** compatibility only.  Application writers should be aware that
    605 ** experimental interfaces are subject to change in point releases.
    606 **
    607 ** These macros used to resolve to various kinds of compiler magic that
    608 ** would generate warning messages when they were used.  But that
    609 ** compiler magic ended up generating such a flurry of bug reports
    610 ** that we have taken it all out and gone back to using simple
    611 ** noop macros.
    612 */
    613 #define SQLITE_DEPRECATED
    614 #define SQLITE_EXPERIMENTAL
    615 
    616 /*
    617 ** Ensure these symbols were not defined by some previous header file.
    618 */
    619 #ifdef SQLITE_VERSION
    620 # undef SQLITE_VERSION
    621 #endif
    622 #ifdef SQLITE_VERSION_NUMBER
    623 # undef SQLITE_VERSION_NUMBER
    624 #endif
    625 
    626 /*
    627 ** CAPI3REF: Compile-Time Library Version Numbers
    628 **
    629 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
    630 ** evaluates to a string literal that is the SQLite version in the
    631 ** format "X.Y.Z" where X is the major version number (always 3 for
    632 ** SQLite3) and Y is the minor version number and Z is the release number.)^
    633 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
    634 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
    635 ** numbers used in [SQLITE_VERSION].)^
    636 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
    637 ** be larger than the release from which it is derived.  Either Y will
    638 ** be held constant and Z will be incremented or else Y will be incremented
    639 ** and Z will be reset to zero.
    640 **
    641 ** Since version 3.6.18, SQLite source code has been stored in the
    642 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
    643 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
    644 ** a string which identifies a particular check-in of SQLite
    645 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
    646 ** string contains the date and time of the check-in (UTC) and an SHA1
    647 ** hash of the entire source tree.
    648 **
    649 ** See also: [sqlite3_libversion()],
    650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
    651 ** [sqlite_version()] and [sqlite_source_id()].
    652 */
    653 #define SQLITE_VERSION        "3.7.6.3"
    654 #define SQLITE_VERSION_NUMBER 3007006
    655 #define SQLITE_SOURCE_ID      "2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e"
    656 
    657 /*
    658 ** CAPI3REF: Run-Time Library Version Numbers
    659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
    660 **
    661 ** These interfaces provide the same information as the [SQLITE_VERSION],
    662 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
    663 ** but are associated with the library instead of the header file.  ^(Cautious
    664 ** programmers might include assert() statements in their application to
    665 ** verify that values returned by these interfaces match the macros in
    666 ** the header, and thus insure that the application is
    667 ** compiled with matching library and header files.
    668 **
    669 ** <blockquote><pre>
    670 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
    671 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
    672 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
    673 ** </pre></blockquote>)^
    674 **
    675 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
    676 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
    677 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
    678 ** function is provided for use in DLLs since DLL users usually do not have
    679 ** direct access to string constants within the DLL.  ^The
    680 ** sqlite3_libversion_number() function returns an integer equal to
    681 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
    682 ** a pointer to a string constant whose value is the same as the
    683 ** [SQLITE_SOURCE_ID] C preprocessor macro.
    684 **
    685 ** See also: [sqlite_version()] and [sqlite_source_id()].
    686 */
    687 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
    688 SQLITE_API const char *sqlite3_libversion(void);
    689 SQLITE_API const char *sqlite3_sourceid(void);
    690 SQLITE_API int sqlite3_libversion_number(void);
    691 
    692 /*
    693 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
    694 **
    695 ** ^The sqlite3_compileoption_used() function returns 0 or 1
    696 ** indicating whether the specified option was defined at
    697 ** compile time.  ^The SQLITE_ prefix may be omitted from the
    698 ** option name passed to sqlite3_compileoption_used().
    699 **
    700 ** ^The sqlite3_compileoption_get() function allows iterating
    701 ** over the list of options that were defined at compile time by
    702 ** returning the N-th compile time option string.  ^If N is out of range,
    703 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
    704 ** prefix is omitted from any strings returned by
    705 ** sqlite3_compileoption_get().
    706 **
    707 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
    708 ** and sqlite3_compileoption_get() may be omitted by specifying the
    709 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
    710 **
    711 ** See also: SQL functions [sqlite_compileoption_used()] and
    712 ** [sqlite_compileoption_get()] and the [compile_options pragma].
    713 */
    714 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
    715 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
    716 SQLITE_API const char *sqlite3_compileoption_get(int N);
    717 #endif
    718 
    719 /*
    720 ** CAPI3REF: Test To See If The Library Is Threadsafe
    721 **
    722 ** ^The sqlite3_threadsafe() function returns zero if and only if
    723 ** SQLite was compiled mutexing code omitted due to the
    724 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
    725 **
    726 ** SQLite can be compiled with or without mutexes.  When
    727 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
    728 ** are enabled and SQLite is threadsafe.  When the
    729 ** [SQLITE_THREADSAFE] macro is 0,
    730 ** the mutexes are omitted.  Without the mutexes, it is not safe
    731 ** to use SQLite concurrently from more than one thread.
    732 **
    733 ** Enabling mutexes incurs a measurable performance penalty.
    734 ** So if speed is of utmost importance, it makes sense to disable
    735 ** the mutexes.  But for maximum safety, mutexes should be enabled.
    736 ** ^The default behavior is for mutexes to be enabled.
    737 **
    738 ** This interface can be used by an application to make sure that the
    739 ** version of SQLite that it is linking against was compiled with
    740 ** the desired setting of the [SQLITE_THREADSAFE] macro.
    741 **
    742 ** This interface only reports on the compile-time mutex setting
    743 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
    744 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
    745 ** can be fully or partially disabled using a call to [sqlite3_config()]
    746 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
    747 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
    748 ** sqlite3_threadsafe() function shows only the compile-time setting of
    749 ** thread safety, not any run-time changes to that setting made by
    750 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
    751 ** is unchanged by calls to sqlite3_config().)^
    752 **
    753 ** See the [threading mode] documentation for additional information.
    754 */
    755 SQLITE_API int sqlite3_threadsafe(void);
    756 
    757 /*
    758 ** CAPI3REF: Database Connection Handle
    759 ** KEYWORDS: {database connection} {database connections}
    760 **
    761 ** Each open SQLite database is represented by a pointer to an instance of
    762 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
    763 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
    764 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
    765 ** is its destructor.  There are many other interfaces (such as
    766 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
    767 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
    768 ** sqlite3 object.
    769 */
    770 typedef struct sqlite3 sqlite3;
    771 
    772 /*
    773 ** CAPI3REF: 64-Bit Integer Types
    774 ** KEYWORDS: sqlite_int64 sqlite_uint64
    775 **
    776 ** Because there is no cross-platform way to specify 64-bit integer types
    777 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
    778 **
    779 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
    780 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
    781 ** compatibility only.
    782 **
    783 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
    784 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
    785 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
    786 ** between 0 and +18446744073709551615 inclusive.
    787 */
    788 #ifdef SQLITE_INT64_TYPE
    789   typedef SQLITE_INT64_TYPE sqlite_int64;
    790   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
    791 #elif defined(_MSC_VER) || defined(__BORLANDC__)
    792   typedef __int64 sqlite_int64;
    793   typedef unsigned __int64 sqlite_uint64;
    794 #else
    795   typedef long long int sqlite_int64;
    796   typedef unsigned long long int sqlite_uint64;
    797 #endif
    798 typedef sqlite_int64 sqlite3_int64;
    799 typedef sqlite_uint64 sqlite3_uint64;
    800 
    801 /*
    802 ** If compiling for a processor that lacks floating point support,
    803 ** substitute integer for floating-point.
    804 */
    805 #ifdef SQLITE_OMIT_FLOATING_POINT
    806 # define double sqlite3_int64
    807 #endif
    808 
    809 /*
    810 ** CAPI3REF: Closing A Database Connection
    811 **
    812 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
    813 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
    814 ** successfully destroyed and all associated resources are deallocated.
    815 **
    816 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
    817 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
    818 ** the [sqlite3] object prior to attempting to close the object.  ^If
    819 ** sqlite3_close() is called on a [database connection] that still has
    820 ** outstanding [prepared statements] or [BLOB handles], then it returns
    821 ** SQLITE_BUSY.
    822 **
    823 ** ^If [sqlite3_close()] is invoked while a transaction is open,
    824 ** the transaction is automatically rolled back.
    825 **
    826 ** The C parameter to [sqlite3_close(C)] must be either a NULL
    827 ** pointer or an [sqlite3] object pointer obtained
    828 ** from [sqlite3_open()], [sqlite3_open16()], or
    829 ** [sqlite3_open_v2()], and not previously closed.
    830 ** ^Calling sqlite3_close() with a NULL pointer argument is a
    831 ** harmless no-op.
    832 */
    833 SQLITE_API int sqlite3_close(sqlite3 *);
    834 
    835 /*
    836 ** The type for a callback function.
    837 ** This is legacy and deprecated.  It is included for historical
    838 ** compatibility and is not documented.
    839 */
    840 typedef int (*sqlite3_callback)(void*,int,char**, char**);
    841 
    842 /*
    843 ** CAPI3REF: One-Step Query Execution Interface
    844 **
    845 ** The sqlite3_exec() interface is a convenience wrapper around
    846 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
    847 ** that allows an application to run multiple statements of SQL
    848 ** without having to use a lot of C code.
    849 **
    850 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
    851 ** semicolon-separate SQL statements passed into its 2nd argument,
    852 ** in the context of the [database connection] passed in as its 1st
    853 ** argument.  ^If the callback function of the 3rd argument to
    854 ** sqlite3_exec() is not NULL, then it is invoked for each result row
    855 ** coming out of the evaluated SQL statements.  ^The 4th argument to
    856 ** to sqlite3_exec() is relayed through to the 1st argument of each
    857 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
    858 ** is NULL, then no callback is ever invoked and result rows are
    859 ** ignored.
    860 **
    861 ** ^If an error occurs while evaluating the SQL statements passed into
    862 ** sqlite3_exec(), then execution of the current statement stops and
    863 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
    864 ** is not NULL then any error message is written into memory obtained
    865 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
    866 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
    867 ** on error message strings returned through the 5th parameter of
    868 ** of sqlite3_exec() after the error message string is no longer needed.
    869 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
    870 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
    871 ** NULL before returning.
    872 **
    873 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
    874 ** routine returns SQLITE_ABORT without invoking the callback again and
    875 ** without running any subsequent SQL statements.
    876 **
    877 ** ^The 2nd argument to the sqlite3_exec() callback function is the
    878 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
    879 ** callback is an array of pointers to strings obtained as if from
    880 ** [sqlite3_column_text()], one for each column.  ^If an element of a
    881 ** result row is NULL then the corresponding string pointer for the
    882 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
    883 ** sqlite3_exec() callback is an array of pointers to strings where each
    884 ** entry represents the name of corresponding result column as obtained
    885 ** from [sqlite3_column_name()].
    886 **
    887 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
    888 ** to an empty string, or a pointer that contains only whitespace and/or
    889 ** SQL comments, then no SQL statements are evaluated and the database
    890 ** is not changed.
    891 **
    892 ** Restrictions:
    893 **
    894 ** <ul>
    895 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
    896 **      is a valid and open [database connection].
    897 ** <li> The application must not close [database connection] specified by
    898 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
    899 ** <li> The application must not modify the SQL statement text passed into
    900 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
    901 ** </ul>
    902 */
    903 SQLITE_API int sqlite3_exec(
    904   sqlite3*,                                  /* An open database */
    905   const char *sql,                           /* SQL to be evaluated */
    906   int (*callback)(void*,int,char**,char**),  /* Callback function */
    907   void *,                                    /* 1st argument to callback */
    908   char **errmsg                              /* Error msg written here */
    909 );
    910 
    911 /*
    912 ** CAPI3REF: Result Codes
    913 ** KEYWORDS: SQLITE_OK {error code} {error codes}
    914 ** KEYWORDS: {result code} {result codes}
    915 **
    916 ** Many SQLite functions return an integer result code from the set shown
    917 ** here in order to indicates success or failure.
    918 **
    919 ** New error codes may be added in future versions of SQLite.
    920 **
    921 ** See also: [SQLITE_IOERR_READ | extended result codes]
    922 */
    923 #define SQLITE_OK           0   /* Successful result */
    924 /* beginning-of-error-codes */
    925 #define SQLITE_ERROR        1   /* SQL error or missing database */
    926 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
    927 #define SQLITE_PERM         3   /* Access permission denied */
    928 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
    929 #define SQLITE_BUSY         5   /* The database file is locked */
    930 #define SQLITE_LOCKED       6   /* A table in the database is locked */
    931 #define SQLITE_NOMEM        7   /* A malloc() failed */
    932 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
    933 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
    934 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
    935 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
    936 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
    937 #define SQLITE_FULL        13   /* Insertion failed because database is full */
    938 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
    939 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
    940 #define SQLITE_EMPTY       16   /* Database is empty */
    941 #define SQLITE_SCHEMA      17   /* The database schema changed */
    942 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
    943 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
    944 #define SQLITE_MISMATCH    20   /* Data type mismatch */
    945 #define SQLITE_MISUSE      21   /* Library used incorrectly */
    946 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
    947 #define SQLITE_AUTH        23   /* Authorization denied */
    948 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
    949 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
    950 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
    951 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
    952 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
    953 /* end-of-error-codes */
    954 
    955 /*
    956 ** CAPI3REF: Extended Result Codes
    957 ** KEYWORDS: {extended error code} {extended error codes}
    958 ** KEYWORDS: {extended result code} {extended result codes}
    959 **
    960 ** In its default configuration, SQLite API routines return one of 26 integer
    961 ** [SQLITE_OK | result codes].  However, experience has shown that many of
    962 ** these result codes are too coarse-grained.  They do not provide as
    963 ** much information about problems as programmers might like.  In an effort to
    964 ** address this, newer versions of SQLite (version 3.3.8 and later) include
    965 ** support for additional result codes that provide more detailed information
    966 ** about errors. The extended result codes are enabled or disabled
    967 ** on a per database connection basis using the
    968 ** [sqlite3_extended_result_codes()] API.
    969 **
    970 ** Some of the available extended result codes are listed here.
    971 ** One may expect the number of extended result codes will be expand
    972 ** over time.  Software that uses extended result codes should expect
    973 ** to see new result codes in future releases of SQLite.
    974 **
    975 ** The SQLITE_OK result code will never be extended.  It will always
    976 ** be exactly zero.
    977 */
    978 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
    979 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
    980 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
    981 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
    982 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
    983 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
    984 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
    985 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
    986 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
    987 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
    988 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
    989 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
    990 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
    991 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
    992 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
    993 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
    994 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
    995 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
    996 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
    997 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
    998 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
    999 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   1000 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   1001 
   1002 /*
   1003 ** CAPI3REF: Flags For File Open Operations
   1004 **
   1005 ** These bit values are intended for use in the
   1006 ** 3rd parameter to the [sqlite3_open_v2()] interface and
   1007 ** in the 4th parameter to the xOpen method of the
   1008 ** [sqlite3_vfs] object.
   1009 */
   1010 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
   1011 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
   1012 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
   1013 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
   1014 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
   1015 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
   1016 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
   1017 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
   1018 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
   1019 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
   1020 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
   1021 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
   1022 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
   1023 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
   1024 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
   1025 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
   1026 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
   1027 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
   1028 
   1029 /* Reserved:                         0x00F00000 */
   1030 
   1031 /*
   1032 ** CAPI3REF: Device Characteristics
   1033 **
   1034 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
   1035 ** object returns an integer which is a vector of the these
   1036 ** bit values expressing I/O characteristics of the mass storage
   1037 ** device that holds the file that the [sqlite3_io_methods]
   1038 ** refers to.
   1039 **
   1040 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   1041 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   1042 ** mean that writes of blocks that are nnn bytes in size and
   1043 ** are aligned to an address which is an integer multiple of
   1044 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
   1045 ** that when data is appended to a file, the data is appended
   1046 ** first then the size of the file is extended, never the other
   1047 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   1048 ** information is written to disk in the same order as calls
   1049 ** to xWrite().
   1050 */
   1051 #define SQLITE_IOCAP_ATOMIC                 0x00000001
   1052 #define SQLITE_IOCAP_ATOMIC512              0x00000002
   1053 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
   1054 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
   1055 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
   1056 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
   1057 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
   1058 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
   1059 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
   1060 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
   1061 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
   1062 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
   1063 
   1064 /*
   1065 ** CAPI3REF: File Locking Levels
   1066 **
   1067 ** SQLite uses one of these integer values as the second
   1068 ** argument to calls it makes to the xLock() and xUnlock() methods
   1069 ** of an [sqlite3_io_methods] object.
   1070 */
   1071 #define SQLITE_LOCK_NONE          0
   1072 #define SQLITE_LOCK_SHARED        1
   1073 #define SQLITE_LOCK_RESERVED      2
   1074 #define SQLITE_LOCK_PENDING       3
   1075 #define SQLITE_LOCK_EXCLUSIVE     4
   1076 
   1077 /*
   1078 ** CAPI3REF: Synchronization Type Flags
   1079 **
   1080 ** When SQLite invokes the xSync() method of an
   1081 ** [sqlite3_io_methods] object it uses a combination of
   1082 ** these integer values as the second argument.
   1083 **
   1084 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
   1085 ** sync operation only needs to flush data to mass storage.  Inode
   1086 ** information need not be flushed. If the lower four bits of the flag
   1087 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
   1088 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
   1089 ** to use Mac OS X style fullsync instead of fsync().
   1090 **
   1091 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
   1092 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
   1093 ** settings.  The [synchronous pragma] determines when calls to the
   1094 ** xSync VFS method occur and applies uniformly across all platforms.
   1095 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
   1096 ** energetic or rigorous or forceful the sync operations are and
   1097 ** only make a difference on Mac OSX for the default SQLite code.
   1098 ** (Third-party VFS implementations might also make the distinction
   1099 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
   1100 ** operating systems natively supported by SQLite, only Mac OSX
   1101 ** cares about the difference.)
   1102 */
   1103 #define SQLITE_SYNC_NORMAL        0x00002
   1104 #define SQLITE_SYNC_FULL          0x00003
   1105 #define SQLITE_SYNC_DATAONLY      0x00010
   1106 
   1107 /*
   1108 ** CAPI3REF: OS Interface Open File Handle
   1109 **
   1110 ** An [sqlite3_file] object represents an open file in the
   1111 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
   1112 ** implementations will
   1113 ** want to subclass this object by appending additional fields
   1114 ** for their own use.  The pMethods entry is a pointer to an
   1115 ** [sqlite3_io_methods] object that defines methods for performing
   1116 ** I/O operations on the open file.
   1117 */
   1118 typedef struct sqlite3_file sqlite3_file;
   1119 struct sqlite3_file {
   1120   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
   1121 };
   1122 
   1123 /*
   1124 ** CAPI3REF: OS Interface File Virtual Methods Object
   1125 **
   1126 ** Every file opened by the [sqlite3_vfs] xOpen method populates an
   1127 ** [sqlite3_file] object (or, more commonly, a subclass of the
   1128 ** [sqlite3_file] object) with a pointer to an instance of this object.
   1129 ** This object defines the methods used to perform various operations
   1130 ** against the open file represented by the [sqlite3_file] object.
   1131 **
   1132 ** If the xOpen method sets the sqlite3_file.pMethods element
   1133 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
   1134 ** may be invoked even if the xOpen reported that it failed.  The
   1135 ** only way to prevent a call to xClose following a failed xOpen
   1136 ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
   1137 **
   1138 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
   1139 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
   1140 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
   1141 ** flag may be ORed in to indicate that only the data of the file
   1142 ** and not its inode needs to be synced.
   1143 **
   1144 ** The integer values to xLock() and xUnlock() are one of
   1145 ** <ul>
   1146 ** <li> [SQLITE_LOCK_NONE],
   1147 ** <li> [SQLITE_LOCK_SHARED],
   1148 ** <li> [SQLITE_LOCK_RESERVED],
   1149 ** <li> [SQLITE_LOCK_PENDING], or
   1150 ** <li> [SQLITE_LOCK_EXCLUSIVE].
   1151 ** </ul>
   1152 ** xLock() increases the lock. xUnlock() decreases the lock.
   1153 ** The xCheckReservedLock() method checks whether any database connection,
   1154 ** either in this process or in some other process, is holding a RESERVED,
   1155 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
   1156 ** if such a lock exists and false otherwise.
   1157 **
   1158 ** The xFileControl() method is a generic interface that allows custom
   1159 ** VFS implementations to directly control an open file using the
   1160 ** [sqlite3_file_control()] interface.  The second "op" argument is an
   1161 ** integer opcode.  The third argument is a generic pointer intended to
   1162 ** point to a structure that may contain arguments or space in which to
   1163 ** write return values.  Potential uses for xFileControl() might be
   1164 ** functions to enable blocking locks with timeouts, to change the
   1165 ** locking strategy (for example to use dot-file locks), to inquire
   1166 ** about the status of a lock, or to break stale locks.  The SQLite
   1167 ** core reserves all opcodes less than 100 for its own use.
   1168 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
   1169 ** Applications that define a custom xFileControl method should use opcodes
   1170 ** greater than 100 to avoid conflicts.  VFS implementations should
   1171 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
   1172 ** recognize.
   1173 **
   1174 ** The xSectorSize() method returns the sector size of the
   1175 ** device that underlies the file.  The sector size is the
   1176 ** minimum write that can be performed without disturbing
   1177 ** other bytes in the file.  The xDeviceCharacteristics()
   1178 ** method returns a bit vector describing behaviors of the
   1179 ** underlying device:
   1180 **
   1181 ** <ul>
   1182 ** <li> [SQLITE_IOCAP_ATOMIC]
   1183 ** <li> [SQLITE_IOCAP_ATOMIC512]
   1184 ** <li> [SQLITE_IOCAP_ATOMIC1K]
   1185 ** <li> [SQLITE_IOCAP_ATOMIC2K]
   1186 ** <li> [SQLITE_IOCAP_ATOMIC4K]
   1187 ** <li> [SQLITE_IOCAP_ATOMIC8K]
   1188 ** <li> [SQLITE_IOCAP_ATOMIC16K]
   1189 ** <li> [SQLITE_IOCAP_ATOMIC32K]
   1190 ** <li> [SQLITE_IOCAP_ATOMIC64K]
   1191 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   1192 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
   1193 ** </ul>
   1194 **
   1195 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   1196 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   1197 ** mean that writes of blocks that are nnn bytes in size and
   1198 ** are aligned to an address which is an integer multiple of
   1199 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
   1200 ** that when data is appended to a file, the data is appended
   1201 ** first then the size of the file is extended, never the other
   1202 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   1203 ** information is written to disk in the same order as calls
   1204 ** to xWrite().
   1205 **
   1206 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
   1207 ** in the unread portions of the buffer with zeros.  A VFS that
   1208 ** fails to zero-fill short reads might seem to work.  However,
   1209 ** failure to zero-fill short reads will eventually lead to
   1210 ** database corruption.
   1211 */
   1212 typedef struct sqlite3_io_methods sqlite3_io_methods;
   1213 struct sqlite3_io_methods {
   1214   int iVersion;
   1215   int (*xClose)(sqlite3_file*);
   1216   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
   1217   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
   1218   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
   1219   int (*xSync)(sqlite3_file*, int flags);
   1220   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
   1221   int (*xLock)(sqlite3_file*, int);
   1222   int (*xUnlock)(sqlite3_file*, int);
   1223   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
   1224   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
   1225   int (*xSectorSize)(sqlite3_file*);
   1226   int (*xDeviceCharacteristics)(sqlite3_file*);
   1227   /* Methods above are valid for version 1 */
   1228   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
   1229   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
   1230   void (*xShmBarrier)(sqlite3_file*);
   1231   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
   1232   /* Methods above are valid for version 2 */
   1233   /* Additional methods may be added in future releases */
   1234 };
   1235 
   1236 /*
   1237 ** CAPI3REF: Standard File Control Opcodes
   1238 **
   1239 ** These integer constants are opcodes for the xFileControl method
   1240 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
   1241 ** interface.
   1242 **
   1243 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
   1244 ** opcode causes the xFileControl method to write the current state of
   1245 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
   1246 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
   1247 ** into an integer that the pArg argument points to. This capability
   1248 ** is used during testing and only needs to be supported when SQLITE_TEST
   1249 ** is defined.
   1250 **
   1251 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
   1252 ** layer a hint of how large the database file will grow to be during the
   1253 ** current transaction.  This hint is not guaranteed to be accurate but it
   1254 ** is often close.  The underlying VFS might choose to preallocate database
   1255 ** file space based on this hint in order to help writes to the database
   1256 ** file run faster.
   1257 **
   1258 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
   1259 ** extends and truncates the database file in chunks of a size specified
   1260 ** by the user. The fourth argument to [sqlite3_file_control()] should
   1261 ** point to an integer (type int) containing the new chunk-size to use
   1262 ** for the nominated database. Allocating database file space in large
   1263 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
   1264 ** improve performance on some systems.
   1265 **
   1266 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
   1267 ** to the [sqlite3_file] object associated with a particular database
   1268 ** connection.  See the [sqlite3_file_control()] documentation for
   1269 ** additional information.
   1270 **
   1271 ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
   1272 ** SQLite and sent to all VFSes in place of a call to the xSync method
   1273 ** when the database connection has [PRAGMA synchronous] set to OFF.)^
   1274 ** Some specialized VFSes need this signal in order to operate correctly
   1275 ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
   1276 ** VFSes do not need this signal and should silently ignore this opcode.
   1277 ** Applications should not call [sqlite3_file_control()] with this
   1278 ** opcode as doing so may disrupt the operation of the specialized VFSes
   1279 ** that do require it.
   1280 */
   1281 #define SQLITE_FCNTL_LOCKSTATE        1
   1282 #define SQLITE_GET_LOCKPROXYFILE      2
   1283 #define SQLITE_SET_LOCKPROXYFILE      3
   1284 #define SQLITE_LAST_ERRNO             4
   1285 #define SQLITE_FCNTL_SIZE_HINT        5
   1286 #define SQLITE_FCNTL_CHUNK_SIZE       6
   1287 #define SQLITE_FCNTL_FILE_POINTER     7
   1288 #define SQLITE_FCNTL_SYNC_OMITTED     8
   1289 
   1290 
   1291 /*
   1292 ** CAPI3REF: Mutex Handle
   1293 **
   1294 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
   1295 ** abstract type for a mutex object.  The SQLite core never looks
   1296 ** at the internal representation of an [sqlite3_mutex].  It only
   1297 ** deals with pointers to the [sqlite3_mutex] object.
   1298 **
   1299 ** Mutexes are created using [sqlite3_mutex_alloc()].
   1300 */
   1301 typedef struct sqlite3_mutex sqlite3_mutex;
   1302 
   1303 /*
   1304 ** CAPI3REF: OS Interface Object
   1305 **
   1306 ** An instance of the sqlite3_vfs object defines the interface between
   1307 ** the SQLite core and the underlying operating system.  The "vfs"
   1308 ** in the name of the object stands for "virtual file system".
   1309 **
   1310 ** The value of the iVersion field is initially 1 but may be larger in
   1311 ** future versions of SQLite.  Additional fields may be appended to this
   1312 ** object when the iVersion value is increased.  Note that the structure
   1313 ** of the sqlite3_vfs object changes in the transaction between
   1314 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
   1315 ** modified.
   1316 **
   1317 ** The szOsFile field is the size of the subclassed [sqlite3_file]
   1318 ** structure used by this VFS.  mxPathname is the maximum length of
   1319 ** a pathname in this VFS.
   1320 **
   1321 ** Registered sqlite3_vfs objects are kept on a linked list formed by
   1322 ** the pNext pointer.  The [sqlite3_vfs_register()]
   1323 ** and [sqlite3_vfs_unregister()] interfaces manage this list
   1324 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
   1325 ** searches the list.  Neither the application code nor the VFS
   1326 ** implementation should use the pNext pointer.
   1327 **
   1328 ** The pNext field is the only field in the sqlite3_vfs
   1329 ** structure that SQLite will ever modify.  SQLite will only access
   1330 ** or modify this field while holding a particular static mutex.
   1331 ** The application should never modify anything within the sqlite3_vfs
   1332 ** object once the object has been registered.
   1333 **
   1334 ** The zName field holds the name of the VFS module.  The name must
   1335 ** be unique across all VFS modules.
   1336 **
   1337 ** ^SQLite guarantees that the zFilename parameter to xOpen
   1338 ** is either a NULL pointer or string obtained
   1339 ** from xFullPathname() with an optional suffix added.
   1340 ** ^If a suffix is added to the zFilename parameter, it will
   1341 ** consist of a single "-" character followed by no more than
   1342 ** 10 alphanumeric and/or "-" characters.
   1343 ** ^SQLite further guarantees that
   1344 ** the string will be valid and unchanged until xClose() is
   1345 ** called. Because of the previous sentence,
   1346 ** the [sqlite3_file] can safely store a pointer to the
   1347 ** filename if it needs to remember the filename for some reason.
   1348 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
   1349 ** must invent its own temporary name for the file.  ^Whenever the
   1350 ** xFilename parameter is NULL it will also be the case that the
   1351 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
   1352 **
   1353 ** The flags argument to xOpen() includes all bits set in
   1354 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
   1355 ** or [sqlite3_open16()] is used, then flags includes at least
   1356 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
   1357 ** If xOpen() opens a file read-only then it sets *pOutFlags to
   1358 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
   1359 **
   1360 ** ^(SQLite will also add one of the following flags to the xOpen()
   1361 ** call, depending on the object being opened:
   1362 **
   1363 ** <ul>
   1364 ** <li>  [SQLITE_OPEN_MAIN_DB]
   1365 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
   1366 ** <li>  [SQLITE_OPEN_TEMP_DB]
   1367 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
   1368 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
   1369 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
   1370 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
   1371 ** <li>  [SQLITE_OPEN_WAL]
   1372 ** </ul>)^
   1373 **
   1374 ** The file I/O implementation can use the object type flags to
   1375 ** change the way it deals with files.  For example, an application
   1376 ** that does not care about crash recovery or rollback might make
   1377 ** the open of a journal file a no-op.  Writes to this journal would
   1378 ** also be no-ops, and any attempt to read the journal would return
   1379 ** SQLITE_IOERR.  Or the implementation might recognize that a database
   1380 ** file will be doing page-aligned sector reads and writes in a random
   1381 ** order and set up its I/O subsystem accordingly.
   1382 **
   1383 ** SQLite might also add one of the following flags to the xOpen method:
   1384 **
   1385 ** <ul>
   1386 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
   1387 ** <li> [SQLITE_OPEN_EXCLUSIVE]
   1388 ** </ul>
   1389 **
   1390 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
   1391 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
   1392 ** will be set for TEMP databases and their journals, transient
   1393 ** databases, and subjournals.
   1394 **
   1395 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
   1396 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
   1397 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
   1398 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
   1399 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
   1400 ** be created, and that it is an error if it already exists.
   1401 ** It is <i>not</i> used to indicate the file should be opened
   1402 ** for exclusive access.
   1403 **
   1404 ** ^At least szOsFile bytes of memory are allocated by SQLite
   1405 ** to hold the  [sqlite3_file] structure passed as the third
   1406 ** argument to xOpen.  The xOpen method does not have to
   1407 ** allocate the structure; it should just fill it in.  Note that
   1408 ** the xOpen method must set the sqlite3_file.pMethods to either
   1409 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
   1410 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
   1411 ** element will be valid after xOpen returns regardless of the success
   1412 ** or failure of the xOpen call.
   1413 **
   1414 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
   1415 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
   1416 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
   1417 ** to test whether a file is at least readable.   The file can be a
   1418 ** directory.
   1419 **
   1420 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
   1421 ** output buffer xFullPathname.  The exact size of the output buffer
   1422 ** is also passed as a parameter to both  methods. If the output buffer
   1423 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
   1424 ** handled as a fatal error by SQLite, vfs implementations should endeavor
   1425 ** to prevent this by setting mxPathname to a sufficiently large value.
   1426 **
   1427 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
   1428 ** interfaces are not strictly a part of the filesystem, but they are
   1429 ** included in the VFS structure for completeness.
   1430 ** The xRandomness() function attempts to return nBytes bytes
   1431 ** of good-quality randomness into zOut.  The return value is
   1432 ** the actual number of bytes of randomness obtained.
   1433 ** The xSleep() method causes the calling thread to sleep for at
   1434 ** least the number of microseconds given.  ^The xCurrentTime()
   1435 ** method returns a Julian Day Number for the current date and time as
   1436 ** a floating point value.
   1437 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
   1438 ** Day Number multipled by 86400000 (the number of milliseconds in
   1439 ** a 24-hour day).
   1440 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
   1441 ** date and time if that method is available (if iVersion is 2 or
   1442 ** greater and the function pointer is not NULL) and will fall back
   1443 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
   1444 **
   1445 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
   1446 ** are not used by the SQLite core.  These optional interfaces are provided
   1447 ** by some VFSes to facilitate testing of the VFS code. By overriding
   1448 ** system calls with functions under its control, a test program can
   1449 ** simulate faults and error conditions that would otherwise be difficult
   1450 ** or impossible to induce.  The set of system calls that can be overridden
   1451 ** varies from one VFS to another, and from one version of the same VFS to the
   1452 ** next.  Applications that use these interfaces must be prepared for any
   1453 ** or all of these interfaces to be NULL or for their behavior to change
   1454 ** from one release to the next.  Applications must not attempt to access
   1455 ** any of these methods if the iVersion of the VFS is less than 3.
   1456 */
   1457 typedef struct sqlite3_vfs sqlite3_vfs;
   1458 typedef void (*sqlite3_syscall_ptr)(void);
   1459 struct sqlite3_vfs {
   1460   int iVersion;            /* Structure version number (currently 3) */
   1461   int szOsFile;            /* Size of subclassed sqlite3_file */
   1462   int mxPathname;          /* Maximum file pathname length */
   1463   sqlite3_vfs *pNext;      /* Next registered VFS */
   1464   const char *zName;       /* Name of this virtual file system */
   1465   void *pAppData;          /* Pointer to application-specific data */
   1466   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
   1467                int flags, int *pOutFlags);
   1468   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
   1469   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
   1470   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
   1471   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
   1472   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
   1473   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
   1474   void (*xDlClose)(sqlite3_vfs*, void*);
   1475   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
   1476   int (*xSleep)(sqlite3_vfs*, int microseconds);
   1477   int (*xCurrentTime)(sqlite3_vfs*, double*);
   1478   int (*xGetLastError)(sqlite3_vfs*, int, char *);
   1479   /*
   1480   ** The methods above are in version 1 of the sqlite_vfs object
   1481   ** definition.  Those that follow are added in version 2 or later
   1482   */
   1483   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
   1484   /*
   1485   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
   1486   ** Those below are for version 3 and greater.
   1487   */
   1488   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
   1489   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
   1490   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
   1491   /*
   1492   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
   1493   ** New fields may be appended in figure versions.  The iVersion
   1494   ** value will increment whenever this happens.
   1495   */
   1496 };
   1497 
   1498 /*
   1499 ** CAPI3REF: Flags for the xAccess VFS method
   1500 **
   1501 ** These integer constants can be used as the third parameter to
   1502 ** the xAccess method of an [sqlite3_vfs] object.  They determine
   1503 ** what kind of permissions the xAccess method is looking for.
   1504 ** With SQLITE_ACCESS_EXISTS, the xAccess method
   1505 ** simply checks whether the file exists.
   1506 ** With SQLITE_ACCESS_READWRITE, the xAccess method
   1507 ** checks whether the named directory is both readable and writable
   1508 ** (in other words, if files can be added, removed, and renamed within
   1509 ** the directory).
   1510 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
   1511 ** [temp_store_directory pragma], though this could change in a future
   1512 ** release of SQLite.
   1513 ** With SQLITE_ACCESS_READ, the xAccess method
   1514 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
   1515 ** currently unused, though it might be used in a future release of
   1516 ** SQLite.
   1517 */
   1518 #define SQLITE_ACCESS_EXISTS    0
   1519 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
   1520 #define SQLITE_ACCESS_READ      2   /* Unused */
   1521 
   1522 /*
   1523 ** CAPI3REF: Flags for the xShmLock VFS method
   1524 **
   1525 ** These integer constants define the various locking operations
   1526 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
   1527 ** following are the only legal combinations of flags to the
   1528 ** xShmLock method:
   1529 **
   1530 ** <ul>
   1531 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
   1532 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
   1533 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
   1534 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
   1535 ** </ul>
   1536 **
   1537 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
   1538 ** was given no the corresponding lock.
   1539 **
   1540 ** The xShmLock method can transition between unlocked and SHARED or
   1541 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
   1542 ** and EXCLUSIVE.
   1543 */
   1544 #define SQLITE_SHM_UNLOCK       1
   1545 #define SQLITE_SHM_LOCK         2
   1546 #define SQLITE_SHM_SHARED       4
   1547 #define SQLITE_SHM_EXCLUSIVE    8
   1548 
   1549 /*
   1550 ** CAPI3REF: Maximum xShmLock index
   1551 **
   1552 ** The xShmLock method on [sqlite3_io_methods] may use values
   1553 ** between 0 and this upper bound as its "offset" argument.
   1554 ** The SQLite core will never attempt to acquire or release a
   1555 ** lock outside of this range
   1556 */
   1557 #define SQLITE_SHM_NLOCK        8
   1558 
   1559 
   1560 /*
   1561 ** CAPI3REF: Initialize The SQLite Library
   1562 **
   1563 ** ^The sqlite3_initialize() routine initializes the
   1564 ** SQLite library.  ^The sqlite3_shutdown() routine
   1565 ** deallocates any resources that were allocated by sqlite3_initialize().
   1566 ** These routines are designed to aid in process initialization and
   1567 ** shutdown on embedded systems.  Workstation applications using
   1568 ** SQLite normally do not need to invoke either of these routines.
   1569 **
   1570 ** A call to sqlite3_initialize() is an "effective" call if it is
   1571 ** the first time sqlite3_initialize() is invoked during the lifetime of
   1572 ** the process, or if it is the first time sqlite3_initialize() is invoked
   1573 ** following a call to sqlite3_shutdown().  ^(Only an effective call
   1574 ** of sqlite3_initialize() does any initialization.  All other calls
   1575 ** are harmless no-ops.)^
   1576 **
   1577 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
   1578 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
   1579 ** an effective call to sqlite3_shutdown() does any deinitialization.
   1580 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
   1581 **
   1582 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
   1583 ** is not.  The sqlite3_shutdown() interface must only be called from a
   1584 ** single thread.  All open [database connections] must be closed and all
   1585 ** other SQLite resources must be deallocated prior to invoking
   1586 ** sqlite3_shutdown().
   1587 **
   1588 ** Among other things, ^sqlite3_initialize() will invoke
   1589 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
   1590 ** will invoke sqlite3_os_end().
   1591 **
   1592 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
   1593 ** ^If for some reason, sqlite3_initialize() is unable to initialize
   1594 ** the library (perhaps it is unable to allocate a needed resource such
   1595 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
   1596 **
   1597 ** ^The sqlite3_initialize() routine is called internally by many other
   1598 ** SQLite interfaces so that an application usually does not need to
   1599 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
   1600 ** calls sqlite3_initialize() so the SQLite library will be automatically
   1601 ** initialized when [sqlite3_open()] is called if it has not be initialized
   1602 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
   1603 ** compile-time option, then the automatic calls to sqlite3_initialize()
   1604 ** are omitted and the application must call sqlite3_initialize() directly
   1605 ** prior to using any other SQLite interface.  For maximum portability,
   1606 ** it is recommended that applications always invoke sqlite3_initialize()
   1607 ** directly prior to using any other SQLite interface.  Future releases
   1608 ** of SQLite may require this.  In other words, the behavior exhibited
   1609 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
   1610 ** default behavior in some future release of SQLite.
   1611 **
   1612 ** The sqlite3_os_init() routine does operating-system specific
   1613 ** initialization of the SQLite library.  The sqlite3_os_end()
   1614 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
   1615 ** performed by these routines include allocation or deallocation
   1616 ** of static resources, initialization of global variables,
   1617 ** setting up a default [sqlite3_vfs] module, or setting up
   1618 ** a default configuration using [sqlite3_config()].
   1619 **
   1620 ** The application should never invoke either sqlite3_os_init()
   1621 ** or sqlite3_os_end() directly.  The application should only invoke
   1622 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
   1623 ** interface is called automatically by sqlite3_initialize() and
   1624 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
   1625 ** implementations for sqlite3_os_init() and sqlite3_os_end()
   1626 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
   1627 ** When [custom builds | built for other platforms]
   1628 ** (using the [SQLITE_OS_OTHER=1] compile-time
   1629 ** option) the application must supply a suitable implementation for
   1630 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
   1631 ** implementation of sqlite3_os_init() or sqlite3_os_end()
   1632 ** must return [SQLITE_OK] on success and some other [error code] upon
   1633 ** failure.
   1634 */
   1635 SQLITE_API int sqlite3_initialize(void);
   1636 SQLITE_API int sqlite3_shutdown(void);
   1637 SQLITE_API int sqlite3_os_init(void);
   1638 SQLITE_API int sqlite3_os_end(void);
   1639 
   1640 /*
   1641 ** CAPI3REF: Configuring The SQLite Library
   1642 **
   1643 ** The sqlite3_config() interface is used to make global configuration
   1644 ** changes to SQLite in order to tune SQLite to the specific needs of
   1645 ** the application.  The default configuration is recommended for most
   1646 ** applications and so this routine is usually not necessary.  It is
   1647 ** provided to support rare applications with unusual needs.
   1648 **
   1649 ** The sqlite3_config() interface is not threadsafe.  The application
   1650 ** must insure that no other SQLite interfaces are invoked by other
   1651 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
   1652 ** may only be invoked prior to library initialization using
   1653 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
   1654 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
   1655 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
   1656 ** Note, however, that ^sqlite3_config() can be called as part of the
   1657 ** implementation of an application-defined [sqlite3_os_init()].
   1658 **
   1659 ** The first argument to sqlite3_config() is an integer
   1660 ** [SQLITE_CONFIG_SINGLETHREAD | configuration option] that determines
   1661 ** what property of SQLite is to be configured.  Subsequent arguments
   1662 ** vary depending on the [SQLITE_CONFIG_SINGLETHREAD | configuration option]
   1663 ** in the first argument.
   1664 **
   1665 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
   1666 ** ^If the option is unknown or SQLite is unable to set the option
   1667 ** then this routine returns a non-zero [error code].
   1668 */
   1669 SQLITE_API int sqlite3_config(int, ...);
   1670 
   1671 /*
   1672 ** CAPI3REF: Configure database connections
   1673 **
   1674 ** The sqlite3_db_config() interface is used to make configuration
   1675 ** changes to a [database connection].  The interface is similar to
   1676 ** [sqlite3_config()] except that the changes apply to a single
   1677 ** [database connection] (specified in the first argument).
   1678 **
   1679 ** The second argument to sqlite3_db_config(D,V,...)  is the
   1680 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
   1681 ** that indicates what aspect of the [database connection] is being configured.
   1682 ** Subsequent arguments vary depending on the configuration verb.
   1683 **
   1684 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
   1685 ** the call is considered successful.
   1686 */
   1687 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
   1688 
   1689 /*
   1690 ** CAPI3REF: Memory Allocation Routines
   1691 **
   1692 ** An instance of this object defines the interface between SQLite
   1693 ** and low-level memory allocation routines.
   1694 **
   1695 ** This object is used in only one place in the SQLite interface.
   1696 ** A pointer to an instance of this object is the argument to
   1697 ** [sqlite3_config()] when the configuration option is
   1698 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
   1699 ** By creating an instance of this object
   1700 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
   1701 ** during configuration, an application can specify an alternative
   1702 ** memory allocation subsystem for SQLite to use for all of its
   1703 ** dynamic memory needs.
   1704 **
   1705 ** Note that SQLite comes with several [built-in memory allocators]
   1706 ** that are perfectly adequate for the overwhelming majority of applications
   1707 ** and that this object is only useful to a tiny minority of applications
   1708 ** with specialized memory allocation requirements.  This object is
   1709 ** also used during testing of SQLite in order to specify an alternative
   1710 ** memory allocator that simulates memory out-of-memory conditions in
   1711 ** order to verify that SQLite recovers gracefully from such
   1712 ** conditions.
   1713 **
   1714 ** The xMalloc and xFree methods must work like the
   1715 ** malloc() and free() functions from the standard C library.
   1716 ** The xRealloc method must work like realloc() from the standard C library
   1717 ** with the exception that if the second argument to xRealloc is zero,
   1718 ** xRealloc must be a no-op - it must not perform any allocation or
   1719 ** deallocation.  ^SQLite guarantees that the second argument to
   1720 ** xRealloc is always a value returned by a prior call to xRoundup.
   1721 ** And so in cases where xRoundup always returns a positive number,
   1722 ** xRealloc can perform exactly as the standard library realloc() and
   1723 ** still be in compliance with this specification.
   1724 **
   1725 ** xSize should return the allocated size of a memory allocation
   1726 ** previously obtained from xMalloc or xRealloc.  The allocated size
   1727 ** is always at least as big as the requested size but may be larger.
   1728 **
   1729 ** The xRoundup method returns what would be the allocated size of
   1730 ** a memory allocation given a particular requested size.  Most memory
   1731 ** allocators round up memory allocations at least to the next multiple
   1732 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
   1733 ** Every memory allocation request coming in through [sqlite3_malloc()]
   1734 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
   1735 ** that causes the corresponding memory allocation to fail.
   1736 **
   1737 ** The xInit method initializes the memory allocator.  (For example,
   1738 ** it might allocate any require mutexes or initialize internal data
   1739 ** structures.  The xShutdown method is invoked (indirectly) by
   1740 ** [sqlite3_shutdown()] and should deallocate any resources acquired
   1741 ** by xInit.  The pAppData pointer is used as the only parameter to
   1742 ** xInit and xShutdown.
   1743 **
   1744 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
   1745 ** the xInit method, so the xInit method need not be threadsafe.  The
   1746 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
   1747 ** not need to be threadsafe either.  For all other methods, SQLite
   1748 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
   1749 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
   1750 ** it is by default) and so the methods are automatically serialized.
   1751 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
   1752 ** methods must be threadsafe or else make their own arrangements for
   1753 ** serialization.
   1754 **
   1755 ** SQLite will never invoke xInit() more than once without an intervening
   1756 ** call to xShutdown().
   1757 */
   1758 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
   1759 struct sqlite3_mem_methods {
   1760   void *(*xMalloc)(int);         /* Memory allocation function */
   1761   void (*xFree)(void*);          /* Free a prior allocation */
   1762   void *(*xRealloc)(void*,int);  /* Resize an allocation */
   1763   int (*xSize)(void*);           /* Return the size of an allocation */
   1764   int (*xRoundup)(int);          /* Round up request size to allocation size */
   1765   int (*xInit)(void*);           /* Initialize the memory allocator */
   1766   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
   1767   void *pAppData;                /* Argument to xInit() and xShutdown() */
   1768 };
   1769 
   1770 /*
   1771 ** CAPI3REF: Configuration Options
   1772 **
   1773 ** These constants are the available integer configuration options that
   1774 ** can be passed as the first argument to the [sqlite3_config()] interface.
   1775 **
   1776 ** New configuration options may be added in future releases of SQLite.
   1777 ** Existing configuration options might be discontinued.  Applications
   1778 ** should check the return code from [sqlite3_config()] to make sure that
   1779 ** the call worked.  The [sqlite3_config()] interface will return a
   1780 ** non-zero [error code] if a discontinued or unsupported configuration option
   1781 ** is invoked.
   1782 **
   1783 ** <dl>
   1784 ** <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
   1785 ** <dd>There are no arguments to this option.  ^This option sets the
   1786 ** [threading mode] to Single-thread.  In other words, it disables
   1787 ** all mutexing and puts SQLite into a mode where it can only be used
   1788 ** by a single thread.   ^If SQLite is compiled with
   1789 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
   1790 ** it is not possible to change the [threading mode] from its default
   1791 ** value of Single-thread and so [sqlite3_config()] will return
   1792 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
   1793 ** configuration option.</dd>
   1794 **
   1795 ** <dt>SQLITE_CONFIG_MULTITHREAD</dt>
   1796 ** <dd>There are no arguments to this option.  ^This option sets the
   1797 ** [threading mode] to Multi-thread.  In other words, it disables
   1798 ** mutexing on [database connection] and [prepared statement] objects.
   1799 ** The application is responsible for serializing access to
   1800 ** [database connections] and [prepared statements].  But other mutexes
   1801 ** are enabled so that SQLite will be safe to use in a multi-threaded
   1802 ** environment as long as no two threads attempt to use the same
   1803 ** [database connection] at the same time.  ^If SQLite is compiled with
   1804 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
   1805 ** it is not possible to set the Multi-thread [threading mode] and
   1806 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
   1807 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
   1808 **
   1809 ** <dt>SQLITE_CONFIG_SERIALIZED</dt>
   1810 ** <dd>There are no arguments to this option.  ^This option sets the
   1811 ** [threading mode] to Serialized. In other words, this option enables
   1812 ** all mutexes including the recursive
   1813 ** mutexes on [database connection] and [prepared statement] objects.
   1814 ** In this mode (which is the default when SQLite is compiled with
   1815 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
   1816 ** to [database connections] and [prepared statements] so that the
   1817 ** application is free to use the same [database connection] or the
   1818 ** same [prepared statement] in different threads at the same time.
   1819 ** ^If SQLite is compiled with
   1820 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
   1821 ** it is not possible to set the Serialized [threading mode] and
   1822 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
   1823 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
   1824 **
   1825 ** <dt>SQLITE_CONFIG_MALLOC</dt>
   1826 ** <dd> ^(This option takes a single argument which is a pointer to an
   1827 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
   1828 ** alternative low-level memory allocation routines to be used in place of
   1829 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
   1830 ** its own private copy of the content of the [sqlite3_mem_methods] structure
   1831 ** before the [sqlite3_config()] call returns.</dd>
   1832 **
   1833 ** <dt>SQLITE_CONFIG_GETMALLOC</dt>
   1834 ** <dd> ^(This option takes a single argument which is a pointer to an
   1835 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
   1836 ** structure is filled with the currently defined memory allocation routines.)^
   1837 ** This option can be used to overload the default memory allocation
   1838 ** routines with a wrapper that simulations memory allocation failure or
   1839 ** tracks memory usage, for example. </dd>
   1840 **
   1841 ** <dt>SQLITE_CONFIG_MEMSTATUS</dt>
   1842 ** <dd> ^This option takes single argument of type int, interpreted as a
   1843 ** boolean, which enables or disables the collection of memory allocation
   1844 ** statistics. ^(When memory allocation statistics are disabled, the
   1845 ** following SQLite interfaces become non-operational:
   1846 **   <ul>
   1847 **   <li> [sqlite3_memory_used()]
   1848 **   <li> [sqlite3_memory_highwater()]
   1849 **   <li> [sqlite3_soft_heap_limit64()]
   1850 **   <li> [sqlite3_status()]
   1851 **   </ul>)^
   1852 ** ^Memory allocation statistics are enabled by default unless SQLite is
   1853 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
   1854 ** allocation statistics are disabled by default.
   1855 ** </dd>
   1856 **
   1857 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
   1858 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
   1859 ** scratch memory.  There are three arguments:  A pointer an 8-byte
   1860 ** aligned memory buffer from which the scratch allocations will be
   1861 ** drawn, the size of each scratch allocation (sz),
   1862 ** and the maximum number of scratch allocations (N).  The sz
   1863 ** argument must be a multiple of 16.
   1864 ** The first argument must be a pointer to an 8-byte aligned buffer
   1865 ** of at least sz*N bytes of memory.
   1866 ** ^SQLite will use no more than two scratch buffers per thread.  So
   1867 ** N should be set to twice the expected maximum number of threads.
   1868 ** ^SQLite will never require a scratch buffer that is more than 6
   1869 ** times the database page size. ^If SQLite needs needs additional
   1870 ** scratch memory beyond what is provided by this configuration option, then
   1871 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
   1872 **
   1873 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
   1874 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
   1875 ** the database page cache with the default page cache implemenation.
   1876 ** This configuration should not be used if an application-define page
   1877 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
   1878 ** There are three arguments to this option: A pointer to 8-byte aligned
   1879 ** memory, the size of each page buffer (sz), and the number of pages (N).
   1880 ** The sz argument should be the size of the largest database page
   1881 ** (a power of two between 512 and 32768) plus a little extra for each
   1882 ** page header.  ^The page header size is 20 to 40 bytes depending on
   1883 ** the host architecture.  ^It is harmless, apart from the wasted memory,
   1884 ** to make sz a little too large.  The first
   1885 ** argument should point to an allocation of at least sz*N bytes of memory.
   1886 ** ^SQLite will use the memory provided by the first argument to satisfy its
   1887 ** memory needs for the first N pages that it adds to cache.  ^If additional
   1888 ** page cache memory is needed beyond what is provided by this option, then
   1889 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
   1890 ** The pointer in the first argument must
   1891 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
   1892 ** will be undefined.</dd>
   1893 **
   1894 ** <dt>SQLITE_CONFIG_HEAP</dt>
   1895 ** <dd> ^This option specifies a static memory buffer that SQLite will use
   1896 ** for all of its dynamic memory allocation needs beyond those provided
   1897 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
   1898 ** There are three arguments: An 8-byte aligned pointer to the memory,
   1899 ** the number of bytes in the memory buffer, and the minimum allocation size.
   1900 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
   1901 ** to using its default memory allocator (the system malloc() implementation),
   1902 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
   1903 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
   1904 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
   1905 ** allocator is engaged to handle all of SQLites memory allocation needs.
   1906 ** The first pointer (the memory pointer) must be aligned to an 8-byte
   1907 ** boundary or subsequent behavior of SQLite will be undefined.
   1908 ** The minimum allocation size is capped at 2^12. Reasonable values
   1909 ** for the minimum allocation size are 2^5 through 2^8.</dd>
   1910 **
   1911 ** <dt>SQLITE_CONFIG_MUTEX</dt>
   1912 ** <dd> ^(This option takes a single argument which is a pointer to an
   1913 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
   1914 ** alternative low-level mutex routines to be used in place
   1915 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
   1916 ** content of the [sqlite3_mutex_methods] structure before the call to
   1917 ** [sqlite3_config()] returns. ^If SQLite is compiled with
   1918 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
   1919 ** the entire mutexing subsystem is omitted from the build and hence calls to
   1920 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
   1921 ** return [SQLITE_ERROR].</dd>
   1922 **
   1923 ** <dt>SQLITE_CONFIG_GETMUTEX</dt>
   1924 ** <dd> ^(This option takes a single argument which is a pointer to an
   1925 ** instance of the [sqlite3_mutex_methods] structure.  The
   1926 ** [sqlite3_mutex_methods]
   1927 ** structure is filled with the currently defined mutex routines.)^
   1928 ** This option can be used to overload the default mutex allocation
   1929 ** routines with a wrapper used to track mutex usage for performance
   1930 ** profiling or testing, for example.   ^If SQLite is compiled with
   1931 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
   1932 ** the entire mutexing subsystem is omitted from the build and hence calls to
   1933 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
   1934 ** return [SQLITE_ERROR].</dd>
   1935 **
   1936 ** <dt>SQLITE_CONFIG_LOOKASIDE</dt>
   1937 ** <dd> ^(This option takes two arguments that determine the default
   1938 ** memory allocation for the lookaside memory allocator on each
   1939 ** [database connection].  The first argument is the
   1940 ** size of each lookaside buffer slot and the second is the number of
   1941 ** slots allocated to each database connection.)^  ^(This option sets the
   1942 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
   1943 ** verb to [sqlite3_db_config()] can be used to change the lookaside
   1944 ** configuration on individual connections.)^ </dd>
   1945 **
   1946 ** <dt>SQLITE_CONFIG_PCACHE</dt>
   1947 ** <dd> ^(This option takes a single argument which is a pointer to
   1948 ** an [sqlite3_pcache_methods] object.  This object specifies the interface
   1949 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
   1950 ** object and uses it for page cache memory allocations.</dd>
   1951 **
   1952 ** <dt>SQLITE_CONFIG_GETPCACHE</dt>
   1953 ** <dd> ^(This option takes a single argument which is a pointer to an
   1954 ** [sqlite3_pcache_methods] object.  SQLite copies of the current
   1955 ** page cache implementation into that object.)^ </dd>
   1956 **
   1957 ** <dt>SQLITE_CONFIG_LOG</dt>
   1958 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
   1959 ** function with a call signature of void(*)(void*,int,const char*),
   1960 ** and a pointer to void. ^If the function pointer is not NULL, it is
   1961 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
   1962 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
   1963 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
   1964 ** passed through as the first parameter to the application-defined logger
   1965 ** function whenever that function is invoked.  ^The second parameter to
   1966 ** the logger function is a copy of the first parameter to the corresponding
   1967 ** [sqlite3_log()] call and is intended to be a [result code] or an
   1968 ** [extended result code].  ^The third parameter passed to the logger is
   1969 ** log message after formatting via [sqlite3_snprintf()].
   1970 ** The SQLite logging interface is not reentrant; the logger function
   1971 ** supplied by the application must not invoke any SQLite interface.
   1972 ** In a multi-threaded application, the application-defined logger
   1973 ** function must be threadsafe. </dd>
   1974 **
   1975 ** </dl>
   1976 */
   1977 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
   1978 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
   1979 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
   1980 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
   1981 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
   1982 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
   1983 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
   1984 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
   1985 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
   1986 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
   1987 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
   1988 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
   1989 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
   1990 #define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
   1991 #define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
   1992 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
   1993 
   1994 /*
   1995 ** CAPI3REF: Database Connection Configuration Options
   1996 **
   1997 ** These constants are the available integer configuration options that
   1998 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
   1999 **
   2000 ** New configuration options may be added in future releases of SQLite.
   2001 ** Existing configuration options might be discontinued.  Applications
   2002 ** should check the return code from [sqlite3_db_config()] to make sure that
   2003 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
   2004 ** non-zero [error code] if a discontinued or unsupported configuration option
   2005 ** is invoked.
   2006 **
   2007 ** <dl>
   2008 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
   2009 ** <dd> ^This option takes three additional arguments that determine the
   2010 ** [lookaside memory allocator] configuration for the [database connection].
   2011 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
   2012 ** pointer to a memory buffer to use for lookaside memory.
   2013 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
   2014 ** may be NULL in which case SQLite will allocate the
   2015 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
   2016 ** size of each lookaside buffer slot.  ^The third argument is the number of
   2017 ** slots.  The size of the buffer in the first argument must be greater than
   2018 ** or equal to the product of the second and third arguments.  The buffer
   2019 ** must be aligned to an 8-byte boundary.  ^If the second argument to
   2020 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
   2021 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
   2022 ** configuration for a database connection can only be changed when that
   2023 ** connection is not currently using lookaside memory, or in other words
   2024 ** when the "current value" returned by
   2025 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
   2026 ** Any attempt to change the lookaside memory configuration when lookaside
   2027 ** memory is in use leaves the configuration unchanged and returns
   2028 ** [SQLITE_BUSY].)^</dd>
   2029 **
   2030 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
   2031 ** <dd> ^This option is used to enable or disable the enforcement of
   2032 ** [foreign key constraints].  There should be two additional arguments.
   2033 ** The first argument is an integer which is 0 to disable FK enforcement,
   2034 ** positive to enable FK enforcement or negative to leave FK enforcement
   2035 ** unchanged.  The second parameter is a pointer to an integer into which
   2036 ** is written 0 or 1 to indicate whether FK enforcement is off or on
   2037 ** following this call.  The second parameter may be a NULL pointer, in
   2038 ** which case the FK enforcement setting is not reported back. </dd>
   2039 **
   2040 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
   2041 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
   2042 ** There should be two additional arguments.
   2043 ** The first argument is an integer which is 0 to disable triggers,
   2044 ** positive to enable triggers or negative to leave the setting unchanged.
   2045 ** The second parameter is a pointer to an integer into which
   2046 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
   2047 ** following this call.  The second parameter may be a NULL pointer, in
   2048 ** which case the trigger setting is not reported back. </dd>
   2049 **
   2050 ** </dl>
   2051 */
   2052 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
   2053 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
   2054 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
   2055 
   2056 
   2057 /*
   2058 ** CAPI3REF: Enable Or Disable Extended Result Codes
   2059 **
   2060 ** ^The sqlite3_extended_result_codes() routine enables or disables the
   2061 ** [extended result codes] feature of SQLite. ^The extended result
   2062 ** codes are disabled by default for historical compatibility.
   2063 */
   2064 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
   2065 
   2066 /*
   2067 ** CAPI3REF: Last Insert Rowid
   2068 **
   2069 ** ^Each entry in an SQLite table has a unique 64-bit signed
   2070 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
   2071 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
   2072 ** names are not also used by explicitly declared columns. ^If
   2073 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
   2074 ** is another alias for the rowid.
   2075 **
   2076 ** ^This routine returns the [rowid] of the most recent
   2077 ** successful [INSERT] into the database from the [database connection]
   2078 ** in the first argument.  ^If no successful [INSERT]s
   2079 ** have ever occurred on that database connection, zero is returned.
   2080 **
   2081 ** ^(If an [INSERT] occurs within a trigger, then the [rowid] of the inserted
   2082 ** row is returned by this routine as long as the trigger is running.
   2083 ** But once the trigger terminates, the value returned by this routine
   2084 ** reverts to the last value inserted before the trigger fired.)^
   2085 **
   2086 ** ^An [INSERT] that fails due to a constraint violation is not a
   2087 ** successful [INSERT] and does not change the value returned by this
   2088 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
   2089 ** and INSERT OR ABORT make no changes to the return value of this
   2090 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
   2091 ** encounters a constraint violation, it does not fail.  The
   2092 ** INSERT continues to completion after deleting rows that caused
   2093 ** the constraint problem so INSERT OR REPLACE will always change
   2094 ** the return value of this interface.)^
   2095 **
   2096 ** ^For the purposes of this routine, an [INSERT] is considered to
   2097 ** be successful even if it is subsequently rolled back.
   2098 **
   2099 ** This function is accessible to SQL statements via the
   2100 ** [last_insert_rowid() SQL function].
   2101 **
   2102 ** If a separate thread performs a new [INSERT] on the same
   2103 ** database connection while the [sqlite3_last_insert_rowid()]
   2104 ** function is running and thus changes the last insert [rowid],
   2105 ** then the value returned by [sqlite3_last_insert_rowid()] is
   2106 ** unpredictable and might not equal either the old or the new
   2107 ** last insert [rowid].
   2108 */
   2109 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
   2110 
   2111 /*
   2112 ** CAPI3REF: Count The Number Of Rows Modified
   2113 **
   2114 ** ^This function returns the number of database rows that were changed
   2115 ** or inserted or deleted by the most recently completed SQL statement
   2116 ** on the [database connection] specified by the first parameter.
   2117 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
   2118 ** or [DELETE] statement are counted.  Auxiliary changes caused by
   2119 ** triggers or [foreign key actions] are not counted.)^ Use the
   2120 ** [sqlite3_total_changes()] function to find the total number of changes
   2121 ** including changes caused by triggers and foreign key actions.
   2122 **
   2123 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
   2124 ** are not counted.  Only real table changes are counted.
   2125 **
   2126 ** ^(A "row change" is a change to a single row of a single table
   2127 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
   2128 ** are changed as side effects of [REPLACE] constraint resolution,
   2129 ** rollback, ABORT processing, [DROP TABLE], or by any other
   2130 ** mechanisms do not count as direct row changes.)^
   2131 **
   2132 ** A "trigger context" is a scope of execution that begins and
   2133 ** ends with the script of a [CREATE TRIGGER | trigger].
   2134 ** Most SQL statements are
   2135 ** evaluated outside of any trigger.  This is the "top level"
   2136 ** trigger context.  If a trigger fires from the top level, a
   2137 ** new trigger context is entered for the duration of that one
   2138 ** trigger.  Subtriggers create subcontexts for their duration.
   2139 **
   2140 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
   2141 ** not create a new trigger context.
   2142 **
   2143 ** ^This function returns the number of direct row changes in the
   2144 ** most recent INSERT, UPDATE, or DELETE statement within the same
   2145 ** trigger context.
   2146 **
   2147 ** ^Thus, when called from the top level, this function returns the
   2148 ** number of changes in the most recent INSERT, UPDATE, or DELETE
   2149 ** that also occurred at the top level.  ^(Within the body of a trigger,
   2150 ** the sqlite3_changes() interface can be called to find the number of
   2151 ** changes in the most recently completed INSERT, UPDATE, or DELETE
   2152 ** statement within the body of the same trigger.
   2153 ** However, the number returned does not include changes
   2154 ** caused by subtriggers since those have their own context.)^
   2155 **
   2156 ** See also the [sqlite3_total_changes()] interface, the
   2157 ** [count_changes pragma], and the [changes() SQL function].
   2158 **
   2159 ** If a separate thread makes changes on the same database connection
   2160 ** while [sqlite3_changes()] is running then the value returned
   2161 ** is unpredictable and not meaningful.
   2162 */
   2163 SQLITE_API int sqlite3_changes(sqlite3*);
   2164 
   2165 /*
   2166 ** CAPI3REF: Total Number Of Rows Modified
   2167 **
   2168 ** ^This function returns the number of row changes caused by [INSERT],
   2169 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
   2170 ** ^(The count returned by sqlite3_total_changes() includes all changes
   2171 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
   2172 ** [foreign key actions]. However,
   2173 ** the count does not include changes used to implement [REPLACE] constraints,
   2174 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
   2175 ** count does not include rows of views that fire an [INSTEAD OF trigger],
   2176 ** though if the INSTEAD OF trigger makes changes of its own, those changes
   2177 ** are counted.)^
   2178 ** ^The sqlite3_total_changes() function counts the changes as soon as
   2179 ** the statement that makes them is completed (when the statement handle
   2180 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
   2181 **
   2182 ** See also the [sqlite3_changes()] interface, the
   2183 ** [count_changes pragma], and the [total_changes() SQL function].
   2184 **
   2185 ** If a separate thread makes changes on the same database connection
   2186 ** while [sqlite3_total_changes()] is running then the value
   2187 ** returned is unpredictable and not meaningful.
   2188 */
   2189 SQLITE_API int sqlite3_total_changes(sqlite3*);
   2190 
   2191 /*
   2192 ** CAPI3REF: Interrupt A Long-Running Query
   2193 **
   2194 ** ^This function causes any pending database operation to abort and
   2195 ** return at its earliest opportunity. This routine is typically
   2196 ** called in response to a user action such as pressing "Cancel"
   2197 ** or Ctrl-C where the user wants a long query operation to halt
   2198 ** immediately.
   2199 **
   2200 ** ^It is safe to call this routine from a thread different from the
   2201 ** thread that is currently running the database operation.  But it
   2202 ** is not safe to call this routine with a [database connection] that
   2203 ** is closed or might close before sqlite3_interrupt() returns.
   2204 **
   2205 ** ^If an SQL operation is very nearly finished at the time when
   2206 ** sqlite3_interrupt() is called, then it might not have an opportunity
   2207 ** to be interrupted and might continue to completion.
   2208 **
   2209 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
   2210 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
   2211 ** that is inside an explicit transaction, then the entire transaction
   2212 ** will be rolled back automatically.
   2213 **
   2214 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
   2215 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
   2216 ** that are started after the sqlite3_interrupt() call and before the
   2217 ** running statements reaches zero are interrupted as if they had been
   2218 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
   2219 ** that are started after the running statement count reaches zero are
   2220 ** not effected by the sqlite3_interrupt().
   2221 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
   2222 ** SQL statements is a no-op and has no effect on SQL statements
   2223 ** that are started after the sqlite3_interrupt() call returns.
   2224 **
   2225 ** If the database connection closes while [sqlite3_interrupt()]
   2226 ** is running then bad things will likely happen.
   2227 */
   2228 SQLITE_API void sqlite3_interrupt(sqlite3*);
   2229 
   2230 /*
   2231 ** CAPI3REF: Determine If An SQL Statement Is Complete
   2232 **
   2233 ** These routines are useful during command-line input to determine if the
   2234 ** currently entered text seems to form a complete SQL statement or
   2235 ** if additional input is needed before sending the text into
   2236 ** SQLite for parsing.  ^These routines return 1 if the input string
   2237 ** appears to be a complete SQL statement.  ^A statement is judged to be
   2238 ** complete if it ends with a semicolon token and is not a prefix of a
   2239 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
   2240 ** string literals or quoted identifier names or comments are not
   2241 ** independent tokens (they are part of the token in which they are
   2242 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
   2243 ** and comments that follow the final semicolon are ignored.
   2244 **
   2245 ** ^These routines return 0 if the statement is incomplete.  ^If a
   2246 ** memory allocation fails, then SQLITE_NOMEM is returned.
   2247 **
   2248 ** ^These routines do not parse the SQL statements thus
   2249 ** will not detect syntactically incorrect SQL.
   2250 **
   2251 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
   2252 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
   2253 ** automatically by sqlite3_complete16().  If that initialization fails,
   2254 ** then the return value from sqlite3_complete16() will be non-zero
   2255 ** regardless of whether or not the input SQL is complete.)^
   2256 **
   2257 ** The input to [sqlite3_complete()] must be a zero-terminated
   2258 ** UTF-8 string.
   2259 **
   2260 ** The input to [sqlite3_complete16()] must be a zero-terminated
   2261 ** UTF-16 string in native byte order.
   2262 */
   2263 SQLITE_API int sqlite3_complete(const char *sql);
   2264 SQLITE_API int sqlite3_complete16(const void *sql);
   2265 
   2266 /*
   2267 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
   2268 **
   2269 ** ^This routine sets a callback function that might be invoked whenever
   2270 ** an attempt is made to open a database table that another thread
   2271 ** or process has locked.
   2272 **
   2273 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
   2274 ** is returned immediately upon encountering the lock.  ^If the busy callback
   2275 ** is not NULL, then the callback might be invoked with two arguments.
   2276 **
   2277 ** ^The first argument to the busy handler is a copy of the void* pointer which
   2278 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
   2279 ** the busy handler callback is the number of times that the busy handler has
   2280 ** been invoked for this locking event.  ^If the
   2281 ** busy callback returns 0, then no additional attempts are made to
   2282 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
   2283 ** ^If the callback returns non-zero, then another attempt
   2284 ** is made to open the database for reading and the cycle repeats.
   2285 **
   2286 ** The presence of a busy handler does not guarantee that it will be invoked
   2287 ** when there is lock contention. ^If SQLite determines that invoking the busy
   2288 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
   2289 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
   2290 ** Consider a scenario where one process is holding a read lock that
   2291 ** it is trying to promote to a reserved lock and
   2292 ** a second process is holding a reserved lock that it is trying
   2293 ** to promote to an exclusive lock.  The first process cannot proceed
   2294 ** because it is blocked by the second and the second process cannot
   2295 ** proceed because it is blocked by the first.  If both processes
   2296 ** invoke the busy handlers, neither will make any progress.  Therefore,
   2297 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
   2298 ** will induce the first process to release its read lock and allow
   2299 ** the second process to proceed.
   2300 **
   2301 ** ^The default busy callback is NULL.
   2302 **
   2303 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
   2304 ** when SQLite is in the middle of a large transaction where all the
   2305 ** changes will not fit into the in-memory cache.  SQLite will
   2306 ** already hold a RESERVED lock on the database file, but it needs
   2307 ** to promote this lock to EXCLUSIVE so that it can spill cache
   2308 ** pages into the database file without harm to concurrent
   2309 ** readers.  ^If it is unable to promote the lock, then the in-memory
   2310 ** cache will be left in an inconsistent state and so the error
   2311 ** code is promoted from the relatively benign [SQLITE_BUSY] to
   2312 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
   2313 ** forces an automatic rollback of the changes.  See the
   2314 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
   2315 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
   2316 ** this is important.
   2317 **
   2318 ** ^(There can only be a single busy handler defined for each
   2319 ** [database connection].  Setting a new busy handler clears any
   2320 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
   2321 ** will also set or clear the busy handler.
   2322 **
   2323 ** The busy callback should not take any actions which modify the
   2324 ** database connection that invoked the busy handler.  Any such actions
   2325 ** result in undefined behavior.
   2326 **
   2327 ** A busy handler must not close the database connection
   2328 ** or [prepared statement] that invoked the busy handler.
   2329 */
   2330 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
   2331 
   2332 /*
   2333 ** CAPI3REF: Set A Busy Timeout
   2334 **
   2335 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
   2336 ** for a specified amount of time when a table is locked.  ^The handler
   2337 ** will sleep multiple times until at least "ms" milliseconds of sleeping
   2338 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
   2339 ** the handler returns 0 which causes [sqlite3_step()] to return
   2340 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
   2341 **
   2342 ** ^Calling this routine with an argument less than or equal to zero
   2343 ** turns off all busy handlers.
   2344 **
   2345 ** ^(There can only be a single busy handler for a particular
   2346 ** [database connection] any any given moment.  If another busy handler
   2347 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
   2348 ** this routine, that other busy handler is cleared.)^
   2349 */
   2350 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
   2351 
   2352 /*
   2353 ** CAPI3REF: Convenience Routines For Running Queries
   2354 **
   2355 ** This is a legacy interface that is preserved for backwards compatibility.
   2356 ** Use of this interface is not recommended.
   2357 **
   2358 ** Definition: A <b>result table</b> is memory data structure created by the
   2359 ** [sqlite3_get_table()] interface.  A result table records the
   2360 ** complete query results from one or more queries.
   2361 **
   2362 ** The table conceptually has a number of rows and columns.  But
   2363 ** these numbers are not part of the result table itself.  These
   2364 ** numbers are obtained separately.  Let N be the number of rows
   2365 ** and M be the number of columns.
   2366 **
   2367 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
   2368 ** There are (N+1)*M elements in the array.  The first M pointers point
   2369 ** to zero-terminated strings that  contain the names of the columns.
   2370 ** The remaining entries all point to query results.  NULL values result
   2371 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
   2372 ** string representation as returned by [sqlite3_column_text()].
   2373 **
   2374 ** A result table might consist of one or more memory allocations.
   2375 ** It is not safe to pass a result table directly to [sqlite3_free()].
   2376 ** A result table should be deallocated using [sqlite3_free_table()].
   2377 **
   2378 ** ^(As an example of the result table format, suppose a query result
   2379 ** is as follows:
   2380 **
   2381 ** <blockquote><pre>
   2382 **        Name        | Age
   2383 **        -----------------------
   2384 **        Alice       | 43
   2385 **        Bob         | 28
   2386 **        Cindy       | 21
   2387 ** </pre></blockquote>
   2388 **
   2389 ** There are two column (M==2) and three rows (N==3).  Thus the
   2390 ** result table has 8 entries.  Suppose the result table is stored
   2391 ** in an array names azResult.  Then azResult holds this content:
   2392 **
   2393 ** <blockquote><pre>
   2394 **        azResult&#91;0] = "Name";
   2395 **        azResult&#91;1] = "Age";
   2396 **        azResult&#91;2] = "Alice";
   2397 **        azResult&#91;3] = "43";
   2398 **        azResult&#91;4] = "Bob";
   2399 **        azResult&#91;5] = "28";
   2400 **        azResult&#91;6] = "Cindy";
   2401 **        azResult&#91;7] = "21";
   2402 ** </pre></blockquote>)^
   2403 **
   2404 ** ^The sqlite3_get_table() function evaluates one or more
   2405 ** semicolon-separated SQL statements in the zero-terminated UTF-8
   2406 ** string of its 2nd parameter and returns a result table to the
   2407 ** pointer given in its 3rd parameter.
   2408 **
   2409 ** After the application has finished with the result from sqlite3_get_table(),
   2410 ** it must pass the result table pointer to sqlite3_free_table() in order to
   2411 ** release the memory that was malloced.  Because of the way the
   2412 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
   2413 ** function must not try to call [sqlite3_free()] directly.  Only
   2414 ** [sqlite3_free_table()] is able to release the memory properly and safely.
   2415 **
   2416 ** The sqlite3_get_table() interface is implemented as a wrapper around
   2417 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
   2418 ** to any internal data structures of SQLite.  It uses only the public
   2419 ** interface defined here.  As a consequence, errors that occur in the
   2420 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
   2421 ** reflected in subsequent calls to [sqlite3_errcode()] or
   2422 ** [sqlite3_errmsg()].
   2423 */
   2424 SQLITE_API int sqlite3_get_table(
   2425   sqlite3 *db,          /* An open database */
   2426   const char *zSql,     /* SQL to be evaluated */
   2427   char ***pazResult,    /* Results of the query */
   2428   int *pnRow,           /* Number of result rows written here */
   2429   int *pnColumn,        /* Number of result columns written here */
   2430   char **pzErrmsg       /* Error msg written here */
   2431 );
   2432 SQLITE_API void sqlite3_free_table(char **result);
   2433 
   2434 /*
   2435 ** CAPI3REF: Formatted String Printing Functions
   2436 **
   2437 ** These routines are work-alikes of the "printf()" family of functions
   2438 ** from the standard C library.
   2439 **
   2440 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
   2441 ** results into memory obtained from [sqlite3_malloc()].
   2442 ** The strings returned by these two routines should be
   2443 ** released by [sqlite3_free()].  ^Both routines return a
   2444 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
   2445 ** memory to hold the resulting string.
   2446 **
   2447 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
   2448 ** the standard C library.  The result is written into the
   2449 ** buffer supplied as the second parameter whose size is given by
   2450 ** the first parameter. Note that the order of the
   2451 ** first two parameters is reversed from snprintf().)^  This is an
   2452 ** historical accident that cannot be fixed without breaking
   2453 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
   2454 ** returns a pointer to its buffer instead of the number of
   2455 ** characters actually written into the buffer.)^  We admit that
   2456 ** the number of characters written would be a more useful return
   2457 ** value but we cannot change the implementation of sqlite3_snprintf()
   2458 ** now without breaking compatibility.
   2459 **
   2460 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
   2461 ** guarantees that the buffer is always zero-terminated.  ^The first
   2462 ** parameter "n" is the total size of the buffer, including space for
   2463 ** the zero terminator.  So the longest string that can be completely
   2464 ** written will be n-1 characters.
   2465 **
   2466 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
   2467 **
   2468 ** These routines all implement some additional formatting
   2469 ** options that are useful for constructing SQL statements.
   2470 ** All of the usual printf() formatting options apply.  In addition, there
   2471 ** is are "%q", "%Q", and "%z" options.
   2472 **
   2473 ** ^(The %q option works like %s in that it substitutes a null-terminated
   2474 ** string from the argument list.  But %q also doubles every '\'' character.
   2475 ** %q is designed for use inside a string literal.)^  By doubling each '\''
   2476 ** character it escapes that character and allows it to be inserted into
   2477 ** the string.
   2478 **
   2479 ** For example, assume the string variable zText contains text as follows:
   2480 **
   2481 ** <blockquote><pre>
   2482 **  char *zText = "It's a happy day!";
   2483 ** </pre></blockquote>
   2484 **
   2485 ** One can use this text in an SQL statement as follows:
   2486 **
   2487 ** <blockquote><pre>
   2488 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
   2489 **  sqlite3_exec(db, zSQL, 0, 0, 0);
   2490 **  sqlite3_free(zSQL);
   2491 ** </pre></blockquote>
   2492 **
   2493 ** Because the %q format string is used, the '\'' character in zText
   2494 ** is escaped and the SQL generated is as follows:
   2495 **
   2496 ** <blockquote><pre>
   2497 **  INSERT INTO table1 VALUES('It''s a happy day!')
   2498 ** </pre></blockquote>
   2499 **
   2500 ** This is correct.  Had we used %s instead of %q, the generated SQL
   2501 ** would have looked like this:
   2502 **
   2503 ** <blockquote><pre>
   2504 **  INSERT INTO table1 VALUES('It's a happy day!');
   2505 ** </pre></blockquote>
   2506 **
   2507 ** This second example is an SQL syntax error.  As a general rule you should
   2508 ** always use %q instead of %s when inserting text into a string literal.
   2509 **
   2510 ** ^(The %Q option works like %q except it also adds single quotes around
   2511 ** the outside of the total string.  Additionally, if the parameter in the
   2512 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
   2513 ** single quotes).)^  So, for example, one could say:
   2514 **
   2515 ** <blockquote><pre>
   2516 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
   2517 **  sqlite3_exec(db, zSQL, 0, 0, 0);
   2518 **  sqlite3_free(zSQL);
   2519 ** </pre></blockquote>
   2520 **
   2521 ** The code above will render a correct SQL statement in the zSQL
   2522 ** variable even if the zText variable is a NULL pointer.
   2523 **
   2524 ** ^(The "%z" formatting option works like "%s" but with the
   2525 ** addition that after the string has been read and copied into
   2526 ** the result, [sqlite3_free()] is called on the input string.)^
   2527 */
   2528 SQLITE_API char *sqlite3_mprintf(const char*,...);
   2529 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
   2530 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
   2531 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
   2532 
   2533 /*
   2534 ** CAPI3REF: Memory Allocation Subsystem
   2535 **
   2536 ** The SQLite core uses these three routines for all of its own
   2537 ** internal memory allocation needs. "Core" in the previous sentence
   2538 ** does not include operating-system specific VFS implementation.  The
   2539 ** Windows VFS uses native malloc() and free() for some operations.
   2540 **
   2541 ** ^The sqlite3_malloc() routine returns a pointer to a block
   2542 ** of memory at least N bytes in length, where N is the parameter.
   2543 ** ^If sqlite3_malloc() is unable to obtain sufficient free
   2544 ** memory, it returns a NULL pointer.  ^If the parameter N to
   2545 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
   2546 ** a NULL pointer.
   2547 **
   2548 ** ^Calling sqlite3_free() with a pointer previously returned
   2549 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
   2550 ** that it might be reused.  ^The sqlite3_free() routine is
   2551 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
   2552 ** to sqlite3_free() is harmless.  After being freed, memory
   2553 ** should neither be read nor written.  Even reading previously freed
   2554 ** memory might result in a segmentation fault or other severe error.
   2555 ** Memory corruption, a segmentation fault, or other severe error
   2556 ** might result if sqlite3_free() is called with a non-NULL pointer that
   2557 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
   2558 **
   2559 ** ^(The sqlite3_realloc() interface attempts to resize a
   2560 ** prior memory allocation to be at least N bytes, where N is the
   2561 ** second parameter.  The memory allocation to be resized is the first
   2562 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
   2563 ** is a NULL pointer then its behavior is identical to calling
   2564 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
   2565 ** ^If the second parameter to sqlite3_realloc() is zero or
   2566 ** negative then the behavior is exactly the same as calling
   2567 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
   2568 ** ^sqlite3_realloc() returns a pointer to a memory allocation
   2569 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
   2570 ** ^If M is the size of the prior allocation, then min(N,M) bytes
   2571 ** of the prior allocation are copied into the beginning of buffer returned
   2572 ** by sqlite3_realloc() and the prior allocation is freed.
   2573 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
   2574 ** is not freed.
   2575 **
   2576 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
   2577 ** is always aligned to at least an 8 byte boundary, or to a
   2578 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
   2579 ** option is used.
   2580 **
   2581 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
   2582 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
   2583 ** implementation of these routines to be omitted.  That capability
   2584 ** is no longer provided.  Only built-in memory allocators can be used.
   2585 **
   2586 ** The Windows OS interface layer calls
   2587 ** the system malloc() and free() directly when converting
   2588 ** filenames between the UTF-8 encoding used by SQLite
   2589 ** and whatever filename encoding is used by the particular Windows
   2590 ** installation.  Memory allocation errors are detected, but
   2591 ** they are reported back as [SQLITE_CANTOPEN] or
   2592 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
   2593 **
   2594 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
   2595 ** must be either NULL or else pointers obtained from a prior
   2596 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
   2597 ** not yet been released.
   2598 **
   2599 ** The application must not read or write any part of
   2600 ** a block of memory after it has been released using
   2601 ** [sqlite3_free()] or [sqlite3_realloc()].
   2602 */
   2603 SQLITE_API void *sqlite3_malloc(int);
   2604 SQLITE_API void *sqlite3_realloc(void*, int);
   2605 SQLITE_API void sqlite3_free(void*);
   2606 
   2607 /*
   2608 ** CAPI3REF: Memory Allocator Statistics
   2609 **
   2610 ** SQLite provides these two interfaces for reporting on the status
   2611 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
   2612 ** routines, which form the built-in memory allocation subsystem.
   2613 **
   2614 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
   2615 ** of memory currently outstanding (malloced but not freed).
   2616 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
   2617 ** value of [sqlite3_memory_used()] since the high-water mark
   2618 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
   2619 ** [sqlite3_memory_highwater()] include any overhead
   2620 ** added by SQLite in its implementation of [sqlite3_malloc()],
   2621 ** but not overhead added by the any underlying system library
   2622 ** routines that [sqlite3_malloc()] may call.
   2623 **
   2624 ** ^The memory high-water mark is reset to the current value of
   2625 ** [sqlite3_memory_used()] if and only if the parameter to
   2626 ** [sqlite3_memory_highwater()] is true.  ^The value returned
   2627 ** by [sqlite3_memory_highwater(1)] is the high-water mark
   2628 ** prior to the reset.
   2629 */
   2630 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
   2631 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
   2632 
   2633 /*
   2634 ** CAPI3REF: Pseudo-Random Number Generator
   2635 **
   2636 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
   2637 ** select random [ROWID | ROWIDs] when inserting new records into a table that
   2638 ** already uses the largest possible [ROWID].  The PRNG is also used for
   2639 ** the build-in random() and randomblob() SQL functions.  This interface allows
   2640 ** applications to access the same PRNG for other purposes.
   2641 **
   2642 ** ^A call to this routine stores N bytes of randomness into buffer P.
   2643 **
   2644 ** ^The first time this routine is invoked (either internally or by
   2645 ** the application) the PRNG is seeded using randomness obtained
   2646 ** from the xRandomness method of the default [sqlite3_vfs] object.
   2647 ** ^On all subsequent invocations, the pseudo-randomness is generated
   2648 ** internally and without recourse to the [sqlite3_vfs] xRandomness
   2649 ** method.
   2650 */
   2651 SQLITE_API void sqlite3_randomness(int N, void *P);
   2652 
   2653 /*
   2654 ** CAPI3REF: Compile-Time Authorization Callbacks
   2655 **
   2656 ** ^This routine registers an authorizer callback with a particular
   2657 ** [database connection], supplied in the first argument.
   2658 ** ^The authorizer callback is invoked as SQL statements are being compiled
   2659 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
   2660 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
   2661 ** points during the compilation process, as logic is being created
   2662 ** to perform various actions, the authorizer callback is invoked to
   2663 ** see if those actions are allowed.  ^The authorizer callback should
   2664 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
   2665 ** specific action but allow the SQL statement to continue to be
   2666 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
   2667 ** rejected with an error.  ^If the authorizer callback returns
   2668 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
   2669 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
   2670 ** the authorizer will fail with an error message.
   2671 **
   2672 ** When the callback returns [SQLITE_OK], that means the operation
   2673 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
   2674 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
   2675 ** authorizer will fail with an error message explaining that
   2676 ** access is denied.
   2677 **
   2678 ** ^The first parameter to the authorizer callback is a copy of the third
   2679 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
   2680 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
   2681 ** the particular action to be authorized. ^The third through sixth parameters
   2682 ** to the callback are zero-terminated strings that contain additional
   2683 ** details about the action to be authorized.
   2684 **
   2685 ** ^If the action code is [SQLITE_READ]
   2686 ** and the callback returns [SQLITE_IGNORE] then the
   2687 ** [prepared statement] statement is constructed to substitute
   2688 ** a NULL value in place of the table column that would have
   2689 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
   2690 ** return can be used to deny an untrusted user access to individual
   2691 ** columns of a table.
   2692 ** ^If the action code is [SQLITE_DELETE] and the callback returns
   2693 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
   2694 ** [truncate optimization] is disabled and all rows are deleted individually.
   2695 **
   2696 ** An authorizer is used when [sqlite3_prepare | preparing]
   2697 ** SQL statements from an untrusted source, to ensure that the SQL statements
   2698 ** do not try to access data they are not allowed to see, or that they do not
   2699 ** try to execute malicious statements that damage the database.  For
   2700 ** example, an application may allow a user to enter arbitrary
   2701 ** SQL queries for evaluation by a database.  But the application does
   2702 ** not want the user to be able to make arbitrary changes to the
   2703 ** database.  An authorizer could then be put in place while the
   2704 ** user-entered SQL is being [sqlite3_prepare | prepared] that
   2705 ** disallows everything except [SELECT] statements.
   2706 **
   2707 ** Applications that need to process SQL from untrusted sources
   2708 ** might also consider lowering resource limits using [sqlite3_limit()]
   2709 ** and limiting database size using the [max_page_count] [PRAGMA]
   2710 ** in addition to using an authorizer.
   2711 **
   2712 ** ^(Only a single authorizer can be in place on a database connection
   2713 ** at a time.  Each call to sqlite3_set_authorizer overrides the
   2714 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
   2715 ** The authorizer is disabled by default.
   2716 **
   2717 ** The authorizer callback must not do anything that will modify
   2718 ** the database connection that invoked the authorizer callback.
   2719 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
   2720 ** database connections for the meaning of "modify" in this paragraph.
   2721 **
   2722 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
   2723 ** statement might be re-prepared during [sqlite3_step()] due to a
   2724 ** schema change.  Hence, the application should ensure that the
   2725 ** correct authorizer callback remains in place during the [sqlite3_step()].
   2726 **
   2727 ** ^Note that the authorizer callback is invoked only during
   2728 ** [sqlite3_prepare()] or its variants.  Authorization is not
   2729 ** performed during statement evaluation in [sqlite3_step()], unless
   2730 ** as stated in the previous paragraph, sqlite3_step() invokes
   2731 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
   2732 */
   2733 SQLITE_API int sqlite3_set_authorizer(
   2734   sqlite3*,
   2735   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
   2736   void *pUserData
   2737 );
   2738 
   2739 /*
   2740 ** CAPI3REF: Authorizer Return Codes
   2741 **
   2742 ** The [sqlite3_set_authorizer | authorizer callback function] must
   2743 ** return either [SQLITE_OK] or one of these two constants in order
   2744 ** to signal SQLite whether or not the action is permitted.  See the
   2745 ** [sqlite3_set_authorizer | authorizer documentation] for additional
   2746 ** information.
   2747 */
   2748 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
   2749 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
   2750 
   2751 /*
   2752 ** CAPI3REF: Authorizer Action Codes
   2753 **
   2754 ** The [sqlite3_set_authorizer()] interface registers a callback function
   2755 ** that is invoked to authorize certain SQL statement actions.  The
   2756 ** second parameter to the callback is an integer code that specifies
   2757 ** what action is being authorized.  These are the integer action codes that
   2758 ** the authorizer callback may be passed.
   2759 **
   2760 ** These action code values signify what kind of operation is to be
   2761 ** authorized.  The 3rd and 4th parameters to the authorization
   2762 ** callback function will be parameters or NULL depending on which of these
   2763 ** codes is used as the second parameter.  ^(The 5th parameter to the
   2764 ** authorizer callback is the name of the database ("main", "temp",
   2765 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
   2766 ** is the name of the inner-most trigger or view that is responsible for
   2767 ** the access attempt or NULL if this access attempt is directly from
   2768 ** top-level SQL code.
   2769 */
   2770 /******************************************* 3rd ************ 4th ***********/
   2771 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
   2772 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
   2773 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
   2774 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
   2775 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
   2776 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
   2777 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
   2778 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
   2779 #define SQLITE_DELETE                9   /* Table Name      NULL            */
   2780 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
   2781 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
   2782 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
   2783 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
   2784 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
   2785 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
   2786 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
   2787 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
   2788 #define SQLITE_INSERT               18   /* Table Name      NULL            */
   2789 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
   2790 #define SQLITE_READ                 20   /* Table Name      Column Name     */
   2791 #define SQLITE_SELECT               21   /* NULL            NULL            */
   2792 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
   2793 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
   2794 #define SQLITE_ATTACH               24   /* Filename        NULL            */
   2795 #define SQLITE_DETACH               25   /* Database Name   NULL            */
   2796 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
   2797 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
   2798 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
   2799 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
   2800 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
   2801 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
   2802 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
   2803 #define SQLITE_COPY                  0   /* No longer used */
   2804 
   2805 /*
   2806 ** CAPI3REF: Tracing And Profiling Functions
   2807 **
   2808 ** These routines register callback functions that can be used for
   2809 ** tracing and profiling the execution of SQL statements.
   2810 **
   2811 ** ^The callback function registered by sqlite3_trace() is invoked at
   2812 ** various times when an SQL statement is being run by [sqlite3_step()].
   2813 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
   2814 ** SQL statement text as the statement first begins executing.
   2815 ** ^(Additional sqlite3_trace() callbacks might occur
   2816 ** as each triggered subprogram is entered.  The callbacks for triggers
   2817 ** contain a UTF-8 SQL comment that identifies the trigger.)^
   2818 **
   2819 ** ^The callback function registered by sqlite3_profile() is invoked
   2820 ** as each SQL statement finishes.  ^The profile callback contains
   2821 ** the original statement text and an estimate of wall-clock time
   2822 ** of how long that statement took to run.  ^The profile callback
   2823 ** time is in units of nanoseconds, however the current implementation
   2824 ** is only capable of millisecond resolution so the six least significant
   2825 ** digits in the time are meaningless.  Future versions of SQLite
   2826 ** might provide greater resolution on the profiler callback.  The
   2827 ** sqlite3_profile() function is considered experimental and is
   2828 ** subject to change in future versions of SQLite.
   2829 */
   2830 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
   2831 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
   2832    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
   2833 
   2834 /*
   2835 ** CAPI3REF: Query Progress Callbacks
   2836 **
   2837 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
   2838 ** function X to be invoked periodically during long running calls to
   2839 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
   2840 ** database connection D.  An example use for this
   2841 ** interface is to keep a GUI updated during a large query.
   2842 **
   2843 ** ^The parameter P is passed through as the only parameter to the
   2844 ** callback function X.  ^The parameter N is the number of
   2845 ** [virtual machine instructions] that are evaluated between successive
   2846 ** invocations of the callback X.
   2847 **
   2848 ** ^Only a single progress handler may be defined at one time per
   2849 ** [database connection]; setting a new progress handler cancels the
   2850 ** old one.  ^Setting parameter X to NULL disables the progress handler.
   2851 ** ^The progress handler is also disabled by setting N to a value less
   2852 ** than 1.
   2853 **
   2854 ** ^If the progress callback returns non-zero, the operation is
   2855 ** interrupted.  This feature can be used to implement a
   2856 ** "Cancel" button on a GUI progress dialog box.
   2857 **
   2858 ** The progress handler callback must not do anything that will modify
   2859 ** the database connection that invoked the progress handler.
   2860 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
   2861 ** database connections for the meaning of "modify" in this paragraph.
   2862 **
   2863 */
   2864 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
   2865 
   2866 /*
   2867 ** CAPI3REF: Opening A New Database Connection
   2868 **
   2869 ** ^These routines open an SQLite database file whose name is given by the
   2870 ** filename argument. ^The filename argument is interpreted as UTF-8 for
   2871 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
   2872 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
   2873 ** returned in *ppDb, even if an error occurs.  The only exception is that
   2874 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
   2875 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
   2876 ** object.)^ ^(If the database is opened (and/or created) successfully, then
   2877 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
   2878 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
   2879 ** an English language description of the error following a failure of any
   2880 ** of the sqlite3_open() routines.
   2881 **
   2882 ** ^The default encoding for the database will be UTF-8 if
   2883 ** sqlite3_open() or sqlite3_open_v2() is called and
   2884 ** UTF-16 in the native byte order if sqlite3_open16() is used.
   2885 **
   2886 ** Whether or not an error occurs when it is opened, resources
   2887 ** associated with the [database connection] handle should be released by
   2888 ** passing it to [sqlite3_close()] when it is no longer required.
   2889 **
   2890 ** The sqlite3_open_v2() interface works like sqlite3_open()
   2891 ** except that it accepts two additional parameters for additional control
   2892 ** over the new database connection.  ^(The flags parameter to
   2893 ** sqlite3_open_v2() can take one of
   2894 ** the following three values, optionally combined with the
   2895 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
   2896 ** and/or [SQLITE_OPEN_PRIVATECACHE] flags:)^
   2897 **
   2898 ** <dl>
   2899 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
   2900 ** <dd>The database is opened in read-only mode.  If the database does not
   2901 ** already exist, an error is returned.</dd>)^
   2902 **
   2903 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
   2904 ** <dd>The database is opened for reading and writing if possible, or reading
   2905 ** only if the file is write protected by the operating system.  In either
   2906 ** case the database must already exist, otherwise an error is returned.</dd>)^
   2907 **
   2908 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
   2909 ** <dd>The database is opened for reading and writing, and is created if
   2910 ** it does not already exist. This is the behavior that is always used for
   2911 ** sqlite3_open() and sqlite3_open16().</dd>)^
   2912 ** </dl>
   2913 **
   2914 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
   2915 ** combinations shown above or one of the combinations shown above combined
   2916 ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX],
   2917 ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags,
   2918 ** then the behavior is undefined.
   2919 **
   2920 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
   2921 ** opens in the multi-thread [threading mode] as long as the single-thread
   2922 ** mode has not been set at compile-time or start-time.  ^If the
   2923 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
   2924 ** in the serialized [threading mode] unless single-thread was
   2925 ** previously selected at compile-time or start-time.
   2926 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
   2927 ** eligible to use [shared cache mode], regardless of whether or not shared
   2928 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
   2929 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
   2930 ** participate in [shared cache mode] even if it is enabled.
   2931 **
   2932 ** ^If the filename is ":memory:", then a private, temporary in-memory database
   2933 ** is created for the connection.  ^This in-memory database will vanish when
   2934 ** the database connection is closed.  Future versions of SQLite might
   2935 ** make use of additional special filenames that begin with the ":" character.
   2936 ** It is recommended that when a database filename actually does begin with
   2937 ** a ":" character you should prefix the filename with a pathname such as
   2938 ** "./" to avoid ambiguity.
   2939 **
   2940 ** ^If the filename is an empty string, then a private, temporary
   2941 ** on-disk database will be created.  ^This private database will be
   2942 ** automatically deleted as soon as the database connection is closed.
   2943 **
   2944 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
   2945 ** [sqlite3_vfs] object that defines the operating system interface that
   2946 ** the new database connection should use.  ^If the fourth parameter is
   2947 ** a NULL pointer then the default [sqlite3_vfs] object is used.
   2948 **
   2949 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
   2950 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
   2951 ** codepage is currently defined.  Filenames containing international
   2952 ** characters must be converted to UTF-8 prior to passing them into
   2953 ** sqlite3_open() or sqlite3_open_v2().
   2954 */
   2955 SQLITE_API int sqlite3_open(
   2956   const char *filename,   /* Database filename (UTF-8) */
   2957   sqlite3 **ppDb          /* OUT: SQLite db handle */
   2958 );
   2959 SQLITE_API int sqlite3_open16(
   2960   const void *filename,   /* Database filename (UTF-16) */
   2961   sqlite3 **ppDb          /* OUT: SQLite db handle */
   2962 );
   2963 SQLITE_API int sqlite3_open_v2(
   2964   const char *filename,   /* Database filename (UTF-8) */
   2965   sqlite3 **ppDb,         /* OUT: SQLite db handle */
   2966   int flags,              /* Flags */
   2967   const char *zVfs        /* Name of VFS module to use */
   2968 );
   2969 
   2970 /*
   2971 ** CAPI3REF: Error Codes And Messages
   2972 **
   2973 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
   2974 ** [extended result code] for the most recent failed sqlite3_* API call
   2975 ** associated with a [database connection]. If a prior API call failed
   2976 ** but the most recent API call succeeded, the return value from
   2977 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
   2978 ** interface is the same except that it always returns the
   2979 ** [extended result code] even when extended result codes are
   2980 ** disabled.
   2981 **
   2982 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
   2983 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
   2984 ** ^(Memory to hold the error message string is managed internally.
   2985 ** The application does not need to worry about freeing the result.
   2986 ** However, the error string might be overwritten or deallocated by
   2987 ** subsequent calls to other SQLite interface functions.)^
   2988 **
   2989 ** When the serialized [threading mode] is in use, it might be the
   2990 ** case that a second error occurs on a separate thread in between
   2991 ** the time of the first error and the call to these interfaces.
   2992 ** When that happens, the second error will be reported since these
   2993 ** interfaces always report the most recent result.  To avoid
   2994 ** this, each thread can obtain exclusive use of the [database connection] D
   2995 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
   2996 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
   2997 ** all calls to the interfaces listed here are completed.
   2998 **
   2999 ** If an interface fails with SQLITE_MISUSE, that means the interface
   3000 ** was invoked incorrectly by the application.  In that case, the
   3001 ** error code and message may or may not be set.
   3002 */
   3003 SQLITE_API int sqlite3_errcode(sqlite3 *db);
   3004 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
   3005 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
   3006 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
   3007 
   3008 /*
   3009 ** CAPI3REF: SQL Statement Object
   3010 ** KEYWORDS: {prepared statement} {prepared statements}
   3011 **
   3012 ** An instance of this object represents a single SQL statement.
   3013 ** This object is variously known as a "prepared statement" or a
   3014 ** "compiled SQL statement" or simply as a "statement".
   3015 **
   3016 ** The life of a statement object goes something like this:
   3017 **
   3018 ** <ol>
   3019 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
   3020 **      function.
   3021 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
   3022 **      interfaces.
   3023 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
   3024 ** <li> Reset the statement using [sqlite3_reset()] then go back
   3025 **      to step 2.  Do this zero or more times.
   3026 ** <li> Destroy the object using [sqlite3_finalize()].
   3027 ** </ol>
   3028 **
   3029 ** Refer to documentation on individual methods above for additional
   3030 ** information.
   3031 */
   3032 typedef struct sqlite3_stmt sqlite3_stmt;
   3033 
   3034 /*
   3035 ** CAPI3REF: Run-time Limits
   3036 **
   3037 ** ^(This interface allows the size of various constructs to be limited
   3038 ** on a connection by connection basis.  The first parameter is the
   3039 ** [database connection] whose limit is to be set or queried.  The
   3040 ** second parameter is one of the [limit categories] that define a
   3041 ** class of constructs to be size limited.  The third parameter is the
   3042 ** new limit for that construct.)^
   3043 **
   3044 ** ^If the new limit is a negative number, the limit is unchanged.
   3045 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
   3046 ** [limits | hard upper bound]
   3047 ** set at compile-time by a C preprocessor macro called
   3048 ** [limits | SQLITE_MAX_<i>NAME</i>].
   3049 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
   3050 ** ^Attempts to increase a limit above its hard upper bound are
   3051 ** silently truncated to the hard upper bound.
   3052 **
   3053 ** ^Regardless of whether or not the limit was changed, the
   3054 ** [sqlite3_limit()] interface returns the prior value of the limit.
   3055 ** ^Hence, to find the current value of a limit without changing it,
   3056 ** simply invoke this interface with the third parameter set to -1.
   3057 **
   3058 ** Run-time limits are intended for use in applications that manage
   3059 ** both their own internal database and also databases that are controlled
   3060 ** by untrusted external sources.  An example application might be a
   3061 ** web browser that has its own databases for storing history and
   3062 ** separate databases controlled by JavaScript applications downloaded
   3063 ** off the Internet.  The internal databases can be given the
   3064 ** large, default limits.  Databases managed by external sources can
   3065 ** be given much smaller limits designed to prevent a denial of service
   3066 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
   3067 ** interface to further control untrusted SQL.  The size of the database
   3068 ** created by an untrusted script can be contained using the
   3069 ** [max_page_count] [PRAGMA].
   3070 **
   3071 ** New run-time limit categories may be added in future releases.
   3072 */
   3073 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
   3074 
   3075 /*
   3076 ** CAPI3REF: Run-Time Limit Categories
   3077 ** KEYWORDS: {limit category} {*limit categories}
   3078 **
   3079 ** These constants define various performance limits
   3080 ** that can be lowered at run-time using [sqlite3_limit()].
   3081 ** The synopsis of the meanings of the various limits is shown below.
   3082 ** Additional information is available at [limits | Limits in SQLite].
   3083 **
   3084 ** <dl>
   3085 ** ^(<dt>SQLITE_LIMIT_LENGTH</dt>
   3086 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
   3087 **
   3088 ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
   3089 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
   3090 **
   3091 ** ^(<dt>SQLITE_LIMIT_COLUMN</dt>
   3092 ** <dd>The maximum number of columns in a table definition or in the
   3093 ** result set of a [SELECT] or the maximum number of columns in an index
   3094 ** or in an ORDER BY or GROUP BY clause.</dd>)^
   3095 **
   3096 ** ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
   3097 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
   3098 **
   3099 ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
   3100 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
   3101 **
   3102 ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
   3103 ** <dd>The maximum number of instructions in a virtual machine program
   3104 ** used to implement an SQL statement.  This limit is not currently
   3105 ** enforced, though that might be added in some future release of
   3106 ** SQLite.</dd>)^
   3107 **
   3108 ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
   3109 ** <dd>The maximum number of arguments on a function.</dd>)^
   3110 **
   3111 ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
   3112 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
   3113 **
   3114 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
   3115 ** <dd>The maximum length of the pattern argument to the [LIKE] or
   3116 ** [GLOB] operators.</dd>)^
   3117 **
   3118 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
   3119 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
   3120 **
   3121 ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
   3122 ** <dd>The maximum depth of recursion for triggers.</dd>)^
   3123 ** </dl>
   3124 */
   3125 #define SQLITE_LIMIT_LENGTH                    0
   3126 #define SQLITE_LIMIT_SQL_LENGTH                1
   3127 #define SQLITE_LIMIT_COLUMN                    2
   3128 #define SQLITE_LIMIT_EXPR_DEPTH                3
   3129 #define SQLITE_LIMIT_COMPOUND_SELECT           4
   3130 #define SQLITE_LIMIT_VDBE_OP                   5
   3131 #define SQLITE_LIMIT_FUNCTION_ARG              6
   3132 #define SQLITE_LIMIT_ATTACHED                  7
   3133 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
   3134 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
   3135 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
   3136 
   3137 /*
   3138 ** CAPI3REF: Compiling An SQL Statement
   3139 ** KEYWORDS: {SQL statement compiler}
   3140 **
   3141 ** To execute an SQL query, it must first be compiled into a byte-code
   3142 ** program using one of these routines.
   3143 **
   3144 ** The first argument, "db", is a [database connection] obtained from a
   3145 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
   3146 ** [sqlite3_open16()].  The database connection must not have been closed.
   3147 **
   3148 ** The second argument, "zSql", is the statement to be compiled, encoded
   3149 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
   3150 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
   3151 ** use UTF-16.
   3152 **
   3153 ** ^If the nByte argument is less than zero, then zSql is read up to the
   3154 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
   3155 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
   3156 ** zSql string ends at either the first '\000' or '\u0000' character or
   3157 ** the nByte-th byte, whichever comes first. If the caller knows
   3158 ** that the supplied string is nul-terminated, then there is a small
   3159 ** performance advantage to be gained by passing an nByte parameter that
   3160 ** is equal to the number of bytes in the input string <i>including</i>
   3161 ** the nul-terminator bytes.
   3162 **
   3163 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
   3164 ** past the end of the first SQL statement in zSql.  These routines only
   3165 ** compile the first statement in zSql, so *pzTail is left pointing to
   3166 ** what remains uncompiled.
   3167 **
   3168 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
   3169 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
   3170 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
   3171 ** string or a comment) then *ppStmt is set to NULL.
   3172 ** The calling procedure is responsible for deleting the compiled
   3173 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
   3174 ** ppStmt may not be NULL.
   3175 **
   3176 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
   3177 ** otherwise an [error code] is returned.
   3178 **
   3179 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
   3180 ** recommended for all new programs. The two older interfaces are retained
   3181 ** for backwards compatibility, but their use is discouraged.
   3182 ** ^In the "v2" interfaces, the prepared statement
   3183 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
   3184 ** original SQL text. This causes the [sqlite3_step()] interface to
   3185 ** behave differently in three ways:
   3186 **
   3187 ** <ol>
   3188 ** <li>
   3189 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
   3190 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
   3191 ** statement and try to run it again.
   3192 ** </li>
   3193 **
   3194 ** <li>
   3195 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
   3196 ** [error codes] or [extended error codes].  ^The legacy behavior was that
   3197 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
   3198 ** and the application would have to make a second call to [sqlite3_reset()]
   3199 ** in order to find the underlying cause of the problem. With the "v2" prepare
   3200 ** interfaces, the underlying reason for the error is returned immediately.
   3201 ** </li>
   3202 **
   3203 ** <li>
   3204 ** ^If the specific value bound to [parameter | host parameter] in the
   3205 ** WHERE clause might influence the choice of query plan for a statement,
   3206 ** then the statement will be automatically recompiled, as if there had been
   3207 ** a schema change, on the first  [sqlite3_step()] call following any change
   3208 ** to the [sqlite3_bind_text | bindings] of that [parameter].
   3209 ** ^The specific value of WHERE-clause [parameter] might influence the
   3210 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
   3211 ** or [GLOB] operator or if the parameter is compared to an indexed column
   3212 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
   3213 ** the
   3214 ** </li>
   3215 ** </ol>
   3216 */
   3217 SQLITE_API int sqlite3_prepare(
   3218   sqlite3 *db,            /* Database handle */
   3219   const char *zSql,       /* SQL statement, UTF-8 encoded */
   3220   int nByte,              /* Maximum length of zSql in bytes. */
   3221   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   3222   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
   3223 );
   3224 SQLITE_API int sqlite3_prepare_v2(
   3225   sqlite3 *db,            /* Database handle */
   3226   const char *zSql,       /* SQL statement, UTF-8 encoded */
   3227   int nByte,              /* Maximum length of zSql in bytes. */
   3228   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   3229   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
   3230 );
   3231 SQLITE_API int sqlite3_prepare16(
   3232   sqlite3 *db,            /* Database handle */
   3233   const void *zSql,       /* SQL statement, UTF-16 encoded */
   3234   int nByte,              /* Maximum length of zSql in bytes. */
   3235   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   3236   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
   3237 );
   3238 SQLITE_API int sqlite3_prepare16_v2(
   3239   sqlite3 *db,            /* Database handle */
   3240   const void *zSql,       /* SQL statement, UTF-16 encoded */
   3241   int nByte,              /* Maximum length of zSql in bytes. */
   3242   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   3243   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
   3244 );
   3245 
   3246 /*
   3247 ** CAPI3REF: Retrieving Statement SQL
   3248 **
   3249 ** ^This interface can be used to retrieve a saved copy of the original
   3250 ** SQL text used to create a [prepared statement] if that statement was
   3251 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
   3252 */
   3253 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
   3254 
   3255 /*
   3256 ** CAPI3REF: Determine If An SQL Statement Writes The Database
   3257 **
   3258 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
   3259 ** and only if the [prepared statement] X makes no direct changes to
   3260 ** the content of the database file.
   3261 **
   3262 ** Note that [application-defined SQL functions] or
   3263 ** [virtual tables] might change the database indirectly as a side effect.
   3264 ** ^(For example, if an application defines a function "eval()" that
   3265 ** calls [sqlite3_exec()], then the following SQL statement would
   3266 ** change the database file through side-effects:
   3267 **
   3268 ** <blockquote><pre>
   3269 **    SELECT eval('DELETE FROM t1') FROM t2;
   3270 ** </pre></blockquote>
   3271 **
   3272 ** But because the [SELECT] statement does not change the database file
   3273 ** directly, sqlite3_stmt_readonly() would still return true.)^
   3274 **
   3275 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
   3276 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
   3277 ** since the statements themselves do not actually modify the database but
   3278 ** rather they control the timing of when other statements modify the
   3279 ** database.  ^The [ATTACH] and [DETACH] statements also cause
   3280 ** sqlite3_stmt_readonly() to return true since, while those statements
   3281 ** change the configuration of a database connection, they do not make
   3282 ** changes to the content of the database files on disk.
   3283 */
   3284 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
   3285 
   3286 /*
   3287 ** CAPI3REF: Dynamically Typed Value Object
   3288 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
   3289 **
   3290 ** SQLite uses the sqlite3_value object to represent all values
   3291 ** that can be stored in a database table. SQLite uses dynamic typing
   3292 ** for the values it stores.  ^Values stored in sqlite3_value objects
   3293 ** can be integers, floating point values, strings, BLOBs, or NULL.
   3294 **
   3295 ** An sqlite3_value object may be either "protected" or "unprotected".
   3296 ** Some interfaces require a protected sqlite3_value.  Other interfaces
   3297 ** will accept either a protected or an unprotected sqlite3_value.
   3298 ** Every interface that accepts sqlite3_value arguments specifies
   3299 ** whether or not it requires a protected sqlite3_value.
   3300 **
   3301 ** The terms "protected" and "unprotected" refer to whether or not
   3302 ** a mutex is held.  An internal mutex is held for a protected
   3303 ** sqlite3_value object but no mutex is held for an unprotected
   3304 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
   3305 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
   3306 ** or if SQLite is run in one of reduced mutex modes
   3307 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
   3308 ** then there is no distinction between protected and unprotected
   3309 ** sqlite3_value objects and they can be used interchangeably.  However,
   3310 ** for maximum code portability it is recommended that applications
   3311 ** still make the distinction between protected and unprotected
   3312 ** sqlite3_value objects even when not strictly required.
   3313 **
   3314 ** ^The sqlite3_value objects that are passed as parameters into the
   3315 ** implementation of [application-defined SQL functions] are protected.
   3316 ** ^The sqlite3_value object returned by
   3317 ** [sqlite3_column_value()] is unprotected.
   3318 ** Unprotected sqlite3_value objects may only be used with
   3319 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
   3320 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
   3321 ** interfaces require protected sqlite3_value objects.
   3322 */
   3323 typedef struct Mem sqlite3_value;
   3324 
   3325 /*
   3326 ** CAPI3REF: SQL Function Context Object
   3327 **
   3328 ** The context in which an SQL function executes is stored in an
   3329 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
   3330 ** is always first parameter to [application-defined SQL functions].
   3331 ** The application-defined SQL function implementation will pass this
   3332 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
   3333 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
   3334 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
   3335 ** and/or [sqlite3_set_auxdata()].
   3336 */
   3337 typedef struct sqlite3_context sqlite3_context;
   3338 
   3339 /*
   3340 ** CAPI3REF: Binding Values To Prepared Statements
   3341 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
   3342 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
   3343 **
   3344 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
   3345 ** literals may be replaced by a [parameter] that matches one of following
   3346 ** templates:
   3347 **
   3348 ** <ul>
   3349 ** <li>  ?
   3350 ** <li>  ?NNN
   3351 ** <li>  :VVV
   3352 ** <li>  @VVV
   3353 ** <li>  $VVV
   3354 ** </ul>
   3355 **
   3356 ** In the templates above, NNN represents an integer literal,
   3357 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
   3358 ** parameters (also called "host parameter names" or "SQL parameters")
   3359 ** can be set using the sqlite3_bind_*() routines defined here.
   3360 **
   3361 ** ^The first argument to the sqlite3_bind_*() routines is always
   3362 ** a pointer to the [sqlite3_stmt] object returned from
   3363 ** [sqlite3_prepare_v2()] or its variants.
   3364 **
   3365 ** ^The second argument is the index of the SQL parameter to be set.
   3366 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
   3367 ** SQL parameter is used more than once, second and subsequent
   3368 ** occurrences have the same index as the first occurrence.
   3369 ** ^The index for named parameters can be looked up using the
   3370 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
   3371 ** for "?NNN" parameters is the value of NNN.
   3372 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
   3373 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
   3374 **
   3375 ** ^The third argument is the value to bind to the parameter.
   3376 **
   3377 ** ^(In those routines that have a fourth argument, its value is the
   3378 ** number of bytes in the parameter.  To be clear: the value is the
   3379 ** number of <u>bytes</u> in the value, not the number of characters.)^
   3380 ** ^If the fourth parameter is negative, the length of the string is
   3381 ** the number of bytes up to the first zero terminator.
   3382 **
   3383 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
   3384 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
   3385 ** string after SQLite has finished with it.  ^The destructor is called
   3386 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
   3387 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
   3388 ** ^If the fifth argument is
   3389 ** the special value [SQLITE_STATIC], then SQLite assumes that the
   3390 ** information is in static, unmanaged space and does not need to be freed.
   3391 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
   3392 ** SQLite makes its own private copy of the data immediately, before
   3393 ** the sqlite3_bind_*() routine returns.
   3394 **
   3395 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
   3396 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
   3397 ** (just an integer to hold its size) while it is being processed.
   3398 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
   3399 ** content is later written using
   3400 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
   3401 ** ^A negative value for the zeroblob results in a zero-length BLOB.
   3402 **
   3403 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
   3404 ** for the [prepared statement] or with a prepared statement for which
   3405 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
   3406 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
   3407 ** routine is passed a [prepared statement] that has been finalized, the
   3408 ** result is undefined and probably harmful.
   3409 **
   3410 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
   3411 ** ^Unbound parameters are interpreted as NULL.
   3412 **
   3413 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
   3414 ** [error code] if anything goes wrong.
   3415 ** ^[SQLITE_RANGE] is returned if the parameter
   3416 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
   3417 **
   3418 ** See also: [sqlite3_bind_parameter_count()],
   3419 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
   3420 */
   3421 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
   3422 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
   3423 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
   3424 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
   3425 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
   3426 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
   3427 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
   3428 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
   3429 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
   3430 
   3431 /*
   3432 ** CAPI3REF: Number Of SQL Parameters
   3433 **
   3434 ** ^This routine can be used to find the number of [SQL parameters]
   3435 ** in a [prepared statement].  SQL parameters are tokens of the
   3436 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
   3437 ** placeholders for values that are [sqlite3_bind_blob | bound]
   3438 ** to the parameters at a later time.
   3439 **
   3440 ** ^(This routine actually returns the index of the largest (rightmost)
   3441 ** parameter. For all forms except ?NNN, this will correspond to the
   3442 ** number of unique parameters.  If parameters of the ?NNN form are used,
   3443 ** there may be gaps in the list.)^
   3444 **
   3445 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
   3446 ** [sqlite3_bind_parameter_name()], and
   3447 ** [sqlite3_bind_parameter_index()].
   3448 */
   3449 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
   3450 
   3451 /*
   3452 ** CAPI3REF: Name Of A Host Parameter
   3453 **
   3454 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
   3455 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
   3456 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
   3457 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
   3458 ** respectively.
   3459 ** In other words, the initial ":" or "$" or "@" or "?"
   3460 ** is included as part of the name.)^
   3461 ** ^Parameters of the form "?" without a following integer have no name
   3462 ** and are referred to as "nameless" or "anonymous parameters".
   3463 **
   3464 ** ^The first host parameter has an index of 1, not 0.
   3465 **
   3466 ** ^If the value N is out of range or if the N-th parameter is
   3467 ** nameless, then NULL is returned.  ^The returned string is
   3468 ** always in UTF-8 encoding even if the named parameter was
   3469 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
   3470 ** [sqlite3_prepare16_v2()].
   3471 **
   3472 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
   3473 ** [sqlite3_bind_parameter_count()], and
   3474 ** [sqlite3_bind_parameter_index()].
   3475 */
   3476 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
   3477 
   3478 /*
   3479 ** CAPI3REF: Index Of A Parameter With A Given Name
   3480 **
   3481 ** ^Return the index of an SQL parameter given its name.  ^The
   3482 ** index value returned is suitable for use as the second
   3483 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
   3484 ** is returned if no matching parameter is found.  ^The parameter
   3485 ** name must be given in UTF-8 even if the original statement
   3486 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
   3487 **
   3488 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
   3489 ** [sqlite3_bind_parameter_count()], and
   3490 ** [sqlite3_bind_parameter_index()].
   3491 */
   3492 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
   3493 
   3494 /*
   3495 ** CAPI3REF: Reset All Bindings On A Prepared Statement
   3496 **
   3497 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
   3498 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
   3499 ** ^Use this routine to reset all host parameters to NULL.
   3500 */
   3501 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
   3502 
   3503 /*
   3504 ** CAPI3REF: Number Of Columns In A Result Set
   3505 **
   3506 ** ^Return the number of columns in the result set returned by the
   3507 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
   3508 ** statement that does not return data (for example an [UPDATE]).
   3509 **
   3510 ** See also: [sqlite3_data_count()]
   3511 */
   3512 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
   3513 
   3514 /*
   3515 ** CAPI3REF: Column Names In A Result Set
   3516 **
   3517 ** ^These routines return the name assigned to a particular column
   3518 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
   3519 ** interface returns a pointer to a zero-terminated UTF-8 string
   3520 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
   3521 ** UTF-16 string.  ^The first parameter is the [prepared statement]
   3522 ** that implements the [SELECT] statement. ^The second parameter is the
   3523 ** column number.  ^The leftmost column is number 0.
   3524 **
   3525 ** ^The returned string pointer is valid until either the [prepared statement]
   3526 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
   3527 ** reprepared by the first call to [sqlite3_step()] for a particular run
   3528 ** or until the next call to
   3529 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
   3530 **
   3531 ** ^If sqlite3_malloc() fails during the processing of either routine
   3532 ** (for example during a conversion from UTF-8 to UTF-16) then a
   3533 ** NULL pointer is returned.
   3534 **
   3535 ** ^The name of a result column is the value of the "AS" clause for
   3536 ** that column, if there is an AS clause.  If there is no AS clause
   3537 ** then the name of the column is unspecified and may change from
   3538 ** one release of SQLite to the next.
   3539 */
   3540 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
   3541 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
   3542 
   3543 /*
   3544 ** CAPI3REF: Source Of Data In A Query Result
   3545 **
   3546 ** ^These routines provide a means to determine the database, table, and
   3547 ** table column that is the origin of a particular result column in
   3548 ** [SELECT] statement.
   3549 ** ^The name of the database or table or column can be returned as
   3550 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
   3551 ** the database name, the _table_ routines return the table name, and
   3552 ** the origin_ routines return the column name.
   3553 ** ^The returned string is valid until the [prepared statement] is destroyed
   3554 ** using [sqlite3_finalize()] or until the statement is automatically
   3555 ** reprepared by the first call to [sqlite3_step()] for a particular run
   3556 ** or until the same information is requested
   3557 ** again in a different encoding.
   3558 **
   3559 ** ^The names returned are the original un-aliased names of the
   3560 ** database, table, and column.
   3561 **
   3562 ** ^The first argument to these interfaces is a [prepared statement].
   3563 ** ^These functions return information about the Nth result column returned by
   3564 ** the statement, where N is the second function argument.
   3565 ** ^The left-most column is column 0 for these routines.
   3566 **
   3567 ** ^If the Nth column returned by the statement is an expression or
   3568 ** subquery and is not a column value, then all of these functions return
   3569 ** NULL.  ^These routine might also return NULL if a memory allocation error
   3570 ** occurs.  ^Otherwise, they return the name of the attached database, table,
   3571 ** or column that query result column was extracted from.
   3572 **
   3573 ** ^As with all other SQLite APIs, those whose names end with "16" return
   3574 ** UTF-16 encoded strings and the other functions return UTF-8.
   3575 **
   3576 ** ^These APIs are only available if the library was compiled with the
   3577 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
   3578 **
   3579 ** If two or more threads call one or more of these routines against the same
   3580 ** prepared statement and column at the same time then the results are
   3581 ** undefined.
   3582 **
   3583 ** If two or more threads call one or more
   3584 ** [sqlite3_column_database_name | column metadata interfaces]
   3585 ** for the same [prepared statement] and result column
   3586 ** at the same time then the results are undefined.
   3587 */
   3588 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
   3589 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
   3590 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
   3591 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
   3592 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
   3593 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
   3594 
   3595 /*
   3596 ** CAPI3REF: Declared Datatype Of A Query Result
   3597 **
   3598 ** ^(The first parameter is a [prepared statement].
   3599 ** If this statement is a [SELECT] statement and the Nth column of the
   3600 ** returned result set of that [SELECT] is a table column (not an
   3601 ** expression or subquery) then the declared type of the table
   3602 ** column is returned.)^  ^If the Nth column of the result set is an
   3603 ** expression or subquery, then a NULL pointer is returned.
   3604 ** ^The returned string is always UTF-8 encoded.
   3605 **
   3606 ** ^(For example, given the database schema:
   3607 **
   3608 ** CREATE TABLE t1(c1 VARIANT);
   3609 **
   3610 ** and the following statement to be compiled:
   3611 **
   3612 ** SELECT c1 + 1, c1 FROM t1;
   3613 **
   3614 ** this routine would return the string "VARIANT" for the second result
   3615 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
   3616 **
   3617 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
   3618 ** is declared to contain a particular type does not mean that the
   3619 ** data stored in that column is of the declared type.  SQLite is
   3620 ** strongly typed, but the typing is dynamic not static.  ^Type
   3621 ** is associated with individual values, not with the containers
   3622 ** used to hold those values.
   3623 */
   3624 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
   3625 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
   3626 
   3627 /*
   3628 ** CAPI3REF: Evaluate An SQL Statement
   3629 **
   3630 ** After a [prepared statement] has been prepared using either
   3631 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
   3632 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
   3633 ** must be called one or more times to evaluate the statement.
   3634 **
   3635 ** The details of the behavior of the sqlite3_step() interface depend
   3636 ** on whether the statement was prepared using the newer "v2" interface
   3637 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
   3638 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
   3639 ** new "v2" interface is recommended for new applications but the legacy
   3640 ** interface will continue to be supported.
   3641 **
   3642 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
   3643 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
   3644 ** ^With the "v2" interface, any of the other [result codes] or
   3645 ** [extended result codes] might be returned as well.
   3646 **
   3647 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
   3648 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
   3649 ** or occurs outside of an explicit transaction, then you can retry the
   3650 ** statement.  If the statement is not a [COMMIT] and occurs within a
   3651 ** explicit transaction then you should rollback the transaction before
   3652 ** continuing.
   3653 **
   3654 ** ^[SQLITE_DONE] means that the statement has finished executing
   3655 ** successfully.  sqlite3_step() should not be called again on this virtual
   3656 ** machine without first calling [sqlite3_reset()] to reset the virtual
   3657 ** machine back to its initial state.
   3658 **
   3659 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
   3660 ** is returned each time a new row of data is ready for processing by the
   3661 ** caller. The values may be accessed using the [column access functions].
   3662 ** sqlite3_step() is called again to retrieve the next row of data.
   3663 **
   3664 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
   3665 ** violation) has occurred.  sqlite3_step() should not be called again on
   3666 ** the VM. More information may be found by calling [sqlite3_errmsg()].
   3667 ** ^With the legacy interface, a more specific error code (for example,
   3668 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
   3669 ** can be obtained by calling [sqlite3_reset()] on the
   3670 ** [prepared statement].  ^In the "v2" interface,
   3671 ** the more specific error code is returned directly by sqlite3_step().
   3672 **
   3673 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
   3674 ** Perhaps it was called on a [prepared statement] that has
   3675 ** already been [sqlite3_finalize | finalized] or on one that had
   3676 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
   3677 ** be the case that the same database connection is being used by two or
   3678 ** more threads at the same moment in time.
   3679 **
   3680 ** For all versions of SQLite up to and including 3.6.23.1, a call to
   3681 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
   3682 ** other than [SQLITE_ROW] before any subsequent invocation of
   3683 ** sqlite3_step().  Failure to reset the prepared statement using
   3684 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
   3685 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
   3686 ** calling [sqlite3_reset()] automatically in this circumstance rather
   3687 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
   3688 ** break because any application that ever receives an SQLITE_MISUSE error
   3689 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
   3690 ** can be used to restore the legacy behavior.
   3691 **
   3692 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
   3693 ** API always returns a generic error code, [SQLITE_ERROR], following any
   3694 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
   3695 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
   3696 ** specific [error codes] that better describes the error.
   3697 ** We admit that this is a goofy design.  The problem has been fixed
   3698 ** with the "v2" interface.  If you prepare all of your SQL statements
   3699 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
   3700 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
   3701 ** then the more specific [error codes] are returned directly
   3702 ** by sqlite3_step().  The use of the "v2" interface is recommended.
   3703 */
   3704 SQLITE_API int sqlite3_step(sqlite3_stmt*);
   3705 
   3706 /*
   3707 ** CAPI3REF: Number of columns in a result set
   3708 **
   3709 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
   3710 ** current row of the result set of [prepared statement] P.
   3711 ** ^If prepared statement P does not have results ready to return
   3712 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
   3713 ** interfaces) then sqlite3_data_count(P) returns 0.
   3714 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
   3715 **
   3716 ** See also: [sqlite3_column_count()]
   3717 */
   3718 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
   3719 
   3720 /*
   3721 ** CAPI3REF: Fundamental Datatypes
   3722 ** KEYWORDS: SQLITE_TEXT
   3723 **
   3724 ** ^(Every value in SQLite has one of five fundamental datatypes:
   3725 **
   3726 ** <ul>
   3727 ** <li> 64-bit signed integer
   3728 ** <li> 64-bit IEEE floating point number
   3729 ** <li> string
   3730 ** <li> BLOB
   3731 ** <li> NULL
   3732 ** </ul>)^
   3733 **
   3734 ** These constants are codes for each of those types.
   3735 **
   3736 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
   3737 ** for a completely different meaning.  Software that links against both
   3738 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
   3739 ** SQLITE_TEXT.
   3740 */
   3741 #define SQLITE_INTEGER  1
   3742 #define SQLITE_FLOAT    2
   3743 #define SQLITE_BLOB     4
   3744 #define SQLITE_NULL     5
   3745 #ifdef SQLITE_TEXT
   3746 # undef SQLITE_TEXT
   3747 #else
   3748 # define SQLITE_TEXT     3
   3749 #endif
   3750 #define SQLITE3_TEXT     3
   3751 
   3752 /*
   3753 ** CAPI3REF: Result Values From A Query
   3754 ** KEYWORDS: {column access functions}
   3755 **
   3756 ** These routines form the "result set" interface.
   3757 **
   3758 ** ^These routines return information about a single column of the current
   3759 ** result row of a query.  ^In every case the first argument is a pointer
   3760 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
   3761 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
   3762 ** and the second argument is the index of the column for which information
   3763 ** should be returned. ^The leftmost column of the result set has the index 0.
   3764 ** ^The number of columns in the result can be determined using
   3765 ** [sqlite3_column_count()].
   3766 **
   3767 ** If the SQL statement does not currently point to a valid row, or if the
   3768 ** column index is out of range, the result is undefined.
   3769 ** These routines may only be called when the most recent call to
   3770 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
   3771 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
   3772 ** If any of these routines are called after [sqlite3_reset()] or
   3773 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
   3774 ** something other than [SQLITE_ROW], the results are undefined.
   3775 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
   3776 ** are called from a different thread while any of these routines
   3777 ** are pending, then the results are undefined.
   3778 **
   3779 ** ^The sqlite3_column_type() routine returns the
   3780 ** [SQLITE_INTEGER | datatype code] for the initial data type
   3781 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
   3782 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
   3783 ** returned by sqlite3_column_type() is only meaningful if no type
   3784 ** conversions have occurred as described below.  After a type conversion,
   3785 ** the value returned by sqlite3_column_type() is undefined.  Future
   3786 ** versions of SQLite may change the behavior of sqlite3_column_type()
   3787 ** following a type conversion.
   3788 **
   3789 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
   3790 ** routine returns the number of bytes in that BLOB or string.
   3791 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
   3792 ** the string to UTF-8 and then returns the number of bytes.
   3793 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
   3794 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
   3795 ** the number of bytes in that string.
   3796 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
   3797 **
   3798 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
   3799 ** routine returns the number of bytes in that BLOB or string.
   3800 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
   3801 ** the string to UTF-16 and then returns the number of bytes.
   3802 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
   3803 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
   3804 ** the number of bytes in that string.
   3805 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
   3806 **
   3807 ** ^The values returned by [sqlite3_column_bytes()] and
   3808 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
   3809 ** of the string.  ^For clarity: the values returned by
   3810 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
   3811 ** bytes in the string, not the number of characters.
   3812 **
   3813 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
   3814 ** even empty strings, are always zero terminated.  ^The return
   3815 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
   3816 **
   3817 ** ^The object returned by [sqlite3_column_value()] is an
   3818 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
   3819 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
   3820 ** If the [unprotected sqlite3_value] object returned by
   3821 ** [sqlite3_column_value()] is used in any other way, including calls
   3822 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
   3823 ** or [sqlite3_value_bytes()], then the behavior is undefined.
   3824 **
   3825 ** These routines attempt to convert the value where appropriate.  ^For
   3826 ** example, if the internal representation is FLOAT and a text result
   3827 ** is requested, [sqlite3_snprintf()] is used internally to perform the
   3828 ** conversion automatically.  ^(The following table details the conversions
   3829 ** that are applied:
   3830 **
   3831 ** <blockquote>
   3832 ** <table border="1">
   3833 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
   3834 **
   3835 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
   3836 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
   3837 ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
   3838 ** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
   3839 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
   3840 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
   3841 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
   3842 ** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
   3843 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
   3844 ** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
   3845 ** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
   3846 ** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
   3847 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
   3848 ** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
   3849 ** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
   3850 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
   3851 ** </table>
   3852 ** </blockquote>)^
   3853 **
   3854 ** The table above makes reference to standard C library functions atoi()
   3855 ** and atof().  SQLite does not really use these functions.  It has its
   3856 ** own equivalent internal routines.  The atoi() and atof() names are
   3857 ** used in the table for brevity and because they are familiar to most
   3858 ** C programmers.
   3859 **
   3860 ** Note that when type conversions occur, pointers returned by prior
   3861 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
   3862 ** sqlite3_column_text16() may be invalidated.
   3863 ** Type conversions and pointer invalidations might occur
   3864 ** in the following cases:
   3865 **
   3866 ** <ul>
   3867 ** <li> The initial content is a BLOB and sqlite3_column_text() or
   3868 **      sqlite3_column_text16() is called.  A zero-terminator might
   3869 **      need to be added to the string.</li>
   3870 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
   3871 **      sqlite3_column_text16() is called.  The content must be converted
   3872 **      to UTF-16.</li>
   3873 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
   3874 **      sqlite3_column_text() is called.  The content must be converted
   3875 **      to UTF-8.</li>
   3876 ** </ul>
   3877 **
   3878 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
   3879 ** not invalidate a prior pointer, though of course the content of the buffer
   3880 ** that the prior pointer references will have been modified.  Other kinds
   3881 ** of conversion are done in place when it is possible, but sometimes they
   3882 ** are not possible and in those cases prior pointers are invalidated.
   3883 **
   3884 ** The safest and easiest to remember policy is to invoke these routines
   3885 ** in one of the following ways:
   3886 **
   3887 ** <ul>
   3888 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
   3889 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
   3890 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
   3891 ** </ul>
   3892 **
   3893 ** In other words, you should call sqlite3_column_text(),
   3894 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
   3895 ** into the desired format, then invoke sqlite3_column_bytes() or
   3896 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
   3897 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
   3898 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
   3899 ** with calls to sqlite3_column_bytes().
   3900 **
   3901 ** ^The pointers returned are valid until a type conversion occurs as
   3902 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
   3903 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
   3904 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
   3905 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
   3906 ** [sqlite3_free()].
   3907 **
   3908 ** ^(If a memory allocation error occurs during the evaluation of any
   3909 ** of these routines, a default value is returned.  The default value
   3910 ** is either the integer 0, the floating point number 0.0, or a NULL
   3911 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
   3912 ** [SQLITE_NOMEM].)^
   3913 */
   3914 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
   3915 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
   3916 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
   3917 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
   3918 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
   3919 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
   3920 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
   3921 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
   3922 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
   3923 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
   3924 
   3925 /*
   3926 ** CAPI3REF: Destroy A Prepared Statement Object
   3927 **
   3928 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
   3929 ** ^If the most recent evaluation of the statement encountered no errors or
   3930 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
   3931 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
   3932 ** sqlite3_finalize(S) returns the appropriate [error code] or
   3933 ** [extended error code].
   3934 **
   3935 ** ^The sqlite3_finalize(S) routine can be called at any point during
   3936 ** the life cycle of [prepared statement] S:
   3937 ** before statement S is ever evaluated, after
   3938 ** one or more calls to [sqlite3_reset()], or after any call
   3939 ** to [sqlite3_step()] regardless of whether or not the statement has
   3940 ** completed execution.
   3941 **
   3942 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
   3943 **
   3944 ** The application must finalize every [prepared statement] in order to avoid
   3945 ** resource leaks.  It is a grievous error for the application to try to use
   3946 ** a prepared statement after it has been finalized.  Any use of a prepared
   3947 ** statement after it has been finalized can result in undefined and
   3948 ** undesirable behavior such as segfaults and heap corruption.
   3949 */
   3950 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
   3951 
   3952 /*
   3953 ** CAPI3REF: Reset A Prepared Statement Object
   3954 **
   3955 ** The sqlite3_reset() function is called to reset a [prepared statement]
   3956 ** object back to its initial state, ready to be re-executed.
   3957 ** ^Any SQL statement variables that had values bound to them using
   3958 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
   3959 ** Use [sqlite3_clear_bindings()] to reset the bindings.
   3960 **
   3961 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
   3962 ** back to the beginning of its program.
   3963 **
   3964 ** ^If the most recent call to [sqlite3_step(S)] for the
   3965 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
   3966 ** or if [sqlite3_step(S)] has never before been called on S,
   3967 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
   3968 **
   3969 ** ^If the most recent call to [sqlite3_step(S)] for the
   3970 ** [prepared statement] S indicated an error, then
   3971 ** [sqlite3_reset(S)] returns an appropriate [error code].
   3972 **
   3973 ** ^The [sqlite3_reset(S)] interface does not change the values
   3974 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
   3975 */
   3976 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
   3977 
   3978 /*
   3979 ** CAPI3REF: Create Or Redefine SQL Functions
   3980 ** KEYWORDS: {function creation routines}
   3981 ** KEYWORDS: {application-defined SQL function}
   3982 ** KEYWORDS: {application-defined SQL functions}
   3983 **
   3984 ** ^These functions (collectively known as "function creation routines")
   3985 ** are used to add SQL functions or aggregates or to redefine the behavior
   3986 ** of existing SQL functions or aggregates.  The only differences between
   3987 ** these routines are the text encoding expected for
   3988 ** the second parameter (the name of the function being created)
   3989 ** and the presence or absence of a destructor callback for
   3990 ** the application data pointer.
   3991 **
   3992 ** ^The first parameter is the [database connection] to which the SQL
   3993 ** function is to be added.  ^If an application uses more than one database
   3994 ** connection then application-defined SQL functions must be added
   3995 ** to each database connection separately.
   3996 **
   3997 ** ^The second parameter is the name of the SQL function to be created or
   3998 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
   3999 ** representation, exclusive of the zero-terminator.  ^Note that the name
   4000 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
   4001 ** ^Any attempt to create a function with a longer name
   4002 ** will result in [SQLITE_MISUSE] being returned.
   4003 **
   4004 ** ^The third parameter (nArg)
   4005 ** is the number of arguments that the SQL function or
   4006 ** aggregate takes. ^If this parameter is -1, then the SQL function or
   4007 ** aggregate may take any number of arguments between 0 and the limit
   4008 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
   4009 ** parameter is less than -1 or greater than 127 then the behavior is
   4010 ** undefined.
   4011 **
   4012 ** ^The fourth parameter, eTextRep, specifies what
   4013 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
   4014 ** its parameters.  Every SQL function implementation must be able to work
   4015 ** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
   4016 ** more efficient with one encoding than another.  ^An application may
   4017 ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
   4018 ** times with the same function but with different values of eTextRep.
   4019 ** ^When multiple implementations of the same function are available, SQLite
   4020 ** will pick the one that involves the least amount of data conversion.
   4021 ** If there is only a single implementation which does not care what text
   4022 ** encoding is used, then the fourth argument should be [SQLITE_ANY].
   4023 **
   4024 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
   4025 ** function can gain access to this pointer using [sqlite3_user_data()].)^
   4026 **
   4027 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
   4028 ** pointers to C-language functions that implement the SQL function or
   4029 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
   4030 ** callback only; NULL pointers must be passed as the xStep and xFinal
   4031 ** parameters. ^An aggregate SQL function requires an implementation of xStep
   4032 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
   4033 ** SQL function or aggregate, pass NULL pointers for all three function
   4034 ** callbacks.
   4035 **
   4036 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
   4037 ** then it is destructor for the application data pointer.
   4038 ** The destructor is invoked when the function is deleted, either by being
   4039 ** overloaded or when the database connection closes.)^
   4040 ** ^The destructor is also invoked if the call to
   4041 ** sqlite3_create_function_v2() fails.
   4042 ** ^When the destructor callback of the tenth parameter is invoked, it
   4043 ** is passed a single argument which is a copy of the application data
   4044 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
   4045 **
   4046 ** ^It is permitted to register multiple implementations of the same
   4047 ** functions with the same name but with either differing numbers of
   4048 ** arguments or differing preferred text encodings.  ^SQLite will use
   4049 ** the implementation that most closely matches the way in which the
   4050 ** SQL function is used.  ^A function implementation with a non-negative
   4051 ** nArg parameter is a better match than a function implementation with
   4052 ** a negative nArg.  ^A function where the preferred text encoding
   4053 ** matches the database encoding is a better
   4054 ** match than a function where the encoding is different.
   4055 ** ^A function where the encoding difference is between UTF16le and UTF16be
   4056 ** is a closer match than a function where the encoding difference is
   4057 ** between UTF8 and UTF16.
   4058 **
   4059 ** ^Built-in functions may be overloaded by new application-defined functions.
   4060 **
   4061 ** ^An application-defined function is permitted to call other
   4062 ** SQLite interfaces.  However, such calls must not
   4063 ** close the database connection nor finalize or reset the prepared
   4064 ** statement in which the function is running.
   4065 */
   4066 SQLITE_API int sqlite3_create_function(
   4067   sqlite3 *db,
   4068   const char *zFunctionName,
   4069   int nArg,
   4070   int eTextRep,
   4071   void *pApp,
   4072   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   4073   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   4074   void (*xFinal)(sqlite3_context*)
   4075 );
   4076 SQLITE_API int sqlite3_create_function16(
   4077   sqlite3 *db,
   4078   const void *zFunctionName,
   4079   int nArg,
   4080   int eTextRep,
   4081   void *pApp,
   4082   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   4083   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   4084   void (*xFinal)(sqlite3_context*)
   4085 );
   4086 SQLITE_API int sqlite3_create_function_v2(
   4087   sqlite3 *db,
   4088   const char *zFunctionName,
   4089   int nArg,
   4090   int eTextRep,
   4091   void *pApp,
   4092   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   4093   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   4094   void (*xFinal)(sqlite3_context*),
   4095   void(*xDestroy)(void*)
   4096 );
   4097 
   4098 /*
   4099 ** CAPI3REF: Text Encodings
   4100 **
   4101 ** These constant define integer codes that represent the various
   4102 ** text encodings supported by SQLite.
   4103 */
   4104 #define SQLITE_UTF8           1
   4105 #define SQLITE_UTF16LE        2
   4106 #define SQLITE_UTF16BE        3
   4107 #define SQLITE_UTF16          4    /* Use native byte order */
   4108 #define SQLITE_ANY            5    /* sqlite3_create_function only */
   4109 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
   4110 
   4111 /*
   4112 ** CAPI3REF: Deprecated Functions
   4113 ** DEPRECATED
   4114 **
   4115 ** These functions are [deprecated].  In order to maintain
   4116 ** backwards compatibility with older code, these functions continue
   4117 ** to be supported.  However, new applications should avoid
   4118 ** the use of these functions.  To help encourage people to avoid
   4119 ** using these functions, we are not going to tell you what they do.
   4120 */
   4121 #ifndef SQLITE_OMIT_DEPRECATED
   4122 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
   4123 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
   4124 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
   4125 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
   4126 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
   4127 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
   4128 #endif
   4129 
   4130 /*
   4131 ** CAPI3REF: Obtaining SQL Function Parameter Values
   4132 **
   4133 ** The C-language implementation of SQL functions and aggregates uses
   4134 ** this set of interface routines to access the parameter values on
   4135 ** the function or aggregate.
   4136 **
   4137 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
   4138 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
   4139 ** define callbacks that implement the SQL functions and aggregates.
   4140 ** The 3rd parameter to these callbacks is an array of pointers to
   4141 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
   4142 ** each parameter to the SQL function.  These routines are used to
   4143 ** extract values from the [sqlite3_value] objects.
   4144 **
   4145 ** These routines work only with [protected sqlite3_value] objects.
   4146 ** Any attempt to use these routines on an [unprotected sqlite3_value]
   4147 ** object results in undefined behavior.
   4148 **
   4149 ** ^These routines work just like the corresponding [column access functions]
   4150 ** except that  these routines take a single [protected sqlite3_value] object
   4151 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
   4152 **
   4153 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
   4154 ** in the native byte-order of the host machine.  ^The
   4155 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
   4156 ** extract UTF-16 strings as big-endian and little-endian respectively.
   4157 **
   4158 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
   4159 ** numeric affinity to the value.  This means that an attempt is
   4160 ** made to convert the value to an integer or floating point.  If
   4161 ** such a conversion is possible without loss of information (in other
   4162 ** words, if the value is a string that looks like a number)
   4163 ** then the conversion is performed.  Otherwise no conversion occurs.
   4164 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
   4165 **
   4166 ** Please pay particular attention to the fact that the pointer returned
   4167 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
   4168 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
   4169 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
   4170 ** or [sqlite3_value_text16()].
   4171 **
   4172 ** These routines must be called from the same thread as
   4173 ** the SQL function that supplied the [sqlite3_value*] parameters.
   4174 */
   4175 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
   4176 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
   4177 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
   4178 SQLITE_API double sqlite3_value_double(sqlite3_value*);
   4179 SQLITE_API int sqlite3_value_int(sqlite3_value*);
   4180 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
   4181 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
   4182 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
   4183 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
   4184 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
   4185 SQLITE_API int sqlite3_value_type(sqlite3_value*);
   4186 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
   4187 
   4188 /*
   4189 ** CAPI3REF: Obtain Aggregate Function Context
   4190 **
   4191 ** Implementations of aggregate SQL functions use this
   4192 ** routine to allocate memory for storing their state.
   4193 **
   4194 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
   4195 ** for a particular aggregate function, SQLite
   4196 ** allocates N of memory, zeroes out that memory, and returns a pointer
   4197 ** to the new memory. ^On second and subsequent calls to
   4198 ** sqlite3_aggregate_context() for the same aggregate function instance,
   4199 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
   4200 ** called once for each invocation of the xStep callback and then one
   4201 ** last time when the xFinal callback is invoked.  ^(When no rows match
   4202 ** an aggregate query, the xStep() callback of the aggregate function
   4203 ** implementation is never called and xFinal() is called exactly once.
   4204 ** In those cases, sqlite3_aggregate_context() might be called for the
   4205 ** first time from within xFinal().)^
   4206 **
   4207 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
   4208 ** less than or equal to zero or if a memory allocate error occurs.
   4209 **
   4210 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
   4211 ** determined by the N parameter on first successful call.  Changing the
   4212 ** value of N in subsequent call to sqlite3_aggregate_context() within
   4213 ** the same aggregate function instance will not resize the memory
   4214 ** allocation.)^
   4215 **
   4216 ** ^SQLite automatically frees the memory allocated by
   4217 ** sqlite3_aggregate_context() when the aggregate query concludes.
   4218 **
   4219 ** The first parameter must be a copy of the
   4220 ** [sqlite3_context | SQL function context] that is the first parameter
   4221 ** to the xStep or xFinal callback routine that implements the aggregate
   4222 ** function.
   4223 **
   4224 ** This routine must be called from the same thread in which
   4225 ** the aggregate SQL function is running.
   4226 */
   4227 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
   4228 
   4229 /*
   4230 ** CAPI3REF: User Data For Functions
   4231 **
   4232 ** ^The sqlite3_user_data() interface returns a copy of
   4233 ** the pointer that was the pUserData parameter (the 5th parameter)
   4234 ** of the [sqlite3_create_function()]
   4235 ** and [sqlite3_create_function16()] routines that originally
   4236 ** registered the application defined function.
   4237 **
   4238 ** This routine must be called from the same thread in which
   4239 ** the application-defined function is running.
   4240 */
   4241 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
   4242 
   4243 /*
   4244 ** CAPI3REF: Database Connection For Functions
   4245 **
   4246 ** ^The sqlite3_context_db_handle() interface returns a copy of
   4247 ** the pointer to the [database connection] (the 1st parameter)
   4248 ** of the [sqlite3_create_function()]
   4249 ** and [sqlite3_create_function16()] routines that originally
   4250 ** registered the application defined function.
   4251 */
   4252 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
   4253 
   4254 /*
   4255 ** CAPI3REF: Function Auxiliary Data
   4256 **
   4257 ** The following two functions may be used by scalar SQL functions to
   4258 ** associate metadata with argument values. If the same value is passed to
   4259 ** multiple invocations of the same SQL function during query execution, under
   4260 ** some circumstances the associated metadata may be preserved. This may
   4261 ** be used, for example, to add a regular-expression matching scalar
   4262 ** function. The compiled version of the regular expression is stored as
   4263 ** metadata associated with the SQL value passed as the regular expression
   4264 ** pattern.  The compiled regular expression can be reused on multiple
   4265 ** invocations of the same function so that the original pattern string
   4266 ** does not need to be recompiled on each invocation.
   4267 **
   4268 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
   4269 ** associated by the sqlite3_set_auxdata() function with the Nth argument
   4270 ** value to the application-defined function. ^If no metadata has been ever
   4271 ** been set for the Nth argument of the function, or if the corresponding
   4272 ** function parameter has changed since the meta-data was set,
   4273 ** then sqlite3_get_auxdata() returns a NULL pointer.
   4274 **
   4275 ** ^The sqlite3_set_auxdata() interface saves the metadata
   4276 ** pointed to by its 3rd parameter as the metadata for the N-th
   4277 ** argument of the application-defined function.  Subsequent
   4278 ** calls to sqlite3_get_auxdata() might return this data, if it has
   4279 ** not been destroyed.
   4280 ** ^If it is not NULL, SQLite will invoke the destructor
   4281 ** function given by the 4th parameter to sqlite3_set_auxdata() on
   4282 ** the metadata when the corresponding function parameter changes
   4283 ** or when the SQL statement completes, whichever comes first.
   4284 **
   4285 ** SQLite is free to call the destructor and drop metadata on any
   4286 ** parameter of any function at any time.  ^The only guarantee is that
   4287 ** the destructor will be called before the metadata is dropped.
   4288 **
   4289 ** ^(In practice, metadata is preserved between function calls for
   4290 ** expressions that are constant at compile time. This includes literal
   4291 ** values and [parameters].)^
   4292 **
   4293 ** These routines must be called from the same thread in which
   4294 ** the SQL function is running.
   4295 */
   4296 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
   4297 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
   4298 
   4299 
   4300 /*
   4301 ** CAPI3REF: Constants Defining Special Destructor Behavior
   4302 **
   4303 ** These are special values for the destructor that is passed in as the
   4304 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
   4305 ** argument is SQLITE_STATIC, it means that the content pointer is constant
   4306 ** and will never change.  It does not need to be destroyed.  ^The
   4307 ** SQLITE_TRANSIENT value means that the content will likely change in
   4308 ** the near future and that SQLite should make its own private copy of
   4309 ** the content before returning.
   4310 **
   4311 ** The typedef is necessary to work around problems in certain
   4312 ** C++ compilers.  See ticket #2191.
   4313 */
   4314 typedef void (*sqlite3_destructor_type)(void*);
   4315 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
   4316 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
   4317 
   4318 /*
   4319 ** CAPI3REF: Setting The Result Of An SQL Function
   4320 **
   4321 ** These routines are used by the xFunc or xFinal callbacks that
   4322 ** implement SQL functions and aggregates.  See
   4323 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
   4324 ** for additional information.
   4325 **
   4326 ** These functions work very much like the [parameter binding] family of
   4327 ** functions used to bind values to host parameters in prepared statements.
   4328 ** Refer to the [SQL parameter] documentation for additional information.
   4329 **
   4330 ** ^The sqlite3_result_blob() interface sets the result from
   4331 ** an application-defined function to be the BLOB whose content is pointed
   4332 ** to by the second parameter and which is N bytes long where N is the
   4333 ** third parameter.
   4334 **
   4335 ** ^The sqlite3_result_zeroblob() interfaces set the result of
   4336 ** the application-defined function to be a BLOB containing all zero
   4337 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
   4338 **
   4339 ** ^The sqlite3_result_double() interface sets the result from
   4340 ** an application-defined function to be a floating point value specified
   4341 ** by its 2nd argument.
   4342 **
   4343 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
   4344 ** cause the implemented SQL function to throw an exception.
   4345 ** ^SQLite uses the string pointed to by the
   4346 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
   4347 ** as the text of an error message.  ^SQLite interprets the error
   4348 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
   4349 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
   4350 ** byte order.  ^If the third parameter to sqlite3_result_error()
   4351 ** or sqlite3_result_error16() is negative then SQLite takes as the error
   4352 ** message all text up through the first zero character.
   4353 ** ^If the third parameter to sqlite3_result_error() or
   4354 ** sqlite3_result_error16() is non-negative then SQLite takes that many
   4355 ** bytes (not characters) from the 2nd parameter as the error message.
   4356 ** ^The sqlite3_result_error() and sqlite3_result_error16()
   4357 ** routines make a private copy of the error message text before
   4358 ** they return.  Hence, the calling function can deallocate or
   4359 ** modify the text after they return without harm.
   4360 ** ^The sqlite3_result_error_code() function changes the error code
   4361 ** returned by SQLite as a result of an error in a function.  ^By default,
   4362 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
   4363 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
   4364 **
   4365 ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
   4366 ** indicating that a string or BLOB is too long to represent.
   4367 **
   4368 ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
   4369 ** indicating that a memory allocation failed.
   4370 **
   4371 ** ^The sqlite3_result_int() interface sets the return value
   4372 ** of the application-defined function to be the 32-bit signed integer
   4373 ** value given in the 2nd argument.
   4374 ** ^The sqlite3_result_int64() interface sets the return value
   4375 ** of the application-defined function to be the 64-bit signed integer
   4376 ** value given in the 2nd argument.
   4377 **
   4378 ** ^The sqlite3_result_null() interface sets the return value
   4379 ** of the application-defined function to be NULL.
   4380 **
   4381 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
   4382 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
   4383 ** set the return value of the application-defined function to be
   4384 ** a text string which is represented as UTF-8, UTF-16 native byte order,
   4385 ** UTF-16 little endian, or UTF-16 big endian, respectively.
   4386 ** ^SQLite takes the text result from the application from
   4387 ** the 2nd parameter of the sqlite3_result_text* interfaces.
   4388 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
   4389 ** is negative, then SQLite takes result text from the 2nd parameter
   4390 ** through the first zero character.
   4391 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
   4392 ** is non-negative, then as many bytes (not characters) of the text
   4393 ** pointed to by the 2nd parameter are taken as the application-defined
   4394 ** function result.
   4395 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
   4396 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
   4397 ** function as the destructor on the text or BLOB result when it has
   4398 ** finished using that result.
   4399 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
   4400 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
   4401 ** assumes that the text or BLOB result is in constant space and does not
   4402 ** copy the content of the parameter nor call a destructor on the content
   4403 ** when it has finished using that result.
   4404 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
   4405 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
   4406 ** then SQLite makes a copy of the result into space obtained from
   4407 ** from [sqlite3_malloc()] before it returns.
   4408 **
   4409 ** ^The sqlite3_result_value() interface sets the result of
   4410 ** the application-defined function to be a copy the
   4411 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
   4412 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
   4413 ** so that the [sqlite3_value] specified in the parameter may change or
   4414 ** be deallocated after sqlite3_result_value() returns without harm.
   4415 ** ^A [protected sqlite3_value] object may always be used where an
   4416 ** [unprotected sqlite3_value] object is required, so either
   4417 ** kind of [sqlite3_value] object can be used with this interface.
   4418 **
   4419 ** If these routines are called from within the different thread
   4420 ** than the one containing the application-defined function that received
   4421 ** the [sqlite3_context] pointer, the results are undefined.
   4422 */
   4423 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
   4424 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
   4425 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
   4426 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
   4427 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
   4428 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
   4429 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
   4430 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
   4431 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
   4432 SQLITE_API void sqlite3_result_null(sqlite3_context*);
   4433 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
   4434 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
   4435 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
   4436 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
   4437 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
   4438 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
   4439 
   4440 /*
   4441 ** CAPI3REF: Define New Collating Sequences
   4442 **
   4443 ** ^These functions add, remove, or modify a [collation] associated
   4444 ** with the [database connection] specified as the first argument.
   4445 **
   4446 ** ^The name of the collation is a UTF-8 string
   4447 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
   4448 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
   4449 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
   4450 ** considered to be the same name.
   4451 **
   4452 ** ^(The third argument (eTextRep) must be one of the constants:
   4453 ** <ul>
   4454 ** <li> [SQLITE_UTF8],
   4455 ** <li> [SQLITE_UTF16LE],
   4456 ** <li> [SQLITE_UTF16BE],
   4457 ** <li> [SQLITE_UTF16], or
   4458 ** <li> [SQLITE_UTF16_ALIGNED].
   4459 ** </ul>)^
   4460 ** ^The eTextRep argument determines the encoding of strings passed
   4461 ** to the collating function callback, xCallback.
   4462 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
   4463 ** force strings to be UTF16 with native byte order.
   4464 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
   4465 ** on an even byte address.
   4466 **
   4467 ** ^The fourth argument, pArg, is an application data pointer that is passed
   4468 ** through as the first argument to the collating function callback.
   4469 **
   4470 ** ^The fifth argument, xCallback, is a pointer to the collating function.
   4471 ** ^Multiple collating functions can be registered using the same name but
   4472 ** with different eTextRep parameters and SQLite will use whichever
   4473 ** function requires the least amount of data transformation.
   4474 ** ^If the xCallback argument is NULL then the collating function is
   4475 ** deleted.  ^When all collating functions having the same name are deleted,
   4476 ** that collation is no longer usable.
   4477 **
   4478 ** ^The collating function callback is invoked with a copy of the pArg
   4479 ** application data pointer and with two strings in the encoding specified
   4480 ** by the eTextRep argument.  The collating function must return an
   4481 ** integer that is negative, zero, or positive
   4482 ** if the first string is less than, equal to, or greater than the second,
   4483 ** respectively.  A collating function must always return the same answer
   4484 ** given the same inputs.  If two or more collating functions are registered
   4485 ** to the same collation name (using different eTextRep values) then all
   4486 ** must give an equivalent answer when invoked with equivalent strings.
   4487 ** The collating function must obey the following properties for all
   4488 ** strings A, B, and C:
   4489 **
   4490 ** <ol>
   4491 ** <li> If A==B then B==A.
   4492 ** <li> If A==B and B==C then A==C.
   4493 ** <li> If A&lt;B THEN B&gt;A.
   4494 ** <li> If A&lt;B and B&lt;C then A&lt;C.
   4495 ** </ol>
   4496 **
   4497 ** If a collating function fails any of the above constraints and that
   4498 ** collating function is  registered and used, then the behavior of SQLite
   4499 ** is undefined.
   4500 **
   4501 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
   4502 ** with the addition that the xDestroy callback is invoked on pArg when
   4503 ** the collating function is deleted.
   4504 ** ^Collating functions are deleted when they are overridden by later
   4505 ** calls to the collation creation functions or when the
   4506 ** [database connection] is closed using [sqlite3_close()].
   4507 **
   4508 ** ^The xDestroy callback is <u>not</u> called if the
   4509 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
   4510 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
   4511 ** check the return code and dispose of the application data pointer
   4512 ** themselves rather than expecting SQLite to deal with it for them.
   4513 ** This is different from every other SQLite interface.  The inconsistency
   4514 ** is unfortunate but cannot be changed without breaking backwards
   4515 ** compatibility.
   4516 **
   4517 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
   4518 */
   4519 SQLITE_API int sqlite3_create_collation(
   4520   sqlite3*,
   4521   const char *zName,
   4522   int eTextRep,
   4523   void *pArg,
   4524   int(*xCompare)(void*,int,const void*,int,const void*)
   4525 );
   4526 SQLITE_API int sqlite3_create_collation_v2(
   4527   sqlite3*,
   4528   const char *zName,
   4529   int eTextRep,
   4530   void *pArg,
   4531   int(*xCompare)(void*,int,const void*,int,const void*),
   4532   void(*xDestroy)(void*)
   4533 );
   4534 SQLITE_API int sqlite3_create_collation16(
   4535   sqlite3*,
   4536   const void *zName,
   4537   int eTextRep,
   4538   void *pArg,
   4539   int(*xCompare)(void*,int,const void*,int,const void*)
   4540 );
   4541 
   4542 /*
   4543 ** CAPI3REF: Collation Needed Callbacks
   4544 **
   4545 ** ^To avoid having to register all collation sequences before a database
   4546 ** can be used, a single callback function may be registered with the
   4547 ** [database connection] to be invoked whenever an undefined collation
   4548 ** sequence is required.
   4549 **
   4550 ** ^If the function is registered using the sqlite3_collation_needed() API,
   4551 ** then it is passed the names of undefined collation sequences as strings
   4552 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
   4553 ** the names are passed as UTF-16 in machine native byte order.
   4554 ** ^A call to either function replaces the existing collation-needed callback.
   4555 **
   4556 ** ^(When the callback is invoked, the first argument passed is a copy
   4557 ** of the second argument to sqlite3_collation_needed() or
   4558 ** sqlite3_collation_needed16().  The second argument is the database
   4559 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
   4560 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
   4561 ** sequence function required.  The fourth parameter is the name of the
   4562 ** required collation sequence.)^
   4563 **
   4564 ** The callback function should register the desired collation using
   4565 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
   4566 ** [sqlite3_create_collation_v2()].
   4567 */
   4568 SQLITE_API int sqlite3_collation_needed(
   4569   sqlite3*,
   4570   void*,
   4571   void(*)(void*,sqlite3*,int eTextRep,const char*)
   4572 );
   4573 SQLITE_API int sqlite3_collation_needed16(
   4574   sqlite3*,
   4575   void*,
   4576   void(*)(void*,sqlite3*,int eTextRep,const void*)
   4577 );
   4578 
   4579 #ifdef SQLITE_HAS_CODEC
   4580 /*
   4581 ** Specify the key for an encrypted database.  This routine should be
   4582 ** called right after sqlite3_open().
   4583 **
   4584 ** The code to implement this API is not available in the public release
   4585 ** of SQLite.
   4586 */
   4587 SQLITE_API int sqlite3_key(
   4588   sqlite3 *db,                   /* Database to be rekeyed */
   4589   const void *pKey, int nKey     /* The key */
   4590 );
   4591 
   4592 /*
   4593 ** Change the key on an open database.  If the current database is not
   4594 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
   4595 ** database is decrypted.
   4596 **
   4597 ** The code to implement this API is not available in the public release
   4598 ** of SQLite.
   4599 */
   4600 SQLITE_API int sqlite3_rekey(
   4601   sqlite3 *db,                   /* Database to be rekeyed */
   4602   const void *pKey, int nKey     /* The new key */
   4603 );
   4604 
   4605 /*
   4606 ** Specify the activation key for a SEE database.  Unless
   4607 ** activated, none of the SEE routines will work.
   4608 */
   4609 SQLITE_API void sqlite3_activate_see(
   4610   const char *zPassPhrase        /* Activation phrase */
   4611 );
   4612 #endif
   4613 
   4614 #ifdef SQLITE_ENABLE_CEROD
   4615 /*
   4616 ** Specify the activation key for a CEROD database.  Unless
   4617 ** activated, none of the CEROD routines will work.
   4618 */
   4619 SQLITE_API void sqlite3_activate_cerod(
   4620   const char *zPassPhrase        /* Activation phrase */
   4621 );
   4622 #endif
   4623 
   4624 /*
   4625 ** CAPI3REF: Suspend Execution For A Short Time
   4626 **
   4627 ** The sqlite3_sleep() function causes the current thread to suspend execution
   4628 ** for at least a number of milliseconds specified in its parameter.
   4629 **
   4630 ** If the operating system does not support sleep requests with
   4631 ** millisecond time resolution, then the time will be rounded up to
   4632 ** the nearest second. The number of milliseconds of sleep actually
   4633 ** requested from the operating system is returned.
   4634 **
   4635 ** ^SQLite implements this interface by calling the xSleep()
   4636 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
   4637 ** of the default VFS is not implemented correctly, or not implemented at
   4638 ** all, then the behavior of sqlite3_sleep() may deviate from the description
   4639 ** in the previous paragraphs.
   4640 */
   4641 SQLITE_API int sqlite3_sleep(int);
   4642 
   4643 /*
   4644 ** CAPI3REF: Name Of The Folder Holding Temporary Files
   4645 **
   4646 ** ^(If this global variable is made to point to a string which is
   4647 ** the name of a folder (a.k.a. directory), then all temporary files
   4648 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
   4649 ** will be placed in that directory.)^  ^If this variable
   4650 ** is a NULL pointer, then SQLite performs a search for an appropriate
   4651 ** temporary file directory.
   4652 **
   4653 ** It is not safe to read or modify this variable in more than one
   4654 ** thread at a time.  It is not safe to read or modify this variable
   4655 ** if a [database connection] is being used at the same time in a separate
   4656 ** thread.
   4657 ** It is intended that this variable be set once
   4658 ** as part of process initialization and before any SQLite interface
   4659 ** routines have been called and that this variable remain unchanged
   4660 ** thereafter.
   4661 **
   4662 ** ^The [temp_store_directory pragma] may modify this variable and cause
   4663 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
   4664 ** the [temp_store_directory pragma] always assumes that any string
   4665 ** that this variable points to is held in memory obtained from
   4666 ** [sqlite3_malloc] and the pragma may attempt to free that memory
   4667 ** using [sqlite3_free].
   4668 ** Hence, if this variable is modified directly, either it should be
   4669 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
   4670 ** or else the use of the [temp_store_directory pragma] should be avoided.
   4671 */
   4672 SQLITE_API char *sqlite3_temp_directory;
   4673 
   4674 /*
   4675 ** CAPI3REF: Test For Auto-Commit Mode
   4676 ** KEYWORDS: {autocommit mode}
   4677 **
   4678 ** ^The sqlite3_get_autocommit() interface returns non-zero or
   4679 ** zero if the given database connection is or is not in autocommit mode,
   4680 ** respectively.  ^Autocommit mode is on by default.
   4681 ** ^Autocommit mode is disabled by a [BEGIN] statement.
   4682 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
   4683 **
   4684 ** If certain kinds of errors occur on a statement within a multi-statement
   4685 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
   4686 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
   4687 ** transaction might be rolled back automatically.  The only way to
   4688 ** find out whether SQLite automatically rolled back the transaction after
   4689 ** an error is to use this function.
   4690 **
   4691 ** If another thread changes the autocommit status of the database
   4692 ** connection while this routine is running, then the return value
   4693 ** is undefined.
   4694 */
   4695 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
   4696 
   4697 /*
   4698 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
   4699 **
   4700 ** ^The sqlite3_db_handle interface returns the [database connection] handle
   4701 ** to which a [prepared statement] belongs.  ^The [database connection]
   4702 ** returned by sqlite3_db_handle is the same [database connection]
   4703 ** that was the first argument
   4704 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
   4705 ** create the statement in the first place.
   4706 */
   4707 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
   4708 
   4709 /*
   4710 ** CAPI3REF: Find the next prepared statement
   4711 **
   4712 ** ^This interface returns a pointer to the next [prepared statement] after
   4713 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
   4714 ** then this interface returns a pointer to the first prepared statement
   4715 ** associated with the database connection pDb.  ^If no prepared statement
   4716 ** satisfies the conditions of this routine, it returns NULL.
   4717 **
   4718 ** The [database connection] pointer D in a call to
   4719 ** [sqlite3_next_stmt(D,S)] must refer to an open database
   4720 ** connection and in particular must not be a NULL pointer.
   4721 */
   4722 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
   4723 
   4724 /*
   4725 ** CAPI3REF: Commit And Rollback Notification Callbacks
   4726 **
   4727 ** ^The sqlite3_commit_hook() interface registers a callback
   4728 ** function to be invoked whenever a transaction is [COMMIT | committed].
   4729 ** ^Any callback set by a previous call to sqlite3_commit_hook()
   4730 ** for the same database connection is overridden.
   4731 ** ^The sqlite3_rollback_hook() interface registers a callback
   4732 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
   4733 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
   4734 ** for the same database connection is overridden.
   4735 ** ^The pArg argument is passed through to the callback.
   4736 ** ^If the callback on a commit hook function returns non-zero,
   4737 ** then the commit is converted into a rollback.
   4738 **
   4739 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
   4740 ** return the P argument from the previous call of the same function
   4741 ** on the same [database connection] D, or NULL for
   4742 ** the first call for each function on D.
   4743 **
   4744 ** The callback implementation must not do anything that will modify
   4745 ** the database connection that invoked the callback.  Any actions
   4746 ** to modify the database connection must be deferred until after the
   4747 ** completion of the [sqlite3_step()] call that triggered the commit
   4748 ** or rollback hook in the first place.
   4749 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
   4750 ** database connections for the meaning of "modify" in this paragraph.
   4751 **
   4752 ** ^Registering a NULL function disables the callback.
   4753 **
   4754 ** ^When the commit hook callback routine returns zero, the [COMMIT]
   4755 ** operation is allowed to continue normally.  ^If the commit hook
   4756 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
   4757 ** ^The rollback hook is invoked on a rollback that results from a commit
   4758 ** hook returning non-zero, just as it would be with any other rollback.
   4759 **
   4760 ** ^For the purposes of this API, a transaction is said to have been
   4761 ** rolled back if an explicit "ROLLBACK" statement is executed, or
   4762 ** an error or constraint causes an implicit rollback to occur.
   4763 ** ^The rollback callback is not invoked if a transaction is
   4764 ** automatically rolled back because the database connection is closed.
   4765 **
   4766 ** See also the [sqlite3_update_hook()] interface.
   4767 */
   4768 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
   4769 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
   4770 
   4771 /*
   4772 ** CAPI3REF: Data Change Notification Callbacks
   4773 **
   4774 ** ^The sqlite3_update_hook() interface registers a callback function
   4775 ** with the [database connection] identified by the first argument
   4776 ** to be invoked whenever a row is updated, inserted or deleted.
   4777 ** ^Any callback set by a previous call to this function
   4778 ** for the same database connection is overridden.
   4779 **
   4780 ** ^The second argument is a pointer to the function to invoke when a
   4781 ** row is updated, inserted or deleted.
   4782 ** ^The first argument to the callback is a copy of the third argument
   4783 ** to sqlite3_update_hook().
   4784 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
   4785 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
   4786 ** to be invoked.
   4787 ** ^The third and fourth arguments to the callback contain pointers to the
   4788 ** database and table name containing the affected row.
   4789 ** ^The final callback parameter is the [rowid] of the row.
   4790 ** ^In the case of an update, this is the [rowid] after the update takes place.
   4791 **
   4792 ** ^(The update hook is not invoked when internal system tables are
   4793 ** modified (i.e. sqlite_master and sqlite_sequence).)^
   4794 **
   4795 ** ^In the current implementation, the update hook
   4796 ** is not invoked when duplication rows are deleted because of an
   4797 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
   4798 ** invoked when rows are deleted using the [truncate optimization].
   4799 ** The exceptions defined in this paragraph might change in a future
   4800 ** release of SQLite.
   4801 **
   4802 ** The update hook implementation must not do anything that will modify
   4803 ** the database connection that invoked the update hook.  Any actions
   4804 ** to modify the database connection must be deferred until after the
   4805 ** completion of the [sqlite3_step()] call that triggered the update hook.
   4806 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
   4807 ** database connections for the meaning of "modify" in this paragraph.
   4808 **
   4809 ** ^The sqlite3_update_hook(D,C,P) function
   4810 ** returns the P argument from the previous call
   4811 ** on the same [database connection] D, or NULL for
   4812 ** the first call on D.
   4813 **
   4814 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
   4815 ** interfaces.
   4816 */
   4817 SQLITE_API void *sqlite3_update_hook(
   4818   sqlite3*,
   4819   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
   4820   void*
   4821 );
   4822 
   4823 /*
   4824 ** CAPI3REF: Enable Or Disable Shared Pager Cache
   4825 ** KEYWORDS: {shared cache}
   4826 **
   4827 ** ^(This routine enables or disables the sharing of the database cache
   4828 ** and schema data structures between [database connection | connections]
   4829 ** to the same database. Sharing is enabled if the argument is true
   4830 ** and disabled if the argument is false.)^
   4831 **
   4832 ** ^Cache sharing is enabled and disabled for an entire process.
   4833 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
   4834 ** sharing was enabled or disabled for each thread separately.
   4835 **
   4836 ** ^(The cache sharing mode set by this interface effects all subsequent
   4837 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
   4838 ** Existing database connections continue use the sharing mode
   4839 ** that was in effect at the time they were opened.)^
   4840 **
   4841 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
   4842 ** successfully.  An [error code] is returned otherwise.)^
   4843 **
   4844 ** ^Shared cache is disabled by default. But this might change in
   4845 ** future releases of SQLite.  Applications that care about shared
   4846 ** cache setting should set it explicitly.
   4847 **
   4848 ** See Also:  [SQLite Shared-Cache Mode]
   4849 */
   4850 SQLITE_API int sqlite3_enable_shared_cache(int);
   4851 
   4852 /*
   4853 ** CAPI3REF: Attempt To Free Heap Memory
   4854 **
   4855 ** ^The sqlite3_release_memory() interface attempts to free N bytes
   4856 ** of heap memory by deallocating non-essential memory allocations
   4857 ** held by the database library.   Memory used to cache database
   4858 ** pages to improve performance is an example of non-essential memory.
   4859 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
   4860 ** which might be more or less than the amount requested.
   4861 ** ^The sqlite3_release_memory() routine is a no-op returning zero
   4862 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
   4863 */
   4864 SQLITE_API int sqlite3_release_memory(int);
   4865 
   4866 /*
   4867 ** CAPI3REF: Impose A Limit On Heap Size
   4868 **
   4869 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
   4870 ** soft limit on the amount of heap memory that may be allocated by SQLite.
   4871 ** ^SQLite strives to keep heap memory utilization below the soft heap
   4872 ** limit by reducing the number of pages held in the page cache
   4873 ** as heap memory usages approaches the limit.
   4874 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
   4875 ** below the limit, it will exceed the limit rather than generate
   4876 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
   4877 ** is advisory only.
   4878 **
   4879 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
   4880 ** the soft heap limit prior to the call.  ^If the argument N is negative
   4881 ** then no change is made to the soft heap limit.  Hence, the current
   4882 ** size of the soft heap limit can be determined by invoking
   4883 ** sqlite3_soft_heap_limit64() with a negative argument.
   4884 **
   4885 ** ^If the argument N is zero then the soft heap limit is disabled.
   4886 **
   4887 ** ^(The soft heap limit is not enforced in the current implementation
   4888 ** if one or more of following conditions are true:
   4889 **
   4890 ** <ul>
   4891 ** <li> The soft heap limit is set to zero.
   4892 ** <li> Memory accounting is disabled using a combination of the
   4893 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
   4894 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
   4895 ** <li> An alternative page cache implementation is specified using
   4896 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
   4897 ** <li> The page cache allocates from its own memory pool supplied
   4898 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
   4899 **      from the heap.
   4900 ** </ul>)^
   4901 **
   4902 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
   4903 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
   4904 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
   4905 ** the soft heap limit is enforced on every memory allocation.  Without
   4906 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
   4907 ** when memory is allocated by the page cache.  Testing suggests that because
   4908 ** the page cache is the predominate memory user in SQLite, most
   4909 ** applications will achieve adequate soft heap limit enforcement without
   4910 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
   4911 **
   4912 ** The circumstances under which SQLite will enforce the soft heap limit may
   4913 ** changes in future releases of SQLite.
   4914 */
   4915 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
   4916 
   4917 /*
   4918 ** CAPI3REF: Deprecated Soft Heap Limit Interface
   4919 ** DEPRECATED
   4920 **
   4921 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
   4922 ** interface.  This routine is provided for historical compatibility
   4923 ** only.  All new applications should use the
   4924 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
   4925 */
   4926 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
   4927 
   4928 
   4929 /*
   4930 ** CAPI3REF: Extract Metadata About A Column Of A Table
   4931 **
   4932 ** ^This routine returns metadata about a specific column of a specific
   4933 ** database table accessible using the [database connection] handle
   4934 ** passed as the first function argument.
   4935 **
   4936 ** ^The column is identified by the second, third and fourth parameters to
   4937 ** this function. ^The second parameter is either the name of the database
   4938 ** (i.e. "main", "temp", or an attached database) containing the specified
   4939 ** table or NULL. ^If it is NULL, then all attached databases are searched
   4940 ** for the table using the same algorithm used by the database engine to
   4941 ** resolve unqualified table references.
   4942 **
   4943 ** ^The third and fourth parameters to this function are the table and column
   4944 ** name of the desired column, respectively. Neither of these parameters
   4945 ** may be NULL.
   4946 **
   4947 ** ^Metadata is returned by writing to the memory locations passed as the 5th
   4948 ** and subsequent parameters to this function. ^Any of these arguments may be
   4949 ** NULL, in which case the corresponding element of metadata is omitted.
   4950 **
   4951 ** ^(<blockquote>
   4952 ** <table border="1">
   4953 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
   4954 **
   4955 ** <tr><td> 5th <td> const char* <td> Data type
   4956 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
   4957 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
   4958 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
   4959 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
   4960 ** </table>
   4961 ** </blockquote>)^
   4962 **
   4963 ** ^The memory pointed to by the character pointers returned for the
   4964 ** declaration type and collation sequence is valid only until the next
   4965 ** call to any SQLite API function.
   4966 **
   4967 ** ^If the specified table is actually a view, an [error code] is returned.
   4968 **
   4969 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
   4970 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
   4971 ** parameters are set for the explicitly declared column. ^(If there is no
   4972 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
   4973 ** parameters are set as follows:
   4974 **
   4975 ** <pre>
   4976 **     data type: "INTEGER"
   4977 **     collation sequence: "BINARY"
   4978 **     not null: 0
   4979 **     primary key: 1
   4980 **     auto increment: 0
   4981 ** </pre>)^
   4982 **
   4983 ** ^(This function may load one or more schemas from database files. If an
   4984 ** error occurs during this process, or if the requested table or column
   4985 ** cannot be found, an [error code] is returned and an error message left
   4986 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
   4987 **
   4988 ** ^This API is only available if the library was compiled with the
   4989 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
   4990 */
   4991 SQLITE_API int sqlite3_table_column_metadata(
   4992   sqlite3 *db,                /* Connection handle */
   4993   const char *zDbName,        /* Database name or NULL */
   4994   const char *zTableName,     /* Table name */
   4995   const char *zColumnName,    /* Column name */
   4996   char const **pzDataType,    /* OUTPUT: Declared data type */
   4997   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
   4998   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
   4999   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
   5000   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
   5001 );
   5002 
   5003 /*
   5004 ** CAPI3REF: Load An Extension
   5005 **
   5006 ** ^This interface loads an SQLite extension library from the named file.
   5007 **
   5008 ** ^The sqlite3_load_extension() interface attempts to load an
   5009 ** SQLite extension library contained in the file zFile.
   5010 **
   5011 ** ^The entry point is zProc.
   5012 ** ^zProc may be 0, in which case the name of the entry point
   5013 ** defaults to "sqlite3_extension_init".
   5014 ** ^The sqlite3_load_extension() interface returns
   5015 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
   5016 ** ^If an error occurs and pzErrMsg is not 0, then the
   5017 ** [sqlite3_load_extension()] interface shall attempt to
   5018 ** fill *pzErrMsg with error message text stored in memory
   5019 ** obtained from [sqlite3_malloc()]. The calling function
   5020 ** should free this memory by calling [sqlite3_free()].
   5021 **
   5022 ** ^Extension loading must be enabled using
   5023 ** [sqlite3_enable_load_extension()] prior to calling this API,
   5024 ** otherwise an error will be returned.
   5025 **
   5026 ** See also the [load_extension() SQL function].
   5027 */
   5028 SQLITE_API int sqlite3_load_extension(
   5029   sqlite3 *db,          /* Load the extension into this database connection */
   5030   const char *zFile,    /* Name of the shared library containing extension */
   5031   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
   5032   char **pzErrMsg       /* Put error message here if not 0 */
   5033 );
   5034 
   5035 /*
   5036 ** CAPI3REF: Enable Or Disable Extension Loading
   5037 **
   5038 ** ^So as not to open security holes in older applications that are
   5039 ** unprepared to deal with extension loading, and as a means of disabling
   5040 ** extension loading while evaluating user-entered SQL, the following API
   5041 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
   5042 **
   5043 ** ^Extension loading is off by default. See ticket #1863.
   5044 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
   5045 ** to turn extension loading on and call it with onoff==0 to turn
   5046 ** it back off again.
   5047 */
   5048 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
   5049 
   5050 /*
   5051 ** CAPI3REF: Automatically Load Statically Linked Extensions
   5052 **
   5053 ** ^This interface causes the xEntryPoint() function to be invoked for
   5054 ** each new [database connection] that is created.  The idea here is that
   5055 ** xEntryPoint() is the entry point for a statically linked SQLite extension
   5056 ** that is to be automatically loaded into all new database connections.
   5057 **
   5058 ** ^(Even though the function prototype shows that xEntryPoint() takes
   5059 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
   5060 ** arguments and expects and integer result as if the signature of the
   5061 ** entry point where as follows:
   5062 **
   5063 ** <blockquote><pre>
   5064 ** &nbsp;  int xEntryPoint(
   5065 ** &nbsp;    sqlite3 *db,
   5066 ** &nbsp;    const char **pzErrMsg,
   5067 ** &nbsp;    const struct sqlite3_api_routines *pThunk
   5068 ** &nbsp;  );
   5069 ** </pre></blockquote>)^
   5070 **
   5071 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
   5072 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
   5073 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
   5074 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
   5075 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
   5076 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
   5077 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
   5078 **
   5079 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
   5080 ** on the list of automatic extensions is a harmless no-op. ^No entry point
   5081 ** will be called more than once for each database connection that is opened.
   5082 **
   5083 ** See also: [sqlite3_reset_auto_extension()].
   5084 */
   5085 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
   5086 
   5087 /*
   5088 ** CAPI3REF: Reset Automatic Extension Loading
   5089 **
   5090 ** ^This interface disables all automatic extensions previously
   5091 ** registered using [sqlite3_auto_extension()].
   5092 */
   5093 SQLITE_API void sqlite3_reset_auto_extension(void);
   5094 
   5095 /*
   5096 ** The interface to the virtual-table mechanism is currently considered
   5097 ** to be experimental.  The interface might change in incompatible ways.
   5098 ** If this is a problem for you, do not use the interface at this time.
   5099 **
   5100 ** When the virtual-table mechanism stabilizes, we will declare the
   5101 ** interface fixed, support it indefinitely, and remove this comment.
   5102 */
   5103 
   5104 /*
   5105 ** Structures used by the virtual table interface
   5106 */
   5107 typedef struct sqlite3_vtab sqlite3_vtab;
   5108 typedef struct sqlite3_index_info sqlite3_index_info;
   5109 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
   5110 typedef struct sqlite3_module sqlite3_module;
   5111 
   5112 /*
   5113 ** CAPI3REF: Virtual Table Object
   5114 ** KEYWORDS: sqlite3_module {virtual table module}
   5115 **
   5116 ** This structure, sometimes called a "virtual table module",
   5117 ** defines the implementation of a [virtual tables].
   5118 ** This structure consists mostly of methods for the module.
   5119 **
   5120 ** ^A virtual table module is created by filling in a persistent
   5121 ** instance of this structure and passing a pointer to that instance
   5122 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
   5123 ** ^The registration remains valid until it is replaced by a different
   5124 ** module or until the [database connection] closes.  The content
   5125 ** of this structure must not change while it is registered with
   5126 ** any database connection.
   5127 */
   5128 struct sqlite3_module {
   5129   int iVersion;
   5130   int (*xCreate)(sqlite3*, void *pAux,
   5131                int argc, const char *const*argv,
   5132                sqlite3_vtab **ppVTab, char**);
   5133   int (*xConnect)(sqlite3*, void *pAux,
   5134                int argc, const char *const*argv,
   5135                sqlite3_vtab **ppVTab, char**);
   5136   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
   5137   int (*xDisconnect)(sqlite3_vtab *pVTab);
   5138   int (*xDestroy)(sqlite3_vtab *pVTab);
   5139   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
   5140   int (*xClose)(sqlite3_vtab_cursor*);
   5141   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
   5142                 int argc, sqlite3_value **argv);
   5143   int (*xNext)(sqlite3_vtab_cursor*);
   5144   int (*xEof)(sqlite3_vtab_cursor*);
   5145   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
   5146   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
   5147   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
   5148   int (*xBegin)(sqlite3_vtab *pVTab);
   5149   int (*xSync)(sqlite3_vtab *pVTab);
   5150   int (*xCommit)(sqlite3_vtab *pVTab);
   5151   int (*xRollback)(sqlite3_vtab *pVTab);
   5152   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
   5153                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
   5154                        void **ppArg);
   5155   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
   5156 };
   5157 
   5158 /*
   5159 ** CAPI3REF: Virtual Table Indexing Information
   5160 ** KEYWORDS: sqlite3_index_info
   5161 **
   5162 ** The sqlite3_index_info structure and its substructures is used as part
   5163 ** of the [virtual table] interface to
   5164 ** pass information into and receive the reply from the [xBestIndex]
   5165 ** method of a [virtual table module].  The fields under **Inputs** are the
   5166 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
   5167 ** results into the **Outputs** fields.
   5168 **
   5169 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
   5170 **
   5171 ** <blockquote>column OP expr</blockquote>
   5172 **
   5173 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
   5174 ** stored in aConstraint[].op using one of the
   5175 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
   5176 ** ^(The index of the column is stored in
   5177 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
   5178 ** expr on the right-hand side can be evaluated (and thus the constraint
   5179 ** is usable) and false if it cannot.)^
   5180 **
   5181 ** ^The optimizer automatically inverts terms of the form "expr OP column"
   5182 ** and makes other simplifications to the WHERE clause in an attempt to
   5183 ** get as many WHERE clause terms into the form shown above as possible.
   5184 ** ^The aConstraint[] array only reports WHERE clause terms that are
   5185 ** relevant to the particular virtual table being queried.
   5186 **
   5187 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
   5188 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
   5189 **
   5190 ** The [xBestIndex] method must fill aConstraintUsage[] with information
   5191 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
   5192 ** the right-hand side of the corresponding aConstraint[] is evaluated
   5193 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
   5194 ** is true, then the constraint is assumed to be fully handled by the
   5195 ** virtual table and is not checked again by SQLite.)^
   5196 **
   5197 ** ^The idxNum and idxPtr values are recorded and passed into the
   5198 ** [xFilter] method.
   5199 ** ^[sqlite3_free()] is used to free idxPtr if and only if
   5200 ** needToFreeIdxPtr is true.
   5201 **
   5202 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
   5203 ** the correct order to satisfy the ORDER BY clause so that no separate
   5204 ** sorting step is required.
   5205 **
   5206 ** ^The estimatedCost value is an estimate of the cost of doing the
   5207 ** particular lookup.  A full scan of a table with N entries should have
   5208 ** a cost of N.  A binary search of a table of N entries should have a
   5209 ** cost of approximately log(N).
   5210 */
   5211 struct sqlite3_index_info {
   5212   /* Inputs */
   5213   int nConstraint;           /* Number of entries in aConstraint */
   5214   struct sqlite3_index_constraint {
   5215      int iColumn;              /* Column on left-hand side of constraint */
   5216      unsigned char op;         /* Constraint operator */
   5217      unsigned char usable;     /* True if this constraint is usable */
   5218      int iTermOffset;          /* Used internally - xBestIndex should ignore */
   5219   } *aConstraint;            /* Table of WHERE clause constraints */
   5220   int nOrderBy;              /* Number of terms in the ORDER BY clause */
   5221   struct sqlite3_index_orderby {
   5222      int iColumn;              /* Column number */
   5223      unsigned char desc;       /* True for DESC.  False for ASC. */
   5224   } *aOrderBy;               /* The ORDER BY clause */
   5225   /* Outputs */
   5226   struct sqlite3_index_constraint_usage {
   5227     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
   5228     unsigned char omit;      /* Do not code a test for this constraint */
   5229   } *aConstraintUsage;
   5230   int idxNum;                /* Number used to identify the index */
   5231   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
   5232   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
   5233   int orderByConsumed;       /* True if output is already ordered */
   5234   double estimatedCost;      /* Estimated cost of using this index */
   5235 };
   5236 
   5237 /*
   5238 ** CAPI3REF: Virtual Table Constraint Operator Codes
   5239 **
   5240 ** These macros defined the allowed values for the
   5241 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
   5242 ** an operator that is part of a constraint term in the wHERE clause of
   5243 ** a query that uses a [virtual table].
   5244 */
   5245 #define SQLITE_INDEX_CONSTRAINT_EQ    2
   5246 #define SQLITE_INDEX_CONSTRAINT_GT    4
   5247 #define SQLITE_INDEX_CONSTRAINT_LE    8
   5248 #define SQLITE_INDEX_CONSTRAINT_LT    16
   5249 #define SQLITE_INDEX_CONSTRAINT_GE    32
   5250 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
   5251 
   5252 /*
   5253 ** CAPI3REF: Register A Virtual Table Implementation
   5254 **
   5255 ** ^These routines are used to register a new [virtual table module] name.
   5256 ** ^Module names must be registered before
   5257 ** creating a new [virtual table] using the module and before using a
   5258 ** preexisting [virtual table] for the module.
   5259 **
   5260 ** ^The module name is registered on the [database connection] specified
   5261 ** by the first parameter.  ^The name of the module is given by the
   5262 ** second parameter.  ^The third parameter is a pointer to
   5263 ** the implementation of the [virtual table module].   ^The fourth
   5264 ** parameter is an arbitrary client data pointer that is passed through
   5265 ** into the [xCreate] and [xConnect] methods of the virtual table module
   5266 ** when a new virtual table is be being created or reinitialized.
   5267 **
   5268 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
   5269 ** is a pointer to a destructor for the pClientData.  ^SQLite will
   5270 ** invoke the destructor function (if it is not NULL) when SQLite
   5271 ** no longer needs the pClientData pointer.  ^The destructor will also
   5272 ** be invoked if the call to sqlite3_create_module_v2() fails.
   5273 ** ^The sqlite3_create_module()
   5274 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
   5275 ** destructor.
   5276 */
   5277 SQLITE_API int sqlite3_create_module(
   5278   sqlite3 *db,               /* SQLite connection to register module with */
   5279   const char *zName,         /* Name of the module */
   5280   const sqlite3_module *p,   /* Methods for the module */
   5281   void *pClientData          /* Client data for xCreate/xConnect */
   5282 );
   5283 SQLITE_API int sqlite3_create_module_v2(
   5284   sqlite3 *db,               /* SQLite connection to register module with */
   5285   const char *zName,         /* Name of the module */
   5286   const sqlite3_module *p,   /* Methods for the module */
   5287   void *pClientData,         /* Client data for xCreate/xConnect */
   5288   void(*xDestroy)(void*)     /* Module destructor function */
   5289 );
   5290 
   5291 /*
   5292 ** CAPI3REF: Virtual Table Instance Object
   5293 ** KEYWORDS: sqlite3_vtab
   5294 **
   5295 ** Every [virtual table module] implementation uses a subclass
   5296 ** of this object to describe a particular instance
   5297 ** of the [virtual table].  Each subclass will
   5298 ** be tailored to the specific needs of the module implementation.
   5299 ** The purpose of this superclass is to define certain fields that are
   5300 ** common to all module implementations.
   5301 **
   5302 ** ^Virtual tables methods can set an error message by assigning a
   5303 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
   5304 ** take care that any prior string is freed by a call to [sqlite3_free()]
   5305 ** prior to assigning a new string to zErrMsg.  ^After the error message
   5306 ** is delivered up to the client application, the string will be automatically
   5307 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
   5308 */
   5309 struct sqlite3_vtab {
   5310   const sqlite3_module *pModule;  /* The module for this virtual table */
   5311   int nRef;                       /* NO LONGER USED */
   5312   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
   5313   /* Virtual table implementations will typically add additional fields */
   5314 };
   5315 
   5316 /*
   5317 ** CAPI3REF: Virtual Table Cursor Object
   5318 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
   5319 **
   5320 ** Every [virtual table module] implementation uses a subclass of the
   5321 ** following structure to describe cursors that point into the
   5322 ** [virtual table] and are used
   5323 ** to loop through the virtual table.  Cursors are created using the
   5324 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
   5325 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
   5326 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
   5327 ** of the module.  Each module implementation will define
   5328 ** the content of a cursor structure to suit its own needs.
   5329 **
   5330 ** This superclass exists in order to define fields of the cursor that
   5331 ** are common to all implementations.
   5332 */
   5333 struct sqlite3_vtab_cursor {
   5334   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
   5335   /* Virtual table implementations will typically add additional fields */
   5336 };
   5337 
   5338 /*
   5339 ** CAPI3REF: Declare The Schema Of A Virtual Table
   5340 **
   5341 ** ^The [xCreate] and [xConnect] methods of a
   5342 ** [virtual table module] call this interface
   5343 ** to declare the format (the names and datatypes of the columns) of
   5344 ** the virtual tables they implement.
   5345 */
   5346 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
   5347 
   5348 /*
   5349 ** CAPI3REF: Overload A Function For A Virtual Table
   5350 **
   5351 ** ^(Virtual tables can provide alternative implementations of functions
   5352 ** using the [xFindFunction] method of the [virtual table module].
   5353 ** But global versions of those functions
   5354 ** must exist in order to be overloaded.)^
   5355 **
   5356 ** ^(This API makes sure a global version of a function with a particular
   5357 ** name and number of parameters exists.  If no such function exists
   5358 ** before this API is called, a new function is created.)^  ^The implementation
   5359 ** of the new function always causes an exception to be thrown.  So
   5360 ** the new function is not good for anything by itself.  Its only
   5361 ** purpose is to be a placeholder function that can be overloaded
   5362 ** by a [virtual table].
   5363 */
   5364 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
   5365 
   5366 /*
   5367 ** The interface to the virtual-table mechanism defined above (back up
   5368 ** to a comment remarkably similar to this one) is currently considered
   5369 ** to be experimental.  The interface might change in incompatible ways.
   5370 ** If this is a problem for you, do not use the interface at this time.
   5371 **
   5372 ** When the virtual-table mechanism stabilizes, we will declare the
   5373 ** interface fixed, support it indefinitely, and remove this comment.
   5374 */
   5375 
   5376 /*
   5377 ** CAPI3REF: A Handle To An Open BLOB
   5378 ** KEYWORDS: {BLOB handle} {BLOB handles}
   5379 **
   5380 ** An instance of this object represents an open BLOB on which
   5381 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
   5382 ** ^Objects of this type are created by [sqlite3_blob_open()]
   5383 ** and destroyed by [sqlite3_blob_close()].
   5384 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
   5385 ** can be used to read or write small subsections of the BLOB.
   5386 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
   5387 */
   5388 typedef struct sqlite3_blob sqlite3_blob;
   5389 
   5390 /*
   5391 ** CAPI3REF: Open A BLOB For Incremental I/O
   5392 **
   5393 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
   5394 ** in row iRow, column zColumn, table zTable in database zDb;
   5395 ** in other words, the same BLOB that would be selected by:
   5396 **
   5397 ** <pre>
   5398 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
   5399 ** </pre>)^
   5400 **
   5401 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
   5402 ** and write access. ^If it is zero, the BLOB is opened for read access.
   5403 ** ^It is not possible to open a column that is part of an index or primary
   5404 ** key for writing. ^If [foreign key constraints] are enabled, it is
   5405 ** not possible to open a column that is part of a [child key] for writing.
   5406 **
   5407 ** ^Note that the database name is not the filename that contains
   5408 ** the database but rather the symbolic name of the database that
   5409 ** appears after the AS keyword when the database is connected using [ATTACH].
   5410 ** ^For the main database file, the database name is "main".
   5411 ** ^For TEMP tables, the database name is "temp".
   5412 **
   5413 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
   5414 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
   5415 ** to be a null pointer.)^
   5416 ** ^This function sets the [database connection] error code and message
   5417 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
   5418 ** functions. ^Note that the *ppBlob variable is always initialized in a
   5419 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
   5420 ** regardless of the success or failure of this routine.
   5421 **
   5422 ** ^(If the row that a BLOB handle points to is modified by an
   5423 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
   5424 ** then the BLOB handle is marked as "expired".
   5425 ** This is true if any column of the row is changed, even a column
   5426 ** other than the one the BLOB handle is open on.)^
   5427 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
   5428 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
   5429 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
   5430 ** rolled back by the expiration of the BLOB.  Such changes will eventually
   5431 ** commit if the transaction continues to completion.)^
   5432 **
   5433 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
   5434 ** the opened blob.  ^The size of a blob may not be changed by this
   5435 ** interface.  Use the [UPDATE] SQL command to change the size of a
   5436 ** blob.
   5437 **
   5438 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
   5439 ** and the built-in [zeroblob] SQL function can be used, if desired,
   5440 ** to create an empty, zero-filled blob in which to read or write using
   5441 ** this interface.
   5442 **
   5443 ** To avoid a resource leak, every open [BLOB handle] should eventually
   5444 ** be released by a call to [sqlite3_blob_close()].
   5445 */
   5446 SQLITE_API int sqlite3_blob_open(
   5447   sqlite3*,
   5448   const char *zDb,
   5449   const char *zTable,
   5450   const char *zColumn,
   5451   sqlite3_int64 iRow,
   5452   int flags,
   5453   sqlite3_blob **ppBlob
   5454 );
   5455 
   5456 /*
   5457 ** CAPI3REF: Move a BLOB Handle to a New Row
   5458 **
   5459 ** ^This function is used to move an existing blob handle so that it points
   5460 ** to a different row of the same database table. ^The new row is identified
   5461 ** by the rowid value passed as the second argument. Only the row can be
   5462 ** changed. ^The database, table and column on which the blob handle is open
   5463 ** remain the same. Moving an existing blob handle to a new row can be
   5464 ** faster than closing the existing handle and opening a new one.
   5465 **
   5466 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
   5467 ** it must exist and there must be either a blob or text value stored in
   5468 ** the nominated column.)^ ^If the new row is not present in the table, or if
   5469 ** it does not contain a blob or text value, or if another error occurs, an
   5470 ** SQLite error code is returned and the blob handle is considered aborted.
   5471 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
   5472 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
   5473 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
   5474 ** always returns zero.
   5475 **
   5476 ** ^This function sets the database handle error code and message.
   5477 */
   5478 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
   5479 
   5480 /*
   5481 ** CAPI3REF: Close A BLOB Handle
   5482 **
   5483 ** ^Closes an open [BLOB handle].
   5484 **
   5485 ** ^Closing a BLOB shall cause the current transaction to commit
   5486 ** if there are no other BLOBs, no pending prepared statements, and the
   5487 ** database connection is in [autocommit mode].
   5488 ** ^If any writes were made to the BLOB, they might be held in cache
   5489 ** until the close operation if they will fit.
   5490 **
   5491 ** ^(Closing the BLOB often forces the changes
   5492 ** out to disk and so if any I/O errors occur, they will likely occur
   5493 ** at the time when the BLOB is closed.  Any errors that occur during
   5494 ** closing are reported as a non-zero return value.)^
   5495 **
   5496 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
   5497 ** an error code, the BLOB is still closed.)^
   5498 **
   5499 ** ^Calling this routine with a null pointer (such as would be returned
   5500 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
   5501 */
   5502 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
   5503 
   5504 /*
   5505 ** CAPI3REF: Return The Size Of An Open BLOB
   5506 **
   5507 ** ^Returns the size in bytes of the BLOB accessible via the
   5508 ** successfully opened [BLOB handle] in its only argument.  ^The
   5509 ** incremental blob I/O routines can only read or overwriting existing
   5510 ** blob content; they cannot change the size of a blob.
   5511 **
   5512 ** This routine only works on a [BLOB handle] which has been created
   5513 ** by a prior successful call to [sqlite3_blob_open()] and which has not
   5514 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
   5515 ** to this routine results in undefined and probably undesirable behavior.
   5516 */
   5517 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
   5518 
   5519 /*
   5520 ** CAPI3REF: Read Data From A BLOB Incrementally
   5521 **
   5522 ** ^(This function is used to read data from an open [BLOB handle] into a
   5523 ** caller-supplied buffer. N bytes of data are copied into buffer Z
   5524 ** from the open BLOB, starting at offset iOffset.)^
   5525 **
   5526 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
   5527 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
   5528 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
   5529 ** ^The size of the blob (and hence the maximum value of N+iOffset)
   5530 ** can be determined using the [sqlite3_blob_bytes()] interface.
   5531 **
   5532 ** ^An attempt to read from an expired [BLOB handle] fails with an
   5533 ** error code of [SQLITE_ABORT].
   5534 **
   5535 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
   5536 ** Otherwise, an [error code] or an [extended error code] is returned.)^
   5537 **
   5538 ** This routine only works on a [BLOB handle] which has been created
   5539 ** by a prior successful call to [sqlite3_blob_open()] and which has not
   5540 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
   5541 ** to this routine results in undefined and probably undesirable behavior.
   5542 **
   5543 ** See also: [sqlite3_blob_write()].
   5544 */
   5545 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
   5546 
   5547 /*
   5548 ** CAPI3REF: Write Data Into A BLOB Incrementally
   5549 **
   5550 ** ^This function is used to write data into an open [BLOB handle] from a
   5551 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
   5552 ** into the open BLOB, starting at offset iOffset.
   5553 **
   5554 ** ^If the [BLOB handle] passed as the first argument was not opened for
   5555 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
   5556 ** this function returns [SQLITE_READONLY].
   5557 **
   5558 ** ^This function may only modify the contents of the BLOB; it is
   5559 ** not possible to increase the size of a BLOB using this API.
   5560 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
   5561 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
   5562 ** less than zero [SQLITE_ERROR] is returned and no data is written.
   5563 ** The size of the BLOB (and hence the maximum value of N+iOffset)
   5564 ** can be determined using the [sqlite3_blob_bytes()] interface.
   5565 **
   5566 ** ^An attempt to write to an expired [BLOB handle] fails with an
   5567 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
   5568 ** before the [BLOB handle] expired are not rolled back by the
   5569 ** expiration of the handle, though of course those changes might
   5570 ** have been overwritten by the statement that expired the BLOB handle
   5571 ** or by other independent statements.
   5572 **
   5573 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
   5574 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
   5575 **
   5576 ** This routine only works on a [BLOB handle] which has been created
   5577 ** by a prior successful call to [sqlite3_blob_open()] and which has not
   5578 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
   5579 ** to this routine results in undefined and probably undesirable behavior.
   5580 **
   5581 ** See also: [sqlite3_blob_read()].
   5582 */
   5583 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
   5584 
   5585 /*
   5586 ** CAPI3REF: Virtual File System Objects
   5587 **
   5588 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
   5589 ** that SQLite uses to interact
   5590 ** with the underlying operating system.  Most SQLite builds come with a
   5591 ** single default VFS that is appropriate for the host computer.
   5592 ** New VFSes can be registered and existing VFSes can be unregistered.
   5593 ** The following interfaces are provided.
   5594 **
   5595 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
   5596 ** ^Names are case sensitive.
   5597 ** ^Names are zero-terminated UTF-8 strings.
   5598 ** ^If there is no match, a NULL pointer is returned.
   5599 ** ^If zVfsName is NULL then the default VFS is returned.
   5600 **
   5601 ** ^New VFSes are registered with sqlite3_vfs_register().
   5602 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
   5603 ** ^The same VFS can be registered multiple times without injury.
   5604 ** ^To make an existing VFS into the default VFS, register it again
   5605 ** with the makeDflt flag set.  If two different VFSes with the
   5606 ** same name are registered, the behavior is undefined.  If a
   5607 ** VFS is registered with a name that is NULL or an empty string,
   5608 ** then the behavior is undefined.
   5609 **
   5610 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
   5611 ** ^(If the default VFS is unregistered, another VFS is chosen as
   5612 ** the default.  The choice for the new VFS is arbitrary.)^
   5613 */
   5614 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
   5615 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
   5616 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
   5617 
   5618 /*
   5619 ** CAPI3REF: Mutexes
   5620 **
   5621 ** The SQLite core uses these routines for thread
   5622 ** synchronization. Though they are intended for internal
   5623 ** use by SQLite, code that links against SQLite is
   5624 ** permitted to use any of these routines.
   5625 **
   5626 ** The SQLite source code contains multiple implementations
   5627 ** of these mutex routines.  An appropriate implementation
   5628 ** is selected automatically at compile-time.  ^(The following
   5629 ** implementations are available in the SQLite core:
   5630 **
   5631 ** <ul>
   5632 ** <li>   SQLITE_MUTEX_OS2
   5633 ** <li>   SQLITE_MUTEX_PTHREAD
   5634 ** <li>   SQLITE_MUTEX_W32
   5635 ** <li>   SQLITE_MUTEX_NOOP
   5636 ** </ul>)^
   5637 **
   5638 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
   5639 ** that does no real locking and is appropriate for use in
   5640 ** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
   5641 ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
   5642 ** are appropriate for use on OS/2, Unix, and Windows.
   5643 **
   5644 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
   5645 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
   5646 ** implementation is included with the library. In this case the
   5647 ** application must supply a custom mutex implementation using the
   5648 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
   5649 ** before calling sqlite3_initialize() or any other public sqlite3_
   5650 ** function that calls sqlite3_initialize().)^
   5651 **
   5652 ** ^The sqlite3_mutex_alloc() routine allocates a new
   5653 ** mutex and returns a pointer to it. ^If it returns NULL
   5654 ** that means that a mutex could not be allocated.  ^SQLite
   5655 ** will unwind its stack and return an error.  ^(The argument
   5656 ** to sqlite3_mutex_alloc() is one of these integer constants:
   5657 **
   5658 ** <ul>
   5659 ** <li>  SQLITE_MUTEX_FAST
   5660 ** <li>  SQLITE_MUTEX_RECURSIVE
   5661 ** <li>  SQLITE_MUTEX_STATIC_MASTER
   5662 ** <li>  SQLITE_MUTEX_STATIC_MEM
   5663 ** <li>  SQLITE_MUTEX_STATIC_MEM2
   5664 ** <li>  SQLITE_MUTEX_STATIC_PRNG
   5665 ** <li>  SQLITE_MUTEX_STATIC_LRU
   5666 ** <li>  SQLITE_MUTEX_STATIC_LRU2
   5667 ** </ul>)^
   5668 **
   5669 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
   5670 ** cause sqlite3_mutex_alloc() to create
   5671 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   5672 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   5673 ** The mutex implementation does not need to make a distinction
   5674 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   5675 ** not want to.  ^SQLite will only request a recursive mutex in
   5676 ** cases where it really needs one.  ^If a faster non-recursive mutex
   5677 ** implementation is available on the host platform, the mutex subsystem
   5678 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   5679 **
   5680 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
   5681 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
   5682 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
   5683 ** used by the current version of SQLite.  Future versions of SQLite
   5684 ** may add additional static mutexes.  Static mutexes are for internal
   5685 ** use by SQLite only.  Applications that use SQLite mutexes should
   5686 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   5687 ** SQLITE_MUTEX_RECURSIVE.
   5688 **
   5689 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   5690 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   5691 ** returns a different mutex on every call.  ^But for the static
   5692 ** mutex types, the same mutex is returned on every call that has
   5693 ** the same type number.
   5694 **
   5695 ** ^The sqlite3_mutex_free() routine deallocates a previously
   5696 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
   5697 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
   5698 ** use when they are deallocated.  Attempting to deallocate a static
   5699 ** mutex results in undefined behavior.  ^SQLite never deallocates
   5700 ** a static mutex.
   5701 **
   5702 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   5703 ** to enter a mutex.  ^If another thread is already within the mutex,
   5704 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   5705 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
   5706 ** upon successful entry.  ^(Mutexes created using
   5707 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
   5708 ** In such cases the,
   5709 ** mutex must be exited an equal number of times before another thread
   5710 ** can enter.)^  ^(If the same thread tries to enter any other
   5711 ** kind of mutex more than once, the behavior is undefined.
   5712 ** SQLite will never exhibit
   5713 ** such behavior in its own use of mutexes.)^
   5714 **
   5715 ** ^(Some systems (for example, Windows 95) do not support the operation
   5716 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
   5717 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
   5718 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
   5719 **
   5720 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
   5721 ** previously entered by the same thread.   ^(The behavior
   5722 ** is undefined if the mutex is not currently entered by the
   5723 ** calling thread or is not currently allocated.  SQLite will
   5724 ** never do either.)^
   5725 **
   5726 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
   5727 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
   5728 ** behave as no-ops.
   5729 **
   5730 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
   5731 */
   5732 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
   5733 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
   5734 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
   5735 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
   5736 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
   5737 
   5738 /*
   5739 ** CAPI3REF: Mutex Methods Object
   5740 **
   5741 ** An instance of this structure defines the low-level routines
   5742 ** used to allocate and use mutexes.
   5743 **
   5744 ** Usually, the default mutex implementations provided by SQLite are
   5745 ** sufficient, however the user has the option of substituting a custom
   5746 ** implementation for specialized deployments or systems for which SQLite
   5747 ** does not provide a suitable implementation. In this case, the user
   5748 ** creates and populates an instance of this structure to pass
   5749 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
   5750 ** Additionally, an instance of this structure can be used as an
   5751 ** output variable when querying the system for the current mutex
   5752 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
   5753 **
   5754 ** ^The xMutexInit method defined by this structure is invoked as
   5755 ** part of system initialization by the sqlite3_initialize() function.
   5756 ** ^The xMutexInit routine is called by SQLite exactly once for each
   5757 ** effective call to [sqlite3_initialize()].
   5758 **
   5759 ** ^The xMutexEnd method defined by this structure is invoked as
   5760 ** part of system shutdown by the sqlite3_shutdown() function. The
   5761 ** implementation of this method is expected to release all outstanding
   5762 ** resources obtained by the mutex methods implementation, especially
   5763 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
   5764 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
   5765 **
   5766 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
   5767 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
   5768 ** xMutexNotheld) implement the following interfaces (respectively):
   5769 **
   5770 ** <ul>
   5771 **   <li>  [sqlite3_mutex_alloc()] </li>
   5772 **   <li>  [sqlite3_mutex_free()] </li>
   5773 **   <li>  [sqlite3_mutex_enter()] </li>
   5774 **   <li>  [sqlite3_mutex_try()] </li>
   5775 **   <li>  [sqlite3_mutex_leave()] </li>
   5776 **   <li>  [sqlite3_mutex_held()] </li>
   5777 **   <li>  [sqlite3_mutex_notheld()] </li>
   5778 ** </ul>)^
   5779 **
   5780 ** The only difference is that the public sqlite3_XXX functions enumerated
   5781 ** above silently ignore any invocations that pass a NULL pointer instead
   5782 ** of a valid mutex handle. The implementations of the methods defined
   5783 ** by this structure are not required to handle this case, the results
   5784 ** of passing a NULL pointer instead of a valid mutex handle are undefined
   5785 ** (i.e. it is acceptable to provide an implementation that segfaults if
   5786 ** it is passed a NULL pointer).
   5787 **
   5788 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
   5789 ** invoke xMutexInit() multiple times within the same process and without
   5790 ** intervening calls to xMutexEnd().  Second and subsequent calls to
   5791 ** xMutexInit() must be no-ops.
   5792 **
   5793 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
   5794 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
   5795 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
   5796 ** memory allocation for a fast or recursive mutex.
   5797 **
   5798 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
   5799 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
   5800 ** If xMutexInit fails in any way, it is expected to clean up after itself
   5801 ** prior to returning.
   5802 */
   5803 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
   5804 struct sqlite3_mutex_methods {
   5805   int (*xMutexInit)(void);
   5806   int (*xMutexEnd)(void);
   5807   sqlite3_mutex *(*xMutexAlloc)(int);
   5808   void (*xMutexFree)(sqlite3_mutex *);
   5809   void (*xMutexEnter)(sqlite3_mutex *);
   5810   int (*xMutexTry)(sqlite3_mutex *);
   5811   void (*xMutexLeave)(sqlite3_mutex *);
   5812   int (*xMutexHeld)(sqlite3_mutex *);
   5813   int (*xMutexNotheld)(sqlite3_mutex *);
   5814 };
   5815 
   5816 /*
   5817 ** CAPI3REF: Mutex Verification Routines
   5818 **
   5819 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
   5820 ** are intended for use inside assert() statements.  ^The SQLite core
   5821 ** never uses these routines except inside an assert() and applications
   5822 ** are advised to follow the lead of the core.  ^The SQLite core only
   5823 ** provides implementations for these routines when it is compiled
   5824 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
   5825 ** are only required to provide these routines if SQLITE_DEBUG is
   5826 ** defined and if NDEBUG is not defined.
   5827 **
   5828 ** ^These routines should return true if the mutex in their argument
   5829 ** is held or not held, respectively, by the calling thread.
   5830 **
   5831 ** ^The implementation is not required to provided versions of these
   5832 ** routines that actually work. If the implementation does not provide working
   5833 ** versions of these routines, it should at least provide stubs that always
   5834 ** return true so that one does not get spurious assertion failures.
   5835 **
   5836 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
   5837 ** the routine should return 1.   This seems counter-intuitive since
   5838 ** clearly the mutex cannot be held if it does not exist.  But the
   5839 ** the reason the mutex does not exist is because the build is not
   5840 ** using mutexes.  And we do not want the assert() containing the
   5841 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
   5842 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
   5843 ** interface should also return 1 when given a NULL pointer.
   5844 */
   5845 #ifndef NDEBUG
   5846 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
   5847 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
   5848 #endif
   5849 
   5850 /*
   5851 ** CAPI3REF: Mutex Types
   5852 **
   5853 ** The [sqlite3_mutex_alloc()] interface takes a single argument
   5854 ** which is one of these integer constants.
   5855 **
   5856 ** The set of static mutexes may change from one SQLite release to the
   5857 ** next.  Applications that override the built-in mutex logic must be
   5858 ** prepared to accommodate additional static mutexes.
   5859 */
   5860 #define SQLITE_MUTEX_FAST             0
   5861 #define SQLITE_MUTEX_RECURSIVE        1
   5862 #define SQLITE_MUTEX_STATIC_MASTER    2
   5863 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
   5864 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
   5865 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
   5866 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
   5867 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
   5868 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
   5869 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
   5870 
   5871 /*
   5872 ** CAPI3REF: Retrieve the mutex for a database connection
   5873 **
   5874 ** ^This interface returns a pointer the [sqlite3_mutex] object that
   5875 ** serializes access to the [database connection] given in the argument
   5876 ** when the [threading mode] is Serialized.
   5877 ** ^If the [threading mode] is Single-thread or Multi-thread then this
   5878 ** routine returns a NULL pointer.
   5879 */
   5880 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
   5881 
   5882 /*
   5883 ** CAPI3REF: Low-Level Control Of Database Files
   5884 **
   5885 ** ^The [sqlite3_file_control()] interface makes a direct call to the
   5886 ** xFileControl method for the [sqlite3_io_methods] object associated
   5887 ** with a particular database identified by the second argument. ^The
   5888 ** name of the database is "main" for the main database or "temp" for the
   5889 ** TEMP database, or the name that appears after the AS keyword for
   5890 ** databases that are added using the [ATTACH] SQL command.
   5891 ** ^A NULL pointer can be used in place of "main" to refer to the
   5892 ** main database file.
   5893 ** ^The third and fourth parameters to this routine
   5894 ** are passed directly through to the second and third parameters of
   5895 ** the xFileControl method.  ^The return value of the xFileControl
   5896 ** method becomes the return value of this routine.
   5897 **
   5898 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
   5899 ** a pointer to the underlying [sqlite3_file] object to be written into
   5900 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
   5901 ** case is a short-circuit path which does not actually invoke the
   5902 ** underlying sqlite3_io_methods.xFileControl method.
   5903 **
   5904 ** ^If the second parameter (zDbName) does not match the name of any
   5905 ** open database file, then SQLITE_ERROR is returned.  ^This error
   5906 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
   5907 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
   5908 ** also return SQLITE_ERROR.  There is no way to distinguish between
   5909 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
   5910 ** xFileControl method.
   5911 **
   5912 ** See also: [SQLITE_FCNTL_LOCKSTATE]
   5913 */
   5914 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
   5915 
   5916 /*
   5917 ** CAPI3REF: Testing Interface
   5918 **
   5919 ** ^The sqlite3_test_control() interface is used to read out internal
   5920 ** state of SQLite and to inject faults into SQLite for testing
   5921 ** purposes.  ^The first parameter is an operation code that determines
   5922 ** the number, meaning, and operation of all subsequent parameters.
   5923 **
   5924 ** This interface is not for use by applications.  It exists solely
   5925 ** for verifying the correct operation of the SQLite library.  Depending
   5926 ** on how the SQLite library is compiled, this interface might not exist.
   5927 **
   5928 ** The details of the operation codes, their meanings, the parameters
   5929 ** they take, and what they do are all subject to change without notice.
   5930 ** Unlike most of the SQLite API, this function is not guaranteed to
   5931 ** operate consistently from one release to the next.
   5932 */
   5933 SQLITE_API int sqlite3_test_control(int op, ...);
   5934 
   5935 /*
   5936 ** CAPI3REF: Testing Interface Operation Codes
   5937 **
   5938 ** These constants are the valid operation code parameters used
   5939 ** as the first argument to [sqlite3_test_control()].
   5940 **
   5941 ** These parameters and their meanings are subject to change
   5942 ** without notice.  These values are for testing purposes only.
   5943 ** Applications should not use any of these parameters or the
   5944 ** [sqlite3_test_control()] interface.
   5945 */
   5946 #define SQLITE_TESTCTRL_FIRST                    5
   5947 #define SQLITE_TESTCTRL_PRNG_SAVE                5
   5948 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
   5949 #define SQLITE_TESTCTRL_PRNG_RESET               7
   5950 #define SQLITE_TESTCTRL_BITVEC_TEST              8
   5951 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
   5952 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
   5953 #define SQLITE_TESTCTRL_PENDING_BYTE            11
   5954 #define SQLITE_TESTCTRL_ASSERT                  12
   5955 #define SQLITE_TESTCTRL_ALWAYS                  13
   5956 #define SQLITE_TESTCTRL_RESERVE                 14
   5957 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
   5958 #define SQLITE_TESTCTRL_ISKEYWORD               16
   5959 #define SQLITE_TESTCTRL_PGHDRSZ                 17
   5960 #define SQLITE_TESTCTRL_SCRATCHMALLOC           18
   5961 #define SQLITE_TESTCTRL_LAST                    18
   5962 
   5963 /*
   5964 ** CAPI3REF: SQLite Runtime Status
   5965 **
   5966 ** ^This interface is used to retrieve runtime status information
   5967 ** about the performance of SQLite, and optionally to reset various
   5968 ** highwater marks.  ^The first argument is an integer code for
   5969 ** the specific parameter to measure.  ^(Recognized integer codes
   5970 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
   5971 ** ^The current value of the parameter is returned into *pCurrent.
   5972 ** ^The highest recorded value is returned in *pHighwater.  ^If the
   5973 ** resetFlag is true, then the highest record value is reset after
   5974 ** *pHighwater is written.  ^(Some parameters do not record the highest
   5975 ** value.  For those parameters
   5976 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
   5977 ** ^(Other parameters record only the highwater mark and not the current
   5978 ** value.  For these latter parameters nothing is written into *pCurrent.)^
   5979 **
   5980 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
   5981 ** non-zero [error code] on failure.
   5982 **
   5983 ** This routine is threadsafe but is not atomic.  This routine can be
   5984 ** called while other threads are running the same or different SQLite
   5985 ** interfaces.  However the values returned in *pCurrent and
   5986 ** *pHighwater reflect the status of SQLite at different points in time
   5987 ** and it is possible that another thread might change the parameter
   5988 ** in between the times when *pCurrent and *pHighwater are written.
   5989 **
   5990 ** See also: [sqlite3_db_status()]
   5991 */
   5992 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
   5993 
   5994 
   5995 /*
   5996 ** CAPI3REF: Status Parameters
   5997 **
   5998 ** These integer constants designate various run-time status parameters
   5999 ** that can be returned by [sqlite3_status()].
   6000 **
   6001 ** <dl>
   6002 ** ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
   6003 ** <dd>This parameter is the current amount of memory checked out
   6004 ** using [sqlite3_malloc()], either directly or indirectly.  The
   6005 ** figure includes calls made to [sqlite3_malloc()] by the application
   6006 ** and internal memory usage by the SQLite library.  Scratch memory
   6007 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
   6008 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
   6009 ** this parameter.  The amount returned is the sum of the allocation
   6010 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
   6011 **
   6012 ** ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
   6013 ** <dd>This parameter records the largest memory allocation request
   6014 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
   6015 ** internal equivalents).  Only the value returned in the
   6016 ** *pHighwater parameter to [sqlite3_status()] is of interest.
   6017 ** The value written into the *pCurrent parameter is undefined.</dd>)^
   6018 **
   6019 ** ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
   6020 ** <dd>This parameter records the number of separate memory allocations
   6021 ** currently checked out.</dd>)^
   6022 **
   6023 ** ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
   6024 ** <dd>This parameter returns the number of pages used out of the
   6025 ** [pagecache memory allocator] that was configured using
   6026 ** [SQLITE_CONFIG_PAGECACHE].  The
   6027 ** value returned is in pages, not in bytes.</dd>)^
   6028 **
   6029 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
   6030 ** <dd>This parameter returns the number of bytes of page cache
   6031 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
   6032 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
   6033 ** returned value includes allocations that overflowed because they
   6034 ** where too large (they were larger than the "sz" parameter to
   6035 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
   6036 ** no space was left in the page cache.</dd>)^
   6037 **
   6038 ** ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
   6039 ** <dd>This parameter records the largest memory allocation request
   6040 ** handed to [pagecache memory allocator].  Only the value returned in the
   6041 ** *pHighwater parameter to [sqlite3_status()] is of interest.
   6042 ** The value written into the *pCurrent parameter is undefined.</dd>)^
   6043 **
   6044 ** ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
   6045 ** <dd>This parameter returns the number of allocations used out of the
   6046 ** [scratch memory allocator] configured using
   6047 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
   6048 ** in bytes.  Since a single thread may only have one scratch allocation
   6049 ** outstanding at time, this parameter also reports the number of threads
   6050 ** using scratch memory at the same time.</dd>)^
   6051 **
   6052 ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
   6053 ** <dd>This parameter returns the number of bytes of scratch memory
   6054 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
   6055 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
   6056 ** returned include overflows because the requested allocation was too
   6057 ** larger (that is, because the requested allocation was larger than the
   6058 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
   6059 ** slots were available.
   6060 ** </dd>)^
   6061 **
   6062 ** ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
   6063 ** <dd>This parameter records the largest memory allocation request
   6064 ** handed to [scratch memory allocator].  Only the value returned in the
   6065 ** *pHighwater parameter to [sqlite3_status()] is of interest.
   6066 ** The value written into the *pCurrent parameter is undefined.</dd>)^
   6067 **
   6068 ** ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
   6069 ** <dd>This parameter records the deepest parser stack.  It is only
   6070 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
   6071 ** </dl>
   6072 **
   6073 ** New status parameters may be added from time to time.
   6074 */
   6075 #define SQLITE_STATUS_MEMORY_USED          0
   6076 #define SQLITE_STATUS_PAGECACHE_USED       1
   6077 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
   6078 #define SQLITE_STATUS_SCRATCH_USED         3
   6079 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
   6080 #define SQLITE_STATUS_MALLOC_SIZE          5
   6081 #define SQLITE_STATUS_PARSER_STACK         6
   6082 #define SQLITE_STATUS_PAGECACHE_SIZE       7
   6083 #define SQLITE_STATUS_SCRATCH_SIZE         8
   6084 #define SQLITE_STATUS_MALLOC_COUNT         9
   6085 
   6086 /*
   6087 ** CAPI3REF: Database Connection Status
   6088 **
   6089 ** ^This interface is used to retrieve runtime status information
   6090 ** about a single [database connection].  ^The first argument is the
   6091 ** database connection object to be interrogated.  ^The second argument
   6092 ** is an integer constant, taken from the set of
   6093 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
   6094 ** determines the parameter to interrogate.  The set of
   6095 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
   6096 ** to grow in future releases of SQLite.
   6097 **
   6098 ** ^The current value of the requested parameter is written into *pCur
   6099 ** and the highest instantaneous value is written into *pHiwtr.  ^If
   6100 ** the resetFlg is true, then the highest instantaneous value is
   6101 ** reset back down to the current value.
   6102 **
   6103 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
   6104 ** non-zero [error code] on failure.
   6105 **
   6106 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
   6107 */
   6108 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
   6109 
   6110 /*
   6111 ** CAPI3REF: Status Parameters for database connections
   6112 **
   6113 ** These constants are the available integer "verbs" that can be passed as
   6114 ** the second argument to the [sqlite3_db_status()] interface.
   6115 **
   6116 ** New verbs may be added in future releases of SQLite. Existing verbs
   6117 ** might be discontinued. Applications should check the return code from
   6118 ** [sqlite3_db_status()] to make sure that the call worked.
   6119 ** The [sqlite3_db_status()] interface will return a non-zero error code
   6120 ** if a discontinued or unsupported verb is invoked.
   6121 **
   6122 ** <dl>
   6123 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
   6124 ** <dd>This parameter returns the number of lookaside memory slots currently
   6125 ** checked out.</dd>)^
   6126 **
   6127 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
   6128 ** <dd>This parameter returns the number malloc attempts that were
   6129 ** satisfied using lookaside memory. Only the high-water value is meaningful;
   6130 ** the current value is always zero.)^
   6131 **
   6132 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
   6133 ** <dd>This parameter returns the number malloc attempts that might have
   6134 ** been satisfied using lookaside memory but failed due to the amount of
   6135 ** memory requested being larger than the lookaside slot size.
   6136 ** Only the high-water value is meaningful;
   6137 ** the current value is always zero.)^
   6138 **
   6139 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
   6140 ** <dd>This parameter returns the number malloc attempts that might have
   6141 ** been satisfied using lookaside memory but failed due to all lookaside
   6142 ** memory already being in use.
   6143 ** Only the high-water value is meaningful;
   6144 ** the current value is always zero.)^
   6145 **
   6146 ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
   6147 ** <dd>This parameter returns the approximate number of of bytes of heap
   6148 ** memory used by all pager caches associated with the database connection.)^
   6149 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
   6150 **
   6151 ** ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
   6152 ** <dd>This parameter returns the approximate number of of bytes of heap
   6153 ** memory used to store the schema for all databases associated
   6154 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
   6155 ** ^The full amount of memory used by the schemas is reported, even if the
   6156 ** schema memory is shared with other database connections due to
   6157 ** [shared cache mode] being enabled.
   6158 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
   6159 **
   6160 ** ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
   6161 ** <dd>This parameter returns the approximate number of of bytes of heap
   6162 ** and lookaside memory used by all prepared statements associated with
   6163 ** the database connection.)^
   6164 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
   6165 ** </dd>
   6166 ** </dl>
   6167 */
   6168 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
   6169 #define SQLITE_DBSTATUS_CACHE_USED           1
   6170 #define SQLITE_DBSTATUS_SCHEMA_USED          2
   6171 #define SQLITE_DBSTATUS_STMT_USED            3
   6172 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
   6173 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
   6174 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
   6175 #define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
   6176 
   6177 
   6178 /*
   6179 ** CAPI3REF: Prepared Statement Status
   6180 **
   6181 ** ^(Each prepared statement maintains various
   6182 ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number
   6183 ** of times it has performed specific operations.)^  These counters can
   6184 ** be used to monitor the performance characteristics of the prepared
   6185 ** statements.  For example, if the number of table steps greatly exceeds
   6186 ** the number of table searches or result rows, that would tend to indicate
   6187 ** that the prepared statement is using a full table scan rather than
   6188 ** an index.
   6189 **
   6190 ** ^(This interface is used to retrieve and reset counter values from
   6191 ** a [prepared statement].  The first argument is the prepared statement
   6192 ** object to be interrogated.  The second argument
   6193 ** is an integer code for a specific [SQLITE_STMTSTATUS_SORT | counter]
   6194 ** to be interrogated.)^
   6195 ** ^The current value of the requested counter is returned.
   6196 ** ^If the resetFlg is true, then the counter is reset to zero after this
   6197 ** interface call returns.
   6198 **
   6199 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
   6200 */
   6201 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
   6202 
   6203 /*
   6204 ** CAPI3REF: Status Parameters for prepared statements
   6205 **
   6206 ** These preprocessor macros define integer codes that name counter
   6207 ** values associated with the [sqlite3_stmt_status()] interface.
   6208 ** The meanings of the various counters are as follows:
   6209 **
   6210 ** <dl>
   6211 ** <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
   6212 ** <dd>^This is the number of times that SQLite has stepped forward in
   6213 ** a table as part of a full table scan.  Large numbers for this counter
   6214 ** may indicate opportunities for performance improvement through
   6215 ** careful use of indices.</dd>
   6216 **
   6217 ** <dt>SQLITE_STMTSTATUS_SORT</dt>
   6218 ** <dd>^This is the number of sort operations that have occurred.
   6219 ** A non-zero value in this counter may indicate an opportunity to
   6220 ** improvement performance through careful use of indices.</dd>
   6221 **
   6222 ** <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
   6223 ** <dd>^This is the number of rows inserted into transient indices that
   6224 ** were created automatically in order to help joins run faster.
   6225 ** A non-zero value in this counter may indicate an opportunity to
   6226 ** improvement performance by adding permanent indices that do not
   6227 ** need to be reinitialized each time the statement is run.</dd>
   6228 **
   6229 ** </dl>
   6230 */
   6231 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
   6232 #define SQLITE_STMTSTATUS_SORT              2
   6233 #define SQLITE_STMTSTATUS_AUTOINDEX         3
   6234 
   6235 /*
   6236 ** CAPI3REF: Custom Page Cache Object
   6237 **
   6238 ** The sqlite3_pcache type is opaque.  It is implemented by
   6239 ** the pluggable module.  The SQLite core has no knowledge of
   6240 ** its size or internal structure and never deals with the
   6241 ** sqlite3_pcache object except by holding and passing pointers
   6242 ** to the object.
   6243 **
   6244 ** See [sqlite3_pcache_methods] for additional information.
   6245 */
   6246 typedef struct sqlite3_pcache sqlite3_pcache;
   6247 
   6248 /*
   6249 ** CAPI3REF: Application Defined Page Cache.
   6250 ** KEYWORDS: {page cache}
   6251 **
   6252 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
   6253 ** register an alternative page cache implementation by passing in an
   6254 ** instance of the sqlite3_pcache_methods structure.)^
   6255 ** In many applications, most of the heap memory allocated by
   6256 ** SQLite is used for the page cache.
   6257 ** By implementing a
   6258 ** custom page cache using this API, an application can better control
   6259 ** the amount of memory consumed by SQLite, the way in which
   6260 ** that memory is allocated and released, and the policies used to
   6261 ** determine exactly which parts of a database file are cached and for
   6262 ** how long.
   6263 **
   6264 ** The alternative page cache mechanism is an
   6265 ** extreme measure that is only needed by the most demanding applications.
   6266 ** The built-in page cache is recommended for most uses.
   6267 **
   6268 ** ^(The contents of the sqlite3_pcache_methods structure are copied to an
   6269 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
   6270 ** the application may discard the parameter after the call to
   6271 ** [sqlite3_config()] returns.)^
   6272 **
   6273 ** ^(The xInit() method is called once for each effective
   6274 ** call to [sqlite3_initialize()])^
   6275 ** (usually only once during the lifetime of the process). ^(The xInit()
   6276 ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
   6277 ** The intent of the xInit() method is to set up global data structures
   6278 ** required by the custom page cache implementation.
   6279 ** ^(If the xInit() method is NULL, then the
   6280 ** built-in default page cache is used instead of the application defined
   6281 ** page cache.)^
   6282 **
   6283 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
   6284 ** It can be used to clean up
   6285 ** any outstanding resources before process shutdown, if required.
   6286 ** ^The xShutdown() method may be NULL.
   6287 **
   6288 ** ^SQLite automatically serializes calls to the xInit method,
   6289 ** so the xInit method need not be threadsafe.  ^The
   6290 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
   6291 ** not need to be threadsafe either.  All other methods must be threadsafe
   6292 ** in multithreaded applications.
   6293 **
   6294 ** ^SQLite will never invoke xInit() more than once without an intervening
   6295 ** call to xShutdown().
   6296 **
   6297 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
   6298 ** SQLite will typically create one cache instance for each open database file,
   6299 ** though this is not guaranteed. ^The
   6300 ** first parameter, szPage, is the size in bytes of the pages that must
   6301 ** be allocated by the cache.  ^szPage will not be a power of two.  ^szPage
   6302 ** will the page size of the database file that is to be cached plus an
   6303 ** increment (here called "R") of less than 250.  SQLite will use the
   6304 ** extra R bytes on each page to store metadata about the underlying
   6305 ** database page on disk.  The value of R depends
   6306 ** on the SQLite version, the target platform, and how SQLite was compiled.
   6307 ** ^(R is constant for a particular build of SQLite. Except, there are two
   6308 ** distinct values of R when SQLite is compiled with the proprietary
   6309 ** ZIPVFS extension.)^  ^The second argument to
   6310 ** xCreate(), bPurgeable, is true if the cache being created will
   6311 ** be used to cache database pages of a file stored on disk, or
   6312 ** false if it is used for an in-memory database. The cache implementation
   6313 ** does not have to do anything special based with the value of bPurgeable;
   6314 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
   6315 ** never invoke xUnpin() except to deliberately delete a page.
   6316 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
   6317 ** false will always have the "discard" flag set to true.
   6318 ** ^Hence, a cache created with bPurgeable false will
   6319 ** never contain any unpinned pages.
   6320 **
   6321 ** ^(The xCachesize() method may be called at any time by SQLite to set the
   6322 ** suggested maximum cache-size (number of pages stored by) the cache
   6323 ** instance passed as the first argument. This is the value configured using
   6324 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
   6325 ** parameter, the implementation is not required to do anything with this
   6326 ** value; it is advisory only.
   6327 **
   6328 ** The xPagecount() method must return the number of pages currently
   6329 ** stored in the cache, both pinned and unpinned.
   6330 **
   6331 ** The xFetch() method locates a page in the cache and returns a pointer to
   6332 ** the page, or a NULL pointer.
   6333 ** A "page", in this context, means a buffer of szPage bytes aligned at an
   6334 ** 8-byte boundary. The page to be fetched is determined by the key. ^The
   6335 ** mimimum key value is 1.  After it has been retrieved using xFetch, the page
   6336 ** is considered to be "pinned".
   6337 **
   6338 ** If the requested page is already in the page cache, then the page cache
   6339 ** implementation must return a pointer to the page buffer with its content
   6340 ** intact.  If the requested page is not already in the cache, then the
   6341 ** cache implementation should use the value of the createFlag
   6342 ** parameter to help it determined what action to take:
   6343 **
   6344 ** <table border=1 width=85% align=center>
   6345 ** <tr><th> createFlag <th> Behaviour when page is not already in cache
   6346 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
   6347 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
   6348 **                 Otherwise return NULL.
   6349 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
   6350 **                 NULL if allocating a new page is effectively impossible.
   6351 ** </table>
   6352 **
   6353 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
   6354 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
   6355 ** failed.)^  In between the to xFetch() calls, SQLite may
   6356 ** attempt to unpin one or more cache pages by spilling the content of
   6357 ** pinned pages to disk and synching the operating system disk cache.
   6358 **
   6359 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
   6360 ** as its second argument.  If the third parameter, discard, is non-zero,
   6361 ** then the page must be evicted from the cache.
   6362 ** ^If the discard parameter is
   6363 ** zero, then the page may be discarded or retained at the discretion of
   6364 ** page cache implementation. ^The page cache implementation
   6365 ** may choose to evict unpinned pages at any time.
   6366 **
   6367 ** The cache must not perform any reference counting. A single
   6368 ** call to xUnpin() unpins the page regardless of the number of prior calls
   6369 ** to xFetch().
   6370 **
   6371 ** The xRekey() method is used to change the key value associated with the
   6372 ** page passed as the second argument. If the cache
   6373 ** previously contains an entry associated with newKey, it must be
   6374 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
   6375 ** to be pinned.
   6376 **
   6377 ** When SQLite calls the xTruncate() method, the cache must discard all
   6378 ** existing cache entries with page numbers (keys) greater than or equal
   6379 ** to the value of the iLimit parameter passed to xTruncate(). If any
   6380 ** of these pages are pinned, they are implicitly unpinned, meaning that
   6381 ** they can be safely discarded.
   6382 **
   6383 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
   6384 ** All resources associated with the specified cache should be freed. ^After
   6385 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
   6386 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
   6387 ** functions.
   6388 */
   6389 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
   6390 struct sqlite3_pcache_methods {
   6391   void *pArg;
   6392   int (*xInit)(void*);
   6393   void (*xShutdown)(void*);
   6394   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
   6395   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
   6396   int (*xPagecount)(sqlite3_pcache*);
   6397   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
   6398   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
   6399   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
   6400   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
   6401   void (*xDestroy)(sqlite3_pcache*);
   6402 };
   6403 
   6404 /*
   6405 ** CAPI3REF: Online Backup Object
   6406 **
   6407 ** The sqlite3_backup object records state information about an ongoing
   6408 ** online backup operation.  ^The sqlite3_backup object is created by
   6409 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
   6410 ** [sqlite3_backup_finish()].
   6411 **
   6412 ** See Also: [Using the SQLite Online Backup API]
   6413 */
   6414 typedef struct sqlite3_backup sqlite3_backup;
   6415 
   6416 /*
   6417 ** CAPI3REF: Online Backup API.
   6418 **
   6419 ** The backup API copies the content of one database into another.
   6420 ** It is useful either for creating backups of databases or
   6421 ** for copying in-memory databases to or from persistent files.
   6422 **
   6423 ** See Also: [Using the SQLite Online Backup API]
   6424 **
   6425 ** ^SQLite holds a write transaction open on the destination database file
   6426 ** for the duration of the backup operation.
   6427 ** ^The source database is read-locked only while it is being read;
   6428 ** it is not locked continuously for the entire backup operation.
   6429 ** ^Thus, the backup may be performed on a live source database without
   6430 ** preventing other database connections from
   6431 ** reading or writing to the source database while the backup is underway.
   6432 **
   6433 ** ^(To perform a backup operation:
   6434 **   <ol>
   6435 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
   6436 **         backup,
   6437 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
   6438 **         the data between the two databases, and finally
   6439 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources
   6440 **         associated with the backup operation.
   6441 **   </ol>)^
   6442 ** There should be exactly one call to sqlite3_backup_finish() for each
   6443 ** successful call to sqlite3_backup_init().
   6444 **
   6445 ** <b>sqlite3_backup_init()</b>
   6446 **
   6447 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
   6448 ** [database connection] associated with the destination database
   6449 ** and the database name, respectively.
   6450 ** ^The database name is "main" for the main database, "temp" for the
   6451 ** temporary database, or the name specified after the AS keyword in
   6452 ** an [ATTACH] statement for an attached database.
   6453 ** ^The S and M arguments passed to
   6454 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
   6455 ** and database name of the source database, respectively.
   6456 ** ^The source and destination [database connections] (parameters S and D)
   6457 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
   6458 ** an error.
   6459 **
   6460 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
   6461 ** returned and an error code and error message are stored in the
   6462 ** destination [database connection] D.
   6463 ** ^The error code and message for the failed call to sqlite3_backup_init()
   6464 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
   6465 ** [sqlite3_errmsg16()] functions.
   6466 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
   6467 ** [sqlite3_backup] object.
   6468 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
   6469 ** sqlite3_backup_finish() functions to perform the specified backup
   6470 ** operation.
   6471 **
   6472 ** <b>sqlite3_backup_step()</b>
   6473 **
   6474 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
   6475 ** the source and destination databases specified by [sqlite3_backup] object B.
   6476 ** ^If N is negative, all remaining source pages are copied.
   6477 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
   6478 ** are still more pages to be copied, then the function returns [SQLITE_OK].
   6479 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
   6480 ** from source to destination, then it returns [SQLITE_DONE].
   6481 ** ^If an error occurs while running sqlite3_backup_step(B,N),
   6482 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
   6483 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
   6484 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
   6485 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
   6486 **
   6487 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
   6488 ** <ol>
   6489 ** <li> the destination database was opened read-only, or
   6490 ** <li> the destination database is using write-ahead-log journaling
   6491 ** and the destination and source page sizes differ, or
   6492 ** <li> the destination database is an in-memory database and the
   6493 ** destination and source page sizes differ.
   6494 ** </ol>)^
   6495 **
   6496 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
   6497 ** the [sqlite3_busy_handler | busy-handler function]
   6498 ** is invoked (if one is specified). ^If the
   6499 ** busy-handler returns non-zero before the lock is available, then
   6500 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
   6501 ** sqlite3_backup_step() can be retried later. ^If the source
   6502 ** [database connection]
   6503 ** is being used to write to the source database when sqlite3_backup_step()
   6504 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
   6505 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
   6506 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
   6507 ** [SQLITE_READONLY] is returned, then
   6508 ** there is no point in retrying the call to sqlite3_backup_step(). These
   6509 ** errors are considered fatal.)^  The application must accept
   6510 ** that the backup operation has failed and pass the backup operation handle
   6511 ** to the sqlite3_backup_finish() to release associated resources.
   6512 **
   6513 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
   6514 ** on the destination file. ^The exclusive lock is not released until either
   6515 ** sqlite3_backup_finish() is called or the backup operation is complete
   6516 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
   6517 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
   6518 ** lasts for the duration of the sqlite3_backup_step() call.
   6519 ** ^Because the source database is not locked between calls to
   6520 ** sqlite3_backup_step(), the source database may be modified mid-way
   6521 ** through the backup process.  ^If the source database is modified by an
   6522 ** external process or via a database connection other than the one being
   6523 ** used by the backup operation, then the backup will be automatically
   6524 ** restarted by the next call to sqlite3_backup_step(). ^If the source
   6525 ** database is modified by the using the same database connection as is used
   6526 ** by the backup operation, then the backup database is automatically
   6527 ** updated at the same time.
   6528 **
   6529 ** <b>sqlite3_backup_finish()</b>
   6530 **
   6531 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
   6532 ** application wishes to abandon the backup operation, the application
   6533 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
   6534 ** ^The sqlite3_backup_finish() interfaces releases all
   6535 ** resources associated with the [sqlite3_backup] object.
   6536 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
   6537 ** active write-transaction on the destination database is rolled back.
   6538 ** The [sqlite3_backup] object is invalid
   6539 ** and may not be used following a call to sqlite3_backup_finish().
   6540 **
   6541 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
   6542 ** sqlite3_backup_step() errors occurred, regardless or whether or not
   6543 ** sqlite3_backup_step() completed.
   6544 ** ^If an out-of-memory condition or IO error occurred during any prior
   6545 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
   6546 ** sqlite3_backup_finish() returns the corresponding [error code].
   6547 **
   6548 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
   6549 ** is not a permanent error and does not affect the return value of
   6550 ** sqlite3_backup_finish().
   6551 **
   6552 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
   6553 **
   6554 ** ^Each call to sqlite3_backup_step() sets two values inside
   6555 ** the [sqlite3_backup] object: the number of pages still to be backed
   6556 ** up and the total number of pages in the source database file.
   6557 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
   6558 ** retrieve these two values, respectively.
   6559 **
   6560 ** ^The values returned by these functions are only updated by
   6561 ** sqlite3_backup_step(). ^If the source database is modified during a backup
   6562 ** operation, then the values are not updated to account for any extra
   6563 ** pages that need to be updated or the size of the source database file
   6564 ** changing.
   6565 **
   6566 ** <b>Concurrent Usage of Database Handles</b>
   6567 **
   6568 ** ^The source [database connection] may be used by the application for other
   6569 ** purposes while a backup operation is underway or being initialized.
   6570 ** ^If SQLite is compiled and configured to support threadsafe database
   6571 ** connections, then the source database connection may be used concurrently
   6572 ** from within other threads.
   6573 **
   6574 ** However, the application must guarantee that the destination
   6575 ** [database connection] is not passed to any other API (by any thread) after
   6576 ** sqlite3_backup_init() is called and before the corresponding call to
   6577 ** sqlite3_backup_finish().  SQLite does not currently check to see
   6578 ** if the application incorrectly accesses the destination [database connection]
   6579 ** and so no error code is reported, but the operations may malfunction
   6580 ** nevertheless.  Use of the destination database connection while a
   6581 ** backup is in progress might also also cause a mutex deadlock.
   6582 **
   6583 ** If running in [shared cache mode], the application must
   6584 ** guarantee that the shared cache used by the destination database
   6585 ** is not accessed while the backup is running. In practice this means
   6586 ** that the application must guarantee that the disk file being
   6587 ** backed up to is not accessed by any connection within the process,
   6588 ** not just the specific connection that was passed to sqlite3_backup_init().
   6589 **
   6590 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
   6591 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
   6592 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
   6593 ** APIs are not strictly speaking threadsafe. If they are invoked at the
   6594 ** same time as another thread is invoking sqlite3_backup_step() it is
   6595 ** possible that they return invalid values.
   6596 */
   6597 SQLITE_API sqlite3_backup *sqlite3_backup_init(
   6598   sqlite3 *pDest,                        /* Destination database handle */
   6599   const char *zDestName,                 /* Destination database name */
   6600   sqlite3 *pSource,                      /* Source database handle */
   6601   const char *zSourceName                /* Source database name */
   6602 );
   6603 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
   6604 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
   6605 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
   6606 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
   6607 
   6608 /*
   6609 ** CAPI3REF: Unlock Notification
   6610 **
   6611 ** ^When running in shared-cache mode, a database operation may fail with
   6612 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
   6613 ** individual tables within the shared-cache cannot be obtained. See
   6614 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
   6615 ** ^This API may be used to register a callback that SQLite will invoke
   6616 ** when the connection currently holding the required lock relinquishes it.
   6617 ** ^This API is only available if the library was compiled with the
   6618 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
   6619 **
   6620 ** See Also: [Using the SQLite Unlock Notification Feature].
   6621 **
   6622 ** ^Shared-cache locks are released when a database connection concludes
   6623 ** its current transaction, either by committing it or rolling it back.
   6624 **
   6625 ** ^When a connection (known as the blocked connection) fails to obtain a
   6626 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
   6627 ** identity of the database connection (the blocking connection) that
   6628 ** has locked the required resource is stored internally. ^After an
   6629 ** application receives an SQLITE_LOCKED error, it may call the
   6630 ** sqlite3_unlock_notify() method with the blocked connection handle as
   6631 ** the first argument to register for a callback that will be invoked
   6632 ** when the blocking connections current transaction is concluded. ^The
   6633 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
   6634 ** call that concludes the blocking connections transaction.
   6635 **
   6636 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
   6637 ** there is a chance that the blocking connection will have already
   6638 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
   6639 ** If this happens, then the specified callback is invoked immediately,
   6640 ** from within the call to sqlite3_unlock_notify().)^
   6641 **
   6642 ** ^If the blocked connection is attempting to obtain a write-lock on a
   6643 ** shared-cache table, and more than one other connection currently holds
   6644 ** a read-lock on the same table, then SQLite arbitrarily selects one of
   6645 ** the other connections to use as the blocking connection.
   6646 **
   6647 ** ^(There may be at most one unlock-notify callback registered by a
   6648 ** blocked connection. If sqlite3_unlock_notify() is called when the
   6649 ** blocked connection already has a registered unlock-notify callback,
   6650 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
   6651 ** called with a NULL pointer as its second argument, then any existing
   6652 ** unlock-notify callback is canceled. ^The blocked connections
   6653 ** unlock-notify callback may also be canceled by closing the blocked
   6654 ** connection using [sqlite3_close()].
   6655 **
   6656 ** The unlock-notify callback is not reentrant. If an application invokes
   6657 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
   6658 ** crash or deadlock may be the result.
   6659 **
   6660 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
   6661 ** returns SQLITE_OK.
   6662 **
   6663 ** <b>Callback Invocation Details</b>
   6664 **
   6665 ** When an unlock-notify callback is registered, the application provides a
   6666 ** single void* pointer that is passed to the callback when it is invoked.
   6667 ** However, the signature of the callback function allows SQLite to pass
   6668 ** it an array of void* context pointers. The first argument passed to
   6669 ** an unlock-notify callback is a pointer to an array of void* pointers,
   6670 ** and the second is the number of entries in the array.
   6671 **
   6672 ** When a blocking connections transaction is concluded, there may be
   6673 ** more than one blocked connection that has registered for an unlock-notify
   6674 ** callback. ^If two or more such blocked connections have specified the
   6675 ** same callback function, then instead of invoking the callback function
   6676 ** multiple times, it is invoked once with the set of void* context pointers
   6677 ** specified by the blocked connections bundled together into an array.
   6678 ** This gives the application an opportunity to prioritize any actions
   6679 ** related to the set of unblocked database connections.
   6680 **
   6681 ** <b>Deadlock Detection</b>
   6682 **
   6683 ** Assuming that after registering for an unlock-notify callback a
   6684 ** database waits for the callback to be issued before taking any further
   6685 ** action (a reasonable assumption), then using this API may cause the
   6686 ** application to deadlock. For example, if connection X is waiting for
   6687 ** connection Y's transaction to be concluded, and similarly connection
   6688 ** Y is waiting on connection X's transaction, then neither connection
   6689 ** will proceed and the system may remain deadlocked indefinitely.
   6690 **
   6691 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
   6692 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
   6693 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
   6694 ** unlock-notify callback is registered. The system is said to be in
   6695 ** a deadlocked state if connection A has registered for an unlock-notify
   6696 ** callback on the conclusion of connection B's transaction, and connection
   6697 ** B has itself registered for an unlock-notify callback when connection
   6698 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
   6699 ** the system is also considered to be deadlocked if connection B has
   6700 ** registered for an unlock-notify callback on the conclusion of connection
   6701 ** C's transaction, where connection C is waiting on connection A. ^Any
   6702 ** number of levels of indirection are allowed.
   6703 **
   6704 ** <b>The "DROP TABLE" Exception</b>
   6705 **
   6706 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
   6707 ** always appropriate to call sqlite3_unlock_notify(). There is however,
   6708 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
   6709 ** SQLite checks if there are any currently executing SELECT statements
   6710 ** that belong to the same connection. If there are, SQLITE_LOCKED is
   6711 ** returned. In this case there is no "blocking connection", so invoking
   6712 ** sqlite3_unlock_notify() results in the unlock-notify callback being
   6713 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
   6714 ** or "DROP INDEX" query, an infinite loop might be the result.
   6715 **
   6716 ** One way around this problem is to check the extended error code returned
   6717 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
   6718 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
   6719 ** the special "DROP TABLE/INDEX" case, the extended error code is just
   6720 ** SQLITE_LOCKED.)^
   6721 */
   6722 SQLITE_API int sqlite3_unlock_notify(
   6723   sqlite3 *pBlocked,                          /* Waiting connection */
   6724   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
   6725   void *pNotifyArg                            /* Argument to pass to xNotify */
   6726 );
   6727 
   6728 
   6729 /*
   6730 ** CAPI3REF: String Comparison
   6731 **
   6732 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
   6733 ** compare the contents of two buffers containing UTF-8 strings in a
   6734 ** case-independent fashion, using the same definition of case independence
   6735 ** that SQLite uses internally when comparing identifiers.
   6736 */
   6737 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
   6738 
   6739 /*
   6740 ** CAPI3REF: Error Logging Interface
   6741 **
   6742 ** ^The [sqlite3_log()] interface writes a message into the error log
   6743 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
   6744 ** ^If logging is enabled, the zFormat string and subsequent arguments are
   6745 ** used with [sqlite3_snprintf()] to generate the final output string.
   6746 **
   6747 ** The sqlite3_log() interface is intended for use by extensions such as
   6748 ** virtual tables, collating functions, and SQL functions.  While there is
   6749 ** nothing to prevent an application from calling sqlite3_log(), doing so
   6750 ** is considered bad form.
   6751 **
   6752 ** The zFormat string must not be NULL.
   6753 **
   6754 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
   6755 ** will not use dynamically allocated memory.  The log message is stored in
   6756 ** a fixed-length buffer on the stack.  If the log message is longer than
   6757 ** a few hundred characters, it will be truncated to the length of the
   6758 ** buffer.
   6759 */
   6760 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
   6761 
   6762 /*
   6763 ** CAPI3REF: Write-Ahead Log Commit Hook
   6764 **
   6765 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
   6766 ** will be invoked each time a database connection commits data to a
   6767 ** [write-ahead log] (i.e. whenever a transaction is committed in
   6768 ** [journal_mode | journal_mode=WAL mode]).
   6769 **
   6770 ** ^The callback is invoked by SQLite after the commit has taken place and
   6771 ** the associated write-lock on the database released, so the implementation
   6772 ** may read, write or [checkpoint] the database as required.
   6773 **
   6774 ** ^The first parameter passed to the callback function when it is invoked
   6775 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
   6776 ** registering the callback. ^The second is a copy of the database handle.
   6777 ** ^The third parameter is the name of the database that was written to -
   6778 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
   6779 ** is the number of pages currently in the write-ahead log file,
   6780 ** including those that were just committed.
   6781 **
   6782 ** The callback function should normally return [SQLITE_OK].  ^If an error
   6783 ** code is returned, that error will propagate back up through the
   6784 ** SQLite code base to cause the statement that provoked the callback
   6785 ** to report an error, though the commit will have still occurred. If the
   6786 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
   6787 ** that does not correspond to any valid SQLite error code, the results
   6788 ** are undefined.
   6789 **
   6790 ** A single database handle may have at most a single write-ahead log callback
   6791 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
   6792 ** previously registered write-ahead log callback. ^Note that the
   6793 ** [sqlite3_wal_autocheckpoint()] interface and the
   6794 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
   6795 ** those overwrite any prior [sqlite3_wal_hook()] settings.
   6796 */
   6797 SQLITE_API void *sqlite3_wal_hook(
   6798   sqlite3*,
   6799   int(*)(void *,sqlite3*,const char*,int),
   6800   void*
   6801 );
   6802 
   6803 /*
   6804 ** CAPI3REF: Configure an auto-checkpoint
   6805 **
   6806 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
   6807 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
   6808 ** to automatically [checkpoint]
   6809 ** after committing a transaction if there are N or
   6810 ** more frames in the [write-ahead log] file.  ^Passing zero or
   6811 ** a negative value as the nFrame parameter disables automatic
   6812 ** checkpoints entirely.
   6813 **
   6814 ** ^The callback registered by this function replaces any existing callback
   6815 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
   6816 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
   6817 ** configured by this function.
   6818 **
   6819 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
   6820 ** from SQL.
   6821 **
   6822 ** ^Every new [database connection] defaults to having the auto-checkpoint
   6823 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
   6824 ** pages.  The use of this interface
   6825 ** is only necessary if the default setting is found to be suboptimal
   6826 ** for a particular application.
   6827 */
   6828 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
   6829 
   6830 /*
   6831 ** CAPI3REF: Checkpoint a database
   6832 **
   6833 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
   6834 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
   6835 ** empty string, then a checkpoint is run on all databases of
   6836 ** connection D.  ^If the database connection D is not in
   6837 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
   6838 **
   6839 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
   6840 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
   6841 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
   6842 ** run whenever the WAL reaches a certain size threshold.
   6843 **
   6844 ** See also: [sqlite3_wal_checkpoint_v2()]
   6845 */
   6846 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
   6847 
   6848 /*
   6849 ** CAPI3REF: Checkpoint a database
   6850 **
   6851 ** Run a checkpoint operation on WAL database zDb attached to database
   6852 ** handle db. The specific operation is determined by the value of the
   6853 ** eMode parameter:
   6854 **
   6855 ** <dl>
   6856 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
   6857 **   Checkpoint as many frames as possible without waiting for any database
   6858 **   readers or writers to finish. Sync the db file if all frames in the log
   6859 **   are checkpointed. This mode is the same as calling
   6860 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
   6861 **
   6862 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
   6863 **   This mode blocks (calls the busy-handler callback) until there is no
   6864 **   database writer and all readers are reading from the most recent database
   6865 **   snapshot. It then checkpoints all frames in the log file and syncs the
   6866 **   database file. This call blocks database writers while it is running,
   6867 **   but not database readers.
   6868 **
   6869 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
   6870 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
   6871 **   checkpointing the log file it blocks (calls the busy-handler callback)
   6872 **   until all readers are reading from the database file only. This ensures
   6873 **   that the next client to write to the database file restarts the log file
   6874 **   from the beginning. This call blocks database writers while it is running,
   6875 **   but not database readers.
   6876 ** </dl>
   6877 **
   6878 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
   6879 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
   6880 ** the total number of checkpointed frames (including any that were already
   6881 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
   6882 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
   6883 ** If no values are available because of an error, they are both set to -1
   6884 ** before returning to communicate this to the caller.
   6885 **
   6886 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
   6887 ** any other process is running a checkpoint operation at the same time, the
   6888 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
   6889 ** busy-handler configured, it will not be invoked in this case.
   6890 **
   6891 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
   6892 ** "writer" lock on the database file. If the writer lock cannot be obtained
   6893 ** immediately, and a busy-handler is configured, it is invoked and the writer
   6894 ** lock retried until either the busy-handler returns 0 or the lock is
   6895 ** successfully obtained. The busy-handler is also invoked while waiting for
   6896 ** database readers as described above. If the busy-handler returns 0 before
   6897 ** the writer lock is obtained or while waiting for database readers, the
   6898 ** checkpoint operation proceeds from that point in the same way as
   6899 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
   6900 ** without blocking any further. SQLITE_BUSY is returned in this case.
   6901 **
   6902 ** If parameter zDb is NULL or points to a zero length string, then the
   6903 ** specified operation is attempted on all WAL databases. In this case the
   6904 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
   6905 ** an SQLITE_BUSY error is encountered when processing one or more of the
   6906 ** attached WAL databases, the operation is still attempted on any remaining
   6907 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
   6908 ** error occurs while processing an attached database, processing is abandoned
   6909 ** and the error code returned to the caller immediately. If no error
   6910 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
   6911 ** databases, SQLITE_OK is returned.
   6912 **
   6913 ** If database zDb is the name of an attached database that is not in WAL
   6914 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
   6915 ** zDb is not NULL (or a zero length string) and is not the name of any
   6916 ** attached database, SQLITE_ERROR is returned to the caller.
   6917 */
   6918 SQLITE_API int sqlite3_wal_checkpoint_v2(
   6919   sqlite3 *db,                    /* Database handle */
   6920   const char *zDb,                /* Name of attached database (or NULL) */
   6921   int eMode,                      /* SQLITE_CHECKPOINT_* value */
   6922   int *pnLog,                     /* OUT: Size of WAL log in frames */
   6923   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
   6924 );
   6925 
   6926 /*
   6927 ** CAPI3REF: Checkpoint operation parameters
   6928 **
   6929 ** These constants can be used as the 3rd parameter to
   6930 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
   6931 ** documentation for additional information about the meaning and use of
   6932 ** each of these values.
   6933 */
   6934 #define SQLITE_CHECKPOINT_PASSIVE 0
   6935 #define SQLITE_CHECKPOINT_FULL    1
   6936 #define SQLITE_CHECKPOINT_RESTART 2
   6937 
   6938 
   6939 /* Begin recover.patch for Chromium */
   6940 /*
   6941 ** Call to initialize the recover virtual-table modules (see recover.c).
   6942 **
   6943 ** This could be loaded by default in main.c, but that would make the
   6944 ** virtual table available to Web SQL.  Breaking it out allows only
   6945 ** selected users to enable it (currently sql/recovery.cc).
   6946 */
   6947 int recoverVtableInit(sqlite3 *db);
   6948 /* End recover.patch for Chromium */
   6949 
   6950 /*
   6951 ** Undo the hack that converts floating point types to integer for
   6952 ** builds on processors without floating point support.
   6953 */
   6954 #ifdef SQLITE_OMIT_FLOATING_POINT
   6955 # undef double
   6956 #endif
   6957 
   6958 #if 0
   6959 }  /* End of the 'extern "C"' block */
   6960 #endif
   6961 #endif
   6962 
   6963 /*
   6964 ** 2010 August 30
   6965 **
   6966 ** The author disclaims copyright to this source code.  In place of
   6967 ** a legal notice, here is a blessing:
   6968 **
   6969 **    May you do good and not evil.
   6970 **    May you find forgiveness for yourself and forgive others.
   6971 **    May you share freely, never taking more than you give.
   6972 **
   6973 *************************************************************************
   6974 */
   6975 
   6976 #ifndef _SQLITE3RTREE_H_
   6977 #define _SQLITE3RTREE_H_
   6978 
   6979 
   6980 #if 0
   6981 extern "C" {
   6982 #endif
   6983 
   6984 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
   6985 
   6986 /*
   6987 ** Register a geometry callback named zGeom that can be used as part of an
   6988 ** R-Tree geometry query as follows:
   6989 **
   6990 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
   6991 */
   6992 SQLITE_API int sqlite3_rtree_geometry_callback(
   6993   sqlite3 *db,
   6994   const char *zGeom,
   6995   int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
   6996   void *pContext
   6997 );
   6998 
   6999 
   7000 /*
   7001 ** A pointer to a structure of the following type is passed as the first
   7002 ** argument to callbacks registered using rtree_geometry_callback().
   7003 */
   7004 struct sqlite3_rtree_geometry {
   7005   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
   7006   int nParam;                     /* Size of array aParam[] */
   7007   double *aParam;                 /* Parameters passed to SQL geom function */
   7008   void *pUser;                    /* Callback implementation user data */
   7009   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
   7010 };
   7011 
   7012 
   7013 #if 0
   7014 }  /* end of the 'extern "C"' block */
   7015 #endif
   7016 
   7017 #endif  /* ifndef _SQLITE3RTREE_H_ */
   7018 
   7019 
   7020 /************** End of sqlite3.h *********************************************/
   7021 /************** Continuing where we left off in sqliteInt.h ******************/
   7022 /************** Include hash.h in the middle of sqliteInt.h ******************/
   7023 /************** Begin file hash.h ********************************************/
   7024 /*
   7025 ** 2001 September 22
   7026 **
   7027 ** The author disclaims copyright to this source code.  In place of
   7028 ** a legal notice, here is a blessing:
   7029 **
   7030 **    May you do good and not evil.
   7031 **    May you find forgiveness for yourself and forgive others.
   7032 **    May you share freely, never taking more than you give.
   7033 **
   7034 *************************************************************************
   7035 ** This is the header file for the generic hash-table implemenation
   7036 ** used in SQLite.
   7037 */
   7038 #ifndef _SQLITE_HASH_H_
   7039 #define _SQLITE_HASH_H_
   7040 
   7041 /* Forward declarations of structures. */
   7042 typedef struct Hash Hash;
   7043 typedef struct HashElem HashElem;
   7044 
   7045 /* A complete hash table is an instance of the following structure.
   7046 ** The internals of this structure are intended to be opaque -- client
   7047 ** code should not attempt to access or modify the fields of this structure
   7048 ** directly.  Change this structure only by using the routines below.
   7049 ** However, some of the "procedures" and "functions" for modifying and
   7050 ** accessing this structure are really macros, so we can't really make
   7051 ** this structure opaque.
   7052 **
   7053 ** All elements of the hash table are on a single doubly-linked list.
   7054 ** Hash.first points to the head of this list.
   7055 **
   7056 ** There are Hash.htsize buckets.  Each bucket points to a spot in
   7057 ** the global doubly-linked list.  The contents of the bucket are the
   7058 ** element pointed to plus the next _ht.count-1 elements in the list.
   7059 **
   7060 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
   7061 ** by a linear search of the global list.  For small tables, the
   7062 ** Hash.ht table is never allocated because if there are few elements
   7063 ** in the table, it is faster to do a linear search than to manage
   7064 ** the hash table.
   7065 */
   7066 struct Hash {
   7067   unsigned int htsize;      /* Number of buckets in the hash table */
   7068   unsigned int count;       /* Number of entries in this table */
   7069   HashElem *first;          /* The first element of the array */
   7070   struct _ht {              /* the hash table */
   7071     int count;                 /* Number of entries with this hash */
   7072     HashElem *chain;           /* Pointer to first entry with this hash */
   7073   } *ht;
   7074 };
   7075 
   7076 /* Each element in the hash table is an instance of the following
   7077 ** structure.  All elements are stored on a single doubly-linked list.
   7078 **
   7079 ** Again, this structure is intended to be opaque, but it can't really
   7080 ** be opaque because it is used by macros.
   7081 */
   7082 struct HashElem {
   7083   HashElem *next, *prev;       /* Next and previous elements in the table */
   7084   void *data;                  /* Data associated with this element */
   7085   const char *pKey; int nKey;  /* Key associated with this element */
   7086 };
   7087 
   7088 /*
   7089 ** Access routines.  To delete, insert a NULL pointer.
   7090 */
   7091 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
   7092 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
   7093 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
   7094 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
   7095 
   7096 /*
   7097 ** Macros for looping over all elements of a hash table.  The idiom is
   7098 ** like this:
   7099 **
   7100 **   Hash h;
   7101 **   HashElem *p;
   7102 **   ...
   7103 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
   7104 **     SomeStructure *pData = sqliteHashData(p);
   7105 **     // do something with pData
   7106 **   }
   7107 */
   7108 #define sqliteHashFirst(H)  ((H)->first)
   7109 #define sqliteHashNext(E)   ((E)->next)
   7110 #define sqliteHashData(E)   ((E)->data)
   7111 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
   7112 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
   7113 
   7114 /*
   7115 ** Number of entries in a hash table
   7116 */
   7117 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
   7118 
   7119 #endif /* _SQLITE_HASH_H_ */
   7120 
   7121 /************** End of hash.h ************************************************/
   7122 /************** Continuing where we left off in sqliteInt.h ******************/
   7123 /************** Include parse.h in the middle of sqliteInt.h *****************/
   7124 /************** Begin file parse.h *******************************************/
   7125 #define TK_SEMI                            1
   7126 #define TK_EXPLAIN                         2
   7127 #define TK_QUERY                           3
   7128 #define TK_PLAN                            4
   7129 #define TK_BEGIN                           5
   7130 #define TK_TRANSACTION                     6
   7131 #define TK_DEFERRED                        7
   7132 #define TK_IMMEDIATE                       8
   7133 #define TK_EXCLUSIVE                       9
   7134 #define TK_COMMIT                         10
   7135 #define TK_END                            11
   7136 #define TK_ROLLBACK                       12
   7137 #define TK_SAVEPOINT                      13
   7138 #define TK_RELEASE                        14
   7139 #define TK_TO                             15
   7140 #define TK_TABLE                          16
   7141 #define TK_CREATE                         17
   7142 #define TK_IF                             18
   7143 #define TK_NOT                            19
   7144 #define TK_EXISTS                         20
   7145 #define TK_TEMP                           21
   7146 #define TK_LP                             22
   7147 #define TK_RP                             23
   7148 #define TK_AS                             24
   7149 #define TK_COMMA                          25
   7150 #define TK_ID                             26
   7151 #define TK_INDEXED                        27
   7152 #define TK_ABORT                          28
   7153 #define TK_ACTION                         29
   7154 #define TK_AFTER                          30
   7155 #define TK_ANALYZE                        31
   7156 #define TK_ASC                            32
   7157 #define TK_ATTACH                         33
   7158 #define TK_BEFORE                         34
   7159 #define TK_BY                             35
   7160 #define TK_CASCADE                        36
   7161 #define TK_CAST                           37
   7162 #define TK_COLUMNKW                       38
   7163 #define TK_CONFLICT                       39
   7164 #define TK_DATABASE                       40
   7165 #define TK_DESC                           41
   7166 #define TK_DETACH                         42
   7167 #define TK_EACH                           43
   7168 #define TK_FAIL                           44
   7169 #define TK_FOR                            45
   7170 #define TK_IGNORE                         46
   7171 #define TK_INITIALLY                      47
   7172 #define TK_INSTEAD                        48
   7173 #define TK_LIKE_KW                        49
   7174 #define TK_MATCH                          50
   7175 #define TK_NO                             51
   7176 #define TK_KEY                            52
   7177 #define TK_OF                             53
   7178 #define TK_OFFSET                         54
   7179 #define TK_PRAGMA                         55
   7180 #define TK_RAISE                          56
   7181 #define TK_REPLACE                        57
   7182 #define TK_RESTRICT                       58
   7183 #define TK_ROW                            59
   7184 #define TK_TRIGGER                        60
   7185 #define TK_VACUUM                         61
   7186 #define TK_VIEW                           62
   7187 #define TK_VIRTUAL                        63
   7188 #define TK_REINDEX                        64
   7189 #define TK_RENAME                         65
   7190 #define TK_CTIME_KW                       66
   7191 #define TK_ANY                            67
   7192 #define TK_OR                             68
   7193 #define TK_AND                            69
   7194 #define TK_IS                             70
   7195 #define TK_BETWEEN                        71
   7196 #define TK_IN                             72
   7197 #define TK_ISNULL                         73
   7198 #define TK_NOTNULL                        74
   7199 #define TK_NE                             75
   7200 #define TK_EQ                             76
   7201 #define TK_GT                             77
   7202 #define TK_LE                             78
   7203 #define TK_LT                             79
   7204 #define TK_GE                             80
   7205 #define TK_ESCAPE                         81
   7206 #define TK_BITAND                         82
   7207 #define TK_BITOR                          83
   7208 #define TK_LSHIFT                         84
   7209 #define TK_RSHIFT                         85
   7210 #define TK_PLUS                           86
   7211 #define TK_MINUS                          87
   7212 #define TK_STAR                           88
   7213 #define TK_SLASH                          89
   7214 #define TK_REM                            90
   7215 #define TK_CONCAT                         91
   7216 #define TK_COLLATE                        92
   7217 #define TK_BITNOT                         93
   7218 #define TK_STRING                         94
   7219 #define TK_JOIN_KW                        95
   7220 #define TK_CONSTRAINT                     96
   7221 #define TK_DEFAULT                        97
   7222 #define TK_NULL                           98
   7223 #define TK_PRIMARY                        99
   7224 #define TK_UNIQUE                         100
   7225 #define TK_CHECK                          101
   7226 #define TK_REFERENCES                     102
   7227 #define TK_AUTOINCR                       103
   7228 #define TK_ON                             104
   7229 #define TK_INSERT                         105
   7230 #define TK_DELETE                         106
   7231 #define TK_UPDATE                         107
   7232 #define TK_SET                            108
   7233 #define TK_DEFERRABLE                     109
   7234 #define TK_FOREIGN                        110
   7235 #define TK_DROP                           111
   7236 #define TK_UNION                          112
   7237 #define TK_ALL                            113
   7238 #define TK_EXCEPT                         114
   7239 #define TK_INTERSECT                      115
   7240 #define TK_SELECT                         116
   7241 #define TK_DISTINCT                       117
   7242 #define TK_DOT                            118
   7243 #define TK_FROM                           119
   7244 #define TK_JOIN                           120
   7245 #define TK_USING                          121
   7246 #define TK_ORDER                          122
   7247 #define TK_GROUP                          123
   7248 #define TK_HAVING                         124
   7249 #define TK_LIMIT                          125
   7250 #define TK_WHERE                          126
   7251 #define TK_INTO                           127
   7252 #define TK_VALUES                         128
   7253 #define TK_INTEGER                        129
   7254 #define TK_FLOAT                          130
   7255 #define TK_BLOB                           131
   7256 #define TK_REGISTER                       132
   7257 #define TK_VARIABLE                       133
   7258 #define TK_CASE                           134
   7259 #define TK_WHEN                           135
   7260 #define TK_THEN                           136
   7261 #define TK_ELSE                           137
   7262 #define TK_INDEX                          138
   7263 #define TK_ALTER                          139
   7264 #define TK_ADD                            140
   7265 #define TK_TO_TEXT                        141
   7266 #define TK_TO_BLOB                        142
   7267 #define TK_TO_NUMERIC                     143
   7268 #define TK_TO_INT                         144
   7269 #define TK_TO_REAL                        145
   7270 #define TK_ISNOT                          146
   7271 #define TK_END_OF_FILE                    147
   7272 #define TK_ILLEGAL                        148
   7273 #define TK_SPACE                          149
   7274 #define TK_UNCLOSED_STRING                150
   7275 #define TK_FUNCTION                       151
   7276 #define TK_COLUMN                         152
   7277 #define TK_AGG_FUNCTION                   153
   7278 #define TK_AGG_COLUMN                     154
   7279 #define TK_CONST_FUNC                     155
   7280 #define TK_UMINUS                         156
   7281 #define TK_UPLUS                          157
   7282 
   7283 /************** End of parse.h ***********************************************/
   7284 /************** Continuing where we left off in sqliteInt.h ******************/
   7285 #include <stdio.h>
   7286 #include <stdlib.h>
   7287 #include <string.h>
   7288 #include <assert.h>
   7289 #include <stddef.h>
   7290 
   7291 /*
   7292 ** If compiling for a processor that lacks floating point support,
   7293 ** substitute integer for floating-point
   7294 */
   7295 #ifdef SQLITE_OMIT_FLOATING_POINT
   7296 # define double sqlite_int64
   7297 # define float sqlite_int64
   7298 # define LONGDOUBLE_TYPE sqlite_int64
   7299 # ifndef SQLITE_BIG_DBL
   7300 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
   7301 # endif
   7302 # define SQLITE_OMIT_DATETIME_FUNCS 1
   7303 # define SQLITE_OMIT_TRACE 1
   7304 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   7305 # undef SQLITE_HAVE_ISNAN
   7306 #endif
   7307 #ifndef SQLITE_BIG_DBL
   7308 # define SQLITE_BIG_DBL (1e99)
   7309 #endif
   7310 
   7311 /*
   7312 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
   7313 ** afterward. Having this macro allows us to cause the C compiler
   7314 ** to omit code used by TEMP tables without messy #ifndef statements.
   7315 */
   7316 #ifdef SQLITE_OMIT_TEMPDB
   7317 #define OMIT_TEMPDB 1
   7318 #else
   7319 #define OMIT_TEMPDB 0
   7320 #endif
   7321 
   7322 /*
   7323 ** The "file format" number is an integer that is incremented whenever
   7324 ** the VDBE-level file format changes.  The following macros define the
   7325 ** the default file format for new databases and the maximum file format
   7326 ** that the library can read.
   7327 */
   7328 #define SQLITE_MAX_FILE_FORMAT 4
   7329 #ifndef SQLITE_DEFAULT_FILE_FORMAT
   7330 # define SQLITE_DEFAULT_FILE_FORMAT 1
   7331 #endif
   7332 
   7333 /*
   7334 ** Determine whether triggers are recursive by default.  This can be
   7335 ** changed at run-time using a pragma.
   7336 */
   7337 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
   7338 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
   7339 #endif
   7340 
   7341 /*
   7342 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
   7343 ** on the command-line
   7344 */
   7345 #ifndef SQLITE_TEMP_STORE
   7346 # define SQLITE_TEMP_STORE 1
   7347 #endif
   7348 
   7349 /*
   7350 ** GCC does not define the offsetof() macro so we'll have to do it
   7351 ** ourselves.
   7352 */
   7353 #ifndef offsetof
   7354 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
   7355 #endif
   7356 
   7357 /*
   7358 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
   7359 ** not, there are still machines out there that use EBCDIC.)
   7360 */
   7361 #if 'A' == '\301'
   7362 # define SQLITE_EBCDIC 1
   7363 #else
   7364 # define SQLITE_ASCII 1
   7365 #endif
   7366 
   7367 /*
   7368 ** Integers of known sizes.  These typedefs might change for architectures
   7369 ** where the sizes very.  Preprocessor macros are available so that the
   7370 ** types can be conveniently redefined at compile-type.  Like this:
   7371 **
   7372 **         cc '-DUINTPTR_TYPE=long long int' ...
   7373 */
   7374 #ifndef UINT32_TYPE
   7375 # ifdef HAVE_UINT32_T
   7376 #  define UINT32_TYPE uint32_t
   7377 # else
   7378 #  define UINT32_TYPE unsigned int
   7379 # endif
   7380 #endif
   7381 #ifndef UINT16_TYPE
   7382 # ifdef HAVE_UINT16_T
   7383 #  define UINT16_TYPE uint16_t
   7384 # else
   7385 #  define UINT16_TYPE unsigned short int
   7386 # endif
   7387 #endif
   7388 #ifndef INT16_TYPE
   7389 # ifdef HAVE_INT16_T
   7390 #  define INT16_TYPE int16_t
   7391 # else
   7392 #  define INT16_TYPE short int
   7393 # endif
   7394 #endif
   7395 #ifndef UINT8_TYPE
   7396 # ifdef HAVE_UINT8_T
   7397 #  define UINT8_TYPE uint8_t
   7398 # else
   7399 #  define UINT8_TYPE unsigned char
   7400 # endif
   7401 #endif
   7402 #ifndef INT8_TYPE
   7403 # ifdef HAVE_INT8_T
   7404 #  define INT8_TYPE int8_t
   7405 # else
   7406 #  define INT8_TYPE signed char
   7407 # endif
   7408 #endif
   7409 #ifndef LONGDOUBLE_TYPE
   7410 # define LONGDOUBLE_TYPE long double
   7411 #endif
   7412 typedef sqlite_int64 i64;          /* 8-byte signed integer */
   7413 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
   7414 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
   7415 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
   7416 typedef INT16_TYPE i16;            /* 2-byte signed integer */
   7417 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
   7418 typedef INT8_TYPE i8;              /* 1-byte signed integer */
   7419 
   7420 /*
   7421 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
   7422 ** that can be stored in a u32 without loss of data.  The value
   7423 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
   7424 ** have to specify the value in the less intuitive manner shown:
   7425 */
   7426 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
   7427 
   7428 /*
   7429 ** Macros to determine whether the machine is big or little endian,
   7430 ** evaluated at runtime.
   7431 */
   7432 #ifdef SQLITE_AMALGAMATION
   7433 SQLITE_PRIVATE const int sqlite3one = 1;
   7434 #else
   7435 SQLITE_PRIVATE const int sqlite3one;
   7436 #endif
   7437 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
   7438                              || defined(__x86_64) || defined(__x86_64__)
   7439 # define SQLITE_BIGENDIAN    0
   7440 # define SQLITE_LITTLEENDIAN 1
   7441 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
   7442 #else
   7443 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
   7444 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
   7445 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
   7446 #endif
   7447 
   7448 /*
   7449 ** Constants for the largest and smallest possible 64-bit signed integers.
   7450 ** These macros are designed to work correctly on both 32-bit and 64-bit
   7451 ** compilers.
   7452 */
   7453 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
   7454 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
   7455 
   7456 /*
   7457 ** Round up a number to the next larger multiple of 8.  This is used
   7458 ** to force 8-byte alignment on 64-bit architectures.
   7459 */
   7460 #define ROUND8(x)     (((x)+7)&~7)
   7461 
   7462 /*
   7463 ** Round down to the nearest multiple of 8
   7464 */
   7465 #define ROUNDDOWN8(x) ((x)&~7)
   7466 
   7467 /*
   7468 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
   7469 ** macro is used only within assert() to verify that the code gets
   7470 ** all alignment restrictions correct.
   7471 **
   7472 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
   7473 ** underlying malloc() implemention might return us 4-byte aligned
   7474 ** pointers.  In that case, only verify 4-byte alignment.
   7475 */
   7476 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
   7477 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
   7478 #else
   7479 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
   7480 #endif
   7481 
   7482 
   7483 /*
   7484 ** An instance of the following structure is used to store the busy-handler
   7485 ** callback for a given sqlite handle.
   7486 **
   7487 ** The sqlite.busyHandler member of the sqlite struct contains the busy
   7488 ** callback for the database handle. Each pager opened via the sqlite
   7489 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
   7490 ** callback is currently invoked only from within pager.c.
   7491 */
   7492 typedef struct BusyHandler BusyHandler;
   7493 struct BusyHandler {
   7494   int (*xFunc)(void *,int);  /* The busy callback */
   7495   void *pArg;                /* First arg to busy callback */
   7496   int nBusy;                 /* Incremented with each busy call */
   7497 };
   7498 
   7499 /*
   7500 ** Name of the master database table.  The master database table
   7501 ** is a special table that holds the names and attributes of all
   7502 ** user tables and indices.
   7503 */
   7504 #define MASTER_NAME       "sqlite_master"
   7505 #define TEMP_MASTER_NAME  "sqlite_temp_master"
   7506 
   7507 /*
   7508 ** The root-page of the master database table.
   7509 */
   7510 #define MASTER_ROOT       1
   7511 
   7512 /*
   7513 ** The name of the schema table.
   7514 */
   7515 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
   7516 
   7517 /*
   7518 ** A convenience macro that returns the number of elements in
   7519 ** an array.
   7520 */
   7521 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
   7522 
   7523 /*
   7524 ** The following value as a destructor means to use sqlite3DbFree().
   7525 ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
   7526 */
   7527 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
   7528 
   7529 /*
   7530 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
   7531 ** not support Writable Static Data (WSD) such as global and static variables.
   7532 ** All variables must either be on the stack or dynamically allocated from
   7533 ** the heap.  When WSD is unsupported, the variable declarations scattered
   7534 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
   7535 ** macro is used for this purpose.  And instead of referencing the variable
   7536 ** directly, we use its constant as a key to lookup the run-time allocated
   7537 ** buffer that holds real variable.  The constant is also the initializer
   7538 ** for the run-time allocated buffer.
   7539 **
   7540 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
   7541 ** macros become no-ops and have zero performance impact.
   7542 */
   7543 #ifdef SQLITE_OMIT_WSD
   7544   #define SQLITE_WSD const
   7545   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
   7546   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
   7547 SQLITE_API   int sqlite3_wsd_init(int N, int J);
   7548 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
   7549 #else
   7550   #define SQLITE_WSD
   7551   #define GLOBAL(t,v) v
   7552   #define sqlite3GlobalConfig sqlite3Config
   7553 #endif
   7554 
   7555 /*
   7556 ** The following macros are used to suppress compiler warnings and to
   7557 ** make it clear to human readers when a function parameter is deliberately
   7558 ** left unused within the body of a function. This usually happens when
   7559 ** a function is called via a function pointer. For example the
   7560 ** implementation of an SQL aggregate step callback may not use the
   7561 ** parameter indicating the number of arguments passed to the aggregate,
   7562 ** if it knows that this is enforced elsewhere.
   7563 **
   7564 ** When a function parameter is not used at all within the body of a function,
   7565 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
   7566 ** However, these macros may also be used to suppress warnings related to
   7567 ** parameters that may or may not be used depending on compilation options.
   7568 ** For example those parameters only used in assert() statements. In these
   7569 ** cases the parameters are named as per the usual conventions.
   7570 */
   7571 #define UNUSED_PARAMETER(x) (void)(x)
   7572 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
   7573 
   7574 /*
   7575 ** Forward references to structures
   7576 */
   7577 typedef struct AggInfo AggInfo;
   7578 typedef struct AuthContext AuthContext;
   7579 typedef struct AutoincInfo AutoincInfo;
   7580 typedef struct Bitvec Bitvec;
   7581 typedef struct CollSeq CollSeq;
   7582 typedef struct Column Column;
   7583 typedef struct Db Db;
   7584 typedef struct Schema Schema;
   7585 typedef struct Expr Expr;
   7586 typedef struct ExprList ExprList;
   7587 typedef struct ExprSpan ExprSpan;
   7588 typedef struct FKey FKey;
   7589 typedef struct FuncDestructor FuncDestructor;
   7590 typedef struct FuncDef FuncDef;
   7591 typedef struct FuncDefHash FuncDefHash;
   7592 typedef struct IdList IdList;
   7593 typedef struct Index Index;
   7594 typedef struct IndexSample IndexSample;
   7595 typedef struct KeyClass KeyClass;
   7596 typedef struct KeyInfo KeyInfo;
   7597 typedef struct Lookaside Lookaside;
   7598 typedef struct LookasideSlot LookasideSlot;
   7599 typedef struct Module Module;
   7600 typedef struct NameContext NameContext;
   7601 typedef struct Parse Parse;
   7602 typedef struct RowSet RowSet;
   7603 typedef struct Savepoint Savepoint;
   7604 typedef struct Select Select;
   7605 typedef struct SrcList SrcList;
   7606 typedef struct StrAccum StrAccum;
   7607 typedef struct Table Table;
   7608 typedef struct TableLock TableLock;
   7609 typedef struct Token Token;
   7610 typedef struct Trigger Trigger;
   7611 typedef struct TriggerPrg TriggerPrg;
   7612 typedef struct TriggerStep TriggerStep;
   7613 typedef struct UnpackedRecord UnpackedRecord;
   7614 typedef struct VTable VTable;
   7615 typedef struct Walker Walker;
   7616 typedef struct WherePlan WherePlan;
   7617 typedef struct WhereInfo WhereInfo;
   7618 typedef struct WhereLevel WhereLevel;
   7619 
   7620 /*
   7621 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
   7622 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
   7623 ** pointer types (i.e. FuncDef) defined above.
   7624 */
   7625 /************** Include btree.h in the middle of sqliteInt.h *****************/
   7626 /************** Begin file btree.h *******************************************/
   7627 /*
   7628 ** 2001 September 15
   7629 **
   7630 ** The author disclaims copyright to this source code.  In place of
   7631 ** a legal notice, here is a blessing:
   7632 **
   7633 **    May you do good and not evil.
   7634 **    May you find forgiveness for yourself and forgive others.
   7635 **    May you share freely, never taking more than you give.
   7636 **
   7637 *************************************************************************
   7638 ** This header file defines the interface that the sqlite B-Tree file
   7639 ** subsystem.  See comments in the source code for a detailed description
   7640 ** of what each interface routine does.
   7641 */
   7642 #ifndef _BTREE_H_
   7643 #define _BTREE_H_
   7644 
   7645 /* TODO: This definition is just included so other modules compile. It
   7646 ** needs to be revisited.
   7647 */
   7648 #define SQLITE_N_BTREE_META 10
   7649 
   7650 /*
   7651 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
   7652 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
   7653 */
   7654 #ifndef SQLITE_DEFAULT_AUTOVACUUM
   7655   #define SQLITE_DEFAULT_AUTOVACUUM 0
   7656 #endif
   7657 
   7658 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
   7659 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
   7660 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
   7661 
   7662 /*
   7663 ** Forward declarations of structure
   7664 */
   7665 typedef struct Btree Btree;
   7666 typedef struct BtCursor BtCursor;
   7667 typedef struct BtShared BtShared;
   7668 
   7669 
   7670 SQLITE_PRIVATE int sqlite3BtreeOpen(
   7671   const char *zFilename,   /* Name of database file to open */
   7672   sqlite3 *db,             /* Associated database connection */
   7673   Btree **ppBtree,         /* Return open Btree* here */
   7674   int flags,               /* Flags */
   7675   int vfsFlags             /* Flags passed through to VFS open */
   7676 );
   7677 
   7678 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
   7679 ** following values.
   7680 **
   7681 ** NOTE:  These values must match the corresponding PAGER_ values in
   7682 ** pager.h.
   7683 */
   7684 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
   7685 #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
   7686 #define BTREE_MEMORY        4  /* This is an in-memory DB */
   7687 #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
   7688 #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
   7689 
   7690 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
   7691 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
   7692 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
   7693 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
   7694 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
   7695 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
   7696 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
   7697 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
   7698 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
   7699 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
   7700 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
   7701 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
   7702 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
   7703 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
   7704 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
   7705 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
   7706 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
   7707 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
   7708 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
   7709 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
   7710 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
   7711 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
   7712 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
   7713 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
   7714 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
   7715 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
   7716 
   7717 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
   7718 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
   7719 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
   7720 
   7721 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
   7722 
   7723 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
   7724 ** of the flags shown below.
   7725 **
   7726 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
   7727 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
   7728 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
   7729 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
   7730 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
   7731 ** indices.)
   7732 */
   7733 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
   7734 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
   7735 
   7736 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
   7737 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
   7738 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
   7739 
   7740 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
   7741 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
   7742 
   7743 /*
   7744 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
   7745 ** should be one of the following values. The integer values are assigned
   7746 ** to constants so that the offset of the corresponding field in an
   7747 ** SQLite database header may be found using the following formula:
   7748 **
   7749 **   offset = 36 + (idx * 4)
   7750 **
   7751 ** For example, the free-page-count field is located at byte offset 36 of
   7752 ** the database file header. The incr-vacuum-flag field is located at
   7753 ** byte offset 64 (== 36+4*7).
   7754 */
   7755 #define BTREE_FREE_PAGE_COUNT     0
   7756 #define BTREE_SCHEMA_VERSION      1
   7757 #define BTREE_FILE_FORMAT         2
   7758 #define BTREE_DEFAULT_CACHE_SIZE  3
   7759 #define BTREE_LARGEST_ROOT_PAGE   4
   7760 #define BTREE_TEXT_ENCODING       5
   7761 #define BTREE_USER_VERSION        6
   7762 #define BTREE_INCR_VACUUM         7
   7763 
   7764 SQLITE_PRIVATE int sqlite3BtreeCursor(
   7765   Btree*,                              /* BTree containing table to open */
   7766   int iTable,                          /* Index of root page */
   7767   int wrFlag,                          /* 1 for writing.  0 for read-only */
   7768   struct KeyInfo*,                     /* First argument to compare function */
   7769   BtCursor *pCursor                    /* Space to write cursor structure */
   7770 );
   7771 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
   7772 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
   7773 
   7774 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
   7775 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
   7776   BtCursor*,
   7777   UnpackedRecord *pUnKey,
   7778   i64 intKey,
   7779   int bias,
   7780   int *pRes
   7781 );
   7782 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
   7783 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
   7784 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
   7785                                   const void *pData, int nData,
   7786                                   int nZero, int bias, int seekResult);
   7787 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
   7788 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
   7789 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
   7790 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
   7791 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
   7792 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
   7793 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
   7794 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
   7795 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
   7796 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
   7797 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
   7798 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
   7799 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
   7800 
   7801 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
   7802 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
   7803 
   7804 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
   7805 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
   7806 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
   7807 
   7808 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
   7809 
   7810 #ifndef NDEBUG
   7811 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
   7812 #endif
   7813 
   7814 #ifndef SQLITE_OMIT_BTREECOUNT
   7815 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
   7816 #endif
   7817 
   7818 #ifdef SQLITE_TEST
   7819 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
   7820 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
   7821 #endif
   7822 
   7823 #ifndef SQLITE_OMIT_WAL
   7824 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
   7825 #endif
   7826 
   7827 /*
   7828 ** If we are not using shared cache, then there is no need to
   7829 ** use mutexes to access the BtShared structures.  So make the
   7830 ** Enter and Leave procedures no-ops.
   7831 */
   7832 #ifndef SQLITE_OMIT_SHARED_CACHE
   7833 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
   7834 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
   7835 #else
   7836 # define sqlite3BtreeEnter(X)
   7837 # define sqlite3BtreeEnterAll(X)
   7838 #endif
   7839 
   7840 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
   7841 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
   7842 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
   7843 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
   7844 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
   7845 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
   7846 #ifndef NDEBUG
   7847   /* These routines are used inside assert() statements only. */
   7848 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
   7849 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
   7850 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
   7851 #endif
   7852 #else
   7853 
   7854 # define sqlite3BtreeSharable(X) 0
   7855 # define sqlite3BtreeLeave(X)
   7856 # define sqlite3BtreeEnterCursor(X)
   7857 # define sqlite3BtreeLeaveCursor(X)
   7858 # define sqlite3BtreeLeaveAll(X)
   7859 
   7860 # define sqlite3BtreeHoldsMutex(X) 1
   7861 # define sqlite3BtreeHoldsAllMutexes(X) 1
   7862 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
   7863 #endif
   7864 
   7865 
   7866 #endif /* _BTREE_H_ */
   7867 
   7868 /************** End of btree.h ***********************************************/
   7869 /************** Continuing where we left off in sqliteInt.h ******************/
   7870 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
   7871 /************** Begin file vdbe.h ********************************************/
   7872 /*
   7873 ** 2001 September 15
   7874 **
   7875 ** The author disclaims copyright to this source code.  In place of
   7876 ** a legal notice, here is a blessing:
   7877 **
   7878 **    May you do good and not evil.
   7879 **    May you find forgiveness for yourself and forgive others.
   7880 **    May you share freely, never taking more than you give.
   7881 **
   7882 *************************************************************************
   7883 ** Header file for the Virtual DataBase Engine (VDBE)
   7884 **
   7885 ** This header defines the interface to the virtual database engine
   7886 ** or VDBE.  The VDBE implements an abstract machine that runs a
   7887 ** simple program to access and modify the underlying database.
   7888 */
   7889 #ifndef _SQLITE_VDBE_H_
   7890 #define _SQLITE_VDBE_H_
   7891 
   7892 /*
   7893 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
   7894 ** in the source file sqliteVdbe.c are allowed to see the insides
   7895 ** of this structure.
   7896 */
   7897 typedef struct Vdbe Vdbe;
   7898 
   7899 /*
   7900 ** The names of the following types declared in vdbeInt.h are required
   7901 ** for the VdbeOp definition.
   7902 */
   7903 typedef struct VdbeFunc VdbeFunc;
   7904 typedef struct Mem Mem;
   7905 typedef struct SubProgram SubProgram;
   7906 
   7907 /*
   7908 ** A single instruction of the virtual machine has an opcode
   7909 ** and as many as three operands.  The instruction is recorded
   7910 ** as an instance of the following structure:
   7911 */
   7912 struct VdbeOp {
   7913   u8 opcode;          /* What operation to perform */
   7914   signed char p4type; /* One of the P4_xxx constants for p4 */
   7915   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
   7916   u8 p5;              /* Fifth parameter is an unsigned character */
   7917   int p1;             /* First operand */
   7918   int p2;             /* Second parameter (often the jump destination) */
   7919   int p3;             /* The third parameter */
   7920   union {             /* fourth parameter */
   7921     int i;                 /* Integer value if p4type==P4_INT32 */
   7922     void *p;               /* Generic pointer */
   7923     char *z;               /* Pointer to data for string (char array) types */
   7924     i64 *pI64;             /* Used when p4type is P4_INT64 */
   7925     double *pReal;         /* Used when p4type is P4_REAL */
   7926     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
   7927     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
   7928     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
   7929     Mem *pMem;             /* Used when p4type is P4_MEM */
   7930     VTable *pVtab;         /* Used when p4type is P4_VTAB */
   7931     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
   7932     int *ai;               /* Used when p4type is P4_INTARRAY */
   7933     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
   7934   } p4;
   7935 #ifdef SQLITE_DEBUG
   7936   char *zComment;          /* Comment to improve readability */
   7937 #endif
   7938 #ifdef VDBE_PROFILE
   7939   int cnt;                 /* Number of times this instruction was executed */
   7940   u64 cycles;              /* Total time spent executing this instruction */
   7941 #endif
   7942 };
   7943 typedef struct VdbeOp VdbeOp;
   7944 
   7945 
   7946 /*
   7947 ** A sub-routine used to implement a trigger program.
   7948 */
   7949 struct SubProgram {
   7950   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
   7951   int nOp;                      /* Elements in aOp[] */
   7952   int nMem;                     /* Number of memory cells required */
   7953   int nCsr;                     /* Number of cursors required */
   7954   void *token;                  /* id that may be used to recursive triggers */
   7955   SubProgram *pNext;            /* Next sub-program already visited */
   7956 };
   7957 
   7958 /*
   7959 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
   7960 ** it takes up less space.
   7961 */
   7962 struct VdbeOpList {
   7963   u8 opcode;          /* What operation to perform */
   7964   signed char p1;     /* First operand */
   7965   signed char p2;     /* Second parameter (often the jump destination) */
   7966   signed char p3;     /* Third parameter */
   7967 };
   7968 typedef struct VdbeOpList VdbeOpList;
   7969 
   7970 /*
   7971 ** Allowed values of VdbeOp.p4type
   7972 */
   7973 #define P4_NOTUSED    0   /* The P4 parameter is not used */
   7974 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
   7975 #define P4_STATIC   (-2)  /* Pointer to a static string */
   7976 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
   7977 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
   7978 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
   7979 #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
   7980 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
   7981 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
   7982 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
   7983 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   7984 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   7985 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   7986 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   7987 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   7988 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
   7989 
   7990 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
   7991 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
   7992 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
   7993 ** gets freed when the Vdbe is finalized so it still should be obtained
   7994 ** from a single sqliteMalloc().  But no copy is made and the calling
   7995 ** function should *not* try to free the KeyInfo.
   7996 */
   7997 #define P4_KEYINFO_HANDOFF (-16)
   7998 #define P4_KEYINFO_STATIC  (-17)
   7999 
   8000 /*
   8001 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
   8002 ** number of columns of data returned by the statement.
   8003 */
   8004 #define COLNAME_NAME     0
   8005 #define COLNAME_DECLTYPE 1
   8006 #define COLNAME_DATABASE 2
   8007 #define COLNAME_TABLE    3
   8008 #define COLNAME_COLUMN   4
   8009 #ifdef SQLITE_ENABLE_COLUMN_METADATA
   8010 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
   8011 #else
   8012 # ifdef SQLITE_OMIT_DECLTYPE
   8013 #   define COLNAME_N      1      /* Store only the name */
   8014 # else
   8015 #   define COLNAME_N      2      /* Store the name and decltype */
   8016 # endif
   8017 #endif
   8018 
   8019 /*
   8020 ** The following macro converts a relative address in the p2 field
   8021 ** of a VdbeOp structure into a negative number so that
   8022 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
   8023 ** the macro again restores the address.
   8024 */
   8025 #define ADDR(X)  (-1-(X))
   8026 
   8027 /*
   8028 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
   8029 ** header file that defines a number for each opcode used by the VDBE.
   8030 */
   8031 /************** Include opcodes.h in the middle of vdbe.h ********************/
   8032 /************** Begin file opcodes.h *****************************************/
   8033 /* Automatically generated.  Do not edit */
   8034 /* See the mkopcodeh.awk script for details */
   8035 #define OP_Goto                                 1
   8036 #define OP_Gosub                                2
   8037 #define OP_Return                               3
   8038 #define OP_Yield                                4
   8039 #define OP_HaltIfNull                           5
   8040 #define OP_Halt                                 6
   8041 #define OP_Integer                              7
   8042 #define OP_Int64                                8
   8043 #define OP_Real                               130   /* same as TK_FLOAT    */
   8044 #define OP_String8                             94   /* same as TK_STRING   */
   8045 #define OP_String                               9
   8046 #define OP_Null                                10
   8047 #define OP_Blob                                11
   8048 #define OP_Variable                            12
   8049 #define OP_Move                                13
   8050 #define OP_Copy                                14
   8051 #define OP_SCopy                               15
   8052 #define OP_ResultRow                           16
   8053 #define OP_Concat                              91   /* same as TK_CONCAT   */
   8054 #define OP_Add                                 86   /* same as TK_PLUS     */
   8055 #define OP_Subtract                            87   /* same as TK_MINUS    */
   8056 #define OP_Multiply                            88   /* same as TK_STAR     */
   8057 #define OP_Divide                              89   /* same as TK_SLASH    */
   8058 #define OP_Remainder                           90   /* same as TK_REM      */
   8059 #define OP_CollSeq                             17
   8060 #define OP_Function                            18
   8061 #define OP_BitAnd                              82   /* same as TK_BITAND   */
   8062 #define OP_BitOr                               83   /* same as TK_BITOR    */
   8063 #define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
   8064 #define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
   8065 #define OP_AddImm                              20
   8066 #define OP_MustBeInt                           21
   8067 #define OP_RealAffinity                        22
   8068 #define OP_ToText                             141   /* same as TK_TO_TEXT  */
   8069 #define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
   8070 #define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
   8071 #define OP_ToInt                              144   /* same as TK_TO_INT   */
   8072 #define OP_ToReal                             145   /* same as TK_TO_REAL  */
   8073 #define OP_Eq                                  76   /* same as TK_EQ       */
   8074 #define OP_Ne                                  75   /* same as TK_NE       */
   8075 #define OP_Lt                                  79   /* same as TK_LT       */
   8076 #define OP_Le                                  78   /* same as TK_LE       */
   8077 #define OP_Gt                                  77   /* same as TK_GT       */
   8078 #define OP_Ge                                  80   /* same as TK_GE       */
   8079 #define OP_Permutation                         23
   8080 #define OP_Compare                             24
   8081 #define OP_Jump                                25
   8082 #define OP_And                                 69   /* same as TK_AND      */
   8083 #define OP_Or                                  68   /* same as TK_OR       */
   8084 #define OP_Not                                 19   /* same as TK_NOT      */
   8085 #define OP_BitNot                              93   /* same as TK_BITNOT   */
   8086 #define OP_If                                  26
   8087 #define OP_IfNot                               27
   8088 #define OP_IsNull                              73   /* same as TK_ISNULL   */
   8089 #define OP_NotNull                             74   /* same as TK_NOTNULL  */
   8090 #define OP_Column                              28
   8091 #define OP_Affinity                            29
   8092 #define OP_MakeRecord                          30
   8093 #define OP_Count                               31
   8094 #define OP_Savepoint                           32
   8095 #define OP_AutoCommit                          33
   8096 #define OP_Transaction                         34
   8097 #define OP_ReadCookie                          35
   8098 #define OP_SetCookie                           36
   8099 #define OP_VerifyCookie                        37
   8100 #define OP_OpenRead                            38
   8101 #define OP_OpenWrite                           39
   8102 #define OP_OpenAutoindex                       40
   8103 #define OP_OpenEphemeral                       41
   8104 #define OP_OpenPseudo                          42
   8105 #define OP_Close                               43
   8106 #define OP_SeekLt                              44
   8107 #define OP_SeekLe                              45
   8108 #define OP_SeekGe                              46
   8109 #define OP_SeekGt                              47
   8110 #define OP_Seek                                48
   8111 #define OP_NotFound                            49
   8112 #define OP_Found                               50
   8113 #define OP_IsUnique                            51
   8114 #define OP_NotExists                           52
   8115 #define OP_Sequence                            53
   8116 #define OP_NewRowid                            54
   8117 #define OP_Insert                              55
   8118 #define OP_InsertInt                           56
   8119 #define OP_Delete                              57
   8120 #define OP_ResetCount                          58
   8121 #define OP_RowKey                              59
   8122 #define OP_RowData                             60
   8123 #define OP_Rowid                               61
   8124 #define OP_NullRow                             62
   8125 #define OP_Last                                63
   8126 #define OP_Sort                                64
   8127 #define OP_Rewind                              65
   8128 #define OP_Prev                                66
   8129 #define OP_Next                                67
   8130 #define OP_IdxInsert                           70
   8131 #define OP_IdxDelete                           71
   8132 #define OP_IdxRowid                            72
   8133 #define OP_IdxLT                               81
   8134 #define OP_IdxGE                               92
   8135 #define OP_Destroy                             95
   8136 #define OP_Clear                               96
   8137 #define OP_CreateIndex                         97
   8138 #define OP_CreateTable                         98
   8139 #define OP_ParseSchema                         99
   8140 #define OP_LoadAnalysis                       100
   8141 #define OP_DropTable                          101
   8142 #define OP_DropIndex                          102
   8143 #define OP_DropTrigger                        103
   8144 #define OP_IntegrityCk                        104
   8145 #define OP_RowSetAdd                          105
   8146 #define OP_RowSetRead                         106
   8147 #define OP_RowSetTest                         107
   8148 #define OP_Program                            108
   8149 #define OP_Param                              109
   8150 #define OP_FkCounter                          110
   8151 #define OP_FkIfZero                           111
   8152 #define OP_MemMax                             112
   8153 #define OP_IfPos                              113
   8154 #define OP_IfNeg                              114
   8155 #define OP_IfZero                             115
   8156 #define OP_AggStep                            116
   8157 #define OP_AggFinal                           117
   8158 #define OP_Checkpoint                         118
   8159 #define OP_JournalMode                        119
   8160 #define OP_Vacuum                             120
   8161 #define OP_IncrVacuum                         121
   8162 #define OP_Expire                             122
   8163 #define OP_TableLock                          123
   8164 #define OP_VBegin                             124
   8165 #define OP_VCreate                            125
   8166 #define OP_VDestroy                           126
   8167 #define OP_VOpen                              127
   8168 #define OP_VFilter                            128
   8169 #define OP_VColumn                            129
   8170 #define OP_VNext                              131
   8171 #define OP_VRename                            132
   8172 #define OP_VUpdate                            133
   8173 #define OP_Pagecount                          134
   8174 #define OP_MaxPgcnt                           135
   8175 #define OP_Trace                              136
   8176 #define OP_Noop                               137
   8177 #define OP_Explain                            138
   8178 
   8179 /* The following opcode values are never used */
   8180 #define OP_NotUsed_139                        139
   8181 #define OP_NotUsed_140                        140
   8182 
   8183 
   8184 /* Properties such as "out2" or "jump" that are specified in
   8185 ** comments following the "case" for each opcode in the vdbe.c
   8186 ** are encoded into bitvectors as follows:
   8187 */
   8188 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
   8189 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
   8190 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
   8191 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
   8192 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
   8193 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
   8194 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
   8195 #define OPFLG_INITIALIZER {\
   8196 /*   0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
   8197 /*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
   8198 /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
   8199 /*  24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
   8200 /*  32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
   8201 /*  40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
   8202 /*  48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
   8203 /*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
   8204 /*  64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
   8205 /*  72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
   8206 /*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
   8207 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
   8208 /*  96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
   8209 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
   8210 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
   8211 /* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
   8212 /* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
   8213 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
   8214 /* 144 */ 0x04, 0x04,}
   8215 
   8216 /************** End of opcodes.h *********************************************/
   8217 /************** Continuing where we left off in vdbe.h ***********************/
   8218 
   8219 /*
   8220 ** Prototypes for the VDBE interface.  See comments on the implementation
   8221 ** for a description of what each of these routines does.
   8222 */
   8223 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
   8224 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
   8225 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
   8226 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
   8227 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
   8228 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
   8229 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
   8230 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
   8231 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
   8232 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
   8233 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
   8234 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   8235 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
   8236 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
   8237 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   8238 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
   8239 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   8240 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
   8241 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
   8242 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
   8243 SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
   8244 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
   8245 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
   8246 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
   8247 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
   8248 #ifdef SQLITE_DEBUG
   8249 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
   8250 SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
   8251 #endif
   8252 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
   8253 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
   8254 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
   8255 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
   8256 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
   8257 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
   8258 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
   8259 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
   8260 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
   8261 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
   8262 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
   8263 #ifndef SQLITE_OMIT_TRACE
   8264 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   8265 #endif
   8266 
   8267 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
   8268 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
   8269 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
   8270 
   8271 #ifndef SQLITE_OMIT_TRIGGER
   8272 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   8273 #endif
   8274 
   8275 
   8276 #ifndef NDEBUG
   8277 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
   8278 # define VdbeComment(X)  sqlite3VdbeComment X
   8279 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
   8280 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
   8281 #else
   8282 # define VdbeComment(X)
   8283 # define VdbeNoopComment(X)
   8284 #endif
   8285 
   8286 #endif
   8287 
   8288 /************** End of vdbe.h ************************************************/
   8289 /************** Continuing where we left off in sqliteInt.h ******************/
   8290 /************** Include pager.h in the middle of sqliteInt.h *****************/
   8291 /************** Begin file pager.h *******************************************/
   8292 /*
   8293 ** 2001 September 15
   8294 **
   8295 ** The author disclaims copyright to this source code.  In place of
   8296 ** a legal notice, here is a blessing:
   8297 **
   8298 **    May you do good and not evil.
   8299 **    May you find forgiveness for yourself and forgive others.
   8300 **    May you share freely, never taking more than you give.
   8301 **
   8302 *************************************************************************
   8303 ** This header file defines the interface that the sqlite page cache
   8304 ** subsystem.  The page cache subsystem reads and writes a file a page
   8305 ** at a time and provides a journal for rollback.
   8306 */
   8307 
   8308 #ifndef _PAGER_H_
   8309 #define _PAGER_H_
   8310 
   8311 /*
   8312 ** Default maximum size for persistent journal files. A negative
   8313 ** value means no limit. This value may be overridden using the
   8314 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
   8315 */
   8316 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
   8317   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
   8318 #endif
   8319 
   8320 /*
   8321 ** The type used to represent a page number.  The first page in a file
   8322 ** is called page 1.  0 is used to represent "not a page".
   8323 */
   8324 typedef u32 Pgno;
   8325 
   8326 /*
   8327 ** Each open file is managed by a separate instance of the "Pager" structure.
   8328 */
   8329 typedef struct Pager Pager;
   8330 
   8331 /*
   8332 ** Handle type for pages.
   8333 */
   8334 typedef struct PgHdr DbPage;
   8335 
   8336 /*
   8337 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
   8338 ** reserved for working around a windows/posix incompatibility). It is
   8339 ** used in the journal to signify that the remainder of the journal file
   8340 ** is devoted to storing a master journal name - there are no more pages to
   8341 ** roll back. See comments for function writeMasterJournal() in pager.c
   8342 ** for details.
   8343 */
   8344 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
   8345 
   8346 /*
   8347 ** Allowed values for the flags parameter to sqlite3PagerOpen().
   8348 **
   8349 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
   8350 */
   8351 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
   8352 #define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
   8353 #define PAGER_MEMORY        0x0004    /* In-memory database */
   8354 
   8355 /*
   8356 ** Valid values for the second argument to sqlite3PagerLockingMode().
   8357 */
   8358 #define PAGER_LOCKINGMODE_QUERY      -1
   8359 #define PAGER_LOCKINGMODE_NORMAL      0
   8360 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
   8361 
   8362 /*
   8363 ** Numeric constants that encode the journalmode.
   8364 */
   8365 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
   8366 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
   8367 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
   8368 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
   8369 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
   8370 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
   8371 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
   8372 
   8373 /*
   8374 ** The remainder of this file contains the declarations of the functions
   8375 ** that make up the Pager sub-system API. See source code comments for
   8376 ** a detailed description of each routine.
   8377 */
   8378 
   8379 /* Open and close a Pager connection. */
   8380 SQLITE_PRIVATE int sqlite3PagerOpen(
   8381   sqlite3_vfs*,
   8382   Pager **ppPager,
   8383   const char*,
   8384   int,
   8385   int,
   8386   int,
   8387   void(*)(DbPage*)
   8388 );
   8389 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
   8390 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
   8391 
   8392 /* Functions used to configure a Pager object. */
   8393 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
   8394 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
   8395 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
   8396 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
   8397 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
   8398 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
   8399 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
   8400 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
   8401 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
   8402 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
   8403 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
   8404 
   8405 /* Functions used to obtain and release page references. */
   8406 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
   8407 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
   8408 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
   8409 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
   8410 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
   8411 
   8412 /* Operations on page references. */
   8413 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
   8414 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
   8415 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
   8416 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
   8417 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
   8418 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
   8419 
   8420 /* Functions used to manage pager transactions and savepoints. */
   8421 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
   8422 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
   8423 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
   8424 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
   8425 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
   8426 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
   8427 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
   8428 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
   8429 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
   8430 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
   8431 
   8432 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
   8433 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
   8434 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
   8435 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
   8436 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
   8437 
   8438 /* Functions used to query pager state and configuration. */
   8439 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
   8440 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
   8441 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
   8442 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
   8443 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
   8444 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
   8445 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
   8446 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
   8447 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
   8448 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
   8449 
   8450 /* Functions used to truncate the database file. */
   8451 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
   8452 
   8453 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
   8454 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
   8455 #endif
   8456 
   8457 /* Functions to support testing and debugging. */
   8458 #if !defined(NDEBUG) || defined(SQLITE_TEST)
   8459 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
   8460 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
   8461 #endif
   8462 #ifdef SQLITE_TEST
   8463 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
   8464 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
   8465   void disable_simulated_io_errors(void);
   8466   void enable_simulated_io_errors(void);
   8467 #else
   8468 # define disable_simulated_io_errors()
   8469 # define enable_simulated_io_errors()
   8470 #endif
   8471 
   8472 #endif /* _PAGER_H_ */
   8473 
   8474 /************** End of pager.h ***********************************************/
   8475 /************** Continuing where we left off in sqliteInt.h ******************/
   8476 /************** Include pcache.h in the middle of sqliteInt.h ****************/
   8477 /************** Begin file pcache.h ******************************************/
   8478 /*
   8479 ** 2008 August 05
   8480 **
   8481 ** The author disclaims copyright to this source code.  In place of
   8482 ** a legal notice, here is a blessing:
   8483 **
   8484 **    May you do good and not evil.
   8485 **    May you find forgiveness for yourself and forgive others.
   8486 **    May you share freely, never taking more than you give.
   8487 **
   8488 *************************************************************************
   8489 ** This header file defines the interface that the sqlite page cache
   8490 ** subsystem.
   8491 */
   8492 
   8493 #ifndef _PCACHE_H_
   8494 
   8495 typedef struct PgHdr PgHdr;
   8496 typedef struct PCache PCache;
   8497 
   8498 /*
   8499 ** Every page in the cache is controlled by an instance of the following
   8500 ** structure.
   8501 */
   8502 struct PgHdr {
   8503   void *pData;                   /* Content of this page */
   8504   void *pExtra;                  /* Extra content */
   8505   PgHdr *pDirty;                 /* Transient list of dirty pages */
   8506   Pgno pgno;                     /* Page number for this page */
   8507   Pager *pPager;                 /* The pager this page is part of */
   8508 #ifdef SQLITE_CHECK_PAGES
   8509   u32 pageHash;                  /* Hash of page content */
   8510 #endif
   8511   u16 flags;                     /* PGHDR flags defined below */
   8512 
   8513   /**********************************************************************
   8514   ** Elements above are public.  All that follows is private to pcache.c
   8515   ** and should not be accessed by other modules.
   8516   */
   8517   i16 nRef;                      /* Number of users of this page */
   8518   PCache *pCache;                /* Cache that owns this page */
   8519 
   8520   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
   8521   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
   8522 };
   8523 
   8524 /* Bit values for PgHdr.flags */
   8525 #define PGHDR_DIRTY             0x002  /* Page has changed */
   8526 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
   8527                                        ** writing this page to the database */
   8528 #define PGHDR_NEED_READ         0x008  /* Content is unread */
   8529 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
   8530 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
   8531 
   8532 /* Initialize and shutdown the page cache subsystem */
   8533 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
   8534 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
   8535 
   8536 /* Page cache buffer management:
   8537 ** These routines implement SQLITE_CONFIG_PAGECACHE.
   8538 */
   8539 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
   8540 
   8541 /* Create a new pager cache.
   8542 ** Under memory stress, invoke xStress to try to make pages clean.
   8543 ** Only clean and unpinned pages can be reclaimed.
   8544 */
   8545 SQLITE_PRIVATE void sqlite3PcacheOpen(
   8546   int szPage,                    /* Size of every page */
   8547   int szExtra,                   /* Extra space associated with each page */
   8548   int bPurgeable,                /* True if pages are on backing store */
   8549   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
   8550   void *pStress,                 /* Argument to xStress */
   8551   PCache *pToInit                /* Preallocated space for the PCache */
   8552 );
   8553 
   8554 /* Modify the page-size after the cache has been created. */
   8555 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
   8556 
   8557 /* Return the size in bytes of a PCache object.  Used to preallocate
   8558 ** storage space.
   8559 */
   8560 SQLITE_PRIVATE int sqlite3PcacheSize(void);
   8561 
   8562 /* One release per successful fetch.  Page is pinned until released.
   8563 ** Reference counted.
   8564 */
   8565 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
   8566 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
   8567 
   8568 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
   8569 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
   8570 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
   8571 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
   8572 
   8573 /* Change a page number.  Used by incr-vacuum. */
   8574 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
   8575 
   8576 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
   8577 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
   8578 
   8579 /* Get a list of all dirty pages in the cache, sorted by page number */
   8580 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
   8581 
   8582 /* Reset and close the cache object */
   8583 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
   8584 
   8585 /* Clear flags from pages of the page cache */
   8586 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
   8587 
   8588 /* Discard the contents of the cache */
   8589 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
   8590 
   8591 /* Return the total number of outstanding page references */
   8592 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
   8593 
   8594 /* Increment the reference count of an existing page */
   8595 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
   8596 
   8597 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
   8598 
   8599 /* Return the total number of pages stored in the cache */
   8600 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
   8601 
   8602 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   8603 /* Iterate through all dirty pages currently stored in the cache. This
   8604 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
   8605 ** library is built.
   8606 */
   8607 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
   8608 #endif
   8609 
   8610 /* Set and get the suggested cache-size for the specified pager-cache.
   8611 **
   8612 ** If no global maximum is configured, then the system attempts to limit
   8613 ** the total number of pages cached by purgeable pager-caches to the sum
   8614 ** of the suggested cache-sizes.
   8615 */
   8616 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
   8617 #ifdef SQLITE_TEST
   8618 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
   8619 #endif
   8620 
   8621 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   8622 /* Try to return memory used by the pcache module to the main memory heap */
   8623 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
   8624 #endif
   8625 
   8626 #ifdef SQLITE_TEST
   8627 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
   8628 #endif
   8629 
   8630 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
   8631 
   8632 #endif /* _PCACHE_H_ */
   8633 
   8634 /************** End of pcache.h **********************************************/
   8635 /************** Continuing where we left off in sqliteInt.h ******************/
   8636 
   8637 /************** Include os.h in the middle of sqliteInt.h ********************/
   8638 /************** Begin file os.h **********************************************/
   8639 /*
   8640 ** 2001 September 16
   8641 **
   8642 ** The author disclaims copyright to this source code.  In place of
   8643 ** a legal notice, here is a blessing:
   8644 **
   8645 **    May you do good and not evil.
   8646 **    May you find forgiveness for yourself and forgive others.
   8647 **    May you share freely, never taking more than you give.
   8648 **
   8649 ******************************************************************************
   8650 **
   8651 ** This header file (together with is companion C source-code file
   8652 ** "os.c") attempt to abstract the underlying operating system so that
   8653 ** the SQLite library will work on both POSIX and windows systems.
   8654 **
   8655 ** This header file is #include-ed by sqliteInt.h and thus ends up
   8656 ** being included by every source file.
   8657 */
   8658 #ifndef _SQLITE_OS_H_
   8659 #define _SQLITE_OS_H_
   8660 
   8661 /*
   8662 ** Figure out if we are dealing with Unix, Windows, or some other
   8663 ** operating system.  After the following block of preprocess macros,
   8664 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
   8665 ** will defined to either 1 or 0.  One of the four will be 1.  The other
   8666 ** three will be 0.
   8667 */
   8668 #if defined(SQLITE_OS_OTHER)
   8669 # if SQLITE_OS_OTHER==1
   8670 #   undef SQLITE_OS_UNIX
   8671 #   define SQLITE_OS_UNIX 0
   8672 #   undef SQLITE_OS_WIN
   8673 #   define SQLITE_OS_WIN 0
   8674 #   undef SQLITE_OS_OS2
   8675 #   define SQLITE_OS_OS2 0
   8676 # else
   8677 #   undef SQLITE_OS_OTHER
   8678 # endif
   8679 #endif
   8680 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
   8681 # define SQLITE_OS_OTHER 0
   8682 # ifndef SQLITE_OS_WIN
   8683 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
   8684 #     define SQLITE_OS_WIN 1
   8685 #     define SQLITE_OS_UNIX 0
   8686 #     define SQLITE_OS_OS2 0
   8687 #   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
   8688 #     define SQLITE_OS_WIN 0
   8689 #     define SQLITE_OS_UNIX 0
   8690 #     define SQLITE_OS_OS2 1
   8691 #   else
   8692 #     define SQLITE_OS_WIN 0
   8693 #     define SQLITE_OS_UNIX 1
   8694 #     define SQLITE_OS_OS2 0
   8695 #  endif
   8696 # else
   8697 #  define SQLITE_OS_UNIX 0
   8698 #  define SQLITE_OS_OS2 0
   8699 # endif
   8700 #else
   8701 # ifndef SQLITE_OS_WIN
   8702 #  define SQLITE_OS_WIN 0
   8703 # endif
   8704 #endif
   8705 
   8706 /*
   8707 ** Determine if we are dealing with WindowsCE - which has a much
   8708 ** reduced API.
   8709 */
   8710 #if defined(_WIN32_WCE)
   8711 # define SQLITE_OS_WINCE 1
   8712 #else
   8713 # define SQLITE_OS_WINCE 0
   8714 #endif
   8715 
   8716 
   8717 /*
   8718 ** Define the maximum size of a temporary filename
   8719 */
   8720 #if SQLITE_OS_WIN
   8721 # include <windows.h>
   8722 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
   8723 #elif SQLITE_OS_OS2
   8724 # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
   8725 #  include <os2safe.h> /* has to be included before os2.h for linking to work */
   8726 # endif
   8727 # define INCL_DOSDATETIME
   8728 # define INCL_DOSFILEMGR
   8729 # define INCL_DOSERRORS
   8730 # define INCL_DOSMISC
   8731 # define INCL_DOSPROCESS
   8732 # define INCL_DOSMODULEMGR
   8733 # define INCL_DOSSEMAPHORES
   8734 # include <os2.h>
   8735 # include <uconv.h>
   8736 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
   8737 #else
   8738 # define SQLITE_TEMPNAME_SIZE 200
   8739 #endif
   8740 
   8741 /* If the SET_FULLSYNC macro is not defined above, then make it
   8742 ** a no-op
   8743 */
   8744 #ifndef SET_FULLSYNC
   8745 # define SET_FULLSYNC(x,y)
   8746 #endif
   8747 
   8748 /*
   8749 ** The default size of a disk sector
   8750 */
   8751 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
   8752 # define SQLITE_DEFAULT_SECTOR_SIZE 512
   8753 #endif
   8754 
   8755 /*
   8756 ** Temporary files are named starting with this prefix followed by 16 random
   8757 ** alphanumeric characters, and no file extension. They are stored in the
   8758 ** OS's standard temporary file directory, and are deleted prior to exit.
   8759 ** If sqlite is being embedded in another program, you may wish to change the
   8760 ** prefix to reflect your program's name, so that if your program exits
   8761 ** prematurely, old temporary files can be easily identified. This can be done
   8762 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
   8763 **
   8764 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
   8765 ** Mcafee started using SQLite in their anti-virus product and it
   8766 ** started putting files with the "sqlite" name in the c:/temp folder.
   8767 ** This annoyed many windows users.  Those users would then do a
   8768 ** Google search for "sqlite", find the telephone numbers of the
   8769 ** developers and call to wake them up at night and complain.
   8770 ** For this reason, the default name prefix is changed to be "sqlite"
   8771 ** spelled backwards.  So the temp files are still identified, but
   8772 ** anybody smart enough to figure out the code is also likely smart
   8773 ** enough to know that calling the developer will not help get rid
   8774 ** of the file.
   8775 */
   8776 #ifndef SQLITE_TEMP_FILE_PREFIX
   8777 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
   8778 #endif
   8779 
   8780 /*
   8781 ** The following values may be passed as the second argument to
   8782 ** sqlite3OsLock(). The various locks exhibit the following semantics:
   8783 **
   8784 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
   8785 ** RESERVED:  A single process may hold a RESERVED lock on a file at
   8786 **            any time. Other processes may hold and obtain new SHARED locks.
   8787 ** PENDING:   A single process may hold a PENDING lock on a file at
   8788 **            any one time. Existing SHARED locks may persist, but no new
   8789 **            SHARED locks may be obtained by other processes.
   8790 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
   8791 **
   8792 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
   8793 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
   8794 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
   8795 ** sqlite3OsLock().
   8796 */
   8797 #define NO_LOCK         0
   8798 #define SHARED_LOCK     1
   8799 #define RESERVED_LOCK   2
   8800 #define PENDING_LOCK    3
   8801 #define EXCLUSIVE_LOCK  4
   8802 
   8803 /*
   8804 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
   8805 **
   8806 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
   8807 ** those functions are not available.  So we use only LockFile() and
   8808 ** UnlockFile().
   8809 **
   8810 ** LockFile() prevents not just writing but also reading by other processes.
   8811 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
   8812 ** byte out of a specific range of bytes. The lock byte is obtained at
   8813 ** random so two separate readers can probably access the file at the
   8814 ** same time, unless they are unlucky and choose the same lock byte.
   8815 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
   8816 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
   8817 ** a single byte of the file that is designated as the reserved lock byte.
   8818 ** A PENDING_LOCK is obtained by locking a designated byte different from
   8819 ** the RESERVED_LOCK byte.
   8820 **
   8821 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
   8822 ** which means we can use reader/writer locks.  When reader/writer locks
   8823 ** are used, the lock is placed on the same range of bytes that is used
   8824 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
   8825 ** will support two or more Win95 readers or two or more WinNT readers.
   8826 ** But a single Win95 reader will lock out all WinNT readers and a single
   8827 ** WinNT reader will lock out all other Win95 readers.
   8828 **
   8829 ** The following #defines specify the range of bytes used for locking.
   8830 ** SHARED_SIZE is the number of bytes available in the pool from which
   8831 ** a random byte is selected for a shared lock.  The pool of bytes for
   8832 ** shared locks begins at SHARED_FIRST.
   8833 **
   8834 ** The same locking strategy and
   8835 ** byte ranges are used for Unix.  This leaves open the possiblity of having
   8836 ** clients on win95, winNT, and unix all talking to the same shared file
   8837 ** and all locking correctly.  To do so would require that samba (or whatever
   8838 ** tool is being used for file sharing) implements locks correctly between
   8839 ** windows and unix.  I'm guessing that isn't likely to happen, but by
   8840 ** using the same locking range we are at least open to the possibility.
   8841 **
   8842 ** Locking in windows is manditory.  For this reason, we cannot store
   8843 ** actual data in the bytes used for locking.  The pager never allocates
   8844 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
   8845 ** that all locks will fit on a single page even at the minimum page size.
   8846 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
   8847 ** is set high so that we don't have to allocate an unused page except
   8848 ** for very large databases.  But one should test the page skipping logic
   8849 ** by setting PENDING_BYTE low and running the entire regression suite.
   8850 **
   8851 ** Changing the value of PENDING_BYTE results in a subtly incompatible
   8852 ** file format.  Depending on how it is changed, you might not notice
   8853 ** the incompatibility right away, even running a full regression test.
   8854 ** The default location of PENDING_BYTE is the first byte past the
   8855 ** 1GB boundary.
   8856 **
   8857 */
   8858 #ifdef SQLITE_OMIT_WSD
   8859 # define PENDING_BYTE     (0x40000000)
   8860 #else
   8861 # define PENDING_BYTE      sqlite3PendingByte
   8862 #endif
   8863 #define RESERVED_BYTE     (PENDING_BYTE+1)
   8864 #define SHARED_FIRST      (PENDING_BYTE+2)
   8865 #define SHARED_SIZE       510
   8866 
   8867 /*
   8868 ** Wrapper around OS specific sqlite3_os_init() function.
   8869 */
   8870 SQLITE_PRIVATE int sqlite3OsInit(void);
   8871 
   8872 /*
   8873 ** Functions for accessing sqlite3_file methods
   8874 */
   8875 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
   8876 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
   8877 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
   8878 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
   8879 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
   8880 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
   8881 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
   8882 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
   8883 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
   8884 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
   8885 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
   8886 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
   8887 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
   8888 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
   8889 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
   8890 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
   8891 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
   8892 
   8893 /*
   8894 ** Functions for accessing sqlite3_vfs methods
   8895 */
   8896 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
   8897 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
   8898 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
   8899 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
   8900 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   8901 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
   8902 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
   8903 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
   8904 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
   8905 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   8906 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
   8907 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
   8908 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
   8909 
   8910 /*
   8911 ** Convenience functions for opening and closing files using
   8912 ** sqlite3_malloc() to obtain space for the file-handle structure.
   8913 */
   8914 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
   8915 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
   8916 
   8917 #endif /* _SQLITE_OS_H_ */
   8918 
   8919 /************** End of os.h **************************************************/
   8920 /************** Continuing where we left off in sqliteInt.h ******************/
   8921 /************** Include mutex.h in the middle of sqliteInt.h *****************/
   8922 /************** Begin file mutex.h *******************************************/
   8923 /*
   8924 ** 2007 August 28
   8925 **
   8926 ** The author disclaims copyright to this source code.  In place of
   8927 ** a legal notice, here is a blessing:
   8928 **
   8929 **    May you do good and not evil.
   8930 **    May you find forgiveness for yourself and forgive others.
   8931 **    May you share freely, never taking more than you give.
   8932 **
   8933 *************************************************************************
   8934 **
   8935 ** This file contains the common header for all mutex implementations.
   8936 ** The sqliteInt.h header #includes this file so that it is available
   8937 ** to all source files.  We break it out in an effort to keep the code
   8938 ** better organized.
   8939 **
   8940 ** NOTE:  source files should *not* #include this header file directly.
   8941 ** Source files should #include the sqliteInt.h file and let that file
   8942 ** include this one indirectly.
   8943 */
   8944 
   8945 
   8946 /*
   8947 ** Figure out what version of the code to use.  The choices are
   8948 **
   8949 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
   8950 **                             mutexes implemention cannot be overridden
   8951 **                             at start-time.
   8952 **
   8953 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
   8954 **                             mutual exclusion is provided.  But this
   8955 **                             implementation can be overridden at
   8956 **                             start-time.
   8957 **
   8958 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
   8959 **
   8960 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
   8961 **
   8962 **   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
   8963 */
   8964 #if !SQLITE_THREADSAFE
   8965 # define SQLITE_MUTEX_OMIT
   8966 #endif
   8967 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
   8968 #  if SQLITE_OS_UNIX
   8969 #    define SQLITE_MUTEX_PTHREADS
   8970 #  elif SQLITE_OS_WIN
   8971 #    define SQLITE_MUTEX_W32
   8972 #  elif SQLITE_OS_OS2
   8973 #    define SQLITE_MUTEX_OS2
   8974 #  else
   8975 #    define SQLITE_MUTEX_NOOP
   8976 #  endif
   8977 #endif
   8978 
   8979 #ifdef SQLITE_MUTEX_OMIT
   8980 /*
   8981 ** If this is a no-op implementation, implement everything as macros.
   8982 */
   8983 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
   8984 #define sqlite3_mutex_free(X)
   8985 #define sqlite3_mutex_enter(X)
   8986 #define sqlite3_mutex_try(X)      SQLITE_OK
   8987 #define sqlite3_mutex_leave(X)
   8988 #define sqlite3_mutex_held(X)     ((void)(X),1)
   8989 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
   8990 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
   8991 #define sqlite3MutexInit()        SQLITE_OK
   8992 #define sqlite3MutexEnd()
   8993 #endif /* defined(SQLITE_MUTEX_OMIT) */
   8994 
   8995 /************** End of mutex.h ***********************************************/
   8996 /************** Continuing where we left off in sqliteInt.h ******************/
   8997 
   8998 
   8999 /*
   9000 ** Each database file to be accessed by the system is an instance
   9001 ** of the following structure.  There are normally two of these structures
   9002 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
   9003 ** aDb[1] is the database file used to hold temporary tables.  Additional
   9004 ** databases may be attached.
   9005 */
   9006 struct Db {
   9007   char *zName;         /* Name of this database */
   9008   Btree *pBt;          /* The B*Tree structure for this database file */
   9009   u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
   9010   u8 safety_level;     /* How aggressive at syncing data to disk */
   9011   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
   9012 };
   9013 
   9014 /*
   9015 ** An instance of the following structure stores a database schema.
   9016 **
   9017 ** Most Schema objects are associated with a Btree.  The exception is
   9018 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
   9019 ** In shared cache mode, a single Schema object can be shared by multiple
   9020 ** Btrees that refer to the same underlying BtShared object.
   9021 **
   9022 ** Schema objects are automatically deallocated when the last Btree that
   9023 ** references them is destroyed.   The TEMP Schema is manually freed by
   9024 ** sqlite3_close().
   9025 *
   9026 ** A thread must be holding a mutex on the corresponding Btree in order
   9027 ** to access Schema content.  This implies that the thread must also be
   9028 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
   9029 ** For a TEMP Schema, on the connection mutex is required.
   9030 */
   9031 struct Schema {
   9032   int schema_cookie;   /* Database schema version number for this file */
   9033   int iGeneration;     /* Generation counter.  Incremented with each change */
   9034   Hash tblHash;        /* All tables indexed by name */
   9035   Hash idxHash;        /* All (named) indices indexed by name */
   9036   Hash trigHash;       /* All triggers indexed by name */
   9037   Hash fkeyHash;       /* All foreign keys by referenced table name */
   9038   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
   9039   u8 file_format;      /* Schema format version for this file */
   9040   u8 enc;              /* Text encoding used by this database */
   9041   u16 flags;           /* Flags associated with this schema */
   9042   int cache_size;      /* Number of pages to use in the cache */
   9043 };
   9044 
   9045 /*
   9046 ** These macros can be used to test, set, or clear bits in the
   9047 ** Db.pSchema->flags field.
   9048 */
   9049 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
   9050 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
   9051 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
   9052 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
   9053 
   9054 /*
   9055 ** Allowed values for the DB.pSchema->flags field.
   9056 **
   9057 ** The DB_SchemaLoaded flag is set after the database schema has been
   9058 ** read into internal hash tables.
   9059 **
   9060 ** DB_UnresetViews means that one or more views have column names that
   9061 ** have been filled out.  If the schema changes, these column names might
   9062 ** changes and so the view will need to be reset.
   9063 */
   9064 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
   9065 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
   9066 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
   9067 
   9068 /*
   9069 ** The number of different kinds of things that can be limited
   9070 ** using the sqlite3_limit() interface.
   9071 */
   9072 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
   9073 
   9074 /*
   9075 ** Lookaside malloc is a set of fixed-size buffers that can be used
   9076 ** to satisfy small transient memory allocation requests for objects
   9077 ** associated with a particular database connection.  The use of
   9078 ** lookaside malloc provides a significant performance enhancement
   9079 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
   9080 ** SQL statements.
   9081 **
   9082 ** The Lookaside structure holds configuration information about the
   9083 ** lookaside malloc subsystem.  Each available memory allocation in
   9084 ** the lookaside subsystem is stored on a linked list of LookasideSlot
   9085 ** objects.
   9086 **
   9087 ** Lookaside allocations are only allowed for objects that are associated
   9088 ** with a particular database connection.  Hence, schema information cannot
   9089 ** be stored in lookaside because in shared cache mode the schema information
   9090 ** is shared by multiple database connections.  Therefore, while parsing
   9091 ** schema information, the Lookaside.bEnabled flag is cleared so that
   9092 ** lookaside allocations are not used to construct the schema objects.
   9093 */
   9094 struct Lookaside {
   9095   u16 sz;                 /* Size of each buffer in bytes */
   9096   u8 bEnabled;            /* False to disable new lookaside allocations */
   9097   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
   9098   int nOut;               /* Number of buffers currently checked out */
   9099   int mxOut;              /* Highwater mark for nOut */
   9100   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
   9101   LookasideSlot *pFree;   /* List of available buffers */
   9102   void *pStart;           /* First byte of available memory space */
   9103   void *pEnd;             /* First byte past end of available space */
   9104 };
   9105 struct LookasideSlot {
   9106   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
   9107 };
   9108 
   9109 /*
   9110 ** A hash table for function definitions.
   9111 **
   9112 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
   9113 ** Collisions are on the FuncDef.pHash chain.
   9114 */
   9115 struct FuncDefHash {
   9116   FuncDef *a[23];       /* Hash table for functions */
   9117 };
   9118 
   9119 /*
   9120 ** Each database connection is an instance of the following structure.
   9121 **
   9122 ** The sqlite.lastRowid records the last insert rowid generated by an
   9123 ** insert statement.  Inserts on views do not affect its value.  Each
   9124 ** trigger has its own context, so that lastRowid can be updated inside
   9125 ** triggers as usual.  The previous value will be restored once the trigger
   9126 ** exits.  Upon entering a before or instead of trigger, lastRowid is no
   9127 ** longer (since after version 2.8.12) reset to -1.
   9128 **
   9129 ** The sqlite.nChange does not count changes within triggers and keeps no
   9130 ** context.  It is reset at start of sqlite3_exec.
   9131 ** The sqlite.lsChange represents the number of changes made by the last
   9132 ** insert, update, or delete statement.  It remains constant throughout the
   9133 ** length of a statement and is then updated by OP_SetCounts.  It keeps a
   9134 ** context stack just like lastRowid so that the count of changes
   9135 ** within a trigger is not seen outside the trigger.  Changes to views do not
   9136 ** affect the value of lsChange.
   9137 ** The sqlite.csChange keeps track of the number of current changes (since
   9138 ** the last statement) and is used to update sqlite_lsChange.
   9139 **
   9140 ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
   9141 ** store the most recent error code and, if applicable, string. The
   9142 ** internal function sqlite3Error() is used to set these variables
   9143 ** consistently.
   9144 */
   9145 struct sqlite3 {
   9146   sqlite3_vfs *pVfs;            /* OS Interface */
   9147   int nDb;                      /* Number of backends currently in use */
   9148   Db *aDb;                      /* All backends */
   9149   int flags;                    /* Miscellaneous flags. See below */
   9150   int openFlags;                /* Flags passed to sqlite3_vfs.xOpen() */
   9151   int errCode;                  /* Most recent error code (SQLITE_*) */
   9152   int errMask;                  /* & result codes with this before returning */
   9153   u8 autoCommit;                /* The auto-commit flag. */
   9154   u8 temp_store;                /* 1: file 2: memory 0: default */
   9155   u8 mallocFailed;              /* True if we have seen a malloc failure */
   9156   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
   9157   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
   9158   u8 suppressErr;               /* Do not issue error messages if true */
   9159   int nextPagesize;             /* Pagesize after VACUUM if >0 */
   9160   int nTable;                   /* Number of tables in the database */
   9161   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
   9162   i64 lastRowid;                /* ROWID of most recent insert (see above) */
   9163   u32 magic;                    /* Magic number for detect library misuse */
   9164   int nChange;                  /* Value returned by sqlite3_changes() */
   9165   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
   9166   sqlite3_mutex *mutex;         /* Connection mutex */
   9167   int aLimit[SQLITE_N_LIMIT];   /* Limits */
   9168   struct sqlite3InitInfo {      /* Information used during initialization */
   9169     int iDb;                    /* When back is being initialized */
   9170     int newTnum;                /* Rootpage of table being initialized */
   9171     u8 busy;                    /* TRUE if currently initializing */
   9172     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
   9173   } init;
   9174   int nExtension;               /* Number of loaded extensions */
   9175   void **aExtension;            /* Array of shared library handles */
   9176   struct Vdbe *pVdbe;           /* List of active virtual machines */
   9177   int activeVdbeCnt;            /* Number of VDBEs currently executing */
   9178   int writeVdbeCnt;             /* Number of active VDBEs that are writing */
   9179   int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
   9180   void (*xTrace)(void*,const char*);        /* Trace function */
   9181   void *pTraceArg;                          /* Argument to the trace function */
   9182   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
   9183   void *pProfileArg;                        /* Argument to profile function */
   9184   void *pCommitArg;                 /* Argument to xCommitCallback() */
   9185   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
   9186   void *pRollbackArg;               /* Argument to xRollbackCallback() */
   9187   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
   9188   void *pUpdateArg;
   9189   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
   9190 #ifndef SQLITE_OMIT_WAL
   9191   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
   9192   void *pWalArg;
   9193 #endif
   9194   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
   9195   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
   9196   void *pCollNeededArg;
   9197   sqlite3_value *pErr;          /* Most recent error message */
   9198   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
   9199   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
   9200   union {
   9201     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
   9202     double notUsed1;            /* Spacer */
   9203   } u1;
   9204   Lookaside lookaside;          /* Lookaside malloc configuration */
   9205 #ifndef SQLITE_OMIT_AUTHORIZATION
   9206   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
   9207                                 /* Access authorization function */
   9208   void *pAuthArg;               /* 1st argument to the access auth function */
   9209 #endif
   9210 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   9211   int (*xProgress)(void *);     /* The progress callback */
   9212   void *pProgressArg;           /* Argument to the progress callback */
   9213   int nProgressOps;             /* Number of opcodes for progress callback */
   9214 #endif
   9215 #ifndef SQLITE_OMIT_VIRTUALTABLE
   9216   Hash aModule;                 /* populated by sqlite3_create_module() */
   9217   Table *pVTab;                 /* vtab with active Connect/Create method */
   9218   VTable **aVTrans;             /* Virtual tables with open transactions */
   9219   int nVTrans;                  /* Allocated size of aVTrans */
   9220   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
   9221 #endif
   9222   FuncDefHash aFunc;            /* Hash table of connection functions */
   9223   Hash aCollSeq;                /* All collating sequences */
   9224   BusyHandler busyHandler;      /* Busy callback */
   9225   int busyTimeout;              /* Busy handler timeout, in msec */
   9226   Db aDbStatic[2];              /* Static space for the 2 default backends */
   9227   Savepoint *pSavepoint;        /* List of active savepoints */
   9228   int nSavepoint;               /* Number of non-transaction savepoints */
   9229   int nStatement;               /* Number of nested statement-transactions  */
   9230   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
   9231   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
   9232   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
   9233 
   9234 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   9235   /* The following variables are all protected by the STATIC_MASTER
   9236   ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
   9237   **
   9238   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
   9239   ** unlock so that it can proceed.
   9240   **
   9241   ** When X.pBlockingConnection==Y, that means that something that X tried
   9242   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
   9243   ** held by Y.
   9244   */
   9245   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
   9246   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
   9247   void *pUnlockArg;                     /* Argument to xUnlockNotify */
   9248   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
   9249   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
   9250 #endif
   9251 };
   9252 
   9253 /*
   9254 ** A macro to discover the encoding of a database.
   9255 */
   9256 #define ENC(db) ((db)->aDb[0].pSchema->enc)
   9257 
   9258 /*
   9259 ** Possible values for the sqlite3.flags.
   9260 */
   9261 #define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
   9262 #define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
   9263 #define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
   9264 #define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
   9265 #define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
   9266                                           /*   DELETE, or UPDATE and return */
   9267                                           /*   the count using a callback. */
   9268 #define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
   9269                                           /*   result set is empty */
   9270 #define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
   9271 #define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
   9272 #define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
   9273 #define SQLITE_NoReadlock     0x00020000  /* Readlocks are omitted when
   9274                                           ** accessing read-only databases */
   9275 #define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
   9276 #define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
   9277 #define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
   9278 #define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
   9279 #define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
   9280 #define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
   9281 #define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
   9282 #define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
   9283 #define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
   9284 #define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
   9285 #define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
   9286 #define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
   9287 #define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
   9288 
   9289 /*
   9290 ** Bits of the sqlite3.flags field that are used by the
   9291 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
   9292 ** These must be the low-order bits of the flags field.
   9293 */
   9294 #define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
   9295 #define SQLITE_ColumnCache    0x02        /* Disable the column cache */
   9296 #define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
   9297 #define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
   9298 #define SQLITE_IndexCover     0x10        /* Disable index covering table */
   9299 #define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
   9300 #define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
   9301 #define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
   9302 
   9303 /*
   9304 ** Possible values for the sqlite.magic field.
   9305 ** The numbers are obtained at random and have no special meaning, other
   9306 ** than being distinct from one another.
   9307 */
   9308 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
   9309 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
   9310 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
   9311 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
   9312 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
   9313 
   9314 /*
   9315 ** Each SQL function is defined by an instance of the following
   9316 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
   9317 ** hash table.  When multiple functions have the same name, the hash table
   9318 ** points to a linked list of these structures.
   9319 */
   9320 struct FuncDef {
   9321   i16 nArg;            /* Number of arguments.  -1 means unlimited */
   9322   u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
   9323   u8 flags;            /* Some combination of SQLITE_FUNC_* */
   9324   void *pUserData;     /* User data parameter */
   9325   FuncDef *pNext;      /* Next function with same name */
   9326   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
   9327   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
   9328   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
   9329   char *zName;         /* SQL name of the function. */
   9330   FuncDef *pHash;      /* Next with a different name but the same hash */
   9331   FuncDestructor *pDestructor;   /* Reference counted destructor function */
   9332 };
   9333 
   9334 /*
   9335 ** This structure encapsulates a user-function destructor callback (as
   9336 ** configured using create_function_v2()) and a reference counter. When
   9337 ** create_function_v2() is called to create a function with a destructor,
   9338 ** a single object of this type is allocated. FuncDestructor.nRef is set to
   9339 ** the number of FuncDef objects created (either 1 or 3, depending on whether
   9340 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
   9341 ** member of each of the new FuncDef objects is set to point to the allocated
   9342 ** FuncDestructor.
   9343 **
   9344 ** Thereafter, when one of the FuncDef objects is deleted, the reference
   9345 ** count on this object is decremented. When it reaches 0, the destructor
   9346 ** is invoked and the FuncDestructor structure freed.
   9347 */
   9348 struct FuncDestructor {
   9349   int nRef;
   9350   void (*xDestroy)(void *);
   9351   void *pUserData;
   9352 };
   9353 
   9354 /*
   9355 ** Possible values for FuncDef.flags
   9356 */
   9357 #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
   9358 #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
   9359 #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
   9360 #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
   9361 #define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
   9362 #define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
   9363 #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
   9364 
   9365 /*
   9366 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
   9367 ** used to create the initializers for the FuncDef structures.
   9368 **
   9369 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
   9370 **     Used to create a scalar function definition of a function zName
   9371 **     implemented by C function xFunc that accepts nArg arguments. The
   9372 **     value passed as iArg is cast to a (void*) and made available
   9373 **     as the user-data (sqlite3_user_data()) for the function. If
   9374 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
   9375 **
   9376 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
   9377 **     Used to create an aggregate function definition implemented by
   9378 **     the C functions xStep and xFinal. The first four parameters
   9379 **     are interpreted in the same way as the first 4 parameters to
   9380 **     FUNCTION().
   9381 **
   9382 **   LIKEFUNC(zName, nArg, pArg, flags)
   9383 **     Used to create a scalar function definition of a function zName
   9384 **     that accepts nArg arguments and is implemented by a call to C
   9385 **     function likeFunc. Argument pArg is cast to a (void *) and made
   9386 **     available as the function user-data (sqlite3_user_data()). The
   9387 **     FuncDef.flags variable is set to the value passed as the flags
   9388 **     parameter.
   9389 */
   9390 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
   9391   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
   9392    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
   9393 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
   9394   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
   9395    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
   9396 #define LIKEFUNC(zName, nArg, arg, flags) \
   9397   {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
   9398 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
   9399   {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
   9400    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
   9401 
   9402 /*
   9403 ** All current savepoints are stored in a linked list starting at
   9404 ** sqlite3.pSavepoint. The first element in the list is the most recently
   9405 ** opened savepoint. Savepoints are added to the list by the vdbe
   9406 ** OP_Savepoint instruction.
   9407 */
   9408 struct Savepoint {
   9409   char *zName;                        /* Savepoint name (nul-terminated) */
   9410   i64 nDeferredCons;                  /* Number of deferred fk violations */
   9411   Savepoint *pNext;                   /* Parent savepoint (if any) */
   9412 };
   9413 
   9414 /*
   9415 ** The following are used as the second parameter to sqlite3Savepoint(),
   9416 ** and as the P1 argument to the OP_Savepoint instruction.
   9417 */
   9418 #define SAVEPOINT_BEGIN      0
   9419 #define SAVEPOINT_RELEASE    1
   9420 #define SAVEPOINT_ROLLBACK   2
   9421 
   9422 
   9423 /*
   9424 ** Each SQLite module (virtual table definition) is defined by an
   9425 ** instance of the following structure, stored in the sqlite3.aModule
   9426 ** hash table.
   9427 */
   9428 struct Module {
   9429   const sqlite3_module *pModule;       /* Callback pointers */
   9430   const char *zName;                   /* Name passed to create_module() */
   9431   void *pAux;                          /* pAux passed to create_module() */
   9432   void (*xDestroy)(void *);            /* Module destructor function */
   9433 };
   9434 
   9435 /*
   9436 ** information about each column of an SQL table is held in an instance
   9437 ** of this structure.
   9438 */
   9439 struct Column {
   9440   char *zName;     /* Name of this column */
   9441   Expr *pDflt;     /* Default value of this column */
   9442   char *zDflt;     /* Original text of the default value */
   9443   char *zType;     /* Data type for this column */
   9444   char *zColl;     /* Collating sequence.  If NULL, use the default */
   9445   u8 notNull;      /* True if there is a NOT NULL constraint */
   9446   u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
   9447   char affinity;   /* One of the SQLITE_AFF_... values */
   9448 #ifndef SQLITE_OMIT_VIRTUALTABLE
   9449   u8 isHidden;     /* True if this column is 'hidden' */
   9450 #endif
   9451 };
   9452 
   9453 /*
   9454 ** A "Collating Sequence" is defined by an instance of the following
   9455 ** structure. Conceptually, a collating sequence consists of a name and
   9456 ** a comparison routine that defines the order of that sequence.
   9457 **
   9458 ** There may two separate implementations of the collation function, one
   9459 ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
   9460 ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
   9461 ** native byte order. When a collation sequence is invoked, SQLite selects
   9462 ** the version that will require the least expensive encoding
   9463 ** translations, if any.
   9464 **
   9465 ** The CollSeq.pUser member variable is an extra parameter that passed in
   9466 ** as the first argument to the UTF-8 comparison function, xCmp.
   9467 ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
   9468 ** xCmp16.
   9469 **
   9470 ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
   9471 ** collating sequence is undefined.  Indices built on an undefined
   9472 ** collating sequence may not be read or written.
   9473 */
   9474 struct CollSeq {
   9475   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
   9476   u8 enc;               /* Text encoding handled by xCmp() */
   9477   u8 type;              /* One of the SQLITE_COLL_... values below */
   9478   void *pUser;          /* First argument to xCmp() */
   9479   int (*xCmp)(void*,int, const void*, int, const void*);
   9480   void (*xDel)(void*);  /* Destructor for pUser */
   9481 };
   9482 
   9483 /*
   9484 ** Allowed values of CollSeq.type:
   9485 */
   9486 #define SQLITE_COLL_BINARY  1  /* The default memcmp() collating sequence */
   9487 #define SQLITE_COLL_NOCASE  2  /* The built-in NOCASE collating sequence */
   9488 #define SQLITE_COLL_REVERSE 3  /* The built-in REVERSE collating sequence */
   9489 #define SQLITE_COLL_USER    0  /* Any other user-defined collating sequence */
   9490 
   9491 /*
   9492 ** A sort order can be either ASC or DESC.
   9493 */
   9494 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
   9495 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
   9496 
   9497 /*
   9498 ** Column affinity types.
   9499 **
   9500 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
   9501 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
   9502 ** the speed a little by numbering the values consecutively.
   9503 **
   9504 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
   9505 ** when multiple affinity types are concatenated into a string and
   9506 ** used as the P4 operand, they will be more readable.
   9507 **
   9508 ** Note also that the numeric types are grouped together so that testing
   9509 ** for a numeric type is a single comparison.
   9510 */
   9511 #define SQLITE_AFF_TEXT     'a'
   9512 #define SQLITE_AFF_NONE     'b'
   9513 #define SQLITE_AFF_NUMERIC  'c'
   9514 #define SQLITE_AFF_INTEGER  'd'
   9515 #define SQLITE_AFF_REAL     'e'
   9516 
   9517 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
   9518 
   9519 /*
   9520 ** The SQLITE_AFF_MASK values masks off the significant bits of an
   9521 ** affinity value.
   9522 */
   9523 #define SQLITE_AFF_MASK     0x67
   9524 
   9525 /*
   9526 ** Additional bit values that can be ORed with an affinity without
   9527 ** changing the affinity.
   9528 */
   9529 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
   9530 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
   9531 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
   9532 
   9533 /*
   9534 ** An object of this type is created for each virtual table present in
   9535 ** the database schema.
   9536 **
   9537 ** If the database schema is shared, then there is one instance of this
   9538 ** structure for each database connection (sqlite3*) that uses the shared
   9539 ** schema. This is because each database connection requires its own unique
   9540 ** instance of the sqlite3_vtab* handle used to access the virtual table
   9541 ** implementation. sqlite3_vtab* handles can not be shared between
   9542 ** database connections, even when the rest of the in-memory database
   9543 ** schema is shared, as the implementation often stores the database
   9544 ** connection handle passed to it via the xConnect() or xCreate() method
   9545 ** during initialization internally. This database connection handle may
   9546 ** then be used by the virtual table implementation to access real tables
   9547 ** within the database. So that they appear as part of the callers
   9548 ** transaction, these accesses need to be made via the same database
   9549 ** connection as that used to execute SQL operations on the virtual table.
   9550 **
   9551 ** All VTable objects that correspond to a single table in a shared
   9552 ** database schema are initially stored in a linked-list pointed to by
   9553 ** the Table.pVTable member variable of the corresponding Table object.
   9554 ** When an sqlite3_prepare() operation is required to access the virtual
   9555 ** table, it searches the list for the VTable that corresponds to the
   9556 ** database connection doing the preparing so as to use the correct
   9557 ** sqlite3_vtab* handle in the compiled query.
   9558 **
   9559 ** When an in-memory Table object is deleted (for example when the
   9560 ** schema is being reloaded for some reason), the VTable objects are not
   9561 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
   9562 ** immediately. Instead, they are moved from the Table.pVTable list to
   9563 ** another linked list headed by the sqlite3.pDisconnect member of the
   9564 ** corresponding sqlite3 structure. They are then deleted/xDisconnected
   9565 ** next time a statement is prepared using said sqlite3*. This is done
   9566 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
   9567 ** Refer to comments above function sqlite3VtabUnlockList() for an
   9568 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
   9569 ** list without holding the corresponding sqlite3.mutex mutex.
   9570 **
   9571 ** The memory for objects of this type is always allocated by
   9572 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
   9573 ** the first argument.
   9574 */
   9575 struct VTable {
   9576   sqlite3 *db;              /* Database connection associated with this table */
   9577   Module *pMod;             /* Pointer to module implementation */
   9578   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
   9579   int nRef;                 /* Number of pointers to this structure */
   9580   VTable *pNext;            /* Next in linked list (see above) */
   9581 };
   9582 
   9583 /*
   9584 ** Each SQL table is represented in memory by an instance of the
   9585 ** following structure.
   9586 **
   9587 ** Table.zName is the name of the table.  The case of the original
   9588 ** CREATE TABLE statement is stored, but case is not significant for
   9589 ** comparisons.
   9590 **
   9591 ** Table.nCol is the number of columns in this table.  Table.aCol is a
   9592 ** pointer to an array of Column structures, one for each column.
   9593 **
   9594 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
   9595 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
   9596 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
   9597 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
   9598 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
   9599 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
   9600 ** the table has any PRIMARY KEY, INTEGER or otherwise.
   9601 **
   9602 ** Table.tnum is the page number for the root BTree page of the table in the
   9603 ** database file.  If Table.iDb is the index of the database table backend
   9604 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
   9605 ** holds temporary tables and indices.  If TF_Ephemeral is set
   9606 ** then the table is stored in a file that is automatically deleted
   9607 ** when the VDBE cursor to the table is closed.  In this case Table.tnum
   9608 ** refers VDBE cursor number that holds the table open, not to the root
   9609 ** page number.  Transient tables are used to hold the results of a
   9610 ** sub-query that appears instead of a real table name in the FROM clause
   9611 ** of a SELECT statement.
   9612 */
   9613 struct Table {
   9614   char *zName;         /* Name of the table or view */
   9615   int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
   9616   int nCol;            /* Number of columns in this table */
   9617   Column *aCol;        /* Information about each column */
   9618   Index *pIndex;       /* List of SQL indexes on this table. */
   9619   int tnum;            /* Root BTree node for this table (see note above) */
   9620   unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
   9621   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
   9622   u16 nRef;            /* Number of pointers to this Table */
   9623   u8 tabFlags;         /* Mask of TF_* values */
   9624   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
   9625   FKey *pFKey;         /* Linked list of all foreign keys in this table */
   9626   char *zColAff;       /* String defining the affinity of each column */
   9627 #ifndef SQLITE_OMIT_CHECK
   9628   Expr *pCheck;        /* The AND of all CHECK constraints */
   9629 #endif
   9630 #ifndef SQLITE_OMIT_ALTERTABLE
   9631   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
   9632 #endif
   9633 #ifndef SQLITE_OMIT_VIRTUALTABLE
   9634   VTable *pVTable;     /* List of VTable objects. */
   9635   int nModuleArg;      /* Number of arguments to the module */
   9636   char **azModuleArg;  /* Text of all module args. [0] is module name */
   9637 #endif
   9638   Trigger *pTrigger;   /* List of triggers stored in pSchema */
   9639   Schema *pSchema;     /* Schema that contains this table */
   9640   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
   9641 };
   9642 
   9643 /*
   9644 ** Allowed values for Tabe.tabFlags.
   9645 */
   9646 #define TF_Readonly        0x01    /* Read-only system table */
   9647 #define TF_Ephemeral       0x02    /* An ephemeral table */
   9648 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
   9649 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
   9650 #define TF_Virtual         0x10    /* Is a virtual table */
   9651 #define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
   9652 
   9653 
   9654 
   9655 /*
   9656 ** Test to see whether or not a table is a virtual table.  This is
   9657 ** done as a macro so that it will be optimized out when virtual
   9658 ** table support is omitted from the build.
   9659 */
   9660 #ifndef SQLITE_OMIT_VIRTUALTABLE
   9661 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
   9662 #  define IsHiddenColumn(X) ((X)->isHidden)
   9663 #else
   9664 #  define IsVirtual(X)      0
   9665 #  define IsHiddenColumn(X) 0
   9666 #endif
   9667 
   9668 /*
   9669 ** Each foreign key constraint is an instance of the following structure.
   9670 **
   9671 ** A foreign key is associated with two tables.  The "from" table is
   9672 ** the table that contains the REFERENCES clause that creates the foreign
   9673 ** key.  The "to" table is the table that is named in the REFERENCES clause.
   9674 ** Consider this example:
   9675 **
   9676 **     CREATE TABLE ex1(
   9677 **       a INTEGER PRIMARY KEY,
   9678 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
   9679 **     );
   9680 **
   9681 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
   9682 **
   9683 ** Each REFERENCES clause generates an instance of the following structure
   9684 ** which is attached to the from-table.  The to-table need not exist when
   9685 ** the from-table is created.  The existence of the to-table is not checked.
   9686 */
   9687 struct FKey {
   9688   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
   9689   FKey *pNextFrom;  /* Next foreign key in pFrom */
   9690   char *zTo;        /* Name of table that the key points to (aka: Parent) */
   9691   FKey *pNextTo;    /* Next foreign key on table named zTo */
   9692   FKey *pPrevTo;    /* Previous foreign key on table named zTo */
   9693   int nCol;         /* Number of columns in this key */
   9694   /* EV: R-30323-21917 */
   9695   u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
   9696   u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
   9697   Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
   9698   struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
   9699     int iFrom;         /* Index of column in pFrom */
   9700     char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
   9701   } aCol[1];        /* One entry for each of nCol column s */
   9702 };
   9703 
   9704 /*
   9705 ** SQLite supports many different ways to resolve a constraint
   9706 ** error.  ROLLBACK processing means that a constraint violation
   9707 ** causes the operation in process to fail and for the current transaction
   9708 ** to be rolled back.  ABORT processing means the operation in process
   9709 ** fails and any prior changes from that one operation are backed out,
   9710 ** but the transaction is not rolled back.  FAIL processing means that
   9711 ** the operation in progress stops and returns an error code.  But prior
   9712 ** changes due to the same operation are not backed out and no rollback
   9713 ** occurs.  IGNORE means that the particular row that caused the constraint
   9714 ** error is not inserted or updated.  Processing continues and no error
   9715 ** is returned.  REPLACE means that preexisting database rows that caused
   9716 ** a UNIQUE constraint violation are removed so that the new insert or
   9717 ** update can proceed.  Processing continues and no error is reported.
   9718 **
   9719 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
   9720 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
   9721 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
   9722 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
   9723 ** referenced table row is propagated into the row that holds the
   9724 ** foreign key.
   9725 **
   9726 ** The following symbolic values are used to record which type
   9727 ** of action to take.
   9728 */
   9729 #define OE_None     0   /* There is no constraint to check */
   9730 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
   9731 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
   9732 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
   9733 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
   9734 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
   9735 
   9736 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
   9737 #define OE_SetNull  7   /* Set the foreign key value to NULL */
   9738 #define OE_SetDflt  8   /* Set the foreign key value to its default */
   9739 #define OE_Cascade  9   /* Cascade the changes */
   9740 
   9741 #define OE_Default  99  /* Do whatever the default action is */
   9742 
   9743 
   9744 /*
   9745 ** An instance of the following structure is passed as the first
   9746 ** argument to sqlite3VdbeKeyCompare and is used to control the
   9747 ** comparison of the two index keys.
   9748 */
   9749 struct KeyInfo {
   9750   sqlite3 *db;        /* The database connection */
   9751   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
   9752   u16 nField;         /* Number of entries in aColl[] */
   9753   u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
   9754   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
   9755 };
   9756 
   9757 /*
   9758 ** An instance of the following structure holds information about a
   9759 ** single index record that has already been parsed out into individual
   9760 ** values.
   9761 **
   9762 ** A record is an object that contains one or more fields of data.
   9763 ** Records are used to store the content of a table row and to store
   9764 ** the key of an index.  A blob encoding of a record is created by
   9765 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
   9766 ** OP_Column opcode.
   9767 **
   9768 ** This structure holds a record that has already been disassembled
   9769 ** into its constituent fields.
   9770 */
   9771 struct UnpackedRecord {
   9772   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
   9773   u16 nField;         /* Number of entries in apMem[] */
   9774   u16 flags;          /* Boolean settings.  UNPACKED_... below */
   9775   i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
   9776   Mem *aMem;          /* Values */
   9777 };
   9778 
   9779 /*
   9780 ** Allowed values of UnpackedRecord.flags
   9781 */
   9782 #define UNPACKED_NEED_FREE     0x0001  /* Memory is from sqlite3Malloc() */
   9783 #define UNPACKED_NEED_DESTROY  0x0002  /* apMem[]s should all be destroyed */
   9784 #define UNPACKED_IGNORE_ROWID  0x0004  /* Ignore trailing rowid on key1 */
   9785 #define UNPACKED_INCRKEY       0x0008  /* Make this key an epsilon larger */
   9786 #define UNPACKED_PREFIX_MATCH  0x0010  /* A prefix match is considered OK */
   9787 #define UNPACKED_PREFIX_SEARCH 0x0020  /* A prefix match is considered OK */
   9788 
   9789 /*
   9790 ** Each SQL index is represented in memory by an
   9791 ** instance of the following structure.
   9792 **
   9793 ** The columns of the table that are to be indexed are described
   9794 ** by the aiColumn[] field of this structure.  For example, suppose
   9795 ** we have the following table and index:
   9796 **
   9797 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
   9798 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
   9799 **
   9800 ** In the Table structure describing Ex1, nCol==3 because there are
   9801 ** three columns in the table.  In the Index structure describing
   9802 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
   9803 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
   9804 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
   9805 ** The second column to be indexed (c1) has an index of 0 in
   9806 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
   9807 **
   9808 ** The Index.onError field determines whether or not the indexed columns
   9809 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
   9810 ** it means this is not a unique index.  Otherwise it is a unique index
   9811 ** and the value of Index.onError indicate the which conflict resolution
   9812 ** algorithm to employ whenever an attempt is made to insert a non-unique
   9813 ** element.
   9814 */
   9815 struct Index {
   9816   char *zName;     /* Name of this index */
   9817   int nColumn;     /* Number of columns in the table used by this index */
   9818   int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
   9819   unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
   9820   Table *pTable;   /* The SQL table being indexed */
   9821   int tnum;        /* Page containing root of this index in database file */
   9822   u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
   9823   u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
   9824   u8 bUnordered;   /* Use this index for == or IN queries only */
   9825   char *zColAff;   /* String defining the affinity of each column */
   9826   Index *pNext;    /* The next index associated with the same table */
   9827   Schema *pSchema; /* Schema containing this index */
   9828   u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
   9829   char **azColl;   /* Array of collation sequence names for index */
   9830   IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
   9831 };
   9832 
   9833 /*
   9834 ** Each sample stored in the sqlite_stat2 table is represented in memory
   9835 ** using a structure of this type.
   9836 */
   9837 struct IndexSample {
   9838   union {
   9839     char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
   9840     double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
   9841   } u;
   9842   u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
   9843   u8 nByte;         /* Size in byte of text or blob. */
   9844 };
   9845 
   9846 /*
   9847 ** Each token coming out of the lexer is an instance of
   9848 ** this structure.  Tokens are also used as part of an expression.
   9849 **
   9850 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
   9851 ** may contain random values.  Do not make any assumptions about Token.dyn
   9852 ** and Token.n when Token.z==0.
   9853 */
   9854 struct Token {
   9855   const char *z;     /* Text of the token.  Not NULL-terminated! */
   9856   unsigned int n;    /* Number of characters in this token */
   9857 };
   9858 
   9859 /*
   9860 ** An instance of this structure contains information needed to generate
   9861 ** code for a SELECT that contains aggregate functions.
   9862 **
   9863 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
   9864 ** pointer to this structure.  The Expr.iColumn field is the index in
   9865 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
   9866 ** code for that node.
   9867 **
   9868 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
   9869 ** original Select structure that describes the SELECT statement.  These
   9870 ** fields do not need to be freed when deallocating the AggInfo structure.
   9871 */
   9872 struct AggInfo {
   9873   u8 directMode;          /* Direct rendering mode means take data directly
   9874                           ** from source tables rather than from accumulators */
   9875   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
   9876                           ** than the source table */
   9877   int sortingIdx;         /* Cursor number of the sorting index */
   9878   ExprList *pGroupBy;     /* The group by clause */
   9879   int nSortingColumn;     /* Number of columns in the sorting index */
   9880   struct AggInfo_col {    /* For each column used in source tables */
   9881     Table *pTab;             /* Source table */
   9882     int iTable;              /* Cursor number of the source table */
   9883     int iColumn;             /* Column number within the source table */
   9884     int iSorterColumn;       /* Column number in the sorting index */
   9885     int iMem;                /* Memory location that acts as accumulator */
   9886     Expr *pExpr;             /* The original expression */
   9887   } *aCol;
   9888   int nColumn;            /* Number of used entries in aCol[] */
   9889   int nColumnAlloc;       /* Number of slots allocated for aCol[] */
   9890   int nAccumulator;       /* Number of columns that show through to the output.
   9891                           ** Additional columns are used only as parameters to
   9892                           ** aggregate functions */
   9893   struct AggInfo_func {   /* For each aggregate function */
   9894     Expr *pExpr;             /* Expression encoding the function */
   9895     FuncDef *pFunc;          /* The aggregate function implementation */
   9896     int iMem;                /* Memory location that acts as accumulator */
   9897     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
   9898   } *aFunc;
   9899   int nFunc;              /* Number of entries in aFunc[] */
   9900   int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
   9901 };
   9902 
   9903 /*
   9904 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
   9905 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
   9906 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
   9907 ** it uses less memory in the Expr object, which is a big memory user
   9908 ** in systems with lots of prepared statements.  And few applications
   9909 ** need more than about 10 or 20 variables.  But some extreme users want
   9910 ** to have prepared statements with over 32767 variables, and for them
   9911 ** the option is available (at compile-time).
   9912 */
   9913 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
   9914 typedef i16 ynVar;
   9915 #else
   9916 typedef int ynVar;
   9917 #endif
   9918 
   9919 /*
   9920 ** Each node of an expression in the parse tree is an instance
   9921 ** of this structure.
   9922 **
   9923 ** Expr.op is the opcode. The integer parser token codes are reused
   9924 ** as opcodes here. For example, the parser defines TK_GE to be an integer
   9925 ** code representing the ">=" operator. This same integer code is reused
   9926 ** to represent the greater-than-or-equal-to operator in the expression
   9927 ** tree.
   9928 **
   9929 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
   9930 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
   9931 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
   9932 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
   9933 ** then Expr.token contains the name of the function.
   9934 **
   9935 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
   9936 ** binary operator. Either or both may be NULL.
   9937 **
   9938 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
   9939 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
   9940 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
   9941 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
   9942 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
   9943 ** valid.
   9944 **
   9945 ** An expression of the form ID or ID.ID refers to a column in a table.
   9946 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
   9947 ** the integer cursor number of a VDBE cursor pointing to that table and
   9948 ** Expr.iColumn is the column number for the specific column.  If the
   9949 ** expression is used as a result in an aggregate SELECT, then the
   9950 ** value is also stored in the Expr.iAgg column in the aggregate so that
   9951 ** it can be accessed after all aggregates are computed.
   9952 **
   9953 ** If the expression is an unbound variable marker (a question mark
   9954 ** character '?' in the original SQL) then the Expr.iTable holds the index
   9955 ** number for that variable.
   9956 **
   9957 ** If the expression is a subquery then Expr.iColumn holds an integer
   9958 ** register number containing the result of the subquery.  If the
   9959 ** subquery gives a constant result, then iTable is -1.  If the subquery
   9960 ** gives a different answer at different times during statement processing
   9961 ** then iTable is the address of a subroutine that computes the subquery.
   9962 **
   9963 ** If the Expr is of type OP_Column, and the table it is selecting from
   9964 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
   9965 ** corresponding table definition.
   9966 **
   9967 ** ALLOCATION NOTES:
   9968 **
   9969 ** Expr objects can use a lot of memory space in database schema.  To
   9970 ** help reduce memory requirements, sometimes an Expr object will be
   9971 ** truncated.  And to reduce the number of memory allocations, sometimes
   9972 ** two or more Expr objects will be stored in a single memory allocation,
   9973 ** together with Expr.zToken strings.
   9974 **
   9975 ** If the EP_Reduced and EP_TokenOnly flags are set when
   9976 ** an Expr object is truncated.  When EP_Reduced is set, then all
   9977 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
   9978 ** are contained within the same memory allocation.  Note, however, that
   9979 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
   9980 ** allocated, regardless of whether or not EP_Reduced is set.
   9981 */
   9982 struct Expr {
   9983   u8 op;                 /* Operation performed by this node */
   9984   char affinity;         /* The affinity of the column or 0 if not a column */
   9985   u16 flags;             /* Various flags.  EP_* See below */
   9986   union {
   9987     char *zToken;          /* Token value. Zero terminated and dequoted */
   9988     int iValue;            /* Non-negative integer value if EP_IntValue */
   9989   } u;
   9990 
   9991   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
   9992   ** space is allocated for the fields below this point. An attempt to
   9993   ** access them will result in a segfault or malfunction.
   9994   *********************************************************************/
   9995 
   9996   Expr *pLeft;           /* Left subnode */
   9997   Expr *pRight;          /* Right subnode */
   9998   union {
   9999     ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
   10000     Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
   10001   } x;
   10002   CollSeq *pColl;        /* The collation type of the column or 0 */
   10003 
   10004   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
   10005   ** space is allocated for the fields below this point. An attempt to
   10006   ** access them will result in a segfault or malfunction.
   10007   *********************************************************************/
   10008 
   10009   int iTable;            /* TK_COLUMN: cursor number of table holding column
   10010                          ** TK_REGISTER: register number
   10011                          ** TK_TRIGGER: 1 -> new, 0 -> old */
   10012   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
   10013                          ** TK_VARIABLE: variable number (always >= 1). */
   10014   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
   10015   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
   10016   u8 flags2;             /* Second set of flags.  EP2_... */
   10017   u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
   10018   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
   10019   Table *pTab;           /* Table for TK_COLUMN expressions. */
   10020 #if SQLITE_MAX_EXPR_DEPTH>0
   10021   int nHeight;           /* Height of the tree headed by this node */
   10022 #endif
   10023 };
   10024 
   10025 /*
   10026 ** The following are the meanings of bits in the Expr.flags field.
   10027 */
   10028 #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
   10029 #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
   10030 #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
   10031 #define EP_Error      0x0008  /* Expression contains one or more errors */
   10032 #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
   10033 #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
   10034 #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
   10035 #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
   10036 #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
   10037 #define EP_FixedDest  0x0200  /* Result needed in a specific register */
   10038 #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
   10039 #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
   10040 
   10041 #define EP_Reduced    0x1000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
   10042 #define EP_TokenOnly  0x2000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
   10043 #define EP_Static     0x4000  /* Held in memory not obtained from malloc() */
   10044 
   10045 /*
   10046 ** The following are the meanings of bits in the Expr.flags2 field.
   10047 */
   10048 #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
   10049 #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
   10050 
   10051 /*
   10052 ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
   10053 ** flag on an expression structure.  This flag is used for VV&A only.  The
   10054 ** routine is implemented as a macro that only works when in debugging mode,
   10055 ** so as not to burden production code.
   10056 */
   10057 #ifdef SQLITE_DEBUG
   10058 # define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
   10059 #else
   10060 # define ExprSetIrreducible(X)
   10061 #endif
   10062 
   10063 /*
   10064 ** These macros can be used to test, set, or clear bits in the
   10065 ** Expr.flags field.
   10066 */
   10067 #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
   10068 #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
   10069 #define ExprSetProperty(E,P)     (E)->flags|=(P)
   10070 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
   10071 
   10072 /*
   10073 ** Macros to determine the number of bytes required by a normal Expr
   10074 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
   10075 ** and an Expr struct with the EP_TokenOnly flag set.
   10076 */
   10077 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
   10078 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
   10079 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
   10080 
   10081 /*
   10082 ** Flags passed to the sqlite3ExprDup() function. See the header comment
   10083 ** above sqlite3ExprDup() for details.
   10084 */
   10085 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
   10086 
   10087 /*
   10088 ** A list of expressions.  Each expression may optionally have a
   10089 ** name.  An expr/name combination can be used in several ways, such
   10090 ** as the list of "expr AS ID" fields following a "SELECT" or in the
   10091 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
   10092 ** also be used as the argument to a function, in which case the a.zName
   10093 ** field is not used.
   10094 */
   10095 struct ExprList {
   10096   int nExpr;             /* Number of expressions on the list */
   10097   int nAlloc;            /* Number of entries allocated below */
   10098   int iECursor;          /* VDBE Cursor associated with this ExprList */
   10099   struct ExprList_item {
   10100     Expr *pExpr;           /* The list of expressions */
   10101     char *zName;           /* Token associated with this expression */
   10102     char *zSpan;           /* Original text of the expression */
   10103     u8 sortOrder;          /* 1 for DESC or 0 for ASC */
   10104     u8 done;               /* A flag to indicate when processing is finished */
   10105     u16 iCol;              /* For ORDER BY, column number in result set */
   10106     u16 iAlias;            /* Index into Parse.aAlias[] for zName */
   10107   } *a;                  /* One entry for each expression */
   10108 };
   10109 
   10110 /*
   10111 ** An instance of this structure is used by the parser to record both
   10112 ** the parse tree for an expression and the span of input text for an
   10113 ** expression.
   10114 */
   10115 struct ExprSpan {
   10116   Expr *pExpr;          /* The expression parse tree */
   10117   const char *zStart;   /* First character of input text */
   10118   const char *zEnd;     /* One character past the end of input text */
   10119 };
   10120 
   10121 /*
   10122 ** An instance of this structure can hold a simple list of identifiers,
   10123 ** such as the list "a,b,c" in the following statements:
   10124 **
   10125 **      INSERT INTO t(a,b,c) VALUES ...;
   10126 **      CREATE INDEX idx ON t(a,b,c);
   10127 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
   10128 **
   10129 ** The IdList.a.idx field is used when the IdList represents the list of
   10130 ** column names after a table name in an INSERT statement.  In the statement
   10131 **
   10132 **     INSERT INTO t(a,b,c) ...
   10133 **
   10134 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
   10135 */
   10136 struct IdList {
   10137   struct IdList_item {
   10138     char *zName;      /* Name of the identifier */
   10139     int idx;          /* Index in some Table.aCol[] of a column named zName */
   10140   } *a;
   10141   int nId;         /* Number of identifiers on the list */
   10142   int nAlloc;      /* Number of entries allocated for a[] below */
   10143 };
   10144 
   10145 /*
   10146 ** The bitmask datatype defined below is used for various optimizations.
   10147 **
   10148 ** Changing this from a 64-bit to a 32-bit type limits the number of
   10149 ** tables in a join to 32 instead of 64.  But it also reduces the size
   10150 ** of the library by 738 bytes on ix86.
   10151 */
   10152 typedef u64 Bitmask;
   10153 
   10154 /*
   10155 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
   10156 */
   10157 #define BMS  ((int)(sizeof(Bitmask)*8))
   10158 
   10159 /*
   10160 ** The following structure describes the FROM clause of a SELECT statement.
   10161 ** Each table or subquery in the FROM clause is a separate element of
   10162 ** the SrcList.a[] array.
   10163 **
   10164 ** With the addition of multiple database support, the following structure
   10165 ** can also be used to describe a particular table such as the table that
   10166 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
   10167 ** such a table must be a simple name: ID.  But in SQLite, the table can
   10168 ** now be identified by a database name, a dot, then the table name: ID.ID.
   10169 **
   10170 ** The jointype starts out showing the join type between the current table
   10171 ** and the next table on the list.  The parser builds the list this way.
   10172 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
   10173 ** jointype expresses the join between the table and the previous table.
   10174 **
   10175 ** In the colUsed field, the high-order bit (bit 63) is set if the table
   10176 ** contains more than 63 columns and the 64-th or later column is used.
   10177 */
   10178 struct SrcList {
   10179   i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
   10180   i16 nAlloc;      /* Number of entries allocated in a[] below */
   10181   struct SrcList_item {
   10182     char *zDatabase;  /* Name of database holding this table */
   10183     char *zName;      /* Name of the table */
   10184     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
   10185     Table *pTab;      /* An SQL table corresponding to zName */
   10186     Select *pSelect;  /* A SELECT statement used in place of a table name */
   10187     u8 isPopulated;   /* Temporary table associated with SELECT is populated */
   10188     u8 jointype;      /* Type of join between this able and the previous */
   10189     u8 notIndexed;    /* True if there is a NOT INDEXED clause */
   10190 #ifndef SQLITE_OMIT_EXPLAIN
   10191     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
   10192 #endif
   10193     int iCursor;      /* The VDBE cursor number used to access this table */
   10194     Expr *pOn;        /* The ON clause of a join */
   10195     IdList *pUsing;   /* The USING clause of a join */
   10196     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
   10197     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
   10198     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
   10199   } a[1];             /* One entry for each identifier on the list */
   10200 };
   10201 
   10202 /*
   10203 ** Permitted values of the SrcList.a.jointype field
   10204 */
   10205 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
   10206 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
   10207 #define JT_NATURAL   0x0004    /* True for a "natural" join */
   10208 #define JT_LEFT      0x0008    /* Left outer join */
   10209 #define JT_RIGHT     0x0010    /* Right outer join */
   10210 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
   10211 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
   10212 
   10213 
   10214 /*
   10215 ** A WherePlan object holds information that describes a lookup
   10216 ** strategy.
   10217 **
   10218 ** This object is intended to be opaque outside of the where.c module.
   10219 ** It is included here only so that that compiler will know how big it
   10220 ** is.  None of the fields in this object should be used outside of
   10221 ** the where.c module.
   10222 **
   10223 ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
   10224 ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
   10225 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
   10226 ** case that more than one of these conditions is true.
   10227 */
   10228 struct WherePlan {
   10229   u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
   10230   u32 nEq;                       /* Number of == constraints */
   10231   double nRow;                   /* Estimated number of rows (for EQP) */
   10232   union {
   10233     Index *pIdx;                   /* Index when WHERE_INDEXED is true */
   10234     struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
   10235     sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
   10236   } u;
   10237 };
   10238 
   10239 /*
   10240 ** For each nested loop in a WHERE clause implementation, the WhereInfo
   10241 ** structure contains a single instance of this structure.  This structure
   10242 ** is intended to be private the the where.c module and should not be
   10243 ** access or modified by other modules.
   10244 **
   10245 ** The pIdxInfo field is used to help pick the best index on a
   10246 ** virtual table.  The pIdxInfo pointer contains indexing
   10247 ** information for the i-th table in the FROM clause before reordering.
   10248 ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
   10249 ** All other information in the i-th WhereLevel object for the i-th table
   10250 ** after FROM clause ordering.
   10251 */
   10252 struct WhereLevel {
   10253   WherePlan plan;       /* query plan for this element of the FROM clause */
   10254   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
   10255   int iTabCur;          /* The VDBE cursor used to access the table */
   10256   int iIdxCur;          /* The VDBE cursor used to access pIdx */
   10257   int addrBrk;          /* Jump here to break out of the loop */
   10258   int addrNxt;          /* Jump here to start the next IN combination */
   10259   int addrCont;         /* Jump here to continue with the next loop cycle */
   10260   int addrFirst;        /* First instruction of interior of the loop */
   10261   u8 iFrom;             /* Which entry in the FROM clause */
   10262   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
   10263   int p1, p2;           /* Operands of the opcode used to ends the loop */
   10264   union {               /* Information that depends on plan.wsFlags */
   10265     struct {
   10266       int nIn;              /* Number of entries in aInLoop[] */
   10267       struct InLoop {
   10268         int iCur;              /* The VDBE cursor used by this IN operator */
   10269         int addrInTop;         /* Top of the IN loop */
   10270       } *aInLoop;           /* Information about each nested IN operator */
   10271     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
   10272   } u;
   10273 
   10274   /* The following field is really not part of the current level.  But
   10275   ** we need a place to cache virtual table index information for each
   10276   ** virtual table in the FROM clause and the WhereLevel structure is
   10277   ** a convenient place since there is one WhereLevel for each FROM clause
   10278   ** element.
   10279   */
   10280   sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
   10281 };
   10282 
   10283 /*
   10284 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
   10285 ** and the WhereInfo.wctrlFlags member.
   10286 */
   10287 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
   10288 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
   10289 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
   10290 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
   10291 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
   10292 #define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
   10293 #define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
   10294 #define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
   10295 #define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
   10296 
   10297 /*
   10298 ** The WHERE clause processing routine has two halves.  The
   10299 ** first part does the start of the WHERE loop and the second
   10300 ** half does the tail of the WHERE loop.  An instance of
   10301 ** this structure is returned by the first half and passed
   10302 ** into the second half to give some continuity.
   10303 */
   10304 struct WhereInfo {
   10305   Parse *pParse;       /* Parsing and code generating context */
   10306   u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
   10307   u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
   10308   u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
   10309   SrcList *pTabList;             /* List of tables in the join */
   10310   int iTop;                      /* The very beginning of the WHERE loop */
   10311   int iContinue;                 /* Jump here to continue with next record */
   10312   int iBreak;                    /* Jump here to break out of the loop */
   10313   int nLevel;                    /* Number of nested loop */
   10314   struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
   10315   double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
   10316   double nRowOut;                /* Estimated number of output rows */
   10317   WhereLevel a[1];               /* Information about each nest loop in WHERE */
   10318 };
   10319 
   10320 /*
   10321 ** A NameContext defines a context in which to resolve table and column
   10322 ** names.  The context consists of a list of tables (the pSrcList) field and
   10323 ** a list of named expression (pEList).  The named expression list may
   10324 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
   10325 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
   10326 ** pEList corresponds to the result set of a SELECT and is NULL for
   10327 ** other statements.
   10328 **
   10329 ** NameContexts can be nested.  When resolving names, the inner-most
   10330 ** context is searched first.  If no match is found, the next outer
   10331 ** context is checked.  If there is still no match, the next context
   10332 ** is checked.  This process continues until either a match is found
   10333 ** or all contexts are check.  When a match is found, the nRef member of
   10334 ** the context containing the match is incremented.
   10335 **
   10336 ** Each subquery gets a new NameContext.  The pNext field points to the
   10337 ** NameContext in the parent query.  Thus the process of scanning the
   10338 ** NameContext list corresponds to searching through successively outer
   10339 ** subqueries looking for a match.
   10340 */
   10341 struct NameContext {
   10342   Parse *pParse;       /* The parser */
   10343   SrcList *pSrcList;   /* One or more tables used to resolve names */
   10344   ExprList *pEList;    /* Optional list of named expressions */
   10345   int nRef;            /* Number of names resolved by this context */
   10346   int nErr;            /* Number of errors encountered while resolving names */
   10347   u8 allowAgg;         /* Aggregate functions allowed here */
   10348   u8 hasAgg;           /* True if aggregates are seen */
   10349   u8 isCheck;          /* True if resolving names in a CHECK constraint */
   10350   int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
   10351   AggInfo *pAggInfo;   /* Information about aggregates at this level */
   10352   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
   10353 };
   10354 
   10355 /*
   10356 ** An instance of the following structure contains all information
   10357 ** needed to generate code for a single SELECT statement.
   10358 **
   10359 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
   10360 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
   10361 ** limit and nOffset to the value of the offset (or 0 if there is not
   10362 ** offset).  But later on, nLimit and nOffset become the memory locations
   10363 ** in the VDBE that record the limit and offset counters.
   10364 **
   10365 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
   10366 ** These addresses must be stored so that we can go back and fill in
   10367 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
   10368 ** the number of columns in P2 can be computed at the same time
   10369 ** as the OP_OpenEphm instruction is coded because not
   10370 ** enough information about the compound query is known at that point.
   10371 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
   10372 ** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
   10373 ** sequences for the ORDER BY clause.
   10374 */
   10375 struct Select {
   10376   ExprList *pEList;      /* The fields of the result */
   10377   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
   10378   char affinity;         /* MakeRecord with this affinity for SRT_Set */
   10379   u16 selFlags;          /* Various SF_* values */
   10380   SrcList *pSrc;         /* The FROM clause */
   10381   Expr *pWhere;          /* The WHERE clause */
   10382   ExprList *pGroupBy;    /* The GROUP BY clause */
   10383   Expr *pHaving;         /* The HAVING clause */
   10384   ExprList *pOrderBy;    /* The ORDER BY clause */
   10385   Select *pPrior;        /* Prior select in a compound select statement */
   10386   Select *pNext;         /* Next select to the left in a compound */
   10387   Select *pRightmost;    /* Right-most select in a compound select statement */
   10388   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
   10389   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
   10390   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
   10391   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
   10392   double nSelectRow;     /* Estimated number of result rows */
   10393 };
   10394 
   10395 /*
   10396 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
   10397 ** "Select Flag".
   10398 */
   10399 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
   10400 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
   10401 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
   10402 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
   10403 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
   10404 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
   10405 
   10406 
   10407 /*
   10408 ** The results of a select can be distributed in several ways.  The
   10409 ** "SRT" prefix means "SELECT Result Type".
   10410 */
   10411 #define SRT_Union        1  /* Store result as keys in an index */
   10412 #define SRT_Except       2  /* Remove result from a UNION index */
   10413 #define SRT_Exists       3  /* Store 1 if the result is not empty */
   10414 #define SRT_Discard      4  /* Do not save the results anywhere */
   10415 
   10416 /* The ORDER BY clause is ignored for all of the above */
   10417 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
   10418 
   10419 #define SRT_Output       5  /* Output each row of result */
   10420 #define SRT_Mem          6  /* Store result in a memory cell */
   10421 #define SRT_Set          7  /* Store results as keys in an index */
   10422 #define SRT_Table        8  /* Store result as data with an automatic rowid */
   10423 #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
   10424 #define SRT_Coroutine   10  /* Generate a single row of result */
   10425 
   10426 /*
   10427 ** A structure used to customize the behavior of sqlite3Select(). See
   10428 ** comments above sqlite3Select() for details.
   10429 */
   10430 typedef struct SelectDest SelectDest;
   10431 struct SelectDest {
   10432   u8 eDest;         /* How to dispose of the results */
   10433   u8 affinity;      /* Affinity used when eDest==SRT_Set */
   10434   int iParm;        /* A parameter used by the eDest disposal method */
   10435   int iMem;         /* Base register where results are written */
   10436   int nMem;         /* Number of registers allocated */
   10437 };
   10438 
   10439 /*
   10440 ** During code generation of statements that do inserts into AUTOINCREMENT
   10441 ** tables, the following information is attached to the Table.u.autoInc.p
   10442 ** pointer of each autoincrement table to record some side information that
   10443 ** the code generator needs.  We have to keep per-table autoincrement
   10444 ** information in case inserts are down within triggers.  Triggers do not
   10445 ** normally coordinate their activities, but we do need to coordinate the
   10446 ** loading and saving of autoincrement information.
   10447 */
   10448 struct AutoincInfo {
   10449   AutoincInfo *pNext;   /* Next info block in a list of them all */
   10450   Table *pTab;          /* Table this info block refers to */
   10451   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
   10452   int regCtr;           /* Memory register holding the rowid counter */
   10453 };
   10454 
   10455 /*
   10456 ** Size of the column cache
   10457 */
   10458 #ifndef SQLITE_N_COLCACHE
   10459 # define SQLITE_N_COLCACHE 10
   10460 #endif
   10461 
   10462 /*
   10463 ** At least one instance of the following structure is created for each
   10464 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
   10465 ** statement. All such objects are stored in the linked list headed at
   10466 ** Parse.pTriggerPrg and deleted once statement compilation has been
   10467 ** completed.
   10468 **
   10469 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
   10470 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
   10471 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
   10472 ** The Parse.pTriggerPrg list never contains two entries with the same
   10473 ** values for both pTrigger and orconf.
   10474 **
   10475 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
   10476 ** accessed (or set to 0 for triggers fired as a result of INSERT
   10477 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
   10478 ** a mask of new.* columns used by the program.
   10479 */
   10480 struct TriggerPrg {
   10481   Trigger *pTrigger;      /* Trigger this program was coded from */
   10482   int orconf;             /* Default ON CONFLICT policy */
   10483   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
   10484   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
   10485   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
   10486 };
   10487 
   10488 /*
   10489 ** The yDbMask datatype for the bitmask of all attached databases.
   10490 */
   10491 #if SQLITE_MAX_ATTACHED>30
   10492   typedef sqlite3_uint64 yDbMask;
   10493 #else
   10494   typedef unsigned int yDbMask;
   10495 #endif
   10496 
   10497 /*
   10498 ** An SQL parser context.  A copy of this structure is passed through
   10499 ** the parser and down into all the parser action routine in order to
   10500 ** carry around information that is global to the entire parse.
   10501 **
   10502 ** The structure is divided into two parts.  When the parser and code
   10503 ** generate call themselves recursively, the first part of the structure
   10504 ** is constant but the second part is reset at the beginning and end of
   10505 ** each recursion.
   10506 **
   10507 ** The nTableLock and aTableLock variables are only used if the shared-cache
   10508 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
   10509 ** used to store the set of table-locks required by the statement being
   10510 ** compiled. Function sqlite3TableLock() is used to add entries to the
   10511 ** list.
   10512 */
   10513 struct Parse {
   10514   sqlite3 *db;         /* The main database structure */
   10515   int rc;              /* Return code from execution */
   10516   char *zErrMsg;       /* An error message */
   10517   Vdbe *pVdbe;         /* An engine for executing database bytecode */
   10518   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
   10519   u8 nameClash;        /* A permanent table name clashes with temp table name */
   10520   u8 checkSchema;      /* Causes schema cookie check after an error */
   10521   u8 nested;           /* Number of nested calls to the parser/code generator */
   10522   u8 parseError;       /* True after a parsing error.  Ticket #1794 */
   10523   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
   10524   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
   10525   int aTempReg[8];     /* Holding area for temporary registers */
   10526   int nRangeReg;       /* Size of the temporary register block */
   10527   int iRangeReg;       /* First register in temporary register block */
   10528   int nErr;            /* Number of errors seen */
   10529   int nTab;            /* Number of previously allocated VDBE cursors */
   10530   int nMem;            /* Number of memory cells used so far */
   10531   int nSet;            /* Number of sets used so far */
   10532   int ckBase;          /* Base register of data during check constraints */
   10533   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
   10534   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
   10535   u8 nColCache;        /* Number of entries in the column cache */
   10536   u8 iColCache;        /* Next entry of the cache to replace */
   10537   struct yColCache {
   10538     int iTable;           /* Table cursor number */
   10539     int iColumn;          /* Table column number */
   10540     u8 tempReg;           /* iReg is a temp register that needs to be freed */
   10541     int iLevel;           /* Nesting level */
   10542     int iReg;             /* Reg with value of this column. 0 means none. */
   10543     int lru;              /* Least recently used entry has the smallest value */
   10544   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
   10545   yDbMask writeMask;   /* Start a write transaction on these databases */
   10546   yDbMask cookieMask;  /* Bitmask of schema verified databases */
   10547   u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
   10548   u8 mayAbort;         /* True if statement may throw an ABORT exception */
   10549   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
   10550   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
   10551 #ifndef SQLITE_OMIT_SHARED_CACHE
   10552   int nTableLock;        /* Number of locks in aTableLock */
   10553   TableLock *aTableLock; /* Required table locks for shared-cache mode */
   10554 #endif
   10555   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
   10556   int regRoot;         /* Register holding root page number for new objects */
   10557   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
   10558   int nMaxArg;         /* Max args passed to user function by sub-program */
   10559 
   10560   /* Information used while coding trigger programs. */
   10561   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
   10562   Table *pTriggerTab;  /* Table triggers are being coded for */
   10563   u32 oldmask;         /* Mask of old.* columns referenced */
   10564   u32 newmask;         /* Mask of new.* columns referenced */
   10565   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
   10566   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
   10567   u8 disableTriggers;  /* True to disable triggers */
   10568   double nQueryLoop;   /* Estimated number of iterations of a query */
   10569 
   10570   /* Above is constant between recursions.  Below is reset before and after
   10571   ** each recursion */
   10572 
   10573   int nVar;            /* Number of '?' variables seen in the SQL so far */
   10574   int nVarExpr;        /* Number of used slots in apVarExpr[] */
   10575   int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
   10576   Expr **apVarExpr;    /* Pointers to :aaa and $aaaa wildcard expressions */
   10577   Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
   10578   int nAlias;          /* Number of aliased result set columns */
   10579   int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
   10580   int *aAlias;         /* Register used to hold aliased result */
   10581   u8 explain;          /* True if the EXPLAIN flag is found on the query */
   10582   Token sNameToken;    /* Token with unqualified schema object name */
   10583   Token sLastToken;    /* The last token parsed */
   10584   const char *zTail;   /* All SQL text past the last semicolon parsed */
   10585   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
   10586   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
   10587   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
   10588 #ifndef SQLITE_OMIT_VIRTUALTABLE
   10589   Token sArg;                /* Complete text of a module argument */
   10590   u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
   10591   int nVtabLock;             /* Number of virtual tables to lock */
   10592   Table **apVtabLock;        /* Pointer to virtual tables needing locking */
   10593 #endif
   10594   int nHeight;            /* Expression tree height of current sub-select */
   10595   Table *pZombieTab;      /* List of Table objects to delete after code gen */
   10596   TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
   10597 
   10598 #ifndef SQLITE_OMIT_EXPLAIN
   10599   int iSelectId;
   10600   int iNextSelectId;
   10601 #endif
   10602 };
   10603 
   10604 #ifdef SQLITE_OMIT_VIRTUALTABLE
   10605   #define IN_DECLARE_VTAB 0
   10606 #else
   10607   #define IN_DECLARE_VTAB (pParse->declareVtab)
   10608 #endif
   10609 
   10610 /*
   10611 ** An instance of the following structure can be declared on a stack and used
   10612 ** to save the Parse.zAuthContext value so that it can be restored later.
   10613 */
   10614 struct AuthContext {
   10615   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
   10616   Parse *pParse;              /* The Parse structure */
   10617 };
   10618 
   10619 /*
   10620 ** Bitfield flags for P5 value in OP_Insert and OP_Delete
   10621 */
   10622 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
   10623 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
   10624 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
   10625 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
   10626 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
   10627 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
   10628 
   10629 /*
   10630  * Each trigger present in the database schema is stored as an instance of
   10631  * struct Trigger.
   10632  *
   10633  * Pointers to instances of struct Trigger are stored in two ways.
   10634  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
   10635  *    database). This allows Trigger structures to be retrieved by name.
   10636  * 2. All triggers associated with a single table form a linked list, using the
   10637  *    pNext member of struct Trigger. A pointer to the first element of the
   10638  *    linked list is stored as the "pTrigger" member of the associated
   10639  *    struct Table.
   10640  *
   10641  * The "step_list" member points to the first element of a linked list
   10642  * containing the SQL statements specified as the trigger program.
   10643  */
   10644 struct Trigger {
   10645   char *zName;            /* The name of the trigger                        */
   10646   char *table;            /* The table or view to which the trigger applies */
   10647   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
   10648   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
   10649   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
   10650   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
   10651                              the <column-list> is stored here */
   10652   Schema *pSchema;        /* Schema containing the trigger */
   10653   Schema *pTabSchema;     /* Schema containing the table */
   10654   TriggerStep *step_list; /* Link list of trigger program steps             */
   10655   Trigger *pNext;         /* Next trigger associated with the table */
   10656 };
   10657 
   10658 /*
   10659 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
   10660 ** determine which.
   10661 **
   10662 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
   10663 ** In that cases, the constants below can be ORed together.
   10664 */
   10665 #define TRIGGER_BEFORE  1
   10666 #define TRIGGER_AFTER   2
   10667 
   10668 /*
   10669  * An instance of struct TriggerStep is used to store a single SQL statement
   10670  * that is a part of a trigger-program.
   10671  *
   10672  * Instances of struct TriggerStep are stored in a singly linked list (linked
   10673  * using the "pNext" member) referenced by the "step_list" member of the
   10674  * associated struct Trigger instance. The first element of the linked list is
   10675  * the first step of the trigger-program.
   10676  *
   10677  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
   10678  * "SELECT" statement. The meanings of the other members is determined by the
   10679  * value of "op" as follows:
   10680  *
   10681  * (op == TK_INSERT)
   10682  * orconf    -> stores the ON CONFLICT algorithm
   10683  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
   10684  *              this stores a pointer to the SELECT statement. Otherwise NULL.
   10685  * target    -> A token holding the quoted name of the table to insert into.
   10686  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
   10687  *              this stores values to be inserted. Otherwise NULL.
   10688  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
   10689  *              statement, then this stores the column-names to be
   10690  *              inserted into.
   10691  *
   10692  * (op == TK_DELETE)
   10693  * target    -> A token holding the quoted name of the table to delete from.
   10694  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
   10695  *              Otherwise NULL.
   10696  *
   10697  * (op == TK_UPDATE)
   10698  * target    -> A token holding the quoted name of the table to update rows of.
   10699  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
   10700  *              Otherwise NULL.
   10701  * pExprList -> A list of the columns to update and the expressions to update
   10702  *              them to. See sqlite3Update() documentation of "pChanges"
   10703  *              argument.
   10704  *
   10705  */
   10706 struct TriggerStep {
   10707   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
   10708   u8 orconf;           /* OE_Rollback etc. */
   10709   Trigger *pTrig;      /* The trigger that this step is a part of */
   10710   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
   10711   Token target;        /* Target table for DELETE, UPDATE, INSERT */
   10712   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
   10713   ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
   10714   IdList *pIdList;     /* Column names for INSERT */
   10715   TriggerStep *pNext;  /* Next in the link-list */
   10716   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
   10717 };
   10718 
   10719 /*
   10720 ** The following structure contains information used by the sqliteFix...
   10721 ** routines as they walk the parse tree to make database references
   10722 ** explicit.
   10723 */
   10724 typedef struct DbFixer DbFixer;
   10725 struct DbFixer {
   10726   Parse *pParse;      /* The parsing context.  Error messages written here */
   10727   const char *zDb;    /* Make sure all objects are contained in this database */
   10728   const char *zType;  /* Type of the container - used for error messages */
   10729   const Token *pName; /* Name of the container - used for error messages */
   10730 };
   10731 
   10732 /*
   10733 ** An objected used to accumulate the text of a string where we
   10734 ** do not necessarily know how big the string will be in the end.
   10735 */
   10736 struct StrAccum {
   10737   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
   10738   char *zBase;         /* A base allocation.  Not from malloc. */
   10739   char *zText;         /* The string collected so far */
   10740   int  nChar;          /* Length of the string so far */
   10741   int  nAlloc;         /* Amount of space allocated in zText */
   10742   int  mxAlloc;        /* Maximum allowed string length */
   10743   u8   mallocFailed;   /* Becomes true if any memory allocation fails */
   10744   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
   10745   u8   tooBig;         /* Becomes true if string size exceeds limits */
   10746 };
   10747 
   10748 /*
   10749 ** A pointer to this structure is used to communicate information
   10750 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
   10751 */
   10752 typedef struct {
   10753   sqlite3 *db;        /* The database being initialized */
   10754   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
   10755   char **pzErrMsg;    /* Error message stored here */
   10756   int rc;             /* Result code stored here */
   10757 } InitData;
   10758 
   10759 /*
   10760 ** Structure containing global configuration data for the SQLite library.
   10761 **
   10762 ** This structure also contains some state information.
   10763 */
   10764 struct Sqlite3Config {
   10765   int bMemstat;                     /* True to enable memory status */
   10766   int bCoreMutex;                   /* True to enable core mutexing */
   10767   int bFullMutex;                   /* True to enable full mutexing */
   10768   int mxStrlen;                     /* Maximum string length */
   10769   int szLookaside;                  /* Default lookaside buffer size */
   10770   int nLookaside;                   /* Default lookaside buffer count */
   10771   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
   10772   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
   10773   sqlite3_pcache_methods pcache;    /* Low-level page-cache interface */
   10774   void *pHeap;                      /* Heap storage space */
   10775   int nHeap;                        /* Size of pHeap[] */
   10776   int mnReq, mxReq;                 /* Min and max heap requests sizes */
   10777   void *pScratch;                   /* Scratch memory */
   10778   int szScratch;                    /* Size of each scratch buffer */
   10779   int nScratch;                     /* Number of scratch buffers */
   10780   void *pPage;                      /* Page cache memory */
   10781   int szPage;                       /* Size of each page in pPage[] */
   10782   int nPage;                        /* Number of pages in pPage[] */
   10783   int mxParserStack;                /* maximum depth of the parser stack */
   10784   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
   10785   /* The above might be initialized to non-zero.  The following need to always
   10786   ** initially be zero, however. */
   10787   int isInit;                       /* True after initialization has finished */
   10788   int inProgress;                   /* True while initialization in progress */
   10789   int isMutexInit;                  /* True after mutexes are initialized */
   10790   int isMallocInit;                 /* True after malloc is initialized */
   10791   int isPCacheInit;                 /* True after malloc is initialized */
   10792   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
   10793   int nRefInitMutex;                /* Number of users of pInitMutex */
   10794   void (*xLog)(void*,int,const char*); /* Function for logging */
   10795   void *pLogArg;                       /* First argument to xLog() */
   10796 };
   10797 
   10798 /*
   10799 ** Context pointer passed down through the tree-walk.
   10800 */
   10801 struct Walker {
   10802   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
   10803   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
   10804   Parse *pParse;                            /* Parser context.  */
   10805   union {                                   /* Extra data for callback */
   10806     NameContext *pNC;                          /* Naming context */
   10807     int i;                                     /* Integer value */
   10808   } u;
   10809 };
   10810 
   10811 /* Forward declarations */
   10812 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
   10813 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
   10814 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
   10815 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
   10816 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
   10817 
   10818 /*
   10819 ** Return code from the parse-tree walking primitives and their
   10820 ** callbacks.
   10821 */
   10822 #define WRC_Continue    0   /* Continue down into children */
   10823 #define WRC_Prune       1   /* Omit children but continue walking siblings */
   10824 #define WRC_Abort       2   /* Abandon the tree walk */
   10825 
   10826 /*
   10827 ** Assuming zIn points to the first byte of a UTF-8 character,
   10828 ** advance zIn to point to the first byte of the next UTF-8 character.
   10829 */
   10830 #define SQLITE_SKIP_UTF8(zIn) {                        \
   10831   if( (*(zIn++))>=0xc0 ){                              \
   10832     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
   10833   }                                                    \
   10834 }
   10835 
   10836 /*
   10837 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
   10838 ** the same name but without the _BKPT suffix.  These macros invoke
   10839 ** routines that report the line-number on which the error originated
   10840 ** using sqlite3_log().  The routines also provide a convenient place
   10841 ** to set a debugger breakpoint.
   10842 */
   10843 SQLITE_PRIVATE int sqlite3CorruptError(int);
   10844 SQLITE_PRIVATE int sqlite3MisuseError(int);
   10845 SQLITE_PRIVATE int sqlite3CantopenError(int);
   10846 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
   10847 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
   10848 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
   10849 
   10850 
   10851 /*
   10852 ** FTS4 is really an extension for FTS3.  It is enabled using the
   10853 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
   10854 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
   10855 */
   10856 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
   10857 # define SQLITE_ENABLE_FTS3
   10858 #endif
   10859 
   10860 /*
   10861 ** The ctype.h header is needed for non-ASCII systems.  It is also
   10862 ** needed by FTS3 when FTS3 is included in the amalgamation.
   10863 */
   10864 #if !defined(SQLITE_ASCII) || \
   10865     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
   10866 # include <ctype.h>
   10867 #endif
   10868 
   10869 /*
   10870 ** The CoreServices.h and CoreFoundation.h headers are needed for excluding a
   10871 ** -journal file from Time Machine backups when its associated database has
   10872 ** previously been excluded by the client code.
   10873 */
   10874 #if defined(__APPLE__)
   10875 #include <CoreServices/CoreServices.h>
   10876 #include <CoreFoundation/CoreFoundation.h>
   10877 #endif
   10878 
   10879 /*
   10880 ** The following macros mimic the standard library functions toupper(),
   10881 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
   10882 ** sqlite versions only work for ASCII characters, regardless of locale.
   10883 */
   10884 #ifdef SQLITE_ASCII
   10885 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
   10886 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
   10887 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
   10888 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
   10889 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
   10890 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
   10891 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
   10892 #else
   10893 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
   10894 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
   10895 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
   10896 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
   10897 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
   10898 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
   10899 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
   10900 #endif
   10901 
   10902 /*
   10903 ** Internal function prototypes
   10904 */
   10905 SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
   10906 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
   10907 #define sqlite3StrNICmp sqlite3_strnicmp
   10908 
   10909 SQLITE_PRIVATE int sqlite3MallocInit(void);
   10910 SQLITE_PRIVATE void sqlite3MallocEnd(void);
   10911 SQLITE_PRIVATE void *sqlite3Malloc(int);
   10912 SQLITE_PRIVATE void *sqlite3MallocZero(int);
   10913 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
   10914 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
   10915 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
   10916 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
   10917 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
   10918 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
   10919 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
   10920 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
   10921 SQLITE_PRIVATE int sqlite3MallocSize(void*);
   10922 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
   10923 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
   10924 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
   10925 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
   10926 SQLITE_PRIVATE void sqlite3PageFree(void*);
   10927 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
   10928 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
   10929 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
   10930 
   10931 /*
   10932 ** On systems with ample stack space and that support alloca(), make
   10933 ** use of alloca() to obtain space for large automatic objects.  By default,
   10934 ** obtain space from malloc().
   10935 **
   10936 ** The alloca() routine never returns NULL.  This will cause code paths
   10937 ** that deal with sqlite3StackAlloc() failures to be unreachable.
   10938 */
   10939 #ifdef SQLITE_USE_ALLOCA
   10940 # define sqlite3StackAllocRaw(D,N)   alloca(N)
   10941 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
   10942 # define sqlite3StackFree(D,P)
   10943 #else
   10944 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
   10945 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
   10946 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
   10947 #endif
   10948 
   10949 #ifdef SQLITE_ENABLE_MEMSYS3
   10950 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
   10951 #endif
   10952 #ifdef SQLITE_ENABLE_MEMSYS5
   10953 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
   10954 #endif
   10955 
   10956 
   10957 #ifndef SQLITE_MUTEX_OMIT
   10958 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
   10959 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
   10960 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
   10961 SQLITE_PRIVATE   int sqlite3MutexInit(void);
   10962 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
   10963 #endif
   10964 
   10965 SQLITE_PRIVATE int sqlite3StatusValue(int);
   10966 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
   10967 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
   10968 
   10969 #ifndef SQLITE_OMIT_FLOATING_POINT
   10970 SQLITE_PRIVATE   int sqlite3IsNaN(double);
   10971 #else
   10972 # define sqlite3IsNaN(X)  0
   10973 #endif
   10974 
   10975 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
   10976 #ifndef SQLITE_OMIT_TRACE
   10977 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
   10978 #endif
   10979 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
   10980 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
   10981 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
   10982 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
   10983 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
   10984 #endif
   10985 #if defined(SQLITE_TEST)
   10986 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
   10987 #endif
   10988 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
   10989 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
   10990 SQLITE_PRIVATE int sqlite3Dequote(char*);
   10991 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
   10992 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
   10993 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
   10994 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
   10995 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
   10996 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
   10997 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
   10998 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
   10999 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
   11000 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
   11001 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
   11002 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
   11003 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
   11004 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
   11005 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
   11006 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
   11007 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
   11008 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
   11009 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
   11010 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
   11011 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
   11012 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
   11013 SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
   11014 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
   11015 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
   11016 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
   11017 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
   11018 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
   11019 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
   11020 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
   11021 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
   11022 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
   11023 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
   11024 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
   11025 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
   11026 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
   11027 
   11028 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
   11029 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
   11030 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
   11031 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
   11032 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
   11033 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
   11034 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
   11035 
   11036 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
   11037 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
   11038 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
   11039 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
   11040 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
   11041 
   11042 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
   11043 
   11044 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
   11045 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
   11046 #else
   11047 # define sqlite3ViewGetColumnNames(A,B) 0
   11048 #endif
   11049 
   11050 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
   11051 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
   11052 #ifndef SQLITE_OMIT_AUTOINCREMENT
   11053 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
   11054 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
   11055 #else
   11056 # define sqlite3AutoincrementBegin(X)
   11057 # define sqlite3AutoincrementEnd(X)
   11058 #endif
   11059 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
   11060 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
   11061 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
   11062 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
   11063 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
   11064 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
   11065 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
   11066                                       Token*, Select*, Expr*, IdList*);
   11067 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
   11068 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
   11069 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
   11070 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
   11071 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
   11072 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
   11073 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
   11074                         Token*, int, int);
   11075 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
   11076 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
   11077 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
   11078                          Expr*,ExprList*,int,Expr*,Expr*);
   11079 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
   11080 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
   11081 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
   11082 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
   11083 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
   11084 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
   11085 #endif
   11086 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
   11087 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
   11088 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
   11089 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
   11090 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
   11091 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
   11092 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
   11093 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
   11094 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
   11095 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
   11096 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
   11097 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
   11098 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
   11099 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
   11100 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
   11101 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
   11102 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
   11103 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
   11104 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
   11105 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
   11106 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
   11107 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
   11108 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
   11109 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
   11110 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
   11111 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
   11112 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
   11113 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
   11114 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
   11115 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
   11116 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
   11117 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
   11118 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
   11119 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
   11120 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
   11121 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
   11122 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
   11123 SQLITE_PRIVATE void sqlite3PrngResetState(void);
   11124 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
   11125 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
   11126 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
   11127 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
   11128 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
   11129 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
   11130 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
   11131 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
   11132 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
   11133 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
   11134 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
   11135 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
   11136 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
   11137 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
   11138 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
   11139 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
   11140 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
   11141 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
   11142 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
   11143 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
   11144                                      int*,int,int,int,int,int*);
   11145 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
   11146 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
   11147 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
   11148 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
   11149 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
   11150 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
   11151 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
   11152 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
   11153 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
   11154 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
   11155 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
   11156 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
   11157 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
   11158 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
   11159 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
   11160 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
   11161 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
   11162 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
   11163 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
   11164 
   11165 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   11166 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
   11167 #endif
   11168 
   11169 #ifndef SQLITE_OMIT_TRIGGER
   11170 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
   11171                            Expr*,int, int);
   11172 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
   11173 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
   11174 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
   11175 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
   11176 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
   11177 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
   11178                             int, int, int);
   11179 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
   11180   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
   11181 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
   11182 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
   11183 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
   11184                                         ExprList*,Select*,u8);
   11185 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
   11186 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
   11187 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
   11188 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
   11189 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
   11190 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
   11191 #else
   11192 # define sqlite3TriggersExist(B,C,D,E,F) 0
   11193 # define sqlite3DeleteTrigger(A,B)
   11194 # define sqlite3DropTriggerPtr(A,B)
   11195 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
   11196 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
   11197 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
   11198 # define sqlite3TriggerList(X, Y) 0
   11199 # define sqlite3ParseToplevel(p) p
   11200 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
   11201 #endif
   11202 
   11203 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
   11204 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
   11205 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
   11206 #ifndef SQLITE_OMIT_AUTHORIZATION
   11207 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
   11208 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
   11209 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
   11210 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
   11211 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
   11212 #else
   11213 # define sqlite3AuthRead(a,b,c,d)
   11214 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
   11215 # define sqlite3AuthContextPush(a,b,c)
   11216 # define sqlite3AuthContextPop(a)  ((void)(a))
   11217 #endif
   11218 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
   11219 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
   11220 SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
   11221 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
   11222 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
   11223 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
   11224 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
   11225 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
   11226 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
   11227 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
   11228 SQLITE_PRIVATE int sqlite3Atoi(const char*);
   11229 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
   11230 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
   11231 SQLITE_PRIVATE int sqlite3Utf8Read(const u8*, const u8**);
   11232 
   11233 /*
   11234 ** Routines to read and write variable-length integers.  These used to
   11235 ** be defined locally, but now we use the varint routines in the util.c
   11236 ** file.  Code should use the MACRO forms below, as the Varint32 versions
   11237 ** are coded to assume the single byte case is already handled (which
   11238 ** the MACRO form does).
   11239 */
   11240 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
   11241 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
   11242 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
   11243 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
   11244 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
   11245 
   11246 /*
   11247 ** The header of a record consists of a sequence variable-length integers.
   11248 ** These integers are almost always small and are encoded as a single byte.
   11249 ** The following macros take advantage this fact to provide a fast encode
   11250 ** and decode of the integers in a record header.  It is faster for the common
   11251 ** case where the integer is a single byte.  It is a little slower when the
   11252 ** integer is two or more bytes.  But overall it is faster.
   11253 **
   11254 ** The following expressions are equivalent:
   11255 **
   11256 **     x = sqlite3GetVarint32( A, &B );
   11257 **     x = sqlite3PutVarint32( A, B );
   11258 **
   11259 **     x = getVarint32( A, B );
   11260 **     x = putVarint32( A, B );
   11261 **
   11262 */
   11263 #define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
   11264 #define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
   11265 #define getVarint    sqlite3GetVarint
   11266 #define putVarint    sqlite3PutVarint
   11267 
   11268 
   11269 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
   11270 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
   11271 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
   11272 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
   11273 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
   11274 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
   11275 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
   11276 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
   11277 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
   11278 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
   11279 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
   11280 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
   11281 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
   11282 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
   11283 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
   11284 SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
   11285 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
   11286 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
   11287 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
   11288 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
   11289 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
   11290 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
   11291 SQLITE_PRIVATE int sqlite3AbsInt32(int);
   11292 
   11293 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
   11294 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
   11295 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
   11296                         void(*)(void*));
   11297 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
   11298 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
   11299 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
   11300 #ifdef SQLITE_ENABLE_STAT2
   11301 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
   11302 #endif
   11303 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
   11304 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
   11305 #ifndef SQLITE_AMALGAMATION
   11306 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
   11307 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
   11308 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
   11309 SQLITE_PRIVATE const Token sqlite3IntTokens[];
   11310 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
   11311 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
   11312 #ifndef SQLITE_OMIT_WSD
   11313 SQLITE_PRIVATE int sqlite3PendingByte;
   11314 #endif
   11315 #endif
   11316 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
   11317 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
   11318 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
   11319 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
   11320 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
   11321 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
   11322 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
   11323 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
   11324 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
   11325 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
   11326 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
   11327 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
   11328 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
   11329 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
   11330 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
   11331 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
   11332 SQLITE_PRIVATE char sqlite3AffinityType(const char*);
   11333 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
   11334 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
   11335 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
   11336 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
   11337 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
   11338 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
   11339 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
   11340 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
   11341 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
   11342 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
   11343 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
   11344 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
   11345 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
   11346 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
   11347 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
   11348   void (*)(sqlite3_context*,int,sqlite3_value **),
   11349   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
   11350   FuncDestructor *pDestructor
   11351 );
   11352 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
   11353 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
   11354 
   11355 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
   11356 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
   11357 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
   11358 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
   11359 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
   11360 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
   11361 
   11362 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
   11363 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
   11364 
   11365 /*
   11366 ** The interface to the LEMON-generated parser
   11367 */
   11368 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
   11369 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
   11370 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
   11371 #ifdef YYTRACKMAXSTACKDEPTH
   11372 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
   11373 #endif
   11374 
   11375 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
   11376 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   11377 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
   11378 #else
   11379 # define sqlite3CloseExtensions(X)
   11380 #endif
   11381 
   11382 #ifndef SQLITE_OMIT_SHARED_CACHE
   11383 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
   11384 #else
   11385   #define sqlite3TableLock(v,w,x,y,z)
   11386 #endif
   11387 
   11388 #ifdef SQLITE_TEST
   11389 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
   11390 #endif
   11391 
   11392 #ifdef SQLITE_OMIT_VIRTUALTABLE
   11393 #  define sqlite3VtabClear(Y)
   11394 #  define sqlite3VtabSync(X,Y) SQLITE_OK
   11395 #  define sqlite3VtabRollback(X)
   11396 #  define sqlite3VtabCommit(X)
   11397 #  define sqlite3VtabInSync(db) 0
   11398 #  define sqlite3VtabLock(X)
   11399 #  define sqlite3VtabUnlock(X)
   11400 #  define sqlite3VtabUnlockList(X)
   11401 #else
   11402 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
   11403 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
   11404 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
   11405 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
   11406 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
   11407 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
   11408 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
   11409 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
   11410 #endif
   11411 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
   11412 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
   11413 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
   11414 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
   11415 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
   11416 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
   11417 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
   11418 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
   11419 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
   11420 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
   11421 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
   11422 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
   11423 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
   11424 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
   11425 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
   11426 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
   11427 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
   11428 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
   11429 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
   11430 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
   11431 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
   11432 
   11433 /* Declarations for functions in fkey.c. All of these are replaced by
   11434 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
   11435 ** key functionality is available. If OMIT_TRIGGER is defined but
   11436 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
   11437 ** this case foreign keys are parsed, but no other functionality is
   11438 ** provided (enforcement of FK constraints requires the triggers sub-system).
   11439 */
   11440 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   11441 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
   11442 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
   11443 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
   11444 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
   11445 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
   11446 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
   11447 #else
   11448   #define sqlite3FkActions(a,b,c,d)
   11449   #define sqlite3FkCheck(a,b,c,d)
   11450   #define sqlite3FkDropTable(a,b,c)
   11451   #define sqlite3FkOldmask(a,b)      0
   11452   #define sqlite3FkRequired(a,b,c,d) 0
   11453 #endif
   11454 #ifndef SQLITE_OMIT_FOREIGN_KEY
   11455 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
   11456 #else
   11457   #define sqlite3FkDelete(a,b)
   11458 #endif
   11459 
   11460 
   11461 /*
   11462 ** Available fault injectors.  Should be numbered beginning with 0.
   11463 */
   11464 #define SQLITE_FAULTINJECTOR_MALLOC     0
   11465 #define SQLITE_FAULTINJECTOR_COUNT      1
   11466 
   11467 /*
   11468 ** The interface to the code in fault.c used for identifying "benign"
   11469 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
   11470 ** is not defined.
   11471 */
   11472 #ifndef SQLITE_OMIT_BUILTIN_TEST
   11473 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
   11474 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
   11475 #else
   11476   #define sqlite3BeginBenignMalloc()
   11477   #define sqlite3EndBenignMalloc()
   11478 #endif
   11479 
   11480 #define IN_INDEX_ROWID           1
   11481 #define IN_INDEX_EPH             2
   11482 #define IN_INDEX_INDEX           3
   11483 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
   11484 
   11485 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   11486 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
   11487 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
   11488 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
   11489 #else
   11490   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
   11491 #endif
   11492 
   11493 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
   11494 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
   11495 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
   11496 
   11497 #if SQLITE_MAX_EXPR_DEPTH>0
   11498 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
   11499 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
   11500 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
   11501 #else
   11502   #define sqlite3ExprSetHeight(x,y)
   11503   #define sqlite3SelectExprHeight(x) 0
   11504   #define sqlite3ExprCheckHeight(x,y)
   11505 #endif
   11506 
   11507 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
   11508 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
   11509 
   11510 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   11511 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
   11512 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
   11513 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
   11514 #else
   11515   #define sqlite3ConnectionBlocked(x,y)
   11516   #define sqlite3ConnectionUnlocked(x)
   11517   #define sqlite3ConnectionClosed(x)
   11518 #endif
   11519 
   11520 #ifdef SQLITE_DEBUG
   11521 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
   11522 #endif
   11523 
   11524 /*
   11525 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
   11526 ** sqlite3IoTrace is a pointer to a printf-like routine used to
   11527 ** print I/O tracing messages.
   11528 */
   11529 #ifdef SQLITE_ENABLE_IOTRACE
   11530 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
   11531 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
   11532 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
   11533 #else
   11534 # define IOTRACE(A)
   11535 # define sqlite3VdbeIOTraceSql(X)
   11536 #endif
   11537 
   11538 /*
   11539 ** These routines are available for the mem2.c debugging memory allocator
   11540 ** only.  They are used to verify that different "types" of memory
   11541 ** allocations are properly tracked by the system.
   11542 **
   11543 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
   11544 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
   11545 ** a single bit set.
   11546 **
   11547 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
   11548 ** argument match the type set by the previous sqlite3MemdebugSetType().
   11549 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
   11550 **
   11551 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
   11552 ** argument match the type set by the previous sqlite3MemdebugSetType().
   11553 **
   11554 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
   11555 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
   11556 ** it might have been allocated by lookaside, except the allocation was
   11557 ** too large or lookaside was already full.  It is important to verify
   11558 ** that allocations that might have been satisfied by lookaside are not
   11559 ** passed back to non-lookaside free() routines.  Asserts such as the
   11560 ** example above are placed on the non-lookaside free() routines to verify
   11561 ** this constraint.
   11562 **
   11563 ** All of this is no-op for a production build.  It only comes into
   11564 ** play when the SQLITE_MEMDEBUG compile-time option is used.
   11565 */
   11566 #ifdef SQLITE_MEMDEBUG
   11567 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
   11568 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
   11569 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
   11570 #else
   11571 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
   11572 # define sqlite3MemdebugHasType(X,Y)  1
   11573 # define sqlite3MemdebugNoType(X,Y)   1
   11574 #endif
   11575 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
   11576 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
   11577 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
   11578 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
   11579 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
   11580 
   11581 #endif /* _SQLITEINT_H_ */
   11582 
   11583 /************** End of sqliteInt.h *******************************************/
   11584 /************** Begin file global.c ******************************************/
   11585 /*
   11586 ** 2008 June 13
   11587 **
   11588 ** The author disclaims copyright to this source code.  In place of
   11589 ** a legal notice, here is a blessing:
   11590 **
   11591 **    May you do good and not evil.
   11592 **    May you find forgiveness for yourself and forgive others.
   11593 **    May you share freely, never taking more than you give.
   11594 **
   11595 *************************************************************************
   11596 **
   11597 ** This file contains definitions of global variables and contants.
   11598 */
   11599 
   11600 /* An array to map all upper-case characters into their corresponding
   11601 ** lower-case character.
   11602 **
   11603 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
   11604 ** handle case conversions for the UTF character set since the tables
   11605 ** involved are nearly as big or bigger than SQLite itself.
   11606 */
   11607 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
   11608 #ifdef SQLITE_ASCII
   11609       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
   11610      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
   11611      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
   11612      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
   11613     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
   11614     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
   11615     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
   11616     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
   11617     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
   11618     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
   11619     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
   11620     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
   11621     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
   11622     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
   11623     252,253,254,255
   11624 #endif
   11625 #ifdef SQLITE_EBCDIC
   11626       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
   11627      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
   11628      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
   11629      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
   11630      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
   11631      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
   11632      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
   11633     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
   11634     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
   11635     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
   11636     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
   11637     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
   11638     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
   11639     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
   11640     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
   11641     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
   11642 #endif
   11643 };
   11644 
   11645 /*
   11646 ** The following 256 byte lookup table is used to support SQLites built-in
   11647 ** equivalents to the following standard library functions:
   11648 **
   11649 **   isspace()                        0x01
   11650 **   isalpha()                        0x02
   11651 **   isdigit()                        0x04
   11652 **   isalnum()                        0x06
   11653 **   isxdigit()                       0x08
   11654 **   toupper()                        0x20
   11655 **   SQLite identifier character      0x40
   11656 **
   11657 ** Bit 0x20 is set if the mapped character requires translation to upper
   11658 ** case. i.e. if the character is a lower-case ASCII character.
   11659 ** If x is a lower-case ASCII character, then its upper-case equivalent
   11660 ** is (x - 0x20). Therefore toupper() can be implemented as:
   11661 **
   11662 **   (x & ~(map[x]&0x20))
   11663 **
   11664 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
   11665 ** array. tolower() is used more often than toupper() by SQLite.
   11666 **
   11667 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
   11668 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
   11669 ** non-ASCII UTF character. Hence the test for whether or not a character is
   11670 ** part of an identifier is 0x46.
   11671 **
   11672 ** SQLite's versions are identical to the standard versions assuming a
   11673 ** locale of "C". They are implemented as macros in sqliteInt.h.
   11674 */
   11675 #ifdef SQLITE_ASCII
   11676 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
   11677   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
   11678   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
   11679   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
   11680   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
   11681   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
   11682   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
   11683   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
   11684   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
   11685 
   11686   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
   11687   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
   11688   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
   11689   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
   11690   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
   11691   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
   11692   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
   11693   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
   11694 
   11695   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
   11696   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
   11697   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
   11698   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
   11699   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
   11700   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
   11701   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
   11702   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
   11703 
   11704   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
   11705   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
   11706   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
   11707   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
   11708   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
   11709   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
   11710   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
   11711   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
   11712 };
   11713 #endif
   11714 
   11715 
   11716 
   11717 /*
   11718 ** The following singleton contains the global configuration for
   11719 ** the SQLite library.
   11720 */
   11721 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   11722    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   11723    1,                         /* bCoreMutex */
   11724    SQLITE_THREADSAFE==1,      /* bFullMutex */
   11725    0x7ffffffe,                /* mxStrlen */
   11726    100,                       /* szLookaside */
   11727    500,                       /* nLookaside */
   11728    {0,0,0,0,0,0,0,0},         /* m */
   11729    {0,0,0,0,0,0,0,0,0},       /* mutex */
   11730    {0,0,0,0,0,0,0,0,0,0,0},   /* pcache */
   11731    (void*)0,                  /* pHeap */
   11732    0,                         /* nHeap */
   11733    0, 0,                      /* mnHeap, mxHeap */
   11734    (void*)0,                  /* pScratch */
   11735    0,                         /* szScratch */
   11736    0,                         /* nScratch */
   11737    (void*)0,                  /* pPage */
   11738    0,                         /* szPage */
   11739    0,                         /* nPage */
   11740    0,                         /* mxParserStack */
   11741    0,                         /* sharedCacheEnabled */
   11742    /* All the rest should always be initialized to zero */
   11743    0,                         /* isInit */
   11744    0,                         /* inProgress */
   11745    0,                         /* isMutexInit */
   11746    0,                         /* isMallocInit */
   11747    0,                         /* isPCacheInit */
   11748    0,                         /* pInitMutex */
   11749    0,                         /* nRefInitMutex */
   11750    0,                         /* xLog */
   11751    0,                         /* pLogArg */
   11752 };
   11753 
   11754 
   11755 /*
   11756 ** Hash table for global functions - functions common to all
   11757 ** database connections.  After initialization, this table is
   11758 ** read-only.
   11759 */
   11760 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
   11761 
   11762 /*
   11763 ** Constant tokens for values 0 and 1.
   11764 */
   11765 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
   11766    { "0", 1 },
   11767    { "1", 1 }
   11768 };
   11769 
   11770 
   11771 /*
   11772 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
   11773 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
   11774 ** the database page that contains the pending byte.  It never attempts
   11775 ** to read or write that page.  The pending byte page is set assign
   11776 ** for use by the VFS layers as space for managing file locks.
   11777 **
   11778 ** During testing, it is often desirable to move the pending byte to
   11779 ** a different position in the file.  This allows code that has to
   11780 ** deal with the pending byte to run on files that are much smaller
   11781 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
   11782 ** move the pending byte.
   11783 **
   11784 ** IMPORTANT:  Changing the pending byte to any value other than
   11785 ** 0x40000000 results in an incompatible database file format!
   11786 ** Changing the pending byte during operating results in undefined
   11787 ** and dileterious behavior.
   11788 */
   11789 #ifndef SQLITE_OMIT_WSD
   11790 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
   11791 #endif
   11792 
   11793 /*
   11794 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
   11795 ** created by mkopcodeh.awk during compilation.  Data is obtained
   11796 ** from the comments following the "case OP_xxxx:" statements in
   11797 ** the vdbe.c file.
   11798 */
   11799 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
   11800 
   11801 /************** End of global.c **********************************************/
   11802 /************** Begin file ctime.c *******************************************/
   11803 /*
   11804 ** 2010 February 23
   11805 **
   11806 ** The author disclaims copyright to this source code.  In place of
   11807 ** a legal notice, here is a blessing:
   11808 **
   11809 **    May you do good and not evil.
   11810 **    May you find forgiveness for yourself and forgive others.
   11811 **    May you share freely, never taking more than you give.
   11812 **
   11813 *************************************************************************
   11814 **
   11815 ** This file implements routines used to report what compile-time options
   11816 ** SQLite was built with.
   11817 */
   11818 
   11819 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   11820 
   11821 
   11822 /*
   11823 ** An array of names of all compile-time options.  This array should
   11824 ** be sorted A-Z.
   11825 **
   11826 ** This array looks large, but in a typical installation actually uses
   11827 ** only a handful of compile-time options, so most times this array is usually
   11828 ** rather short and uses little memory space.
   11829 */
   11830 static const char * const azCompileOpt[] = {
   11831 
   11832 /* These macros are provided to "stringify" the value of the define
   11833 ** for those options in which the value is meaningful. */
   11834 #define CTIMEOPT_VAL_(opt) #opt
   11835 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
   11836 
   11837 #ifdef SQLITE_32BIT_ROWID
   11838   "32BIT_ROWID",
   11839 #endif
   11840 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
   11841   "4_BYTE_ALIGNED_MALLOC",
   11842 #endif
   11843 #ifdef SQLITE_CASE_SENSITIVE_LIKE
   11844   "CASE_SENSITIVE_LIKE",
   11845 #endif
   11846 #ifdef SQLITE_CHECK_PAGES
   11847   "CHECK_PAGES",
   11848 #endif
   11849 #ifdef SQLITE_COVERAGE_TEST
   11850   "COVERAGE_TEST",
   11851 #endif
   11852 #ifdef SQLITE_DEBUG
   11853   "DEBUG",
   11854 #endif
   11855 #ifdef SQLITE_DEFAULT_LOCKING_MODE
   11856   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
   11857 #endif
   11858 #ifdef SQLITE_DISABLE_DIRSYNC
   11859   "DISABLE_DIRSYNC",
   11860 #endif
   11861 #ifdef SQLITE_DISABLE_LFS
   11862   "DISABLE_LFS",
   11863 #endif
   11864 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   11865   "ENABLE_ATOMIC_WRITE",
   11866 #endif
   11867 #ifdef SQLITE_ENABLE_CEROD
   11868   "ENABLE_CEROD",
   11869 #endif
   11870 #ifdef SQLITE_ENABLE_COLUMN_METADATA
   11871   "ENABLE_COLUMN_METADATA",
   11872 #endif
   11873 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   11874   "ENABLE_EXPENSIVE_ASSERT",
   11875 #endif
   11876 #ifdef SQLITE_ENABLE_FTS1
   11877   "ENABLE_FTS1",
   11878 #endif
   11879 #ifdef SQLITE_ENABLE_FTS2
   11880   "ENABLE_FTS2",
   11881 #endif
   11882 #ifdef SQLITE_ENABLE_FTS3
   11883   "ENABLE_FTS3",
   11884 #endif
   11885 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
   11886   "ENABLE_FTS3_PARENTHESIS",
   11887 #endif
   11888 #ifdef SQLITE_ENABLE_FTS4
   11889   "ENABLE_FTS4",
   11890 #endif
   11891 #ifdef SQLITE_ENABLE_ICU
   11892   "ENABLE_ICU",
   11893 #endif
   11894 #ifdef SQLITE_ENABLE_IOTRACE
   11895   "ENABLE_IOTRACE",
   11896 #endif
   11897 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
   11898   "ENABLE_LOAD_EXTENSION",
   11899 #endif
   11900 #ifdef SQLITE_ENABLE_LOCKING_STYLE
   11901   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
   11902 #endif
   11903 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   11904   "ENABLE_MEMORY_MANAGEMENT",
   11905 #endif
   11906 #ifdef SQLITE_ENABLE_MEMSYS3
   11907   "ENABLE_MEMSYS3",
   11908 #endif
   11909 #ifdef SQLITE_ENABLE_MEMSYS5
   11910   "ENABLE_MEMSYS5",
   11911 #endif
   11912 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
   11913   "ENABLE_OVERSIZE_CELL_CHECK",
   11914 #endif
   11915 #ifdef SQLITE_ENABLE_RTREE
   11916   "ENABLE_RTREE",
   11917 #endif
   11918 #ifdef SQLITE_ENABLE_STAT2
   11919   "ENABLE_STAT2",
   11920 #endif
   11921 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   11922   "ENABLE_UNLOCK_NOTIFY",
   11923 #endif
   11924 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   11925   "ENABLE_UPDATE_DELETE_LIMIT",
   11926 #endif
   11927 #ifdef SQLITE_HAS_CODEC
   11928   "HAS_CODEC",
   11929 #endif
   11930 #ifdef SQLITE_HAVE_ISNAN
   11931   "HAVE_ISNAN",
   11932 #endif
   11933 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   11934   "HOMEGROWN_RECURSIVE_MUTEX",
   11935 #endif
   11936 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
   11937   "IGNORE_AFP_LOCK_ERRORS",
   11938 #endif
   11939 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   11940   "IGNORE_FLOCK_LOCK_ERRORS",
   11941 #endif
   11942 #ifdef SQLITE_INT64_TYPE
   11943   "INT64_TYPE",
   11944 #endif
   11945 #ifdef SQLITE_LOCK_TRACE
   11946   "LOCK_TRACE",
   11947 #endif
   11948 #ifdef SQLITE_MEMDEBUG
   11949   "MEMDEBUG",
   11950 #endif
   11951 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   11952   "MIXED_ENDIAN_64BIT_FLOAT",
   11953 #endif
   11954 #ifdef SQLITE_NO_SYNC
   11955   "NO_SYNC",
   11956 #endif
   11957 #ifdef SQLITE_OMIT_ALTERTABLE
   11958   "OMIT_ALTERTABLE",
   11959 #endif
   11960 #ifdef SQLITE_OMIT_ANALYZE
   11961   "OMIT_ANALYZE",
   11962 #endif
   11963 #ifdef SQLITE_OMIT_ATTACH
   11964   "OMIT_ATTACH",
   11965 #endif
   11966 #ifdef SQLITE_OMIT_AUTHORIZATION
   11967   "OMIT_AUTHORIZATION",
   11968 #endif
   11969 #ifdef SQLITE_OMIT_AUTOINCREMENT
   11970   "OMIT_AUTOINCREMENT",
   11971 #endif
   11972 #ifdef SQLITE_OMIT_AUTOINIT
   11973   "OMIT_AUTOINIT",
   11974 #endif
   11975 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
   11976   "OMIT_AUTOMATIC_INDEX",
   11977 #endif
   11978 #ifdef SQLITE_OMIT_AUTORESET
   11979   "OMIT_AUTORESET",
   11980 #endif
   11981 #ifdef SQLITE_OMIT_AUTOVACUUM
   11982   "OMIT_AUTOVACUUM",
   11983 #endif
   11984 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
   11985   "OMIT_BETWEEN_OPTIMIZATION",
   11986 #endif
   11987 #ifdef SQLITE_OMIT_BLOB_LITERAL
   11988   "OMIT_BLOB_LITERAL",
   11989 #endif
   11990 #ifdef SQLITE_OMIT_BTREECOUNT
   11991   "OMIT_BTREECOUNT",
   11992 #endif
   11993 #ifdef SQLITE_OMIT_BUILTIN_TEST
   11994   "OMIT_BUILTIN_TEST",
   11995 #endif
   11996 #ifdef SQLITE_OMIT_CAST
   11997   "OMIT_CAST",
   11998 #endif
   11999 #ifdef SQLITE_OMIT_CHECK
   12000   "OMIT_CHECK",
   12001 #endif
   12002 /* // redundant
   12003 ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
   12004 **   "OMIT_COMPILEOPTION_DIAGS",
   12005 ** #endif
   12006 */
   12007 #ifdef SQLITE_OMIT_COMPLETE
   12008   "OMIT_COMPLETE",
   12009 #endif
   12010 #ifdef SQLITE_OMIT_COMPOUND_SELECT
   12011   "OMIT_COMPOUND_SELECT",
   12012 #endif
   12013 #ifdef SQLITE_OMIT_DATETIME_FUNCS
   12014   "OMIT_DATETIME_FUNCS",
   12015 #endif
   12016 #ifdef SQLITE_OMIT_DECLTYPE
   12017   "OMIT_DECLTYPE",
   12018 #endif
   12019 #ifdef SQLITE_OMIT_DEPRECATED
   12020   "OMIT_DEPRECATED",
   12021 #endif
   12022 #ifdef SQLITE_OMIT_DISKIO
   12023   "OMIT_DISKIO",
   12024 #endif
   12025 #ifdef SQLITE_OMIT_EXPLAIN
   12026   "OMIT_EXPLAIN",
   12027 #endif
   12028 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
   12029   "OMIT_FLAG_PRAGMAS",
   12030 #endif
   12031 #ifdef SQLITE_OMIT_FLOATING_POINT
   12032   "OMIT_FLOATING_POINT",
   12033 #endif
   12034 #ifdef SQLITE_OMIT_FOREIGN_KEY
   12035   "OMIT_FOREIGN_KEY",
   12036 #endif
   12037 #ifdef SQLITE_OMIT_GET_TABLE
   12038   "OMIT_GET_TABLE",
   12039 #endif
   12040 #ifdef SQLITE_OMIT_INCRBLOB
   12041   "OMIT_INCRBLOB",
   12042 #endif
   12043 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
   12044   "OMIT_INTEGRITY_CHECK",
   12045 #endif
   12046 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
   12047   "OMIT_LIKE_OPTIMIZATION",
   12048 #endif
   12049 #ifdef SQLITE_OMIT_LOAD_EXTENSION
   12050   "OMIT_LOAD_EXTENSION",
   12051 #endif
   12052 #ifdef SQLITE_OMIT_LOCALTIME
   12053   "OMIT_LOCALTIME",
   12054 #endif
   12055 #ifdef SQLITE_OMIT_LOOKASIDE
   12056   "OMIT_LOOKASIDE",
   12057 #endif
   12058 #ifdef SQLITE_OMIT_MEMORYDB
   12059   "OMIT_MEMORYDB",
   12060 #endif
   12061 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
   12062   "OMIT_OR_OPTIMIZATION",
   12063 #endif
   12064 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
   12065   "OMIT_PAGER_PRAGMAS",
   12066 #endif
   12067 #ifdef SQLITE_OMIT_PRAGMA
   12068   "OMIT_PRAGMA",
   12069 #endif
   12070 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
   12071   "OMIT_PROGRESS_CALLBACK",
   12072 #endif
   12073 #ifdef SQLITE_OMIT_QUICKBALANCE
   12074   "OMIT_QUICKBALANCE",
   12075 #endif
   12076 #ifdef SQLITE_OMIT_REINDEX
   12077   "OMIT_REINDEX",
   12078 #endif
   12079 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
   12080   "OMIT_SCHEMA_PRAGMAS",
   12081 #endif
   12082 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
   12083   "OMIT_SCHEMA_VERSION_PRAGMAS",
   12084 #endif
   12085 #ifdef SQLITE_OMIT_SHARED_CACHE
   12086   "OMIT_SHARED_CACHE",
   12087 #endif
   12088 #ifdef SQLITE_OMIT_SUBQUERY
   12089   "OMIT_SUBQUERY",
   12090 #endif
   12091 #ifdef SQLITE_OMIT_TCL_VARIABLE
   12092   "OMIT_TCL_VARIABLE",
   12093 #endif
   12094 #ifdef SQLITE_OMIT_TEMPDB
   12095   "OMIT_TEMPDB",
   12096 #endif
   12097 #ifdef SQLITE_OMIT_TRACE
   12098   "OMIT_TRACE",
   12099 #endif
   12100 #ifdef SQLITE_OMIT_TRIGGER
   12101   "OMIT_TRIGGER",
   12102 #endif
   12103 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
   12104   "OMIT_TRUNCATE_OPTIMIZATION",
   12105 #endif
   12106 #ifdef SQLITE_OMIT_UTF16
   12107   "OMIT_UTF16",
   12108 #endif
   12109 #ifdef SQLITE_OMIT_VACUUM
   12110   "OMIT_VACUUM",
   12111 #endif
   12112 #ifdef SQLITE_OMIT_VIEW
   12113   "OMIT_VIEW",
   12114 #endif
   12115 #ifdef SQLITE_OMIT_VIRTUALTABLE
   12116   "OMIT_VIRTUALTABLE",
   12117 #endif
   12118 #ifdef SQLITE_OMIT_WAL
   12119   "OMIT_WAL",
   12120 #endif
   12121 #ifdef SQLITE_OMIT_WSD
   12122   "OMIT_WSD",
   12123 #endif
   12124 #ifdef SQLITE_OMIT_XFER_OPT
   12125   "OMIT_XFER_OPT",
   12126 #endif
   12127 #ifdef SQLITE_PERFORMANCE_TRACE
   12128   "PERFORMANCE_TRACE",
   12129 #endif
   12130 #ifdef SQLITE_PROXY_DEBUG
   12131   "PROXY_DEBUG",
   12132 #endif
   12133 #ifdef SQLITE_SECURE_DELETE
   12134   "SECURE_DELETE",
   12135 #endif
   12136 #ifdef SQLITE_SMALL_STACK
   12137   "SMALL_STACK",
   12138 #endif
   12139 #ifdef SQLITE_SOUNDEX
   12140   "SOUNDEX",
   12141 #endif
   12142 #ifdef SQLITE_TCL
   12143   "TCL",
   12144 #endif
   12145 #ifdef SQLITE_TEMP_STORE
   12146   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
   12147 #endif
   12148 #ifdef SQLITE_TEST
   12149   "TEST",
   12150 #endif
   12151 #ifdef SQLITE_THREADSAFE
   12152   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
   12153 #endif
   12154 #ifdef SQLITE_USE_ALLOCA
   12155   "USE_ALLOCA",
   12156 #endif
   12157 #ifdef SQLITE_ZERO_MALLOC
   12158   "ZERO_MALLOC"
   12159 #endif
   12160 };
   12161 
   12162 /*
   12163 ** Given the name of a compile-time option, return true if that option
   12164 ** was used and false if not.
   12165 **
   12166 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
   12167 ** is not required for a match.
   12168 */
   12169 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
   12170   int i, n;
   12171   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
   12172   n = sqlite3Strlen30(zOptName);
   12173 
   12174   /* Since ArraySize(azCompileOpt) is normally in single digits, a
   12175   ** linear search is adequate.  No need for a binary search. */
   12176   for(i=0; i<ArraySize(azCompileOpt); i++){
   12177     if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
   12178        && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
   12179   }
   12180   return 0;
   12181 }
   12182 
   12183 /*
   12184 ** Return the N-th compile-time option string.  If N is out of range,
   12185 ** return a NULL pointer.
   12186 */
   12187 SQLITE_API const char *sqlite3_compileoption_get(int N){
   12188   if( N>=0 && N<ArraySize(azCompileOpt) ){
   12189     return azCompileOpt[N];
   12190   }
   12191   return 0;
   12192 }
   12193 
   12194 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   12195 
   12196 /************** End of ctime.c ***********************************************/
   12197 /************** Begin file status.c ******************************************/
   12198 /*
   12199 ** 2008 June 18
   12200 **
   12201 ** The author disclaims copyright to this source code.  In place of
   12202 ** a legal notice, here is a blessing:
   12203 **
   12204 **    May you do good and not evil.
   12205 **    May you find forgiveness for yourself and forgive others.
   12206 **    May you share freely, never taking more than you give.
   12207 **
   12208 *************************************************************************
   12209 **
   12210 ** This module implements the sqlite3_status() interface and related
   12211 ** functionality.
   12212 */
   12213 /************** Include vdbeInt.h in the middle of status.c ******************/
   12214 /************** Begin file vdbeInt.h *****************************************/
   12215 /*
   12216 ** 2003 September 6
   12217 **
   12218 ** The author disclaims copyright to this source code.  In place of
   12219 ** a legal notice, here is a blessing:
   12220 **
   12221 **    May you do good and not evil.
   12222 **    May you find forgiveness for yourself and forgive others.
   12223 **    May you share freely, never taking more than you give.
   12224 **
   12225 *************************************************************************
   12226 ** This is the header file for information that is private to the
   12227 ** VDBE.  This information used to all be at the top of the single
   12228 ** source code file "vdbe.c".  When that file became too big (over
   12229 ** 6000 lines long) it was split up into several smaller files and
   12230 ** this header information was factored out.
   12231 */
   12232 #ifndef _VDBEINT_H_
   12233 #define _VDBEINT_H_
   12234 
   12235 /*
   12236 ** SQL is translated into a sequence of instructions to be
   12237 ** executed by a virtual machine.  Each instruction is an instance
   12238 ** of the following structure.
   12239 */
   12240 typedef struct VdbeOp Op;
   12241 
   12242 /*
   12243 ** Boolean values
   12244 */
   12245 typedef unsigned char Bool;
   12246 
   12247 /*
   12248 ** A cursor is a pointer into a single BTree within a database file.
   12249 ** The cursor can seek to a BTree entry with a particular key, or
   12250 ** loop over all entries of the Btree.  You can also insert new BTree
   12251 ** entries or retrieve the key or data from the entry that the cursor
   12252 ** is currently pointing to.
   12253 **
   12254 ** Every cursor that the virtual machine has open is represented by an
   12255 ** instance of the following structure.
   12256 */
   12257 struct VdbeCursor {
   12258   BtCursor *pCursor;    /* The cursor structure of the backend */
   12259   Btree *pBt;           /* Separate file holding temporary table */
   12260   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
   12261   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
   12262   int pseudoTableReg;   /* Register holding pseudotable content. */
   12263   int nField;           /* Number of fields in the header */
   12264   Bool zeroed;          /* True if zeroed out and ready for reuse */
   12265   Bool rowidIsValid;    /* True if lastRowid is valid */
   12266   Bool atFirst;         /* True if pointing to first entry */
   12267   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
   12268   Bool nullRow;         /* True if pointing to a row with no data */
   12269   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
   12270   Bool isTable;         /* True if a table requiring integer keys */
   12271   Bool isIndex;         /* True if an index containing keys only - no data */
   12272   Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
   12273   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
   12274   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
   12275   i64 seqCount;         /* Sequence counter */
   12276   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
   12277   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
   12278 
   12279   /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
   12280   ** OP_IsUnique opcode on this cursor. */
   12281   int seekResult;
   12282 
   12283   /* Cached information about the header for the data record that the
   12284   ** cursor is currently pointing to.  Only valid if cacheStatus matches
   12285   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
   12286   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
   12287   ** the cache is out of date.
   12288   **
   12289   ** aRow might point to (ephemeral) data for the current row, or it might
   12290   ** be NULL.
   12291   */
   12292   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
   12293   int payloadSize;      /* Total number of bytes in the record */
   12294   u32 *aType;           /* Type values for all entries in the record */
   12295   u32 *aOffset;         /* Cached offsets to the start of each columns data */
   12296   u8 *aRow;             /* Data for the current row, if all on one page */
   12297 };
   12298 typedef struct VdbeCursor VdbeCursor;
   12299 
   12300 /*
   12301 ** When a sub-program is executed (OP_Program), a structure of this type
   12302 ** is allocated to store the current value of the program counter, as
   12303 ** well as the current memory cell array and various other frame specific
   12304 ** values stored in the Vdbe struct. When the sub-program is finished,
   12305 ** these values are copied back to the Vdbe from the VdbeFrame structure,
   12306 ** restoring the state of the VM to as it was before the sub-program
   12307 ** began executing.
   12308 **
   12309 ** The memory for a VdbeFrame object is allocated and managed by a memory
   12310 ** cell in the parent (calling) frame. When the memory cell is deleted or
   12311 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
   12312 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
   12313 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
   12314 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
   12315 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
   12316 ** child frame are released.
   12317 **
   12318 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
   12319 ** set to NULL if the currently executing frame is the main program.
   12320 */
   12321 typedef struct VdbeFrame VdbeFrame;
   12322 struct VdbeFrame {
   12323   Vdbe *v;                /* VM this frame belongs to */
   12324   int pc;                 /* Program Counter in parent (calling) frame */
   12325   Op *aOp;                /* Program instructions for parent frame */
   12326   int nOp;                /* Size of aOp array */
   12327   Mem *aMem;              /* Array of memory cells for parent frame */
   12328   int nMem;               /* Number of entries in aMem */
   12329   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
   12330   u16 nCursor;            /* Number of entries in apCsr */
   12331   void *token;            /* Copy of SubProgram.token */
   12332   int nChildMem;          /* Number of memory cells for child frame */
   12333   int nChildCsr;          /* Number of cursors for child frame */
   12334   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
   12335   int nChange;            /* Statement changes (Vdbe.nChanges)     */
   12336   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
   12337 };
   12338 
   12339 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
   12340 
   12341 /*
   12342 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
   12343 */
   12344 #define CACHE_STALE 0
   12345 
   12346 /*
   12347 ** Internally, the vdbe manipulates nearly all SQL values as Mem
   12348 ** structures. Each Mem struct may cache multiple representations (string,
   12349 ** integer etc.) of the same value.
   12350 */
   12351 struct Mem {
   12352   sqlite3 *db;        /* The associated database connection */
   12353   char *z;            /* String or BLOB value */
   12354   double r;           /* Real value */
   12355   union {
   12356     i64 i;              /* Integer value used when MEM_Int is set in flags */
   12357     int nZero;          /* Used when bit MEM_Zero is set in flags */
   12358     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   12359     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   12360     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   12361   } u;
   12362   int n;              /* Number of characters in string value, excluding '\0' */
   12363   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   12364   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
   12365   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   12366 #ifdef SQLITE_DEBUG
   12367   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   12368   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   12369 #endif
   12370   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   12371   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
   12372 };
   12373 
   12374 /* One or more of the following flags are set to indicate the validOK
   12375 ** representations of the value stored in the Mem struct.
   12376 **
   12377 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   12378 ** No other flags may be set in this case.
   12379 **
   12380 ** If the MEM_Str flag is set then Mem.z points at a string representation.
   12381 ** Usually this is encoded in the same unicode encoding as the main
   12382 ** database (see below for exceptions). If the MEM_Term flag is also
   12383 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
   12384 ** flags may coexist with the MEM_Str flag.
   12385 */
   12386 #define MEM_Null      0x0001   /* Value is NULL */
   12387 #define MEM_Str       0x0002   /* Value is a string */
   12388 #define MEM_Int       0x0004   /* Value is an integer */
   12389 #define MEM_Real      0x0008   /* Value is a real number */
   12390 #define MEM_Blob      0x0010   /* Value is a BLOB */
   12391 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   12392 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   12393 #define MEM_Invalid   0x0080   /* Value is undefined */
   12394 #define MEM_TypeMask  0x00ff   /* Mask of type bits */
   12395 
   12396 /* Whenever Mem contains a valid string or blob representation, one of
   12397 ** the following flags must be set to determine the memory management
   12398 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
   12399 ** string is \000 or \u0000 terminated
   12400 */
   12401 #define MEM_Term      0x0200   /* String rep is nul terminated */
   12402 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
   12403 #define MEM_Static    0x0800   /* Mem.z points to a static string */
   12404 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
   12405 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
   12406 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
   12407 #ifdef SQLITE_OMIT_INCRBLOB
   12408   #undef MEM_Zero
   12409   #define MEM_Zero 0x0000
   12410 #endif
   12411 
   12412 /*
   12413 ** Clear any existing type flags from a Mem and replace them with f
   12414 */
   12415 #define MemSetTypeFlag(p, f) \
   12416    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
   12417 
   12418 /*
   12419 ** Return true if a memory cell is not marked as invalid.  This macro
   12420 ** is for use inside assert() statements only.
   12421 */
   12422 #ifdef SQLITE_DEBUG
   12423 #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
   12424 #endif
   12425 
   12426 
   12427 /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
   12428 ** additional information about auxiliary information bound to arguments
   12429 ** of the function.  This is used to implement the sqlite3_get_auxdata()
   12430 ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
   12431 ** that can be associated with a constant argument to a function.  This
   12432 ** allows functions such as "regexp" to compile their constant regular
   12433 ** expression argument once and reused the compiled code for multiple
   12434 ** invocations.
   12435 */
   12436 struct VdbeFunc {
   12437   FuncDef *pFunc;               /* The definition of the function */
   12438   int nAux;                     /* Number of entries allocated for apAux[] */
   12439   struct AuxData {
   12440     void *pAux;                   /* Aux data for the i-th argument */
   12441     void (*xDelete)(void *);      /* Destructor for the aux data */
   12442   } apAux[1];                   /* One slot for each function argument */
   12443 };
   12444 
   12445 /*
   12446 ** The "context" argument for a installable function.  A pointer to an
   12447 ** instance of this structure is the first argument to the routines used
   12448 ** implement the SQL functions.
   12449 **
   12450 ** There is a typedef for this structure in sqlite.h.  So all routines,
   12451 ** even the public interface to SQLite, can use a pointer to this structure.
   12452 ** But this file is the only place where the internal details of this
   12453 ** structure are known.
   12454 **
   12455 ** This structure is defined inside of vdbeInt.h because it uses substructures
   12456 ** (Mem) which are only defined there.
   12457 */
   12458 struct sqlite3_context {
   12459   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
   12460   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
   12461   Mem s;                /* The return value is stored here */
   12462   Mem *pMem;            /* Memory cell used to store aggregate context */
   12463   int isError;          /* Error code returned by the function. */
   12464   CollSeq *pColl;       /* Collating sequence */
   12465 };
   12466 
   12467 /*
   12468 ** An instance of the virtual machine.  This structure contains the complete
   12469 ** state of the virtual machine.
   12470 **
   12471 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
   12472 ** is really a pointer to an instance of this structure.
   12473 **
   12474 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
   12475 ** any virtual table method invocations made by the vdbe program. It is
   12476 ** set to 2 for xDestroy method calls and 1 for all other methods. This
   12477 ** variable is used for two purposes: to allow xDestroy methods to execute
   12478 ** "DROP TABLE" statements and to prevent some nasty side effects of
   12479 ** malloc failure when SQLite is invoked recursively by a virtual table
   12480 ** method function.
   12481 */
   12482 struct Vdbe {
   12483   sqlite3 *db;            /* The database connection that owns this statement */
   12484   Op *aOp;                /* Space to hold the virtual machine's program */
   12485   Mem *aMem;              /* The memory locations */
   12486   Mem **apArg;            /* Arguments to currently executing user function */
   12487   Mem *aColName;          /* Column names to return */
   12488   Mem *pResultSet;        /* Pointer to an array of results */
   12489   int nMem;               /* Number of memory locations currently allocated */
   12490   int nOp;                /* Number of instructions in the program */
   12491   int nOpAlloc;           /* Number of slots allocated for aOp[] */
   12492   int nLabel;             /* Number of labels used */
   12493   int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
   12494   int *aLabel;            /* Space to hold the labels */
   12495   u16 nResColumn;         /* Number of columns in one row of the result set */
   12496   u16 nCursor;            /* Number of slots in apCsr[] */
   12497   u32 magic;              /* Magic number for sanity checking */
   12498   char *zErrMsg;          /* Error message written here */
   12499   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
   12500   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
   12501   Mem *aVar;              /* Values for the OP_Variable opcode. */
   12502   char **azVar;           /* Name of variables */
   12503   ynVar nVar;             /* Number of entries in aVar[] */
   12504   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   12505   int pc;                 /* The program counter */
   12506   int rc;                 /* Value to return */
   12507   u8 errorAction;         /* Recovery action to do in case of an error */
   12508   u8 okVar;               /* True if azVar[] has been initialized */
   12509   u8 explain;             /* True if EXPLAIN present on SQL command */
   12510   u8 changeCntOn;         /* True to update the change-counter */
   12511   u8 expired;             /* True if the VM needs to be recompiled */
   12512   u8 runOnlyOnce;         /* Automatically expire on reset */
   12513   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   12514   u8 inVtabMethod;        /* See comments above */
   12515   u8 usesStmtJournal;     /* True if uses a statement journal */
   12516   u8 readOnly;            /* True for read-only statements */
   12517   u8 isPrepareV2;         /* True if prepared with prepare_v2() */
   12518   int nChange;            /* Number of db changes made since last reset */
   12519   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
   12520   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
   12521   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
   12522   int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
   12523 #ifndef SQLITE_OMIT_TRACE
   12524   i64 startTime;          /* Time when query started - used for profiling */
   12525 #endif
   12526   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
   12527   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
   12528   char *zSql;             /* Text of the SQL statement that generated this */
   12529   void *pFree;            /* Free this when deleting the vdbe */
   12530 #ifdef SQLITE_DEBUG
   12531   FILE *trace;            /* Write an execution trace here, if not NULL */
   12532 #endif
   12533   VdbeFrame *pFrame;      /* Parent frame */
   12534   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   12535   int nFrame;             /* Number of frames in pFrame list */
   12536   u32 expmask;            /* Binding to these vars invalidates VM */
   12537   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
   12538 };
   12539 
   12540 /*
   12541 ** The following are allowed values for Vdbe.magic
   12542 */
   12543 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
   12544 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
   12545 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
   12546 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
   12547 
   12548 /*
   12549 ** Function prototypes
   12550 */
   12551 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
   12552 void sqliteVdbePopStack(Vdbe*,int);
   12553 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
   12554 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
   12555 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
   12556 #endif
   12557 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
   12558 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
   12559 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
   12560 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   12561 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
   12562 
   12563 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   12564 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
   12565 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
   12566 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   12567 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
   12568 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
   12569 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
   12570 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
   12571 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
   12572 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
   12573 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
   12574 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
   12575 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
   12576 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   12577 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
   12578 #ifdef SQLITE_OMIT_FLOATING_POINT
   12579 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
   12580 #else
   12581 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
   12582 #endif
   12583 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
   12584 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   12585 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
   12586 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
   12587 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
   12588 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
   12589 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
   12590 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
   12591 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
   12592 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
   12593 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
   12594 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   12595 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
   12596 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
   12597 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   12598 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
   12599 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   12600 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
   12601 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
   12602 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
   12603 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
   12604 
   12605 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   12606 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
   12607 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
   12608 #else
   12609 # define sqlite3VdbeEnter(X)
   12610 # define sqlite3VdbeLeave(X)
   12611 #endif
   12612 
   12613 #ifdef SQLITE_DEBUG
   12614 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
   12615 #endif
   12616 
   12617 #ifndef SQLITE_OMIT_FOREIGN_KEY
   12618 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
   12619 #else
   12620 # define sqlite3VdbeCheckFk(p,i) 0
   12621 #endif
   12622 
   12623 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
   12624 #ifdef SQLITE_DEBUG
   12625 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
   12626 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
   12627 #endif
   12628 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
   12629 
   12630 #ifndef SQLITE_OMIT_INCRBLOB
   12631 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
   12632 #else
   12633   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
   12634 #endif
   12635 
   12636 #endif /* !defined(_VDBEINT_H_) */
   12637 
   12638 /************** End of vdbeInt.h *********************************************/
   12639 /************** Continuing where we left off in status.c *********************/
   12640 
   12641 /*
   12642 ** Variables in which to record status information.
   12643 */
   12644 typedef struct sqlite3StatType sqlite3StatType;
   12645 static SQLITE_WSD struct sqlite3StatType {
   12646   int nowValue[10];         /* Current value */
   12647   int mxValue[10];          /* Maximum value */
   12648 } sqlite3Stat = { {0,}, {0,} };
   12649 
   12650 
   12651 /* The "wsdStat" macro will resolve to the status information
   12652 ** state vector.  If writable static data is unsupported on the target,
   12653 ** we have to locate the state vector at run-time.  In the more common
   12654 ** case where writable static data is supported, wsdStat can refer directly
   12655 ** to the "sqlite3Stat" state vector declared above.
   12656 */
   12657 #ifdef SQLITE_OMIT_WSD
   12658 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
   12659 # define wsdStat x[0]
   12660 #else
   12661 # define wsdStatInit
   12662 # define wsdStat sqlite3Stat
   12663 #endif
   12664 
   12665 /*
   12666 ** Return the current value of a status parameter.
   12667 */
   12668 SQLITE_PRIVATE int sqlite3StatusValue(int op){
   12669   wsdStatInit;
   12670   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   12671   return wsdStat.nowValue[op];
   12672 }
   12673 
   12674 /*
   12675 ** Add N to the value of a status record.  It is assumed that the
   12676 ** caller holds appropriate locks.
   12677 */
   12678 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
   12679   wsdStatInit;
   12680   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   12681   wsdStat.nowValue[op] += N;
   12682   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
   12683     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   12684   }
   12685 }
   12686 
   12687 /*
   12688 ** Set the value of a status to X.
   12689 */
   12690 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
   12691   wsdStatInit;
   12692   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   12693   wsdStat.nowValue[op] = X;
   12694   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
   12695     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   12696   }
   12697 }
   12698 
   12699 /*
   12700 ** Query status information.
   12701 **
   12702 ** This implementation assumes that reading or writing an aligned
   12703 ** 32-bit integer is an atomic operation.  If that assumption is not true,
   12704 ** then this routine is not threadsafe.
   12705 */
   12706 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
   12707   wsdStatInit;
   12708   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
   12709     return SQLITE_MISUSE_BKPT;
   12710   }
   12711   *pCurrent = wsdStat.nowValue[op];
   12712   *pHighwater = wsdStat.mxValue[op];
   12713   if( resetFlag ){
   12714     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   12715   }
   12716   return SQLITE_OK;
   12717 }
   12718 
   12719 /*
   12720 ** Query status information for a single database connection
   12721 */
   12722 SQLITE_API int sqlite3_db_status(
   12723   sqlite3 *db,          /* The database connection whose status is desired */
   12724   int op,               /* Status verb */
   12725   int *pCurrent,        /* Write current value here */
   12726   int *pHighwater,      /* Write high-water mark here */
   12727   int resetFlag         /* Reset high-water mark if true */
   12728 ){
   12729   int rc = SQLITE_OK;   /* Return code */
   12730   sqlite3_mutex_enter(db->mutex);
   12731   switch( op ){
   12732     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
   12733       *pCurrent = db->lookaside.nOut;
   12734       *pHighwater = db->lookaside.mxOut;
   12735       if( resetFlag ){
   12736         db->lookaside.mxOut = db->lookaside.nOut;
   12737       }
   12738       break;
   12739     }
   12740 
   12741     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
   12742     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
   12743     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
   12744       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
   12745       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
   12746       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
   12747       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
   12748       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
   12749       *pCurrent = 0;
   12750       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
   12751       if( resetFlag ){
   12752         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
   12753       }
   12754       break;
   12755     }
   12756 
   12757     /*
   12758     ** Return an approximation for the amount of memory currently used
   12759     ** by all pagers associated with the given database connection.  The
   12760     ** highwater mark is meaningless and is returned as zero.
   12761     */
   12762     case SQLITE_DBSTATUS_CACHE_USED: {
   12763       int totalUsed = 0;
   12764       int i;
   12765       sqlite3BtreeEnterAll(db);
   12766       for(i=0; i<db->nDb; i++){
   12767         Btree *pBt = db->aDb[i].pBt;
   12768         if( pBt ){
   12769           Pager *pPager = sqlite3BtreePager(pBt);
   12770           totalUsed += sqlite3PagerMemUsed(pPager);
   12771         }
   12772       }
   12773       sqlite3BtreeLeaveAll(db);
   12774       *pCurrent = totalUsed;
   12775       *pHighwater = 0;
   12776       break;
   12777     }
   12778 
   12779     /*
   12780     ** *pCurrent gets an accurate estimate of the amount of memory used
   12781     ** to store the schema for all databases (main, temp, and any ATTACHed
   12782     ** databases.  *pHighwater is set to zero.
   12783     */
   12784     case SQLITE_DBSTATUS_SCHEMA_USED: {
   12785       int i;                      /* Used to iterate through schemas */
   12786       int nByte = 0;              /* Used to accumulate return value */
   12787 
   12788       sqlite3BtreeEnterAll(db);
   12789       db->pnBytesFreed = &nByte;
   12790       for(i=0; i<db->nDb; i++){
   12791         Schema *pSchema = db->aDb[i].pSchema;
   12792         if( ALWAYS(pSchema!=0) ){
   12793           HashElem *p;
   12794 
   12795           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
   12796               pSchema->tblHash.count
   12797             + pSchema->trigHash.count
   12798             + pSchema->idxHash.count
   12799             + pSchema->fkeyHash.count
   12800           );
   12801           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
   12802           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
   12803           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
   12804           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
   12805 
   12806           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
   12807             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
   12808           }
   12809           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
   12810             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
   12811           }
   12812         }
   12813       }
   12814       db->pnBytesFreed = 0;
   12815       sqlite3BtreeLeaveAll(db);
   12816 
   12817       *pHighwater = 0;
   12818       *pCurrent = nByte;
   12819       break;
   12820     }
   12821 
   12822     /*
   12823     ** *pCurrent gets an accurate estimate of the amount of memory used
   12824     ** to store all prepared statements.
   12825     ** *pHighwater is set to zero.
   12826     */
   12827     case SQLITE_DBSTATUS_STMT_USED: {
   12828       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
   12829       int nByte = 0;              /* Used to accumulate return value */
   12830 
   12831       db->pnBytesFreed = &nByte;
   12832       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
   12833         sqlite3VdbeDeleteObject(db, pVdbe);
   12834       }
   12835       db->pnBytesFreed = 0;
   12836 
   12837       *pHighwater = 0;
   12838       *pCurrent = nByte;
   12839 
   12840       break;
   12841     }
   12842 
   12843     default: {
   12844       rc = SQLITE_ERROR;
   12845     }
   12846   }
   12847   sqlite3_mutex_leave(db->mutex);
   12848   return rc;
   12849 }
   12850 
   12851 /************** End of status.c **********************************************/
   12852 /************** Begin file date.c ********************************************/
   12853 /*
   12854 ** 2003 October 31
   12855 **
   12856 ** The author disclaims copyright to this source code.  In place of
   12857 ** a legal notice, here is a blessing:
   12858 **
   12859 **    May you do good and not evil.
   12860 **    May you find forgiveness for yourself and forgive others.
   12861 **    May you share freely, never taking more than you give.
   12862 **
   12863 *************************************************************************
   12864 ** This file contains the C functions that implement date and time
   12865 ** functions for SQLite.
   12866 **
   12867 ** There is only one exported symbol in this file - the function
   12868 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
   12869 ** All other code has file scope.
   12870 **
   12871 ** SQLite processes all times and dates as Julian Day numbers.  The
   12872 ** dates and times are stored as the number of days since noon
   12873 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
   12874 ** calendar system.
   12875 **
   12876 ** 1970-01-01 00:00:00 is JD 2440587.5
   12877 ** 2000-01-01 00:00:00 is JD 2451544.5
   12878 **
   12879 ** This implemention requires years to be expressed as a 4-digit number
   12880 ** which means that only dates between 0000-01-01 and 9999-12-31 can
   12881 ** be represented, even though julian day numbers allow a much wider
   12882 ** range of dates.
   12883 **
   12884 ** The Gregorian calendar system is used for all dates and times,
   12885 ** even those that predate the Gregorian calendar.  Historians usually
   12886 ** use the Julian calendar for dates prior to 1582-10-15 and for some
   12887 ** dates afterwards, depending on locale.  Beware of this difference.
   12888 **
   12889 ** The conversion algorithms are implemented based on descriptions
   12890 ** in the following text:
   12891 **
   12892 **      Jean Meeus
   12893 **      Astronomical Algorithms, 2nd Edition, 1998
   12894 **      ISBM 0-943396-61-1
   12895 **      Willmann-Bell, Inc
   12896 **      Richmond, Virginia (USA)
   12897 */
   12898 #include <time.h>
   12899 
   12900 #ifndef SQLITE_OMIT_DATETIME_FUNCS
   12901 
   12902 /*
   12903 ** On recent Windows platforms, the localtime_s() function is available
   12904 ** as part of the "Secure CRT". It is essentially equivalent to
   12905 ** localtime_r() available under most POSIX platforms, except that the
   12906 ** order of the parameters is reversed.
   12907 **
   12908 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
   12909 **
   12910 ** If the user has not indicated to use localtime_r() or localtime_s()
   12911 ** already, check for an MSVC build environment that provides
   12912 ** localtime_s().
   12913 */
   12914 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
   12915      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
   12916 #define HAVE_LOCALTIME_S 1
   12917 #endif
   12918 
   12919 /*
   12920 ** A structure for holding a single date and time.
   12921 */
   12922 typedef struct DateTime DateTime;
   12923 struct DateTime {
   12924   sqlite3_int64 iJD; /* The julian day number times 86400000 */
   12925   int Y, M, D;       /* Year, month, and day */
   12926   int h, m;          /* Hour and minutes */
   12927   int tz;            /* Timezone offset in minutes */
   12928   double s;          /* Seconds */
   12929   char validYMD;     /* True (1) if Y,M,D are valid */
   12930   char validHMS;     /* True (1) if h,m,s are valid */
   12931   char validJD;      /* True (1) if iJD is valid */
   12932   char validTZ;      /* True (1) if tz is valid */
   12933 };
   12934 
   12935 
   12936 /*
   12937 ** Convert zDate into one or more integers.  Additional arguments
   12938 ** come in groups of 5 as follows:
   12939 **
   12940 **       N       number of digits in the integer
   12941 **       min     minimum allowed value of the integer
   12942 **       max     maximum allowed value of the integer
   12943 **       nextC   first character after the integer
   12944 **       pVal    where to write the integers value.
   12945 **
   12946 ** Conversions continue until one with nextC==0 is encountered.
   12947 ** The function returns the number of successful conversions.
   12948 */
   12949 static int getDigits(const char *zDate, ...){
   12950   va_list ap;
   12951   int val;
   12952   int N;
   12953   int min;
   12954   int max;
   12955   int nextC;
   12956   int *pVal;
   12957   int cnt = 0;
   12958   va_start(ap, zDate);
   12959   do{
   12960     N = va_arg(ap, int);
   12961     min = va_arg(ap, int);
   12962     max = va_arg(ap, int);
   12963     nextC = va_arg(ap, int);
   12964     pVal = va_arg(ap, int*);
   12965     val = 0;
   12966     while( N-- ){
   12967       if( !sqlite3Isdigit(*zDate) ){
   12968         goto end_getDigits;
   12969       }
   12970       val = val*10 + *zDate - '0';
   12971       zDate++;
   12972     }
   12973     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
   12974       goto end_getDigits;
   12975     }
   12976     *pVal = val;
   12977     zDate++;
   12978     cnt++;
   12979   }while( nextC );
   12980 end_getDigits:
   12981   va_end(ap);
   12982   return cnt;
   12983 }
   12984 
   12985 /*
   12986 ** Parse a timezone extension on the end of a date-time.
   12987 ** The extension is of the form:
   12988 **
   12989 **        (+/-)HH:MM
   12990 **
   12991 ** Or the "zulu" notation:
   12992 **
   12993 **        Z
   12994 **
   12995 ** If the parse is successful, write the number of minutes
   12996 ** of change in p->tz and return 0.  If a parser error occurs,
   12997 ** return non-zero.
   12998 **
   12999 ** A missing specifier is not considered an error.
   13000 */
   13001 static int parseTimezone(const char *zDate, DateTime *p){
   13002   int sgn = 0;
   13003   int nHr, nMn;
   13004   int c;
   13005   while( sqlite3Isspace(*zDate) ){ zDate++; }
   13006   p->tz = 0;
   13007   c = *zDate;
   13008   if( c=='-' ){
   13009     sgn = -1;
   13010   }else if( c=='+' ){
   13011     sgn = +1;
   13012   }else if( c=='Z' || c=='z' ){
   13013     zDate++;
   13014     goto zulu_time;
   13015   }else{
   13016     return c!=0;
   13017   }
   13018   zDate++;
   13019   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
   13020     return 1;
   13021   }
   13022   zDate += 5;
   13023   p->tz = sgn*(nMn + nHr*60);
   13024 zulu_time:
   13025   while( sqlite3Isspace(*zDate) ){ zDate++; }
   13026   return *zDate!=0;
   13027 }
   13028 
   13029 /*
   13030 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
   13031 ** The HH, MM, and SS must each be exactly 2 digits.  The
   13032 ** fractional seconds FFFF can be one or more digits.
   13033 **
   13034 ** Return 1 if there is a parsing error and 0 on success.
   13035 */
   13036 static int parseHhMmSs(const char *zDate, DateTime *p){
   13037   int h, m, s;
   13038   double ms = 0.0;
   13039   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
   13040     return 1;
   13041   }
   13042   zDate += 5;
   13043   if( *zDate==':' ){
   13044     zDate++;
   13045     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
   13046       return 1;
   13047     }
   13048     zDate += 2;
   13049     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
   13050       double rScale = 1.0;
   13051       zDate++;
   13052       while( sqlite3Isdigit(*zDate) ){
   13053         ms = ms*10.0 + *zDate - '0';
   13054         rScale *= 10.0;
   13055         zDate++;
   13056       }
   13057       ms /= rScale;
   13058     }
   13059   }else{
   13060     s = 0;
   13061   }
   13062   p->validJD = 0;
   13063   p->validHMS = 1;
   13064   p->h = h;
   13065   p->m = m;
   13066   p->s = s + ms;
   13067   if( parseTimezone(zDate, p) ) return 1;
   13068   p->validTZ = (p->tz!=0)?1:0;
   13069   return 0;
   13070 }
   13071 
   13072 /*
   13073 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
   13074 ** that the YYYY-MM-DD is according to the Gregorian calendar.
   13075 **
   13076 ** Reference:  Meeus page 61
   13077 */
   13078 static void computeJD(DateTime *p){
   13079   int Y, M, D, A, B, X1, X2;
   13080 
   13081   if( p->validJD ) return;
   13082   if( p->validYMD ){
   13083     Y = p->Y;
   13084     M = p->M;
   13085     D = p->D;
   13086   }else{
   13087     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
   13088     M = 1;
   13089     D = 1;
   13090   }
   13091   if( M<=2 ){
   13092     Y--;
   13093     M += 12;
   13094   }
   13095   A = Y/100;
   13096   B = 2 - A + (A/4);
   13097   X1 = 36525*(Y+4716)/100;
   13098   X2 = 306001*(M+1)/10000;
   13099   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
   13100   p->validJD = 1;
   13101   if( p->validHMS ){
   13102     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
   13103     if( p->validTZ ){
   13104       p->iJD -= p->tz*60000;
   13105       p->validYMD = 0;
   13106       p->validHMS = 0;
   13107       p->validTZ = 0;
   13108     }
   13109   }
   13110 }
   13111 
   13112 /*
   13113 ** Parse dates of the form
   13114 **
   13115 **     YYYY-MM-DD HH:MM:SS.FFF
   13116 **     YYYY-MM-DD HH:MM:SS
   13117 **     YYYY-MM-DD HH:MM
   13118 **     YYYY-MM-DD
   13119 **
   13120 ** Write the result into the DateTime structure and return 0
   13121 ** on success and 1 if the input string is not a well-formed
   13122 ** date.
   13123 */
   13124 static int parseYyyyMmDd(const char *zDate, DateTime *p){
   13125   int Y, M, D, neg;
   13126 
   13127   if( zDate[0]=='-' ){
   13128     zDate++;
   13129     neg = 1;
   13130   }else{
   13131     neg = 0;
   13132   }
   13133   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
   13134     return 1;
   13135   }
   13136   zDate += 10;
   13137   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
   13138   if( parseHhMmSs(zDate, p)==0 ){
   13139     /* We got the time */
   13140   }else if( *zDate==0 ){
   13141     p->validHMS = 0;
   13142   }else{
   13143     return 1;
   13144   }
   13145   p->validJD = 0;
   13146   p->validYMD = 1;
   13147   p->Y = neg ? -Y : Y;
   13148   p->M = M;
   13149   p->D = D;
   13150   if( p->validTZ ){
   13151     computeJD(p);
   13152   }
   13153   return 0;
   13154 }
   13155 
   13156 /*
   13157 ** Set the time to the current time reported by the VFS
   13158 */
   13159 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
   13160   sqlite3 *db = sqlite3_context_db_handle(context);
   13161   sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
   13162   p->validJD = 1;
   13163 }
   13164 
   13165 /*
   13166 ** Attempt to parse the given string into a Julian Day Number.  Return
   13167 ** the number of errors.
   13168 **
   13169 ** The following are acceptable forms for the input string:
   13170 **
   13171 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
   13172 **      DDDD.DD
   13173 **      now
   13174 **
   13175 ** In the first form, the +/-HH:MM is always optional.  The fractional
   13176 ** seconds extension (the ".FFF") is optional.  The seconds portion
   13177 ** (":SS.FFF") is option.  The year and date can be omitted as long
   13178 ** as there is a time string.  The time string can be omitted as long
   13179 ** as there is a year and date.
   13180 */
   13181 static int parseDateOrTime(
   13182   sqlite3_context *context,
   13183   const char *zDate,
   13184   DateTime *p
   13185 ){
   13186   double r;
   13187   if( parseYyyyMmDd(zDate,p)==0 ){
   13188     return 0;
   13189   }else if( parseHhMmSs(zDate, p)==0 ){
   13190     return 0;
   13191   }else if( sqlite3StrICmp(zDate,"now")==0){
   13192     setDateTimeToCurrent(context, p);
   13193     return 0;
   13194   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
   13195     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
   13196     p->validJD = 1;
   13197     return 0;
   13198   }
   13199   return 1;
   13200 }
   13201 
   13202 /*
   13203 ** Compute the Year, Month, and Day from the julian day number.
   13204 */
   13205 static void computeYMD(DateTime *p){
   13206   int Z, A, B, C, D, E, X1;
   13207   if( p->validYMD ) return;
   13208   if( !p->validJD ){
   13209     p->Y = 2000;
   13210     p->M = 1;
   13211     p->D = 1;
   13212   }else{
   13213     Z = (int)((p->iJD + 43200000)/86400000);
   13214     A = (int)((Z - 1867216.25)/36524.25);
   13215     A = Z + 1 + A - (A/4);
   13216     B = A + 1524;
   13217     C = (int)((B - 122.1)/365.25);
   13218     D = (36525*C)/100;
   13219     E = (int)((B-D)/30.6001);
   13220     X1 = (int)(30.6001*E);
   13221     p->D = B - D - X1;
   13222     p->M = E<14 ? E-1 : E-13;
   13223     p->Y = p->M>2 ? C - 4716 : C - 4715;
   13224   }
   13225   p->validYMD = 1;
   13226 }
   13227 
   13228 /*
   13229 ** Compute the Hour, Minute, and Seconds from the julian day number.
   13230 */
   13231 static void computeHMS(DateTime *p){
   13232   int s;
   13233   if( p->validHMS ) return;
   13234   computeJD(p);
   13235   s = (int)((p->iJD + 43200000) % 86400000);
   13236   p->s = s/1000.0;
   13237   s = (int)p->s;
   13238   p->s -= s;
   13239   p->h = s/3600;
   13240   s -= p->h*3600;
   13241   p->m = s/60;
   13242   p->s += s - p->m*60;
   13243   p->validHMS = 1;
   13244 }
   13245 
   13246 /*
   13247 ** Compute both YMD and HMS
   13248 */
   13249 static void computeYMD_HMS(DateTime *p){
   13250   computeYMD(p);
   13251   computeHMS(p);
   13252 }
   13253 
   13254 /*
   13255 ** Clear the YMD and HMS and the TZ
   13256 */
   13257 static void clearYMD_HMS_TZ(DateTime *p){
   13258   p->validYMD = 0;
   13259   p->validHMS = 0;
   13260   p->validTZ = 0;
   13261 }
   13262 
   13263 #ifndef SQLITE_OMIT_LOCALTIME
   13264 /*
   13265 ** Compute the difference (in milliseconds)
   13266 ** between localtime and UTC (a.k.a. GMT)
   13267 ** for the time value p where p is in UTC.
   13268 */
   13269 static sqlite3_int64 localtimeOffset(DateTime *p){
   13270   DateTime x, y;
   13271   time_t t;
   13272   x = *p;
   13273   computeYMD_HMS(&x);
   13274   if( x.Y<1971 || x.Y>=2038 ){
   13275     x.Y = 2000;
   13276     x.M = 1;
   13277     x.D = 1;
   13278     x.h = 0;
   13279     x.m = 0;
   13280     x.s = 0.0;
   13281   } else {
   13282     int s = (int)(x.s + 0.5);
   13283     x.s = s;
   13284   }
   13285   x.tz = 0;
   13286   x.validJD = 0;
   13287   computeJD(&x);
   13288   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
   13289 #ifdef HAVE_LOCALTIME_R
   13290   {
   13291     struct tm sLocal;
   13292     localtime_r(&t, &sLocal);
   13293     y.Y = sLocal.tm_year + 1900;
   13294     y.M = sLocal.tm_mon + 1;
   13295     y.D = sLocal.tm_mday;
   13296     y.h = sLocal.tm_hour;
   13297     y.m = sLocal.tm_min;
   13298     y.s = sLocal.tm_sec;
   13299   }
   13300 #elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
   13301   {
   13302     struct tm sLocal;
   13303     localtime_s(&sLocal, &t);
   13304     y.Y = sLocal.tm_year + 1900;
   13305     y.M = sLocal.tm_mon + 1;
   13306     y.D = sLocal.tm_mday;
   13307     y.h = sLocal.tm_hour;
   13308     y.m = sLocal.tm_min;
   13309     y.s = sLocal.tm_sec;
   13310   }
   13311 #else
   13312   {
   13313     struct tm *pTm;
   13314     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   13315     pTm = localtime(&t);
   13316     y.Y = pTm->tm_year + 1900;
   13317     y.M = pTm->tm_mon + 1;
   13318     y.D = pTm->tm_mday;
   13319     y.h = pTm->tm_hour;
   13320     y.m = pTm->tm_min;
   13321     y.s = pTm->tm_sec;
   13322     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   13323   }
   13324 #endif
   13325   y.validYMD = 1;
   13326   y.validHMS = 1;
   13327   y.validJD = 0;
   13328   y.validTZ = 0;
   13329   computeJD(&y);
   13330   return y.iJD - x.iJD;
   13331 }
   13332 #endif /* SQLITE_OMIT_LOCALTIME */
   13333 
   13334 /*
   13335 ** Process a modifier to a date-time stamp.  The modifiers are
   13336 ** as follows:
   13337 **
   13338 **     NNN days
   13339 **     NNN hours
   13340 **     NNN minutes
   13341 **     NNN.NNNN seconds
   13342 **     NNN months
   13343 **     NNN years
   13344 **     start of month
   13345 **     start of year
   13346 **     start of week
   13347 **     start of day
   13348 **     weekday N
   13349 **     unixepoch
   13350 **     localtime
   13351 **     utc
   13352 **
   13353 ** Return 0 on success and 1 if there is any kind of error.
   13354 */
   13355 static int parseModifier(const char *zMod, DateTime *p){
   13356   int rc = 1;
   13357   int n;
   13358   double r;
   13359   char *z, zBuf[30];
   13360   z = zBuf;
   13361   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
   13362     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
   13363   }
   13364   z[n] = 0;
   13365   switch( z[0] ){
   13366 #ifndef SQLITE_OMIT_LOCALTIME
   13367     case 'l': {
   13368       /*    localtime
   13369       **
   13370       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
   13371       ** show local time.
   13372       */
   13373       if( strcmp(z, "localtime")==0 ){
   13374         computeJD(p);
   13375         p->iJD += localtimeOffset(p);
   13376         clearYMD_HMS_TZ(p);
   13377         rc = 0;
   13378       }
   13379       break;
   13380     }
   13381 #endif
   13382     case 'u': {
   13383       /*
   13384       **    unixepoch
   13385       **
   13386       ** Treat the current value of p->iJD as the number of
   13387       ** seconds since 1970.  Convert to a real julian day number.
   13388       */
   13389       if( strcmp(z, "unixepoch")==0 && p->validJD ){
   13390         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
   13391         clearYMD_HMS_TZ(p);
   13392         rc = 0;
   13393       }
   13394 #ifndef SQLITE_OMIT_LOCALTIME
   13395       else if( strcmp(z, "utc")==0 ){
   13396         sqlite3_int64 c1;
   13397         computeJD(p);
   13398         c1 = localtimeOffset(p);
   13399         p->iJD -= c1;
   13400         clearYMD_HMS_TZ(p);
   13401         p->iJD += c1 - localtimeOffset(p);
   13402         rc = 0;
   13403       }
   13404 #endif
   13405       break;
   13406     }
   13407     case 'w': {
   13408       /*
   13409       **    weekday N
   13410       **
   13411       ** Move the date to the same time on the next occurrence of
   13412       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
   13413       ** date is already on the appropriate weekday, this is a no-op.
   13414       */
   13415       if( strncmp(z, "weekday ", 8)==0
   13416                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
   13417                && (n=(int)r)==r && n>=0 && r<7 ){
   13418         sqlite3_int64 Z;
   13419         computeYMD_HMS(p);
   13420         p->validTZ = 0;
   13421         p->validJD = 0;
   13422         computeJD(p);
   13423         Z = ((p->iJD + 129600000)/86400000) % 7;
   13424         if( Z>n ) Z -= 7;
   13425         p->iJD += (n - Z)*86400000;
   13426         clearYMD_HMS_TZ(p);
   13427         rc = 0;
   13428       }
   13429       break;
   13430     }
   13431     case 's': {
   13432       /*
   13433       **    start of TTTTT
   13434       **
   13435       ** Move the date backwards to the beginning of the current day,
   13436       ** or month or year.
   13437       */
   13438       if( strncmp(z, "start of ", 9)!=0 ) break;
   13439       z += 9;
   13440       computeYMD(p);
   13441       p->validHMS = 1;
   13442       p->h = p->m = 0;
   13443       p->s = 0.0;
   13444       p->validTZ = 0;
   13445       p->validJD = 0;
   13446       if( strcmp(z,"month")==0 ){
   13447         p->D = 1;
   13448         rc = 0;
   13449       }else if( strcmp(z,"year")==0 ){
   13450         computeYMD(p);
   13451         p->M = 1;
   13452         p->D = 1;
   13453         rc = 0;
   13454       }else if( strcmp(z,"day")==0 ){
   13455         rc = 0;
   13456       }
   13457       break;
   13458     }
   13459     case '+':
   13460     case '-':
   13461     case '0':
   13462     case '1':
   13463     case '2':
   13464     case '3':
   13465     case '4':
   13466     case '5':
   13467     case '6':
   13468     case '7':
   13469     case '8':
   13470     case '9': {
   13471       double rRounder;
   13472       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
   13473       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
   13474         rc = 1;
   13475         break;
   13476       }
   13477       if( z[n]==':' ){
   13478         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
   13479         ** specified number of hours, minutes, seconds, and fractional seconds
   13480         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
   13481         ** omitted.
   13482         */
   13483         const char *z2 = z;
   13484         DateTime tx;
   13485         sqlite3_int64 day;
   13486         if( !sqlite3Isdigit(*z2) ) z2++;
   13487         memset(&tx, 0, sizeof(tx));
   13488         if( parseHhMmSs(z2, &tx) ) break;
   13489         computeJD(&tx);
   13490         tx.iJD -= 43200000;
   13491         day = tx.iJD/86400000;
   13492         tx.iJD -= day*86400000;
   13493         if( z[0]=='-' ) tx.iJD = -tx.iJD;
   13494         computeJD(p);
   13495         clearYMD_HMS_TZ(p);
   13496         p->iJD += tx.iJD;
   13497         rc = 0;
   13498         break;
   13499       }
   13500       z += n;
   13501       while( sqlite3Isspace(*z) ) z++;
   13502       n = sqlite3Strlen30(z);
   13503       if( n>10 || n<3 ) break;
   13504       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
   13505       computeJD(p);
   13506       rc = 0;
   13507       rRounder = r<0 ? -0.5 : +0.5;
   13508       if( n==3 && strcmp(z,"day")==0 ){
   13509         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
   13510       }else if( n==4 && strcmp(z,"hour")==0 ){
   13511         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
   13512       }else if( n==6 && strcmp(z,"minute")==0 ){
   13513         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
   13514       }else if( n==6 && strcmp(z,"second")==0 ){
   13515         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
   13516       }else if( n==5 && strcmp(z,"month")==0 ){
   13517         int x, y;
   13518         computeYMD_HMS(p);
   13519         p->M += (int)r;
   13520         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
   13521         p->Y += x;
   13522         p->M -= x*12;
   13523         p->validJD = 0;
   13524         computeJD(p);
   13525         y = (int)r;
   13526         if( y!=r ){
   13527           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
   13528         }
   13529       }else if( n==4 && strcmp(z,"year")==0 ){
   13530         int y = (int)r;
   13531         computeYMD_HMS(p);
   13532         p->Y += y;
   13533         p->validJD = 0;
   13534         computeJD(p);
   13535         if( y!=r ){
   13536           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
   13537         }
   13538       }else{
   13539         rc = 1;
   13540       }
   13541       clearYMD_HMS_TZ(p);
   13542       break;
   13543     }
   13544     default: {
   13545       break;
   13546     }
   13547   }
   13548   return rc;
   13549 }
   13550 
   13551 /*
   13552 ** Process time function arguments.  argv[0] is a date-time stamp.
   13553 ** argv[1] and following are modifiers.  Parse them all and write
   13554 ** the resulting time into the DateTime structure p.  Return 0
   13555 ** on success and 1 if there are any errors.
   13556 **
   13557 ** If there are zero parameters (if even argv[0] is undefined)
   13558 ** then assume a default value of "now" for argv[0].
   13559 */
   13560 static int isDate(
   13561   sqlite3_context *context,
   13562   int argc,
   13563   sqlite3_value **argv,
   13564   DateTime *p
   13565 ){
   13566   int i;
   13567   const unsigned char *z;
   13568   int eType;
   13569   memset(p, 0, sizeof(*p));
   13570   if( argc==0 ){
   13571     setDateTimeToCurrent(context, p);
   13572   }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
   13573                    || eType==SQLITE_INTEGER ){
   13574     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
   13575     p->validJD = 1;
   13576   }else{
   13577     z = sqlite3_value_text(argv[0]);
   13578     if( !z || parseDateOrTime(context, (char*)z, p) ){
   13579       return 1;
   13580     }
   13581   }
   13582   for(i=1; i<argc; i++){
   13583     if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
   13584       return 1;
   13585     }
   13586   }
   13587   return 0;
   13588 }
   13589 
   13590 
   13591 /*
   13592 ** The following routines implement the various date and time functions
   13593 ** of SQLite.
   13594 */
   13595 
   13596 /*
   13597 **    julianday( TIMESTRING, MOD, MOD, ...)
   13598 **
   13599 ** Return the julian day number of the date specified in the arguments
   13600 */
   13601 static void juliandayFunc(
   13602   sqlite3_context *context,
   13603   int argc,
   13604   sqlite3_value **argv
   13605 ){
   13606   DateTime x;
   13607   if( isDate(context, argc, argv, &x)==0 ){
   13608     computeJD(&x);
   13609     sqlite3_result_double(context, x.iJD/86400000.0);
   13610   }
   13611 }
   13612 
   13613 /*
   13614 **    datetime( TIMESTRING, MOD, MOD, ...)
   13615 **
   13616 ** Return YYYY-MM-DD HH:MM:SS
   13617 */
   13618 static void datetimeFunc(
   13619   sqlite3_context *context,
   13620   int argc,
   13621   sqlite3_value **argv
   13622 ){
   13623   DateTime x;
   13624   if( isDate(context, argc, argv, &x)==0 ){
   13625     char zBuf[100];
   13626     computeYMD_HMS(&x);
   13627     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
   13628                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
   13629     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   13630   }
   13631 }
   13632 
   13633 /*
   13634 **    time( TIMESTRING, MOD, MOD, ...)
   13635 **
   13636 ** Return HH:MM:SS
   13637 */
   13638 static void timeFunc(
   13639   sqlite3_context *context,
   13640   int argc,
   13641   sqlite3_value **argv
   13642 ){
   13643   DateTime x;
   13644   if( isDate(context, argc, argv, &x)==0 ){
   13645     char zBuf[100];
   13646     computeHMS(&x);
   13647     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
   13648     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   13649   }
   13650 }
   13651 
   13652 /*
   13653 **    date( TIMESTRING, MOD, MOD, ...)
   13654 **
   13655 ** Return YYYY-MM-DD
   13656 */
   13657 static void dateFunc(
   13658   sqlite3_context *context,
   13659   int argc,
   13660   sqlite3_value **argv
   13661 ){
   13662   DateTime x;
   13663   if( isDate(context, argc, argv, &x)==0 ){
   13664     char zBuf[100];
   13665     computeYMD(&x);
   13666     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
   13667     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   13668   }
   13669 }
   13670 
   13671 /*
   13672 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
   13673 **
   13674 ** Return a string described by FORMAT.  Conversions as follows:
   13675 **
   13676 **   %d  day of month
   13677 **   %f  ** fractional seconds  SS.SSS
   13678 **   %H  hour 00-24
   13679 **   %j  day of year 000-366
   13680 **   %J  ** Julian day number
   13681 **   %m  month 01-12
   13682 **   %M  minute 00-59
   13683 **   %s  seconds since 1970-01-01
   13684 **   %S  seconds 00-59
   13685 **   %w  day of week 0-6  sunday==0
   13686 **   %W  week of year 00-53
   13687 **   %Y  year 0000-9999
   13688 **   %%  %
   13689 */
   13690 static void strftimeFunc(
   13691   sqlite3_context *context,
   13692   int argc,
   13693   sqlite3_value **argv
   13694 ){
   13695   DateTime x;
   13696   u64 n;
   13697   size_t i,j;
   13698   char *z;
   13699   sqlite3 *db;
   13700   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
   13701   char zBuf[100];
   13702   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
   13703   db = sqlite3_context_db_handle(context);
   13704   for(i=0, n=1; zFmt[i]; i++, n++){
   13705     if( zFmt[i]=='%' ){
   13706       switch( zFmt[i+1] ){
   13707         case 'd':
   13708         case 'H':
   13709         case 'm':
   13710         case 'M':
   13711         case 'S':
   13712         case 'W':
   13713           n++;
   13714           /* fall thru */
   13715         case 'w':
   13716         case '%':
   13717           break;
   13718         case 'f':
   13719           n += 8;
   13720           break;
   13721         case 'j':
   13722           n += 3;
   13723           break;
   13724         case 'Y':
   13725           n += 8;
   13726           break;
   13727         case 's':
   13728         case 'J':
   13729           n += 50;
   13730           break;
   13731         default:
   13732           return;  /* ERROR.  return a NULL */
   13733       }
   13734       i++;
   13735     }
   13736   }
   13737   testcase( n==sizeof(zBuf)-1 );
   13738   testcase( n==sizeof(zBuf) );
   13739   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   13740   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
   13741   if( n<sizeof(zBuf) ){
   13742     z = zBuf;
   13743   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
   13744     sqlite3_result_error_toobig(context);
   13745     return;
   13746   }else{
   13747     z = sqlite3DbMallocRaw(db, (int)n);
   13748     if( z==0 ){
   13749       sqlite3_result_error_nomem(context);
   13750       return;
   13751     }
   13752   }
   13753   computeJD(&x);
   13754   computeYMD_HMS(&x);
   13755   for(i=j=0; zFmt[i]; i++){
   13756     if( zFmt[i]!='%' ){
   13757       z[j++] = zFmt[i];
   13758     }else{
   13759       i++;
   13760       switch( zFmt[i] ){
   13761         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
   13762         case 'f': {
   13763           double s = x.s;
   13764           if( s>59.999 ) s = 59.999;
   13765           sqlite3_snprintf(7, &z[j],"%06.3f", s);
   13766           j += sqlite3Strlen30(&z[j]);
   13767           break;
   13768         }
   13769         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
   13770         case 'W': /* Fall thru */
   13771         case 'j': {
   13772           int nDay;             /* Number of days since 1st day of year */
   13773           DateTime y = x;
   13774           y.validJD = 0;
   13775           y.M = 1;
   13776           y.D = 1;
   13777           computeJD(&y);
   13778           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
   13779           if( zFmt[i]=='W' ){
   13780             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
   13781             wd = (int)(((x.iJD+43200000)/86400000)%7);
   13782             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
   13783             j += 2;
   13784           }else{
   13785             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
   13786             j += 3;
   13787           }
   13788           break;
   13789         }
   13790         case 'J': {
   13791           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
   13792           j+=sqlite3Strlen30(&z[j]);
   13793           break;
   13794         }
   13795         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
   13796         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
   13797         case 's': {
   13798           sqlite3_snprintf(30,&z[j],"%lld",
   13799                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
   13800           j += sqlite3Strlen30(&z[j]);
   13801           break;
   13802         }
   13803         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
   13804         case 'w': {
   13805           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
   13806           break;
   13807         }
   13808         case 'Y': {
   13809           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
   13810           break;
   13811         }
   13812         default:   z[j++] = '%'; break;
   13813       }
   13814     }
   13815   }
   13816   z[j] = 0;
   13817   sqlite3_result_text(context, z, -1,
   13818                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
   13819 }
   13820 
   13821 /*
   13822 ** current_time()
   13823 **
   13824 ** This function returns the same value as time('now').
   13825 */
   13826 static void ctimeFunc(
   13827   sqlite3_context *context,
   13828   int NotUsed,
   13829   sqlite3_value **NotUsed2
   13830 ){
   13831   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   13832   timeFunc(context, 0, 0);
   13833 }
   13834 
   13835 /*
   13836 ** current_date()
   13837 **
   13838 ** This function returns the same value as date('now').
   13839 */
   13840 static void cdateFunc(
   13841   sqlite3_context *context,
   13842   int NotUsed,
   13843   sqlite3_value **NotUsed2
   13844 ){
   13845   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   13846   dateFunc(context, 0, 0);
   13847 }
   13848 
   13849 /*
   13850 ** current_timestamp()
   13851 **
   13852 ** This function returns the same value as datetime('now').
   13853 */
   13854 static void ctimestampFunc(
   13855   sqlite3_context *context,
   13856   int NotUsed,
   13857   sqlite3_value **NotUsed2
   13858 ){
   13859   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   13860   datetimeFunc(context, 0, 0);
   13861 }
   13862 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
   13863 
   13864 #ifdef SQLITE_OMIT_DATETIME_FUNCS
   13865 /*
   13866 ** If the library is compiled to omit the full-scale date and time
   13867 ** handling (to get a smaller binary), the following minimal version
   13868 ** of the functions current_time(), current_date() and current_timestamp()
   13869 ** are included instead. This is to support column declarations that
   13870 ** include "DEFAULT CURRENT_TIME" etc.
   13871 **
   13872 ** This function uses the C-library functions time(), gmtime()
   13873 ** and strftime(). The format string to pass to strftime() is supplied
   13874 ** as the user-data for the function.
   13875 */
   13876 static void currentTimeFunc(
   13877   sqlite3_context *context,
   13878   int argc,
   13879   sqlite3_value **argv
   13880 ){
   13881   time_t t;
   13882   char *zFormat = (char *)sqlite3_user_data(context);
   13883   sqlite3 *db;
   13884   sqlite3_int64 iT;
   13885   char zBuf[20];
   13886 
   13887   UNUSED_PARAMETER(argc);
   13888   UNUSED_PARAMETER(argv);
   13889 
   13890   db = sqlite3_context_db_handle(context);
   13891   sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
   13892   t = iT/1000 - 10000*(sqlite3_int64)21086676;
   13893 #ifdef HAVE_GMTIME_R
   13894   {
   13895     struct tm sNow;
   13896     gmtime_r(&t, &sNow);
   13897     strftime(zBuf, 20, zFormat, &sNow);
   13898   }
   13899 #else
   13900   {
   13901     struct tm *pTm;
   13902     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   13903     pTm = gmtime(&t);
   13904     strftime(zBuf, 20, zFormat, pTm);
   13905     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   13906   }
   13907 #endif
   13908 
   13909   sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   13910 }
   13911 #endif
   13912 
   13913 /*
   13914 ** This function registered all of the above C functions as SQL
   13915 ** functions.  This should be the only routine in this file with
   13916 ** external linkage.
   13917 */
   13918 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
   13919   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
   13920 #ifndef SQLITE_OMIT_DATETIME_FUNCS
   13921     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
   13922     FUNCTION(date,             -1, 0, 0, dateFunc      ),
   13923     FUNCTION(time,             -1, 0, 0, timeFunc      ),
   13924     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
   13925     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
   13926     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
   13927     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
   13928     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
   13929 #else
   13930     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
   13931     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
   13932     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
   13933 #endif
   13934   };
   13935   int i;
   13936   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   13937   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
   13938 
   13939   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
   13940     sqlite3FuncDefInsert(pHash, &aFunc[i]);
   13941   }
   13942 }
   13943 
   13944 /************** End of date.c ************************************************/
   13945 /************** Begin file os.c **********************************************/
   13946 /*
   13947 ** 2005 November 29
   13948 **
   13949 ** The author disclaims copyright to this source code.  In place of
   13950 ** a legal notice, here is a blessing:
   13951 **
   13952 **    May you do good and not evil.
   13953 **    May you find forgiveness for yourself and forgive others.
   13954 **    May you share freely, never taking more than you give.
   13955 **
   13956 ******************************************************************************
   13957 **
   13958 ** This file contains OS interface code that is common to all
   13959 ** architectures.
   13960 */
   13961 #define _SQLITE_OS_C_ 1
   13962 #undef _SQLITE_OS_C_
   13963 
   13964 /*
   13965 ** The default SQLite sqlite3_vfs implementations do not allocate
   13966 ** memory (actually, os_unix.c allocates a small amount of memory
   13967 ** from within OsOpen()), but some third-party implementations may.
   13968 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
   13969 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
   13970 **
   13971 ** The following functions are instrumented for malloc() failure
   13972 ** testing:
   13973 **
   13974 **     sqlite3OsOpen()
   13975 **     sqlite3OsRead()
   13976 **     sqlite3OsWrite()
   13977 **     sqlite3OsSync()
   13978 **     sqlite3OsLock()
   13979 **
   13980 */
   13981 #if defined(SQLITE_TEST)
   13982 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
   13983   #define DO_OS_MALLOC_TEST(x)                                       \
   13984   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
   13985     void *pTstAlloc = sqlite3Malloc(10);                             \
   13986     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
   13987     sqlite3_free(pTstAlloc);                                         \
   13988   }
   13989 #else
   13990   #define DO_OS_MALLOC_TEST(x)
   13991 #endif
   13992 
   13993 /*
   13994 ** The following routines are convenience wrappers around methods
   13995 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
   13996 ** of this would be completely automatic if SQLite were coded using
   13997 ** C++ instead of plain old C.
   13998 */
   13999 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
   14000   int rc = SQLITE_OK;
   14001   if( pId->pMethods ){
   14002     rc = pId->pMethods->xClose(pId);
   14003     pId->pMethods = 0;
   14004   }
   14005   return rc;
   14006 }
   14007 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
   14008   DO_OS_MALLOC_TEST(id);
   14009   return id->pMethods->xRead(id, pBuf, amt, offset);
   14010 }
   14011 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
   14012   DO_OS_MALLOC_TEST(id);
   14013   return id->pMethods->xWrite(id, pBuf, amt, offset);
   14014 }
   14015 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
   14016   return id->pMethods->xTruncate(id, size);
   14017 }
   14018 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
   14019   DO_OS_MALLOC_TEST(id);
   14020   return id->pMethods->xSync(id, flags);
   14021 }
   14022 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
   14023   DO_OS_MALLOC_TEST(id);
   14024   return id->pMethods->xFileSize(id, pSize);
   14025 }
   14026 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
   14027   DO_OS_MALLOC_TEST(id);
   14028   return id->pMethods->xLock(id, lockType);
   14029 }
   14030 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
   14031   return id->pMethods->xUnlock(id, lockType);
   14032 }
   14033 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
   14034   DO_OS_MALLOC_TEST(id);
   14035   return id->pMethods->xCheckReservedLock(id, pResOut);
   14036 }
   14037 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
   14038   return id->pMethods->xFileControl(id, op, pArg);
   14039 }
   14040 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
   14041   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
   14042   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
   14043 }
   14044 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
   14045   return id->pMethods->xDeviceCharacteristics(id);
   14046 }
   14047 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
   14048   return id->pMethods->xShmLock(id, offset, n, flags);
   14049 }
   14050 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
   14051   id->pMethods->xShmBarrier(id);
   14052 }
   14053 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
   14054   return id->pMethods->xShmUnmap(id, deleteFlag);
   14055 }
   14056 SQLITE_PRIVATE int sqlite3OsShmMap(
   14057   sqlite3_file *id,               /* Database file handle */
   14058   int iPage,
   14059   int pgsz,
   14060   int bExtend,                    /* True to extend file if necessary */
   14061   void volatile **pp              /* OUT: Pointer to mapping */
   14062 ){
   14063   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
   14064 }
   14065 
   14066 /*
   14067 ** The next group of routines are convenience wrappers around the
   14068 ** VFS methods.
   14069 */
   14070 SQLITE_PRIVATE int sqlite3OsOpen(
   14071   sqlite3_vfs *pVfs,
   14072   const char *zPath,
   14073   sqlite3_file *pFile,
   14074   int flags,
   14075   int *pFlagsOut
   14076 ){
   14077   int rc;
   14078   DO_OS_MALLOC_TEST(0);
   14079   /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
   14080   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
   14081   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
   14082   ** reaching the VFS. */
   14083   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut);
   14084   assert( rc==SQLITE_OK || pFile->pMethods==0 );
   14085   return rc;
   14086 }
   14087 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
   14088   return pVfs->xDelete(pVfs, zPath, dirSync);
   14089 }
   14090 SQLITE_PRIVATE int sqlite3OsAccess(
   14091   sqlite3_vfs *pVfs,
   14092   const char *zPath,
   14093   int flags,
   14094   int *pResOut
   14095 ){
   14096   DO_OS_MALLOC_TEST(0);
   14097   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
   14098 }
   14099 SQLITE_PRIVATE int sqlite3OsFullPathname(
   14100   sqlite3_vfs *pVfs,
   14101   const char *zPath,
   14102   int nPathOut,
   14103   char *zPathOut
   14104 ){
   14105   zPathOut[0] = 0;
   14106   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
   14107 }
   14108 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   14109 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
   14110   return pVfs->xDlOpen(pVfs, zPath);
   14111 }
   14112 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   14113   pVfs->xDlError(pVfs, nByte, zBufOut);
   14114 }
   14115 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
   14116   return pVfs->xDlSym(pVfs, pHdle, zSym);
   14117 }
   14118 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
   14119   pVfs->xDlClose(pVfs, pHandle);
   14120 }
   14121 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   14122 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   14123   return pVfs->xRandomness(pVfs, nByte, zBufOut);
   14124 }
   14125 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
   14126   return pVfs->xSleep(pVfs, nMicro);
   14127 }
   14128 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
   14129   int rc;
   14130   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
   14131   ** method to get the current date and time if that method is available
   14132   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
   14133   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
   14134   ** unavailable.
   14135   */
   14136   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
   14137     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
   14138   }else{
   14139     double r;
   14140     rc = pVfs->xCurrentTime(pVfs, &r);
   14141     *pTimeOut = (sqlite3_int64)(r*86400000.0);
   14142   }
   14143   return rc;
   14144 }
   14145 
   14146 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
   14147   sqlite3_vfs *pVfs,
   14148   const char *zFile,
   14149   sqlite3_file **ppFile,
   14150   int flags,
   14151   int *pOutFlags
   14152 ){
   14153   int rc = SQLITE_NOMEM;
   14154   sqlite3_file *pFile;
   14155   pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
   14156   if( pFile ){
   14157     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
   14158     if( rc!=SQLITE_OK ){
   14159       sqlite3_free(pFile);
   14160     }else{
   14161       *ppFile = pFile;
   14162     }
   14163   }
   14164   return rc;
   14165 }
   14166 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
   14167   int rc = SQLITE_OK;
   14168   assert( pFile );
   14169   rc = sqlite3OsClose(pFile);
   14170   sqlite3_free(pFile);
   14171   return rc;
   14172 }
   14173 
   14174 /*
   14175 ** This function is a wrapper around the OS specific implementation of
   14176 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
   14177 ** ability to simulate a malloc failure, so that the handling of an
   14178 ** error in sqlite3_os_init() by the upper layers can be tested.
   14179 */
   14180 SQLITE_PRIVATE int sqlite3OsInit(void){
   14181   void *p = sqlite3_malloc(10);
   14182   if( p==0 ) return SQLITE_NOMEM;
   14183   sqlite3_free(p);
   14184   return sqlite3_os_init();
   14185 }
   14186 
   14187 /*
   14188 ** The list of all registered VFS implementations.
   14189 */
   14190 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
   14191 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
   14192 
   14193 /*
   14194 ** Locate a VFS by name.  If no name is given, simply return the
   14195 ** first VFS on the list.
   14196 */
   14197 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
   14198   sqlite3_vfs *pVfs = 0;
   14199 #if SQLITE_THREADSAFE
   14200   sqlite3_mutex *mutex;
   14201 #endif
   14202 #ifndef SQLITE_OMIT_AUTOINIT
   14203   int rc = sqlite3_initialize();
   14204   if( rc ) return 0;
   14205 #endif
   14206 #if SQLITE_THREADSAFE
   14207   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   14208 #endif
   14209   sqlite3_mutex_enter(mutex);
   14210   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
   14211     if( zVfs==0 ) break;
   14212     if( strcmp(zVfs, pVfs->zName)==0 ) break;
   14213   }
   14214   sqlite3_mutex_leave(mutex);
   14215   return pVfs;
   14216 }
   14217 
   14218 /*
   14219 ** Unlink a VFS from the linked list
   14220 */
   14221 static void vfsUnlink(sqlite3_vfs *pVfs){
   14222   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
   14223   if( pVfs==0 ){
   14224     /* No-op */
   14225   }else if( vfsList==pVfs ){
   14226     vfsList = pVfs->pNext;
   14227   }else if( vfsList ){
   14228     sqlite3_vfs *p = vfsList;
   14229     while( p->pNext && p->pNext!=pVfs ){
   14230       p = p->pNext;
   14231     }
   14232     if( p->pNext==pVfs ){
   14233       p->pNext = pVfs->pNext;
   14234     }
   14235   }
   14236 }
   14237 
   14238 /*
   14239 ** Register a VFS with the system.  It is harmless to register the same
   14240 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
   14241 ** true.
   14242 */
   14243 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
   14244   sqlite3_mutex *mutex = 0;
   14245 #ifndef SQLITE_OMIT_AUTOINIT
   14246   int rc = sqlite3_initialize();
   14247   if( rc ) return rc;
   14248 #endif
   14249   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   14250   sqlite3_mutex_enter(mutex);
   14251   vfsUnlink(pVfs);
   14252   if( makeDflt || vfsList==0 ){
   14253     pVfs->pNext = vfsList;
   14254     vfsList = pVfs;
   14255   }else{
   14256     pVfs->pNext = vfsList->pNext;
   14257     vfsList->pNext = pVfs;
   14258   }
   14259   assert(vfsList);
   14260   sqlite3_mutex_leave(mutex);
   14261   return SQLITE_OK;
   14262 }
   14263 
   14264 /*
   14265 ** Unregister a VFS so that it is no longer accessible.
   14266 */
   14267 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
   14268 #if SQLITE_THREADSAFE
   14269   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   14270 #endif
   14271   sqlite3_mutex_enter(mutex);
   14272   vfsUnlink(pVfs);
   14273   sqlite3_mutex_leave(mutex);
   14274   return SQLITE_OK;
   14275 }
   14276 
   14277 /************** End of os.c **************************************************/
   14278 /************** Begin file fault.c *******************************************/
   14279 /*
   14280 ** 2008 Jan 22
   14281 **
   14282 ** The author disclaims copyright to this source code.  In place of
   14283 ** a legal notice, here is a blessing:
   14284 **
   14285 **    May you do good and not evil.
   14286 **    May you find forgiveness for yourself and forgive others.
   14287 **    May you share freely, never taking more than you give.
   14288 **
   14289 *************************************************************************
   14290 **
   14291 ** This file contains code to support the concept of "benign"
   14292 ** malloc failures (when the xMalloc() or xRealloc() method of the
   14293 ** sqlite3_mem_methods structure fails to allocate a block of memory
   14294 ** and returns 0).
   14295 **
   14296 ** Most malloc failures are non-benign. After they occur, SQLite
   14297 ** abandons the current operation and returns an error code (usually
   14298 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
   14299 ** fatal. For example, if a malloc fails while resizing a hash table, this
   14300 ** is completely recoverable simply by not carrying out the resize. The
   14301 ** hash table will continue to function normally.  So a malloc failure
   14302 ** during a hash table resize is a benign fault.
   14303 */
   14304 
   14305 
   14306 #ifndef SQLITE_OMIT_BUILTIN_TEST
   14307 
   14308 /*
   14309 ** Global variables.
   14310 */
   14311 typedef struct BenignMallocHooks BenignMallocHooks;
   14312 static SQLITE_WSD struct BenignMallocHooks {
   14313   void (*xBenignBegin)(void);
   14314   void (*xBenignEnd)(void);
   14315 } sqlite3Hooks = { 0, 0 };
   14316 
   14317 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
   14318 ** structure.  If writable static data is unsupported on the target,
   14319 ** we have to locate the state vector at run-time.  In the more common
   14320 ** case where writable static data is supported, wsdHooks can refer directly
   14321 ** to the "sqlite3Hooks" state vector declared above.
   14322 */
   14323 #ifdef SQLITE_OMIT_WSD
   14324 # define wsdHooksInit \
   14325   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
   14326 # define wsdHooks x[0]
   14327 #else
   14328 # define wsdHooksInit
   14329 # define wsdHooks sqlite3Hooks
   14330 #endif
   14331 
   14332 
   14333 /*
   14334 ** Register hooks to call when sqlite3BeginBenignMalloc() and
   14335 ** sqlite3EndBenignMalloc() are called, respectively.
   14336 */
   14337 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
   14338   void (*xBenignBegin)(void),
   14339   void (*xBenignEnd)(void)
   14340 ){
   14341   wsdHooksInit;
   14342   wsdHooks.xBenignBegin = xBenignBegin;
   14343   wsdHooks.xBenignEnd = xBenignEnd;
   14344 }
   14345 
   14346 /*
   14347 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
   14348 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
   14349 ** indicates that subsequent malloc failures are non-benign.
   14350 */
   14351 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
   14352   wsdHooksInit;
   14353   if( wsdHooks.xBenignBegin ){
   14354     wsdHooks.xBenignBegin();
   14355   }
   14356 }
   14357 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
   14358   wsdHooksInit;
   14359   if( wsdHooks.xBenignEnd ){
   14360     wsdHooks.xBenignEnd();
   14361   }
   14362 }
   14363 
   14364 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
   14365 
   14366 /************** End of fault.c ***********************************************/
   14367 /************** Begin file mem0.c ********************************************/
   14368 /*
   14369 ** 2008 October 28
   14370 **
   14371 ** The author disclaims copyright to this source code.  In place of
   14372 ** a legal notice, here is a blessing:
   14373 **
   14374 **    May you do good and not evil.
   14375 **    May you find forgiveness for yourself and forgive others.
   14376 **    May you share freely, never taking more than you give.
   14377 **
   14378 *************************************************************************
   14379 **
   14380 ** This file contains a no-op memory allocation drivers for use when
   14381 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
   14382 ** here always fail.  SQLite will not operate with these drivers.  These
   14383 ** are merely placeholders.  Real drivers must be substituted using
   14384 ** sqlite3_config() before SQLite will operate.
   14385 */
   14386 
   14387 /*
   14388 ** This version of the memory allocator is the default.  It is
   14389 ** used when no other memory allocator is specified using compile-time
   14390 ** macros.
   14391 */
   14392 #ifdef SQLITE_ZERO_MALLOC
   14393 
   14394 /*
   14395 ** No-op versions of all memory allocation routines
   14396 */
   14397 static void *sqlite3MemMalloc(int nByte){ return 0; }
   14398 static void sqlite3MemFree(void *pPrior){ return; }
   14399 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
   14400 static int sqlite3MemSize(void *pPrior){ return 0; }
   14401 static int sqlite3MemRoundup(int n){ return n; }
   14402 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
   14403 static void sqlite3MemShutdown(void *NotUsed){ return; }
   14404 
   14405 /*
   14406 ** This routine is the only routine in this file with external linkage.
   14407 **
   14408 ** Populate the low-level memory allocation function pointers in
   14409 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   14410 */
   14411 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   14412   static const sqlite3_mem_methods defaultMethods = {
   14413      sqlite3MemMalloc,
   14414      sqlite3MemFree,
   14415      sqlite3MemRealloc,
   14416      sqlite3MemSize,
   14417      sqlite3MemRoundup,
   14418      sqlite3MemInit,
   14419      sqlite3MemShutdown,
   14420      0
   14421   };
   14422   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   14423 }
   14424 
   14425 #endif /* SQLITE_ZERO_MALLOC */
   14426 
   14427 /************** End of mem0.c ************************************************/
   14428 /************** Begin file mem1.c ********************************************/
   14429 /*
   14430 ** 2007 August 14
   14431 **
   14432 ** The author disclaims copyright to this source code.  In place of
   14433 ** a legal notice, here is a blessing:
   14434 **
   14435 **    May you do good and not evil.
   14436 **    May you find forgiveness for yourself and forgive others.
   14437 **    May you share freely, never taking more than you give.
   14438 **
   14439 *************************************************************************
   14440 **
   14441 ** This file contains low-level memory allocation drivers for when
   14442 ** SQLite will use the standard C-library malloc/realloc/free interface
   14443 ** to obtain the memory it needs.
   14444 **
   14445 ** This file contains implementations of the low-level memory allocation
   14446 ** routines specified in the sqlite3_mem_methods object.
   14447 */
   14448 
   14449 /*
   14450 ** This version of the memory allocator is the default.  It is
   14451 ** used when no other memory allocator is specified using compile-time
   14452 ** macros.
   14453 */
   14454 #ifdef SQLITE_SYSTEM_MALLOC
   14455 
   14456 /*
   14457 ** Like malloc(), but remember the size of the allocation
   14458 ** so that we can find it later using sqlite3MemSize().
   14459 **
   14460 ** For this low-level routine, we are guaranteed that nByte>0 because
   14461 ** cases of nByte<=0 will be intercepted and dealt with by higher level
   14462 ** routines.
   14463 */
   14464 static void *sqlite3MemMalloc(int nByte){
   14465   sqlite3_int64 *p;
   14466   assert( nByte>0 );
   14467   nByte = ROUND8(nByte);
   14468   p = malloc( nByte+8 );
   14469   if( p ){
   14470     p[0] = nByte;
   14471     p++;
   14472   }else{
   14473     testcase( sqlite3GlobalConfig.xLog!=0 );
   14474     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   14475   }
   14476   return (void *)p;
   14477 }
   14478 
   14479 /*
   14480 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
   14481 ** or sqlite3MemRealloc().
   14482 **
   14483 ** For this low-level routine, we already know that pPrior!=0 since
   14484 ** cases where pPrior==0 will have been intecepted and dealt with
   14485 ** by higher-level routines.
   14486 */
   14487 static void sqlite3MemFree(void *pPrior){
   14488   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
   14489   assert( pPrior!=0 );
   14490   p--;
   14491   free(p);
   14492 }
   14493 
   14494 /*
   14495 ** Report the allocated size of a prior return from xMalloc()
   14496 ** or xRealloc().
   14497 */
   14498 static int sqlite3MemSize(void *pPrior){
   14499   sqlite3_int64 *p;
   14500   if( pPrior==0 ) return 0;
   14501   p = (sqlite3_int64*)pPrior;
   14502   p--;
   14503   return (int)p[0];
   14504 }
   14505 
   14506 /*
   14507 ** Like realloc().  Resize an allocation previously obtained from
   14508 ** sqlite3MemMalloc().
   14509 **
   14510 ** For this low-level interface, we know that pPrior!=0.  Cases where
   14511 ** pPrior==0 while have been intercepted by higher-level routine and
   14512 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
   14513 ** cases where nByte<=0 will have been intercepted by higher-level
   14514 ** routines and redirected to xFree.
   14515 */
   14516 static void *sqlite3MemRealloc(void *pPrior, int nByte){
   14517   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
   14518   assert( pPrior!=0 && nByte>0 );
   14519   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
   14520   p--;
   14521   p = realloc(p, nByte+8 );
   14522   if( p ){
   14523     p[0] = nByte;
   14524     p++;
   14525   }else{
   14526     testcase( sqlite3GlobalConfig.xLog!=0 );
   14527     sqlite3_log(SQLITE_NOMEM,
   14528       "failed memory resize %u to %u bytes",
   14529       sqlite3MemSize(pPrior), nByte);
   14530   }
   14531   return (void*)p;
   14532 }
   14533 
   14534 /*
   14535 ** Round up a request size to the next valid allocation size.
   14536 */
   14537 static int sqlite3MemRoundup(int n){
   14538   return ROUND8(n);
   14539 }
   14540 
   14541 /*
   14542 ** Initialize this module.
   14543 */
   14544 static int sqlite3MemInit(void *NotUsed){
   14545   UNUSED_PARAMETER(NotUsed);
   14546   return SQLITE_OK;
   14547 }
   14548 
   14549 /*
   14550 ** Deinitialize this module.
   14551 */
   14552 static void sqlite3MemShutdown(void *NotUsed){
   14553   UNUSED_PARAMETER(NotUsed);
   14554   return;
   14555 }
   14556 
   14557 /*
   14558 ** This routine is the only routine in this file with external linkage.
   14559 **
   14560 ** Populate the low-level memory allocation function pointers in
   14561 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   14562 */
   14563 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   14564   static const sqlite3_mem_methods defaultMethods = {
   14565      sqlite3MemMalloc,
   14566      sqlite3MemFree,
   14567      sqlite3MemRealloc,
   14568      sqlite3MemSize,
   14569      sqlite3MemRoundup,
   14570      sqlite3MemInit,
   14571      sqlite3MemShutdown,
   14572      0
   14573   };
   14574   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   14575 }
   14576 
   14577 #endif /* SQLITE_SYSTEM_MALLOC */
   14578 
   14579 /************** End of mem1.c ************************************************/
   14580 /************** Begin file mem2.c ********************************************/
   14581 /*
   14582 ** 2007 August 15
   14583 **
   14584 ** The author disclaims copyright to this source code.  In place of
   14585 ** a legal notice, here is a blessing:
   14586 **
   14587 **    May you do good and not evil.
   14588 **    May you find forgiveness for yourself and forgive others.
   14589 **    May you share freely, never taking more than you give.
   14590 **
   14591 *************************************************************************
   14592 **
   14593 ** This file contains low-level memory allocation drivers for when
   14594 ** SQLite will use the standard C-library malloc/realloc/free interface
   14595 ** to obtain the memory it needs while adding lots of additional debugging
   14596 ** information to each allocation in order to help detect and fix memory
   14597 ** leaks and memory usage errors.
   14598 **
   14599 ** This file contains implementations of the low-level memory allocation
   14600 ** routines specified in the sqlite3_mem_methods object.
   14601 */
   14602 
   14603 /*
   14604 ** This version of the memory allocator is used only if the
   14605 ** SQLITE_MEMDEBUG macro is defined
   14606 */
   14607 #ifdef SQLITE_MEMDEBUG
   14608 
   14609 /*
   14610 ** The backtrace functionality is only available with GLIBC
   14611 */
   14612 #ifdef __GLIBC__
   14613   extern int backtrace(void**,int);
   14614   extern void backtrace_symbols_fd(void*const*,int,int);
   14615 #else
   14616 # define backtrace(A,B) 1
   14617 # define backtrace_symbols_fd(A,B,C)
   14618 #endif
   14619 
   14620 /*
   14621 ** Each memory allocation looks like this:
   14622 **
   14623 **  ------------------------------------------------------------------------
   14624 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
   14625 **  ------------------------------------------------------------------------
   14626 **
   14627 ** The application code sees only a pointer to the allocation.  We have
   14628 ** to back up from the allocation pointer to find the MemBlockHdr.  The
   14629 ** MemBlockHdr tells us the size of the allocation and the number of
   14630 ** backtrace pointers.  There is also a guard word at the end of the
   14631 ** MemBlockHdr.
   14632 */
   14633 struct MemBlockHdr {
   14634   i64 iSize;                          /* Size of this allocation */
   14635   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
   14636   char nBacktrace;                    /* Number of backtraces on this alloc */
   14637   char nBacktraceSlots;               /* Available backtrace slots */
   14638   u8 nTitle;                          /* Bytes of title; includes '\0' */
   14639   u8 eType;                           /* Allocation type code */
   14640   int iForeGuard;                     /* Guard word for sanity */
   14641 };
   14642 
   14643 /*
   14644 ** Guard words
   14645 */
   14646 #define FOREGUARD 0x80F5E153
   14647 #define REARGUARD 0xE4676B53
   14648 
   14649 /*
   14650 ** Number of malloc size increments to track.
   14651 */
   14652 #define NCSIZE  1000
   14653 
   14654 /*
   14655 ** All of the static variables used by this module are collected
   14656 ** into a single structure named "mem".  This is to keep the
   14657 ** static variables organized and to reduce namespace pollution
   14658 ** when this module is combined with other in the amalgamation.
   14659 */
   14660 static struct {
   14661 
   14662   /*
   14663   ** Mutex to control access to the memory allocation subsystem.
   14664   */
   14665   sqlite3_mutex *mutex;
   14666 
   14667   /*
   14668   ** Head and tail of a linked list of all outstanding allocations
   14669   */
   14670   struct MemBlockHdr *pFirst;
   14671   struct MemBlockHdr *pLast;
   14672 
   14673   /*
   14674   ** The number of levels of backtrace to save in new allocations.
   14675   */
   14676   int nBacktrace;
   14677   void (*xBacktrace)(int, int, void **);
   14678 
   14679   /*
   14680   ** Title text to insert in front of each block
   14681   */
   14682   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
   14683   char zTitle[100];  /* The title text */
   14684 
   14685   /*
   14686   ** sqlite3MallocDisallow() increments the following counter.
   14687   ** sqlite3MallocAllow() decrements it.
   14688   */
   14689   int disallow; /* Do not allow memory allocation */
   14690 
   14691   /*
   14692   ** Gather statistics on the sizes of memory allocations.
   14693   ** nAlloc[i] is the number of allocation attempts of i*8
   14694   ** bytes.  i==NCSIZE is the number of allocation attempts for
   14695   ** sizes more than NCSIZE*8 bytes.
   14696   */
   14697   int nAlloc[NCSIZE];      /* Total number of allocations */
   14698   int nCurrent[NCSIZE];    /* Current number of allocations */
   14699   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
   14700 
   14701 } mem;
   14702 
   14703 
   14704 /*
   14705 ** Adjust memory usage statistics
   14706 */
   14707 static void adjustStats(int iSize, int increment){
   14708   int i = ROUND8(iSize)/8;
   14709   if( i>NCSIZE-1 ){
   14710     i = NCSIZE - 1;
   14711   }
   14712   if( increment>0 ){
   14713     mem.nAlloc[i]++;
   14714     mem.nCurrent[i]++;
   14715     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
   14716       mem.mxCurrent[i] = mem.nCurrent[i];
   14717     }
   14718   }else{
   14719     mem.nCurrent[i]--;
   14720     assert( mem.nCurrent[i]>=0 );
   14721   }
   14722 }
   14723 
   14724 /*
   14725 ** Given an allocation, find the MemBlockHdr for that allocation.
   14726 **
   14727 ** This routine checks the guards at either end of the allocation and
   14728 ** if they are incorrect it asserts.
   14729 */
   14730 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
   14731   struct MemBlockHdr *p;
   14732   int *pInt;
   14733   u8 *pU8;
   14734   int nReserve;
   14735 
   14736   p = (struct MemBlockHdr*)pAllocation;
   14737   p--;
   14738   assert( p->iForeGuard==(int)FOREGUARD );
   14739   nReserve = ROUND8(p->iSize);
   14740   pInt = (int*)pAllocation;
   14741   pU8 = (u8*)pAllocation;
   14742   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
   14743   /* This checks any of the "extra" bytes allocated due
   14744   ** to rounding up to an 8 byte boundary to ensure
   14745   ** they haven't been overwritten.
   14746   */
   14747   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
   14748   return p;
   14749 }
   14750 
   14751 /*
   14752 ** Return the number of bytes currently allocated at address p.
   14753 */
   14754 static int sqlite3MemSize(void *p){
   14755   struct MemBlockHdr *pHdr;
   14756   if( !p ){
   14757     return 0;
   14758   }
   14759   pHdr = sqlite3MemsysGetHeader(p);
   14760   return pHdr->iSize;
   14761 }
   14762 
   14763 /*
   14764 ** Initialize the memory allocation subsystem.
   14765 */
   14766 static int sqlite3MemInit(void *NotUsed){
   14767   UNUSED_PARAMETER(NotUsed);
   14768   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
   14769   if( !sqlite3GlobalConfig.bMemstat ){
   14770     /* If memory status is enabled, then the malloc.c wrapper will already
   14771     ** hold the STATIC_MEM mutex when the routines here are invoked. */
   14772     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   14773   }
   14774   return SQLITE_OK;
   14775 }
   14776 
   14777 /*
   14778 ** Deinitialize the memory allocation subsystem.
   14779 */
   14780 static void sqlite3MemShutdown(void *NotUsed){
   14781   UNUSED_PARAMETER(NotUsed);
   14782   mem.mutex = 0;
   14783 }
   14784 
   14785 /*
   14786 ** Round up a request size to the next valid allocation size.
   14787 */
   14788 static int sqlite3MemRoundup(int n){
   14789   return ROUND8(n);
   14790 }
   14791 
   14792 /*
   14793 ** Fill a buffer with pseudo-random bytes.  This is used to preset
   14794 ** the content of a new memory allocation to unpredictable values and
   14795 ** to clear the content of a freed allocation to unpredictable values.
   14796 */
   14797 static void randomFill(char *pBuf, int nByte){
   14798   unsigned int x, y, r;
   14799   x = SQLITE_PTR_TO_INT(pBuf);
   14800   y = nByte | 1;
   14801   while( nByte >= 4 ){
   14802     x = (x>>1) ^ (-(x&1) & 0xd0000001);
   14803     y = y*1103515245 + 12345;
   14804     r = x ^ y;
   14805     *(int*)pBuf = r;
   14806     pBuf += 4;
   14807     nByte -= 4;
   14808   }
   14809   while( nByte-- > 0 ){
   14810     x = (x>>1) ^ (-(x&1) & 0xd0000001);
   14811     y = y*1103515245 + 12345;
   14812     r = x ^ y;
   14813     *(pBuf++) = r & 0xff;
   14814   }
   14815 }
   14816 
   14817 /*
   14818 ** Allocate nByte bytes of memory.
   14819 */
   14820 static void *sqlite3MemMalloc(int nByte){
   14821   struct MemBlockHdr *pHdr;
   14822   void **pBt;
   14823   char *z;
   14824   int *pInt;
   14825   void *p = 0;
   14826   int totalSize;
   14827   int nReserve;
   14828   sqlite3_mutex_enter(mem.mutex);
   14829   assert( mem.disallow==0 );
   14830   nReserve = ROUND8(nByte);
   14831   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
   14832                mem.nBacktrace*sizeof(void*) + mem.nTitle;
   14833   p = malloc(totalSize);
   14834   if( p ){
   14835     z = p;
   14836     pBt = (void**)&z[mem.nTitle];
   14837     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
   14838     pHdr->pNext = 0;
   14839     pHdr->pPrev = mem.pLast;
   14840     if( mem.pLast ){
   14841       mem.pLast->pNext = pHdr;
   14842     }else{
   14843       mem.pFirst = pHdr;
   14844     }
   14845     mem.pLast = pHdr;
   14846     pHdr->iForeGuard = FOREGUARD;
   14847     pHdr->eType = MEMTYPE_HEAP;
   14848     pHdr->nBacktraceSlots = mem.nBacktrace;
   14849     pHdr->nTitle = mem.nTitle;
   14850     if( mem.nBacktrace ){
   14851       void *aAddr[40];
   14852       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
   14853       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
   14854       assert(pBt[0]);
   14855       if( mem.xBacktrace ){
   14856         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
   14857       }
   14858     }else{
   14859       pHdr->nBacktrace = 0;
   14860     }
   14861     if( mem.nTitle ){
   14862       memcpy(z, mem.zTitle, mem.nTitle);
   14863     }
   14864     pHdr->iSize = nByte;
   14865     adjustStats(nByte, +1);
   14866     pInt = (int*)&pHdr[1];
   14867     pInt[nReserve/sizeof(int)] = REARGUARD;
   14868     randomFill((char*)pInt, nByte);
   14869     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
   14870     p = (void*)pInt;
   14871   }
   14872   sqlite3_mutex_leave(mem.mutex);
   14873   return p;
   14874 }
   14875 
   14876 /*
   14877 ** Free memory.
   14878 */
   14879 static void sqlite3MemFree(void *pPrior){
   14880   struct MemBlockHdr *pHdr;
   14881   void **pBt;
   14882   char *z;
   14883   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
   14884        || mem.mutex!=0 );
   14885   pHdr = sqlite3MemsysGetHeader(pPrior);
   14886   pBt = (void**)pHdr;
   14887   pBt -= pHdr->nBacktraceSlots;
   14888   sqlite3_mutex_enter(mem.mutex);
   14889   if( pHdr->pPrev ){
   14890     assert( pHdr->pPrev->pNext==pHdr );
   14891     pHdr->pPrev->pNext = pHdr->pNext;
   14892   }else{
   14893     assert( mem.pFirst==pHdr );
   14894     mem.pFirst = pHdr->pNext;
   14895   }
   14896   if( pHdr->pNext ){
   14897     assert( pHdr->pNext->pPrev==pHdr );
   14898     pHdr->pNext->pPrev = pHdr->pPrev;
   14899   }else{
   14900     assert( mem.pLast==pHdr );
   14901     mem.pLast = pHdr->pPrev;
   14902   }
   14903   z = (char*)pBt;
   14904   z -= pHdr->nTitle;
   14905   adjustStats(pHdr->iSize, -1);
   14906   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
   14907                 pHdr->iSize + sizeof(int) + pHdr->nTitle);
   14908   free(z);
   14909   sqlite3_mutex_leave(mem.mutex);
   14910 }
   14911 
   14912 /*
   14913 ** Change the size of an existing memory allocation.
   14914 **
   14915 ** For this debugging implementation, we *always* make a copy of the
   14916 ** allocation into a new place in memory.  In this way, if the
   14917 ** higher level code is using pointer to the old allocation, it is
   14918 ** much more likely to break and we are much more liking to find
   14919 ** the error.
   14920 */
   14921 static void *sqlite3MemRealloc(void *pPrior, int nByte){
   14922   struct MemBlockHdr *pOldHdr;
   14923   void *pNew;
   14924   assert( mem.disallow==0 );
   14925   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
   14926   pOldHdr = sqlite3MemsysGetHeader(pPrior);
   14927   pNew = sqlite3MemMalloc(nByte);
   14928   if( pNew ){
   14929     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
   14930     if( nByte>pOldHdr->iSize ){
   14931       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
   14932     }
   14933     sqlite3MemFree(pPrior);
   14934   }
   14935   return pNew;
   14936 }
   14937 
   14938 /*
   14939 ** Populate the low-level memory allocation function pointers in
   14940 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   14941 */
   14942 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   14943   static const sqlite3_mem_methods defaultMethods = {
   14944      sqlite3MemMalloc,
   14945      sqlite3MemFree,
   14946      sqlite3MemRealloc,
   14947      sqlite3MemSize,
   14948      sqlite3MemRoundup,
   14949      sqlite3MemInit,
   14950      sqlite3MemShutdown,
   14951      0
   14952   };
   14953   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   14954 }
   14955 
   14956 /*
   14957 ** Set the "type" of an allocation.
   14958 */
   14959 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
   14960   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   14961     struct MemBlockHdr *pHdr;
   14962     pHdr = sqlite3MemsysGetHeader(p);
   14963     assert( pHdr->iForeGuard==FOREGUARD );
   14964     pHdr->eType = eType;
   14965   }
   14966 }
   14967 
   14968 /*
   14969 ** Return TRUE if the mask of type in eType matches the type of the
   14970 ** allocation p.  Also return true if p==NULL.
   14971 **
   14972 ** This routine is designed for use within an assert() statement, to
   14973 ** verify the type of an allocation.  For example:
   14974 **
   14975 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   14976 */
   14977 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
   14978   int rc = 1;
   14979   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   14980     struct MemBlockHdr *pHdr;
   14981     pHdr = sqlite3MemsysGetHeader(p);
   14982     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
   14983     if( (pHdr->eType&eType)==0 ){
   14984       rc = 0;
   14985     }
   14986   }
   14987   return rc;
   14988 }
   14989 
   14990 /*
   14991 ** Return TRUE if the mask of type in eType matches no bits of the type of the
   14992 ** allocation p.  Also return true if p==NULL.
   14993 **
   14994 ** This routine is designed for use within an assert() statement, to
   14995 ** verify the type of an allocation.  For example:
   14996 **
   14997 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   14998 */
   14999 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
   15000   int rc = 1;
   15001   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   15002     struct MemBlockHdr *pHdr;
   15003     pHdr = sqlite3MemsysGetHeader(p);
   15004     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
   15005     if( (pHdr->eType&eType)!=0 ){
   15006       rc = 0;
   15007     }
   15008   }
   15009   return rc;
   15010 }
   15011 
   15012 /*
   15013 ** Set the number of backtrace levels kept for each allocation.
   15014 ** A value of zero turns off backtracing.  The number is always rounded
   15015 ** up to a multiple of 2.
   15016 */
   15017 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
   15018   if( depth<0 ){ depth = 0; }
   15019   if( depth>20 ){ depth = 20; }
   15020   depth = (depth+1)&0xfe;
   15021   mem.nBacktrace = depth;
   15022 }
   15023 
   15024 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
   15025   mem.xBacktrace = xBacktrace;
   15026 }
   15027 
   15028 /*
   15029 ** Set the title string for subsequent allocations.
   15030 */
   15031 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
   15032   unsigned int n = sqlite3Strlen30(zTitle) + 1;
   15033   sqlite3_mutex_enter(mem.mutex);
   15034   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
   15035   memcpy(mem.zTitle, zTitle, n);
   15036   mem.zTitle[n] = 0;
   15037   mem.nTitle = ROUND8(n);
   15038   sqlite3_mutex_leave(mem.mutex);
   15039 }
   15040 
   15041 SQLITE_PRIVATE void sqlite3MemdebugSync(){
   15042   struct MemBlockHdr *pHdr;
   15043   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   15044     void **pBt = (void**)pHdr;
   15045     pBt -= pHdr->nBacktraceSlots;
   15046     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
   15047   }
   15048 }
   15049 
   15050 /*
   15051 ** Open the file indicated and write a log of all unfreed memory
   15052 ** allocations into that log.
   15053 */
   15054 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
   15055   FILE *out;
   15056   struct MemBlockHdr *pHdr;
   15057   void **pBt;
   15058   int i;
   15059   out = fopen(zFilename, "w");
   15060   if( out==0 ){
   15061     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   15062                     zFilename);
   15063     return;
   15064   }
   15065   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   15066     char *z = (char*)pHdr;
   15067     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
   15068     fprintf(out, "**** %lld bytes at %p from %s ****\n",
   15069             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
   15070     if( pHdr->nBacktrace ){
   15071       fflush(out);
   15072       pBt = (void**)pHdr;
   15073       pBt -= pHdr->nBacktraceSlots;
   15074       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
   15075       fprintf(out, "\n");
   15076     }
   15077   }
   15078   fprintf(out, "COUNTS:\n");
   15079   for(i=0; i<NCSIZE-1; i++){
   15080     if( mem.nAlloc[i] ){
   15081       fprintf(out, "   %5d: %10d %10d %10d\n",
   15082             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
   15083     }
   15084   }
   15085   if( mem.nAlloc[NCSIZE-1] ){
   15086     fprintf(out, "   %5d: %10d %10d %10d\n",
   15087              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
   15088              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
   15089   }
   15090   fclose(out);
   15091 }
   15092 
   15093 /*
   15094 ** Return the number of times sqlite3MemMalloc() has been called.
   15095 */
   15096 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
   15097   int i;
   15098   int nTotal = 0;
   15099   for(i=0; i<NCSIZE; i++){
   15100     nTotal += mem.nAlloc[i];
   15101   }
   15102   return nTotal;
   15103 }
   15104 
   15105 
   15106 #endif /* SQLITE_MEMDEBUG */
   15107 
   15108 /************** End of mem2.c ************************************************/
   15109 /************** Begin file mem3.c ********************************************/
   15110 /*
   15111 ** 2007 October 14
   15112 **
   15113 ** The author disclaims copyright to this source code.  In place of
   15114 ** a legal notice, here is a blessing:
   15115 **
   15116 **    May you do good and not evil.
   15117 **    May you find forgiveness for yourself and forgive others.
   15118 **    May you share freely, never taking more than you give.
   15119 **
   15120 *************************************************************************
   15121 ** This file contains the C functions that implement a memory
   15122 ** allocation subsystem for use by SQLite.
   15123 **
   15124 ** This version of the memory allocation subsystem omits all
   15125 ** use of malloc(). The SQLite user supplies a block of memory
   15126 ** before calling sqlite3_initialize() from which allocations
   15127 ** are made and returned by the xMalloc() and xRealloc()
   15128 ** implementations. Once sqlite3_initialize() has been called,
   15129 ** the amount of memory available to SQLite is fixed and cannot
   15130 ** be changed.
   15131 **
   15132 ** This version of the memory allocation subsystem is included
   15133 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
   15134 */
   15135 
   15136 /*
   15137 ** This version of the memory allocator is only built into the library
   15138 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
   15139 ** mean that the library will use a memory-pool by default, just that
   15140 ** it is available. The mempool allocator is activated by calling
   15141 ** sqlite3_config().
   15142 */
   15143 #ifdef SQLITE_ENABLE_MEMSYS3
   15144 
   15145 /*
   15146 ** Maximum size (in Mem3Blocks) of a "small" chunk.
   15147 */
   15148 #define MX_SMALL 10
   15149 
   15150 
   15151 /*
   15152 ** Number of freelist hash slots
   15153 */
   15154 #define N_HASH  61
   15155 
   15156 /*
   15157 ** A memory allocation (also called a "chunk") consists of two or
   15158 ** more blocks where each block is 8 bytes.  The first 8 bytes are
   15159 ** a header that is not returned to the user.
   15160 **
   15161 ** A chunk is two or more blocks that is either checked out or
   15162 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
   15163 ** size of the allocation in blocks if the allocation is free.
   15164 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
   15165 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
   15166 ** is true if the previous chunk is checked out and false if the
   15167 ** previous chunk is free.  The u.hdr.prevSize field is the size of
   15168 ** the previous chunk in blocks if the previous chunk is on the
   15169 ** freelist. If the previous chunk is checked out, then
   15170 ** u.hdr.prevSize can be part of the data for that chunk and should
   15171 ** not be read or written.
   15172 **
   15173 ** We often identify a chunk by its index in mem3.aPool[].  When
   15174 ** this is done, the chunk index refers to the second block of
   15175 ** the chunk.  In this way, the first chunk has an index of 1.
   15176 ** A chunk index of 0 means "no such chunk" and is the equivalent
   15177 ** of a NULL pointer.
   15178 **
   15179 ** The second block of free chunks is of the form u.list.  The
   15180 ** two fields form a double-linked list of chunks of related sizes.
   15181 ** Pointers to the head of the list are stored in mem3.aiSmall[]
   15182 ** for smaller chunks and mem3.aiHash[] for larger chunks.
   15183 **
   15184 ** The second block of a chunk is user data if the chunk is checked
   15185 ** out.  If a chunk is checked out, the user data may extend into
   15186 ** the u.hdr.prevSize value of the following chunk.
   15187 */
   15188 typedef struct Mem3Block Mem3Block;
   15189 struct Mem3Block {
   15190   union {
   15191     struct {
   15192       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
   15193       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
   15194     } hdr;
   15195     struct {
   15196       u32 next;       /* Index in mem3.aPool[] of next free chunk */
   15197       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
   15198     } list;
   15199   } u;
   15200 };
   15201 
   15202 /*
   15203 ** All of the static variables used by this module are collected
   15204 ** into a single structure named "mem3".  This is to keep the
   15205 ** static variables organized and to reduce namespace pollution
   15206 ** when this module is combined with other in the amalgamation.
   15207 */
   15208 static SQLITE_WSD struct Mem3Global {
   15209   /*
   15210   ** Memory available for allocation. nPool is the size of the array
   15211   ** (in Mem3Blocks) pointed to by aPool less 2.
   15212   */
   15213   u32 nPool;
   15214   Mem3Block *aPool;
   15215 
   15216   /*
   15217   ** True if we are evaluating an out-of-memory callback.
   15218   */
   15219   int alarmBusy;
   15220 
   15221   /*
   15222   ** Mutex to control access to the memory allocation subsystem.
   15223   */
   15224   sqlite3_mutex *mutex;
   15225 
   15226   /*
   15227   ** The minimum amount of free space that we have seen.
   15228   */
   15229   u32 mnMaster;
   15230 
   15231   /*
   15232   ** iMaster is the index of the master chunk.  Most new allocations
   15233   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
   15234   ** of the current master.  iMaster is 0 if there is not master chunk.
   15235   ** The master chunk is not in either the aiHash[] or aiSmall[].
   15236   */
   15237   u32 iMaster;
   15238   u32 szMaster;
   15239 
   15240   /*
   15241   ** Array of lists of free blocks according to the block size
   15242   ** for smaller chunks, or a hash on the block size for larger
   15243   ** chunks.
   15244   */
   15245   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
   15246   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
   15247 } mem3 = { 97535575 };
   15248 
   15249 #define mem3 GLOBAL(struct Mem3Global, mem3)
   15250 
   15251 /*
   15252 ** Unlink the chunk at mem3.aPool[i] from list it is currently
   15253 ** on.  *pRoot is the list that i is a member of.
   15254 */
   15255 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
   15256   u32 next = mem3.aPool[i].u.list.next;
   15257   u32 prev = mem3.aPool[i].u.list.prev;
   15258   assert( sqlite3_mutex_held(mem3.mutex) );
   15259   if( prev==0 ){
   15260     *pRoot = next;
   15261   }else{
   15262     mem3.aPool[prev].u.list.next = next;
   15263   }
   15264   if( next ){
   15265     mem3.aPool[next].u.list.prev = prev;
   15266   }
   15267   mem3.aPool[i].u.list.next = 0;
   15268   mem3.aPool[i].u.list.prev = 0;
   15269 }
   15270 
   15271 /*
   15272 ** Unlink the chunk at index i from
   15273 ** whatever list is currently a member of.
   15274 */
   15275 static void memsys3Unlink(u32 i){
   15276   u32 size, hash;
   15277   assert( sqlite3_mutex_held(mem3.mutex) );
   15278   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
   15279   assert( i>=1 );
   15280   size = mem3.aPool[i-1].u.hdr.size4x/4;
   15281   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
   15282   assert( size>=2 );
   15283   if( size <= MX_SMALL ){
   15284     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
   15285   }else{
   15286     hash = size % N_HASH;
   15287     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
   15288   }
   15289 }
   15290 
   15291 /*
   15292 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
   15293 ** at *pRoot.
   15294 */
   15295 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
   15296   assert( sqlite3_mutex_held(mem3.mutex) );
   15297   mem3.aPool[i].u.list.next = *pRoot;
   15298   mem3.aPool[i].u.list.prev = 0;
   15299   if( *pRoot ){
   15300     mem3.aPool[*pRoot].u.list.prev = i;
   15301   }
   15302   *pRoot = i;
   15303 }
   15304 
   15305 /*
   15306 ** Link the chunk at index i into either the appropriate
   15307 ** small chunk list, or into the large chunk hash table.
   15308 */
   15309 static void memsys3Link(u32 i){
   15310   u32 size, hash;
   15311   assert( sqlite3_mutex_held(mem3.mutex) );
   15312   assert( i>=1 );
   15313   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
   15314   size = mem3.aPool[i-1].u.hdr.size4x/4;
   15315   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
   15316   assert( size>=2 );
   15317   if( size <= MX_SMALL ){
   15318     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
   15319   }else{
   15320     hash = size % N_HASH;
   15321     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
   15322   }
   15323 }
   15324 
   15325 /*
   15326 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   15327 ** will already be held (obtained by code in malloc.c) if
   15328 ** sqlite3GlobalConfig.bMemStat is true.
   15329 */
   15330 static void memsys3Enter(void){
   15331   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
   15332     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   15333   }
   15334   sqlite3_mutex_enter(mem3.mutex);
   15335 }
   15336 static void memsys3Leave(void){
   15337   sqlite3_mutex_leave(mem3.mutex);
   15338 }
   15339 
   15340 /*
   15341 ** Called when we are unable to satisfy an allocation of nBytes.
   15342 */
   15343 static void memsys3OutOfMemory(int nByte){
   15344   if( !mem3.alarmBusy ){
   15345     mem3.alarmBusy = 1;
   15346     assert( sqlite3_mutex_held(mem3.mutex) );
   15347     sqlite3_mutex_leave(mem3.mutex);
   15348     sqlite3_release_memory(nByte);
   15349     sqlite3_mutex_enter(mem3.mutex);
   15350     mem3.alarmBusy = 0;
   15351   }
   15352 }
   15353 
   15354 
   15355 /*
   15356 ** Chunk i is a free chunk that has been unlinked.  Adjust its
   15357 ** size parameters for check-out and return a pointer to the
   15358 ** user portion of the chunk.
   15359 */
   15360 static void *memsys3Checkout(u32 i, u32 nBlock){
   15361   u32 x;
   15362   assert( sqlite3_mutex_held(mem3.mutex) );
   15363   assert( i>=1 );
   15364   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
   15365   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
   15366   x = mem3.aPool[i-1].u.hdr.size4x;
   15367   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
   15368   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
   15369   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
   15370   return &mem3.aPool[i];
   15371 }
   15372 
   15373 /*
   15374 ** Carve a piece off of the end of the mem3.iMaster free chunk.
   15375 ** Return a pointer to the new allocation.  Or, if the master chunk
   15376 ** is not large enough, return 0.
   15377 */
   15378 static void *memsys3FromMaster(u32 nBlock){
   15379   assert( sqlite3_mutex_held(mem3.mutex) );
   15380   assert( mem3.szMaster>=nBlock );
   15381   if( nBlock>=mem3.szMaster-1 ){
   15382     /* Use the entire master */
   15383     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
   15384     mem3.iMaster = 0;
   15385     mem3.szMaster = 0;
   15386     mem3.mnMaster = 0;
   15387     return p;
   15388   }else{
   15389     /* Split the master block.  Return the tail. */
   15390     u32 newi, x;
   15391     newi = mem3.iMaster + mem3.szMaster - nBlock;
   15392     assert( newi > mem3.iMaster+1 );
   15393     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
   15394     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
   15395     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
   15396     mem3.szMaster -= nBlock;
   15397     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
   15398     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   15399     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   15400     if( mem3.szMaster < mem3.mnMaster ){
   15401       mem3.mnMaster = mem3.szMaster;
   15402     }
   15403     return (void*)&mem3.aPool[newi];
   15404   }
   15405 }
   15406 
   15407 /*
   15408 ** *pRoot is the head of a list of free chunks of the same size
   15409 ** or same size hash.  In other words, *pRoot is an entry in either
   15410 ** mem3.aiSmall[] or mem3.aiHash[].
   15411 **
   15412 ** This routine examines all entries on the given list and tries
   15413 ** to coalesce each entries with adjacent free chunks.
   15414 **
   15415 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
   15416 ** the current mem3.iMaster with the new larger chunk.  In order for
   15417 ** this mem3.iMaster replacement to work, the master chunk must be
   15418 ** linked into the hash tables.  That is not the normal state of
   15419 ** affairs, of course.  The calling routine must link the master
   15420 ** chunk before invoking this routine, then must unlink the (possibly
   15421 ** changed) master chunk once this routine has finished.
   15422 */
   15423 static void memsys3Merge(u32 *pRoot){
   15424   u32 iNext, prev, size, i, x;
   15425 
   15426   assert( sqlite3_mutex_held(mem3.mutex) );
   15427   for(i=*pRoot; i>0; i=iNext){
   15428     iNext = mem3.aPool[i].u.list.next;
   15429     size = mem3.aPool[i-1].u.hdr.size4x;
   15430     assert( (size&1)==0 );
   15431     if( (size&2)==0 ){
   15432       memsys3UnlinkFromList(i, pRoot);
   15433       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
   15434       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
   15435       if( prev==iNext ){
   15436         iNext = mem3.aPool[prev].u.list.next;
   15437       }
   15438       memsys3Unlink(prev);
   15439       size = i + size/4 - prev;
   15440       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
   15441       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
   15442       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
   15443       memsys3Link(prev);
   15444       i = prev;
   15445     }else{
   15446       size /= 4;
   15447     }
   15448     if( size>mem3.szMaster ){
   15449       mem3.iMaster = i;
   15450       mem3.szMaster = size;
   15451     }
   15452   }
   15453 }
   15454 
   15455 /*
   15456 ** Return a block of memory of at least nBytes in size.
   15457 ** Return NULL if unable.
   15458 **
   15459 ** This function assumes that the necessary mutexes, if any, are
   15460 ** already held by the caller. Hence "Unsafe".
   15461 */
   15462 static void *memsys3MallocUnsafe(int nByte){
   15463   u32 i;
   15464   u32 nBlock;
   15465   u32 toFree;
   15466 
   15467   assert( sqlite3_mutex_held(mem3.mutex) );
   15468   assert( sizeof(Mem3Block)==8 );
   15469   if( nByte<=12 ){
   15470     nBlock = 2;
   15471   }else{
   15472     nBlock = (nByte + 11)/8;
   15473   }
   15474   assert( nBlock>=2 );
   15475 
   15476   /* STEP 1:
   15477   ** Look for an entry of the correct size in either the small
   15478   ** chunk table or in the large chunk hash table.  This is
   15479   ** successful most of the time (about 9 times out of 10).
   15480   */
   15481   if( nBlock <= MX_SMALL ){
   15482     i = mem3.aiSmall[nBlock-2];
   15483     if( i>0 ){
   15484       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
   15485       return memsys3Checkout(i, nBlock);
   15486     }
   15487   }else{
   15488     int hash = nBlock % N_HASH;
   15489     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
   15490       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
   15491         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
   15492         return memsys3Checkout(i, nBlock);
   15493       }
   15494     }
   15495   }
   15496 
   15497   /* STEP 2:
   15498   ** Try to satisfy the allocation by carving a piece off of the end
   15499   ** of the master chunk.  This step usually works if step 1 fails.
   15500   */
   15501   if( mem3.szMaster>=nBlock ){
   15502     return memsys3FromMaster(nBlock);
   15503   }
   15504 
   15505 
   15506   /* STEP 3:
   15507   ** Loop through the entire memory pool.  Coalesce adjacent free
   15508   ** chunks.  Recompute the master chunk as the largest free chunk.
   15509   ** Then try again to satisfy the allocation by carving a piece off
   15510   ** of the end of the master chunk.  This step happens very
   15511   ** rarely (we hope!)
   15512   */
   15513   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
   15514     memsys3OutOfMemory(toFree);
   15515     if( mem3.iMaster ){
   15516       memsys3Link(mem3.iMaster);
   15517       mem3.iMaster = 0;
   15518       mem3.szMaster = 0;
   15519     }
   15520     for(i=0; i<N_HASH; i++){
   15521       memsys3Merge(&mem3.aiHash[i]);
   15522     }
   15523     for(i=0; i<MX_SMALL-1; i++){
   15524       memsys3Merge(&mem3.aiSmall[i]);
   15525     }
   15526     if( mem3.szMaster ){
   15527       memsys3Unlink(mem3.iMaster);
   15528       if( mem3.szMaster>=nBlock ){
   15529         return memsys3FromMaster(nBlock);
   15530       }
   15531     }
   15532   }
   15533 
   15534   /* If none of the above worked, then we fail. */
   15535   return 0;
   15536 }
   15537 
   15538 /*
   15539 ** Free an outstanding memory allocation.
   15540 **
   15541 ** This function assumes that the necessary mutexes, if any, are
   15542 ** already held by the caller. Hence "Unsafe".
   15543 */
   15544 void memsys3FreeUnsafe(void *pOld){
   15545   Mem3Block *p = (Mem3Block*)pOld;
   15546   int i;
   15547   u32 size, x;
   15548   assert( sqlite3_mutex_held(mem3.mutex) );
   15549   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
   15550   i = p - mem3.aPool;
   15551   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
   15552   size = mem3.aPool[i-1].u.hdr.size4x/4;
   15553   assert( i+size<=mem3.nPool+1 );
   15554   mem3.aPool[i-1].u.hdr.size4x &= ~1;
   15555   mem3.aPool[i+size-1].u.hdr.prevSize = size;
   15556   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
   15557   memsys3Link(i);
   15558 
   15559   /* Try to expand the master using the newly freed chunk */
   15560   if( mem3.iMaster ){
   15561     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
   15562       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
   15563       mem3.iMaster -= size;
   15564       mem3.szMaster += size;
   15565       memsys3Unlink(mem3.iMaster);
   15566       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   15567       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   15568       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
   15569     }
   15570     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   15571     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
   15572       memsys3Unlink(mem3.iMaster+mem3.szMaster);
   15573       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
   15574       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   15575       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
   15576     }
   15577   }
   15578 }
   15579 
   15580 /*
   15581 ** Return the size of an outstanding allocation, in bytes.  The
   15582 ** size returned omits the 8-byte header overhead.  This only
   15583 ** works for chunks that are currently checked out.
   15584 */
   15585 static int memsys3Size(void *p){
   15586   Mem3Block *pBlock;
   15587   if( p==0 ) return 0;
   15588   pBlock = (Mem3Block*)p;
   15589   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
   15590   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
   15591 }
   15592 
   15593 /*
   15594 ** Round up a request size to the next valid allocation size.
   15595 */
   15596 static int memsys3Roundup(int n){
   15597   if( n<=12 ){
   15598     return 12;
   15599   }else{
   15600     return ((n+11)&~7) - 4;
   15601   }
   15602 }
   15603 
   15604 /*
   15605 ** Allocate nBytes of memory.
   15606 */
   15607 static void *memsys3Malloc(int nBytes){
   15608   sqlite3_int64 *p;
   15609   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
   15610   memsys3Enter();
   15611   p = memsys3MallocUnsafe(nBytes);
   15612   memsys3Leave();
   15613   return (void*)p;
   15614 }
   15615 
   15616 /*
   15617 ** Free memory.
   15618 */
   15619 void memsys3Free(void *pPrior){
   15620   assert( pPrior );
   15621   memsys3Enter();
   15622   memsys3FreeUnsafe(pPrior);
   15623   memsys3Leave();
   15624 }
   15625 
   15626 /*
   15627 ** Change the size of an existing memory allocation
   15628 */
   15629 void *memsys3Realloc(void *pPrior, int nBytes){
   15630   int nOld;
   15631   void *p;
   15632   if( pPrior==0 ){
   15633     return sqlite3_malloc(nBytes);
   15634   }
   15635   if( nBytes<=0 ){
   15636     sqlite3_free(pPrior);
   15637     return 0;
   15638   }
   15639   nOld = memsys3Size(pPrior);
   15640   if( nBytes<=nOld && nBytes>=nOld-128 ){
   15641     return pPrior;
   15642   }
   15643   memsys3Enter();
   15644   p = memsys3MallocUnsafe(nBytes);
   15645   if( p ){
   15646     if( nOld<nBytes ){
   15647       memcpy(p, pPrior, nOld);
   15648     }else{
   15649       memcpy(p, pPrior, nBytes);
   15650     }
   15651     memsys3FreeUnsafe(pPrior);
   15652   }
   15653   memsys3Leave();
   15654   return p;
   15655 }
   15656 
   15657 /*
   15658 ** Initialize this module.
   15659 */
   15660 static int memsys3Init(void *NotUsed){
   15661   UNUSED_PARAMETER(NotUsed);
   15662   if( !sqlite3GlobalConfig.pHeap ){
   15663     return SQLITE_ERROR;
   15664   }
   15665 
   15666   /* Store a pointer to the memory block in global structure mem3. */
   15667   assert( sizeof(Mem3Block)==8 );
   15668   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
   15669   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
   15670 
   15671   /* Initialize the master block. */
   15672   mem3.szMaster = mem3.nPool;
   15673   mem3.mnMaster = mem3.szMaster;
   15674   mem3.iMaster = 1;
   15675   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
   15676   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
   15677   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
   15678 
   15679   return SQLITE_OK;
   15680 }
   15681 
   15682 /*
   15683 ** Deinitialize this module.
   15684 */
   15685 static void memsys3Shutdown(void *NotUsed){
   15686   UNUSED_PARAMETER(NotUsed);
   15687   mem3.mutex = 0;
   15688   return;
   15689 }
   15690 
   15691 
   15692 
   15693 /*
   15694 ** Open the file indicated and write a log of all unfreed memory
   15695 ** allocations into that log.
   15696 */
   15697 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
   15698 #ifdef SQLITE_DEBUG
   15699   FILE *out;
   15700   u32 i, j;
   15701   u32 size;
   15702   if( zFilename==0 || zFilename[0]==0 ){
   15703     out = stdout;
   15704   }else{
   15705     out = fopen(zFilename, "w");
   15706     if( out==0 ){
   15707       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   15708                       zFilename);
   15709       return;
   15710     }
   15711   }
   15712   memsys3Enter();
   15713   fprintf(out, "CHUNKS:\n");
   15714   for(i=1; i<=mem3.nPool; i+=size/4){
   15715     size = mem3.aPool[i-1].u.hdr.size4x;
   15716     if( size/4<=1 ){
   15717       fprintf(out, "%p size error\n", &mem3.aPool[i]);
   15718       assert( 0 );
   15719       break;
   15720     }
   15721     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
   15722       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
   15723       assert( 0 );
   15724       break;
   15725     }
   15726     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
   15727       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
   15728       assert( 0 );
   15729       break;
   15730     }
   15731     if( size&1 ){
   15732       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
   15733     }else{
   15734       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
   15735                   i==mem3.iMaster ? " **master**" : "");
   15736     }
   15737   }
   15738   for(i=0; i<MX_SMALL-1; i++){
   15739     if( mem3.aiSmall[i]==0 ) continue;
   15740     fprintf(out, "small(%2d):", i);
   15741     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
   15742       fprintf(out, " %p(%d)", &mem3.aPool[j],
   15743               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
   15744     }
   15745     fprintf(out, "\n");
   15746   }
   15747   for(i=0; i<N_HASH; i++){
   15748     if( mem3.aiHash[i]==0 ) continue;
   15749     fprintf(out, "hash(%2d):", i);
   15750     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
   15751       fprintf(out, " %p(%d)", &mem3.aPool[j],
   15752               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
   15753     }
   15754     fprintf(out, "\n");
   15755   }
   15756   fprintf(out, "master=%d\n", mem3.iMaster);
   15757   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
   15758   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
   15759   sqlite3_mutex_leave(mem3.mutex);
   15760   if( out==stdout ){
   15761     fflush(stdout);
   15762   }else{
   15763     fclose(out);
   15764   }
   15765 #else
   15766   UNUSED_PARAMETER(zFilename);
   15767 #endif
   15768 }
   15769 
   15770 /*
   15771 ** This routine is the only routine in this file with external
   15772 ** linkage.
   15773 **
   15774 ** Populate the low-level memory allocation function pointers in
   15775 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
   15776 ** arguments specify the block of memory to manage.
   15777 **
   15778 ** This routine is only called by sqlite3_config(), and therefore
   15779 ** is not required to be threadsafe (it is not).
   15780 */
   15781 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
   15782   static const sqlite3_mem_methods mempoolMethods = {
   15783      memsys3Malloc,
   15784      memsys3Free,
   15785      memsys3Realloc,
   15786      memsys3Size,
   15787      memsys3Roundup,
   15788      memsys3Init,
   15789      memsys3Shutdown,
   15790      0
   15791   };
   15792   return &mempoolMethods;
   15793 }
   15794 
   15795 #endif /* SQLITE_ENABLE_MEMSYS3 */
   15796 
   15797 /************** End of mem3.c ************************************************/
   15798 /************** Begin file mem5.c ********************************************/
   15799 /*
   15800 ** 2007 October 14
   15801 **
   15802 ** The author disclaims copyright to this source code.  In place of
   15803 ** a legal notice, here is a blessing:
   15804 **
   15805 **    May you do good and not evil.
   15806 **    May you find forgiveness for yourself and forgive others.
   15807 **    May you share freely, never taking more than you give.
   15808 **
   15809 *************************************************************************
   15810 ** This file contains the C functions that implement a memory
   15811 ** allocation subsystem for use by SQLite.
   15812 **
   15813 ** This version of the memory allocation subsystem omits all
   15814 ** use of malloc(). The application gives SQLite a block of memory
   15815 ** before calling sqlite3_initialize() from which allocations
   15816 ** are made and returned by the xMalloc() and xRealloc()
   15817 ** implementations. Once sqlite3_initialize() has been called,
   15818 ** the amount of memory available to SQLite is fixed and cannot
   15819 ** be changed.
   15820 **
   15821 ** This version of the memory allocation subsystem is included
   15822 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
   15823 **
   15824 ** This memory allocator uses the following algorithm:
   15825 **
   15826 **   1.  All memory allocations sizes are rounded up to a power of 2.
   15827 **
   15828 **   2.  If two adjacent free blocks are the halves of a larger block,
   15829 **       then the two blocks are coalesed into the single larger block.
   15830 **
   15831 **   3.  New memory is allocated from the first available free block.
   15832 **
   15833 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
   15834 ** Concerning Dynamic Storage Allocation". Journal of the Association for
   15835 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
   15836 **
   15837 ** Let n be the size of the largest allocation divided by the minimum
   15838 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
   15839 ** be the maximum amount of memory ever outstanding at one time.  Let
   15840 ** N be the total amount of memory available for allocation.  Robson
   15841 ** proved that this memory allocator will never breakdown due to
   15842 ** fragmentation as long as the following constraint holds:
   15843 **
   15844 **      N >=  M*(1 + log2(n)/2) - n + 1
   15845 **
   15846 ** The sqlite3_status() logic tracks the maximum values of n and M so
   15847 ** that an application can, at any time, verify this constraint.
   15848 */
   15849 
   15850 /*
   15851 ** This version of the memory allocator is used only when
   15852 ** SQLITE_ENABLE_MEMSYS5 is defined.
   15853 */
   15854 #ifdef SQLITE_ENABLE_MEMSYS5
   15855 
   15856 /*
   15857 ** A minimum allocation is an instance of the following structure.
   15858 ** Larger allocations are an array of these structures where the
   15859 ** size of the array is a power of 2.
   15860 **
   15861 ** The size of this object must be a power of two.  That fact is
   15862 ** verified in memsys5Init().
   15863 */
   15864 typedef struct Mem5Link Mem5Link;
   15865 struct Mem5Link {
   15866   int next;       /* Index of next free chunk */
   15867   int prev;       /* Index of previous free chunk */
   15868 };
   15869 
   15870 /*
   15871 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
   15872 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
   15873 ** it is not actually possible to reach this limit.
   15874 */
   15875 #define LOGMAX 30
   15876 
   15877 /*
   15878 ** Masks used for mem5.aCtrl[] elements.
   15879 */
   15880 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
   15881 #define CTRL_FREE     0x20    /* True if not checked out */
   15882 
   15883 /*
   15884 ** All of the static variables used by this module are collected
   15885 ** into a single structure named "mem5".  This is to keep the
   15886 ** static variables organized and to reduce namespace pollution
   15887 ** when this module is combined with other in the amalgamation.
   15888 */
   15889 static SQLITE_WSD struct Mem5Global {
   15890   /*
   15891   ** Memory available for allocation
   15892   */
   15893   int szAtom;      /* Smallest possible allocation in bytes */
   15894   int nBlock;      /* Number of szAtom sized blocks in zPool */
   15895   u8 *zPool;       /* Memory available to be allocated */
   15896 
   15897   /*
   15898   ** Mutex to control access to the memory allocation subsystem.
   15899   */
   15900   sqlite3_mutex *mutex;
   15901 
   15902   /*
   15903   ** Performance statistics
   15904   */
   15905   u64 nAlloc;         /* Total number of calls to malloc */
   15906   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
   15907   u64 totalExcess;    /* Total internal fragmentation */
   15908   u32 currentOut;     /* Current checkout, including internal fragmentation */
   15909   u32 currentCount;   /* Current number of distinct checkouts */
   15910   u32 maxOut;         /* Maximum instantaneous currentOut */
   15911   u32 maxCount;       /* Maximum instantaneous currentCount */
   15912   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
   15913 
   15914   /*
   15915   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
   15916   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
   15917   ** and so forth.
   15918   */
   15919   int aiFreelist[LOGMAX+1];
   15920 
   15921   /*
   15922   ** Space for tracking which blocks are checked out and the size
   15923   ** of each block.  One byte per block.
   15924   */
   15925   u8 *aCtrl;
   15926 
   15927 } mem5;
   15928 
   15929 /*
   15930 ** Access the static variable through a macro for SQLITE_OMIT_WSD
   15931 */
   15932 #define mem5 GLOBAL(struct Mem5Global, mem5)
   15933 
   15934 /*
   15935 ** Assuming mem5.zPool is divided up into an array of Mem5Link
   15936 ** structures, return a pointer to the idx-th such lik.
   15937 */
   15938 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
   15939 
   15940 /*
   15941 ** Unlink the chunk at mem5.aPool[i] from list it is currently
   15942 ** on.  It should be found on mem5.aiFreelist[iLogsize].
   15943 */
   15944 static void memsys5Unlink(int i, int iLogsize){
   15945   int next, prev;
   15946   assert( i>=0 && i<mem5.nBlock );
   15947   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   15948   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
   15949 
   15950   next = MEM5LINK(i)->next;
   15951   prev = MEM5LINK(i)->prev;
   15952   if( prev<0 ){
   15953     mem5.aiFreelist[iLogsize] = next;
   15954   }else{
   15955     MEM5LINK(prev)->next = next;
   15956   }
   15957   if( next>=0 ){
   15958     MEM5LINK(next)->prev = prev;
   15959   }
   15960 }
   15961 
   15962 /*
   15963 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
   15964 ** free list.
   15965 */
   15966 static void memsys5Link(int i, int iLogsize){
   15967   int x;
   15968   assert( sqlite3_mutex_held(mem5.mutex) );
   15969   assert( i>=0 && i<mem5.nBlock );
   15970   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   15971   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
   15972 
   15973   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
   15974   MEM5LINK(i)->prev = -1;
   15975   if( x>=0 ){
   15976     assert( x<mem5.nBlock );
   15977     MEM5LINK(x)->prev = i;
   15978   }
   15979   mem5.aiFreelist[iLogsize] = i;
   15980 }
   15981 
   15982 /*
   15983 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   15984 ** will already be held (obtained by code in malloc.c) if
   15985 ** sqlite3GlobalConfig.bMemStat is true.
   15986 */
   15987 static void memsys5Enter(void){
   15988   sqlite3_mutex_enter(mem5.mutex);
   15989 }
   15990 static void memsys5Leave(void){
   15991   sqlite3_mutex_leave(mem5.mutex);
   15992 }
   15993 
   15994 /*
   15995 ** Return the size of an outstanding allocation, in bytes.  The
   15996 ** size returned omits the 8-byte header overhead.  This only
   15997 ** works for chunks that are currently checked out.
   15998 */
   15999 static int memsys5Size(void *p){
   16000   int iSize = 0;
   16001   if( p ){
   16002     int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
   16003     assert( i>=0 && i<mem5.nBlock );
   16004     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
   16005   }
   16006   return iSize;
   16007 }
   16008 
   16009 /*
   16010 ** Find the first entry on the freelist iLogsize.  Unlink that
   16011 ** entry and return its index.
   16012 */
   16013 static int memsys5UnlinkFirst(int iLogsize){
   16014   int i;
   16015   int iFirst;
   16016 
   16017   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   16018   i = iFirst = mem5.aiFreelist[iLogsize];
   16019   assert( iFirst>=0 );
   16020   while( i>0 ){
   16021     if( i<iFirst ) iFirst = i;
   16022     i = MEM5LINK(i)->next;
   16023   }
   16024   memsys5Unlink(iFirst, iLogsize);
   16025   return iFirst;
   16026 }
   16027 
   16028 /*
   16029 ** Return a block of memory of at least nBytes in size.
   16030 ** Return NULL if unable.  Return NULL if nBytes==0.
   16031 **
   16032 ** The caller guarantees that nByte positive.
   16033 **
   16034 ** The caller has obtained a mutex prior to invoking this
   16035 ** routine so there is never any chance that two or more
   16036 ** threads can be in this routine at the same time.
   16037 */
   16038 static void *memsys5MallocUnsafe(int nByte){
   16039   int i;           /* Index of a mem5.aPool[] slot */
   16040   int iBin;        /* Index into mem5.aiFreelist[] */
   16041   int iFullSz;     /* Size of allocation rounded up to power of 2 */
   16042   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
   16043 
   16044   /* nByte must be a positive */
   16045   assert( nByte>0 );
   16046 
   16047   /* Keep track of the maximum allocation request.  Even unfulfilled
   16048   ** requests are counted */
   16049   if( (u32)nByte>mem5.maxRequest ){
   16050     mem5.maxRequest = nByte;
   16051   }
   16052 
   16053   /* Abort if the requested allocation size is larger than the largest
   16054   ** power of two that we can represent using 32-bit signed integers.
   16055   */
   16056   if( nByte > 0x40000000 ){
   16057     return 0;
   16058   }
   16059 
   16060   /* Round nByte up to the next valid power of two */
   16061   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
   16062 
   16063   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   16064   ** block.  If not, then split a block of the next larger power of
   16065   ** two in order to create a new free block of size iLogsize.
   16066   */
   16067   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
   16068   if( iBin>LOGMAX ){
   16069     testcase( sqlite3GlobalConfig.xLog!=0 );
   16070     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
   16071     return 0;
   16072   }
   16073   i = memsys5UnlinkFirst(iBin);
   16074   while( iBin>iLogsize ){
   16075     int newSize;
   16076 
   16077     iBin--;
   16078     newSize = 1 << iBin;
   16079     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
   16080     memsys5Link(i+newSize, iBin);
   16081   }
   16082   mem5.aCtrl[i] = iLogsize;
   16083 
   16084   /* Update allocator performance statistics. */
   16085   mem5.nAlloc++;
   16086   mem5.totalAlloc += iFullSz;
   16087   mem5.totalExcess += iFullSz - nByte;
   16088   mem5.currentCount++;
   16089   mem5.currentOut += iFullSz;
   16090   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
   16091   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
   16092 
   16093   /* Return a pointer to the allocated memory. */
   16094   return (void*)&mem5.zPool[i*mem5.szAtom];
   16095 }
   16096 
   16097 /*
   16098 ** Free an outstanding memory allocation.
   16099 */
   16100 static void memsys5FreeUnsafe(void *pOld){
   16101   u32 size, iLogsize;
   16102   int iBlock;
   16103 
   16104   /* Set iBlock to the index of the block pointed to by pOld in
   16105   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
   16106   */
   16107   iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
   16108 
   16109   /* Check that the pointer pOld points to a valid, non-free block. */
   16110   assert( iBlock>=0 && iBlock<mem5.nBlock );
   16111   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
   16112   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
   16113 
   16114   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
   16115   size = 1<<iLogsize;
   16116   assert( iBlock+size-1<(u32)mem5.nBlock );
   16117 
   16118   mem5.aCtrl[iBlock] |= CTRL_FREE;
   16119   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
   16120   assert( mem5.currentCount>0 );
   16121   assert( mem5.currentOut>=(size*mem5.szAtom) );
   16122   mem5.currentCount--;
   16123   mem5.currentOut -= size*mem5.szAtom;
   16124   assert( mem5.currentOut>0 || mem5.currentCount==0 );
   16125   assert( mem5.currentCount>0 || mem5.currentOut==0 );
   16126 
   16127   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
   16128   while( ALWAYS(iLogsize<LOGMAX) ){
   16129     int iBuddy;
   16130     if( (iBlock>>iLogsize) & 1 ){
   16131       iBuddy = iBlock - size;
   16132     }else{
   16133       iBuddy = iBlock + size;
   16134     }
   16135     assert( iBuddy>=0 );
   16136     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
   16137     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
   16138     memsys5Unlink(iBuddy, iLogsize);
   16139     iLogsize++;
   16140     if( iBuddy<iBlock ){
   16141       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
   16142       mem5.aCtrl[iBlock] = 0;
   16143       iBlock = iBuddy;
   16144     }else{
   16145       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
   16146       mem5.aCtrl[iBuddy] = 0;
   16147     }
   16148     size *= 2;
   16149   }
   16150   memsys5Link(iBlock, iLogsize);
   16151 }
   16152 
   16153 /*
   16154 ** Allocate nBytes of memory
   16155 */
   16156 static void *memsys5Malloc(int nBytes){
   16157   sqlite3_int64 *p = 0;
   16158   if( nBytes>0 ){
   16159     memsys5Enter();
   16160     p = memsys5MallocUnsafe(nBytes);
   16161     memsys5Leave();
   16162   }
   16163   return (void*)p;
   16164 }
   16165 
   16166 /*
   16167 ** Free memory.
   16168 **
   16169 ** The outer layer memory allocator prevents this routine from
   16170 ** being called with pPrior==0.
   16171 */
   16172 static void memsys5Free(void *pPrior){
   16173   assert( pPrior!=0 );
   16174   memsys5Enter();
   16175   memsys5FreeUnsafe(pPrior);
   16176   memsys5Leave();
   16177 }
   16178 
   16179 /*
   16180 ** Change the size of an existing memory allocation.
   16181 **
   16182 ** The outer layer memory allocator prevents this routine from
   16183 ** being called with pPrior==0.
   16184 **
   16185 ** nBytes is always a value obtained from a prior call to
   16186 ** memsys5Round().  Hence nBytes is always a non-negative power
   16187 ** of two.  If nBytes==0 that means that an oversize allocation
   16188 ** (an allocation larger than 0x40000000) was requested and this
   16189 ** routine should return 0 without freeing pPrior.
   16190 */
   16191 static void *memsys5Realloc(void *pPrior, int nBytes){
   16192   int nOld;
   16193   void *p;
   16194   assert( pPrior!=0 );
   16195   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
   16196   assert( nBytes>=0 );
   16197   if( nBytes==0 ){
   16198     return 0;
   16199   }
   16200   nOld = memsys5Size(pPrior);
   16201   if( nBytes<=nOld ){
   16202     return pPrior;
   16203   }
   16204   memsys5Enter();
   16205   p = memsys5MallocUnsafe(nBytes);
   16206   if( p ){
   16207     memcpy(p, pPrior, nOld);
   16208     memsys5FreeUnsafe(pPrior);
   16209   }
   16210   memsys5Leave();
   16211   return p;
   16212 }
   16213 
   16214 /*
   16215 ** Round up a request size to the next valid allocation size.  If
   16216 ** the allocation is too large to be handled by this allocation system,
   16217 ** return 0.
   16218 **
   16219 ** All allocations must be a power of two and must be expressed by a
   16220 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
   16221 ** or 1073741824 bytes.
   16222 */
   16223 static int memsys5Roundup(int n){
   16224   int iFullSz;
   16225   if( n > 0x40000000 ) return 0;
   16226   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
   16227   return iFullSz;
   16228 }
   16229 
   16230 /*
   16231 ** Return the ceiling of the logarithm base 2 of iValue.
   16232 **
   16233 ** Examples:   memsys5Log(1) -> 0
   16234 **             memsys5Log(2) -> 1
   16235 **             memsys5Log(4) -> 2
   16236 **             memsys5Log(5) -> 3
   16237 **             memsys5Log(8) -> 3
   16238 **             memsys5Log(9) -> 4
   16239 */
   16240 static int memsys5Log(int iValue){
   16241   int iLog;
   16242   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
   16243   return iLog;
   16244 }
   16245 
   16246 /*
   16247 ** Initialize the memory allocator.
   16248 **
   16249 ** This routine is not threadsafe.  The caller must be holding a mutex
   16250 ** to prevent multiple threads from entering at the same time.
   16251 */
   16252 static int memsys5Init(void *NotUsed){
   16253   int ii;            /* Loop counter */
   16254   int nByte;         /* Number of bytes of memory available to this allocator */
   16255   u8 *zByte;         /* Memory usable by this allocator */
   16256   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
   16257   int iOffset;       /* An offset into mem5.aCtrl[] */
   16258 
   16259   UNUSED_PARAMETER(NotUsed);
   16260 
   16261   /* For the purposes of this routine, disable the mutex */
   16262   mem5.mutex = 0;
   16263 
   16264   /* The size of a Mem5Link object must be a power of two.  Verify that
   16265   ** this is case.
   16266   */
   16267   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
   16268 
   16269   nByte = sqlite3GlobalConfig.nHeap;
   16270   zByte = (u8*)sqlite3GlobalConfig.pHeap;
   16271   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
   16272 
   16273   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
   16274   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
   16275   mem5.szAtom = (1<<nMinLog);
   16276   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
   16277     mem5.szAtom = mem5.szAtom << 1;
   16278   }
   16279 
   16280   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
   16281   mem5.zPool = zByte;
   16282   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
   16283 
   16284   for(ii=0; ii<=LOGMAX; ii++){
   16285     mem5.aiFreelist[ii] = -1;
   16286   }
   16287 
   16288   iOffset = 0;
   16289   for(ii=LOGMAX; ii>=0; ii--){
   16290     int nAlloc = (1<<ii);
   16291     if( (iOffset+nAlloc)<=mem5.nBlock ){
   16292       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
   16293       memsys5Link(iOffset, ii);
   16294       iOffset += nAlloc;
   16295     }
   16296     assert((iOffset+nAlloc)>mem5.nBlock);
   16297   }
   16298 
   16299   /* If a mutex is required for normal operation, allocate one */
   16300   if( sqlite3GlobalConfig.bMemstat==0 ){
   16301     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   16302   }
   16303 
   16304   return SQLITE_OK;
   16305 }
   16306 
   16307 /*
   16308 ** Deinitialize this module.
   16309 */
   16310 static void memsys5Shutdown(void *NotUsed){
   16311   UNUSED_PARAMETER(NotUsed);
   16312   mem5.mutex = 0;
   16313   return;
   16314 }
   16315 
   16316 #ifdef SQLITE_TEST
   16317 /*
   16318 ** Open the file indicated and write a log of all unfreed memory
   16319 ** allocations into that log.
   16320 */
   16321 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
   16322   FILE *out;
   16323   int i, j, n;
   16324   int nMinLog;
   16325 
   16326   if( zFilename==0 || zFilename[0]==0 ){
   16327     out = stdout;
   16328   }else{
   16329     out = fopen(zFilename, "w");
   16330     if( out==0 ){
   16331       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   16332                       zFilename);
   16333       return;
   16334     }
   16335   }
   16336   memsys5Enter();
   16337   nMinLog = memsys5Log(mem5.szAtom);
   16338   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
   16339     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
   16340     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
   16341   }
   16342   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
   16343   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
   16344   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
   16345   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
   16346   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
   16347   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
   16348   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
   16349   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
   16350   memsys5Leave();
   16351   if( out==stdout ){
   16352     fflush(stdout);
   16353   }else{
   16354     fclose(out);
   16355   }
   16356 }
   16357 #endif
   16358 
   16359 /*
   16360 ** This routine is the only routine in this file with external
   16361 ** linkage. It returns a pointer to a static sqlite3_mem_methods
   16362 ** struct populated with the memsys5 methods.
   16363 */
   16364 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
   16365   static const sqlite3_mem_methods memsys5Methods = {
   16366      memsys5Malloc,
   16367      memsys5Free,
   16368      memsys5Realloc,
   16369      memsys5Size,
   16370      memsys5Roundup,
   16371      memsys5Init,
   16372      memsys5Shutdown,
   16373      0
   16374   };
   16375   return &memsys5Methods;
   16376 }
   16377 
   16378 #endif /* SQLITE_ENABLE_MEMSYS5 */
   16379 
   16380 /************** End of mem5.c ************************************************/
   16381 /************** Begin file mutex.c *******************************************/
   16382 /*
   16383 ** 2007 August 14
   16384 **
   16385 ** The author disclaims copyright to this source code.  In place of
   16386 ** a legal notice, here is a blessing:
   16387 **
   16388 **    May you do good and not evil.
   16389 **    May you find forgiveness for yourself and forgive others.
   16390 **    May you share freely, never taking more than you give.
   16391 **
   16392 *************************************************************************
   16393 ** This file contains the C functions that implement mutexes.
   16394 **
   16395 ** This file contains code that is common across all mutex implementations.
   16396 */
   16397 
   16398 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
   16399 /*
   16400 ** For debugging purposes, record when the mutex subsystem is initialized
   16401 ** and uninitialized so that we can assert() if there is an attempt to
   16402 ** allocate a mutex while the system is uninitialized.
   16403 */
   16404 static SQLITE_WSD int mutexIsInit = 0;
   16405 #endif /* SQLITE_DEBUG */
   16406 
   16407 
   16408 #ifndef SQLITE_MUTEX_OMIT
   16409 /*
   16410 ** Initialize the mutex system.
   16411 */
   16412 SQLITE_PRIVATE int sqlite3MutexInit(void){
   16413   int rc = SQLITE_OK;
   16414   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
   16415     /* If the xMutexAlloc method has not been set, then the user did not
   16416     ** install a mutex implementation via sqlite3_config() prior to
   16417     ** sqlite3_initialize() being called. This block copies pointers to
   16418     ** the default implementation into the sqlite3GlobalConfig structure.
   16419     */
   16420     sqlite3_mutex_methods const *pFrom;
   16421     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
   16422 
   16423     if( sqlite3GlobalConfig.bCoreMutex ){
   16424       pFrom = sqlite3DefaultMutex();
   16425     }else{
   16426       pFrom = sqlite3NoopMutex();
   16427     }
   16428     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
   16429     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
   16430            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
   16431     pTo->xMutexAlloc = pFrom->xMutexAlloc;
   16432   }
   16433   rc = sqlite3GlobalConfig.mutex.xMutexInit();
   16434 
   16435 #ifdef SQLITE_DEBUG
   16436   GLOBAL(int, mutexIsInit) = 1;
   16437 #endif
   16438 
   16439   return rc;
   16440 }
   16441 
   16442 /*
   16443 ** Shutdown the mutex system. This call frees resources allocated by
   16444 ** sqlite3MutexInit().
   16445 */
   16446 SQLITE_PRIVATE int sqlite3MutexEnd(void){
   16447   int rc = SQLITE_OK;
   16448   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
   16449     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
   16450   }
   16451 
   16452 #ifdef SQLITE_DEBUG
   16453   GLOBAL(int, mutexIsInit) = 0;
   16454 #endif
   16455 
   16456   return rc;
   16457 }
   16458 
   16459 /*
   16460 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
   16461 */
   16462 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
   16463 #ifndef SQLITE_OMIT_AUTOINIT
   16464   if( sqlite3_initialize() ) return 0;
   16465 #endif
   16466   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
   16467 }
   16468 
   16469 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
   16470   if( !sqlite3GlobalConfig.bCoreMutex ){
   16471     return 0;
   16472   }
   16473   assert( GLOBAL(int, mutexIsInit) );
   16474   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
   16475 }
   16476 
   16477 /*
   16478 ** Free a dynamic mutex.
   16479 */
   16480 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
   16481   if( p ){
   16482     sqlite3GlobalConfig.mutex.xMutexFree(p);
   16483   }
   16484 }
   16485 
   16486 /*
   16487 ** Obtain the mutex p. If some other thread already has the mutex, block
   16488 ** until it can be obtained.
   16489 */
   16490 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
   16491   if( p ){
   16492     sqlite3GlobalConfig.mutex.xMutexEnter(p);
   16493   }
   16494 }
   16495 
   16496 /*
   16497 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
   16498 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
   16499 */
   16500 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
   16501   int rc = SQLITE_OK;
   16502   if( p ){
   16503     return sqlite3GlobalConfig.mutex.xMutexTry(p);
   16504   }
   16505   return rc;
   16506 }
   16507 
   16508 /*
   16509 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
   16510 ** entered by the same thread.  The behavior is undefined if the mutex
   16511 ** is not currently entered. If a NULL pointer is passed as an argument
   16512 ** this function is a no-op.
   16513 */
   16514 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
   16515   if( p ){
   16516     sqlite3GlobalConfig.mutex.xMutexLeave(p);
   16517   }
   16518 }
   16519 
   16520 #ifndef NDEBUG
   16521 /*
   16522 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   16523 ** intended for use inside assert() statements.
   16524 */
   16525 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
   16526   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
   16527 }
   16528 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
   16529   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
   16530 }
   16531 #endif
   16532 
   16533 #endif /* SQLITE_MUTEX_OMIT */
   16534 
   16535 /************** End of mutex.c ***********************************************/
   16536 /************** Begin file mutex_noop.c **************************************/
   16537 /*
   16538 ** 2008 October 07
   16539 **
   16540 ** The author disclaims copyright to this source code.  In place of
   16541 ** a legal notice, here is a blessing:
   16542 **
   16543 **    May you do good and not evil.
   16544 **    May you find forgiveness for yourself and forgive others.
   16545 **    May you share freely, never taking more than you give.
   16546 **
   16547 *************************************************************************
   16548 ** This file contains the C functions that implement mutexes.
   16549 **
   16550 ** This implementation in this file does not provide any mutual
   16551 ** exclusion and is thus suitable for use only in applications
   16552 ** that use SQLite in a single thread.  The routines defined
   16553 ** here are place-holders.  Applications can substitute working
   16554 ** mutex routines at start-time using the
   16555 **
   16556 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
   16557 **
   16558 ** interface.
   16559 **
   16560 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
   16561 ** that does error checking on mutexes to make sure they are being
   16562 ** called correctly.
   16563 */
   16564 
   16565 #ifndef SQLITE_MUTEX_OMIT
   16566 
   16567 #ifndef SQLITE_DEBUG
   16568 /*
   16569 ** Stub routines for all mutex methods.
   16570 **
   16571 ** This routines provide no mutual exclusion or error checking.
   16572 */
   16573 static int noopMutexInit(void){ return SQLITE_OK; }
   16574 static int noopMutexEnd(void){ return SQLITE_OK; }
   16575 static sqlite3_mutex *noopMutexAlloc(int id){
   16576   UNUSED_PARAMETER(id);
   16577   return (sqlite3_mutex*)8;
   16578 }
   16579 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   16580 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   16581 static int noopMutexTry(sqlite3_mutex *p){
   16582   UNUSED_PARAMETER(p);
   16583   return SQLITE_OK;
   16584 }
   16585 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   16586 
   16587 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
   16588   static const sqlite3_mutex_methods sMutex = {
   16589     noopMutexInit,
   16590     noopMutexEnd,
   16591     noopMutexAlloc,
   16592     noopMutexFree,
   16593     noopMutexEnter,
   16594     noopMutexTry,
   16595     noopMutexLeave,
   16596 
   16597     0,
   16598     0,
   16599   };
   16600 
   16601   return &sMutex;
   16602 }
   16603 #endif /* !SQLITE_DEBUG */
   16604 
   16605 #ifdef SQLITE_DEBUG
   16606 /*
   16607 ** In this implementation, error checking is provided for testing
   16608 ** and debugging purposes.  The mutexes still do not provide any
   16609 ** mutual exclusion.
   16610 */
   16611 
   16612 /*
   16613 ** The mutex object
   16614 */
   16615 typedef struct sqlite3_debug_mutex {
   16616   int id;     /* The mutex type */
   16617   int cnt;    /* Number of entries without a matching leave */
   16618 } sqlite3_debug_mutex;
   16619 
   16620 /*
   16621 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   16622 ** intended for use inside assert() statements.
   16623 */
   16624 static int debugMutexHeld(sqlite3_mutex *pX){
   16625   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16626   return p==0 || p->cnt>0;
   16627 }
   16628 static int debugMutexNotheld(sqlite3_mutex *pX){
   16629   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16630   return p==0 || p->cnt==0;
   16631 }
   16632 
   16633 /*
   16634 ** Initialize and deinitialize the mutex subsystem.
   16635 */
   16636 static int debugMutexInit(void){ return SQLITE_OK; }
   16637 static int debugMutexEnd(void){ return SQLITE_OK; }
   16638 
   16639 /*
   16640 ** The sqlite3_mutex_alloc() routine allocates a new
   16641 ** mutex and returns a pointer to it.  If it returns NULL
   16642 ** that means that a mutex could not be allocated.
   16643 */
   16644 static sqlite3_mutex *debugMutexAlloc(int id){
   16645   static sqlite3_debug_mutex aStatic[6];
   16646   sqlite3_debug_mutex *pNew = 0;
   16647   switch( id ){
   16648     case SQLITE_MUTEX_FAST:
   16649     case SQLITE_MUTEX_RECURSIVE: {
   16650       pNew = sqlite3Malloc(sizeof(*pNew));
   16651       if( pNew ){
   16652         pNew->id = id;
   16653         pNew->cnt = 0;
   16654       }
   16655       break;
   16656     }
   16657     default: {
   16658       assert( id-2 >= 0 );
   16659       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
   16660       pNew = &aStatic[id-2];
   16661       pNew->id = id;
   16662       break;
   16663     }
   16664   }
   16665   return (sqlite3_mutex*)pNew;
   16666 }
   16667 
   16668 /*
   16669 ** This routine deallocates a previously allocated mutex.
   16670 */
   16671 static void debugMutexFree(sqlite3_mutex *pX){
   16672   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16673   assert( p->cnt==0 );
   16674   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   16675   sqlite3_free(p);
   16676 }
   16677 
   16678 /*
   16679 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   16680 ** to enter a mutex.  If another thread is already within the mutex,
   16681 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   16682 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   16683 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   16684 ** be entered multiple times by the same thread.  In such cases the,
   16685 ** mutex must be exited an equal number of times before another thread
   16686 ** can enter.  If the same thread tries to enter any other kind of mutex
   16687 ** more than once, the behavior is undefined.
   16688 */
   16689 static void debugMutexEnter(sqlite3_mutex *pX){
   16690   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16691   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   16692   p->cnt++;
   16693 }
   16694 static int debugMutexTry(sqlite3_mutex *pX){
   16695   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16696   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   16697   p->cnt++;
   16698   return SQLITE_OK;
   16699 }
   16700 
   16701 /*
   16702 ** The sqlite3_mutex_leave() routine exits a mutex that was
   16703 ** previously entered by the same thread.  The behavior
   16704 ** is undefined if the mutex is not currently entered or
   16705 ** is not currently allocated.  SQLite will never do either.
   16706 */
   16707 static void debugMutexLeave(sqlite3_mutex *pX){
   16708   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   16709   assert( debugMutexHeld(pX) );
   16710   p->cnt--;
   16711   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   16712 }
   16713 
   16714 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
   16715   static const sqlite3_mutex_methods sMutex = {
   16716     debugMutexInit,
   16717     debugMutexEnd,
   16718     debugMutexAlloc,
   16719     debugMutexFree,
   16720     debugMutexEnter,
   16721     debugMutexTry,
   16722     debugMutexLeave,
   16723 
   16724     debugMutexHeld,
   16725     debugMutexNotheld
   16726   };
   16727 
   16728   return &sMutex;
   16729 }
   16730 #endif /* SQLITE_DEBUG */
   16731 
   16732 /*
   16733 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
   16734 ** is used regardless of the run-time threadsafety setting.
   16735 */
   16736 #ifdef SQLITE_MUTEX_NOOP
   16737 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   16738   return sqlite3NoopMutex();
   16739 }
   16740 #endif /* SQLITE_MUTEX_NOOP */
   16741 #endif /* SQLITE_MUTEX_OMIT */
   16742 
   16743 /************** End of mutex_noop.c ******************************************/
   16744 /************** Begin file mutex_os2.c ***************************************/
   16745 /*
   16746 ** 2007 August 28
   16747 **
   16748 ** The author disclaims copyright to this source code.  In place of
   16749 ** a legal notice, here is a blessing:
   16750 **
   16751 **    May you do good and not evil.
   16752 **    May you find forgiveness for yourself and forgive others.
   16753 **    May you share freely, never taking more than you give.
   16754 **
   16755 *************************************************************************
   16756 ** This file contains the C functions that implement mutexes for OS/2
   16757 */
   16758 
   16759 /*
   16760 ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
   16761 ** See the mutex.h file for details.
   16762 */
   16763 #ifdef SQLITE_MUTEX_OS2
   16764 
   16765 /********************** OS/2 Mutex Implementation **********************
   16766 **
   16767 ** This implementation of mutexes is built using the OS/2 API.
   16768 */
   16769 
   16770 /*
   16771 ** The mutex object
   16772 ** Each recursive mutex is an instance of the following structure.
   16773 */
   16774 struct sqlite3_mutex {
   16775   HMTX mutex;       /* Mutex controlling the lock */
   16776   int  id;          /* Mutex type */
   16777 #ifdef SQLITE_DEBUG
   16778  int   trace;       /* True to trace changes */
   16779 #endif
   16780 };
   16781 
   16782 #ifdef SQLITE_DEBUG
   16783 #define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
   16784 #else
   16785 #define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
   16786 #endif
   16787 
   16788 /*
   16789 ** Initialize and deinitialize the mutex subsystem.
   16790 */
   16791 static int os2MutexInit(void){ return SQLITE_OK; }
   16792 static int os2MutexEnd(void){ return SQLITE_OK; }
   16793 
   16794 /*
   16795 ** The sqlite3_mutex_alloc() routine allocates a new
   16796 ** mutex and returns a pointer to it.  If it returns NULL
   16797 ** that means that a mutex could not be allocated.
   16798 ** SQLite will unwind its stack and return an error.  The argument
   16799 ** to sqlite3_mutex_alloc() is one of these integer constants:
   16800 **
   16801 ** <ul>
   16802 ** <li>  SQLITE_MUTEX_FAST
   16803 ** <li>  SQLITE_MUTEX_RECURSIVE
   16804 ** <li>  SQLITE_MUTEX_STATIC_MASTER
   16805 ** <li>  SQLITE_MUTEX_STATIC_MEM
   16806 ** <li>  SQLITE_MUTEX_STATIC_MEM2
   16807 ** <li>  SQLITE_MUTEX_STATIC_PRNG
   16808 ** <li>  SQLITE_MUTEX_STATIC_LRU
   16809 ** <li>  SQLITE_MUTEX_STATIC_LRU2
   16810 ** </ul>
   16811 **
   16812 ** The first two constants cause sqlite3_mutex_alloc() to create
   16813 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   16814 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   16815 ** The mutex implementation does not need to make a distinction
   16816 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   16817 ** not want to.  But SQLite will only request a recursive mutex in
   16818 ** cases where it really needs one.  If a faster non-recursive mutex
   16819 ** implementation is available on the host platform, the mutex subsystem
   16820 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   16821 **
   16822 ** The other allowed parameters to sqlite3_mutex_alloc() each return
   16823 ** a pointer to a static preexisting mutex.  Six static mutexes are
   16824 ** used by the current version of SQLite.  Future versions of SQLite
   16825 ** may add additional static mutexes.  Static mutexes are for internal
   16826 ** use by SQLite only.  Applications that use SQLite mutexes should
   16827 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   16828 ** SQLITE_MUTEX_RECURSIVE.
   16829 **
   16830 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   16831 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   16832 ** returns a different mutex on every call.  But for the static
   16833 ** mutex types, the same mutex is returned on every call that has
   16834 ** the same type number.
   16835 */
   16836 static sqlite3_mutex *os2MutexAlloc(int iType){
   16837   sqlite3_mutex *p = NULL;
   16838   switch( iType ){
   16839     case SQLITE_MUTEX_FAST:
   16840     case SQLITE_MUTEX_RECURSIVE: {
   16841       p = sqlite3MallocZero( sizeof(*p) );
   16842       if( p ){
   16843         p->id = iType;
   16844         if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
   16845           sqlite3_free( p );
   16846           p = NULL;
   16847         }
   16848       }
   16849       break;
   16850     }
   16851     default: {
   16852       static volatile int isInit = 0;
   16853       static sqlite3_mutex staticMutexes[6] = {
   16854         SQLITE3_MUTEX_INITIALIZER,
   16855         SQLITE3_MUTEX_INITIALIZER,
   16856         SQLITE3_MUTEX_INITIALIZER,
   16857         SQLITE3_MUTEX_INITIALIZER,
   16858         SQLITE3_MUTEX_INITIALIZER,
   16859         SQLITE3_MUTEX_INITIALIZER,
   16860       };
   16861       if ( !isInit ){
   16862         APIRET rc;
   16863         PTIB ptib;
   16864         PPIB ppib;
   16865         HMTX mutex;
   16866         char name[32];
   16867         DosGetInfoBlocks( &ptib, &ppib );
   16868         sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
   16869                           ppib->pib_ulpid );
   16870         while( !isInit ){
   16871           mutex = 0;
   16872           rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
   16873           if( rc == NO_ERROR ){
   16874             unsigned int i;
   16875             if( !isInit ){
   16876               for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
   16877                 DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
   16878               }
   16879               isInit = 1;
   16880             }
   16881             DosCloseMutexSem( mutex );
   16882           }else if( rc == ERROR_DUPLICATE_NAME ){
   16883             DosSleep( 1 );
   16884           }else{
   16885             return p;
   16886           }
   16887         }
   16888       }
   16889       assert( iType-2 >= 0 );
   16890       assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
   16891       p = &staticMutexes[iType-2];
   16892       p->id = iType;
   16893       break;
   16894     }
   16895   }
   16896   return p;
   16897 }
   16898 
   16899 
   16900 /*
   16901 ** This routine deallocates a previously allocated mutex.
   16902 ** SQLite is careful to deallocate every mutex that it allocates.
   16903 */
   16904 static void os2MutexFree(sqlite3_mutex *p){
   16905 #ifdef SQLITE_DEBUG
   16906   TID tid;
   16907   PID pid;
   16908   ULONG ulCount;
   16909   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   16910   assert( ulCount==0 );
   16911   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   16912 #endif
   16913   DosCloseMutexSem( p->mutex );
   16914   sqlite3_free( p );
   16915 }
   16916 
   16917 #ifdef SQLITE_DEBUG
   16918 /*
   16919 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   16920 ** intended for use inside assert() statements.
   16921 */
   16922 static int os2MutexHeld(sqlite3_mutex *p){
   16923   TID tid;
   16924   PID pid;
   16925   ULONG ulCount;
   16926   PTIB ptib;
   16927   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   16928   if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
   16929     return 0;
   16930   DosGetInfoBlocks(&ptib, NULL);
   16931   return tid==ptib->tib_ptib2->tib2_ultid;
   16932 }
   16933 static int os2MutexNotheld(sqlite3_mutex *p){
   16934   TID tid;
   16935   PID pid;
   16936   ULONG ulCount;
   16937   PTIB ptib;
   16938   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   16939   if( ulCount==0 )
   16940     return 1;
   16941   DosGetInfoBlocks(&ptib, NULL);
   16942   return tid!=ptib->tib_ptib2->tib2_ultid;
   16943 }
   16944 static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
   16945   TID   tid;
   16946   PID   pid;
   16947   ULONG ulCount;
   16948   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   16949   printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
   16950 }
   16951 #endif
   16952 
   16953 /*
   16954 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   16955 ** to enter a mutex.  If another thread is already within the mutex,
   16956 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   16957 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   16958 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   16959 ** be entered multiple times by the same thread.  In such cases the,
   16960 ** mutex must be exited an equal number of times before another thread
   16961 ** can enter.  If the same thread tries to enter any other kind of mutex
   16962 ** more than once, the behavior is undefined.
   16963 */
   16964 static void os2MutexEnter(sqlite3_mutex *p){
   16965   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   16966   DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
   16967 #ifdef SQLITE_DEBUG
   16968   if( p->trace ) os2MutexTrace(p, "enter");
   16969 #endif
   16970 }
   16971 static int os2MutexTry(sqlite3_mutex *p){
   16972   int rc = SQLITE_BUSY;
   16973   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   16974   if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
   16975     rc = SQLITE_OK;
   16976 #ifdef SQLITE_DEBUG
   16977     if( p->trace ) os2MutexTrace(p, "try");
   16978 #endif
   16979   }
   16980   return rc;
   16981 }
   16982 
   16983 /*
   16984 ** The sqlite3_mutex_leave() routine exits a mutex that was
   16985 ** previously entered by the same thread.  The behavior
   16986 ** is undefined if the mutex is not currently entered or
   16987 ** is not currently allocated.  SQLite will never do either.
   16988 */
   16989 static void os2MutexLeave(sqlite3_mutex *p){
   16990   assert( os2MutexHeld(p) );
   16991   DosReleaseMutexSem(p->mutex);
   16992 #ifdef SQLITE_DEBUG
   16993   if( p->trace ) os2MutexTrace(p, "leave");
   16994 #endif
   16995 }
   16996 
   16997 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   16998   static const sqlite3_mutex_methods sMutex = {
   16999     os2MutexInit,
   17000     os2MutexEnd,
   17001     os2MutexAlloc,
   17002     os2MutexFree,
   17003     os2MutexEnter,
   17004     os2MutexTry,
   17005     os2MutexLeave,
   17006 #ifdef SQLITE_DEBUG
   17007     os2MutexHeld,
   17008     os2MutexNotheld
   17009 #else
   17010     0,
   17011     0
   17012 #endif
   17013   };
   17014 
   17015   return &sMutex;
   17016 }
   17017 #endif /* SQLITE_MUTEX_OS2 */
   17018 
   17019 /************** End of mutex_os2.c *******************************************/
   17020 /************** Begin file mutex_unix.c **************************************/
   17021 /*
   17022 ** 2007 August 28
   17023 **
   17024 ** The author disclaims copyright to this source code.  In place of
   17025 ** a legal notice, here is a blessing:
   17026 **
   17027 **    May you do good and not evil.
   17028 **    May you find forgiveness for yourself and forgive others.
   17029 **    May you share freely, never taking more than you give.
   17030 **
   17031 *************************************************************************
   17032 ** This file contains the C functions that implement mutexes for pthreads
   17033 */
   17034 
   17035 /*
   17036 ** The code in this file is only used if we are compiling threadsafe
   17037 ** under unix with pthreads.
   17038 **
   17039 ** Note that this implementation requires a version of pthreads that
   17040 ** supports recursive mutexes.
   17041 */
   17042 #ifdef SQLITE_MUTEX_PTHREADS
   17043 
   17044 #include <pthread.h>
   17045 
   17046 /*
   17047 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
   17048 ** are necessary under two condidtions:  (1) Debug builds and (2) using
   17049 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
   17050 */
   17051 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
   17052 # define SQLITE_MUTEX_NREF 1
   17053 #else
   17054 # define SQLITE_MUTEX_NREF 0
   17055 #endif
   17056 
   17057 /*
   17058 ** Each recursive mutex is an instance of the following structure.
   17059 */
   17060 struct sqlite3_mutex {
   17061   pthread_mutex_t mutex;     /* Mutex controlling the lock */
   17062 #if SQLITE_MUTEX_NREF
   17063   int id;                    /* Mutex type */
   17064   volatile int nRef;         /* Number of entrances */
   17065   volatile pthread_t owner;  /* Thread that is within this mutex */
   17066   int trace;                 /* True to trace changes */
   17067 #endif
   17068 };
   17069 #if SQLITE_MUTEX_NREF
   17070 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
   17071 #else
   17072 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
   17073 #endif
   17074 
   17075 /*
   17076 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17077 ** intended for use only inside assert() statements.  On some platforms,
   17078 ** there might be race conditions that can cause these routines to
   17079 ** deliver incorrect results.  In particular, if pthread_equal() is
   17080 ** not an atomic operation, then these routines might delivery
   17081 ** incorrect results.  On most platforms, pthread_equal() is a
   17082 ** comparison of two integers and is therefore atomic.  But we are
   17083 ** told that HPUX is not such a platform.  If so, then these routines
   17084 ** will not always work correctly on HPUX.
   17085 **
   17086 ** On those platforms where pthread_equal() is not atomic, SQLite
   17087 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
   17088 ** make sure no assert() statements are evaluated and hence these
   17089 ** routines are never called.
   17090 */
   17091 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
   17092 static int pthreadMutexHeld(sqlite3_mutex *p){
   17093   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
   17094 }
   17095 static int pthreadMutexNotheld(sqlite3_mutex *p){
   17096   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
   17097 }
   17098 #endif
   17099 
   17100 /*
   17101 ** Initialize and deinitialize the mutex subsystem.
   17102 */
   17103 static int pthreadMutexInit(void){ return SQLITE_OK; }
   17104 static int pthreadMutexEnd(void){ return SQLITE_OK; }
   17105 
   17106 /*
   17107 ** The sqlite3_mutex_alloc() routine allocates a new
   17108 ** mutex and returns a pointer to it.  If it returns NULL
   17109 ** that means that a mutex could not be allocated.  SQLite
   17110 ** will unwind its stack and return an error.  The argument
   17111 ** to sqlite3_mutex_alloc() is one of these integer constants:
   17112 **
   17113 ** <ul>
   17114 ** <li>  SQLITE_MUTEX_FAST
   17115 ** <li>  SQLITE_MUTEX_RECURSIVE
   17116 ** <li>  SQLITE_MUTEX_STATIC_MASTER
   17117 ** <li>  SQLITE_MUTEX_STATIC_MEM
   17118 ** <li>  SQLITE_MUTEX_STATIC_MEM2
   17119 ** <li>  SQLITE_MUTEX_STATIC_PRNG
   17120 ** <li>  SQLITE_MUTEX_STATIC_LRU
   17121 ** <li>  SQLITE_MUTEX_STATIC_PMEM
   17122 ** </ul>
   17123 **
   17124 ** The first two constants cause sqlite3_mutex_alloc() to create
   17125 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   17126 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   17127 ** The mutex implementation does not need to make a distinction
   17128 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   17129 ** not want to.  But SQLite will only request a recursive mutex in
   17130 ** cases where it really needs one.  If a faster non-recursive mutex
   17131 ** implementation is available on the host platform, the mutex subsystem
   17132 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   17133 **
   17134 ** The other allowed parameters to sqlite3_mutex_alloc() each return
   17135 ** a pointer to a static preexisting mutex.  Six static mutexes are
   17136 ** used by the current version of SQLite.  Future versions of SQLite
   17137 ** may add additional static mutexes.  Static mutexes are for internal
   17138 ** use by SQLite only.  Applications that use SQLite mutexes should
   17139 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   17140 ** SQLITE_MUTEX_RECURSIVE.
   17141 **
   17142 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   17143 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   17144 ** returns a different mutex on every call.  But for the static
   17145 ** mutex types, the same mutex is returned on every call that has
   17146 ** the same type number.
   17147 */
   17148 static sqlite3_mutex *pthreadMutexAlloc(int iType){
   17149   static sqlite3_mutex staticMutexes[] = {
   17150     SQLITE3_MUTEX_INITIALIZER,
   17151     SQLITE3_MUTEX_INITIALIZER,
   17152     SQLITE3_MUTEX_INITIALIZER,
   17153     SQLITE3_MUTEX_INITIALIZER,
   17154     SQLITE3_MUTEX_INITIALIZER,
   17155     SQLITE3_MUTEX_INITIALIZER
   17156   };
   17157   sqlite3_mutex *p;
   17158   switch( iType ){
   17159     case SQLITE_MUTEX_RECURSIVE: {
   17160       p = sqlite3MallocZero( sizeof(*p) );
   17161       if( p ){
   17162 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   17163         /* If recursive mutexes are not available, we will have to
   17164         ** build our own.  See below. */
   17165         pthread_mutex_init(&p->mutex, 0);
   17166 #else
   17167         /* Use a recursive mutex if it is available */
   17168         pthread_mutexattr_t recursiveAttr;
   17169         pthread_mutexattr_init(&recursiveAttr);
   17170         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
   17171         pthread_mutex_init(&p->mutex, &recursiveAttr);
   17172         pthread_mutexattr_destroy(&recursiveAttr);
   17173 #endif
   17174 #if SQLITE_MUTEX_NREF
   17175         p->id = iType;
   17176 #endif
   17177       }
   17178       break;
   17179     }
   17180     case SQLITE_MUTEX_FAST: {
   17181       p = sqlite3MallocZero( sizeof(*p) );
   17182       if( p ){
   17183 #if SQLITE_MUTEX_NREF
   17184         p->id = iType;
   17185 #endif
   17186         pthread_mutex_init(&p->mutex, 0);
   17187       }
   17188       break;
   17189     }
   17190     default: {
   17191       assert( iType-2 >= 0 );
   17192       assert( iType-2 < ArraySize(staticMutexes) );
   17193       p = &staticMutexes[iType-2];
   17194 #if SQLITE_MUTEX_NREF
   17195       p->id = iType;
   17196 #endif
   17197       break;
   17198     }
   17199   }
   17200   return p;
   17201 }
   17202 
   17203 
   17204 /*
   17205 ** This routine deallocates a previously
   17206 ** allocated mutex.  SQLite is careful to deallocate every
   17207 ** mutex that it allocates.
   17208 */
   17209 static void pthreadMutexFree(sqlite3_mutex *p){
   17210   assert( p->nRef==0 );
   17211   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   17212   pthread_mutex_destroy(&p->mutex);
   17213   sqlite3_free(p);
   17214 }
   17215 
   17216 /*
   17217 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   17218 ** to enter a mutex.  If another thread is already within the mutex,
   17219 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   17220 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   17221 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   17222 ** be entered multiple times by the same thread.  In such cases the,
   17223 ** mutex must be exited an equal number of times before another thread
   17224 ** can enter.  If the same thread tries to enter any other kind of mutex
   17225 ** more than once, the behavior is undefined.
   17226 */
   17227 static void pthreadMutexEnter(sqlite3_mutex *p){
   17228   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
   17229 
   17230 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   17231   /* If recursive mutexes are not available, then we have to grow
   17232   ** our own.  This implementation assumes that pthread_equal()
   17233   ** is atomic - that it cannot be deceived into thinking self
   17234   ** and p->owner are equal if p->owner changes between two values
   17235   ** that are not equal to self while the comparison is taking place.
   17236   ** This implementation also assumes a coherent cache - that
   17237   ** separate processes cannot read different values from the same
   17238   ** address at the same time.  If either of these two conditions
   17239   ** are not met, then the mutexes will fail and problems will result.
   17240   */
   17241   {
   17242     pthread_t self = pthread_self();
   17243     if( p->nRef>0 && pthread_equal(p->owner, self) ){
   17244       p->nRef++;
   17245     }else{
   17246       pthread_mutex_lock(&p->mutex);
   17247       assert( p->nRef==0 );
   17248       p->owner = self;
   17249       p->nRef = 1;
   17250     }
   17251   }
   17252 #else
   17253   /* Use the built-in recursive mutexes if they are available.
   17254   */
   17255   pthread_mutex_lock(&p->mutex);
   17256 #if SQLITE_MUTEX_NREF
   17257   assert( p->nRef>0 || p->owner==0 );
   17258   p->owner = pthread_self();
   17259   p->nRef++;
   17260 #endif
   17261 #endif
   17262 
   17263 #ifdef SQLITE_DEBUG
   17264   if( p->trace ){
   17265     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17266   }
   17267 #endif
   17268 }
   17269 static int pthreadMutexTry(sqlite3_mutex *p){
   17270   int rc;
   17271   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
   17272 
   17273 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   17274   /* If recursive mutexes are not available, then we have to grow
   17275   ** our own.  This implementation assumes that pthread_equal()
   17276   ** is atomic - that it cannot be deceived into thinking self
   17277   ** and p->owner are equal if p->owner changes between two values
   17278   ** that are not equal to self while the comparison is taking place.
   17279   ** This implementation also assumes a coherent cache - that
   17280   ** separate processes cannot read different values from the same
   17281   ** address at the same time.  If either of these two conditions
   17282   ** are not met, then the mutexes will fail and problems will result.
   17283   */
   17284   {
   17285     pthread_t self = pthread_self();
   17286     if( p->nRef>0 && pthread_equal(p->owner, self) ){
   17287       p->nRef++;
   17288       rc = SQLITE_OK;
   17289     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
   17290       assert( p->nRef==0 );
   17291       p->owner = self;
   17292       p->nRef = 1;
   17293       rc = SQLITE_OK;
   17294     }else{
   17295       rc = SQLITE_BUSY;
   17296     }
   17297   }
   17298 #else
   17299   /* Use the built-in recursive mutexes if they are available.
   17300   */
   17301   if( pthread_mutex_trylock(&p->mutex)==0 ){
   17302 #if SQLITE_MUTEX_NREF
   17303     p->owner = pthread_self();
   17304     p->nRef++;
   17305 #endif
   17306     rc = SQLITE_OK;
   17307   }else{
   17308     rc = SQLITE_BUSY;
   17309   }
   17310 #endif
   17311 
   17312 #ifdef SQLITE_DEBUG
   17313   if( rc==SQLITE_OK && p->trace ){
   17314     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17315   }
   17316 #endif
   17317   return rc;
   17318 }
   17319 
   17320 /*
   17321 ** The sqlite3_mutex_leave() routine exits a mutex that was
   17322 ** previously entered by the same thread.  The behavior
   17323 ** is undefined if the mutex is not currently entered or
   17324 ** is not currently allocated.  SQLite will never do either.
   17325 */
   17326 static void pthreadMutexLeave(sqlite3_mutex *p){
   17327   assert( pthreadMutexHeld(p) );
   17328 #if SQLITE_MUTEX_NREF
   17329   p->nRef--;
   17330   if( p->nRef==0 ) p->owner = 0;
   17331 #endif
   17332   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   17333 
   17334 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   17335   if( p->nRef==0 ){
   17336     pthread_mutex_unlock(&p->mutex);
   17337   }
   17338 #else
   17339   pthread_mutex_unlock(&p->mutex);
   17340 #endif
   17341 
   17342 #ifdef SQLITE_DEBUG
   17343   if( p->trace ){
   17344     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17345   }
   17346 #endif
   17347 }
   17348 
   17349 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   17350   static const sqlite3_mutex_methods sMutex = {
   17351     pthreadMutexInit,
   17352     pthreadMutexEnd,
   17353     pthreadMutexAlloc,
   17354     pthreadMutexFree,
   17355     pthreadMutexEnter,
   17356     pthreadMutexTry,
   17357     pthreadMutexLeave,
   17358 #ifdef SQLITE_DEBUG
   17359     pthreadMutexHeld,
   17360     pthreadMutexNotheld
   17361 #else
   17362     0,
   17363     0
   17364 #endif
   17365   };
   17366 
   17367   return &sMutex;
   17368 }
   17369 
   17370 #endif /* SQLITE_MUTEX_PTHREAD */
   17371 
   17372 /************** End of mutex_unix.c ******************************************/
   17373 /************** Begin file mutex_w32.c ***************************************/
   17374 /*
   17375 ** 2007 August 14
   17376 **
   17377 ** The author disclaims copyright to this source code.  In place of
   17378 ** a legal notice, here is a blessing:
   17379 **
   17380 **    May you do good and not evil.
   17381 **    May you find forgiveness for yourself and forgive others.
   17382 **    May you share freely, never taking more than you give.
   17383 **
   17384 *************************************************************************
   17385 ** This file contains the C functions that implement mutexes for win32
   17386 */
   17387 
   17388 /*
   17389 ** The code in this file is only used if we are compiling multithreaded
   17390 ** on a win32 system.
   17391 */
   17392 #ifdef SQLITE_MUTEX_W32
   17393 
   17394 /*
   17395 ** Each recursive mutex is an instance of the following structure.
   17396 */
   17397 struct sqlite3_mutex {
   17398   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
   17399   int id;                    /* Mutex type */
   17400 #ifdef SQLITE_DEBUG
   17401   volatile int nRef;         /* Number of enterances */
   17402   volatile DWORD owner;      /* Thread holding this mutex */
   17403   int trace;                 /* True to trace changes */
   17404 #endif
   17405 };
   17406 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
   17407 #ifdef SQLITE_DEBUG
   17408 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
   17409 #else
   17410 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
   17411 #endif
   17412 
   17413 /*
   17414 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
   17415 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
   17416 **
   17417 ** Here is an interesting observation:  Win95, Win98, and WinME lack
   17418 ** the LockFileEx() API.  But we can still statically link against that
   17419 ** API as long as we don't call it win running Win95/98/ME.  A call to
   17420 ** this routine is used to determine if the host is Win95/98/ME or
   17421 ** WinNT/2K/XP so that we will know whether or not we can safely call
   17422 ** the LockFileEx() API.
   17423 **
   17424 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
   17425 ** which is only available if your application was compiled with
   17426 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
   17427 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
   17428 ** this out as well.
   17429 */
   17430 #if 0
   17431 #if SQLITE_OS_WINCE
   17432 # define mutexIsNT()  (1)
   17433 #else
   17434   static int mutexIsNT(void){
   17435     static int osType = 0;
   17436     if( osType==0 ){
   17437       OSVERSIONINFO sInfo;
   17438       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
   17439       GetVersionEx(&sInfo);
   17440       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   17441     }
   17442     return osType==2;
   17443   }
   17444 #endif /* SQLITE_OS_WINCE */
   17445 #endif
   17446 
   17447 #ifdef SQLITE_DEBUG
   17448 /*
   17449 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17450 ** intended for use only inside assert() statements.
   17451 */
   17452 static int winMutexHeld(sqlite3_mutex *p){
   17453   return p->nRef!=0 && p->owner==GetCurrentThreadId();
   17454 }
   17455 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
   17456   return p->nRef==0 || p->owner!=tid;
   17457 }
   17458 static int winMutexNotheld(sqlite3_mutex *p){
   17459   DWORD tid = GetCurrentThreadId();
   17460   return winMutexNotheld2(p, tid);
   17461 }
   17462 #endif
   17463 
   17464 
   17465 /*
   17466 ** Initialize and deinitialize the mutex subsystem.
   17467 */
   17468 static sqlite3_mutex winMutex_staticMutexes[6] = {
   17469   SQLITE3_MUTEX_INITIALIZER,
   17470   SQLITE3_MUTEX_INITIALIZER,
   17471   SQLITE3_MUTEX_INITIALIZER,
   17472   SQLITE3_MUTEX_INITIALIZER,
   17473   SQLITE3_MUTEX_INITIALIZER,
   17474   SQLITE3_MUTEX_INITIALIZER
   17475 };
   17476 static int winMutex_isInit = 0;
   17477 /* As winMutexInit() and winMutexEnd() are called as part
   17478 ** of the sqlite3_initialize and sqlite3_shutdown()
   17479 ** processing, the "interlocked" magic is probably not
   17480 ** strictly necessary.
   17481 */
   17482 static long winMutex_lock = 0;
   17483 
   17484 static int winMutexInit(void){
   17485   /* The first to increment to 1 does actual initialization */
   17486   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
   17487     int i;
   17488     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   17489       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
   17490     }
   17491     winMutex_isInit = 1;
   17492   }else{
   17493     /* Someone else is in the process of initing the static mutexes */
   17494     while( !winMutex_isInit ){
   17495       Sleep(1);
   17496     }
   17497   }
   17498   return SQLITE_OK;
   17499 }
   17500 
   17501 static int winMutexEnd(void){
   17502   /* The first to decrement to 0 does actual shutdown
   17503   ** (which should be the last to shutdown.) */
   17504   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
   17505     if( winMutex_isInit==1 ){
   17506       int i;
   17507       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   17508         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
   17509       }
   17510       winMutex_isInit = 0;
   17511     }
   17512   }
   17513   return SQLITE_OK;
   17514 }
   17515 
   17516 /*
   17517 ** The sqlite3_mutex_alloc() routine allocates a new
   17518 ** mutex and returns a pointer to it.  If it returns NULL
   17519 ** that means that a mutex could not be allocated.  SQLite
   17520 ** will unwind its stack and return an error.  The argument
   17521 ** to sqlite3_mutex_alloc() is one of these integer constants:
   17522 **
   17523 ** <ul>
   17524 ** <li>  SQLITE_MUTEX_FAST
   17525 ** <li>  SQLITE_MUTEX_RECURSIVE
   17526 ** <li>  SQLITE_MUTEX_STATIC_MASTER
   17527 ** <li>  SQLITE_MUTEX_STATIC_MEM
   17528 ** <li>  SQLITE_MUTEX_STATIC_MEM2
   17529 ** <li>  SQLITE_MUTEX_STATIC_PRNG
   17530 ** <li>  SQLITE_MUTEX_STATIC_LRU
   17531 ** <li>  SQLITE_MUTEX_STATIC_PMEM
   17532 ** </ul>
   17533 **
   17534 ** The first two constants cause sqlite3_mutex_alloc() to create
   17535 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   17536 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   17537 ** The mutex implementation does not need to make a distinction
   17538 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   17539 ** not want to.  But SQLite will only request a recursive mutex in
   17540 ** cases where it really needs one.  If a faster non-recursive mutex
   17541 ** implementation is available on the host platform, the mutex subsystem
   17542 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   17543 **
   17544 ** The other allowed parameters to sqlite3_mutex_alloc() each return
   17545 ** a pointer to a static preexisting mutex.  Six static mutexes are
   17546 ** used by the current version of SQLite.  Future versions of SQLite
   17547 ** may add additional static mutexes.  Static mutexes are for internal
   17548 ** use by SQLite only.  Applications that use SQLite mutexes should
   17549 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   17550 ** SQLITE_MUTEX_RECURSIVE.
   17551 **
   17552 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   17553 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   17554 ** returns a different mutex on every call.  But for the static
   17555 ** mutex types, the same mutex is returned on every call that has
   17556 ** the same type number.
   17557 */
   17558 static sqlite3_mutex *winMutexAlloc(int iType){
   17559   sqlite3_mutex *p;
   17560 
   17561   switch( iType ){
   17562     case SQLITE_MUTEX_FAST:
   17563     case SQLITE_MUTEX_RECURSIVE: {
   17564       p = sqlite3MallocZero( sizeof(*p) );
   17565       if( p ){
   17566 #ifdef SQLITE_DEBUG
   17567         p->id = iType;
   17568 #endif
   17569         InitializeCriticalSection(&p->mutex);
   17570       }
   17571       break;
   17572     }
   17573     default: {
   17574       assert( winMutex_isInit==1 );
   17575       assert( iType-2 >= 0 );
   17576       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
   17577       p = &winMutex_staticMutexes[iType-2];
   17578 #ifdef SQLITE_DEBUG
   17579       p->id = iType;
   17580 #endif
   17581       break;
   17582     }
   17583   }
   17584   return p;
   17585 }
   17586 
   17587 
   17588 /*
   17589 ** This routine deallocates a previously
   17590 ** allocated mutex.  SQLite is careful to deallocate every
   17591 ** mutex that it allocates.
   17592 */
   17593 static void winMutexFree(sqlite3_mutex *p){
   17594   assert( p );
   17595   assert( p->nRef==0 && p->owner==0 );
   17596   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   17597   DeleteCriticalSection(&p->mutex);
   17598   sqlite3_free(p);
   17599 }
   17600 
   17601 /*
   17602 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   17603 ** to enter a mutex.  If another thread is already within the mutex,
   17604 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   17605 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   17606 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   17607 ** be entered multiple times by the same thread.  In such cases the,
   17608 ** mutex must be exited an equal number of times before another thread
   17609 ** can enter.  If the same thread tries to enter any other kind of mutex
   17610 ** more than once, the behavior is undefined.
   17611 */
   17612 static void winMutexEnter(sqlite3_mutex *p){
   17613 #ifdef SQLITE_DEBUG
   17614   DWORD tid = GetCurrentThreadId();
   17615   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   17616 #endif
   17617   EnterCriticalSection(&p->mutex);
   17618 #ifdef SQLITE_DEBUG
   17619   assert( p->nRef>0 || p->owner==0 );
   17620   p->owner = tid;
   17621   p->nRef++;
   17622   if( p->trace ){
   17623     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17624   }
   17625 #endif
   17626 }
   17627 static int winMutexTry(sqlite3_mutex *p){
   17628 #ifndef NDEBUG
   17629   DWORD tid = GetCurrentThreadId();
   17630 #endif
   17631   int rc = SQLITE_BUSY;
   17632   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   17633   /*
   17634   ** The sqlite3_mutex_try() routine is very rarely used, and when it
   17635   ** is used it is merely an optimization.  So it is OK for it to always
   17636   ** fail.
   17637   **
   17638   ** The TryEnterCriticalSection() interface is only available on WinNT.
   17639   ** And some windows compilers complain if you try to use it without
   17640   ** first doing some #defines that prevent SQLite from building on Win98.
   17641   ** For that reason, we will omit this optimization for now.  See
   17642   ** ticket #2685.
   17643   */
   17644 #if 0
   17645   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
   17646     p->owner = tid;
   17647     p->nRef++;
   17648     rc = SQLITE_OK;
   17649   }
   17650 #else
   17651   UNUSED_PARAMETER(p);
   17652 #endif
   17653 #ifdef SQLITE_DEBUG
   17654   if( rc==SQLITE_OK && p->trace ){
   17655     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17656   }
   17657 #endif
   17658   return rc;
   17659 }
   17660 
   17661 /*
   17662 ** The sqlite3_mutex_leave() routine exits a mutex that was
   17663 ** previously entered by the same thread.  The behavior
   17664 ** is undefined if the mutex is not currently entered or
   17665 ** is not currently allocated.  SQLite will never do either.
   17666 */
   17667 static void winMutexLeave(sqlite3_mutex *p){
   17668 #ifndef NDEBUG
   17669   DWORD tid = GetCurrentThreadId();
   17670   assert( p->nRef>0 );
   17671   assert( p->owner==tid );
   17672   p->nRef--;
   17673   if( p->nRef==0 ) p->owner = 0;
   17674   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   17675 #endif
   17676   LeaveCriticalSection(&p->mutex);
   17677 #ifdef SQLITE_DEBUG
   17678   if( p->trace ){
   17679     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   17680   }
   17681 #endif
   17682 }
   17683 
   17684 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   17685   static const sqlite3_mutex_methods sMutex = {
   17686     winMutexInit,
   17687     winMutexEnd,
   17688     winMutexAlloc,
   17689     winMutexFree,
   17690     winMutexEnter,
   17691     winMutexTry,
   17692     winMutexLeave,
   17693 #ifdef SQLITE_DEBUG
   17694     winMutexHeld,
   17695     winMutexNotheld
   17696 #else
   17697     0,
   17698     0
   17699 #endif
   17700   };
   17701 
   17702   return &sMutex;
   17703 }
   17704 #endif /* SQLITE_MUTEX_W32 */
   17705 
   17706 /************** End of mutex_w32.c *******************************************/
   17707 /************** Begin file malloc.c ******************************************/
   17708 /*
   17709 ** 2001 September 15
   17710 **
   17711 ** The author disclaims copyright to this source code.  In place of
   17712 ** a legal notice, here is a blessing:
   17713 **
   17714 **    May you do good and not evil.
   17715 **    May you find forgiveness for yourself and forgive others.
   17716 **    May you share freely, never taking more than you give.
   17717 **
   17718 *************************************************************************
   17719 **
   17720 ** Memory allocation functions used throughout sqlite.
   17721 */
   17722 
   17723 /*
   17724 ** Attempt to release up to n bytes of non-essential memory currently
   17725 ** held by SQLite. An example of non-essential memory is memory used to
   17726 ** cache database pages that are not currently in use.
   17727 */
   17728 SQLITE_API int sqlite3_release_memory(int n){
   17729 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   17730   return sqlite3PcacheReleaseMemory(n);
   17731 #else
   17732   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
   17733   ** is a no-op returning zero if SQLite is not compiled with
   17734   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
   17735   UNUSED_PARAMETER(n);
   17736   return 0;
   17737 #endif
   17738 }
   17739 
   17740 /*
   17741 ** An instance of the following object records the location of
   17742 ** each unused scratch buffer.
   17743 */
   17744 typedef struct ScratchFreeslot {
   17745   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
   17746 } ScratchFreeslot;
   17747 
   17748 /*
   17749 ** State information local to the memory allocation subsystem.
   17750 */
   17751 static SQLITE_WSD struct Mem0Global {
   17752   sqlite3_mutex *mutex;         /* Mutex to serialize access */
   17753 
   17754   /*
   17755   ** The alarm callback and its arguments.  The mem0.mutex lock will
   17756   ** be held while the callback is running.  Recursive calls into
   17757   ** the memory subsystem are allowed, but no new callbacks will be
   17758   ** issued.
   17759   */
   17760   sqlite3_int64 alarmThreshold;
   17761   void (*alarmCallback)(void*, sqlite3_int64,int);
   17762   void *alarmArg;
   17763 
   17764   /*
   17765   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
   17766   ** (so that a range test can be used to determine if an allocation
   17767   ** being freed came from pScratch) and a pointer to the list of
   17768   ** unused scratch allocations.
   17769   */
   17770   void *pScratchEnd;
   17771   ScratchFreeslot *pScratchFree;
   17772   u32 nScratchFree;
   17773 
   17774   /*
   17775   ** True if heap is nearly "full" where "full" is defined by the
   17776   ** sqlite3_soft_heap_limit() setting.
   17777   */
   17778   int nearlyFull;
   17779 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
   17780 
   17781 #define mem0 GLOBAL(struct Mem0Global, mem0)
   17782 
   17783 /*
   17784 ** This routine runs when the memory allocator sees that the
   17785 ** total memory allocation is about to exceed the soft heap
   17786 ** limit.
   17787 */
   17788 static void softHeapLimitEnforcer(
   17789   void *NotUsed,
   17790   sqlite3_int64 NotUsed2,
   17791   int allocSize
   17792 ){
   17793   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   17794   sqlite3_release_memory(allocSize);
   17795 }
   17796 
   17797 /*
   17798 ** Change the alarm callback
   17799 */
   17800 static int sqlite3MemoryAlarm(
   17801   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
   17802   void *pArg,
   17803   sqlite3_int64 iThreshold
   17804 ){
   17805   int nUsed;
   17806   sqlite3_mutex_enter(mem0.mutex);
   17807   mem0.alarmCallback = xCallback;
   17808   mem0.alarmArg = pArg;
   17809   mem0.alarmThreshold = iThreshold;
   17810   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   17811   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
   17812   sqlite3_mutex_leave(mem0.mutex);
   17813   return SQLITE_OK;
   17814 }
   17815 
   17816 #ifndef SQLITE_OMIT_DEPRECATED
   17817 /*
   17818 ** Deprecated external interface.  Internal/core SQLite code
   17819 ** should call sqlite3MemoryAlarm.
   17820 */
   17821 SQLITE_API int sqlite3_memory_alarm(
   17822   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
   17823   void *pArg,
   17824   sqlite3_int64 iThreshold
   17825 ){
   17826   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
   17827 }
   17828 #endif
   17829 
   17830 /*
   17831 ** Set the soft heap-size limit for the library. Passing a zero or
   17832 ** negative value indicates no limit.
   17833 */
   17834 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
   17835   sqlite3_int64 priorLimit;
   17836   sqlite3_int64 excess;
   17837 #ifndef SQLITE_OMIT_AUTOINIT
   17838   sqlite3_initialize();
   17839 #endif
   17840   sqlite3_mutex_enter(mem0.mutex);
   17841   priorLimit = mem0.alarmThreshold;
   17842   sqlite3_mutex_leave(mem0.mutex);
   17843   if( n<0 ) return priorLimit;
   17844   if( n>0 ){
   17845     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
   17846   }else{
   17847     sqlite3MemoryAlarm(0, 0, 0);
   17848   }
   17849   excess = sqlite3_memory_used() - n;
   17850   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
   17851   return priorLimit;
   17852 }
   17853 SQLITE_API void sqlite3_soft_heap_limit(int n){
   17854   if( n<0 ) n = 0;
   17855   sqlite3_soft_heap_limit64(n);
   17856 }
   17857 
   17858 /*
   17859 ** Initialize the memory allocation subsystem.
   17860 */
   17861 SQLITE_PRIVATE int sqlite3MallocInit(void){
   17862   if( sqlite3GlobalConfig.m.xMalloc==0 ){
   17863     sqlite3MemSetDefault();
   17864   }
   17865   memset(&mem0, 0, sizeof(mem0));
   17866   if( sqlite3GlobalConfig.bCoreMutex ){
   17867     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   17868   }
   17869   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
   17870       && sqlite3GlobalConfig.nScratch>0 ){
   17871     int i, n, sz;
   17872     ScratchFreeslot *pSlot;
   17873     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
   17874     sqlite3GlobalConfig.szScratch = sz;
   17875     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
   17876     n = sqlite3GlobalConfig.nScratch;
   17877     mem0.pScratchFree = pSlot;
   17878     mem0.nScratchFree = n;
   17879     for(i=0; i<n-1; i++){
   17880       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
   17881       pSlot = pSlot->pNext;
   17882     }
   17883     pSlot->pNext = 0;
   17884     mem0.pScratchEnd = (void*)&pSlot[1];
   17885   }else{
   17886     mem0.pScratchEnd = 0;
   17887     sqlite3GlobalConfig.pScratch = 0;
   17888     sqlite3GlobalConfig.szScratch = 0;
   17889     sqlite3GlobalConfig.nScratch = 0;
   17890   }
   17891   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
   17892       || sqlite3GlobalConfig.nPage<1 ){
   17893     sqlite3GlobalConfig.pPage = 0;
   17894     sqlite3GlobalConfig.szPage = 0;
   17895     sqlite3GlobalConfig.nPage = 0;
   17896   }
   17897   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
   17898 }
   17899 
   17900 /*
   17901 ** Return true if the heap is currently under memory pressure - in other
   17902 ** words if the amount of heap used is close to the limit set by
   17903 ** sqlite3_soft_heap_limit().
   17904 */
   17905 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
   17906   return mem0.nearlyFull;
   17907 }
   17908 
   17909 /*
   17910 ** Deinitialize the memory allocation subsystem.
   17911 */
   17912 SQLITE_PRIVATE void sqlite3MallocEnd(void){
   17913   if( sqlite3GlobalConfig.m.xShutdown ){
   17914     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
   17915   }
   17916   memset(&mem0, 0, sizeof(mem0));
   17917 }
   17918 
   17919 /*
   17920 ** Return the amount of memory currently checked out.
   17921 */
   17922 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
   17923   int n, mx;
   17924   sqlite3_int64 res;
   17925   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
   17926   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
   17927   return res;
   17928 }
   17929 
   17930 /*
   17931 ** Return the maximum amount of memory that has ever been
   17932 ** checked out since either the beginning of this process
   17933 ** or since the most recent reset.
   17934 */
   17935 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
   17936   int n, mx;
   17937   sqlite3_int64 res;
   17938   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
   17939   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
   17940   return res;
   17941 }
   17942 
   17943 /*
   17944 ** Trigger the alarm
   17945 */
   17946 static void sqlite3MallocAlarm(int nByte){
   17947   void (*xCallback)(void*,sqlite3_int64,int);
   17948   sqlite3_int64 nowUsed;
   17949   void *pArg;
   17950   if( mem0.alarmCallback==0 ) return;
   17951   xCallback = mem0.alarmCallback;
   17952   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   17953   pArg = mem0.alarmArg;
   17954   mem0.alarmCallback = 0;
   17955   sqlite3_mutex_leave(mem0.mutex);
   17956   xCallback(pArg, nowUsed, nByte);
   17957   sqlite3_mutex_enter(mem0.mutex);
   17958   mem0.alarmCallback = xCallback;
   17959   mem0.alarmArg = pArg;
   17960 }
   17961 
   17962 /*
   17963 ** Do a memory allocation with statistics and alarms.  Assume the
   17964 ** lock is already held.
   17965 */
   17966 static int mallocWithAlarm(int n, void **pp){
   17967   int nFull;
   17968   void *p;
   17969   assert( sqlite3_mutex_held(mem0.mutex) );
   17970   nFull = sqlite3GlobalConfig.m.xRoundup(n);
   17971   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
   17972   if( mem0.alarmCallback!=0 ){
   17973     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   17974     if( nUsed+nFull >= mem0.alarmThreshold ){
   17975       mem0.nearlyFull = 1;
   17976       sqlite3MallocAlarm(nFull);
   17977     }else{
   17978       mem0.nearlyFull = 0;
   17979     }
   17980   }
   17981   p = sqlite3GlobalConfig.m.xMalloc(nFull);
   17982 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   17983   if( p==0 && mem0.alarmCallback ){
   17984     sqlite3MallocAlarm(nFull);
   17985     p = sqlite3GlobalConfig.m.xMalloc(nFull);
   17986   }
   17987 #endif
   17988   if( p ){
   17989     nFull = sqlite3MallocSize(p);
   17990     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
   17991     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
   17992   }
   17993   *pp = p;
   17994   return nFull;
   17995 }
   17996 
   17997 /*
   17998 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   17999 ** assumes the memory subsystem has already been initialized.
   18000 */
   18001 SQLITE_PRIVATE void *sqlite3Malloc(int n){
   18002   void *p;
   18003   if( n<=0               /* IMP: R-65312-04917 */
   18004    || n>=0x7fffff00
   18005   ){
   18006     /* A memory allocation of a number of bytes which is near the maximum
   18007     ** signed integer value might cause an integer overflow inside of the
   18008     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   18009     ** 255 bytes of overhead.  SQLite itself will never use anything near
   18010     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   18011     p = 0;
   18012   }else if( sqlite3GlobalConfig.bMemstat ){
   18013     sqlite3_mutex_enter(mem0.mutex);
   18014     mallocWithAlarm(n, &p);
   18015     sqlite3_mutex_leave(mem0.mutex);
   18016   }else{
   18017     p = sqlite3GlobalConfig.m.xMalloc(n);
   18018   }
   18019   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
   18020   return p;
   18021 }
   18022 
   18023 /*
   18024 ** This version of the memory allocation is for use by the application.
   18025 ** First make sure the memory subsystem is initialized, then do the
   18026 ** allocation.
   18027 */
   18028 SQLITE_API void *sqlite3_malloc(int n){
   18029 #ifndef SQLITE_OMIT_AUTOINIT
   18030   if( sqlite3_initialize() ) return 0;
   18031 #endif
   18032   return sqlite3Malloc(n);
   18033 }
   18034 
   18035 /*
   18036 ** Each thread may only have a single outstanding allocation from
   18037 ** xScratchMalloc().  We verify this constraint in the single-threaded
   18038 ** case by setting scratchAllocOut to 1 when an allocation
   18039 ** is outstanding clearing it when the allocation is freed.
   18040 */
   18041 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18042 static int scratchAllocOut = 0;
   18043 #endif
   18044 
   18045 
   18046 /*
   18047 ** Allocate memory that is to be used and released right away.
   18048 ** This routine is similar to alloca() in that it is not intended
   18049 ** for situations where the memory might be held long-term.  This
   18050 ** routine is intended to get memory to old large transient data
   18051 ** structures that would not normally fit on the stack of an
   18052 ** embedded processor.
   18053 */
   18054 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
   18055   void *p;
   18056   assert( n>0 );
   18057 
   18058   sqlite3_mutex_enter(mem0.mutex);
   18059   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
   18060     p = mem0.pScratchFree;
   18061     mem0.pScratchFree = mem0.pScratchFree->pNext;
   18062     mem0.nScratchFree--;
   18063     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
   18064     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   18065     sqlite3_mutex_leave(mem0.mutex);
   18066   }else{
   18067     if( sqlite3GlobalConfig.bMemstat ){
   18068       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   18069       n = mallocWithAlarm(n, &p);
   18070       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
   18071       sqlite3_mutex_leave(mem0.mutex);
   18072     }else{
   18073       sqlite3_mutex_leave(mem0.mutex);
   18074       p = sqlite3GlobalConfig.m.xMalloc(n);
   18075     }
   18076     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
   18077   }
   18078   assert( sqlite3_mutex_notheld(mem0.mutex) );
   18079 
   18080 
   18081 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18082   /* Verify that no more than two scratch allocations per thread
   18083   ** are outstanding at one time.  (This is only checked in the
   18084   ** single-threaded case since checking in the multi-threaded case
   18085   ** would be much more complicated.) */
   18086   assert( scratchAllocOut<=1 );
   18087   if( p ) scratchAllocOut++;
   18088 #endif
   18089 
   18090   return p;
   18091 }
   18092 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
   18093   if( p ){
   18094 
   18095 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18096     /* Verify that no more than two scratch allocation per thread
   18097     ** is outstanding at one time.  (This is only checked in the
   18098     ** single-threaded case since checking in the multi-threaded case
   18099     ** would be much more complicated.) */
   18100     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
   18101     scratchAllocOut--;
   18102 #endif
   18103 
   18104     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
   18105       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
   18106       ScratchFreeslot *pSlot;
   18107       pSlot = (ScratchFreeslot*)p;
   18108       sqlite3_mutex_enter(mem0.mutex);
   18109       pSlot->pNext = mem0.pScratchFree;
   18110       mem0.pScratchFree = pSlot;
   18111       mem0.nScratchFree++;
   18112       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
   18113       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
   18114       sqlite3_mutex_leave(mem0.mutex);
   18115     }else{
   18116       /* Release memory back to the heap */
   18117       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
   18118       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
   18119       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   18120       if( sqlite3GlobalConfig.bMemstat ){
   18121         int iSize = sqlite3MallocSize(p);
   18122         sqlite3_mutex_enter(mem0.mutex);
   18123         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
   18124         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   18125         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   18126         sqlite3GlobalConfig.m.xFree(p);
   18127         sqlite3_mutex_leave(mem0.mutex);
   18128       }else{
   18129         sqlite3GlobalConfig.m.xFree(p);
   18130       }
   18131     }
   18132   }
   18133 }
   18134 
   18135 /*
   18136 ** TRUE if p is a lookaside memory allocation from db
   18137 */
   18138 #ifndef SQLITE_OMIT_LOOKASIDE
   18139 static int isLookaside(sqlite3 *db, void *p){
   18140   return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
   18141 }
   18142 #else
   18143 #define isLookaside(A,B) 0
   18144 #endif
   18145 
   18146 /*
   18147 ** Return the size of a memory allocation previously obtained from
   18148 ** sqlite3Malloc() or sqlite3_malloc().
   18149 */
   18150 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
   18151   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   18152   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   18153   return sqlite3GlobalConfig.m.xSize(p);
   18154 }
   18155 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
   18156   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   18157   if( db && isLookaside(db, p) ){
   18158     return db->lookaside.sz;
   18159   }else{
   18160     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   18161     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   18162     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   18163     return sqlite3GlobalConfig.m.xSize(p);
   18164   }
   18165 }
   18166 
   18167 /*
   18168 ** Free memory previously obtained from sqlite3Malloc().
   18169 */
   18170 SQLITE_API void sqlite3_free(void *p){
   18171   if( p==0 ) return;  /* IMP: R-49053-54554 */
   18172   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   18173   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   18174   if( sqlite3GlobalConfig.bMemstat ){
   18175     sqlite3_mutex_enter(mem0.mutex);
   18176     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   18177     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   18178     sqlite3GlobalConfig.m.xFree(p);
   18179     sqlite3_mutex_leave(mem0.mutex);
   18180   }else{
   18181     sqlite3GlobalConfig.m.xFree(p);
   18182   }
   18183 }
   18184 
   18185 /*
   18186 ** Free memory that might be associated with a particular database
   18187 ** connection.
   18188 */
   18189 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
   18190   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   18191   if( db ){
   18192     if( db->pnBytesFreed ){
   18193       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
   18194       return;
   18195     }
   18196     if( isLookaside(db, p) ){
   18197       LookasideSlot *pBuf = (LookasideSlot*)p;
   18198       pBuf->pNext = db->lookaside.pFree;
   18199       db->lookaside.pFree = pBuf;
   18200       db->lookaside.nOut--;
   18201       return;
   18202     }
   18203   }
   18204   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   18205   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   18206   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   18207   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   18208   sqlite3_free(p);
   18209 }
   18210 
   18211 /*
   18212 ** Change the size of an existing memory allocation
   18213 */
   18214 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
   18215   int nOld, nNew;
   18216   void *pNew;
   18217   if( pOld==0 ){
   18218     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
   18219   }
   18220   if( nBytes<=0 ){
   18221     sqlite3_free(pOld); /* IMP: R-31593-10574 */
   18222     return 0;
   18223   }
   18224   if( nBytes>=0x7fffff00 ){
   18225     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   18226     return 0;
   18227   }
   18228   nOld = sqlite3MallocSize(pOld);
   18229   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
   18230   ** argument to xRealloc is always a value returned by a prior call to
   18231   ** xRoundup. */
   18232   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
   18233   if( nOld==nNew ){
   18234     pNew = pOld;
   18235   }else if( sqlite3GlobalConfig.bMemstat ){
   18236     sqlite3_mutex_enter(mem0.mutex);
   18237     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
   18238     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
   18239           mem0.alarmThreshold ){
   18240       sqlite3MallocAlarm(nNew-nOld);
   18241     }
   18242     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   18243     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   18244     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   18245     if( pNew==0 && mem0.alarmCallback ){
   18246       sqlite3MallocAlarm(nBytes);
   18247       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   18248     }
   18249     if( pNew ){
   18250       nNew = sqlite3MallocSize(pNew);
   18251       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   18252     }
   18253     sqlite3_mutex_leave(mem0.mutex);
   18254   }else{
   18255     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   18256   }
   18257   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
   18258   return pNew;
   18259 }
   18260 
   18261 /*
   18262 ** The public interface to sqlite3Realloc.  Make sure that the memory
   18263 ** subsystem is initialized prior to invoking sqliteRealloc.
   18264 */
   18265 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
   18266 #ifndef SQLITE_OMIT_AUTOINIT
   18267   if( sqlite3_initialize() ) return 0;
   18268 #endif
   18269   return sqlite3Realloc(pOld, n);
   18270 }
   18271 
   18272 
   18273 /*
   18274 ** Allocate and zero memory.
   18275 */
   18276 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
   18277   void *p = sqlite3Malloc(n);
   18278   if( p ){
   18279     memset(p, 0, n);
   18280   }
   18281   return p;
   18282 }
   18283 
   18284 /*
   18285 ** Allocate and zero memory.  If the allocation fails, make
   18286 ** the mallocFailed flag in the connection pointer.
   18287 */
   18288 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
   18289   void *p = sqlite3DbMallocRaw(db, n);
   18290   if( p ){
   18291     memset(p, 0, n);
   18292   }
   18293   return p;
   18294 }
   18295 
   18296 /*
   18297 ** Allocate and zero memory.  If the allocation fails, make
   18298 ** the mallocFailed flag in the connection pointer.
   18299 **
   18300 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
   18301 ** failure on the same database connection) then always return 0.
   18302 ** Hence for a particular database connection, once malloc starts
   18303 ** failing, it fails consistently until mallocFailed is reset.
   18304 ** This is an important assumption.  There are many places in the
   18305 ** code that do things like this:
   18306 **
   18307 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   18308 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   18309 **         if( b ) a[10] = 9;
   18310 **
   18311 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   18312 ** that all prior mallocs (ex: "a") worked too.
   18313 */
   18314 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
   18315   void *p;
   18316   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   18317   assert( db==0 || db->pnBytesFreed==0 );
   18318 #ifndef SQLITE_OMIT_LOOKASIDE
   18319   if( db ){
   18320     LookasideSlot *pBuf;
   18321     if( db->mallocFailed ){
   18322       return 0;
   18323     }
   18324     if( db->lookaside.bEnabled ){
   18325       if( n>db->lookaside.sz ){
   18326         db->lookaside.anStat[1]++;
   18327       }else if( (pBuf = db->lookaside.pFree)==0 ){
   18328         db->lookaside.anStat[2]++;
   18329       }else{
   18330         db->lookaside.pFree = pBuf->pNext;
   18331         db->lookaside.nOut++;
   18332         db->lookaside.anStat[0]++;
   18333         if( db->lookaside.nOut>db->lookaside.mxOut ){
   18334           db->lookaside.mxOut = db->lookaside.nOut;
   18335         }
   18336         return (void*)pBuf;
   18337       }
   18338     }
   18339   }
   18340 #else
   18341   if( db && db->mallocFailed ){
   18342     return 0;
   18343   }
   18344 #endif
   18345   p = sqlite3Malloc(n);
   18346   if( !p && db ){
   18347     db->mallocFailed = 1;
   18348   }
   18349   sqlite3MemdebugSetType(p, MEMTYPE_DB |
   18350          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   18351   return p;
   18352 }
   18353 
   18354 /*
   18355 ** Resize the block of memory pointed to by p to n bytes. If the
   18356 ** resize fails, set the mallocFailed flag in the connection object.
   18357 */
   18358 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
   18359   void *pNew = 0;
   18360   assert( db!=0 );
   18361   assert( sqlite3_mutex_held(db->mutex) );
   18362   if( db->mallocFailed==0 ){
   18363     if( p==0 ){
   18364       return sqlite3DbMallocRaw(db, n);
   18365     }
   18366     if( isLookaside(db, p) ){
   18367       if( n<=db->lookaside.sz ){
   18368         return p;
   18369       }
   18370       pNew = sqlite3DbMallocRaw(db, n);
   18371       if( pNew ){
   18372         memcpy(pNew, p, db->lookaside.sz);
   18373         sqlite3DbFree(db, p);
   18374       }
   18375     }else{
   18376       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   18377       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   18378       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   18379       pNew = sqlite3_realloc(p, n);
   18380       if( !pNew ){
   18381         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   18382         db->mallocFailed = 1;
   18383       }
   18384       sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
   18385             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   18386     }
   18387   }
   18388   return pNew;
   18389 }
   18390 
   18391 /*
   18392 ** Attempt to reallocate p.  If the reallocation fails, then free p
   18393 ** and set the mallocFailed flag in the database connection.
   18394 */
   18395 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
   18396   void *pNew;
   18397   pNew = sqlite3DbRealloc(db, p, n);
   18398   if( !pNew ){
   18399     sqlite3DbFree(db, p);
   18400   }
   18401   return pNew;
   18402 }
   18403 
   18404 /*
   18405 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
   18406 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
   18407 ** is because when memory debugging is turned on, these two functions are
   18408 ** called via macros that record the current file and line number in the
   18409 ** ThreadData structure.
   18410 */
   18411 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
   18412   char *zNew;
   18413   size_t n;
   18414   if( z==0 ){
   18415     return 0;
   18416   }
   18417   n = sqlite3Strlen30(z) + 1;
   18418   assert( (n&0x7fffffff)==n );
   18419   zNew = sqlite3DbMallocRaw(db, (int)n);
   18420   if( zNew ){
   18421     memcpy(zNew, z, n);
   18422   }
   18423   return zNew;
   18424 }
   18425 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
   18426   char *zNew;
   18427   if( z==0 ){
   18428     return 0;
   18429   }
   18430   assert( (n&0x7fffffff)==n );
   18431   zNew = sqlite3DbMallocRaw(db, n+1);
   18432   if( zNew ){
   18433     memcpy(zNew, z, n);
   18434     zNew[n] = 0;
   18435   }
   18436   return zNew;
   18437 }
   18438 
   18439 /*
   18440 ** Create a string from the zFromat argument and the va_list that follows.
   18441 ** Store the string in memory obtained from sqliteMalloc() and make *pz
   18442 ** point to that string.
   18443 */
   18444 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
   18445   va_list ap;
   18446   char *z;
   18447 
   18448   va_start(ap, zFormat);
   18449   z = sqlite3VMPrintf(db, zFormat, ap);
   18450   va_end(ap);
   18451   sqlite3DbFree(db, *pz);
   18452   *pz = z;
   18453 }
   18454 
   18455 
   18456 /*
   18457 ** This function must be called before exiting any API function (i.e.
   18458 ** returning control to the user) that has called sqlite3_malloc or
   18459 ** sqlite3_realloc.
   18460 **
   18461 ** The returned value is normally a copy of the second argument to this
   18462 ** function. However, if a malloc() failure has occurred since the previous
   18463 ** invocation SQLITE_NOMEM is returned instead.
   18464 **
   18465 ** If the first argument, db, is not NULL and a malloc() error has occurred,
   18466 ** then the connection error-code (the value returned by sqlite3_errcode())
   18467 ** is set to SQLITE_NOMEM.
   18468 */
   18469 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
   18470   /* If the db handle is not NULL, then we must hold the connection handle
   18471   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
   18472   ** is unsafe, as is the call to sqlite3Error().
   18473   */
   18474   assert( !db || sqlite3_mutex_held(db->mutex) );
   18475   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
   18476     sqlite3Error(db, SQLITE_NOMEM, 0);
   18477     db->mallocFailed = 0;
   18478     rc = SQLITE_NOMEM;
   18479   }
   18480   return rc & (db ? db->errMask : 0xff);
   18481 }
   18482 
   18483 /************** End of malloc.c **********************************************/
   18484 /************** Begin file printf.c ******************************************/
   18485 /*
   18486 ** The "printf" code that follows dates from the 1980's.  It is in
   18487 ** the public domain.  The original comments are included here for
   18488 ** completeness.  They are very out-of-date but might be useful as
   18489 ** an historical reference.  Most of the "enhancements" have been backed
   18490 ** out so that the functionality is now the same as standard printf().
   18491 **
   18492 **************************************************************************
   18493 **
   18494 ** The following modules is an enhanced replacement for the "printf" subroutines
   18495 ** found in the standard C library.  The following enhancements are
   18496 ** supported:
   18497 **
   18498 **      +  Additional functions.  The standard set of "printf" functions
   18499 **         includes printf, fprintf, sprintf, vprintf, vfprintf, and
   18500 **         vsprintf.  This module adds the following:
   18501 **
   18502 **           *  snprintf -- Works like sprintf, but has an extra argument
   18503 **                          which is the size of the buffer written to.
   18504 **
   18505 **           *  mprintf --  Similar to sprintf.  Writes output to memory
   18506 **                          obtained from malloc.
   18507 **
   18508 **           *  xprintf --  Calls a function to dispose of output.
   18509 **
   18510 **           *  nprintf --  No output, but returns the number of characters
   18511 **                          that would have been output by printf.
   18512 **
   18513 **           *  A v- version (ex: vsnprintf) of every function is also
   18514 **              supplied.
   18515 **
   18516 **      +  A few extensions to the formatting notation are supported:
   18517 **
   18518 **           *  The "=" flag (similar to "-") causes the output to be
   18519 **              be centered in the appropriately sized field.
   18520 **
   18521 **           *  The %b field outputs an integer in binary notation.
   18522 **
   18523 **           *  The %c field now accepts a precision.  The character output
   18524 **              is repeated by the number of times the precision specifies.
   18525 **
   18526 **           *  The %' field works like %c, but takes as its character the
   18527 **              next character of the format string, instead of the next
   18528 **              argument.  For example,  printf("%.78'-")  prints 78 minus
   18529 **              signs, the same as  printf("%.78c",'-').
   18530 **
   18531 **      +  When compiled using GCC on a SPARC, this version of printf is
   18532 **         faster than the library printf for SUN OS 4.1.
   18533 **
   18534 **      +  All functions are fully reentrant.
   18535 **
   18536 */
   18537 
   18538 /*
   18539 ** Conversion types fall into various categories as defined by the
   18540 ** following enumeration.
   18541 */
   18542 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
   18543 #define etFLOAT       2 /* Floating point.  %f */
   18544 #define etEXP         3 /* Exponentional notation. %e and %E */
   18545 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
   18546 #define etSIZE        5 /* Return number of characters processed so far. %n */
   18547 #define etSTRING      6 /* Strings. %s */
   18548 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
   18549 #define etPERCENT     8 /* Percent symbol. %% */
   18550 #define etCHARX       9 /* Characters. %c */
   18551 /* The rest are extensions, not normally found in printf() */
   18552 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
   18553 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
   18554                           NULL pointers replaced by SQL NULL.  %Q */
   18555 #define etTOKEN      12 /* a pointer to a Token structure */
   18556 #define etSRCLIST    13 /* a pointer to a SrcList */
   18557 #define etPOINTER    14 /* The %p conversion */
   18558 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
   18559 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
   18560 
   18561 #define etINVALID     0 /* Any unrecognized conversion type */
   18562 
   18563 
   18564 /*
   18565 ** An "etByte" is an 8-bit unsigned value.
   18566 */
   18567 typedef unsigned char etByte;
   18568 
   18569 /*
   18570 ** Each builtin conversion character (ex: the 'd' in "%d") is described
   18571 ** by an instance of the following structure
   18572 */
   18573 typedef struct et_info {   /* Information about each format field */
   18574   char fmttype;            /* The format field code letter */
   18575   etByte base;             /* The base for radix conversion */
   18576   etByte flags;            /* One or more of FLAG_ constants below */
   18577   etByte type;             /* Conversion paradigm */
   18578   etByte charset;          /* Offset into aDigits[] of the digits string */
   18579   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
   18580 } et_info;
   18581 
   18582 /*
   18583 ** Allowed values for et_info.flags
   18584 */
   18585 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
   18586 #define FLAG_INTERN  2     /* True if for internal use only */
   18587 #define FLAG_STRING  4     /* Allow infinity precision */
   18588 
   18589 
   18590 /*
   18591 ** The following table is searched linearly, so it is good to put the
   18592 ** most frequently used conversion types first.
   18593 */
   18594 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
   18595 static const char aPrefix[] = "-x0\000X0";
   18596 static const et_info fmtinfo[] = {
   18597   {  'd', 10, 1, etRADIX,      0,  0 },
   18598   {  's',  0, 4, etSTRING,     0,  0 },
   18599   {  'g',  0, 1, etGENERIC,    30, 0 },
   18600   {  'z',  0, 4, etDYNSTRING,  0,  0 },
   18601   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
   18602   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
   18603   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
   18604   {  'c',  0, 0, etCHARX,      0,  0 },
   18605   {  'o',  8, 0, etRADIX,      0,  2 },
   18606   {  'u', 10, 0, etRADIX,      0,  0 },
   18607   {  'x', 16, 0, etRADIX,      16, 1 },
   18608   {  'X', 16, 0, etRADIX,      0,  4 },
   18609 #ifndef SQLITE_OMIT_FLOATING_POINT
   18610   {  'f',  0, 1, etFLOAT,      0,  0 },
   18611   {  'e',  0, 1, etEXP,        30, 0 },
   18612   {  'E',  0, 1, etEXP,        14, 0 },
   18613   {  'G',  0, 1, etGENERIC,    14, 0 },
   18614 #endif
   18615   {  'i', 10, 1, etRADIX,      0,  0 },
   18616   {  'n',  0, 0, etSIZE,       0,  0 },
   18617   {  '%',  0, 0, etPERCENT,    0,  0 },
   18618   {  'p', 16, 0, etPOINTER,    0,  1 },
   18619 
   18620 /* All the rest have the FLAG_INTERN bit set and are thus for internal
   18621 ** use only */
   18622   {  'T',  0, 2, etTOKEN,      0,  0 },
   18623   {  'S',  0, 2, etSRCLIST,    0,  0 },
   18624   {  'r', 10, 3, etORDINAL,    0,  0 },
   18625 };
   18626 
   18627 /*
   18628 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
   18629 ** conversions will work.
   18630 */
   18631 #ifndef SQLITE_OMIT_FLOATING_POINT
   18632 /*
   18633 ** "*val" is a double such that 0.1 <= *val < 10.0
   18634 ** Return the ascii code for the leading digit of *val, then
   18635 ** multiply "*val" by 10.0 to renormalize.
   18636 **
   18637 ** Example:
   18638 **     input:     *val = 3.14159
   18639 **     output:    *val = 1.4159    function return = '3'
   18640 **
   18641 ** The counter *cnt is incremented each time.  After counter exceeds
   18642 ** 16 (the number of significant digits in a 64-bit float) '0' is
   18643 ** always returned.
   18644 */
   18645 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
   18646   int digit;
   18647   LONGDOUBLE_TYPE d;
   18648   if( (*cnt)++ >= 16 ) return '0';
   18649   digit = (int)*val;
   18650   d = digit;
   18651   digit += '0';
   18652   *val = (*val - d)*10.0;
   18653   return (char)digit;
   18654 }
   18655 #endif /* SQLITE_OMIT_FLOATING_POINT */
   18656 
   18657 /*
   18658 ** Append N space characters to the given string buffer.
   18659 */
   18660 static void appendSpace(StrAccum *pAccum, int N){
   18661   static const char zSpaces[] = "                             ";
   18662   while( N>=(int)sizeof(zSpaces)-1 ){
   18663     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
   18664     N -= sizeof(zSpaces)-1;
   18665   }
   18666   if( N>0 ){
   18667     sqlite3StrAccumAppend(pAccum, zSpaces, N);
   18668   }
   18669 }
   18670 
   18671 /*
   18672 ** On machines with a small stack size, you can redefine the
   18673 ** SQLITE_PRINT_BUF_SIZE to be less than 350.
   18674 */
   18675 #ifndef SQLITE_PRINT_BUF_SIZE
   18676 # if defined(SQLITE_SMALL_STACK)
   18677 #   define SQLITE_PRINT_BUF_SIZE 50
   18678 # else
   18679 #   define SQLITE_PRINT_BUF_SIZE 350
   18680 # endif
   18681 #endif
   18682 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
   18683 
   18684 /*
   18685 ** The root program.  All variations call this core.
   18686 **
   18687 ** INPUTS:
   18688 **   func   This is a pointer to a function taking three arguments
   18689 **            1. A pointer to anything.  Same as the "arg" parameter.
   18690 **            2. A pointer to the list of characters to be output
   18691 **               (Note, this list is NOT null terminated.)
   18692 **            3. An integer number of characters to be output.
   18693 **               (Note: This number might be zero.)
   18694 **
   18695 **   arg    This is the pointer to anything which will be passed as the
   18696 **          first argument to "func".  Use it for whatever you like.
   18697 **
   18698 **   fmt    This is the format string, as in the usual print.
   18699 **
   18700 **   ap     This is a pointer to a list of arguments.  Same as in
   18701 **          vfprint.
   18702 **
   18703 ** OUTPUTS:
   18704 **          The return value is the total number of characters sent to
   18705 **          the function "func".  Returns -1 on a error.
   18706 **
   18707 ** Note that the order in which automatic variables are declared below
   18708 ** seems to make a big difference in determining how fast this beast
   18709 ** will run.
   18710 */
   18711 SQLITE_PRIVATE void sqlite3VXPrintf(
   18712   StrAccum *pAccum,                  /* Accumulate results here */
   18713   int useExtended,                   /* Allow extended %-conversions */
   18714   const char *fmt,                   /* Format string */
   18715   va_list ap                         /* arguments */
   18716 ){
   18717   int c;                     /* Next character in the format string */
   18718   char *bufpt;               /* Pointer to the conversion buffer */
   18719   int precision;             /* Precision of the current field */
   18720   int length;                /* Length of the field */
   18721   int idx;                   /* A general purpose loop counter */
   18722   int width;                 /* Width of the current field */
   18723   etByte flag_leftjustify;   /* True if "-" flag is present */
   18724   etByte flag_plussign;      /* True if "+" flag is present */
   18725   etByte flag_blanksign;     /* True if " " flag is present */
   18726   etByte flag_alternateform; /* True if "#" flag is present */
   18727   etByte flag_altform2;      /* True if "!" flag is present */
   18728   etByte flag_zeropad;       /* True if field width constant starts with zero */
   18729   etByte flag_long;          /* True if "l" flag is present */
   18730   etByte flag_longlong;      /* True if the "ll" flag is present */
   18731   etByte done;               /* Loop termination flag */
   18732   sqlite_uint64 longvalue;   /* Value for integer types */
   18733   LONGDOUBLE_TYPE realvalue; /* Value for real types */
   18734   const et_info *infop;      /* Pointer to the appropriate info structure */
   18735   char buf[etBUFSIZE];       /* Conversion buffer */
   18736   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
   18737   etByte xtype = 0;          /* Conversion paradigm */
   18738   char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
   18739 #ifndef SQLITE_OMIT_FLOATING_POINT
   18740   int  exp, e2;              /* exponent of real numbers */
   18741   double rounder;            /* Used for rounding floating point values */
   18742   etByte flag_dp;            /* True if decimal point should be shown */
   18743   etByte flag_rtz;           /* True if trailing zeros should be removed */
   18744   etByte flag_exp;           /* True to force display of the exponent */
   18745   int nsd;                   /* Number of significant digits returned */
   18746 #endif
   18747 
   18748   length = 0;
   18749   bufpt = 0;
   18750   for(; (c=(*fmt))!=0; ++fmt){
   18751     if( c!='%' ){
   18752       int amt;
   18753       bufpt = (char *)fmt;
   18754       amt = 1;
   18755       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
   18756       sqlite3StrAccumAppend(pAccum, bufpt, amt);
   18757       if( c==0 ) break;
   18758     }
   18759     if( (c=(*++fmt))==0 ){
   18760       sqlite3StrAccumAppend(pAccum, "%", 1);
   18761       break;
   18762     }
   18763     /* Find out what flags are present */
   18764     flag_leftjustify = flag_plussign = flag_blanksign =
   18765      flag_alternateform = flag_altform2 = flag_zeropad = 0;
   18766     done = 0;
   18767     do{
   18768       switch( c ){
   18769         case '-':   flag_leftjustify = 1;     break;
   18770         case '+':   flag_plussign = 1;        break;
   18771         case ' ':   flag_blanksign = 1;       break;
   18772         case '#':   flag_alternateform = 1;   break;
   18773         case '!':   flag_altform2 = 1;        break;
   18774         case '0':   flag_zeropad = 1;         break;
   18775         default:    done = 1;                 break;
   18776       }
   18777     }while( !done && (c=(*++fmt))!=0 );
   18778     /* Get the field width */
   18779     width = 0;
   18780     if( c=='*' ){
   18781       width = va_arg(ap,int);
   18782       if( width<0 ){
   18783         flag_leftjustify = 1;
   18784         width = -width;
   18785       }
   18786       c = *++fmt;
   18787     }else{
   18788       while( c>='0' && c<='9' ){
   18789         width = width*10 + c - '0';
   18790         c = *++fmt;
   18791       }
   18792     }
   18793     if( width > etBUFSIZE-10 ){
   18794       width = etBUFSIZE-10;
   18795     }
   18796     /* Get the precision */
   18797     if( c=='.' ){
   18798       precision = 0;
   18799       c = *++fmt;
   18800       if( c=='*' ){
   18801         precision = va_arg(ap,int);
   18802         if( precision<0 ) precision = -precision;
   18803         c = *++fmt;
   18804       }else{
   18805         while( c>='0' && c<='9' ){
   18806           precision = precision*10 + c - '0';
   18807           c = *++fmt;
   18808         }
   18809       }
   18810     }else{
   18811       precision = -1;
   18812     }
   18813     /* Get the conversion type modifier */
   18814     if( c=='l' ){
   18815       flag_long = 1;
   18816       c = *++fmt;
   18817       if( c=='l' ){
   18818         flag_longlong = 1;
   18819         c = *++fmt;
   18820       }else{
   18821         flag_longlong = 0;
   18822       }
   18823     }else{
   18824       flag_long = flag_longlong = 0;
   18825     }
   18826     /* Fetch the info entry for the field */
   18827     infop = &fmtinfo[0];
   18828     xtype = etINVALID;
   18829     for(idx=0; idx<ArraySize(fmtinfo); idx++){
   18830       if( c==fmtinfo[idx].fmttype ){
   18831         infop = &fmtinfo[idx];
   18832         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
   18833           xtype = infop->type;
   18834         }else{
   18835           return;
   18836         }
   18837         break;
   18838       }
   18839     }
   18840     zExtra = 0;
   18841 
   18842 
   18843     /* Limit the precision to prevent overflowing buf[] during conversion */
   18844     if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
   18845       precision = etBUFSIZE-40;
   18846     }
   18847 
   18848     /*
   18849     ** At this point, variables are initialized as follows:
   18850     **
   18851     **   flag_alternateform          TRUE if a '#' is present.
   18852     **   flag_altform2               TRUE if a '!' is present.
   18853     **   flag_plussign               TRUE if a '+' is present.
   18854     **   flag_leftjustify            TRUE if a '-' is present or if the
   18855     **                               field width was negative.
   18856     **   flag_zeropad                TRUE if the width began with 0.
   18857     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
   18858     **                               the conversion character.
   18859     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
   18860     **                               the conversion character.
   18861     **   flag_blanksign              TRUE if a ' ' is present.
   18862     **   width                       The specified field width.  This is
   18863     **                               always non-negative.  Zero is the default.
   18864     **   precision                   The specified precision.  The default
   18865     **                               is -1.
   18866     **   xtype                       The class of the conversion.
   18867     **   infop                       Pointer to the appropriate info struct.
   18868     */
   18869     switch( xtype ){
   18870       case etPOINTER:
   18871         flag_longlong = sizeof(char*)==sizeof(i64);
   18872         flag_long = sizeof(char*)==sizeof(long int);
   18873         /* Fall through into the next case */
   18874       case etORDINAL:
   18875       case etRADIX:
   18876         if( infop->flags & FLAG_SIGNED ){
   18877           i64 v;
   18878           if( flag_longlong ){
   18879             v = va_arg(ap,i64);
   18880           }else if( flag_long ){
   18881             v = va_arg(ap,long int);
   18882           }else{
   18883             v = va_arg(ap,int);
   18884           }
   18885           if( v<0 ){
   18886             if( v==SMALLEST_INT64 ){
   18887               longvalue = ((u64)1)<<63;
   18888             }else{
   18889               longvalue = -v;
   18890             }
   18891             prefix = '-';
   18892           }else{
   18893             longvalue = v;
   18894             if( flag_plussign )        prefix = '+';
   18895             else if( flag_blanksign )  prefix = ' ';
   18896             else                       prefix = 0;
   18897           }
   18898         }else{
   18899           if( flag_longlong ){
   18900             longvalue = va_arg(ap,u64);
   18901           }else if( flag_long ){
   18902             longvalue = va_arg(ap,unsigned long int);
   18903           }else{
   18904             longvalue = va_arg(ap,unsigned int);
   18905           }
   18906           prefix = 0;
   18907         }
   18908         if( longvalue==0 ) flag_alternateform = 0;
   18909         if( flag_zeropad && precision<width-(prefix!=0) ){
   18910           precision = width-(prefix!=0);
   18911         }
   18912         bufpt = &buf[etBUFSIZE-1];
   18913         if( xtype==etORDINAL ){
   18914           static const char zOrd[] = "thstndrd";
   18915           int x = (int)(longvalue % 10);
   18916           if( x>=4 || (longvalue/10)%10==1 ){
   18917             x = 0;
   18918           }
   18919           buf[etBUFSIZE-3] = zOrd[x*2];
   18920           buf[etBUFSIZE-2] = zOrd[x*2+1];
   18921           bufpt -= 2;
   18922         }
   18923         {
   18924           register const char *cset;      /* Use registers for speed */
   18925           register int base;
   18926           cset = &aDigits[infop->charset];
   18927           base = infop->base;
   18928           do{                                           /* Convert to ascii */
   18929             *(--bufpt) = cset[longvalue%base];
   18930             longvalue = longvalue/base;
   18931           }while( longvalue>0 );
   18932         }
   18933         length = (int)(&buf[etBUFSIZE-1]-bufpt);
   18934         for(idx=precision-length; idx>0; idx--){
   18935           *(--bufpt) = '0';                             /* Zero pad */
   18936         }
   18937         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
   18938         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
   18939           const char *pre;
   18940           char x;
   18941           pre = &aPrefix[infop->prefix];
   18942           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
   18943         }
   18944         length = (int)(&buf[etBUFSIZE-1]-bufpt);
   18945         break;
   18946       case etFLOAT:
   18947       case etEXP:
   18948       case etGENERIC:
   18949         realvalue = va_arg(ap,double);
   18950 #ifdef SQLITE_OMIT_FLOATING_POINT
   18951         length = 0;
   18952 #else
   18953         if( precision<0 ) precision = 6;         /* Set default precision */
   18954         if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
   18955         if( realvalue<0.0 ){
   18956           realvalue = -realvalue;
   18957           prefix = '-';
   18958         }else{
   18959           if( flag_plussign )          prefix = '+';
   18960           else if( flag_blanksign )    prefix = ' ';
   18961           else                         prefix = 0;
   18962         }
   18963         if( xtype==etGENERIC && precision>0 ) precision--;
   18964 #if 0
   18965         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
   18966         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
   18967 #else
   18968         /* It makes more sense to use 0.5 */
   18969         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
   18970 #endif
   18971         if( xtype==etFLOAT ) realvalue += rounder;
   18972         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
   18973         exp = 0;
   18974         if( sqlite3IsNaN((double)realvalue) ){
   18975           bufpt = "NaN";
   18976           length = 3;
   18977           break;
   18978         }
   18979         if( realvalue>0.0 ){
   18980           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
   18981           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
   18982           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
   18983           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
   18984           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
   18985           if( exp>350 ){
   18986             if( prefix=='-' ){
   18987               bufpt = "-Inf";
   18988             }else if( prefix=='+' ){
   18989               bufpt = "+Inf";
   18990             }else{
   18991               bufpt = "Inf";
   18992             }
   18993             length = sqlite3Strlen30(bufpt);
   18994             break;
   18995           }
   18996         }
   18997         bufpt = buf;
   18998         /*
   18999         ** If the field type is etGENERIC, then convert to either etEXP
   19000         ** or etFLOAT, as appropriate.
   19001         */
   19002         flag_exp = xtype==etEXP;
   19003         if( xtype!=etFLOAT ){
   19004           realvalue += rounder;
   19005           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
   19006         }
   19007         if( xtype==etGENERIC ){
   19008           flag_rtz = !flag_alternateform;
   19009           if( exp<-4 || exp>precision ){
   19010             xtype = etEXP;
   19011           }else{
   19012             precision = precision - exp;
   19013             xtype = etFLOAT;
   19014           }
   19015         }else{
   19016           flag_rtz = 0;
   19017         }
   19018         if( xtype==etEXP ){
   19019           e2 = 0;
   19020         }else{
   19021           e2 = exp;
   19022         }
   19023         nsd = 0;
   19024         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
   19025         /* The sign in front of the number */
   19026         if( prefix ){
   19027           *(bufpt++) = prefix;
   19028         }
   19029         /* Digits prior to the decimal point */
   19030         if( e2<0 ){
   19031           *(bufpt++) = '0';
   19032         }else{
   19033           for(; e2>=0; e2--){
   19034             *(bufpt++) = et_getdigit(&realvalue,&nsd);
   19035           }
   19036         }
   19037         /* The decimal point */
   19038         if( flag_dp ){
   19039           *(bufpt++) = '.';
   19040         }
   19041         /* "0" digits after the decimal point but before the first
   19042         ** significant digit of the number */
   19043         for(e2++; e2<0; precision--, e2++){
   19044           assert( precision>0 );
   19045           *(bufpt++) = '0';
   19046         }
   19047         /* Significant digits after the decimal point */
   19048         while( (precision--)>0 ){
   19049           *(bufpt++) = et_getdigit(&realvalue,&nsd);
   19050         }
   19051         /* Remove trailing zeros and the "." if no digits follow the "." */
   19052         if( flag_rtz && flag_dp ){
   19053           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
   19054           assert( bufpt>buf );
   19055           if( bufpt[-1]=='.' ){
   19056             if( flag_altform2 ){
   19057               *(bufpt++) = '0';
   19058             }else{
   19059               *(--bufpt) = 0;
   19060             }
   19061           }
   19062         }
   19063         /* Add the "eNNN" suffix */
   19064         if( flag_exp || xtype==etEXP ){
   19065           *(bufpt++) = aDigits[infop->charset];
   19066           if( exp<0 ){
   19067             *(bufpt++) = '-'; exp = -exp;
   19068           }else{
   19069             *(bufpt++) = '+';
   19070           }
   19071           if( exp>=100 ){
   19072             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
   19073             exp %= 100;
   19074           }
   19075           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
   19076           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
   19077         }
   19078         *bufpt = 0;
   19079 
   19080         /* The converted number is in buf[] and zero terminated. Output it.
   19081         ** Note that the number is in the usual order, not reversed as with
   19082         ** integer conversions. */
   19083         length = (int)(bufpt-buf);
   19084         bufpt = buf;
   19085 
   19086         /* Special case:  Add leading zeros if the flag_zeropad flag is
   19087         ** set and we are not left justified */
   19088         if( flag_zeropad && !flag_leftjustify && length < width){
   19089           int i;
   19090           int nPad = width - length;
   19091           for(i=width; i>=nPad; i--){
   19092             bufpt[i] = bufpt[i-nPad];
   19093           }
   19094           i = prefix!=0;
   19095           while( nPad-- ) bufpt[i++] = '0';
   19096           length = width;
   19097         }
   19098 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
   19099         break;
   19100       case etSIZE:
   19101         *(va_arg(ap,int*)) = pAccum->nChar;
   19102         length = width = 0;
   19103         break;
   19104       case etPERCENT:
   19105         buf[0] = '%';
   19106         bufpt = buf;
   19107         length = 1;
   19108         break;
   19109       case etCHARX:
   19110         c = va_arg(ap,int);
   19111         buf[0] = (char)c;
   19112         if( precision>=0 ){
   19113           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
   19114           length = precision;
   19115         }else{
   19116           length =1;
   19117         }
   19118         bufpt = buf;
   19119         break;
   19120       case etSTRING:
   19121       case etDYNSTRING:
   19122         bufpt = va_arg(ap,char*);
   19123         if( bufpt==0 ){
   19124           bufpt = "";
   19125         }else if( xtype==etDYNSTRING ){
   19126           zExtra = bufpt;
   19127         }
   19128         if( precision>=0 ){
   19129           for(length=0; length<precision && bufpt[length]; length++){}
   19130         }else{
   19131           length = sqlite3Strlen30(bufpt);
   19132         }
   19133         break;
   19134       case etSQLESCAPE:
   19135       case etSQLESCAPE2:
   19136       case etSQLESCAPE3: {
   19137         int i, j, k, n, isnull;
   19138         int needQuote;
   19139         char ch;
   19140         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
   19141         char *escarg = va_arg(ap,char*);
   19142         isnull = escarg==0;
   19143         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
   19144         k = precision;
   19145         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
   19146           if( ch==q )  n++;
   19147         }
   19148         needQuote = !isnull && xtype==etSQLESCAPE2;
   19149         n += i + 1 + needQuote*2;
   19150         if( n>etBUFSIZE ){
   19151           bufpt = zExtra = sqlite3Malloc( n );
   19152           if( bufpt==0 ){
   19153             pAccum->mallocFailed = 1;
   19154             return;
   19155           }
   19156         }else{
   19157           bufpt = buf;
   19158         }
   19159         j = 0;
   19160         if( needQuote ) bufpt[j++] = q;
   19161         k = i;
   19162         for(i=0; i<k; i++){
   19163           bufpt[j++] = ch = escarg[i];
   19164           if( ch==q ) bufpt[j++] = ch;
   19165         }
   19166         if( needQuote ) bufpt[j++] = q;
   19167         bufpt[j] = 0;
   19168         length = j;
   19169         /* The precision in %q and %Q means how many input characters to
   19170         ** consume, not the length of the output...
   19171         ** if( precision>=0 && precision<length ) length = precision; */
   19172         break;
   19173       }
   19174       case etTOKEN: {
   19175         Token *pToken = va_arg(ap, Token*);
   19176         if( pToken ){
   19177           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
   19178         }
   19179         length = width = 0;
   19180         break;
   19181       }
   19182       case etSRCLIST: {
   19183         SrcList *pSrc = va_arg(ap, SrcList*);
   19184         int k = va_arg(ap, int);
   19185         struct SrcList_item *pItem = &pSrc->a[k];
   19186         assert( k>=0 && k<pSrc->nSrc );
   19187         if( pItem->zDatabase ){
   19188           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
   19189           sqlite3StrAccumAppend(pAccum, ".", 1);
   19190         }
   19191         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
   19192         length = width = 0;
   19193         break;
   19194       }
   19195       default: {
   19196         assert( xtype==etINVALID );
   19197         return;
   19198       }
   19199     }/* End switch over the format type */
   19200     /*
   19201     ** The text of the conversion is pointed to by "bufpt" and is
   19202     ** "length" characters long.  The field width is "width".  Do
   19203     ** the output.
   19204     */
   19205     if( !flag_leftjustify ){
   19206       register int nspace;
   19207       nspace = width-length;
   19208       if( nspace>0 ){
   19209         appendSpace(pAccum, nspace);
   19210       }
   19211     }
   19212     if( length>0 ){
   19213       sqlite3StrAccumAppend(pAccum, bufpt, length);
   19214     }
   19215     if( flag_leftjustify ){
   19216       register int nspace;
   19217       nspace = width-length;
   19218       if( nspace>0 ){
   19219         appendSpace(pAccum, nspace);
   19220       }
   19221     }
   19222     if( zExtra ){
   19223       sqlite3_free(zExtra);
   19224     }
   19225   }/* End for loop over the format string */
   19226 } /* End of function */
   19227 
   19228 /*
   19229 ** Append N bytes of text from z to the StrAccum object.
   19230 */
   19231 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   19232   assert( z!=0 || N==0 );
   19233   if( p->tooBig | p->mallocFailed ){
   19234     testcase(p->tooBig);
   19235     testcase(p->mallocFailed);
   19236     return;
   19237   }
   19238   if( N<0 ){
   19239     N = sqlite3Strlen30(z);
   19240   }
   19241   if( N==0 || NEVER(z==0) ){
   19242     return;
   19243   }
   19244   if( p->nChar+N >= p->nAlloc ){
   19245     char *zNew;
   19246     if( !p->useMalloc ){
   19247       p->tooBig = 1;
   19248       N = p->nAlloc - p->nChar - 1;
   19249       if( N<=0 ){
   19250         return;
   19251       }
   19252     }else{
   19253       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   19254       i64 szNew = p->nChar;
   19255       szNew += N + 1;
   19256       if( szNew > p->mxAlloc ){
   19257         sqlite3StrAccumReset(p);
   19258         p->tooBig = 1;
   19259         return;
   19260       }else{
   19261         p->nAlloc = (int)szNew;
   19262       }
   19263       if( p->useMalloc==1 ){
   19264         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   19265       }else{
   19266         zNew = sqlite3_realloc(zOld, p->nAlloc);
   19267       }
   19268       if( zNew ){
   19269         if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
   19270         p->zText = zNew;
   19271       }else{
   19272         p->mallocFailed = 1;
   19273         sqlite3StrAccumReset(p);
   19274         return;
   19275       }
   19276     }
   19277   }
   19278   memcpy(&p->zText[p->nChar], z, N);
   19279   p->nChar += N;
   19280 }
   19281 
   19282 /*
   19283 ** Finish off a string by making sure it is zero-terminated.
   19284 ** Return a pointer to the resulting string.  Return a NULL
   19285 ** pointer if any kind of error was encountered.
   19286 */
   19287 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
   19288   if( p->zText ){
   19289     p->zText[p->nChar] = 0;
   19290     if( p->useMalloc && p->zText==p->zBase ){
   19291       if( p->useMalloc==1 ){
   19292         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   19293       }else{
   19294         p->zText = sqlite3_malloc(p->nChar+1);
   19295       }
   19296       if( p->zText ){
   19297         memcpy(p->zText, p->zBase, p->nChar+1);
   19298       }else{
   19299         p->mallocFailed = 1;
   19300       }
   19301     }
   19302   }
   19303   return p->zText;
   19304 }
   19305 
   19306 /*
   19307 ** Reset an StrAccum string.  Reclaim all malloced memory.
   19308 */
   19309 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
   19310   if( p->zText!=p->zBase ){
   19311     if( p->useMalloc==1 ){
   19312       sqlite3DbFree(p->db, p->zText);
   19313     }else{
   19314       sqlite3_free(p->zText);
   19315     }
   19316   }
   19317   p->zText = 0;
   19318 }
   19319 
   19320 /*
   19321 ** Initialize a string accumulator
   19322 */
   19323 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
   19324   p->zText = p->zBase = zBase;
   19325   p->db = 0;
   19326   p->nChar = 0;
   19327   p->nAlloc = n;
   19328   p->mxAlloc = mx;
   19329   p->useMalloc = 1;
   19330   p->tooBig = 0;
   19331   p->mallocFailed = 0;
   19332 }
   19333 
   19334 /*
   19335 ** Print into memory obtained from sqliteMalloc().  Use the internal
   19336 ** %-conversion extensions.
   19337 */
   19338 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
   19339   char *z;
   19340   char zBase[SQLITE_PRINT_BUF_SIZE];
   19341   StrAccum acc;
   19342   assert( db!=0 );
   19343   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
   19344                       db->aLimit[SQLITE_LIMIT_LENGTH]);
   19345   acc.db = db;
   19346   sqlite3VXPrintf(&acc, 1, zFormat, ap);
   19347   z = sqlite3StrAccumFinish(&acc);
   19348   if( acc.mallocFailed ){
   19349     db->mallocFailed = 1;
   19350   }
   19351   return z;
   19352 }
   19353 
   19354 /*
   19355 ** Print into memory obtained from sqliteMalloc().  Use the internal
   19356 ** %-conversion extensions.
   19357 */
   19358 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
   19359   va_list ap;
   19360   char *z;
   19361   va_start(ap, zFormat);
   19362   z = sqlite3VMPrintf(db, zFormat, ap);
   19363   va_end(ap);
   19364   return z;
   19365 }
   19366 
   19367 /*
   19368 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
   19369 ** the string and before returnning.  This routine is intended to be used
   19370 ** to modify an existing string.  For example:
   19371 **
   19372 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
   19373 **
   19374 */
   19375 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
   19376   va_list ap;
   19377   char *z;
   19378   va_start(ap, zFormat);
   19379   z = sqlite3VMPrintf(db, zFormat, ap);
   19380   va_end(ap);
   19381   sqlite3DbFree(db, zStr);
   19382   return z;
   19383 }
   19384 
   19385 /*
   19386 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
   19387 ** %-conversion extensions.
   19388 */
   19389 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
   19390   char *z;
   19391   char zBase[SQLITE_PRINT_BUF_SIZE];
   19392   StrAccum acc;
   19393 #ifndef SQLITE_OMIT_AUTOINIT
   19394   if( sqlite3_initialize() ) return 0;
   19395 #endif
   19396   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   19397   acc.useMalloc = 2;
   19398   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   19399   z = sqlite3StrAccumFinish(&acc);
   19400   return z;
   19401 }
   19402 
   19403 /*
   19404 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
   19405 ** %-conversion extensions.
   19406 */
   19407 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
   19408   va_list ap;
   19409   char *z;
   19410 #ifndef SQLITE_OMIT_AUTOINIT
   19411   if( sqlite3_initialize() ) return 0;
   19412 #endif
   19413   va_start(ap, zFormat);
   19414   z = sqlite3_vmprintf(zFormat, ap);
   19415   va_end(ap);
   19416   return z;
   19417 }
   19418 
   19419 /*
   19420 ** sqlite3_snprintf() works like snprintf() except that it ignores the
   19421 ** current locale settings.  This is important for SQLite because we
   19422 ** are not able to use a "," as the decimal point in place of "." as
   19423 ** specified by some locales.
   19424 **
   19425 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
   19426 ** from the snprintf() standard.  Unfortunately, it is too late to change
   19427 ** this without breaking compatibility, so we just have to live with the
   19428 ** mistake.
   19429 **
   19430 ** sqlite3_vsnprintf() is the varargs version.
   19431 */
   19432 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
   19433   StrAccum acc;
   19434   if( n<=0 ) return zBuf;
   19435   sqlite3StrAccumInit(&acc, zBuf, n, 0);
   19436   acc.useMalloc = 0;
   19437   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   19438   return sqlite3StrAccumFinish(&acc);
   19439 }
   19440 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
   19441   char *z;
   19442   va_list ap;
   19443   va_start(ap,zFormat);
   19444   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
   19445   va_end(ap);
   19446   return z;
   19447 }
   19448 
   19449 /*
   19450 ** This is the routine that actually formats the sqlite3_log() message.
   19451 ** We house it in a separate routine from sqlite3_log() to avoid using
   19452 ** stack space on small-stack systems when logging is disabled.
   19453 **
   19454 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
   19455 ** allocate memory because it might be called while the memory allocator
   19456 ** mutex is held.
   19457 */
   19458 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
   19459   StrAccum acc;                          /* String accumulator */
   19460   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
   19461 
   19462   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
   19463   acc.useMalloc = 0;
   19464   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   19465   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
   19466                            sqlite3StrAccumFinish(&acc));
   19467 }
   19468 
   19469 /*
   19470 ** Format and write a message to the log if logging is enabled.
   19471 */
   19472 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
   19473   va_list ap;                             /* Vararg list */
   19474   if( sqlite3GlobalConfig.xLog ){
   19475     va_start(ap, zFormat);
   19476     renderLogMsg(iErrCode, zFormat, ap);
   19477     va_end(ap);
   19478   }
   19479 }
   19480 
   19481 #if defined(SQLITE_DEBUG)
   19482 /*
   19483 ** A version of printf() that understands %lld.  Used for debugging.
   19484 ** The printf() built into some versions of windows does not understand %lld
   19485 ** and segfaults if you give it a long long int.
   19486 */
   19487 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
   19488   va_list ap;
   19489   StrAccum acc;
   19490   char zBuf[500];
   19491   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
   19492   acc.useMalloc = 0;
   19493   va_start(ap,zFormat);
   19494   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   19495   va_end(ap);
   19496   sqlite3StrAccumFinish(&acc);
   19497   fprintf(stdout,"%s", zBuf);
   19498   fflush(stdout);
   19499 }
   19500 #endif
   19501 
   19502 #ifndef SQLITE_OMIT_TRACE
   19503 /*
   19504 ** variable-argument wrapper around sqlite3VXPrintf().
   19505 */
   19506 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
   19507   va_list ap;
   19508   va_start(ap,zFormat);
   19509   sqlite3VXPrintf(p, 1, zFormat, ap);
   19510   va_end(ap);
   19511 }
   19512 #endif
   19513 
   19514 /************** End of printf.c **********************************************/
   19515 /************** Begin file random.c ******************************************/
   19516 /*
   19517 ** 2001 September 15
   19518 **
   19519 ** The author disclaims copyright to this source code.  In place of
   19520 ** a legal notice, here is a blessing:
   19521 **
   19522 **    May you do good and not evil.
   19523 **    May you find forgiveness for yourself and forgive others.
   19524 **    May you share freely, never taking more than you give.
   19525 **
   19526 *************************************************************************
   19527 ** This file contains code to implement a pseudo-random number
   19528 ** generator (PRNG) for SQLite.
   19529 **
   19530 ** Random numbers are used by some of the database backends in order
   19531 ** to generate random integer keys for tables or random filenames.
   19532 */
   19533 
   19534 
   19535 /* All threads share a single random number generator.
   19536 ** This structure is the current state of the generator.
   19537 */
   19538 static SQLITE_WSD struct sqlite3PrngType {
   19539   unsigned char isInit;          /* True if initialized */
   19540   unsigned char i, j;            /* State variables */
   19541   unsigned char s[256];          /* State variables */
   19542 } sqlite3Prng;
   19543 
   19544 /*
   19545 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
   19546 ** must be held while executing this routine.
   19547 **
   19548 ** Why not just use a library random generator like lrand48() for this?
   19549 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
   19550 ** good source of random numbers.  The lrand48() library function may
   19551 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
   19552 ** subtle problems on some systems that could cause problems.  It is hard
   19553 ** to know.  To minimize the risk of problems due to bad lrand48()
   19554 ** implementations, SQLite uses this random number generator based
   19555 ** on RC4, which we know works very well.
   19556 **
   19557 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
   19558 ** randomness any more.  But we will leave this code in all the same.
   19559 */
   19560 static u8 randomByte(void){
   19561   unsigned char t;
   19562 
   19563 
   19564   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
   19565   ** state vector.  If writable static data is unsupported on the target,
   19566   ** we have to locate the state vector at run-time.  In the more common
   19567   ** case where writable static data is supported, wsdPrng can refer directly
   19568   ** to the "sqlite3Prng" state vector declared above.
   19569   */
   19570 #ifdef SQLITE_OMIT_WSD
   19571   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
   19572 # define wsdPrng p[0]
   19573 #else
   19574 # define wsdPrng sqlite3Prng
   19575 #endif
   19576 
   19577 
   19578   /* Initialize the state of the random number generator once,
   19579   ** the first time this routine is called.  The seed value does
   19580   ** not need to contain a lot of randomness since we are not
   19581   ** trying to do secure encryption or anything like that...
   19582   **
   19583   ** Nothing in this file or anywhere else in SQLite does any kind of
   19584   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
   19585   ** number generator) not as an encryption device.
   19586   */
   19587   if( !wsdPrng.isInit ){
   19588     int i;
   19589     char k[256];
   19590     wsdPrng.j = 0;
   19591     wsdPrng.i = 0;
   19592     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
   19593     for(i=0; i<256; i++){
   19594       wsdPrng.s[i] = (u8)i;
   19595     }
   19596     for(i=0; i<256; i++){
   19597       wsdPrng.j += wsdPrng.s[i] + k[i];
   19598       t = wsdPrng.s[wsdPrng.j];
   19599       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
   19600       wsdPrng.s[i] = t;
   19601     }
   19602     wsdPrng.isInit = 1;
   19603   }
   19604 
   19605   /* Generate and return single random byte
   19606   */
   19607   wsdPrng.i++;
   19608   t = wsdPrng.s[wsdPrng.i];
   19609   wsdPrng.j += t;
   19610   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
   19611   wsdPrng.s[wsdPrng.j] = t;
   19612   t += wsdPrng.s[wsdPrng.i];
   19613   return wsdPrng.s[t];
   19614 }
   19615 
   19616 /*
   19617 ** Return N random bytes.
   19618 */
   19619 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
   19620   unsigned char *zBuf = pBuf;
   19621 #if SQLITE_THREADSAFE
   19622   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
   19623 #endif
   19624   sqlite3_mutex_enter(mutex);
   19625   while( N-- ){
   19626     *(zBuf++) = randomByte();
   19627   }
   19628   sqlite3_mutex_leave(mutex);
   19629 }
   19630 
   19631 #ifndef SQLITE_OMIT_BUILTIN_TEST
   19632 /*
   19633 ** For testing purposes, we sometimes want to preserve the state of
   19634 ** PRNG and restore the PRNG to its saved state at a later time, or
   19635 ** to reset the PRNG to its initial state.  These routines accomplish
   19636 ** those tasks.
   19637 **
   19638 ** The sqlite3_test_control() interface calls these routines to
   19639 ** control the PRNG.
   19640 */
   19641 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
   19642 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
   19643   memcpy(
   19644     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   19645     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   19646     sizeof(sqlite3Prng)
   19647   );
   19648 }
   19649 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
   19650   memcpy(
   19651     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   19652     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   19653     sizeof(sqlite3Prng)
   19654   );
   19655 }
   19656 SQLITE_PRIVATE void sqlite3PrngResetState(void){
   19657   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
   19658 }
   19659 #endif /* SQLITE_OMIT_BUILTIN_TEST */
   19660 
   19661 /************** End of random.c **********************************************/
   19662 /************** Begin file utf.c *********************************************/
   19663 /*
   19664 ** 2004 April 13
   19665 **
   19666 ** The author disclaims copyright to this source code.  In place of
   19667 ** a legal notice, here is a blessing:
   19668 **
   19669 **    May you do good and not evil.
   19670 **    May you find forgiveness for yourself and forgive others.
   19671 **    May you share freely, never taking more than you give.
   19672 **
   19673 *************************************************************************
   19674 ** This file contains routines used to translate between UTF-8,
   19675 ** UTF-16, UTF-16BE, and UTF-16LE.
   19676 **
   19677 ** Notes on UTF-8:
   19678 **
   19679 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
   19680 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
   19681 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
   19682 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
   19683 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
   19684 **
   19685 **
   19686 ** Notes on UTF-16:  (with wwww+1==uuuuu)
   19687 **
   19688 **      Word-0               Word-1          Value
   19689 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
   19690 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
   19691 **
   19692 **
   19693 ** BOM or Byte Order Mark:
   19694 **     0xff 0xfe   little-endian utf-16 follows
   19695 **     0xfe 0xff   big-endian utf-16 follows
   19696 **
   19697 */
   19698 
   19699 #ifndef SQLITE_AMALGAMATION
   19700 /*
   19701 ** The following constant value is used by the SQLITE_BIGENDIAN and
   19702 ** SQLITE_LITTLEENDIAN macros.
   19703 */
   19704 SQLITE_PRIVATE const int sqlite3one = 1;
   19705 #endif /* SQLITE_AMALGAMATION */
   19706 
   19707 /*
   19708 ** This lookup table is used to help decode the first byte of
   19709 ** a multi-byte UTF8 character.
   19710 */
   19711 static const unsigned char sqlite3Utf8Trans1[] = {
   19712   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   19713   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   19714   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   19715   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
   19716   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   19717   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   19718   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   19719   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
   19720 };
   19721 
   19722 
   19723 #define WRITE_UTF8(zOut, c) {                          \
   19724   if( c<0x00080 ){                                     \
   19725     *zOut++ = (u8)(c&0xFF);                            \
   19726   }                                                    \
   19727   else if( c<0x00800 ){                                \
   19728     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
   19729     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   19730   }                                                    \
   19731   else if( c<0x10000 ){                                \
   19732     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
   19733     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
   19734     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   19735   }else{                                               \
   19736     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
   19737     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
   19738     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
   19739     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   19740   }                                                    \
   19741 }
   19742 
   19743 #define WRITE_UTF16LE(zOut, c) {                                    \
   19744   if( c<=0xFFFF ){                                                  \
   19745     *zOut++ = (u8)(c&0x00FF);                                       \
   19746     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
   19747   }else{                                                            \
   19748     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   19749     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
   19750     *zOut++ = (u8)(c&0x00FF);                                       \
   19751     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
   19752   }                                                                 \
   19753 }
   19754 
   19755 #define WRITE_UTF16BE(zOut, c) {                                    \
   19756   if( c<=0xFFFF ){                                                  \
   19757     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
   19758     *zOut++ = (u8)(c&0x00FF);                                       \
   19759   }else{                                                            \
   19760     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
   19761     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   19762     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
   19763     *zOut++ = (u8)(c&0x00FF);                                       \
   19764   }                                                                 \
   19765 }
   19766 
   19767 #define READ_UTF16LE(zIn, TERM, c){                                   \
   19768   c = (*zIn++);                                                       \
   19769   c += ((*zIn++)<<8);                                                 \
   19770   if( c>=0xD800 && c<0xE000 && TERM ){                                \
   19771     int c2 = (*zIn++);                                                \
   19772     c2 += ((*zIn++)<<8);                                              \
   19773     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   19774   }                                                                   \
   19775 }
   19776 
   19777 #define READ_UTF16BE(zIn, TERM, c){                                   \
   19778   c = ((*zIn++)<<8);                                                  \
   19779   c += (*zIn++);                                                      \
   19780   if( c>=0xD800 && c<0xE000 && TERM ){                                \
   19781     int c2 = ((*zIn++)<<8);                                           \
   19782     c2 += (*zIn++);                                                   \
   19783     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   19784   }                                                                   \
   19785 }
   19786 
   19787 /*
   19788 ** Translate a single UTF-8 character.  Return the unicode value.
   19789 **
   19790 ** During translation, assume that the byte that zTerm points
   19791 ** is a 0x00.
   19792 **
   19793 ** Write a pointer to the next unread byte back into *pzNext.
   19794 **
   19795 ** Notes On Invalid UTF-8:
   19796 **
   19797 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
   19798 **     be encoded as a multi-byte character.  Any multi-byte character that
   19799 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
   19800 **
   19801 **  *  This routine never allows a UTF16 surrogate value to be encoded.
   19802 **     If a multi-byte character attempts to encode a value between
   19803 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
   19804 **
   19805 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
   19806 **     byte of a character are interpreted as single-byte characters
   19807 **     and rendered as themselves even though they are technically
   19808 **     invalid characters.
   19809 **
   19810 **  *  This routine accepts an infinite number of different UTF8 encodings
   19811 **     for unicode values 0x80 and greater.  It do not change over-length
   19812 **     encodings to 0xfffd as some systems recommend.
   19813 */
   19814 #define READ_UTF8(zIn, zTerm, c)                           \
   19815   c = *(zIn++);                                            \
   19816   if( c>=0xc0 ){                                           \
   19817     c = sqlite3Utf8Trans1[c-0xc0];                         \
   19818     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
   19819       c = (c<<6) + (0x3f & *(zIn++));                      \
   19820     }                                                      \
   19821     if( c<0x80                                             \
   19822         || (c&0xFFFFF800)==0xD800                          \
   19823         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
   19824   }
   19825 SQLITE_PRIVATE int sqlite3Utf8Read(
   19826   const unsigned char *zIn,       /* First byte of UTF-8 character */
   19827   const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
   19828 ){
   19829   unsigned int c;
   19830 
   19831   /* Same as READ_UTF8() above but without the zTerm parameter.
   19832   ** For this routine, we assume the UTF8 string is always zero-terminated.
   19833   */
   19834   c = *(zIn++);
   19835   if( c>=0xc0 ){
   19836     c = sqlite3Utf8Trans1[c-0xc0];
   19837     while( (*zIn & 0xc0)==0x80 ){
   19838       c = (c<<6) + (0x3f & *(zIn++));
   19839     }
   19840     if( c<0x80
   19841         || (c&0xFFFFF800)==0xD800
   19842         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
   19843   }
   19844   *pzNext = zIn;
   19845   return c;
   19846 }
   19847 
   19848 
   19849 
   19850 
   19851 /*
   19852 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
   19853 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
   19854 */
   19855 /* #define TRANSLATE_TRACE 1 */
   19856 
   19857 #ifndef SQLITE_OMIT_UTF16
   19858 /*
   19859 ** This routine transforms the internal text encoding used by pMem to
   19860 ** desiredEnc. It is an error if the string is already of the desired
   19861 ** encoding, or if *pMem does not contain a string value.
   19862 */
   19863 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
   19864   int len;                    /* Maximum length of output string in bytes */
   19865   unsigned char *zOut;                  /* Output buffer */
   19866   unsigned char *zIn;                   /* Input iterator */
   19867   unsigned char *zTerm;                 /* End of input */
   19868   unsigned char *z;                     /* Output iterator */
   19869   unsigned int c;
   19870 
   19871   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   19872   assert( pMem->flags&MEM_Str );
   19873   assert( pMem->enc!=desiredEnc );
   19874   assert( pMem->enc!=0 );
   19875   assert( pMem->n>=0 );
   19876 
   19877 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   19878   {
   19879     char zBuf[100];
   19880     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   19881     fprintf(stderr, "INPUT:  %s\n", zBuf);
   19882   }
   19883 #endif
   19884 
   19885   /* If the translation is between UTF-16 little and big endian, then
   19886   ** all that is required is to swap the byte order. This case is handled
   19887   ** differently from the others.
   19888   */
   19889   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
   19890     u8 temp;
   19891     int rc;
   19892     rc = sqlite3VdbeMemMakeWriteable(pMem);
   19893     if( rc!=SQLITE_OK ){
   19894       assert( rc==SQLITE_NOMEM );
   19895       return SQLITE_NOMEM;
   19896     }
   19897     zIn = (u8*)pMem->z;
   19898     zTerm = &zIn[pMem->n&~1];
   19899     while( zIn<zTerm ){
   19900       temp = *zIn;
   19901       *zIn = *(zIn+1);
   19902       zIn++;
   19903       *zIn++ = temp;
   19904     }
   19905     pMem->enc = desiredEnc;
   19906     goto translate_out;
   19907   }
   19908 
   19909   /* Set len to the maximum number of bytes required in the output buffer. */
   19910   if( desiredEnc==SQLITE_UTF8 ){
   19911     /* When converting from UTF-16, the maximum growth results from
   19912     ** translating a 2-byte character to a 4-byte UTF-8 character.
   19913     ** A single byte is required for the output string
   19914     ** nul-terminator.
   19915     */
   19916     pMem->n &= ~1;
   19917     len = pMem->n * 2 + 1;
   19918   }else{
   19919     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
   19920     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
   19921     ** character. Two bytes are required in the output buffer for the
   19922     ** nul-terminator.
   19923     */
   19924     len = pMem->n * 2 + 2;
   19925   }
   19926 
   19927   /* Set zIn to point at the start of the input buffer and zTerm to point 1
   19928   ** byte past the end.
   19929   **
   19930   ** Variable zOut is set to point at the output buffer, space obtained
   19931   ** from sqlite3_malloc().
   19932   */
   19933   zIn = (u8*)pMem->z;
   19934   zTerm = &zIn[pMem->n];
   19935   zOut = sqlite3DbMallocRaw(pMem->db, len);
   19936   if( !zOut ){
   19937     return SQLITE_NOMEM;
   19938   }
   19939   z = zOut;
   19940 
   19941   if( pMem->enc==SQLITE_UTF8 ){
   19942     if( desiredEnc==SQLITE_UTF16LE ){
   19943       /* UTF-8 -> UTF-16 Little-endian */
   19944       while( zIn<zTerm ){
   19945         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   19946         READ_UTF8(zIn, zTerm, c);
   19947         WRITE_UTF16LE(z, c);
   19948       }
   19949     }else{
   19950       assert( desiredEnc==SQLITE_UTF16BE );
   19951       /* UTF-8 -> UTF-16 Big-endian */
   19952       while( zIn<zTerm ){
   19953         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   19954         READ_UTF8(zIn, zTerm, c);
   19955         WRITE_UTF16BE(z, c);
   19956       }
   19957     }
   19958     pMem->n = (int)(z - zOut);
   19959     *z++ = 0;
   19960   }else{
   19961     assert( desiredEnc==SQLITE_UTF8 );
   19962     if( pMem->enc==SQLITE_UTF16LE ){
   19963       /* UTF-16 Little-endian -> UTF-8 */
   19964       while( zIn<zTerm ){
   19965         READ_UTF16LE(zIn, zIn<zTerm, c);
   19966         WRITE_UTF8(z, c);
   19967       }
   19968     }else{
   19969       /* UTF-16 Big-endian -> UTF-8 */
   19970       while( zIn<zTerm ){
   19971         READ_UTF16BE(zIn, zIn<zTerm, c);
   19972         WRITE_UTF8(z, c);
   19973       }
   19974     }
   19975     pMem->n = (int)(z - zOut);
   19976   }
   19977   *z = 0;
   19978   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   19979 
   19980   sqlite3VdbeMemRelease(pMem);
   19981   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
   19982   pMem->enc = desiredEnc;
   19983   pMem->flags |= (MEM_Term|MEM_Dyn);
   19984   pMem->z = (char*)zOut;
   19985   pMem->zMalloc = pMem->z;
   19986 
   19987 translate_out:
   19988 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   19989   {
   19990     char zBuf[100];
   19991     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   19992     fprintf(stderr, "OUTPUT: %s\n", zBuf);
   19993   }
   19994 #endif
   19995   return SQLITE_OK;
   19996 }
   19997 
   19998 /*
   19999 ** This routine checks for a byte-order mark at the beginning of the
   20000 ** UTF-16 string stored in *pMem. If one is present, it is removed and
   20001 ** the encoding of the Mem adjusted. This routine does not do any
   20002 ** byte-swapping, it just sets Mem.enc appropriately.
   20003 **
   20004 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
   20005 ** changed by this function.
   20006 */
   20007 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
   20008   int rc = SQLITE_OK;
   20009   u8 bom = 0;
   20010 
   20011   assert( pMem->n>=0 );
   20012   if( pMem->n>1 ){
   20013     u8 b1 = *(u8 *)pMem->z;
   20014     u8 b2 = *(((u8 *)pMem->z) + 1);
   20015     if( b1==0xFE && b2==0xFF ){
   20016       bom = SQLITE_UTF16BE;
   20017     }
   20018     if( b1==0xFF && b2==0xFE ){
   20019       bom = SQLITE_UTF16LE;
   20020     }
   20021   }
   20022 
   20023   if( bom ){
   20024     rc = sqlite3VdbeMemMakeWriteable(pMem);
   20025     if( rc==SQLITE_OK ){
   20026       pMem->n -= 2;
   20027       memmove(pMem->z, &pMem->z[2], pMem->n);
   20028       pMem->z[pMem->n] = '\0';
   20029       pMem->z[pMem->n+1] = '\0';
   20030       pMem->flags |= MEM_Term;
   20031       pMem->enc = bom;
   20032     }
   20033   }
   20034   return rc;
   20035 }
   20036 #endif /* SQLITE_OMIT_UTF16 */
   20037 
   20038 /*
   20039 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
   20040 ** return the number of unicode characters in pZ up to (but not including)
   20041 ** the first 0x00 byte. If nByte is not less than zero, return the
   20042 ** number of unicode characters in the first nByte of pZ (or up to
   20043 ** the first 0x00, whichever comes first).
   20044 */
   20045 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
   20046   int r = 0;
   20047   const u8 *z = (const u8*)zIn;
   20048   const u8 *zTerm;
   20049   if( nByte>=0 ){
   20050     zTerm = &z[nByte];
   20051   }else{
   20052     zTerm = (const u8*)(-1);
   20053   }
   20054   assert( z<=zTerm );
   20055   while( *z!=0 && z<zTerm ){
   20056     SQLITE_SKIP_UTF8(z);
   20057     r++;
   20058   }
   20059   return r;
   20060 }
   20061 
   20062 /* This test function is not currently used by the automated test-suite.
   20063 ** Hence it is only available in debug builds.
   20064 */
   20065 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   20066 /*
   20067 ** Translate UTF-8 to UTF-8.
   20068 **
   20069 ** This has the effect of making sure that the string is well-formed
   20070 ** UTF-8.  Miscoded characters are removed.
   20071 **
   20072 ** The translation is done in-place and aborted if the output
   20073 ** overruns the input.
   20074 */
   20075 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
   20076   unsigned char *zOut = zIn;
   20077   unsigned char *zStart = zIn;
   20078   u32 c;
   20079 
   20080   while( zIn[0] && zOut<=zIn ){
   20081     c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
   20082     if( c!=0xfffd ){
   20083       WRITE_UTF8(zOut, c);
   20084     }
   20085   }
   20086   *zOut = 0;
   20087   return (int)(zOut - zStart);
   20088 }
   20089 #endif
   20090 
   20091 #ifndef SQLITE_OMIT_UTF16
   20092 /*
   20093 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
   20094 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
   20095 ** be freed by the calling function.
   20096 **
   20097 ** NULL is returned if there is an allocation error.
   20098 */
   20099 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
   20100   Mem m;
   20101   memset(&m, 0, sizeof(m));
   20102   m.db = db;
   20103   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
   20104   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   20105   if( db->mallocFailed ){
   20106     sqlite3VdbeMemRelease(&m);
   20107     m.z = 0;
   20108   }
   20109   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
   20110   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
   20111   assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
   20112   assert( m.z || db->mallocFailed );
   20113   return m.z;
   20114 }
   20115 
   20116 /*
   20117 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
   20118 ** enc. A pointer to the new string is returned, and the value of *pnOut
   20119 ** is set to the length of the returned string in bytes. The call should
   20120 ** arrange to call sqlite3DbFree() on the returned pointer when it is
   20121 ** no longer required.
   20122 **
   20123 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
   20124 ** flag set.
   20125 */
   20126 #ifdef SQLITE_ENABLE_STAT2
   20127 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
   20128   Mem m;
   20129   memset(&m, 0, sizeof(m));
   20130   m.db = db;
   20131   sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
   20132   if( sqlite3VdbeMemTranslate(&m, enc) ){
   20133     assert( db->mallocFailed );
   20134     return 0;
   20135   }
   20136   assert( m.z==m.zMalloc );
   20137   *pnOut = m.n;
   20138   return m.z;
   20139 }
   20140 #endif
   20141 
   20142 /*
   20143 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
   20144 ** Return the number of bytes in the first nChar unicode characters
   20145 ** in pZ.  nChar must be non-negative.
   20146 */
   20147 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
   20148   int c;
   20149   unsigned char const *z = zIn;
   20150   int n = 0;
   20151 
   20152   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
   20153     while( n<nChar ){
   20154       READ_UTF16BE(z, 1, c);
   20155       n++;
   20156     }
   20157   }else{
   20158     while( n<nChar ){
   20159       READ_UTF16LE(z, 1, c);
   20160       n++;
   20161     }
   20162   }
   20163   return (int)(z-(unsigned char const *)zIn);
   20164 }
   20165 
   20166 #if defined(SQLITE_TEST)
   20167 /*
   20168 ** This routine is called from the TCL test function "translate_selftest".
   20169 ** It checks that the primitives for serializing and deserializing
   20170 ** characters in each encoding are inverses of each other.
   20171 */
   20172 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
   20173   unsigned int i, t;
   20174   unsigned char zBuf[20];
   20175   unsigned char *z;
   20176   int n;
   20177   unsigned int c;
   20178 
   20179   for(i=0; i<0x00110000; i++){
   20180     z = zBuf;
   20181     WRITE_UTF8(z, i);
   20182     n = (int)(z-zBuf);
   20183     assert( n>0 && n<=4 );
   20184     z[0] = 0;
   20185     z = zBuf;
   20186     c = sqlite3Utf8Read(z, (const u8**)&z);
   20187     t = i;
   20188     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
   20189     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
   20190     assert( c==t );
   20191     assert( (z-zBuf)==n );
   20192   }
   20193   for(i=0; i<0x00110000; i++){
   20194     if( i>=0xD800 && i<0xE000 ) continue;
   20195     z = zBuf;
   20196     WRITE_UTF16LE(z, i);
   20197     n = (int)(z-zBuf);
   20198     assert( n>0 && n<=4 );
   20199     z[0] = 0;
   20200     z = zBuf;
   20201     READ_UTF16LE(z, 1, c);
   20202     assert( c==i );
   20203     assert( (z-zBuf)==n );
   20204   }
   20205   for(i=0; i<0x00110000; i++){
   20206     if( i>=0xD800 && i<0xE000 ) continue;
   20207     z = zBuf;
   20208     WRITE_UTF16BE(z, i);
   20209     n = (int)(z-zBuf);
   20210     assert( n>0 && n<=4 );
   20211     z[0] = 0;
   20212     z = zBuf;
   20213     READ_UTF16BE(z, 1, c);
   20214     assert( c==i );
   20215     assert( (z-zBuf)==n );
   20216   }
   20217 }
   20218 #endif /* SQLITE_TEST */
   20219 #endif /* SQLITE_OMIT_UTF16 */
   20220 
   20221 /************** End of utf.c *************************************************/
   20222 /************** Begin file util.c ********************************************/
   20223 /*
   20224 ** 2001 September 15
   20225 **
   20226 ** The author disclaims copyright to this source code.  In place of
   20227 ** a legal notice, here is a blessing:
   20228 **
   20229 **    May you do good and not evil.
   20230 **    May you find forgiveness for yourself and forgive others.
   20231 **    May you share freely, never taking more than you give.
   20232 **
   20233 *************************************************************************
   20234 ** Utility functions used throughout sqlite.
   20235 **
   20236 ** This file contains functions for allocating memory, comparing
   20237 ** strings, and stuff like that.
   20238 **
   20239 */
   20240 #ifdef SQLITE_HAVE_ISNAN
   20241 # include <math.h>
   20242 #endif
   20243 
   20244 /*
   20245 ** Routine needed to support the testcase() macro.
   20246 */
   20247 #ifdef SQLITE_COVERAGE_TEST
   20248 SQLITE_PRIVATE void sqlite3Coverage(int x){
   20249   static unsigned dummy = 0;
   20250   dummy += (unsigned)x;
   20251 }
   20252 #endif
   20253 
   20254 #ifndef SQLITE_OMIT_FLOATING_POINT
   20255 /*
   20256 ** Return true if the floating point value is Not a Number (NaN).
   20257 **
   20258 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
   20259 ** Otherwise, we have our own implementation that works on most systems.
   20260 */
   20261 SQLITE_PRIVATE int sqlite3IsNaN(double x){
   20262   int rc;   /* The value return */
   20263 #if !defined(SQLITE_HAVE_ISNAN)
   20264   /*
   20265   ** Systems that support the isnan() library function should probably
   20266   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
   20267   ** found that many systems do not have a working isnan() function so
   20268   ** this implementation is provided as an alternative.
   20269   **
   20270   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
   20271   ** On the other hand, the use of -ffast-math comes with the following
   20272   ** warning:
   20273   **
   20274   **      This option [-ffast-math] should never be turned on by any
   20275   **      -O option since it can result in incorrect output for programs
   20276   **      which depend on an exact implementation of IEEE or ISO
   20277   **      rules/specifications for math functions.
   20278   **
   20279   ** Under MSVC, this NaN test may fail if compiled with a floating-
   20280   ** point precision mode other than /fp:precise.  From the MSDN
   20281   ** documentation:
   20282   **
   20283   **      The compiler [with /fp:precise] will properly handle comparisons
   20284   **      involving NaN. For example, x != x evaluates to true if x is NaN
   20285   **      ...
   20286   */
   20287 #ifdef __FAST_MATH__
   20288 # error SQLite will not work correctly with the -ffast-math option of GCC.
   20289 #endif
   20290   volatile double y = x;
   20291   volatile double z = y;
   20292   rc = (y!=z);
   20293 #else  /* if defined(SQLITE_HAVE_ISNAN) */
   20294   rc = isnan(x);
   20295 #endif /* SQLITE_HAVE_ISNAN */
   20296   testcase( rc );
   20297   return rc;
   20298 }
   20299 #endif /* SQLITE_OMIT_FLOATING_POINT */
   20300 
   20301 /*
   20302 ** Compute a string length that is limited to what can be stored in
   20303 ** lower 30 bits of a 32-bit signed integer.
   20304 **
   20305 ** The value returned will never be negative.  Nor will it ever be greater
   20306 ** than the actual length of the string.  For very long strings (greater
   20307 ** than 1GiB) the value returned might be less than the true string length.
   20308 */
   20309 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
   20310   const char *z2 = z;
   20311   if( z==0 ) return 0;
   20312   while( *z2 ){ z2++; }
   20313   return 0x3fffffff & (int)(z2 - z);
   20314 }
   20315 
   20316 /*
   20317 ** Set the most recent error code and error string for the sqlite
   20318 ** handle "db". The error code is set to "err_code".
   20319 **
   20320 ** If it is not NULL, string zFormat specifies the format of the
   20321 ** error string in the style of the printf functions: The following
   20322 ** format characters are allowed:
   20323 **
   20324 **      %s      Insert a string
   20325 **      %z      A string that should be freed after use
   20326 **      %d      Insert an integer
   20327 **      %T      Insert a token
   20328 **      %S      Insert the first element of a SrcList
   20329 **
   20330 ** zFormat and any string tokens that follow it are assumed to be
   20331 ** encoded in UTF-8.
   20332 **
   20333 ** To clear the most recent error for sqlite handle "db", sqlite3Error
   20334 ** should be called with err_code set to SQLITE_OK and zFormat set
   20335 ** to NULL.
   20336 */
   20337 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
   20338   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
   20339     db->errCode = err_code;
   20340     if( zFormat ){
   20341       char *z;
   20342       va_list ap;
   20343       va_start(ap, zFormat);
   20344       z = sqlite3VMPrintf(db, zFormat, ap);
   20345       va_end(ap);
   20346       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
   20347     }else{
   20348       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
   20349     }
   20350   }
   20351 }
   20352 
   20353 /*
   20354 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
   20355 ** The following formatting characters are allowed:
   20356 **
   20357 **      %s      Insert a string
   20358 **      %z      A string that should be freed after use
   20359 **      %d      Insert an integer
   20360 **      %T      Insert a token
   20361 **      %S      Insert the first element of a SrcList
   20362 **
   20363 ** This function should be used to report any error that occurs whilst
   20364 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
   20365 ** last thing the sqlite3_prepare() function does is copy the error
   20366 ** stored by this function into the database handle using sqlite3Error().
   20367 ** Function sqlite3Error() should be used during statement execution
   20368 ** (sqlite3_step() etc.).
   20369 */
   20370 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
   20371   char *zMsg;
   20372   va_list ap;
   20373   sqlite3 *db = pParse->db;
   20374   va_start(ap, zFormat);
   20375   zMsg = sqlite3VMPrintf(db, zFormat, ap);
   20376   va_end(ap);
   20377   if( db->suppressErr ){
   20378     sqlite3DbFree(db, zMsg);
   20379   }else{
   20380     pParse->nErr++;
   20381     sqlite3DbFree(db, pParse->zErrMsg);
   20382     pParse->zErrMsg = zMsg;
   20383     pParse->rc = SQLITE_ERROR;
   20384   }
   20385 }
   20386 
   20387 /*
   20388 ** Convert an SQL-style quoted string into a normal string by removing
   20389 ** the quote characters.  The conversion is done in-place.  If the
   20390 ** input does not begin with a quote character, then this routine
   20391 ** is a no-op.
   20392 **
   20393 ** The input string must be zero-terminated.  A new zero-terminator
   20394 ** is added to the dequoted string.
   20395 **
   20396 ** The return value is -1 if no dequoting occurs or the length of the
   20397 ** dequoted string, exclusive of the zero terminator, if dequoting does
   20398 ** occur.
   20399 **
   20400 ** 2002-Feb-14: This routine is extended to remove MS-Access style
   20401 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
   20402 ** "a-b-c".
   20403 */
   20404 SQLITE_PRIVATE int sqlite3Dequote(char *z){
   20405   char quote;
   20406   int i, j;
   20407   if( z==0 ) return -1;
   20408   quote = z[0];
   20409   switch( quote ){
   20410     case '\'':  break;
   20411     case '"':   break;
   20412     case '`':   break;                /* For MySQL compatibility */
   20413     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
   20414     default:    return -1;
   20415   }
   20416   for(i=1, j=0; ALWAYS(z[i]); i++){
   20417     if( z[i]==quote ){
   20418       if( z[i+1]==quote ){
   20419         z[j++] = quote;
   20420         i++;
   20421       }else{
   20422         break;
   20423       }
   20424     }else{
   20425       z[j++] = z[i];
   20426     }
   20427   }
   20428   z[j] = 0;
   20429   return j;
   20430 }
   20431 
   20432 /* Convenient short-hand */
   20433 #define UpperToLower sqlite3UpperToLower
   20434 
   20435 /*
   20436 ** Some systems have stricmp().  Others have strcasecmp().  Because
   20437 ** there is no consistency, we will define our own.
   20438 **
   20439 ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
   20440 ** applications and extensions to compare the contents of two buffers
   20441 ** containing UTF-8 strings in a case-independent fashion, using the same
   20442 ** definition of case independence that SQLite uses internally when
   20443 ** comparing identifiers.
   20444 */
   20445 SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
   20446   register unsigned char *a, *b;
   20447   a = (unsigned char *)zLeft;
   20448   b = (unsigned char *)zRight;
   20449   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   20450   return UpperToLower[*a] - UpperToLower[*b];
   20451 }
   20452 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
   20453   register unsigned char *a, *b;
   20454   a = (unsigned char *)zLeft;
   20455   b = (unsigned char *)zRight;
   20456   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   20457   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
   20458 }
   20459 
   20460 /*
   20461 ** The string z[] is an text representation of a real number.
   20462 ** Convert this string to a double and write it into *pResult.
   20463 **
   20464 ** The string z[] is length bytes in length (bytes, not characters) and
   20465 ** uses the encoding enc.  The string is not necessarily zero-terminated.
   20466 **
   20467 ** Return TRUE if the result is a valid real number (or integer) and FALSE
   20468 ** if the string is empty or contains extraneous text.  Valid numbers
   20469 ** are in one of these formats:
   20470 **
   20471 **    [+-]digits[E[+-]digits]
   20472 **    [+-]digits.[digits][E[+-]digits]
   20473 **    [+-].digits[E[+-]digits]
   20474 **
   20475 ** Leading and trailing whitespace is ignored for the purpose of determining
   20476 ** validity.
   20477 **
   20478 ** If some prefix of the input string is a valid number, this routine
   20479 ** returns FALSE but it still converts the prefix and writes the result
   20480 ** into *pResult.
   20481 */
   20482 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
   20483 #ifndef SQLITE_OMIT_FLOATING_POINT
   20484   int incr = (enc==SQLITE_UTF8?1:2);
   20485   const char *zEnd = z + length;
   20486   /* sign * significand * (10 ^ (esign * exponent)) */
   20487   int sign = 1;    /* sign of significand */
   20488   i64 s = 0;       /* significand */
   20489   int d = 0;       /* adjust exponent for shifting decimal point */
   20490   int esign = 1;   /* sign of exponent */
   20491   int e = 0;       /* exponent */
   20492   int eValid = 1;  /* True exponent is either not used or is well-formed */
   20493   double result;
   20494   int nDigits = 0;
   20495 
   20496   *pResult = 0.0;   /* Default return value, in case of an error */
   20497 
   20498   if( enc==SQLITE_UTF16BE ) z++;
   20499 
   20500   /* skip leading spaces */
   20501   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
   20502   if( z>=zEnd ) return 0;
   20503 
   20504   /* get sign of significand */
   20505   if( *z=='-' ){
   20506     sign = -1;
   20507     z+=incr;
   20508   }else if( *z=='+' ){
   20509     z+=incr;
   20510   }
   20511 
   20512   /* skip leading zeroes */
   20513   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
   20514 
   20515   /* copy max significant digits to significand */
   20516   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
   20517     s = s*10 + (*z - '0');
   20518     z+=incr, nDigits++;
   20519   }
   20520 
   20521   /* skip non-significant significand digits
   20522   ** (increase exponent by d to shift decimal left) */
   20523   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
   20524   if( z>=zEnd ) goto do_atof_calc;
   20525 
   20526   /* if decimal point is present */
   20527   if( *z=='.' ){
   20528     z+=incr;
   20529     /* copy digits from after decimal to significand
   20530     ** (decrease exponent by d to shift decimal right) */
   20531     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
   20532       s = s*10 + (*z - '0');
   20533       z+=incr, nDigits++, d--;
   20534     }
   20535     /* skip non-significant digits */
   20536     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
   20537   }
   20538   if( z>=zEnd ) goto do_atof_calc;
   20539 
   20540   /* if exponent is present */
   20541   if( *z=='e' || *z=='E' ){
   20542     z+=incr;
   20543     eValid = 0;
   20544     if( z>=zEnd ) goto do_atof_calc;
   20545     /* get sign of exponent */
   20546     if( *z=='-' ){
   20547       esign = -1;
   20548       z+=incr;
   20549     }else if( *z=='+' ){
   20550       z+=incr;
   20551     }
   20552     /* copy digits to exponent */
   20553     while( z<zEnd && sqlite3Isdigit(*z) ){
   20554       e = e*10 + (*z - '0');
   20555       z+=incr;
   20556       eValid = 1;
   20557     }
   20558   }
   20559 
   20560   /* skip trailing spaces */
   20561   if( nDigits && eValid ){
   20562     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
   20563   }
   20564 
   20565 do_atof_calc:
   20566   /* adjust exponent by d, and update sign */
   20567   e = (e*esign) + d;
   20568   if( e<0 ) {
   20569     esign = -1;
   20570     e *= -1;
   20571   } else {
   20572     esign = 1;
   20573   }
   20574 
   20575   /* if 0 significand */
   20576   if( !s ) {
   20577     /* In the IEEE 754 standard, zero is signed.
   20578     ** Add the sign if we've seen at least one digit */
   20579     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
   20580   } else {
   20581     /* attempt to reduce exponent */
   20582     if( esign>0 ){
   20583       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
   20584     }else{
   20585       while( !(s%10) && e>0 ) e--,s/=10;
   20586     }
   20587 
   20588     /* adjust the sign of significand */
   20589     s = sign<0 ? -s : s;
   20590 
   20591     /* if exponent, scale significand as appropriate
   20592     ** and store in result. */
   20593     if( e ){
   20594       double scale = 1.0;
   20595       /* attempt to handle extremely small/large numbers better */
   20596       if( e>307 && e<342 ){
   20597         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
   20598         if( esign<0 ){
   20599           result = s / scale;
   20600           result /= 1.0e+308;
   20601         }else{
   20602           result = s * scale;
   20603           result *= 1.0e+308;
   20604         }
   20605       }else{
   20606         /* 1.0e+22 is the largest power of 10 than can be
   20607         ** represented exactly. */
   20608         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
   20609         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
   20610         if( esign<0 ){
   20611           result = s / scale;
   20612         }else{
   20613           result = s * scale;
   20614         }
   20615       }
   20616     } else {
   20617       result = (double)s;
   20618     }
   20619   }
   20620 
   20621   /* store the result */
   20622   *pResult = result;
   20623 
   20624   /* return true if number and no extra non-whitespace chracters after */
   20625   return z>=zEnd && nDigits>0 && eValid;
   20626 #else
   20627   return !sqlite3Atoi64(z, pResult, length, enc);
   20628 #endif /* SQLITE_OMIT_FLOATING_POINT */
   20629 }
   20630 
   20631 /*
   20632 ** Compare the 19-character string zNum against the text representation
   20633 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
   20634 ** if zNum is less than, equal to, or greater than the string.
   20635 ** Note that zNum must contain exactly 19 characters.
   20636 **
   20637 ** Unlike memcmp() this routine is guaranteed to return the difference
   20638 ** in the values of the last digit if the only difference is in the
   20639 ** last digit.  So, for example,
   20640 **
   20641 **      compare2pow63("9223372036854775800", 1)
   20642 **
   20643 ** will return -8.
   20644 */
   20645 static int compare2pow63(const char *zNum, int incr){
   20646   int c = 0;
   20647   int i;
   20648                     /* 012345678901234567 */
   20649   const char *pow63 = "922337203685477580";
   20650   for(i=0; c==0 && i<18; i++){
   20651     c = (zNum[i*incr]-pow63[i])*10;
   20652   }
   20653   if( c==0 ){
   20654     c = zNum[18*incr] - '8';
   20655     testcase( c==(-1) );
   20656     testcase( c==0 );
   20657     testcase( c==(+1) );
   20658   }
   20659   return c;
   20660 }
   20661 
   20662 
   20663 /*
   20664 ** Convert zNum to a 64-bit signed integer.
   20665 **
   20666 ** If the zNum value is representable as a 64-bit twos-complement
   20667 ** integer, then write that value into *pNum and return 0.
   20668 **
   20669 ** If zNum is exactly 9223372036854665808, return 2.  This special
   20670 ** case is broken out because while 9223372036854665808 cannot be a
   20671 ** signed 64-bit integer, its negative -9223372036854665808 can be.
   20672 **
   20673 ** If zNum is too big for a 64-bit integer and is not
   20674 ** 9223372036854665808 then return 1.
   20675 **
   20676 ** length is the number of bytes in the string (bytes, not characters).
   20677 ** The string is not necessarily zero-terminated.  The encoding is
   20678 ** given by enc.
   20679 */
   20680 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
   20681   int incr = (enc==SQLITE_UTF8?1:2);
   20682   u64 u = 0;
   20683   int neg = 0; /* assume positive */
   20684   int i;
   20685   int c = 0;
   20686   const char *zStart;
   20687   const char *zEnd = zNum + length;
   20688   if( enc==SQLITE_UTF16BE ) zNum++;
   20689   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
   20690   if( zNum<zEnd ){
   20691     if( *zNum=='-' ){
   20692       neg = 1;
   20693       zNum+=incr;
   20694     }else if( *zNum=='+' ){
   20695       zNum+=incr;
   20696     }
   20697   }
   20698   zStart = zNum;
   20699   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
   20700   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
   20701     u = u*10 + c - '0';
   20702   }
   20703   if( u>LARGEST_INT64 ){
   20704     *pNum = SMALLEST_INT64;
   20705   }else if( neg ){
   20706     *pNum = -(i64)u;
   20707   }else{
   20708     *pNum = (i64)u;
   20709   }
   20710   testcase( i==18 );
   20711   testcase( i==19 );
   20712   testcase( i==20 );
   20713   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
   20714     /* zNum is empty or contains non-numeric text or is longer
   20715     ** than 19 digits (thus guaranteeing that it is too large) */
   20716     return 1;
   20717   }else if( i<19*incr ){
   20718     /* Less than 19 digits, so we know that it fits in 64 bits */
   20719     assert( u<=LARGEST_INT64 );
   20720     return 0;
   20721   }else{
   20722     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
   20723     c = compare2pow63(zNum, incr);
   20724     if( c<0 ){
   20725       /* zNum is less than 9223372036854775808 so it fits */
   20726       assert( u<=LARGEST_INT64 );
   20727       return 0;
   20728     }else if( c>0 ){
   20729       /* zNum is greater than 9223372036854775808 so it overflows */
   20730       return 1;
   20731     }else{
   20732       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
   20733       ** special case 2 overflow if positive */
   20734       assert( u-1==LARGEST_INT64 );
   20735       assert( (*pNum)==SMALLEST_INT64 );
   20736       return neg ? 0 : 2;
   20737     }
   20738   }
   20739 }
   20740 
   20741 /*
   20742 ** If zNum represents an integer that will fit in 32-bits, then set
   20743 ** *pValue to that integer and return true.  Otherwise return false.
   20744 **
   20745 ** Any non-numeric characters that following zNum are ignored.
   20746 ** This is different from sqlite3Atoi64() which requires the
   20747 ** input number to be zero-terminated.
   20748 */
   20749 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
   20750   sqlite_int64 v = 0;
   20751   int i, c;
   20752   int neg = 0;
   20753   if( zNum[0]=='-' ){
   20754     neg = 1;
   20755     zNum++;
   20756   }else if( zNum[0]=='+' ){
   20757     zNum++;
   20758   }
   20759   while( zNum[0]=='0' ) zNum++;
   20760   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
   20761     v = v*10 + c;
   20762   }
   20763 
   20764   /* The longest decimal representation of a 32 bit integer is 10 digits:
   20765   **
   20766   **             1234567890
   20767   **     2^31 -> 2147483648
   20768   */
   20769   testcase( i==10 );
   20770   if( i>10 ){
   20771     return 0;
   20772   }
   20773   testcase( v-neg==2147483647 );
   20774   if( v-neg>2147483647 ){
   20775     return 0;
   20776   }
   20777   if( neg ){
   20778     v = -v;
   20779   }
   20780   *pValue = (int)v;
   20781   return 1;
   20782 }
   20783 
   20784 /*
   20785 ** Return a 32-bit integer value extracted from a string.  If the
   20786 ** string is not an integer, just return 0.
   20787 */
   20788 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
   20789   int x = 0;
   20790   if( z ) sqlite3GetInt32(z, &x);
   20791   return x;
   20792 }
   20793 
   20794 /*
   20795 ** The variable-length integer encoding is as follows:
   20796 **
   20797 ** KEY:
   20798 **         A = 0xxxxxxx    7 bits of data and one flag bit
   20799 **         B = 1xxxxxxx    7 bits of data and one flag bit
   20800 **         C = xxxxxxxx    8 bits of data
   20801 **
   20802 **  7 bits - A
   20803 ** 14 bits - BA
   20804 ** 21 bits - BBA
   20805 ** 28 bits - BBBA
   20806 ** 35 bits - BBBBA
   20807 ** 42 bits - BBBBBA
   20808 ** 49 bits - BBBBBBA
   20809 ** 56 bits - BBBBBBBA
   20810 ** 64 bits - BBBBBBBBC
   20811 */
   20812 
   20813 /*
   20814 ** Write a 64-bit variable-length integer to memory starting at p[0].
   20815 ** The length of data write will be between 1 and 9 bytes.  The number
   20816 ** of bytes written is returned.
   20817 **
   20818 ** A variable-length integer consists of the lower 7 bits of each byte
   20819 ** for all bytes that have the 8th bit set and one byte with the 8th
   20820 ** bit clear.  Except, if we get to the 9th byte, it stores the full
   20821 ** 8 bits and is the last byte.
   20822 */
   20823 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
   20824   int i, j, n;
   20825   u8 buf[10];
   20826   if( v & (((u64)0xff000000)<<32) ){
   20827     p[8] = (u8)v;
   20828     v >>= 8;
   20829     for(i=7; i>=0; i--){
   20830       p[i] = (u8)((v & 0x7f) | 0x80);
   20831       v >>= 7;
   20832     }
   20833     return 9;
   20834   }
   20835   n = 0;
   20836   do{
   20837     buf[n++] = (u8)((v & 0x7f) | 0x80);
   20838     v >>= 7;
   20839   }while( v!=0 );
   20840   buf[0] &= 0x7f;
   20841   assert( n<=9 );
   20842   for(i=0, j=n-1; j>=0; j--, i++){
   20843     p[i] = buf[j];
   20844   }
   20845   return n;
   20846 }
   20847 
   20848 /*
   20849 ** This routine is a faster version of sqlite3PutVarint() that only
   20850 ** works for 32-bit positive integers and which is optimized for
   20851 ** the common case of small integers.  A MACRO version, putVarint32,
   20852 ** is provided which inlines the single-byte case.  All code should use
   20853 ** the MACRO version as this function assumes the single-byte case has
   20854 ** already been handled.
   20855 */
   20856 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
   20857 #ifndef putVarint32
   20858   if( (v & ~0x7f)==0 ){
   20859     p[0] = v;
   20860     return 1;
   20861   }
   20862 #endif
   20863   if( (v & ~0x3fff)==0 ){
   20864     p[0] = (u8)((v>>7) | 0x80);
   20865     p[1] = (u8)(v & 0x7f);
   20866     return 2;
   20867   }
   20868   return sqlite3PutVarint(p, v);
   20869 }
   20870 
   20871 /*
   20872 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
   20873 ** are defined here rather than simply putting the constant expressions
   20874 ** inline in order to work around bugs in the RVT compiler.
   20875 **
   20876 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
   20877 **
   20878 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
   20879 */
   20880 #define SLOT_2_0     0x001fc07f
   20881 #define SLOT_4_2_0   0xf01fc07f
   20882 
   20883 
   20884 /*
   20885 ** Read a 64-bit variable-length integer from memory starting at p[0].
   20886 ** Return the number of bytes read.  The value is stored in *v.
   20887 */
   20888 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
   20889   u32 a,b,s;
   20890 
   20891   a = *p;
   20892   /* a: p0 (unmasked) */
   20893   if (!(a&0x80))
   20894   {
   20895     *v = a;
   20896     return 1;
   20897   }
   20898 
   20899   p++;
   20900   b = *p;
   20901   /* b: p1 (unmasked) */
   20902   if (!(b&0x80))
   20903   {
   20904     a &= 0x7f;
   20905     a = a<<7;
   20906     a |= b;
   20907     *v = a;
   20908     return 2;
   20909   }
   20910 
   20911   /* Verify that constants are precomputed correctly */
   20912   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
   20913   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
   20914 
   20915   p++;
   20916   a = a<<14;
   20917   a |= *p;
   20918   /* a: p0<<14 | p2 (unmasked) */
   20919   if (!(a&0x80))
   20920   {
   20921     a &= SLOT_2_0;
   20922     b &= 0x7f;
   20923     b = b<<7;
   20924     a |= b;
   20925     *v = a;
   20926     return 3;
   20927   }
   20928 
   20929   /* CSE1 from below */
   20930   a &= SLOT_2_0;
   20931   p++;
   20932   b = b<<14;
   20933   b |= *p;
   20934   /* b: p1<<14 | p3 (unmasked) */
   20935   if (!(b&0x80))
   20936   {
   20937     b &= SLOT_2_0;
   20938     /* moved CSE1 up */
   20939     /* a &= (0x7f<<14)|(0x7f); */
   20940     a = a<<7;
   20941     a |= b;
   20942     *v = a;
   20943     return 4;
   20944   }
   20945 
   20946   /* a: p0<<14 | p2 (masked) */
   20947   /* b: p1<<14 | p3 (unmasked) */
   20948   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   20949   /* moved CSE1 up */
   20950   /* a &= (0x7f<<14)|(0x7f); */
   20951   b &= SLOT_2_0;
   20952   s = a;
   20953   /* s: p0<<14 | p2 (masked) */
   20954 
   20955   p++;
   20956   a = a<<14;
   20957   a |= *p;
   20958   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   20959   if (!(a&0x80))
   20960   {
   20961     /* we can skip these cause they were (effectively) done above in calc'ing s */
   20962     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   20963     /* b &= (0x7f<<14)|(0x7f); */
   20964     b = b<<7;
   20965     a |= b;
   20966     s = s>>18;
   20967     *v = ((u64)s)<<32 | a;
   20968     return 5;
   20969   }
   20970 
   20971   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   20972   s = s<<7;
   20973   s |= b;
   20974   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   20975 
   20976   p++;
   20977   b = b<<14;
   20978   b |= *p;
   20979   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
   20980   if (!(b&0x80))
   20981   {
   20982     /* we can skip this cause it was (effectively) done above in calc'ing s */
   20983     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   20984     a &= SLOT_2_0;
   20985     a = a<<7;
   20986     a |= b;
   20987     s = s>>18;
   20988     *v = ((u64)s)<<32 | a;
   20989     return 6;
   20990   }
   20991 
   20992   p++;
   20993   a = a<<14;
   20994   a |= *p;
   20995   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
   20996   if (!(a&0x80))
   20997   {
   20998     a &= SLOT_4_2_0;
   20999     b &= SLOT_2_0;
   21000     b = b<<7;
   21001     a |= b;
   21002     s = s>>11;
   21003     *v = ((u64)s)<<32 | a;
   21004     return 7;
   21005   }
   21006 
   21007   /* CSE2 from below */
   21008   a &= SLOT_2_0;
   21009   p++;
   21010   b = b<<14;
   21011   b |= *p;
   21012   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
   21013   if (!(b&0x80))
   21014   {
   21015     b &= SLOT_4_2_0;
   21016     /* moved CSE2 up */
   21017     /* a &= (0x7f<<14)|(0x7f); */
   21018     a = a<<7;
   21019     a |= b;
   21020     s = s>>4;
   21021     *v = ((u64)s)<<32 | a;
   21022     return 8;
   21023   }
   21024 
   21025   p++;
   21026   a = a<<15;
   21027   a |= *p;
   21028   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
   21029 
   21030   /* moved CSE2 up */
   21031   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
   21032   b &= SLOT_2_0;
   21033   b = b<<8;
   21034   a |= b;
   21035 
   21036   s = s<<4;
   21037   b = p[-4];
   21038   b &= 0x7f;
   21039   b = b>>3;
   21040   s |= b;
   21041 
   21042   *v = ((u64)s)<<32 | a;
   21043 
   21044   return 9;
   21045 }
   21046 
   21047 /*
   21048 ** Read a 32-bit variable-length integer from memory starting at p[0].
   21049 ** Return the number of bytes read.  The value is stored in *v.
   21050 **
   21051 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
   21052 ** integer, then set *v to 0xffffffff.
   21053 **
   21054 ** A MACRO version, getVarint32, is provided which inlines the
   21055 ** single-byte case.  All code should use the MACRO version as
   21056 ** this function assumes the single-byte case has already been handled.
   21057 */
   21058 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
   21059   u32 a,b;
   21060 
   21061   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
   21062   ** by the getVarin32() macro */
   21063   a = *p;
   21064   /* a: p0 (unmasked) */
   21065 #ifndef getVarint32
   21066   if (!(a&0x80))
   21067   {
   21068     /* Values between 0 and 127 */
   21069     *v = a;
   21070     return 1;
   21071   }
   21072 #endif
   21073 
   21074   /* The 2-byte case */
   21075   p++;
   21076   b = *p;
   21077   /* b: p1 (unmasked) */
   21078   if (!(b&0x80))
   21079   {
   21080     /* Values between 128 and 16383 */
   21081     a &= 0x7f;
   21082     a = a<<7;
   21083     *v = a | b;
   21084     return 2;
   21085   }
   21086 
   21087   /* The 3-byte case */
   21088   p++;
   21089   a = a<<14;
   21090   a |= *p;
   21091   /* a: p0<<14 | p2 (unmasked) */
   21092   if (!(a&0x80))
   21093   {
   21094     /* Values between 16384 and 2097151 */
   21095     a &= (0x7f<<14)|(0x7f);
   21096     b &= 0x7f;
   21097     b = b<<7;
   21098     *v = a | b;
   21099     return 3;
   21100   }
   21101 
   21102   /* A 32-bit varint is used to store size information in btrees.
   21103   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
   21104   ** A 3-byte varint is sufficient, for example, to record the size
   21105   ** of a 1048569-byte BLOB or string.
   21106   **
   21107   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
   21108   ** rare larger cases can be handled by the slower 64-bit varint
   21109   ** routine.
   21110   */
   21111 #if 1
   21112   {
   21113     u64 v64;
   21114     u8 n;
   21115 
   21116     p -= 2;
   21117     n = sqlite3GetVarint(p, &v64);
   21118     assert( n>3 && n<=9 );
   21119     if( (v64 & SQLITE_MAX_U32)!=v64 ){
   21120       *v = 0xffffffff;
   21121     }else{
   21122       *v = (u32)v64;
   21123     }
   21124     return n;
   21125   }
   21126 
   21127 #else
   21128   /* For following code (kept for historical record only) shows an
   21129   ** unrolling for the 3- and 4-byte varint cases.  This code is
   21130   ** slightly faster, but it is also larger and much harder to test.
   21131   */
   21132   p++;
   21133   b = b<<14;
   21134   b |= *p;
   21135   /* b: p1<<14 | p3 (unmasked) */
   21136   if (!(b&0x80))
   21137   {
   21138     /* Values between 2097152 and 268435455 */
   21139     b &= (0x7f<<14)|(0x7f);
   21140     a &= (0x7f<<14)|(0x7f);
   21141     a = a<<7;
   21142     *v = a | b;
   21143     return 4;
   21144   }
   21145 
   21146   p++;
   21147   a = a<<14;
   21148   a |= *p;
   21149   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   21150   if (!(a&0x80))
   21151   {
   21152     /* Values  between 268435456 and 34359738367 */
   21153     a &= SLOT_4_2_0;
   21154     b &= SLOT_4_2_0;
   21155     b = b<<7;
   21156     *v = a | b;
   21157     return 5;
   21158   }
   21159 
   21160   /* We can only reach this point when reading a corrupt database
   21161   ** file.  In that case we are not in any hurry.  Use the (relatively
   21162   ** slow) general-purpose sqlite3GetVarint() routine to extract the
   21163   ** value. */
   21164   {
   21165     u64 v64;
   21166     u8 n;
   21167 
   21168     p -= 4;
   21169     n = sqlite3GetVarint(p, &v64);
   21170     assert( n>5 && n<=9 );
   21171     *v = (u32)v64;
   21172     return n;
   21173   }
   21174 #endif
   21175 }
   21176 
   21177 /*
   21178 ** Return the number of bytes that will be needed to store the given
   21179 ** 64-bit integer.
   21180 */
   21181 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
   21182   int i = 0;
   21183   do{
   21184     i++;
   21185     v >>= 7;
   21186   }while( v!=0 && ALWAYS(i<9) );
   21187   return i;
   21188 }
   21189 
   21190 
   21191 /*
   21192 ** Read or write a four-byte big-endian integer value.
   21193 */
   21194 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
   21195   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
   21196 }
   21197 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
   21198   p[0] = (u8)(v>>24);
   21199   p[1] = (u8)(v>>16);
   21200   p[2] = (u8)(v>>8);
   21201   p[3] = (u8)v;
   21202 }
   21203 
   21204 
   21205 
   21206 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
   21207 /*
   21208 ** Translate a single byte of Hex into an integer.
   21209 ** This routine only works if h really is a valid hexadecimal
   21210 ** character:  0..9a..fA..F
   21211 */
   21212 static u8 hexToInt(int h){
   21213   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
   21214 #ifdef SQLITE_ASCII
   21215   h += 9*(1&(h>>6));
   21216 #endif
   21217 #ifdef SQLITE_EBCDIC
   21218   h += 9*(1&~(h>>4));
   21219 #endif
   21220   return (u8)(h & 0xf);
   21221 }
   21222 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
   21223 
   21224 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
   21225 /*
   21226 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
   21227 ** value.  Return a pointer to its binary value.  Space to hold the
   21228 ** binary value has been obtained from malloc and must be freed by
   21229 ** the calling routine.
   21230 */
   21231 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
   21232   char *zBlob;
   21233   int i;
   21234 
   21235   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
   21236   n--;
   21237   if( zBlob ){
   21238     for(i=0; i<n; i+=2){
   21239       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
   21240     }
   21241     zBlob[i/2] = 0;
   21242   }
   21243   return zBlob;
   21244 }
   21245 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
   21246 
   21247 /*
   21248 ** Log an error that is an API call on a connection pointer that should
   21249 ** not have been used.  The "type" of connection pointer is given as the
   21250 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
   21251 */
   21252 static void logBadConnection(const char *zType){
   21253   sqlite3_log(SQLITE_MISUSE,
   21254      "API call with %s database connection pointer",
   21255      zType
   21256   );
   21257 }
   21258 
   21259 /*
   21260 ** Check to make sure we have a valid db pointer.  This test is not
   21261 ** foolproof but it does provide some measure of protection against
   21262 ** misuse of the interface such as passing in db pointers that are
   21263 ** NULL or which have been previously closed.  If this routine returns
   21264 ** 1 it means that the db pointer is valid and 0 if it should not be
   21265 ** dereferenced for any reason.  The calling function should invoke
   21266 ** SQLITE_MISUSE immediately.
   21267 **
   21268 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
   21269 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
   21270 ** open properly and is not fit for general use but which can be
   21271 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
   21272 */
   21273 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
   21274   u32 magic;
   21275   if( db==0 ){
   21276     logBadConnection("NULL");
   21277     return 0;
   21278   }
   21279   magic = db->magic;
   21280   if( magic!=SQLITE_MAGIC_OPEN ){
   21281     if( sqlite3SafetyCheckSickOrOk(db) ){
   21282       testcase( sqlite3GlobalConfig.xLog!=0 );
   21283       logBadConnection("unopened");
   21284     }
   21285     return 0;
   21286   }else{
   21287     return 1;
   21288   }
   21289 }
   21290 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
   21291   u32 magic;
   21292   magic = db->magic;
   21293   if( magic!=SQLITE_MAGIC_SICK &&
   21294       magic!=SQLITE_MAGIC_OPEN &&
   21295       magic!=SQLITE_MAGIC_BUSY ){
   21296     testcase( sqlite3GlobalConfig.xLog!=0 );
   21297     logBadConnection("invalid");
   21298     return 0;
   21299   }else{
   21300     return 1;
   21301   }
   21302 }
   21303 
   21304 /*
   21305 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
   21306 ** the other 64-bit signed integer at *pA and store the result in *pA.
   21307 ** Return 0 on success.  Or if the operation would have resulted in an
   21308 ** overflow, leave *pA unchanged and return 1.
   21309 */
   21310 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
   21311   i64 iA = *pA;
   21312   testcase( iA==0 ); testcase( iA==1 );
   21313   testcase( iB==-1 ); testcase( iB==0 );
   21314   if( iB>=0 ){
   21315     testcase( iA>0 && LARGEST_INT64 - iA == iB );
   21316     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
   21317     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
   21318     *pA += iB;
   21319   }else{
   21320     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
   21321     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
   21322     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
   21323     *pA += iB;
   21324   }
   21325   return 0;
   21326 }
   21327 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
   21328   testcase( iB==SMALLEST_INT64+1 );
   21329   if( iB==SMALLEST_INT64 ){
   21330     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
   21331     if( (*pA)>=0 ) return 1;
   21332     *pA -= iB;
   21333     return 0;
   21334   }else{
   21335     return sqlite3AddInt64(pA, -iB);
   21336   }
   21337 }
   21338 #define TWOPOWER32 (((i64)1)<<32)
   21339 #define TWOPOWER31 (((i64)1)<<31)
   21340 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
   21341   i64 iA = *pA;
   21342   i64 iA1, iA0, iB1, iB0, r;
   21343 
   21344   iA1 = iA/TWOPOWER32;
   21345   iA0 = iA % TWOPOWER32;
   21346   iB1 = iB/TWOPOWER32;
   21347   iB0 = iB % TWOPOWER32;
   21348   if( iA1*iB1 != 0 ) return 1;
   21349   assert( iA1*iB0==0 || iA0*iB1==0 );
   21350   r = iA1*iB0 + iA0*iB1;
   21351   testcase( r==(-TWOPOWER31)-1 );
   21352   testcase( r==(-TWOPOWER31) );
   21353   testcase( r==TWOPOWER31 );
   21354   testcase( r==TWOPOWER31-1 );
   21355   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
   21356   r *= TWOPOWER32;
   21357   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
   21358   *pA = r;
   21359   return 0;
   21360 }
   21361 
   21362 /*
   21363 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
   21364 ** if the integer has a value of -2147483648, return +2147483647
   21365 */
   21366 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
   21367   if( x>=0 ) return x;
   21368   if( x==(int)0x80000000 ) return 0x7fffffff;
   21369   return -x;
   21370 }
   21371 
   21372 /************** End of util.c ************************************************/
   21373 /************** Begin file hash.c ********************************************/
   21374 /*
   21375 ** 2001 September 22
   21376 **
   21377 ** The author disclaims copyright to this source code.  In place of
   21378 ** a legal notice, here is a blessing:
   21379 **
   21380 **    May you do good and not evil.
   21381 **    May you find forgiveness for yourself and forgive others.
   21382 **    May you share freely, never taking more than you give.
   21383 **
   21384 *************************************************************************
   21385 ** This is the implementation of generic hash-tables
   21386 ** used in SQLite.
   21387 */
   21388 
   21389 /* Turn bulk memory into a hash table object by initializing the
   21390 ** fields of the Hash structure.
   21391 **
   21392 ** "pNew" is a pointer to the hash table that is to be initialized.
   21393 */
   21394 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
   21395   assert( pNew!=0 );
   21396   pNew->first = 0;
   21397   pNew->count = 0;
   21398   pNew->htsize = 0;
   21399   pNew->ht = 0;
   21400 }
   21401 
   21402 /* Remove all entries from a hash table.  Reclaim all memory.
   21403 ** Call this routine to delete a hash table or to reset a hash table
   21404 ** to the empty state.
   21405 */
   21406 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
   21407   HashElem *elem;         /* For looping over all elements of the table */
   21408 
   21409   assert( pH!=0 );
   21410   elem = pH->first;
   21411   pH->first = 0;
   21412   sqlite3_free(pH->ht);
   21413   pH->ht = 0;
   21414   pH->htsize = 0;
   21415   while( elem ){
   21416     HashElem *next_elem = elem->next;
   21417     sqlite3_free(elem);
   21418     elem = next_elem;
   21419   }
   21420   pH->count = 0;
   21421 }
   21422 
   21423 /*
   21424 ** The hashing function.
   21425 */
   21426 static unsigned int strHash(const char *z, int nKey){
   21427   int h = 0;
   21428   assert( nKey>=0 );
   21429   while( nKey > 0  ){
   21430     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
   21431     nKey--;
   21432   }
   21433   return h;
   21434 }
   21435 
   21436 
   21437 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
   21438 ** insert pNew into the pEntry hash bucket.
   21439 */
   21440 static void insertElement(
   21441   Hash *pH,              /* The complete hash table */
   21442   struct _ht *pEntry,    /* The entry into which pNew is inserted */
   21443   HashElem *pNew         /* The element to be inserted */
   21444 ){
   21445   HashElem *pHead;       /* First element already in pEntry */
   21446   if( pEntry ){
   21447     pHead = pEntry->count ? pEntry->chain : 0;
   21448     pEntry->count++;
   21449     pEntry->chain = pNew;
   21450   }else{
   21451     pHead = 0;
   21452   }
   21453   if( pHead ){
   21454     pNew->next = pHead;
   21455     pNew->prev = pHead->prev;
   21456     if( pHead->prev ){ pHead->prev->next = pNew; }
   21457     else             { pH->first = pNew; }
   21458     pHead->prev = pNew;
   21459   }else{
   21460     pNew->next = pH->first;
   21461     if( pH->first ){ pH->first->prev = pNew; }
   21462     pNew->prev = 0;
   21463     pH->first = pNew;
   21464   }
   21465 }
   21466 
   21467 
   21468 /* Resize the hash table so that it cantains "new_size" buckets.
   21469 **
   21470 ** The hash table might fail to resize if sqlite3_malloc() fails or
   21471 ** if the new size is the same as the prior size.
   21472 ** Return TRUE if the resize occurs and false if not.
   21473 */
   21474 static int rehash(Hash *pH, unsigned int new_size){
   21475   struct _ht *new_ht;            /* The new hash table */
   21476   HashElem *elem, *next_elem;    /* For looping over existing elements */
   21477 
   21478 #if SQLITE_MALLOC_SOFT_LIMIT>0
   21479   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
   21480     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
   21481   }
   21482   if( new_size==pH->htsize ) return 0;
   21483 #endif
   21484 
   21485   /* The inability to allocates space for a larger hash table is
   21486   ** a performance hit but it is not a fatal error.  So mark the
   21487   ** allocation as a benign.
   21488   */
   21489   sqlite3BeginBenignMalloc();
   21490   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
   21491   sqlite3EndBenignMalloc();
   21492 
   21493   if( new_ht==0 ) return 0;
   21494   sqlite3_free(pH->ht);
   21495   pH->ht = new_ht;
   21496   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
   21497   memset(new_ht, 0, new_size*sizeof(struct _ht));
   21498   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
   21499     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
   21500     next_elem = elem->next;
   21501     insertElement(pH, &new_ht[h], elem);
   21502   }
   21503   return 1;
   21504 }
   21505 
   21506 /* This function (for internal use only) locates an element in an
   21507 ** hash table that matches the given key.  The hash for this key has
   21508 ** already been computed and is passed as the 4th parameter.
   21509 */
   21510 static HashElem *findElementGivenHash(
   21511   const Hash *pH,     /* The pH to be searched */
   21512   const char *pKey,   /* The key we are searching for */
   21513   int nKey,           /* Bytes in key (not counting zero terminator) */
   21514   unsigned int h      /* The hash for this key. */
   21515 ){
   21516   HashElem *elem;                /* Used to loop thru the element list */
   21517   int count;                     /* Number of elements left to test */
   21518 
   21519   if( pH->ht ){
   21520     struct _ht *pEntry = &pH->ht[h];
   21521     elem = pEntry->chain;
   21522     count = pEntry->count;
   21523   }else{
   21524     elem = pH->first;
   21525     count = pH->count;
   21526   }
   21527   while( count-- && ALWAYS(elem) ){
   21528     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
   21529       return elem;
   21530     }
   21531     elem = elem->next;
   21532   }
   21533   return 0;
   21534 }
   21535 
   21536 /* Remove a single entry from the hash table given a pointer to that
   21537 ** element and a hash on the element's key.
   21538 */
   21539 static void removeElementGivenHash(
   21540   Hash *pH,         /* The pH containing "elem" */
   21541   HashElem* elem,   /* The element to be removed from the pH */
   21542   unsigned int h    /* Hash value for the element */
   21543 ){
   21544   struct _ht *pEntry;
   21545   if( elem->prev ){
   21546     elem->prev->next = elem->next;
   21547   }else{
   21548     pH->first = elem->next;
   21549   }
   21550   if( elem->next ){
   21551     elem->next->prev = elem->prev;
   21552   }
   21553   if( pH->ht ){
   21554     pEntry = &pH->ht[h];
   21555     if( pEntry->chain==elem ){
   21556       pEntry->chain = elem->next;
   21557     }
   21558     pEntry->count--;
   21559     assert( pEntry->count>=0 );
   21560   }
   21561   sqlite3_free( elem );
   21562   pH->count--;
   21563   if( pH->count<=0 ){
   21564     assert( pH->first==0 );
   21565     assert( pH->count==0 );
   21566     sqlite3HashClear(pH);
   21567   }
   21568 }
   21569 
   21570 /* Attempt to locate an element of the hash table pH with a key
   21571 ** that matches pKey,nKey.  Return the data for this element if it is
   21572 ** found, or NULL if there is no match.
   21573 */
   21574 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
   21575   HashElem *elem;    /* The element that matches key */
   21576   unsigned int h;    /* A hash on key */
   21577 
   21578   assert( pH!=0 );
   21579   assert( pKey!=0 );
   21580   assert( nKey>=0 );
   21581   if( pH->ht ){
   21582     h = strHash(pKey, nKey) % pH->htsize;
   21583   }else{
   21584     h = 0;
   21585   }
   21586   elem = findElementGivenHash(pH, pKey, nKey, h);
   21587   return elem ? elem->data : 0;
   21588 }
   21589 
   21590 /* Insert an element into the hash table pH.  The key is pKey,nKey
   21591 ** and the data is "data".
   21592 **
   21593 ** If no element exists with a matching key, then a new
   21594 ** element is created and NULL is returned.
   21595 **
   21596 ** If another element already exists with the same key, then the
   21597 ** new data replaces the old data and the old data is returned.
   21598 ** The key is not copied in this instance.  If a malloc fails, then
   21599 ** the new data is returned and the hash table is unchanged.
   21600 **
   21601 ** If the "data" parameter to this function is NULL, then the
   21602 ** element corresponding to "key" is removed from the hash table.
   21603 */
   21604 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
   21605   unsigned int h;       /* the hash of the key modulo hash table size */
   21606   HashElem *elem;       /* Used to loop thru the element list */
   21607   HashElem *new_elem;   /* New element added to the pH */
   21608 
   21609   assert( pH!=0 );
   21610   assert( pKey!=0 );
   21611   assert( nKey>=0 );
   21612   if( pH->htsize ){
   21613     h = strHash(pKey, nKey) % pH->htsize;
   21614   }else{
   21615     h = 0;
   21616   }
   21617   elem = findElementGivenHash(pH,pKey,nKey,h);
   21618   if( elem ){
   21619     void *old_data = elem->data;
   21620     if( data==0 ){
   21621       removeElementGivenHash(pH,elem,h);
   21622     }else{
   21623       elem->data = data;
   21624       elem->pKey = pKey;
   21625       assert(nKey==elem->nKey);
   21626     }
   21627     return old_data;
   21628   }
   21629   if( data==0 ) return 0;
   21630   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
   21631   if( new_elem==0 ) return data;
   21632   new_elem->pKey = pKey;
   21633   new_elem->nKey = nKey;
   21634   new_elem->data = data;
   21635   pH->count++;
   21636   if( pH->count>=10 && pH->count > 2*pH->htsize ){
   21637     if( rehash(pH, pH->count*2) ){
   21638       assert( pH->htsize>0 );
   21639       h = strHash(pKey, nKey) % pH->htsize;
   21640     }
   21641   }
   21642   if( pH->ht ){
   21643     insertElement(pH, &pH->ht[h], new_elem);
   21644   }else{
   21645     insertElement(pH, 0, new_elem);
   21646   }
   21647   return 0;
   21648 }
   21649 
   21650 /************** End of hash.c ************************************************/
   21651 /************** Begin file opcodes.c *****************************************/
   21652 /* Automatically generated.  Do not edit */
   21653 /* See the mkopcodec.awk script for details. */
   21654 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
   21655 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
   21656  static const char *const azName[] = { "?",
   21657      /*   1 */ "Goto",
   21658      /*   2 */ "Gosub",
   21659      /*   3 */ "Return",
   21660      /*   4 */ "Yield",
   21661      /*   5 */ "HaltIfNull",
   21662      /*   6 */ "Halt",
   21663      /*   7 */ "Integer",
   21664      /*   8 */ "Int64",
   21665      /*   9 */ "String",
   21666      /*  10 */ "Null",
   21667      /*  11 */ "Blob",
   21668      /*  12 */ "Variable",
   21669      /*  13 */ "Move",
   21670      /*  14 */ "Copy",
   21671      /*  15 */ "SCopy",
   21672      /*  16 */ "ResultRow",
   21673      /*  17 */ "CollSeq",
   21674      /*  18 */ "Function",
   21675      /*  19 */ "Not",
   21676      /*  20 */ "AddImm",
   21677      /*  21 */ "MustBeInt",
   21678      /*  22 */ "RealAffinity",
   21679      /*  23 */ "Permutation",
   21680      /*  24 */ "Compare",
   21681      /*  25 */ "Jump",
   21682      /*  26 */ "If",
   21683      /*  27 */ "IfNot",
   21684      /*  28 */ "Column",
   21685      /*  29 */ "Affinity",
   21686      /*  30 */ "MakeRecord",
   21687      /*  31 */ "Count",
   21688      /*  32 */ "Savepoint",
   21689      /*  33 */ "AutoCommit",
   21690      /*  34 */ "Transaction",
   21691      /*  35 */ "ReadCookie",
   21692      /*  36 */ "SetCookie",
   21693      /*  37 */ "VerifyCookie",
   21694      /*  38 */ "OpenRead",
   21695      /*  39 */ "OpenWrite",
   21696      /*  40 */ "OpenAutoindex",
   21697      /*  41 */ "OpenEphemeral",
   21698      /*  42 */ "OpenPseudo",
   21699      /*  43 */ "Close",
   21700      /*  44 */ "SeekLt",
   21701      /*  45 */ "SeekLe",
   21702      /*  46 */ "SeekGe",
   21703      /*  47 */ "SeekGt",
   21704      /*  48 */ "Seek",
   21705      /*  49 */ "NotFound",
   21706      /*  50 */ "Found",
   21707      /*  51 */ "IsUnique",
   21708      /*  52 */ "NotExists",
   21709      /*  53 */ "Sequence",
   21710      /*  54 */ "NewRowid",
   21711      /*  55 */ "Insert",
   21712      /*  56 */ "InsertInt",
   21713      /*  57 */ "Delete",
   21714      /*  58 */ "ResetCount",
   21715      /*  59 */ "RowKey",
   21716      /*  60 */ "RowData",
   21717      /*  61 */ "Rowid",
   21718      /*  62 */ "NullRow",
   21719      /*  63 */ "Last",
   21720      /*  64 */ "Sort",
   21721      /*  65 */ "Rewind",
   21722      /*  66 */ "Prev",
   21723      /*  67 */ "Next",
   21724      /*  68 */ "Or",
   21725      /*  69 */ "And",
   21726      /*  70 */ "IdxInsert",
   21727      /*  71 */ "IdxDelete",
   21728      /*  72 */ "IdxRowid",
   21729      /*  73 */ "IsNull",
   21730      /*  74 */ "NotNull",
   21731      /*  75 */ "Ne",
   21732      /*  76 */ "Eq",
   21733      /*  77 */ "Gt",
   21734      /*  78 */ "Le",
   21735      /*  79 */ "Lt",
   21736      /*  80 */ "Ge",
   21737      /*  81 */ "IdxLT",
   21738      /*  82 */ "BitAnd",
   21739      /*  83 */ "BitOr",
   21740      /*  84 */ "ShiftLeft",
   21741      /*  85 */ "ShiftRight",
   21742      /*  86 */ "Add",
   21743      /*  87 */ "Subtract",
   21744      /*  88 */ "Multiply",
   21745      /*  89 */ "Divide",
   21746      /*  90 */ "Remainder",
   21747      /*  91 */ "Concat",
   21748      /*  92 */ "IdxGE",
   21749      /*  93 */ "BitNot",
   21750      /*  94 */ "String8",
   21751      /*  95 */ "Destroy",
   21752      /*  96 */ "Clear",
   21753      /*  97 */ "CreateIndex",
   21754      /*  98 */ "CreateTable",
   21755      /*  99 */ "ParseSchema",
   21756      /* 100 */ "LoadAnalysis",
   21757      /* 101 */ "DropTable",
   21758      /* 102 */ "DropIndex",
   21759      /* 103 */ "DropTrigger",
   21760      /* 104 */ "IntegrityCk",
   21761      /* 105 */ "RowSetAdd",
   21762      /* 106 */ "RowSetRead",
   21763      /* 107 */ "RowSetTest",
   21764      /* 108 */ "Program",
   21765      /* 109 */ "Param",
   21766      /* 110 */ "FkCounter",
   21767      /* 111 */ "FkIfZero",
   21768      /* 112 */ "MemMax",
   21769      /* 113 */ "IfPos",
   21770      /* 114 */ "IfNeg",
   21771      /* 115 */ "IfZero",
   21772      /* 116 */ "AggStep",
   21773      /* 117 */ "AggFinal",
   21774      /* 118 */ "Checkpoint",
   21775      /* 119 */ "JournalMode",
   21776      /* 120 */ "Vacuum",
   21777      /* 121 */ "IncrVacuum",
   21778      /* 122 */ "Expire",
   21779      /* 123 */ "TableLock",
   21780      /* 124 */ "VBegin",
   21781      /* 125 */ "VCreate",
   21782      /* 126 */ "VDestroy",
   21783      /* 127 */ "VOpen",
   21784      /* 128 */ "VFilter",
   21785      /* 129 */ "VColumn",
   21786      /* 130 */ "Real",
   21787      /* 131 */ "VNext",
   21788      /* 132 */ "VRename",
   21789      /* 133 */ "VUpdate",
   21790      /* 134 */ "Pagecount",
   21791      /* 135 */ "MaxPgcnt",
   21792      /* 136 */ "Trace",
   21793      /* 137 */ "Noop",
   21794      /* 138 */ "Explain",
   21795      /* 139 */ "NotUsed_139",
   21796      /* 140 */ "NotUsed_140",
   21797      /* 141 */ "ToText",
   21798      /* 142 */ "ToBlob",
   21799      /* 143 */ "ToNumeric",
   21800      /* 144 */ "ToInt",
   21801      /* 145 */ "ToReal",
   21802   };
   21803   return azName[i];
   21804 }
   21805 #endif
   21806 
   21807 /************** End of opcodes.c *********************************************/
   21808 /************** Begin file os_os2.c ******************************************/
   21809 /*
   21810 ** 2006 Feb 14
   21811 **
   21812 ** The author disclaims copyright to this source code.  In place of
   21813 ** a legal notice, here is a blessing:
   21814 **
   21815 **    May you do good and not evil.
   21816 **    May you find forgiveness for yourself and forgive others.
   21817 **    May you share freely, never taking more than you give.
   21818 **
   21819 ******************************************************************************
   21820 **
   21821 ** This file contains code that is specific to OS/2.
   21822 */
   21823 
   21824 
   21825 #if SQLITE_OS_OS2
   21826 
   21827 /*
   21828 ** A Note About Memory Allocation:
   21829 **
   21830 ** This driver uses malloc()/free() directly rather than going through
   21831 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
   21832 ** are designed for use on embedded systems where memory is scarce and
   21833 ** malloc failures happen frequently.  OS/2 does not typically run on
   21834 ** embedded systems, and when it does the developers normally have bigger
   21835 ** problems to worry about than running out of memory.  So there is not
   21836 ** a compelling need to use the wrappers.
   21837 **
   21838 ** But there is a good reason to not use the wrappers.  If we use the
   21839 ** wrappers then we will get simulated malloc() failures within this
   21840 ** driver.  And that causes all kinds of problems for our tests.  We
   21841 ** could enhance SQLite to deal with simulated malloc failures within
   21842 ** the OS driver, but the code to deal with those failure would not
   21843 ** be exercised on Linux (which does not need to malloc() in the driver)
   21844 ** and so we would have difficulty writing coverage tests for that
   21845 ** code.  Better to leave the code out, we think.
   21846 **
   21847 ** The point of this discussion is as follows:  When creating a new
   21848 ** OS layer for an embedded system, if you use this file as an example,
   21849 ** avoid the use of malloc()/free().  Those routines work ok on OS/2
   21850 ** desktops but not so well in embedded systems.
   21851 */
   21852 
   21853 /*
   21854 ** Macros used to determine whether or not to use threads.
   21855 */
   21856 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
   21857 # define SQLITE_OS2_THREADS 1
   21858 #endif
   21859 
   21860 /*
   21861 ** Include code that is common to all os_*.c files
   21862 */
   21863 /************** Include os_common.h in the middle of os_os2.c ****************/
   21864 /************** Begin file os_common.h ***************************************/
   21865 /*
   21866 ** 2004 May 22
   21867 **
   21868 ** The author disclaims copyright to this source code.  In place of
   21869 ** a legal notice, here is a blessing:
   21870 **
   21871 **    May you do good and not evil.
   21872 **    May you find forgiveness for yourself and forgive others.
   21873 **    May you share freely, never taking more than you give.
   21874 **
   21875 ******************************************************************************
   21876 **
   21877 ** This file contains macros and a little bit of code that is common to
   21878 ** all of the platform-specific files (os_*.c) and is #included into those
   21879 ** files.
   21880 **
   21881 ** This file should be #included by the os_*.c files only.  It is not a
   21882 ** general purpose header file.
   21883 */
   21884 #ifndef _OS_COMMON_H_
   21885 #define _OS_COMMON_H_
   21886 
   21887 /*
   21888 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   21889 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   21890 ** switch.  The following code should catch this problem at compile-time.
   21891 */
   21892 #ifdef MEMORY_DEBUG
   21893 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   21894 #endif
   21895 
   21896 #ifdef SQLITE_DEBUG
   21897 SQLITE_PRIVATE int sqlite3OSTrace = 0;
   21898 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   21899 #else
   21900 #define OSTRACE(X)
   21901 #endif
   21902 
   21903 /*
   21904 ** Macros for performance tracing.  Normally turned off.  Only works
   21905 ** on i486 hardware.
   21906 */
   21907 #ifdef SQLITE_PERFORMANCE_TRACE
   21908 
   21909 /*
   21910 ** hwtime.h contains inline assembler code for implementing
   21911 ** high-performance timing routines.
   21912 */
   21913 /************** Include hwtime.h in the middle of os_common.h ****************/
   21914 /************** Begin file hwtime.h ******************************************/
   21915 /*
   21916 ** 2008 May 27
   21917 **
   21918 ** The author disclaims copyright to this source code.  In place of
   21919 ** a legal notice, here is a blessing:
   21920 **
   21921 **    May you do good and not evil.
   21922 **    May you find forgiveness for yourself and forgive others.
   21923 **    May you share freely, never taking more than you give.
   21924 **
   21925 ******************************************************************************
   21926 **
   21927 ** This file contains inline asm code for retrieving "high-performance"
   21928 ** counters for x86 class CPUs.
   21929 */
   21930 #ifndef _HWTIME_H_
   21931 #define _HWTIME_H_
   21932 
   21933 /*
   21934 ** The following routine only works on pentium-class (or newer) processors.
   21935 ** It uses the RDTSC opcode to read the cycle count value out of the
   21936 ** processor and returns that value.  This can be used for high-res
   21937 ** profiling.
   21938 */
   21939 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   21940       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   21941 
   21942   #if defined(__GNUC__)
   21943 
   21944   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   21945      unsigned int lo, hi;
   21946      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   21947      return (sqlite_uint64)hi << 32 | lo;
   21948   }
   21949 
   21950   #elif defined(_MSC_VER)
   21951 
   21952   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   21953      __asm {
   21954         rdtsc
   21955         ret       ; return value at EDX:EAX
   21956      }
   21957   }
   21958 
   21959   #endif
   21960 
   21961 #elif (defined(__GNUC__) && defined(__x86_64__))
   21962 
   21963   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   21964       unsigned long val;
   21965       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   21966       return val;
   21967   }
   21968 
   21969 #elif (defined(__GNUC__) && defined(__ppc__))
   21970 
   21971   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   21972       unsigned long long retval;
   21973       unsigned long junk;
   21974       __asm__ __volatile__ ("\n\
   21975           1:      mftbu   %1\n\
   21976                   mftb    %L0\n\
   21977                   mftbu   %0\n\
   21978                   cmpw    %0,%1\n\
   21979                   bne     1b"
   21980                   : "=r" (retval), "=r" (junk));
   21981       return retval;
   21982   }
   21983 
   21984 #else
   21985 
   21986   #error Need implementation of sqlite3Hwtime() for your platform.
   21987 
   21988   /*
   21989   ** To compile without implementing sqlite3Hwtime() for your platform,
   21990   ** you can remove the above #error and use the following
   21991   ** stub function.  You will lose timing support for many
   21992   ** of the debugging and testing utilities, but it should at
   21993   ** least compile and run.
   21994   */
   21995 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   21996 
   21997 #endif
   21998 
   21999 #endif /* !defined(_HWTIME_H_) */
   22000 
   22001 /************** End of hwtime.h **********************************************/
   22002 /************** Continuing where we left off in os_common.h ******************/
   22003 
   22004 static sqlite_uint64 g_start;
   22005 static sqlite_uint64 g_elapsed;
   22006 #define TIMER_START       g_start=sqlite3Hwtime()
   22007 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   22008 #define TIMER_ELAPSED     g_elapsed
   22009 #else
   22010 #define TIMER_START
   22011 #define TIMER_END
   22012 #define TIMER_ELAPSED     ((sqlite_uint64)0)
   22013 #endif
   22014 
   22015 /*
   22016 ** If we compile with the SQLITE_TEST macro set, then the following block
   22017 ** of code will give us the ability to simulate a disk I/O error.  This
   22018 ** is used for testing the I/O recovery logic.
   22019 */
   22020 #ifdef SQLITE_TEST
   22021 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   22022 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   22023 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   22024 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   22025 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   22026 SQLITE_API int sqlite3_diskfull_pending = 0;
   22027 SQLITE_API int sqlite3_diskfull = 0;
   22028 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   22029 #define SimulateIOError(CODE)  \
   22030   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   22031        || sqlite3_io_error_pending-- == 1 )  \
   22032               { local_ioerr(); CODE; }
   22033 static void local_ioerr(){
   22034   IOTRACE(("IOERR\n"));
   22035   sqlite3_io_error_hit++;
   22036   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   22037 }
   22038 #define SimulateDiskfullError(CODE) \
   22039    if( sqlite3_diskfull_pending ){ \
   22040      if( sqlite3_diskfull_pending == 1 ){ \
   22041        local_ioerr(); \
   22042        sqlite3_diskfull = 1; \
   22043        sqlite3_io_error_hit = 1; \
   22044        CODE; \
   22045      }else{ \
   22046        sqlite3_diskfull_pending--; \
   22047      } \
   22048    }
   22049 #else
   22050 #define SimulateIOErrorBenign(X)
   22051 #define SimulateIOError(A)
   22052 #define SimulateDiskfullError(A)
   22053 #endif
   22054 
   22055 /*
   22056 ** When testing, keep a count of the number of open files.
   22057 */
   22058 #ifdef SQLITE_TEST
   22059 SQLITE_API int sqlite3_open_file_count = 0;
   22060 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   22061 #else
   22062 #define OpenCounter(X)
   22063 #endif
   22064 
   22065 #endif /* !defined(_OS_COMMON_H_) */
   22066 
   22067 /************** End of os_common.h *******************************************/
   22068 /************** Continuing where we left off in os_os2.c *********************/
   22069 
   22070 /* Forward references */
   22071 typedef struct os2File os2File;         /* The file structure */
   22072 typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
   22073 typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
   22074 
   22075 /*
   22076 ** The os2File structure is subclass of sqlite3_file specific for the OS/2
   22077 ** protability layer.
   22078 */
   22079 struct os2File {
   22080   const sqlite3_io_methods *pMethod;  /* Always the first entry */
   22081   HFILE h;                  /* Handle for accessing the file */
   22082   int flags;                /* Flags provided to os2Open() */
   22083   int locktype;             /* Type of lock currently held on this file */
   22084   int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
   22085   char *zFullPathCp;        /* Full path name of this file */
   22086   os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
   22087 };
   22088 
   22089 #define LOCK_TIMEOUT 10L /* the default locking timeout */
   22090 
   22091 /*
   22092 ** Missing from some versions of the OS/2 toolkit -
   22093 ** used to allocate from high memory if possible
   22094 */
   22095 #ifndef OBJ_ANY
   22096 # define OBJ_ANY 0x00000400
   22097 #endif
   22098 
   22099 /*****************************************************************************
   22100 ** The next group of routines implement the I/O methods specified
   22101 ** by the sqlite3_io_methods object.
   22102 ******************************************************************************/
   22103 
   22104 /*
   22105 ** Close a file.
   22106 */
   22107 static int os2Close( sqlite3_file *id ){
   22108   APIRET rc;
   22109   os2File *pFile = (os2File*)id;
   22110 
   22111   assert( id!=0 );
   22112   OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
   22113 
   22114   rc = DosClose( pFile->h );
   22115 
   22116   if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
   22117     DosForceDelete( (PSZ)pFile->zFullPathCp );
   22118 
   22119   free( pFile->zFullPathCp );
   22120   pFile->zFullPathCp = NULL;
   22121   pFile->locktype = NO_LOCK;
   22122   pFile->h = (HFILE)-1;
   22123   pFile->flags = 0;
   22124 
   22125   OpenCounter( -1 );
   22126   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   22127 }
   22128 
   22129 /*
   22130 ** Read data from a file into a buffer.  Return SQLITE_OK if all
   22131 ** bytes were read successfully and SQLITE_IOERR if anything goes
   22132 ** wrong.
   22133 */
   22134 static int os2Read(
   22135   sqlite3_file *id,               /* File to read from */
   22136   void *pBuf,                     /* Write content into this buffer */
   22137   int amt,                        /* Number of bytes to read */
   22138   sqlite3_int64 offset            /* Begin reading at this offset */
   22139 ){
   22140   ULONG fileLocation = 0L;
   22141   ULONG got;
   22142   os2File *pFile = (os2File*)id;
   22143   assert( id!=0 );
   22144   SimulateIOError( return SQLITE_IOERR_READ );
   22145   OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
   22146   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   22147     return SQLITE_IOERR;
   22148   }
   22149   if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
   22150     return SQLITE_IOERR_READ;
   22151   }
   22152   if( got == (ULONG)amt )
   22153     return SQLITE_OK;
   22154   else {
   22155     /* Unread portions of the input buffer must be zero-filled */
   22156     memset(&((char*)pBuf)[got], 0, amt-got);
   22157     return SQLITE_IOERR_SHORT_READ;
   22158   }
   22159 }
   22160 
   22161 /*
   22162 ** Write data from a buffer into a file.  Return SQLITE_OK on success
   22163 ** or some other error code on failure.
   22164 */
   22165 static int os2Write(
   22166   sqlite3_file *id,               /* File to write into */
   22167   const void *pBuf,               /* The bytes to be written */
   22168   int amt,                        /* Number of bytes to write */
   22169   sqlite3_int64 offset            /* Offset into the file to begin writing at */
   22170 ){
   22171   ULONG fileLocation = 0L;
   22172   APIRET rc = NO_ERROR;
   22173   ULONG wrote;
   22174   os2File *pFile = (os2File*)id;
   22175   assert( id!=0 );
   22176   SimulateIOError( return SQLITE_IOERR_WRITE );
   22177   SimulateDiskfullError( return SQLITE_FULL );
   22178   OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
   22179   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   22180     return SQLITE_IOERR;
   22181   }
   22182   assert( amt>0 );
   22183   while( amt > 0 &&
   22184          ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
   22185          wrote > 0
   22186   ){
   22187     amt -= wrote;
   22188     pBuf = &((char*)pBuf)[wrote];
   22189   }
   22190 
   22191   return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
   22192 }
   22193 
   22194 /*
   22195 ** Truncate an open file to a specified size
   22196 */
   22197 static int os2Truncate( sqlite3_file *id, i64 nByte ){
   22198   APIRET rc;
   22199   os2File *pFile = (os2File*)id;
   22200   assert( id!=0 );
   22201   OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
   22202   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
   22203 
   22204   /* If the user has configured a chunk-size for this file, truncate the
   22205   ** file so that it consists of an integer number of chunks (i.e. the
   22206   ** actual file size after the operation may be larger than the requested
   22207   ** size).
   22208   */
   22209   if( pFile->szChunk ){
   22210     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   22211   }
   22212 
   22213   rc = DosSetFileSize( pFile->h, nByte );
   22214   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
   22215 }
   22216 
   22217 #ifdef SQLITE_TEST
   22218 /*
   22219 ** Count the number of fullsyncs and normal syncs.  This is used to test
   22220 ** that syncs and fullsyncs are occuring at the right times.
   22221 */
   22222 SQLITE_API int sqlite3_sync_count = 0;
   22223 SQLITE_API int sqlite3_fullsync_count = 0;
   22224 #endif
   22225 
   22226 /*
   22227 ** Make sure all writes to a particular file are committed to disk.
   22228 */
   22229 static int os2Sync( sqlite3_file *id, int flags ){
   22230   os2File *pFile = (os2File*)id;
   22231   OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
   22232 #ifdef SQLITE_TEST
   22233   if( flags & SQLITE_SYNC_FULL){
   22234     sqlite3_fullsync_count++;
   22235   }
   22236   sqlite3_sync_count++;
   22237 #endif
   22238   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   22239   ** no-op
   22240   */
   22241 #ifdef SQLITE_NO_SYNC
   22242   UNUSED_PARAMETER(pFile);
   22243   return SQLITE_OK;
   22244 #else
   22245   return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   22246 #endif
   22247 }
   22248 
   22249 /*
   22250 ** Determine the current size of a file in bytes
   22251 */
   22252 static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
   22253   APIRET rc = NO_ERROR;
   22254   FILESTATUS3 fsts3FileInfo;
   22255   memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
   22256   assert( id!=0 );
   22257   SimulateIOError( return SQLITE_IOERR_FSTAT );
   22258   rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
   22259   if( rc == NO_ERROR ){
   22260     *pSize = fsts3FileInfo.cbFile;
   22261     return SQLITE_OK;
   22262   }else{
   22263     return SQLITE_IOERR_FSTAT;
   22264   }
   22265 }
   22266 
   22267 /*
   22268 ** Acquire a reader lock.
   22269 */
   22270 static int getReadLock( os2File *pFile ){
   22271   FILELOCK  LockArea,
   22272             UnlockArea;
   22273   APIRET res;
   22274   memset(&LockArea, 0, sizeof(LockArea));
   22275   memset(&UnlockArea, 0, sizeof(UnlockArea));
   22276   LockArea.lOffset = SHARED_FIRST;
   22277   LockArea.lRange = SHARED_SIZE;
   22278   UnlockArea.lOffset = 0L;
   22279   UnlockArea.lRange = 0L;
   22280   res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   22281   OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
   22282   return res;
   22283 }
   22284 
   22285 /*
   22286 ** Undo a readlock
   22287 */
   22288 static int unlockReadLock( os2File *id ){
   22289   FILELOCK  LockArea,
   22290             UnlockArea;
   22291   APIRET res;
   22292   memset(&LockArea, 0, sizeof(LockArea));
   22293   memset(&UnlockArea, 0, sizeof(UnlockArea));
   22294   LockArea.lOffset = 0L;
   22295   LockArea.lRange = 0L;
   22296   UnlockArea.lOffset = SHARED_FIRST;
   22297   UnlockArea.lRange = SHARED_SIZE;
   22298   res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   22299   OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
   22300   return res;
   22301 }
   22302 
   22303 /*
   22304 ** Lock the file with the lock specified by parameter locktype - one
   22305 ** of the following:
   22306 **
   22307 **     (1) SHARED_LOCK
   22308 **     (2) RESERVED_LOCK
   22309 **     (3) PENDING_LOCK
   22310 **     (4) EXCLUSIVE_LOCK
   22311 **
   22312 ** Sometimes when requesting one lock state, additional lock states
   22313 ** are inserted in between.  The locking might fail on one of the later
   22314 ** transitions leaving the lock state different from what it started but
   22315 ** still short of its goal.  The following chart shows the allowed
   22316 ** transitions and the inserted intermediate states:
   22317 **
   22318 **    UNLOCKED -> SHARED
   22319 **    SHARED -> RESERVED
   22320 **    SHARED -> (PENDING) -> EXCLUSIVE
   22321 **    RESERVED -> (PENDING) -> EXCLUSIVE
   22322 **    PENDING -> EXCLUSIVE
   22323 **
   22324 ** This routine will only increase a lock.  The os2Unlock() routine
   22325 ** erases all locks at once and returns us immediately to locking level 0.
   22326 ** It is not possible to lower the locking level one step at a time.  You
   22327 ** must go straight to locking level 0.
   22328 */
   22329 static int os2Lock( sqlite3_file *id, int locktype ){
   22330   int rc = SQLITE_OK;       /* Return code from subroutines */
   22331   APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
   22332   int newLocktype;       /* Set pFile->locktype to this value before exiting */
   22333   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   22334   FILELOCK  LockArea,
   22335             UnlockArea;
   22336   os2File *pFile = (os2File*)id;
   22337   memset(&LockArea, 0, sizeof(LockArea));
   22338   memset(&UnlockArea, 0, sizeof(UnlockArea));
   22339   assert( pFile!=0 );
   22340   OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
   22341 
   22342   /* If there is already a lock of this type or more restrictive on the
   22343   ** os2File, do nothing. Don't use the end_lock: exit path, as
   22344   ** sqlite3_mutex_enter() hasn't been called yet.
   22345   */
   22346   if( pFile->locktype>=locktype ){
   22347     OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
   22348     return SQLITE_OK;
   22349   }
   22350 
   22351   /* Make sure the locking sequence is correct
   22352   */
   22353   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
   22354   assert( locktype!=PENDING_LOCK );
   22355   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
   22356 
   22357   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
   22358   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   22359   ** the PENDING_LOCK byte is temporary.
   22360   */
   22361   newLocktype = pFile->locktype;
   22362   if( pFile->locktype==NO_LOCK
   22363       || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
   22364   ){
   22365     LockArea.lOffset = PENDING_BYTE;
   22366     LockArea.lRange = 1L;
   22367     UnlockArea.lOffset = 0L;
   22368     UnlockArea.lRange = 0L;
   22369 
   22370     /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
   22371     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
   22372     if( res == NO_ERROR ){
   22373       gotPendingLock = 1;
   22374       OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
   22375     }
   22376   }
   22377 
   22378   /* Acquire a shared lock
   22379   */
   22380   if( locktype==SHARED_LOCK && res == NO_ERROR ){
   22381     assert( pFile->locktype==NO_LOCK );
   22382     res = getReadLock(pFile);
   22383     if( res == NO_ERROR ){
   22384       newLocktype = SHARED_LOCK;
   22385     }
   22386     OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
   22387   }
   22388 
   22389   /* Acquire a RESERVED lock
   22390   */
   22391   if( locktype==RESERVED_LOCK && res == NO_ERROR ){
   22392     assert( pFile->locktype==SHARED_LOCK );
   22393     LockArea.lOffset = RESERVED_BYTE;
   22394     LockArea.lRange = 1L;
   22395     UnlockArea.lOffset = 0L;
   22396     UnlockArea.lRange = 0L;
   22397     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22398     if( res == NO_ERROR ){
   22399       newLocktype = RESERVED_LOCK;
   22400     }
   22401     OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
   22402   }
   22403 
   22404   /* Acquire a PENDING lock
   22405   */
   22406   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   22407     newLocktype = PENDING_LOCK;
   22408     gotPendingLock = 0;
   22409     OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
   22410                pFile->h ));
   22411   }
   22412 
   22413   /* Acquire an EXCLUSIVE lock
   22414   */
   22415   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   22416     assert( pFile->locktype>=SHARED_LOCK );
   22417     res = unlockReadLock(pFile);
   22418     OSTRACE(( "unreadlock = %d\n", res ));
   22419     LockArea.lOffset = SHARED_FIRST;
   22420     LockArea.lRange = SHARED_SIZE;
   22421     UnlockArea.lOffset = 0L;
   22422     UnlockArea.lRange = 0L;
   22423     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22424     if( res == NO_ERROR ){
   22425       newLocktype = EXCLUSIVE_LOCK;
   22426     }else{
   22427       OSTRACE(( "OS/2 error-code = %d\n", res ));
   22428       getReadLock(pFile);
   22429     }
   22430     OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
   22431   }
   22432 
   22433   /* If we are holding a PENDING lock that ought to be released, then
   22434   ** release it now.
   22435   */
   22436   if( gotPendingLock && locktype==SHARED_LOCK ){
   22437     int r;
   22438     LockArea.lOffset = 0L;
   22439     LockArea.lRange = 0L;
   22440     UnlockArea.lOffset = PENDING_BYTE;
   22441     UnlockArea.lRange = 1L;
   22442     r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22443     OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
   22444   }
   22445 
   22446   /* Update the state of the lock has held in the file descriptor then
   22447   ** return the appropriate result code.
   22448   */
   22449   if( res == NO_ERROR ){
   22450     rc = SQLITE_OK;
   22451   }else{
   22452     OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   22453               locktype, newLocktype ));
   22454     rc = SQLITE_BUSY;
   22455   }
   22456   pFile->locktype = newLocktype;
   22457   OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
   22458   return rc;
   22459 }
   22460 
   22461 /*
   22462 ** This routine checks if there is a RESERVED lock held on the specified
   22463 ** file by this or any other process. If such a lock is held, return
   22464 ** non-zero, otherwise zero.
   22465 */
   22466 static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
   22467   int r = 0;
   22468   os2File *pFile = (os2File*)id;
   22469   assert( pFile!=0 );
   22470   if( pFile->locktype>=RESERVED_LOCK ){
   22471     r = 1;
   22472     OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
   22473   }else{
   22474     FILELOCK  LockArea,
   22475               UnlockArea;
   22476     APIRET rc = NO_ERROR;
   22477     memset(&LockArea, 0, sizeof(LockArea));
   22478     memset(&UnlockArea, 0, sizeof(UnlockArea));
   22479     LockArea.lOffset = RESERVED_BYTE;
   22480     LockArea.lRange = 1L;
   22481     UnlockArea.lOffset = 0L;
   22482     UnlockArea.lRange = 0L;
   22483     rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22484     OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
   22485     if( rc == NO_ERROR ){
   22486       APIRET rcu = NO_ERROR; /* return code for unlocking */
   22487       LockArea.lOffset = 0L;
   22488       LockArea.lRange = 0L;
   22489       UnlockArea.lOffset = RESERVED_BYTE;
   22490       UnlockArea.lRange = 1L;
   22491       rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22492       OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
   22493     }
   22494     r = !(rc == NO_ERROR);
   22495     OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
   22496   }
   22497   *pOut = r;
   22498   return SQLITE_OK;
   22499 }
   22500 
   22501 /*
   22502 ** Lower the locking level on file descriptor id to locktype.  locktype
   22503 ** must be either NO_LOCK or SHARED_LOCK.
   22504 **
   22505 ** If the locking level of the file descriptor is already at or below
   22506 ** the requested locking level, this routine is a no-op.
   22507 **
   22508 ** It is not possible for this routine to fail if the second argument
   22509 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
   22510 ** might return SQLITE_IOERR;
   22511 */
   22512 static int os2Unlock( sqlite3_file *id, int locktype ){
   22513   int type;
   22514   os2File *pFile = (os2File*)id;
   22515   APIRET rc = SQLITE_OK;
   22516   APIRET res = NO_ERROR;
   22517   FILELOCK  LockArea,
   22518             UnlockArea;
   22519   memset(&LockArea, 0, sizeof(LockArea));
   22520   memset(&UnlockArea, 0, sizeof(UnlockArea));
   22521   assert( pFile!=0 );
   22522   assert( locktype<=SHARED_LOCK );
   22523   OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
   22524   type = pFile->locktype;
   22525   if( type>=EXCLUSIVE_LOCK ){
   22526     LockArea.lOffset = 0L;
   22527     LockArea.lRange = 0L;
   22528     UnlockArea.lOffset = SHARED_FIRST;
   22529     UnlockArea.lRange = SHARED_SIZE;
   22530     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22531     OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
   22532     if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
   22533       /* This should never happen.  We should always be able to
   22534       ** reacquire the read lock */
   22535       OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
   22536       rc = SQLITE_IOERR_UNLOCK;
   22537     }
   22538   }
   22539   if( type>=RESERVED_LOCK ){
   22540     LockArea.lOffset = 0L;
   22541     LockArea.lRange = 0L;
   22542     UnlockArea.lOffset = RESERVED_BYTE;
   22543     UnlockArea.lRange = 1L;
   22544     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22545     OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
   22546   }
   22547   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
   22548     res = unlockReadLock(pFile);
   22549     OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
   22550               pFile->h, type, locktype, res ));
   22551   }
   22552   if( type>=PENDING_LOCK ){
   22553     LockArea.lOffset = 0L;
   22554     LockArea.lRange = 0L;
   22555     UnlockArea.lOffset = PENDING_BYTE;
   22556     UnlockArea.lRange = 1L;
   22557     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   22558     OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
   22559   }
   22560   pFile->locktype = locktype;
   22561   OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
   22562   return rc;
   22563 }
   22564 
   22565 /*
   22566 ** Control and query of the open file handle.
   22567 */
   22568 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
   22569   switch( op ){
   22570     case SQLITE_FCNTL_LOCKSTATE: {
   22571       *(int*)pArg = ((os2File*)id)->locktype;
   22572       OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
   22573                 ((os2File*)id)->h, ((os2File*)id)->locktype ));
   22574       return SQLITE_OK;
   22575     }
   22576     case SQLITE_FCNTL_CHUNK_SIZE: {
   22577       ((os2File*)id)->szChunk = *(int*)pArg;
   22578       return SQLITE_OK;
   22579     }
   22580     case SQLITE_FCNTL_SIZE_HINT: {
   22581       sqlite3_int64 sz = *(sqlite3_int64*)pArg;
   22582       SimulateIOErrorBenign(1);
   22583       os2Truncate(id, sz);
   22584       SimulateIOErrorBenign(0);
   22585       return SQLITE_OK;
   22586     }
   22587     case SQLITE_FCNTL_SYNC_OMITTED: {
   22588       return SQLITE_OK;
   22589     }
   22590   }
   22591   return SQLITE_NOTFOUND;
   22592 }
   22593 
   22594 /*
   22595 ** Return the sector size in bytes of the underlying block device for
   22596 ** the specified file. This is almost always 512 bytes, but may be
   22597 ** larger for some devices.
   22598 **
   22599 ** SQLite code assumes this function cannot fail. It also assumes that
   22600 ** if two files are created in the same file-system directory (i.e.
   22601 ** a database and its journal file) that the sector size will be the
   22602 ** same for both.
   22603 */
   22604 static int os2SectorSize(sqlite3_file *id){
   22605   UNUSED_PARAMETER(id);
   22606   return SQLITE_DEFAULT_SECTOR_SIZE;
   22607 }
   22608 
   22609 /*
   22610 ** Return a vector of device characteristics.
   22611 */
   22612 static int os2DeviceCharacteristics(sqlite3_file *id){
   22613   UNUSED_PARAMETER(id);
   22614   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
   22615 }
   22616 
   22617 
   22618 /*
   22619 ** Character set conversion objects used by conversion routines.
   22620 */
   22621 static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
   22622 static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
   22623 
   22624 /*
   22625 ** Helper function to initialize the conversion objects from and to UTF-8.
   22626 */
   22627 static void initUconvObjects( void ){
   22628   if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
   22629     ucUtf8 = NULL;
   22630   if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
   22631     uclCp = NULL;
   22632 }
   22633 
   22634 /*
   22635 ** Helper function to free the conversion objects from and to UTF-8.
   22636 */
   22637 static void freeUconvObjects( void ){
   22638   if ( ucUtf8 )
   22639     UniFreeUconvObject( ucUtf8 );
   22640   if ( uclCp )
   22641     UniFreeUconvObject( uclCp );
   22642   ucUtf8 = NULL;
   22643   uclCp = NULL;
   22644 }
   22645 
   22646 /*
   22647 ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
   22648 ** The two-step process: first convert the incoming UTF-8 string
   22649 ** into UCS-2 and then from UCS-2 to the current codepage.
   22650 ** The returned char pointer has to be freed.
   22651 */
   22652 static char *convertUtf8PathToCp( const char *in ){
   22653   UniChar tempPath[CCHMAXPATH];
   22654   char *out = (char *)calloc( CCHMAXPATH, 1 );
   22655 
   22656   if( !out )
   22657     return NULL;
   22658 
   22659   if( !ucUtf8 || !uclCp )
   22660     initUconvObjects();
   22661 
   22662   /* determine string for the conversion of UTF-8 which is CP1208 */
   22663   if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   22664     return out; /* if conversion fails, return the empty string */
   22665 
   22666   /* conversion for current codepage which can be used for paths */
   22667   UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
   22668 
   22669   return out;
   22670 }
   22671 
   22672 /*
   22673 ** Helper function to convert filenames from local codepage to UTF-8.
   22674 ** The two-step process: first convert the incoming codepage-specific
   22675 ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
   22676 ** The returned char pointer has to be freed.
   22677 **
   22678 ** This function is non-static to be able to use this in shell.c and
   22679 ** similar applications that take command line arguments.
   22680 */
   22681 char *convertCpPathToUtf8( const char *in ){
   22682   UniChar tempPath[CCHMAXPATH];
   22683   char *out = (char *)calloc( CCHMAXPATH, 1 );
   22684 
   22685   if( !out )
   22686     return NULL;
   22687 
   22688   if( !ucUtf8 || !uclCp )
   22689     initUconvObjects();
   22690 
   22691   /* conversion for current codepage which can be used for paths */
   22692   if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   22693     return out; /* if conversion fails, return the empty string */
   22694 
   22695   /* determine string for the conversion of UTF-8 which is CP1208 */
   22696   UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
   22697 
   22698   return out;
   22699 }
   22700 
   22701 
   22702 #ifndef SQLITE_OMIT_WAL
   22703 
   22704 /*
   22705 ** Use main database file for interprocess locking. If un-defined
   22706 ** a separate file is created for this purpose. The file will be
   22707 ** used only to set file locks. There will be no data written to it.
   22708 */
   22709 #define SQLITE_OS2_NO_WAL_LOCK_FILE
   22710 
   22711 #if 0
   22712 static void _ERR_TRACE( const char *fmt, ... ) {
   22713   va_list  ap;
   22714   va_start(ap, fmt);
   22715   vfprintf(stderr, fmt, ap);
   22716   fflush(stderr);
   22717 }
   22718 #define ERR_TRACE(rc, msg)        \
   22719         if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
   22720 #else
   22721 #define ERR_TRACE(rc, msg)
   22722 #endif
   22723 
   22724 /*
   22725 ** Helper functions to obtain and relinquish the global mutex. The
   22726 ** global mutex is used to protect os2ShmNodeList.
   22727 **
   22728 ** Function os2ShmMutexHeld() is used to assert() that the global mutex
   22729 ** is held when required. This function is only used as part of assert()
   22730 ** statements. e.g.
   22731 **
   22732 **   os2ShmEnterMutex()
   22733 **     assert( os2ShmMutexHeld() );
   22734 **   os2ShmLeaveMutex()
   22735 */
   22736 static void os2ShmEnterMutex(void){
   22737   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   22738 }
   22739 static void os2ShmLeaveMutex(void){
   22740   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   22741 }
   22742 #ifdef SQLITE_DEBUG
   22743 static int os2ShmMutexHeld(void) {
   22744   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   22745 }
   22746 int GetCurrentProcessId(void) {
   22747   PPIB pib;
   22748   DosGetInfoBlocks(NULL, &pib);
   22749   return (int)pib->pib_ulpid;
   22750 }
   22751 #endif
   22752 
   22753 /*
   22754 ** Object used to represent a the shared memory area for a single log file.
   22755 ** When multiple threads all reference the same log-summary, each thread has
   22756 ** its own os2File object, but they all point to a single instance of this
   22757 ** object.  In other words, each log-summary is opened only once per process.
   22758 **
   22759 ** os2ShmMutexHeld() must be true when creating or destroying
   22760 ** this object or while reading or writing the following fields:
   22761 **
   22762 **      nRef
   22763 **      pNext
   22764 **
   22765 ** The following fields are read-only after the object is created:
   22766 **
   22767 **      szRegion
   22768 **      hLockFile
   22769 **      shmBaseName
   22770 **
   22771 ** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
   22772 ** os2ShmMutexHeld() is true when reading or writing any other field
   22773 ** in this structure.
   22774 **
   22775 */
   22776 struct os2ShmNode {
   22777   sqlite3_mutex *mutex;      /* Mutex to access this object */
   22778   os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
   22779 
   22780   int szRegion;              /* Size of shared-memory regions */
   22781 
   22782   int nRegion;               /* Size of array apRegion */
   22783   void **apRegion;           /* Array of pointers to shared-memory regions */
   22784 
   22785   int nRef;                  /* Number of os2ShmLink objects pointing to this */
   22786   os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
   22787 
   22788   HFILE hLockFile;           /* File used for inter-process memory locking */
   22789   char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
   22790 };
   22791 
   22792 
   22793 /*
   22794 ** Structure used internally by this VFS to record the state of an
   22795 ** open shared memory connection.
   22796 **
   22797 ** The following fields are initialized when this object is created and
   22798 ** are read-only thereafter:
   22799 **
   22800 **    os2Shm.pShmNode
   22801 **    os2Shm.id
   22802 **
   22803 ** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
   22804 ** while accessing any read/write fields.
   22805 */
   22806 struct os2ShmLink {
   22807   os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
   22808   os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
   22809   u32 sharedMask;            /* Mask of shared locks held */
   22810   u32 exclMask;              /* Mask of exclusive locks held */
   22811 #ifdef SQLITE_DEBUG
   22812   u8 id;                     /* Id of this connection with its os2ShmNode */
   22813 #endif
   22814 };
   22815 
   22816 
   22817 /*
   22818 ** A global list of all os2ShmNode objects.
   22819 **
   22820 ** The os2ShmMutexHeld() must be true while reading or writing this list.
   22821 */
   22822 static os2ShmNode *os2ShmNodeList = NULL;
   22823 
   22824 /*
   22825 ** Constants used for locking
   22826 */
   22827 #ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
   22828 #define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
   22829 #else
   22830 #define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
   22831 #endif
   22832 
   22833 #define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   22834 
   22835 /*
   22836 ** Apply advisory locks for all n bytes beginning at ofst.
   22837 */
   22838 #define _SHM_UNLCK  1   /* no lock */
   22839 #define _SHM_RDLCK  2   /* shared lock, no wait */
   22840 #define _SHM_WRLCK  3   /* exlusive lock, no wait */
   22841 #define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
   22842 static int os2ShmSystemLock(
   22843   os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
   22844   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
   22845   int ofst,             /* Offset to first byte to be locked/unlocked */
   22846   int nByte             /* Number of bytes to lock or unlock */
   22847 ){
   22848   APIRET rc;
   22849   FILELOCK area;
   22850   ULONG mode, timeout;
   22851 
   22852   /* Access to the os2ShmNode object is serialized by the caller */
   22853   assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
   22854 
   22855   mode = 1;     /* shared lock */
   22856   timeout = 0;  /* no wait */
   22857   area.lOffset = ofst;
   22858   area.lRange = nByte;
   22859 
   22860   switch( lockType ) {
   22861     case _SHM_WRLCK_WAIT:
   22862       timeout = (ULONG)-1;      /* wait forever */
   22863     case _SHM_WRLCK:
   22864       mode = 0;                 /* exclusive lock */
   22865     case _SHM_RDLCK:
   22866       rc = DosSetFileLocks(pNode->hLockFile,
   22867                            NULL, &area, timeout, mode);
   22868       break;
   22869     /* case _SHM_UNLCK: */
   22870     default:
   22871       rc = DosSetFileLocks(pNode->hLockFile,
   22872                            &area, NULL, 0, 0);
   22873       break;
   22874   }
   22875 
   22876   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
   22877            pNode->hLockFile,
   22878            rc==SQLITE_OK ? "ok" : "failed",
   22879            lockType==_SHM_UNLCK ? "Unlock" : "Lock",
   22880            rc));
   22881 
   22882   ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
   22883 
   22884   return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
   22885 }
   22886 
   22887 /*
   22888 ** Find an os2ShmNode in global list or allocate a new one, if not found.
   22889 **
   22890 ** This is not a VFS shared-memory method; it is a utility function called
   22891 ** by VFS shared-memory methods.
   22892 */
   22893 static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
   22894   os2ShmLink *pLink;
   22895   os2ShmNode *pNode;
   22896   int cbShmName, rc = SQLITE_OK;
   22897   char shmName[CCHMAXPATH + 30];
   22898 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
   22899   ULONG action;
   22900 #endif
   22901 
   22902   /* We need some additional space at the end to append the region number */
   22903   cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
   22904   if( cbShmName >= CCHMAXPATH-8 )
   22905     return SQLITE_IOERR_SHMOPEN;
   22906 
   22907   /* Replace colon in file name to form a valid shared memory name */
   22908   shmName[10+1] = '!';
   22909 
   22910   /* Allocate link object (we free it later in case of failure) */
   22911   pLink = sqlite3_malloc( sizeof(*pLink) );
   22912   if( !pLink )
   22913     return SQLITE_NOMEM;
   22914 
   22915   /* Access node list */
   22916   os2ShmEnterMutex();
   22917 
   22918   /* Find node by it's shared memory base name */
   22919   for( pNode = os2ShmNodeList;
   22920        pNode && stricmp(shmName, pNode->shmBaseName) != 0;
   22921        pNode = pNode->pNext )   ;
   22922 
   22923   /* Not found: allocate a new node */
   22924   if( !pNode ) {
   22925     pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
   22926     if( pNode ) {
   22927       memset(pNode, 0, sizeof(*pNode) );
   22928       pNode->szRegion = szRegion;
   22929       pNode->hLockFile = (HFILE)-1;
   22930       strcpy(pNode->shmBaseName, shmName);
   22931 
   22932 #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
   22933       if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
   22934 #else
   22935       sprintf(shmName, "%s-lck", fd->zFullPathCp);
   22936       if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
   22937                   OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
   22938                   OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
   22939                   OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
   22940                   NULL) != 0 ) {
   22941 #endif
   22942         sqlite3_free(pNode);
   22943         rc = SQLITE_IOERR;
   22944       } else {
   22945         pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   22946         if( !pNode->mutex ) {
   22947           sqlite3_free(pNode);
   22948           rc = SQLITE_NOMEM;
   22949         }
   22950       }
   22951     } else {
   22952       rc = SQLITE_NOMEM;
   22953     }
   22954 
   22955     if( rc == SQLITE_OK ) {
   22956       pNode->pNext = os2ShmNodeList;
   22957       os2ShmNodeList = pNode;
   22958     } else {
   22959       pNode = NULL;
   22960     }
   22961   } else if( pNode->szRegion != szRegion ) {
   22962     rc = SQLITE_IOERR_SHMSIZE;
   22963     pNode = NULL;
   22964   }
   22965 
   22966   if( pNode ) {
   22967     sqlite3_mutex_enter(pNode->mutex);
   22968 
   22969     memset(pLink, 0, sizeof(*pLink));
   22970 
   22971     pLink->pShmNode = pNode;
   22972     pLink->pNext = pNode->pFirst;
   22973     pNode->pFirst = pLink;
   22974     pNode->nRef++;
   22975 
   22976     fd->pShmLink = pLink;
   22977 
   22978     sqlite3_mutex_leave(pNode->mutex);
   22979 
   22980   } else {
   22981     /* Error occured. Free our link object. */
   22982     sqlite3_free(pLink);
   22983   }
   22984 
   22985   os2ShmLeaveMutex();
   22986 
   22987   ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))
   22988 
   22989   return rc;
   22990 }
   22991 
   22992 /*
   22993 ** Purge the os2ShmNodeList list of all entries with nRef==0.
   22994 **
   22995 ** This is not a VFS shared-memory method; it is a utility function called
   22996 ** by VFS shared-memory methods.
   22997 */
   22998 static void os2PurgeShmNodes( int deleteFlag ) {
   22999   os2ShmNode *pNode;
   23000   os2ShmNode **ppNode;
   23001 
   23002   os2ShmEnterMutex();
   23003 
   23004   ppNode = &os2ShmNodeList;
   23005 
   23006   while( *ppNode ) {
   23007     pNode = *ppNode;
   23008 
   23009     if( pNode->nRef == 0 ) {
   23010       *ppNode = pNode->pNext;
   23011 
   23012       if( pNode->apRegion ) {
   23013         /* Prevent other processes from resizing the shared memory */
   23014         os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
   23015 
   23016         while( pNode->nRegion-- ) {
   23017 #ifdef SQLITE_DEBUG
   23018           int rc =
   23019 #endif
   23020           DosFreeMem(pNode->apRegion[pNode->nRegion]);
   23021 
   23022           OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
   23023                   (int)GetCurrentProcessId(), pNode->nRegion,
   23024                   rc == 0 ? "ok" : "failed"));
   23025         }
   23026 
   23027         /* Allow other processes to resize the shared memory */
   23028         os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
   23029 
   23030         sqlite3_free(pNode->apRegion);
   23031       }
   23032 
   23033       DosClose(pNode->hLockFile);
   23034 
   23035 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
   23036       if( deleteFlag ) {
   23037          char fileName[CCHMAXPATH];
   23038          /* Skip "\\SHAREMEM\\" */
   23039          sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
   23040          /* restore colon */
   23041          fileName[1] = ':';
   23042 
   23043          DosForceDelete(fileName);
   23044       }
   23045 #endif
   23046 
   23047       sqlite3_mutex_free(pNode->mutex);
   23048 
   23049       sqlite3_free(pNode);
   23050 
   23051     } else {
   23052       ppNode = &pNode->pNext;
   23053     }
   23054   }
   23055 
   23056   os2ShmLeaveMutex();
   23057 }
   23058 
   23059 /*
   23060 ** This function is called to obtain a pointer to region iRegion of the
   23061 ** shared-memory associated with the database file id. Shared-memory regions
   23062 ** are numbered starting from zero. Each shared-memory region is szRegion
   23063 ** bytes in size.
   23064 **
   23065 ** If an error occurs, an error code is returned and *pp is set to NULL.
   23066 **
   23067 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
   23068 ** region has not been allocated (by any client, including one running in a
   23069 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
   23070 ** bExtend is non-zero and the requested shared-memory region has not yet
   23071 ** been allocated, it is allocated by this function.
   23072 **
   23073 ** If the shared-memory region has already been allocated or is allocated by
   23074 ** this call as described above, then it is mapped into this processes
   23075 ** address space (if it is not already), *pp is set to point to the mapped
   23076 ** memory and SQLITE_OK returned.
   23077 */
   23078 static int os2ShmMap(
   23079   sqlite3_file *id,               /* Handle open on database file */
   23080   int iRegion,                    /* Region to retrieve */
   23081   int szRegion,                   /* Size of regions */
   23082   int bExtend,                    /* True to extend block if necessary */
   23083   void volatile **pp              /* OUT: Mapped memory */
   23084 ){
   23085   PVOID pvTemp;
   23086   void **apRegion;
   23087   os2ShmNode *pNode;
   23088   int n, rc = SQLITE_OK;
   23089   char shmName[CCHMAXPATH];
   23090   os2File *pFile = (os2File*)id;
   23091 
   23092   *pp = NULL;
   23093 
   23094   if( !pFile->pShmLink )
   23095     rc = os2OpenSharedMemory( pFile, szRegion );
   23096 
   23097   if( rc == SQLITE_OK ) {
   23098     pNode = pFile->pShmLink->pShmNode ;
   23099 
   23100     sqlite3_mutex_enter(pNode->mutex);
   23101 
   23102     assert( szRegion==pNode->szRegion );
   23103 
   23104     /* Unmapped region ? */
   23105     if( iRegion >= pNode->nRegion ) {
   23106       /* Prevent other processes from resizing the shared memory */
   23107       os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
   23108 
   23109       apRegion = sqlite3_realloc(
   23110         pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
   23111 
   23112       if( apRegion ) {
   23113         pNode->apRegion = apRegion;
   23114 
   23115         while( pNode->nRegion <= iRegion ) {
   23116           sprintf(shmName, "%s-%u",
   23117                   pNode->shmBaseName, pNode->nRegion);
   23118 
   23119           if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
   23120                 PAG_READ | PAG_WRITE) != NO_ERROR ) {
   23121             if( !bExtend )
   23122               break;
   23123 
   23124             if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
   23125                   PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
   23126                 DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
   23127                   PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
   23128               rc = SQLITE_NOMEM;
   23129               break;
   23130             }
   23131           }
   23132 
   23133           apRegion[pNode->nRegion++] = pvTemp;
   23134         }
   23135 
   23136         /* zero out remaining entries */
   23137         for( n = pNode->nRegion; n <= iRegion; n++ )
   23138           pNode->apRegion[n] = NULL;
   23139 
   23140         /* Return this region (maybe zero) */
   23141         *pp = pNode->apRegion[iRegion];
   23142       } else {
   23143         rc = SQLITE_NOMEM;
   23144       }
   23145 
   23146       /* Allow other processes to resize the shared memory */
   23147       os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
   23148 
   23149     } else {
   23150       /* Region has been mapped previously */
   23151       *pp = pNode->apRegion[iRegion];
   23152     }
   23153 
   23154     sqlite3_mutex_leave(pNode->mutex);
   23155   }
   23156 
   23157   ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
   23158                  pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
   23159 
   23160   return rc;
   23161 }
   23162 
   23163 /*
   23164 ** Close a connection to shared-memory.  Delete the underlying
   23165 ** storage if deleteFlag is true.
   23166 **
   23167 ** If there is no shared memory associated with the connection then this
   23168 ** routine is a harmless no-op.
   23169 */
   23170 static int os2ShmUnmap(
   23171   sqlite3_file *id,               /* The underlying database file */
   23172   int deleteFlag                  /* Delete shared-memory if true */
   23173 ){
   23174   os2File *pFile = (os2File*)id;
   23175   os2ShmLink *pLink = pFile->pShmLink;
   23176 
   23177   if( pLink ) {
   23178     int nRef = -1;
   23179     os2ShmLink **ppLink;
   23180     os2ShmNode *pNode = pLink->pShmNode;
   23181 
   23182     sqlite3_mutex_enter(pNode->mutex);
   23183 
   23184     for( ppLink = &pNode->pFirst;
   23185          *ppLink && *ppLink != pLink;
   23186          ppLink = &(*ppLink)->pNext )   ;
   23187 
   23188     assert(*ppLink);
   23189 
   23190     if( *ppLink ) {
   23191       *ppLink = pLink->pNext;
   23192       nRef = --pNode->nRef;
   23193     } else {
   23194       ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
   23195                     pNode->shmBaseName))
   23196     }
   23197 
   23198     pFile->pShmLink = NULL;
   23199     sqlite3_free(pLink);
   23200 
   23201     sqlite3_mutex_leave(pNode->mutex);
   23202 
   23203     if( nRef == 0 )
   23204       os2PurgeShmNodes( deleteFlag );
   23205   }
   23206 
   23207   return SQLITE_OK;
   23208 }
   23209 
   23210 /*
   23211 ** Change the lock state for a shared-memory segment.
   23212 **
   23213 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
   23214 ** different here than in posix.  In xShmLock(), one can go from unlocked
   23215 ** to shared and back or from unlocked to exclusive and back.  But one may
   23216 ** not go from shared to exclusive or from exclusive to shared.
   23217 */
   23218 static int os2ShmLock(
   23219   sqlite3_file *id,          /* Database file holding the shared memory */
   23220   int ofst,                  /* First lock to acquire or release */
   23221   int n,                     /* Number of locks to acquire or release */
   23222   int flags                  /* What to do with the lock */
   23223 ){
   23224   u32 mask;                             /* Mask of locks to take or release */
   23225   int rc = SQLITE_OK;                   /* Result code */
   23226   os2File *pFile = (os2File*)id;
   23227   os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
   23228   os2ShmLink *pX;                       /* For looping over all siblings */
   23229   os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
   23230 
   23231   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   23232   assert( n>=1 );
   23233   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   23234        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   23235        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   23236        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   23237   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   23238 
   23239   mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
   23240   assert( n>1 || mask==(1<<ofst) );
   23241 
   23242 
   23243   sqlite3_mutex_enter(pShmNode->mutex);
   23244 
   23245   if( flags & SQLITE_SHM_UNLOCK ){
   23246     u32 allMask = 0; /* Mask of locks held by siblings */
   23247 
   23248     /* See if any siblings hold this same lock */
   23249     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   23250       if( pX==p ) continue;
   23251       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   23252       allMask |= pX->sharedMask;
   23253     }
   23254 
   23255     /* Unlock the system-level locks */
   23256     if( (mask & allMask)==0 ){
   23257       rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
   23258     }else{
   23259       rc = SQLITE_OK;
   23260     }
   23261 
   23262     /* Undo the local locks */
   23263     if( rc==SQLITE_OK ){
   23264       p->exclMask &= ~mask;
   23265       p->sharedMask &= ~mask;
   23266     }
   23267   }else if( flags & SQLITE_SHM_SHARED ){
   23268     u32 allShared = 0;  /* Union of locks held by connections other than "p" */
   23269 
   23270     /* Find out which shared locks are already held by sibling connections.
   23271     ** If any sibling already holds an exclusive lock, go ahead and return
   23272     ** SQLITE_BUSY.
   23273     */
   23274     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   23275       if( (pX->exclMask & mask)!=0 ){
   23276         rc = SQLITE_BUSY;
   23277         break;
   23278       }
   23279       allShared |= pX->sharedMask;
   23280     }
   23281 
   23282     /* Get shared locks at the system level, if necessary */
   23283     if( rc==SQLITE_OK ){
   23284       if( (allShared & mask)==0 ){
   23285         rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
   23286       }else{
   23287         rc = SQLITE_OK;
   23288       }
   23289     }
   23290 
   23291     /* Get the local shared locks */
   23292     if( rc==SQLITE_OK ){
   23293       p->sharedMask |= mask;
   23294     }
   23295   }else{
   23296     /* Make sure no sibling connections hold locks that will block this
   23297     ** lock.  If any do, return SQLITE_BUSY right away.
   23298     */
   23299     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   23300       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   23301         rc = SQLITE_BUSY;
   23302         break;
   23303       }
   23304     }
   23305 
   23306     /* Get the exclusive locks at the system level.  Then if successful
   23307     ** also mark the local connection as being locked.
   23308     */
   23309     if( rc==SQLITE_OK ){
   23310       rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
   23311       if( rc==SQLITE_OK ){
   23312         assert( (p->sharedMask & mask)==0 );
   23313         p->exclMask |= mask;
   23314       }
   23315     }
   23316   }
   23317 
   23318   sqlite3_mutex_leave(pShmNode->mutex);
   23319 
   23320   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
   23321            p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
   23322            rc ? "failed" : "ok"));
   23323 
   23324   ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
   23325                  ofst, n, flags, rc))
   23326 
   23327   return rc;
   23328 }
   23329 
   23330 /*
   23331 ** Implement a memory barrier or memory fence on shared memory.
   23332 **
   23333 ** All loads and stores begun before the barrier must complete before
   23334 ** any load or store begun after the barrier.
   23335 */
   23336 static void os2ShmBarrier(
   23337   sqlite3_file *id                /* Database file holding the shared memory */
   23338 ){
   23339   UNUSED_PARAMETER(id);
   23340   os2ShmEnterMutex();
   23341   os2ShmLeaveMutex();
   23342 }
   23343 
   23344 #else
   23345 # define os2ShmMap     0
   23346 # define os2ShmLock    0
   23347 # define os2ShmBarrier 0
   23348 # define os2ShmUnmap   0
   23349 #endif /* #ifndef SQLITE_OMIT_WAL */
   23350 
   23351 
   23352 /*
   23353 ** This vector defines all the methods that can operate on an
   23354 ** sqlite3_file for os2.
   23355 */
   23356 static const sqlite3_io_methods os2IoMethod = {
   23357   2,                              /* iVersion */
   23358   os2Close,                       /* xClose */
   23359   os2Read,                        /* xRead */
   23360   os2Write,                       /* xWrite */
   23361   os2Truncate,                    /* xTruncate */
   23362   os2Sync,                        /* xSync */
   23363   os2FileSize,                    /* xFileSize */
   23364   os2Lock,                        /* xLock */
   23365   os2Unlock,                      /* xUnlock */
   23366   os2CheckReservedLock,           /* xCheckReservedLock */
   23367   os2FileControl,                 /* xFileControl */
   23368   os2SectorSize,                  /* xSectorSize */
   23369   os2DeviceCharacteristics,       /* xDeviceCharacteristics */
   23370   os2ShmMap,                      /* xShmMap */
   23371   os2ShmLock,                     /* xShmLock */
   23372   os2ShmBarrier,                  /* xShmBarrier */
   23373   os2ShmUnmap                     /* xShmUnmap */
   23374 };
   23375 
   23376 
   23377 /***************************************************************************
   23378 ** Here ends the I/O methods that form the sqlite3_io_methods object.
   23379 **
   23380 ** The next block of code implements the VFS methods.
   23381 ****************************************************************************/
   23382 
   23383 /*
   23384 ** Create a temporary file name in zBuf.  zBuf must be big enough to
   23385 ** hold at pVfs->mxPathname characters.
   23386 */
   23387 static int getTempname(int nBuf, char *zBuf ){
   23388   static const char zChars[] =
   23389     "abcdefghijklmnopqrstuvwxyz"
   23390     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   23391     "0123456789";
   23392   int i, j;
   23393   PSZ zTempPathCp;
   23394   char zTempPath[CCHMAXPATH];
   23395   ULONG ulDriveNum, ulDriveMap;
   23396 
   23397   /* It's odd to simulate an io-error here, but really this is just
   23398   ** using the io-error infrastructure to test that SQLite handles this
   23399   ** function failing.
   23400   */
   23401   SimulateIOError( return SQLITE_IOERR );
   23402 
   23403   if( sqlite3_temp_directory ) {
   23404     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
   23405   } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
   23406              DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
   23407              DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
   23408     char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
   23409     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
   23410     free( zTempPathUTF );
   23411   } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
   23412     zTempPath[0] = (char)('A' + ulDriveNum - 1);
   23413     zTempPath[1] = ':';
   23414     zTempPath[2] = '\0';
   23415   } else {
   23416     zTempPath[0] = '\0';
   23417   }
   23418 
   23419   /* Strip off a trailing slashes or backslashes, otherwise we would get *
   23420    * multiple (back)slashes which causes DosOpen() to fail.              *
   23421    * Trailing spaces are not allowed, either.                            */
   23422   j = sqlite3Strlen30(zTempPath);
   23423   while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
   23424                     zTempPath[j-1] == ' ' ) ){
   23425     j--;
   23426   }
   23427   zTempPath[j] = '\0';
   23428 
   23429   /* We use 20 bytes to randomize the name */
   23430   sqlite3_snprintf(nBuf-22, zBuf,
   23431                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
   23432   j = sqlite3Strlen30(zBuf);
   23433   sqlite3_randomness( 20, &zBuf[j] );
   23434   for( i = 0; i < 20; i++, j++ ){
   23435     zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   23436   }
   23437   zBuf[j] = 0;
   23438 
   23439   OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
   23440   return SQLITE_OK;
   23441 }
   23442 
   23443 
   23444 /*
   23445 ** Turn a relative pathname into a full pathname.  Write the full
   23446 ** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
   23447 ** bytes in size.
   23448 */
   23449 static int os2FullPathname(
   23450   sqlite3_vfs *pVfs,          /* Pointer to vfs object */
   23451   const char *zRelative,      /* Possibly relative input path */
   23452   int nFull,                  /* Size of output buffer in bytes */
   23453   char *zFull                 /* Output buffer */
   23454 ){
   23455   char *zRelativeCp = convertUtf8PathToCp( zRelative );
   23456   char zFullCp[CCHMAXPATH] = "\0";
   23457   char *zFullUTF;
   23458   APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
   23459                                 zFullCp, CCHMAXPATH );
   23460   free( zRelativeCp );
   23461   zFullUTF = convertCpPathToUtf8( zFullCp );
   23462   sqlite3_snprintf( nFull, zFull, zFullUTF );
   23463   free( zFullUTF );
   23464   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   23465 }
   23466 
   23467 
   23468 /*
   23469 ** Open a file.
   23470 */
   23471 static int os2Open(
   23472   sqlite3_vfs *pVfs,            /* Not used */
   23473   const char *zName,            /* Name of the file (UTF-8) */
   23474   sqlite3_file *id,             /* Write the SQLite file handle here */
   23475   int flags,                    /* Open mode flags */
   23476   int *pOutFlags                /* Status return flags */
   23477 ){
   23478   HFILE h;
   23479   ULONG ulOpenFlags = 0;
   23480   ULONG ulOpenMode = 0;
   23481   ULONG ulAction = 0;
   23482   ULONG rc;
   23483   os2File *pFile = (os2File*)id;
   23484   const char *zUtf8Name = zName;
   23485   char *zNameCp;
   23486   char  zTmpname[CCHMAXPATH];
   23487 
   23488   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   23489   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   23490   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   23491 #ifndef NDEBUG
   23492   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   23493   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   23494   int eType        = (flags & 0xFFFFFF00);
   23495   int isOpenJournal = (isCreate && (
   23496         eType==SQLITE_OPEN_MASTER_JOURNAL
   23497      || eType==SQLITE_OPEN_MAIN_JOURNAL
   23498      || eType==SQLITE_OPEN_WAL
   23499   ));
   23500 #endif
   23501 
   23502   UNUSED_PARAMETER(pVfs);
   23503   assert( id!=0 );
   23504 
   23505   /* Check the following statements are true:
   23506   **
   23507   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
   23508   **   (b) if CREATE is set, then READWRITE must also be set, and
   23509   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   23510   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   23511   */
   23512   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   23513   assert(isCreate==0 || isReadWrite);
   23514   assert(isExclusive==0 || isCreate);
   23515   assert(isDelete==0 || isCreate);
   23516 
   23517   /* The main DB, main journal, WAL file and master journal are never
   23518   ** automatically deleted. Nor are they ever temporary files.  */
   23519   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   23520   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   23521   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   23522   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   23523 
   23524   /* Assert that the upper layer has set one of the "file-type" flags. */
   23525   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
   23526        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
   23527        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
   23528        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   23529   );
   23530 
   23531   memset( pFile, 0, sizeof(*pFile) );
   23532   pFile->h = (HFILE)-1;
   23533 
   23534   /* If the second argument to this function is NULL, generate a
   23535   ** temporary file name to use
   23536   */
   23537   if( !zUtf8Name ){
   23538     assert(isDelete && !isOpenJournal);
   23539     rc = getTempname(CCHMAXPATH, zTmpname);
   23540     if( rc!=SQLITE_OK ){
   23541       return rc;
   23542     }
   23543     zUtf8Name = zTmpname;
   23544   }
   23545 
   23546   if( isReadWrite ){
   23547     ulOpenMode |= OPEN_ACCESS_READWRITE;
   23548   }else{
   23549     ulOpenMode |= OPEN_ACCESS_READONLY;
   23550   }
   23551 
   23552   /* Open in random access mode for possibly better speed.  Allow full
   23553   ** sharing because file locks will provide exclusive access when needed.
   23554   ** The handle should not be inherited by child processes and we don't
   23555   ** want popups from the critical error handler.
   23556   */
   23557   ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
   23558                 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
   23559 
   23560   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
   23561   ** created. SQLite doesn't use it to indicate "exclusive access"
   23562   ** as it is usually understood.
   23563   */
   23564   if( isExclusive ){
   23565     /* Creates a new file, only if it does not already exist. */
   23566     /* If the file exists, it fails. */
   23567     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
   23568   }else if( isCreate ){
   23569     /* Open existing file, or create if it doesn't exist */
   23570     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
   23571   }else{
   23572     /* Opens a file, only if it exists. */
   23573     ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
   23574   }
   23575 
   23576   zNameCp = convertUtf8PathToCp( zUtf8Name );
   23577   rc = DosOpen( (PSZ)zNameCp,
   23578                 &h,
   23579                 &ulAction,
   23580                 0L,
   23581                 FILE_NORMAL,
   23582                 ulOpenFlags,
   23583                 ulOpenMode,
   23584                 (PEAOP2)NULL );
   23585   free( zNameCp );
   23586 
   23587   if( rc != NO_ERROR ){
   23588     OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
   23589               rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
   23590 
   23591     if( isReadWrite ){
   23592       return os2Open( pVfs, zName, id,
   23593                       ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
   23594                       pOutFlags );
   23595     }else{
   23596       return SQLITE_CANTOPEN;
   23597     }
   23598   }
   23599 
   23600   if( pOutFlags ){
   23601     *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
   23602   }
   23603 
   23604   os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
   23605   pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
   23606   pFile->pMethod = &os2IoMethod;
   23607   pFile->flags = flags;
   23608   pFile->h = h;
   23609 
   23610   OpenCounter(+1);
   23611   OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
   23612   return SQLITE_OK;
   23613 }
   23614 
   23615 /*
   23616 ** Delete the named file.
   23617 */
   23618 static int os2Delete(
   23619   sqlite3_vfs *pVfs,                     /* Not used on os2 */
   23620   const char *zFilename,                 /* Name of file to delete */
   23621   int syncDir                            /* Not used on os2 */
   23622 ){
   23623   APIRET rc;
   23624   char *zFilenameCp;
   23625   SimulateIOError( return SQLITE_IOERR_DELETE );
   23626   zFilenameCp = convertUtf8PathToCp( zFilename );
   23627   rc = DosDelete( (PSZ)zFilenameCp );
   23628   free( zFilenameCp );
   23629   OSTRACE(( "DELETE \"%s\"\n", zFilename ));
   23630   return (rc == NO_ERROR ||
   23631           rc == ERROR_FILE_NOT_FOUND ||
   23632           rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
   23633 }
   23634 
   23635 /*
   23636 ** Check the existance and status of a file.
   23637 */
   23638 static int os2Access(
   23639   sqlite3_vfs *pVfs,        /* Not used on os2 */
   23640   const char *zFilename,    /* Name of file to check */
   23641   int flags,                /* Type of test to make on this file */
   23642   int *pOut                 /* Write results here */
   23643 ){
   23644   APIRET rc;
   23645   FILESTATUS3 fsts3ConfigInfo;
   23646   char *zFilenameCp;
   23647 
   23648   UNUSED_PARAMETER(pVfs);
   23649   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   23650 
   23651   zFilenameCp = convertUtf8PathToCp( zFilename );
   23652   rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
   23653                          &fsts3ConfigInfo, sizeof(FILESTATUS3) );
   23654   free( zFilenameCp );
   23655   OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
   23656             fsts3ConfigInfo.attrFile, flags, rc ));
   23657 
   23658   switch( flags ){
   23659     case SQLITE_ACCESS_EXISTS:
   23660       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
   23661       ** as if it does not exist.
   23662       */
   23663       if( fsts3ConfigInfo.cbFile == 0 )
   23664         rc = ERROR_FILE_NOT_FOUND;
   23665       break;
   23666     case SQLITE_ACCESS_READ:
   23667       break;
   23668     case SQLITE_ACCESS_READWRITE:
   23669       if( fsts3ConfigInfo.attrFile & FILE_READONLY )
   23670         rc = ERROR_ACCESS_DENIED;
   23671       break;
   23672     default:
   23673       rc = ERROR_FILE_NOT_FOUND;
   23674       assert( !"Invalid flags argument" );
   23675   }
   23676 
   23677   *pOut = (rc == NO_ERROR);
   23678   OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
   23679 
   23680   return SQLITE_OK;
   23681 }
   23682 
   23683 
   23684 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   23685 /*
   23686 ** Interfaces for opening a shared library, finding entry points
   23687 ** within the shared library, and closing the shared library.
   23688 */
   23689 /*
   23690 ** Interfaces for opening a shared library, finding entry points
   23691 ** within the shared library, and closing the shared library.
   23692 */
   23693 static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   23694   HMODULE hmod;
   23695   APIRET rc;
   23696   char *zFilenameCp = convertUtf8PathToCp(zFilename);
   23697   rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
   23698   free(zFilenameCp);
   23699   return rc != NO_ERROR ? 0 : (void*)hmod;
   23700 }
   23701 /*
   23702 ** A no-op since the error code is returned on the DosLoadModule call.
   23703 ** os2Dlopen returns zero if DosLoadModule is not successful.
   23704 */
   23705 static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
   23706 /* no-op */
   23707 }
   23708 static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
   23709   PFN pfn;
   23710   APIRET rc;
   23711   rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
   23712   if( rc != NO_ERROR ){
   23713     /* if the symbol itself was not found, search again for the same
   23714      * symbol with an extra underscore, that might be needed depending
   23715      * on the calling convention */
   23716     char _zSymbol[256] = "_";
   23717     strncat(_zSymbol, zSymbol, 254);
   23718     rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
   23719   }
   23720   return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
   23721 }
   23722 static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
   23723   DosFreeModule((HMODULE)pHandle);
   23724 }
   23725 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   23726   #define os2DlOpen 0
   23727   #define os2DlError 0
   23728   #define os2DlSym 0
   23729   #define os2DlClose 0
   23730 #endif
   23731 
   23732 
   23733 /*
   23734 ** Write up to nBuf bytes of randomness into zBuf.
   23735 */
   23736 static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
   23737   int n = 0;
   23738 #if defined(SQLITE_TEST)
   23739   n = nBuf;
   23740   memset(zBuf, 0, nBuf);
   23741 #else
   23742   int i;
   23743   PPIB ppib;
   23744   PTIB ptib;
   23745   DATETIME dt;
   23746   static unsigned c = 0;
   23747   /* Ordered by variation probability */
   23748   static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
   23749                             QSV_MAXPRMEM, QSV_MAXSHMEM,
   23750                             QSV_TOTAVAILMEM, QSV_TOTRESMEM };
   23751 
   23752   /* 8 bytes; timezone and weekday don't increase the randomness much */
   23753   if( (int)sizeof(dt)-3 <= nBuf - n ){
   23754     c += 0x0100;
   23755     DosGetDateTime(&dt);
   23756     dt.year = (USHORT)((dt.year - 1900) | c);
   23757     memcpy(&zBuf[n], &dt, sizeof(dt)-3);
   23758     n += sizeof(dt)-3;
   23759   }
   23760 
   23761   /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
   23762   if( (int)sizeof(ULONG) <= nBuf - n ){
   23763     DosGetInfoBlocks(&ptib, &ppib);
   23764     *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
   23765                                  ptib->tib_ptib2->tib2_ultid);
   23766     n += sizeof(ULONG);
   23767   }
   23768 
   23769   /* Up to 6 * 4 bytes; variables depend on the system state */
   23770   for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
   23771     DosQuerySysInfo(svIdx[i], svIdx[i],
   23772                     (PULONG)&zBuf[n], sizeof(ULONG));
   23773     n += sizeof(ULONG);
   23774   }
   23775 #endif
   23776 
   23777   return n;
   23778 }
   23779 
   23780 /*
   23781 ** Sleep for a little while.  Return the amount of time slept.
   23782 ** The argument is the number of microseconds we want to sleep.
   23783 ** The return value is the number of microseconds of sleep actually
   23784 ** requested from the underlying operating system, a number which
   23785 ** might be greater than or equal to the argument, but not less
   23786 ** than the argument.
   23787 */
   23788 static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
   23789   DosSleep( (microsec/1000) );
   23790   return microsec;
   23791 }
   23792 
   23793 /*
   23794 ** The following variable, if set to a non-zero value, becomes the result
   23795 ** returned from sqlite3OsCurrentTime().  This is used for testing.
   23796 */
   23797 #ifdef SQLITE_TEST
   23798 SQLITE_API int sqlite3_current_time = 0;
   23799 #endif
   23800 
   23801 /*
   23802 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   23803 ** the current time and date as a Julian Day number times 86_400_000.  In
   23804 ** other words, write into *piNow the number of milliseconds since the Julian
   23805 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   23806 ** proleptic Gregorian calendar.
   23807 **
   23808 ** On success, return 0.  Return 1 if the time and date cannot be found.
   23809 */
   23810 static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
   23811 #ifdef SQLITE_TEST
   23812   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   23813 #endif
   23814   int year, month, datepart, timepart;
   23815 
   23816   DATETIME dt;
   23817   DosGetDateTime( &dt );
   23818 
   23819   year = dt.year;
   23820   month = dt.month;
   23821 
   23822   /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
   23823   ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
   23824   ** Calculate the Julian days
   23825   */
   23826   datepart = (int)dt.day - 32076 +
   23827     1461*(year + 4800 + (month - 14)/12)/4 +
   23828     367*(month - 2 - (month - 14)/12*12)/12 -
   23829     3*((year + 4900 + (month - 14)/12)/100)/4;
   23830 
   23831   /* Time in milliseconds, hours to noon added */
   23832   timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
   23833     ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
   23834 
   23835   *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
   23836 
   23837 #ifdef SQLITE_TEST
   23838   if( sqlite3_current_time ){
   23839     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   23840   }
   23841 #endif
   23842 
   23843   UNUSED_PARAMETER(pVfs);
   23844   return 0;
   23845 }
   23846 
   23847 /*
   23848 ** Find the current time (in Universal Coordinated Time).  Write the
   23849 ** current time and date as a Julian Day number into *prNow and
   23850 ** return 0.  Return 1 if the time and date cannot be found.
   23851 */
   23852 static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
   23853   int rc;
   23854   sqlite3_int64 i;
   23855   rc = os2CurrentTimeInt64(pVfs, &i);
   23856   if( !rc ){
   23857     *prNow = i/86400000.0;
   23858   }
   23859   return rc;
   23860 }
   23861 
   23862 /*
   23863 ** The idea is that this function works like a combination of
   23864 ** GetLastError() and FormatMessage() on windows (or errno and
   23865 ** strerror_r() on unix). After an error is returned by an OS
   23866 ** function, SQLite calls this function with zBuf pointing to
   23867 ** a buffer of nBuf bytes. The OS layer should populate the
   23868 ** buffer with a nul-terminated UTF-8 encoded error message
   23869 ** describing the last IO error to have occurred within the calling
   23870 ** thread.
   23871 **
   23872 ** If the error message is too large for the supplied buffer,
   23873 ** it should be truncated. The return value of xGetLastError
   23874 ** is zero if the error message fits in the buffer, or non-zero
   23875 ** otherwise (if the message was truncated). If non-zero is returned,
   23876 ** then it is not necessary to include the nul-terminator character
   23877 ** in the output buffer.
   23878 **
   23879 ** Not supplying an error message will have no adverse effect
   23880 ** on SQLite. It is fine to have an implementation that never
   23881 ** returns an error message:
   23882 **
   23883 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   23884 **     assert(zBuf[0]=='\0');
   23885 **     return 0;
   23886 **   }
   23887 **
   23888 ** However if an error message is supplied, it will be incorporated
   23889 ** by sqlite into the error message available to the user using
   23890 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
   23891 */
   23892 static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   23893   assert(zBuf[0]=='\0');
   23894   return 0;
   23895 }
   23896 
   23897 /*
   23898 ** Initialize and deinitialize the operating system interface.
   23899 */
   23900 SQLITE_API int sqlite3_os_init(void){
   23901   static sqlite3_vfs os2Vfs = {
   23902     3,                 /* iVersion */
   23903     sizeof(os2File),   /* szOsFile */
   23904     CCHMAXPATH,        /* mxPathname */
   23905     0,                 /* pNext */
   23906     "os2",             /* zName */
   23907     0,                 /* pAppData */
   23908 
   23909     os2Open,           /* xOpen */
   23910     os2Delete,         /* xDelete */
   23911     os2Access,         /* xAccess */
   23912     os2FullPathname,   /* xFullPathname */
   23913     os2DlOpen,         /* xDlOpen */
   23914     os2DlError,        /* xDlError */
   23915     os2DlSym,          /* xDlSym */
   23916     os2DlClose,        /* xDlClose */
   23917     os2Randomness,     /* xRandomness */
   23918     os2Sleep,          /* xSleep */
   23919     os2CurrentTime,    /* xCurrentTime */
   23920     os2GetLastError,   /* xGetLastError */
   23921     os2CurrentTimeInt64, /* xCurrentTimeInt64 */
   23922     0,                 /* xSetSystemCall */
   23923     0,                 /* xGetSystemCall */
   23924     0                  /* xNextSystemCall */
   23925   };
   23926   sqlite3_vfs_register(&os2Vfs, 1);
   23927   initUconvObjects();
   23928 /*  sqlite3OSTrace = 1; */
   23929   return SQLITE_OK;
   23930 }
   23931 SQLITE_API int sqlite3_os_end(void){
   23932   freeUconvObjects();
   23933   return SQLITE_OK;
   23934 }
   23935 
   23936 #endif /* SQLITE_OS_OS2 */
   23937 
   23938 /************** End of os_os2.c **********************************************/
   23939 /************** Begin file os_unix.c *****************************************/
   23940 /*
   23941 ** 2004 May 22
   23942 **
   23943 ** The author disclaims copyright to this source code.  In place of
   23944 ** a legal notice, here is a blessing:
   23945 **
   23946 **    May you do good and not evil.
   23947 **    May you find forgiveness for yourself and forgive others.
   23948 **    May you share freely, never taking more than you give.
   23949 **
   23950 ******************************************************************************
   23951 **
   23952 ** This file contains the VFS implementation for unix-like operating systems
   23953 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
   23954 **
   23955 ** There are actually several different VFS implementations in this file.
   23956 ** The differences are in the way that file locking is done.  The default
   23957 ** implementation uses Posix Advisory Locks.  Alternative implementations
   23958 ** use flock(), dot-files, various proprietary locking schemas, or simply
   23959 ** skip locking all together.
   23960 **
   23961 ** This source file is organized into divisions where the logic for various
   23962 ** subfunctions is contained within the appropriate division.  PLEASE
   23963 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
   23964 ** in the correct division and should be clearly labeled.
   23965 **
   23966 ** The layout of divisions is as follows:
   23967 **
   23968 **   *  General-purpose declarations and utility functions.
   23969 **   *  Unique file ID logic used by VxWorks.
   23970 **   *  Various locking primitive implementations (all except proxy locking):
   23971 **      + for Posix Advisory Locks
   23972 **      + for no-op locks
   23973 **      + for dot-file locks
   23974 **      + for flock() locking
   23975 **      + for named semaphore locks (VxWorks only)
   23976 **      + for AFP filesystem locks (MacOSX only)
   23977 **   *  sqlite3_file methods not associated with locking.
   23978 **   *  Definitions of sqlite3_io_methods objects for all locking
   23979 **      methods plus "finder" functions for each locking method.
   23980 **   *  sqlite3_vfs method implementations.
   23981 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
   23982 **   *  Definitions of sqlite3_vfs objects for all locking methods
   23983 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
   23984 */
   23985 #if SQLITE_OS_UNIX              /* This file is used on unix only */
   23986 
   23987 /*
   23988 ** There are various methods for file locking used for concurrency
   23989 ** control:
   23990 **
   23991 **   1. POSIX locking (the default),
   23992 **   2. No locking,
   23993 **   3. Dot-file locking,
   23994 **   4. flock() locking,
   23995 **   5. AFP locking (OSX only),
   23996 **   6. Named POSIX semaphores (VXWorks only),
   23997 **   7. proxy locking. (OSX only)
   23998 **
   23999 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
   24000 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
   24001 ** selection of the appropriate locking style based on the filesystem
   24002 ** where the database is located.
   24003 */
   24004 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
   24005 #  if defined(__APPLE__)
   24006 #    define SQLITE_ENABLE_LOCKING_STYLE 1
   24007 #  else
   24008 #    define SQLITE_ENABLE_LOCKING_STYLE 0
   24009 #  endif
   24010 #endif
   24011 
   24012 /*
   24013 ** Define the OS_VXWORKS pre-processor macro to 1 if building on
   24014 ** vxworks, or 0 otherwise.
   24015 */
   24016 #ifndef OS_VXWORKS
   24017 #  if defined(__RTP__) || defined(_WRS_KERNEL)
   24018 #    define OS_VXWORKS 1
   24019 #  else
   24020 #    define OS_VXWORKS 0
   24021 #  endif
   24022 #endif
   24023 
   24024 /*
   24025 ** These #defines should enable >2GB file support on Posix if the
   24026 ** underlying operating system supports it.  If the OS lacks
   24027 ** large file support, these should be no-ops.
   24028 **
   24029 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
   24030 ** on the compiler command line.  This is necessary if you are compiling
   24031 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
   24032 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
   24033 ** without this option, LFS is enable.  But LFS does not exist in the kernel
   24034 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
   24035 ** portability you should omit LFS.
   24036 **
   24037 ** The previous paragraph was written in 2005.  (This paragraph is written
   24038 ** on 2008-11-28.) These days, all Linux kernels support large files, so
   24039 ** you should probably leave LFS enabled.  But some embedded platforms might
   24040 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
   24041 */
   24042 #ifndef SQLITE_DISABLE_LFS
   24043 # define _LARGE_FILE       1
   24044 # ifndef _FILE_OFFSET_BITS
   24045 #   define _FILE_OFFSET_BITS 64
   24046 # endif
   24047 # define _LARGEFILE_SOURCE 1
   24048 #endif
   24049 
   24050 /*
   24051 ** standard include files.
   24052 */
   24053 #include <sys/types.h>
   24054 #include <sys/stat.h>
   24055 #include <fcntl.h>
   24056 #include <unistd.h>
   24057 #include <sys/time.h>
   24058 #include <errno.h>
   24059 #ifndef SQLITE_OMIT_WAL
   24060 #include <sys/mman.h>
   24061 #endif
   24062 
   24063 #if SQLITE_ENABLE_LOCKING_STYLE
   24064 # include <sys/ioctl.h>
   24065 # if OS_VXWORKS
   24066 #  include <semaphore.h>
   24067 #  include <limits.h>
   24068 # else
   24069 #  include <sys/file.h>
   24070 #  include <sys/param.h>
   24071 # endif
   24072 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
   24073 
   24074 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
   24075 # include <sys/mount.h>
   24076 #endif
   24077 
   24078 /*
   24079 ** Allowed values of unixFile.fsFlags
   24080 */
   24081 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
   24082 
   24083 /*
   24084 ** If we are to be thread-safe, include the pthreads header and define
   24085 ** the SQLITE_UNIX_THREADS macro.
   24086 */
   24087 #if SQLITE_THREADSAFE
   24088 # define SQLITE_UNIX_THREADS 1
   24089 #endif
   24090 
   24091 /*
   24092 ** Default permissions when creating a new file
   24093 */
   24094 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
   24095 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
   24096 #endif
   24097 
   24098 /*
   24099  ** Default permissions when creating auto proxy dir
   24100  */
   24101 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
   24102 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
   24103 #endif
   24104 
   24105 /*
   24106 ** Maximum supported path-length.
   24107 */
   24108 #define MAX_PATHNAME 512
   24109 
   24110 /*
   24111 ** Only set the lastErrno if the error code is a real error and not
   24112 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
   24113 */
   24114 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
   24115 
   24116 /* Forward references */
   24117 typedef struct unixShm unixShm;               /* Connection shared memory */
   24118 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
   24119 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
   24120 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
   24121 
   24122 /*
   24123 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
   24124 ** cannot be closed immediately. In these cases, instances of the following
   24125 ** structure are used to store the file descriptor while waiting for an
   24126 ** opportunity to either close or reuse it.
   24127 */
   24128 struct UnixUnusedFd {
   24129   int fd;                   /* File descriptor to close */
   24130   int flags;                /* Flags this file descriptor was opened with */
   24131   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
   24132 };
   24133 
   24134 /*
   24135 ** The unixFile structure is subclass of sqlite3_file specific to the unix
   24136 ** VFS implementations.
   24137 */
   24138 typedef struct unixFile unixFile;
   24139 struct unixFile {
   24140   sqlite3_io_methods const *pMethod;  /* Always the first entry */
   24141   unixInodeInfo *pInode;              /* Info about locks on this inode */
   24142   int h;                              /* The file descriptor */
   24143   unsigned char eFileLock;            /* The type of lock held on this fd */
   24144   unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
   24145   int lastErrno;                      /* The unix errno from last I/O error */
   24146   void *lockingContext;               /* Locking style specific state */
   24147   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
   24148   const char *zPath;                  /* Name of the file */
   24149   unixShm *pShm;                      /* Shared memory segment information */
   24150   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
   24151 #if SQLITE_ENABLE_LOCKING_STYLE
   24152   int openFlags;                      /* The flags specified at open() */
   24153 #endif
   24154 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
   24155   unsigned fsFlags;                   /* cached details from statfs() */
   24156 #endif
   24157 #if OS_VXWORKS
   24158   int isDelete;                       /* Delete on close if true */
   24159   struct vxworksFileId *pId;          /* Unique file ID */
   24160 #endif
   24161 #ifndef NDEBUG
   24162   /* The next group of variables are used to track whether or not the
   24163   ** transaction counter in bytes 24-27 of database files are updated
   24164   ** whenever any part of the database changes.  An assertion fault will
   24165   ** occur if a file is updated without also updating the transaction
   24166   ** counter.  This test is made to avoid new problems similar to the
   24167   ** one described by ticket #3584.
   24168   */
   24169   unsigned char transCntrChng;   /* True if the transaction counter changed */
   24170   unsigned char dbUpdate;        /* True if any part of database file changed */
   24171   unsigned char inNormalWrite;   /* True if in a normal write operation */
   24172 #endif
   24173 #ifdef SQLITE_TEST
   24174   /* In test mode, increase the size of this structure a bit so that
   24175   ** it is larger than the struct CrashFile defined in test6.c.
   24176   */
   24177   char aPadding[32];
   24178 #endif
   24179 };
   24180 
   24181 /*
   24182 ** Allowed values for the unixFile.ctrlFlags bitmask:
   24183 */
   24184 #define UNIXFILE_EXCL   0x01     /* Connections from one process only */
   24185 #define UNIXFILE_RDONLY 0x02     /* Connection is read only */
   24186 #define UNIXFILE_DIRSYNC 0x04    /* Directory sync needed */
   24187 
   24188 /*
   24189 ** Include code that is common to all os_*.c files
   24190 */
   24191 /************** Include os_common.h in the middle of os_unix.c ***************/
   24192 /************** Begin file os_common.h ***************************************/
   24193 /*
   24194 ** 2004 May 22
   24195 **
   24196 ** The author disclaims copyright to this source code.  In place of
   24197 ** a legal notice, here is a blessing:
   24198 **
   24199 **    May you do good and not evil.
   24200 **    May you find forgiveness for yourself and forgive others.
   24201 **    May you share freely, never taking more than you give.
   24202 **
   24203 ******************************************************************************
   24204 **
   24205 ** This file contains macros and a little bit of code that is common to
   24206 ** all of the platform-specific files (os_*.c) and is #included into those
   24207 ** files.
   24208 **
   24209 ** This file should be #included by the os_*.c files only.  It is not a
   24210 ** general purpose header file.
   24211 */
   24212 #ifndef _OS_COMMON_H_
   24213 #define _OS_COMMON_H_
   24214 
   24215 /*
   24216 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   24217 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   24218 ** switch.  The following code should catch this problem at compile-time.
   24219 */
   24220 #ifdef MEMORY_DEBUG
   24221 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   24222 #endif
   24223 
   24224 #ifdef SQLITE_DEBUG
   24225 SQLITE_PRIVATE int sqlite3OSTrace = 0;
   24226 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   24227 #else
   24228 #define OSTRACE(X)
   24229 #endif
   24230 
   24231 /*
   24232 ** Macros for performance tracing.  Normally turned off.  Only works
   24233 ** on i486 hardware.
   24234 */
   24235 #ifdef SQLITE_PERFORMANCE_TRACE
   24236 
   24237 /*
   24238 ** hwtime.h contains inline assembler code for implementing
   24239 ** high-performance timing routines.
   24240 */
   24241 /************** Include hwtime.h in the middle of os_common.h ****************/
   24242 /************** Begin file hwtime.h ******************************************/
   24243 /*
   24244 ** 2008 May 27
   24245 **
   24246 ** The author disclaims copyright to this source code.  In place of
   24247 ** a legal notice, here is a blessing:
   24248 **
   24249 **    May you do good and not evil.
   24250 **    May you find forgiveness for yourself and forgive others.
   24251 **    May you share freely, never taking more than you give.
   24252 **
   24253 ******************************************************************************
   24254 **
   24255 ** This file contains inline asm code for retrieving "high-performance"
   24256 ** counters for x86 class CPUs.
   24257 */
   24258 #ifndef _HWTIME_H_
   24259 #define _HWTIME_H_
   24260 
   24261 /*
   24262 ** The following routine only works on pentium-class (or newer) processors.
   24263 ** It uses the RDTSC opcode to read the cycle count value out of the
   24264 ** processor and returns that value.  This can be used for high-res
   24265 ** profiling.
   24266 */
   24267 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   24268       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   24269 
   24270   #if defined(__GNUC__)
   24271 
   24272   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   24273      unsigned int lo, hi;
   24274      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   24275      return (sqlite_uint64)hi << 32 | lo;
   24276   }
   24277 
   24278   #elif defined(_MSC_VER)
   24279 
   24280   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   24281      __asm {
   24282         rdtsc
   24283         ret       ; return value at EDX:EAX
   24284      }
   24285   }
   24286 
   24287   #endif
   24288 
   24289 #elif (defined(__GNUC__) && defined(__x86_64__))
   24290 
   24291   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   24292       unsigned long val;
   24293       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   24294       return val;
   24295   }
   24296 
   24297 #elif (defined(__GNUC__) && defined(__ppc__))
   24298 
   24299   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   24300       unsigned long long retval;
   24301       unsigned long junk;
   24302       __asm__ __volatile__ ("\n\
   24303           1:      mftbu   %1\n\
   24304                   mftb    %L0\n\
   24305                   mftbu   %0\n\
   24306                   cmpw    %0,%1\n\
   24307                   bne     1b"
   24308                   : "=r" (retval), "=r" (junk));
   24309       return retval;
   24310   }
   24311 
   24312 #else
   24313 
   24314   #error Need implementation of sqlite3Hwtime() for your platform.
   24315 
   24316   /*
   24317   ** To compile without implementing sqlite3Hwtime() for your platform,
   24318   ** you can remove the above #error and use the following
   24319   ** stub function.  You will lose timing support for many
   24320   ** of the debugging and testing utilities, but it should at
   24321   ** least compile and run.
   24322   */
   24323 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   24324 
   24325 #endif
   24326 
   24327 #endif /* !defined(_HWTIME_H_) */
   24328 
   24329 /************** End of hwtime.h **********************************************/
   24330 /************** Continuing where we left off in os_common.h ******************/
   24331 
   24332 static sqlite_uint64 g_start;
   24333 static sqlite_uint64 g_elapsed;
   24334 #define TIMER_START       g_start=sqlite3Hwtime()
   24335 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   24336 #define TIMER_ELAPSED     g_elapsed
   24337 #else
   24338 #define TIMER_START
   24339 #define TIMER_END
   24340 #define TIMER_ELAPSED     ((sqlite_uint64)0)
   24341 #endif
   24342 
   24343 /*
   24344 ** If we compile with the SQLITE_TEST macro set, then the following block
   24345 ** of code will give us the ability to simulate a disk I/O error.  This
   24346 ** is used for testing the I/O recovery logic.
   24347 */
   24348 #ifdef SQLITE_TEST
   24349 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   24350 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   24351 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   24352 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   24353 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   24354 SQLITE_API int sqlite3_diskfull_pending = 0;
   24355 SQLITE_API int sqlite3_diskfull = 0;
   24356 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   24357 #define SimulateIOError(CODE)  \
   24358   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   24359        || sqlite3_io_error_pending-- == 1 )  \
   24360               { local_ioerr(); CODE; }
   24361 static void local_ioerr(){
   24362   IOTRACE(("IOERR\n"));
   24363   sqlite3_io_error_hit++;
   24364   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   24365 }
   24366 #define SimulateDiskfullError(CODE) \
   24367    if( sqlite3_diskfull_pending ){ \
   24368      if( sqlite3_diskfull_pending == 1 ){ \
   24369        local_ioerr(); \
   24370        sqlite3_diskfull = 1; \
   24371        sqlite3_io_error_hit = 1; \
   24372        CODE; \
   24373      }else{ \
   24374        sqlite3_diskfull_pending--; \
   24375      } \
   24376    }
   24377 #else
   24378 #define SimulateIOErrorBenign(X)
   24379 #define SimulateIOError(A)
   24380 #define SimulateDiskfullError(A)
   24381 #endif
   24382 
   24383 /*
   24384 ** When testing, keep a count of the number of open files.
   24385 */
   24386 #ifdef SQLITE_TEST
   24387 SQLITE_API int sqlite3_open_file_count = 0;
   24388 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   24389 #else
   24390 #define OpenCounter(X)
   24391 #endif
   24392 
   24393 #endif /* !defined(_OS_COMMON_H_) */
   24394 
   24395 /************** End of os_common.h *******************************************/
   24396 /************** Continuing where we left off in os_unix.c ********************/
   24397 
   24398 /*
   24399 ** Define various macros that are missing from some systems.
   24400 */
   24401 #ifndef O_LARGEFILE
   24402 # define O_LARGEFILE 0
   24403 #endif
   24404 #ifdef SQLITE_DISABLE_LFS
   24405 # undef O_LARGEFILE
   24406 # define O_LARGEFILE 0
   24407 #endif
   24408 #ifndef O_NOFOLLOW
   24409 # define O_NOFOLLOW 0
   24410 #endif
   24411 #ifndef O_BINARY
   24412 # define O_BINARY 0
   24413 #endif
   24414 
   24415 /*
   24416 ** The threadid macro resolves to the thread-id or to 0.  Used for
   24417 ** testing and debugging only.
   24418 */
   24419 #if SQLITE_THREADSAFE
   24420 #define threadid pthread_self()
   24421 #else
   24422 #define threadid 0
   24423 #endif
   24424 
   24425 /* Forward reference */
   24426 static int openDirectory(const char*, int*);
   24427 
   24428 /*
   24429 ** Many system calls are accessed through pointer-to-functions so that
   24430 ** they may be overridden at runtime to facilitate fault injection during
   24431 ** testing and sandboxing.  The following array holds the names and pointers
   24432 ** to all overrideable system calls.
   24433 */
   24434 static struct unix_syscall {
   24435   const char *zName;            /* Name of the sytem call */
   24436   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
   24437   sqlite3_syscall_ptr pDefault; /* Default value */
   24438 } aSyscall[] = {
   24439   { "open",         (sqlite3_syscall_ptr)open,       0  },
   24440 #define osOpen      ((int(*)(const char*,int,...))aSyscall[0].pCurrent)
   24441 
   24442   { "close",        (sqlite3_syscall_ptr)close,      0  },
   24443 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
   24444 
   24445   { "access",       (sqlite3_syscall_ptr)access,     0  },
   24446 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
   24447 
   24448   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
   24449 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
   24450 
   24451   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
   24452 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
   24453 
   24454 /*
   24455 ** The DJGPP compiler environment looks mostly like Unix, but it
   24456 ** lacks the fcntl() system call.  So redefine fcntl() to be something
   24457 ** that always succeeds.  This means that locking does not occur under
   24458 ** DJGPP.  But it is DOS - what did you expect?
   24459 */
   24460 #ifdef __DJGPP__
   24461   { "fstat",        0,                 0  },
   24462 #define osFstat(a,b,c)    0
   24463 #else
   24464   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
   24465 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
   24466 #endif
   24467 
   24468   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
   24469 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
   24470 
   24471   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
   24472 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
   24473 
   24474   { "read",         (sqlite3_syscall_ptr)read,       0  },
   24475 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
   24476 
   24477 #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
   24478   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
   24479 #else
   24480   { "pread",        (sqlite3_syscall_ptr)0,          0  },
   24481 #endif
   24482 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
   24483 
   24484 #if defined(USE_PREAD64)
   24485   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
   24486 #else
   24487   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
   24488 #endif
   24489 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
   24490 
   24491   { "write",        (sqlite3_syscall_ptr)write,      0  },
   24492 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
   24493 
   24494 #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
   24495   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
   24496 #else
   24497   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
   24498 #endif
   24499 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
   24500                     aSyscall[12].pCurrent)
   24501 
   24502 #if defined(USE_PREAD64)
   24503   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
   24504 #else
   24505   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
   24506 #endif
   24507 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
   24508                     aSyscall[13].pCurrent)
   24509 
   24510 #if SQLITE_ENABLE_LOCKING_STYLE
   24511   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
   24512 #else
   24513   { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
   24514 #endif
   24515 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
   24516 
   24517 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
   24518   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
   24519 #else
   24520   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
   24521 #endif
   24522 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
   24523 
   24524   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
   24525 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
   24526 
   24527   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
   24528 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
   24529 
   24530 }; /* End of the overrideable system calls */
   24531 
   24532 /*
   24533 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   24534 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
   24535 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
   24536 ** system call named zName.
   24537 */
   24538 static int unixSetSystemCall(
   24539   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
   24540   const char *zName,            /* Name of system call to override */
   24541   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
   24542 ){
   24543   unsigned int i;
   24544   int rc = SQLITE_NOTFOUND;
   24545 
   24546   UNUSED_PARAMETER(pNotUsed);
   24547   if( zName==0 ){
   24548     /* If no zName is given, restore all system calls to their default
   24549     ** settings and return NULL
   24550     */
   24551     rc = SQLITE_OK;
   24552     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   24553       if( aSyscall[i].pDefault ){
   24554         aSyscall[i].pCurrent = aSyscall[i].pDefault;
   24555       }
   24556     }
   24557   }else{
   24558     /* If zName is specified, operate on only the one system call
   24559     ** specified.
   24560     */
   24561     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   24562       if( strcmp(zName, aSyscall[i].zName)==0 ){
   24563         if( aSyscall[i].pDefault==0 ){
   24564           aSyscall[i].pDefault = aSyscall[i].pCurrent;
   24565         }
   24566         rc = SQLITE_OK;
   24567         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
   24568         aSyscall[i].pCurrent = pNewFunc;
   24569         break;
   24570       }
   24571     }
   24572   }
   24573   return rc;
   24574 }
   24575 
   24576 /*
   24577 ** Return the value of a system call.  Return NULL if zName is not a
   24578 ** recognized system call name.  NULL is also returned if the system call
   24579 ** is currently undefined.
   24580 */
   24581 static sqlite3_syscall_ptr unixGetSystemCall(
   24582   sqlite3_vfs *pNotUsed,
   24583   const char *zName
   24584 ){
   24585   unsigned int i;
   24586 
   24587   UNUSED_PARAMETER(pNotUsed);
   24588   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   24589     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
   24590   }
   24591   return 0;
   24592 }
   24593 
   24594 /*
   24595 ** Return the name of the first system call after zName.  If zName==NULL
   24596 ** then return the name of the first system call.  Return NULL if zName
   24597 ** is the last system call or if zName is not the name of a valid
   24598 ** system call.
   24599 */
   24600 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
   24601   int i = -1;
   24602 
   24603   UNUSED_PARAMETER(p);
   24604   if( zName ){
   24605     for(i=0; i<ArraySize(aSyscall)-1; i++){
   24606       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
   24607     }
   24608   }
   24609   for(i++; i<ArraySize(aSyscall); i++){
   24610     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
   24611   }
   24612   return 0;
   24613 }
   24614 
   24615 /*
   24616 ** Retry open() calls that fail due to EINTR
   24617 */
   24618 static int robust_open(const char *z, int f, int m){
   24619   int rc;
   24620   do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
   24621   return rc;
   24622 }
   24623 
   24624 /*
   24625 ** Helper functions to obtain and relinquish the global mutex. The
   24626 ** global mutex is used to protect the unixInodeInfo and
   24627 ** vxworksFileId objects used by this file, all of which may be
   24628 ** shared by multiple threads.
   24629 **
   24630 ** Function unixMutexHeld() is used to assert() that the global mutex
   24631 ** is held when required. This function is only used as part of assert()
   24632 ** statements. e.g.
   24633 **
   24634 **   unixEnterMutex()
   24635 **     assert( unixMutexHeld() );
   24636 **   unixEnterLeave()
   24637 */
   24638 static void unixEnterMutex(void){
   24639   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   24640 }
   24641 static void unixLeaveMutex(void){
   24642   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   24643 }
   24644 #ifdef SQLITE_DEBUG
   24645 static int unixMutexHeld(void) {
   24646   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   24647 }
   24648 #endif
   24649 
   24650 
   24651 #ifdef SQLITE_DEBUG
   24652 /*
   24653 ** Helper function for printing out trace information from debugging
   24654 ** binaries. This returns the string represetation of the supplied
   24655 ** integer lock-type.
   24656 */
   24657 static const char *azFileLock(int eFileLock){
   24658   switch( eFileLock ){
   24659     case NO_LOCK: return "NONE";
   24660     case SHARED_LOCK: return "SHARED";
   24661     case RESERVED_LOCK: return "RESERVED";
   24662     case PENDING_LOCK: return "PENDING";
   24663     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
   24664   }
   24665   return "ERROR";
   24666 }
   24667 #endif
   24668 
   24669 #ifdef SQLITE_LOCK_TRACE
   24670 /*
   24671 ** Print out information about all locking operations.
   24672 **
   24673 ** This routine is used for troubleshooting locks on multithreaded
   24674 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
   24675 ** command-line option on the compiler.  This code is normally
   24676 ** turned off.
   24677 */
   24678 static int lockTrace(int fd, int op, struct flock *p){
   24679   char *zOpName, *zType;
   24680   int s;
   24681   int savedErrno;
   24682   if( op==F_GETLK ){
   24683     zOpName = "GETLK";
   24684   }else if( op==F_SETLK ){
   24685     zOpName = "SETLK";
   24686   }else{
   24687     s = osFcntl(fd, op, p);
   24688     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
   24689     return s;
   24690   }
   24691   if( p->l_type==F_RDLCK ){
   24692     zType = "RDLCK";
   24693   }else if( p->l_type==F_WRLCK ){
   24694     zType = "WRLCK";
   24695   }else if( p->l_type==F_UNLCK ){
   24696     zType = "UNLCK";
   24697   }else{
   24698     assert( 0 );
   24699   }
   24700   assert( p->l_whence==SEEK_SET );
   24701   s = osFcntl(fd, op, p);
   24702   savedErrno = errno;
   24703   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
   24704      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
   24705      (int)p->l_pid, s);
   24706   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
   24707     struct flock l2;
   24708     l2 = *p;
   24709     osFcntl(fd, F_GETLK, &l2);
   24710     if( l2.l_type==F_RDLCK ){
   24711       zType = "RDLCK";
   24712     }else if( l2.l_type==F_WRLCK ){
   24713       zType = "WRLCK";
   24714     }else if( l2.l_type==F_UNLCK ){
   24715       zType = "UNLCK";
   24716     }else{
   24717       assert( 0 );
   24718     }
   24719     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
   24720        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
   24721   }
   24722   errno = savedErrno;
   24723   return s;
   24724 }
   24725 #undef osFcntl
   24726 #define osFcntl lockTrace
   24727 #endif /* SQLITE_LOCK_TRACE */
   24728 
   24729 /*
   24730 ** Retry ftruncate() calls that fail due to EINTR
   24731 */
   24732 static int robust_ftruncate(int h, sqlite3_int64 sz){
   24733   int rc;
   24734   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
   24735   return rc;
   24736 }
   24737 
   24738 /*
   24739 ** This routine translates a standard POSIX errno code into something
   24740 ** useful to the clients of the sqlite3 functions.  Specifically, it is
   24741 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
   24742 ** and a variety of "please close the file descriptor NOW" errors into
   24743 ** SQLITE_IOERR
   24744 **
   24745 ** Errors during initialization of locks, or file system support for locks,
   24746 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
   24747 */
   24748 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
   24749   switch (posixError) {
   24750 #if 0
   24751   /* At one point this code was not commented out. In theory, this branch
   24752   ** should never be hit, as this function should only be called after
   24753   ** a locking-related function (i.e. fcntl()) has returned non-zero with
   24754   ** the value of errno as the first argument. Since a system call has failed,
   24755   ** errno should be non-zero.
   24756   **
   24757   ** Despite this, if errno really is zero, we still don't want to return
   24758   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
   24759   ** propagated back to the caller. Commenting this branch out means errno==0
   24760   ** will be handled by the "default:" case below.
   24761   */
   24762   case 0:
   24763     return SQLITE_OK;
   24764 #endif
   24765 
   24766   case EAGAIN:
   24767   case ETIMEDOUT:
   24768   case EBUSY:
   24769   case EINTR:
   24770   case ENOLCK:
   24771     /* random NFS retry error, unless during file system support
   24772      * introspection, in which it actually means what it says */
   24773     return SQLITE_BUSY;
   24774 
   24775   case EACCES:
   24776     /* EACCES is like EAGAIN during locking operations, but not any other time*/
   24777     if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
   24778 	(sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
   24779 	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
   24780 	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
   24781       return SQLITE_BUSY;
   24782     }
   24783     /* else fall through */
   24784   case EPERM:
   24785     return SQLITE_PERM;
   24786 
   24787   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
   24788   ** this module never makes such a call. And the code in SQLite itself
   24789   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
   24790   ** this case is also commented out. If the system does set errno to EDEADLK,
   24791   ** the default SQLITE_IOERR_XXX code will be returned. */
   24792 #if 0
   24793   case EDEADLK:
   24794     return SQLITE_IOERR_BLOCKED;
   24795 #endif
   24796 
   24797 #if EOPNOTSUPP!=ENOTSUP
   24798   case EOPNOTSUPP:
   24799     /* something went terribly awry, unless during file system support
   24800      * introspection, in which it actually means what it says */
   24801 #endif
   24802 #ifdef ENOTSUP
   24803   case ENOTSUP:
   24804     /* invalid fd, unless during file system support introspection, in which
   24805      * it actually means what it says */
   24806 #endif
   24807   case EIO:
   24808   case EBADF:
   24809   case EINVAL:
   24810   case ENOTCONN:
   24811   case ENODEV:
   24812   case ENXIO:
   24813   case ENOENT:
   24814   case ESTALE:
   24815   case ENOSYS:
   24816     /* these should force the client to close the file and reconnect */
   24817 
   24818   default:
   24819     return sqliteIOErr;
   24820   }
   24821 }
   24822 
   24823 
   24824 
   24825 /******************************************************************************
   24826 ****************** Begin Unique File ID Utility Used By VxWorks ***************
   24827 **
   24828 ** On most versions of unix, we can get a unique ID for a file by concatenating
   24829 ** the device number and the inode number.  But this does not work on VxWorks.
   24830 ** On VxWorks, a unique file id must be based on the canonical filename.
   24831 **
   24832 ** A pointer to an instance of the following structure can be used as a
   24833 ** unique file ID in VxWorks.  Each instance of this structure contains
   24834 ** a copy of the canonical filename.  There is also a reference count.
   24835 ** The structure is reclaimed when the number of pointers to it drops to
   24836 ** zero.
   24837 **
   24838 ** There are never very many files open at one time and lookups are not
   24839 ** a performance-critical path, so it is sufficient to put these
   24840 ** structures on a linked list.
   24841 */
   24842 struct vxworksFileId {
   24843   struct vxworksFileId *pNext;  /* Next in a list of them all */
   24844   int nRef;                     /* Number of references to this one */
   24845   int nName;                    /* Length of the zCanonicalName[] string */
   24846   char *zCanonicalName;         /* Canonical filename */
   24847 };
   24848 
   24849 #if OS_VXWORKS
   24850 /*
   24851 ** All unique filenames are held on a linked list headed by this
   24852 ** variable:
   24853 */
   24854 static struct vxworksFileId *vxworksFileList = 0;
   24855 
   24856 /*
   24857 ** Simplify a filename into its canonical form
   24858 ** by making the following changes:
   24859 **
   24860 **  * removing any trailing and duplicate /
   24861 **  * convert /./ into just /
   24862 **  * convert /A/../ where A is any simple name into just /
   24863 **
   24864 ** Changes are made in-place.  Return the new name length.
   24865 **
   24866 ** The original filename is in z[0..n-1].  Return the number of
   24867 ** characters in the simplified name.
   24868 */
   24869 static int vxworksSimplifyName(char *z, int n){
   24870   int i, j;
   24871   while( n>1 && z[n-1]=='/' ){ n--; }
   24872   for(i=j=0; i<n; i++){
   24873     if( z[i]=='/' ){
   24874       if( z[i+1]=='/' ) continue;
   24875       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
   24876         i += 1;
   24877         continue;
   24878       }
   24879       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
   24880         while( j>0 && z[j-1]!='/' ){ j--; }
   24881         if( j>0 ){ j--; }
   24882         i += 2;
   24883         continue;
   24884       }
   24885     }
   24886     z[j++] = z[i];
   24887   }
   24888   z[j] = 0;
   24889   return j;
   24890 }
   24891 
   24892 /*
   24893 ** Find a unique file ID for the given absolute pathname.  Return
   24894 ** a pointer to the vxworksFileId object.  This pointer is the unique
   24895 ** file ID.
   24896 **
   24897 ** The nRef field of the vxworksFileId object is incremented before
   24898 ** the object is returned.  A new vxworksFileId object is created
   24899 ** and added to the global list if necessary.
   24900 **
   24901 ** If a memory allocation error occurs, return NULL.
   24902 */
   24903 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
   24904   struct vxworksFileId *pNew;         /* search key and new file ID */
   24905   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
   24906   int n;                              /* Length of zAbsoluteName string */
   24907 
   24908   assert( zAbsoluteName[0]=='/' );
   24909   n = (int)strlen(zAbsoluteName);
   24910   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
   24911   if( pNew==0 ) return 0;
   24912   pNew->zCanonicalName = (char*)&pNew[1];
   24913   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
   24914   n = vxworksSimplifyName(pNew->zCanonicalName, n);
   24915 
   24916   /* Search for an existing entry that matching the canonical name.
   24917   ** If found, increment the reference count and return a pointer to
   24918   ** the existing file ID.
   24919   */
   24920   unixEnterMutex();
   24921   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
   24922     if( pCandidate->nName==n
   24923      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
   24924     ){
   24925        sqlite3_free(pNew);
   24926        pCandidate->nRef++;
   24927        unixLeaveMutex();
   24928        return pCandidate;
   24929     }
   24930   }
   24931 
   24932   /* No match was found.  We will make a new file ID */
   24933   pNew->nRef = 1;
   24934   pNew->nName = n;
   24935   pNew->pNext = vxworksFileList;
   24936   vxworksFileList = pNew;
   24937   unixLeaveMutex();
   24938   return pNew;
   24939 }
   24940 
   24941 /*
   24942 ** Decrement the reference count on a vxworksFileId object.  Free
   24943 ** the object when the reference count reaches zero.
   24944 */
   24945 static void vxworksReleaseFileId(struct vxworksFileId *pId){
   24946   unixEnterMutex();
   24947   assert( pId->nRef>0 );
   24948   pId->nRef--;
   24949   if( pId->nRef==0 ){
   24950     struct vxworksFileId **pp;
   24951     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
   24952     assert( *pp==pId );
   24953     *pp = pId->pNext;
   24954     sqlite3_free(pId);
   24955   }
   24956   unixLeaveMutex();
   24957 }
   24958 #endif /* OS_VXWORKS */
   24959 /*************** End of Unique File ID Utility Used By VxWorks ****************
   24960 ******************************************************************************/
   24961 
   24962 
   24963 /******************************************************************************
   24964 *************************** Posix Advisory Locking ****************************
   24965 **
   24966 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
   24967 ** section 6.5.2.2 lines 483 through 490 specify that when a process
   24968 ** sets or clears a lock, that operation overrides any prior locks set
   24969 ** by the same process.  It does not explicitly say so, but this implies
   24970 ** that it overrides locks set by the same process using a different
   24971 ** file descriptor.  Consider this test case:
   24972 **
   24973 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
   24974 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
   24975 **
   24976 ** Suppose ./file1 and ./file2 are really the same file (because
   24977 ** one is a hard or symbolic link to the other) then if you set
   24978 ** an exclusive lock on fd1, then try to get an exclusive lock
   24979 ** on fd2, it works.  I would have expected the second lock to
   24980 ** fail since there was already a lock on the file due to fd1.
   24981 ** But not so.  Since both locks came from the same process, the
   24982 ** second overrides the first, even though they were on different
   24983 ** file descriptors opened on different file names.
   24984 **
   24985 ** This means that we cannot use POSIX locks to synchronize file access
   24986 ** among competing threads of the same process.  POSIX locks will work fine
   24987 ** to synchronize access for threads in separate processes, but not
   24988 ** threads within the same process.
   24989 **
   24990 ** To work around the problem, SQLite has to manage file locks internally
   24991 ** on its own.  Whenever a new database is opened, we have to find the
   24992 ** specific inode of the database file (the inode is determined by the
   24993 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
   24994 ** and check for locks already existing on that inode.  When locks are
   24995 ** created or removed, we have to look at our own internal record of the
   24996 ** locks to see if another thread has previously set a lock on that same
   24997 ** inode.
   24998 **
   24999 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
   25000 ** For VxWorks, we have to use the alternative unique ID system based on
   25001 ** canonical filename and implemented in the previous division.)
   25002 **
   25003 ** The sqlite3_file structure for POSIX is no longer just an integer file
   25004 ** descriptor.  It is now a structure that holds the integer file
   25005 ** descriptor and a pointer to a structure that describes the internal
   25006 ** locks on the corresponding inode.  There is one locking structure
   25007 ** per inode, so if the same inode is opened twice, both unixFile structures
   25008 ** point to the same locking structure.  The locking structure keeps
   25009 ** a reference count (so we will know when to delete it) and a "cnt"
   25010 ** field that tells us its internal lock status.  cnt==0 means the
   25011 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
   25012 ** cnt>0 means there are cnt shared locks on the file.
   25013 **
   25014 ** Any attempt to lock or unlock a file first checks the locking
   25015 ** structure.  The fcntl() system call is only invoked to set a
   25016 ** POSIX lock if the internal lock structure transitions between
   25017 ** a locked and an unlocked state.
   25018 **
   25019 ** But wait:  there are yet more problems with POSIX advisory locks.
   25020 **
   25021 ** If you close a file descriptor that points to a file that has locks,
   25022 ** all locks on that file that are owned by the current process are
   25023 ** released.  To work around this problem, each unixInodeInfo object
   25024 ** maintains a count of the number of pending locks on tha inode.
   25025 ** When an attempt is made to close an unixFile, if there are
   25026 ** other unixFile open on the same inode that are holding locks, the call
   25027 ** to close() the file descriptor is deferred until all of the locks clear.
   25028 ** The unixInodeInfo structure keeps a list of file descriptors that need to
   25029 ** be closed and that list is walked (and cleared) when the last lock
   25030 ** clears.
   25031 **
   25032 ** Yet another problem:  LinuxThreads do not play well with posix locks.
   25033 **
   25034 ** Many older versions of linux use the LinuxThreads library which is
   25035 ** not posix compliant.  Under LinuxThreads, a lock created by thread
   25036 ** A cannot be modified or overridden by a different thread B.
   25037 ** Only thread A can modify the lock.  Locking behavior is correct
   25038 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
   25039 ** on linux - with NPTL a lock created by thread A can override locks
   25040 ** in thread B.  But there is no way to know at compile-time which
   25041 ** threading library is being used.  So there is no way to know at
   25042 ** compile-time whether or not thread A can override locks on thread B.
   25043 ** One has to do a run-time check to discover the behavior of the
   25044 ** current process.
   25045 **
   25046 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
   25047 ** was dropped beginning with version 3.7.0.  SQLite will still work with
   25048 ** LinuxThreads provided that (1) there is no more than one connection
   25049 ** per database file in the same process and (2) database connections
   25050 ** do not move across threads.
   25051 */
   25052 
   25053 /*
   25054 ** An instance of the following structure serves as the key used
   25055 ** to locate a particular unixInodeInfo object.
   25056 */
   25057 struct unixFileId {
   25058   dev_t dev;                  /* Device number */
   25059 #if OS_VXWORKS
   25060   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
   25061 #else
   25062   ino_t ino;                  /* Inode number */
   25063 #endif
   25064 };
   25065 
   25066 /*
   25067 ** An instance of the following structure is allocated for each open
   25068 ** inode.  Or, on LinuxThreads, there is one of these structures for
   25069 ** each inode opened by each thread.
   25070 **
   25071 ** A single inode can have multiple file descriptors, so each unixFile
   25072 ** structure contains a pointer to an instance of this object and this
   25073 ** object keeps a count of the number of unixFile pointing to it.
   25074 */
   25075 struct unixInodeInfo {
   25076   struct unixFileId fileId;       /* The lookup key */
   25077   int nShared;                    /* Number of SHARED locks held */
   25078   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
   25079   unsigned char bProcessLock;     /* An exclusive process lock is held */
   25080   int nRef;                       /* Number of pointers to this structure */
   25081   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
   25082   int nLock;                      /* Number of outstanding file locks */
   25083   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
   25084   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
   25085   unixInodeInfo *pPrev;           /*    .... doubly linked */
   25086 #if defined(SQLITE_ENABLE_LOCKING_STYLE)
   25087   unsigned long long sharedByte;  /* for AFP simulated shared lock */
   25088 #endif
   25089 #if OS_VXWORKS
   25090   sem_t *pSem;                    /* Named POSIX semaphore */
   25091   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
   25092 #endif
   25093 };
   25094 
   25095 /*
   25096 ** A lists of all unixInodeInfo objects.
   25097 */
   25098 static unixInodeInfo *inodeList = 0;
   25099 
   25100 /*
   25101 **
   25102 ** This function - unixLogError_x(), is only ever called via the macro
   25103 ** unixLogError().
   25104 **
   25105 ** It is invoked after an error occurs in an OS function and errno has been
   25106 ** set. It logs a message using sqlite3_log() containing the current value of
   25107 ** errno and, if possible, the human-readable equivalent from strerror() or
   25108 ** strerror_r().
   25109 **
   25110 ** The first argument passed to the macro should be the error code that
   25111 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
   25112 ** The two subsequent arguments should be the name of the OS function that
   25113 ** failed (e.g. "unlink", "open") and the the associated file-system path,
   25114 ** if any.
   25115 */
   25116 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
   25117 static int unixLogErrorAtLine(
   25118   int errcode,                    /* SQLite error code */
   25119   const char *zFunc,              /* Name of OS function that failed */
   25120   const char *zPath,              /* File path associated with error */
   25121   int iLine                       /* Source line number where error occurred */
   25122 ){
   25123   char *zErr;                     /* Message from strerror() or equivalent */
   25124   int iErrno = errno;             /* Saved syscall error number */
   25125 
   25126   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
   25127   ** the strerror() function to obtain the human-readable error message
   25128   ** equivalent to errno. Otherwise, use strerror_r().
   25129   */
   25130 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
   25131   char aErr[80];
   25132   memset(aErr, 0, sizeof(aErr));
   25133   zErr = aErr;
   25134 
   25135   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
   25136   ** assume that the system provides the the GNU version of strerror_r() that
   25137   ** returns a pointer to a buffer containing the error message. That pointer
   25138   ** may point to aErr[], or it may point to some static storage somewhere.
   25139   ** Otherwise, assume that the system provides the POSIX version of
   25140   ** strerror_r(), which always writes an error message into aErr[].
   25141   **
   25142   ** If the code incorrectly assumes that it is the POSIX version that is
   25143   ** available, the error message will often be an empty string. Not a
   25144   ** huge problem. Incorrectly concluding that the GNU version is available
   25145   ** could lead to a segfault though.
   25146   */
   25147 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
   25148   zErr =
   25149 # endif
   25150   strerror_r(iErrno, aErr, sizeof(aErr)-1);
   25151 
   25152 #elif SQLITE_THREADSAFE
   25153   /* This is a threadsafe build, but strerror_r() is not available. */
   25154   zErr = "";
   25155 #else
   25156   /* Non-threadsafe build, use strerror(). */
   25157   zErr = strerror(iErrno);
   25158 #endif
   25159 
   25160   assert( errcode!=SQLITE_OK );
   25161   if( zPath==0 ) zPath = "";
   25162   sqlite3_log(errcode,
   25163       "os_unix.c:%d: (%d) %s(%s) - %s",
   25164       iLine, iErrno, zFunc, zPath, zErr
   25165   );
   25166 
   25167   return errcode;
   25168 }
   25169 
   25170 /*
   25171 ** Close a file descriptor.
   25172 **
   25173 ** We assume that close() almost always works, since it is only in a
   25174 ** very sick application or on a very sick platform that it might fail.
   25175 ** If it does fail, simply leak the file descriptor, but do log the
   25176 ** error.
   25177 **
   25178 ** Note that it is not safe to retry close() after EINTR since the
   25179 ** file descriptor might have already been reused by another thread.
   25180 ** So we don't even try to recover from an EINTR.  Just log the error
   25181 ** and move on.
   25182 */
   25183 static void robust_close(unixFile *pFile, int h, int lineno){
   25184   if( osClose(h) ){
   25185     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
   25186                        pFile ? pFile->zPath : 0, lineno);
   25187   }
   25188 }
   25189 
   25190 /*
   25191 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
   25192 */
   25193 static void closePendingFds(unixFile *pFile){
   25194   unixInodeInfo *pInode = pFile->pInode;
   25195   UnixUnusedFd *p;
   25196   UnixUnusedFd *pNext;
   25197   for(p=pInode->pUnused; p; p=pNext){
   25198     pNext = p->pNext;
   25199     robust_close(pFile, p->fd, __LINE__);
   25200     sqlite3_free(p);
   25201   }
   25202   pInode->pUnused = 0;
   25203 }
   25204 
   25205 /*
   25206 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
   25207 **
   25208 ** The mutex entered using the unixEnterMutex() function must be held
   25209 ** when this function is called.
   25210 */
   25211 static void releaseInodeInfo(unixFile *pFile){
   25212   unixInodeInfo *pInode = pFile->pInode;
   25213   assert( unixMutexHeld() );
   25214   if( ALWAYS(pInode) ){
   25215     pInode->nRef--;
   25216     if( pInode->nRef==0 ){
   25217       assert( pInode->pShmNode==0 );
   25218       closePendingFds(pFile);
   25219       if( pInode->pPrev ){
   25220         assert( pInode->pPrev->pNext==pInode );
   25221         pInode->pPrev->pNext = pInode->pNext;
   25222       }else{
   25223         assert( inodeList==pInode );
   25224         inodeList = pInode->pNext;
   25225       }
   25226       if( pInode->pNext ){
   25227         assert( pInode->pNext->pPrev==pInode );
   25228         pInode->pNext->pPrev = pInode->pPrev;
   25229       }
   25230       sqlite3_free(pInode);
   25231     }
   25232   }
   25233 }
   25234 
   25235 /*
   25236 ** Given a file descriptor, locate the unixInodeInfo object that
   25237 ** describes that file descriptor.  Create a new one if necessary.  The
   25238 ** return value might be uninitialized if an error occurs.
   25239 **
   25240 ** The mutex entered using the unixEnterMutex() function must be held
   25241 ** when this function is called.
   25242 **
   25243 ** Return an appropriate error code.
   25244 */
   25245 static int findInodeInfo(
   25246   unixFile *pFile,               /* Unix file with file desc used in the key */
   25247   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
   25248 ){
   25249   int rc;                        /* System call return code */
   25250   int fd;                        /* The file descriptor for pFile */
   25251   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
   25252   struct stat statbuf;           /* Low-level file information */
   25253   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
   25254 
   25255   assert( unixMutexHeld() );
   25256 
   25257   /* Get low-level information about the file that we can used to
   25258   ** create a unique name for the file.
   25259   */
   25260   fd = pFile->h;
   25261   rc = osFstat(fd, &statbuf);
   25262   if( rc!=0 ){
   25263     pFile->lastErrno = errno;
   25264 #ifdef EOVERFLOW
   25265     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
   25266 #endif
   25267     return SQLITE_IOERR;
   25268   }
   25269 
   25270 #ifdef __APPLE__
   25271   /* On OS X on an msdos filesystem, the inode number is reported
   25272   ** incorrectly for zero-size files.  See ticket #3260.  To work
   25273   ** around this problem (we consider it a bug in OS X, not SQLite)
   25274   ** we always increase the file size to 1 by writing a single byte
   25275   ** prior to accessing the inode number.  The one byte written is
   25276   ** an ASCII 'S' character which also happens to be the first byte
   25277   ** in the header of every SQLite database.  In this way, if there
   25278   ** is a race condition such that another thread has already populated
   25279   ** the first page of the database, no damage is done.
   25280   */
   25281   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
   25282     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
   25283     if( rc!=1 ){
   25284       pFile->lastErrno = errno;
   25285       return SQLITE_IOERR;
   25286     }
   25287     rc = osFstat(fd, &statbuf);
   25288     if( rc!=0 ){
   25289       pFile->lastErrno = errno;
   25290       return SQLITE_IOERR;
   25291     }
   25292   }
   25293 #endif
   25294 
   25295   memset(&fileId, 0, sizeof(fileId));
   25296   fileId.dev = statbuf.st_dev;
   25297 #if OS_VXWORKS
   25298   fileId.pId = pFile->pId;
   25299 #else
   25300   fileId.ino = statbuf.st_ino;
   25301 #endif
   25302   pInode = inodeList;
   25303   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
   25304     pInode = pInode->pNext;
   25305   }
   25306   if( pInode==0 ){
   25307     pInode = sqlite3_malloc( sizeof(*pInode) );
   25308     if( pInode==0 ){
   25309       return SQLITE_NOMEM;
   25310     }
   25311     memset(pInode, 0, sizeof(*pInode));
   25312     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
   25313     pInode->nRef = 1;
   25314     pInode->pNext = inodeList;
   25315     pInode->pPrev = 0;
   25316     if( inodeList ) inodeList->pPrev = pInode;
   25317     inodeList = pInode;
   25318   }else{
   25319     pInode->nRef++;
   25320   }
   25321   *ppInode = pInode;
   25322   return SQLITE_OK;
   25323 }
   25324 
   25325 
   25326 /*
   25327 ** This routine checks if there is a RESERVED lock held on the specified
   25328 ** file by this or any other process. If such a lock is held, set *pResOut
   25329 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   25330 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   25331 */
   25332 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
   25333   int rc = SQLITE_OK;
   25334   int reserved = 0;
   25335   unixFile *pFile = (unixFile*)id;
   25336 
   25337   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   25338 
   25339   assert( pFile );
   25340   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
   25341 
   25342   /* Check if a thread in this process holds such a lock */
   25343   if( pFile->pInode->eFileLock>SHARED_LOCK ){
   25344     reserved = 1;
   25345   }
   25346 
   25347   /* Otherwise see if some other process holds it.
   25348   */
   25349 #ifndef __DJGPP__
   25350   if( !reserved && !pFile->pInode->bProcessLock ){
   25351     struct flock lock;
   25352     lock.l_whence = SEEK_SET;
   25353     lock.l_start = RESERVED_BYTE;
   25354     lock.l_len = 1;
   25355     lock.l_type = F_WRLCK;
   25356     if( osFcntl(pFile->h, F_GETLK, &lock) ){
   25357       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
   25358       pFile->lastErrno = errno;
   25359     } else if( lock.l_type!=F_UNLCK ){
   25360       reserved = 1;
   25361     }
   25362   }
   25363 #endif
   25364 
   25365   unixLeaveMutex();
   25366   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
   25367 
   25368   *pResOut = reserved;
   25369   return rc;
   25370 }
   25371 
   25372 /*
   25373 ** Attempt to set a system-lock on the file pFile.  The lock is
   25374 ** described by pLock.
   25375 **
   25376 ** If the pFile was opened read/write from unix-excl, then the only lock
   25377 ** ever obtained is an exclusive lock, and it is obtained exactly once
   25378 ** the first time any lock is attempted.  All subsequent system locking
   25379 ** operations become no-ops.  Locking operations still happen internally,
   25380 ** in order to coordinate access between separate database connections
   25381 ** within this process, but all of that is handled in memory and the
   25382 ** operating system does not participate.
   25383 **
   25384 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
   25385 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
   25386 ** and is read-only.
   25387 **
   25388 ** Zero is returned if the call completes successfully, or -1 if a call
   25389 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
   25390 */
   25391 static int unixFileLock(unixFile *pFile, struct flock *pLock){
   25392   int rc;
   25393   unixInodeInfo *pInode = pFile->pInode;
   25394   assert( unixMutexHeld() );
   25395   assert( pInode!=0 );
   25396   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
   25397    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
   25398   ){
   25399     if( pInode->bProcessLock==0 ){
   25400       struct flock lock;
   25401       assert( pInode->nLock==0 );
   25402       lock.l_whence = SEEK_SET;
   25403       lock.l_start = SHARED_FIRST;
   25404       lock.l_len = SHARED_SIZE;
   25405       lock.l_type = F_WRLCK;
   25406       rc = osFcntl(pFile->h, F_SETLK, &lock);
   25407       if( rc<0 ) return rc;
   25408       pInode->bProcessLock = 1;
   25409       pInode->nLock++;
   25410     }else{
   25411       rc = 0;
   25412     }
   25413   }else{
   25414     rc = osFcntl(pFile->h, F_SETLK, pLock);
   25415   }
   25416   return rc;
   25417 }
   25418 
   25419 /*
   25420 ** Lock the file with the lock specified by parameter eFileLock - one
   25421 ** of the following:
   25422 **
   25423 **     (1) SHARED_LOCK
   25424 **     (2) RESERVED_LOCK
   25425 **     (3) PENDING_LOCK
   25426 **     (4) EXCLUSIVE_LOCK
   25427 **
   25428 ** Sometimes when requesting one lock state, additional lock states
   25429 ** are inserted in between.  The locking might fail on one of the later
   25430 ** transitions leaving the lock state different from what it started but
   25431 ** still short of its goal.  The following chart shows the allowed
   25432 ** transitions and the inserted intermediate states:
   25433 **
   25434 **    UNLOCKED -> SHARED
   25435 **    SHARED -> RESERVED
   25436 **    SHARED -> (PENDING) -> EXCLUSIVE
   25437 **    RESERVED -> (PENDING) -> EXCLUSIVE
   25438 **    PENDING -> EXCLUSIVE
   25439 **
   25440 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   25441 ** routine to lower a locking level.
   25442 */
   25443 static int unixLock(sqlite3_file *id, int eFileLock){
   25444   /* The following describes the implementation of the various locks and
   25445   ** lock transitions in terms of the POSIX advisory shared and exclusive
   25446   ** lock primitives (called read-locks and write-locks below, to avoid
   25447   ** confusion with SQLite lock names). The algorithms are complicated
   25448   ** slightly in order to be compatible with windows systems simultaneously
   25449   ** accessing the same database file, in case that is ever required.
   25450   **
   25451   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
   25452   ** byte', each single bytes at well known offsets, and the 'shared byte
   25453   ** range', a range of 510 bytes at a well known offset.
   25454   **
   25455   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
   25456   ** byte'.  If this is successful, a random byte from the 'shared byte
   25457   ** range' is read-locked and the lock on the 'pending byte' released.
   25458   **
   25459   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
   25460   ** A RESERVED lock is implemented by grabbing a write-lock on the
   25461   ** 'reserved byte'.
   25462   **
   25463   ** A process may only obtain a PENDING lock after it has obtained a
   25464   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
   25465   ** on the 'pending byte'. This ensures that no new SHARED locks can be
   25466   ** obtained, but existing SHARED locks are allowed to persist. A process
   25467   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
   25468   ** This property is used by the algorithm for rolling back a journal file
   25469   ** after a crash.
   25470   **
   25471   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
   25472   ** implemented by obtaining a write-lock on the entire 'shared byte
   25473   ** range'. Since all other locks require a read-lock on one of the bytes
   25474   ** within this range, this ensures that no other locks are held on the
   25475   ** database.
   25476   **
   25477   ** The reason a single byte cannot be used instead of the 'shared byte
   25478   ** range' is that some versions of windows do not support read-locks. By
   25479   ** locking a random byte from a range, concurrent SHARED locks may exist
   25480   ** even if the locking primitive used is always a write-lock.
   25481   */
   25482   int rc = SQLITE_OK;
   25483   unixFile *pFile = (unixFile*)id;
   25484   unixInodeInfo *pInode = pFile->pInode;
   25485   struct flock lock;
   25486   int tErrno = 0;
   25487 
   25488   assert( pFile );
   25489   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
   25490       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
   25491       azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
   25492 
   25493   /* If there is already a lock of this type or more restrictive on the
   25494   ** unixFile, do nothing. Don't use the end_lock: exit path, as
   25495   ** unixEnterMutex() hasn't been called yet.
   25496   */
   25497   if( pFile->eFileLock>=eFileLock ){
   25498     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
   25499             azFileLock(eFileLock)));
   25500     return SQLITE_OK;
   25501   }
   25502 
   25503   /* Make sure the locking sequence is correct.
   25504   **  (1) We never move from unlocked to anything higher than shared lock.
   25505   **  (2) SQLite never explicitly requests a pendig lock.
   25506   **  (3) A shared lock is always held when a reserve lock is requested.
   25507   */
   25508   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
   25509   assert( eFileLock!=PENDING_LOCK );
   25510   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
   25511 
   25512   /* This mutex is needed because pFile->pInode is shared across threads
   25513   */
   25514   unixEnterMutex();
   25515   pInode = pFile->pInode;
   25516 
   25517   /* If some thread using this PID has a lock via a different unixFile*
   25518   ** handle that precludes the requested lock, return BUSY.
   25519   */
   25520   if( (pFile->eFileLock!=pInode->eFileLock &&
   25521           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
   25522   ){
   25523     rc = SQLITE_BUSY;
   25524     goto end_lock;
   25525   }
   25526 
   25527   /* If a SHARED lock is requested, and some thread using this PID already
   25528   ** has a SHARED or RESERVED lock, then increment reference counts and
   25529   ** return SQLITE_OK.
   25530   */
   25531   if( eFileLock==SHARED_LOCK &&
   25532       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
   25533     assert( eFileLock==SHARED_LOCK );
   25534     assert( pFile->eFileLock==0 );
   25535     assert( pInode->nShared>0 );
   25536     pFile->eFileLock = SHARED_LOCK;
   25537     pInode->nShared++;
   25538     pInode->nLock++;
   25539     goto end_lock;
   25540   }
   25541 
   25542 
   25543   /* A PENDING lock is needed before acquiring a SHARED lock and before
   25544   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
   25545   ** be released.
   25546   */
   25547   lock.l_len = 1L;
   25548   lock.l_whence = SEEK_SET;
   25549   if( eFileLock==SHARED_LOCK
   25550       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
   25551   ){
   25552     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
   25553     lock.l_start = PENDING_BYTE;
   25554     if( unixFileLock(pFile, &lock) ){
   25555       tErrno = errno;
   25556       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   25557       if( rc!=SQLITE_BUSY ){
   25558         pFile->lastErrno = tErrno;
   25559       }
   25560       goto end_lock;
   25561     }
   25562   }
   25563 
   25564 
   25565   /* If control gets to this point, then actually go ahead and make
   25566   ** operating system calls for the specified lock.
   25567   */
   25568   if( eFileLock==SHARED_LOCK ){
   25569     assert( pInode->nShared==0 );
   25570     assert( pInode->eFileLock==0 );
   25571     assert( rc==SQLITE_OK );
   25572 
   25573     /* Now get the read-lock */
   25574     lock.l_start = SHARED_FIRST;
   25575     lock.l_len = SHARED_SIZE;
   25576     if( unixFileLock(pFile, &lock) ){
   25577       tErrno = errno;
   25578       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   25579     }
   25580 
   25581     /* Drop the temporary PENDING lock */
   25582     lock.l_start = PENDING_BYTE;
   25583     lock.l_len = 1L;
   25584     lock.l_type = F_UNLCK;
   25585     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
   25586       /* This could happen with a network mount */
   25587       tErrno = errno;
   25588       rc = SQLITE_IOERR_UNLOCK;
   25589     }
   25590 
   25591     if( rc ){
   25592       if( rc!=SQLITE_BUSY ){
   25593         pFile->lastErrno = tErrno;
   25594       }
   25595       goto end_lock;
   25596     }else{
   25597       pFile->eFileLock = SHARED_LOCK;
   25598       pInode->nLock++;
   25599       pInode->nShared = 1;
   25600     }
   25601   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
   25602     /* We are trying for an exclusive lock but another thread in this
   25603     ** same process is still holding a shared lock. */
   25604     rc = SQLITE_BUSY;
   25605   }else{
   25606     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
   25607     ** assumed that there is a SHARED or greater lock on the file
   25608     ** already.
   25609     */
   25610     assert( 0!=pFile->eFileLock );
   25611     lock.l_type = F_WRLCK;
   25612 
   25613     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
   25614     if( eFileLock==RESERVED_LOCK ){
   25615       lock.l_start = RESERVED_BYTE;
   25616       lock.l_len = 1L;
   25617     }else{
   25618       lock.l_start = SHARED_FIRST;
   25619       lock.l_len = SHARED_SIZE;
   25620     }
   25621 
   25622     if( unixFileLock(pFile, &lock) ){
   25623       tErrno = errno;
   25624       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   25625       if( rc!=SQLITE_BUSY ){
   25626         pFile->lastErrno = tErrno;
   25627       }
   25628     }
   25629   }
   25630 
   25631 
   25632 #ifndef NDEBUG
   25633   /* Set up the transaction-counter change checking flags when
   25634   ** transitioning from a SHARED to a RESERVED lock.  The change
   25635   ** from SHARED to RESERVED marks the beginning of a normal
   25636   ** write operation (not a hot journal rollback).
   25637   */
   25638   if( rc==SQLITE_OK
   25639    && pFile->eFileLock<=SHARED_LOCK
   25640    && eFileLock==RESERVED_LOCK
   25641   ){
   25642     pFile->transCntrChng = 0;
   25643     pFile->dbUpdate = 0;
   25644     pFile->inNormalWrite = 1;
   25645   }
   25646 #endif
   25647 
   25648 
   25649   if( rc==SQLITE_OK ){
   25650     pFile->eFileLock = eFileLock;
   25651     pInode->eFileLock = eFileLock;
   25652   }else if( eFileLock==EXCLUSIVE_LOCK ){
   25653     pFile->eFileLock = PENDING_LOCK;
   25654     pInode->eFileLock = PENDING_LOCK;
   25655   }
   25656 
   25657 end_lock:
   25658   unixLeaveMutex();
   25659   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
   25660       rc==SQLITE_OK ? "ok" : "failed"));
   25661   return rc;
   25662 }
   25663 
   25664 /*
   25665 ** Add the file descriptor used by file handle pFile to the corresponding
   25666 ** pUnused list.
   25667 */
   25668 static void setPendingFd(unixFile *pFile){
   25669   unixInodeInfo *pInode = pFile->pInode;
   25670   UnixUnusedFd *p = pFile->pUnused;
   25671   p->pNext = pInode->pUnused;
   25672   pInode->pUnused = p;
   25673   pFile->h = -1;
   25674   pFile->pUnused = 0;
   25675 }
   25676 
   25677 /*
   25678 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   25679 ** must be either NO_LOCK or SHARED_LOCK.
   25680 **
   25681 ** If the locking level of the file descriptor is already at or below
   25682 ** the requested locking level, this routine is a no-op.
   25683 **
   25684 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
   25685 ** the byte range is divided into 2 parts and the first part is unlocked then
   25686 ** set to a read lock, then the other part is simply unlocked.  This works
   25687 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
   25688 ** remove the write lock on a region when a read lock is set.
   25689 */
   25690 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
   25691   unixFile *pFile = (unixFile*)id;
   25692   unixInodeInfo *pInode;
   25693   struct flock lock;
   25694   int rc = SQLITE_OK;
   25695   int h;
   25696 
   25697   assert( pFile );
   25698   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
   25699       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
   25700       getpid()));
   25701 
   25702   assert( eFileLock<=SHARED_LOCK );
   25703   if( pFile->eFileLock<=eFileLock ){
   25704     return SQLITE_OK;
   25705   }
   25706   unixEnterMutex();
   25707   h = pFile->h;
   25708   pInode = pFile->pInode;
   25709   assert( pInode->nShared!=0 );
   25710   if( pFile->eFileLock>SHARED_LOCK ){
   25711     assert( pInode->eFileLock==pFile->eFileLock );
   25712     SimulateIOErrorBenign(1);
   25713     SimulateIOError( h=(-1) )
   25714     SimulateIOErrorBenign(0);
   25715 
   25716 #ifndef NDEBUG
   25717     /* When reducing a lock such that other processes can start
   25718     ** reading the database file again, make sure that the
   25719     ** transaction counter was updated if any part of the database
   25720     ** file changed.  If the transaction counter is not updated,
   25721     ** other connections to the same file might not realize that
   25722     ** the file has changed and hence might not know to flush their
   25723     ** cache.  The use of a stale cache can lead to database corruption.
   25724     */
   25725 #if 0
   25726     assert( pFile->inNormalWrite==0
   25727          || pFile->dbUpdate==0
   25728          || pFile->transCntrChng==1 );
   25729 #endif
   25730     pFile->inNormalWrite = 0;
   25731 #endif
   25732 
   25733     /* downgrading to a shared lock on NFS involves clearing the write lock
   25734     ** before establishing the readlock - to avoid a race condition we downgrade
   25735     ** the lock in 2 blocks, so that part of the range will be covered by a
   25736     ** write lock until the rest is covered by a read lock:
   25737     **  1:   [WWWWW]
   25738     **  2:   [....W]
   25739     **  3:   [RRRRW]
   25740     **  4:   [RRRR.]
   25741     */
   25742     if( eFileLock==SHARED_LOCK ){
   25743 
   25744 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
   25745       (void)handleNFSUnlock;
   25746       assert( handleNFSUnlock==0 );
   25747 #endif
   25748 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   25749       if( handleNFSUnlock ){
   25750         int tErrno;               /* Error code from system call errors */
   25751         off_t divSize = SHARED_SIZE - 1;
   25752 
   25753         lock.l_type = F_UNLCK;
   25754         lock.l_whence = SEEK_SET;
   25755         lock.l_start = SHARED_FIRST;
   25756         lock.l_len = divSize;
   25757         if( unixFileLock(pFile, &lock)==(-1) ){
   25758           tErrno = errno;
   25759           rc = SQLITE_IOERR_UNLOCK;
   25760           if( IS_LOCK_ERROR(rc) ){
   25761             pFile->lastErrno = tErrno;
   25762           }
   25763           goto end_unlock;
   25764         }
   25765         lock.l_type = F_RDLCK;
   25766         lock.l_whence = SEEK_SET;
   25767         lock.l_start = SHARED_FIRST;
   25768         lock.l_len = divSize;
   25769         if( unixFileLock(pFile, &lock)==(-1) ){
   25770           tErrno = errno;
   25771           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
   25772           if( IS_LOCK_ERROR(rc) ){
   25773             pFile->lastErrno = tErrno;
   25774           }
   25775           goto end_unlock;
   25776         }
   25777         lock.l_type = F_UNLCK;
   25778         lock.l_whence = SEEK_SET;
   25779         lock.l_start = SHARED_FIRST+divSize;
   25780         lock.l_len = SHARED_SIZE-divSize;
   25781         if( unixFileLock(pFile, &lock)==(-1) ){
   25782           tErrno = errno;
   25783           rc = SQLITE_IOERR_UNLOCK;
   25784           if( IS_LOCK_ERROR(rc) ){
   25785             pFile->lastErrno = tErrno;
   25786           }
   25787           goto end_unlock;
   25788         }
   25789       }else
   25790 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   25791       {
   25792         lock.l_type = F_RDLCK;
   25793         lock.l_whence = SEEK_SET;
   25794         lock.l_start = SHARED_FIRST;
   25795         lock.l_len = SHARED_SIZE;
   25796         if( unixFileLock(pFile, &lock) ){
   25797           /* In theory, the call to unixFileLock() cannot fail because another
   25798           ** process is holding an incompatible lock. If it does, this
   25799           ** indicates that the other process is not following the locking
   25800           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
   25801           ** SQLITE_BUSY would confuse the upper layer (in practice it causes
   25802           ** an assert to fail). */
   25803           rc = SQLITE_IOERR_RDLOCK;
   25804           pFile->lastErrno = errno;
   25805           goto end_unlock;
   25806         }
   25807       }
   25808     }
   25809     lock.l_type = F_UNLCK;
   25810     lock.l_whence = SEEK_SET;
   25811     lock.l_start = PENDING_BYTE;
   25812     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
   25813     if( unixFileLock(pFile, &lock)==0 ){
   25814       pInode->eFileLock = SHARED_LOCK;
   25815     }else{
   25816       rc = SQLITE_IOERR_UNLOCK;
   25817       pFile->lastErrno = errno;
   25818       goto end_unlock;
   25819     }
   25820   }
   25821   if( eFileLock==NO_LOCK ){
   25822     /* Decrement the shared lock counter.  Release the lock using an
   25823     ** OS call only when all threads in this same process have released
   25824     ** the lock.
   25825     */
   25826     pInode->nShared--;
   25827     if( pInode->nShared==0 ){
   25828       lock.l_type = F_UNLCK;
   25829       lock.l_whence = SEEK_SET;
   25830       lock.l_start = lock.l_len = 0L;
   25831       SimulateIOErrorBenign(1);
   25832       SimulateIOError( h=(-1) )
   25833       SimulateIOErrorBenign(0);
   25834       if( unixFileLock(pFile, &lock)==0 ){
   25835         pInode->eFileLock = NO_LOCK;
   25836       }else{
   25837         rc = SQLITE_IOERR_UNLOCK;
   25838 	pFile->lastErrno = errno;
   25839         pInode->eFileLock = NO_LOCK;
   25840         pFile->eFileLock = NO_LOCK;
   25841       }
   25842     }
   25843 
   25844     /* Decrement the count of locks against this same file.  When the
   25845     ** count reaches zero, close any other file descriptors whose close
   25846     ** was deferred because of outstanding locks.
   25847     */
   25848     pInode->nLock--;
   25849     assert( pInode->nLock>=0 );
   25850     if( pInode->nLock==0 ){
   25851       closePendingFds(pFile);
   25852     }
   25853   }
   25854 
   25855 end_unlock:
   25856   unixLeaveMutex();
   25857   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
   25858   return rc;
   25859 }
   25860 
   25861 /*
   25862 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   25863 ** must be either NO_LOCK or SHARED_LOCK.
   25864 **
   25865 ** If the locking level of the file descriptor is already at or below
   25866 ** the requested locking level, this routine is a no-op.
   25867 */
   25868 static int unixUnlock(sqlite3_file *id, int eFileLock){
   25869   return posixUnlock(id, eFileLock, 0);
   25870 }
   25871 
   25872 /*
   25873 ** This function performs the parts of the "close file" operation
   25874 ** common to all locking schemes. It closes the directory and file
   25875 ** handles, if they are valid, and sets all fields of the unixFile
   25876 ** structure to 0.
   25877 **
   25878 ** It is *not* necessary to hold the mutex when this routine is called,
   25879 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
   25880 ** vxworksReleaseFileId() routine.
   25881 */
   25882 static int closeUnixFile(sqlite3_file *id){
   25883   unixFile *pFile = (unixFile*)id;
   25884   if( pFile->h>=0 ){
   25885     robust_close(pFile, pFile->h, __LINE__);
   25886     pFile->h = -1;
   25887   }
   25888 #if OS_VXWORKS
   25889   if( pFile->pId ){
   25890     if( pFile->isDelete ){
   25891       osUnlink(pFile->pId->zCanonicalName);
   25892     }
   25893     vxworksReleaseFileId(pFile->pId);
   25894     pFile->pId = 0;
   25895   }
   25896 #endif
   25897   OSTRACE(("CLOSE   %-3d\n", pFile->h));
   25898   OpenCounter(-1);
   25899   sqlite3_free(pFile->pUnused);
   25900   memset(pFile, 0, sizeof(unixFile));
   25901   return SQLITE_OK;
   25902 }
   25903 
   25904 /*
   25905 ** Close a file.
   25906 */
   25907 static int unixClose(sqlite3_file *id){
   25908   int rc = SQLITE_OK;
   25909   unixFile *pFile = (unixFile *)id;
   25910   unixUnlock(id, NO_LOCK);
   25911   unixEnterMutex();
   25912 
   25913   /* unixFile.pInode is always valid here. Otherwise, a different close
   25914   ** routine (e.g. nolockClose()) would be called instead.
   25915   */
   25916   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
   25917   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
   25918     /* If there are outstanding locks, do not actually close the file just
   25919     ** yet because that would clear those locks.  Instead, add the file
   25920     ** descriptor to pInode->pUnused list.  It will be automatically closed
   25921     ** when the last lock is cleared.
   25922     */
   25923     setPendingFd(pFile);
   25924   }
   25925   releaseInodeInfo(pFile);
   25926   rc = closeUnixFile(id);
   25927   unixLeaveMutex();
   25928   return rc;
   25929 }
   25930 
   25931 /************** End of the posix advisory lock implementation *****************
   25932 ******************************************************************************/
   25933 
   25934 /******************************************************************************
   25935 ****************************** No-op Locking **********************************
   25936 **
   25937 ** Of the various locking implementations available, this is by far the
   25938 ** simplest:  locking is ignored.  No attempt is made to lock the database
   25939 ** file for reading or writing.
   25940 **
   25941 ** This locking mode is appropriate for use on read-only databases
   25942 ** (ex: databases that are burned into CD-ROM, for example.)  It can
   25943 ** also be used if the application employs some external mechanism to
   25944 ** prevent simultaneous access of the same database by two or more
   25945 ** database connections.  But there is a serious risk of database
   25946 ** corruption if this locking mode is used in situations where multiple
   25947 ** database connections are accessing the same database file at the same
   25948 ** time and one or more of those connections are writing.
   25949 */
   25950 
   25951 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
   25952   UNUSED_PARAMETER(NotUsed);
   25953   *pResOut = 0;
   25954   return SQLITE_OK;
   25955 }
   25956 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
   25957   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   25958   return SQLITE_OK;
   25959 }
   25960 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
   25961   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   25962   return SQLITE_OK;
   25963 }
   25964 
   25965 /*
   25966 ** Close the file.
   25967 */
   25968 static int nolockClose(sqlite3_file *id) {
   25969   return closeUnixFile(id);
   25970 }
   25971 
   25972 /******************* End of the no-op lock implementation *********************
   25973 ******************************************************************************/
   25974 
   25975 /******************************************************************************
   25976 ************************* Begin dot-file Locking ******************************
   25977 **
   25978 ** The dotfile locking implementation uses the existance of separate lock
   25979 ** files in order to control access to the database.  This works on just
   25980 ** about every filesystem imaginable.  But there are serious downsides:
   25981 **
   25982 **    (1)  There is zero concurrency.  A single reader blocks all other
   25983 **         connections from reading or writing the database.
   25984 **
   25985 **    (2)  An application crash or power loss can leave stale lock files
   25986 **         sitting around that need to be cleared manually.
   25987 **
   25988 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
   25989 ** other locking strategy is available.
   25990 **
   25991 ** Dotfile locking works by creating a file in the same directory as the
   25992 ** database and with the same name but with a ".lock" extension added.
   25993 ** The existance of a lock file implies an EXCLUSIVE lock.  All other lock
   25994 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
   25995 */
   25996 
   25997 /*
   25998 ** The file suffix added to the data base filename in order to create the
   25999 ** lock file.
   26000 */
   26001 #define DOTLOCK_SUFFIX ".lock"
   26002 
   26003 /*
   26004 ** This routine checks if there is a RESERVED lock held on the specified
   26005 ** file by this or any other process. If such a lock is held, set *pResOut
   26006 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26007 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26008 **
   26009 ** In dotfile locking, either a lock exists or it does not.  So in this
   26010 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
   26011 ** is held on the file and false if the file is unlocked.
   26012 */
   26013 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
   26014   int rc = SQLITE_OK;
   26015   int reserved = 0;
   26016   unixFile *pFile = (unixFile*)id;
   26017 
   26018   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26019 
   26020   assert( pFile );
   26021 
   26022   /* Check if a thread in this process holds such a lock */
   26023   if( pFile->eFileLock>SHARED_LOCK ){
   26024     /* Either this connection or some other connection in the same process
   26025     ** holds a lock on the file.  No need to check further. */
   26026     reserved = 1;
   26027   }else{
   26028     /* The lock is held if and only if the lockfile exists */
   26029     const char *zLockFile = (const char*)pFile->lockingContext;
   26030     reserved = osAccess(zLockFile, 0)==0;
   26031   }
   26032   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
   26033   *pResOut = reserved;
   26034   return rc;
   26035 }
   26036 
   26037 /*
   26038 ** Lock the file with the lock specified by parameter eFileLock - one
   26039 ** of the following:
   26040 **
   26041 **     (1) SHARED_LOCK
   26042 **     (2) RESERVED_LOCK
   26043 **     (3) PENDING_LOCK
   26044 **     (4) EXCLUSIVE_LOCK
   26045 **
   26046 ** Sometimes when requesting one lock state, additional lock states
   26047 ** are inserted in between.  The locking might fail on one of the later
   26048 ** transitions leaving the lock state different from what it started but
   26049 ** still short of its goal.  The following chart shows the allowed
   26050 ** transitions and the inserted intermediate states:
   26051 **
   26052 **    UNLOCKED -> SHARED
   26053 **    SHARED -> RESERVED
   26054 **    SHARED -> (PENDING) -> EXCLUSIVE
   26055 **    RESERVED -> (PENDING) -> EXCLUSIVE
   26056 **    PENDING -> EXCLUSIVE
   26057 **
   26058 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26059 ** routine to lower a locking level.
   26060 **
   26061 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
   26062 ** But we track the other locking levels internally.
   26063 */
   26064 static int dotlockLock(sqlite3_file *id, int eFileLock) {
   26065   unixFile *pFile = (unixFile*)id;
   26066   int fd;
   26067   char *zLockFile = (char *)pFile->lockingContext;
   26068   int rc = SQLITE_OK;
   26069 
   26070 
   26071   /* If we have any lock, then the lock file already exists.  All we have
   26072   ** to do is adjust our internal record of the lock level.
   26073   */
   26074   if( pFile->eFileLock > NO_LOCK ){
   26075     pFile->eFileLock = eFileLock;
   26076 #if !OS_VXWORKS
   26077     /* Always update the timestamp on the old file */
   26078     utimes(zLockFile, NULL);
   26079 #endif
   26080     return SQLITE_OK;
   26081   }
   26082 
   26083   /* grab an exclusive lock */
   26084   fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
   26085   if( fd<0 ){
   26086     /* failed to open/create the file, someone else may have stolen the lock */
   26087     int tErrno = errno;
   26088     if( EEXIST == tErrno ){
   26089       rc = SQLITE_BUSY;
   26090     } else {
   26091       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26092       if( IS_LOCK_ERROR(rc) ){
   26093         pFile->lastErrno = tErrno;
   26094       }
   26095     }
   26096     return rc;
   26097   }
   26098   robust_close(pFile, fd, __LINE__);
   26099 
   26100   /* got it, set the type and return ok */
   26101   pFile->eFileLock = eFileLock;
   26102   return rc;
   26103 }
   26104 
   26105 /*
   26106 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26107 ** must be either NO_LOCK or SHARED_LOCK.
   26108 **
   26109 ** If the locking level of the file descriptor is already at or below
   26110 ** the requested locking level, this routine is a no-op.
   26111 **
   26112 ** When the locking level reaches NO_LOCK, delete the lock file.
   26113 */
   26114 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
   26115   unixFile *pFile = (unixFile*)id;
   26116   char *zLockFile = (char *)pFile->lockingContext;
   26117 
   26118   assert( pFile );
   26119   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
   26120 	   pFile->eFileLock, getpid()));
   26121   assert( eFileLock<=SHARED_LOCK );
   26122 
   26123   /* no-op if possible */
   26124   if( pFile->eFileLock==eFileLock ){
   26125     return SQLITE_OK;
   26126   }
   26127 
   26128   /* To downgrade to shared, simply update our internal notion of the
   26129   ** lock state.  No need to mess with the file on disk.
   26130   */
   26131   if( eFileLock==SHARED_LOCK ){
   26132     pFile->eFileLock = SHARED_LOCK;
   26133     return SQLITE_OK;
   26134   }
   26135 
   26136   /* To fully unlock the database, delete the lock file */
   26137   assert( eFileLock==NO_LOCK );
   26138   if( osUnlink(zLockFile) ){
   26139     int rc = 0;
   26140     int tErrno = errno;
   26141     if( ENOENT != tErrno ){
   26142       rc = SQLITE_IOERR_UNLOCK;
   26143     }
   26144     if( IS_LOCK_ERROR(rc) ){
   26145       pFile->lastErrno = tErrno;
   26146     }
   26147     return rc;
   26148   }
   26149   pFile->eFileLock = NO_LOCK;
   26150   return SQLITE_OK;
   26151 }
   26152 
   26153 /*
   26154 ** Close a file.  Make sure the lock has been released before closing.
   26155 */
   26156 static int dotlockClose(sqlite3_file *id) {
   26157   int rc;
   26158   if( id ){
   26159     unixFile *pFile = (unixFile*)id;
   26160     dotlockUnlock(id, NO_LOCK);
   26161     sqlite3_free(pFile->lockingContext);
   26162   }
   26163   rc = closeUnixFile(id);
   26164   return rc;
   26165 }
   26166 /****************** End of the dot-file lock implementation *******************
   26167 ******************************************************************************/
   26168 
   26169 /******************************************************************************
   26170 ************************** Begin flock Locking ********************************
   26171 **
   26172 ** Use the flock() system call to do file locking.
   26173 **
   26174 ** flock() locking is like dot-file locking in that the various
   26175 ** fine-grain locking levels supported by SQLite are collapsed into
   26176 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
   26177 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
   26178 ** still works when you do this, but concurrency is reduced since
   26179 ** only a single process can be reading the database at a time.
   26180 **
   26181 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
   26182 ** compiling for VXWORKS.
   26183 */
   26184 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
   26185 
   26186 /*
   26187 ** Retry flock() calls that fail with EINTR
   26188 */
   26189 #ifdef EINTR
   26190 static int robust_flock(int fd, int op){
   26191   int rc;
   26192   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
   26193   return rc;
   26194 }
   26195 #else
   26196 # define robust_flock(a,b) flock(a,b)
   26197 #endif
   26198 
   26199 
   26200 /*
   26201 ** This routine checks if there is a RESERVED lock held on the specified
   26202 ** file by this or any other process. If such a lock is held, set *pResOut
   26203 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26204 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26205 */
   26206 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
   26207   int rc = SQLITE_OK;
   26208   int reserved = 0;
   26209   unixFile *pFile = (unixFile*)id;
   26210 
   26211   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26212 
   26213   assert( pFile );
   26214 
   26215   /* Check if a thread in this process holds such a lock */
   26216   if( pFile->eFileLock>SHARED_LOCK ){
   26217     reserved = 1;
   26218   }
   26219 
   26220   /* Otherwise see if some other process holds it. */
   26221   if( !reserved ){
   26222     /* attempt to get the lock */
   26223     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
   26224     if( !lrc ){
   26225       /* got the lock, unlock it */
   26226       lrc = robust_flock(pFile->h, LOCK_UN);
   26227       if ( lrc ) {
   26228         int tErrno = errno;
   26229         /* unlock failed with an error */
   26230         lrc = SQLITE_IOERR_UNLOCK;
   26231         if( IS_LOCK_ERROR(lrc) ){
   26232           pFile->lastErrno = tErrno;
   26233           rc = lrc;
   26234         }
   26235       }
   26236     } else {
   26237       int tErrno = errno;
   26238       reserved = 1;
   26239       /* someone else might have it reserved */
   26240       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26241       if( IS_LOCK_ERROR(lrc) ){
   26242         pFile->lastErrno = tErrno;
   26243         rc = lrc;
   26244       }
   26245     }
   26246   }
   26247   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
   26248 
   26249 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   26250   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
   26251     rc = SQLITE_OK;
   26252     reserved=1;
   26253   }
   26254 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   26255   *pResOut = reserved;
   26256   return rc;
   26257 }
   26258 
   26259 /*
   26260 ** Lock the file with the lock specified by parameter eFileLock - one
   26261 ** of the following:
   26262 **
   26263 **     (1) SHARED_LOCK
   26264 **     (2) RESERVED_LOCK
   26265 **     (3) PENDING_LOCK
   26266 **     (4) EXCLUSIVE_LOCK
   26267 **
   26268 ** Sometimes when requesting one lock state, additional lock states
   26269 ** are inserted in between.  The locking might fail on one of the later
   26270 ** transitions leaving the lock state different from what it started but
   26271 ** still short of its goal.  The following chart shows the allowed
   26272 ** transitions and the inserted intermediate states:
   26273 **
   26274 **    UNLOCKED -> SHARED
   26275 **    SHARED -> RESERVED
   26276 **    SHARED -> (PENDING) -> EXCLUSIVE
   26277 **    RESERVED -> (PENDING) -> EXCLUSIVE
   26278 **    PENDING -> EXCLUSIVE
   26279 **
   26280 ** flock() only really support EXCLUSIVE locks.  We track intermediate
   26281 ** lock states in the sqlite3_file structure, but all locks SHARED or
   26282 ** above are really EXCLUSIVE locks and exclude all other processes from
   26283 ** access the file.
   26284 **
   26285 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26286 ** routine to lower a locking level.
   26287 */
   26288 static int flockLock(sqlite3_file *id, int eFileLock) {
   26289   int rc = SQLITE_OK;
   26290   unixFile *pFile = (unixFile*)id;
   26291 
   26292   assert( pFile );
   26293 
   26294   /* if we already have a lock, it is exclusive.
   26295   ** Just adjust level and punt on outta here. */
   26296   if (pFile->eFileLock > NO_LOCK) {
   26297     pFile->eFileLock = eFileLock;
   26298     return SQLITE_OK;
   26299   }
   26300 
   26301   /* grab an exclusive lock */
   26302 
   26303   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
   26304     int tErrno = errno;
   26305     /* didn't get, must be busy */
   26306     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26307     if( IS_LOCK_ERROR(rc) ){
   26308       pFile->lastErrno = tErrno;
   26309     }
   26310   } else {
   26311     /* got it, set the type and return ok */
   26312     pFile->eFileLock = eFileLock;
   26313   }
   26314   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
   26315            rc==SQLITE_OK ? "ok" : "failed"));
   26316 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   26317   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
   26318     rc = SQLITE_BUSY;
   26319   }
   26320 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   26321   return rc;
   26322 }
   26323 
   26324 
   26325 /*
   26326 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26327 ** must be either NO_LOCK or SHARED_LOCK.
   26328 **
   26329 ** If the locking level of the file descriptor is already at or below
   26330 ** the requested locking level, this routine is a no-op.
   26331 */
   26332 static int flockUnlock(sqlite3_file *id, int eFileLock) {
   26333   unixFile *pFile = (unixFile*)id;
   26334 
   26335   assert( pFile );
   26336   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
   26337            pFile->eFileLock, getpid()));
   26338   assert( eFileLock<=SHARED_LOCK );
   26339 
   26340   /* no-op if possible */
   26341   if( pFile->eFileLock==eFileLock ){
   26342     return SQLITE_OK;
   26343   }
   26344 
   26345   /* shared can just be set because we always have an exclusive */
   26346   if (eFileLock==SHARED_LOCK) {
   26347     pFile->eFileLock = eFileLock;
   26348     return SQLITE_OK;
   26349   }
   26350 
   26351   /* no, really, unlock. */
   26352   if( robust_flock(pFile->h, LOCK_UN) ){
   26353 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   26354     return SQLITE_OK;
   26355 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   26356     return SQLITE_IOERR_UNLOCK;
   26357   }else{
   26358     pFile->eFileLock = NO_LOCK;
   26359     return SQLITE_OK;
   26360   }
   26361 }
   26362 
   26363 /*
   26364 ** Close a file.
   26365 */
   26366 static int flockClose(sqlite3_file *id) {
   26367   if( id ){
   26368     flockUnlock(id, NO_LOCK);
   26369   }
   26370   return closeUnixFile(id);
   26371 }
   26372 
   26373 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
   26374 
   26375 /******************* End of the flock lock implementation *********************
   26376 ******************************************************************************/
   26377 
   26378 /******************************************************************************
   26379 ************************ Begin Named Semaphore Locking ************************
   26380 **
   26381 ** Named semaphore locking is only supported on VxWorks.
   26382 **
   26383 ** Semaphore locking is like dot-lock and flock in that it really only
   26384 ** supports EXCLUSIVE locking.  Only a single process can read or write
   26385 ** the database file at a time.  This reduces potential concurrency, but
   26386 ** makes the lock implementation much easier.
   26387 */
   26388 #if OS_VXWORKS
   26389 
   26390 /*
   26391 ** This routine checks if there is a RESERVED lock held on the specified
   26392 ** file by this or any other process. If such a lock is held, set *pResOut
   26393 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26394 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26395 */
   26396 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
   26397   int rc = SQLITE_OK;
   26398   int reserved = 0;
   26399   unixFile *pFile = (unixFile*)id;
   26400 
   26401   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26402 
   26403   assert( pFile );
   26404 
   26405   /* Check if a thread in this process holds such a lock */
   26406   if( pFile->eFileLock>SHARED_LOCK ){
   26407     reserved = 1;
   26408   }
   26409 
   26410   /* Otherwise see if some other process holds it. */
   26411   if( !reserved ){
   26412     sem_t *pSem = pFile->pInode->pSem;
   26413     struct stat statBuf;
   26414 
   26415     if( sem_trywait(pSem)==-1 ){
   26416       int tErrno = errno;
   26417       if( EAGAIN != tErrno ){
   26418         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
   26419         pFile->lastErrno = tErrno;
   26420       } else {
   26421         /* someone else has the lock when we are in NO_LOCK */
   26422         reserved = (pFile->eFileLock < SHARED_LOCK);
   26423       }
   26424     }else{
   26425       /* we could have it if we want it */
   26426       sem_post(pSem);
   26427     }
   26428   }
   26429   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
   26430 
   26431   *pResOut = reserved;
   26432   return rc;
   26433 }
   26434 
   26435 /*
   26436 ** Lock the file with the lock specified by parameter eFileLock - one
   26437 ** of the following:
   26438 **
   26439 **     (1) SHARED_LOCK
   26440 **     (2) RESERVED_LOCK
   26441 **     (3) PENDING_LOCK
   26442 **     (4) EXCLUSIVE_LOCK
   26443 **
   26444 ** Sometimes when requesting one lock state, additional lock states
   26445 ** are inserted in between.  The locking might fail on one of the later
   26446 ** transitions leaving the lock state different from what it started but
   26447 ** still short of its goal.  The following chart shows the allowed
   26448 ** transitions and the inserted intermediate states:
   26449 **
   26450 **    UNLOCKED -> SHARED
   26451 **    SHARED -> RESERVED
   26452 **    SHARED -> (PENDING) -> EXCLUSIVE
   26453 **    RESERVED -> (PENDING) -> EXCLUSIVE
   26454 **    PENDING -> EXCLUSIVE
   26455 **
   26456 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
   26457 ** lock states in the sqlite3_file structure, but all locks SHARED or
   26458 ** above are really EXCLUSIVE locks and exclude all other processes from
   26459 ** access the file.
   26460 **
   26461 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26462 ** routine to lower a locking level.
   26463 */
   26464 static int semLock(sqlite3_file *id, int eFileLock) {
   26465   unixFile *pFile = (unixFile*)id;
   26466   int fd;
   26467   sem_t *pSem = pFile->pInode->pSem;
   26468   int rc = SQLITE_OK;
   26469 
   26470   /* if we already have a lock, it is exclusive.
   26471   ** Just adjust level and punt on outta here. */
   26472   if (pFile->eFileLock > NO_LOCK) {
   26473     pFile->eFileLock = eFileLock;
   26474     rc = SQLITE_OK;
   26475     goto sem_end_lock;
   26476   }
   26477 
   26478   /* lock semaphore now but bail out when already locked. */
   26479   if( sem_trywait(pSem)==-1 ){
   26480     rc = SQLITE_BUSY;
   26481     goto sem_end_lock;
   26482   }
   26483 
   26484   /* got it, set the type and return ok */
   26485   pFile->eFileLock = eFileLock;
   26486 
   26487  sem_end_lock:
   26488   return rc;
   26489 }
   26490 
   26491 /*
   26492 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26493 ** must be either NO_LOCK or SHARED_LOCK.
   26494 **
   26495 ** If the locking level of the file descriptor is already at or below
   26496 ** the requested locking level, this routine is a no-op.
   26497 */
   26498 static int semUnlock(sqlite3_file *id, int eFileLock) {
   26499   unixFile *pFile = (unixFile*)id;
   26500   sem_t *pSem = pFile->pInode->pSem;
   26501 
   26502   assert( pFile );
   26503   assert( pSem );
   26504   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
   26505 	   pFile->eFileLock, getpid()));
   26506   assert( eFileLock<=SHARED_LOCK );
   26507 
   26508   /* no-op if possible */
   26509   if( pFile->eFileLock==eFileLock ){
   26510     return SQLITE_OK;
   26511   }
   26512 
   26513   /* shared can just be set because we always have an exclusive */
   26514   if (eFileLock==SHARED_LOCK) {
   26515     pFile->eFileLock = eFileLock;
   26516     return SQLITE_OK;
   26517   }
   26518 
   26519   /* no, really unlock. */
   26520   if ( sem_post(pSem)==-1 ) {
   26521     int rc, tErrno = errno;
   26522     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
   26523     if( IS_LOCK_ERROR(rc) ){
   26524       pFile->lastErrno = tErrno;
   26525     }
   26526     return rc;
   26527   }
   26528   pFile->eFileLock = NO_LOCK;
   26529   return SQLITE_OK;
   26530 }
   26531 
   26532 /*
   26533  ** Close a file.
   26534  */
   26535 static int semClose(sqlite3_file *id) {
   26536   if( id ){
   26537     unixFile *pFile = (unixFile*)id;
   26538     semUnlock(id, NO_LOCK);
   26539     assert( pFile );
   26540     unixEnterMutex();
   26541     releaseInodeInfo(pFile);
   26542     unixLeaveMutex();
   26543     closeUnixFile(id);
   26544   }
   26545   return SQLITE_OK;
   26546 }
   26547 
   26548 #endif /* OS_VXWORKS */
   26549 /*
   26550 ** Named semaphore locking is only available on VxWorks.
   26551 **
   26552 *************** End of the named semaphore lock implementation ****************
   26553 ******************************************************************************/
   26554 
   26555 
   26556 /******************************************************************************
   26557 *************************** Begin AFP Locking *********************************
   26558 **
   26559 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
   26560 ** on Apple Macintosh computers - both OS9 and OSX.
   26561 **
   26562 ** Third-party implementations of AFP are available.  But this code here
   26563 ** only works on OSX.
   26564 */
   26565 
   26566 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   26567 /*
   26568 ** The afpLockingContext structure contains all afp lock specific state
   26569 */
   26570 typedef struct afpLockingContext afpLockingContext;
   26571 struct afpLockingContext {
   26572   int reserved;
   26573   const char *dbPath;             /* Name of the open file */
   26574 };
   26575 
   26576 struct ByteRangeLockPB2
   26577 {
   26578   unsigned long long offset;        /* offset to first byte to lock */
   26579   unsigned long long length;        /* nbr of bytes to lock */
   26580   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
   26581   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
   26582   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
   26583   int fd;                           /* file desc to assoc this lock with */
   26584 };
   26585 
   26586 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
   26587 
   26588 /*
   26589 ** This is a utility for setting or clearing a bit-range lock on an
   26590 ** AFP filesystem.
   26591 **
   26592 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
   26593 */
   26594 static int afpSetLock(
   26595   const char *path,              /* Name of the file to be locked or unlocked */
   26596   unixFile *pFile,               /* Open file descriptor on path */
   26597   unsigned long long offset,     /* First byte to be locked */
   26598   unsigned long long length,     /* Number of bytes to lock */
   26599   int setLockFlag                /* True to set lock.  False to clear lock */
   26600 ){
   26601   struct ByteRangeLockPB2 pb;
   26602   int err;
   26603 
   26604   pb.unLockFlag = setLockFlag ? 0 : 1;
   26605   pb.startEndFlag = 0;
   26606   pb.offset = offset;
   26607   pb.length = length;
   26608   pb.fd = pFile->h;
   26609 
   26610   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
   26611     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
   26612     offset, length));
   26613   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
   26614   if ( err==-1 ) {
   26615     int rc;
   26616     int tErrno = errno;
   26617     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
   26618              path, tErrno, strerror(tErrno)));
   26619 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
   26620     rc = SQLITE_BUSY;
   26621 #else
   26622     rc = sqliteErrorFromPosixError(tErrno,
   26623                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
   26624 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
   26625     if( IS_LOCK_ERROR(rc) ){
   26626       pFile->lastErrno = tErrno;
   26627     }
   26628     return rc;
   26629   } else {
   26630     return SQLITE_OK;
   26631   }
   26632 }
   26633 
   26634 /*
   26635 ** This routine checks if there is a RESERVED lock held on the specified
   26636 ** file by this or any other process. If such a lock is held, set *pResOut
   26637 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26638 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26639 */
   26640 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
   26641   int rc = SQLITE_OK;
   26642   int reserved = 0;
   26643   unixFile *pFile = (unixFile*)id;
   26644 
   26645   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26646 
   26647   assert( pFile );
   26648   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
   26649   if( context->reserved ){
   26650     *pResOut = 1;
   26651     return SQLITE_OK;
   26652   }
   26653   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
   26654 
   26655   /* Check if a thread in this process holds such a lock */
   26656   if( pFile->pInode->eFileLock>SHARED_LOCK ){
   26657     reserved = 1;
   26658   }
   26659 
   26660   /* Otherwise see if some other process holds it.
   26661    */
   26662   if( !reserved ){
   26663     /* lock the RESERVED byte */
   26664     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
   26665     if( SQLITE_OK==lrc ){
   26666       /* if we succeeded in taking the reserved lock, unlock it to restore
   26667       ** the original state */
   26668       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
   26669     } else {
   26670       /* if we failed to get the lock then someone else must have it */
   26671       reserved = 1;
   26672     }
   26673     if( IS_LOCK_ERROR(lrc) ){
   26674       rc=lrc;
   26675     }
   26676   }
   26677 
   26678   unixLeaveMutex();
   26679   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
   26680 
   26681   *pResOut = reserved;
   26682   return rc;
   26683 }
   26684 
   26685 /*
   26686 ** Lock the file with the lock specified by parameter eFileLock - one
   26687 ** of the following:
   26688 **
   26689 **     (1) SHARED_LOCK
   26690 **     (2) RESERVED_LOCK
   26691 **     (3) PENDING_LOCK
   26692 **     (4) EXCLUSIVE_LOCK
   26693 **
   26694 ** Sometimes when requesting one lock state, additional lock states
   26695 ** are inserted in between.  The locking might fail on one of the later
   26696 ** transitions leaving the lock state different from what it started but
   26697 ** still short of its goal.  The following chart shows the allowed
   26698 ** transitions and the inserted intermediate states:
   26699 **
   26700 **    UNLOCKED -> SHARED
   26701 **    SHARED -> RESERVED
   26702 **    SHARED -> (PENDING) -> EXCLUSIVE
   26703 **    RESERVED -> (PENDING) -> EXCLUSIVE
   26704 **    PENDING -> EXCLUSIVE
   26705 **
   26706 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26707 ** routine to lower a locking level.
   26708 */
   26709 static int afpLock(sqlite3_file *id, int eFileLock){
   26710   int rc = SQLITE_OK;
   26711   unixFile *pFile = (unixFile*)id;
   26712   unixInodeInfo *pInode = pFile->pInode;
   26713   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
   26714 
   26715   assert( pFile );
   26716   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
   26717            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
   26718            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
   26719 
   26720   /* If there is already a lock of this type or more restrictive on the
   26721   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
   26722   ** unixEnterMutex() hasn't been called yet.
   26723   */
   26724   if( pFile->eFileLock>=eFileLock ){
   26725     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
   26726            azFileLock(eFileLock)));
   26727     return SQLITE_OK;
   26728   }
   26729 
   26730   /* Make sure the locking sequence is correct
   26731   **  (1) We never move from unlocked to anything higher than shared lock.
   26732   **  (2) SQLite never explicitly requests a pendig lock.
   26733   **  (3) A shared lock is always held when a reserve lock is requested.
   26734   */
   26735   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
   26736   assert( eFileLock!=PENDING_LOCK );
   26737   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
   26738 
   26739   /* This mutex is needed because pFile->pInode is shared across threads
   26740   */
   26741   unixEnterMutex();
   26742   pInode = pFile->pInode;
   26743 
   26744   /* If some thread using this PID has a lock via a different unixFile*
   26745   ** handle that precludes the requested lock, return BUSY.
   26746   */
   26747   if( (pFile->eFileLock!=pInode->eFileLock &&
   26748        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
   26749      ){
   26750     rc = SQLITE_BUSY;
   26751     goto afp_end_lock;
   26752   }
   26753 
   26754   /* If a SHARED lock is requested, and some thread using this PID already
   26755   ** has a SHARED or RESERVED lock, then increment reference counts and
   26756   ** return SQLITE_OK.
   26757   */
   26758   if( eFileLock==SHARED_LOCK &&
   26759      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
   26760     assert( eFileLock==SHARED_LOCK );
   26761     assert( pFile->eFileLock==0 );
   26762     assert( pInode->nShared>0 );
   26763     pFile->eFileLock = SHARED_LOCK;
   26764     pInode->nShared++;
   26765     pInode->nLock++;
   26766     goto afp_end_lock;
   26767   }
   26768 
   26769   /* A PENDING lock is needed before acquiring a SHARED lock and before
   26770   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
   26771   ** be released.
   26772   */
   26773   if( eFileLock==SHARED_LOCK
   26774       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
   26775   ){
   26776     int failed;
   26777     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
   26778     if (failed) {
   26779       rc = failed;
   26780       goto afp_end_lock;
   26781     }
   26782   }
   26783 
   26784   /* If control gets to this point, then actually go ahead and make
   26785   ** operating system calls for the specified lock.
   26786   */
   26787   if( eFileLock==SHARED_LOCK ){
   26788     int lrc1, lrc2, lrc1Errno;
   26789     long lk, mask;
   26790 
   26791     assert( pInode->nShared==0 );
   26792     assert( pInode->eFileLock==0 );
   26793 
   26794     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
   26795     /* Now get the read-lock SHARED_LOCK */
   26796     /* note that the quality of the randomness doesn't matter that much */
   26797     lk = random();
   26798     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
   26799     lrc1 = afpSetLock(context->dbPath, pFile,
   26800           SHARED_FIRST+pInode->sharedByte, 1, 1);
   26801     if( IS_LOCK_ERROR(lrc1) ){
   26802       lrc1Errno = pFile->lastErrno;
   26803     }
   26804     /* Drop the temporary PENDING lock */
   26805     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
   26806 
   26807     if( IS_LOCK_ERROR(lrc1) ) {
   26808       pFile->lastErrno = lrc1Errno;
   26809       rc = lrc1;
   26810       goto afp_end_lock;
   26811     } else if( IS_LOCK_ERROR(lrc2) ){
   26812       rc = lrc2;
   26813       goto afp_end_lock;
   26814     } else if( lrc1 != SQLITE_OK ) {
   26815       rc = lrc1;
   26816     } else {
   26817       pFile->eFileLock = SHARED_LOCK;
   26818       pInode->nLock++;
   26819       pInode->nShared = 1;
   26820     }
   26821   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
   26822     /* We are trying for an exclusive lock but another thread in this
   26823      ** same process is still holding a shared lock. */
   26824     rc = SQLITE_BUSY;
   26825   }else{
   26826     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
   26827     ** assumed that there is a SHARED or greater lock on the file
   26828     ** already.
   26829     */
   26830     int failed = 0;
   26831     assert( 0!=pFile->eFileLock );
   26832     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
   26833         /* Acquire a RESERVED lock */
   26834         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
   26835       if( !failed ){
   26836         context->reserved = 1;
   26837       }
   26838     }
   26839     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
   26840       /* Acquire an EXCLUSIVE lock */
   26841 
   26842       /* Remove the shared lock before trying the range.  we'll need to
   26843       ** reestablish the shared lock if we can't get the  afpUnlock
   26844       */
   26845       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
   26846                          pInode->sharedByte, 1, 0)) ){
   26847         int failed2 = SQLITE_OK;
   26848         /* now attemmpt to get the exclusive lock range */
   26849         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
   26850                                SHARED_SIZE, 1);
   26851         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
   26852                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
   26853           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
   26854           ** a critical I/O error
   26855           */
   26856           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
   26857                SQLITE_IOERR_LOCK;
   26858           goto afp_end_lock;
   26859         }
   26860       }else{
   26861         rc = failed;
   26862       }
   26863     }
   26864     if( failed ){
   26865       rc = failed;
   26866     }
   26867   }
   26868 
   26869   if( rc==SQLITE_OK ){
   26870     pFile->eFileLock = eFileLock;
   26871     pInode->eFileLock = eFileLock;
   26872   }else if( eFileLock==EXCLUSIVE_LOCK ){
   26873     pFile->eFileLock = PENDING_LOCK;
   26874     pInode->eFileLock = PENDING_LOCK;
   26875   }
   26876 
   26877 afp_end_lock:
   26878   unixLeaveMutex();
   26879   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
   26880          rc==SQLITE_OK ? "ok" : "failed"));
   26881   return rc;
   26882 }
   26883 
   26884 /*
   26885 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26886 ** must be either NO_LOCK or SHARED_LOCK.
   26887 **
   26888 ** If the locking level of the file descriptor is already at or below
   26889 ** the requested locking level, this routine is a no-op.
   26890 */
   26891 static int afpUnlock(sqlite3_file *id, int eFileLock) {
   26892   int rc = SQLITE_OK;
   26893   unixFile *pFile = (unixFile*)id;
   26894   unixInodeInfo *pInode;
   26895   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
   26896   int skipShared = 0;
   26897 #ifdef SQLITE_TEST
   26898   int h = pFile->h;
   26899 #endif
   26900 
   26901   assert( pFile );
   26902   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
   26903            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
   26904            getpid()));
   26905 
   26906   assert( eFileLock<=SHARED_LOCK );
   26907   if( pFile->eFileLock<=eFileLock ){
   26908     return SQLITE_OK;
   26909   }
   26910   unixEnterMutex();
   26911   pInode = pFile->pInode;
   26912   assert( pInode->nShared!=0 );
   26913   if( pFile->eFileLock>SHARED_LOCK ){
   26914     assert( pInode->eFileLock==pFile->eFileLock );
   26915     SimulateIOErrorBenign(1);
   26916     SimulateIOError( h=(-1) )
   26917     SimulateIOErrorBenign(0);
   26918 
   26919 #ifndef NDEBUG
   26920     /* When reducing a lock such that other processes can start
   26921     ** reading the database file again, make sure that the
   26922     ** transaction counter was updated if any part of the database
   26923     ** file changed.  If the transaction counter is not updated,
   26924     ** other connections to the same file might not realize that
   26925     ** the file has changed and hence might not know to flush their
   26926     ** cache.  The use of a stale cache can lead to database corruption.
   26927     */
   26928     assert( pFile->inNormalWrite==0
   26929            || pFile->dbUpdate==0
   26930            || pFile->transCntrChng==1 );
   26931     pFile->inNormalWrite = 0;
   26932 #endif
   26933 
   26934     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
   26935       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
   26936       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
   26937         /* only re-establish the shared lock if necessary */
   26938         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
   26939         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
   26940       } else {
   26941         skipShared = 1;
   26942       }
   26943     }
   26944     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
   26945       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
   26946     }
   26947     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
   26948       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
   26949       if( !rc ){
   26950         context->reserved = 0;
   26951       }
   26952     }
   26953     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
   26954       pInode->eFileLock = SHARED_LOCK;
   26955     }
   26956   }
   26957   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
   26958 
   26959     /* Decrement the shared lock counter.  Release the lock using an
   26960     ** OS call only when all threads in this same process have released
   26961     ** the lock.
   26962     */
   26963     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
   26964     pInode->nShared--;
   26965     if( pInode->nShared==0 ){
   26966       SimulateIOErrorBenign(1);
   26967       SimulateIOError( h=(-1) )
   26968       SimulateIOErrorBenign(0);
   26969       if( !skipShared ){
   26970         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
   26971       }
   26972       if( !rc ){
   26973         pInode->eFileLock = NO_LOCK;
   26974         pFile->eFileLock = NO_LOCK;
   26975       }
   26976     }
   26977     if( rc==SQLITE_OK ){
   26978       pInode->nLock--;
   26979       assert( pInode->nLock>=0 );
   26980       if( pInode->nLock==0 ){
   26981         closePendingFds(pFile);
   26982       }
   26983     }
   26984   }
   26985 
   26986   unixLeaveMutex();
   26987   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
   26988   return rc;
   26989 }
   26990 
   26991 /*
   26992 ** Close a file & cleanup AFP specific locking context
   26993 */
   26994 static int afpClose(sqlite3_file *id) {
   26995   int rc = SQLITE_OK;
   26996   if( id ){
   26997     unixFile *pFile = (unixFile*)id;
   26998     afpUnlock(id, NO_LOCK);
   26999     unixEnterMutex();
   27000     if( pFile->pInode && pFile->pInode->nLock ){
   27001       /* If there are outstanding locks, do not actually close the file just
   27002       ** yet because that would clear those locks.  Instead, add the file
   27003       ** descriptor to pInode->aPending.  It will be automatically closed when
   27004       ** the last lock is cleared.
   27005       */
   27006       setPendingFd(pFile);
   27007     }
   27008     releaseInodeInfo(pFile);
   27009     sqlite3_free(pFile->lockingContext);
   27010     rc = closeUnixFile(id);
   27011     unixLeaveMutex();
   27012   }
   27013   return rc;
   27014 }
   27015 
   27016 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   27017 /*
   27018 ** The code above is the AFP lock implementation.  The code is specific
   27019 ** to MacOSX and does not work on other unix platforms.  No alternative
   27020 ** is available.  If you don't compile for a mac, then the "unix-afp"
   27021 ** VFS is not available.
   27022 **
   27023 ********************* End of the AFP lock implementation **********************
   27024 ******************************************************************************/
   27025 
   27026 /******************************************************************************
   27027 *************************** Begin NFS Locking ********************************/
   27028 
   27029 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   27030 /*
   27031  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   27032  ** must be either NO_LOCK or SHARED_LOCK.
   27033  **
   27034  ** If the locking level of the file descriptor is already at or below
   27035  ** the requested locking level, this routine is a no-op.
   27036  */
   27037 static int nfsUnlock(sqlite3_file *id, int eFileLock){
   27038   return posixUnlock(id, eFileLock, 1);
   27039 }
   27040 
   27041 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   27042 /*
   27043 ** The code above is the NFS lock implementation.  The code is specific
   27044 ** to MacOSX and does not work on other unix platforms.  No alternative
   27045 ** is available.
   27046 **
   27047 ********************* End of the NFS lock implementation **********************
   27048 ******************************************************************************/
   27049 
   27050 /******************************************************************************
   27051 **************** Non-locking sqlite3_file methods *****************************
   27052 **
   27053 ** The next division contains implementations for all methods of the
   27054 ** sqlite3_file object other than the locking methods.  The locking
   27055 ** methods were defined in divisions above (one locking method per
   27056 ** division).  Those methods that are common to all locking modes
   27057 ** are gather together into this division.
   27058 */
   27059 
   27060 /*
   27061 ** Seek to the offset passed as the second argument, then read cnt
   27062 ** bytes into pBuf. Return the number of bytes actually read.
   27063 **
   27064 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
   27065 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
   27066 ** one system to another.  Since SQLite does not define USE_PREAD
   27067 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
   27068 ** See tickets #2741 and #2681.
   27069 **
   27070 ** To avoid stomping the errno value on a failed read the lastErrno value
   27071 ** is set before returning.
   27072 */
   27073 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
   27074   int got;
   27075 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
   27076   i64 newOffset;
   27077 #endif
   27078   TIMER_START;
   27079 #if defined(USE_PREAD)
   27080   do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
   27081   SimulateIOError( got = -1 );
   27082 #elif defined(USE_PREAD64)
   27083   do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
   27084   SimulateIOError( got = -1 );
   27085 #else
   27086   newOffset = lseek(id->h, offset, SEEK_SET);
   27087   SimulateIOError( newOffset-- );
   27088   if( newOffset!=offset ){
   27089     if( newOffset == -1 ){
   27090       ((unixFile*)id)->lastErrno = errno;
   27091     }else{
   27092       ((unixFile*)id)->lastErrno = 0;
   27093     }
   27094     return -1;
   27095   }
   27096   do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
   27097 #endif
   27098   TIMER_END;
   27099   if( got<0 ){
   27100     ((unixFile*)id)->lastErrno = errno;
   27101   }
   27102   OSTRACE(("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
   27103   return got;
   27104 }
   27105 
   27106 /*
   27107 ** Read data from a file into a buffer.  Return SQLITE_OK if all
   27108 ** bytes were read successfully and SQLITE_IOERR if anything goes
   27109 ** wrong.
   27110 */
   27111 static int unixRead(
   27112   sqlite3_file *id,
   27113   void *pBuf,
   27114   int amt,
   27115   sqlite3_int64 offset
   27116 ){
   27117   unixFile *pFile = (unixFile *)id;
   27118   int got;
   27119   assert( id );
   27120 
   27121   /* If this is a database file (not a journal, master-journal or temp
   27122   ** file), the bytes in the locking range should never be read or written. */
   27123 #if 0
   27124   assert( pFile->pUnused==0
   27125        || offset>=PENDING_BYTE+512
   27126        || offset+amt<=PENDING_BYTE
   27127   );
   27128 #endif
   27129 
   27130   got = seekAndRead(pFile, offset, pBuf, amt);
   27131   if( got==amt ){
   27132     return SQLITE_OK;
   27133   }else if( got<0 ){
   27134     /* lastErrno set by seekAndRead */
   27135     return SQLITE_IOERR_READ;
   27136   }else{
   27137     pFile->lastErrno = 0; /* not a system error */
   27138     /* Unread parts of the buffer must be zero-filled */
   27139     memset(&((char*)pBuf)[got], 0, amt-got);
   27140     return SQLITE_IOERR_SHORT_READ;
   27141   }
   27142 }
   27143 
   27144 /*
   27145 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
   27146 ** Return the number of bytes actually read.  Update the offset.
   27147 **
   27148 ** To avoid stomping the errno value on a failed write the lastErrno value
   27149 ** is set before returning.
   27150 */
   27151 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
   27152   int got;
   27153 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
   27154   i64 newOffset;
   27155 #endif
   27156   TIMER_START;
   27157 #if defined(USE_PREAD)
   27158   do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
   27159 #elif defined(USE_PREAD64)
   27160   do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
   27161 #else
   27162   newOffset = lseek(id->h, offset, SEEK_SET);
   27163   SimulateIOError( newOffset-- );
   27164   if( newOffset!=offset ){
   27165     if( newOffset == -1 ){
   27166       ((unixFile*)id)->lastErrno = errno;
   27167     }else{
   27168       ((unixFile*)id)->lastErrno = 0;
   27169     }
   27170     return -1;
   27171   }
   27172   do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
   27173 #endif
   27174   TIMER_END;
   27175   if( got<0 ){
   27176     ((unixFile*)id)->lastErrno = errno;
   27177   }
   27178 
   27179   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
   27180   return got;
   27181 }
   27182 
   27183 
   27184 /*
   27185 ** Write data from a buffer into a file.  Return SQLITE_OK on success
   27186 ** or some other error code on failure.
   27187 */
   27188 static int unixWrite(
   27189   sqlite3_file *id,
   27190   const void *pBuf,
   27191   int amt,
   27192   sqlite3_int64 offset
   27193 ){
   27194   unixFile *pFile = (unixFile*)id;
   27195   int wrote = 0;
   27196   assert( id );
   27197   assert( amt>0 );
   27198 
   27199   /* If this is a database file (not a journal, master-journal or temp
   27200   ** file), the bytes in the locking range should never be read or written. */
   27201 #if 0
   27202   assert( pFile->pUnused==0
   27203        || offset>=PENDING_BYTE+512
   27204        || offset+amt<=PENDING_BYTE
   27205   );
   27206 #endif
   27207 
   27208 #ifndef NDEBUG
   27209   /* If we are doing a normal write to a database file (as opposed to
   27210   ** doing a hot-journal rollback or a write to some file other than a
   27211   ** normal database file) then record the fact that the database
   27212   ** has changed.  If the transaction counter is modified, record that
   27213   ** fact too.
   27214   */
   27215   if( pFile->inNormalWrite ){
   27216     pFile->dbUpdate = 1;  /* The database has been modified */
   27217     if( offset<=24 && offset+amt>=27 ){
   27218       int rc;
   27219       char oldCntr[4];
   27220       SimulateIOErrorBenign(1);
   27221       rc = seekAndRead(pFile, 24, oldCntr, 4);
   27222       SimulateIOErrorBenign(0);
   27223       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
   27224         pFile->transCntrChng = 1;  /* The transaction counter has changed */
   27225       }
   27226     }
   27227   }
   27228 #endif
   27229 
   27230   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
   27231     amt -= wrote;
   27232     offset += wrote;
   27233     pBuf = &((char*)pBuf)[wrote];
   27234   }
   27235   SimulateIOError(( wrote=(-1), amt=1 ));
   27236   SimulateDiskfullError(( wrote=0, amt=1 ));
   27237 
   27238   if( amt>0 ){
   27239     if( wrote<0 ){
   27240       /* lastErrno set by seekAndWrite */
   27241       return SQLITE_IOERR_WRITE;
   27242     }else{
   27243       pFile->lastErrno = 0; /* not a system error */
   27244       return SQLITE_FULL;
   27245     }
   27246   }
   27247 
   27248   return SQLITE_OK;
   27249 }
   27250 
   27251 #ifdef SQLITE_TEST
   27252 /*
   27253 ** Count the number of fullsyncs and normal syncs.  This is used to test
   27254 ** that syncs and fullsyncs are occurring at the right times.
   27255 */
   27256 SQLITE_API int sqlite3_sync_count = 0;
   27257 SQLITE_API int sqlite3_fullsync_count = 0;
   27258 #endif
   27259 
   27260 /*
   27261 ** We do not trust systems to provide a working fdatasync().  Some do.
   27262 ** Others do no.  To be safe, we will stick with the (slower) fsync().
   27263 ** If you know that your system does support fdatasync() correctly,
   27264 ** then simply compile with -Dfdatasync=fdatasync
   27265 */
   27266 #if !defined(fdatasync) && !defined(__linux__)
   27267 # define fdatasync fsync
   27268 #endif
   27269 
   27270 /*
   27271 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
   27272 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
   27273 ** only available on Mac OS X.  But that could change.
   27274 */
   27275 #ifdef F_FULLFSYNC
   27276 # define HAVE_FULLFSYNC 1
   27277 #else
   27278 # define HAVE_FULLFSYNC 0
   27279 #endif
   27280 
   27281 
   27282 /*
   27283 ** The fsync() system call does not work as advertised on many
   27284 ** unix systems.  The following procedure is an attempt to make
   27285 ** it work better.
   27286 **
   27287 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
   27288 ** for testing when we want to run through the test suite quickly.
   27289 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
   27290 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
   27291 ** or power failure will likely corrupt the database file.
   27292 **
   27293 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
   27294 ** The idea behind dataOnly is that it should only write the file content
   27295 ** to disk, not the inode.  We only set dataOnly if the file size is
   27296 ** unchanged since the file size is part of the inode.  However,
   27297 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
   27298 ** file size has changed.  The only real difference between fdatasync()
   27299 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
   27300 ** inode if the mtime or owner or other inode attributes have changed.
   27301 ** We only care about the file size, not the other file attributes, so
   27302 ** as far as SQLite is concerned, an fdatasync() is always adequate.
   27303 ** So, we always use fdatasync() if it is available, regardless of
   27304 ** the value of the dataOnly flag.
   27305 */
   27306 static int full_fsync(int fd, int fullSync, int dataOnly){
   27307   int rc;
   27308 
   27309   /* The following "ifdef/elif/else/" block has the same structure as
   27310   ** the one below. It is replicated here solely to avoid cluttering
   27311   ** up the real code with the UNUSED_PARAMETER() macros.
   27312   */
   27313 #ifdef SQLITE_NO_SYNC
   27314   UNUSED_PARAMETER(fd);
   27315   UNUSED_PARAMETER(fullSync);
   27316   UNUSED_PARAMETER(dataOnly);
   27317 #elif HAVE_FULLFSYNC
   27318   UNUSED_PARAMETER(dataOnly);
   27319 #else
   27320   UNUSED_PARAMETER(fullSync);
   27321   UNUSED_PARAMETER(dataOnly);
   27322 #endif
   27323 
   27324   /* Record the number of times that we do a normal fsync() and
   27325   ** FULLSYNC.  This is used during testing to verify that this procedure
   27326   ** gets called with the correct arguments.
   27327   */
   27328 #ifdef SQLITE_TEST
   27329   if( fullSync ) sqlite3_fullsync_count++;
   27330   sqlite3_sync_count++;
   27331 #endif
   27332 
   27333   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   27334   ** no-op
   27335   */
   27336 #ifdef SQLITE_NO_SYNC
   27337   rc = SQLITE_OK;
   27338 #elif HAVE_FULLFSYNC
   27339   if( fullSync ){
   27340     rc = osFcntl(fd, F_FULLFSYNC, 0);
   27341   }else{
   27342     rc = 1;
   27343   }
   27344   /* If the FULLFSYNC failed, fall back to attempting an fsync().
   27345   ** It shouldn't be possible for fullfsync to fail on the local
   27346   ** file system (on OSX), so failure indicates that FULLFSYNC
   27347   ** isn't supported for this file system. So, attempt an fsync
   27348   ** and (for now) ignore the overhead of a superfluous fcntl call.
   27349   ** It'd be better to detect fullfsync support once and avoid
   27350   ** the fcntl call every time sync is called.
   27351   */
   27352   if( rc ) rc = fsync(fd);
   27353 
   27354 #elif defined(__APPLE__)
   27355   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
   27356   ** so currently we default to the macro that redefines fdatasync to fsync
   27357   */
   27358   rc = fsync(fd);
   27359 #else
   27360   rc = fdatasync(fd);
   27361 #if OS_VXWORKS
   27362   if( rc==-1 && errno==ENOTSUP ){
   27363     rc = fsync(fd);
   27364   }
   27365 #endif /* OS_VXWORKS */
   27366 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
   27367 
   27368   if( OS_VXWORKS && rc!= -1 ){
   27369     rc = 0;
   27370   }
   27371   return rc;
   27372 }
   27373 
   27374 /*
   27375 ** Open a file descriptor to the directory containing file zFilename.
   27376 ** If successful, *pFd is set to the opened file descriptor and
   27377 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
   27378 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
   27379 ** value.
   27380 **
   27381 ** The directory file descriptor is used for only one thing - to
   27382 ** fsync() a directory to make sure file creation and deletion events
   27383 ** are flushed to disk.  Such fsyncs are not needed on newer
   27384 ** journaling filesystems, but are required on older filesystems.
   27385 **
   27386 ** This routine can be overridden using the xSetSysCall interface.
   27387 ** The ability to override this routine was added in support of the
   27388 ** chromium sandbox.  Opening a directory is a security risk (we are
   27389 ** told) so making it overrideable allows the chromium sandbox to
   27390 ** replace this routine with a harmless no-op.  To make this routine
   27391 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
   27392 ** *pFd set to a negative number.
   27393 **
   27394 ** If SQLITE_OK is returned, the caller is responsible for closing
   27395 ** the file descriptor *pFd using close().
   27396 */
   27397 static int openDirectory(const char *zFilename, int *pFd){
   27398   int ii;
   27399   int fd = -1;
   27400   char zDirname[MAX_PATHNAME+1];
   27401 
   27402   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
   27403   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
   27404   if( ii>0 ){
   27405     zDirname[ii] = '\0';
   27406     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
   27407     if( fd>=0 ){
   27408 #ifdef FD_CLOEXEC
   27409       osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
   27410 #endif
   27411       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
   27412     }
   27413   }
   27414   *pFd = fd;
   27415   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
   27416 }
   27417 
   27418 /*
   27419 ** Make sure all writes to a particular file are committed to disk.
   27420 **
   27421 ** If dataOnly==0 then both the file itself and its metadata (file
   27422 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
   27423 ** file data is synced.
   27424 **
   27425 ** Under Unix, also make sure that the directory entry for the file
   27426 ** has been created by fsync-ing the directory that contains the file.
   27427 ** If we do not do this and we encounter a power failure, the directory
   27428 ** entry for the journal might not exist after we reboot.  The next
   27429 ** SQLite to access the file will not know that the journal exists (because
   27430 ** the directory entry for the journal was never created) and the transaction
   27431 ** will not roll back - possibly leading to database corruption.
   27432 */
   27433 static int unixSync(sqlite3_file *id, int flags){
   27434   int rc;
   27435   unixFile *pFile = (unixFile*)id;
   27436 
   27437   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
   27438   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
   27439 
   27440   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
   27441   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
   27442       || (flags&0x0F)==SQLITE_SYNC_FULL
   27443   );
   27444 
   27445   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
   27446   ** line is to test that doing so does not cause any problems.
   27447   */
   27448   SimulateDiskfullError( return SQLITE_FULL );
   27449 
   27450   assert( pFile );
   27451   OSTRACE(("SYNC    %-3d\n", pFile->h));
   27452   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
   27453   SimulateIOError( rc=1 );
   27454   if( rc ){
   27455     pFile->lastErrno = errno;
   27456     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
   27457   }
   27458 
   27459   /* Also fsync the directory containing the file if the DIRSYNC flag
   27460   ** is set.  This is a one-time occurrance.  Many systems (examples: AIX)
   27461   ** are unable to fsync a directory, so ignore errors on the fsync.
   27462   */
   27463   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
   27464     int dirfd;
   27465     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
   27466             HAVE_FULLFSYNC, isFullsync));
   27467     rc = osOpenDirectory(pFile->zPath, &dirfd);
   27468     if( rc==SQLITE_OK && dirfd>=0 ){
   27469       full_fsync(dirfd, 0, 0);
   27470       robust_close(pFile, dirfd, __LINE__);
   27471     }else if( rc==SQLITE_CANTOPEN ){
   27472       rc = SQLITE_OK;
   27473     }
   27474     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
   27475   }
   27476   return rc;
   27477 }
   27478 
   27479 /*
   27480 ** Truncate an open file to a specified size
   27481 */
   27482 static int unixTruncate(sqlite3_file *id, i64 nByte){
   27483   unixFile *pFile = (unixFile *)id;
   27484   int rc;
   27485   assert( pFile );
   27486   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
   27487 
   27488   /* If the user has configured a chunk-size for this file, truncate the
   27489   ** file so that it consists of an integer number of chunks (i.e. the
   27490   ** actual file size after the operation may be larger than the requested
   27491   ** size).
   27492   */
   27493   if( pFile->szChunk ){
   27494     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   27495   }
   27496 
   27497   rc = robust_ftruncate(pFile->h, (off_t)nByte);
   27498   if( rc ){
   27499     pFile->lastErrno = errno;
   27500     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
   27501   }else{
   27502 #ifndef NDEBUG
   27503     /* If we are doing a normal write to a database file (as opposed to
   27504     ** doing a hot-journal rollback or a write to some file other than a
   27505     ** normal database file) and we truncate the file to zero length,
   27506     ** that effectively updates the change counter.  This might happen
   27507     ** when restoring a database using the backup API from a zero-length
   27508     ** source.
   27509     */
   27510     if( pFile->inNormalWrite && nByte==0 ){
   27511       pFile->transCntrChng = 1;
   27512     }
   27513 #endif
   27514 
   27515     return SQLITE_OK;
   27516   }
   27517 }
   27518 
   27519 /*
   27520 ** Determine the current size of a file in bytes
   27521 */
   27522 static int unixFileSize(sqlite3_file *id, i64 *pSize){
   27523   int rc;
   27524   struct stat buf;
   27525   assert( id );
   27526   rc = osFstat(((unixFile*)id)->h, &buf);
   27527   SimulateIOError( rc=1 );
   27528   if( rc!=0 ){
   27529     ((unixFile*)id)->lastErrno = errno;
   27530     return SQLITE_IOERR_FSTAT;
   27531   }
   27532   *pSize = buf.st_size;
   27533 
   27534   /* When opening a zero-size database, the findInodeInfo() procedure
   27535   ** writes a single byte into that file in order to work around a bug
   27536   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
   27537   ** layers, we need to report this file size as zero even though it is
   27538   ** really 1.   Ticket #3260.
   27539   */
   27540   if( *pSize==1 ) *pSize = 0;
   27541 
   27542 
   27543   return SQLITE_OK;
   27544 }
   27545 
   27546 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   27547 /*
   27548 ** Handler for proxy-locking file-control verbs.  Defined below in the
   27549 ** proxying locking division.
   27550 */
   27551 static int proxyFileControl(sqlite3_file*,int,void*);
   27552 #endif
   27553 
   27554 /*
   27555 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
   27556 ** file-control operation.
   27557 **
   27558 ** If the user has configured a chunk-size for this file, it could be
   27559 ** that the file needs to be extended at this point. Otherwise, the
   27560 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
   27561 */
   27562 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
   27563   if( pFile->szChunk ){
   27564     i64 nSize;                    /* Required file size */
   27565     struct stat buf;              /* Used to hold return values of fstat() */
   27566 
   27567     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
   27568 
   27569     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
   27570     if( nSize>(i64)buf.st_size ){
   27571 
   27572 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
   27573       /* The code below is handling the return value of osFallocate()
   27574       ** correctly. posix_fallocate() is defined to "returns zero on success,
   27575       ** or an error number on  failure". See the manpage for details. */
   27576       int err;
   27577       do{
   27578         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
   27579       }while( err==EINTR );
   27580       if( err ) return SQLITE_IOERR_WRITE;
   27581 #else
   27582       /* If the OS does not have posix_fallocate(), fake it. First use
   27583       ** ftruncate() to set the file size, then write a single byte to
   27584       ** the last byte in each block within the extended region. This
   27585       ** is the same technique used by glibc to implement posix_fallocate()
   27586       ** on systems that do not have a real fallocate() system call.
   27587       */
   27588       int nBlk = buf.st_blksize;  /* File-system block size */
   27589       i64 iWrite;                 /* Next offset to write to */
   27590 
   27591       if( robust_ftruncate(pFile->h, nSize) ){
   27592         pFile->lastErrno = errno;
   27593         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
   27594       }
   27595       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
   27596       while( iWrite<nSize ){
   27597         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
   27598         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
   27599         iWrite += nBlk;
   27600       }
   27601 #endif
   27602     }
   27603   }
   27604 
   27605   return SQLITE_OK;
   27606 }
   27607 
   27608 /*
   27609 ** Information and control of an open file handle.
   27610 */
   27611 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
   27612   switch( op ){
   27613     case SQLITE_FCNTL_LOCKSTATE: {
   27614       *(int*)pArg = ((unixFile*)id)->eFileLock;
   27615       return SQLITE_OK;
   27616     }
   27617     case SQLITE_LAST_ERRNO: {
   27618       *(int*)pArg = ((unixFile*)id)->lastErrno;
   27619       return SQLITE_OK;
   27620     }
   27621     case SQLITE_FCNTL_CHUNK_SIZE: {
   27622       ((unixFile*)id)->szChunk = *(int *)pArg;
   27623       return SQLITE_OK;
   27624     }
   27625     case SQLITE_FCNTL_SIZE_HINT: {
   27626       return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
   27627     }
   27628 #ifndef NDEBUG
   27629     /* The pager calls this method to signal that it has done
   27630     ** a rollback and that the database is therefore unchanged and
   27631     ** it hence it is OK for the transaction change counter to be
   27632     ** unchanged.
   27633     */
   27634     case SQLITE_FCNTL_DB_UNCHANGED: {
   27635       ((unixFile*)id)->dbUpdate = 0;
   27636       return SQLITE_OK;
   27637     }
   27638 #endif
   27639 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   27640     case SQLITE_SET_LOCKPROXYFILE:
   27641     case SQLITE_GET_LOCKPROXYFILE: {
   27642       return proxyFileControl(id,op,pArg);
   27643     }
   27644 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
   27645     case SQLITE_FCNTL_SYNC_OMITTED: {
   27646       return SQLITE_OK;  /* A no-op */
   27647     }
   27648   }
   27649   return SQLITE_NOTFOUND;
   27650 }
   27651 
   27652 /*
   27653 ** Return the sector size in bytes of the underlying block device for
   27654 ** the specified file. This is almost always 512 bytes, but may be
   27655 ** larger for some devices.
   27656 **
   27657 ** SQLite code assumes this function cannot fail. It also assumes that
   27658 ** if two files are created in the same file-system directory (i.e.
   27659 ** a database and its journal file) that the sector size will be the
   27660 ** same for both.
   27661 */
   27662 static int unixSectorSize(sqlite3_file *NotUsed){
   27663   UNUSED_PARAMETER(NotUsed);
   27664   return SQLITE_DEFAULT_SECTOR_SIZE;
   27665 }
   27666 
   27667 /*
   27668 ** Return the device characteristics for the file. This is always 0 for unix.
   27669 */
   27670 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
   27671   UNUSED_PARAMETER(NotUsed);
   27672   return 0;
   27673 }
   27674 
   27675 #ifndef SQLITE_OMIT_WAL
   27676 
   27677 
   27678 /*
   27679 ** Object used to represent an shared memory buffer.
   27680 **
   27681 ** When multiple threads all reference the same wal-index, each thread
   27682 ** has its own unixShm object, but they all point to a single instance
   27683 ** of this unixShmNode object.  In other words, each wal-index is opened
   27684 ** only once per process.
   27685 **
   27686 ** Each unixShmNode object is connected to a single unixInodeInfo object.
   27687 ** We could coalesce this object into unixInodeInfo, but that would mean
   27688 ** every open file that does not use shared memory (in other words, most
   27689 ** open files) would have to carry around this extra information.  So
   27690 ** the unixInodeInfo object contains a pointer to this unixShmNode object
   27691 ** and the unixShmNode object is created only when needed.
   27692 **
   27693 ** unixMutexHeld() must be true when creating or destroying
   27694 ** this object or while reading or writing the following fields:
   27695 **
   27696 **      nRef
   27697 **
   27698 ** The following fields are read-only after the object is created:
   27699 **
   27700 **      fid
   27701 **      zFilename
   27702 **
   27703 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
   27704 ** unixMutexHeld() is true when reading or writing any other field
   27705 ** in this structure.
   27706 */
   27707 struct unixShmNode {
   27708   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
   27709   sqlite3_mutex *mutex;      /* Mutex to access this object */
   27710   char *zFilename;           /* Name of the mmapped file */
   27711   int h;                     /* Open file descriptor */
   27712   int szRegion;              /* Size of shared-memory regions */
   27713   int nRegion;               /* Size of array apRegion */
   27714   char **apRegion;           /* Array of mapped shared-memory regions */
   27715   int nRef;                  /* Number of unixShm objects pointing to this */
   27716   unixShm *pFirst;           /* All unixShm objects pointing to this */
   27717 #ifdef SQLITE_DEBUG
   27718   u8 exclMask;               /* Mask of exclusive locks held */
   27719   u8 sharedMask;             /* Mask of shared locks held */
   27720   u8 nextShmId;              /* Next available unixShm.id value */
   27721 #endif
   27722 };
   27723 
   27724 /*
   27725 ** Structure used internally by this VFS to record the state of an
   27726 ** open shared memory connection.
   27727 **
   27728 ** The following fields are initialized when this object is created and
   27729 ** are read-only thereafter:
   27730 **
   27731 **    unixShm.pFile
   27732 **    unixShm.id
   27733 **
   27734 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
   27735 ** while accessing any read/write fields.
   27736 */
   27737 struct unixShm {
   27738   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
   27739   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
   27740   u8 hasMutex;               /* True if holding the unixShmNode mutex */
   27741   u16 sharedMask;            /* Mask of shared locks held */
   27742   u16 exclMask;              /* Mask of exclusive locks held */
   27743 #ifdef SQLITE_DEBUG
   27744   u8 id;                     /* Id of this connection within its unixShmNode */
   27745 #endif
   27746 };
   27747 
   27748 /*
   27749 ** Constants used for locking
   27750 */
   27751 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
   27752 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   27753 
   27754 /*
   27755 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
   27756 **
   27757 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
   27758 ** otherwise.
   27759 */
   27760 static int unixShmSystemLock(
   27761   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
   27762   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
   27763   int ofst,              /* First byte of the locking range */
   27764   int n                  /* Number of bytes to lock */
   27765 ){
   27766   struct flock f;       /* The posix advisory locking structure */
   27767   int rc = SQLITE_OK;   /* Result code form fcntl() */
   27768 
   27769   /* Access to the unixShmNode object is serialized by the caller */
   27770   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
   27771 
   27772   /* Shared locks never span more than one byte */
   27773   assert( n==1 || lockType!=F_RDLCK );
   27774 
   27775   /* Locks are within range */
   27776   assert( n>=1 && n<SQLITE_SHM_NLOCK );
   27777 
   27778   if( pShmNode->h>=0 ){
   27779     /* Initialize the locking parameters */
   27780     memset(&f, 0, sizeof(f));
   27781     f.l_type = lockType;
   27782     f.l_whence = SEEK_SET;
   27783     f.l_start = ofst;
   27784     f.l_len = n;
   27785 
   27786     rc = osFcntl(pShmNode->h, F_SETLK, &f);
   27787     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
   27788   }
   27789 
   27790   /* Update the global lock state and do debug tracing */
   27791 #ifdef SQLITE_DEBUG
   27792   { u16 mask;
   27793   OSTRACE(("SHM-LOCK "));
   27794   mask = (1<<(ofst+n)) - (1<<ofst);
   27795   if( rc==SQLITE_OK ){
   27796     if( lockType==F_UNLCK ){
   27797       OSTRACE(("unlock %d ok", ofst));
   27798       pShmNode->exclMask &= ~mask;
   27799       pShmNode->sharedMask &= ~mask;
   27800     }else if( lockType==F_RDLCK ){
   27801       OSTRACE(("read-lock %d ok", ofst));
   27802       pShmNode->exclMask &= ~mask;
   27803       pShmNode->sharedMask |= mask;
   27804     }else{
   27805       assert( lockType==F_WRLCK );
   27806       OSTRACE(("write-lock %d ok", ofst));
   27807       pShmNode->exclMask |= mask;
   27808       pShmNode->sharedMask &= ~mask;
   27809     }
   27810   }else{
   27811     if( lockType==F_UNLCK ){
   27812       OSTRACE(("unlock %d failed", ofst));
   27813     }else if( lockType==F_RDLCK ){
   27814       OSTRACE(("read-lock failed"));
   27815     }else{
   27816       assert( lockType==F_WRLCK );
   27817       OSTRACE(("write-lock %d failed", ofst));
   27818     }
   27819   }
   27820   OSTRACE((" - afterwards %03x,%03x\n",
   27821            pShmNode->sharedMask, pShmNode->exclMask));
   27822   }
   27823 #endif
   27824 
   27825   return rc;
   27826 }
   27827 
   27828 
   27829 /*
   27830 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
   27831 **
   27832 ** This is not a VFS shared-memory method; it is a utility function called
   27833 ** by VFS shared-memory methods.
   27834 */
   27835 static void unixShmPurge(unixFile *pFd){
   27836   unixShmNode *p = pFd->pInode->pShmNode;
   27837   assert( unixMutexHeld() );
   27838   if( p && p->nRef==0 ){
   27839     int i;
   27840     assert( p->pInode==pFd->pInode );
   27841     if( p->mutex ) sqlite3_mutex_free(p->mutex);
   27842     for(i=0; i<p->nRegion; i++){
   27843       if( p->h>=0 ){
   27844         munmap(p->apRegion[i], p->szRegion);
   27845       }else{
   27846         sqlite3_free(p->apRegion[i]);
   27847       }
   27848     }
   27849     sqlite3_free(p->apRegion);
   27850     if( p->h>=0 ){
   27851       robust_close(pFd, p->h, __LINE__);
   27852       p->h = -1;
   27853     }
   27854     p->pInode->pShmNode = 0;
   27855     sqlite3_free(p);
   27856   }
   27857 }
   27858 
   27859 /*
   27860 ** Open a shared-memory area associated with open database file pDbFd.
   27861 ** This particular implementation uses mmapped files.
   27862 **
   27863 ** The file used to implement shared-memory is in the same directory
   27864 ** as the open database file and has the same name as the open database
   27865 ** file with the "-shm" suffix added.  For example, if the database file
   27866 ** is "/home/user1/config.db" then the file that is created and mmapped
   27867 ** for shared memory will be called "/home/user1/config.db-shm".
   27868 **
   27869 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
   27870 ** some other tmpfs mount. But if a file in a different directory
   27871 ** from the database file is used, then differing access permissions
   27872 ** or a chroot() might cause two different processes on the same
   27873 ** database to end up using different files for shared memory -
   27874 ** meaning that their memory would not really be shared - resulting
   27875 ** in database corruption.  Nevertheless, this tmpfs file usage
   27876 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
   27877 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
   27878 ** option results in an incompatible build of SQLite;  builds of SQLite
   27879 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
   27880 ** same database file at the same time, database corruption will likely
   27881 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
   27882 ** "unsupported" and may go away in a future SQLite release.
   27883 **
   27884 ** When opening a new shared-memory file, if no other instances of that
   27885 ** file are currently open, in this process or in other processes, then
   27886 ** the file must be truncated to zero length or have its header cleared.
   27887 **
   27888 ** If the original database file (pDbFd) is using the "unix-excl" VFS
   27889 ** that means that an exclusive lock is held on the database file and
   27890 ** that no other processes are able to read or write the database.  In
   27891 ** that case, we do not really need shared memory.  No shared memory
   27892 ** file is created.  The shared memory will be simulated with heap memory.
   27893 */
   27894 static int unixOpenSharedMemory(unixFile *pDbFd){
   27895   struct unixShm *p = 0;          /* The connection to be opened */
   27896   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
   27897   int rc;                         /* Result code */
   27898   unixInodeInfo *pInode;          /* The inode of fd */
   27899   char *zShmFilename;             /* Name of the file used for SHM */
   27900   int nShmFilename;               /* Size of the SHM filename in bytes */
   27901 
   27902   /* Allocate space for the new unixShm object. */
   27903   p = sqlite3_malloc( sizeof(*p) );
   27904   if( p==0 ) return SQLITE_NOMEM;
   27905   memset(p, 0, sizeof(*p));
   27906   assert( pDbFd->pShm==0 );
   27907 
   27908   /* Check to see if a unixShmNode object already exists. Reuse an existing
   27909   ** one if present. Create a new one if necessary.
   27910   */
   27911   unixEnterMutex();
   27912   pInode = pDbFd->pInode;
   27913   pShmNode = pInode->pShmNode;
   27914   if( pShmNode==0 ){
   27915     struct stat sStat;                 /* fstat() info for database file */
   27916 
   27917     /* Call fstat() to figure out the permissions on the database file. If
   27918     ** a new *-shm file is created, an attempt will be made to create it
   27919     ** with the same permissions. The actual permissions the file is created
   27920     ** with are subject to the current umask setting.
   27921     */
   27922     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
   27923       rc = SQLITE_IOERR_FSTAT;
   27924       goto shm_open_err;
   27925     }
   27926 
   27927 #ifdef SQLITE_SHM_DIRECTORY
   27928     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
   27929 #else
   27930     nShmFilename = 5 + (int)strlen(pDbFd->zPath);
   27931 #endif
   27932     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
   27933     if( pShmNode==0 ){
   27934       rc = SQLITE_NOMEM;
   27935       goto shm_open_err;
   27936     }
   27937     memset(pShmNode, 0, sizeof(*pShmNode));
   27938     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
   27939 #ifdef SQLITE_SHM_DIRECTORY
   27940     sqlite3_snprintf(nShmFilename, zShmFilename,
   27941                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
   27942                      (u32)sStat.st_ino, (u32)sStat.st_dev);
   27943 #else
   27944     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
   27945 #endif
   27946     pShmNode->h = -1;
   27947     pDbFd->pInode->pShmNode = pShmNode;
   27948     pShmNode->pInode = pDbFd->pInode;
   27949     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   27950     if( pShmNode->mutex==0 ){
   27951       rc = SQLITE_NOMEM;
   27952       goto shm_open_err;
   27953     }
   27954 
   27955     if( pInode->bProcessLock==0 ){
   27956       pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
   27957                                (sStat.st_mode & 0777));
   27958       if( pShmNode->h<0 ){
   27959         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
   27960         goto shm_open_err;
   27961       }
   27962 
   27963       /* Check to see if another process is holding the dead-man switch.
   27964       ** If not, truncate the file to zero length.
   27965       */
   27966       rc = SQLITE_OK;
   27967       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
   27968         if( robust_ftruncate(pShmNode->h, 0) ){
   27969           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
   27970         }
   27971       }
   27972       if( rc==SQLITE_OK ){
   27973         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
   27974       }
   27975       if( rc ) goto shm_open_err;
   27976     }
   27977   }
   27978 
   27979   /* Make the new connection a child of the unixShmNode */
   27980   p->pShmNode = pShmNode;
   27981 #ifdef SQLITE_DEBUG
   27982   p->id = pShmNode->nextShmId++;
   27983 #endif
   27984   pShmNode->nRef++;
   27985   pDbFd->pShm = p;
   27986   unixLeaveMutex();
   27987 
   27988   /* The reference count on pShmNode has already been incremented under
   27989   ** the cover of the unixEnterMutex() mutex and the pointer from the
   27990   ** new (struct unixShm) object to the pShmNode has been set. All that is
   27991   ** left to do is to link the new object into the linked list starting
   27992   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
   27993   ** mutex.
   27994   */
   27995   sqlite3_mutex_enter(pShmNode->mutex);
   27996   p->pNext = pShmNode->pFirst;
   27997   pShmNode->pFirst = p;
   27998   sqlite3_mutex_leave(pShmNode->mutex);
   27999   return SQLITE_OK;
   28000 
   28001   /* Jump here on any error */
   28002 shm_open_err:
   28003   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
   28004   sqlite3_free(p);
   28005   unixLeaveMutex();
   28006   return rc;
   28007 }
   28008 
   28009 /*
   28010 ** This function is called to obtain a pointer to region iRegion of the
   28011 ** shared-memory associated with the database file fd. Shared-memory regions
   28012 ** are numbered starting from zero. Each shared-memory region is szRegion
   28013 ** bytes in size.
   28014 **
   28015 ** If an error occurs, an error code is returned and *pp is set to NULL.
   28016 **
   28017 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
   28018 ** region has not been allocated (by any client, including one running in a
   28019 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
   28020 ** bExtend is non-zero and the requested shared-memory region has not yet
   28021 ** been allocated, it is allocated by this function.
   28022 **
   28023 ** If the shared-memory region has already been allocated or is allocated by
   28024 ** this call as described above, then it is mapped into this processes
   28025 ** address space (if it is not already), *pp is set to point to the mapped
   28026 ** memory and SQLITE_OK returned.
   28027 */
   28028 static int unixShmMap(
   28029   sqlite3_file *fd,               /* Handle open on database file */
   28030   int iRegion,                    /* Region to retrieve */
   28031   int szRegion,                   /* Size of regions */
   28032   int bExtend,                    /* True to extend file if necessary */
   28033   void volatile **pp              /* OUT: Mapped memory */
   28034 ){
   28035   unixFile *pDbFd = (unixFile*)fd;
   28036   unixShm *p;
   28037   unixShmNode *pShmNode;
   28038   int rc = SQLITE_OK;
   28039 
   28040   /* If the shared-memory file has not yet been opened, open it now. */
   28041   if( pDbFd->pShm==0 ){
   28042     rc = unixOpenSharedMemory(pDbFd);
   28043     if( rc!=SQLITE_OK ) return rc;
   28044   }
   28045 
   28046   p = pDbFd->pShm;
   28047   pShmNode = p->pShmNode;
   28048   sqlite3_mutex_enter(pShmNode->mutex);
   28049   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
   28050   assert( pShmNode->pInode==pDbFd->pInode );
   28051   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
   28052   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
   28053 
   28054   if( pShmNode->nRegion<=iRegion ){
   28055     char **apNew;                      /* New apRegion[] array */
   28056     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
   28057     struct stat sStat;                 /* Used by fstat() */
   28058 
   28059     pShmNode->szRegion = szRegion;
   28060 
   28061     if( pShmNode->h>=0 ){
   28062       /* The requested region is not mapped into this processes address space.
   28063       ** Check to see if it has been allocated (i.e. if the wal-index file is
   28064       ** large enough to contain the requested region).
   28065       */
   28066       if( osFstat(pShmNode->h, &sStat) ){
   28067         rc = SQLITE_IOERR_SHMSIZE;
   28068         goto shmpage_out;
   28069       }
   28070 
   28071       if( sStat.st_size<nByte ){
   28072         /* The requested memory region does not exist. If bExtend is set to
   28073         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
   28074         **
   28075         ** Alternatively, if bExtend is true, use ftruncate() to allocate
   28076         ** the requested memory region.
   28077         */
   28078         if( !bExtend ) goto shmpage_out;
   28079         if( robust_ftruncate(pShmNode->h, nByte) ){
   28080           rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
   28081                             pShmNode->zFilename);
   28082           goto shmpage_out;
   28083         }
   28084       }
   28085     }
   28086 
   28087     /* Map the requested memory region into this processes address space. */
   28088     apNew = (char **)sqlite3_realloc(
   28089         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
   28090     );
   28091     if( !apNew ){
   28092       rc = SQLITE_IOERR_NOMEM;
   28093       goto shmpage_out;
   28094     }
   28095     pShmNode->apRegion = apNew;
   28096     while(pShmNode->nRegion<=iRegion){
   28097       void *pMem;
   28098       if( pShmNode->h>=0 ){
   28099         pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE,
   28100             MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
   28101         );
   28102         if( pMem==MAP_FAILED ){
   28103           rc = SQLITE_IOERR;
   28104           goto shmpage_out;
   28105         }
   28106       }else{
   28107         pMem = sqlite3_malloc(szRegion);
   28108         if( pMem==0 ){
   28109           rc = SQLITE_NOMEM;
   28110           goto shmpage_out;
   28111         }
   28112         memset(pMem, 0, szRegion);
   28113       }
   28114       pShmNode->apRegion[pShmNode->nRegion] = pMem;
   28115       pShmNode->nRegion++;
   28116     }
   28117   }
   28118 
   28119 shmpage_out:
   28120   if( pShmNode->nRegion>iRegion ){
   28121     *pp = pShmNode->apRegion[iRegion];
   28122   }else{
   28123     *pp = 0;
   28124   }
   28125   sqlite3_mutex_leave(pShmNode->mutex);
   28126   return rc;
   28127 }
   28128 
   28129 /*
   28130 ** Change the lock state for a shared-memory segment.
   28131 **
   28132 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
   28133 ** different here than in posix.  In xShmLock(), one can go from unlocked
   28134 ** to shared and back or from unlocked to exclusive and back.  But one may
   28135 ** not go from shared to exclusive or from exclusive to shared.
   28136 */
   28137 static int unixShmLock(
   28138   sqlite3_file *fd,          /* Database file holding the shared memory */
   28139   int ofst,                  /* First lock to acquire or release */
   28140   int n,                     /* Number of locks to acquire or release */
   28141   int flags                  /* What to do with the lock */
   28142 ){
   28143   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
   28144   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
   28145   unixShm *pX;                          /* For looping over all siblings */
   28146   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
   28147   int rc = SQLITE_OK;                   /* Result code */
   28148   u16 mask;                             /* Mask of locks to take or release */
   28149 
   28150   assert( pShmNode==pDbFd->pInode->pShmNode );
   28151   assert( pShmNode->pInode==pDbFd->pInode );
   28152   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   28153   assert( n>=1 );
   28154   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   28155        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   28156        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   28157        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   28158   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   28159   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
   28160   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
   28161 
   28162   mask = (1<<(ofst+n)) - (1<<ofst);
   28163   assert( n>1 || mask==(1<<ofst) );
   28164   sqlite3_mutex_enter(pShmNode->mutex);
   28165   if( flags & SQLITE_SHM_UNLOCK ){
   28166     u16 allMask = 0; /* Mask of locks held by siblings */
   28167 
   28168     /* See if any siblings hold this same lock */
   28169     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   28170       if( pX==p ) continue;
   28171       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   28172       allMask |= pX->sharedMask;
   28173     }
   28174 
   28175     /* Unlock the system-level locks */
   28176     if( (mask & allMask)==0 ){
   28177       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
   28178     }else{
   28179       rc = SQLITE_OK;
   28180     }
   28181 
   28182     /* Undo the local locks */
   28183     if( rc==SQLITE_OK ){
   28184       p->exclMask &= ~mask;
   28185       p->sharedMask &= ~mask;
   28186     }
   28187   }else if( flags & SQLITE_SHM_SHARED ){
   28188     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
   28189 
   28190     /* Find out which shared locks are already held by sibling connections.
   28191     ** If any sibling already holds an exclusive lock, go ahead and return
   28192     ** SQLITE_BUSY.
   28193     */
   28194     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   28195       if( (pX->exclMask & mask)!=0 ){
   28196         rc = SQLITE_BUSY;
   28197         break;
   28198       }
   28199       allShared |= pX->sharedMask;
   28200     }
   28201 
   28202     /* Get shared locks at the system level, if necessary */
   28203     if( rc==SQLITE_OK ){
   28204       if( (allShared & mask)==0 ){
   28205         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
   28206       }else{
   28207         rc = SQLITE_OK;
   28208       }
   28209     }
   28210 
   28211     /* Get the local shared locks */
   28212     if( rc==SQLITE_OK ){
   28213       p->sharedMask |= mask;
   28214     }
   28215   }else{
   28216     /* Make sure no sibling connections hold locks that will block this
   28217     ** lock.  If any do, return SQLITE_BUSY right away.
   28218     */
   28219     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   28220       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   28221         rc = SQLITE_BUSY;
   28222         break;
   28223       }
   28224     }
   28225 
   28226     /* Get the exclusive locks at the system level.  Then if successful
   28227     ** also mark the local connection as being locked.
   28228     */
   28229     if( rc==SQLITE_OK ){
   28230       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
   28231       if( rc==SQLITE_OK ){
   28232         assert( (p->sharedMask & mask)==0 );
   28233         p->exclMask |= mask;
   28234       }
   28235     }
   28236   }
   28237   sqlite3_mutex_leave(pShmNode->mutex);
   28238   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
   28239            p->id, getpid(), p->sharedMask, p->exclMask));
   28240   return rc;
   28241 }
   28242 
   28243 /*
   28244 ** Implement a memory barrier or memory fence on shared memory.
   28245 **
   28246 ** All loads and stores begun before the barrier must complete before
   28247 ** any load or store begun after the barrier.
   28248 */
   28249 static void unixShmBarrier(
   28250   sqlite3_file *fd                /* Database file holding the shared memory */
   28251 ){
   28252   UNUSED_PARAMETER(fd);
   28253   unixEnterMutex();
   28254   unixLeaveMutex();
   28255 }
   28256 
   28257 /*
   28258 ** Close a connection to shared-memory.  Delete the underlying
   28259 ** storage if deleteFlag is true.
   28260 **
   28261 ** If there is no shared memory associated with the connection then this
   28262 ** routine is a harmless no-op.
   28263 */
   28264 static int unixShmUnmap(
   28265   sqlite3_file *fd,               /* The underlying database file */
   28266   int deleteFlag                  /* Delete shared-memory if true */
   28267 ){
   28268   unixShm *p;                     /* The connection to be closed */
   28269   unixShmNode *pShmNode;          /* The underlying shared-memory file */
   28270   unixShm **pp;                   /* For looping over sibling connections */
   28271   unixFile *pDbFd;                /* The underlying database file */
   28272 
   28273   pDbFd = (unixFile*)fd;
   28274   p = pDbFd->pShm;
   28275   if( p==0 ) return SQLITE_OK;
   28276   pShmNode = p->pShmNode;
   28277 
   28278   assert( pShmNode==pDbFd->pInode->pShmNode );
   28279   assert( pShmNode->pInode==pDbFd->pInode );
   28280 
   28281   /* Remove connection p from the set of connections associated
   28282   ** with pShmNode */
   28283   sqlite3_mutex_enter(pShmNode->mutex);
   28284   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
   28285   *pp = p->pNext;
   28286 
   28287   /* Free the connection p */
   28288   sqlite3_free(p);
   28289   pDbFd->pShm = 0;
   28290   sqlite3_mutex_leave(pShmNode->mutex);
   28291 
   28292   /* If pShmNode->nRef has reached 0, then close the underlying
   28293   ** shared-memory file, too */
   28294   unixEnterMutex();
   28295   assert( pShmNode->nRef>0 );
   28296   pShmNode->nRef--;
   28297   if( pShmNode->nRef==0 ){
   28298     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
   28299     unixShmPurge(pDbFd);
   28300   }
   28301   unixLeaveMutex();
   28302 
   28303   return SQLITE_OK;
   28304 }
   28305 
   28306 
   28307 #else
   28308 # define unixShmMap     0
   28309 # define unixShmLock    0
   28310 # define unixShmBarrier 0
   28311 # define unixShmUnmap   0
   28312 #endif /* #ifndef SQLITE_OMIT_WAL */
   28313 
   28314 /*
   28315 ** Here ends the implementation of all sqlite3_file methods.
   28316 **
   28317 ********************** End sqlite3_file Methods *******************************
   28318 ******************************************************************************/
   28319 
   28320 /*
   28321 ** This division contains definitions of sqlite3_io_methods objects that
   28322 ** implement various file locking strategies.  It also contains definitions
   28323 ** of "finder" functions.  A finder-function is used to locate the appropriate
   28324 ** sqlite3_io_methods object for a particular database file.  The pAppData
   28325 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
   28326 ** the correct finder-function for that VFS.
   28327 **
   28328 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
   28329 ** object.  The only interesting finder-function is autolockIoFinder, which
   28330 ** looks at the filesystem type and tries to guess the best locking
   28331 ** strategy from that.
   28332 **
   28333 ** For finder-funtion F, two objects are created:
   28334 **
   28335 **    (1) The real finder-function named "FImpt()".
   28336 **
   28337 **    (2) A constant pointer to this function named just "F".
   28338 **
   28339 **
   28340 ** A pointer to the F pointer is used as the pAppData value for VFS
   28341 ** objects.  We have to do this instead of letting pAppData point
   28342 ** directly at the finder-function since C90 rules prevent a void*
   28343 ** from be cast into a function pointer.
   28344 **
   28345 **
   28346 ** Each instance of this macro generates two objects:
   28347 **
   28348 **   *  A constant sqlite3_io_methods object call METHOD that has locking
   28349 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
   28350 **
   28351 **   *  An I/O method finder function called FINDER that returns a pointer
   28352 **      to the METHOD object in the previous bullet.
   28353 */
   28354 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
   28355 static const sqlite3_io_methods METHOD = {                                   \
   28356    VERSION,                    /* iVersion */                                \
   28357    CLOSE,                      /* xClose */                                  \
   28358    unixRead,                   /* xRead */                                   \
   28359    unixWrite,                  /* xWrite */                                  \
   28360    unixTruncate,               /* xTruncate */                               \
   28361    unixSync,                   /* xSync */                                   \
   28362    unixFileSize,               /* xFileSize */                               \
   28363    LOCK,                       /* xLock */                                   \
   28364    UNLOCK,                     /* xUnlock */                                 \
   28365    CKLOCK,                     /* xCheckReservedLock */                      \
   28366    unixFileControl,            /* xFileControl */                            \
   28367    unixSectorSize,             /* xSectorSize */                             \
   28368    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
   28369    unixShmMap,                 /* xShmMap */                                 \
   28370    unixShmLock,                /* xShmLock */                                \
   28371    unixShmBarrier,             /* xShmBarrier */                             \
   28372    unixShmUnmap                /* xShmUnmap */                               \
   28373 };                                                                           \
   28374 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
   28375   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
   28376   return &METHOD;                                                            \
   28377 }                                                                            \
   28378 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
   28379     = FINDER##Impl;
   28380 
   28381 /*
   28382 ** Here are all of the sqlite3_io_methods objects for each of the
   28383 ** locking strategies.  Functions that return pointers to these methods
   28384 ** are also created.
   28385 */
   28386 IOMETHODS(
   28387   posixIoFinder,            /* Finder function name */
   28388   posixIoMethods,           /* sqlite3_io_methods object name */
   28389   2,                        /* shared memory is enabled */
   28390   unixClose,                /* xClose method */
   28391   unixLock,                 /* xLock method */
   28392   unixUnlock,               /* xUnlock method */
   28393   unixCheckReservedLock     /* xCheckReservedLock method */
   28394 )
   28395 IOMETHODS(
   28396   nolockIoFinder,           /* Finder function name */
   28397   nolockIoMethods,          /* sqlite3_io_methods object name */
   28398   1,                        /* shared memory is disabled */
   28399   nolockClose,              /* xClose method */
   28400   nolockLock,               /* xLock method */
   28401   nolockUnlock,             /* xUnlock method */
   28402   nolockCheckReservedLock   /* xCheckReservedLock method */
   28403 )
   28404 IOMETHODS(
   28405   dotlockIoFinder,          /* Finder function name */
   28406   dotlockIoMethods,         /* sqlite3_io_methods object name */
   28407   1,                        /* shared memory is disabled */
   28408   dotlockClose,             /* xClose method */
   28409   dotlockLock,              /* xLock method */
   28410   dotlockUnlock,            /* xUnlock method */
   28411   dotlockCheckReservedLock  /* xCheckReservedLock method */
   28412 )
   28413 
   28414 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
   28415 IOMETHODS(
   28416   flockIoFinder,            /* Finder function name */
   28417   flockIoMethods,           /* sqlite3_io_methods object name */
   28418   1,                        /* shared memory is disabled */
   28419   flockClose,               /* xClose method */
   28420   flockLock,                /* xLock method */
   28421   flockUnlock,              /* xUnlock method */
   28422   flockCheckReservedLock    /* xCheckReservedLock method */
   28423 )
   28424 #endif
   28425 
   28426 #if OS_VXWORKS
   28427 IOMETHODS(
   28428   semIoFinder,              /* Finder function name */
   28429   semIoMethods,             /* sqlite3_io_methods object name */
   28430   1,                        /* shared memory is disabled */
   28431   semClose,                 /* xClose method */
   28432   semLock,                  /* xLock method */
   28433   semUnlock,                /* xUnlock method */
   28434   semCheckReservedLock      /* xCheckReservedLock method */
   28435 )
   28436 #endif
   28437 
   28438 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28439 IOMETHODS(
   28440   afpIoFinder,              /* Finder function name */
   28441   afpIoMethods,             /* sqlite3_io_methods object name */
   28442   1,                        /* shared memory is disabled */
   28443   afpClose,                 /* xClose method */
   28444   afpLock,                  /* xLock method */
   28445   afpUnlock,                /* xUnlock method */
   28446   afpCheckReservedLock      /* xCheckReservedLock method */
   28447 )
   28448 #endif
   28449 
   28450 /*
   28451 ** The proxy locking method is a "super-method" in the sense that it
   28452 ** opens secondary file descriptors for the conch and lock files and
   28453 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
   28454 ** secondary files.  For this reason, the division that implements
   28455 ** proxy locking is located much further down in the file.  But we need
   28456 ** to go ahead and define the sqlite3_io_methods and finder function
   28457 ** for proxy locking here.  So we forward declare the I/O methods.
   28458 */
   28459 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28460 static int proxyClose(sqlite3_file*);
   28461 static int proxyLock(sqlite3_file*, int);
   28462 static int proxyUnlock(sqlite3_file*, int);
   28463 static int proxyCheckReservedLock(sqlite3_file*, int*);
   28464 IOMETHODS(
   28465   proxyIoFinder,            /* Finder function name */
   28466   proxyIoMethods,           /* sqlite3_io_methods object name */
   28467   1,                        /* shared memory is disabled */
   28468   proxyClose,               /* xClose method */
   28469   proxyLock,                /* xLock method */
   28470   proxyUnlock,              /* xUnlock method */
   28471   proxyCheckReservedLock    /* xCheckReservedLock method */
   28472 )
   28473 #endif
   28474 
   28475 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
   28476 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28477 IOMETHODS(
   28478   nfsIoFinder,               /* Finder function name */
   28479   nfsIoMethods,              /* sqlite3_io_methods object name */
   28480   1,                         /* shared memory is disabled */
   28481   unixClose,                 /* xClose method */
   28482   unixLock,                  /* xLock method */
   28483   nfsUnlock,                 /* xUnlock method */
   28484   unixCheckReservedLock      /* xCheckReservedLock method */
   28485 )
   28486 #endif
   28487 
   28488 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28489 /*
   28490 ** This "finder" function attempts to determine the best locking strategy
   28491 ** for the database file "filePath".  It then returns the sqlite3_io_methods
   28492 ** object that implements that strategy.
   28493 **
   28494 ** This is for MacOSX only.
   28495 */
   28496 static const sqlite3_io_methods *autolockIoFinderImpl(
   28497   const char *filePath,    /* name of the database file */
   28498   unixFile *pNew           /* open file object for the database file */
   28499 ){
   28500   static const struct Mapping {
   28501     const char *zFilesystem;              /* Filesystem type name */
   28502     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
   28503   } aMap[] = {
   28504     { "hfs",    &posixIoMethods },
   28505     { "ufs",    &posixIoMethods },
   28506     { "afpfs",  &afpIoMethods },
   28507     { "smbfs",  &afpIoMethods },
   28508     { "webdav", &nolockIoMethods },
   28509     { 0, 0 }
   28510   };
   28511   int i;
   28512   struct statfs fsInfo;
   28513   struct flock lockInfo;
   28514 
   28515   if( !filePath ){
   28516     /* If filePath==NULL that means we are dealing with a transient file
   28517     ** that does not need to be locked. */
   28518     return &nolockIoMethods;
   28519   }
   28520   if( statfs(filePath, &fsInfo) != -1 ){
   28521     if( fsInfo.f_flags & MNT_RDONLY ){
   28522       return &nolockIoMethods;
   28523     }
   28524     for(i=0; aMap[i].zFilesystem; i++){
   28525       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
   28526         return aMap[i].pMethods;
   28527       }
   28528     }
   28529   }
   28530 
   28531   /* Default case. Handles, amongst others, "nfs".
   28532   ** Test byte-range lock using fcntl(). If the call succeeds,
   28533   ** assume that the file-system supports POSIX style locks.
   28534   */
   28535   lockInfo.l_len = 1;
   28536   lockInfo.l_start = 0;
   28537   lockInfo.l_whence = SEEK_SET;
   28538   lockInfo.l_type = F_RDLCK;
   28539   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
   28540     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
   28541       return &nfsIoMethods;
   28542     } else {
   28543       return &posixIoMethods;
   28544     }
   28545   }else{
   28546     return &dotlockIoMethods;
   28547   }
   28548 }
   28549 static const sqlite3_io_methods
   28550   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
   28551 
   28552 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   28553 
   28554 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
   28555 /*
   28556 ** This "finder" function attempts to determine the best locking strategy
   28557 ** for the database file "filePath".  It then returns the sqlite3_io_methods
   28558 ** object that implements that strategy.
   28559 **
   28560 ** This is for VXWorks only.
   28561 */
   28562 static const sqlite3_io_methods *autolockIoFinderImpl(
   28563   const char *filePath,    /* name of the database file */
   28564   unixFile *pNew           /* the open file object */
   28565 ){
   28566   struct flock lockInfo;
   28567 
   28568   if( !filePath ){
   28569     /* If filePath==NULL that means we are dealing with a transient file
   28570     ** that does not need to be locked. */
   28571     return &nolockIoMethods;
   28572   }
   28573 
   28574   /* Test if fcntl() is supported and use POSIX style locks.
   28575   ** Otherwise fall back to the named semaphore method.
   28576   */
   28577   lockInfo.l_len = 1;
   28578   lockInfo.l_start = 0;
   28579   lockInfo.l_whence = SEEK_SET;
   28580   lockInfo.l_type = F_RDLCK;
   28581   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
   28582     return &posixIoMethods;
   28583   }else{
   28584     return &semIoMethods;
   28585   }
   28586 }
   28587 static const sqlite3_io_methods
   28588   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
   28589 
   28590 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
   28591 
   28592 /*
   28593 ** An abstract type for a pointer to a IO method finder function:
   28594 */
   28595 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
   28596 
   28597 
   28598 /****************************************************************************
   28599 **************************** sqlite3_vfs methods ****************************
   28600 **
   28601 ** This division contains the implementation of methods on the
   28602 ** sqlite3_vfs object.
   28603 */
   28604 
   28605 /*
   28606 ** Initializes a unixFile structure with zeros.
   28607 */
   28608 void initUnixFile(sqlite3_file* file) {
   28609   memset(file, 0, sizeof(unixFile));
   28610 }
   28611 
   28612 /*
   28613 ** Initialize the contents of the unixFile structure pointed to by pId.
   28614 */
   28615 int fillInUnixFile(
   28616   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
   28617   int h,                  /* Open file descriptor of file being opened */
   28618   int syncDir,            /* True to sync directory on first sync */
   28619   sqlite3_file *pId,      /* Write to the unixFile structure here */
   28620   const char *zFilename,  /* Name of the file being opened */
   28621   int noLock,             /* Omit locking if true */
   28622   int isDelete,           /* Delete on close if true */
   28623   int isReadOnly          /* True if the file is opened read-only */
   28624 ){
   28625   const sqlite3_io_methods *pLockingStyle;
   28626   unixFile *pNew = (unixFile *)pId;
   28627   int rc = SQLITE_OK;
   28628 
   28629   assert( pNew->pInode==NULL );
   28630 
   28631   /* Parameter isDelete is only used on vxworks. Express this explicitly
   28632   ** here to prevent compiler warnings about unused parameters.
   28633   */
   28634   UNUSED_PARAMETER(isDelete);
   28635 
   28636   /* Usually the path zFilename should not be a relative pathname. The
   28637   ** exception is when opening the proxy "conch" file in builds that
   28638   ** include the special Apple locking styles.
   28639   */
   28640 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28641   assert( zFilename==0 || zFilename[0]=='/'
   28642     || pVfs->pAppData==(void*)&autolockIoFinder );
   28643 #else
   28644   assert( zFilename==0 || zFilename[0]=='/' );
   28645 #endif
   28646 
   28647   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
   28648   pNew->h = h;
   28649   pNew->zPath = zFilename;
   28650   if( strcmp(pVfs->zName,"unix-excl")==0 ){
   28651     pNew->ctrlFlags = UNIXFILE_EXCL;
   28652   }else{
   28653     pNew->ctrlFlags = 0;
   28654   }
   28655   if( isReadOnly ){
   28656     pNew->ctrlFlags |= UNIXFILE_RDONLY;
   28657   }
   28658   if( syncDir ){
   28659     pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
   28660   }
   28661 
   28662 #if OS_VXWORKS
   28663   pNew->pId = vxworksFindFileId(zFilename);
   28664   if( pNew->pId==0 ){
   28665     noLock = 1;
   28666     rc = SQLITE_NOMEM;
   28667   }
   28668 #endif
   28669 
   28670   if( noLock ){
   28671     pLockingStyle = &nolockIoMethods;
   28672   }else{
   28673     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
   28674 #if SQLITE_ENABLE_LOCKING_STYLE
   28675     /* Cache zFilename in the locking context (AFP and dotlock override) for
   28676     ** proxyLock activation is possible (remote proxy is based on db name)
   28677     ** zFilename remains valid until file is closed, to support */
   28678     pNew->lockingContext = (void*)zFilename;
   28679 #endif
   28680   }
   28681 
   28682   if( pLockingStyle == &posixIoMethods
   28683 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   28684     || pLockingStyle == &nfsIoMethods
   28685 #endif
   28686   ){
   28687     unixEnterMutex();
   28688     rc = findInodeInfo(pNew, &pNew->pInode);
   28689     if( rc!=SQLITE_OK ){
   28690       /* If an error occured in findInodeInfo(), close the file descriptor
   28691       ** immediately, before releasing the mutex. findInodeInfo() may fail
   28692       ** in two scenarios:
   28693       **
   28694       **   (a) A call to fstat() failed.
   28695       **   (b) A malloc failed.
   28696       **
   28697       ** Scenario (b) may only occur if the process is holding no other
   28698       ** file descriptors open on the same file. If there were other file
   28699       ** descriptors on this file, then no malloc would be required by
   28700       ** findInodeInfo(). If this is the case, it is quite safe to close
   28701       ** handle h - as it is guaranteed that no posix locks will be released
   28702       ** by doing so.
   28703       **
   28704       ** If scenario (a) caused the error then things are not so safe. The
   28705       ** implicit assumption here is that if fstat() fails, things are in
   28706       ** such bad shape that dropping a lock or two doesn't matter much.
   28707       */
   28708       robust_close(pNew, h, __LINE__);
   28709       h = -1;
   28710     }
   28711     unixLeaveMutex();
   28712   }
   28713 
   28714 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   28715   else if( pLockingStyle == &afpIoMethods ){
   28716     /* AFP locking uses the file path so it needs to be included in
   28717     ** the afpLockingContext.
   28718     */
   28719     afpLockingContext *pCtx;
   28720     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
   28721     if( pCtx==0 ){
   28722       rc = SQLITE_NOMEM;
   28723     }else{
   28724       /* NB: zFilename exists and remains valid until the file is closed
   28725       ** according to requirement F11141.  So we do not need to make a
   28726       ** copy of the filename. */
   28727       pCtx->dbPath = zFilename;
   28728       pCtx->reserved = 0;
   28729       srandomdev();
   28730       unixEnterMutex();
   28731       rc = findInodeInfo(pNew, &pNew->pInode);
   28732       if( rc!=SQLITE_OK ){
   28733         sqlite3_free(pNew->lockingContext);
   28734         robust_close(pNew, h, __LINE__);
   28735         h = -1;
   28736       }
   28737       unixLeaveMutex();
   28738     }
   28739   }
   28740 #endif
   28741 
   28742   else if( pLockingStyle == &dotlockIoMethods ){
   28743     /* Dotfile locking uses the file path so it needs to be included in
   28744     ** the dotlockLockingContext
   28745     */
   28746     char *zLockFile;
   28747     int nFilename;
   28748     nFilename = (int)strlen(zFilename) + 6;
   28749     zLockFile = (char *)sqlite3_malloc(nFilename);
   28750     if( zLockFile==0 ){
   28751       rc = SQLITE_NOMEM;
   28752     }else{
   28753       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
   28754     }
   28755     pNew->lockingContext = zLockFile;
   28756   }
   28757 
   28758 #if OS_VXWORKS
   28759   else if( pLockingStyle == &semIoMethods ){
   28760     /* Named semaphore locking uses the file path so it needs to be
   28761     ** included in the semLockingContext
   28762     */
   28763     unixEnterMutex();
   28764     rc = findInodeInfo(pNew, &pNew->pInode);
   28765     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
   28766       char *zSemName = pNew->pInode->aSemName;
   28767       int n;
   28768       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
   28769                        pNew->pId->zCanonicalName);
   28770       for( n=1; zSemName[n]; n++ )
   28771         if( zSemName[n]=='/' ) zSemName[n] = '_';
   28772       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
   28773       if( pNew->pInode->pSem == SEM_FAILED ){
   28774         rc = SQLITE_NOMEM;
   28775         pNew->pInode->aSemName[0] = '\0';
   28776       }
   28777     }
   28778     unixLeaveMutex();
   28779   }
   28780 #endif
   28781 
   28782   pNew->lastErrno = 0;
   28783 #if OS_VXWORKS
   28784   if( rc!=SQLITE_OK ){
   28785     if( h>=0 ) robust_close(pNew, h, __LINE__);
   28786     h = -1;
   28787     osUnlink(zFilename);
   28788     isDelete = 0;
   28789   }
   28790   pNew->isDelete = isDelete;
   28791 #endif
   28792   if( rc!=SQLITE_OK ){
   28793     if( h>=0 ) robust_close(pNew, h, __LINE__);
   28794   }else{
   28795     pNew->pMethod = pLockingStyle;
   28796     OpenCounter(+1);
   28797   }
   28798   return rc;
   28799 }
   28800 
   28801 /*
   28802 ** Return the name of a directory in which to put temporary files.
   28803 ** If no suitable temporary file directory can be found, return NULL.
   28804 */
   28805 static const char *unixTempFileDir(void){
   28806   static const char *azDirs[] = {
   28807      0,
   28808      0,
   28809      "/var/tmp",
   28810      "/usr/tmp",
   28811      "/tmp",
   28812      0        /* List terminator */
   28813   };
   28814   unsigned int i;
   28815   struct stat buf;
   28816   const char *zDir = 0;
   28817 
   28818   azDirs[0] = sqlite3_temp_directory;
   28819   if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
   28820   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
   28821     if( zDir==0 ) continue;
   28822     if( osStat(zDir, &buf) ) continue;
   28823     if( !S_ISDIR(buf.st_mode) ) continue;
   28824     if( osAccess(zDir, 07) ) continue;
   28825     break;
   28826   }
   28827   return zDir;
   28828 }
   28829 
   28830 /*
   28831 ** Create a temporary file name in zBuf.  zBuf must be allocated
   28832 ** by the calling process and must be big enough to hold at least
   28833 ** pVfs->mxPathname bytes.
   28834 */
   28835 static int unixGetTempname(int nBuf, char *zBuf){
   28836   static const unsigned char zChars[] =
   28837     "abcdefghijklmnopqrstuvwxyz"
   28838     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   28839     "0123456789";
   28840   unsigned int i, j;
   28841   const char *zDir;
   28842 
   28843   /* It's odd to simulate an io-error here, but really this is just
   28844   ** using the io-error infrastructure to test that SQLite handles this
   28845   ** function failing.
   28846   */
   28847   SimulateIOError( return SQLITE_IOERR );
   28848 
   28849   zDir = unixTempFileDir();
   28850   if( zDir==0 ) zDir = ".";
   28851 
   28852   /* Check that the output buffer is large enough for the temporary file
   28853   ** name. If it is not, return SQLITE_ERROR.
   28854   */
   28855   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
   28856     return SQLITE_ERROR;
   28857   }
   28858 
   28859   do{
   28860     sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
   28861     j = (int)strlen(zBuf);
   28862     sqlite3_randomness(15, &zBuf[j]);
   28863     for(i=0; i<15; i++, j++){
   28864       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   28865     }
   28866     zBuf[j] = 0;
   28867   }while( osAccess(zBuf,0)==0 );
   28868   return SQLITE_OK;
   28869 }
   28870 
   28871 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   28872 /*
   28873 ** Routine to transform a unixFile into a proxy-locking unixFile.
   28874 ** Implementation in the proxy-lock division, but used by unixOpen()
   28875 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
   28876 */
   28877 static int proxyTransformUnixFile(unixFile*, const char*);
   28878 #endif
   28879 
   28880 /*
   28881 ** Search for an unused file descriptor that was opened on the database
   28882 ** file (not a journal or master-journal file) identified by pathname
   28883 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
   28884 ** argument to this function.
   28885 **
   28886 ** Such a file descriptor may exist if a database connection was closed
   28887 ** but the associated file descriptor could not be closed because some
   28888 ** other file descriptor open on the same file is holding a file-lock.
   28889 ** Refer to comments in the unixClose() function and the lengthy comment
   28890 ** describing "Posix Advisory Locking" at the start of this file for
   28891 ** further details. Also, ticket #4018.
   28892 **
   28893 ** If a suitable file descriptor is found, then it is returned. If no
   28894 ** such file descriptor is located, -1 is returned.
   28895 */
   28896 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
   28897   UnixUnusedFd *pUnused = 0;
   28898 
   28899   /* Do not search for an unused file descriptor on vxworks. Not because
   28900   ** vxworks would not benefit from the change (it might, we're not sure),
   28901   ** but because no way to test it is currently available. It is better
   28902   ** not to risk breaking vxworks support for the sake of such an obscure
   28903   ** feature.  */
   28904 #if !OS_VXWORKS
   28905   struct stat sStat;                   /* Results of stat() call */
   28906 
   28907   /* A stat() call may fail for various reasons. If this happens, it is
   28908   ** almost certain that an open() call on the same path will also fail.
   28909   ** For this reason, if an error occurs in the stat() call here, it is
   28910   ** ignored and -1 is returned. The caller will try to open a new file
   28911   ** descriptor on the same path, fail, and return an error to SQLite.
   28912   **
   28913   ** Even if a subsequent open() call does succeed, the consequences of
   28914   ** not searching for a resusable file descriptor are not dire.  */
   28915   if( 0==osStat(zPath, &sStat) ){
   28916     unixInodeInfo *pInode;
   28917 
   28918     unixEnterMutex();
   28919     pInode = inodeList;
   28920     while( pInode && (pInode->fileId.dev!=sStat.st_dev
   28921                      || pInode->fileId.ino!=sStat.st_ino) ){
   28922        pInode = pInode->pNext;
   28923     }
   28924     if( pInode ){
   28925       UnixUnusedFd **pp;
   28926       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
   28927       pUnused = *pp;
   28928       if( pUnused ){
   28929         *pp = pUnused->pNext;
   28930       }
   28931     }
   28932     unixLeaveMutex();
   28933   }
   28934 #endif    /* if !OS_VXWORKS */
   28935   return pUnused;
   28936 }
   28937 
   28938 /*
   28939 ** This function is called by unixOpen() to determine the unix permissions
   28940 ** to create new files with. If no error occurs, then SQLITE_OK is returned
   28941 ** and a value suitable for passing as the third argument to open(2) is
   28942 ** written to *pMode. If an IO error occurs, an SQLite error code is
   28943 ** returned and the value of *pMode is not modified.
   28944 **
   28945 ** If the file being opened is a temporary file, it is always created with
   28946 ** the octal permissions 0600 (read/writable by owner only). If the file
   28947 ** is a database or master journal file, it is created with the permissions
   28948 ** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
   28949 **
   28950 ** Finally, if the file being opened is a WAL or regular journal file, then
   28951 ** this function queries the file-system for the permissions on the
   28952 ** corresponding database file and sets *pMode to this value. Whenever
   28953 ** possible, WAL and journal files are created using the same permissions
   28954 ** as the associated database file.
   28955 */
   28956 static int findCreateFileMode(
   28957   const char *zPath,              /* Path of file (possibly) being created */
   28958   int flags,                      /* Flags passed as 4th argument to xOpen() */
   28959   mode_t *pMode                   /* OUT: Permissions to open file with */
   28960 ){
   28961   int rc = SQLITE_OK;             /* Return Code */
   28962   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
   28963     char zDb[MAX_PATHNAME+1];     /* Database file path */
   28964     int nDb;                      /* Number of valid bytes in zDb */
   28965     struct stat sStat;            /* Output of stat() on database file */
   28966 
   28967     /* zPath is a path to a WAL or journal file. The following block derives
   28968     ** the path to the associated database file from zPath. This block handles
   28969     ** the following naming conventions:
   28970     **
   28971     **   "<path to db>-journal"
   28972     **   "<path to db>-wal"
   28973     **   "<path to db>-journal-NNNN"
   28974     **   "<path to db>-wal-NNNN"
   28975     **
   28976     ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are
   28977     ** used by the test_multiplex.c module.
   28978     */
   28979     nDb = sqlite3Strlen30(zPath) - 1;
   28980     while( nDb>0 && zPath[nDb]!='l' ) nDb--;
   28981     nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7);
   28982     memcpy(zDb, zPath, nDb);
   28983     zDb[nDb] = '\0';
   28984 
   28985     if( 0==osStat(zDb, &sStat) ){
   28986       *pMode = sStat.st_mode & 0777;
   28987     }else{
   28988       rc = SQLITE_IOERR_FSTAT;
   28989     }
   28990   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
   28991     *pMode = 0600;
   28992   }else{
   28993     *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
   28994   }
   28995   return rc;
   28996 }
   28997 
   28998 /*
   28999 ** Initializes a unixFile structure with zeros.
   29000 */
   29001 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file) {
   29002   memset(file, 0, sizeof(unixFile));
   29003 }
   29004 
   29005 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs,
   29006                                                int fd,
   29007                                                int dirfd,
   29008                                                sqlite3_file* file,
   29009                                                const char* fileName,
   29010                                                int noLock,
   29011                                                int isDelete) {
   29012   return fillInUnixFile(vfs, fd, dirfd, file, fileName, noLock, isDelete, 0);
   29013 }
   29014 
   29015 /*
   29016 ** Search for an unused file descriptor that was opened on the database file.
   29017 ** If a suitable file descriptor if found, then it is stored in *fd; otherwise,
   29018 ** *fd is not modified.
   29019 **
   29020 ** If a reusable file descriptor is not found, and a new UnixUnusedFd cannot
   29021 ** be allocated, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK is returned.
   29022 */
   29023 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file,
   29024                                               const char* fileName,
   29025                                               int flags,
   29026                                               int* fd) {
   29027   unixFile* unixSQLite3File = (unixFile*)file;
   29028   int fileType = flags & 0xFFFFFF00;
   29029   if (fileType == SQLITE_OPEN_MAIN_DB) {
   29030     UnixUnusedFd *unusedFd = findReusableFd(fileName, flags);
   29031     if (unusedFd) {
   29032       *fd = unusedFd->fd;
   29033     } else {
   29034       unusedFd = sqlite3_malloc(sizeof(*unusedFd));
   29035       if (!unusedFd) {
   29036         return SQLITE_NOMEM;
   29037       }
   29038     }
   29039     unixSQLite3File->pUnused = unusedFd;
   29040   }
   29041   return SQLITE_OK;
   29042 }
   29043 
   29044 /*
   29045 ** Marks 'fd' as the unused file descriptor for 'pFile'.
   29046 */
   29047 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file,
   29048                                                   int fd,
   29049                                                   int flags) {
   29050   unixFile* unixSQLite3File = (unixFile*)file;
   29051   if (unixSQLite3File->pUnused) {
   29052     unixSQLite3File->pUnused->fd = fd;
   29053     unixSQLite3File->pUnused->flags = flags;
   29054   }
   29055 }
   29056 
   29057 /*
   29058 ** Destroys pFile's field that keeps track of the unused file descriptor.
   29059 */
   29060 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file) {
   29061   unixFile* unixSQLite3File = (unixFile*)file;
   29062   sqlite3_free(unixSQLite3File->pUnused);
   29063 }
   29064 
   29065 /*
   29066 ** Open the file zPath.
   29067 **
   29068 ** Previously, the SQLite OS layer used three functions in place of this
   29069 ** one:
   29070 **
   29071 **     sqlite3OsOpenReadWrite();
   29072 **     sqlite3OsOpenReadOnly();
   29073 **     sqlite3OsOpenExclusive();
   29074 **
   29075 ** These calls correspond to the following combinations of flags:
   29076 **
   29077 **     ReadWrite() ->     (READWRITE | CREATE)
   29078 **     ReadOnly()  ->     (READONLY)
   29079 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
   29080 **
   29081 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
   29082 ** true, the file was configured to be automatically deleted when the
   29083 ** file handle closed. To achieve the same effect using this new
   29084 ** interface, add the DELETEONCLOSE flag to those specified above for
   29085 ** OpenExclusive().
   29086 */
   29087 static int unixOpen(
   29088   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
   29089   const char *zPath,           /* Pathname of file to be opened */
   29090   sqlite3_file *pFile,         /* The file descriptor to be filled in */
   29091   int flags,                   /* Input flags to control the opening */
   29092   int *pOutFlags               /* Output flags returned to SQLite core */
   29093 ){
   29094   unixFile *p = (unixFile *)pFile;
   29095   int fd = -1;                   /* File descriptor returned by open() */
   29096   int openFlags = 0;             /* Flags to pass to open() */
   29097   int eType = flags&0xFFFFFF00;  /* Type of file to open */
   29098   int noLock;                    /* True to omit locking primitives */
   29099   int rc = SQLITE_OK;            /* Function Return Code */
   29100 
   29101   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   29102   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   29103   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   29104   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   29105   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   29106 #if SQLITE_ENABLE_LOCKING_STYLE
   29107   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
   29108 #endif
   29109 
   29110   /* If creating a master or main-file journal, this function will open
   29111   ** a file-descriptor on the directory too. The first time unixSync()
   29112   ** is called the directory file descriptor will be fsync()ed and close()d.
   29113   */
   29114   int syncDir = (isCreate && (
   29115         eType==SQLITE_OPEN_MASTER_JOURNAL
   29116      || eType==SQLITE_OPEN_MAIN_JOURNAL
   29117      || eType==SQLITE_OPEN_WAL
   29118   ));
   29119 
   29120   /* If argument zPath is a NULL pointer, this function is required to open
   29121   ** a temporary file. Use this buffer to store the file name in.
   29122   */
   29123   char zTmpname[MAX_PATHNAME+1];
   29124   const char *zName = zPath;
   29125 
   29126   /* Check the following statements are true:
   29127   **
   29128   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
   29129   **   (b) if CREATE is set, then READWRITE must also be set, and
   29130   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   29131   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   29132   */
   29133   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   29134   assert(isCreate==0 || isReadWrite);
   29135   assert(isExclusive==0 || isCreate);
   29136   assert(isDelete==0 || isCreate);
   29137 
   29138   /* The main DB, main journal, WAL file and master journal are never
   29139   ** automatically deleted. Nor are they ever temporary files.  */
   29140   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   29141   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   29142   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   29143   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   29144 
   29145   /* Assert that the upper layer has set one of the "file-type" flags. */
   29146   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
   29147        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
   29148        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
   29149        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   29150   );
   29151 
   29152   chromium_sqlite3_initialize_unix_sqlite3_file(pFile);
   29153 
   29154   if( eType==SQLITE_OPEN_MAIN_DB ){
   29155     rc = chromium_sqlite3_get_reusable_file_handle(pFile, zName, flags, &fd);
   29156     if( rc!=SQLITE_OK ){
   29157       return rc;
   29158     }
   29159   }else if( !zName ){
   29160     /* If zName is NULL, the upper layer is requesting a temp file. */
   29161     assert(isDelete && !syncDir);
   29162     rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
   29163     if( rc!=SQLITE_OK ){
   29164       return rc;
   29165     }
   29166     zName = zTmpname;
   29167   }
   29168 
   29169   /* Determine the value of the flags parameter passed to POSIX function
   29170   ** open(). These must be calculated even if open() is not called, as
   29171   ** they may be stored as part of the file handle and used by the
   29172   ** 'conch file' locking functions later on.  */
   29173   if( isReadonly )  openFlags |= O_RDONLY;
   29174   if( isReadWrite ) openFlags |= O_RDWR;
   29175   if( isCreate )    openFlags |= O_CREAT;
   29176   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
   29177   openFlags |= (O_LARGEFILE|O_BINARY);
   29178 
   29179   if( fd<0 ){
   29180     mode_t openMode;              /* Permissions to create file with */
   29181     rc = findCreateFileMode(zName, flags, &openMode);
   29182     if( rc!=SQLITE_OK ){
   29183       assert( !p->pUnused );
   29184       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
   29185       return rc;
   29186     }
   29187     fd = robust_open(zName, openFlags, openMode);
   29188     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
   29189     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
   29190       /* Failed to open the file for read/write access. Try read-only. */
   29191       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
   29192       openFlags &= ~(O_RDWR|O_CREAT);
   29193       flags |= SQLITE_OPEN_READONLY;
   29194       openFlags |= O_RDONLY;
   29195       isReadonly = 1;
   29196       fd = robust_open(zName, openFlags, openMode);
   29197     }
   29198     if( fd<0 ){
   29199       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
   29200       goto open_finished;
   29201     }
   29202   }
   29203   assert( fd>=0 );
   29204   if( pOutFlags ){
   29205     *pOutFlags = flags;
   29206   }
   29207 
   29208   chromium_sqlite3_update_reusable_file_handle(pFile, fd, flags);
   29209 
   29210   if( isDelete ){
   29211 #if OS_VXWORKS
   29212     zPath = zName;
   29213 #else
   29214     osUnlink(zName);
   29215 #endif
   29216   }
   29217 #if SQLITE_ENABLE_LOCKING_STYLE
   29218   else{
   29219     p->openFlags = openFlags;
   29220   }
   29221 #endif
   29222 
   29223 #ifdef FD_CLOEXEC
   29224   osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
   29225 #endif
   29226 
   29227   noLock = eType!=SQLITE_OPEN_MAIN_DB;
   29228 
   29229 
   29230 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
   29231   struct statfs fsInfo;
   29232   if( fstatfs(fd, &fsInfo) == -1 ){
   29233     ((unixFile*)pFile)->lastErrno = errno;
   29234     robust_close(p, fd, __LINE__);
   29235     return SQLITE_IOERR_ACCESS;
   29236   }
   29237   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
   29238     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
   29239   }
   29240 #endif
   29241 
   29242 #if SQLITE_ENABLE_LOCKING_STYLE
   29243 #if SQLITE_PREFER_PROXY_LOCKING
   29244   isAutoProxy = 1;
   29245 #endif
   29246   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
   29247     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
   29248     int useProxy = 0;
   29249 
   29250     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
   29251     ** never use proxy, NULL means use proxy for non-local files only.  */
   29252     if( envforce!=NULL ){
   29253       useProxy = atoi(envforce)>0;
   29254     }else{
   29255       struct statfs fsInfo;
   29256       if( statfs(zPath, &fsInfo) == -1 ){
   29257         /* In theory, the close(fd) call is sub-optimal. If the file opened
   29258         ** with fd is a database file, and there are other connections open
   29259         ** on that file that are currently holding advisory locks on it,
   29260         ** then the call to close() will cancel those locks. In practice,
   29261         ** we're assuming that statfs() doesn't fail very often. At least
   29262         ** not while other file descriptors opened by the same process on
   29263         ** the same file are working.  */
   29264         p->lastErrno = errno;
   29265         robust_close(p, fd, __LINE__);
   29266         rc = SQLITE_IOERR_ACCESS;
   29267         goto open_finished;
   29268       }
   29269       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
   29270     }
   29271     if( useProxy ){
   29272       rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
   29273                           isDelete, isReadonly);
   29274       if( rc==SQLITE_OK ){
   29275         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
   29276         if( rc!=SQLITE_OK ){
   29277           /* Use unixClose to clean up the resources added in fillInUnixFile
   29278           ** and clear all the structure's references.  Specifically,
   29279           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
   29280           */
   29281           unixClose(pFile);
   29282           return rc;
   29283         }
   29284       }
   29285       goto open_finished;
   29286     }
   29287   }
   29288 #endif
   29289 
   29290   rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
   29291                       isDelete, isReadonly);
   29292 open_finished:
   29293   if( rc!=SQLITE_OK ){
   29294     chromium_sqlite3_destroy_reusable_file_handle(pFile);
   29295   }
   29296   return rc;
   29297 }
   29298 
   29299 
   29300 /*
   29301 ** Delete the file at zPath. If the dirSync argument is true, fsync()
   29302 ** the directory after deleting the file.
   29303 */
   29304 static int unixDelete(
   29305   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
   29306   const char *zPath,        /* Name of file to be deleted */
   29307   int dirSync               /* If true, fsync() directory after deleting file */
   29308 ){
   29309   int rc = SQLITE_OK;
   29310   UNUSED_PARAMETER(NotUsed);
   29311   SimulateIOError(return SQLITE_IOERR_DELETE);
   29312   if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
   29313     return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
   29314   }
   29315 #ifndef SQLITE_DISABLE_DIRSYNC
   29316   if( dirSync ){
   29317     int fd;
   29318     rc = osOpenDirectory(zPath, &fd);
   29319     if( rc==SQLITE_OK ){
   29320 #if OS_VXWORKS
   29321       if( fsync(fd)==-1 )
   29322 #else
   29323       if( fsync(fd) )
   29324 #endif
   29325       {
   29326         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
   29327       }
   29328       robust_close(0, fd, __LINE__);
   29329     }else if( rc==SQLITE_CANTOPEN ){
   29330       rc = SQLITE_OK;
   29331     }
   29332   }
   29333 #endif
   29334   return rc;
   29335 }
   29336 
   29337 /*
   29338 ** Test the existance of or access permissions of file zPath. The
   29339 ** test performed depends on the value of flags:
   29340 **
   29341 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
   29342 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
   29343 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
   29344 **
   29345 ** Otherwise return 0.
   29346 */
   29347 static int unixAccess(
   29348   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
   29349   const char *zPath,      /* Path of the file to examine */
   29350   int flags,              /* What do we want to learn about the zPath file? */
   29351   int *pResOut            /* Write result boolean here */
   29352 ){
   29353   int amode = 0;
   29354   UNUSED_PARAMETER(NotUsed);
   29355   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   29356   switch( flags ){
   29357     case SQLITE_ACCESS_EXISTS:
   29358       amode = F_OK;
   29359       break;
   29360     case SQLITE_ACCESS_READWRITE:
   29361       amode = W_OK|R_OK;
   29362       break;
   29363     case SQLITE_ACCESS_READ:
   29364       amode = R_OK;
   29365       break;
   29366 
   29367     default:
   29368       assert(!"Invalid flags argument");
   29369   }
   29370   *pResOut = (osAccess(zPath, amode)==0);
   29371   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
   29372     struct stat buf;
   29373     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
   29374       *pResOut = 0;
   29375     }
   29376   }
   29377   return SQLITE_OK;
   29378 }
   29379 
   29380 
   29381 /*
   29382 ** Turn a relative pathname into a full pathname. The relative path
   29383 ** is stored as a nul-terminated string in the buffer pointed to by
   29384 ** zPath.
   29385 **
   29386 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
   29387 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
   29388 ** this buffer before returning.
   29389 */
   29390 static int unixFullPathname(
   29391   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
   29392   const char *zPath,            /* Possibly relative input path */
   29393   int nOut,                     /* Size of output buffer in bytes */
   29394   char *zOut                    /* Output buffer */
   29395 ){
   29396 
   29397   /* It's odd to simulate an io-error here, but really this is just
   29398   ** using the io-error infrastructure to test that SQLite handles this
   29399   ** function failing. This function could fail if, for example, the
   29400   ** current working directory has been unlinked.
   29401   */
   29402   SimulateIOError( return SQLITE_ERROR );
   29403 
   29404   assert( pVfs->mxPathname==MAX_PATHNAME );
   29405   UNUSED_PARAMETER(pVfs);
   29406 
   29407   zOut[nOut-1] = '\0';
   29408   if( zPath[0]=='/' ){
   29409     sqlite3_snprintf(nOut, zOut, "%s", zPath);
   29410   }else{
   29411     int nCwd;
   29412     if( osGetcwd(zOut, nOut-1)==0 ){
   29413       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
   29414     }
   29415     nCwd = (int)strlen(zOut);
   29416     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
   29417   }
   29418   return SQLITE_OK;
   29419 }
   29420 
   29421 
   29422 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   29423 /*
   29424 ** Interfaces for opening a shared library, finding entry points
   29425 ** within the shared library, and closing the shared library.
   29426 */
   29427 #include <dlfcn.h>
   29428 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
   29429   UNUSED_PARAMETER(NotUsed);
   29430   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
   29431 }
   29432 
   29433 /*
   29434 ** SQLite calls this function immediately after a call to unixDlSym() or
   29435 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
   29436 ** message is available, it is written to zBufOut. If no error message
   29437 ** is available, zBufOut is left unmodified and SQLite uses a default
   29438 ** error message.
   29439 */
   29440 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
   29441   const char *zErr;
   29442   UNUSED_PARAMETER(NotUsed);
   29443   unixEnterMutex();
   29444   zErr = dlerror();
   29445   if( zErr ){
   29446     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
   29447   }
   29448   unixLeaveMutex();
   29449 }
   29450 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
   29451   /*
   29452   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
   29453   ** cast into a pointer to a function.  And yet the library dlsym() routine
   29454   ** returns a void* which is really a pointer to a function.  So how do we
   29455   ** use dlsym() with -pedantic-errors?
   29456   **
   29457   ** Variable x below is defined to be a pointer to a function taking
   29458   ** parameters void* and const char* and returning a pointer to a function.
   29459   ** We initialize x by assigning it a pointer to the dlsym() function.
   29460   ** (That assignment requires a cast.)  Then we call the function that
   29461   ** x points to.
   29462   **
   29463   ** This work-around is unlikely to work correctly on any system where
   29464   ** you really cannot cast a function pointer into void*.  But then, on the
   29465   ** other hand, dlsym() will not work on such a system either, so we have
   29466   ** not really lost anything.
   29467   */
   29468   void (*(*x)(void*,const char*))(void);
   29469   UNUSED_PARAMETER(NotUsed);
   29470   x = (void(*(*)(void*,const char*))(void))dlsym;
   29471   return (*x)(p, zSym);
   29472 }
   29473 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
   29474   UNUSED_PARAMETER(NotUsed);
   29475   dlclose(pHandle);
   29476 }
   29477 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   29478   #define unixDlOpen  0
   29479   #define unixDlError 0
   29480   #define unixDlSym   0
   29481   #define unixDlClose 0
   29482 #endif
   29483 
   29484 /*
   29485 ** Write nBuf bytes of random data to the supplied buffer zBuf.
   29486 */
   29487 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
   29488   UNUSED_PARAMETER(NotUsed);
   29489   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
   29490 
   29491   /* We have to initialize zBuf to prevent valgrind from reporting
   29492   ** errors.  The reports issued by valgrind are incorrect - we would
   29493   ** prefer that the randomness be increased by making use of the
   29494   ** uninitialized space in zBuf - but valgrind errors tend to worry
   29495   ** some users.  Rather than argue, it seems easier just to initialize
   29496   ** the whole array and silence valgrind, even if that means less randomness
   29497   ** in the random seed.
   29498   **
   29499   ** When testing, initializing zBuf[] to zero is all we do.  That means
   29500   ** that we always use the same random number sequence.  This makes the
   29501   ** tests repeatable.
   29502   */
   29503   memset(zBuf, 0, nBuf);
   29504 #if !defined(SQLITE_TEST)
   29505   {
   29506     int pid, fd;
   29507     fd = robust_open("/dev/urandom", O_RDONLY, 0);
   29508     if( fd<0 ){
   29509       time_t t;
   29510       time(&t);
   29511       memcpy(zBuf, &t, sizeof(t));
   29512       pid = getpid();
   29513       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
   29514       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
   29515       nBuf = sizeof(t) + sizeof(pid);
   29516     }else{
   29517       do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
   29518       robust_close(0, fd, __LINE__);
   29519     }
   29520   }
   29521 #endif
   29522   return nBuf;
   29523 }
   29524 
   29525 
   29526 /*
   29527 ** Sleep for a little while.  Return the amount of time slept.
   29528 ** The argument is the number of microseconds we want to sleep.
   29529 ** The return value is the number of microseconds of sleep actually
   29530 ** requested from the underlying operating system, a number which
   29531 ** might be greater than or equal to the argument, but not less
   29532 ** than the argument.
   29533 */
   29534 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
   29535 #if OS_VXWORKS
   29536   struct timespec sp;
   29537 
   29538   sp.tv_sec = microseconds / 1000000;
   29539   sp.tv_nsec = (microseconds % 1000000) * 1000;
   29540   nanosleep(&sp, NULL);
   29541   UNUSED_PARAMETER(NotUsed);
   29542   return microseconds;
   29543 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
   29544   usleep(microseconds);
   29545   UNUSED_PARAMETER(NotUsed);
   29546   return microseconds;
   29547 #else
   29548   int seconds = (microseconds+999999)/1000000;
   29549   sleep(seconds);
   29550   UNUSED_PARAMETER(NotUsed);
   29551   return seconds*1000000;
   29552 #endif
   29553 }
   29554 
   29555 /*
   29556 ** The following variable, if set to a non-zero value, is interpreted as
   29557 ** the number of seconds since 1970 and is used to set the result of
   29558 ** sqlite3OsCurrentTime() during testing.
   29559 */
   29560 #ifdef SQLITE_TEST
   29561 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
   29562 #endif
   29563 
   29564 /*
   29565 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   29566 ** the current time and date as a Julian Day number times 86_400_000.  In
   29567 ** other words, write into *piNow the number of milliseconds since the Julian
   29568 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   29569 ** proleptic Gregorian calendar.
   29570 **
   29571 ** On success, return 0.  Return 1 if the time and date cannot be found.
   29572 */
   29573 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
   29574   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   29575 #if defined(NO_GETTOD)
   29576   time_t t;
   29577   time(&t);
   29578   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
   29579 #elif OS_VXWORKS
   29580   struct timespec sNow;
   29581   clock_gettime(CLOCK_REALTIME, &sNow);
   29582   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
   29583 #else
   29584   struct timeval sNow;
   29585   gettimeofday(&sNow, 0);
   29586   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
   29587 #endif
   29588 
   29589 #ifdef SQLITE_TEST
   29590   if( sqlite3_current_time ){
   29591     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   29592   }
   29593 #endif
   29594   UNUSED_PARAMETER(NotUsed);
   29595   return 0;
   29596 }
   29597 
   29598 /*
   29599 ** Find the current time (in Universal Coordinated Time).  Write the
   29600 ** current time and date as a Julian Day number into *prNow and
   29601 ** return 0.  Return 1 if the time and date cannot be found.
   29602 */
   29603 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
   29604   sqlite3_int64 i;
   29605   UNUSED_PARAMETER(NotUsed);
   29606   unixCurrentTimeInt64(0, &i);
   29607   *prNow = i/86400000.0;
   29608   return 0;
   29609 }
   29610 
   29611 /*
   29612 ** We added the xGetLastError() method with the intention of providing
   29613 ** better low-level error messages when operating-system problems come up
   29614 ** during SQLite operation.  But so far, none of that has been implemented
   29615 ** in the core.  So this routine is never called.  For now, it is merely
   29616 ** a place-holder.
   29617 */
   29618 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
   29619   UNUSED_PARAMETER(NotUsed);
   29620   UNUSED_PARAMETER(NotUsed2);
   29621   UNUSED_PARAMETER(NotUsed3);
   29622   return 0;
   29623 }
   29624 
   29625 
   29626 /*
   29627 ************************ End of sqlite3_vfs methods ***************************
   29628 ******************************************************************************/
   29629 
   29630 /******************************************************************************
   29631 ************************** Begin Proxy Locking ********************************
   29632 **
   29633 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
   29634 ** other locking methods on secondary lock files.  Proxy locking is a
   29635 ** meta-layer over top of the primitive locking implemented above.  For
   29636 ** this reason, the division that implements of proxy locking is deferred
   29637 ** until late in the file (here) after all of the other I/O methods have
   29638 ** been defined - so that the primitive locking methods are available
   29639 ** as services to help with the implementation of proxy locking.
   29640 **
   29641 ****
   29642 **
   29643 ** The default locking schemes in SQLite use byte-range locks on the
   29644 ** database file to coordinate safe, concurrent access by multiple readers
   29645 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
   29646 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
   29647 ** as POSIX read & write locks over fixed set of locations (via fsctl),
   29648 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
   29649 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
   29650 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
   29651 ** address in the shared range is taken for a SHARED lock, the entire
   29652 ** shared range is taken for an EXCLUSIVE lock):
   29653 **
   29654 **      PENDING_BYTE        0x40000000
   29655 **      RESERVED_BYTE       0x40000001
   29656 **      SHARED_RANGE        0x40000002 -> 0x40000200
   29657 **
   29658 ** This works well on the local file system, but shows a nearly 100x
   29659 ** slowdown in read performance on AFP because the AFP client disables
   29660 ** the read cache when byte-range locks are present.  Enabling the read
   29661 ** cache exposes a cache coherency problem that is present on all OS X
   29662 ** supported network file systems.  NFS and AFP both observe the
   29663 ** close-to-open semantics for ensuring cache coherency
   29664 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
   29665 ** address the requirements for concurrent database access by multiple
   29666 ** readers and writers
   29667 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
   29668 **
   29669 ** To address the performance and cache coherency issues, proxy file locking
   29670 ** changes the way database access is controlled by limiting access to a
   29671 ** single host at a time and moving file locks off of the database file
   29672 ** and onto a proxy file on the local file system.
   29673 **
   29674 **
   29675 ** Using proxy locks
   29676 ** -----------------
   29677 **
   29678 ** C APIs
   29679 **
   29680 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
   29681 **                       <proxy_path> | ":auto:");
   29682 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
   29683 **
   29684 **
   29685 ** SQL pragmas
   29686 **
   29687 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
   29688 **  PRAGMA [database.]lock_proxy_file
   29689 **
   29690 ** Specifying ":auto:" means that if there is a conch file with a matching
   29691 ** host ID in it, the proxy path in the conch file will be used, otherwise
   29692 ** a proxy path based on the user's temp dir
   29693 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
   29694 ** actual proxy file name is generated from the name and path of the
   29695 ** database file.  For example:
   29696 **
   29697 **       For database path "/Users/me/foo.db"
   29698 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
   29699 **
   29700 ** Once a lock proxy is configured for a database connection, it can not
   29701 ** be removed, however it may be switched to a different proxy path via
   29702 ** the above APIs (assuming the conch file is not being held by another
   29703 ** connection or process).
   29704 **
   29705 **
   29706 ** How proxy locking works
   29707 ** -----------------------
   29708 **
   29709 ** Proxy file locking relies primarily on two new supporting files:
   29710 **
   29711 **   *  conch file to limit access to the database file to a single host
   29712 **      at a time
   29713 **
   29714 **   *  proxy file to act as a proxy for the advisory locks normally
   29715 **      taken on the database
   29716 **
   29717 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
   29718 ** by taking an sqlite-style shared lock on the conch file, reading the
   29719 ** contents and comparing the host's unique host ID (see below) and lock
   29720 ** proxy path against the values stored in the conch.  The conch file is
   29721 ** stored in the same directory as the database file and the file name
   29722 ** is patterned after the database file name as ".<databasename>-conch".
   29723 ** If the conch file does not exist, or it's contents do not match the
   29724 ** host ID and/or proxy path, then the lock is escalated to an exclusive
   29725 ** lock and the conch file contents is updated with the host ID and proxy
   29726 ** path and the lock is downgraded to a shared lock again.  If the conch
   29727 ** is held by another process (with a shared lock), the exclusive lock
   29728 ** will fail and SQLITE_BUSY is returned.
   29729 **
   29730 ** The proxy file - a single-byte file used for all advisory file locks
   29731 ** normally taken on the database file.   This allows for safe sharing
   29732 ** of the database file for multiple readers and writers on the same
   29733 ** host (the conch ensures that they all use the same local lock file).
   29734 **
   29735 ** Requesting the lock proxy does not immediately take the conch, it is
   29736 ** only taken when the first request to lock database file is made.
   29737 ** This matches the semantics of the traditional locking behavior, where
   29738 ** opening a connection to a database file does not take a lock on it.
   29739 ** The shared lock and an open file descriptor are maintained until
   29740 ** the connection to the database is closed.
   29741 **
   29742 ** The proxy file and the lock file are never deleted so they only need
   29743 ** to be created the first time they are used.
   29744 **
   29745 ** Configuration options
   29746 ** ---------------------
   29747 **
   29748 **  SQLITE_PREFER_PROXY_LOCKING
   29749 **
   29750 **       Database files accessed on non-local file systems are
   29751 **       automatically configured for proxy locking, lock files are
   29752 **       named automatically using the same logic as
   29753 **       PRAGMA lock_proxy_file=":auto:"
   29754 **
   29755 **  SQLITE_PROXY_DEBUG
   29756 **
   29757 **       Enables the logging of error messages during host id file
   29758 **       retrieval and creation
   29759 **
   29760 **  LOCKPROXYDIR
   29761 **
   29762 **       Overrides the default directory used for lock proxy files that
   29763 **       are named automatically via the ":auto:" setting
   29764 **
   29765 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
   29766 **
   29767 **       Permissions to use when creating a directory for storing the
   29768 **       lock proxy files, only used when LOCKPROXYDIR is not set.
   29769 **
   29770 **
   29771 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
   29772 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
   29773 ** force proxy locking to be used for every database file opened, and 0
   29774 ** will force automatic proxy locking to be disabled for all database
   29775 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
   29776 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
   29777 */
   29778 
   29779 /*
   29780 ** Proxy locking is only available on MacOSX
   29781 */
   29782 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29783 
   29784 /*
   29785 ** The proxyLockingContext has the path and file structures for the remote
   29786 ** and local proxy files in it
   29787 */
   29788 typedef struct proxyLockingContext proxyLockingContext;
   29789 struct proxyLockingContext {
   29790   unixFile *conchFile;         /* Open conch file */
   29791   char *conchFilePath;         /* Name of the conch file */
   29792   unixFile *lockProxy;         /* Open proxy lock file */
   29793   char *lockProxyPath;         /* Name of the proxy lock file */
   29794   char *dbPath;                /* Name of the open file */
   29795   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
   29796   void *oldLockingContext;     /* Original lockingcontext to restore on close */
   29797   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
   29798 };
   29799 
   29800 /*
   29801 ** The proxy lock file path for the database at dbPath is written into lPath,
   29802 ** which must point to valid, writable memory large enough for a maxLen length
   29803 ** file path.
   29804 */
   29805 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
   29806   int len;
   29807   int dbLen;
   29808   int i;
   29809 
   29810 #ifdef LOCKPROXYDIR
   29811   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
   29812 #else
   29813 # ifdef _CS_DARWIN_USER_TEMP_DIR
   29814   {
   29815     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
   29816       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
   29817                lPath, errno, getpid()));
   29818       return SQLITE_IOERR_LOCK;
   29819     }
   29820     len = strlcat(lPath, "sqliteplocks", maxLen);
   29821   }
   29822 # else
   29823   len = strlcpy(lPath, "/tmp/", maxLen);
   29824 # endif
   29825 #endif
   29826 
   29827   if( lPath[len-1]!='/' ){
   29828     len = strlcat(lPath, "/", maxLen);
   29829   }
   29830 
   29831   /* transform the db path to a unique cache name */
   29832   dbLen = (int)strlen(dbPath);
   29833   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
   29834     char c = dbPath[i];
   29835     lPath[i+len] = (c=='/')?'_':c;
   29836   }
   29837   lPath[i+len]='\0';
   29838   strlcat(lPath, ":auto:", maxLen);
   29839   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
   29840   return SQLITE_OK;
   29841 }
   29842 
   29843 /*
   29844  ** Creates the lock file and any missing directories in lockPath
   29845  */
   29846 static int proxyCreateLockPath(const char *lockPath){
   29847   int i, len;
   29848   char buf[MAXPATHLEN];
   29849   int start = 0;
   29850 
   29851   assert(lockPath!=NULL);
   29852   /* try to create all the intermediate directories */
   29853   len = (int)strlen(lockPath);
   29854   buf[0] = lockPath[0];
   29855   for( i=1; i<len; i++ ){
   29856     if( lockPath[i] == '/' && (i - start > 0) ){
   29857       /* only mkdir if leaf dir != "." or "/" or ".." */
   29858       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
   29859          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
   29860         buf[i]='\0';
   29861         if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
   29862           int err=errno;
   29863           if( err!=EEXIST ) {
   29864             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
   29865                      "'%s' proxy lock path=%s pid=%d\n",
   29866                      buf, strerror(err), lockPath, getpid()));
   29867             return err;
   29868           }
   29869         }
   29870       }
   29871       start=i+1;
   29872     }
   29873     buf[i] = lockPath[i];
   29874   }
   29875   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
   29876   return 0;
   29877 }
   29878 
   29879 /*
   29880 ** Create a new VFS file descriptor (stored in memory obtained from
   29881 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
   29882 **
   29883 ** The caller is responsible not only for closing the file descriptor
   29884 ** but also for freeing the memory associated with the file descriptor.
   29885 */
   29886 static int proxyCreateUnixFile(
   29887     const char *path,        /* path for the new unixFile */
   29888     unixFile **ppFile,       /* unixFile created and returned by ref */
   29889     int islockfile           /* if non zero missing dirs will be created */
   29890 ) {
   29891   int fd = -1;
   29892   unixFile *pNew;
   29893   int rc = SQLITE_OK;
   29894   int openFlags = O_RDWR | O_CREAT;
   29895   sqlite3_vfs dummyVfs;
   29896   int terrno = 0;
   29897   UnixUnusedFd *pUnused = NULL;
   29898 
   29899   /* 1. first try to open/create the file
   29900   ** 2. if that fails, and this is a lock file (not-conch), try creating
   29901   ** the parent directories and then try again.
   29902   ** 3. if that fails, try to open the file read-only
   29903   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
   29904   */
   29905   pUnused = findReusableFd(path, openFlags);
   29906   if( pUnused ){
   29907     fd = pUnused->fd;
   29908   }else{
   29909     pUnused = sqlite3_malloc(sizeof(*pUnused));
   29910     if( !pUnused ){
   29911       return SQLITE_NOMEM;
   29912     }
   29913   }
   29914   if( fd<0 ){
   29915     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   29916     terrno = errno;
   29917     if( fd<0 && errno==ENOENT && islockfile ){
   29918       if( proxyCreateLockPath(path) == SQLITE_OK ){
   29919         fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   29920       }
   29921     }
   29922   }
   29923   if( fd<0 ){
   29924     openFlags = O_RDONLY;
   29925     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   29926     terrno = errno;
   29927   }
   29928   if( fd<0 ){
   29929     if( islockfile ){
   29930       return SQLITE_BUSY;
   29931     }
   29932     switch (terrno) {
   29933       case EACCES:
   29934         return SQLITE_PERM;
   29935       case EIO:
   29936         return SQLITE_IOERR_LOCK; /* even though it is the conch */
   29937       default:
   29938         return SQLITE_CANTOPEN_BKPT;
   29939     }
   29940   }
   29941 
   29942   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
   29943   if( pNew==NULL ){
   29944     rc = SQLITE_NOMEM;
   29945     goto end_create_proxy;
   29946   }
   29947   memset(pNew, 0, sizeof(unixFile));
   29948   pNew->openFlags = openFlags;
   29949   memset(&dummyVfs, 0, sizeof(dummyVfs));
   29950   dummyVfs.pAppData = (void*)&autolockIoFinder;
   29951   dummyVfs.zName = "dummy";
   29952   pUnused->fd = fd;
   29953   pUnused->flags = openFlags;
   29954   pNew->pUnused = pUnused;
   29955 
   29956   rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
   29957   if( rc==SQLITE_OK ){
   29958     *ppFile = pNew;
   29959     return SQLITE_OK;
   29960   }
   29961 end_create_proxy:
   29962   robust_close(pNew, fd, __LINE__);
   29963   sqlite3_free(pNew);
   29964   sqlite3_free(pUnused);
   29965   return rc;
   29966 }
   29967 
   29968 #ifdef SQLITE_TEST
   29969 /* simulate multiple hosts by creating unique hostid file paths */
   29970 SQLITE_API int sqlite3_hostid_num = 0;
   29971 #endif
   29972 
   29973 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
   29974 
   29975 /* Not always defined in the headers as it ought to be */
   29976 extern int gethostuuid(uuid_t id, const struct timespec *wait);
   29977 
   29978 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
   29979 ** bytes of writable memory.
   29980 */
   29981 static int proxyGetHostID(unsigned char *pHostID, int *pError){
   29982   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
   29983   memset(pHostID, 0, PROXY_HOSTIDLEN);
   29984 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
   29985                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
   29986   {
   29987     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
   29988     if( gethostuuid(pHostID, &timeout) ){
   29989       int err = errno;
   29990       if( pError ){
   29991         *pError = err;
   29992       }
   29993       return SQLITE_IOERR;
   29994     }
   29995   }
   29996 #endif
   29997 #ifdef SQLITE_TEST
   29998   /* simulate multiple hosts by creating unique hostid file paths */
   29999   if( sqlite3_hostid_num != 0){
   30000     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
   30001   }
   30002 #endif
   30003 
   30004   return SQLITE_OK;
   30005 }
   30006 
   30007 /* The conch file contains the header, host id and lock file path
   30008  */
   30009 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
   30010 #define PROXY_HEADERLEN    1   /* conch file header length */
   30011 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
   30012 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
   30013 
   30014 /*
   30015 ** Takes an open conch file, copies the contents to a new path and then moves
   30016 ** it back.  The newly created file's file descriptor is assigned to the
   30017 ** conch file structure and finally the original conch file descriptor is
   30018 ** closed.  Returns zero if successful.
   30019 */
   30020 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
   30021   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30022   unixFile *conchFile = pCtx->conchFile;
   30023   char tPath[MAXPATHLEN];
   30024   char buf[PROXY_MAXCONCHLEN];
   30025   char *cPath = pCtx->conchFilePath;
   30026   size_t readLen = 0;
   30027   size_t pathLen = 0;
   30028   char errmsg[64] = "";
   30029   int fd = -1;
   30030   int rc = -1;
   30031   UNUSED_PARAMETER(myHostID);
   30032 
   30033   /* create a new path by replace the trailing '-conch' with '-break' */
   30034   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
   30035   if( pathLen>MAXPATHLEN || pathLen<6 ||
   30036      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
   30037     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
   30038     goto end_breaklock;
   30039   }
   30040   /* read the conch content */
   30041   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
   30042   if( readLen<PROXY_PATHINDEX ){
   30043     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
   30044     goto end_breaklock;
   30045   }
   30046   /* write it out to the temporary break file */
   30047   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
   30048                    SQLITE_DEFAULT_FILE_PERMISSIONS);
   30049   if( fd<0 ){
   30050     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
   30051     goto end_breaklock;
   30052   }
   30053   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
   30054     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
   30055     goto end_breaklock;
   30056   }
   30057   if( rename(tPath, cPath) ){
   30058     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
   30059     goto end_breaklock;
   30060   }
   30061   rc = 0;
   30062   fprintf(stderr, "broke stale lock on %s\n", cPath);
   30063   robust_close(pFile, conchFile->h, __LINE__);
   30064   conchFile->h = fd;
   30065   conchFile->openFlags = O_RDWR | O_CREAT;
   30066 
   30067 end_breaklock:
   30068   if( rc ){
   30069     if( fd>=0 ){
   30070       osUnlink(tPath);
   30071       robust_close(pFile, fd, __LINE__);
   30072     }
   30073     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
   30074   }
   30075   return rc;
   30076 }
   30077 
   30078 /* Take the requested lock on the conch file and break a stale lock if the
   30079 ** host id matches.
   30080 */
   30081 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
   30082   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30083   unixFile *conchFile = pCtx->conchFile;
   30084   int rc = SQLITE_OK;
   30085   int nTries = 0;
   30086   struct timespec conchModTime;
   30087 
   30088   do {
   30089     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
   30090     nTries ++;
   30091     if( rc==SQLITE_BUSY ){
   30092       /* If the lock failed (busy):
   30093        * 1st try: get the mod time of the conch, wait 0.5s and try again.
   30094        * 2nd try: fail if the mod time changed or host id is different, wait
   30095        *           10 sec and try again
   30096        * 3rd try: break the lock unless the mod time has changed.
   30097        */
   30098       struct stat buf;
   30099       if( osFstat(conchFile->h, &buf) ){
   30100         pFile->lastErrno = errno;
   30101         return SQLITE_IOERR_LOCK;
   30102       }
   30103 
   30104       if( nTries==1 ){
   30105         conchModTime = buf.st_mtimespec;
   30106         usleep(500000); /* wait 0.5 sec and try the lock again*/
   30107         continue;
   30108       }
   30109 
   30110       assert( nTries>1 );
   30111       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
   30112          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
   30113         return SQLITE_BUSY;
   30114       }
   30115 
   30116       if( nTries==2 ){
   30117         char tBuf[PROXY_MAXCONCHLEN];
   30118         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
   30119         if( len<0 ){
   30120           pFile->lastErrno = errno;
   30121           return SQLITE_IOERR_LOCK;
   30122         }
   30123         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
   30124           /* don't break the lock if the host id doesn't match */
   30125           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
   30126             return SQLITE_BUSY;
   30127           }
   30128         }else{
   30129           /* don't break the lock on short read or a version mismatch */
   30130           return SQLITE_BUSY;
   30131         }
   30132         usleep(10000000); /* wait 10 sec and try the lock again */
   30133         continue;
   30134       }
   30135 
   30136       assert( nTries==3 );
   30137       if( 0==proxyBreakConchLock(pFile, myHostID) ){
   30138         rc = SQLITE_OK;
   30139         if( lockType==EXCLUSIVE_LOCK ){
   30140           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
   30141         }
   30142         if( !rc ){
   30143           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
   30144         }
   30145       }
   30146     }
   30147   } while( rc==SQLITE_BUSY && nTries<3 );
   30148 
   30149   return rc;
   30150 }
   30151 
   30152 /* Takes the conch by taking a shared lock and read the contents conch, if
   30153 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
   30154 ** lockPath means that the lockPath in the conch file will be used if the
   30155 ** host IDs match, or a new lock path will be generated automatically
   30156 ** and written to the conch file.
   30157 */
   30158 static int proxyTakeConch(unixFile *pFile){
   30159   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30160 
   30161   if( pCtx->conchHeld!=0 ){
   30162     return SQLITE_OK;
   30163   }else{
   30164     unixFile *conchFile = pCtx->conchFile;
   30165     uuid_t myHostID;
   30166     int pError = 0;
   30167     char readBuf[PROXY_MAXCONCHLEN];
   30168     char lockPath[MAXPATHLEN];
   30169     char *tempLockPath = NULL;
   30170     int rc = SQLITE_OK;
   30171     int createConch = 0;
   30172     int hostIdMatch = 0;
   30173     int readLen = 0;
   30174     int tryOldLockPath = 0;
   30175     int forceNewLockPath = 0;
   30176 
   30177     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
   30178              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
   30179 
   30180     rc = proxyGetHostID(myHostID, &pError);
   30181     if( (rc&0xff)==SQLITE_IOERR ){
   30182       pFile->lastErrno = pError;
   30183       goto end_takeconch;
   30184     }
   30185     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
   30186     if( rc!=SQLITE_OK ){
   30187       goto end_takeconch;
   30188     }
   30189     /* read the existing conch file */
   30190     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
   30191     if( readLen<0 ){
   30192       /* I/O error: lastErrno set by seekAndRead */
   30193       pFile->lastErrno = conchFile->lastErrno;
   30194       rc = SQLITE_IOERR_READ;
   30195       goto end_takeconch;
   30196     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
   30197              readBuf[0]!=(char)PROXY_CONCHVERSION ){
   30198       /* a short read or version format mismatch means we need to create a new
   30199       ** conch file.
   30200       */
   30201       createConch = 1;
   30202     }
   30203     /* if the host id matches and the lock path already exists in the conch
   30204     ** we'll try to use the path there, if we can't open that path, we'll
   30205     ** retry with a new auto-generated path
   30206     */
   30207     do { /* in case we need to try again for an :auto: named lock file */
   30208 
   30209       if( !createConch && !forceNewLockPath ){
   30210         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
   30211                                   PROXY_HOSTIDLEN);
   30212         /* if the conch has data compare the contents */
   30213         if( !pCtx->lockProxyPath ){
   30214           /* for auto-named local lock file, just check the host ID and we'll
   30215            ** use the local lock file path that's already in there
   30216            */
   30217           if( hostIdMatch ){
   30218             size_t pathLen = (readLen - PROXY_PATHINDEX);
   30219 
   30220             if( pathLen>=MAXPATHLEN ){
   30221               pathLen=MAXPATHLEN-1;
   30222             }
   30223             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
   30224             lockPath[pathLen] = 0;
   30225             tempLockPath = lockPath;
   30226             tryOldLockPath = 1;
   30227             /* create a copy of the lock path if the conch is taken */
   30228             goto end_takeconch;
   30229           }
   30230         }else if( hostIdMatch
   30231                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
   30232                            readLen-PROXY_PATHINDEX)
   30233         ){
   30234           /* conch host and lock path match */
   30235           goto end_takeconch;
   30236         }
   30237       }
   30238 
   30239       /* if the conch isn't writable and doesn't match, we can't take it */
   30240       if( (conchFile->openFlags&O_RDWR) == 0 ){
   30241         rc = SQLITE_BUSY;
   30242         goto end_takeconch;
   30243       }
   30244 
   30245       /* either the conch didn't match or we need to create a new one */
   30246       if( !pCtx->lockProxyPath ){
   30247         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
   30248         tempLockPath = lockPath;
   30249         /* create a copy of the lock path _only_ if the conch is taken */
   30250       }
   30251 
   30252       /* update conch with host and path (this will fail if other process
   30253       ** has a shared lock already), if the host id matches, use the big
   30254       ** stick.
   30255       */
   30256       futimes(conchFile->h, NULL);
   30257       if( hostIdMatch && !createConch ){
   30258         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
   30259           /* We are trying for an exclusive lock but another thread in this
   30260            ** same process is still holding a shared lock. */
   30261           rc = SQLITE_BUSY;
   30262         } else {
   30263           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
   30264         }
   30265       }else{
   30266         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
   30267       }
   30268       if( rc==SQLITE_OK ){
   30269         char writeBuffer[PROXY_MAXCONCHLEN];
   30270         int writeSize = 0;
   30271 
   30272         writeBuffer[0] = (char)PROXY_CONCHVERSION;
   30273         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
   30274         if( pCtx->lockProxyPath!=NULL ){
   30275           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
   30276         }else{
   30277           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
   30278         }
   30279         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
   30280         robust_ftruncate(conchFile->h, writeSize);
   30281         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
   30282         fsync(conchFile->h);
   30283         /* If we created a new conch file (not just updated the contents of a
   30284          ** valid conch file), try to match the permissions of the database
   30285          */
   30286         if( rc==SQLITE_OK && createConch ){
   30287           struct stat buf;
   30288           int err = osFstat(pFile->h, &buf);
   30289           if( err==0 ){
   30290             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
   30291                                         S_IROTH|S_IWOTH);
   30292             /* try to match the database file R/W permissions, ignore failure */
   30293 #ifndef SQLITE_PROXY_DEBUG
   30294             osFchmod(conchFile->h, cmode);
   30295 #else
   30296             do{
   30297               rc = osFchmod(conchFile->h, cmode);
   30298             }while( rc==(-1) && errno==EINTR );
   30299             if( rc!=0 ){
   30300               int code = errno;
   30301               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
   30302                       cmode, code, strerror(code));
   30303             } else {
   30304               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
   30305             }
   30306           }else{
   30307             int code = errno;
   30308             fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
   30309                     err, code, strerror(code));
   30310 #endif
   30311           }
   30312         }
   30313       }
   30314       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
   30315 
   30316     end_takeconch:
   30317       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
   30318       if( rc==SQLITE_OK && pFile->openFlags ){
   30319         if( pFile->h>=0 ){
   30320           robust_close(pFile, pFile->h, __LINE__);
   30321         }
   30322         pFile->h = -1;
   30323         int fd = robust_open(pCtx->dbPath, pFile->openFlags,
   30324                       SQLITE_DEFAULT_FILE_PERMISSIONS);
   30325         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
   30326         if( fd>=0 ){
   30327           pFile->h = fd;
   30328         }else{
   30329           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
   30330            during locking */
   30331         }
   30332       }
   30333       if( rc==SQLITE_OK && !pCtx->lockProxy ){
   30334         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
   30335         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
   30336         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
   30337           /* we couldn't create the proxy lock file with the old lock file path
   30338            ** so try again via auto-naming
   30339            */
   30340           forceNewLockPath = 1;
   30341           tryOldLockPath = 0;
   30342           continue; /* go back to the do {} while start point, try again */
   30343         }
   30344       }
   30345       if( rc==SQLITE_OK ){
   30346         /* Need to make a copy of path if we extracted the value
   30347          ** from the conch file or the path was allocated on the stack
   30348          */
   30349         if( tempLockPath ){
   30350           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
   30351           if( !pCtx->lockProxyPath ){
   30352             rc = SQLITE_NOMEM;
   30353           }
   30354         }
   30355       }
   30356       if( rc==SQLITE_OK ){
   30357         pCtx->conchHeld = 1;
   30358 
   30359         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
   30360           afpLockingContext *afpCtx;
   30361           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
   30362           afpCtx->dbPath = pCtx->lockProxyPath;
   30363         }
   30364       } else {
   30365         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
   30366       }
   30367       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
   30368                rc==SQLITE_OK?"ok":"failed"));
   30369       return rc;
   30370     } while (1); /* in case we need to retry the :auto: lock file -
   30371                  ** we should never get here except via the 'continue' call. */
   30372   }
   30373 }
   30374 
   30375 /*
   30376 ** If pFile holds a lock on a conch file, then release that lock.
   30377 */
   30378 static int proxyReleaseConch(unixFile *pFile){
   30379   int rc = SQLITE_OK;         /* Subroutine return code */
   30380   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
   30381   unixFile *conchFile;        /* Name of the conch file */
   30382 
   30383   pCtx = (proxyLockingContext *)pFile->lockingContext;
   30384   conchFile = pCtx->conchFile;
   30385   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
   30386            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
   30387            getpid()));
   30388   if( pCtx->conchHeld>0 ){
   30389     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
   30390   }
   30391   pCtx->conchHeld = 0;
   30392   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
   30393            (rc==SQLITE_OK ? "ok" : "failed")));
   30394   return rc;
   30395 }
   30396 
   30397 /*
   30398 ** Given the name of a database file, compute the name of its conch file.
   30399 ** Store the conch filename in memory obtained from sqlite3_malloc().
   30400 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
   30401 ** or SQLITE_NOMEM if unable to obtain memory.
   30402 **
   30403 ** The caller is responsible for ensuring that the allocated memory
   30404 ** space is eventually freed.
   30405 **
   30406 ** *pConchPath is set to NULL if a memory allocation error occurs.
   30407 */
   30408 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
   30409   int i;                        /* Loop counter */
   30410   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
   30411   char *conchPath;              /* buffer in which to construct conch name */
   30412 
   30413   /* Allocate space for the conch filename and initialize the name to
   30414   ** the name of the original database file. */
   30415   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
   30416   if( conchPath==0 ){
   30417     return SQLITE_NOMEM;
   30418   }
   30419   memcpy(conchPath, dbPath, len+1);
   30420 
   30421   /* now insert a "." before the last / character */
   30422   for( i=(len-1); i>=0; i-- ){
   30423     if( conchPath[i]=='/' ){
   30424       i++;
   30425       break;
   30426     }
   30427   }
   30428   conchPath[i]='.';
   30429   while ( i<len ){
   30430     conchPath[i+1]=dbPath[i];
   30431     i++;
   30432   }
   30433 
   30434   /* append the "-conch" suffix to the file */
   30435   memcpy(&conchPath[i+1], "-conch", 7);
   30436   assert( (int)strlen(conchPath) == len+7 );
   30437 
   30438   return SQLITE_OK;
   30439 }
   30440 
   30441 
   30442 /* Takes a fully configured proxy locking-style unix file and switches
   30443 ** the local lock file path
   30444 */
   30445 static int switchLockProxyPath(unixFile *pFile, const char *path) {
   30446   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
   30447   char *oldPath = pCtx->lockProxyPath;
   30448   int rc = SQLITE_OK;
   30449 
   30450   if( pFile->eFileLock!=NO_LOCK ){
   30451     return SQLITE_BUSY;
   30452   }
   30453 
   30454   /* nothing to do if the path is NULL, :auto: or matches the existing path */
   30455   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
   30456     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
   30457     return SQLITE_OK;
   30458   }else{
   30459     unixFile *lockProxy = pCtx->lockProxy;
   30460     pCtx->lockProxy=NULL;
   30461     pCtx->conchHeld = 0;
   30462     if( lockProxy!=NULL ){
   30463       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
   30464       if( rc ) return rc;
   30465       sqlite3_free(lockProxy);
   30466     }
   30467     sqlite3_free(oldPath);
   30468     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
   30469   }
   30470 
   30471   return rc;
   30472 }
   30473 
   30474 /*
   30475 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
   30476 ** is a string buffer at least MAXPATHLEN+1 characters in size.
   30477 **
   30478 ** This routine find the filename associated with pFile and writes it
   30479 ** int dbPath.
   30480 */
   30481 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
   30482 #if defined(__APPLE__)
   30483   if( pFile->pMethod == &afpIoMethods ){
   30484     /* afp style keeps a reference to the db path in the filePath field
   30485     ** of the struct */
   30486     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
   30487     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
   30488   } else
   30489 #endif
   30490   if( pFile->pMethod == &dotlockIoMethods ){
   30491     /* dot lock style uses the locking context to store the dot lock
   30492     ** file path */
   30493     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
   30494     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
   30495   }else{
   30496     /* all other styles use the locking context to store the db file path */
   30497     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
   30498     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
   30499   }
   30500   return SQLITE_OK;
   30501 }
   30502 
   30503 /*
   30504 ** Takes an already filled in unix file and alters it so all file locking
   30505 ** will be performed on the local proxy lock file.  The following fields
   30506 ** are preserved in the locking context so that they can be restored and
   30507 ** the unix structure properly cleaned up at close time:
   30508 **  ->lockingContext
   30509 **  ->pMethod
   30510 */
   30511 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
   30512   proxyLockingContext *pCtx;
   30513   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
   30514   char *lockPath=NULL;
   30515   int rc = SQLITE_OK;
   30516 
   30517   if( pFile->eFileLock!=NO_LOCK ){
   30518     return SQLITE_BUSY;
   30519   }
   30520   proxyGetDbPathForUnixFile(pFile, dbPath);
   30521   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
   30522     lockPath=NULL;
   30523   }else{
   30524     lockPath=(char *)path;
   30525   }
   30526 
   30527   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
   30528            (lockPath ? lockPath : ":auto:"), getpid()));
   30529 
   30530   pCtx = sqlite3_malloc( sizeof(*pCtx) );
   30531   if( pCtx==0 ){
   30532     return SQLITE_NOMEM;
   30533   }
   30534   memset(pCtx, 0, sizeof(*pCtx));
   30535 
   30536   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
   30537   if( rc==SQLITE_OK ){
   30538     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
   30539     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
   30540       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
   30541       ** (c) the file system is read-only, then enable no-locking access.
   30542       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
   30543       ** that openFlags will have only one of O_RDONLY or O_RDWR.
   30544       */
   30545       struct statfs fsInfo;
   30546       struct stat conchInfo;
   30547       int goLockless = 0;
   30548 
   30549       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
   30550         int err = errno;
   30551         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
   30552           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
   30553         }
   30554       }
   30555       if( goLockless ){
   30556         pCtx->conchHeld = -1; /* read only FS/ lockless */
   30557         rc = SQLITE_OK;
   30558       }
   30559     }
   30560   }
   30561   if( rc==SQLITE_OK && lockPath ){
   30562     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
   30563   }
   30564 
   30565   if( rc==SQLITE_OK ){
   30566     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
   30567     if( pCtx->dbPath==NULL ){
   30568       rc = SQLITE_NOMEM;
   30569     }
   30570   }
   30571   if( rc==SQLITE_OK ){
   30572     /* all memory is allocated, proxys are created and assigned,
   30573     ** switch the locking context and pMethod then return.
   30574     */
   30575     pCtx->oldLockingContext = pFile->lockingContext;
   30576     pFile->lockingContext = pCtx;
   30577     pCtx->pOldMethod = pFile->pMethod;
   30578     pFile->pMethod = &proxyIoMethods;
   30579   }else{
   30580     if( pCtx->conchFile ){
   30581       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
   30582       sqlite3_free(pCtx->conchFile);
   30583     }
   30584     sqlite3DbFree(0, pCtx->lockProxyPath);
   30585     sqlite3_free(pCtx->conchFilePath);
   30586     sqlite3_free(pCtx);
   30587   }
   30588   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
   30589            (rc==SQLITE_OK ? "ok" : "failed")));
   30590   return rc;
   30591 }
   30592 
   30593 
   30594 /*
   30595 ** This routine handles sqlite3_file_control() calls that are specific
   30596 ** to proxy locking.
   30597 */
   30598 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
   30599   switch( op ){
   30600     case SQLITE_GET_LOCKPROXYFILE: {
   30601       unixFile *pFile = (unixFile*)id;
   30602       if( pFile->pMethod == &proxyIoMethods ){
   30603         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
   30604         proxyTakeConch(pFile);
   30605         if( pCtx->lockProxyPath ){
   30606           *(const char **)pArg = pCtx->lockProxyPath;
   30607         }else{
   30608           *(const char **)pArg = ":auto: (not held)";
   30609         }
   30610       } else {
   30611         *(const char **)pArg = NULL;
   30612       }
   30613       return SQLITE_OK;
   30614     }
   30615     case SQLITE_SET_LOCKPROXYFILE: {
   30616       unixFile *pFile = (unixFile*)id;
   30617       int rc = SQLITE_OK;
   30618       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
   30619       if( pArg==NULL || (const char *)pArg==0 ){
   30620         if( isProxyStyle ){
   30621           /* turn off proxy locking - not supported */
   30622           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
   30623         }else{
   30624           /* turn off proxy locking - already off - NOOP */
   30625           rc = SQLITE_OK;
   30626         }
   30627       }else{
   30628         const char *proxyPath = (const char *)pArg;
   30629         if( isProxyStyle ){
   30630           proxyLockingContext *pCtx =
   30631             (proxyLockingContext*)pFile->lockingContext;
   30632           if( !strcmp(pArg, ":auto:")
   30633            || (pCtx->lockProxyPath &&
   30634                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
   30635           ){
   30636             rc = SQLITE_OK;
   30637           }else{
   30638             rc = switchLockProxyPath(pFile, proxyPath);
   30639           }
   30640         }else{
   30641           /* turn on proxy file locking */
   30642           rc = proxyTransformUnixFile(pFile, proxyPath);
   30643         }
   30644       }
   30645       return rc;
   30646     }
   30647     default: {
   30648       assert( 0 );  /* The call assures that only valid opcodes are sent */
   30649     }
   30650   }
   30651   /*NOTREACHED*/
   30652   return SQLITE_ERROR;
   30653 }
   30654 
   30655 /*
   30656 ** Within this division (the proxying locking implementation) the procedures
   30657 ** above this point are all utilities.  The lock-related methods of the
   30658 ** proxy-locking sqlite3_io_method object follow.
   30659 */
   30660 
   30661 
   30662 /*
   30663 ** This routine checks if there is a RESERVED lock held on the specified
   30664 ** file by this or any other process. If such a lock is held, set *pResOut
   30665 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   30666 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   30667 */
   30668 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
   30669   unixFile *pFile = (unixFile*)id;
   30670   int rc = proxyTakeConch(pFile);
   30671   if( rc==SQLITE_OK ){
   30672     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30673     if( pCtx->conchHeld>0 ){
   30674       unixFile *proxy = pCtx->lockProxy;
   30675       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
   30676     }else{ /* conchHeld < 0 is lockless */
   30677       pResOut=0;
   30678     }
   30679   }
   30680   return rc;
   30681 }
   30682 
   30683 /*
   30684 ** Lock the file with the lock specified by parameter eFileLock - one
   30685 ** of the following:
   30686 **
   30687 **     (1) SHARED_LOCK
   30688 **     (2) RESERVED_LOCK
   30689 **     (3) PENDING_LOCK
   30690 **     (4) EXCLUSIVE_LOCK
   30691 **
   30692 ** Sometimes when requesting one lock state, additional lock states
   30693 ** are inserted in between.  The locking might fail on one of the later
   30694 ** transitions leaving the lock state different from what it started but
   30695 ** still short of its goal.  The following chart shows the allowed
   30696 ** transitions and the inserted intermediate states:
   30697 **
   30698 **    UNLOCKED -> SHARED
   30699 **    SHARED -> RESERVED
   30700 **    SHARED -> (PENDING) -> EXCLUSIVE
   30701 **    RESERVED -> (PENDING) -> EXCLUSIVE
   30702 **    PENDING -> EXCLUSIVE
   30703 **
   30704 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   30705 ** routine to lower a locking level.
   30706 */
   30707 static int proxyLock(sqlite3_file *id, int eFileLock) {
   30708   unixFile *pFile = (unixFile*)id;
   30709   int rc = proxyTakeConch(pFile);
   30710   if( rc==SQLITE_OK ){
   30711     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30712     if( pCtx->conchHeld>0 ){
   30713       unixFile *proxy = pCtx->lockProxy;
   30714       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
   30715       pFile->eFileLock = proxy->eFileLock;
   30716     }else{
   30717       /* conchHeld < 0 is lockless */
   30718     }
   30719   }
   30720   return rc;
   30721 }
   30722 
   30723 
   30724 /*
   30725 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   30726 ** must be either NO_LOCK or SHARED_LOCK.
   30727 **
   30728 ** If the locking level of the file descriptor is already at or below
   30729 ** the requested locking level, this routine is a no-op.
   30730 */
   30731 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
   30732   unixFile *pFile = (unixFile*)id;
   30733   int rc = proxyTakeConch(pFile);
   30734   if( rc==SQLITE_OK ){
   30735     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30736     if( pCtx->conchHeld>0 ){
   30737       unixFile *proxy = pCtx->lockProxy;
   30738       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
   30739       pFile->eFileLock = proxy->eFileLock;
   30740     }else{
   30741       /* conchHeld < 0 is lockless */
   30742     }
   30743   }
   30744   return rc;
   30745 }
   30746 
   30747 /*
   30748 ** Close a file that uses proxy locks.
   30749 */
   30750 static int proxyClose(sqlite3_file *id) {
   30751   if( id ){
   30752     unixFile *pFile = (unixFile*)id;
   30753     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   30754     unixFile *lockProxy = pCtx->lockProxy;
   30755     unixFile *conchFile = pCtx->conchFile;
   30756     int rc = SQLITE_OK;
   30757 
   30758     if( lockProxy ){
   30759       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
   30760       if( rc ) return rc;
   30761       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
   30762       if( rc ) return rc;
   30763       sqlite3_free(lockProxy);
   30764       pCtx->lockProxy = 0;
   30765     }
   30766     if( conchFile ){
   30767       if( pCtx->conchHeld ){
   30768         rc = proxyReleaseConch(pFile);
   30769         if( rc ) return rc;
   30770       }
   30771       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
   30772       if( rc ) return rc;
   30773       sqlite3_free(conchFile);
   30774     }
   30775     sqlite3DbFree(0, pCtx->lockProxyPath);
   30776     sqlite3_free(pCtx->conchFilePath);
   30777     sqlite3DbFree(0, pCtx->dbPath);
   30778     /* restore the original locking context and pMethod then close it */
   30779     pFile->lockingContext = pCtx->oldLockingContext;
   30780     pFile->pMethod = pCtx->pOldMethod;
   30781     sqlite3_free(pCtx);
   30782     return pFile->pMethod->xClose(id);
   30783   }
   30784   return SQLITE_OK;
   30785 }
   30786 
   30787 
   30788 
   30789 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   30790 /*
   30791 ** The proxy locking style is intended for use with AFP filesystems.
   30792 ** And since AFP is only supported on MacOSX, the proxy locking is also
   30793 ** restricted to MacOSX.
   30794 **
   30795 **
   30796 ******************* End of the proxy lock implementation **********************
   30797 ******************************************************************************/
   30798 
   30799 /*
   30800 ** Initialize the operating system interface.
   30801 **
   30802 ** This routine registers all VFS implementations for unix-like operating
   30803 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
   30804 ** should be the only routines in this file that are visible from other
   30805 ** files.
   30806 **
   30807 ** This routine is called once during SQLite initialization and by a
   30808 ** single thread.  The memory allocation and mutex subsystems have not
   30809 ** necessarily been initialized when this routine is called, and so they
   30810 ** should not be used.
   30811 */
   30812 SQLITE_API int sqlite3_os_init(void){
   30813   /*
   30814   ** The following macro defines an initializer for an sqlite3_vfs object.
   30815   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
   30816   ** to the "finder" function.  (pAppData is a pointer to a pointer because
   30817   ** silly C90 rules prohibit a void* from being cast to a function pointer
   30818   ** and so we have to go through the intermediate pointer to avoid problems
   30819   ** when compiling with -pedantic-errors on GCC.)
   30820   **
   30821   ** The FINDER parameter to this macro is the name of the pointer to the
   30822   ** finder-function.  The finder-function returns a pointer to the
   30823   ** sqlite_io_methods object that implements the desired locking
   30824   ** behaviors.  See the division above that contains the IOMETHODS
   30825   ** macro for addition information on finder-functions.
   30826   **
   30827   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
   30828   ** object.  But the "autolockIoFinder" available on MacOSX does a little
   30829   ** more than that; it looks at the filesystem type that hosts the
   30830   ** database file and tries to choose an locking method appropriate for
   30831   ** that filesystem time.
   30832   */
   30833   #define UNIXVFS(VFSNAME, FINDER) {                        \
   30834     3,                    /* iVersion */                    \
   30835     sizeof(unixFile),     /* szOsFile */                    \
   30836     MAX_PATHNAME,         /* mxPathname */                  \
   30837     0,                    /* pNext */                       \
   30838     VFSNAME,              /* zName */                       \
   30839     (void*)&FINDER,       /* pAppData */                    \
   30840     unixOpen,             /* xOpen */                       \
   30841     unixDelete,           /* xDelete */                     \
   30842     unixAccess,           /* xAccess */                     \
   30843     unixFullPathname,     /* xFullPathname */               \
   30844     unixDlOpen,           /* xDlOpen */                     \
   30845     unixDlError,          /* xDlError */                    \
   30846     unixDlSym,            /* xDlSym */                      \
   30847     unixDlClose,          /* xDlClose */                    \
   30848     unixRandomness,       /* xRandomness */                 \
   30849     unixSleep,            /* xSleep */                      \
   30850     unixCurrentTime,      /* xCurrentTime */                \
   30851     unixGetLastError,     /* xGetLastError */               \
   30852     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
   30853     unixSetSystemCall,    /* xSetSystemCall */              \
   30854     unixGetSystemCall,    /* xGetSystemCall */              \
   30855     unixNextSystemCall,   /* xNextSystemCall */             \
   30856   }
   30857 
   30858   /*
   30859   ** All default VFSes for unix are contained in the following array.
   30860   **
   30861   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
   30862   ** by the SQLite core when the VFS is registered.  So the following
   30863   ** array cannot be const.
   30864   */
   30865   static sqlite3_vfs aVfs[] = {
   30866 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
   30867     UNIXVFS("unix",          autolockIoFinder ),
   30868 #else
   30869     UNIXVFS("unix",          posixIoFinder ),
   30870 #endif
   30871     UNIXVFS("unix-none",     nolockIoFinder ),
   30872     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
   30873     UNIXVFS("unix-excl",     posixIoFinder ),
   30874 #if OS_VXWORKS
   30875     UNIXVFS("unix-namedsem", semIoFinder ),
   30876 #endif
   30877 #if SQLITE_ENABLE_LOCKING_STYLE
   30878     UNIXVFS("unix-posix",    posixIoFinder ),
   30879 #if !OS_VXWORKS
   30880     UNIXVFS("unix-flock",    flockIoFinder ),
   30881 #endif
   30882 #endif
   30883 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   30884     UNIXVFS("unix-afp",      afpIoFinder ),
   30885     UNIXVFS("unix-nfs",      nfsIoFinder ),
   30886     UNIXVFS("unix-proxy",    proxyIoFinder ),
   30887 #endif
   30888   };
   30889   unsigned int i;          /* Loop counter */
   30890 
   30891   /* Double-check that the aSyscall[] array has been constructed
   30892   ** correctly.  See ticket [bb3a86e890c8e96ab] */
   30893   assert( ArraySize(aSyscall)==18 );
   30894 
   30895   /* Register all VFSes defined in the aVfs[] array */
   30896   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
   30897     sqlite3_vfs_register(&aVfs[i], i==0);
   30898   }
   30899   return SQLITE_OK;
   30900 }
   30901 
   30902 /*
   30903 ** Shutdown the operating system interface.
   30904 **
   30905 ** Some operating systems might need to do some cleanup in this routine,
   30906 ** to release dynamically allocated objects.  But not on unix.
   30907 ** This routine is a no-op for unix.
   30908 */
   30909 SQLITE_API int sqlite3_os_end(void){
   30910   return SQLITE_OK;
   30911 }
   30912 
   30913 #endif /* SQLITE_OS_UNIX */
   30914 
   30915 /************** End of os_unix.c *********************************************/
   30916 /************** Begin file os_win.c ******************************************/
   30917 /*
   30918 ** 2004 May 22
   30919 **
   30920 ** The author disclaims copyright to this source code.  In place of
   30921 ** a legal notice, here is a blessing:
   30922 **
   30923 **    May you do good and not evil.
   30924 **    May you find forgiveness for yourself and forgive others.
   30925 **    May you share freely, never taking more than you give.
   30926 **
   30927 ******************************************************************************
   30928 **
   30929 ** This file contains code that is specific to windows.
   30930 */
   30931 #if SQLITE_OS_WIN               /* This file is used for windows only */
   30932 
   30933 
   30934 /*
   30935 ** A Note About Memory Allocation:
   30936 **
   30937 ** This driver uses malloc()/free() directly rather than going through
   30938 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
   30939 ** are designed for use on embedded systems where memory is scarce and
   30940 ** malloc failures happen frequently.  Win32 does not typically run on
   30941 ** embedded systems, and when it does the developers normally have bigger
   30942 ** problems to worry about than running out of memory.  So there is not
   30943 ** a compelling need to use the wrappers.
   30944 **
   30945 ** But there is a good reason to not use the wrappers.  If we use the
   30946 ** wrappers then we will get simulated malloc() failures within this
   30947 ** driver.  And that causes all kinds of problems for our tests.  We
   30948 ** could enhance SQLite to deal with simulated malloc failures within
   30949 ** the OS driver, but the code to deal with those failure would not
   30950 ** be exercised on Linux (which does not need to malloc() in the driver)
   30951 ** and so we would have difficulty writing coverage tests for that
   30952 ** code.  Better to leave the code out, we think.
   30953 **
   30954 ** The point of this discussion is as follows:  When creating a new
   30955 ** OS layer for an embedded system, if you use this file as an example,
   30956 ** avoid the use of malloc()/free().  Those routines work ok on windows
   30957 ** desktops but not so well in embedded systems.
   30958 */
   30959 
   30960 #include <winbase.h>
   30961 
   30962 #ifdef __CYGWIN__
   30963 # include <sys/cygwin.h>
   30964 #endif
   30965 
   30966 /*
   30967 ** Macros used to determine whether or not to use threads.
   30968 */
   30969 #if defined(THREADSAFE) && THREADSAFE
   30970 # define SQLITE_W32_THREADS 1
   30971 #endif
   30972 
   30973 /*
   30974 ** Include code that is common to all os_*.c files
   30975 */
   30976 /************** Include os_common.h in the middle of os_win.c ****************/
   30977 /************** Begin file os_common.h ***************************************/
   30978 /*
   30979 ** 2004 May 22
   30980 **
   30981 ** The author disclaims copyright to this source code.  In place of
   30982 ** a legal notice, here is a blessing:
   30983 **
   30984 **    May you do good and not evil.
   30985 **    May you find forgiveness for yourself and forgive others.
   30986 **    May you share freely, never taking more than you give.
   30987 **
   30988 ******************************************************************************
   30989 **
   30990 ** This file contains macros and a little bit of code that is common to
   30991 ** all of the platform-specific files (os_*.c) and is #included into those
   30992 ** files.
   30993 **
   30994 ** This file should be #included by the os_*.c files only.  It is not a
   30995 ** general purpose header file.
   30996 */
   30997 #ifndef _OS_COMMON_H_
   30998 #define _OS_COMMON_H_
   30999 
   31000 /*
   31001 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   31002 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   31003 ** switch.  The following code should catch this problem at compile-time.
   31004 */
   31005 #ifdef MEMORY_DEBUG
   31006 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   31007 #endif
   31008 
   31009 #ifdef SQLITE_DEBUG
   31010 SQLITE_PRIVATE int sqlite3OSTrace = 0;
   31011 #define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   31012 #else
   31013 #define OSTRACE(X)
   31014 #endif
   31015 
   31016 /*
   31017 ** Macros for performance tracing.  Normally turned off.  Only works
   31018 ** on i486 hardware.
   31019 */
   31020 #ifdef SQLITE_PERFORMANCE_TRACE
   31021 
   31022 /*
   31023 ** hwtime.h contains inline assembler code for implementing
   31024 ** high-performance timing routines.
   31025 */
   31026 /************** Include hwtime.h in the middle of os_common.h ****************/
   31027 /************** Begin file hwtime.h ******************************************/
   31028 /*
   31029 ** 2008 May 27
   31030 **
   31031 ** The author disclaims copyright to this source code.  In place of
   31032 ** a legal notice, here is a blessing:
   31033 **
   31034 **    May you do good and not evil.
   31035 **    May you find forgiveness for yourself and forgive others.
   31036 **    May you share freely, never taking more than you give.
   31037 **
   31038 ******************************************************************************
   31039 **
   31040 ** This file contains inline asm code for retrieving "high-performance"
   31041 ** counters for x86 class CPUs.
   31042 */
   31043 #ifndef _HWTIME_H_
   31044 #define _HWTIME_H_
   31045 
   31046 /*
   31047 ** The following routine only works on pentium-class (or newer) processors.
   31048 ** It uses the RDTSC opcode to read the cycle count value out of the
   31049 ** processor and returns that value.  This can be used for high-res
   31050 ** profiling.
   31051 */
   31052 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   31053       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   31054 
   31055   #if defined(__GNUC__)
   31056 
   31057   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31058      unsigned int lo, hi;
   31059      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   31060      return (sqlite_uint64)hi << 32 | lo;
   31061   }
   31062 
   31063   #elif defined(_MSC_VER)
   31064 
   31065   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   31066      __asm {
   31067         rdtsc
   31068         ret       ; return value at EDX:EAX
   31069      }
   31070   }
   31071 
   31072   #endif
   31073 
   31074 #elif (defined(__GNUC__) && defined(__x86_64__))
   31075 
   31076   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31077       unsigned long val;
   31078       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   31079       return val;
   31080   }
   31081 
   31082 #elif (defined(__GNUC__) && defined(__ppc__))
   31083 
   31084   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31085       unsigned long long retval;
   31086       unsigned long junk;
   31087       __asm__ __volatile__ ("\n\
   31088           1:      mftbu   %1\n\
   31089                   mftb    %L0\n\
   31090                   mftbu   %0\n\
   31091                   cmpw    %0,%1\n\
   31092                   bne     1b"
   31093                   : "=r" (retval), "=r" (junk));
   31094       return retval;
   31095   }
   31096 
   31097 #else
   31098 
   31099   #error Need implementation of sqlite3Hwtime() for your platform.
   31100 
   31101   /*
   31102   ** To compile without implementing sqlite3Hwtime() for your platform,
   31103   ** you can remove the above #error and use the following
   31104   ** stub function.  You will lose timing support for many
   31105   ** of the debugging and testing utilities, but it should at
   31106   ** least compile and run.
   31107   */
   31108 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   31109 
   31110 #endif
   31111 
   31112 #endif /* !defined(_HWTIME_H_) */
   31113 
   31114 /************** End of hwtime.h **********************************************/
   31115 /************** Continuing where we left off in os_common.h ******************/
   31116 
   31117 static sqlite_uint64 g_start;
   31118 static sqlite_uint64 g_elapsed;
   31119 #define TIMER_START       g_start=sqlite3Hwtime()
   31120 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   31121 #define TIMER_ELAPSED     g_elapsed
   31122 #else
   31123 #define TIMER_START
   31124 #define TIMER_END
   31125 #define TIMER_ELAPSED     ((sqlite_uint64)0)
   31126 #endif
   31127 
   31128 /*
   31129 ** If we compile with the SQLITE_TEST macro set, then the following block
   31130 ** of code will give us the ability to simulate a disk I/O error.  This
   31131 ** is used for testing the I/O recovery logic.
   31132 */
   31133 #ifdef SQLITE_TEST
   31134 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   31135 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   31136 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   31137 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   31138 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   31139 SQLITE_API int sqlite3_diskfull_pending = 0;
   31140 SQLITE_API int sqlite3_diskfull = 0;
   31141 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   31142 #define SimulateIOError(CODE)  \
   31143   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   31144        || sqlite3_io_error_pending-- == 1 )  \
   31145               { local_ioerr(); CODE; }
   31146 static void local_ioerr(){
   31147   IOTRACE(("IOERR\n"));
   31148   sqlite3_io_error_hit++;
   31149   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   31150 }
   31151 #define SimulateDiskfullError(CODE) \
   31152    if( sqlite3_diskfull_pending ){ \
   31153      if( sqlite3_diskfull_pending == 1 ){ \
   31154        local_ioerr(); \
   31155        sqlite3_diskfull = 1; \
   31156        sqlite3_io_error_hit = 1; \
   31157        CODE; \
   31158      }else{ \
   31159        sqlite3_diskfull_pending--; \
   31160      } \
   31161    }
   31162 #else
   31163 #define SimulateIOErrorBenign(X)
   31164 #define SimulateIOError(A)
   31165 #define SimulateDiskfullError(A)
   31166 #endif
   31167 
   31168 /*
   31169 ** When testing, keep a count of the number of open files.
   31170 */
   31171 #ifdef SQLITE_TEST
   31172 SQLITE_API int sqlite3_open_file_count = 0;
   31173 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   31174 #else
   31175 #define OpenCounter(X)
   31176 #endif
   31177 
   31178 #endif /* !defined(_OS_COMMON_H_) */
   31179 
   31180 /************** End of os_common.h *******************************************/
   31181 /************** Continuing where we left off in os_win.c *********************/
   31182 
   31183 /*
   31184 ** Some microsoft compilers lack this definition.
   31185 */
   31186 #ifndef INVALID_FILE_ATTRIBUTES
   31187 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
   31188 #endif
   31189 
   31190 /*
   31191 ** Determine if we are dealing with WindowsCE - which has a much
   31192 ** reduced API.
   31193 */
   31194 #if SQLITE_OS_WINCE
   31195 # define AreFileApisANSI() 1
   31196 # define FormatMessageW(a,b,c,d,e,f,g) 0
   31197 #endif
   31198 
   31199 /* Forward references */
   31200 typedef struct winShm winShm;           /* A connection to shared-memory */
   31201 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
   31202 
   31203 /*
   31204 ** WinCE lacks native support for file locking so we have to fake it
   31205 ** with some code of our own.
   31206 */
   31207 #if SQLITE_OS_WINCE
   31208 typedef struct winceLock {
   31209   int nReaders;       /* Number of reader locks obtained */
   31210   BOOL bPending;      /* Indicates a pending lock has been obtained */
   31211   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
   31212   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
   31213 } winceLock;
   31214 #endif
   31215 
   31216 /*
   31217 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
   31218 ** portability layer.
   31219 */
   31220 typedef struct winFile winFile;
   31221 struct winFile {
   31222   const sqlite3_io_methods *pMethod; /*** Must be first ***/
   31223   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
   31224   HANDLE h;               /* Handle for accessing the file */
   31225   unsigned char locktype; /* Type of lock currently held on this file */
   31226   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
   31227   DWORD lastErrno;        /* The Windows errno from the last I/O error */
   31228   DWORD sectorSize;       /* Sector size of the device file is on */
   31229   winShm *pShm;           /* Instance of shared memory on this file */
   31230   const char *zPath;      /* Full pathname of this file */
   31231   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
   31232 #if SQLITE_OS_WINCE
   31233   WCHAR *zDeleteOnClose;  /* Name of file to delete when closing */
   31234   HANDLE hMutex;          /* Mutex used to control access to shared lock */
   31235   HANDLE hShared;         /* Shared memory segment used for locking */
   31236   winceLock local;        /* Locks obtained by this instance of winFile */
   31237   winceLock *shared;      /* Global shared lock memory for the file  */
   31238 #endif
   31239 };
   31240 
   31241 /*
   31242 ** Forward prototypes.
   31243 */
   31244 static int getSectorSize(
   31245     sqlite3_vfs *pVfs,
   31246     const char *zRelative     /* UTF-8 file name */
   31247 );
   31248 
   31249 /*
   31250 ** The following variable is (normally) set once and never changes
   31251 ** thereafter.  It records whether the operating system is Win95
   31252 ** or WinNT.
   31253 **
   31254 ** 0:   Operating system unknown.
   31255 ** 1:   Operating system is Win95.
   31256 ** 2:   Operating system is WinNT.
   31257 **
   31258 ** In order to facilitate testing on a WinNT system, the test fixture
   31259 ** can manually set this value to 1 to emulate Win98 behavior.
   31260 */
   31261 #ifdef SQLITE_TEST
   31262 SQLITE_API int sqlite3_os_type = 0;
   31263 #else
   31264 static int sqlite3_os_type = 0;
   31265 #endif
   31266 
   31267 /*
   31268 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
   31269 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
   31270 **
   31271 ** Here is an interesting observation:  Win95, Win98, and WinME lack
   31272 ** the LockFileEx() API.  But we can still statically link against that
   31273 ** API as long as we don't call it when running Win95/98/ME.  A call to
   31274 ** this routine is used to determine if the host is Win95/98/ME or
   31275 ** WinNT/2K/XP so that we will know whether or not we can safely call
   31276 ** the LockFileEx() API.
   31277 */
   31278 #if SQLITE_OS_WINCE
   31279 # define isNT()  (1)
   31280 #else
   31281   static int isNT(void){
   31282     if( sqlite3_os_type==0 ){
   31283       OSVERSIONINFO sInfo;
   31284       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
   31285       GetVersionEx(&sInfo);
   31286       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   31287     }
   31288     return sqlite3_os_type==2;
   31289   }
   31290 #endif /* SQLITE_OS_WINCE */
   31291 
   31292 /*
   31293 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
   31294 **
   31295 ** Space to hold the returned string is obtained from malloc.
   31296 */
   31297 static WCHAR *utf8ToUnicode(const char *zFilename){
   31298   int nChar;
   31299   WCHAR *zWideFilename;
   31300 
   31301   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
   31302   zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
   31303   if( zWideFilename==0 ){
   31304     return 0;
   31305   }
   31306   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
   31307   if( nChar==0 ){
   31308     free(zWideFilename);
   31309     zWideFilename = 0;
   31310   }
   31311   return zWideFilename;
   31312 }
   31313 
   31314 /*
   31315 ** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
   31316 ** obtained from malloc().
   31317 */
   31318 static char *unicodeToUtf8(const WCHAR *zWideFilename){
   31319   int nByte;
   31320   char *zFilename;
   31321 
   31322   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
   31323   zFilename = malloc( nByte );
   31324   if( zFilename==0 ){
   31325     return 0;
   31326   }
   31327   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
   31328                               0, 0);
   31329   if( nByte == 0 ){
   31330     free(zFilename);
   31331     zFilename = 0;
   31332   }
   31333   return zFilename;
   31334 }
   31335 
   31336 /*
   31337 ** Convert an ansi string to microsoft unicode, based on the
   31338 ** current codepage settings for file apis.
   31339 **
   31340 ** Space to hold the returned string is obtained
   31341 ** from malloc.
   31342 */
   31343 static WCHAR *mbcsToUnicode(const char *zFilename){
   31344   int nByte;
   31345   WCHAR *zMbcsFilename;
   31346   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
   31347 
   31348   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
   31349   zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
   31350   if( zMbcsFilename==0 ){
   31351     return 0;
   31352   }
   31353   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
   31354   if( nByte==0 ){
   31355     free(zMbcsFilename);
   31356     zMbcsFilename = 0;
   31357   }
   31358   return zMbcsFilename;
   31359 }
   31360 
   31361 /*
   31362 ** Convert microsoft unicode to multibyte character string, based on the
   31363 ** user's Ansi codepage.
   31364 **
   31365 ** Space to hold the returned string is obtained from
   31366 ** malloc().
   31367 */
   31368 static char *unicodeToMbcs(const WCHAR *zWideFilename){
   31369   int nByte;
   31370   char *zFilename;
   31371   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
   31372 
   31373   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
   31374   zFilename = malloc( nByte );
   31375   if( zFilename==0 ){
   31376     return 0;
   31377   }
   31378   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
   31379                               0, 0);
   31380   if( nByte == 0 ){
   31381     free(zFilename);
   31382     zFilename = 0;
   31383   }
   31384   return zFilename;
   31385 }
   31386 
   31387 /*
   31388 ** Convert multibyte character string to UTF-8.  Space to hold the
   31389 ** returned string is obtained from malloc().
   31390 */
   31391 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
   31392   char *zFilenameUtf8;
   31393   WCHAR *zTmpWide;
   31394 
   31395   zTmpWide = mbcsToUnicode(zFilename);
   31396   if( zTmpWide==0 ){
   31397     return 0;
   31398   }
   31399   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
   31400   free(zTmpWide);
   31401   return zFilenameUtf8;
   31402 }
   31403 
   31404 /*
   31405 ** Convert UTF-8 to multibyte character string.  Space to hold the
   31406 ** returned string is obtained from malloc().
   31407 */
   31408 static char *utf8ToMbcs(const char *zFilename){
   31409   char *zFilenameMbcs;
   31410   WCHAR *zTmpWide;
   31411 
   31412   zTmpWide = utf8ToUnicode(zFilename);
   31413   if( zTmpWide==0 ){
   31414     return 0;
   31415   }
   31416   zFilenameMbcs = unicodeToMbcs(zTmpWide);
   31417   free(zTmpWide);
   31418   return zFilenameMbcs;
   31419 }
   31420 
   31421 #if SQLITE_OS_WINCE
   31422 /*************************************************************************
   31423 ** This section contains code for WinCE only.
   31424 */
   31425 /*
   31426 ** WindowsCE does not have a localtime() function.  So create a
   31427 ** substitute.
   31428 */
   31429 struct tm *__cdecl localtime(const time_t *t)
   31430 {
   31431   static struct tm y;
   31432   FILETIME uTm, lTm;
   31433   SYSTEMTIME pTm;
   31434   sqlite3_int64 t64;
   31435   t64 = *t;
   31436   t64 = (t64 + 11644473600)*10000000;
   31437   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
   31438   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
   31439   FileTimeToLocalFileTime(&uTm,&lTm);
   31440   FileTimeToSystemTime(&lTm,&pTm);
   31441   y.tm_year = pTm.wYear - 1900;
   31442   y.tm_mon = pTm.wMonth - 1;
   31443   y.tm_wday = pTm.wDayOfWeek;
   31444   y.tm_mday = pTm.wDay;
   31445   y.tm_hour = pTm.wHour;
   31446   y.tm_min = pTm.wMinute;
   31447   y.tm_sec = pTm.wSecond;
   31448   return &y;
   31449 }
   31450 
   31451 /* This will never be called, but defined to make the code compile */
   31452 #define GetTempPathA(a,b)
   31453 
   31454 #define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
   31455 #define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
   31456 #define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
   31457 
   31458 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
   31459 
   31460 /*
   31461 ** Acquire a lock on the handle h
   31462 */
   31463 static void winceMutexAcquire(HANDLE h){
   31464    DWORD dwErr;
   31465    do {
   31466      dwErr = WaitForSingleObject(h, INFINITE);
   31467    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
   31468 }
   31469 /*
   31470 ** Release a lock acquired by winceMutexAcquire()
   31471 */
   31472 #define winceMutexRelease(h) ReleaseMutex(h)
   31473 
   31474 /*
   31475 ** Create the mutex and shared memory used for locking in the file
   31476 ** descriptor pFile
   31477 */
   31478 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
   31479   WCHAR *zTok;
   31480   WCHAR *zName = utf8ToUnicode(zFilename);
   31481   BOOL bInit = TRUE;
   31482 
   31483   /* Initialize the local lockdata */
   31484   ZeroMemory(&pFile->local, sizeof(pFile->local));
   31485 
   31486   /* Replace the backslashes from the filename and lowercase it
   31487   ** to derive a mutex name. */
   31488   zTok = CharLowerW(zName);
   31489   for (;*zTok;zTok++){
   31490     if (*zTok == '\\') *zTok = '_';
   31491   }
   31492 
   31493   /* Create/open the named mutex */
   31494   pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
   31495   if (!pFile->hMutex){
   31496     pFile->lastErrno = GetLastError();
   31497     free(zName);
   31498     return FALSE;
   31499   }
   31500 
   31501   /* Acquire the mutex before continuing */
   31502   winceMutexAcquire(pFile->hMutex);
   31503 
   31504   /* Since the names of named mutexes, semaphores, file mappings etc are
   31505   ** case-sensitive, take advantage of that by uppercasing the mutex name
   31506   ** and using that as the shared filemapping name.
   31507   */
   31508   CharUpperW(zName);
   31509   pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
   31510                                        PAGE_READWRITE, 0, sizeof(winceLock),
   31511                                        zName);
   31512 
   31513   /* Set a flag that indicates we're the first to create the memory so it
   31514   ** must be zero-initialized */
   31515   if (GetLastError() == ERROR_ALREADY_EXISTS){
   31516     bInit = FALSE;
   31517   }
   31518 
   31519   free(zName);
   31520 
   31521   /* If we succeeded in making the shared memory handle, map it. */
   31522   if (pFile->hShared){
   31523     pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
   31524              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
   31525     /* If mapping failed, close the shared memory handle and erase it */
   31526     if (!pFile->shared){
   31527       pFile->lastErrno = GetLastError();
   31528       CloseHandle(pFile->hShared);
   31529       pFile->hShared = NULL;
   31530     }
   31531   }
   31532 
   31533   /* If shared memory could not be created, then close the mutex and fail */
   31534   if (pFile->hShared == NULL){
   31535     winceMutexRelease(pFile->hMutex);
   31536     CloseHandle(pFile->hMutex);
   31537     pFile->hMutex = NULL;
   31538     return FALSE;
   31539   }
   31540 
   31541   /* Initialize the shared memory if we're supposed to */
   31542   if (bInit) {
   31543     ZeroMemory(pFile->shared, sizeof(winceLock));
   31544   }
   31545 
   31546   winceMutexRelease(pFile->hMutex);
   31547   return TRUE;
   31548 }
   31549 
   31550 /*
   31551 ** Destroy the part of winFile that deals with wince locks
   31552 */
   31553 static void winceDestroyLock(winFile *pFile){
   31554   if (pFile->hMutex){
   31555     /* Acquire the mutex */
   31556     winceMutexAcquire(pFile->hMutex);
   31557 
   31558     /* The following blocks should probably assert in debug mode, but they
   31559        are to cleanup in case any locks remained open */
   31560     if (pFile->local.nReaders){
   31561       pFile->shared->nReaders --;
   31562     }
   31563     if (pFile->local.bReserved){
   31564       pFile->shared->bReserved = FALSE;
   31565     }
   31566     if (pFile->local.bPending){
   31567       pFile->shared->bPending = FALSE;
   31568     }
   31569     if (pFile->local.bExclusive){
   31570       pFile->shared->bExclusive = FALSE;
   31571     }
   31572 
   31573     /* De-reference and close our copy of the shared memory handle */
   31574     UnmapViewOfFile(pFile->shared);
   31575     CloseHandle(pFile->hShared);
   31576 
   31577     /* Done with the mutex */
   31578     winceMutexRelease(pFile->hMutex);
   31579     CloseHandle(pFile->hMutex);
   31580     pFile->hMutex = NULL;
   31581   }
   31582 }
   31583 
   31584 /*
   31585 ** An implementation of the LockFile() API of windows for wince
   31586 */
   31587 static BOOL winceLockFile(
   31588   HANDLE *phFile,
   31589   DWORD dwFileOffsetLow,
   31590   DWORD dwFileOffsetHigh,
   31591   DWORD nNumberOfBytesToLockLow,
   31592   DWORD nNumberOfBytesToLockHigh
   31593 ){
   31594   winFile *pFile = HANDLE_TO_WINFILE(phFile);
   31595   BOOL bReturn = FALSE;
   31596 
   31597   UNUSED_PARAMETER(dwFileOffsetHigh);
   31598   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
   31599 
   31600   if (!pFile->hMutex) return TRUE;
   31601   winceMutexAcquire(pFile->hMutex);
   31602 
   31603   /* Wanting an exclusive lock? */
   31604   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
   31605        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
   31606     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
   31607        pFile->shared->bExclusive = TRUE;
   31608        pFile->local.bExclusive = TRUE;
   31609        bReturn = TRUE;
   31610     }
   31611   }
   31612 
   31613   /* Want a read-only lock? */
   31614   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
   31615            nNumberOfBytesToLockLow == 1){
   31616     if (pFile->shared->bExclusive == 0){
   31617       pFile->local.nReaders ++;
   31618       if (pFile->local.nReaders == 1){
   31619         pFile->shared->nReaders ++;
   31620       }
   31621       bReturn = TRUE;
   31622     }
   31623   }
   31624 
   31625   /* Want a pending lock? */
   31626   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
   31627     /* If no pending lock has been acquired, then acquire it */
   31628     if (pFile->shared->bPending == 0) {
   31629       pFile->shared->bPending = TRUE;
   31630       pFile->local.bPending = TRUE;
   31631       bReturn = TRUE;
   31632     }
   31633   }
   31634 
   31635   /* Want a reserved lock? */
   31636   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
   31637     if (pFile->shared->bReserved == 0) {
   31638       pFile->shared->bReserved = TRUE;
   31639       pFile->local.bReserved = TRUE;
   31640       bReturn = TRUE;
   31641     }
   31642   }
   31643 
   31644   winceMutexRelease(pFile->hMutex);
   31645   return bReturn;
   31646 }
   31647 
   31648 /*
   31649 ** An implementation of the UnlockFile API of windows for wince
   31650 */
   31651 static BOOL winceUnlockFile(
   31652   HANDLE *phFile,
   31653   DWORD dwFileOffsetLow,
   31654   DWORD dwFileOffsetHigh,
   31655   DWORD nNumberOfBytesToUnlockLow,
   31656   DWORD nNumberOfBytesToUnlockHigh
   31657 ){
   31658   winFile *pFile = HANDLE_TO_WINFILE(phFile);
   31659   BOOL bReturn = FALSE;
   31660 
   31661   UNUSED_PARAMETER(dwFileOffsetHigh);
   31662   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
   31663 
   31664   if (!pFile->hMutex) return TRUE;
   31665   winceMutexAcquire(pFile->hMutex);
   31666 
   31667   /* Releasing a reader lock or an exclusive lock */
   31668   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
   31669     /* Did we have an exclusive lock? */
   31670     if (pFile->local.bExclusive){
   31671       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
   31672       pFile->local.bExclusive = FALSE;
   31673       pFile->shared->bExclusive = FALSE;
   31674       bReturn = TRUE;
   31675     }
   31676 
   31677     /* Did we just have a reader lock? */
   31678     else if (pFile->local.nReaders){
   31679       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
   31680       pFile->local.nReaders --;
   31681       if (pFile->local.nReaders == 0)
   31682       {
   31683         pFile->shared->nReaders --;
   31684       }
   31685       bReturn = TRUE;
   31686     }
   31687   }
   31688 
   31689   /* Releasing a pending lock */
   31690   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
   31691     if (pFile->local.bPending){
   31692       pFile->local.bPending = FALSE;
   31693       pFile->shared->bPending = FALSE;
   31694       bReturn = TRUE;
   31695     }
   31696   }
   31697   /* Releasing a reserved lock */
   31698   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
   31699     if (pFile->local.bReserved) {
   31700       pFile->local.bReserved = FALSE;
   31701       pFile->shared->bReserved = FALSE;
   31702       bReturn = TRUE;
   31703     }
   31704   }
   31705 
   31706   winceMutexRelease(pFile->hMutex);
   31707   return bReturn;
   31708 }
   31709 
   31710 /*
   31711 ** An implementation of the LockFileEx() API of windows for wince
   31712 */
   31713 static BOOL winceLockFileEx(
   31714   HANDLE *phFile,
   31715   DWORD dwFlags,
   31716   DWORD dwReserved,
   31717   DWORD nNumberOfBytesToLockLow,
   31718   DWORD nNumberOfBytesToLockHigh,
   31719   LPOVERLAPPED lpOverlapped
   31720 ){
   31721   UNUSED_PARAMETER(dwReserved);
   31722   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
   31723 
   31724   /* If the caller wants a shared read lock, forward this call
   31725   ** to winceLockFile */
   31726   if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
   31727       dwFlags == 1 &&
   31728       nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
   31729     return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
   31730   }
   31731   return FALSE;
   31732 }
   31733 /*
   31734 ** End of the special code for wince
   31735 *****************************************************************************/
   31736 #endif /* SQLITE_OS_WINCE */
   31737 
   31738 /*****************************************************************************
   31739 ** The next group of routines implement the I/O methods specified
   31740 ** by the sqlite3_io_methods object.
   31741 ******************************************************************************/
   31742 
   31743 /*
   31744 ** Some microsoft compilers lack this definition.
   31745 */
   31746 #ifndef INVALID_SET_FILE_POINTER
   31747 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
   31748 #endif
   31749 
   31750 /*
   31751 ** Move the current position of the file handle passed as the first
   31752 ** argument to offset iOffset within the file. If successful, return 0.
   31753 ** Otherwise, set pFile->lastErrno and return non-zero.
   31754 */
   31755 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
   31756   LONG upperBits;                 /* Most sig. 32 bits of new offset */
   31757   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
   31758   DWORD dwRet;                    /* Value returned by SetFilePointer() */
   31759 
   31760   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
   31761   lowerBits = (LONG)(iOffset & 0xffffffff);
   31762 
   31763   /* API oddity: If successful, SetFilePointer() returns a dword
   31764   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
   31765   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
   31766   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
   31767   ** whether an error has actually occured, it is also necessary to call
   31768   ** GetLastError().
   31769   */
   31770   dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
   31771   if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
   31772     pFile->lastErrno = GetLastError();
   31773     return 1;
   31774   }
   31775 
   31776   return 0;
   31777 }
   31778 
   31779 /*
   31780 ** Close a file.
   31781 **
   31782 ** It is reported that an attempt to close a handle might sometimes
   31783 ** fail.  This is a very unreasonable result, but windows is notorious
   31784 ** for being unreasonable so I do not doubt that it might happen.  If
   31785 ** the close fails, we pause for 100 milliseconds and try again.  As
   31786 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
   31787 ** giving up and returning an error.
   31788 */
   31789 #define MX_CLOSE_ATTEMPT 3
   31790 static int winClose(sqlite3_file *id){
   31791   int rc, cnt = 0;
   31792   winFile *pFile = (winFile*)id;
   31793 
   31794   assert( id!=0 );
   31795   assert( pFile->pShm==0 );
   31796   OSTRACE(("CLOSE %d\n", pFile->h));
   31797   do{
   31798     rc = CloseHandle(pFile->h);
   31799     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
   31800   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
   31801 #if SQLITE_OS_WINCE
   31802 #define WINCE_DELETION_ATTEMPTS 3
   31803   winceDestroyLock(pFile);
   31804   if( pFile->zDeleteOnClose ){
   31805     int cnt = 0;
   31806     while(
   31807            DeleteFileW(pFile->zDeleteOnClose)==0
   31808         && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
   31809         && cnt++ < WINCE_DELETION_ATTEMPTS
   31810     ){
   31811        Sleep(100);  /* Wait a little before trying again */
   31812     }
   31813     free(pFile->zDeleteOnClose);
   31814   }
   31815 #endif
   31816   OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
   31817   OpenCounter(-1);
   31818   return rc ? SQLITE_OK : SQLITE_IOERR;
   31819 }
   31820 
   31821 /*
   31822 ** Read data from a file into a buffer.  Return SQLITE_OK if all
   31823 ** bytes were read successfully and SQLITE_IOERR if anything goes
   31824 ** wrong.
   31825 */
   31826 static int winRead(
   31827   sqlite3_file *id,          /* File to read from */
   31828   void *pBuf,                /* Write content into this buffer */
   31829   int amt,                   /* Number of bytes to read */
   31830   sqlite3_int64 offset       /* Begin reading at this offset */
   31831 ){
   31832   winFile *pFile = (winFile*)id;  /* file handle */
   31833   DWORD nRead;                    /* Number of bytes actually read from file */
   31834 
   31835   assert( id!=0 );
   31836   SimulateIOError(return SQLITE_IOERR_READ);
   31837   OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
   31838 
   31839   if( seekWinFile(pFile, offset) ){
   31840     return SQLITE_FULL;
   31841   }
   31842   if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
   31843     pFile->lastErrno = GetLastError();
   31844     return SQLITE_IOERR_READ;
   31845   }
   31846   if( nRead<(DWORD)amt ){
   31847     /* Unread parts of the buffer must be zero-filled */
   31848     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
   31849     return SQLITE_IOERR_SHORT_READ;
   31850   }
   31851 
   31852   return SQLITE_OK;
   31853 }
   31854 
   31855 /*
   31856 ** Write data from a buffer into a file.  Return SQLITE_OK on success
   31857 ** or some other error code on failure.
   31858 */
   31859 static int winWrite(
   31860   sqlite3_file *id,               /* File to write into */
   31861   const void *pBuf,               /* The bytes to be written */
   31862   int amt,                        /* Number of bytes to write */
   31863   sqlite3_int64 offset            /* Offset into the file to begin writing at */
   31864 ){
   31865   int rc;                         /* True if error has occured, else false */
   31866   winFile *pFile = (winFile*)id;  /* File handle */
   31867 
   31868   assert( amt>0 );
   31869   assert( pFile );
   31870   SimulateIOError(return SQLITE_IOERR_WRITE);
   31871   SimulateDiskfullError(return SQLITE_FULL);
   31872 
   31873   OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
   31874 
   31875   rc = seekWinFile(pFile, offset);
   31876   if( rc==0 ){
   31877     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
   31878     int nRem = amt;               /* Number of bytes yet to be written */
   31879     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
   31880 
   31881     while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){
   31882       aRem += nWrite;
   31883       nRem -= nWrite;
   31884     }
   31885     if( nRem>0 ){
   31886       pFile->lastErrno = GetLastError();
   31887       rc = 1;
   31888     }
   31889   }
   31890 
   31891   if( rc ){
   31892     if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
   31893       return SQLITE_FULL;
   31894     }
   31895     return SQLITE_IOERR_WRITE;
   31896   }
   31897   return SQLITE_OK;
   31898 }
   31899 
   31900 /*
   31901 ** Truncate an open file to a specified size
   31902 */
   31903 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
   31904   winFile *pFile = (winFile*)id;  /* File handle object */
   31905   int rc = SQLITE_OK;             /* Return code for this function */
   31906 
   31907   assert( pFile );
   31908 
   31909   OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
   31910   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
   31911 
   31912   /* If the user has configured a chunk-size for this file, truncate the
   31913   ** file so that it consists of an integer number of chunks (i.e. the
   31914   ** actual file size after the operation may be larger than the requested
   31915   ** size).
   31916   */
   31917   if( pFile->szChunk ){
   31918     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   31919   }
   31920 
   31921   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
   31922   if( seekWinFile(pFile, nByte) ){
   31923     rc = SQLITE_IOERR_TRUNCATE;
   31924   }else if( 0==SetEndOfFile(pFile->h) ){
   31925     pFile->lastErrno = GetLastError();
   31926     rc = SQLITE_IOERR_TRUNCATE;
   31927   }
   31928 
   31929   OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
   31930   return rc;
   31931 }
   31932 
   31933 #ifdef SQLITE_TEST
   31934 /*
   31935 ** Count the number of fullsyncs and normal syncs.  This is used to test
   31936 ** that syncs and fullsyncs are occuring at the right times.
   31937 */
   31938 SQLITE_API int sqlite3_sync_count = 0;
   31939 SQLITE_API int sqlite3_fullsync_count = 0;
   31940 #endif
   31941 
   31942 /*
   31943 ** Make sure all writes to a particular file are committed to disk.
   31944 */
   31945 static int winSync(sqlite3_file *id, int flags){
   31946 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || defined(SQLITE_DEBUG)
   31947   winFile *pFile = (winFile*)id;
   31948 #else
   31949   UNUSED_PARAMETER(id);
   31950 #endif
   31951 
   31952   assert( pFile );
   31953   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
   31954   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
   31955       || (flags&0x0F)==SQLITE_SYNC_FULL
   31956   );
   31957 
   31958   OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
   31959 
   31960 #ifndef SQLITE_TEST
   31961   UNUSED_PARAMETER(flags);
   31962 #else
   31963   if( flags & SQLITE_SYNC_FULL ){
   31964     sqlite3_fullsync_count++;
   31965   }
   31966   sqlite3_sync_count++;
   31967 #endif
   31968 
   31969   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
   31970   ** line is to test that doing so does not cause any problems.
   31971   */
   31972   SimulateDiskfullError( return SQLITE_FULL );
   31973   SimulateIOError( return SQLITE_IOERR; );
   31974 
   31975   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   31976   ** no-op
   31977   */
   31978 #ifdef SQLITE_NO_SYNC
   31979   return SQLITE_OK;
   31980 #else
   31981   if( FlushFileBuffers(pFile->h) ){
   31982     return SQLITE_OK;
   31983   }else{
   31984     pFile->lastErrno = GetLastError();
   31985     return SQLITE_IOERR;
   31986   }
   31987 #endif
   31988 }
   31989 
   31990 /*
   31991 ** Determine the current size of a file in bytes
   31992 */
   31993 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
   31994   DWORD upperBits;
   31995   DWORD lowerBits;
   31996   winFile *pFile = (winFile*)id;
   31997   DWORD error;
   31998 
   31999   assert( id!=0 );
   32000   SimulateIOError(return SQLITE_IOERR_FSTAT);
   32001   lowerBits = GetFileSize(pFile->h, &upperBits);
   32002   if(   (lowerBits == INVALID_FILE_SIZE)
   32003      && ((error = GetLastError()) != NO_ERROR) )
   32004   {
   32005     pFile->lastErrno = error;
   32006     return SQLITE_IOERR_FSTAT;
   32007   }
   32008   *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
   32009   return SQLITE_OK;
   32010 }
   32011 
   32012 /*
   32013 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
   32014 */
   32015 #ifndef LOCKFILE_FAIL_IMMEDIATELY
   32016 # define LOCKFILE_FAIL_IMMEDIATELY 1
   32017 #endif
   32018 
   32019 /*
   32020 ** Acquire a reader lock.
   32021 ** Different API routines are called depending on whether or not this
   32022 ** is Win95 or WinNT.
   32023 */
   32024 static int getReadLock(winFile *pFile){
   32025   int res;
   32026   if( isNT() ){
   32027     OVERLAPPED ovlp;
   32028     ovlp.Offset = SHARED_FIRST;
   32029     ovlp.OffsetHigh = 0;
   32030     ovlp.hEvent = 0;
   32031     res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
   32032                      0, SHARED_SIZE, 0, &ovlp);
   32033 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   32034 */
   32035 #if SQLITE_OS_WINCE==0
   32036   }else{
   32037     int lk;
   32038     sqlite3_randomness(sizeof(lk), &lk);
   32039     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
   32040     res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
   32041 #endif
   32042   }
   32043   if( res == 0 ){
   32044     pFile->lastErrno = GetLastError();
   32045   }
   32046   return res;
   32047 }
   32048 
   32049 /*
   32050 ** Undo a readlock
   32051 */
   32052 static int unlockReadLock(winFile *pFile){
   32053   int res;
   32054   if( isNT() ){
   32055     res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   32056 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   32057 */
   32058 #if SQLITE_OS_WINCE==0
   32059   }else{
   32060     res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
   32061 #endif
   32062   }
   32063   if( res == 0 ){
   32064     pFile->lastErrno = GetLastError();
   32065   }
   32066   return res;
   32067 }
   32068 
   32069 /*
   32070 ** Lock the file with the lock specified by parameter locktype - one
   32071 ** of the following:
   32072 **
   32073 **     (1) SHARED_LOCK
   32074 **     (2) RESERVED_LOCK
   32075 **     (3) PENDING_LOCK
   32076 **     (4) EXCLUSIVE_LOCK
   32077 **
   32078 ** Sometimes when requesting one lock state, additional lock states
   32079 ** are inserted in between.  The locking might fail on one of the later
   32080 ** transitions leaving the lock state different from what it started but
   32081 ** still short of its goal.  The following chart shows the allowed
   32082 ** transitions and the inserted intermediate states:
   32083 **
   32084 **    UNLOCKED -> SHARED
   32085 **    SHARED -> RESERVED
   32086 **    SHARED -> (PENDING) -> EXCLUSIVE
   32087 **    RESERVED -> (PENDING) -> EXCLUSIVE
   32088 **    PENDING -> EXCLUSIVE
   32089 **
   32090 ** This routine will only increase a lock.  The winUnlock() routine
   32091 ** erases all locks at once and returns us immediately to locking level 0.
   32092 ** It is not possible to lower the locking level one step at a time.  You
   32093 ** must go straight to locking level 0.
   32094 */
   32095 static int winLock(sqlite3_file *id, int locktype){
   32096   int rc = SQLITE_OK;    /* Return code from subroutines */
   32097   int res = 1;           /* Result of a windows lock call */
   32098   int newLocktype;       /* Set pFile->locktype to this value before exiting */
   32099   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   32100   winFile *pFile = (winFile*)id;
   32101   DWORD error = NO_ERROR;
   32102 
   32103   assert( id!=0 );
   32104   OSTRACE(("LOCK %d %d was %d(%d)\n",
   32105            pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
   32106 
   32107   /* If there is already a lock of this type or more restrictive on the
   32108   ** OsFile, do nothing. Don't use the end_lock: exit path, as
   32109   ** sqlite3OsEnterMutex() hasn't been called yet.
   32110   */
   32111   if( pFile->locktype>=locktype ){
   32112     return SQLITE_OK;
   32113   }
   32114 
   32115   /* Make sure the locking sequence is correct
   32116   */
   32117   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
   32118   assert( locktype!=PENDING_LOCK );
   32119   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
   32120 
   32121   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
   32122   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   32123   ** the PENDING_LOCK byte is temporary.
   32124   */
   32125   newLocktype = pFile->locktype;
   32126   if(   (pFile->locktype==NO_LOCK)
   32127      || (   (locktype==EXCLUSIVE_LOCK)
   32128          && (pFile->locktype==RESERVED_LOCK))
   32129   ){
   32130     int cnt = 3;
   32131     while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
   32132       /* Try 3 times to get the pending lock.  The pending lock might be
   32133       ** held by another reader process who will release it momentarily.
   32134       */
   32135       OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
   32136       Sleep(1);
   32137     }
   32138     gotPendingLock = res;
   32139     if( !res ){
   32140       error = GetLastError();
   32141     }
   32142   }
   32143 
   32144   /* Acquire a shared lock
   32145   */
   32146   if( locktype==SHARED_LOCK && res ){
   32147     assert( pFile->locktype==NO_LOCK );
   32148     res = getReadLock(pFile);
   32149     if( res ){
   32150       newLocktype = SHARED_LOCK;
   32151     }else{
   32152       error = GetLastError();
   32153     }
   32154   }
   32155 
   32156   /* Acquire a RESERVED lock
   32157   */
   32158   if( locktype==RESERVED_LOCK && res ){
   32159     assert( pFile->locktype==SHARED_LOCK );
   32160     res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   32161     if( res ){
   32162       newLocktype = RESERVED_LOCK;
   32163     }else{
   32164       error = GetLastError();
   32165     }
   32166   }
   32167 
   32168   /* Acquire a PENDING lock
   32169   */
   32170   if( locktype==EXCLUSIVE_LOCK && res ){
   32171     newLocktype = PENDING_LOCK;
   32172     gotPendingLock = 0;
   32173   }
   32174 
   32175   /* Acquire an EXCLUSIVE lock
   32176   */
   32177   if( locktype==EXCLUSIVE_LOCK && res ){
   32178     assert( pFile->locktype>=SHARED_LOCK );
   32179     res = unlockReadLock(pFile);
   32180     OSTRACE(("unreadlock = %d\n", res));
   32181     res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   32182     if( res ){
   32183       newLocktype = EXCLUSIVE_LOCK;
   32184     }else{
   32185       error = GetLastError();
   32186       OSTRACE(("error-code = %d\n", error));
   32187       getReadLock(pFile);
   32188     }
   32189   }
   32190 
   32191   /* If we are holding a PENDING lock that ought to be released, then
   32192   ** release it now.
   32193   */
   32194   if( gotPendingLock && locktype==SHARED_LOCK ){
   32195     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
   32196   }
   32197 
   32198   /* Update the state of the lock has held in the file descriptor then
   32199   ** return the appropriate result code.
   32200   */
   32201   if( res ){
   32202     rc = SQLITE_OK;
   32203   }else{
   32204     OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   32205            locktype, newLocktype));
   32206     pFile->lastErrno = error;
   32207     rc = SQLITE_BUSY;
   32208   }
   32209   pFile->locktype = (u8)newLocktype;
   32210   return rc;
   32211 }
   32212 
   32213 /*
   32214 ** This routine checks if there is a RESERVED lock held on the specified
   32215 ** file by this or any other process. If such a lock is held, return
   32216 ** non-zero, otherwise zero.
   32217 */
   32218 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
   32219   int rc;
   32220   winFile *pFile = (winFile*)id;
   32221 
   32222   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   32223 
   32224   assert( id!=0 );
   32225   if( pFile->locktype>=RESERVED_LOCK ){
   32226     rc = 1;
   32227     OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
   32228   }else{
   32229     rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   32230     if( rc ){
   32231       UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   32232     }
   32233     rc = !rc;
   32234     OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
   32235   }
   32236   *pResOut = rc;
   32237   return SQLITE_OK;
   32238 }
   32239 
   32240 /*
   32241 ** Lower the locking level on file descriptor id to locktype.  locktype
   32242 ** must be either NO_LOCK or SHARED_LOCK.
   32243 **
   32244 ** If the locking level of the file descriptor is already at or below
   32245 ** the requested locking level, this routine is a no-op.
   32246 **
   32247 ** It is not possible for this routine to fail if the second argument
   32248 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
   32249 ** might return SQLITE_IOERR;
   32250 */
   32251 static int winUnlock(sqlite3_file *id, int locktype){
   32252   int type;
   32253   winFile *pFile = (winFile*)id;
   32254   int rc = SQLITE_OK;
   32255   assert( pFile!=0 );
   32256   assert( locktype<=SHARED_LOCK );
   32257   OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
   32258           pFile->locktype, pFile->sharedLockByte));
   32259   type = pFile->locktype;
   32260   if( type>=EXCLUSIVE_LOCK ){
   32261     UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   32262     if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
   32263       /* This should never happen.  We should always be able to
   32264       ** reacquire the read lock */
   32265       rc = SQLITE_IOERR_UNLOCK;
   32266     }
   32267   }
   32268   if( type>=RESERVED_LOCK ){
   32269     UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   32270   }
   32271   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
   32272     unlockReadLock(pFile);
   32273   }
   32274   if( type>=PENDING_LOCK ){
   32275     UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
   32276   }
   32277   pFile->locktype = (u8)locktype;
   32278   return rc;
   32279 }
   32280 
   32281 /*
   32282 ** Control and query of the open file handle.
   32283 */
   32284 static int winFileControl(sqlite3_file *id, int op, void *pArg){
   32285   switch( op ){
   32286     case SQLITE_FCNTL_LOCKSTATE: {
   32287       *(int*)pArg = ((winFile*)id)->locktype;
   32288       return SQLITE_OK;
   32289     }
   32290     case SQLITE_LAST_ERRNO: {
   32291       *(int*)pArg = (int)((winFile*)id)->lastErrno;
   32292       return SQLITE_OK;
   32293     }
   32294     case SQLITE_FCNTL_CHUNK_SIZE: {
   32295       ((winFile*)id)->szChunk = *(int *)pArg;
   32296       return SQLITE_OK;
   32297     }
   32298     case SQLITE_FCNTL_SIZE_HINT: {
   32299       sqlite3_int64 sz = *(sqlite3_int64*)pArg;
   32300       SimulateIOErrorBenign(1);
   32301       winTruncate(id, sz);
   32302       SimulateIOErrorBenign(0);
   32303       return SQLITE_OK;
   32304     }
   32305     case SQLITE_FCNTL_SYNC_OMITTED: {
   32306       return SQLITE_OK;
   32307     }
   32308   }
   32309   return SQLITE_NOTFOUND;
   32310 }
   32311 
   32312 /*
   32313 ** Return the sector size in bytes of the underlying block device for
   32314 ** the specified file. This is almost always 512 bytes, but may be
   32315 ** larger for some devices.
   32316 **
   32317 ** SQLite code assumes this function cannot fail. It also assumes that
   32318 ** if two files are created in the same file-system directory (i.e.
   32319 ** a database and its journal file) that the sector size will be the
   32320 ** same for both.
   32321 */
   32322 static int winSectorSize(sqlite3_file *id){
   32323   assert( id!=0 );
   32324   return (int)(((winFile*)id)->sectorSize);
   32325 }
   32326 
   32327 /*
   32328 ** Return a vector of device characteristics.
   32329 */
   32330 static int winDeviceCharacteristics(sqlite3_file *id){
   32331   UNUSED_PARAMETER(id);
   32332   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
   32333 }
   32334 
   32335 #ifndef SQLITE_OMIT_WAL
   32336 
   32337 /*
   32338 ** Windows will only let you create file view mappings
   32339 ** on allocation size granularity boundaries.
   32340 ** During sqlite3_os_init() we do a GetSystemInfo()
   32341 ** to get the granularity size.
   32342 */
   32343 SYSTEM_INFO winSysInfo;
   32344 
   32345 /*
   32346 ** Helper functions to obtain and relinquish the global mutex. The
   32347 ** global mutex is used to protect the winLockInfo objects used by
   32348 ** this file, all of which may be shared by multiple threads.
   32349 **
   32350 ** Function winShmMutexHeld() is used to assert() that the global mutex
   32351 ** is held when required. This function is only used as part of assert()
   32352 ** statements. e.g.
   32353 **
   32354 **   winShmEnterMutex()
   32355 **     assert( winShmMutexHeld() );
   32356 **   winShmLeaveMutex()
   32357 */
   32358 static void winShmEnterMutex(void){
   32359   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   32360 }
   32361 static void winShmLeaveMutex(void){
   32362   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   32363 }
   32364 #ifdef SQLITE_DEBUG
   32365 static int winShmMutexHeld(void) {
   32366   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   32367 }
   32368 #endif
   32369 
   32370 /*
   32371 ** Object used to represent a single file opened and mmapped to provide
   32372 ** shared memory.  When multiple threads all reference the same
   32373 ** log-summary, each thread has its own winFile object, but they all
   32374 ** point to a single instance of this object.  In other words, each
   32375 ** log-summary is opened only once per process.
   32376 **
   32377 ** winShmMutexHeld() must be true when creating or destroying
   32378 ** this object or while reading or writing the following fields:
   32379 **
   32380 **      nRef
   32381 **      pNext
   32382 **
   32383 ** The following fields are read-only after the object is created:
   32384 **
   32385 **      fid
   32386 **      zFilename
   32387 **
   32388 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
   32389 ** winShmMutexHeld() is true when reading or writing any other field
   32390 ** in this structure.
   32391 **
   32392 */
   32393 struct winShmNode {
   32394   sqlite3_mutex *mutex;      /* Mutex to access this object */
   32395   char *zFilename;           /* Name of the file */
   32396   winFile hFile;             /* File handle from winOpen */
   32397 
   32398   int szRegion;              /* Size of shared-memory regions */
   32399   int nRegion;               /* Size of array apRegion */
   32400   struct ShmRegion {
   32401     HANDLE hMap;             /* File handle from CreateFileMapping */
   32402     void *pMap;
   32403   } *aRegion;
   32404   DWORD lastErrno;           /* The Windows errno from the last I/O error */
   32405 
   32406   int nRef;                  /* Number of winShm objects pointing to this */
   32407   winShm *pFirst;            /* All winShm objects pointing to this */
   32408   winShmNode *pNext;         /* Next in list of all winShmNode objects */
   32409 #ifdef SQLITE_DEBUG
   32410   u8 nextShmId;              /* Next available winShm.id value */
   32411 #endif
   32412 };
   32413 
   32414 /*
   32415 ** A global array of all winShmNode objects.
   32416 **
   32417 ** The winShmMutexHeld() must be true while reading or writing this list.
   32418 */
   32419 static winShmNode *winShmNodeList = 0;
   32420 
   32421 /*
   32422 ** Structure used internally by this VFS to record the state of an
   32423 ** open shared memory connection.
   32424 **
   32425 ** The following fields are initialized when this object is created and
   32426 ** are read-only thereafter:
   32427 **
   32428 **    winShm.pShmNode
   32429 **    winShm.id
   32430 **
   32431 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
   32432 ** while accessing any read/write fields.
   32433 */
   32434 struct winShm {
   32435   winShmNode *pShmNode;      /* The underlying winShmNode object */
   32436   winShm *pNext;             /* Next winShm with the same winShmNode */
   32437   u8 hasMutex;               /* True if holding the winShmNode mutex */
   32438   u16 sharedMask;            /* Mask of shared locks held */
   32439   u16 exclMask;              /* Mask of exclusive locks held */
   32440 #ifdef SQLITE_DEBUG
   32441   u8 id;                     /* Id of this connection with its winShmNode */
   32442 #endif
   32443 };
   32444 
   32445 /*
   32446 ** Constants used for locking
   32447 */
   32448 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
   32449 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   32450 
   32451 /*
   32452 ** Apply advisory locks for all n bytes beginning at ofst.
   32453 */
   32454 #define _SHM_UNLCK  1
   32455 #define _SHM_RDLCK  2
   32456 #define _SHM_WRLCK  3
   32457 static int winShmSystemLock(
   32458   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
   32459   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
   32460   int ofst,             /* Offset to first byte to be locked/unlocked */
   32461   int nByte             /* Number of bytes to lock or unlock */
   32462 ){
   32463   OVERLAPPED ovlp;
   32464   DWORD dwFlags;
   32465   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
   32466 
   32467   /* Access to the winShmNode object is serialized by the caller */
   32468   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
   32469 
   32470   /* Initialize the locking parameters */
   32471   dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
   32472   if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
   32473 
   32474   memset(&ovlp, 0, sizeof(OVERLAPPED));
   32475   ovlp.Offset = ofst;
   32476 
   32477   /* Release/Acquire the system-level lock */
   32478   if( lockType==_SHM_UNLCK ){
   32479     rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
   32480   }else{
   32481     rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
   32482   }
   32483 
   32484   if( rc!= 0 ){
   32485     rc = SQLITE_OK;
   32486   }else{
   32487     pFile->lastErrno =  GetLastError();
   32488     rc = SQLITE_BUSY;
   32489   }
   32490 
   32491   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
   32492            pFile->hFile.h,
   32493            rc==SQLITE_OK ? "ok" : "failed",
   32494            lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
   32495            pFile->lastErrno));
   32496 
   32497   return rc;
   32498 }
   32499 
   32500 /* Forward references to VFS methods */
   32501 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
   32502 static int winDelete(sqlite3_vfs *,const char*,int);
   32503 
   32504 /*
   32505 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
   32506 **
   32507 ** This is not a VFS shared-memory method; it is a utility function called
   32508 ** by VFS shared-memory methods.
   32509 */
   32510 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
   32511   winShmNode **pp;
   32512   winShmNode *p;
   32513   BOOL bRc;
   32514   assert( winShmMutexHeld() );
   32515   pp = &winShmNodeList;
   32516   while( (p = *pp)!=0 ){
   32517     if( p->nRef==0 ){
   32518       int i;
   32519       if( p->mutex ) sqlite3_mutex_free(p->mutex);
   32520       for(i=0; i<p->nRegion; i++){
   32521         bRc = UnmapViewOfFile(p->aRegion[i].pMap);
   32522         OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
   32523                  (int)GetCurrentProcessId(), i,
   32524                  bRc ? "ok" : "failed"));
   32525         bRc = CloseHandle(p->aRegion[i].hMap);
   32526         OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
   32527                  (int)GetCurrentProcessId(), i,
   32528                  bRc ? "ok" : "failed"));
   32529       }
   32530       if( p->hFile.h != INVALID_HANDLE_VALUE ){
   32531         SimulateIOErrorBenign(1);
   32532         winClose((sqlite3_file *)&p->hFile);
   32533         SimulateIOErrorBenign(0);
   32534       }
   32535       if( deleteFlag ){
   32536         SimulateIOErrorBenign(1);
   32537         winDelete(pVfs, p->zFilename, 0);
   32538         SimulateIOErrorBenign(0);
   32539       }
   32540       *pp = p->pNext;
   32541       sqlite3_free(p->aRegion);
   32542       sqlite3_free(p);
   32543     }else{
   32544       pp = &p->pNext;
   32545     }
   32546   }
   32547 }
   32548 
   32549 /*
   32550 ** Open the shared-memory area associated with database file pDbFd.
   32551 **
   32552 ** When opening a new shared-memory file, if no other instances of that
   32553 ** file are currently open, in this process or in other processes, then
   32554 ** the file must be truncated to zero length or have its header cleared.
   32555 */
   32556 static int winOpenSharedMemory(winFile *pDbFd){
   32557   struct winShm *p;                  /* The connection to be opened */
   32558   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
   32559   int rc;                            /* Result code */
   32560   struct winShmNode *pNew;           /* Newly allocated winShmNode */
   32561   int nName;                         /* Size of zName in bytes */
   32562 
   32563   assert( pDbFd->pShm==0 );    /* Not previously opened */
   32564 
   32565   /* Allocate space for the new sqlite3_shm object.  Also speculatively
   32566   ** allocate space for a new winShmNode and filename.
   32567   */
   32568   p = sqlite3_malloc( sizeof(*p) );
   32569   if( p==0 ) return SQLITE_NOMEM;
   32570   memset(p, 0, sizeof(*p));
   32571   nName = sqlite3Strlen30(pDbFd->zPath);
   32572   pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
   32573   if( pNew==0 ){
   32574     sqlite3_free(p);
   32575     return SQLITE_NOMEM;
   32576   }
   32577   memset(pNew, 0, sizeof(*pNew));
   32578   pNew->zFilename = (char*)&pNew[1];
   32579   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
   32580 
   32581   /* Look to see if there is an existing winShmNode that can be used.
   32582   ** If no matching winShmNode currently exists, create a new one.
   32583   */
   32584   winShmEnterMutex();
   32585   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
   32586     /* TBD need to come up with better match here.  Perhaps
   32587     ** use FILE_ID_BOTH_DIR_INFO Structure.
   32588     */
   32589     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
   32590   }
   32591   if( pShmNode ){
   32592     sqlite3_free(pNew);
   32593   }else{
   32594     pShmNode = pNew;
   32595     pNew = 0;
   32596     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
   32597     pShmNode->pNext = winShmNodeList;
   32598     winShmNodeList = pShmNode;
   32599 
   32600     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   32601     if( pShmNode->mutex==0 ){
   32602       rc = SQLITE_NOMEM;
   32603       goto shm_open_err;
   32604     }
   32605 
   32606     rc = winOpen(pDbFd->pVfs,
   32607                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
   32608                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
   32609                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
   32610                  0);
   32611     if( SQLITE_OK!=rc ){
   32612       rc = SQLITE_CANTOPEN_BKPT;
   32613       goto shm_open_err;
   32614     }
   32615 
   32616     /* Check to see if another process is holding the dead-man switch.
   32617     ** If not, truncate the file to zero length.
   32618     */
   32619     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
   32620       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
   32621       if( rc!=SQLITE_OK ){
   32622         rc = SQLITE_IOERR_SHMOPEN;
   32623       }
   32624     }
   32625     if( rc==SQLITE_OK ){
   32626       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
   32627       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
   32628     }
   32629     if( rc ) goto shm_open_err;
   32630   }
   32631 
   32632   /* Make the new connection a child of the winShmNode */
   32633   p->pShmNode = pShmNode;
   32634 #ifdef SQLITE_DEBUG
   32635   p->id = pShmNode->nextShmId++;
   32636 #endif
   32637   pShmNode->nRef++;
   32638   pDbFd->pShm = p;
   32639   winShmLeaveMutex();
   32640 
   32641   /* The reference count on pShmNode has already been incremented under
   32642   ** the cover of the winShmEnterMutex() mutex and the pointer from the
   32643   ** new (struct winShm) object to the pShmNode has been set. All that is
   32644   ** left to do is to link the new object into the linked list starting
   32645   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
   32646   ** mutex.
   32647   */
   32648   sqlite3_mutex_enter(pShmNode->mutex);
   32649   p->pNext = pShmNode->pFirst;
   32650   pShmNode->pFirst = p;
   32651   sqlite3_mutex_leave(pShmNode->mutex);
   32652   return SQLITE_OK;
   32653 
   32654   /* Jump here on any error */
   32655 shm_open_err:
   32656   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
   32657   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
   32658   sqlite3_free(p);
   32659   sqlite3_free(pNew);
   32660   winShmLeaveMutex();
   32661   return rc;
   32662 }
   32663 
   32664 /*
   32665 ** Close a connection to shared-memory.  Delete the underlying
   32666 ** storage if deleteFlag is true.
   32667 */
   32668 static int winShmUnmap(
   32669   sqlite3_file *fd,          /* Database holding shared memory */
   32670   int deleteFlag             /* Delete after closing if true */
   32671 ){
   32672   winFile *pDbFd;       /* Database holding shared-memory */
   32673   winShm *p;            /* The connection to be closed */
   32674   winShmNode *pShmNode; /* The underlying shared-memory file */
   32675   winShm **pp;          /* For looping over sibling connections */
   32676 
   32677   pDbFd = (winFile*)fd;
   32678   p = pDbFd->pShm;
   32679   if( p==0 ) return SQLITE_OK;
   32680   pShmNode = p->pShmNode;
   32681 
   32682   /* Remove connection p from the set of connections associated
   32683   ** with pShmNode */
   32684   sqlite3_mutex_enter(pShmNode->mutex);
   32685   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
   32686   *pp = p->pNext;
   32687 
   32688   /* Free the connection p */
   32689   sqlite3_free(p);
   32690   pDbFd->pShm = 0;
   32691   sqlite3_mutex_leave(pShmNode->mutex);
   32692 
   32693   /* If pShmNode->nRef has reached 0, then close the underlying
   32694   ** shared-memory file, too */
   32695   winShmEnterMutex();
   32696   assert( pShmNode->nRef>0 );
   32697   pShmNode->nRef--;
   32698   if( pShmNode->nRef==0 ){
   32699     winShmPurge(pDbFd->pVfs, deleteFlag);
   32700   }
   32701   winShmLeaveMutex();
   32702 
   32703   return SQLITE_OK;
   32704 }
   32705 
   32706 /*
   32707 ** Change the lock state for a shared-memory segment.
   32708 */
   32709 static int winShmLock(
   32710   sqlite3_file *fd,          /* Database file holding the shared memory */
   32711   int ofst,                  /* First lock to acquire or release */
   32712   int n,                     /* Number of locks to acquire or release */
   32713   int flags                  /* What to do with the lock */
   32714 ){
   32715   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
   32716   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
   32717   winShm *pX;                           /* For looping over all siblings */
   32718   winShmNode *pShmNode = p->pShmNode;
   32719   int rc = SQLITE_OK;                   /* Result code */
   32720   u16 mask;                             /* Mask of locks to take or release */
   32721 
   32722   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   32723   assert( n>=1 );
   32724   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   32725        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   32726        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   32727        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   32728   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   32729 
   32730   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
   32731   assert( n>1 || mask==(1<<ofst) );
   32732   sqlite3_mutex_enter(pShmNode->mutex);
   32733   if( flags & SQLITE_SHM_UNLOCK ){
   32734     u16 allMask = 0; /* Mask of locks held by siblings */
   32735 
   32736     /* See if any siblings hold this same lock */
   32737     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   32738       if( pX==p ) continue;
   32739       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   32740       allMask |= pX->sharedMask;
   32741     }
   32742 
   32743     /* Unlock the system-level locks */
   32744     if( (mask & allMask)==0 ){
   32745       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
   32746     }else{
   32747       rc = SQLITE_OK;
   32748     }
   32749 
   32750     /* Undo the local locks */
   32751     if( rc==SQLITE_OK ){
   32752       p->exclMask &= ~mask;
   32753       p->sharedMask &= ~mask;
   32754     }
   32755   }else if( flags & SQLITE_SHM_SHARED ){
   32756     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
   32757 
   32758     /* Find out which shared locks are already held by sibling connections.
   32759     ** If any sibling already holds an exclusive lock, go ahead and return
   32760     ** SQLITE_BUSY.
   32761     */
   32762     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   32763       if( (pX->exclMask & mask)!=0 ){
   32764         rc = SQLITE_BUSY;
   32765         break;
   32766       }
   32767       allShared |= pX->sharedMask;
   32768     }
   32769 
   32770     /* Get shared locks at the system level, if necessary */
   32771     if( rc==SQLITE_OK ){
   32772       if( (allShared & mask)==0 ){
   32773         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
   32774       }else{
   32775         rc = SQLITE_OK;
   32776       }
   32777     }
   32778 
   32779     /* Get the local shared locks */
   32780     if( rc==SQLITE_OK ){
   32781       p->sharedMask |= mask;
   32782     }
   32783   }else{
   32784     /* Make sure no sibling connections hold locks that will block this
   32785     ** lock.  If any do, return SQLITE_BUSY right away.
   32786     */
   32787     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   32788       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   32789         rc = SQLITE_BUSY;
   32790         break;
   32791       }
   32792     }
   32793 
   32794     /* Get the exclusive locks at the system level.  Then if successful
   32795     ** also mark the local connection as being locked.
   32796     */
   32797     if( rc==SQLITE_OK ){
   32798       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
   32799       if( rc==SQLITE_OK ){
   32800         assert( (p->sharedMask & mask)==0 );
   32801         p->exclMask |= mask;
   32802       }
   32803     }
   32804   }
   32805   sqlite3_mutex_leave(pShmNode->mutex);
   32806   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
   32807            p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
   32808            rc ? "failed" : "ok"));
   32809   return rc;
   32810 }
   32811 
   32812 /*
   32813 ** Implement a memory barrier or memory fence on shared memory.
   32814 **
   32815 ** All loads and stores begun before the barrier must complete before
   32816 ** any load or store begun after the barrier.
   32817 */
   32818 static void winShmBarrier(
   32819   sqlite3_file *fd          /* Database holding the shared memory */
   32820 ){
   32821   UNUSED_PARAMETER(fd);
   32822   /* MemoryBarrier(); // does not work -- do not know why not */
   32823   winShmEnterMutex();
   32824   winShmLeaveMutex();
   32825 }
   32826 
   32827 /*
   32828 ** This function is called to obtain a pointer to region iRegion of the
   32829 ** shared-memory associated with the database file fd. Shared-memory regions
   32830 ** are numbered starting from zero. Each shared-memory region is szRegion
   32831 ** bytes in size.
   32832 **
   32833 ** If an error occurs, an error code is returned and *pp is set to NULL.
   32834 **
   32835 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
   32836 ** region has not been allocated (by any client, including one running in a
   32837 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
   32838 ** isWrite is non-zero and the requested shared-memory region has not yet
   32839 ** been allocated, it is allocated by this function.
   32840 **
   32841 ** If the shared-memory region has already been allocated or is allocated by
   32842 ** this call as described above, then it is mapped into this processes
   32843 ** address space (if it is not already), *pp is set to point to the mapped
   32844 ** memory and SQLITE_OK returned.
   32845 */
   32846 static int winShmMap(
   32847   sqlite3_file *fd,               /* Handle open on database file */
   32848   int iRegion,                    /* Region to retrieve */
   32849   int szRegion,                   /* Size of regions */
   32850   int isWrite,                    /* True to extend file if necessary */
   32851   void volatile **pp              /* OUT: Mapped memory */
   32852 ){
   32853   winFile *pDbFd = (winFile*)fd;
   32854   winShm *p = pDbFd->pShm;
   32855   winShmNode *pShmNode;
   32856   int rc = SQLITE_OK;
   32857 
   32858   if( !p ){
   32859     rc = winOpenSharedMemory(pDbFd);
   32860     if( rc!=SQLITE_OK ) return rc;
   32861     p = pDbFd->pShm;
   32862   }
   32863   pShmNode = p->pShmNode;
   32864 
   32865   sqlite3_mutex_enter(pShmNode->mutex);
   32866   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
   32867 
   32868   if( pShmNode->nRegion<=iRegion ){
   32869     struct ShmRegion *apNew;           /* New aRegion[] array */
   32870     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
   32871     sqlite3_int64 sz;                  /* Current size of wal-index file */
   32872 
   32873     pShmNode->szRegion = szRegion;
   32874 
   32875     /* The requested region is not mapped into this processes address space.
   32876     ** Check to see if it has been allocated (i.e. if the wal-index file is
   32877     ** large enough to contain the requested region).
   32878     */
   32879     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
   32880     if( rc!=SQLITE_OK ){
   32881       rc = SQLITE_IOERR_SHMSIZE;
   32882       goto shmpage_out;
   32883     }
   32884 
   32885     if( sz<nByte ){
   32886       /* The requested memory region does not exist. If isWrite is set to
   32887       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
   32888       **
   32889       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
   32890       ** the requested memory region.
   32891       */
   32892       if( !isWrite ) goto shmpage_out;
   32893       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
   32894       if( rc!=SQLITE_OK ){
   32895         rc = SQLITE_IOERR_SHMSIZE;
   32896         goto shmpage_out;
   32897       }
   32898     }
   32899 
   32900     /* Map the requested memory region into this processes address space. */
   32901     apNew = (struct ShmRegion *)sqlite3_realloc(
   32902         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
   32903     );
   32904     if( !apNew ){
   32905       rc = SQLITE_IOERR_NOMEM;
   32906       goto shmpage_out;
   32907     }
   32908     pShmNode->aRegion = apNew;
   32909 
   32910     while( pShmNode->nRegion<=iRegion ){
   32911       HANDLE hMap;                /* file-mapping handle */
   32912       void *pMap = 0;             /* Mapped memory region */
   32913 
   32914       hMap = CreateFileMapping(pShmNode->hFile.h,
   32915           NULL, PAGE_READWRITE, 0, nByte, NULL
   32916       );
   32917       OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
   32918                (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
   32919                hMap ? "ok" : "failed"));
   32920       if( hMap ){
   32921         int iOffset = pShmNode->nRegion*szRegion;
   32922         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
   32923         pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
   32924             0, iOffset - iOffsetShift, szRegion + iOffsetShift
   32925         );
   32926         OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
   32927                  (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
   32928                  pMap ? "ok" : "failed"));
   32929       }
   32930       if( !pMap ){
   32931         pShmNode->lastErrno = GetLastError();
   32932         rc = SQLITE_IOERR;
   32933         if( hMap ) CloseHandle(hMap);
   32934         goto shmpage_out;
   32935       }
   32936 
   32937       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
   32938       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
   32939       pShmNode->nRegion++;
   32940     }
   32941   }
   32942 
   32943 shmpage_out:
   32944   if( pShmNode->nRegion>iRegion ){
   32945     int iOffset = iRegion*szRegion;
   32946     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
   32947     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
   32948     *pp = (void *)&p[iOffsetShift];
   32949   }else{
   32950     *pp = 0;
   32951   }
   32952   sqlite3_mutex_leave(pShmNode->mutex);
   32953   return rc;
   32954 }
   32955 
   32956 #else
   32957 # define winShmMap     0
   32958 # define winShmLock    0
   32959 # define winShmBarrier 0
   32960 # define winShmUnmap   0
   32961 #endif /* #ifndef SQLITE_OMIT_WAL */
   32962 
   32963 /*
   32964 ** Here ends the implementation of all sqlite3_file methods.
   32965 **
   32966 ********************** End sqlite3_file Methods *******************************
   32967 ******************************************************************************/
   32968 
   32969 /*
   32970 ** This vector defines all the methods that can operate on an
   32971 ** sqlite3_file for win32.
   32972 */
   32973 static const sqlite3_io_methods winIoMethod = {
   32974   2,                              /* iVersion */
   32975   winClose,                       /* xClose */
   32976   winRead,                        /* xRead */
   32977   winWrite,                       /* xWrite */
   32978   winTruncate,                    /* xTruncate */
   32979   winSync,                        /* xSync */
   32980   winFileSize,                    /* xFileSize */
   32981   winLock,                        /* xLock */
   32982   winUnlock,                      /* xUnlock */
   32983   winCheckReservedLock,           /* xCheckReservedLock */
   32984   winFileControl,                 /* xFileControl */
   32985   winSectorSize,                  /* xSectorSize */
   32986   winDeviceCharacteristics,       /* xDeviceCharacteristics */
   32987   winShmMap,                      /* xShmMap */
   32988   winShmLock,                     /* xShmLock */
   32989   winShmBarrier,                  /* xShmBarrier */
   32990   winShmUnmap                     /* xShmUnmap */
   32991 };
   32992 
   32993 /****************************************************************************
   32994 **************************** sqlite3_vfs methods ****************************
   32995 **
   32996 ** This division contains the implementation of methods on the
   32997 ** sqlite3_vfs object.
   32998 */
   32999 
   33000 /*
   33001 ** Convert a UTF-8 filename into whatever form the underlying
   33002 ** operating system wants filenames in.  Space to hold the result
   33003 ** is obtained from malloc and must be freed by the calling
   33004 ** function.
   33005 */
   33006 static void *convertUtf8Filename(const char *zFilename){
   33007   void *zConverted = 0;
   33008   if( isNT() ){
   33009     zConverted = utf8ToUnicode(zFilename);
   33010 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33011 */
   33012 #if SQLITE_OS_WINCE==0
   33013   }else{
   33014     zConverted = utf8ToMbcs(zFilename);
   33015 #endif
   33016   }
   33017   /* caller will handle out of memory */
   33018   return zConverted;
   33019 }
   33020 
   33021 /*
   33022 ** Create a temporary file name in zBuf.  zBuf must be big enough to
   33023 ** hold at pVfs->mxPathname characters.
   33024 */
   33025 static int getTempname(int nBuf, char *zBuf){
   33026   static char zChars[] =
   33027     "abcdefghijklmnopqrstuvwxyz"
   33028     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   33029     "0123456789";
   33030   size_t i, j;
   33031   char zTempPath[MAX_PATH+1];
   33032 
   33033   /* It's odd to simulate an io-error here, but really this is just
   33034   ** using the io-error infrastructure to test that SQLite handles this
   33035   ** function failing.
   33036   */
   33037   SimulateIOError( return SQLITE_IOERR );
   33038 
   33039   if( sqlite3_temp_directory ){
   33040     sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
   33041   }else if( isNT() ){
   33042     char *zMulti;
   33043     WCHAR zWidePath[MAX_PATH];
   33044     GetTempPathW(MAX_PATH-30, zWidePath);
   33045     zMulti = unicodeToUtf8(zWidePath);
   33046     if( zMulti ){
   33047       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
   33048       free(zMulti);
   33049     }else{
   33050       return SQLITE_NOMEM;
   33051     }
   33052 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33053 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33054 ** it's important to not reference them for WINCE builds.
   33055 */
   33056 #if SQLITE_OS_WINCE==0
   33057   }else{
   33058     char *zUtf8;
   33059     char zMbcsPath[MAX_PATH];
   33060     GetTempPathA(MAX_PATH-30, zMbcsPath);
   33061     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
   33062     if( zUtf8 ){
   33063       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
   33064       free(zUtf8);
   33065     }else{
   33066       return SQLITE_NOMEM;
   33067     }
   33068 #endif
   33069   }
   33070 
   33071   /* Check that the output buffer is large enough for the temporary file
   33072   ** name. If it is not, return SQLITE_ERROR.
   33073   */
   33074   if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
   33075     return SQLITE_ERROR;
   33076   }
   33077 
   33078   for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
   33079   zTempPath[i] = 0;
   33080 
   33081   sqlite3_snprintf(nBuf-17, zBuf,
   33082                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
   33083   j = sqlite3Strlen30(zBuf);
   33084   sqlite3_randomness(15, &zBuf[j]);
   33085   for(i=0; i<15; i++, j++){
   33086     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   33087   }
   33088   zBuf[j] = 0;
   33089 
   33090   OSTRACE(("TEMP FILENAME: %s\n", zBuf));
   33091   return SQLITE_OK;
   33092 }
   33093 
   33094 /*
   33095 ** The return value of getLastErrorMsg
   33096 ** is zero if the error message fits in the buffer, or non-zero
   33097 ** otherwise (if the message was truncated).
   33098 */
   33099 static int getLastErrorMsg(int nBuf, char *zBuf){
   33100   /* FormatMessage returns 0 on failure.  Otherwise it
   33101   ** returns the number of TCHARs written to the output
   33102   ** buffer, excluding the terminating null char.
   33103   */
   33104   DWORD error = GetLastError();
   33105   DWORD dwLen = 0;
   33106   char *zOut = 0;
   33107 
   33108   if( isNT() ){
   33109     WCHAR *zTempWide = NULL;
   33110     dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
   33111                            NULL,
   33112                            error,
   33113                            0,
   33114                            (LPWSTR) &zTempWide,
   33115                            0,
   33116                            0);
   33117     if( dwLen > 0 ){
   33118       /* allocate a buffer and convert to UTF8 */
   33119       zOut = unicodeToUtf8(zTempWide);
   33120       /* free the system buffer allocated by FormatMessage */
   33121       LocalFree(zTempWide);
   33122     }
   33123 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33124 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33125 ** it's important to not reference them for WINCE builds.
   33126 */
   33127 #if SQLITE_OS_WINCE==0
   33128   }else{
   33129     char *zTemp = NULL;
   33130     dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
   33131                            NULL,
   33132                            error,
   33133                            0,
   33134                            (LPSTR) &zTemp,
   33135                            0,
   33136                            0);
   33137     if( dwLen > 0 ){
   33138       /* allocate a buffer and convert to UTF8 */
   33139       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
   33140       /* free the system buffer allocated by FormatMessage */
   33141       LocalFree(zTemp);
   33142     }
   33143 #endif
   33144   }
   33145   if( 0 == dwLen ){
   33146     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
   33147   }else{
   33148     /* copy a maximum of nBuf chars to output buffer */
   33149     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
   33150     /* free the UTF8 buffer */
   33151     free(zOut);
   33152   }
   33153   return 0;
   33154 }
   33155 
   33156 /*
   33157 ** Open a file.
   33158 */
   33159 static int winOpen(
   33160   sqlite3_vfs *pVfs,        /* Not used */
   33161   const char *zName,        /* Name of the file (UTF-8) */
   33162   sqlite3_file *id,         /* Write the SQLite file handle here */
   33163   int flags,                /* Open mode flags */
   33164   int *pOutFlags            /* Status return flags */
   33165 ){
   33166   HANDLE h;
   33167   DWORD dwDesiredAccess;
   33168   DWORD dwShareMode;
   33169   DWORD dwCreationDisposition;
   33170   DWORD dwFlagsAndAttributes = 0;
   33171 #if SQLITE_OS_WINCE
   33172   int isTemp = 0;
   33173 #endif
   33174   winFile *pFile = (winFile*)id;
   33175   void *zConverted;              /* Filename in OS encoding */
   33176   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
   33177 
   33178   /* If argument zPath is a NULL pointer, this function is required to open
   33179   ** a temporary file. Use this buffer to store the file name in.
   33180   */
   33181   char zTmpname[MAX_PATH+1];     /* Buffer used to create temp filename */
   33182 
   33183   int rc = SQLITE_OK;            /* Function Return Code */
   33184 #if !defined(NDEBUG) || SQLITE_OS_WINCE
   33185   int eType = flags&0xFFFFFF00;  /* Type of file to open */
   33186 #endif
   33187 
   33188   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   33189   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   33190   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   33191 #ifndef NDEBUG
   33192   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   33193 #endif
   33194   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   33195 
   33196 #ifndef NDEBUG
   33197   int isOpenJournal = (isCreate && (
   33198         eType==SQLITE_OPEN_MASTER_JOURNAL
   33199      || eType==SQLITE_OPEN_MAIN_JOURNAL
   33200      || eType==SQLITE_OPEN_WAL
   33201   ));
   33202 #endif
   33203 
   33204   /* Check the following statements are true:
   33205   **
   33206   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
   33207   **   (b) if CREATE is set, then READWRITE must also be set, and
   33208   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   33209   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   33210   */
   33211   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   33212   assert(isCreate==0 || isReadWrite);
   33213   assert(isExclusive==0 || isCreate);
   33214   assert(isDelete==0 || isCreate);
   33215 
   33216   /* The main DB, main journal, WAL file and master journal are never
   33217   ** automatically deleted. Nor are they ever temporary files.  */
   33218   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   33219   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   33220   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   33221   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   33222 
   33223   /* Assert that the upper layer has set one of the "file-type" flags. */
   33224   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
   33225        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
   33226        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
   33227        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   33228   );
   33229 
   33230   assert( id!=0 );
   33231   UNUSED_PARAMETER(pVfs);
   33232 
   33233   pFile->h = INVALID_HANDLE_VALUE;
   33234 
   33235   /* If the second argument to this function is NULL, generate a
   33236   ** temporary file name to use
   33237   */
   33238   if( !zUtf8Name ){
   33239     assert(isDelete && !isOpenJournal);
   33240     rc = getTempname(MAX_PATH+1, zTmpname);
   33241     if( rc!=SQLITE_OK ){
   33242       return rc;
   33243     }
   33244     zUtf8Name = zTmpname;
   33245   }
   33246 
   33247   /* Convert the filename to the system encoding. */
   33248   zConverted = convertUtf8Filename(zUtf8Name);
   33249   if( zConverted==0 ){
   33250     return SQLITE_NOMEM;
   33251   }
   33252 
   33253   if( isReadWrite ){
   33254     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
   33255   }else{
   33256     dwDesiredAccess = GENERIC_READ;
   33257   }
   33258 
   33259   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
   33260   ** created. SQLite doesn't use it to indicate "exclusive access"
   33261   ** as it is usually understood.
   33262   */
   33263   if( isExclusive ){
   33264     /* Creates a new file, only if it does not already exist. */
   33265     /* If the file exists, it fails. */
   33266     dwCreationDisposition = CREATE_NEW;
   33267   }else if( isCreate ){
   33268     /* Open existing file, or create if it doesn't exist */
   33269     dwCreationDisposition = OPEN_ALWAYS;
   33270   }else{
   33271     /* Opens a file, only if it exists. */
   33272     dwCreationDisposition = OPEN_EXISTING;
   33273   }
   33274 
   33275   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
   33276 
   33277   if( isDelete ){
   33278 #if SQLITE_OS_WINCE
   33279     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
   33280     isTemp = 1;
   33281 #else
   33282     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
   33283                                | FILE_ATTRIBUTE_HIDDEN
   33284                                | FILE_FLAG_DELETE_ON_CLOSE;
   33285 #endif
   33286   }else{
   33287     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
   33288   }
   33289   /* Reports from the internet are that performance is always
   33290   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
   33291 #if SQLITE_OS_WINCE
   33292   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
   33293 #endif
   33294 
   33295   if( isNT() ){
   33296     h = CreateFileW((WCHAR*)zConverted,
   33297        dwDesiredAccess,
   33298        dwShareMode,
   33299        NULL,
   33300        dwCreationDisposition,
   33301        dwFlagsAndAttributes,
   33302        NULL
   33303     );
   33304 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33305 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33306 ** it's important to not reference them for WINCE builds.
   33307 */
   33308 #if SQLITE_OS_WINCE==0
   33309   }else{
   33310     h = CreateFileA((char*)zConverted,
   33311        dwDesiredAccess,
   33312        dwShareMode,
   33313        NULL,
   33314        dwCreationDisposition,
   33315        dwFlagsAndAttributes,
   33316        NULL
   33317     );
   33318 #endif
   33319   }
   33320 
   33321   OSTRACE(("OPEN %d %s 0x%lx %s\n",
   33322            h, zName, dwDesiredAccess,
   33323            h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
   33324 
   33325   if( h==INVALID_HANDLE_VALUE ){
   33326     pFile->lastErrno = GetLastError();
   33327     free(zConverted);
   33328     if( isReadWrite ){
   33329       return winOpen(pVfs, zName, id,
   33330              ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
   33331     }else{
   33332       return SQLITE_CANTOPEN_BKPT;
   33333     }
   33334   }
   33335 
   33336   if( pOutFlags ){
   33337     if( isReadWrite ){
   33338       *pOutFlags = SQLITE_OPEN_READWRITE;
   33339     }else{
   33340       *pOutFlags = SQLITE_OPEN_READONLY;
   33341     }
   33342   }
   33343 
   33344   memset(pFile, 0, sizeof(*pFile));
   33345   pFile->pMethod = &winIoMethod;
   33346   pFile->h = h;
   33347   pFile->lastErrno = NO_ERROR;
   33348   pFile->pVfs = pVfs;
   33349   pFile->pShm = 0;
   33350   pFile->zPath = zName;
   33351   pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
   33352 
   33353 #if SQLITE_OS_WINCE
   33354   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
   33355        && !winceCreateLock(zName, pFile)
   33356   ){
   33357     CloseHandle(h);
   33358     free(zConverted);
   33359     return SQLITE_CANTOPEN_BKPT;
   33360   }
   33361   if( isTemp ){
   33362     pFile->zDeleteOnClose = zConverted;
   33363   }else
   33364 #endif
   33365   {
   33366     free(zConverted);
   33367   }
   33368 
   33369   OpenCounter(+1);
   33370   return rc;
   33371 }
   33372 
   33373 /*
   33374 ** Delete the named file.
   33375 **
   33376 ** Note that windows does not allow a file to be deleted if some other
   33377 ** process has it open.  Sometimes a virus scanner or indexing program
   33378 ** will open a journal file shortly after it is created in order to do
   33379 ** whatever it does.  While this other process is holding the
   33380 ** file open, we will be unable to delete it.  To work around this
   33381 ** problem, we delay 100 milliseconds and try to delete again.  Up
   33382 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
   33383 ** up and returning an error.
   33384 */
   33385 #define MX_DELETION_ATTEMPTS 5
   33386 static int winDelete(
   33387   sqlite3_vfs *pVfs,          /* Not used on win32 */
   33388   const char *zFilename,      /* Name of file to delete */
   33389   int syncDir                 /* Not used on win32 */
   33390 ){
   33391   int cnt = 0;
   33392   DWORD rc;
   33393   DWORD error = 0;
   33394   void *zConverted;
   33395   UNUSED_PARAMETER(pVfs);
   33396   UNUSED_PARAMETER(syncDir);
   33397 
   33398   SimulateIOError(return SQLITE_IOERR_DELETE);
   33399   zConverted = convertUtf8Filename(zFilename);
   33400   if( zConverted==0 ){
   33401     return SQLITE_NOMEM;
   33402   }
   33403   if( isNT() ){
   33404     do{
   33405       DeleteFileW(zConverted);
   33406     }while(   (   ((rc = GetFileAttributesW(zConverted)) != INVALID_FILE_ATTRIBUTES)
   33407                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
   33408            && (++cnt < MX_DELETION_ATTEMPTS)
   33409            && (Sleep(100), 1) );
   33410 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33411 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33412 ** it's important to not reference them for WINCE builds.
   33413 */
   33414 #if SQLITE_OS_WINCE==0
   33415   }else{
   33416     do{
   33417       DeleteFileA(zConverted);
   33418     }while(   (   ((rc = GetFileAttributesA(zConverted)) != INVALID_FILE_ATTRIBUTES)
   33419                || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
   33420            && (++cnt < MX_DELETION_ATTEMPTS)
   33421            && (Sleep(100), 1) );
   33422 #endif
   33423   }
   33424   free(zConverted);
   33425   OSTRACE(("DELETE \"%s\" %s\n", zFilename,
   33426        ( (rc==INVALID_FILE_ATTRIBUTES) && (error==ERROR_FILE_NOT_FOUND)) ?
   33427          "ok" : "failed" ));
   33428 
   33429   return (   (rc == INVALID_FILE_ATTRIBUTES)
   33430           && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
   33431 }
   33432 
   33433 /*
   33434 ** Check the existance and status of a file.
   33435 */
   33436 static int winAccess(
   33437   sqlite3_vfs *pVfs,         /* Not used on win32 */
   33438   const char *zFilename,     /* Name of file to check */
   33439   int flags,                 /* Type of test to make on this file */
   33440   int *pResOut               /* OUT: Result */
   33441 ){
   33442   DWORD attr;
   33443   int rc = 0;
   33444   void *zConverted;
   33445   UNUSED_PARAMETER(pVfs);
   33446 
   33447   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   33448   zConverted = convertUtf8Filename(zFilename);
   33449   if( zConverted==0 ){
   33450     return SQLITE_NOMEM;
   33451   }
   33452   if( isNT() ){
   33453     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
   33454     memset(&sAttrData, 0, sizeof(sAttrData));
   33455     if( GetFileAttributesExW((WCHAR*)zConverted,
   33456                              GetFileExInfoStandard,
   33457                              &sAttrData) ){
   33458       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
   33459       ** as if it does not exist.
   33460       */
   33461       if(    flags==SQLITE_ACCESS_EXISTS
   33462           && sAttrData.nFileSizeHigh==0
   33463           && sAttrData.nFileSizeLow==0 ){
   33464         attr = INVALID_FILE_ATTRIBUTES;
   33465       }else{
   33466         attr = sAttrData.dwFileAttributes;
   33467       }
   33468     }else{
   33469       if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
   33470         free(zConverted);
   33471         return SQLITE_IOERR_ACCESS;
   33472       }else{
   33473         attr = INVALID_FILE_ATTRIBUTES;
   33474       }
   33475     }
   33476 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33477 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33478 ** it's important to not reference them for WINCE builds.
   33479 */
   33480 #if SQLITE_OS_WINCE==0
   33481   }else{
   33482     attr = GetFileAttributesA((char*)zConverted);
   33483 #endif
   33484   }
   33485   free(zConverted);
   33486   switch( flags ){
   33487     case SQLITE_ACCESS_READ:
   33488     case SQLITE_ACCESS_EXISTS:
   33489       rc = attr!=INVALID_FILE_ATTRIBUTES;
   33490       break;
   33491     case SQLITE_ACCESS_READWRITE:
   33492       rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
   33493       break;
   33494     default:
   33495       assert(!"Invalid flags argument");
   33496   }
   33497   *pResOut = rc;
   33498   return SQLITE_OK;
   33499 }
   33500 
   33501 
   33502 /*
   33503 ** Turn a relative pathname into a full pathname.  Write the full
   33504 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
   33505 ** bytes in size.
   33506 */
   33507 static int winFullPathname(
   33508   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
   33509   const char *zRelative,        /* Possibly relative input path */
   33510   int nFull,                    /* Size of output buffer in bytes */
   33511   char *zFull                   /* Output buffer */
   33512 ){
   33513 
   33514 #if defined(__CYGWIN__)
   33515   SimulateIOError( return SQLITE_ERROR );
   33516   UNUSED_PARAMETER(nFull);
   33517   cygwin_conv_to_full_win32_path(zRelative, zFull);
   33518   return SQLITE_OK;
   33519 #endif
   33520 
   33521 #if SQLITE_OS_WINCE
   33522   SimulateIOError( return SQLITE_ERROR );
   33523   UNUSED_PARAMETER(nFull);
   33524   /* WinCE has no concept of a relative pathname, or so I am told. */
   33525   sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
   33526   return SQLITE_OK;
   33527 #endif
   33528 
   33529 #if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
   33530   int nByte;
   33531   void *zConverted;
   33532   char *zOut;
   33533 
   33534   /* It's odd to simulate an io-error here, but really this is just
   33535   ** using the io-error infrastructure to test that SQLite handles this
   33536   ** function failing. This function could fail if, for example, the
   33537   ** current working directory has been unlinked.
   33538   */
   33539   SimulateIOError( return SQLITE_ERROR );
   33540   UNUSED_PARAMETER(nFull);
   33541   zConverted = convertUtf8Filename(zRelative);
   33542   if( isNT() ){
   33543     WCHAR *zTemp;
   33544     nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
   33545     zTemp = malloc( nByte*sizeof(zTemp[0]) );
   33546     if( zTemp==0 ){
   33547       free(zConverted);
   33548       return SQLITE_NOMEM;
   33549     }
   33550     GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
   33551     free(zConverted);
   33552     zOut = unicodeToUtf8(zTemp);
   33553     free(zTemp);
   33554 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33555 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33556 ** it's important to not reference them for WINCE builds.
   33557 */
   33558 #if SQLITE_OS_WINCE==0
   33559   }else{
   33560     char *zTemp;
   33561     nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
   33562     zTemp = malloc( nByte*sizeof(zTemp[0]) );
   33563     if( zTemp==0 ){
   33564       free(zConverted);
   33565       return SQLITE_NOMEM;
   33566     }
   33567     GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
   33568     free(zConverted);
   33569     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
   33570     free(zTemp);
   33571 #endif
   33572   }
   33573   if( zOut ){
   33574     sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
   33575     free(zOut);
   33576     return SQLITE_OK;
   33577   }else{
   33578     return SQLITE_NOMEM;
   33579   }
   33580 #endif
   33581 }
   33582 
   33583 /*
   33584 ** Get the sector size of the device used to store
   33585 ** file.
   33586 */
   33587 static int getSectorSize(
   33588     sqlite3_vfs *pVfs,
   33589     const char *zRelative     /* UTF-8 file name */
   33590 ){
   33591   DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
   33592   /* GetDiskFreeSpace is not supported under WINCE */
   33593 #if SQLITE_OS_WINCE
   33594   UNUSED_PARAMETER(pVfs);
   33595   UNUSED_PARAMETER(zRelative);
   33596 #else
   33597   char zFullpath[MAX_PATH+1];
   33598   int rc;
   33599   DWORD dwRet = 0;
   33600   DWORD dwDummy;
   33601 
   33602   /*
   33603   ** We need to get the full path name of the file
   33604   ** to get the drive letter to look up the sector
   33605   ** size.
   33606   */
   33607   SimulateIOErrorBenign(1);
   33608   rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
   33609   SimulateIOErrorBenign(0);
   33610   if( rc == SQLITE_OK )
   33611   {
   33612     void *zConverted = convertUtf8Filename(zFullpath);
   33613     if( zConverted ){
   33614       if( isNT() ){
   33615         /* trim path to just drive reference */
   33616         WCHAR *p = zConverted;
   33617         for(;*p;p++){
   33618           if( *p == '\\' ){
   33619             *p = '\0';
   33620             break;
   33621           }
   33622         }
   33623         dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
   33624                                   &dwDummy,
   33625                                   &bytesPerSector,
   33626                                   &dwDummy,
   33627                                   &dwDummy);
   33628       }else{
   33629         /* trim path to just drive reference */
   33630         char *p = (char *)zConverted;
   33631         for(;*p;p++){
   33632           if( *p == '\\' ){
   33633             *p = '\0';
   33634             break;
   33635           }
   33636         }
   33637         dwRet = GetDiskFreeSpaceA((char*)zConverted,
   33638                                   &dwDummy,
   33639                                   &bytesPerSector,
   33640                                   &dwDummy,
   33641                                   &dwDummy);
   33642       }
   33643       free(zConverted);
   33644     }
   33645     if( !dwRet ){
   33646       bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
   33647     }
   33648   }
   33649 #endif
   33650   return (int) bytesPerSector;
   33651 }
   33652 
   33653 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   33654 /*
   33655 ** Interfaces for opening a shared library, finding entry points
   33656 ** within the shared library, and closing the shared library.
   33657 */
   33658 /*
   33659 ** Interfaces for opening a shared library, finding entry points
   33660 ** within the shared library, and closing the shared library.
   33661 */
   33662 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   33663   HANDLE h;
   33664   void *zConverted = convertUtf8Filename(zFilename);
   33665   UNUSED_PARAMETER(pVfs);
   33666   if( zConverted==0 ){
   33667     return 0;
   33668   }
   33669   if( isNT() ){
   33670     h = LoadLibraryW((WCHAR*)zConverted);
   33671 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
   33672 ** Since the ASCII version of these Windows API do not exist for WINCE,
   33673 ** it's important to not reference them for WINCE builds.
   33674 */
   33675 #if SQLITE_OS_WINCE==0
   33676   }else{
   33677     h = LoadLibraryA((char*)zConverted);
   33678 #endif
   33679   }
   33680   free(zConverted);
   33681   return (void*)h;
   33682 }
   33683 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
   33684   UNUSED_PARAMETER(pVfs);
   33685   getLastErrorMsg(nBuf, zBufOut);
   33686 }
   33687 void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
   33688   UNUSED_PARAMETER(pVfs);
   33689 #if SQLITE_OS_WINCE
   33690   /* The GetProcAddressA() routine is only available on wince. */
   33691   return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
   33692 #else
   33693   /* All other windows platforms expect GetProcAddress() to take
   33694   ** an Ansi string regardless of the _UNICODE setting */
   33695   return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
   33696 #endif
   33697 }
   33698 void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
   33699   UNUSED_PARAMETER(pVfs);
   33700   FreeLibrary((HANDLE)pHandle);
   33701 }
   33702 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   33703   #define winDlOpen  0
   33704   #define winDlError 0
   33705   #define winDlSym   0
   33706   #define winDlClose 0
   33707 #endif
   33708 
   33709 
   33710 /*
   33711 ** Write up to nBuf bytes of randomness into zBuf.
   33712 */
   33713 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   33714   int n = 0;
   33715   UNUSED_PARAMETER(pVfs);
   33716 #if defined(SQLITE_TEST)
   33717   n = nBuf;
   33718   memset(zBuf, 0, nBuf);
   33719 #else
   33720   if( sizeof(SYSTEMTIME)<=nBuf-n ){
   33721     SYSTEMTIME x;
   33722     GetSystemTime(&x);
   33723     memcpy(&zBuf[n], &x, sizeof(x));
   33724     n += sizeof(x);
   33725   }
   33726   if( sizeof(DWORD)<=nBuf-n ){
   33727     DWORD pid = GetCurrentProcessId();
   33728     memcpy(&zBuf[n], &pid, sizeof(pid));
   33729     n += sizeof(pid);
   33730   }
   33731   if( sizeof(DWORD)<=nBuf-n ){
   33732     DWORD cnt = GetTickCount();
   33733     memcpy(&zBuf[n], &cnt, sizeof(cnt));
   33734     n += sizeof(cnt);
   33735   }
   33736   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
   33737     LARGE_INTEGER i;
   33738     QueryPerformanceCounter(&i);
   33739     memcpy(&zBuf[n], &i, sizeof(i));
   33740     n += sizeof(i);
   33741   }
   33742 #endif
   33743   return n;
   33744 }
   33745 
   33746 
   33747 /*
   33748 ** Sleep for a little while.  Return the amount of time slept.
   33749 */
   33750 static int winSleep(sqlite3_vfs *pVfs, int microsec){
   33751   Sleep((microsec+999)/1000);
   33752   UNUSED_PARAMETER(pVfs);
   33753   return ((microsec+999)/1000)*1000;
   33754 }
   33755 
   33756 /*
   33757 ** The following variable, if set to a non-zero value, is interpreted as
   33758 ** the number of seconds since 1970 and is used to set the result of
   33759 ** sqlite3OsCurrentTime() during testing.
   33760 */
   33761 #ifdef SQLITE_TEST
   33762 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
   33763 #endif
   33764 
   33765 /*
   33766 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   33767 ** the current time and date as a Julian Day number times 86_400_000.  In
   33768 ** other words, write into *piNow the number of milliseconds since the Julian
   33769 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   33770 ** proleptic Gregorian calendar.
   33771 **
   33772 ** On success, return 0.  Return 1 if the time and date cannot be found.
   33773 */
   33774 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
   33775   /* FILETIME structure is a 64-bit value representing the number of
   33776      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
   33777   */
   33778   FILETIME ft;
   33779   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
   33780 #ifdef SQLITE_TEST
   33781   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   33782 #endif
   33783   /* 2^32 - to avoid use of LL and warnings in gcc */
   33784   static const sqlite3_int64 max32BitValue =
   33785       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
   33786 
   33787 #if SQLITE_OS_WINCE
   33788   SYSTEMTIME time;
   33789   GetSystemTime(&time);
   33790   /* if SystemTimeToFileTime() fails, it returns zero. */
   33791   if (!SystemTimeToFileTime(&time,&ft)){
   33792     return 1;
   33793   }
   33794 #else
   33795   GetSystemTimeAsFileTime( &ft );
   33796 #endif
   33797 
   33798   *piNow = winFiletimeEpoch +
   33799             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
   33800                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
   33801 
   33802 #ifdef SQLITE_TEST
   33803   if( sqlite3_current_time ){
   33804     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   33805   }
   33806 #endif
   33807   UNUSED_PARAMETER(pVfs);
   33808   return 0;
   33809 }
   33810 
   33811 /*
   33812 ** Find the current time (in Universal Coordinated Time).  Write the
   33813 ** current time and date as a Julian Day number into *prNow and
   33814 ** return 0.  Return 1 if the time and date cannot be found.
   33815 */
   33816 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
   33817   int rc;
   33818   sqlite3_int64 i;
   33819   rc = winCurrentTimeInt64(pVfs, &i);
   33820   if( !rc ){
   33821     *prNow = i/86400000.0;
   33822   }
   33823   return rc;
   33824 }
   33825 
   33826 /*
   33827 ** The idea is that this function works like a combination of
   33828 ** GetLastError() and FormatMessage() on windows (or errno and
   33829 ** strerror_r() on unix). After an error is returned by an OS
   33830 ** function, SQLite calls this function with zBuf pointing to
   33831 ** a buffer of nBuf bytes. The OS layer should populate the
   33832 ** buffer with a nul-terminated UTF-8 encoded error message
   33833 ** describing the last IO error to have occurred within the calling
   33834 ** thread.
   33835 **
   33836 ** If the error message is too large for the supplied buffer,
   33837 ** it should be truncated. The return value of xGetLastError
   33838 ** is zero if the error message fits in the buffer, or non-zero
   33839 ** otherwise (if the message was truncated). If non-zero is returned,
   33840 ** then it is not necessary to include the nul-terminator character
   33841 ** in the output buffer.
   33842 **
   33843 ** Not supplying an error message will have no adverse effect
   33844 ** on SQLite. It is fine to have an implementation that never
   33845 ** returns an error message:
   33846 **
   33847 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   33848 **     assert(zBuf[0]=='\0');
   33849 **     return 0;
   33850 **   }
   33851 **
   33852 ** However if an error message is supplied, it will be incorporated
   33853 ** by sqlite into the error message available to the user using
   33854 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
   33855 */
   33856 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   33857   UNUSED_PARAMETER(pVfs);
   33858   return getLastErrorMsg(nBuf, zBuf);
   33859 }
   33860 
   33861 
   33862 
   33863 /*
   33864 ** Initialize and deinitialize the operating system interface.
   33865 */
   33866 SQLITE_API int sqlite3_os_init(void){
   33867   static sqlite3_vfs winVfs = {
   33868     3,                   /* iVersion */
   33869     sizeof(winFile),     /* szOsFile */
   33870     MAX_PATH,            /* mxPathname */
   33871     0,                   /* pNext */
   33872     "win32",             /* zName */
   33873     0,                   /* pAppData */
   33874     winOpen,             /* xOpen */
   33875     winDelete,           /* xDelete */
   33876     winAccess,           /* xAccess */
   33877     winFullPathname,     /* xFullPathname */
   33878     winDlOpen,           /* xDlOpen */
   33879     winDlError,          /* xDlError */
   33880     winDlSym,            /* xDlSym */
   33881     winDlClose,          /* xDlClose */
   33882     winRandomness,       /* xRandomness */
   33883     winSleep,            /* xSleep */
   33884     winCurrentTime,      /* xCurrentTime */
   33885     winGetLastError,     /* xGetLastError */
   33886     winCurrentTimeInt64, /* xCurrentTimeInt64 */
   33887     0,                   /* xSetSystemCall */
   33888     0,                   /* xGetSystemCall */
   33889     0,                   /* xNextSystemCall */
   33890   };
   33891 
   33892 #ifndef SQLITE_OMIT_WAL
   33893   /* get memory map allocation granularity */
   33894   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
   33895   GetSystemInfo(&winSysInfo);
   33896   assert(winSysInfo.dwAllocationGranularity > 0);
   33897 #endif
   33898 
   33899   sqlite3_vfs_register(&winVfs, 1);
   33900   return SQLITE_OK;
   33901 }
   33902 SQLITE_API int sqlite3_os_end(void){
   33903   return SQLITE_OK;
   33904 }
   33905 
   33906 void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle) {
   33907   winFile* winSQLite3File = (winFile*)file;
   33908   memset(file, 0, sizeof(*file));
   33909   winSQLite3File->pMethod = &winIoMethod;
   33910   winSQLite3File->h = handle;
   33911 }
   33912 
   33913 #endif /* SQLITE_OS_WIN */
   33914 
   33915 /************** End of os_win.c **********************************************/
   33916 /************** Begin file bitvec.c ******************************************/
   33917 /*
   33918 ** 2008 February 16
   33919 **
   33920 ** The author disclaims copyright to this source code.  In place of
   33921 ** a legal notice, here is a blessing:
   33922 **
   33923 **    May you do good and not evil.
   33924 **    May you find forgiveness for yourself and forgive others.
   33925 **    May you share freely, never taking more than you give.
   33926 **
   33927 *************************************************************************
   33928 ** This file implements an object that represents a fixed-length
   33929 ** bitmap.  Bits are numbered starting with 1.
   33930 **
   33931 ** A bitmap is used to record which pages of a database file have been
   33932 ** journalled during a transaction, or which pages have the "dont-write"
   33933 ** property.  Usually only a few pages are meet either condition.
   33934 ** So the bitmap is usually sparse and has low cardinality.
   33935 ** But sometimes (for example when during a DROP of a large table) most
   33936 ** or all of the pages in a database can get journalled.  In those cases,
   33937 ** the bitmap becomes dense with high cardinality.  The algorithm needs
   33938 ** to handle both cases well.
   33939 **
   33940 ** The size of the bitmap is fixed when the object is created.
   33941 **
   33942 ** All bits are clear when the bitmap is created.  Individual bits
   33943 ** may be set or cleared one at a time.
   33944 **
   33945 ** Test operations are about 100 times more common that set operations.
   33946 ** Clear operations are exceedingly rare.  There are usually between
   33947 ** 5 and 500 set operations per Bitvec object, though the number of sets can
   33948 ** sometimes grow into tens of thousands or larger.  The size of the
   33949 ** Bitvec object is the number of pages in the database file at the
   33950 ** start of a transaction, and is thus usually less than a few thousand,
   33951 ** but can be as large as 2 billion for a really big database.
   33952 */
   33953 
   33954 /* Size of the Bitvec structure in bytes. */
   33955 #define BITVEC_SZ        512
   33956 
   33957 /* Round the union size down to the nearest pointer boundary, since that's how
   33958 ** it will be aligned within the Bitvec struct. */
   33959 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
   33960 
   33961 /* Type of the array "element" for the bitmap representation.
   33962 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
   33963 ** Setting this to the "natural word" size of your CPU may improve
   33964 ** performance. */
   33965 #define BITVEC_TELEM     u8
   33966 /* Size, in bits, of the bitmap element. */
   33967 #define BITVEC_SZELEM    8
   33968 /* Number of elements in a bitmap array. */
   33969 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
   33970 /* Number of bits in the bitmap array. */
   33971 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
   33972 
   33973 /* Number of u32 values in hash table. */
   33974 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
   33975 /* Maximum number of entries in hash table before
   33976 ** sub-dividing and re-hashing. */
   33977 #define BITVEC_MXHASH    (BITVEC_NINT/2)
   33978 /* Hashing function for the aHash representation.
   33979 ** Empirical testing showed that the *37 multiplier
   33980 ** (an arbitrary prime)in the hash function provided
   33981 ** no fewer collisions than the no-op *1. */
   33982 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
   33983 
   33984 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
   33985 
   33986 
   33987 /*
   33988 ** A bitmap is an instance of the following structure.
   33989 **
   33990 ** This bitmap records the existance of zero or more bits
   33991 ** with values between 1 and iSize, inclusive.
   33992 **
   33993 ** There are three possible representations of the bitmap.
   33994 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
   33995 ** bitmap.  The least significant bit is bit 1.
   33996 **
   33997 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
   33998 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
   33999 **
   34000 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
   34001 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
   34002 ** handles up to iDivisor separate values of i.  apSub[0] holds
   34003 ** values between 1 and iDivisor.  apSub[1] holds values between
   34004 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
   34005 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
   34006 ** to hold deal with values between 1 and iDivisor.
   34007 */
   34008 struct Bitvec {
   34009   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
   34010   u32 nSet;       /* Number of bits that are set - only valid for aHash
   34011                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
   34012                   ** this would be 125. */
   34013   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
   34014                   /* Should >=0 for apSub element. */
   34015                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
   34016                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
   34017   union {
   34018     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
   34019     u32 aHash[BITVEC_NINT];      /* Hash table representation */
   34020     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
   34021   } u;
   34022 };
   34023 
   34024 /*
   34025 ** Create a new bitmap object able to handle bits between 0 and iSize,
   34026 ** inclusive.  Return a pointer to the new object.  Return NULL if
   34027 ** malloc fails.
   34028 */
   34029 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
   34030   Bitvec *p;
   34031   assert( sizeof(*p)==BITVEC_SZ );
   34032   p = sqlite3MallocZero( sizeof(*p) );
   34033   if( p ){
   34034     p->iSize = iSize;
   34035   }
   34036   return p;
   34037 }
   34038 
   34039 /*
   34040 ** Check to see if the i-th bit is set.  Return true or false.
   34041 ** If p is NULL (if the bitmap has not been created) or if
   34042 ** i is out of range, then return false.
   34043 */
   34044 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
   34045   if( p==0 ) return 0;
   34046   if( i>p->iSize || i==0 ) return 0;
   34047   i--;
   34048   while( p->iDivisor ){
   34049     u32 bin = i/p->iDivisor;
   34050     i = i%p->iDivisor;
   34051     p = p->u.apSub[bin];
   34052     if (!p) {
   34053       return 0;
   34054     }
   34055   }
   34056   if( p->iSize<=BITVEC_NBIT ){
   34057     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
   34058   } else{
   34059     u32 h = BITVEC_HASH(i++);
   34060     while( p->u.aHash[h] ){
   34061       if( p->u.aHash[h]==i ) return 1;
   34062       h = (h+1) % BITVEC_NINT;
   34063     }
   34064     return 0;
   34065   }
   34066 }
   34067 
   34068 /*
   34069 ** Set the i-th bit.  Return 0 on success and an error code if
   34070 ** anything goes wrong.
   34071 **
   34072 ** This routine might cause sub-bitmaps to be allocated.  Failing
   34073 ** to get the memory needed to hold the sub-bitmap is the only
   34074 ** that can go wrong with an insert, assuming p and i are valid.
   34075 **
   34076 ** The calling function must ensure that p is a valid Bitvec object
   34077 ** and that the value for "i" is within range of the Bitvec object.
   34078 ** Otherwise the behavior is undefined.
   34079 */
   34080 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
   34081   u32 h;
   34082   if( p==0 ) return SQLITE_OK;
   34083   assert( i>0 );
   34084   assert( i<=p->iSize );
   34085   i--;
   34086   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
   34087     u32 bin = i/p->iDivisor;
   34088     i = i%p->iDivisor;
   34089     if( p->u.apSub[bin]==0 ){
   34090       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
   34091       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
   34092     }
   34093     p = p->u.apSub[bin];
   34094   }
   34095   if( p->iSize<=BITVEC_NBIT ){
   34096     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
   34097     return SQLITE_OK;
   34098   }
   34099   h = BITVEC_HASH(i++);
   34100   /* if there wasn't a hash collision, and this doesn't */
   34101   /* completely fill the hash, then just add it without */
   34102   /* worring about sub-dividing and re-hashing. */
   34103   if( !p->u.aHash[h] ){
   34104     if (p->nSet<(BITVEC_NINT-1)) {
   34105       goto bitvec_set_end;
   34106     } else {
   34107       goto bitvec_set_rehash;
   34108     }
   34109   }
   34110   /* there was a collision, check to see if it's already */
   34111   /* in hash, if not, try to find a spot for it */
   34112   do {
   34113     if( p->u.aHash[h]==i ) return SQLITE_OK;
   34114     h++;
   34115     if( h>=BITVEC_NINT ) h = 0;
   34116   } while( p->u.aHash[h] );
   34117   /* we didn't find it in the hash.  h points to the first */
   34118   /* available free spot. check to see if this is going to */
   34119   /* make our hash too "full".  */
   34120 bitvec_set_rehash:
   34121   if( p->nSet>=BITVEC_MXHASH ){
   34122     unsigned int j;
   34123     int rc;
   34124     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
   34125     if( aiValues==0 ){
   34126       return SQLITE_NOMEM;
   34127     }else{
   34128       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
   34129       memset(p->u.apSub, 0, sizeof(p->u.apSub));
   34130       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
   34131       rc = sqlite3BitvecSet(p, i);
   34132       for(j=0; j<BITVEC_NINT; j++){
   34133         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
   34134       }
   34135       sqlite3StackFree(0, aiValues);
   34136       return rc;
   34137     }
   34138   }
   34139 bitvec_set_end:
   34140   p->nSet++;
   34141   p->u.aHash[h] = i;
   34142   return SQLITE_OK;
   34143 }
   34144 
   34145 /*
   34146 ** Clear the i-th bit.
   34147 **
   34148 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
   34149 ** that BitvecClear can use to rebuilt its hash table.
   34150 */
   34151 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
   34152   if( p==0 ) return;
   34153   assert( i>0 );
   34154   i--;
   34155   while( p->iDivisor ){
   34156     u32 bin = i/p->iDivisor;
   34157     i = i%p->iDivisor;
   34158     p = p->u.apSub[bin];
   34159     if (!p) {
   34160       return;
   34161     }
   34162   }
   34163   if( p->iSize<=BITVEC_NBIT ){
   34164     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
   34165   }else{
   34166     unsigned int j;
   34167     u32 *aiValues = pBuf;
   34168     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
   34169     memset(p->u.aHash, 0, sizeof(p->u.aHash));
   34170     p->nSet = 0;
   34171     for(j=0; j<BITVEC_NINT; j++){
   34172       if( aiValues[j] && aiValues[j]!=(i+1) ){
   34173         u32 h = BITVEC_HASH(aiValues[j]-1);
   34174         p->nSet++;
   34175         while( p->u.aHash[h] ){
   34176           h++;
   34177           if( h>=BITVEC_NINT ) h = 0;
   34178         }
   34179         p->u.aHash[h] = aiValues[j];
   34180       }
   34181     }
   34182   }
   34183 }
   34184 
   34185 /*
   34186 ** Destroy a bitmap object.  Reclaim all memory used.
   34187 */
   34188 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
   34189   if( p==0 ) return;
   34190   if( p->iDivisor ){
   34191     unsigned int i;
   34192     for(i=0; i<BITVEC_NPTR; i++){
   34193       sqlite3BitvecDestroy(p->u.apSub[i]);
   34194     }
   34195   }
   34196   sqlite3_free(p);
   34197 }
   34198 
   34199 /*
   34200 ** Return the value of the iSize parameter specified when Bitvec *p
   34201 ** was created.
   34202 */
   34203 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
   34204   return p->iSize;
   34205 }
   34206 
   34207 #ifndef SQLITE_OMIT_BUILTIN_TEST
   34208 /*
   34209 ** Let V[] be an array of unsigned characters sufficient to hold
   34210 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
   34211 ** Then the following macros can be used to set, clear, or test
   34212 ** individual bits within V.
   34213 */
   34214 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
   34215 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
   34216 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
   34217 
   34218 /*
   34219 ** This routine runs an extensive test of the Bitvec code.
   34220 **
   34221 ** The input is an array of integers that acts as a program
   34222 ** to test the Bitvec.  The integers are opcodes followed
   34223 ** by 0, 1, or 3 operands, depending on the opcode.  Another
   34224 ** opcode follows immediately after the last operand.
   34225 **
   34226 ** There are 6 opcodes numbered from 0 through 5.  0 is the
   34227 ** "halt" opcode and causes the test to end.
   34228 **
   34229 **    0          Halt and return the number of errors
   34230 **    1 N S X    Set N bits beginning with S and incrementing by X
   34231 **    2 N S X    Clear N bits beginning with S and incrementing by X
   34232 **    3 N        Set N randomly chosen bits
   34233 **    4 N        Clear N randomly chosen bits
   34234 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
   34235 **
   34236 ** The opcodes 1 through 4 perform set and clear operations are performed
   34237 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
   34238 ** Opcode 5 works on the linear array only, not on the Bitvec.
   34239 ** Opcode 5 is used to deliberately induce a fault in order to
   34240 ** confirm that error detection works.
   34241 **
   34242 ** At the conclusion of the test the linear array is compared
   34243 ** against the Bitvec object.  If there are any differences,
   34244 ** an error is returned.  If they are the same, zero is returned.
   34245 **
   34246 ** If a memory allocation error occurs, return -1.
   34247 */
   34248 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
   34249   Bitvec *pBitvec = 0;
   34250   unsigned char *pV = 0;
   34251   int rc = -1;
   34252   int i, nx, pc, op;
   34253   void *pTmpSpace;
   34254 
   34255   /* Allocate the Bitvec to be tested and a linear array of
   34256   ** bits to act as the reference */
   34257   pBitvec = sqlite3BitvecCreate( sz );
   34258   pV = sqlite3_malloc( (sz+7)/8 + 1 );
   34259   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
   34260   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
   34261   memset(pV, 0, (sz+7)/8 + 1);
   34262 
   34263   /* NULL pBitvec tests */
   34264   sqlite3BitvecSet(0, 1);
   34265   sqlite3BitvecClear(0, 1, pTmpSpace);
   34266 
   34267   /* Run the program */
   34268   pc = 0;
   34269   while( (op = aOp[pc])!=0 ){
   34270     switch( op ){
   34271       case 1:
   34272       case 2:
   34273       case 5: {
   34274         nx = 4;
   34275         i = aOp[pc+2] - 1;
   34276         aOp[pc+2] += aOp[pc+3];
   34277         break;
   34278       }
   34279       case 3:
   34280       case 4:
   34281       default: {
   34282         nx = 2;
   34283         sqlite3_randomness(sizeof(i), &i);
   34284         break;
   34285       }
   34286     }
   34287     if( (--aOp[pc+1]) > 0 ) nx = 0;
   34288     pc += nx;
   34289     i = (i & 0x7fffffff)%sz;
   34290     if( (op & 1)!=0 ){
   34291       SETBIT(pV, (i+1));
   34292       if( op!=5 ){
   34293         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
   34294       }
   34295     }else{
   34296       CLEARBIT(pV, (i+1));
   34297       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
   34298     }
   34299   }
   34300 
   34301   /* Test to make sure the linear array exactly matches the
   34302   ** Bitvec object.  Start with the assumption that they do
   34303   ** match (rc==0).  Change rc to non-zero if a discrepancy
   34304   ** is found.
   34305   */
   34306   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
   34307           + sqlite3BitvecTest(pBitvec, 0)
   34308           + (sqlite3BitvecSize(pBitvec) - sz);
   34309   for(i=1; i<=sz; i++){
   34310     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
   34311       rc = i;
   34312       break;
   34313     }
   34314   }
   34315 
   34316   /* Free allocated structure */
   34317 bitvec_end:
   34318   sqlite3_free(pTmpSpace);
   34319   sqlite3_free(pV);
   34320   sqlite3BitvecDestroy(pBitvec);
   34321   return rc;
   34322 }
   34323 #endif /* SQLITE_OMIT_BUILTIN_TEST */
   34324 
   34325 /************** End of bitvec.c **********************************************/
   34326 /************** Begin file pcache.c ******************************************/
   34327 /*
   34328 ** 2008 August 05
   34329 **
   34330 ** The author disclaims copyright to this source code.  In place of
   34331 ** a legal notice, here is a blessing:
   34332 **
   34333 **    May you do good and not evil.
   34334 **    May you find forgiveness for yourself and forgive others.
   34335 **    May you share freely, never taking more than you give.
   34336 **
   34337 *************************************************************************
   34338 ** This file implements that page cache.
   34339 */
   34340 
   34341 /*
   34342 ** A complete page cache is an instance of this structure.
   34343 */
   34344 struct PCache {
   34345   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
   34346   PgHdr *pSynced;                     /* Last synced page in dirty page list */
   34347   int nRef;                           /* Number of referenced pages */
   34348   int nMax;                           /* Configured cache size */
   34349   int szPage;                         /* Size of every page in this cache */
   34350   int szExtra;                        /* Size of extra space for each page */
   34351   int bPurgeable;                     /* True if pages are on backing store */
   34352   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
   34353   void *pStress;                      /* Argument to xStress */
   34354   sqlite3_pcache *pCache;             /* Pluggable cache module */
   34355   PgHdr *pPage1;                      /* Reference to page 1 */
   34356 };
   34357 
   34358 /*
   34359 ** Some of the assert() macros in this code are too expensive to run
   34360 ** even during normal debugging.  Use them only rarely on long-running
   34361 ** tests.  Enable the expensive asserts using the
   34362 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
   34363 */
   34364 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   34365 # define expensive_assert(X)  assert(X)
   34366 #else
   34367 # define expensive_assert(X)
   34368 #endif
   34369 
   34370 /********************************** Linked List Management ********************/
   34371 
   34372 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
   34373 /*
   34374 ** Check that the pCache->pSynced variable is set correctly. If it
   34375 ** is not, either fail an assert or return zero. Otherwise, return
   34376 ** non-zero. This is only used in debugging builds, as follows:
   34377 **
   34378 **   expensive_assert( pcacheCheckSynced(pCache) );
   34379 */
   34380 static int pcacheCheckSynced(PCache *pCache){
   34381   PgHdr *p;
   34382   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
   34383     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
   34384   }
   34385   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
   34386 }
   34387 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
   34388 
   34389 /*
   34390 ** Remove page pPage from the list of dirty pages.
   34391 */
   34392 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
   34393   PCache *p = pPage->pCache;
   34394 
   34395   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
   34396   assert( pPage->pDirtyPrev || pPage==p->pDirty );
   34397 
   34398   /* Update the PCache1.pSynced variable if necessary. */
   34399   if( p->pSynced==pPage ){
   34400     PgHdr *pSynced = pPage->pDirtyPrev;
   34401     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
   34402       pSynced = pSynced->pDirtyPrev;
   34403     }
   34404     p->pSynced = pSynced;
   34405   }
   34406 
   34407   if( pPage->pDirtyNext ){
   34408     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
   34409   }else{
   34410     assert( pPage==p->pDirtyTail );
   34411     p->pDirtyTail = pPage->pDirtyPrev;
   34412   }
   34413   if( pPage->pDirtyPrev ){
   34414     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
   34415   }else{
   34416     assert( pPage==p->pDirty );
   34417     p->pDirty = pPage->pDirtyNext;
   34418   }
   34419   pPage->pDirtyNext = 0;
   34420   pPage->pDirtyPrev = 0;
   34421 
   34422   expensive_assert( pcacheCheckSynced(p) );
   34423 }
   34424 
   34425 /*
   34426 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
   34427 ** pPage).
   34428 */
   34429 static void pcacheAddToDirtyList(PgHdr *pPage){
   34430   PCache *p = pPage->pCache;
   34431 
   34432   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   34433 
   34434   pPage->pDirtyNext = p->pDirty;
   34435   if( pPage->pDirtyNext ){
   34436     assert( pPage->pDirtyNext->pDirtyPrev==0 );
   34437     pPage->pDirtyNext->pDirtyPrev = pPage;
   34438   }
   34439   p->pDirty = pPage;
   34440   if( !p->pDirtyTail ){
   34441     p->pDirtyTail = pPage;
   34442   }
   34443   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
   34444     p->pSynced = pPage;
   34445   }
   34446   expensive_assert( pcacheCheckSynced(p) );
   34447 }
   34448 
   34449 /*
   34450 ** Wrapper around the pluggable caches xUnpin method. If the cache is
   34451 ** being used for an in-memory database, this function is a no-op.
   34452 */
   34453 static void pcacheUnpin(PgHdr *p){
   34454   PCache *pCache = p->pCache;
   34455   if( pCache->bPurgeable ){
   34456     if( p->pgno==1 ){
   34457       pCache->pPage1 = 0;
   34458     }
   34459     sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
   34460   }
   34461 }
   34462 
   34463 /*************************************************** General Interfaces ******
   34464 **
   34465 ** Initialize and shutdown the page cache subsystem. Neither of these
   34466 ** functions are threadsafe.
   34467 */
   34468 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
   34469   if( sqlite3GlobalConfig.pcache.xInit==0 ){
   34470     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
   34471     ** built-in default page cache is used instead of the application defined
   34472     ** page cache. */
   34473     sqlite3PCacheSetDefault();
   34474   }
   34475   return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
   34476 }
   34477 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
   34478   if( sqlite3GlobalConfig.pcache.xShutdown ){
   34479     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
   34480     sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
   34481   }
   34482 }
   34483 
   34484 /*
   34485 ** Return the size in bytes of a PCache object.
   34486 */
   34487 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
   34488 
   34489 /*
   34490 ** Create a new PCache object. Storage space to hold the object
   34491 ** has already been allocated and is passed in as the p pointer.
   34492 ** The caller discovers how much space needs to be allocated by
   34493 ** calling sqlite3PcacheSize().
   34494 */
   34495 SQLITE_PRIVATE void sqlite3PcacheOpen(
   34496   int szPage,                  /* Size of every page */
   34497   int szExtra,                 /* Extra space associated with each page */
   34498   int bPurgeable,              /* True if pages are on backing store */
   34499   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
   34500   void *pStress,               /* Argument to xStress */
   34501   PCache *p                    /* Preallocated space for the PCache */
   34502 ){
   34503   memset(p, 0, sizeof(PCache));
   34504   p->szPage = szPage;
   34505   p->szExtra = szExtra;
   34506   p->bPurgeable = bPurgeable;
   34507   p->xStress = xStress;
   34508   p->pStress = pStress;
   34509   p->nMax = 100;
   34510 }
   34511 
   34512 /*
   34513 ** Change the page size for PCache object. The caller must ensure that there
   34514 ** are no outstanding page references when this function is called.
   34515 */
   34516 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
   34517   assert( pCache->nRef==0 && pCache->pDirty==0 );
   34518   if( pCache->pCache ){
   34519     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
   34520     pCache->pCache = 0;
   34521     pCache->pPage1 = 0;
   34522   }
   34523   pCache->szPage = szPage;
   34524 }
   34525 
   34526 /*
   34527 ** Try to obtain a page from the cache.
   34528 */
   34529 SQLITE_PRIVATE int sqlite3PcacheFetch(
   34530   PCache *pCache,       /* Obtain the page from this cache */
   34531   Pgno pgno,            /* Page number to obtain */
   34532   int createFlag,       /* If true, create page if it does not exist already */
   34533   PgHdr **ppPage        /* Write the page here */
   34534 ){
   34535   PgHdr *pPage = 0;
   34536   int eCreate;
   34537 
   34538   assert( pCache!=0 );
   34539   assert( createFlag==1 || createFlag==0 );
   34540   assert( pgno>0 );
   34541 
   34542   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
   34543   ** allocate it now.
   34544   */
   34545   if( !pCache->pCache && createFlag ){
   34546     sqlite3_pcache *p;
   34547     int nByte;
   34548     nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
   34549     p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
   34550     if( !p ){
   34551       return SQLITE_NOMEM;
   34552     }
   34553     sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
   34554     pCache->pCache = p;
   34555   }
   34556 
   34557   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
   34558   if( pCache->pCache ){
   34559     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
   34560   }
   34561 
   34562   if( !pPage && eCreate==1 ){
   34563     PgHdr *pPg;
   34564 
   34565     /* Find a dirty page to write-out and recycle. First try to find a
   34566     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   34567     ** cleared), but if that is not possible settle for any other
   34568     ** unreferenced dirty page.
   34569     */
   34570     expensive_assert( pcacheCheckSynced(pCache) );
   34571     for(pPg=pCache->pSynced;
   34572         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
   34573         pPg=pPg->pDirtyPrev
   34574     );
   34575     pCache->pSynced = pPg;
   34576     if( !pPg ){
   34577       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   34578     }
   34579     if( pPg ){
   34580       int rc;
   34581       rc = pCache->xStress(pCache->pStress, pPg);
   34582       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   34583         return rc;
   34584       }
   34585     }
   34586 
   34587     pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
   34588   }
   34589 
   34590   if( pPage ){
   34591     if( !pPage->pData ){
   34592       memset(pPage, 0, sizeof(PgHdr));
   34593       pPage->pData = (void *)&pPage[1];
   34594       pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
   34595       memset(pPage->pExtra, 0, pCache->szExtra);
   34596       pPage->pCache = pCache;
   34597       pPage->pgno = pgno;
   34598     }
   34599     assert( pPage->pCache==pCache );
   34600     assert( pPage->pgno==pgno );
   34601     assert( pPage->pData==(void *)&pPage[1] );
   34602     assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
   34603 
   34604     if( 0==pPage->nRef ){
   34605       pCache->nRef++;
   34606     }
   34607     pPage->nRef++;
   34608     if( pgno==1 ){
   34609       pCache->pPage1 = pPage;
   34610     }
   34611   }
   34612   *ppPage = pPage;
   34613   return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
   34614 }
   34615 
   34616 /*
   34617 ** Decrement the reference count on a page. If the page is clean and the
   34618 ** reference count drops to 0, then it is made elible for recycling.
   34619 */
   34620 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
   34621   assert( p->nRef>0 );
   34622   p->nRef--;
   34623   if( p->nRef==0 ){
   34624     PCache *pCache = p->pCache;
   34625     pCache->nRef--;
   34626     if( (p->flags&PGHDR_DIRTY)==0 ){
   34627       pcacheUnpin(p);
   34628     }else{
   34629       /* Move the page to the head of the dirty list. */
   34630       pcacheRemoveFromDirtyList(p);
   34631       pcacheAddToDirtyList(p);
   34632     }
   34633   }
   34634 }
   34635 
   34636 /*
   34637 ** Increase the reference count of a supplied page by 1.
   34638 */
   34639 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
   34640   assert(p->nRef>0);
   34641   p->nRef++;
   34642 }
   34643 
   34644 /*
   34645 ** Drop a page from the cache. There must be exactly one reference to the
   34646 ** page. This function deletes that reference, so after it returns the
   34647 ** page pointed to by p is invalid.
   34648 */
   34649 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
   34650   PCache *pCache;
   34651   assert( p->nRef==1 );
   34652   if( p->flags&PGHDR_DIRTY ){
   34653     pcacheRemoveFromDirtyList(p);
   34654   }
   34655   pCache = p->pCache;
   34656   pCache->nRef--;
   34657   if( p->pgno==1 ){
   34658     pCache->pPage1 = 0;
   34659   }
   34660   sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
   34661 }
   34662 
   34663 /*
   34664 ** Make sure the page is marked as dirty. If it isn't dirty already,
   34665 ** make it so.
   34666 */
   34667 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
   34668   p->flags &= ~PGHDR_DONT_WRITE;
   34669   assert( p->nRef>0 );
   34670   if( 0==(p->flags & PGHDR_DIRTY) ){
   34671     p->flags |= PGHDR_DIRTY;
   34672     pcacheAddToDirtyList( p);
   34673   }
   34674 }
   34675 
   34676 /*
   34677 ** Make sure the page is marked as clean. If it isn't clean already,
   34678 ** make it so.
   34679 */
   34680 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
   34681   if( (p->flags & PGHDR_DIRTY) ){
   34682     pcacheRemoveFromDirtyList(p);
   34683     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
   34684     if( p->nRef==0 ){
   34685       pcacheUnpin(p);
   34686     }
   34687   }
   34688 }
   34689 
   34690 /*
   34691 ** Make every page in the cache clean.
   34692 */
   34693 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
   34694   PgHdr *p;
   34695   while( (p = pCache->pDirty)!=0 ){
   34696     sqlite3PcacheMakeClean(p);
   34697   }
   34698 }
   34699 
   34700 /*
   34701 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
   34702 */
   34703 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
   34704   PgHdr *p;
   34705   for(p=pCache->pDirty; p; p=p->pDirtyNext){
   34706     p->flags &= ~PGHDR_NEED_SYNC;
   34707   }
   34708   pCache->pSynced = pCache->pDirtyTail;
   34709 }
   34710 
   34711 /*
   34712 ** Change the page number of page p to newPgno.
   34713 */
   34714 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
   34715   PCache *pCache = p->pCache;
   34716   assert( p->nRef>0 );
   34717   assert( newPgno>0 );
   34718   sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
   34719   p->pgno = newPgno;
   34720   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
   34721     pcacheRemoveFromDirtyList(p);
   34722     pcacheAddToDirtyList(p);
   34723   }
   34724 }
   34725 
   34726 /*
   34727 ** Drop every cache entry whose page number is greater than "pgno". The
   34728 ** caller must ensure that there are no outstanding references to any pages
   34729 ** other than page 1 with a page number greater than pgno.
   34730 **
   34731 ** If there is a reference to page 1 and the pgno parameter passed to this
   34732 ** function is 0, then the data area associated with page 1 is zeroed, but
   34733 ** the page object is not dropped.
   34734 */
   34735 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
   34736   if( pCache->pCache ){
   34737     PgHdr *p;
   34738     PgHdr *pNext;
   34739     for(p=pCache->pDirty; p; p=pNext){
   34740       pNext = p->pDirtyNext;
   34741       /* This routine never gets call with a positive pgno except right
   34742       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
   34743       ** it must be that pgno==0.
   34744       */
   34745       assert( p->pgno>0 );
   34746       if( ALWAYS(p->pgno>pgno) ){
   34747         assert( p->flags&PGHDR_DIRTY );
   34748         sqlite3PcacheMakeClean(p);
   34749       }
   34750     }
   34751     if( pgno==0 && pCache->pPage1 ){
   34752       memset(pCache->pPage1->pData, 0, pCache->szPage);
   34753       pgno = 1;
   34754     }
   34755     sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
   34756   }
   34757 }
   34758 
   34759 /*
   34760 ** Close a cache.
   34761 */
   34762 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
   34763   if( pCache->pCache ){
   34764     sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
   34765   }
   34766 }
   34767 
   34768 /*
   34769 ** Discard the contents of the cache.
   34770 */
   34771 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
   34772   sqlite3PcacheTruncate(pCache, 0);
   34773 }
   34774 
   34775 /*
   34776 ** Merge two lists of pages connected by pDirty and in pgno order.
   34777 ** Do not both fixing the pDirtyPrev pointers.
   34778 */
   34779 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
   34780   PgHdr result, *pTail;
   34781   pTail = &result;
   34782   while( pA && pB ){
   34783     if( pA->pgno<pB->pgno ){
   34784       pTail->pDirty = pA;
   34785       pTail = pA;
   34786       pA = pA->pDirty;
   34787     }else{
   34788       pTail->pDirty = pB;
   34789       pTail = pB;
   34790       pB = pB->pDirty;
   34791     }
   34792   }
   34793   if( pA ){
   34794     pTail->pDirty = pA;
   34795   }else if( pB ){
   34796     pTail->pDirty = pB;
   34797   }else{
   34798     pTail->pDirty = 0;
   34799   }
   34800   return result.pDirty;
   34801 }
   34802 
   34803 /*
   34804 ** Sort the list of pages in accending order by pgno.  Pages are
   34805 ** connected by pDirty pointers.  The pDirtyPrev pointers are
   34806 ** corrupted by this sort.
   34807 **
   34808 ** Since there cannot be more than 2^31 distinct pages in a database,
   34809 ** there cannot be more than 31 buckets required by the merge sorter.
   34810 ** One extra bucket is added to catch overflow in case something
   34811 ** ever changes to make the previous sentence incorrect.
   34812 */
   34813 #define N_SORT_BUCKET  32
   34814 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
   34815   PgHdr *a[N_SORT_BUCKET], *p;
   34816   int i;
   34817   memset(a, 0, sizeof(a));
   34818   while( pIn ){
   34819     p = pIn;
   34820     pIn = p->pDirty;
   34821     p->pDirty = 0;
   34822     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
   34823       if( a[i]==0 ){
   34824         a[i] = p;
   34825         break;
   34826       }else{
   34827         p = pcacheMergeDirtyList(a[i], p);
   34828         a[i] = 0;
   34829       }
   34830     }
   34831     if( NEVER(i==N_SORT_BUCKET-1) ){
   34832       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
   34833       ** the input list.  But that is impossible.
   34834       */
   34835       a[i] = pcacheMergeDirtyList(a[i], p);
   34836     }
   34837   }
   34838   p = a[0];
   34839   for(i=1; i<N_SORT_BUCKET; i++){
   34840     p = pcacheMergeDirtyList(p, a[i]);
   34841   }
   34842   return p;
   34843 }
   34844 
   34845 /*
   34846 ** Return a list of all dirty pages in the cache, sorted by page number.
   34847 */
   34848 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
   34849   PgHdr *p;
   34850   for(p=pCache->pDirty; p; p=p->pDirtyNext){
   34851     p->pDirty = p->pDirtyNext;
   34852   }
   34853   return pcacheSortDirtyList(pCache->pDirty);
   34854 }
   34855 
   34856 /*
   34857 ** Return the total number of referenced pages held by the cache.
   34858 */
   34859 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
   34860   return pCache->nRef;
   34861 }
   34862 
   34863 /*
   34864 ** Return the number of references to the page supplied as an argument.
   34865 */
   34866 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
   34867   return p->nRef;
   34868 }
   34869 
   34870 /*
   34871 ** Return the total number of pages in the cache.
   34872 */
   34873 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
   34874   int nPage = 0;
   34875   if( pCache->pCache ){
   34876     nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
   34877   }
   34878   return nPage;
   34879 }
   34880 
   34881 #ifdef SQLITE_TEST
   34882 /*
   34883 ** Get the suggested cache-size value.
   34884 */
   34885 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
   34886   return pCache->nMax;
   34887 }
   34888 #endif
   34889 
   34890 /*
   34891 ** Set the suggested cache-size value.
   34892 */
   34893 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
   34894   pCache->nMax = mxPage;
   34895   if( pCache->pCache ){
   34896     sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
   34897   }
   34898 }
   34899 
   34900 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   34901 /*
   34902 ** For all dirty pages currently in the cache, invoke the specified
   34903 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
   34904 ** defined.
   34905 */
   34906 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
   34907   PgHdr *pDirty;
   34908   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
   34909     xIter(pDirty);
   34910   }
   34911 }
   34912 #endif
   34913 
   34914 /************** End of pcache.c **********************************************/
   34915 /************** Begin file pcache1.c *****************************************/
   34916 /*
   34917 ** 2008 November 05
   34918 **
   34919 ** The author disclaims copyright to this source code.  In place of
   34920 ** a legal notice, here is a blessing:
   34921 **
   34922 **    May you do good and not evil.
   34923 **    May you find forgiveness for yourself and forgive others.
   34924 **    May you share freely, never taking more than you give.
   34925 **
   34926 *************************************************************************
   34927 **
   34928 ** This file implements the default page cache implementation (the
   34929 ** sqlite3_pcache interface). It also contains part of the implementation
   34930 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
   34931 ** If the default page cache implementation is overriden, then neither of
   34932 ** these two features are available.
   34933 */
   34934 
   34935 
   34936 typedef struct PCache1 PCache1;
   34937 typedef struct