bugfix: pgp_init
authorvb
Mon, 01 Sep 2014 18:36:42 +0200
changeset 26843e629627bf
parent 25 5af1602f5c66
child 27 096ad5873ef3
bugfix: pgp_init
src/pEpEngine.c
src/pgp_gpg.c
src/sqlite3.c
src/sqlite3.h
test/pEpEngineTest.cc
test/pEpEngineTest.vcxproj
     1.1 --- a/src/pEpEngine.c	Mon Sep 01 17:50:28 2014 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Sep 01 18:36:42 2014 +0200
     1.3 @@ -32,7 +32,7 @@
     1.4  	
     1.5  	_session->version = PEP_ENGINE_VERSION;
     1.6  
     1.7 -    status_result = pgp_init(session);
     1.8 +    status_result = pgp_init(_session);
     1.9      assert(status_result == PEP_STATUS_OK);
    1.10      if (status_result != PEP_STATUS_OK) {
    1.11          free(_session);
    1.12 @@ -244,6 +244,11 @@
    1.13  			sqlite3_finalize(_session->log);
    1.14  			sqlite3_finalize(_session->get_identity);
    1.15  			sqlite3_finalize(_session->set_identity);
    1.16 +            sqlite3_finalize(_session->set_person);
    1.17 +            sqlite3_finalize(_session->set_pgp_keypair);
    1.18 +            sqlite3_finalize(_session->set_trust);
    1.19 +            sqlite3_finalize(_session->get_trust);
    1.20 +
    1.21  			sqlite3_close_v2(_session->db);
    1.22  			sqlite3_close_v2(_session->system_db);
    1.23  		}
     2.1 --- a/src/pgp_gpg.c	Mon Sep 01 17:50:28 2014 +0200
     2.2 +++ b/src/pgp_gpg.c	Mon Sep 01 18:36:42 2014 +0200
     2.3 @@ -35,9 +35,8 @@
     2.4      return true;
     2.5  }
     2.6  
     2.7 -PEP_STATUS pgp_init(PEP_SESSION session)
     2.8 +PEP_STATUS pgp_init(pEpSession *_session)
     2.9  {
    2.10 -    pEpSession *_session = (pEpSession *) session;
    2.11      gpgme_error_t gpgme_error;
    2.12      bool bResult = ensure_keyserver();
    2.13      assert(bResult);
    2.14 @@ -214,6 +213,7 @@
    2.15          free(_session);
    2.16          return PEP_INIT_GPGME_INIT_FAILED;
    2.17      }
    2.18 +    assert(_session->ctx);
    2.19  
    2.20      gpgme_error = _session->gpg.gpgme_set_protocol(_session->ctx,
    2.21          GPGME_PROTOCOL_OpenPGP);
     3.1 --- a/src/sqlite3.c	Mon Sep 01 17:50:28 2014 +0200
     3.2 +++ b/src/sqlite3.c	Mon Sep 01 18:36:42 2014 +0200
     3.3 @@ -1,6 +1,6 @@
     3.4  /******************************************************************************
     3.5  ** This file is an amalgamation of many separate C source files from SQLite
     3.6 -** version 3.8.2.  By combining all the individual C code files into this 
     3.7 +** version 3.8.6.  By combining all the individual C code files into this 
     3.8  ** single large file, the entire code can be compiled as a single translation
     3.9  ** unit.  This allows many compilers to do optimizations that would not be
    3.10  ** possible if the files were compiled separately.  Performance improvements
    3.11 @@ -25,6 +25,93 @@
    3.12  #ifndef SQLITE_API
    3.13  # define SQLITE_API
    3.14  #endif
    3.15 +/************** Begin file sqliteInt.h ***************************************/
    3.16 +/*
    3.17 +** 2001 September 15
    3.18 +**
    3.19 +** The author disclaims copyright to this source code.  In place of
    3.20 +** a legal notice, here is a blessing:
    3.21 +**
    3.22 +**    May you do good and not evil.
    3.23 +**    May you find forgiveness for yourself and forgive others.
    3.24 +**    May you share freely, never taking more than you give.
    3.25 +**
    3.26 +*************************************************************************
    3.27 +** Internal interface definitions for SQLite.
    3.28 +**
    3.29 +*/
    3.30 +#ifndef _SQLITEINT_H_
    3.31 +#define _SQLITEINT_H_
    3.32 +
    3.33 +/*
    3.34 +** These #defines should enable >2GB file support on POSIX if the
    3.35 +** underlying operating system supports it.  If the OS lacks
    3.36 +** large file support, or if the OS is windows, these should be no-ops.
    3.37 +**
    3.38 +** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
    3.39 +** system #includes.  Hence, this block of code must be the very first
    3.40 +** code in all source files.
    3.41 +**
    3.42 +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    3.43 +** on the compiler command line.  This is necessary if you are compiling
    3.44 +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    3.45 +** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    3.46 +** without this option, LFS is enable.  But LFS does not exist in the kernel
    3.47 +** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    3.48 +** portability you should omit LFS.
    3.49 +**
    3.50 +** The previous paragraph was written in 2005.  (This paragraph is written
    3.51 +** on 2008-11-28.) These days, all Linux kernels support large files, so
    3.52 +** you should probably leave LFS enabled.  But some embedded platforms might
    3.53 +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
    3.54 +**
    3.55 +** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    3.56 +*/
    3.57 +#ifndef SQLITE_DISABLE_LFS
    3.58 +# define _LARGE_FILE       1
    3.59 +# ifndef _FILE_OFFSET_BITS
    3.60 +#   define _FILE_OFFSET_BITS 64
    3.61 +# endif
    3.62 +# define _LARGEFILE_SOURCE 1
    3.63 +#endif
    3.64 +
    3.65 +/*
    3.66 +** For MinGW, check to see if we can include the header file containing its
    3.67 +** version information, among other things.  Normally, this internal MinGW
    3.68 +** header file would [only] be included automatically by other MinGW header
    3.69 +** files; however, the contained version information is now required by this
    3.70 +** header file to work around binary compatibility issues (see below) and
    3.71 +** this is the only known way to reliably obtain it.  This entire #if block
    3.72 +** would be completely unnecessary if there was any other way of detecting
    3.73 +** MinGW via their preprocessor (e.g. if they customized their GCC to define
    3.74 +** some MinGW-specific macros).  When compiling for MinGW, either the
    3.75 +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
    3.76 +** defined; otherwise, detection of conditions specific to MinGW will be
    3.77 +** disabled.
    3.78 +*/
    3.79 +#if defined(_HAVE_MINGW_H)
    3.80 +# include "mingw.h"
    3.81 +#elif defined(_HAVE__MINGW_H)
    3.82 +# include "_mingw.h"
    3.83 +#endif
    3.84 +
    3.85 +/*
    3.86 +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
    3.87 +** define is required to maintain binary compatibility with the MSVC runtime
    3.88 +** library in use (e.g. for Windows XP).
    3.89 +*/
    3.90 +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
    3.91 +    defined(_WIN32) && !defined(_WIN64) && \
    3.92 +    defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
    3.93 +    defined(__MSVCRT__)
    3.94 +# define _USE_32BIT_TIME_T
    3.95 +#endif
    3.96 +
    3.97 +/* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
    3.98 +** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
    3.99 +** MinGW.
   3.100 +*/
   3.101 +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
   3.102  /************** Begin file sqlite3.h *****************************************/
   3.103  /*
   3.104  ** 2001 September 15
   3.105 @@ -135,9 +222,9 @@
   3.106  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   3.107  ** [sqlite_version()] and [sqlite_source_id()].
   3.108  */
   3.109 -#define SQLITE_VERSION        "3.8.2"
   3.110 -#define SQLITE_VERSION_NUMBER 3008002
   3.111 -#define SQLITE_SOURCE_ID      "2013-12-06 14:53:30 27392118af4c38c5203a04b8013e1afdb1cebd0d"
   3.112 +#define SQLITE_VERSION        "3.8.6"
   3.113 +#define SQLITE_VERSION_NUMBER 3008006
   3.114 +#define SQLITE_SOURCE_ID      "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
   3.115  
   3.116  /*
   3.117  ** CAPI3REF: Run-Time Library Version Numbers
   3.118 @@ -297,7 +384,7 @@
   3.119  **
   3.120  ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   3.121  ** for the [sqlite3] object.
   3.122 -** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
   3.123 +** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   3.124  ** the [sqlite3] object is successfully destroyed and all associated
   3.125  ** resources are deallocated.
   3.126  **
   3.127 @@ -305,7 +392,7 @@
   3.128  ** statements or unfinished sqlite3_backup objects then sqlite3_close()
   3.129  ** will leave the database connection open and return [SQLITE_BUSY].
   3.130  ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   3.131 -** and unfinished sqlite3_backups, then the database connection becomes
   3.132 +** and/or unfinished sqlite3_backups, then the database connection becomes
   3.133  ** an unusable "zombie" which will automatically be deallocated when the
   3.134  ** last prepared statement is finalized or the last sqlite3_backup is
   3.135  ** finished.  The sqlite3_close_v2() interface is intended for use with
   3.136 @@ -318,7 +405,7 @@
   3.137  ** with the [sqlite3] object prior to attempting to close the object.  ^If
   3.138  ** sqlite3_close_v2() is called on a [database connection] that still has
   3.139  ** outstanding [prepared statements], [BLOB handles], and/or
   3.140 -** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   3.141 +** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
   3.142  ** of resources is deferred until all [prepared statements], [BLOB handles],
   3.143  ** and [sqlite3_backup] objects are also destroyed.
   3.144  **
   3.145 @@ -414,16 +501,14 @@
   3.146  
   3.147  /*
   3.148  ** CAPI3REF: Result Codes
   3.149 -** KEYWORDS: SQLITE_OK {error code} {error codes}
   3.150 -** KEYWORDS: {result code} {result codes}
   3.151 +** KEYWORDS: {result code definitions}
   3.152  **
   3.153  ** Many SQLite functions return an integer result code from the set shown
   3.154  ** here in order to indicate success or failure.
   3.155  **
   3.156  ** New error codes may be added in future versions of SQLite.
   3.157  **
   3.158 -** See also: [SQLITE_IOERR_READ | extended result codes],
   3.159 -** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
   3.160 +** See also: [extended result code definitions]
   3.161  */
   3.162  #define SQLITE_OK           0   /* Successful result */
   3.163  /* beginning-of-error-codes */
   3.164 @@ -461,26 +546,19 @@
   3.165  
   3.166  /*
   3.167  ** CAPI3REF: Extended Result Codes
   3.168 -** KEYWORDS: {extended error code} {extended error codes}
   3.169 -** KEYWORDS: {extended result code} {extended result codes}
   3.170 -**
   3.171 -** In its default configuration, SQLite API routines return one of 26 integer
   3.172 -** [SQLITE_OK | result codes].  However, experience has shown that many of
   3.173 +** KEYWORDS: {extended result code definitions}
   3.174 +**
   3.175 +** In its default configuration, SQLite API routines return one of 30 integer
   3.176 +** [result codes].  However, experience has shown that many of
   3.177  ** these result codes are too coarse-grained.  They do not provide as
   3.178  ** much information about problems as programmers might like.  In an effort to
   3.179  ** address this, newer versions of SQLite (version 3.3.8 and later) include
   3.180  ** support for additional result codes that provide more detailed information
   3.181 -** about errors. The extended result codes are enabled or disabled
   3.182 +** about errors. These [extended result codes] are enabled or disabled
   3.183  ** on a per database connection basis using the
   3.184 -** [sqlite3_extended_result_codes()] API.
   3.185 -**
   3.186 -** Some of the available extended result codes are listed here.
   3.187 -** One may expect the number of extended result codes will increase
   3.188 -** over time.  Software that uses extended result codes should expect
   3.189 -** to see new result codes in future releases of SQLite.
   3.190 -**
   3.191 -** The SQLITE_OK result code will never be extended.  It will always
   3.192 -** be exactly zero.
   3.193 +** [sqlite3_extended_result_codes()] API.  Or, the extended code for
   3.194 +** the most recent error can be obtained using
   3.195 +** [sqlite3_extended_errcode()].
   3.196  */
   3.197  #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
   3.198  #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
   3.199 @@ -519,6 +597,7 @@
   3.200  #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   3.201  #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   3.202  #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
   3.203 +#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
   3.204  #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   3.205  #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
   3.206  #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
   3.207 @@ -586,7 +665,11 @@
   3.208  ** after reboot following a crash or power loss, the only bytes in a
   3.209  ** file that were written at the application level might have changed
   3.210  ** and that adjacent bytes, even bytes within the same sector are
   3.211 -** guaranteed to be unchanged.
   3.212 +** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   3.213 +** flag indicate that a file cannot be deleted when open.  The
   3.214 +** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   3.215 +** read-only media and cannot be changed even by processes with
   3.216 +** elevated privileges.
   3.217  */
   3.218  #define SQLITE_IOCAP_ATOMIC                 0x00000001
   3.219  #define SQLITE_IOCAP_ATOMIC512              0x00000002
   3.220 @@ -601,6 +684,7 @@
   3.221  #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
   3.222  #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
   3.223  #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
   3.224 +#define SQLITE_IOCAP_IMMUTABLE              0x00002000
   3.225  
   3.226  /*
   3.227  ** CAPI3REF: File Locking Levels
   3.228 @@ -707,7 +791,7 @@
   3.229  ** locking strategy (for example to use dot-file locks), to inquire
   3.230  ** about the status of a lock, or to break stale locks.  The SQLite
   3.231  ** core reserves all opcodes less than 100 for its own use.
   3.232 -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
   3.233 +** A [file control opcodes | list of opcodes] less than 100 is available.
   3.234  ** Applications that define a custom xFileControl method should use opcodes
   3.235  ** greater than 100 to avoid conflicts.  VFS implementations should
   3.236  ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
   3.237 @@ -780,6 +864,7 @@
   3.238  
   3.239  /*
   3.240  ** CAPI3REF: Standard File Control Opcodes
   3.241 +** KEYWORDS: {file control opcodes} {file control opcode}
   3.242  **
   3.243  ** These integer constants are opcodes for the xFileControl method
   3.244  ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
   3.245 @@ -817,15 +902,29 @@
   3.246  ** additional information.
   3.247  **
   3.248  ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
   3.249 -** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
   3.250 -** SQLite and sent to all VFSes in place of a call to the xSync method
   3.251 -** when the database connection has [PRAGMA synchronous] set to OFF.)^
   3.252 -** Some specialized VFSes need this signal in order to operate correctly
   3.253 -** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
   3.254 -** VFSes do not need this signal and should silently ignore this opcode.
   3.255 -** Applications should not call [sqlite3_file_control()] with this
   3.256 -** opcode as doing so may disrupt the operation of the specialized VFSes
   3.257 -** that do require it.  
   3.258 +** No longer in use.
   3.259 +**
   3.260 +** <li>[[SQLITE_FCNTL_SYNC]]
   3.261 +** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
   3.262 +** sent to the VFS immediately before the xSync method is invoked on a
   3.263 +** database file descriptor. Or, if the xSync method is not invoked 
   3.264 +** because the user has configured SQLite with 
   3.265 +** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place 
   3.266 +** of the xSync method. In most cases, the pointer argument passed with
   3.267 +** this file-control is NULL. However, if the database file is being synced
   3.268 +** as part of a multi-database commit, the argument points to a nul-terminated
   3.269 +** string containing the transactions master-journal file name. VFSes that 
   3.270 +** do not need this signal should silently ignore this opcode. Applications 
   3.271 +** should not call [sqlite3_file_control()] with this opcode as doing so may 
   3.272 +** disrupt the operation of the specialized VFSes that do require it.  
   3.273 +**
   3.274 +** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
   3.275 +** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
   3.276 +** and sent to the VFS after a transaction has been committed immediately
   3.277 +** but before the database is unlocked. VFSes that do not need this signal
   3.278 +** should silently ignore this opcode. Applications should not call
   3.279 +** [sqlite3_file_control()] with this opcode as doing so may disrupt the 
   3.280 +** operation of the specialized VFSes that do require it.  
   3.281  **
   3.282  ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
   3.283  ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
   3.284 @@ -949,6 +1048,18 @@
   3.285  ** SQLite stack may generate instances of this file control if
   3.286  ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
   3.287  **
   3.288 +** <li>[[SQLITE_FCNTL_HAS_MOVED]]
   3.289 +** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
   3.290 +** pointer to an integer and it writes a boolean into that integer depending
   3.291 +** on whether or not the file has been renamed, moved, or deleted since it
   3.292 +** was first opened.
   3.293 +**
   3.294 +** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
   3.295 +** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
   3.296 +** opcode causes the xFileControl method to swap the file handle with the one
   3.297 +** pointed to by the pArg argument.  This capability is used during testing
   3.298 +** and only needs to be supported when SQLITE_TEST is defined.
   3.299 +**
   3.300  ** </ul>
   3.301  */
   3.302  #define SQLITE_FCNTL_LOCKSTATE               1
   3.303 @@ -969,6 +1080,10 @@
   3.304  #define SQLITE_FCNTL_TEMPFILENAME           16
   3.305  #define SQLITE_FCNTL_MMAP_SIZE              18
   3.306  #define SQLITE_FCNTL_TRACE                  19
   3.307 +#define SQLITE_FCNTL_HAS_MOVED              20
   3.308 +#define SQLITE_FCNTL_SYNC                   21
   3.309 +#define SQLITE_FCNTL_COMMIT_PHASETWO        22
   3.310 +#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
   3.311  
   3.312  /*
   3.313  ** CAPI3REF: Mutex Handle
   3.314 @@ -2029,27 +2144,33 @@
   3.315  /*
   3.316  ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
   3.317  **
   3.318 -** ^This routine sets a callback function that might be invoked whenever
   3.319 -** an attempt is made to open a database table that another thread
   3.320 -** or process has locked.
   3.321 -**
   3.322 -** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
   3.323 +** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
   3.324 +** that might be invoked with argument P whenever
   3.325 +** an attempt is made to access a database table associated with
   3.326 +** [database connection] D when another thread
   3.327 +** or process has the table locked.
   3.328 +** The sqlite3_busy_handler() interface is used to implement
   3.329 +** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
   3.330 +**
   3.331 +** ^If the busy callback is NULL, then [SQLITE_BUSY]
   3.332  ** is returned immediately upon encountering the lock.  ^If the busy callback
   3.333  ** is not NULL, then the callback might be invoked with two arguments.
   3.334  **
   3.335  ** ^The first argument to the busy handler is a copy of the void* pointer which
   3.336  ** is the third argument to sqlite3_busy_handler().  ^The second argument to
   3.337  ** the busy handler callback is the number of times that the busy handler has
   3.338 -** been invoked for this locking event.  ^If the
   3.339 +** been invoked for the same locking event.  ^If the
   3.340  ** busy callback returns 0, then no additional attempts are made to
   3.341 -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
   3.342 +** access the database and [SQLITE_BUSY] is returned
   3.343 +** to the application.
   3.344  ** ^If the callback returns non-zero, then another attempt
   3.345 -** is made to open the database for reading and the cycle repeats.
   3.346 +** is made to access the database and the cycle repeats.
   3.347  **
   3.348  ** The presence of a busy handler does not guarantee that it will be invoked
   3.349  ** when there is lock contention. ^If SQLite determines that invoking the busy
   3.350  ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
   3.351 -** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
   3.352 +** to the application instead of invoking the 
   3.353 +** busy handler.
   3.354  ** Consider a scenario where one process is holding a read lock that
   3.355  ** it is trying to promote to a reserved lock and
   3.356  ** a second process is holding a reserved lock that it is trying
   3.357 @@ -2063,28 +2184,15 @@
   3.358  **
   3.359  ** ^The default busy callback is NULL.
   3.360  **
   3.361 -** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
   3.362 -** when SQLite is in the middle of a large transaction where all the
   3.363 -** changes will not fit into the in-memory cache.  SQLite will
   3.364 -** already hold a RESERVED lock on the database file, but it needs
   3.365 -** to promote this lock to EXCLUSIVE so that it can spill cache
   3.366 -** pages into the database file without harm to concurrent
   3.367 -** readers.  ^If it is unable to promote the lock, then the in-memory
   3.368 -** cache will be left in an inconsistent state and so the error
   3.369 -** code is promoted from the relatively benign [SQLITE_BUSY] to
   3.370 -** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
   3.371 -** forces an automatic rollback of the changes.  See the
   3.372 -** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
   3.373 -** CorruptionFollowingBusyError</a> wiki page for a discussion of why
   3.374 -** this is important.
   3.375 -**
   3.376  ** ^(There can only be a single busy handler defined for each
   3.377  ** [database connection].  Setting a new busy handler clears any
   3.378  ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
   3.379 -** will also set or clear the busy handler.
   3.380 +** or evaluating [PRAGMA busy_timeout=N] will change the
   3.381 +** busy handler and thus clear any previously set busy handler.
   3.382  **
   3.383  ** The busy callback should not take any actions which modify the
   3.384 -** database connection that invoked the busy handler.  Any such actions
   3.385 +** database connection that invoked the busy handler.  In other words,
   3.386 +** the busy handler is not reentrant.  Any such actions
   3.387  ** result in undefined behavior.
   3.388  ** 
   3.389  ** A busy handler must not close the database connection
   3.390 @@ -2100,7 +2208,7 @@
   3.391  ** will sleep multiple times until at least "ms" milliseconds of sleeping
   3.392  ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
   3.393  ** the handler returns 0 which causes [sqlite3_step()] to return
   3.394 -** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
   3.395 +** [SQLITE_BUSY].
   3.396  **
   3.397  ** ^Calling this routine with an argument less than or equal to zero
   3.398  ** turns off all busy handlers.
   3.399 @@ -2109,6 +2217,8 @@
   3.400  ** [database connection] any any given moment.  If another busy handler
   3.401  ** was defined  (using [sqlite3_busy_handler()]) prior to calling
   3.402  ** this routine, that other busy handler is cleared.)^
   3.403 +**
   3.404 +** See also:  [PRAGMA busy_timeout]
   3.405  */
   3.406  SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
   3.407  
   3.408 @@ -2403,11 +2513,13 @@
   3.409  ** applications to access the same PRNG for other purposes.
   3.410  **
   3.411  ** ^A call to this routine stores N bytes of randomness into buffer P.
   3.412 -**
   3.413 -** ^The first time this routine is invoked (either internally or by
   3.414 -** the application) the PRNG is seeded using randomness obtained
   3.415 -** from the xRandomness method of the default [sqlite3_vfs] object.
   3.416 -** ^On all subsequent invocations, the pseudo-randomness is generated
   3.417 +** ^If N is less than one, then P can be a NULL pointer.
   3.418 +**
   3.419 +** ^If this routine has not been previously called or if the previous
   3.420 +** call had N less than one, then the PRNG is seeded using randomness
   3.421 +** obtained from the xRandomness method of the default [sqlite3_vfs] object.
   3.422 +** ^If the previous call to this routine had an N of 1 or more then
   3.423 +** the pseudo-randomness is generated
   3.424  ** internally and without recourse to the [sqlite3_vfs] xRandomness
   3.425  ** method.
   3.426  */
   3.427 @@ -2508,8 +2620,8 @@
   3.428  ** [sqlite3_set_authorizer | authorizer documentation] for additional
   3.429  ** information.
   3.430  **
   3.431 -** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
   3.432 -** from the [sqlite3_vtab_on_conflict()] interface.
   3.433 +** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
   3.434 +** returned from the [sqlite3_vtab_on_conflict()] interface.
   3.435  */
   3.436  #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
   3.437  #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
   3.438 @@ -2567,6 +2679,7 @@
   3.439  #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
   3.440  #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
   3.441  #define SQLITE_COPY                  0   /* No longer used */
   3.442 +#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
   3.443  
   3.444  /*
   3.445  ** CAPI3REF: Tracing And Profiling Functions
   3.446 @@ -2779,6 +2892,30 @@
   3.447  **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
   3.448  **     a URI filename, its value overrides any behavior requested by setting
   3.449  **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
   3.450 +**
   3.451 +**  <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
   3.452 +**     "1") or "false" (or "off" or "no" or "0") to indicate that the
   3.453 +**     [powersafe overwrite] property does or does not apply to the
   3.454 +**     storage media on which the database file resides.  ^The psow query
   3.455 +**     parameter only works for the built-in unix and Windows VFSes.
   3.456 +**
   3.457 +**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
   3.458 +**     which if set disables file locking in rollback journal modes.  This
   3.459 +**     is useful for accessing a database on a filesystem that does not
   3.460 +**     support locking.  Caution:  Database corruption might result if two
   3.461 +**     or more processes write to the same database and any one of those
   3.462 +**     processes uses nolock=1.
   3.463 +**
   3.464 +**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
   3.465 +**     parameter that indicates that the database file is stored on
   3.466 +**     read-only media.  ^When immutable is set, SQLite assumes that the
   3.467 +**     database file cannot be changed, even by a process with higher
   3.468 +**     privilege, and so the database is opened read-only and all locking
   3.469 +**     and change detection is disabled.  Caution: Setting the immutable
   3.470 +**     property on a database file that does in fact change can result
   3.471 +**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
   3.472 +**     See also: [SQLITE_IOCAP_IMMUTABLE].
   3.473 +**       
   3.474  ** </ul>
   3.475  **
   3.476  ** ^Specifying an unknown parameter in the query component of a URI is not an
   3.477 @@ -2808,8 +2945,9 @@
   3.478  **          Open file "data.db" in the current directory for read-only access.
   3.479  **          Regardless of whether or not shared-cache mode is enabled by
   3.480  **          default, use a private cache.
   3.481 -** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
   3.482 -**          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
   3.483 +** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
   3.484 +**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
   3.485 +**          that uses dot-files in place of posix advisory locking.
   3.486  ** <tr><td> file:data.db?mode=readonly <td> 
   3.487  **          An error. "readonly" is not a valid option for the "mode" parameter.
   3.488  ** </table>
   3.489 @@ -3985,15 +4123,24 @@
   3.490  **
   3.491  ** ^The fourth parameter, eTextRep, specifies what
   3.492  ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
   3.493 -** its parameters.  Every SQL function implementation must be able to work
   3.494 -** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
   3.495 -** more efficient with one encoding than another.  ^An application may
   3.496 -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
   3.497 -** times with the same function but with different values of eTextRep.
   3.498 +** its parameters.  The application should set this parameter to
   3.499 +** [SQLITE_UTF16LE] if the function implementation invokes 
   3.500 +** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
   3.501 +** implementation invokes [sqlite3_value_text16be()] on an input, or
   3.502 +** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
   3.503 +** otherwise.  ^The same SQL function may be registered multiple times using
   3.504 +** different preferred text encodings, with different implementations for
   3.505 +** each encoding.
   3.506  ** ^When multiple implementations of the same function are available, SQLite
   3.507  ** will pick the one that involves the least amount of data conversion.
   3.508 -** If there is only a single implementation which does not care what text
   3.509 -** encoding is used, then the fourth argument should be [SQLITE_ANY].
   3.510 +**
   3.511 +** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
   3.512 +** to signal that the function will always return the same result given
   3.513 +** the same inputs within a single SQL statement.  Most SQL functions are
   3.514 +** deterministic.  The built-in [random()] SQL function is an example of a
   3.515 +** function that is not deterministic.  The SQLite query planner is able to
   3.516 +** perform additional optimizations on deterministic functions, so use
   3.517 +** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
   3.518  **
   3.519  ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
   3.520  ** function can gain access to this pointer using [sqlite3_user_data()].)^
   3.521 @@ -4079,10 +4226,20 @@
   3.522  #define SQLITE_UTF16LE        2
   3.523  #define SQLITE_UTF16BE        3
   3.524  #define SQLITE_UTF16          4    /* Use native byte order */
   3.525 -#define SQLITE_ANY            5    /* sqlite3_create_function only */
   3.526 +#define SQLITE_ANY            5    /* Deprecated */
   3.527  #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
   3.528  
   3.529  /*
   3.530 +** CAPI3REF: Function Flags
   3.531 +**
   3.532 +** These constants may be ORed together with the 
   3.533 +** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
   3.534 +** to [sqlite3_create_function()], [sqlite3_create_function16()], or
   3.535 +** [sqlite3_create_function_v2()].
   3.536 +*/
   3.537 +#define SQLITE_DETERMINISTIC    0x800
   3.538 +
   3.539 +/*
   3.540  ** CAPI3REF: Deprecated Functions
   3.541  ** DEPRECATED
   3.542  **
   3.543 @@ -4651,6 +4808,13 @@
   3.544  ** is a NULL pointer, then SQLite performs a search for an appropriate
   3.545  ** temporary file directory.
   3.546  **
   3.547 +** Applications are strongly discouraged from using this global variable.
   3.548 +** It is required to set a temporary folder on Windows Runtime (WinRT).
   3.549 +** But for all other platforms, it is highly recommended that applications
   3.550 +** neither read nor write this variable.  This global variable is a relic
   3.551 +** that exists for backwards compatibility of legacy applications and should
   3.552 +** be avoided in new projects.
   3.553 +**
   3.554  ** It is not safe to read or modify this variable in more than one
   3.555  ** thread at a time.  It is not safe to read or modify this variable
   3.556  ** if a [database connection] is being used at the same time in a separate
   3.557 @@ -4669,6 +4833,11 @@
   3.558  ** Hence, if this variable is modified directly, either it should be
   3.559  ** made NULL or made to point to memory obtained from [sqlite3_malloc]
   3.560  ** or else the use of the [temp_store_directory pragma] should be avoided.
   3.561 +** Except when requested by the [temp_store_directory pragma], SQLite
   3.562 +** does not free the memory that sqlite3_temp_directory points to.  If
   3.563 +** the application wants that memory to be freed, it must do
   3.564 +** so itself, taking care to only do so after all [database connection]
   3.565 +** objects have been destroyed.
   3.566  **
   3.567  ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   3.568  ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
   3.569 @@ -5803,10 +5972,12 @@
   3.570  ** <li>  SQLITE_MUTEX_RECURSIVE
   3.571  ** <li>  SQLITE_MUTEX_STATIC_MASTER
   3.572  ** <li>  SQLITE_MUTEX_STATIC_MEM
   3.573 -** <li>  SQLITE_MUTEX_STATIC_MEM2
   3.574 +** <li>  SQLITE_MUTEX_STATIC_OPEN
   3.575  ** <li>  SQLITE_MUTEX_STATIC_PRNG
   3.576  ** <li>  SQLITE_MUTEX_STATIC_LRU
   3.577 -** <li>  SQLITE_MUTEX_STATIC_LRU2
   3.578 +** <li>  SQLITE_MUTEX_STATIC_PMEM
   3.579 +** <li>  SQLITE_MUTEX_STATIC_APP1
   3.580 +** <li>  SQLITE_MUTEX_STATIC_APP2
   3.581  ** </ul>)^
   3.582  **
   3.583  ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
   3.584 @@ -6010,6 +6181,9 @@
   3.585  #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
   3.586  #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
   3.587  #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
   3.588 +#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
   3.589 +#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
   3.590 +#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
   3.591  
   3.592  /*
   3.593  ** CAPI3REF: Retrieve the mutex for a database connection
   3.594 @@ -6103,7 +6277,10 @@
   3.595  #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
   3.596  #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
   3.597  #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
   3.598 -#define SQLITE_TESTCTRL_LAST                    20
   3.599 +#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
   3.600 +#define SQLITE_TESTCTRL_BYTEORDER               22
   3.601 +#define SQLITE_TESTCTRL_ISINIT                  23
   3.602 +#define SQLITE_TESTCTRL_LAST                    23
   3.603  
   3.604  /*
   3.605  ** CAPI3REF: SQLite Runtime Status
   3.606 @@ -7087,6 +7264,9 @@
   3.607  ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
   3.608  ** from SQL.
   3.609  **
   3.610 +** ^Checkpoints initiated by this mechanism are
   3.611 +** [sqlite3_wal_checkpoint_v2|PASSIVE].
   3.612 +**
   3.613  ** ^Every new [database connection] defaults to having the auto-checkpoint
   3.614  ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
   3.615  ** pages.  The use of this interface
   3.616 @@ -7103,6 +7283,10 @@
   3.617  ** empty string, then a checkpoint is run on all databases of
   3.618  ** connection D.  ^If the database connection D is not in
   3.619  ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
   3.620 +** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
   3.621 +** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
   3.622 +** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
   3.623 +** or RESET checkpoint.
   3.624  **
   3.625  ** ^The [wal_checkpoint pragma] can be used to invoke this interface
   3.626  ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
   3.627 @@ -7125,10 +7309,12 @@
   3.628  **   Checkpoint as many frames as possible without waiting for any database 
   3.629  **   readers or writers to finish. Sync the db file if all frames in the log
   3.630  **   are checkpointed. This mode is the same as calling 
   3.631 -**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
   3.632 +**   sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
   3.633 +**   is never invoked.
   3.634  **
   3.635  ** <dt>SQLITE_CHECKPOINT_FULL<dd>
   3.636 -**   This mode blocks (calls the busy-handler callback) until there is no
   3.637 +**   This mode blocks (it invokes the
   3.638 +**   [sqlite3_busy_handler|busy-handler callback]) until there is no
   3.639  **   database writer and all readers are reading from the most recent database
   3.640  **   snapshot. It then checkpoints all frames in the log file and syncs the
   3.641  **   database file. This call blocks database writers while it is running,
   3.642 @@ -7136,7 +7322,8 @@
   3.643  **
   3.644  ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
   3.645  **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
   3.646 -**   checkpointing the log file it blocks (calls the busy-handler callback)
   3.647 +**   checkpointing the log file it blocks (calls the 
   3.648 +**   [sqlite3_busy_handler|busy-handler callback])
   3.649  **   until all readers are reading from the database file only. This ensures 
   3.650  **   that the next client to write to the database file restarts the log file 
   3.651  **   from the beginning. This call blocks database writers while it is running,
   3.652 @@ -7274,6 +7461,7 @@
   3.653  
   3.654  /*
   3.655  ** CAPI3REF: Conflict resolution modes
   3.656 +** KEYWORDS: {conflict resolution mode}
   3.657  **
   3.658  ** These constants are returned by [sqlite3_vtab_on_conflict()] to
   3.659  ** inform a [virtual table] implementation what the [ON CONFLICT] mode
   3.660 @@ -7326,6 +7514,16 @@
   3.661  #endif
   3.662  
   3.663  typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
   3.664 +typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
   3.665 +
   3.666 +/* The double-precision datatype used by RTree depends on the
   3.667 +** SQLITE_RTREE_INT_ONLY compile-time option.
   3.668 +*/
   3.669 +#ifdef SQLITE_RTREE_INT_ONLY
   3.670 +  typedef sqlite3_int64 sqlite3_rtree_dbl;
   3.671 +#else
   3.672 +  typedef double sqlite3_rtree_dbl;
   3.673 +#endif
   3.674  
   3.675  /*
   3.676  ** Register a geometry callback named zGeom that can be used as part of an
   3.677 @@ -7336,11 +7534,7 @@
   3.678  SQLITE_API int sqlite3_rtree_geometry_callback(
   3.679    sqlite3 *db,
   3.680    const char *zGeom,
   3.681 -#ifdef SQLITE_RTREE_INT_ONLY
   3.682 -  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
   3.683 -#else
   3.684 -  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
   3.685 -#endif
   3.686 +  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
   3.687    void *pContext
   3.688  );
   3.689  
   3.690 @@ -7352,11 +7546,60 @@
   3.691  struct sqlite3_rtree_geometry {
   3.692    void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
   3.693    int nParam;                     /* Size of array aParam[] */
   3.694 -  double *aParam;                 /* Parameters passed to SQL geom function */
   3.695 +  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
   3.696    void *pUser;                    /* Callback implementation user data */
   3.697    void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
   3.698  };
   3.699  
   3.700 +/*
   3.701 +** Register a 2nd-generation geometry callback named zScore that can be 
   3.702 +** used as part of an R-Tree geometry query as follows:
   3.703 +**
   3.704 +**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
   3.705 +*/
   3.706 +SQLITE_API int sqlite3_rtree_query_callback(
   3.707 +  sqlite3 *db,
   3.708 +  const char *zQueryFunc,
   3.709 +  int (*xQueryFunc)(sqlite3_rtree_query_info*),
   3.710 +  void *pContext,
   3.711 +  void (*xDestructor)(void*)
   3.712 +);
   3.713 +
   3.714 +
   3.715 +/*
   3.716 +** A pointer to a structure of the following type is passed as the 
   3.717 +** argument to scored geometry callback registered using
   3.718 +** sqlite3_rtree_query_callback().
   3.719 +**
   3.720 +** Note that the first 5 fields of this structure are identical to
   3.721 +** sqlite3_rtree_geometry.  This structure is a subclass of
   3.722 +** sqlite3_rtree_geometry.
   3.723 +*/
   3.724 +struct sqlite3_rtree_query_info {
   3.725 +  void *pContext;                   /* pContext from when function registered */
   3.726 +  int nParam;                       /* Number of function parameters */
   3.727 +  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
   3.728 +  void *pUser;                      /* callback can use this, if desired */
   3.729 +  void (*xDelUser)(void*);          /* function to free pUser */
   3.730 +  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
   3.731 +  unsigned int *anQueue;            /* Number of pending entries in the queue */
   3.732 +  int nCoord;                       /* Number of coordinates */
   3.733 +  int iLevel;                       /* Level of current node or entry */
   3.734 +  int mxLevel;                      /* The largest iLevel value in the tree */
   3.735 +  sqlite3_int64 iRowid;             /* Rowid for current entry */
   3.736 +  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
   3.737 +  int eParentWithin;                /* Visibility of parent node */
   3.738 +  int eWithin;                      /* OUT: Visiblity */
   3.739 +  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
   3.740 +};
   3.741 +
   3.742 +/*
   3.743 +** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
   3.744 +*/
   3.745 +#define NOT_WITHIN       0   /* Object completely outside of query region */
   3.746 +#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
   3.747 +#define FULLY_WITHIN     2   /* Object fully contained within query region */
   3.748 +
   3.749  
   3.750  #if 0
   3.751  }  /* end of the 'extern "C"' block */
   3.752 @@ -7366,50 +7609,7 @@
   3.753  
   3.754  
   3.755  /************** End of sqlite3.h *********************************************/
   3.756 -/************** Begin file sqliteInt.h ***************************************/
   3.757 -/*
   3.758 -** 2001 September 15
   3.759 -**
   3.760 -** The author disclaims copyright to this source code.  In place of
   3.761 -** a legal notice, here is a blessing:
   3.762 -**
   3.763 -**    May you do good and not evil.
   3.764 -**    May you find forgiveness for yourself and forgive others.
   3.765 -**    May you share freely, never taking more than you give.
   3.766 -**
   3.767 -*************************************************************************
   3.768 -** Internal interface definitions for SQLite.
   3.769 -**
   3.770 -*/
   3.771 -#ifndef _SQLITEINT_H_
   3.772 -#define _SQLITEINT_H_
   3.773 -
   3.774 -/*
   3.775 -** These #defines should enable >2GB file support on POSIX if the
   3.776 -** underlying operating system supports it.  If the OS lacks
   3.777 -** large file support, or if the OS is windows, these should be no-ops.
   3.778 -**
   3.779 -** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
   3.780 -** system #includes.  Hence, this block of code must be the very first
   3.781 -** code in all source files.
   3.782 -**
   3.783 -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
   3.784 -** on the compiler command line.  This is necessary if you are compiling
   3.785 -** on a recent machine (ex: Red Hat 7.2) but you want your code to work
   3.786 -** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
   3.787 -** without this option, LFS is enable.  But LFS does not exist in the kernel
   3.788 -** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
   3.789 -** portability you should omit LFS.
   3.790 -**
   3.791 -** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
   3.792 -*/
   3.793 -#ifndef SQLITE_DISABLE_LFS
   3.794 -# define _LARGE_FILE       1
   3.795 -# ifndef _FILE_OFFSET_BITS
   3.796 -#   define _FILE_OFFSET_BITS 64
   3.797 -# endif
   3.798 -# define _LARGEFILE_SOURCE 1
   3.799 -#endif
   3.800 +/************** Continuing where we left off in sqliteInt.h ******************/
   3.801  
   3.802  /*
   3.803  ** Include the configuration header output by 'configure' if we're using the
   3.804 @@ -7996,163 +8196,165 @@
   3.805  /************** Continuing where we left off in sqliteInt.h ******************/
   3.806  /************** Include parse.h in the middle of sqliteInt.h *****************/
   3.807  /************** Begin file parse.h *******************************************/
   3.808 -#define TK_SEMI                            1
   3.809 -#define TK_EXPLAIN                         2
   3.810 -#define TK_QUERY                           3
   3.811 -#define TK_PLAN                            4
   3.812 -#define TK_BEGIN                           5
   3.813 -#define TK_TRANSACTION                     6
   3.814 -#define TK_DEFERRED                        7
   3.815 -#define TK_IMMEDIATE                       8
   3.816 -#define TK_EXCLUSIVE                       9
   3.817 -#define TK_COMMIT                         10
   3.818 -#define TK_END                            11
   3.819 -#define TK_ROLLBACK                       12
   3.820 -#define TK_SAVEPOINT                      13
   3.821 -#define TK_RELEASE                        14
   3.822 -#define TK_TO                             15
   3.823 -#define TK_TABLE                          16
   3.824 -#define TK_CREATE                         17
   3.825 -#define TK_IF                             18
   3.826 -#define TK_NOT                            19
   3.827 -#define TK_EXISTS                         20
   3.828 -#define TK_TEMP                           21
   3.829 -#define TK_LP                             22
   3.830 -#define TK_RP                             23
   3.831 -#define TK_AS                             24
   3.832 -#define TK_WITHOUT                        25
   3.833 -#define TK_COMMA                          26
   3.834 -#define TK_ID                             27
   3.835 -#define TK_INDEXED                        28
   3.836 -#define TK_ABORT                          29
   3.837 -#define TK_ACTION                         30
   3.838 -#define TK_AFTER                          31
   3.839 -#define TK_ANALYZE                        32
   3.840 -#define TK_ASC                            33
   3.841 -#define TK_ATTACH                         34
   3.842 -#define TK_BEFORE                         35
   3.843 -#define TK_BY                             36
   3.844 -#define TK_CASCADE                        37
   3.845 -#define TK_CAST                           38
   3.846 -#define TK_COLUMNKW                       39
   3.847 -#define TK_CONFLICT                       40
   3.848 -#define TK_DATABASE                       41
   3.849 -#define TK_DESC                           42
   3.850 -#define TK_DETACH                         43
   3.851 -#define TK_EACH                           44
   3.852 -#define TK_FAIL                           45
   3.853 -#define TK_FOR                            46
   3.854 -#define TK_IGNORE                         47
   3.855 -#define TK_INITIALLY                      48
   3.856 -#define TK_INSTEAD                        49
   3.857 -#define TK_LIKE_KW                        50
   3.858 -#define TK_MATCH                          51
   3.859 -#define TK_NO                             52
   3.860 -#define TK_KEY                            53
   3.861 -#define TK_OF                             54
   3.862 -#define TK_OFFSET                         55
   3.863 -#define TK_PRAGMA                         56
   3.864 -#define TK_RAISE                          57
   3.865 -#define TK_REPLACE                        58
   3.866 -#define TK_RESTRICT                       59
   3.867 -#define TK_ROW                            60
   3.868 -#define TK_TRIGGER                        61
   3.869 -#define TK_VACUUM                         62
   3.870 -#define TK_VIEW                           63
   3.871 -#define TK_VIRTUAL                        64
   3.872 -#define TK_REINDEX                        65
   3.873 -#define TK_RENAME                         66
   3.874 -#define TK_CTIME_KW                       67
   3.875 -#define TK_ANY                            68
   3.876 -#define TK_OR                             69
   3.877 -#define TK_AND                            70
   3.878 -#define TK_IS                             71
   3.879 -#define TK_BETWEEN                        72
   3.880 -#define TK_IN                             73
   3.881 -#define TK_ISNULL                         74
   3.882 -#define TK_NOTNULL                        75
   3.883 -#define TK_NE                             76
   3.884 -#define TK_EQ                             77
   3.885 -#define TK_GT                             78
   3.886 -#define TK_LE                             79
   3.887 -#define TK_LT                             80
   3.888 -#define TK_GE                             81
   3.889 -#define TK_ESCAPE                         82
   3.890 -#define TK_BITAND                         83
   3.891 -#define TK_BITOR                          84
   3.892 -#define TK_LSHIFT                         85
   3.893 -#define TK_RSHIFT                         86
   3.894 -#define TK_PLUS                           87
   3.895 -#define TK_MINUS                          88
   3.896 -#define TK_STAR                           89
   3.897 -#define TK_SLASH                          90
   3.898 -#define TK_REM                            91
   3.899 -#define TK_CONCAT                         92
   3.900 -#define TK_COLLATE                        93
   3.901 -#define TK_BITNOT                         94
   3.902 -#define TK_STRING                         95
   3.903 -#define TK_JOIN_KW                        96
   3.904 -#define TK_CONSTRAINT                     97
   3.905 -#define TK_DEFAULT                        98
   3.906 -#define TK_NULL                           99
   3.907 -#define TK_PRIMARY                        100
   3.908 -#define TK_UNIQUE                         101
   3.909 -#define TK_CHECK                          102
   3.910 -#define TK_REFERENCES                     103
   3.911 -#define TK_AUTOINCR                       104
   3.912 -#define TK_ON                             105
   3.913 -#define TK_INSERT                         106
   3.914 -#define TK_DELETE                         107
   3.915 -#define TK_UPDATE                         108
   3.916 -#define TK_SET                            109
   3.917 -#define TK_DEFERRABLE                     110
   3.918 -#define TK_FOREIGN                        111
   3.919 -#define TK_DROP                           112
   3.920 -#define TK_UNION                          113
   3.921 -#define TK_ALL                            114
   3.922 -#define TK_EXCEPT                         115
   3.923 -#define TK_INTERSECT                      116
   3.924 -#define TK_SELECT                         117
   3.925 -#define TK_DISTINCT                       118
   3.926 -#define TK_DOT                            119
   3.927 -#define TK_FROM                           120
   3.928 -#define TK_JOIN                           121
   3.929 -#define TK_USING                          122
   3.930 -#define TK_ORDER                          123
   3.931 -#define TK_GROUP                          124
   3.932 -#define TK_HAVING                         125
   3.933 -#define TK_LIMIT                          126
   3.934 -#define TK_WHERE                          127
   3.935 -#define TK_INTO                           128
   3.936 -#define TK_VALUES                         129
   3.937 -#define TK_INTEGER                        130
   3.938 -#define TK_FLOAT                          131
   3.939 -#define TK_BLOB                           132
   3.940 -#define TK_REGISTER                       133
   3.941 -#define TK_VARIABLE                       134
   3.942 -#define TK_CASE                           135
   3.943 -#define TK_WHEN                           136
   3.944 -#define TK_THEN                           137
   3.945 -#define TK_ELSE                           138
   3.946 -#define TK_INDEX                          139
   3.947 -#define TK_ALTER                          140
   3.948 -#define TK_ADD                            141
   3.949 -#define TK_TO_TEXT                        142
   3.950 -#define TK_TO_BLOB                        143
   3.951 -#define TK_TO_NUMERIC                     144
   3.952 -#define TK_TO_INT                         145
   3.953 -#define TK_TO_REAL                        146
   3.954 -#define TK_ISNOT                          147
   3.955 -#define TK_END_OF_FILE                    148
   3.956 -#define TK_ILLEGAL                        149
   3.957 -#define TK_SPACE                          150
   3.958 -#define TK_UNCLOSED_STRING                151
   3.959 -#define TK_FUNCTION                       152
   3.960 -#define TK_COLUMN                         153
   3.961 -#define TK_AGG_FUNCTION                   154
   3.962 -#define TK_AGG_COLUMN                     155
   3.963 -#define TK_UMINUS                         156
   3.964 -#define TK_UPLUS                          157
   3.965 +#define TK_SEMI                             1
   3.966 +#define TK_EXPLAIN                          2
   3.967 +#define TK_QUERY                            3
   3.968 +#define TK_PLAN                             4
   3.969 +#define TK_BEGIN                            5
   3.970 +#define TK_TRANSACTION                      6
   3.971 +#define TK_DEFERRED                         7
   3.972 +#define TK_IMMEDIATE                        8
   3.973 +#define TK_EXCLUSIVE                        9
   3.974 +#define TK_COMMIT                          10
   3.975 +#define TK_END                             11
   3.976 +#define TK_ROLLBACK                        12
   3.977 +#define TK_SAVEPOINT                       13
   3.978 +#define TK_RELEASE                         14
   3.979 +#define TK_TO                              15
   3.980 +#define TK_TABLE                           16
   3.981 +#define TK_CREATE                          17
   3.982 +#define TK_IF                              18
   3.983 +#define TK_NOT                             19
   3.984 +#define TK_EXISTS                          20
   3.985 +#define TK_TEMP                            21
   3.986 +#define TK_LP                              22
   3.987 +#define TK_RP                              23
   3.988 +#define TK_AS                              24
   3.989 +#define TK_WITHOUT                         25
   3.990 +#define TK_COMMA                           26
   3.991 +#define TK_ID                              27
   3.992 +#define TK_INDEXED                         28
   3.993 +#define TK_ABORT                           29
   3.994 +#define TK_ACTION                          30
   3.995 +#define TK_AFTER                           31
   3.996 +#define TK_ANALYZE                         32
   3.997 +#define TK_ASC                             33
   3.998 +#define TK_ATTACH                          34
   3.999 +#define TK_BEFORE                          35
  3.1000 +#define TK_BY                              36
  3.1001 +#define TK_CASCADE                         37
  3.1002 +#define TK_CAST                            38
  3.1003 +#define TK_COLUMNKW                        39
  3.1004 +#define TK_CONFLICT                        40
  3.1005 +#define TK_DATABASE                        41
  3.1006 +#define TK_DESC                            42
  3.1007 +#define TK_DETACH                          43
  3.1008 +#define TK_EACH                            44
  3.1009 +#define TK_FAIL                            45
  3.1010 +#define TK_FOR                             46
  3.1011 +#define TK_IGNORE                          47
  3.1012 +#define TK_INITIALLY                       48
  3.1013 +#define TK_INSTEAD                         49
  3.1014 +#define TK_LIKE_KW                         50
  3.1015 +#define TK_MATCH                           51
  3.1016 +#define TK_NO                              52
  3.1017 +#define TK_KEY                             53
  3.1018 +#define TK_OF                              54
  3.1019 +#define TK_OFFSET                          55
  3.1020 +#define TK_PRAGMA                          56
  3.1021 +#define TK_RAISE                           57
  3.1022 +#define TK_RECURSIVE                       58
  3.1023 +#define TK_REPLACE                         59
  3.1024 +#define TK_RESTRICT                        60
  3.1025 +#define TK_ROW                             61
  3.1026 +#define TK_TRIGGER                         62
  3.1027 +#define TK_VACUUM                          63
  3.1028 +#define TK_VIEW                            64
  3.1029 +#define TK_VIRTUAL                         65
  3.1030 +#define TK_WITH                            66
  3.1031 +#define TK_REINDEX                         67
  3.1032 +#define TK_RENAME                          68
  3.1033 +#define TK_CTIME_KW                        69
  3.1034 +#define TK_ANY                             70
  3.1035 +#define TK_OR                              71
  3.1036 +#define TK_AND                             72
  3.1037 +#define TK_IS                              73
  3.1038 +#define TK_BETWEEN                         74
  3.1039 +#define TK_IN                              75
  3.1040 +#define TK_ISNULL                          76
  3.1041 +#define TK_NOTNULL                         77
  3.1042 +#define TK_NE                              78
  3.1043 +#define TK_EQ                              79
  3.1044 +#define TK_GT                              80
  3.1045 +#define TK_LE                              81
  3.1046 +#define TK_LT                              82
  3.1047 +#define TK_GE                              83
  3.1048 +#define TK_ESCAPE                          84
  3.1049 +#define TK_BITAND                          85
  3.1050 +#define TK_BITOR                           86
  3.1051 +#define TK_LSHIFT                          87
  3.1052 +#define TK_RSHIFT                          88
  3.1053 +#define TK_PLUS                            89
  3.1054 +#define TK_MINUS                           90
  3.1055 +#define TK_STAR                            91
  3.1056 +#define TK_SLASH                           92
  3.1057 +#define TK_REM                             93
  3.1058 +#define TK_CONCAT                          94
  3.1059 +#define TK_COLLATE                         95
  3.1060 +#define TK_BITNOT                          96
  3.1061 +#define TK_STRING                          97
  3.1062 +#define TK_JOIN_KW                         98
  3.1063 +#define TK_CONSTRAINT                      99
  3.1064 +#define TK_DEFAULT                        100
  3.1065 +#define TK_NULL                           101
  3.1066 +#define TK_PRIMARY                        102
  3.1067 +#define TK_UNIQUE                         103
  3.1068 +#define TK_CHECK                          104
  3.1069 +#define TK_REFERENCES                     105
  3.1070 +#define TK_AUTOINCR                       106
  3.1071 +#define TK_ON                             107
  3.1072 +#define TK_INSERT                         108
  3.1073 +#define TK_DELETE                         109
  3.1074 +#define TK_UPDATE                         110
  3.1075 +#define TK_SET                            111
  3.1076 +#define TK_DEFERRABLE                     112
  3.1077 +#define TK_FOREIGN                        113
  3.1078 +#define TK_DROP                           114
  3.1079 +#define TK_UNION                          115
  3.1080 +#define TK_ALL                            116
  3.1081 +#define TK_EXCEPT                         117
  3.1082 +#define TK_INTERSECT                      118
  3.1083 +#define TK_SELECT                         119
  3.1084 +#define TK_VALUES                         120
  3.1085 +#define TK_DISTINCT                       121
  3.1086 +#define TK_DOT                            122
  3.1087 +#define TK_FROM                           123
  3.1088 +#define TK_JOIN                           124
  3.1089 +#define TK_USING                          125
  3.1090 +#define TK_ORDER                          126
  3.1091 +#define TK_GROUP                          127
  3.1092 +#define TK_HAVING                         128
  3.1093 +#define TK_LIMIT                          129
  3.1094 +#define TK_WHERE                          130
  3.1095 +#define TK_INTO                           131
  3.1096 +#define TK_INTEGER                        132
  3.1097 +#define TK_FLOAT                          133
  3.1098 +#define TK_BLOB                           134
  3.1099 +#define TK_VARIABLE                       135
  3.1100 +#define TK_CASE                           136
  3.1101 +#define TK_WHEN                           137
  3.1102 +#define TK_THEN                           138
  3.1103 +#define TK_ELSE                           139
  3.1104 +#define TK_INDEX                          140
  3.1105 +#define TK_ALTER                          141
  3.1106 +#define TK_ADD                            142
  3.1107 +#define TK_TO_TEXT                        143
  3.1108 +#define TK_TO_BLOB                        144
  3.1109 +#define TK_TO_NUMERIC                     145
  3.1110 +#define TK_TO_INT                         146
  3.1111 +#define TK_TO_REAL                        147
  3.1112 +#define TK_ISNOT                          148
  3.1113 +#define TK_END_OF_FILE                    149
  3.1114 +#define TK_ILLEGAL                        150
  3.1115 +#define TK_SPACE                          151
  3.1116 +#define TK_UNCLOSED_STRING                152
  3.1117 +#define TK_FUNCTION                       153
  3.1118 +#define TK_COLUMN                         154
  3.1119 +#define TK_AGG_FUNCTION                   155
  3.1120 +#define TK_AGG_COLUMN                     156
  3.1121 +#define TK_UMINUS                         157
  3.1122 +#define TK_UPLUS                          158
  3.1123 +#define TK_REGISTER                       159
  3.1124  
  3.1125  /************** End of parse.h ***********************************************/
  3.1126  /************** Continuing where we left off in sqliteInt.h ******************/
  3.1127 @@ -8324,10 +8526,10 @@
  3.1128  ** gives a possible range of values of approximately 1.0e986 to 1e-986.
  3.1129  ** But the allowed values are "grainy".  Not every value is representable.
  3.1130  ** For example, quantities 16 and 17 are both represented by a LogEst
  3.1131 -** of 40.  However, since LogEst quantatites are suppose to be estimates,
  3.1132 +** of 40.  However, since LogEst quantaties are suppose to be estimates,
  3.1133  ** not exact values, this imprecision is not a problem.
  3.1134  **
  3.1135 -** "LogEst" is short for "Logarithimic Estimate".
  3.1136 +** "LogEst" is short for "Logarithmic Estimate".
  3.1137  **
  3.1138  ** Examples:
  3.1139  **      1 -> 0              20 -> 43          10000 -> 132
  3.1140 @@ -8345,22 +8547,39 @@
  3.1141  
  3.1142  /*
  3.1143  ** Macros to determine whether the machine is big or little endian,
  3.1144 -** evaluated at runtime.
  3.1145 +** and whether or not that determination is run-time or compile-time.
  3.1146 +**
  3.1147 +** For best performance, an attempt is made to guess at the byte-order
  3.1148 +** using C-preprocessor macros.  If that is unsuccessful, or if
  3.1149 +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
  3.1150 +** at run-time.
  3.1151  */
  3.1152  #ifdef SQLITE_AMALGAMATION
  3.1153  SQLITE_PRIVATE const int sqlite3one = 1;
  3.1154  #else
  3.1155  SQLITE_PRIVATE const int sqlite3one;
  3.1156  #endif
  3.1157 -#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  3.1158 -                             || defined(__x86_64) || defined(__x86_64__)
  3.1159 +#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
  3.1160 +     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
  3.1161 +     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
  3.1162 +     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
  3.1163 +# define SQLITE_BYTEORDER    1234
  3.1164  # define SQLITE_BIGENDIAN    0
  3.1165  # define SQLITE_LITTLEENDIAN 1
  3.1166  # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  3.1167 -#else
  3.1168 +#endif
  3.1169 +#if (defined(sparc)    || defined(__ppc__))  \
  3.1170 +    && !defined(SQLITE_RUNTIME_BYTEORDER)
  3.1171 +# define SQLITE_BYTEORDER    4321
  3.1172 +# define SQLITE_BIGENDIAN    1
  3.1173 +# define SQLITE_LITTLEENDIAN 0
  3.1174 +# define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
  3.1175 +#endif
  3.1176 +#if !defined(SQLITE_BYTEORDER)
  3.1177 +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
  3.1178  # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  3.1179  # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  3.1180 -# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  3.1181 +# define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  3.1182  #endif
  3.1183  
  3.1184  /*
  3.1185 @@ -8583,6 +8802,7 @@
  3.1186  typedef struct Module Module;
  3.1187  typedef struct NameContext NameContext;
  3.1188  typedef struct Parse Parse;
  3.1189 +typedef struct PrintfArguments PrintfArguments;
  3.1190  typedef struct RowSet RowSet;
  3.1191  typedef struct Savepoint Savepoint;
  3.1192  typedef struct Select Select;
  3.1193 @@ -8600,6 +8820,7 @@
  3.1194  typedef struct VtabCtx VtabCtx;
  3.1195  typedef struct Walker Walker;
  3.1196  typedef struct WhereInfo WhereInfo;
  3.1197 +typedef struct With With;
  3.1198  
  3.1199  /*
  3.1200  ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  3.1201 @@ -8673,7 +8894,9 @@
  3.1202  
  3.1203  SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  3.1204  SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  3.1205 -SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  3.1206 +#if SQLITE_MAX_MMAP_SIZE>0
  3.1207 +SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  3.1208 +#endif
  3.1209  SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  3.1210  SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  3.1211  SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  3.1212 @@ -8723,6 +8946,7 @@
  3.1213  
  3.1214  SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  3.1215  SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  3.1216 +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
  3.1217  SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  3.1218  
  3.1219  SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  3.1220 @@ -8792,17 +9016,16 @@
  3.1221  SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
  3.1222  SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  3.1223  SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  3.1224 -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  3.1225 -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  3.1226  
  3.1227  SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  3.1228  SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  3.1229  
  3.1230  SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  3.1231 -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  3.1232 +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
  3.1233  SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  3.1234  SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  3.1235  SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  3.1236 +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
  3.1237  
  3.1238  #ifndef NDEBUG
  3.1239  SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  3.1240 @@ -8933,9 +9156,12 @@
  3.1241    char *zComment;          /* Comment to improve readability */
  3.1242  #endif
  3.1243  #ifdef VDBE_PROFILE
  3.1244 -  int cnt;                 /* Number of times this instruction was executed */
  3.1245 +  u32 cnt;                 /* Number of times this instruction was executed */
  3.1246    u64 cycles;              /* Total time spent executing this instruction */
  3.1247  #endif
  3.1248 +#ifdef SQLITE_VDBE_COVERAGE
  3.1249 +  int iSrcLine;            /* Source-code line that generated this opcode */
  3.1250 +#endif
  3.1251  };
  3.1252  typedef struct VdbeOp VdbeOp;
  3.1253  
  3.1254 @@ -9039,145 +9265,151 @@
  3.1255  #define OP_Checkpoint     11
  3.1256  #define OP_JournalMode    12
  3.1257  #define OP_Vacuum         13
  3.1258 -#define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
  3.1259 +#define OP_VFilter        14 /* synopsis: iplan=r[P3] zplan='P4'           */
  3.1260  #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
  3.1261  #define OP_Goto           16
  3.1262  #define OP_Gosub          17
  3.1263  #define OP_Return         18
  3.1264  #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  3.1265 -#define OP_Yield          20
  3.1266 -#define OP_HaltIfNull     21 /* synopsis: if r[P3] null then halt          */
  3.1267 -#define OP_Halt           22
  3.1268 -#define OP_Integer        23 /* synopsis: r[P2]=P1                         */
  3.1269 -#define OP_Int64          24 /* synopsis: r[P2]=P4                         */
  3.1270 -#define OP_String         25 /* synopsis: r[P2]='P4' (len=P1)              */
  3.1271 -#define OP_Null           26 /* synopsis: r[P2..P3]=NULL                   */
  3.1272 -#define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
  3.1273 -#define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
  3.1274 -#define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
  3.1275 -#define OP_Copy           30 /* synopsis: r[P2@P3]=r[P1@P3]                */
  3.1276 -#define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
  3.1277 -#define OP_ResultRow      32 /* synopsis: output=r[P1@P2]                  */
  3.1278 -#define OP_CollSeq        33
  3.1279 -#define OP_AddImm         34 /* synopsis: r[P1]=r[P1]+P2                   */
  3.1280 -#define OP_MustBeInt      35
  3.1281 -#define OP_RealAffinity   36
  3.1282 -#define OP_Permutation    37
  3.1283 -#define OP_Compare        38
  3.1284 -#define OP_Jump           39
  3.1285 -#define OP_Once           40
  3.1286 -#define OP_If             41
  3.1287 -#define OP_IfNot          42
  3.1288 -#define OP_Column         43 /* synopsis: r[P3]=PX                         */
  3.1289 -#define OP_Affinity       44 /* synopsis: affinity(r[P1@P2])               */
  3.1290 -#define OP_MakeRecord     45 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  3.1291 -#define OP_Count          46 /* synopsis: r[P2]=count()                    */
  3.1292 -#define OP_ReadCookie     47
  3.1293 -#define OP_SetCookie      48
  3.1294 -#define OP_VerifyCookie   49
  3.1295 -#define OP_OpenRead       50 /* synopsis: root=P2 iDb=P3                   */
  3.1296 -#define OP_OpenWrite      51 /* synopsis: root=P2 iDb=P3                   */
  3.1297 -#define OP_OpenAutoindex  52 /* synopsis: nColumn=P2                       */
  3.1298 -#define OP_OpenEphemeral  53 /* synopsis: nColumn=P2                       */
  3.1299 -#define OP_SorterOpen     54
  3.1300 -#define OP_OpenPseudo     55 /* synopsis: content in r[P2@P3]              */
  3.1301 -#define OP_Close          56
  3.1302 -#define OP_SeekLt         57 /* synopsis: key=r[P3@P4]                     */
  3.1303 -#define OP_SeekLe         58 /* synopsis: key=r[P3@P4]                     */
  3.1304 -#define OP_SeekGe         59 /* synopsis: key=r[P3@P4]                     */
  3.1305 -#define OP_SeekGt         60 /* synopsis: key=r[P3@P4]                     */
  3.1306 -#define OP_Seek           61 /* synopsis: intkey=r[P2]                     */
  3.1307 -#define OP_NoConflict     62 /* synopsis: key=r[P3@P4]                     */
  3.1308 -#define OP_NotFound       63 /* synopsis: key=r[P3@P4]                     */
  3.1309 -#define OP_Found          64 /* synopsis: key=r[P3@P4]                     */
  3.1310 -#define OP_NotExists      65 /* synopsis: intkey=r[P3]                     */
  3.1311 -#define OP_Sequence       66 /* synopsis: r[P2]=rowid                      */
  3.1312 -#define OP_NewRowid       67 /* synopsis: r[P2]=rowid                      */
  3.1313 -#define OP_Insert         68 /* synopsis: intkey=r[P3] data=r[P2]          */
  3.1314 -#define OP_Or             69 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  3.1315 -#define OP_And            70 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  3.1316 -#define OP_InsertInt      71 /* synopsis: intkey=P3 data=r[P2]             */
  3.1317 -#define OP_Delete         72
  3.1318 -#define OP_ResetCount     73
  3.1319 -#define OP_IsNull         74 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  3.1320 -#define OP_NotNull        75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  3.1321 -#define OP_Ne             76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  3.1322 -#define OP_Eq             77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  3.1323 -#define OP_Gt             78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  3.1324 -#define OP_Le             79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  3.1325 -#define OP_Lt             80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  3.1326 -#define OP_Ge             81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  3.1327 -#define OP_SorterCompare  82 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
  3.1328 -#define OP_BitAnd         83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  3.1329 -#define OP_BitOr          84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  3.1330 -#define OP_ShiftLeft      85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  3.1331 -#define OP_ShiftRight     86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  3.1332 -#define OP_Add            87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  3.1333 -#define OP_Subtract       88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  3.1334 -#define OP_Multiply       89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  3.1335 -#define OP_Divide         90 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  3.1336 -#define OP_Remainder      91 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  3.1337 -#define OP_Concat         92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  3.1338 -#define OP_SorterData     93 /* synopsis: r[P2]=data                       */
  3.1339 -#define OP_BitNot         94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  3.1340 -#define OP_String8        95 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  3.1341 -#define OP_RowKey         96 /* synopsis: r[P2]=key                        */
  3.1342 -#define OP_RowData        97 /* synopsis: r[P2]=data                       */
  3.1343 -#define OP_Rowid          98 /* synopsis: r[P2]=rowid                      */
  3.1344 -#define OP_NullRow        99
  3.1345 -#define OP_Last          100
  3.1346 -#define OP_SorterSort    101
  3.1347 -#define OP_Sort          102
  3.1348 -#define OP_Rewind        103
  3.1349 -#define OP_SorterInsert  104
  3.1350 -#define OP_IdxInsert     105 /* synopsis: key=r[P2]                        */
  3.1351 -#define OP_IdxDelete     106 /* synopsis: key=r[P2@P3]                     */
  3.1352 -#define OP_IdxRowid      107 /* synopsis: r[P2]=rowid                      */
  3.1353 -#define OP_IdxLT         108 /* synopsis: key=r[P3@P4]                     */
  3.1354 -#define OP_IdxGE         109 /* synopsis: key=r[P3@P4]                     */
  3.1355 -#define OP_Destroy       110
  3.1356 -#define OP_Clear         111
  3.1357 -#define OP_CreateIndex   112 /* synopsis: r[P2]=root iDb=P1                */
  3.1358 -#define OP_CreateTable   113 /* synopsis: r[P2]=root iDb=P1                */
  3.1359 -#define OP_ParseSchema   114
  3.1360 -#define OP_LoadAnalysis  115
  3.1361 -#define OP_DropTable     116
  3.1362 -#define OP_DropIndex     117
  3.1363 -#define OP_DropTrigger   118
  3.1364 -#define OP_IntegrityCk   119
  3.1365 -#define OP_RowSetAdd     120 /* synopsis: rowset(P1)=r[P2]                 */
  3.1366 -#define OP_RowSetRead    121 /* synopsis: r[P3]=rowset(P1)                 */
  3.1367 -#define OP_RowSetTest    122 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  3.1368 -#define OP_Program       123
  3.1369 -#define OP_Param         124
  3.1370 -#define OP_FkCounter     125 /* synopsis: fkctr[P1]+=P2                    */
  3.1371 -#define OP_FkIfZero      126 /* synopsis: if fkctr[P1]==0 goto P2          */
  3.1372 -#define OP_MemMax        127 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  3.1373 -#define OP_IfPos         128 /* synopsis: if r[P1]>0 goto P2               */
  3.1374 -#define OP_IfNeg         129 /* synopsis: if r[P1]<0 goto P2               */
  3.1375 -#define OP_IfZero        130 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  3.1376 -#define OP_Real          131 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  3.1377 -#define OP_AggFinal      132 /* synopsis: accum=r[P1] N=P2                 */
  3.1378 -#define OP_IncrVacuum    133
  3.1379 -#define OP_Expire        134
  3.1380 -#define OP_TableLock     135 /* synopsis: iDb=P1 root=P2 write=P3          */
  3.1381 -#define OP_VBegin        136
  3.1382 -#define OP_VCreate       137
  3.1383 -#define OP_VDestroy      138
  3.1384 -#define OP_VOpen         139
  3.1385 -#define OP_VColumn       140 /* synopsis: r[P3]=vcolumn(P2)                */
  3.1386 -#define OP_VNext         141
  3.1387 -#define OP_ToText        142 /* same as TK_TO_TEXT                         */
  3.1388 -#define OP_ToBlob        143 /* same as TK_TO_BLOB                         */
  3.1389 -#define OP_ToNumeric     144 /* same as TK_TO_NUMERIC                      */
  3.1390 -#define OP_ToInt         145 /* same as TK_TO_INT                          */
  3.1391 -#define OP_ToReal        146 /* same as TK_TO_REAL                         */
  3.1392 -#define OP_VRename       147
  3.1393 -#define OP_Pagecount     148
  3.1394 -#define OP_MaxPgcnt      149
  3.1395 -#define OP_Trace         150
  3.1396 -#define OP_Noop          151
  3.1397 -#define OP_Explain       152
  3.1398 +#define OP_InitCoroutine  20
  3.1399 +#define OP_EndCoroutine   21
  3.1400 +#define OP_Yield          22
  3.1401 +#define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
  3.1402 +#define OP_Halt           24
  3.1403 +#define OP_Integer        25 /* synopsis: r[P2]=P1                         */
  3.1404 +#define OP_Int64          26 /* synopsis: r[P2]=P4                         */
  3.1405 +#define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
  3.1406 +#define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
  3.1407 +#define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
  3.1408 +#define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
  3.1409 +#define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
  3.1410 +#define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
  3.1411 +#define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  3.1412 +#define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
  3.1413 +#define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
  3.1414 +#define OP_CollSeq        36
  3.1415 +#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  3.1416 +#define OP_MustBeInt      38
  3.1417 +#define OP_RealAffinity   39
  3.1418 +#define OP_Permutation    40
  3.1419 +#define OP_Compare        41 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  3.1420 +#define OP_Jump           42
  3.1421 +#define OP_Once           43
  3.1422 +#define OP_If             44
  3.1423 +#define OP_IfNot          45
  3.1424 +#define OP_Column         46 /* synopsis: r[P3]=PX                         */
  3.1425 +#define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
  3.1426 +#define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  3.1427 +#define OP_Count          49 /* synopsis: r[P2]=count()                    */
  3.1428 +#define OP_ReadCookie     50
  3.1429 +#define OP_SetCookie      51
  3.1430 +#define OP_ReopenIdx      52 /* synopsis: root=P2 iDb=P3                   */
  3.1431 +#define OP_OpenRead       53 /* synopsis: root=P2 iDb=P3                   */
  3.1432 +#define OP_OpenWrite      54 /* synopsis: root=P2 iDb=P3                   */
  3.1433 +#define OP_OpenAutoindex  55 /* synopsis: nColumn=P2                       */
  3.1434 +#define OP_OpenEphemeral  56 /* synopsis: nColumn=P2                       */
  3.1435 +#define OP_SorterOpen     57
  3.1436 +#define OP_OpenPseudo     58 /* synopsis: P3 columns in r[P2]              */
  3.1437 +#define OP_Close          59
  3.1438 +#define OP_SeekLT         60 /* synopsis: key=r[P3@P4]                     */
  3.1439 +#define OP_SeekLE         61 /* synopsis: key=r[P3@P4]                     */
  3.1440 +#define OP_SeekGE         62 /* synopsis: key=r[P3@P4]                     */
  3.1441 +#define OP_SeekGT         63 /* synopsis: key=r[P3@P4]                     */
  3.1442 +#define OP_Seek           64 /* synopsis: intkey=r[P2]                     */
  3.1443 +#define OP_NoConflict     65 /* synopsis: key=r[P3@P4]                     */
  3.1444 +#define OP_NotFound       66 /* synopsis: key=r[P3@P4]                     */
  3.1445 +#define OP_Found          67 /* synopsis: key=r[P3@P4]                     */
  3.1446 +#define OP_NotExists      68 /* synopsis: intkey=r[P3]                     */
  3.1447 +#define OP_Sequence       69 /* synopsis: r[P2]=cursor[P1].ctr++           */
  3.1448 +#define OP_NewRowid       70 /* synopsis: r[P2]=rowid                      */
  3.1449 +#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  3.1450 +#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  3.1451 +#define OP_Insert         73 /* synopsis: intkey=r[P3] data=r[P2]          */
  3.1452 +#define OP_InsertInt      74 /* synopsis: intkey=P3 data=r[P2]             */
  3.1453 +#define OP_Delete         75
  3.1454 +#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  3.1455 +#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  3.1456 +#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  3.1457 +#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  3.1458 +#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  3.1459 +#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  3.1460 +#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  3.1461 +#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  3.1462 +#define OP_ResetCount     84
  3.1463 +#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  3.1464 +#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  3.1465 +#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  3.1466 +#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  3.1467 +#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  3.1468 +#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  3.1469 +#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  3.1470 +#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  3.1471 +#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  3.1472 +#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  3.1473 +#define OP_SorterCompare  95 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  3.1474 +#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  3.1475 +#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  3.1476 +#define OP_SorterData     98 /* synopsis: r[P2]=data                       */
  3.1477 +#define OP_RowKey         99 /* synopsis: r[P2]=key                        */
  3.1478 +#define OP_RowData       100 /* synopsis: r[P2]=data                       */
  3.1479 +#define OP_Rowid         101 /* synopsis: r[P2]=rowid                      */
  3.1480 +#define OP_NullRow       102
  3.1481 +#define OP_Last          103
  3.1482 +#define OP_SorterSort    104
  3.1483 +#define OP_Sort          105
  3.1484 +#define OP_Rewind        106
  3.1485 +#define OP_SorterInsert  107
  3.1486 +#define OP_IdxInsert     108 /* synopsis: key=r[P2]                        */
  3.1487 +#define OP_IdxDelete     109 /* synopsis: key=r[P2@P3]                     */
  3.1488 +#define OP_IdxRowid      110 /* synopsis: r[P2]=rowid                      */
  3.1489 +#define OP_IdxLE         111 /* synopsis: key=r[P3@P4]                     */
  3.1490 +#define OP_IdxGT         112 /* synopsis: key=r[P3@P4]                     */
  3.1491 +#define OP_IdxLT         113 /* synopsis: key=r[P3@P4]                     */
  3.1492 +#define OP_IdxGE         114 /* synopsis: key=r[P3@P4]                     */
  3.1493 +#define OP_Destroy       115
  3.1494 +#define OP_Clear         116
  3.1495 +#define OP_ResetSorter   117
  3.1496 +#define OP_CreateIndex   118 /* synopsis: r[P2]=root iDb=P1                */
  3.1497 +#define OP_CreateTable   119 /* synopsis: r[P2]=root iDb=P1                */
  3.1498 +#define OP_ParseSchema   120
  3.1499 +#define OP_LoadAnalysis  121
  3.1500 +#define OP_DropTable     122
  3.1501 +#define OP_DropIndex     123
  3.1502 +#define OP_DropTrigger   124
  3.1503 +#define OP_IntegrityCk   125
  3.1504 +#define OP_RowSetAdd     126 /* synopsis: rowset(P1)=r[P2]                 */
  3.1505 +#define OP_RowSetRead    127 /* synopsis: r[P3]=rowset(P1)                 */
  3.1506 +#define OP_RowSetTest    128 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  3.1507 +#define OP_Program       129
  3.1508 +#define OP_Param         130
  3.1509 +#define OP_FkCounter     131 /* synopsis: fkctr[P1]+=P2                    */
  3.1510 +#define OP_FkIfZero      132 /* synopsis: if fkctr[P1]==0 goto P2          */
  3.1511 +#define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  3.1512 +#define OP_MemMax        134 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  3.1513 +#define OP_IfPos         135 /* synopsis: if r[P1]>0 goto P2               */
  3.1514 +#define OP_IfNeg         136 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
  3.1515 +#define OP_IfZero        137 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  3.1516 +#define OP_AggFinal      138 /* synopsis: accum=r[P1] N=P2                 */
  3.1517 +#define OP_IncrVacuum    139
  3.1518 +#define OP_Expire        140
  3.1519 +#define OP_TableLock     141 /* synopsis: iDb=P1 root=P2 write=P3          */
  3.1520 +#define OP_VBegin        142
  3.1521 +#define OP_ToText        143 /* same as TK_TO_TEXT                         */
  3.1522 +#define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
  3.1523 +#define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
  3.1524 +#define OP_ToInt         146 /* same as TK_TO_INT                          */
  3.1525 +#define OP_ToReal        147 /* same as TK_TO_REAL                         */
  3.1526 +#define OP_VCreate       148
  3.1527 +#define OP_VDestroy      149
  3.1528 +#define OP_VOpen         150
  3.1529 +#define OP_VColumn       151 /* synopsis: r[P3]=vcolumn(P2)                */
  3.1530 +#define OP_VNext         152
  3.1531 +#define OP_VRename       153
  3.1532 +#define OP_Pagecount     154
  3.1533 +#define OP_MaxPgcnt      155
  3.1534 +#define OP_Init          156 /* synopsis: Start at P2                      */
  3.1535 +#define OP_Noop          157
  3.1536 +#define OP_Explain       158
  3.1537  
  3.1538  
  3.1539  /* Properties such as "out2" or "jump" that are specified in
  3.1540 @@ -9194,24 +9426,24 @@
  3.1541  #define OPFLG_INITIALIZER {\
  3.1542  /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  3.1543  /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
  3.1544 -/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x04, 0x10, 0x00, 0x02,\
  3.1545 -/*  24 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x20,\
  3.1546 -/*  32 */ 0x00, 0x00, 0x04, 0x05, 0x04, 0x00, 0x00, 0x01,\
  3.1547 -/*  40 */ 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x02,\
  3.1548 -/*  48 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  3.1549 -/*  56 */ 0x00, 0x11, 0x11, 0x11, 0x11, 0x08, 0x11, 0x11,\
  3.1550 -/*  64 */ 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c, 0x4c, 0x00,\
  3.1551 -/*  72 */ 0x00, 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15,\
  3.1552 -/*  80 */ 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  3.1553 -/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00, 0x24, 0x02,\
  3.1554 -/*  96 */ 0x00, 0x00, 0x02, 0x00, 0x01, 0x01, 0x01, 0x01,\
  3.1555 -/* 104 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  3.1556 -/* 112 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  3.1557 -/* 120 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  3.1558 -/* 128 */ 0x05, 0x05, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00,\
  3.1559 -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x04,\
  3.1560 -/* 144 */ 0x04, 0x04, 0x04, 0x00, 0x02, 0x02, 0x00, 0x00,\
  3.1561 -/* 152 */ 0x00,}
  3.1562 +/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
  3.1563 +/*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
  3.1564 +/*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
  3.1565 +/*  40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
  3.1566 +/*  48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
  3.1567 +/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
  3.1568 +/*  64 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x4c,\
  3.1569 +/*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
  3.1570 +/*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  3.1571 +/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  3.1572 +/*  96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
  3.1573 +/* 104 */ 0x01, 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01,\
  3.1574 +/* 112 */ 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02,\
  3.1575 +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45,\
  3.1576 +/* 128 */ 0x15, 0x01, 0x02, 0x00, 0x01, 0x02, 0x08, 0x05,\
  3.1577 +/* 136 */ 0x05, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,\
  3.1578 +/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,\
  3.1579 +/* 152 */ 0x01, 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
  3.1580  
  3.1581  /************** End of opcodes.h *********************************************/
  3.1582  /************** Continuing where we left off in vdbe.h ***********************/
  3.1583 @@ -9220,14 +9452,14 @@
  3.1584  ** Prototypes for the VDBE interface.  See comments on the implementation
  3.1585  ** for a description of what each of these routines does.
  3.1586  */
  3.1587 -SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  3.1588 +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
  3.1589  SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  3.1590  SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  3.1591  SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  3.1592  SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  3.1593  SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  3.1594  SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  3.1595 -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  3.1596 +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
  3.1597  SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  3.1598  SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  3.1599  SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  3.1600 @@ -9235,6 +9467,7 @@
  3.1601  SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  3.1602  SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  3.1603  SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  3.1604 +SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
  3.1605  SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  3.1606  SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
  3.1607  SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  3.1608 @@ -9265,11 +9498,15 @@
  3.1609  #ifndef SQLITE_OMIT_TRACE
  3.1610  SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  3.1611  #endif
  3.1612 +SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
  3.1613  
  3.1614  SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  3.1615 -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  3.1616 +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
  3.1617  SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  3.1618  
  3.1619 +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*,int);
  3.1620 +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  3.1621 +
  3.1622  #ifndef SQLITE_OMIT_TRIGGER
  3.1623  SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  3.1624  #endif
  3.1625 @@ -9297,6 +9534,43 @@
  3.1626  # define VdbeModuleComment(X)
  3.1627  #endif
  3.1628  
  3.1629 +/*
  3.1630 +** The VdbeCoverage macros are used to set a coverage testing point
  3.1631 +** for VDBE branch instructions.  The coverage testing points are line
  3.1632 +** numbers in the sqlite3.c source file.  VDBE branch coverage testing
  3.1633 +** only works with an amalagmation build.  That's ok since a VDBE branch
  3.1634 +** coverage build designed for testing the test suite only.  No application
  3.1635 +** should ever ship with VDBE branch coverage measuring turned on.
  3.1636 +**
  3.1637 +**    VdbeCoverage(v)                  // Mark the previously coded instruction
  3.1638 +**                                     // as a branch
  3.1639 +**
  3.1640 +**    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
  3.1641 +**
  3.1642 +**    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
  3.1643 +**
  3.1644 +**    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
  3.1645 +**
  3.1646 +** Every VDBE branch operation must be tagged with one of the macros above.
  3.1647 +** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
  3.1648 +** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
  3.1649 +** routine in vdbe.c, alerting the developer to the missed tag.
  3.1650 +*/
  3.1651 +#ifdef SQLITE_VDBE_COVERAGE
  3.1652 +SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
  3.1653 +# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
  3.1654 +# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
  3.1655 +# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
  3.1656 +# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
  3.1657 +# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
  3.1658 +#else
  3.1659 +# define VdbeCoverage(v)
  3.1660 +# define VdbeCoverageIf(v,x)
  3.1661 +# define VdbeCoverageAlwaysTaken(v)
  3.1662 +# define VdbeCoverageNeverTaken(v)
  3.1663 +# define VDBE_OFFSET_LINENO(x) 0
  3.1664 +#endif
  3.1665 +
  3.1666  #endif
  3.1667  
  3.1668  /************** End of vdbe.h ************************************************/
  3.1669 @@ -9441,6 +9715,7 @@
  3.1670  SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  3.1671  SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  3.1672  SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  3.1673 +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
  3.1674  
  3.1675  /* Operations on page references. */
  3.1676  SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  3.1677 @@ -9455,7 +9730,7 @@
  3.1678  SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  3.1679  SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  3.1680  SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  3.1681 -SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  3.1682 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
  3.1683  SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  3.1684  SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  3.1685  SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  3.1686 @@ -9707,83 +9982,71 @@
  3.1687  #define _SQLITE_OS_H_
  3.1688  
  3.1689  /*
  3.1690 -** Figure out if we are dealing with Unix, Windows, or some other
  3.1691 -** operating system.  After the following block of preprocess macros,
  3.1692 -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  3.1693 -** will defined to either 1 or 0.  One of the four will be 1.  The other 
  3.1694 -** three will be 0.
  3.1695 +** Attempt to automatically detect the operating system and setup the
  3.1696 +** necessary pre-processor macros for it.
  3.1697 +*/
  3.1698 +/************** Include os_setup.h in the middle of os.h *********************/
  3.1699 +/************** Begin file os_setup.h ****************************************/
  3.1700 +/*
  3.1701 +** 2013 November 25
  3.1702 +**
  3.1703 +** The author disclaims copyright to this source code.  In place of
  3.1704 +** a legal notice, here is a blessing:
  3.1705 +**
  3.1706 +**    May you do good and not evil.
  3.1707 +**    May you find forgiveness for yourself and forgive others.
  3.1708 +**    May you share freely, never taking more than you give.
  3.1709 +**
  3.1710 +******************************************************************************
  3.1711 +**
  3.1712 +** This file contains pre-processor directives related to operating system
  3.1713 +** detection and/or setup.
  3.1714 +*/
  3.1715 +#ifndef _OS_SETUP_H_
  3.1716 +#define _OS_SETUP_H_
  3.1717 +
  3.1718 +/*
  3.1719 +** Figure out if we are dealing with Unix, Windows, or some other operating
  3.1720 +** system.
  3.1721 +**
  3.1722 +** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
  3.1723 +** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
  3.1724 +** the three will be 1.  The other two will be 0.
  3.1725  */
  3.1726  #if defined(SQLITE_OS_OTHER)
  3.1727 -# if SQLITE_OS_OTHER==1
  3.1728 -#   undef SQLITE_OS_UNIX
  3.1729 -#   define SQLITE_OS_UNIX 0
  3.1730 -#   undef SQLITE_OS_WIN
  3.1731 -#   define SQLITE_OS_WIN 0
  3.1732 -# else
  3.1733 -#   undef SQLITE_OS_OTHER
  3.1734 -# endif
  3.1735 +#  if SQLITE_OS_OTHER==1
  3.1736 +#    undef SQLITE_OS_UNIX
  3.1737 +#    define SQLITE_OS_UNIX 0
  3.1738 +#    undef SQLITE_OS_WIN
  3.1739 +#    define SQLITE_OS_WIN 0
  3.1740 +#  else
  3.1741 +#    undef SQLITE_OS_OTHER
  3.1742 +#  endif
  3.1743  #endif
  3.1744  #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  3.1745 -# define SQLITE_OS_OTHER 0
  3.1746 -# ifndef SQLITE_OS_WIN
  3.1747 -#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  3.1748 -#     define SQLITE_OS_WIN 1
  3.1749 -#     define SQLITE_OS_UNIX 0
  3.1750 -#   else
  3.1751 -#     define SQLITE_OS_WIN 0
  3.1752 -#     define SQLITE_OS_UNIX 1
  3.1753 +#  define SQLITE_OS_OTHER 0
  3.1754 +#  ifndef SQLITE_OS_WIN
  3.1755 +#    if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
  3.1756 +        defined(__MINGW32__) || defined(__BORLANDC__)
  3.1757 +#      define SQLITE_OS_WIN 1
  3.1758 +#      define SQLITE_OS_UNIX 0
  3.1759 +#    else
  3.1760 +#      define SQLITE_OS_WIN 0
  3.1761 +#      define SQLITE_OS_UNIX 1
  3.1762 +#    endif
  3.1763 +#  else
  3.1764 +#    define SQLITE_OS_UNIX 0
  3.1765  #  endif
  3.1766 -# else
  3.1767 -#  define SQLITE_OS_UNIX 0
  3.1768 -# endif
  3.1769 -#else
  3.1770 -# ifndef SQLITE_OS_WIN
  3.1771 -#  define SQLITE_OS_WIN 0
  3.1772 -# endif
  3.1773 -#endif
  3.1774 -
  3.1775 -#if SQLITE_OS_WIN
  3.1776 -# include <windows.h>
  3.1777 -#endif
  3.1778 -
  3.1779 -/*
  3.1780 -** Determine if we are dealing with Windows NT.
  3.1781 -**
  3.1782 -** We ought to be able to determine if we are compiling for win98 or winNT
  3.1783 -** using the _WIN32_WINNT macro as follows:
  3.1784 -**
  3.1785 -** #if defined(_WIN32_WINNT)
  3.1786 -** # define SQLITE_OS_WINNT 1
  3.1787 -** #else
  3.1788 -** # define SQLITE_OS_WINNT 0
  3.1789 -** #endif
  3.1790 -**
  3.1791 -** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  3.1792 -** so the above test does not work.  We'll just assume that everything is
  3.1793 -** winNT unless the programmer explicitly says otherwise by setting
  3.1794 -** SQLITE_OS_WINNT to 0.
  3.1795 -*/
  3.1796 -#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  3.1797 -# define SQLITE_OS_WINNT 1
  3.1798 -#endif
  3.1799 -
  3.1800 -/*
  3.1801 -** Determine if we are dealing with WindowsCE - which has a much
  3.1802 -** reduced API.
  3.1803 -*/
  3.1804 -#if defined(_WIN32_WCE)
  3.1805 -# define SQLITE_OS_WINCE 1
  3.1806 -#else
  3.1807 -# define SQLITE_OS_WINCE 0
  3.1808 -#endif
  3.1809 -
  3.1810 -/*
  3.1811 -** Determine if we are dealing with WinRT, which provides only a subset of
  3.1812 -** the full Win32 API.
  3.1813 -*/
  3.1814 -#if !defined(SQLITE_OS_WINRT)
  3.1815 -# define SQLITE_OS_WINRT 0
  3.1816 -#endif
  3.1817 +#else
  3.1818 +#  ifndef SQLITE_OS_WIN
  3.1819 +#    define SQLITE_OS_WIN 0
  3.1820 +#  endif
  3.1821 +#endif
  3.1822 +
  3.1823 +#endif /* _OS_SETUP_H_ */
  3.1824 +
  3.1825 +/************** End of os_setup.h ********************************************/
  3.1826 +/************** Continuing where we left off in os.h *************************/
  3.1827  
  3.1828  /* If the SET_FULLSYNC macro is not defined above, then make it
  3.1829  ** a no-op
  3.1830 @@ -10087,7 +10350,7 @@
  3.1831    Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  3.1832    u8 file_format;      /* Schema format version for this file */
  3.1833    u8 enc;              /* Text encoding used by this database */
  3.1834 -  u16 flags;           /* Flags associated with this schema */
  3.1835 +  u16 schemaFlags;     /* Flags associated with this schema */
  3.1836    int cache_size;      /* Number of pages to use in the cache */
  3.1837  };
  3.1838  
  3.1839 @@ -10095,10 +10358,10 @@
  3.1840  ** These macros can be used to test, set, or clear bits in the 
  3.1841  ** Db.pSchema->flags field.
  3.1842  */
  3.1843 -#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
  3.1844 -#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
  3.1845 -#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
  3.1846 -#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
  3.1847 +#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
  3.1848 +#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
  3.1849 +#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
  3.1850 +#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
  3.1851  
  3.1852  /*
  3.1853  ** Allowed values for the DB.pSchema->flags field.
  3.1854 @@ -10327,7 +10590,7 @@
  3.1855  #define SQLITE_ColumnCache    0x0002   /* Column cache */
  3.1856  #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
  3.1857  #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
  3.1858 -#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
  3.1859 +/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
  3.1860  #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
  3.1861  #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
  3.1862  #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
  3.1863 @@ -10353,8 +10616,7 @@
  3.1864  ** Return true if it OK to factor constant expressions into the initialization
  3.1865  ** code. The argument is a Parse object for the code generator.
  3.1866  */
  3.1867 -#define ConstFactorOk(P) \
  3.1868 -  ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
  3.1869 +#define ConstFactorOk(P) ((P)->okConstFactor)
  3.1870  
  3.1871  /*
  3.1872  ** Possible values for the sqlite.magic field.
  3.1873 @@ -10580,10 +10842,16 @@
  3.1874  /*
  3.1875  ** Additional bit values that can be ORed with an affinity without
  3.1876  ** changing the affinity.
  3.1877 +**
  3.1878 +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
  3.1879 +** It causes an assert() to fire if either operand to a comparison
  3.1880 +** operator is NULL.  It is added to certain comparison operators to
  3.1881 +** prove that the operands are always NOT NULL.
  3.1882  */
  3.1883  #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
  3.1884  #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
  3.1885  #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
  3.1886 +#define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
  3.1887  
  3.1888  /*
  3.1889  ** An object of this type is created for each virtual table present in
  3.1890 @@ -10677,12 +10945,15 @@
  3.1891  #ifndef SQLITE_OMIT_CHECK
  3.1892    ExprList *pCheck;    /* All CHECK constraints */
  3.1893  #endif
  3.1894 -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  3.1895 +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  3.1896    int tnum;            /* Root BTree node for this table (see note above) */
  3.1897    i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  3.1898    i16 nCol;            /* Number of columns in this table */
  3.1899    u16 nRef;            /* Number of pointers to this Table */
  3.1900    LogEst szTabRow;     /* Estimated size of each table row in bytes */
  3.1901 +#ifdef SQLITE_ENABLE_COSTMULT
  3.1902 +  LogEst costMult;     /* Cost multiplier for using this table */
  3.1903 +#endif
  3.1904    u8 tabFlags;         /* Mask of TF_* values */
  3.1905    u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  3.1906  #ifndef SQLITE_OMIT_ALTERTABLE
  3.1907 @@ -10699,7 +10970,7 @@
  3.1908  };
  3.1909  
  3.1910  /*
  3.1911 -** Allowed values for Tabe.tabFlags.
  3.1912 +** Allowed values for Table.tabFlags.
  3.1913  */
  3.1914  #define TF_Readonly        0x01    /* Read-only system table */
  3.1915  #define TF_Ephemeral       0x02    /* An ephemeral table */
  3.1916 @@ -10842,19 +11113,20 @@
  3.1917  **
  3.1918  ** This structure holds a record that has already been disassembled
  3.1919  ** into its constituent fields.
  3.1920 +**
  3.1921 +** The r1 and r2 member variables are only used by the optimized comparison
  3.1922 +** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
  3.1923  */
  3.1924  struct UnpackedRecord {
  3.1925    KeyInfo *pKeyInfo;  /* Collation and sort-order information */
  3.1926    u16 nField;         /* Number of entries in apMem[] */
  3.1927 -  u8 flags;           /* Boolean settings.  UNPACKED_... below */
  3.1928 +  i8 default_rc;      /* Comparison result if keys are equal */
  3.1929 +  u8 isCorrupt;       /* Corruption detected by xRecordCompare() */
  3.1930    Mem *aMem;          /* Values */
  3.1931 -};
  3.1932 -
  3.1933 -/*
  3.1934 -** Allowed values of UnpackedRecord.flags
  3.1935 -*/
  3.1936 -#define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
  3.1937 -#define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
  3.1938 +  int r1;             /* Value to return if (lhs > rhs) */
  3.1939 +  int r2;             /* Value to return if (rhs < lhs) */
  3.1940 +};
  3.1941 +
  3.1942  
  3.1943  /*
  3.1944  ** Each SQL index is represented in memory by an
  3.1945 @@ -10885,7 +11157,7 @@
  3.1946  struct Index {
  3.1947    char *zName;             /* Name of this index */
  3.1948    i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
  3.1949 -  tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
  3.1950 +  LogEst *aiRowLogEst;     /* From ANALYZE: Est. rows selected by each column */
  3.1951    Table *pTable;           /* The SQL table being indexed */
  3.1952    char *zColAff;           /* String defining the affinity of each column */
  3.1953    Index *pNext;            /* The next index associated with the same table */
  3.1954 @@ -10899,7 +11171,7 @@
  3.1955    u16 nKeyCol;             /* Number of columns forming the key */
  3.1956    u16 nColumn;             /* Number of columns stored in the index */
  3.1957    u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  3.1958 -  unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  3.1959 +  unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  3.1960    unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  3.1961    unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  3.1962    unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  3.1963 @@ -10913,6 +11185,19 @@
  3.1964  };
  3.1965  
  3.1966  /*
  3.1967 +** Allowed values for Index.idxType
  3.1968 +*/
  3.1969 +#define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
  3.1970 +#define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
  3.1971 +#define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
  3.1972 +
  3.1973 +/* Return true if index X is a PRIMARY KEY index */
  3.1974 +#define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  3.1975 +
  3.1976 +/* Return true if index X is a UNIQUE index */
  3.1977 +#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
  3.1978 +
  3.1979 +/*
  3.1980  ** Each sample stored in the sqlite_stat3 table is represented in memory 
  3.1981  ** using a structure of this type.  See documentation at the top of the
  3.1982  ** analyze.c source file for additional information.
  3.1983 @@ -10959,6 +11244,7 @@
  3.1984    int sortingIdx;         /* Cursor number of the sorting index */
  3.1985    int sortingIdxPTab;     /* Cursor number of pseudo-table */
  3.1986    int nSortingColumn;     /* Number of columns in the sorting index */
  3.1987 +  int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
  3.1988    ExprList *pGroupBy;     /* The group by clause */
  3.1989    struct AggInfo_col {    /* For each column used in source tables */
  3.1990      Table *pTab;             /* Source table */
  3.1991 @@ -11115,8 +11401,8 @@
  3.1992  #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  3.1993  #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  3.1994  #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  3.1995 -#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
  3.1996 -      /* unused      0x000200 */
  3.1997 +#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
  3.1998 +#define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
  3.1999  #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
  3.2000  #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
  3.2001  #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
  3.2002 @@ -11180,7 +11466,6 @@
  3.2003  */
  3.2004  struct ExprList {
  3.2005    int nExpr;             /* Number of expressions on the list */
  3.2006 -  int iECursor;          /* VDBE Cursor associated with this ExprList */
  3.2007    struct ExprList_item { /* For each expression in the list */
  3.2008      Expr *pExpr;            /* The list of expressions */
  3.2009      char *zName;            /* Token associated with this expression */
  3.2010 @@ -11251,6 +11536,7 @@
  3.2011  ** A bit in a Bitmask
  3.2012  */
  3.2013  #define MASKBIT(n)   (((Bitmask)1)<<(n))
  3.2014 +#define MASKBIT32(n) (((unsigned int)1)<<(n))
  3.2015  
  3.2016  /*
  3.2017  ** The following structure describes the FROM clause of a SELECT statement.
  3.2018 @@ -11272,8 +11558,8 @@
  3.2019  ** contains more than 63 columns and the 64-th or later column is used.
  3.2020  */
  3.2021  struct SrcList {
  3.2022 -  u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
  3.2023 -  u8 nAlloc;      /* Number of entries allocated in a[] below */
  3.2024 +  int nSrc;        /* Number of tables or subqueries in the FROM clause */
  3.2025 +  u32 nAlloc;      /* Number of entries allocated in a[] below */
  3.2026    struct SrcList_item {
  3.2027      Schema *pSchema;  /* Schema to which this item is fixed */
  3.2028      char *zDatabase;  /* Name of database holding this table */
  3.2029 @@ -11283,10 +11569,12 @@
  3.2030      Select *pSelect;  /* A SELECT statement used in place of a table name */
  3.2031      int addrFillSub;  /* Address of subroutine to manifest a subquery */
  3.2032      int regReturn;    /* Register holding return address of addrFillSub */
  3.2033 +    int regResult;    /* Registers holding results of a co-routine */
  3.2034      u8 jointype;      /* Type of join between this able and the previous */
  3.2035      unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
  3.2036      unsigned isCorrelated :1;  /* True if sub-query is correlated */
  3.2037      unsigned viaCoroutine :1;  /* Implemented as a co-routine */
  3.2038 +    unsigned isRecursive :1;   /* True for recursive reference in WITH */
  3.2039  #ifndef SQLITE_OMIT_EXPLAIN
  3.2040      u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
  3.2041  #endif
  3.2042 @@ -11327,6 +11615,8 @@
  3.2043  #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
  3.2044  #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
  3.2045  #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
  3.2046 +#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
  3.2047 +#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  3.2048  
  3.2049  /* Allowed return values from sqlite3WhereIsDistinct()
  3.2050  */
  3.2051 @@ -11401,7 +11691,7 @@
  3.2052    u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  3.2053    u16 selFlags;          /* Various SF_* values */
  3.2054    int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  3.2055 -  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
  3.2056 +  int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  3.2057    u64 nSelectRow;        /* Estimated number of result rows */
  3.2058    SrcList *pSrc;         /* The FROM clause */
  3.2059    Expr *pWhere;          /* The WHERE clause */
  3.2060 @@ -11410,9 +11700,9 @@
  3.2061    ExprList *pOrderBy;    /* The ORDER BY clause */
  3.2062    Select *pPrior;        /* Prior select in a compound select statement */
  3.2063    Select *pNext;         /* Next select to the left in a compound */
  3.2064 -  Select *pRightmost;    /* Right-most select in a compound select statement */
  3.2065    Expr *pLimit;          /* LIMIT expression. NULL means not used. */
  3.2066    Expr *pOffset;         /* OFFSET expression. NULL means not used. */
  3.2067 +  With *pWith;           /* WITH clause attached to this select. Or NULL. */
  3.2068  };
  3.2069  
  3.2070  /*
  3.2071 @@ -11425,42 +11715,109 @@
  3.2072  #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  3.2073  #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  3.2074  #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  3.2075 -#define SF_UseSorter       0x0040  /* Sort using a sorter */
  3.2076 +                    /*     0x0040  NOT USED */
  3.2077  #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  3.2078 -#define SF_Materialize     0x0100  /* Force materialization of views */
  3.2079 +                    /*     0x0100  NOT USED */
  3.2080  #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  3.2081  #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  3.2082 -
  3.2083 -
  3.2084 -/*
  3.2085 -** The results of a select can be distributed in several ways.  The
  3.2086 -** "SRT" prefix means "SELECT Result Type".
  3.2087 +#define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  3.2088 +#define SF_Compound        0x1000  /* Part of a compound query */
  3.2089 +
  3.2090 +
  3.2091 +/*
  3.2092 +** The results of a SELECT can be distributed in several ways, as defined
  3.2093 +** by one of the following macros.  The "SRT" prefix means "SELECT Result
  3.2094 +** Type".
  3.2095 +**
  3.2096 +**     SRT_Union       Store results as a key in a temporary index 
  3.2097 +**                     identified by pDest->iSDParm.
  3.2098 +**
  3.2099 +**     SRT_Except      Remove results from the temporary index pDest->iSDParm.
  3.2100 +**
  3.2101 +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
  3.2102 +**                     set is not empty.
  3.2103 +**
  3.2104 +**     SRT_Discard     Throw the results away.  This is used by SELECT
  3.2105 +**                     statements within triggers whose only purpose is
  3.2106 +**                     the side-effects of functions.
  3.2107 +**
  3.2108 +** All of the above are free to ignore their ORDER BY clause. Those that
  3.2109 +** follow must honor the ORDER BY clause.
  3.2110 +**
  3.2111 +**     SRT_Output      Generate a row of output (using the OP_ResultRow
  3.2112 +**                     opcode) for each row in the result set.
  3.2113 +**
  3.2114 +**     SRT_Mem         Only valid if the result is a single column.
  3.2115 +**                     Store the first column of the first result row
  3.2116 +**                     in register pDest->iSDParm then abandon the rest
  3.2117 +**                     of the query.  This destination implies "LIMIT 1".
  3.2118 +**
  3.2119 +**     SRT_Set         The result must be a single column.  Store each
  3.2120 +**                     row of result as the key in table pDest->iSDParm. 
  3.2121 +**                     Apply the affinity pDest->affSdst before storing
  3.2122 +**                     results.  Used to implement "IN (SELECT ...)".
  3.2123 +**
  3.2124 +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
  3.2125 +**                     the result there. The cursor is left open after
  3.2126 +**                     returning.  This is like SRT_Table except that
  3.2127 +**                     this destination uses OP_OpenEphemeral to create
  3.2128 +**                     the table first.
  3.2129 +**
  3.2130 +**     SRT_Coroutine   Generate a co-routine that returns a new row of
  3.2131 +**                     results each time it is invoked.  The entry point
  3.2132 +**                     of the co-routine is stored in register pDest->iSDParm
  3.2133 +**                     and the result row is stored in pDest->nDest registers
  3.2134 +**                     starting with pDest->iSdst.
  3.2135 +**
  3.2136 +**     SRT_Table       Store results in temporary table pDest->iSDParm.
  3.2137 +**     SRT_Fifo        This is like SRT_EphemTab except that the table
  3.2138 +**                     is assumed to already be open.  SRT_Fifo has
  3.2139 +**                     the additional property of being able to ignore
  3.2140 +**                     the ORDER BY clause.
  3.2141 +**
  3.2142 +**     SRT_DistFifo    Store results in a temporary table pDest->iSDParm.
  3.2143 +**                     But also use temporary table pDest->iSDParm+1 as
  3.2144 +**                     a record of all prior results and ignore any duplicate
  3.2145 +**                     rows.  Name means:  "Distinct Fifo".
  3.2146 +**
  3.2147 +**     SRT_Queue       Store results in priority queue pDest->iSDParm (really
  3.2148 +**                     an index).  Append a sequence number so that all entries
  3.2149 +**                     are distinct.
  3.2150 +**
  3.2151 +**     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
  3.2152 +**                     the same record has never been stored before.  The
  3.2153 +**                     index at pDest->iSDParm+1 hold all prior stores.
  3.2154  */
  3.2155  #define SRT_Union        1  /* Store result as keys in an index */
  3.2156  #define SRT_Except       2  /* Remove result from a UNION index */
  3.2157  #define SRT_Exists       3  /* Store 1 if the result is not empty */
  3.2158  #define SRT_Discard      4  /* Do not save the results anywhere */
  3.2159 +#define SRT_Fifo         5  /* Store result as data with an automatic rowid */
  3.2160 +#define SRT_DistFifo     6  /* Like SRT_Fifo, but unique results only */
  3.2161 +#define SRT_Queue        7  /* Store result in an queue */
  3.2162 +#define SRT_DistQueue    8  /* Like SRT_Queue, but unique results only */
  3.2163  
  3.2164  /* The ORDER BY clause is ignored for all of the above */
  3.2165 -#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
  3.2166 -
  3.2167 -#define SRT_Output       5  /* Output each row of result */
  3.2168 -#define SRT_Mem          6  /* Store result in a memory cell */
  3.2169 -#define SRT_Set          7  /* Store results as keys in an index */
  3.2170 -#define SRT_Table        8  /* Store result as data with an automatic rowid */
  3.2171 -#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
  3.2172 -#define SRT_Coroutine   10  /* Generate a single row of result */
  3.2173 +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
  3.2174 +
  3.2175 +#define SRT_Output       9  /* Output each row of result */
  3.2176 +#define SRT_Mem         10  /* Store result in a memory cell */
  3.2177 +#define SRT_Set         11  /* Store results as keys in an index */
  3.2178 +#define SRT_EphemTab    12  /* Create transient tab and store like SRT_Table */
  3.2179 +#define SRT_Coroutine   13  /* Generate a single row of result */
  3.2180 +#define SRT_Table       14  /* Store result as data with an automatic rowid */
  3.2181  
  3.2182  /*
  3.2183  ** An instance of this object describes where to put of the results of
  3.2184  ** a SELECT statement.
  3.2185  */
  3.2186  struct SelectDest {
  3.2187 -  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
  3.2188 -  char affSdst;     /* Affinity used when eDest==SRT_Set */
  3.2189 -  int iSDParm;      /* A parameter used by the eDest disposal method */
  3.2190 -  int iSdst;        /* Base register where results are written */
  3.2191 -  int nSdst;        /* Number of registers allocated */
  3.2192 +  u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
  3.2193 +  char affSdst;        /* Affinity used when eDest==SRT_Set */
  3.2194 +  int iSDParm;         /* A parameter used by the eDest disposal method */
  3.2195 +  int iSdst;           /* Base register where results are written */
  3.2196 +  int nSdst;           /* Number of registers allocated */
  3.2197 +  ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
  3.2198  };
  3.2199  
  3.2200  /*
  3.2201 @@ -11516,9 +11873,19 @@
  3.2202  ** The yDbMask datatype for the bitmask of all attached databases.
  3.2203  */
  3.2204  #if SQLITE_MAX_ATTACHED>30
  3.2205 -  typedef sqlite3_uint64 yDbMask;
  3.2206 +  typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
  3.2207 +# define DbMaskTest(M,I)    (((M)[(I)/8]&(1<<((I)&7)))!=0)
  3.2208 +# define DbMaskZero(M)      memset((M),0,sizeof(M))
  3.2209 +# define DbMaskSet(M,I)     (M)[(I)/8]|=(1<<((I)&7))
  3.2210 +# define DbMaskAllZero(M)   sqlite3DbMaskAllZero(M)
  3.2211 +# define DbMaskNonZero(M)   (sqlite3DbMaskAllZero(M)==0)
  3.2212  #else
  3.2213    typedef unsigned int yDbMask;
  3.2214 +# define DbMaskTest(M,I)    (((M)&(((yDbMask)1)<<(I)))!=0)
  3.2215 +# define DbMaskZero(M)      (M)=0
  3.2216 +# define DbMaskSet(M,I)     (M)|=(((yDbMask)1)<<(I))
  3.2217 +# define DbMaskAllZero(M)   (M)==0
  3.2218 +# define DbMaskNonZero(M)   (M)!=0
  3.2219  #endif
  3.2220  
  3.2221  /*
  3.2222 @@ -11546,12 +11913,10 @@
  3.2223    u8 checkSchema;      /* Causes schema cookie check after an error */
  3.2224    u8 nested;           /* Number of nested calls to the parser/code generator */
  3.2225    u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
  3.2226 -  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
  3.2227 -  u8 nColCache;        /* Number of entries in aColCache[] */
  3.2228 -  u8 iColCache;        /* Next entry in aColCache[] to replace */
  3.2229    u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
  3.2230    u8 mayAbort;         /* True if statement may throw an ABORT exception */
  3.2231    u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
  3.2232 +  u8 okConstFactor;    /* OK to factor out constants */
  3.2233    int aTempReg[8];     /* Holding area for temporary registers */
  3.2234    int nRangeReg;       /* Size of the temporary register block */
  3.2235    int iRangeReg;       /* First register in temporary register block */
  3.2236 @@ -11560,27 +11925,30 @@
  3.2237    int nMem;            /* Number of memory cells used so far */
  3.2238    int nSet;            /* Number of sets used so far */
  3.2239    int nOnce;           /* Number of OP_Once instructions so far */
  3.2240 +  int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
  3.2241 +  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
  3.2242    int ckBase;          /* Base register of data during check constraints */
  3.2243    int iPartIdxTab;     /* Table corresponding to a partial index */
  3.2244    int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  3.2245    int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
  3.2246 +  int nLabel;          /* Number of labels used */
  3.2247 +  int *aLabel;         /* Space to hold the labels */
  3.2248    struct yColCache {
  3.2249      int iTable;           /* Table cursor number */
  3.2250 -    int iColumn;          /* Table column number */
  3.2251 +    i16 iColumn;          /* Table column number */
  3.2252      u8 tempReg;           /* iReg is a temp register that needs to be freed */
  3.2253      int iLevel;           /* Nesting level */
  3.2254      int iReg;             /* Reg with value of this column. 0 means none. */
  3.2255      int lru;              /* Least recently used entry has the smallest value */
  3.2256    } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  3.2257    ExprList *pConstExpr;/* Constant expressions */
  3.2258 +  Token constraintName;/* Name of the constraint currently being parsed */
  3.2259    yDbMask writeMask;   /* Start a write transaction on these databases */
  3.2260    yDbMask cookieMask;  /* Bitmask of schema verified databases */
  3.2261 -  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  3.2262    int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  3.2263    int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  3.2264    int regRoot;         /* Register holding root page number for new objects */
  3.2265    int nMaxArg;         /* Max args passed to user function by sub-program */
  3.2266 -  Token constraintName;/* Name of the constraint currently being parsed */
  3.2267  #ifndef SQLITE_OMIT_SHARED_CACHE
  3.2268    int nTableLock;        /* Number of locks in aTableLock */
  3.2269    TableLock *aTableLock; /* Required table locks for shared-cache mode */
  3.2270 @@ -11599,12 +11967,17 @@
  3.2271    u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  3.2272    u8 disableTriggers;  /* True to disable triggers */
  3.2273  
  3.2274 -  /* Above is constant between recursions.  Below is reset before and after
  3.2275 -  ** each recursion */
  3.2276 +  /************************************************************************
  3.2277 +  ** Above is constant between recursions.  Below is reset before and after
  3.2278 +  ** each recursion.  The boundary between these two regions is determined
  3.2279 +  ** using offsetof(Parse,nVar) so the nVar field must be the first field
  3.2280 +  ** in the recursive region.
  3.2281 +  ************************************************************************/
  3.2282  
  3.2283    int nVar;                 /* Number of '?' variables seen in the SQL so far */
  3.2284    int nzVar;                /* Number of available slots in azVar[] */
  3.2285    u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
  3.2286 +  u8 bFreeWith;             /* True if pWith should be freed with parser */
  3.2287    u8 explain;               /* True if the EXPLAIN flag is found on the query */
  3.2288  #ifndef SQLITE_OMIT_VIRTUALTABLE
  3.2289    u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
  3.2290 @@ -11630,6 +12003,7 @@
  3.2291  #endif
  3.2292    Table *pZombieTab;        /* List of Table objects to delete after code gen */
  3.2293    TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
  3.2294 +  With *pWith;              /* Current WITH clause, or NULL */
  3.2295  };
  3.2296  
  3.2297  /*
  3.2298 @@ -11749,7 +12123,7 @@
  3.2299    Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
  3.2300    Token target;        /* Target table for DELETE, UPDATE, INSERT */
  3.2301    Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
  3.2302 -  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
  3.2303 +  ExprList *pExprList; /* SET clause for UPDATE. */
  3.2304    IdList *pIdList;     /* Column names for INSERT */
  3.2305    TriggerStep *pNext;  /* Next in the link-list */
  3.2306    TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
  3.2307 @@ -11836,15 +12210,25 @@
  3.2308    int isMutexInit;                  /* True after mutexes are initialized */
  3.2309    int isMallocInit;                 /* True after malloc is initialized */
  3.2310    int isPCacheInit;                 /* True after malloc is initialized */
  3.2311 +  int nRefInitMutex;                /* Number of users of pInitMutex */
  3.2312    sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
  3.2313 -  int nRefInitMutex;                /* Number of users of pInitMutex */
  3.2314    void (*xLog)(void*,int,const char*); /* Function for logging */
  3.2315    void *pLogArg;                       /* First argument to xLog() */
  3.2316 -  int bLocaltimeFault;              /* True to fail localtime() calls */
  3.2317  #ifdef SQLITE_ENABLE_SQLLOG
  3.2318    void(*xSqllog)(void*,sqlite3*,const char*, int);
  3.2319    void *pSqllogArg;
  3.2320  #endif
  3.2321 +#ifdef SQLITE_VDBE_COVERAGE
  3.2322 +  /* The following callback (if not NULL) is invoked on every VDBE branch
  3.2323 +  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
  3.2324 +  */
  3.2325 +  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
  3.2326 +  void *pVdbeBranchArg;                                     /* 1st argument */
  3.2327 +#endif
  3.2328 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  3.2329 +  int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
  3.2330 +#endif
  3.2331 +  int bLocaltimeFault;              /* True to fail localtime() calls */
  3.2332  };
  3.2333  
  3.2334  /*
  3.2335 @@ -11871,9 +12255,9 @@
  3.2336  struct Walker {
  3.2337    int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
  3.2338    int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
  3.2339 +  void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
  3.2340    Parse *pParse;                            /* Parser context.  */
  3.2341    int walkerDepth;                          /* Number of subqueries */
  3.2342 -  u8 bSelectDepthFirst;                     /* Do subqueries first */
  3.2343    union {                                   /* Extra data for callback */
  3.2344      NameContext *pNC;                          /* Naming context */
  3.2345      int i;                                     /* Integer value */
  3.2346 @@ -11898,6 +12282,21 @@
  3.2347  #define WRC_Abort       2   /* Abandon the tree walk */
  3.2348  
  3.2349  /*
  3.2350 +** An instance of this structure represents a set of one or more CTEs
  3.2351 +** (common table expressions) created by a single WITH clause.
  3.2352 +*/
  3.2353 +struct With {
  3.2354 +  int nCte;                       /* Number of CTEs in the WITH clause */
  3.2355 +  With *pOuter;                   /* Containing WITH clause, or NULL */
  3.2356 +  struct Cte {                    /* For each CTE in the WITH clause.... */
  3.2357 +    char *zName;                    /* Name of this CTE */
  3.2358 +    ExprList *pCols;                /* List of explicit column names, or NULL */
  3.2359 +    Select *pSelect;                /* The definition of this CTE */
  3.2360 +    const char *zErr;               /* Error message for circular references */
  3.2361 +  } a[1];
  3.2362 +};
  3.2363 +
  3.2364 +/*
  3.2365  ** Assuming zIn points to the first byte of a UTF-8 character,
  3.2366  ** advance zIn to point to the first byte of the next UTF-8 character.
  3.2367  */
  3.2368 @@ -12036,10 +12435,20 @@
  3.2369  # define sqlite3IsNaN(X)  0
  3.2370  #endif
  3.2371  
  3.2372 -SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
  3.2373 -#ifndef SQLITE_OMIT_TRACE
  3.2374 -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
  3.2375 -#endif
  3.2376 +/*
  3.2377 +** An instance of the following structure holds information about SQL
  3.2378 +** functions arguments that are the parameters to the printf() function.
  3.2379 +*/
  3.2380 +struct PrintfArguments {
  3.2381 +  int nArg;                /* Total number of arguments */
  3.2382 +  int nUsed;               /* Number of arguments used so far */
  3.2383 +  sqlite3_value **apArg;   /* The argument values */
  3.2384 +};
  3.2385 +
  3.2386 +#define SQLITE_PRINTF_INTERNAL 0x01
  3.2387 +#define SQLITE_PRINTF_SQLFUNC  0x02
  3.2388 +SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
  3.2389 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
  3.2390  SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
  3.2391  SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  3.2392  SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
  3.2393 @@ -12121,6 +12530,12 @@
  3.2394  SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  3.2395  SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
  3.2396  
  3.2397 +#ifdef SQLITE_OMIT_BUILTIN_TEST
  3.2398 +# define sqlite3FaultSim(X) SQLITE_OK
  3.2399 +#else
  3.2400 +SQLITE_PRIVATE   int sqlite3FaultSim(int);
  3.2401 +#endif
  3.2402 +
  3.2403  SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
  3.2404  SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
  3.2405  SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
  3.2406 @@ -12132,7 +12547,7 @@
  3.2407  SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
  3.2408  SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
  3.2409  SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
  3.2410 -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
  3.2411 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
  3.2412  SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
  3.2413  
  3.2414  SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
  3.2415 @@ -12143,6 +12558,9 @@
  3.2416  # define sqlite3ViewGetColumnNames(A,B) 0
  3.2417  #endif
  3.2418  
  3.2419 +#if SQLITE_MAX_ATTACHED>30
  3.2420 +SQLITE_PRIVATE   int sqlite3DbMaskAllZero(yDbMask);
  3.2421 +#endif
  3.2422  SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
  3.2423  SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
  3.2424  SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
  3.2425 @@ -12153,8 +12571,7 @@
  3.2426  # define sqlite3AutoincrementBegin(X)
  3.2427  # define sqlite3AutoincrementEnd(X)
  3.2428  #endif
  3.2429 -SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  3.2430 -SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  3.2431 +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
  3.2432  SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  3.2433  SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  3.2434  SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
  3.2435 @@ -12189,6 +12606,7 @@
  3.2436  SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
  3.2437  SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
  3.2438  SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
  3.2439 +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
  3.2440  SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
  3.2441  SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
  3.2442  SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
  3.2443 @@ -12197,15 +12615,16 @@
  3.2444  SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
  3.2445  SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
  3.2446  SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
  3.2447 -SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
  3.2448 +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
  3.2449  SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
  3.2450  SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
  3.2451  SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  3.2452 -SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
  3.2453 +SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
  3.2454 +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
  3.2455  SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
  3.2456  SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  3.2457  SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  3.2458 -SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  3.2459 +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  3.2460  SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
  3.2461  #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
  3.2462  #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  3.2463 @@ -12229,7 +12648,6 @@
  3.2464  SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
  3.2465  SQLITE_PRIVATE void sqlite3PrngSaveState(void);
  3.2466  SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
  3.2467 -SQLITE_PRIVATE void sqlite3PrngResetState(void);
  3.2468  SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
  3.2469  SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
  3.2470  SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  3.2471 @@ -12244,12 +12662,12 @@
  3.2472  SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
  3.2473  SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
  3.2474  SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
  3.2475 -SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  3.2476  SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  3.2477  SQLITE_PRIVATE int sqlite3IsRowid(const char*);
  3.2478  SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
  3.2479  SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  3.2480 -SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
  3.2481 +SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
  3.2482 +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
  3.2483  SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
  3.2484                                       u8,u8,int,int*);
  3.2485  SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
  3.2486 @@ -12293,7 +12711,7 @@
  3.2487  SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  3.2488  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
  3.2489  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
  3.2490 -                                        ExprList*,Select*,u8);
  3.2491 +                                        Select*,u8);
  3.2492  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
  3.2493  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
  3.2494  SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  3.2495 @@ -12388,18 +12806,18 @@
  3.2496  
  3.2497  
  3.2498  SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3.2499 -SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
  3.2500 +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
  3.2501  SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3.2502  SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3.2503  SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
  3.2504  SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
  3.2505 +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
  3.2506  SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
  3.2507  SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3.2508  SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
  3.2509  SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3.2510  
  3.2511 -#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) || \
  3.2512 -    defined(SQLITE_DEBUG_OS_TRACE)
  3.2513 +#if defined(SQLITE_TEST) 
  3.2514  SQLITE_PRIVATE const char *sqlite3ErrName(int);
  3.2515  #endif
  3.2516  
  3.2517 @@ -12408,7 +12826,7 @@
  3.2518  SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  3.2519  SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  3.2520  SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  3.2521 -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
  3.2522 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
  3.2523  SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  3.2524  SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
  3.2525  SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
  3.2526 @@ -12423,12 +12841,13 @@
  3.2527  #else
  3.2528  # define sqlite3FileSuffix3(X,Y)
  3.2529  #endif
  3.2530 -SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
  3.2531 +SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
  3.2532  
  3.2533  SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
  3.2534  SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
  3.2535  SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  3.2536                          void(*)(void*));
  3.2537 +SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
  3.2538  SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
  3.2539  SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
  3.2540  SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  3.2541 @@ -12494,6 +12913,7 @@
  3.2542  
  3.2543  SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
  3.2544  SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  3.2545 +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3.2546  SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
  3.2547  SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
  3.2548  SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
  3.2549 @@ -12506,7 +12926,9 @@
  3.2550  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  3.2551  SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
  3.2552  SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
  3.2553 +SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
  3.2554  SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
  3.2555 +SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  3.2556  #endif
  3.2557  
  3.2558  /*
  3.2559 @@ -12585,6 +13007,14 @@
  3.2560  SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  3.2561  SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  3.2562  #endif
  3.2563 +#ifndef SQLITE_OMIT_CTE
  3.2564 +SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
  3.2565 +SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
  3.2566 +SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
  3.2567 +#else
  3.2568 +#define sqlite3WithPush(x,y,z)
  3.2569 +#define sqlite3WithDelete(x,y)
  3.2570 +#endif
  3.2571  
  3.2572  /* Declarations for functions in fkey.c. All of these are replaced by
  3.2573  ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  3.2574 @@ -12635,11 +13065,21 @@
  3.2575    #define sqlite3EndBenignMalloc()
  3.2576  #endif
  3.2577  
  3.2578 -#define IN_INDEX_ROWID           1
  3.2579 -#define IN_INDEX_EPH             2
  3.2580 -#define IN_INDEX_INDEX_ASC       3
  3.2581 -#define IN_INDEX_INDEX_DESC      4
  3.2582 -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
  3.2583 +/*
  3.2584 +** Allowed return values from sqlite3FindInIndex()
  3.2585 +*/
  3.2586 +#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
  3.2587 +#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
  3.2588 +#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
  3.2589 +#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
  3.2590 +#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
  3.2591 +/*
  3.2592 +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
  3.2593 +*/
  3.2594 +#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
  3.2595 +#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
  3.2596 +#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
  3.2597 +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  3.2598  
  3.2599  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3.2600  SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  3.2601 @@ -12917,15 +13357,22 @@
  3.2602     0,                         /* isMutexInit */
  3.2603     0,                         /* isMallocInit */
  3.2604     0,                         /* isPCacheInit */
  3.2605 +   0,                         /* nRefInitMutex */
  3.2606     0,                         /* pInitMutex */
  3.2607 -   0,                         /* nRefInitMutex */
  3.2608     0,                         /* xLog */
  3.2609     0,                         /* pLogArg */
  3.2610 -   0,                         /* bLocaltimeFault */
  3.2611  #ifdef SQLITE_ENABLE_SQLLOG
  3.2612     0,                         /* xSqllog */
  3.2613 -   0                          /* pSqllogArg */
  3.2614 -#endif
  3.2615 +   0,                         /* pSqllogArg */
  3.2616 +#endif
  3.2617 +#ifdef SQLITE_VDBE_COVERAGE
  3.2618 +   0,                         /* xVdbeBranch */
  3.2619 +   0,                         /* pVbeBranchArg */
  3.2620 +#endif
  3.2621 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  3.2622 +   0,                         /* xTestCallback */
  3.2623 +#endif
  3.2624 +   0                          /* bLocaltimeFault */
  3.2625  };
  3.2626  
  3.2627  /*
  3.2628 @@ -13192,6 +13639,9 @@
  3.2629  #ifdef SQLITE_OMIT_COMPOUND_SELECT
  3.2630    "OMIT_COMPOUND_SELECT",
  3.2631  #endif
  3.2632 +#ifdef SQLITE_OMIT_CTE
  3.2633 +  "OMIT_CTE",
  3.2634 +#endif
  3.2635  #ifdef SQLITE_OMIT_DATETIME_FUNCS
  3.2636    "OMIT_DATETIME_FUNCS",
  3.2637  #endif
  3.2638 @@ -13476,14 +13926,18 @@
  3.2639    int pseudoTableReg;   /* Register holding pseudotable content. */
  3.2640    i16 nField;           /* Number of fields in the header */
  3.2641    u16 nHdrParsed;       /* Number of header fields parsed so far */
  3.2642 +#ifdef SQLITE_DEBUG
  3.2643 +  u8 seekOp;            /* Most recent seek operation on this cursor */
  3.2644 +#endif
  3.2645    i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
  3.2646    u8 nullRow;           /* True if pointing to a row with no data */
  3.2647    u8 rowidIsValid;      /* True if lastRowid is valid */
  3.2648    u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
  3.2649 +  Bool isEphemeral:1;   /* True for an ephemeral table */
  3.2650    Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
  3.2651    Bool isTable:1;       /* True if a table requiring integer keys */
  3.2652    Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
  3.2653 -  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
  3.2654 +  Pgno pgnoRoot;        /* Root page of the open btree cursor */
  3.2655    sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
  3.2656    i64 seqCount;         /* Sequence counter */
  3.2657    i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
  3.2658 @@ -13577,7 +14031,6 @@
  3.2659    } u;
  3.2660    int n;              /* Number of characters in string value, excluding '\0' */
  3.2661    u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
  3.2662 -  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
  3.2663    u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
  3.2664  #ifdef SQLITE_DEBUG
  3.2665    Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
  3.2666 @@ -13604,9 +14057,10 @@
  3.2667  #define MEM_Int       0x0004   /* Value is an integer */
  3.2668  #define MEM_Real      0x0008   /* Value is a real number */
  3.2669  #define MEM_Blob      0x0010   /* Value is a BLOB */
  3.2670 +#define MEM_AffMask   0x001f   /* Mask of affinity bits */
  3.2671  #define MEM_RowSet    0x0020   /* Value is a RowSet object */
  3.2672  #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
  3.2673 -#define MEM_Invalid   0x0080   /* Value is undefined */
  3.2674 +#define MEM_Undefined 0x0080   /* Value is undefined */
  3.2675  #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
  3.2676  #define MEM_TypeMask  0x01ff   /* Mask of type bits */
  3.2677  
  3.2678 @@ -13617,7 +14071,7 @@
  3.2679  ** string is \000 or \u0000 terminated
  3.2680  */
  3.2681  #define MEM_Term      0x0200   /* String rep is nul terminated */
  3.2682 -#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
  3.2683 +#define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
  3.2684  #define MEM_Static    0x0800   /* Mem.z points to a static string */
  3.2685  #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
  3.2686  #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
  3.2687 @@ -13638,7 +14092,7 @@
  3.2688  ** is for use inside assert() statements only.
  3.2689  */
  3.2690  #ifdef SQLITE_DEBUG
  3.2691 -#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
  3.2692 +#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
  3.2693  #endif
  3.2694  
  3.2695  /*
  3.2696 @@ -13720,12 +14174,9 @@
  3.2697    Mem **apArg;            /* Arguments to currently executing user function */
  3.2698    Mem *aColName;          /* Column names to return */
  3.2699    Mem *pResultSet;        /* Pointer to an array of results */
  3.2700 +  Parse *pParse;          /* Parsing context used to create this Vdbe */
  3.2701    int nMem;               /* Number of memory locations currently allocated */
  3.2702    int nOp;                /* Number of instructions in the program */
  3.2703 -  int nOpAlloc;           /* Number of slots allocated for aOp[] */
  3.2704 -  int nLabel;             /* Number of labels used */
  3.2705 -  int *aLabel;            /* Space to hold the labels */
  3.2706 -  u16 nResColumn;         /* Number of columns in one row of the result set */
  3.2707    int nCursor;            /* Number of slots in apCsr[] */
  3.2708    u32 magic;              /* Magic number for sanity checking */
  3.2709    char *zErrMsg;          /* Error message written here */
  3.2710 @@ -13738,6 +14189,7 @@
  3.2711    u32 cacheCtr;           /* VdbeCursor row cache generation counter */
  3.2712    int pc;                 /* The program counter */
  3.2713    int rc;                 /* Value to return */
  3.2714 +  u16 nResColumn;         /* Number of columns in one row of the result set */
  3.2715    u8 errorAction;         /* Recovery action to do in case of an error */
  3.2716    u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  3.2717    bft explain:2;          /* True if EXPLAIN present on SQL command */
  3.2718 @@ -13797,14 +14249,13 @@
  3.2719  #endif
  3.2720  SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
  3.2721  SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
  3.2722 -SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
  3.2723 +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
  3.2724  SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
  3.2725  SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
  3.2726  
  3.2727  int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
  3.2728  SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
  3.2729  SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
  3.2730 -SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
  3.2731  SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
  3.2732  SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
  3.2733  SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
  3.2734 @@ -13835,19 +14286,20 @@
  3.2735  SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
  3.2736  SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
  3.2737  SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
  3.2738 +#define VdbeMemDynamic(X)  \
  3.2739 +  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
  3.2740  #define VdbeMemRelease(X)  \
  3.2741 -  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
  3.2742 -    sqlite3VdbeMemReleaseExternal(X);
  3.2743 +  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
  3.2744  SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
  3.2745  SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
  3.2746  SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
  3.2747  SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
  3.2748  SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
  3.2749  SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
  3.2750 -SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
  3.2751  SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
  3.2752  
  3.2753  SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
  3.2754 +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
  3.2755  SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
  3.2756  SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
  3.2757  SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
  3.2758 @@ -13865,6 +14317,7 @@
  3.2759  
  3.2760  #ifdef SQLITE_DEBUG
  3.2761  SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
  3.2762 +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
  3.2763  #endif
  3.2764  
  3.2765  #ifndef SQLITE_OMIT_FOREIGN_KEY
  3.2766 @@ -15387,7 +15840,21 @@
  3.2767  ** routine has no return value since the return value would be meaningless.
  3.2768  */
  3.2769  SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
  3.2770 -  DO_OS_MALLOC_TEST(id);
  3.2771 +#ifdef SQLITE_TEST
  3.2772 +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
  3.2773 +    /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
  3.2774 +    ** is using a regular VFS, it is called after the corresponding 
  3.2775 +    ** transaction has been committed. Injecting a fault at this point 
  3.2776 +    ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
  3.2777 +    ** but the transaction is committed anyway.
  3.2778 +    **
  3.2779 +    ** The core must call OsFileControl() though, not OsFileControlHint(),
  3.2780 +    ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
  3.2781 +    ** means the commit really has failed and an error should be returned
  3.2782 +    ** to the user.  */
  3.2783 +    DO_OS_MALLOC_TEST(id);
  3.2784 +  }
  3.2785 +#endif
  3.2786    return id->pMethods->xFileControl(id, op, pArg);
  3.2787  }
  3.2788  SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
  3.2789 @@ -17572,7 +18039,7 @@
  3.2790    ** block.  If not, then split a block of the next larger power of
  3.2791    ** two in order to create a new free block of size iLogsize.
  3.2792    */
  3.2793 -  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
  3.2794 +  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
  3.2795    if( iBin>LOGMAX ){
  3.2796      testcase( sqlite3GlobalConfig.xLog!=0 );
  3.2797      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
  3.2798 @@ -17599,6 +18066,12 @@
  3.2799    if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
  3.2800    if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
  3.2801  
  3.2802 +#ifdef SQLITE_DEBUG
  3.2803 +  /* Make sure the allocated memory does not assume that it is set to zero
  3.2804 +  ** or retains a value from a previous allocation */
  3.2805 +  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
  3.2806 +#endif
  3.2807 +
  3.2808    /* Return a pointer to the allocated memory. */
  3.2809    return (void*)&mem5.zPool[i*mem5.szAtom];
  3.2810  }
  3.2811 @@ -17656,6 +18129,13 @@
  3.2812      }
  3.2813      size *= 2;
  3.2814    }
  3.2815 +
  3.2816 +#ifdef SQLITE_DEBUG
  3.2817 +  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
  3.2818 +  ** not used after being freed */
  3.2819 +  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
  3.2820 +#endif
  3.2821 +
  3.2822    memsys5Link(iBlock, iLogsize);
  3.2823  }
  3.2824  
  3.2825 @@ -17970,7 +18450,7 @@
  3.2826  */
  3.2827  SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
  3.2828  #ifndef SQLITE_OMIT_AUTOINIT
  3.2829 -  if( sqlite3_initialize() ) return 0;
  3.2830 +  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
  3.2831  #endif
  3.2832    return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
  3.2833  }
  3.2834 @@ -18151,7 +18631,7 @@
  3.2835  ** that means that a mutex could not be allocated. 
  3.2836  */
  3.2837  static sqlite3_mutex *debugMutexAlloc(int id){
  3.2838 -  static sqlite3_debug_mutex aStatic[6];
  3.2839 +  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
  3.2840    sqlite3_debug_mutex *pNew = 0;
  3.2841    switch( id ){
  3.2842      case SQLITE_MUTEX_FAST:
  3.2843 @@ -18348,10 +18828,13 @@
  3.2844  ** <li>  SQLITE_MUTEX_RECURSIVE
  3.2845  ** <li>  SQLITE_MUTEX_STATIC_MASTER
  3.2846  ** <li>  SQLITE_MUTEX_STATIC_MEM
  3.2847 -** <li>  SQLITE_MUTEX_STATIC_MEM2
  3.2848 +** <li>  SQLITE_MUTEX_STATIC_OPEN
  3.2849  ** <li>  SQLITE_MUTEX_STATIC_PRNG
  3.2850  ** <li>  SQLITE_MUTEX_STATIC_LRU
  3.2851  ** <li>  SQLITE_MUTEX_STATIC_PMEM
  3.2852 +** <li>  SQLITE_MUTEX_STATIC_APP1
  3.2853 +** <li>  SQLITE_MUTEX_STATIC_APP2
  3.2854 +** <li>  SQLITE_MUTEX_STATIC_APP3
  3.2855  ** </ul>
  3.2856  **
  3.2857  ** The first two constants cause sqlite3_mutex_alloc() to create
  3.2858 @@ -18385,6 +18868,9 @@
  3.2859      SQLITE3_MUTEX_INITIALIZER,
  3.2860      SQLITE3_MUTEX_INITIALIZER,
  3.2861      SQLITE3_MUTEX_INITIALIZER,
  3.2862 +    SQLITE3_MUTEX_INITIALIZER,
  3.2863 +    SQLITE3_MUTEX_INITIALIZER,
  3.2864 +    SQLITE3_MUTEX_INITIALIZER,
  3.2865      SQLITE3_MUTEX_INITIALIZER
  3.2866    };
  3.2867    sqlite3_mutex *p;
  3.2868 @@ -18615,12 +19101,303 @@
  3.2869  **    May you share freely, never taking more than you give.
  3.2870  **
  3.2871  *************************************************************************
  3.2872 -** This file contains the C functions that implement mutexes for win32
  3.2873 -*/
  3.2874 +** This file contains the C functions that implement mutexes for Win32.
  3.2875 +*/
  3.2876 +
  3.2877 +#if SQLITE_OS_WIN
  3.2878 +/*
  3.2879 +** Include code that is common to all os_*.c files
  3.2880 +*/
  3.2881 +/************** Include os_common.h in the middle of mutex_w32.c *************/
  3.2882 +/************** Begin file os_common.h ***************************************/
  3.2883 +/*
  3.2884 +** 2004 May 22
  3.2885 +**
  3.2886 +** The author disclaims copyright to this source code.  In place of
  3.2887 +** a legal notice, here is a blessing:
  3.2888 +**
  3.2889 +**    May you do good and not evil.
  3.2890 +**    May you find forgiveness for yourself and forgive others.
  3.2891 +**    May you share freely, never taking more than you give.
  3.2892 +**
  3.2893 +******************************************************************************
  3.2894 +**
  3.2895 +** This file contains macros and a little bit of code that is common to
  3.2896 +** all of the platform-specific files (os_*.c) and is #included into those
  3.2897 +** files.
  3.2898 +**
  3.2899 +** This file should be #included by the os_*.c files only.  It is not a
  3.2900 +** general purpose header file.
  3.2901 +*/
  3.2902 +#ifndef _OS_COMMON_H_
  3.2903 +#define _OS_COMMON_H_
  3.2904 +
  3.2905 +/*
  3.2906 +** At least two bugs have slipped in because we changed the MEMORY_DEBUG
  3.2907 +** macro to SQLITE_DEBUG and some older makefiles have not yet made the
  3.2908 +** switch.  The following code should catch this problem at compile-time.
  3.2909 +*/
  3.2910 +#ifdef MEMORY_DEBUG
  3.2911 +# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
  3.2912 +#endif
  3.2913 +
  3.2914 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  3.2915 +# ifndef SQLITE_DEBUG_OS_TRACE
  3.2916 +#   define SQLITE_DEBUG_OS_TRACE 0
  3.2917 +# endif
  3.2918 +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
  3.2919 +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
  3.2920 +#else
  3.2921 +# define OSTRACE(X)
  3.2922 +#endif
  3.2923 +
  3.2924 +/*
  3.2925 +** Macros for performance tracing.  Normally turned off.  Only works
  3.2926 +** on i486 hardware.
  3.2927 +*/
  3.2928 +#ifdef SQLITE_PERFORMANCE_TRACE
  3.2929 +
  3.2930 +/* 
  3.2931 +** hwtime.h contains inline assembler code for implementing 
  3.2932 +** high-performance timing routines.
  3.2933 +*/
  3.2934 +/************** Include hwtime.h in the middle of os_common.h ****************/
  3.2935 +/************** Begin file hwtime.h ******************************************/
  3.2936 +/*
  3.2937 +** 2008 May 27
  3.2938 +**
  3.2939 +** The author disclaims copyright to this source code.  In place of
  3.2940 +** a legal notice, here is a blessing:
  3.2941 +**
  3.2942 +**    May you do good and not evil.
  3.2943 +**    May you find forgiveness for yourself and forgive others.
  3.2944 +**    May you share freely, never taking more than you give.
  3.2945 +**
  3.2946 +******************************************************************************
  3.2947 +**
  3.2948 +** This file contains inline asm code for retrieving "high-performance"
  3.2949 +** counters for x86 class CPUs.
  3.2950 +*/
  3.2951 +#ifndef _HWTIME_H_
  3.2952 +#define _HWTIME_H_
  3.2953 +
  3.2954 +/*
  3.2955 +** The following routine only works on pentium-class (or newer) processors.
  3.2956 +** It uses the RDTSC opcode to read the cycle count value out of the
  3.2957 +** processor and returns that value.  This can be used for high-res
  3.2958 +** profiling.
  3.2959 +*/
  3.2960 +#if (defined(__GNUC__) || defined(_MSC_VER)) && \
  3.2961 +      (defined(i386) || defined(__i386__) || defined(_M_IX86))
  3.2962 +
  3.2963 +  #if defined(__GNUC__)
  3.2964 +
  3.2965 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  3.2966 +     unsigned int lo, hi;
  3.2967 +     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  3.2968 +     return (sqlite_uint64)hi << 32 | lo;
  3.2969 +  }
  3.2970 +
  3.2971 +  #elif defined(_MSC_VER)
  3.2972 +
  3.2973 +  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
  3.2974 +     __asm {
  3.2975 +        rdtsc
  3.2976 +        ret       ; return value at EDX:EAX
  3.2977 +     }
  3.2978 +  }
  3.2979 +
  3.2980 +  #endif
  3.2981 +
  3.2982 +#elif (defined(__GNUC__) && defined(__x86_64__))
  3.2983 +
  3.2984 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  3.2985 +      unsigned long val;
  3.2986 +      __asm__ __volatile__ ("rdtsc" : "=A" (val));
  3.2987 +      return val;
  3.2988 +  }
  3.2989 + 
  3.2990 +#elif (defined(__GNUC__) && defined(__ppc__))
  3.2991 +
  3.2992 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  3.2993 +      unsigned long long retval;
  3.2994 +      unsigned long junk;
  3.2995 +      __asm__ __volatile__ ("\n\
  3.2996 +          1:      mftbu   %1\n\
  3.2997 +                  mftb    %L0\n\
  3.2998 +                  mftbu   %0\n\
  3.2999 +                  cmpw    %0,%1\n\
  3.3000 +                  bne     1b"
  3.3001 +                  : "=r" (retval), "=r" (junk));
  3.3002 +      return retval;
  3.3003 +  }
  3.3004 +
  3.3005 +#else
  3.3006 +
  3.3007 +  #error Need implementation of sqlite3Hwtime() for your platform.
  3.3008 +
  3.3009 +  /*
  3.3010 +  ** To compile without implementing sqlite3Hwtime() for your platform,
  3.3011 +  ** you can remove the above #error and use the following
  3.3012 +  ** stub function.  You will lose timing support for many
  3.3013 +  ** of the debugging and testing utilities, but it should at
  3.3014 +  ** least compile and run.
  3.3015 +  */
  3.3016 +SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
  3.3017 +
  3.3018 +#endif
  3.3019 +
  3.3020 +#endif /* !defined(_HWTIME_H_) */
  3.3021 +
  3.3022 +/************** End of hwtime.h **********************************************/
  3.3023 +/************** Continuing where we left off in os_common.h ******************/
  3.3024 +
  3.3025 +static sqlite_uint64 g_start;
  3.3026 +static sqlite_uint64 g_elapsed;
  3.3027 +#define TIMER_START       g_start=sqlite3Hwtime()
  3.3028 +#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
  3.3029 +#define TIMER_ELAPSED     g_elapsed
  3.3030 +#else
  3.3031 +#define TIMER_START
  3.3032 +#define TIMER_END
  3.3033 +#define TIMER_ELAPSED     ((sqlite_uint64)0)
  3.3034 +#endif
  3.3035 +
  3.3036 +/*
  3.3037 +** If we compile with the SQLITE_TEST macro set, then the following block
  3.3038 +** of code will give us the ability to simulate a disk I/O error.  This
  3.3039 +** is used for testing the I/O recovery logic.
  3.3040 +*/
  3.3041 +#ifdef SQLITE_TEST
  3.3042 +SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
  3.3043 +SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
  3.3044 +SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
  3.3045 +SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
  3.3046 +SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
  3.3047 +SQLITE_API int sqlite3_diskfull_pending = 0;
  3.3048 +SQLITE_API int sqlite3_diskfull = 0;
  3.3049 +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
  3.3050 +#define SimulateIOError(CODE)  \
  3.3051 +  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
  3.3052 +       || sqlite3_io_error_pending-- == 1 )  \
  3.3053 +              { local_ioerr(); CODE; }
  3.3054 +static void local_ioerr(){
  3.3055 +  IOTRACE(("IOERR\n"));
  3.3056 +  sqlite3_io_error_hit++;
  3.3057 +  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
  3.3058 +}
  3.3059 +#define SimulateDiskfullError(CODE) \
  3.3060 +   if( sqlite3_diskfull_pending ){ \
  3.3061 +     if( sqlite3_diskfull_pending == 1 ){ \
  3.3062 +       local_ioerr(); \
  3.3063 +       sqlite3_diskfull = 1; \
  3.3064 +       sqlite3_io_error_hit = 1; \
  3.3065 +       CODE; \
  3.3066 +     }else{ \
  3.3067 +       sqlite3_diskfull_pending--; \
  3.3068 +     } \
  3.3069 +   }
  3.3070 +#else
  3.3071 +#define SimulateIOErrorBenign(X)
  3.3072 +#define SimulateIOError(A)
  3.3073 +#define SimulateDiskfullError(A)
  3.3074 +#endif
  3.3075 +
  3.3076 +/*
  3.3077 +** When testing, keep a count of the number of open files.
  3.3078 +*/
  3.3079 +#ifdef SQLITE_TEST
  3.3080 +SQLITE_API int sqlite3_open_file_count = 0;
  3.3081 +#define OpenCounter(X)  sqlite3_open_file_count+=(X)
  3.3082 +#else
  3.3083 +#define OpenCounter(X)
  3.3084 +#endif
  3.3085 +
  3.3086 +#endif /* !defined(_OS_COMMON_H_) */
  3.3087 +
  3.3088 +/************** End of os_common.h *******************************************/
  3.3089 +/************** Continuing where we left off in mutex_w32.c ******************/
  3.3090 +
  3.3091 +/*
  3.3092 +** Include the header file for the Windows VFS.
  3.3093 +*/
  3.3094 +/************** Include os_win.h in the middle of mutex_w32.c ****************/
  3.3095 +/************** Begin file os_win.h ******************************************/
  3.3096 +/*
  3.3097 +** 2013 November 25
  3.3098 +**
  3.3099 +** The author disclaims copyright to this source code.  In place of
  3.3100 +** a legal notice, here is a blessing:
  3.3101 +**
  3.3102 +**    May you do good and not evil.
  3.3103 +**    May you find forgiveness for yourself and forgive others.
  3.3104 +**    May you share freely, never taking more than you give.
  3.3105 +**
  3.3106 +******************************************************************************
  3.3107 +**
  3.3108 +** This file contains code that is specific to Windows.
  3.3109 +*/
  3.3110 +#ifndef _OS_WIN_H_
  3.3111 +#define _OS_WIN_H_
  3.3112 +
  3.3113 +/*
  3.3114 +** Include the primary Windows SDK header file.
  3.3115 +*/
  3.3116 +#include "windows.h"
  3.3117 +
  3.3118 +#ifdef __CYGWIN__
  3.3119 +# include <sys/cygwin.h>
  3.3120 +# include <errno.h> /* amalgamator: dontcache */
  3.3121 +#endif
  3.3122 +
  3.3123 +/*
  3.3124 +** Determine if we are dealing with Windows NT.
  3.3125 +**
  3.3126 +** We ought to be able to determine if we are compiling for Windows 9x or
  3.3127 +** Windows NT using the _WIN32_WINNT macro as follows:
  3.3128 +**
  3.3129 +** #if defined(_WIN32_WINNT)
  3.3130 +** # define SQLITE_OS_WINNT 1
  3.3131 +** #else
  3.3132 +** # define SQLITE_OS_WINNT 0
  3.3133 +** #endif
  3.3134 +**
  3.3135 +** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
  3.3136 +** it ought to, so the above test does not work.  We'll just assume that
  3.3137 +** everything is Windows NT unless the programmer explicitly says otherwise
  3.3138 +** by setting SQLITE_OS_WINNT to 0.
  3.3139 +*/
  3.3140 +#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  3.3141 +# define SQLITE_OS_WINNT 1
  3.3142 +#endif
  3.3143 +
  3.3144 +/*
  3.3145 +** Determine if we are dealing with Windows CE - which has a much reduced
  3.3146 +** API.
  3.3147 +*/
  3.3148 +#if defined(_WIN32_WCE)
  3.3149 +# define SQLITE_OS_WINCE 1
  3.3150 +#else
  3.3151 +# define SQLITE_OS_WINCE 0
  3.3152 +#endif
  3.3153 +
  3.3154 +/*
  3.3155 +** Determine if we are dealing with WinRT, which provides only a subset of
  3.3156 +** the full Win32 API.
  3.3157 +*/
  3.3158 +#if !defined(SQLITE_OS_WINRT)
  3.3159 +# define SQLITE_OS_WINRT 0
  3.3160 +#endif
  3.3161 +
  3.3162 +#endif /* _OS_WIN_H_ */
  3.3163 +
  3.3164 +/************** End of os_win.h **********************************************/
  3.3165 +/************** Continuing where we left off in mutex_w32.c ******************/
  3.3166 +#endif
  3.3167  
  3.3168  /*
  3.3169  ** The code in this file is only used if we are compiling multithreaded
  3.3170 -** on a win32 system.
  3.3171 +** on a Win32 system.
  3.3172  */
  3.3173  #ifdef SQLITE_MUTEX_W32
  3.3174  
  3.3175 @@ -18633,50 +19410,24 @@
  3.3176  #ifdef SQLITE_DEBUG
  3.3177    volatile int nRef;         /* Number of enterances */
  3.3178    volatile DWORD owner;      /* Thread holding this mutex */
  3.3179 -  int trace;                 /* True to trace changes */
  3.3180 -#endif
  3.3181 -};
  3.3182 +  volatile int trace;        /* True to trace changes */
  3.3183 +#endif
  3.3184 +};
  3.3185 +
  3.3186 +/*
  3.3187 +** These are the initializer values used when declaring a "static" mutex
  3.3188 +** on Win32.  It should be noted that all mutexes require initialization
  3.3189 +** on the Win32 platform.
  3.3190 +*/
  3.3191  #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
  3.3192 +
  3.3193  #ifdef SQLITE_DEBUG
  3.3194 -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
  3.3195 +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
  3.3196 +                                    0L, (DWORD)0, 0 }
  3.3197  #else
  3.3198  #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
  3.3199  #endif
  3.3200  
  3.3201 -/*
  3.3202 -** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
  3.3203 -** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
  3.3204 -**
  3.3205 -** Here is an interesting observation:  Win95, Win98, and WinME lack
  3.3206 -** the LockFileEx() API.  But we can still statically link against that
  3.3207 -** API as long as we don't call it win running Win95/98/ME.  A call to
  3.3208 -** this routine is used to determine if the host is Win95/98/ME or
  3.3209 -** WinNT/2K/XP so that we will know whether or not we can safely call
  3.3210 -** the LockFileEx() API.
  3.3211 -**
  3.3212 -** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
  3.3213 -** which is only available if your application was compiled with 
  3.3214 -** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
  3.3215 -** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
  3.3216 -** this out as well.
  3.3217 -*/
  3.3218 -#if 0
  3.3219 -#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
  3.3220 -# define mutexIsNT()  (1)
  3.3221 -#else
  3.3222 -  static int mutexIsNT(void){
  3.3223 -    static int osType = 0;
  3.3224 -    if( osType==0 ){
  3.3225 -      OSVERSIONINFO sInfo;
  3.3226 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  3.3227 -      GetVersionEx(&sInfo);
  3.3228 -      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  3.3229 -    }
  3.3230 -    return osType==2;
  3.3231 -  }
  3.3232 -#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
  3.3233 -#endif
  3.3234 -
  3.3235  #ifdef SQLITE_DEBUG
  3.3236  /*
  3.3237  ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  3.3238 @@ -18685,20 +19436,24 @@
  3.3239  static int winMutexHeld(sqlite3_mutex *p){
  3.3240    return p->nRef!=0 && p->owner==GetCurrentThreadId();
  3.3241  }
  3.3242 +
  3.3243  static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
  3.3244    return p->nRef==0 || p->owner!=tid;
  3.3245  }
  3.3246 +
  3.3247  static int winMutexNotheld(sqlite3_mutex *p){
  3.3248 -  DWORD tid = GetCurrentThreadId(); 
  3.3249 +  DWORD tid = GetCurrentThreadId();
  3.3250    return winMutexNotheld2(p, tid);
  3.3251  }
  3.3252  #endif
  3.3253  
  3.3254 -
  3.3255  /*
  3.3256  ** Initialize and deinitialize the mutex subsystem.
  3.3257  */
  3.3258 -static sqlite3_mutex winMutex_staticMutexes[6] = {
  3.3259 +static sqlite3_mutex winMutex_staticMutexes[] = {
  3.3260 +  SQLITE3_MUTEX_INITIALIZER,
  3.3261 +  SQLITE3_MUTEX_INITIALIZER,
  3.3262 +  SQLITE3_MUTEX_INITIALIZER,
  3.3263    SQLITE3_MUTEX_INITIALIZER,
  3.3264    SQLITE3_MUTEX_INITIALIZER,
  3.3265    SQLITE3_MUTEX_INITIALIZER,
  3.3266 @@ -18706,17 +19461,20 @@
  3.3267    SQLITE3_MUTEX_INITIALIZER,
  3.3268    SQLITE3_MUTEX_INITIALIZER
  3.3269  };
  3.3270 +
  3.3271  static int winMutex_isInit = 0;
  3.3272 -/* As winMutexInit() and winMutexEnd() are called as part
  3.3273 -** of the sqlite3_initialize and sqlite3_shutdown()
  3.3274 -** processing, the "interlocked" magic is probably not
  3.3275 -** strictly necessary.
  3.3276 -*/
  3.3277 -static LONG winMutex_lock = 0;
  3.3278 -
  3.3279 +static int winMutex_isNt = -1; /* <0 means "need to query" */
  3.3280 +
  3.3281 +/* As the winMutexInit() and winMutexEnd() functions are called as part
  3.3282 +** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
  3.3283 +** "interlocked" magic used here is probably not strictly necessary.
  3.3284 +*/
  3.3285 +static LONG volatile winMutex_lock = 0;
  3.3286 +
  3.3287 +SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
  3.3288  SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
  3.3289  
  3.3290 -static int winMutexInit(void){ 
  3.3291 +static int winMutexInit(void){
  3.3292    /* The first to increment to 1 does actual initialization */
  3.3293    if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
  3.3294      int i;
  3.3295 @@ -18729,16 +19487,17 @@
  3.3296      }
  3.3297      winMutex_isInit = 1;
  3.3298    }else{
  3.3299 -    /* Someone else is in the process of initing the static mutexes */
  3.3300 +    /* Another thread is (in the process of) initializing the static
  3.3301 +    ** mutexes */
  3.3302      while( !winMutex_isInit ){
  3.3303        sqlite3_win32_sleep(1);
  3.3304      }
  3.3305    }
  3.3306 -  return SQLITE_OK; 
  3.3307 -}
  3.3308 -
  3.3309 -static int winMutexEnd(void){ 
  3.3310 -  /* The first to decrement to 0 does actual shutdown 
  3.3311 +  return SQLITE_OK;
  3.3312 +}
  3.3313 +
  3.3314 +static int winMutexEnd(void){
  3.3315 +  /* The first to decrement to 0 does actual shutdown
  3.3316    ** (which should be the last to shutdown.) */
  3.3317    if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
  3.3318      if( winMutex_isInit==1 ){
  3.3319 @@ -18749,7 +19508,7 @@
  3.3320        winMutex_isInit = 0;
  3.3321      }
  3.3322    }
  3.3323 -  return SQLITE_OK; 
  3.3324 +  return SQLITE_OK;
  3.3325  }
  3.3326  
  3.3327  /*
  3.3328 @@ -18764,10 +19523,13 @@
  3.3329  ** <li>  SQLITE_MUTEX_RECURSIVE
  3.3330  ** <li>  SQLITE_MUTEX_STATIC_MASTER
  3.3331  ** <li>  SQLITE_MUTEX_STATIC_MEM
  3.3332 -** <li>  SQLITE_MUTEX_STATIC_MEM2
  3.3333 +** <li>  SQLITE_MUTEX_STATIC_OPEN
  3.3334  ** <li>  SQLITE_MUTEX_STATIC_PRNG
  3.3335  ** <li>  SQLITE_MUTEX_STATIC_LRU
  3.3336  ** <li>  SQLITE_MUTEX_STATIC_PMEM
  3.3337 +** <li>  SQLITE_MUTEX_STATIC_APP1
  3.3338 +** <li>  SQLITE_MUTEX_STATIC_APP2
  3.3339 +** <li>  SQLITE_MUTEX_STATIC_APP3
  3.3340  ** </ul>
  3.3341  **
  3.3342  ** The first two constants cause sqlite3_mutex_alloc() to create
  3.3343 @@ -18790,7 +19552,7 @@
  3.3344  **
  3.3345  ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  3.3346  ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  3.3347 -** returns a different mutex on every call.  But for the static 
  3.3348 +** returns a different mutex on every call.  But for the static
  3.3349  ** mutex types, the same mutex is returned on every call that has
  3.3350  ** the same type number.
  3.3351  */
  3.3352 @@ -18801,9 +19563,12 @@
  3.3353      case SQLITE_MUTEX_FAST:
  3.3354      case SQLITE_MUTEX_RECURSIVE: {
  3.3355        p = sqlite3MallocZero( sizeof(*p) );
  3.3356 -      if( p ){  
  3.3357 +      if( p ){
  3.3358  #ifdef SQLITE_DEBUG
  3.3359          p->id = iType;
  3.3360 +#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
  3.3361 +        p->trace = 1;
  3.3362 +#endif
  3.3363  #endif
  3.3364  #if SQLITE_OS_WINRT
  3.3365          InitializeCriticalSectionEx(&p->mutex, 0, 0);
  3.3366 @@ -18814,12 +19579,15 @@
  3.3367        break;
  3.3368      }
  3.3369      default: {
  3.3370 -      assert( winMutex_isInit==1 );
  3.3371        assert( iType-2 >= 0 );
  3.3372        assert( iType-2 < ArraySize(winMutex_staticMutexes) );
  3.3373 +      assert( winMutex_isInit==1 );
  3.3374        p = &winMutex_staticMutexes[iType-2];
  3.3375  #ifdef SQLITE_DEBUG
  3.3376        p->id = iType;
  3.3377 +#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
  3.3378 +      p->trace = 1;
  3.3379 +#endif
  3.3380  #endif
  3.3381        break;
  3.3382      }
  3.3383 @@ -18835,8 +19603,11 @@
  3.3384  */
  3.3385  static void winMutexFree(sqlite3_mutex *p){
  3.3386    assert( p );
  3.3387 +#ifdef SQLITE_DEBUG
  3.3388    assert( p->nRef==0 && p->owner==0 );
  3.3389    assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  3.3390 +#endif
  3.3391 +  assert( winMutex_isInit==1 );
  3.3392    DeleteCriticalSection(&p->mutex);
  3.3393    sqlite3_free(p);
  3.3394  }
  3.3395 @@ -18853,30 +19624,39 @@
  3.3396  ** more than once, the behavior is undefined.
  3.3397  */
  3.3398  static void winMutexEnter(sqlite3_mutex *p){
  3.3399 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  3.3400 +  DWORD tid = GetCurrentThreadId();
  3.3401 +#endif
  3.3402  #ifdef SQLITE_DEBUG
  3.3403 -  DWORD tid = GetCurrentThreadId(); 
  3.3404 +  assert( p );
  3.3405    assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  3.3406 -#endif
  3.3407 +#else
  3.3408 +  assert( p );
  3.3409 +#endif
  3.3410 +  assert( winMutex_isInit==1 );
  3.3411    EnterCriticalSection(&p->mutex);
  3.3412  #ifdef SQLITE_DEBUG
  3.3413    assert( p->nRef>0 || p->owner==0 );
  3.3414 -  p->owner = tid; 
  3.3415 +  p->owner = tid;
  3.3416    p->nRef++;
  3.3417    if( p->trace ){
  3.3418 -    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  3.3419 -  }
  3.3420 -#endif
  3.3421 -}
  3.3422 +    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
  3.3423 +             tid, p, p->trace, p->nRef));
  3.3424 +  }
  3.3425 +#endif
  3.3426 +}
  3.3427 +
  3.3428  static int winMutexTry(sqlite3_mutex *p){
  3.3429 -#ifndef NDEBUG
  3.3430 -  DWORD tid = GetCurrentThreadId(); 
  3.3431 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  3.3432 +  DWORD tid = GetCurrentThreadId();
  3.3433  #endif
  3.3434    int rc = SQLITE_BUSY;
  3.3435 +  assert( p );
  3.3436    assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  3.3437    /*
  3.3438    ** The sqlite3_mutex_try() routine is very rarely used, and when it
  3.3439    ** is used it is merely an optimization.  So it is OK for it to always
  3.3440 -  ** fail.  
  3.3441 +  ** fail.
  3.3442    **
  3.3443    ** The TryEnterCriticalSection() interface is only available on WinNT.
  3.3444    ** And some windows compilers complain if you try to use it without
  3.3445 @@ -18884,18 +19664,27 @@
  3.3446    ** For that reason, we will omit this optimization for now.  See
  3.3447    ** ticket #2685.
  3.3448    */
  3.3449 -#if 0
  3.3450 -  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
  3.3451 +#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
  3.3452 +  assert( winMutex_isInit==1 );
  3.3453 +  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
  3.3454 +  if( winMutex_isNt<0 ){
  3.3455 +    winMutex_isNt = sqlite3_win32_is_nt();
  3.3456 +  }
  3.3457 +  assert( winMutex_isNt==0 || winMutex_isNt==1 );
  3.3458 +  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
  3.3459 +#ifdef SQLITE_DEBUG
  3.3460      p->owner = tid;
  3.3461      p->nRef++;
  3.3462 +#endif
  3.3463      rc = SQLITE_OK;
  3.3464    }
  3.3465  #else
  3.3466    UNUSED_PARAMETER(p);
  3.3467  #endif
  3.3468  #ifdef SQLITE_DEBUG
  3.3469 -  if( rc==SQLITE_OK && p->trace ){
  3.3470 -    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  3.3471 +  if( p->trace ){
  3.3472 +    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
  3.3473 +             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
  3.3474    }
  3.3475  #endif
  3.3476    return rc;
  3.3477 @@ -18908,18 +19697,23 @@
  3.3478  ** is not currently allocated.  SQLite will never do either.
  3.3479  */
  3.3480  static void winMutexLeave(sqlite3_mutex *p){
  3.3481 -#ifndef NDEBUG
  3.3482 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  3.3483    DWORD tid = GetCurrentThreadId();
  3.3484 +#endif
  3.3485 +  assert( p );
  3.3486 +#ifdef SQLITE_DEBUG
  3.3487    assert( p->nRef>0 );
  3.3488    assert( p->owner==tid );
  3.3489    p->nRef--;
  3.3490    if( p->nRef==0 ) p->owner = 0;
  3.3491    assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  3.3492  #endif
  3.3493 +  assert( winMutex_isInit==1 );
  3.3494    LeaveCriticalSection(&p->mutex);
  3.3495  #ifdef SQLITE_DEBUG
  3.3496    if( p->trace ){
  3.3497 -    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  3.3498 +    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
  3.3499 +             tid, p, p->trace, p->nRef));
  3.3500    }
  3.3501  #endif
  3.3502  }
  3.3503 @@ -18941,9 +19735,9 @@
  3.3504      0
  3.3505  #endif
  3.3506    };
  3.3507 -
  3.3508    return &sMutex;
  3.3509  }
  3.3510 +
  3.3511  #endif /* SQLITE_MUTEX_W32 */
  3.3512  
  3.3513  /************** End of mutex_w32.c *******************************************/
  3.3514 @@ -19382,7 +20176,7 @@
  3.3515  */
  3.3516  #ifndef SQLITE_OMIT_LOOKASIDE
  3.3517  static int isLookaside(sqlite3 *db, void *p){
  3.3518 -  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
  3.3519 +  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
  3.3520  }
  3.3521  #else
  3.3522  #define isLookaside(A,B) 0
  3.3523 @@ -19398,8 +20192,9 @@
  3.3524    return sqlite3GlobalConfig.m.xSize(p);
  3.3525  }
  3.3526  SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
  3.3527 -  assert( db==0 || sqlite3_mutex_held(db->mutex) );
  3.3528 -  if( db && isLookaside(db, p) ){
  3.3529 +  assert( db!=0 );
  3.3530 +  assert( sqlite3_mutex_held(db->mutex) );
  3.3531 +  if( isLookaside(db, p) ){
  3.3532      return db->lookaside.sz;
  3.3533    }else{
  3.3534      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
  3.3535 @@ -19869,18 +20664,29 @@
  3.3536  #endif /* SQLITE_OMIT_FLOATING_POINT */
  3.3537  
  3.3538  /*
  3.3539 -** Append N space characters to the given string buffer.
  3.3540 -*/
  3.3541 -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
  3.3542 -  static const char zSpaces[] = "                             ";
  3.3543 -  while( N>=(int)sizeof(zSpaces)-1 ){
  3.3544 -    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
  3.3545 -    N -= sizeof(zSpaces)-1;
  3.3546 -  }
  3.3547 -  if( N>0 ){
  3.3548 -    sqlite3StrAccumAppend(pAccum, zSpaces, N);
  3.3549 -  }
  3.3550 -}
  3.3551 +** Set the StrAccum object to an error mode.
  3.3552 +*/
  3.3553 +static void setStrAccumError(StrAccum *p, u8 eError){
  3.3554 +  p->accError = eError;
  3.3555 +  p->nAlloc = 0;
  3.3556 +}
  3.3557 +
  3.3558 +/*
  3.3559 +** Extra argument values from a PrintfArguments object
  3.3560 +*/
  3.3561 +static sqlite3_int64 getIntArg(PrintfArguments *p){
  3.3562 +  if( p->nArg<=p->nUsed ) return 0;
  3.3563 +  return sqlite3_value_int64(p->apArg[p->nUsed++]);
  3.3564 +}
  3.3565 +static double getDoubleArg(PrintfArguments *p){
  3.3566 +  if( p->nArg<=p->nUsed ) return 0.0;
  3.3567 +  return sqlite3_value_double(p->apArg[p->nUsed++]);
  3.3568 +}
  3.3569 +static char *getTextArg(PrintfArguments *p){
  3.3570 +  if( p->nArg<=p->nUsed ) return 0;
  3.3571 +  return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
  3.3572 +}
  3.3573 +
  3.3574  
  3.3575  /*
  3.3576  ** On machines with a small stack size, you can redefine the
  3.3577 @@ -19895,10 +20701,10 @@
  3.3578  ** Render a string given by "fmt" into the StrAccum object.
  3.3579  */
  3.3580  SQLITE_PRIVATE void sqlite3VXPrintf(
  3.3581 -  StrAccum *pAccum,                  /* Accumulate results here */
  3.3582 -  int useExtended,                   /* Allow extended %-conversions */
  3.3583 -  const char *fmt,                   /* Format string */
  3.3584 -  va_list ap                         /* arguments */
  3.3585 +  StrAccum *pAccum,          /* Accumulate results here */
  3.3586 +  u32 bFlags,                /* SQLITE_PRINTF_* flags */
  3.3587 +  const char *fmt,           /* Format string */
  3.3588 +  va_list ap                 /* arguments */
  3.3589  ){
  3.3590    int c;                     /* Next character in the format string */
  3.3591    char *bufpt;               /* Pointer to the conversion buffer */
  3.3592 @@ -19916,6 +20722,8 @@
  3.3593    etByte flag_longlong;      /* True if the "ll" flag is present */
  3.3594    etByte done;               /* Loop termination flag */
  3.3595    etByte xtype = 0;          /* Conversion paradigm */
  3.3596 +  u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
  3.3597 +  u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
  3.3598    char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
  3.3599    sqlite_uint64 longvalue;   /* Value for integer types */
  3.3600    LONGDOUBLE_TYPE realvalue; /* Value for real types */
  3.3601 @@ -19930,16 +20738,23 @@
  3.3602    etByte flag_dp;            /* True if decimal point should be shown */
  3.3603    etByte flag_rtz;           /* True if trailing zeros should be removed */
  3.3604  #endif
  3.3605 +  PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
  3.3606    char buf[etBUFSIZE];       /* Conversion buffer */
  3.3607  
  3.3608    bufpt = 0;
  3.3609 +  if( bFlags ){
  3.3610 +    if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
  3.3611 +      pArgList = va_arg(ap, PrintfArguments*);
  3.3612 +    }
  3.3613 +    useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
  3.3614 +  }else{
  3.3615 +    bArgList = useIntern = 0;
  3.3616 +  }
  3.3617    for(; (c=(*fmt))!=0; ++fmt){
  3.3618      if( c!='%' ){
  3.3619 -      int amt;
  3.3620        bufpt = (char *)fmt;
  3.3621 -      amt = 1;
  3.3622 -      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
  3.3623 -      sqlite3StrAccumAppend(pAccum, bufpt, amt);
  3.3624 +      while( (c=(*++fmt))!='%' && c!=0 ){};
  3.3625 +      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
  3.3626        if( c==0 ) break;
  3.3627      }
  3.3628      if( (c=(*++fmt))==0 ){
  3.3629 @@ -19964,7 +20779,11 @@
  3.3630      /* Get the field width */
  3.3631      width = 0;
  3.3632      if( c=='*' ){
  3.3633 -      width = va_arg(ap,int);
  3.3634 +      if( bArgList ){
  3.3635 +        width = (int)getIntArg(pArgList);
  3.3636 +      }else{
  3.3637 +        width = va_arg(ap,int);
  3.3638 +      }
  3.3639        if( width<0 ){
  3.3640          flag_leftjustify = 1;
  3.3641          width = -width;
  3.3642 @@ -19981,7 +20800,11 @@
  3.3643        precision = 0;
  3.3644        c = *++fmt;
  3.3645        if( c=='*' ){
  3.3646 -        precision = va_arg(ap,int);
  3.3647 +        if( bArgList ){
  3.3648 +          precision = (int)getIntArg(pArgList);
  3.3649 +        }else{
  3.3650 +          precision = va_arg(ap,int);
  3.3651 +        }
  3.3652          if( precision<0 ) precision = -precision;
  3.3653          c = *++fmt;
  3.3654        }else{
  3.3655 @@ -20012,7 +20835,7 @@
  3.3656      for(idx=0; idx<ArraySize(fmtinfo); idx++){
  3.3657        if( c==fmtinfo[idx].fmttype ){
  3.3658          infop = &fmtinfo[idx];
  3.3659 -        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
  3.3660 +        if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
  3.3661            xtype = infop->type;
  3.3662          }else{
  3.3663            return;
  3.3664 @@ -20052,7 +20875,9 @@
  3.3665        case etRADIX:
  3.3666          if( infop->flags & FLAG_SIGNED ){
  3.3667            i64 v;
  3.3668 -          if( flag_longlong ){
  3.3669 +          if( bArgList ){
  3.3670 +            v = getIntArg(pArgList);
  3.3671 +          }else if( flag_longlong ){
  3.3672              v = va_arg(ap,i64);
  3.3673            }else if( flag_long ){
  3.3674              v = va_arg(ap,long int);
  3.3675 @@ -20073,7 +20898,9 @@
  3.3676              else                       prefix = 0;
  3.3677            }
  3.3678          }else{
  3.3679 -          if( flag_longlong ){
  3.3680 +          if( bArgList ){
  3.3681 +            longvalue = (u64)getIntArg(pArgList);
  3.3682 +          }else if( flag_longlong ){
  3.3683              longvalue = va_arg(ap,u64);
  3.3684            }else if( flag_long ){
  3.3685              longvalue = va_arg(ap,unsigned long int);
  3.3686 @@ -20093,7 +20920,7 @@
  3.3687            nOut = precision + 10;
  3.3688            zOut = zExtra = sqlite3Malloc( nOut );
  3.3689            if( zOut==0 ){
  3.3690 -            pAccum->accError = STRACCUM_NOMEM;
  3.3691 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  3.3692              return;
  3.3693            }
  3.3694          }
  3.3695 @@ -20108,10 +20935,8 @@
  3.3696            *(--bufpt) = zOrd[x*2];
  3.3697          }
  3.3698          {
  3.3699 -          register const char *cset;      /* Use registers for speed */
  3.3700 -          register int base;
  3.3701 -          cset = &aDigits[infop->charset];
  3.3702 -          base = infop->base;
  3.3703 +          const char *cset = &aDigits[infop->charset];
  3.3704 +          u8 base = infop->base;
  3.3705            do{                                           /* Convert to ascii */
  3.3706              *(--bufpt) = cset[longvalue%base];
  3.3707              longvalue = longvalue/base;
  3.3708 @@ -20133,7 +20958,11 @@
  3.3709        case etFLOAT:
  3.3710        case etEXP:
  3.3711        case etGENERIC:
  3.3712 -        realvalue = va_arg(ap,double);
  3.3713 +        if( bArgList ){
  3.3714 +          realvalue = getDoubleArg(pArgList);
  3.3715 +        }else{
  3.3716 +          realvalue = va_arg(ap,double);
  3.3717 +        }
  3.3718  #ifdef SQLITE_OMIT_FLOATING_POINT
  3.3719          length = 0;
  3.3720  #else
  3.3721 @@ -20205,7 +21034,7 @@
  3.3722          if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
  3.3723            bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
  3.3724            if( bufpt==0 ){
  3.3725 -            pAccum->accError = STRACCUM_NOMEM;
  3.3726 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  3.3727              return;
  3.3728            }
  3.3729          }
  3.3730 @@ -20288,7 +21117,9 @@
  3.3731  #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
  3.3732          break;
  3.3733        case etSIZE:
  3.3734 -        *(va_arg(ap,int*)) = pAccum->nChar;
  3.3735 +        if( !bArgList ){
  3.3736 +          *(va_arg(ap,int*)) = pAccum->nChar;
  3.3737 +        }
  3.3738          length = width = 0;
  3.3739          break;
  3.3740        case etPERCENT:
  3.3741 @@ -20297,7 +21128,12 @@
  3.3742          length = 1;
  3.3743          break;
  3.3744        case etCHARX:
  3.3745 -        c = va_arg(ap,int);
  3.3746 +        if( bArgList ){
  3.3747 +          bufpt = getTextArg(pArgList);
  3.3748 +          c = bufpt ? bufpt[0] : 0;
  3.3749 +        }else{
  3.3750 +          c = va_arg(ap,int);
  3.3751 +        }
  3.3752          buf[0] = (char)c;
  3.3753          if( precision>=0 ){
  3.3754            for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
  3.3755 @@ -20309,10 +21145,14 @@
  3.3756          break;
  3.3757        case etSTRING:
  3.3758        case etDYNSTRING:
  3.3759 -        bufpt = va_arg(ap,char*);
  3.3760 +        if( bArgList ){
  3.3761 +          bufpt = getTextArg(pArgList);
  3.3762 +        }else{
  3.3763 +          bufpt = va_arg(ap,char*);
  3.3764 +        }
  3.3765          if( bufpt==0 ){
  3.3766            bufpt = "";
  3.3767 -        }else if( xtype==etDYNSTRING ){
  3.3768 +        }else if( xtype==etDYNSTRING && !bArgList ){
  3.3769            zExtra = bufpt;
  3.3770          }
  3.3771          if( precision>=0 ){
  3.3772 @@ -20328,7 +21168,13 @@
  3.3773          int needQuote;
  3.3774          char ch;
  3.3775          char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
  3.3776 -        char *escarg = va_arg(ap,char*);
  3.3777 +        char *escarg;
  3.3778 +
  3.3779 +        if( bArgList ){
  3.3780 +          escarg = getTextArg(pArgList);
  3.3781 +        }else{
  3.3782 +          escarg = va_arg(ap,char*);
  3.3783 +        }
  3.3784          isnull = escarg==0;
  3.3785          if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
  3.3786          k = precision;
  3.3787 @@ -20340,7 +21186,7 @@
  3.3788          if( n>etBUFSIZE ){
  3.3789            bufpt = zExtra = sqlite3Malloc( n );
  3.3790            if( bufpt==0 ){
  3.3791 -            pAccum->accError = STRACCUM_NOMEM;
  3.3792 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  3.3793              return;
  3.3794            }
  3.3795          }else{
  3.3796 @@ -20363,7 +21209,8 @@
  3.3797        }
  3.3798        case etTOKEN: {
  3.3799          Token *pToken = va_arg(ap, Token*);
  3.3800 -        if( pToken ){
  3.3801 +        assert( bArgList==0 );
  3.3802 +        if( pToken && pToken->n ){
  3.3803            sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
  3.3804          }
  3.3805          length = width = 0;
  3.3806 @@ -20373,12 +21220,13 @@
  3.3807          SrcList *pSrc = va_arg(ap, SrcList*);
  3.3808          int k = va_arg(ap, int);
  3.3809          struct SrcList_item *pItem = &pSrc->a[k];
  3.3810 +        assert( bArgList==0 );
  3.3811          assert( k>=0 && k<pSrc->nSrc );
  3.3812          if( pItem->zDatabase ){
  3.3813 -          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
  3.3814 +          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
  3.3815            sqlite3StrAccumAppend(pAccum, ".", 1);
  3.3816          }
  3.3817 -        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
  3.3818 +        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
  3.3819          length = width = 0;
  3.3820          break;
  3.3821        }
  3.3822 @@ -20392,82 +21240,114 @@
  3.3823      ** "length" characters long.  The field width is "width".  Do
  3.3824      ** the output.
  3.3825      */
  3.3826 -    if( !flag_leftjustify ){
  3.3827 -      register int nspace;
  3.3828 -      nspace = width-length;
  3.3829 -      if( nspace>0 ){
  3.3830 -        sqlite3AppendSpace(pAccum, nspace);
  3.3831 -      }
  3.3832 -    }
  3.3833 -    if( length>0 ){
  3.3834 -      sqlite3StrAccumAppend(pAccum, bufpt, length);
  3.3835 -    }
  3.3836 -    if( flag_leftjustify ){
  3.3837 -      register int nspace;
  3.3838 -      nspace = width-length;
  3.3839 -      if( nspace>0 ){
  3.3840 -        sqlite3AppendSpace(pAccum, nspace);
  3.3841 -      }
  3.3842 -    }
  3.3843 -    sqlite3_free(zExtra);
  3.3844 +    width -= length;
  3.3845 +    if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
  3.3846 +    sqlite3StrAccumAppend(pAccum, bufpt, length);
  3.3847 +    if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
  3.3848 +
  3.3849 +    if( zExtra ) sqlite3_free(zExtra);
  3.3850    }/* End for loop over the format string */
  3.3851  } /* End of function */
  3.3852  
  3.3853  /*
  3.3854 -** Append N bytes of text from z to the StrAccum object.
  3.3855 -*/
  3.3856 -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
  3.3857 -  assert( z!=0 || N==0 );
  3.3858 +** Enlarge the memory allocation on a StrAccum object so that it is
  3.3859 +** able to accept at least N more bytes of text.
  3.3860 +**
  3.3861 +** Return the number of bytes of text that StrAccum is able to accept
  3.3862 +** after the attempted enlargement.  The value returned might be zero.
  3.3863 +*/
  3.3864 +static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
  3.3865 +  char *zNew;
  3.3866 +  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
  3.3867    if( p->accError ){
  3.3868      testcase(p->accError==STRACCUM_TOOBIG);
  3.3869      testcase(p->accError==STRACCUM_NOMEM);
  3.3870 +    return 0;
  3.3871 +  }
  3.3872 +  if( !p->useMalloc ){
  3.3873 +    N = p->nAlloc - p->nChar - 1;
  3.3874 +    setStrAccumError(p, STRACCUM_TOOBIG);
  3.3875 +    return N;
  3.3876 +  }else{
  3.3877 +    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
  3.3878 +    i64 szNew = p->nChar;
  3.3879 +    szNew += N + 1;
  3.3880 +    if( szNew > p->mxAlloc ){
  3.3881 +      sqlite3StrAccumReset(p);
  3.3882 +      setStrAccumError(p, STRACCUM_TOOBIG);
  3.3883 +      return 0;
  3.3884 +    }else{
  3.3885 +      p->nAlloc = (int)szNew;
  3.3886 +    }
  3.3887 +    if( p->useMalloc==1 ){
  3.3888 +      zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
  3.3889 +    }else{
  3.3890 +      zNew = sqlite3_realloc(zOld, p->nAlloc);
  3.3891 +    }
  3.3892 +    if( zNew ){
  3.3893 +      assert( p->zText!=0 || p->nChar==0 );
  3.3894 +      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
  3.3895 +      p->zText = zNew;
  3.3896 +    }else{
  3.3897 +      sqlite3StrAccumReset(p);
  3.3898 +      setStrAccumError(p, STRACCUM_NOMEM);
  3.3899 +      return 0;
  3.3900 +    }
  3.3901 +  }
  3.3902 +  return N;
  3.3903 +}
  3.3904 +
  3.3905 +/*
  3.3906 +** Append N space characters to the given string buffer.
  3.3907 +*/
  3.3908 +SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){
  3.3909 +  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
  3.3910 +  while( (N--)>0 ) p->zText[p->nChar++] = ' ';
  3.3911 +}
  3.3912 +
  3.3913 +/*
  3.3914 +** The StrAccum "p" is not large enough to accept N new bytes of z[].
  3.3915 +** So enlarge if first, then do the append.
  3.3916 +**
  3.3917 +** This is a helper routine to sqlite3StrAccumAppend() that does special-case
  3.3918 +** work (enlarging the buffer) using tail recursion, so that the
  3.3919 +** sqlite3StrAccumAppend() routine can use fast calling semantics.
  3.3920 +*/
  3.3921 +static void enlargeAndAppend(StrAccum *p, const char *z, int N){
  3.3922 +  N = sqlite3StrAccumEnlarge(p, N);
  3.3923 +  if( N>0 ){
  3.3924 +    memcpy(&p->zText[p->nChar], z, N);
  3.3925 +    p->nChar += N;
  3.3926 +  }
  3.3927 +}
  3.3928 +
  3.3929 +/*
  3.3930 +** Append N bytes of text from z to the StrAccum object.  Increase the
  3.3931 +** size of the memory allocation for StrAccum if necessary.
  3.3932 +*/
  3.3933 +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
  3.3934 +  assert( z!=0 );
  3.3935 +  assert( p->zText!=0 || p->nChar==0 || p->accError );
  3.3936 +  assert( N>=0 );
  3.3937 +  assert( p->accError==0 || p->nAlloc==0 );
  3.3938 +  if( p->nChar+N >= p->nAlloc ){
  3.3939 +    enlargeAndAppend(p,z,N);
  3.3940      return;
  3.3941    }
  3.3942 -  assert( p->zText!=0 || p->nChar==0 );
  3.3943 -  if( N<=0 ){
  3.3944 -    if( N==0 || z[0]==0 ) return;
  3.3945 -    N = sqlite3Strlen30(z);
  3.3946 -  }
  3.3947 -  if( p->nChar+N >= p->nAlloc ){
  3.3948 -    char *zNew;
  3.3949 -    if( !p->useMalloc ){
  3.3950 -      p->accError = STRACCUM_TOOBIG;
  3.3951 -      N = p->nAlloc - p->nChar - 1;
  3.3952 -      if( N<=0 ){
  3.3953 -        return;
  3.3954 -      }
  3.3955 -    }else{
  3.3956 -      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
  3.3957 -      i64 szNew = p->nChar;
  3.3958 -      szNew += N + 1;
  3.3959 -      if( szNew > p->mxAlloc ){
  3.3960 -        sqlite3StrAccumReset(p);
  3.3961 -        p->accError = STRACCUM_TOOBIG;
  3.3962 -        return;
  3.3963 -      }else{
  3.3964 -        p->nAlloc = (int)szNew;
  3.3965 -      }
  3.3966 -      if( p->useMalloc==1 ){
  3.3967 -        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
  3.3968 -      }else{
  3.3969 -        zNew = sqlite3_realloc(zOld, p->nAlloc);
  3.3970 -      }
  3.3971 -      if( zNew ){
  3.3972 -        if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
  3.3973 -        p->zText = zNew;
  3.3974 -      }else{
  3.3975 -        p->accError = STRACCUM_NOMEM;
  3.3976 -        sqlite3StrAccumReset(p);
  3.3977 -        return;
  3.3978 -      }
  3.3979 -    }
  3.3980 -  }
  3.3981    assert( p->zText );
  3.3982    memcpy(&p->zText[p->nChar], z, N);
  3.3983    p->nChar += N;
  3.3984  }
  3.3985  
  3.3986  /*
  3.3987 +** Append the complete text of zero-terminated string z[] to the p string.
  3.3988 +*/
  3.3989 +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
  3.3990 +  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
  3.3991 +}
  3.3992 +
  3.3993 +
  3.3994 +/*
  3.3995  ** Finish off a string by making sure it is zero-terminated.
  3.3996  ** Return a pointer to the resulting string.  Return a NULL
  3.3997  ** pointer if any kind of error was encountered.
  3.3998 @@ -20484,7 +21364,7 @@
  3.3999        if( p->zText ){
  3.4000          memcpy(p->zText, p->zBase, p->nChar+1);
  3.4001        }else{
  3.4002 -        p->accError = STRACCUM_NOMEM;
  3.4003 +        setStrAccumError(p, STRACCUM_NOMEM);
  3.4004        }
  3.4005      }
  3.4006    }
  3.4007 @@ -20530,7 +21410,7 @@
  3.4008    sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
  3.4009                        db->aLimit[SQLITE_LIMIT_LENGTH]);
  3.4010    acc.db = db;
  3.4011 -  sqlite3VXPrintf(&acc, 1, zFormat, ap);
  3.4012 +  sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
  3.4013    z = sqlite3StrAccumFinish(&acc);
  3.4014    if( acc.accError==STRACCUM_NOMEM ){
  3.4015      db->mallocFailed = 1;
  3.4016 @@ -20686,17 +21566,15 @@
  3.4017  }
  3.4018  #endif
  3.4019  
  3.4020 -#ifndef SQLITE_OMIT_TRACE
  3.4021  /*
  3.4022  ** variable-argument wrapper around sqlite3VXPrintf().
  3.4023  */
  3.4024 -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
  3.4025 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
  3.4026    va_list ap;
  3.4027    va_start(ap,zFormat);
  3.4028 -  sqlite3VXPrintf(p, 1, zFormat, ap);
  3.4029 +  sqlite3VXPrintf(p, bFlags, zFormat, ap);
  3.4030    va_end(ap);
  3.4031  }
  3.4032 -#endif
  3.4033  
  3.4034  /************** End of printf.c **********************************************/
  3.4035  /************** Begin file random.c ******************************************/
  3.4036 @@ -20753,6 +21631,12 @@
  3.4037    sqlite3_mutex_enter(mutex);
  3.4038  #endif
  3.4039  
  3.4040 +  if( N<=0 ){
  3.4041 +    wsdPrng.isInit = 0;
  3.4042 +    sqlite3_mutex_leave(mutex);
  3.4043 +    return;
  3.4044 +  }
  3.4045 +
  3.4046    /* Initialize the state of the random number generator once,
  3.4047    ** the first time this routine is called.  The seed value does
  3.4048    ** not need to contain a lot of randomness since we are not
  3.4049 @@ -20780,7 +21664,8 @@
  3.4050      wsdPrng.isInit = 1;
  3.4051    }
  3.4052  
  3.4053 -  while( N-- ){
  3.4054 +  assert( N>0 );
  3.4055 +  do{
  3.4056      wsdPrng.i++;
  3.4057      t = wsdPrng.s[wsdPrng.i];
  3.4058      wsdPrng.j += t;
  3.4059 @@ -20788,7 +21673,7 @@
  3.4060      wsdPrng.s[wsdPrng.j] = t;
  3.4061      t += wsdPrng.s[wsdPrng.i];
  3.4062      *(zBuf++) = wsdPrng.s[t];
  3.4063 -  }
  3.4064 +  }while( --N );
  3.4065    sqlite3_mutex_leave(mutex);
  3.4066  }
  3.4067  
  3.4068 @@ -20817,9 +21702,6 @@
  3.4069      sizeof(sqlite3Prng)
  3.4070    );
  3.4071  }
  3.4072 -SQLITE_PRIVATE void sqlite3PrngResetState(void){
  3.4073 -  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
  3.4074 -}
  3.4075  #endif /* SQLITE_OMIT_BUILTIN_TEST */
  3.4076  
  3.4077  /************** End of random.c **********************************************/
  3.4078 @@ -20972,8 +21854,8 @@
  3.4079  **     and rendered as themselves even though they are technically
  3.4080  **     invalid characters.
  3.4081  **
  3.4082 -**  *  This routine accepts an infinite number of different UTF8 encodings
  3.4083 -**     for unicode values 0x80 and greater.  It do not change over-length
  3.4084 +**  *  This routine accepts over-length UTF8 encodings
  3.4085 +**     for unicode values 0x80 and greater.  It does not change over-length
  3.4086  **     encodings to 0xfffd as some systems recommend.
  3.4087  */
  3.4088  #define READ_UTF8(zIn, zTerm, c)                           \
  3.4089 @@ -21141,7 +22023,7 @@
  3.4090    sqlite3VdbeMemRelease(pMem);
  3.4091    pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
  3.4092    pMem->enc = desiredEnc;
  3.4093 -  pMem->flags |= (MEM_Term|MEM_Dyn);
  3.4094 +  pMem->flags |= (MEM_Term);
  3.4095    pMem->z = (char*)zOut;
  3.4096    pMem->zMalloc = pMem->z;
  3.4097  
  3.4098 @@ -21269,7 +22151,6 @@
  3.4099    }
  3.4100    assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
  3.4101    assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
  3.4102 -  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
  3.4103    assert( m.z || db->mallocFailed );
  3.4104    return m.z;
  3.4105  }
  3.4106 @@ -21387,6 +22268,24 @@
  3.4107  }
  3.4108  #endif
  3.4109  
  3.4110 +/*
  3.4111 +** Give a callback to the test harness that can be used to simulate faults
  3.4112 +** in places where it is difficult or expensive to do so purely by means
  3.4113 +** of inputs.
  3.4114 +**
  3.4115 +** The intent of the integer argument is to let the fault simulator know
  3.4116 +** which of multiple sqlite3FaultSim() calls has been hit.
  3.4117 +**
  3.4118 +** Return whatever integer value the test callback returns, or return
  3.4119 +** SQLITE_OK if no test callback is installed.
  3.4120 +*/
  3.4121 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  3.4122 +SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
  3.4123 +  int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
  3.4124 +  return xCallback ? xCallback(iTest) : SQLITE_OK;
  3.4125 +}
  3.4126 +#endif
  3.4127 +
  3.4128  #ifndef SQLITE_OMIT_FLOATING_POINT
  3.4129  /*
  3.4130  ** Return true if the floating point value is Not a Number (NaN).
  3.4131 @@ -21471,18 +22370,17 @@
  3.4132  ** to NULL.
  3.4133  */
  3.4134  SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
  3.4135 -  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
  3.4136 -    db->errCode = err_code;
  3.4137 -    if( zFormat ){
  3.4138 -      char *z;
  3.4139 -      va_list ap;
  3.4140 -      va_start(ap, zFormat);
  3.4141 -      z = sqlite3VMPrintf(db, zFormat, ap);
  3.4142 -      va_end(ap);
  3.4143 -      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
  3.4144 -    }else{
  3.4145 -      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
  3.4146 -    }
  3.4147 +  assert( db!=0 );
  3.4148 +  db->errCode = err_code;
  3.4149 +  if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
  3.4150 +    char *z;
  3.4151 +    va_list ap;
  3.4152 +    va_start(ap, zFormat);
  3.4153 +    z = sqlite3VMPrintf(db, zFormat, ap);
  3.4154 +    va_end(ap);
  3.4155 +    sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
  3.4156 +  }else if( db->pErr ){
  3.4157 +    sqlite3ValueSetNull(db->pErr);
  3.4158    }
  3.4159  }
  3.4160  
  3.4161 @@ -21814,9 +22712,9 @@
  3.4162    return c;
  3.4163  }
  3.4164  
  3.4165 -
  3.4166 -/*
  3.4167 -** Convert zNum to a 64-bit signed integer.
  3.4168 +/*
  3.4169 +** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
  3.4170 +** routine does *not* accept hexadecimal notation.
  3.4171  **
  3.4172  ** If the zNum value is representable as a 64-bit twos-complement 
  3.4173  ** integer, then write that value into *pNum and return 0.
  3.4174 @@ -21905,9 +22803,43 @@
  3.4175  }
  3.4176  
  3.4177  /*
  3.4178 +** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
  3.4179 +** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
  3.4180 +** whereas sqlite3Atoi64() does not.
  3.4181 +**
  3.4182 +** Returns:
  3.4183 +**
  3.4184 +**     0    Successful transformation.  Fits in a 64-bit signed integer.
  3.4185 +**     1    Integer too large for a 64-bit signed integer or is malformed
  3.4186 +**     2    Special case of 9223372036854775808
  3.4187 +*/
  3.4188 +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
  3.4189 +#ifndef SQLITE_OMIT_HEX_INTEGER
  3.4190 +  if( z[0]=='0'
  3.4191 +   && (z[1]=='x' || z[1]=='X')
  3.4192 +   && sqlite3Isxdigit(z[2])
  3.4193 +  ){
  3.4194 +    u64 u = 0;
  3.4195 +    int i, k;
  3.4196 +    for(i=2; z[i]=='0'; i++){}
  3.4197 +    for(k=i; sqlite3Isxdigit(z[k]); k++){
  3.4198 +      u = u*16 + sqlite3HexToInt(z[k]);
  3.4199 +    }
  3.4200 +    memcpy(pOut, &u, 8);
  3.4201 +    return (z[k]==0 && k-i<=16) ? 0 : 1;
  3.4202 +  }else
  3.4203 +#endif /* SQLITE_OMIT_HEX_INTEGER */
  3.4204 +  {
  3.4205 +    return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
  3.4206 +  }
  3.4207 +}
  3.4208 +
  3.4209 +/*
  3.4210  ** If zNum represents an integer that will fit in 32-bits, then set
  3.4211  ** *pValue to that integer and return true.  Otherwise return false.
  3.4212  **
  3.4213 +** This routine accepts both decimal and hexadecimal notation for integers.
  3.4214 +**
  3.4215  ** Any non-numeric characters that following zNum are ignored.
  3.4216  ** This is different from sqlite3Atoi64() which requires the
  3.4217  ** input number to be zero-terminated.
  3.4218 @@ -21922,7 +22854,25 @@
  3.4219    }else if( zNum[0]=='+' ){
  3.4220      zNum++;
  3.4221    }
  3.4222 -  while( zNum[0]=='0' ) zNum++;
  3.4223 +#ifndef SQLITE_OMIT_HEX_INTEGER
  3.4224 +  else if( zNum[0]=='0'
  3.4225 +        && (zNum[1]=='x' || zNum[1]=='X')
  3.4226 +        && sqlite3Isxdigit(zNum[2])
  3.4227 +  ){
  3.4228 +    u32 u = 0;
  3.4229 +    zNum += 2;
  3.4230 +    while( zNum[0]=='0' ) zNum++;
  3.4231 +    for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
  3.4232 +      u = u*16 + sqlite3HexToInt(zNum[i]);
  3.4233 +    }
  3.4234 +    if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
  3.4235 +      memcpy(pValue, &u, 4);
  3.4236 +      return 1;
  3.4237 +    }else{
  3.4238 +      return 0;
  3.4239 +    }
  3.4240 +  }
  3.4241 +#endif
  3.4242    for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
  3.4243      v = v*10 + c;
  3.4244    }
  3.4245 @@ -22358,7 +23308,8 @@
  3.4246  ** Read or write a four-byte big-endian integer value.
  3.4247  */
  3.4248  SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
  3.4249 -  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  3.4250 +  testcase( p[0]&0x80 );
  3.4251 +  return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  3.4252  }
  3.4253  SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
  3.4254    p[0] = (u8)(v>>24);
  3.4255 @@ -22479,13 +23430,12 @@
  3.4256      testcase( iA>0 && LARGEST_INT64 - iA == iB );
  3.4257      testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
  3.4258      if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
  3.4259 -    *pA += iB;
  3.4260    }else{
  3.4261      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
  3.4262      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
  3.4263      if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
  3.4264 -    *pA += iB;
  3.4265 -  }
  3.4266 +  }
  3.4267 +  *pA += iB;
  3.4268    return 0; 
  3.4269  }
  3.4270  SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
  3.4271 @@ -22509,9 +23459,18 @@
  3.4272    iA0 = iA % TWOPOWER32;
  3.4273    iB1 = iB/TWOPOWER32;
  3.4274    iB0 = iB % TWOPOWER32;
  3.4275 -  if( iA1*iB1 != 0 ) return 1;
  3.4276 -  assert( iA1*iB0==0 || iA0*iB1==0 );
  3.4277 -  r = iA1*iB0 + iA0*iB1;
  3.4278 +  if( iA1==0 ){
  3.4279 +    if( iB1==0 ){
  3.4280 +      *pA *= iB;
  3.4281 +      return 0;
  3.4282 +    }
  3.4283 +    r = iA0*iB1;
  3.4284 +  }else if( iB1==0 ){
  3.4285 +    r = iA1*iB0;
  3.4286 +  }else{
  3.4287 +    /* If both iA1 and iB1 are non-zero, overflow will result */
  3.4288 +    return 1;
  3.4289 +  }
  3.4290    testcase( r==(-TWOPOWER31)-1 );
  3.4291    testcase( r==(-TWOPOWER31) );
  3.4292    testcase( r==TWOPOWER31 );
  3.4293 @@ -22594,8 +23553,8 @@
  3.4294  }
  3.4295  
  3.4296  /*
  3.4297 -** Convert an integer into a LogEst.  In other words, compute a
  3.4298 -** good approximatation for 10*log2(x).
  3.4299 +** Convert an integer into a LogEst.  In other words, compute an
  3.4300 +** approximation for 10*log2(x).
  3.4301  */
  3.4302  SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
  3.4303    static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
  3.4304 @@ -22699,7 +23658,7 @@
  3.4305  ** The hashing function.
  3.4306  */
  3.4307  static unsigned int strHash(const char *z, int nKey){
  3.4308 -  int h = 0;
  3.4309 +  unsigned int h = 0;
  3.4310    assert( nKey>=0 );
  3.4311    while( nKey > 0  ){
  3.4312      h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
  3.4313 @@ -22951,145 +23910,151 @@
  3.4314       /*  11 */ "Checkpoint"       OpHelp(""),
  3.4315       /*  12 */ "JournalMode"      OpHelp(""),
  3.4316       /*  13 */ "Vacuum"           OpHelp(""),
  3.4317 -     /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
  3.4318 +     /*  14 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
  3.4319       /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
  3.4320       /*  16 */ "Goto"             OpHelp(""),
  3.4321       /*  17 */ "Gosub"            OpHelp(""),
  3.4322       /*  18 */ "Return"           OpHelp(""),
  3.4323       /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
  3.4324 -     /*  20 */ "Yield"            OpHelp(""),
  3.4325 -     /*  21 */ "HaltIfNull"       OpHelp("if r[P3] null then halt"),
  3.4326 -     /*  22 */ "Halt"             OpHelp(""),
  3.4327 -     /*  23 */ "Integer"          OpHelp("r[P2]=P1"),
  3.4328 -     /*  24 */ "Int64"            OpHelp("r[P2]=P4"),
  3.4329 -     /*  25 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  3.4330 -     /*  26 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  3.4331 -     /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  3.4332 -     /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  3.4333 -     /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  3.4334 -     /*  30 */ "Copy"             OpHelp("r[P2@P3]=r[P1@P3]"),
  3.4335 -     /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  3.4336 -     /*  32 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  3.4337 -     /*  33 */ "CollSeq"          OpHelp(""),
  3.4338 -     /*  34 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  3.4339 -     /*  35 */ "MustBeInt"        OpHelp(""),
  3.4340 -     /*  36 */ "RealAffinity"     OpHelp(""),
  3.4341 -     /*  37 */ "Permutation"      OpHelp(""),
  3.4342 -     /*  38 */ "Compare"          OpHelp(""),
  3.4343 -     /*  39 */ "Jump"             OpHelp(""),
  3.4344 -     /*  40 */ "Once"             OpHelp(""),
  3.4345 -     /*  41 */ "If"               OpHelp(""),
  3.4346 -     /*  42 */ "IfNot"            OpHelp(""),
  3.4347 -     /*  43 */ "Column"           OpHelp("r[P3]=PX"),
  3.4348 -     /*  44 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  3.4349 -     /*  45 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  3.4350 -     /*  46 */ "Count"            OpHelp("r[P2]=count()"),
  3.4351 -     /*  47 */ "ReadCookie"       OpHelp(""),
  3.4352 -     /*  48 */ "SetCookie"        OpHelp(""),
  3.4353 -     /*  49 */ "VerifyCookie"     OpHelp(""),
  3.4354 -     /*  50 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  3.4355 -     /*  51 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  3.4356 -     /*  52 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  3.4357 -     /*  53 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  3.4358 -     /*  54 */ "SorterOpen"       OpHelp(""),
  3.4359 -     /*  55 */ "OpenPseudo"       OpHelp("content in r[P2@P3]"),
  3.4360 -     /*  56 */ "Close"            OpHelp(""),
  3.4361 -     /*  57 */ "SeekLt"           OpHelp("key=r[P3@P4]"),
  3.4362 -     /*  58 */ "SeekLe"           OpHelp("key=r[P3@P4]"),
  3.4363 -     /*  59 */ "SeekGe"           OpHelp("key=r[P3@P4]"),
  3.4364 -     /*  60 */ "SeekGt"           OpHelp("key=r[P3@P4]"),
  3.4365 -     /*  61 */ "Seek"             OpHelp("intkey=r[P2]"),
  3.4366 -     /*  62 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  3.4367 -     /*  63 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  3.4368 -     /*  64 */ "Found"            OpHelp("key=r[P3@P4]"),
  3.4369 -     /*  65 */ "NotExists"        OpHelp("intkey=r[P3]"),
  3.4370 -     /*  66 */ "Sequence"         OpHelp("r[P2]=rowid"),
  3.4371 -     /*  67 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  3.4372 -     /*  68 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  3.4373 -     /*  69 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  3.4374 -     /*  70 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  3.4375 -     /*  71 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  3.4376 -     /*  72 */ "Delete"           OpHelp(""),
  3.4377 -     /*  73 */ "ResetCount"       OpHelp(""),
  3.4378 -     /*  74 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  3.4379 -     /*  75 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  3.4380 -     /*  76 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  3.4381 -     /*  77 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  3.4382 -     /*  78 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  3.4383 -     /*  79 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  3.4384 -     /*  80 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  3.4385 -     /*  81 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  3.4386 -     /*  82 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
  3.4387 -     /*  83 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  3.4388 -     /*  84 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  3.4389 -     /*  85 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  3.4390 -     /*  86 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  3.4391 -     /*  87 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  3.4392 -     /*  88 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  3.4393 -     /*  89 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  3.4394 -     /*  90 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  3.4395 -     /*  91 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  3.4396 -     /*  92 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  3.4397 -     /*  93 */ "SorterData"       OpHelp("r[P2]=data"),
  3.4398 -     /*  94 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  3.4399 -     /*  95 */ "String8"          OpHelp("r[P2]='P4'"),
  3.4400 -     /*  96 */ "RowKey"           OpHelp("r[P2]=key"),
  3.4401 -     /*  97 */ "RowData"          OpHelp("r[P2]=data"),
  3.4402 -     /*  98 */ "Rowid"            OpHelp("r[P2]=rowid"),
  3.4403 -     /*  99 */ "NullRow"          OpHelp(""),
  3.4404 -     /* 100 */ "Last"             OpHelp(""),
  3.4405 -     /* 101 */ "SorterSort"       OpHelp(""),
  3.4406 -     /* 102 */ "Sort"             OpHelp(""),
  3.4407 -     /* 103 */ "Rewind"           OpHelp(""),
  3.4408 -     /* 104 */ "SorterInsert"     OpHelp(""),
  3.4409 -     /* 105 */ "IdxInsert"        OpHelp("key=r[P2]"),
  3.4410 -     /* 106 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  3.4411 -     /* 107 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  3.4412 -     /* 108 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  3.4413 -     /* 109 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  3.4414 -     /* 110 */ "Destroy"          OpHelp(""),
  3.4415 -     /* 111 */ "Clear"            OpHelp(""),
  3.4416 -     /* 112 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  3.4417 -     /* 113 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  3.4418 -     /* 114 */ "ParseSchema"      OpHelp(""),
  3.4419 -     /* 115 */ "LoadAnalysis"     OpHelp(""),
  3.4420 -     /* 116 */ "DropTable"        OpHelp(""),
  3.4421 -     /* 117 */ "DropIndex"        OpHelp(""),
  3.4422 -     /* 118 */ "DropTrigger"      OpHelp(""),
  3.4423 -     /* 119 */ "IntegrityCk"      OpHelp(""),
  3.4424 -     /* 120 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  3.4425 -     /* 121 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  3.4426 -     /* 122 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  3.4427 -     /* 123 */ "Program"          OpHelp(""),
  3.4428 -     /* 124 */ "Param"            OpHelp(""),
  3.4429 -     /* 125 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  3.4430 -     /* 126 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  3.4431 -     /* 127 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  3.4432 -     /* 128 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
  3.4433 -     /* 129 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
  3.4434 -     /* 130 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
  3.4435 -     /* 131 */ "Real"             OpHelp("r[P2]=P4"),
  3.4436 -     /* 132 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  3.4437 -     /* 133 */ "IncrVacuum"       OpHelp(""),
  3.4438 -     /* 134 */ "Expire"           OpHelp(""),
  3.4439 -     /* 135 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  3.4440 -     /* 136 */ "VBegin"           OpHelp(""),
  3.4441 -     /* 137 */ "VCreate"          OpHelp(""),
  3.4442 -     /* 138 */ "VDestroy"         OpHelp(""),
  3.4443 -     /* 139 */ "VOpen"            OpHelp(""),
  3.4444 -     /* 140 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  3.4445 -     /* 141 */ "VNext"            OpHelp(""),
  3.4446 -     /* 142 */ "ToText"           OpHelp(""),
  3.4447 -     /* 143 */ "ToBlob"           OpHelp(""),
  3.4448 -     /* 144 */ "ToNumeric"        OpHelp(""),
  3.4449 -     /* 145 */ "ToInt"            OpHelp(""),
  3.4450 -     /* 146 */ "ToReal"           OpHelp(""),
  3.4451 -     /* 147 */ "VRename"          OpHelp(""),
  3.4452 -     /* 148 */ "Pagecount"        OpHelp(""),
  3.4453 -     /* 149 */ "MaxPgcnt"         OpHelp(""),
  3.4454 -     /* 150 */ "Trace"            OpHelp(""),
  3.4455 -     /* 151 */ "Noop"             OpHelp(""),
  3.4456 -     /* 152 */ "Explain"          OpHelp(""),
  3.4457 +     /*  20 */ "InitCoroutine"    OpHelp(""),
  3.4458 +     /*  21 */ "EndCoroutine"     OpHelp(""),
  3.4459 +     /*  22 */ "Yield"            OpHelp(""),
  3.4460 +     /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  3.4461 +     /*  24 */ "Halt"             OpHelp(""),
  3.4462 +     /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
  3.4463 +     /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
  3.4464 +     /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  3.4465 +     /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  3.4466 +     /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  3.4467 +     /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  3.4468 +     /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  3.4469 +     /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  3.4470 +     /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  3.4471 +     /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  3.4472 +     /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  3.4473 +     /*  36 */ "CollSeq"          OpHelp(""),
  3.4474 +     /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  3.4475 +     /*  38 */ "MustBeInt"        OpHelp(""),
  3.4476 +     /*  39 */ "RealAffinity"     OpHelp(""),
  3.4477 +     /*  40 */ "Permutation"      OpHelp(""),
  3.4478 +     /*  41 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  3.4479 +     /*  42 */ "Jump"             OpHelp(""),
  3.4480 +     /*  43 */ "Once"             OpHelp(""),
  3.4481 +     /*  44 */ "If"               OpHelp(""),
  3.4482 +     /*  45 */ "IfNot"            OpHelp(""),
  3.4483 +     /*  46 */ "Column"           OpHelp("r[P3]=PX"),
  3.4484 +     /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  3.4485 +     /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  3.4486 +     /*  49 */ "Count"            OpHelp("r[P2]=count()"),
  3.4487 +     /*  50 */ "ReadCookie"       OpHelp(""),
  3.4488 +     /*  51 */ "SetCookie"        OpHelp(""),
  3.4489 +     /*  52 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  3.4490 +     /*  53 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  3.4491 +     /*  54 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  3.4492 +     /*  55 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  3.4493 +     /*  56 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  3.4494 +     /*  57 */ "SorterOpen"       OpHelp(""),
  3.4495 +     /*  58 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  3.4496 +     /*  59 */ "Close"            OpHelp(""),
  3.4497 +     /*  60 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  3.4498 +     /*  61 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  3.4499 +     /*  62 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  3.4500 +     /*  63 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  3.4501 +     /*  64 */ "Seek"             OpHelp("intkey=r[P2]"),
  3.4502 +     /*  65 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  3.4503 +     /*  66 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  3.4504 +     /*  67 */ "Found"            OpHelp("key=r[P3@P4]"),
  3.4505 +     /*  68 */ "NotExists"        OpHelp("intkey=r[P3]"),
  3.4506 +     /*  69 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  3.4507 +     /*  70 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  3.4508 +     /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  3.4509 +     /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  3.4510 +     /*  73 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  3.4511 +     /*  74 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  3.4512 +     /*  75 */ "Delete"           OpHelp(""),
  3.4513 +     /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  3.4514 +     /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  3.4515 +     /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  3.4516 +     /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  3.4517 +     /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  3.4518 +     /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  3.4519 +     /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  3.4520 +     /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  3.4521 +     /*  84 */ "ResetCount"       OpHelp(""),
  3.4522 +     /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  3.4523 +     /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  3.4524 +     /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  3.4525 +     /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  3.4526 +     /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  3.4527 +     /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  3.4528 +     /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  3.4529 +     /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  3.4530 +     /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  3.4531 +     /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  3.4532 +     /*  95 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  3.4533 +     /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  3.4534 +     /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
  3.4535 +     /*  98 */ "SorterData"       OpHelp("r[P2]=data"),
  3.4536 +     /*  99 */ "RowKey"           OpHelp("r[P2]=key"),
  3.4537 +     /* 100 */ "RowData"          OpHelp("r[P2]=data"),
  3.4538 +     /* 101 */ "Rowid"            OpHelp("r[P2]=rowid"),
  3.4539 +     /* 102 */ "NullRow"          OpHelp(""),
  3.4540 +     /* 103 */ "Last"             OpHelp(""),
  3.4541 +     /* 104 */ "SorterSort"       OpHelp(""),
  3.4542 +     /* 105 */ "Sort"             OpHelp(""),
  3.4543 +     /* 106 */ "Rewind"           OpHelp(""),
  3.4544 +     /* 107 */ "SorterInsert"     OpHelp(""),
  3.4545 +     /* 108 */ "IdxInsert"        OpHelp("key=r[P2]"),
  3.4546 +     /* 109 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  3.4547 +     /* 110 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  3.4548 +     /* 111 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  3.4549 +     /* 112 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  3.4550 +     /* 113 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  3.4551 +     /* 114 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  3.4552 +     /* 115 */ "Destroy"          OpHelp(""),
  3.4553 +     /* 116 */ "Clear"            OpHelp(""),
  3.4554 +     /* 117 */ "ResetSorter"      OpHelp(""),
  3.4555 +     /* 118 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  3.4556 +     /* 119 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  3.4557 +     /* 120 */ "ParseSchema"      OpHelp(""),
  3.4558 +     /* 121 */ "LoadAnalysis"     OpHelp(""),
  3.4559 +     /* 122 */ "DropTable"        OpHelp(""),
  3.4560 +     /* 123 */ "DropIndex"        OpHelp(""),
  3.4561 +     /* 124 */ "DropTrigger"      OpHelp(""),
  3.4562 +     /* 125 */ "IntegrityCk"      OpHelp(""),
  3.4563 +     /* 126 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  3.4564 +     /* 127 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  3.4565 +     /* 128 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  3.4566 +     /* 129 */ "Program"          OpHelp(""),
  3.4567 +     /* 130 */ "Param"            OpHelp(""),
  3.4568 +     /* 131 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  3.4569 +     /* 132 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  3.4570 +     /* 133 */ "Real"             OpHelp("r[P2]=P4"),
  3.4571 +     /* 134 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  3.4572 +     /* 135 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
  3.4573 +     /* 136 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
  3.4574 +     /* 137 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
  3.4575 +     /* 138 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  3.4576 +     /* 139 */ "IncrVacuum"       OpHelp(""),
  3.4577 +     /* 140 */ "Expire"           OpHelp(""),
  3.4578 +     /* 141 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  3.4579 +     /* 142 */ "VBegin"           OpHelp(""),
  3.4580 +     /* 143 */ "ToText"           OpHelp(""),
  3.4581 +     /* 144 */ "ToBlob"           OpHelp(""),
  3.4582 +     /* 145 */ "ToNumeric"        OpHelp(""),
  3.4583 +     /* 146 */ "ToInt"            OpHelp(""),
  3.4584 +     /* 147 */ "ToReal"           OpHelp(""),
  3.4585 +     /* 148 */ "VCreate"          OpHelp(""),
  3.4586 +     /* 149 */ "VDestroy"         OpHelp(""),
  3.4587 +     /* 150 */ "VOpen"            OpHelp(""),
  3.4588 +     /* 151 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  3.4589 +     /* 152 */ "VNext"            OpHelp(""),
  3.4590 +     /* 153 */ "VRename"          OpHelp(""),
  3.4591 +     /* 154 */ "Pagecount"        OpHelp(""),
  3.4592 +     /* 155 */ "MaxPgcnt"         OpHelp(""),
  3.4593 +     /* 156 */ "Init"             OpHelp("Start at P2"),
  3.4594 +     /* 157 */ "Noop"             OpHelp(""),
  3.4595 +     /* 158 */ "Explain"          OpHelp(""),
  3.4596    };
  3.4597    return azName[i];
  3.4598  }
  3.4599 @@ -23182,32 +24147,6 @@
  3.4600  #endif
  3.4601  
  3.4602  /*
  3.4603 -** These #defines should enable >2GB file support on Posix if the
  3.4604 -** underlying operating system supports it.  If the OS lacks
  3.4605 -** large file support, these should be no-ops.
  3.4606 -**
  3.4607 -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  3.4608 -** on the compiler command line.  This is necessary if you are compiling
  3.4609 -** on a recent machine (ex: RedHat 7.2) but you want your code to work
  3.4610 -** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
  3.4611 -** without this option, LFS is enable.  But LFS does not exist in the kernel
  3.4612 -** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
  3.4613 -** portability you should omit LFS.
  3.4614 -**
  3.4615 -** The previous paragraph was written in 2005.  (This paragraph is written
  3.4616 -** on 2008-11-28.) These days, all Linux kernels support large files, so
  3.4617 -** you should probably leave LFS enabled.  But some embedded platforms might
  3.4618 -** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
  3.4619 -*/
  3.4620 -#ifndef SQLITE_DISABLE_LFS
  3.4621 -# define _LARGE_FILE       1
  3.4622 -# ifndef _FILE_OFFSET_BITS
  3.4623 -#   define _FILE_OFFSET_BITS 64
  3.4624 -# endif
  3.4625 -# define _LARGEFILE_SOURCE 1
  3.4626 -#endif
  3.4627 -
  3.4628 -/*
  3.4629  ** standard include files.
  3.4630  */
  3.4631  #include <sys/types.h>
  3.4632 @@ -23218,11 +24157,10 @@
  3.4633  #include <sys/time.h>
  3.4634  #include <errno.h>
  3.4635  #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  3.4636 -#include <sys/mman.h>
  3.4637 -#endif
  3.4638 -
  3.4639 -
  3.4640 -#if SQLITE_ENABLE_LOCKING_STYLE
  3.4641 +# include <sys/mman.h>
  3.4642 +#endif
  3.4643 +
  3.4644 +#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
  3.4645  # include <sys/ioctl.h>
  3.4646  # if OS_VXWORKS
  3.4647  #  include <semaphore.h>
  3.4648 @@ -23358,6 +24296,12 @@
  3.4649  #endif
  3.4650  };
  3.4651  
  3.4652 +/* This variable holds the process id (pid) from when the xRandomness()
  3.4653 +** method was called.  If xOpen() is called from a different process id,
  3.4654 +** indicating that a fork() has occurred, the PRNG will be reset.
  3.4655 +*/
  3.4656 +static int randomnessPid = 0;
  3.4657 +
  3.4658  /*
  3.4659  ** Allowed values for the unixFile.ctrlFlags bitmask:
  3.4660  */
  3.4661 @@ -23644,11 +24588,16 @@
  3.4662  ** we are not running as root.
  3.4663  */
  3.4664  static int posixFchown(int fd, uid_t uid, gid_t gid){
  3.4665 +#if OS_VXWORKS
  3.4666 +  return 0;
  3.4667 +#else
  3.4668    return geteuid() ? 0 : fchown(fd,uid,gid);
  3.4669 +#endif
  3.4670  }
  3.4671  
  3.4672  /* Forward reference */
  3.4673  static int openDirectory(const char*, int*);
  3.4674 +static int unixGetpagesize(void);
  3.4675  
  3.4676  /*
  3.4677  ** Many system calls are accessed through pointer-to-functions so that
  3.4678 @@ -23699,7 +24648,7 @@
  3.4679    { "read",         (sqlite3_syscall_ptr)read,       0  },
  3.4680  #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
  3.4681  
  3.4682 -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  3.4683 +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
  3.4684    { "pread",        (sqlite3_syscall_ptr)pread,      0  },
  3.4685  #else
  3.4686    { "pread",        (sqlite3_syscall_ptr)0,          0  },
  3.4687 @@ -23716,7 +24665,7 @@
  3.4688    { "write",        (sqlite3_syscall_ptr)write,      0  },
  3.4689  #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
  3.4690  
  3.4691 -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  3.4692 +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
  3.4693    { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
  3.4694  #else
  3.4695    { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
  3.4696 @@ -23770,6 +24719,9 @@
  3.4697    { "mremap",       (sqlite3_syscall_ptr)0,               0 },
  3.4698  #endif
  3.4699  #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
  3.4700 +  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
  3.4701 +#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
  3.4702 +
  3.4703  #endif
  3.4704  
  3.4705  }; /* End of the overrideable system calls */
  3.4706 @@ -24083,16 +25035,6 @@
  3.4707    case EPERM: 
  3.4708      return SQLITE_PERM;
  3.4709      
  3.4710 -  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
  3.4711 -  ** this module never makes such a call. And the code in SQLite itself 
  3.4712 -  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
  3.4713 -  ** this case is also commented out. If the system does set errno to EDEADLK,
  3.4714 -  ** the default SQLITE_IOERR_XXX code will be returned. */
  3.4715 -#if 0
  3.4716 -  case EDEADLK:
  3.4717 -    return SQLITE_IOERR_BLOCKED;
  3.4718 -#endif
  3.4719 -    
  3.4720  #if EOPNOTSUPP!=ENOTSUP
  3.4721    case EOPNOTSUPP: 
  3.4722      /* something went terribly awry, unless during file system support 
  3.4723 @@ -24621,6 +25563,19 @@
  3.4724    return SQLITE_OK;
  3.4725  }
  3.4726  
  3.4727 +/*
  3.4728 +** Return TRUE if pFile has been renamed or unlinked since it was first opened.
  3.4729 +*/
  3.4730 +static int fileHasMoved(unixFile *pFile){
  3.4731 +#if OS_VXWORKS
  3.4732 +  return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
  3.4733 +#else
  3.4734 +  struct stat buf;
  3.4735 +  return pFile->pInode!=0 &&
  3.4736 +      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
  3.4737 +#endif
  3.4738 +}
  3.4739 +
  3.4740  
  3.4741  /*
  3.4742  ** Check a unixFile that is a database.  Verify the following:
  3.4743 @@ -24655,10 +25610,7 @@
  3.4744      pFile->ctrlFlags |= UNIXFILE_WARNED;
  3.4745      return;
  3.4746    }
  3.4747 -  if( pFile->pInode!=0
  3.4748 -   && ((rc = osStat(pFile->zPath, &buf))!=0
  3.4749 -       || buf.st_ino!=pFile->pInode->fileId.ino)
  3.4750 -  ){
  3.4751 +  if( fileHasMoved(pFile) ){
  3.4752      sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
  3.4753      pFile->ctrlFlags |= UNIXFILE_WARNED;
  3.4754      return;
  3.4755 @@ -25235,6 +26187,13 @@
  3.4756      pFile->pId = 0;
  3.4757    }
  3.4758  #endif
  3.4759 +#ifdef SQLITE_UNLINK_AFTER_CLOSE
  3.4760 +  if( pFile->ctrlFlags & UNIXFILE_DELETE ){
  3.4761 +    osUnlink(pFile->zPath);
  3.4762 +    sqlite3_free(*(char**)&pFile->zPath);
  3.4763 +    pFile->zPath = 0;
  3.4764 +  }
  3.4765 +#endif
  3.4766    OSTRACE(("CLOSE   %-3d\n", pFile->h));
  3.4767    OpenCounter(-1);
  3.4768    sqlite3_free(pFile->pUnused);
  3.4769 @@ -25757,7 +26716,6 @@
  3.4770    /* Otherwise see if some other process holds it. */
  3.4771    if( !reserved ){
  3.4772      sem_t *pSem = pFile->pInode->pSem;
  3.4773 -    struct stat statBuf;
  3.4774  
  3.4775      if( sem_trywait(pSem)==-1 ){
  3.4776        int tErrno = errno;
  3.4777 @@ -25810,7 +26768,6 @@
  3.4778  */
  3.4779  static int semLock(sqlite3_file *id, int eFileLock) {
  3.4780    unixFile *pFile = (unixFile*)id;
  3.4781 -  int fd;
  3.4782    sem_t *pSem = pFile->pInode->pSem;
  3.4783    int rc = SQLITE_OK;
  3.4784  
  3.4785 @@ -27107,6 +28064,10 @@
  3.4786        }
  3.4787        return SQLITE_OK;
  3.4788      }
  3.4789 +    case SQLITE_FCNTL_HAS_MOVED: {
  3.4790 +      *(int*)pArg = fileHasMoved(pFile);
  3.4791 +      return SQLITE_OK;
  3.4792 +    }
  3.4793  #if SQLITE_MAX_MMAP_SIZE>0
  3.4794      case SQLITE_FCNTL_MMAP_SIZE: {
  3.4795        i64 newLimit = *(i64*)pArg;
  3.4796 @@ -27269,9 +28230,26 @@
  3.4797    return rc;
  3.4798  }
  3.4799  
  3.4800 +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  3.4801 +
  3.4802 +/*
  3.4803 +** Return the system page size.
  3.4804 +**
  3.4805 +** This function should not be called directly by other code in this file. 
  3.4806 +** Instead, it should be called via macro osGetpagesize().
  3.4807 +*/
  3.4808 +static int unixGetpagesize(void){
  3.4809 +#if defined(_BSD_SOURCE)
  3.4810 +  return getpagesize();
  3.4811 +#else
  3.4812 +  return (int)sysconf(_SC_PAGESIZE);
  3.4813 +#endif
  3.4814 +}
  3.4815 +
  3.4816 +#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
  3.4817 +
  3.4818  #ifndef SQLITE_OMIT_WAL
  3.4819  
  3.4820 -
  3.4821  /*
  3.4822  ** Object used to represent an shared memory buffer.  
  3.4823  **
  3.4824 @@ -27387,7 +28365,7 @@
  3.4825  #ifdef SQLITE_DEBUG
  3.4826    { u16 mask;
  3.4827    OSTRACE(("SHM-LOCK "));
  3.4828 -  mask = ofst>31 ? 0xffffffff : (1<<(ofst+n)) - (1<<ofst);
  3.4829 +  mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
  3.4830    if( rc==SQLITE_OK ){
  3.4831      if( lockType==F_UNLCK ){
  3.4832        OSTRACE(("unlock %d ok", ofst));
  3.4833 @@ -27421,6 +28399,22 @@
  3.4834    return rc;        
  3.4835  }
  3.4836  
  3.4837 +/*
  3.4838 +** Return the minimum number of 32KB shm regions that should be mapped at
  3.4839 +** a time, assuming that each mapping must be an integer multiple of the
  3.4840 +** current system page-size.
  3.4841 +**
  3.4842 +** Usually, this is 1. The exception seems to be systems that are configured
  3.4843 +** to use 64KB pages - in this case each mapping must cover at least two
  3.4844 +** shm regions.
  3.4845 +*/
  3.4846 +static int unixShmRegionPerMap(void){
  3.4847 +  int shmsz = 32*1024;            /* SHM region size */
  3.4848 +  int pgsz = osGetpagesize();   /* System page size */
  3.4849 +  assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
  3.4850 +  if( pgsz<shmsz ) return 1;
  3.4851 +  return pgsz/shmsz;
  3.4852 +}
  3.4853  
  3.4854  /*
  3.4855  ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
  3.4856 @@ -27432,10 +28426,11 @@
  3.4857    unixShmNode *p = pFd->pInode->pShmNode;
  3.4858    assert( unixMutexHeld() );
  3.4859    if( p && p->nRef==0 ){
  3.4860 +    int nShmPerMap = unixShmRegionPerMap();
  3.4861      int i;
  3.4862      assert( p->pInode==pFd->pInode );
  3.4863      sqlite3_mutex_free(p->mutex);
  3.4864 -    for(i=0; i<p->nRegion; i++){
  3.4865 +    for(i=0; i<p->nRegion; i+=nShmPerMap){
  3.4866        if( p->h>=0 ){
  3.4867          osMunmap(p->apRegion[i], p->szRegion);
  3.4868        }else{
  3.4869 @@ -27642,6 +28637,8 @@
  3.4870    unixShm *p;
  3.4871    unixShmNode *pShmNode;
  3.4872    int rc = SQLITE_OK;
  3.4873 +  int nShmPerMap = unixShmRegionPerMap();
  3.4874 +  int nReqRegion;
  3.4875  
  3.4876    /* If the shared-memory file has not yet been opened, open it now. */
  3.4877    if( pDbFd->pShm==0 ){
  3.4878 @@ -27657,9 +28654,12 @@
  3.4879    assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
  3.4880    assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
  3.4881  
  3.4882 -  if( pShmNode->nRegion<=iRegion ){
  3.4883 +  /* Minimum number of regions required to be mapped. */
  3.4884 +  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
  3.4885 +
  3.4886 +  if( pShmNode->nRegion<nReqRegion ){
  3.4887      char **apNew;                      /* New apRegion[] array */
  3.4888 -    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
  3.4889 +    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
  3.4890      struct stat sStat;                 /* Used by fstat() */
  3.4891  
  3.4892      pShmNode->szRegion = szRegion;
  3.4893 @@ -27708,17 +28708,19 @@
  3.4894  
  3.4895      /* Map the requested memory region into this processes address space. */
  3.4896      apNew = (char **)sqlite3_realloc(
  3.4897 -        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
  3.4898 +        pShmNode->apRegion, nReqRegion*sizeof(char *)
  3.4899      );
  3.4900      if( !apNew ){
  3.4901        rc = SQLITE_IOERR_NOMEM;
  3.4902        goto shmpage_out;
  3.4903      }
  3.4904      pShmNode->apRegion = apNew;
  3.4905 -    while(pShmNode->nRegion<=iRegion){
  3.4906 +    while( pShmNode->nRegion<nReqRegion ){
  3.4907 +      int nMap = szRegion*nShmPerMap;
  3.4908 +      int i;
  3.4909        void *pMem;
  3.4910        if( pShmNode->h>=0 ){
  3.4911 -        pMem = osMmap(0, szRegion,
  3.4912 +        pMem = osMmap(0, nMap,
  3.4913              pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  3.4914              MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
  3.4915          );
  3.4916 @@ -27734,8 +28736,11 @@
  3.4917          }
  3.4918          memset(pMem, 0, szRegion);
  3.4919        }
  3.4920 -      pShmNode->apRegion[pShmNode->nRegion] = pMem;
  3.4921 -      pShmNode->nRegion++;
  3.4922 +
  3.4923 +      for(i=0; i<nShmPerMap; i++){
  3.4924 +        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
  3.4925 +      }
  3.4926 +      pShmNode->nRegion += nShmPerMap;
  3.4927      }
  3.4928    }
  3.4929  
  3.4930 @@ -27950,19 +28955,6 @@
  3.4931  }
  3.4932  
  3.4933  /*
  3.4934 -** Return the system page size.
  3.4935 -*/
  3.4936 -static int unixGetPagesize(void){
  3.4937 -#if HAVE_MREMAP
  3.4938 -  return 512;
  3.4939 -#elif defined(_BSD_SOURCE)
  3.4940 -  return getpagesize();
  3.4941 -#else
  3.4942 -  return (int)sysconf(_SC_PAGESIZE);
  3.4943 -#endif
  3.4944 -}
  3.4945 -
  3.4946 -/*
  3.4947  ** Attempt to set the size of the memory mapping maintained by file 
  3.4948  ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
  3.4949  **
  3.4950 @@ -27998,8 +28990,12 @@
  3.4951    if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
  3.4952  
  3.4953    if( pOrig ){
  3.4954 -    const int szSyspage = unixGetPagesize();
  3.4955 +#if HAVE_MREMAP
  3.4956 +    i64 nReuse = pFd->mmapSize;
  3.4957 +#else
  3.4958 +    const int szSyspage = osGetpagesize();
  3.4959      i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
  3.4960 +#endif
  3.4961      u8 *pReq = &pOrig[nReuse];
  3.4962  
  3.4963      /* Unmap any pages of the existing mapping that cannot be reused. */
  3.4964 @@ -28138,10 +29134,10 @@
  3.4965  ** may now be invalid and should be unmapped.
  3.4966  */
  3.4967  static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  3.4968 +#if SQLITE_MAX_MMAP_SIZE>0
  3.4969    unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  3.4970    UNUSED_PARAMETER(iOff);
  3.4971  
  3.4972 -#if SQLITE_MAX_MMAP_SIZE>0
  3.4973    /* If p==0 (unmap the entire file) then there must be no outstanding 
  3.4974    ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  3.4975    ** then there must be at least one outstanding.  */
  3.4976 @@ -28157,6 +29153,10 @@
  3.4977    }
  3.4978  
  3.4979    assert( pFd->nFetchOut>=0 );
  3.4980 +#else
  3.4981 +  UNUSED_PARAMETER(fd);
  3.4982 +  UNUSED_PARAMETER(p);
  3.4983 +  UNUSED_PARAMETER(iOff);
  3.4984  #endif
  3.4985    return SQLITE_OK;
  3.4986  }
  3.4987 @@ -28947,6 +29947,16 @@
  3.4988         || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  3.4989    );
  3.4990  
  3.4991 +  /* Detect a pid change and reset the PRNG.  There is a race condition
  3.4992 +  ** here such that two or more threads all trying to open databases at
  3.4993 +  ** the same instant might all reset the PRNG.  But multiple resets
  3.4994 +  ** are harmless.
  3.4995 +  */
  3.4996 +  if( randomnessPid!=getpid() ){
  3.4997 +    randomnessPid = getpid();
  3.4998 +    sqlite3_randomness(0,0);
  3.4999 +  }
  3.5000 +
  3.5001    memset(p, 0, sizeof(unixFile));
  3.5002  
  3.5003    if( eType==SQLITE_OPEN_MAIN_DB ){
  3.5004 @@ -29038,6 +30048,12 @@
  3.5005    if( isDelete ){
  3.5006  #if OS_VXWORKS
  3.5007      zPath = zName;
  3.5008 +#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
  3.5009 +    zPath = sqlite3_mprintf("%s", zName);
  3.5010 +    if( zPath==0 ){
  3.5011 +      robust_close(p, fd, __LINE__);
  3.5012 +      return SQLITE_NOMEM;
  3.5013 +    }
  3.5014  #else
  3.5015      osUnlink(zName);
  3.5016  #endif
  3.5017 @@ -29138,7 +30154,11 @@
  3.5018    UNUSED_PARAMETER(NotUsed);
  3.5019    SimulateIOError(return SQLITE_IOERR_DELETE);
  3.5020    if( osUnlink(zPath)==(-1) ){
  3.5021 -    if( errno==ENOENT ){
  3.5022 +    if( errno==ENOENT
  3.5023 +#if OS_VXWORKS
  3.5024 +        || errno==0x380003
  3.5025 +#endif
  3.5026 +    ){
  3.5027        rc = SQLITE_IOERR_DELETE_NOENT;
  3.5028      }else{
  3.5029        rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  3.5030 @@ -29334,18 +30354,18 @@
  3.5031    ** tests repeatable.
  3.5032    */
  3.5033    memset(zBuf, 0, nBuf);
  3.5034 +  randomnessPid = getpid();  
  3.5035  #if !defined(SQLITE_TEST)
  3.5036    {
  3.5037 -    int pid, fd, got;
  3.5038 +    int fd, got;
  3.5039      fd = robust_open("/dev/urandom", O_RDONLY, 0);
  3.5040      if( fd<0 ){
  3.5041        time_t t;
  3.5042        time(&t);
  3.5043        memcpy(zBuf, &t, sizeof(t));
  3.5044 -      pid = getpid();
  3.5045 -      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
  3.5046 -      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
  3.5047 -      nBuf = sizeof(t) + sizeof(pid);
  3.5048 +      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
  3.5049 +      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
  3.5050 +      nBuf = sizeof(t) + sizeof(randomnessPid);
  3.5051      }else{
  3.5052        do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
  3.5053        robust_close(0, fd, __LINE__);
  3.5054 @@ -30731,7 +31751,7 @@
  3.5055  
  3.5056    /* Double-check that the aSyscall[] array has been constructed
  3.5057    ** correctly.  See ticket [bb3a86e890c8e96ab] */
  3.5058 -  assert( ArraySize(aSyscall)==24 );
  3.5059 +  assert( ArraySize(aSyscall)==25 );
  3.5060  
  3.5061    /* Register all VFSes defined in the aVfs[] array */
  3.5062    for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  3.5063 @@ -30771,11 +31791,6 @@
  3.5064  */
  3.5065  #if SQLITE_OS_WIN               /* This file is used for Windows only */
  3.5066  
  3.5067 -#ifdef __CYGWIN__
  3.5068 -# include <sys/cygwin.h>
  3.5069 -# include <errno.h> /* amalgamator: keep */
  3.5070 -#endif
  3.5071 -
  3.5072  /*
  3.5073  ** Include code that is common to all os_*.c files
  3.5074  */
  3.5075 @@ -30990,6 +32005,10 @@
  3.5076  /************** Continuing where we left off in os_win.c *********************/
  3.5077  
  3.5078  /*
  3.5079 +** Include the header file for the Windows VFS.
  3.5080 +*/
  3.5081 +
  3.5082 +/*
  3.5083  ** Compiling and using WAL mode requires several APIs that are only
  3.5084  ** available in Windows platforms based on the NT kernel.
  3.5085  */
  3.5086 @@ -31036,18 +32055,14 @@
  3.5087  #endif
  3.5088  
  3.5089  /*
  3.5090 -** Check if the GetVersionEx[AW] functions should be considered deprecated
  3.5091 -** and avoid using them in that case.  It should be noted here that if the
  3.5092 -** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
  3.5093 -** (whether via this block or via being manually specified), that implies
  3.5094 -** the underlying operating system will always be based on the Windows NT
  3.5095 -** Kernel.
  3.5096 +** Check to see if the GetVersionEx[AW] functions are deprecated on the
  3.5097 +** target system.  GetVersionEx was first deprecated in Win8.1.
  3.5098  */
  3.5099  #ifndef SQLITE_WIN32_GETVERSIONEX
  3.5100  #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
  3.5101 -#    define SQLITE_WIN32_GETVERSIONEX   0
  3.5102 +#    define SQLITE_WIN32_GETVERSIONEX   0   /* GetVersionEx() is deprecated */
  3.5103  #  else
  3.5104 -#    define SQLITE_WIN32_GETVERSIONEX   1
  3.5105 +#    define SQLITE_WIN32_GETVERSIONEX   1   /* GetVersionEx() is current */
  3.5106  #  endif
  3.5107  #endif
  3.5108  
  3.5109 @@ -31119,7 +32134,7 @@
  3.5110  ** [sometimes] not used by the code (e.g. via conditional compilation).
  3.5111  */
  3.5112  #ifndef UNUSED_VARIABLE_VALUE
  3.5113 -#  define UNUSED_VARIABLE_VALUE(x) (void)(x)
  3.5114 +#  define UNUSED_VARIABLE_VALUE(x)      (void)(x)
  3.5115  #endif
  3.5116  
  3.5117  /*
  3.5118 @@ -31168,7 +32183,7 @@
  3.5119  ** Some Microsoft compilers lack this definition.
  3.5120  */
  3.5121  #ifndef INVALID_FILE_ATTRIBUTES
  3.5122 -# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
  3.5123 +# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
  3.5124  #endif
  3.5125  
  3.5126  #ifndef FILE_FLAG_MASK
  3.5127 @@ -31218,7 +32233,7 @@
  3.5128    int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
  3.5129  #if SQLITE_OS_WINCE
  3.5130    LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
  3.5131 -  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
  3.5132 +  HANDLE hMutex;          /* Mutex used to control access to shared lock */
  3.5133    HANDLE hShared;         /* Shared memory segment used for locking */
  3.5134    winceLock local;        /* Locks obtained by this instance of winFile */
  3.5135    winceLock *shared;      /* Global shared lock memory for the file  */
  3.5136 @@ -31378,10 +32393,9 @@
  3.5137  ** can manually set this value to 1 to emulate Win98 behavior.
  3.5138  */
  3.5139  #ifdef SQLITE_TEST
  3.5140 -SQLITE_API int sqlite3_os_type = 0;
  3.5141 -#elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
  3.5142 -      defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
  3.5143 -static int sqlite3_os_type = 0;
  3.5144 +SQLITE_API LONG volatile sqlite3_os_type = 0;
  3.5145 +#else
  3.5146 +static LONG volatile sqlite3_os_type = 0;
  3.5147  #endif
  3.5148  
  3.5149  #ifndef SYSCALL
  3.5150 @@ -32012,6 +33026,22 @@
  3.5151  #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
  3.5152          LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
  3.5153  
  3.5154 +/*
  3.5155 +** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
  3.5156 +**       is really just a macro that uses a compiler intrinsic (e.g. x64).
  3.5157 +**       So do not try to make this is into a redefinable interface.
  3.5158 +*/
  3.5159 +#if defined(InterlockedCompareExchange)
  3.5160 +  { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
  3.5161 +
  3.5162 +#define osInterlockedCompareExchange InterlockedCompareExchange
  3.5163 +#else
  3.5164 +  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
  3.5165 +
  3.5166 +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
  3.5167 +        LONG,LONG))aSyscall[76].pCurrent)
  3.5168 +#endif /* defined(InterlockedCompareExchange) */
  3.5169 +
  3.5170  }; /* End of the overrideable system calls */
  3.5171  
  3.5172  /*
  3.5173 @@ -32262,22 +33292,38 @@
  3.5174  #elif !defined(SQLITE_WIN32_HAS_WIDE)
  3.5175  # define osIsNT()  (0)
  3.5176  #else
  3.5177 -  static int osIsNT(void){
  3.5178 -    if( sqlite3_os_type==0 ){
  3.5179 -#if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  3.5180 -      OSVERSIONINFOW sInfo;
  3.5181 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  3.5182 -      osGetVersionExW(&sInfo);
  3.5183 -#else
  3.5184 -      OSVERSIONINFOA sInfo;
  3.5185 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  3.5186 -      osGetVersionExA(&sInfo);
  3.5187 -#endif
  3.5188 -      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  3.5189 -    }
  3.5190 -    return sqlite3_os_type==2;
  3.5191 -  }
  3.5192 -#endif
  3.5193 +# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
  3.5194 +#endif
  3.5195 +
  3.5196 +/*
  3.5197 +** This function determines if the machine is running a version of Windows
  3.5198 +** based on the NT kernel.
  3.5199 +*/
  3.5200 +SQLITE_API int sqlite3_win32_is_nt(void){
  3.5201 +#if defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
  3.5202 +  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
  3.5203 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
  3.5204 +        defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  3.5205 +    OSVERSIONINFOW sInfo;
  3.5206 +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  3.5207 +    osGetVersionExW(&sInfo);
  3.5208 +    osInterlockedCompareExchange(&sqlite3_os_type,
  3.5209 +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
  3.5210 +#elif defined(SQLITE_WIN32_HAS_ANSI)
  3.5211 +    OSVERSIONINFOA sInfo;
  3.5212 +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  3.5213 +    osGetVersionExA(&sInfo);
  3.5214 +    osInterlockedCompareExchange(&sqlite3_os_type,
  3.5215 +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
  3.5216 +#endif
  3.5217 +  }
  3.5218 +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
  3.5219 +#elif SQLITE_TEST
  3.5220 +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
  3.5221 +#else
  3.5222 +  return 1;
  3.5223 +#endif
  3.5224 +}
  3.5225  
  3.5226  #ifdef SQLITE_WIN32_MALLOC
  3.5227  /*
  3.5228 @@ -32485,7 +33531,7 @@
  3.5229  #endif /* SQLITE_WIN32_MALLOC */
  3.5230  
  3.5231  /*
  3.5232 -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
  3.5233 +** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  3.5234  **
  3.5235  ** Space to hold the returned string is obtained from malloc.
  3.5236  */
  3.5237 @@ -32538,7 +33584,7 @@
  3.5238  /*
  3.5239  ** Convert an ANSI string to Microsoft Unicode, based on the
  3.5240  ** current codepage settings for file apis.
  3.5241 -** 
  3.5242 +**
  3.5243  ** Space to hold the returned string is obtained
  3.5244  ** from sqlite3_malloc.
  3.5245  */
  3.5246 @@ -32612,7 +33658,7 @@
  3.5247  }
  3.5248  
  3.5249  /*
  3.5250 -** Convert UTF-8 to multibyte character string.  Space to hold the 
  3.5251 +** Convert UTF-8 to multibyte character string.  Space to hold the
  3.5252  ** returned string is obtained from sqlite3_malloc().
  3.5253  */
  3.5254  SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  3.5255 @@ -32752,11 +33798,11 @@
  3.5256  **
  3.5257  ** This routine is invoked after an error occurs in an OS function.
  3.5258  ** It logs a message using sqlite3_log() containing the current value of
  3.5259 -** error code and, if possible, the human-readable equivalent from 
  3.5260 +** error code and, if possible, the human-readable equivalent from
  3.5261  ** FormatMessage.
  3.5262  **
  3.5263  ** The first argument passed to the macro should be the error code that
  3.5264 -** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
  3.5265 +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
  3.5266  ** The two subsequent arguments should be the name of the OS function that
  3.5267  ** failed and the associated file-system path, if any.
  3.5268  */
  3.5269 @@ -32787,7 +33833,7 @@
  3.5270  
  3.5271  /*
  3.5272  ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
  3.5273 -** will be retried following a locking error - probably caused by 
  3.5274 +** will be retried following a locking error - probably caused by
  3.5275  ** antivirus software.  Also the initial delay before the first retry.
  3.5276  ** The delay increases linearly with each retry.
  3.5277  */
  3.5278 @@ -32801,6 +33847,32 @@
  3.5279  static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
  3.5280  
  3.5281  /*
  3.5282 +** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
  3.5283 +** error code obtained via GetLastError() is eligible to be retried.  It
  3.5284 +** must accept the error code DWORD as its only argument and should return
  3.5285 +** non-zero if the error code is transient in nature and the operation
  3.5286 +** responsible for generating the original error might succeed upon being
  3.5287 +** retried.  The argument to this macro should be a variable.
  3.5288 +**
  3.5289 +** Additionally, a macro named "winIoerrCanRetry2" may be defined.  If it
  3.5290 +** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
  3.5291 +** returns zero.  The "winIoerrCanRetry2" macro is completely optional and
  3.5292 +** may be used to include additional error codes in the set that should
  3.5293 +** result in the failing I/O operation being retried by the caller.  If
  3.5294 +** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
  3.5295 +** identical to those of the "winIoerrCanRetry1" macro.
  3.5296 +*/
  3.5297 +#if !defined(winIoerrCanRetry1)
  3.5298 +#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED)        || \
  3.5299 +                              ((a)==ERROR_SHARING_VIOLATION)    || \
  3.5300 +                              ((a)==ERROR_LOCK_VIOLATION)       || \
  3.5301 +                              ((a)==ERROR_DEV_NOT_EXIST)        || \
  3.5302 +                              ((a)==ERROR_NETNAME_DELETED)      || \
  3.5303 +                              ((a)==ERROR_SEM_TIMEOUT)          || \
  3.5304 +                              ((a)==ERROR_NETWORK_UNREACHABLE))
  3.5305 +#endif
  3.5306 +
  3.5307 +/*
  3.5308  ** If a ReadFile() or WriteFile() error occurs, invoke this routine
  3.5309  ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
  3.5310  ** to give up with an error.
  3.5311 @@ -32813,13 +33885,18 @@
  3.5312      }
  3.5313      return 0;
  3.5314    }
  3.5315 -  if( e==ERROR_ACCESS_DENIED ||
  3.5316 -      e==ERROR_LOCK_VIOLATION ||
  3.5317 -      e==ERROR_SHARING_VIOLATION ){
  3.5318 +  if( winIoerrCanRetry1(e) ){
  3.5319      sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
  3.5320      ++*pnRetry;
  3.5321      return 1;
  3.5322    }
  3.5323 +#if defined(winIoerrCanRetry2)
  3.5324 +  else if( winIoerrCanRetry2(e) ){
  3.5325 +    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
  3.5326 +    ++*pnRetry;
  3.5327 +    return 1;
  3.5328 +  }
  3.5329 +#endif
  3.5330    if( pError ){
  3.5331      *pError = e;
  3.5332    }
  3.5333 @@ -32831,7 +33908,7 @@
  3.5334  */
  3.5335  static void winLogIoerr(int nRetry){
  3.5336    if( nRetry ){
  3.5337 -    sqlite3_log(SQLITE_IOERR, 
  3.5338 +    sqlite3_log(SQLITE_IOERR,
  3.5339        "delayed %dms for lock/sharing conflict",
  3.5340        winIoerrRetryDelay*nRetry*(nRetry+1)/2
  3.5341      );
  3.5342 @@ -32925,17 +34002,17 @@
  3.5343  
  3.5344    /* Acquire the mutex before continuing */
  3.5345    winceMutexAcquire(pFile->hMutex);
  3.5346 -  
  3.5347 -  /* Since the names of named mutexes, semaphores, file mappings etc are 
  3.5348 +
  3.5349 +  /* Since the names of named mutexes, semaphores, file mappings etc are
  3.5350    ** case-sensitive, take advantage of that by uppercasing the mutex name
  3.5351    ** and using that as the shared filemapping name.
  3.5352    */
  3.5353    osCharUpperW(zName);
  3.5354    pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  3.5355                                          PAGE_READWRITE, 0, sizeof(winceLock),
  3.5356 -                                        zName);  
  3.5357 -
  3.5358 -  /* Set a flag that indicates we're the first to create the memory so it 
  3.5359 +                                        zName);
  3.5360 +
  3.5361 +  /* Set a flag that indicates we're the first to create the memory so it
  3.5362    ** must be zero-initialized */
  3.5363    lastErrno = osGetLastError();
  3.5364    if (lastErrno == ERROR_ALREADY_EXISTS){
  3.5365 @@ -32946,7 +34023,7 @@
  3.5366  
  3.5367    /* If we succeeded in making the shared memory handle, map it. */
  3.5368    if( pFile->hShared ){
  3.5369 -    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
  3.5370 +    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
  3.5371               FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
  3.5372      /* If mapping failed, close the shared memory handle and erase it */
  3.5373      if( !pFile->shared ){
  3.5374 @@ -32972,7 +34049,7 @@
  3.5375      pFile->hMutex = NULL;
  3.5376      return SQLITE_IOERR;
  3.5377    }
  3.5378 -  
  3.5379 +
  3.5380    /* Initialize the shared memory if we're supposed to */
  3.5381    if( bInit ){
  3.5382      memset(pFile->shared, 0, sizeof(winceLock));
  3.5383 @@ -33010,13 +34087,13 @@
  3.5384      osCloseHandle(pFile->hShared);
  3.5385  
  3.5386      /* Done with the mutex */
  3.5387 -    winceMutexRelease(pFile->hMutex);    
  3.5388 +    winceMutexRelease(pFile->hMutex);
  3.5389      osCloseHandle(pFile->hMutex);
  3.5390      pFile->hMutex = NULL;
  3.5391    }
  3.5392  }
  3.5393  
  3.5394 -/* 
  3.5395 +/*
  3.5396  ** An implementation of the LockFile() API of Windows for CE
  3.5397  */
  3.5398  static BOOL winceLockFile(
  3.5399 @@ -33227,8 +34304,8 @@
  3.5400  #endif
  3.5401  
  3.5402  /*
  3.5403 -** Move the current position of the file handle passed as the first 
  3.5404 -** argument to offset iOffset within the file. If successful, return 0. 
  3.5405 +** Move the current position of the file handle passed as the first
  3.5406 +** argument to offset iOffset within the file. If successful, return 0.
  3.5407  ** Otherwise, set pFile->lastErrno and return non-zero.
  3.5408  */
  3.5409  static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
  3.5410 @@ -33243,11 +34320,11 @@
  3.5411    upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
  3.5412    lowerBits = (LONG)(iOffset & 0xffffffff);
  3.5413  
  3.5414 -  /* API oddity: If successful, SetFilePointer() returns a dword 
  3.5415 +  /* API oddity: If successful, SetFilePointer() returns a dword
  3.5416    ** containing the lower 32-bits of the new file-offset. Or, if it fails,
  3.5417 -  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
  3.5418 -  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
  3.5419 -  ** whether an error has actually occurred, it is also necessary to call 
  3.5420 +  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
  3.5421 +  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
  3.5422 +  ** whether an error has actually occurred, it is also necessary to call
  3.5423    ** GetLastError().
  3.5424    */
  3.5425    dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
  3.5426 @@ -33330,7 +34407,7 @@
  3.5427      int cnt = 0;
  3.5428      while(
  3.5429             osDeleteFileW(pFile->zDeleteOnClose)==0
  3.5430 -        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
  3.5431 +        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
  3.5432          && cnt++ < WINCE_DELETION_ATTEMPTS
  3.5433      ){
  3.5434         sqlite3_win32_sleep(100);  /* Wait a little before trying again */
  3.5435 @@ -33758,7 +34835,7 @@
  3.5436      pFile->lastErrno = osGetLastError();
  3.5437      /* No need to log a failure to lock */
  3.5438    }
  3.5439 -  OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  3.5440 +  OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
  3.5441    return res;
  3.5442  }
  3.5443  
  3.5444 @@ -33782,7 +34859,7 @@
  3.5445      winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
  3.5446                  "winUnlockReadLock", pFile->zPath);
  3.5447    }
  3.5448 -  OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  3.5449 +  OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
  3.5450    return res;
  3.5451  }
  3.5452  
  3.5453 @@ -33857,8 +34934,16 @@
  3.5454        ** If you are using this code as a model for alternative VFSes, do not
  3.5455        ** copy this retry logic.  It is a hack intended for Windows only.
  3.5456        */
  3.5457 -      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
  3.5458 -               pFile->h, cnt, sqlite3ErrName(res)));
  3.5459 +      lastErrno = osGetLastError();
  3.5460 +      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
  3.5461 +               pFile->h, cnt, res));
  3.5462 +      if( lastErrno==ERROR_INVALID_HANDLE ){
  3.5463 +        pFile->lastErrno = lastErrno;
  3.5464 +        rc = SQLITE_IOERR_LOCK;
  3.5465 +        OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
  3.5466 +                 pFile->h, cnt, sqlite3ErrName(rc)));
  3.5467 +        return rc;
  3.5468 +      }
  3.5469        if( cnt ) sqlite3_win32_sleep(1);
  3.5470      }
  3.5471      gotPendingLock = res;
  3.5472 @@ -33943,7 +35028,7 @@
  3.5473  ** non-zero, otherwise zero.
  3.5474  */
  3.5475  static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
  3.5476 -  int rc;
  3.5477 +  int res;
  3.5478    winFile *pFile = (winFile*)id;
  3.5479  
  3.5480    SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  3.5481 @@ -33951,17 +35036,17 @@
  3.5482  
  3.5483    assert( id!=0 );
  3.5484    if( pFile->locktype>=RESERVED_LOCK ){
  3.5485 -    rc = 1;
  3.5486 -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
  3.5487 -  }else{
  3.5488 -    rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
  3.5489 -    if( rc ){
  3.5490 +    res = 1;
  3.5491 +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
  3.5492 +  }else{
  3.5493 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
  3.5494 +    if( res ){
  3.5495        winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
  3.5496      }
  3.5497 -    rc = !rc;
  3.5498 -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
  3.5499 -  }
  3.5500 -  *pResOut = rc;
  3.5501 +    res = !res;
  3.5502 +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
  3.5503 +  }
  3.5504 +  *pResOut = res;
  3.5505    OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
  3.5506             pFile->h, pResOut, *pResOut));
  3.5507    return SQLITE_OK;
  3.5508 @@ -34083,7 +35168,7 @@
  3.5509        return SQLITE_OK;
  3.5510      }
  3.5511      case SQLITE_FCNTL_VFSNAME: {
  3.5512 -      *(char**)pArg = sqlite3_mprintf("win32");
  3.5513 +      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  3.5514        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  3.5515        return SQLITE_OK;
  3.5516      }
  3.5517 @@ -34102,6 +35187,17 @@
  3.5518        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  3.5519        return SQLITE_OK;
  3.5520      }
  3.5521 +#ifdef SQLITE_TEST
  3.5522 +    case SQLITE_FCNTL_WIN32_SET_HANDLE: {
  3.5523 +      LPHANDLE phFile = (LPHANDLE)pArg;
  3.5524 +      HANDLE hOldFile = pFile->h;
  3.5525 +      pFile->h = *phFile;
  3.5526 +      *phFile = hOldFile;
  3.5527 +      OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
  3.5528 +               hOldFile, pFile->h));
  3.5529 +      return SQLITE_OK;
  3.5530 +    }
  3.5531 +#endif
  3.5532      case SQLITE_FCNTL_TEMPFILENAME: {
  3.5533        char *zTFile = 0;
  3.5534        int rc = winGetTempname(pFile->pVfs, &zTFile);
  3.5535 @@ -34159,23 +35255,23 @@
  3.5536           ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
  3.5537  }
  3.5538  
  3.5539 -/* 
  3.5540 +/*
  3.5541  ** Windows will only let you create file view mappings
  3.5542  ** on allocation size granularity boundaries.
  3.5543  ** During sqlite3_os_init() we do a GetSystemInfo()
  3.5544  ** to get the granularity size.
  3.5545  */
  3.5546 -SYSTEM_INFO winSysInfo;
  3.5547 +static SYSTEM_INFO winSysInfo;
  3.5548  
  3.5549  #ifndef SQLITE_OMIT_WAL
  3.5550  
  3.5551  /*
  3.5552  ** Helper functions to obtain and relinquish the global mutex. The
  3.5553 -** global mutex is used to protect the winLockInfo objects used by 
  3.5554 +** global mutex is used to protect the winLockInfo objects used by
  3.5555  ** this file, all of which may be shared by multiple threads.
  3.5556  **
  3.5557 -** Function winShmMutexHeld() is used to assert() that the global mutex 
  3.5558 -** is held when required. This function is only used as part of assert() 
  3.5559 +** Function winShmMutexHeld() is used to assert() that the global mutex
  3.5560 +** is held when required. This function is only used as part of assert()
  3.5561  ** statements. e.g.
  3.5562  **
  3.5563  **   winShmEnterMutex()
  3.5564 @@ -34188,7 +35284,7 @@
  3.5565  static void winShmLeaveMutex(void){
  3.5566    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  3.5567  }
  3.5568 -#ifdef SQLITE_DEBUG
  3.5569 +#ifndef NDEBUG
  3.5570  static int winShmMutexHeld(void) {
  3.5571    return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  3.5572  }
  3.5573 @@ -34205,10 +35301,10 @@
  3.5574  ** this object or while reading or writing the following fields:
  3.5575  **
  3.5576  **      nRef
  3.5577 -**      pNext 
  3.5578 +**      pNext
  3.5579  **
  3.5580  ** The following fields are read-only after the object is created:
  3.5581 -** 
  3.5582 +**
  3.5583  **      fid
  3.5584  **      zFilename
  3.5585  **
  3.5586 @@ -34304,7 +35400,7 @@
  3.5587      if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
  3.5588      rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
  3.5589    }
  3.5590 -  
  3.5591 +
  3.5592    if( rc!= 0 ){
  3.5593      rc = SQLITE_OK;
  3.5594    }else{
  3.5595 @@ -34400,7 +35496,7 @@
  3.5596    }
  3.5597    pNew->zFilename = (char*)&pNew[1];
  3.5598    sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  3.5599 -  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  3.5600 +  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
  3.5601  
  3.5602    /* Look to see if there is an existing winShmNode that can be used.
  3.5603    ** If no matching winShmNode currently exists, create a new one.
  3.5604 @@ -34437,7 +35533,7 @@
  3.5605      }
  3.5606  
  3.5607      /* Check to see if another process is holding the dead-man switch.
  3.5608 -    ** If not, truncate the file to zero length. 
  3.5609 +    ** If not, truncate the file to zero length.
  3.5610      */
  3.5611      if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
  3.5612        rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
  3.5613 @@ -34466,7 +35562,7 @@
  3.5614    ** the cover of the winShmEnterMutex() mutex and the pointer from the
  3.5615    ** new (struct winShm) object to the pShmNode has been set. All that is
  3.5616    ** left to do is to link the new object into the linked list starting
  3.5617 -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
  3.5618 +  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
  3.5619    ** mutex.
  3.5620    */
  3.5621    sqlite3_mutex_enter(pShmNode->mutex);
  3.5622 @@ -34486,7 +35582,7 @@
  3.5623  }
  3.5624  
  3.5625  /*
  3.5626 -** Close a connection to shared-memory.  Delete the underlying 
  3.5627 +** Close a connection to shared-memory.  Delete the underlying
  3.5628  ** storage if deleteFlag is true.
  3.5629  */
  3.5630  static int winShmUnmap(
  3.5631 @@ -34575,7 +35671,7 @@
  3.5632      if( rc==SQLITE_OK ){
  3.5633        p->exclMask &= ~mask;
  3.5634        p->sharedMask &= ~mask;
  3.5635 -    } 
  3.5636 +    }
  3.5637    }else if( flags & SQLITE_SHM_SHARED ){
  3.5638      u16 allShared = 0;  /* Union of locks held by connections other than "p" */
  3.5639  
  3.5640 @@ -34614,7 +35710,7 @@
  3.5641          break;
  3.5642        }
  3.5643      }
  3.5644 -  
  3.5645 +
  3.5646      /* Get the exclusive locks at the system level.  Then if successful
  3.5647      ** also mark the local connection as being locked.
  3.5648      */
  3.5649 @@ -34634,7 +35730,7 @@
  3.5650  }
  3.5651  
  3.5652  /*
  3.5653 -** Implement a memory barrier or memory fence on shared memory.  
  3.5654 +** Implement a memory barrier or memory fence on shared memory.
  3.5655  **
  3.5656  ** All loads and stores begun before the barrier must complete before
  3.5657  ** any load or store begun after the barrier.
  3.5658 @@ -34649,22 +35745,22 @@
  3.5659  }
  3.5660  
  3.5661  /*
  3.5662 -** This function is called to obtain a pointer to region iRegion of the 
  3.5663 -** shared-memory associated with the database file fd. Shared-memory regions 
  3.5664 -** are numbered starting from zero. Each shared-memory region is szRegion 
  3.5665 +** This function is called to obtain a pointer to region iRegion of the
  3.5666 +** shared-memory associated with the database file fd. Shared-memory regions
  3.5667 +** are numbered starting from zero. Each shared-memory region is szRegion
  3.5668  ** bytes in size.
  3.5669  **
  3.5670  ** If an error occurs, an error code is returned and *pp is set to NULL.
  3.5671  **
  3.5672  ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
  3.5673  ** region has not been allocated (by any client, including one running in a
  3.5674 -** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
  3.5675 -** isWrite is non-zero and the requested shared-memory region has not yet 
  3.5676 +** separate process), then *pp is set to NULL and SQLITE_OK returned. If
  3.5677 +** isWrite is non-zero and the requested shared-memory region has not yet
  3.5678  ** been allocated, it is allocated by this function.
  3.5679  **
  3.5680  ** If the shared-memory region has already been allocated or is allocated by
  3.5681 -** this call as described above, then it is mapped into this processes 
  3.5682 -** address space (if it is not already), *pp is set to point to the mapped 
  3.5683 +** this call as described above, then it is mapped into this processes
  3.5684 +** address space (if it is not already), *pp is set to point to the mapped
  3.5685  ** memory and SQLITE_OK returned.
  3.5686  */
  3.5687  static int winShmMap(
  3.5688 @@ -34736,17 +35832,17 @@
  3.5689      while( pShmNode->nRegion<=iRegion ){
  3.5690        HANDLE hMap = NULL;         /* file-mapping handle */
  3.5691        void *pMap = 0;             /* Mapped memory region */
  3.5692 -     
  3.5693 +
  3.5694  #if SQLITE_OS_WINRT
  3.5695        hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  3.5696            NULL, PAGE_READWRITE, nByte, NULL
  3.5697        );
  3.5698  #elif defined(SQLITE_WIN32_HAS_WIDE)
  3.5699 -      hMap = osCreateFileMappingW(pShmNode->hFile.h, 
  3.5700 +      hMap = osCreateFileMappingW(pShmNode->hFile.h,
  3.5701            NULL, PAGE_READWRITE, 0, nByte, NULL
  3.5702        );
  3.5703  #elif defined(SQLITE_WIN32_HAS_ANSI)
  3.5704 -      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
  3.5705 +      hMap = osCreateFileMappingA(pShmNode->hFile.h,
  3.5706            NULL, PAGE_READWRITE, 0, nByte, NULL
  3.5707        );
  3.5708  #endif
  3.5709 @@ -34843,14 +35939,14 @@
  3.5710  
  3.5711  /*
  3.5712  ** Memory map or remap the file opened by file-descriptor pFd (if the file
  3.5713 -** is already mapped, the existing mapping is replaced by the new). Or, if 
  3.5714 -** there already exists a mapping for this file, and there are still 
  3.5715 +** is already mapped, the existing mapping is replaced by the new). Or, if
  3.5716 +** there already exists a mapping for this file, and there are still
  3.5717  ** outstanding xFetch() references to it, this function is a no-op.
  3.5718  **
  3.5719 -** If parameter nByte is non-negative, then it is the requested size of 
  3.5720 -** the mapping to create. Otherwise, if nByte is less than zero, then the 
  3.5721 +** If parameter nByte is non-negative, then it is the requested size of
  3.5722 +** the mapping to create. Otherwise, if nByte is less than zero, then the
  3.5723  ** requested size is the size of the file on disk. The actual size of the
  3.5724 -** created mapping is either the requested size or the value configured 
  3.5725 +** created mapping is either the requested size or the value configured
  3.5726  ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
  3.5727  **
  3.5728  ** SQLITE_OK is returned if no error occurs (even if the mapping is not
  3.5729 @@ -34879,7 +35975,7 @@
  3.5730      nMap = pFd->mmapSizeMax;
  3.5731    }
  3.5732    nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
  3.5733 - 
  3.5734 +
  3.5735    if( nMap==0 && pFd->mmapSize>0 ){
  3.5736      winUnmapfile(pFd);
  3.5737    }
  3.5738 @@ -34951,7 +36047,7 @@
  3.5739  ** Finally, if an error does occur, return an SQLite error code. The final
  3.5740  ** value of *pp is undefined in this case.
  3.5741  **
  3.5742 -** If this function does return a pointer, the caller must eventually 
  3.5743 +** If this function does return a pointer, the caller must eventually
  3.5744  ** release the reference by calling winUnfetch().
  3.5745  */
  3.5746  static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
  3.5747 @@ -34986,20 +36082,20 @@
  3.5748  }
  3.5749  
  3.5750  /*
  3.5751 -** If the third argument is non-NULL, then this function releases a 
  3.5752 +** If the third argument is non-NULL, then this function releases a
  3.5753  ** reference obtained by an earlier call to winFetch(). The second
  3.5754  ** argument passed to this function must be the same as the corresponding
  3.5755 -** argument that was passed to the winFetch() invocation. 
  3.5756 -**
  3.5757 -** Or, if the third argument is NULL, then this function is being called 
  3.5758 -** to inform the VFS layer that, according to POSIX, any existing mapping 
  3.5759 +** argument that was passed to the winFetch() invocation.
  3.5760 +**
  3.5761 +** Or, if the third argument is NULL, then this function is being called
  3.5762 +** to inform the VFS layer that, according to POSIX, any existing mapping
  3.5763  ** may now be invalid and should be unmapped.
  3.5764  */
  3.5765  static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  3.5766  #if SQLITE_MAX_MMAP_SIZE>0
  3.5767    winFile *pFd = (winFile*)fd;   /* The underlying database file */
  3.5768  
  3.5769 -  /* If p==0 (unmap the entire file) then there must be no outstanding 
  3.5770 +  /* If p==0 (unmap the entire file) then there must be no outstanding
  3.5771    ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  3.5772    ** then there must be at least one outstanding.  */
  3.5773    assert( (p==0)==(pFd->nFetchOut==0) );
  3.5774 @@ -35145,7 +36241,7 @@
  3.5775  
  3.5776    /* It's odd to simulate an io-error here, but really this is just
  3.5777    ** using the io-error infrastructure to test that SQLite handles this
  3.5778 -  ** function failing. 
  3.5779 +  ** function failing.
  3.5780    */
  3.5781    SimulateIOError( return SQLITE_IOERR );
  3.5782  
  3.5783 @@ -35327,7 +36423,7 @@
  3.5784    }
  3.5785  
  3.5786    /*
  3.5787 -  ** Check that the output buffer is large enough for the temporary file 
  3.5788 +  ** Check that the output buffer is large enough for the temporary file
  3.5789    ** name in the following format:
  3.5790    **
  3.5791    **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
  3.5792 @@ -35430,8 +36526,8 @@
  3.5793  
  3.5794  #ifndef NDEBUG
  3.5795    int isOpenJournal = (isCreate && (
  3.5796 -        eType==SQLITE_OPEN_MASTER_JOURNAL 
  3.5797 -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
  3.5798 +        eType==SQLITE_OPEN_MASTER_JOURNAL
  3.5799 +     || eType==SQLITE_OPEN_MAIN_JOURNAL
  3.5800       || eType==SQLITE_OPEN_WAL
  3.5801    ));
  3.5802  #endif
  3.5803 @@ -35439,9 +36535,9 @@
  3.5804    OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
  3.5805             zUtf8Name, id, flags, pOutFlags));
  3.5806  
  3.5807 -  /* Check the following statements are true: 
  3.5808 -  **
  3.5809 -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  3.5810 +  /* Check the following statements are true:
  3.5811 +  **
  3.5812 +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
  3.5813    **   (b) if CREATE is set, then READWRITE must also be set, and
  3.5814    **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  3.5815    **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  3.5816 @@ -35451,7 +36547,7 @@
  3.5817    assert(isExclusive==0 || isCreate);
  3.5818    assert(isDelete==0 || isCreate);
  3.5819  
  3.5820 -  /* The main DB, main journal, WAL file and master journal are never 
  3.5821 +  /* The main DB, main journal, WAL file and master journal are never
  3.5822    ** automatically deleted. Nor are they ever temporary files.  */
  3.5823    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  3.5824    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  3.5825 @@ -35459,9 +36555,9 @@
  3.5826    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  3.5827  
  3.5828    /* Assert that the upper layer has set one of the "file-type" flags. */
  3.5829 -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  3.5830 -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  3.5831 -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  3.5832 +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
  3.5833 +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
  3.5834 +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
  3.5835         || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  3.5836    );
  3.5837  
  3.5838 @@ -35476,8 +36572,8 @@
  3.5839    }
  3.5840  #endif
  3.5841  
  3.5842 -  /* If the second argument to this function is NULL, generate a 
  3.5843 -  ** temporary file name to use 
  3.5844 +  /* If the second argument to this function is NULL, generate a
  3.5845 +  ** temporary file name to use
  3.5846    */
  3.5847    if( !zUtf8Name ){
  3.5848      assert( isDelete && !isOpenJournal );
  3.5849 @@ -35517,8 +36613,8 @@
  3.5850      dwDesiredAccess = GENERIC_READ;
  3.5851    }
  3.5852  
  3.5853 -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
  3.5854 -  ** created. SQLite doesn't use it to indicate "exclusive access" 
  3.5855 +  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
  3.5856 +  ** created. SQLite doesn't use it to indicate "exclusive access"
  3.5857    ** as it is usually understood.
  3.5858    */
  3.5859    if( isExclusive ){
  3.5860 @@ -35607,7 +36703,7 @@
  3.5861      sqlite3_free(zConverted);
  3.5862      sqlite3_free(zTmpname);
  3.5863      if( isReadWrite && !isExclusive ){
  3.5864 -      return winOpen(pVfs, zName, id, 
  3.5865 +      return winOpen(pVfs, zName, id,
  3.5866           ((flags|SQLITE_OPEN_READONLY) &
  3.5867                       ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  3.5868           pOutFlags);
  3.5869 @@ -35816,14 +36912,14 @@
  3.5870      WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  3.5871      memset(&sAttrData, 0, sizeof(sAttrData));
  3.5872      while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  3.5873 -                             GetFileExInfoStandard, 
  3.5874 +                             GetFileExInfoStandard,
  3.5875                               &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
  3.5876      if( rc ){
  3.5877        /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  3.5878        ** as if it does not exist.
  3.5879        */
  3.5880        if(    flags==SQLITE_ACCESS_EXISTS
  3.5881 -          && sAttrData.nFileSizeHigh==0 
  3.5882 +          && sAttrData.nFileSizeHigh==0
  3.5883            && sAttrData.nFileSizeLow==0 ){
  3.5884          attr = INVALID_FILE_ATTRIBUTES;
  3.5885        }else{
  3.5886 @@ -35922,7 +37018,7 @@
  3.5887    int nFull,                    /* Size of output buffer in bytes */
  3.5888    char *zFull                   /* Output buffer */
  3.5889  ){
  3.5890 -  
  3.5891 +
  3.5892  #if defined(__CYGWIN__)
  3.5893    SimulateIOError( return SQLITE_ERROR );
  3.5894    UNUSED_PARAMETER(nFull);
  3.5895 @@ -36099,15 +37195,29 @@
  3.5896  ** Interfaces for opening a shared library, finding entry points
  3.5897  ** within the shared library, and closing the shared library.
  3.5898  */
  3.5899 -/*
  3.5900 -** Interfaces for opening a shared library, finding entry points
  3.5901 -** within the shared library, and closing the shared library.
  3.5902 -*/
  3.5903  static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  3.5904    HANDLE h;
  3.5905 +#if defined(__CYGWIN__)
  3.5906 +  int nFull = pVfs->mxPathname+1;
  3.5907 +  char *zFull = sqlite3MallocZero( nFull );
  3.5908 +  void *zConverted = 0;
  3.5909 +  if( zFull==0 ){
  3.5910 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  3.5911 +    return 0;
  3.5912 +  }
  3.5913 +  if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
  3.5914 +    sqlite3_free(zFull);
  3.5915 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  3.5916 +    return 0;
  3.5917 +  }
  3.5918 +  zConverted = winConvertFromUtf8Filename(zFull);
  3.5919 +  sqlite3_free(zFull);
  3.5920 +#else
  3.5921    void *zConverted = winConvertFromUtf8Filename(zFilename);
  3.5922    UNUSED_PARAMETER(pVfs);
  3.5923 +#endif
  3.5924    if( zConverted==0 ){
  3.5925 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  3.5926      return 0;
  3.5927    }
  3.5928    if( osIsNT() ){
  3.5929 @@ -36122,6 +37232,7 @@
  3.5930      h = osLoadLibraryA((char*)zConverted);
  3.5931    }
  3.5932  #endif
  3.5933 +  OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
  3.5934    sqlite3_free(zConverted);
  3.5935    return (void*)h;
  3.5936  }
  3.5937 @@ -36130,12 +37241,17 @@
  3.5938    winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
  3.5939  }
  3.5940  static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
  3.5941 +  FARPROC proc;
  3.5942    UNUSED_PARAMETER(pVfs);
  3.5943 -  return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
  3.5944 +  proc = osGetProcAddressA((HANDLE)pH, zSym);
  3.5945 +  OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
  3.5946 +           (void*)pH, zSym, (void*)proc));
  3.5947 +  return (void(*)(void))proc;
  3.5948  }
  3.5949  static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
  3.5950    UNUSED_PARAMETER(pVfs);
  3.5951    osFreeLibrary((HANDLE)pHandle);
  3.5952 +  OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
  3.5953  }
  3.5954  #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  3.5955    #define winDlOpen  0
  3.5956 @@ -36215,12 +37331,12 @@
  3.5957  ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  3.5958  ** proleptic Gregorian calendar.
  3.5959  **
  3.5960 -** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
  3.5961 +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
  3.5962  ** cannot be found.
  3.5963  */
  3.5964  static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
  3.5965 -  /* FILETIME structure is a 64-bit value representing the number of 
  3.5966 -     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
  3.5967 +  /* FILETIME structure is a 64-bit value representing the number of
  3.5968 +     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
  3.5969    */
  3.5970    FILETIME ft;
  3.5971    static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
  3.5972 @@ -36228,7 +37344,7 @@
  3.5973    static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
  3.5974  #endif
  3.5975    /* 2^32 - to avoid use of LL and warnings in gcc */
  3.5976 -  static const sqlite3_int64 max32BitValue = 
  3.5977 +  static const sqlite3_int64 max32BitValue =
  3.5978        (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
  3.5979        (sqlite3_int64)294967296;
  3.5980  
  3.5981 @@ -36244,7 +37360,7 @@
  3.5982  #endif
  3.5983  
  3.5984    *piNow = winFiletimeEpoch +
  3.5985 -            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
  3.5986 +            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
  3.5987                 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
  3.5988  
  3.5989  #ifdef SQLITE_TEST
  3.5990 @@ -36363,7 +37479,7 @@
  3.5991  
  3.5992    /* Double-check that the aSyscall[] array has been constructed
  3.5993    ** correctly.  See ticket [bb3a86e890c8e96ab] */
  3.5994 -  assert( ArraySize(aSyscall)==76 );
  3.5995 +  assert( ArraySize(aSyscall)==77 );
  3.5996  
  3.5997    /* get memory map allocation granularity */
  3.5998    memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  3.5999 @@ -36381,10 +37497,10 @@
  3.6000    sqlite3_vfs_register(&winLongPathVfs, 0);
  3.6001  #endif
  3.6002  
  3.6003 -  return SQLITE_OK; 
  3.6004 -}
  3.6005 -
  3.6006 -SQLITE_API int sqlite3_os_end(void){ 
  3.6007 +  return SQLITE_OK;
  3.6008 +}
  3.6009 +
  3.6010 +SQLITE_API int sqlite3_os_end(void){
  3.6011  #if SQLITE_OS_WINRT
  3.6012    if( sleepObj!=NULL ){
  3.6013      osCloseHandle(sleepObj);
  3.6014 @@ -36831,7 +37947,8 @@
  3.6015    int szCache;                        /* Configured cache size */
  3.6016    int szPage;                         /* Size of every page in this cache */
  3.6017    int szExtra;                        /* Size of extra space for each page */
  3.6018 -  int bPurgeable;                     /* True if pages are on backing store */
  3.6019 +  u8 bPurgeable;                      /* True if pages are on backing store */
  3.6020 +  u8 eCreate;                         /* eCreate value for for xFetch() */
  3.6021    int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
  3.6022    void *pStress;                      /* Argument to xStress */
  3.6023    sqlite3_pcache *pCache;             /* Pluggable cache module */
  3.6024 @@ -36898,6 +38015,10 @@
  3.6025    }else{
  3.6026      assert( pPage==p->pDirty );
  3.6027      p->pDirty = pPage->pDirtyNext;
  3.6028 +    if( p->pDirty==0 && p->bPurgeable ){
  3.6029 +      assert( p->eCreate==1 );
  3.6030 +      p->eCreate = 2;
  3.6031 +    }
  3.6032    }
  3.6033    pPage->pDirtyNext = 0;
  3.6034    pPage->pDirtyPrev = 0;
  3.6035 @@ -36918,6 +38039,9 @@
  3.6036    if( pPage->pDirtyNext ){
  3.6037      assert( pPage->pDirtyNext->pDirtyPrev==0 );
  3.6038      pPage->pDirtyNext->pDirtyPrev = pPage;
  3.6039 +  }else if( p->bPurgeable ){
  3.6040 +    assert( p->eCreate==2 );
  3.6041 +    p->eCreate = 1;
  3.6042    }
  3.6043    p->pDirty = pPage;
  3.6044    if( !p->pDirtyTail ){
  3.6045 @@ -36987,6 +38111,7 @@
  3.6046    p->szPage = szPage;
  3.6047    p->szExtra = szExtra;
  3.6048    p->bPurgeable = bPurgeable;
  3.6049 +  p->eCreate = 2;
  3.6050    p->xStress = xStress;
  3.6051    p->pStress = pStress;
  3.6052    p->szCache = 100;
  3.6053 @@ -37026,7 +38151,7 @@
  3.6054    int createFlag,       /* If true, create page if it does not exist already */
  3.6055    PgHdr **ppPage        /* Write the page here */
  3.6056  ){
  3.6057 -  sqlite3_pcache_page *pPage = 0;
  3.6058 +  sqlite3_pcache_page *pPage;
  3.6059    PgHdr *pPgHdr = 0;
  3.6060    int eCreate;
  3.6061  
  3.6062 @@ -37037,8 +38162,12 @@
  3.6063    /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
  3.6064    ** allocate it now.
  3.6065    */
  3.6066 -  if( !pCache->pCache && createFlag ){
  3.6067 +  if( !pCache->pCache ){
  3.6068      sqlite3_pcache *p;
  3.6069 +    if( !createFlag ){
  3.6070 +      *ppPage = 0;
  3.6071 +      return SQLITE_OK;
  3.6072 +    }
  3.6073      p = sqlite3GlobalConfig.pcache2.xCreate(
  3.6074          pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
  3.6075      );
  3.6076 @@ -37049,11 +38178,16 @@
  3.6077      pCache->pCache = p;
  3.6078    }
  3.6079  
  3.6080 -  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
  3.6081 -  if( pCache->pCache ){
  3.6082 -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  3.6083 -  }
  3.6084 -
  3.6085 +  /* eCreate defines what to do if the page does not exist.
  3.6086 +  **    0     Do not allocate a new page.  (createFlag==0)
  3.6087 +  **    1     Allocate a new page if doing so is inexpensive.
  3.6088 +  **          (createFlag==1 AND bPurgeable AND pDirty)
  3.6089 +  **    2     Allocate a new page even it doing so is difficult.
  3.6090 +  **          (createFlag==1 AND !(bPurgeable AND pDirty)
  3.6091 +  */
  3.6092 +  eCreate = createFlag==0 ? 0 : pCache->eCreate;
  3.6093 +  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
  3.6094 +  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  3.6095    if( !pPage && eCreate==1 ){
  3.6096      PgHdr *pPg;
  3.6097  
  3.6098 @@ -37525,6 +38659,7 @@
  3.6099  struct PgHdr1 {
  3.6100    sqlite3_pcache_page page;
  3.6101    unsigned int iKey;             /* Key value (page number) */
  3.6102 +  u8 isPinned;                   /* Page in use, not on the LRU list */
  3.6103    PgHdr1 *pNext;                 /* Next in hash table chain */
  3.6104    PCache1 *pCache;               /* Cache that currently owns this page */
  3.6105    PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
  3.6106 @@ -37853,34 +38988,32 @@
  3.6107  ** LRU list, then this function is a no-op.
  3.6108  **
  3.6109  ** The PGroup mutex must be held when this function is called.
  3.6110 -**
  3.6111 -** If pPage is NULL then this routine is a no-op.
  3.6112  */
  3.6113  static void pcache1PinPage(PgHdr1 *pPage){
  3.6114    PCache1 *pCache;
  3.6115    PGroup *pGroup;
  3.6116  
  3.6117 -  if( pPage==0 ) return;
  3.6118 +  assert( pPage!=0 );
  3.6119 +  assert( pPage->isPinned==0 );
  3.6120    pCache = pPage->pCache;
  3.6121    pGroup = pCache->pGroup;
  3.6122 +  assert( pPage->pLruNext || pPage==pGroup->pLruTail );
  3.6123 +  assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
  3.6124    assert( sqlite3_mutex_held(pGroup->mutex) );
  3.6125 -  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
  3.6126 -    if( pPage->pLruPrev ){
  3.6127 -      pPage->pLruPrev->pLruNext = pPage->pLruNext;
  3.6128 -    }
  3.6129 -    if( pPage->pLruNext ){
  3.6130 -      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
  3.6131 -    }
  3.6132 -    if( pGroup->pLruHead==pPage ){
  3.6133 -      pGroup->pLruHead = pPage->pLruNext;
  3.6134 -    }
  3.6135 -    if( pGroup->pLruTail==pPage ){
  3.6136 -      pGroup->pLruTail = pPage->pLruPrev;
  3.6137 -    }
  3.6138 -    pPage->pLruNext = 0;
  3.6139 -    pPage->pLruPrev = 0;
  3.6140 -    pPage->pCache->nRecyclable--;
  3.6141 -  }
  3.6142 +  if( pPage->pLruPrev ){
  3.6143 +    pPage->pLruPrev->pLruNext = pPage->pLruNext;
  3.6144 +  }else{
  3.6145 +    pGroup->pLruHead = pPage->pLruNext;
  3.6146 +  }
  3.6147 +  if( pPage->pLruNext ){
  3.6148 +    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
  3.6149 +  }else{
  3.6150 +    pGroup->pLruTail = pPage->pLruPrev;
  3.6151 +  }
  3.6152 +  pPage->pLruNext = 0;
  3.6153 +  pPage->pLruPrev = 0;
  3.6154 +  pPage->isPinned = 1;
  3.6155 +  pCache->nRecyclable--;
  3.6156  }
  3.6157  
  3.6158  
  3.6159 @@ -37912,6 +39045,7 @@
  3.6160    while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
  3.6161      PgHdr1 *p = pGroup->pLruTail;
  3.6162      assert( p->pCache->pGroup==pGroup );
  3.6163 +    assert( p->isPinned==0 );
  3.6164      pcache1PinPage(p);
  3.6165      pcache1RemoveFromHash(p);
  3.6166      pcache1FreePage(p);
  3.6167 @@ -37939,7 +39073,7 @@
  3.6168        if( pPage->iKey>=iLimit ){
  3.6169          pCache->nPage--;
  3.6170          *pp = pPage->pNext;
  3.6171 -        pcache1PinPage(pPage);
  3.6172 +        if( !pPage->isPinned ) pcache1PinPage(pPage);
  3.6173          pcache1FreePage(pPage);
  3.6174        }else{
  3.6175          pp = &pPage->pNext;
  3.6176 @@ -38149,6 +39283,7 @@
  3.6177    PGroup *pGroup;
  3.6178    PgHdr1 *pPage = 0;
  3.6179  
  3.6180 +  assert( offsetof(PgHdr1,page)==0 );
  3.6181    assert( pCache->bPurgeable || createFlag!=1 );
  3.6182    assert( pCache->bPurgeable || pCache->nMin==0 );
  3.6183    assert( pCache->bPurgeable==0 || pCache->nMin==10 );
  3.6184 @@ -38162,8 +39297,11 @@
  3.6185    }
  3.6186  
  3.6187    /* Step 2: Abort if no existing page is found and createFlag is 0 */
  3.6188 -  if( pPage || createFlag==0 ){
  3.6189 -    pcache1PinPage(pPage);
  3.6190 +  if( pPage ){
  3.6191 +    if( !pPage->isPinned ) pcache1PinPage(pPage);
  3.6192 +    goto fetch_out;
  3.6193 +  }
  3.6194 +  if( createFlag==0 ){
  3.6195      goto fetch_out;
  3.6196    }
  3.6197  
  3.6198 @@ -38204,6 +39342,7 @@
  3.6199    )){
  3.6200      PCache1 *pOther;
  3.6201      pPage = pGroup->pLruTail;
  3.6202 +    assert( pPage->isPinned==0 );
  3.6203      pcache1RemoveFromHash(pPage);
  3.6204      pcache1PinPage(pPage);
  3.6205      pOther = pPage->pCache;
  3.6206 @@ -38240,6 +39379,7 @@
  3.6207      pPage->pCache = pCache;
  3.6208      pPage->pLruPrev = 0;
  3.6209      pPage->pLruNext = 0;
  3.6210 +    pPage->isPinned = 1;
  3.6211      *(void **)pPage->page.pExtra = 0;
  3.6212      pCache->apHash[h] = pPage;
  3.6213    }
  3.6214 @@ -38249,7 +39389,7 @@
  3.6215      pCache->iMaxKey = iKey;
  3.6216    }
  3.6217    pcache1LeaveMutex(pGroup);
  3.6218 -  return &pPage->page;
  3.6219 +  return (sqlite3_pcache_page*)pPage;
  3.6220  }
  3.6221  
  3.6222  
  3.6223 @@ -38275,6 +39415,7 @@
  3.6224    */
  3.6225    assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
  3.6226    assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
  3.6227 +  assert( pPage->isPinned==1 );
  3.6228  
  3.6229    if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
  3.6230      pcache1RemoveFromHash(pPage);
  3.6231 @@ -38290,6 +39431,7 @@
  3.6232        pGroup->pLruHead = pPage;
  3.6233      }
  3.6234      pCache->nRecyclable++;
  3.6235 +    pPage->isPinned = 0;
  3.6236    }
  3.6237  
  3.6238    pcache1LeaveMutex(pCache->pGroup);
  3.6239 @@ -38416,6 +39558,7 @@
  3.6240  #ifdef SQLITE_PCACHE_SEPARATE_HEADER
  3.6241        nFree += sqlite3MemSize(p);
  3.6242  #endif
  3.6243 +      assert( p->isPinned==0 );
  3.6244        pcache1PinPage(p);
  3.6245        pcache1RemoveFromHash(p);
  3.6246        pcache1FreePage(p);
  3.6247 @@ -38440,6 +39583,7 @@
  3.6248    PgHdr1 *p;
  3.6249    int nRecyclable = 0;
  3.6250    for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
  3.6251 +    assert( p->isPinned==0 );
  3.6252      nRecyclable++;
  3.6253    }
  3.6254    *pnCurrent = pcache1.grp.nCurrentPage;
  3.6255 @@ -38564,8 +39708,8 @@
  3.6256    struct RowSetEntry *pFresh;    /* Source of new entry objects */
  3.6257    struct RowSetEntry *pForest;   /* List of binary trees of entries */
  3.6258    u16 nFresh;                    /* Number of objects on pFresh */
  3.6259 -  u8 rsFlags;                    /* Various flags */
  3.6260 -  u8 iBatch;                     /* Current insert batch */
  3.6261 +  u16 rsFlags;                   /* Various flags */
  3.6262 +  int iBatch;                    /* Current insert batch */
  3.6263  };
  3.6264  
  3.6265  /*
  3.6266 @@ -38899,7 +40043,7 @@
  3.6267  ** on pRowSet->pEntry, then sort those entires into the forest at
  3.6268  ** pRowSet->pForest so that they can be tested.
  3.6269  */
  3.6270 -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
  3.6271 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
  3.6272    struct RowSetEntry *p, *pTree;
  3.6273  
  3.6274    /* This routine is never called after sqlite3RowSetNext() */
  3.6275 @@ -39728,7 +40872,8 @@
  3.6276    u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
  3.6277    u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
  3.6278    u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
  3.6279 -  u8 tempFile;                /* zFilename is a temporary file */
  3.6280 +  u8 tempFile;                /* zFilename is a temporary or immutable file */
  3.6281 +  u8 noLock;                  /* Do not lock (except in WAL mode) */
  3.6282    u8 readOnly;                /* True for a read-only database */
  3.6283    u8 memDb;                   /* True to inhibit all file I/O */
  3.6284  
  3.6285 @@ -40126,15 +41271,12 @@
  3.6286  static int subjRequiresPage(PgHdr *pPg){
  3.6287    Pager *pPager = pPg->pPager;
  3.6288    PagerSavepoint *p;
  3.6289 -  Pgno pgno;
  3.6290 -  int i;
  3.6291 -  if( pPager->nSavepoint ){
  3.6292 -    pgno = pPg->pgno;
  3.6293 -    for(i=0; i<pPager->nSavepoint; i++){
  3.6294 -      p = &pPager->aSavepoint[i];
  3.6295 -      if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
  3.6296 -        return 1;
  3.6297 -      }
  3.6298 +  Pgno pgno = pPg->pgno;
  3.6299 +  int i;
  3.6300 +  for(i=0; i<pPager->nSavepoint; i++){
  3.6301 +    p = &pPager->aSavepoint[i];
  3.6302 +    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
  3.6303 +      return 1;
  3.6304      }
  3.6305    }
  3.6306    return 0;
  3.6307 @@ -40143,8 +41285,8 @@
  3.6308  /*
  3.6309  ** Return true if the page is already in the journal file.
  3.6310  */
  3.6311 -static int pageInJournal(PgHdr *pPg){
  3.6312 -  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
  3.6313 +static int pageInJournal(Pager *pPager, PgHdr *pPg){
  3.6314 +  return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
  3.6315  }
  3.6316  
  3.6317  /*
  3.6318 @@ -40196,7 +41338,7 @@
  3.6319    assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
  3.6320    if( isOpen(pPager->fd) ){
  3.6321      assert( pPager->eLock>=eLock );
  3.6322 -    rc = sqlite3OsUnlock(pPager->fd, eLock);
  3.6323 +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
  3.6324      if( pPager->eLock!=UNKNOWN_LOCK ){
  3.6325        pPager->eLock = (u8)eLock;
  3.6326      }
  3.6327 @@ -40220,7 +41362,7 @@
  3.6328  
  3.6329    assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
  3.6330    if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
  3.6331 -    rc = sqlite3OsLock(pPager->fd, eLock);
  3.6332 +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
  3.6333      if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
  3.6334        pPager->eLock = (u8)eLock;
  3.6335        IOTRACE(("LOCK %p %d\n", pPager, eLock))
  3.6336 @@ -40351,6 +41493,7 @@
  3.6337     || szJ<16
  3.6338     || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
  3.6339     || len>=nMaster 
  3.6340 +   || len==0 
  3.6341     || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
  3.6342     || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
  3.6343     || memcmp(aMagic, aJournalMagic, 8)
  3.6344 @@ -40728,12 +41871,11 @@
  3.6345  
  3.6346    if( !zMaster 
  3.6347     || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
  3.6348 -   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
  3.6349 +   || !isOpen(pPager->jfd)
  3.6350    ){
  3.6351      return SQLITE_OK;
  3.6352    }
  3.6353    pPager->setMaster = 1;
  3.6354 -  assert( isOpen(pPager->jfd) );
  3.6355    assert( pPager->journalHdr <= pPager->journalOff );
  3.6356  
  3.6357    /* Calculate the length in bytes and the checksum of zMaster */
  3.6358 @@ -40787,7 +41929,7 @@
  3.6359  ** already in memory.
  3.6360  */
  3.6361  static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
  3.6362 -  PgHdr *p;                         /* Return value */
  3.6363 +  PgHdr *p = 0;                     /* Return value */
  3.6364  
  3.6365    /* It is not possible for a call to PcacheFetch() with createFlag==0 to
  3.6366    ** fail, since no attempt to allocate dynamic memory will be made.
  3.6367 @@ -41091,7 +42233,7 @@
  3.6368      PgHdr *p = pager_lookup(pPager, 1);
  3.6369      if( p ){
  3.6370        p->pageHash = 0;
  3.6371 -      sqlite3PagerUnref(p);
  3.6372 +      sqlite3PagerUnrefNotNull(p);
  3.6373      }
  3.6374    }
  3.6375  #endif
  3.6376 @@ -41120,6 +42262,11 @@
  3.6377      rc = pager_truncate(pPager, pPager->dbSize);
  3.6378    }
  3.6379  
  3.6380 +  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
  3.6381 +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
  3.6382 +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  3.6383 +  }
  3.6384 +
  3.6385    if( !pPager->exclusiveMode 
  3.6386     && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
  3.6387    ){
  3.6388 @@ -41933,7 +43080,7 @@
  3.6389    if( rc==SQLITE_OK
  3.6390     && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  3.6391    ){
  3.6392 -    rc = sqlite3PagerSync(pPager);
  3.6393 +    rc = sqlite3PagerSync(pPager, 0);
  3.6394    }
  3.6395    if( rc==SQLITE_OK ){
  3.6396      rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
  3.6397 @@ -42079,7 +43226,7 @@
  3.6398        if( rc==SQLITE_OK ){
  3.6399          pPager->xReiniter(pPg);
  3.6400        }
  3.6401 -      sqlite3PagerUnref(pPg);
  3.6402 +      sqlite3PagerUnrefNotNull(pPg);
  3.6403      }
  3.6404    }
  3.6405  
  3.6406 @@ -43434,7 +44581,7 @@
  3.6407      assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
  3.6408      assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
  3.6409      assert( pagerUseWal(pPager) 
  3.6410 -         || pageInJournal(pPg) 
  3.6411 +         || pageInJournal(pPager, pPg) 
  3.6412           || pPg->pgno>pPager->dbOrigSize 
  3.6413      );
  3.6414      rc = openSubJournal(pPager);
  3.6415 @@ -43774,30 +44921,38 @@
  3.6416      **    + The value returned by sqlite3OsSectorSize()
  3.6417      **    + The largest page size that can be written atomically.
  3.6418      */
  3.6419 -    if( rc==SQLITE_OK && !readOnly ){
  3.6420 -      setSectorSize(pPager);
  3.6421 -      assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
  3.6422 -      if( szPageDflt<pPager->sectorSize ){
  3.6423 -        if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  3.6424 -          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  3.6425 -        }else{
  3.6426 -          szPageDflt = (u32)pPager->sectorSize;
  3.6427 -        }
  3.6428 -      }
  3.6429 +    if( rc==SQLITE_OK ){
  3.6430 +      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  3.6431 +      if( !readOnly ){
  3.6432 +        setSectorSize(pPager);
  3.6433 +        assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
  3.6434 +        if( szPageDflt<pPager->sectorSize ){
  3.6435 +          if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  3.6436 +            szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  3.6437 +          }else{
  3.6438 +            szPageDflt = (u32)pPager->sectorSize;
  3.6439 +          }
  3.6440 +        }
  3.6441  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3.6442 -      {
  3.6443 -        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  3.6444 -        int ii;
  3.6445 -        assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  3.6446 -        assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  3.6447 -        assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  3.6448 -        for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  3.6449 -          if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
  3.6450 -            szPageDflt = ii;
  3.6451 -          }
  3.6452 -        }
  3.6453 -      }
  3.6454 -#endif
  3.6455 +        {
  3.6456 +          int ii;
  3.6457 +          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  3.6458 +          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  3.6459 +          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  3.6460 +          for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  3.6461 +            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
  3.6462 +              szPageDflt = ii;
  3.6463 +            }
  3.6464 +          }
  3.6465 +        }
  3.6466 +#endif
  3.6467 +      }
  3.6468 +      pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
  3.6469 +      if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
  3.6470 +       || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
  3.6471 +          vfsFlags |= SQLITE_OPEN_READONLY;
  3.6472 +          goto act_like_temp_file;
  3.6473 +      }
  3.6474      }
  3.6475    }else{
  3.6476      /* If a temporary file is requested, it is not opened immediately.
  3.6477 @@ -43807,10 +44962,14 @@
  3.6478      ** This branch is also run for an in-memory database. An in-memory
  3.6479      ** database is the same as a temp-file that is never written out to
  3.6480      ** disk and uses an in-memory rollback journal.
  3.6481 +    **
  3.6482 +    ** This branch also runs for files marked as immutable.
  3.6483      */ 
  3.6484 +act_like_temp_file:
  3.6485      tempFile = 1;
  3.6486 -    pPager->eState = PAGER_READER;
  3.6487 -    pPager->eLock = EXCLUSIVE_LOCK;
  3.6488 +    pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
  3.6489 +    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE locking mode */
  3.6490 +    pPager->noLock = 1;                /* Do no locking */
  3.6491      readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
  3.6492    }
  3.6493  
  3.6494 @@ -43851,9 +45010,6 @@
  3.6495    /* pPager->nPage = 0; */
  3.6496    pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
  3.6497    /* pPager->state = PAGER_UNLOCK; */
  3.6498 -#if 0
  3.6499 -  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
  3.6500 -#endif
  3.6501    /* pPager->errMask = 0; */
  3.6502    pPager->tempFile = (u8)tempFile;
  3.6503    assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
  3.6504 @@ -43899,6 +45055,30 @@
  3.6505  }
  3.6506  
  3.6507  
  3.6508 +/* Verify that the database file has not be deleted or renamed out from
  3.6509 +** under the pager.  Return SQLITE_OK if the database is still were it ought
  3.6510 +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
  3.6511 +** code from sqlite3OsAccess()) if the database has gone missing.
  3.6512 +*/
  3.6513 +static int databaseIsUnmoved(Pager *pPager){
  3.6514 +  int bHasMoved = 0;
  3.6515 +  int rc;
  3.6516 +
  3.6517 +  if( pPager->tempFile ) return SQLITE_OK;
  3.6518 +  if( pPager->dbSize==0 ) return SQLITE_OK;
  3.6519 +  assert( pPager->zFilename && pPager->zFilename[0] );
  3.6520 +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
  3.6521 +  if( rc==SQLITE_NOTFOUND ){
  3.6522 +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
  3.6523 +    ** has not been moved.  That is the historical behavior of SQLite: prior to
  3.6524 +    ** version 3.8.3, it never checked */
  3.6525 +    rc = SQLITE_OK;
  3.6526 +  }else if( rc==SQLITE_OK && bHasMoved ){
  3.6527 +    rc = SQLITE_READONLY_DBMOVED;
  3.6528 +  }
  3.6529 +  return rc;
  3.6530 +}
  3.6531 +
  3.6532  
  3.6533  /*
  3.6534  ** This function is called after transitioning from PAGER_UNLOCK to
  3.6535 @@ -43964,15 +45144,17 @@
  3.6536      if( rc==SQLITE_OK && !locked ){
  3.6537        Pgno nPage;                 /* Number of pages in database file */
  3.6538  
  3.6539 -      /* Check the size of the database file. If it consists of 0 pages,
  3.6540 -      ** then delete the journal file. See the header comment above for 
  3.6541 -      ** the reasoning here.  Delete the obsolete journal file under
  3.6542 -      ** a RESERVED lock to avoid race conditions and to avoid violating
  3.6543 -      ** [H33020].
  3.6544 -      */
  3.6545        rc = pagerPagecount(pPager, &nPage);
  3.6546        if( rc==SQLITE_OK ){
  3.6547 -        if( nPage==0 ){
  3.6548 +        /* If the database is zero pages in size, that means that either (1) the
  3.6549 +        ** journal is a remnant from a prior database with the same name where
  3.6550 +        ** the database file but not the journal was deleted, or (2) the initial
  3.6551 +        ** transaction that populates a new database is being rolled back.
  3.6552 +        ** In either case, the journal file can be deleted.  However, take care
  3.6553 +        ** not to delete the journal file if it is already open due to
  3.6554 +        ** journal_mode=PERSIST.
  3.6555 +        */
  3.6556 +        if( nPage==0 && !jrnlOpen ){
  3.6557            sqlite3BeginBenignMalloc();
  3.6558            if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
  3.6559              sqlite3OsDelete(pVfs, pPager->zJournal, 0);
  3.6560 @@ -44370,7 +45552,7 @@
  3.6561        if( rc!=SQLITE_OK ) goto pager_acquire_err;
  3.6562      }
  3.6563  
  3.6564 -    if( iFrame==0 && bMmapOk ){
  3.6565 +    if( bMmapOk && iFrame==0 ){
  3.6566        void *pData = 0;
  3.6567  
  3.6568        rc = sqlite3OsFetch(pPager->fd, 
  3.6569 @@ -44511,16 +45693,19 @@
  3.6570  ** are released, a rollback occurs and the lock on the database is
  3.6571  ** removed.
  3.6572  */
  3.6573 +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
  3.6574 +  Pager *pPager;
  3.6575 +  assert( pPg!=0 );
  3.6576 +  pPager = pPg->pPager;
  3.6577 +  if( pPg->flags & PGHDR_MMAP ){
  3.6578 +    pagerReleaseMapPage(pPg);
  3.6579 +  }else{
  3.6580 +    sqlite3PcacheRelease(pPg);
  3.6581 +  }
  3.6582 +  pagerUnlockIfUnused(pPager);
  3.6583 +}
  3.6584  SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
  3.6585 -  if( pPg ){
  3.6586 -    Pager *pPager = pPg->pPager;
  3.6587 -    if( pPg->flags & PGHDR_MMAP ){
  3.6588 -      pagerReleaseMapPage(pPg);
  3.6589 -    }else{
  3.6590 -      sqlite3PcacheRelease(pPg);
  3.6591 -    }
  3.6592 -    pagerUnlockIfUnused(pPager);
  3.6593 -  }
  3.6594 +  if( pPg ) sqlite3PagerUnrefNotNull(pPg);
  3.6595  }
  3.6596  
  3.6597  /*
  3.6598 @@ -44575,13 +45760,19 @@
  3.6599              (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
  3.6600              (SQLITE_OPEN_MAIN_JOURNAL)
  3.6601            );
  3.6602 -  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3.6603 -        rc = sqlite3JournalOpen(
  3.6604 -            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  3.6605 -        );
  3.6606 -  #else
  3.6607 -        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  3.6608 -  #endif
  3.6609 +
  3.6610 +        /* Verify that the database still has the same name as it did when
  3.6611 +        ** it was originally opened. */
  3.6612 +        rc = databaseIsUnmoved(pPager);
  3.6613 +        if( rc==SQLITE_OK ){
  3.6614 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3.6615 +          rc = sqlite3JournalOpen(
  3.6616 +              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  3.6617 +          );
  3.6618 +#else
  3.6619 +          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  3.6620 +#endif
  3.6621 +        }
  3.6622        }
  3.6623        assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  3.6624      }
  3.6625 @@ -44702,9 +45893,9 @@
  3.6626  ** of any open savepoints as appropriate.
  3.6627  */
  3.6628  static int pager_write(PgHdr *pPg){
  3.6629 -  void *pData = pPg->pData;
  3.6630    Pager *pPager = pPg->pPager;
  3.6631    int rc = SQLITE_OK;
  3.6632 +  int inJournal;
  3.6633  
  3.6634    /* This routine is not called unless a write-transaction has already 
  3.6635    ** been started. The journal file may or may not be open at this point.
  3.6636 @@ -44715,14 +45906,8 @@
  3.6637         || pPager->eState==PAGER_WRITER_DBMOD
  3.6638    );
  3.6639    assert( assert_pager_state(pPager) );
  3.6640 -
  3.6641 -  /* If an error has been previously detected, report the same error
  3.6642 -  ** again. This should not happen, but the check provides robustness. */
  3.6643 -  if( NEVER(pPager->errCode) )  return pPager->errCode;
  3.6644 -
  3.6645 -  /* Higher-level routines never call this function if database is not
  3.6646 -  ** writable.  But check anyway, just for robustness. */
  3.6647 -  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
  3.6648 +  assert( pPager->errCode==0 );
  3.6649 +  assert( pPager->readOnly==0 );
  3.6650  
  3.6651    CHECK_PAGE(pPg);
  3.6652  
  3.6653 @@ -44746,7 +45931,8 @@
  3.6654    ** to the journal then we can return right away.
  3.6655    */
  3.6656    sqlite3PcacheMakeDirty(pPg);
  3.6657 -  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
  3.6658 +  inJournal = pageInJournal(pPager, pPg);
  3.6659 +  if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
  3.6660      assert( !pagerUseWal(pPager) );
  3.6661    }else{
  3.6662    
  3.6663 @@ -44754,7 +45940,7 @@
  3.6664      ** EXCLUSIVE lock on the main database file.  Write the current page to
  3.6665      ** the transaction journal if it is not there already.
  3.6666      */
  3.6667 -    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
  3.6668 +    if( !inJournal && !pagerUseWal(pPager) ){
  3.6669        assert( pagerUseWal(pPager)==0 );
  3.6670        if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
  3.6671          u32 cksum;
  3.6672 @@ -44767,7 +45953,7 @@
  3.6673          assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  3.6674  
  3.6675          assert( pPager->journalHdr<=pPager->journalOff );
  3.6676 -        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  3.6677 +        CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  3.6678          cksum = pager_cksum(pPager, (u8*)pData2);
  3.6679  
  3.6680          /* Even if an IO or diskfull error occurs while journalling the
  3.6681 @@ -44819,7 +46005,7 @@
  3.6682      ** the statement journal format differs from the standard journal format
  3.6683      ** in that it omits the checksums and the header.
  3.6684      */
  3.6685 -    if( subjRequiresPage(pPg) ){
  3.6686 +    if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
  3.6687        rc = subjournalPage(pPg);
  3.6688      }
  3.6689    }
  3.6690 @@ -44851,19 +46037,19 @@
  3.6691  
  3.6692    PgHdr *pPg = pDbPage;
  3.6693    Pager *pPager = pPg->pPager;
  3.6694 -  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  3.6695  
  3.6696    assert( (pPg->flags & PGHDR_MMAP)==0 );
  3.6697    assert( pPager->eState>=PAGER_WRITER_LOCKED );
  3.6698    assert( pPager->eState!=PAGER_ERROR );
  3.6699    assert( assert_pager_state(pPager) );
  3.6700  
  3.6701 -  if( nPagePerSector>1 ){
  3.6702 +  if( pPager->sectorSize > (u32)pPager->pageSize ){
  3.6703      Pgno nPageCount;          /* Total number of pages in database file */
  3.6704      Pgno pg1;                 /* First page of the sector pPg is located on. */
  3.6705      int nPage = 0;            /* Number of pages starting at pg1 to journal */
  3.6706      int ii;                   /* Loop counter */
  3.6707      int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
  3.6708 +    Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  3.6709  
  3.6710      /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
  3.6711      ** a journal header to be written between the pages journaled by
  3.6712 @@ -44902,14 +46088,14 @@
  3.6713              if( pPage->flags&PGHDR_NEED_SYNC ){
  3.6714                needSync = 1;
  3.6715              }
  3.6716 -            sqlite3PagerUnref(pPage);
  3.6717 +            sqlite3PagerUnrefNotNull(pPage);
  3.6718            }
  3.6719          }
  3.6720        }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
  3.6721          if( pPage->flags&PGHDR_NEED_SYNC ){
  3.6722            needSync = 1;
  3.6723          }
  3.6724 -        sqlite3PagerUnref(pPage);
  3.6725 +        sqlite3PagerUnrefNotNull(pPage);
  3.6726        }
  3.6727      }
  3.6728  
  3.6729 @@ -44925,7 +46111,7 @@
  3.6730          PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  3.6731          if( pPage ){
  3.6732            pPage->flags |= PGHDR_NEED_SYNC;
  3.6733 -          sqlite3PagerUnref(pPage);
  3.6734 +          sqlite3PagerUnrefNotNull(pPage);
  3.6735          }
  3.6736        }
  3.6737      }
  3.6738 @@ -45078,17 +46264,17 @@
  3.6739  ** If successful, or if called on a pager for which it is a no-op, this
  3.6740  ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
  3.6741  */
  3.6742 -SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
  3.6743 -  int rc = SQLITE_OK;
  3.6744 -  if( !pPager->noSync ){
  3.6745 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
  3.6746 +  int rc = SQLITE_OK;
  3.6747 +
  3.6748 +  if( isOpen(pPager->fd) ){
  3.6749 +    void *pArg = (void*)zMaster;
  3.6750 +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
  3.6751 +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  3.6752 +  }
  3.6753 +  if( rc==SQLITE_OK && !pPager->noSync ){
  3.6754      assert( !MEMDB );
  3.6755      rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
  3.6756 -  }else if( isOpen(pPager->fd) ){
  3.6757 -    assert( !MEMDB );
  3.6758 -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
  3.6759 -    if( rc==SQLITE_NOTFOUND ){
  3.6760 -      rc = SQLITE_OK;
  3.6761 -    }
  3.6762    }
  3.6763    return rc;
  3.6764  }
  3.6765 @@ -45287,7 +46473,7 @@
  3.6766    
  3.6767        /* Finally, sync the database file. */
  3.6768        if( !noSync ){
  3.6769 -        rc = sqlite3PagerSync(pPager);
  3.6770 +        rc = sqlite3PagerSync(pPager, zMaster);
  3.6771        }
  3.6772        IOTRACE(("DBSYNC %p\n", pPager))
  3.6773      }
  3.6774 @@ -45416,7 +46602,9 @@
  3.6775  
  3.6776    assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
  3.6777    assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
  3.6778 -          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
  3.6779 +          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR 
  3.6780 +          || rc==SQLITE_CANTOPEN
  3.6781 +  );
  3.6782  
  3.6783    /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  3.6784    ** cache. So call pager_error() on the way out to make any error persistent.
  3.6785 @@ -45819,7 +47007,7 @@
  3.6786    if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
  3.6787      needSyncPgno = pPg->pgno;
  3.6788      assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
  3.6789 -            pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
  3.6790 +            pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
  3.6791      assert( pPg->flags&PGHDR_DIRTY );
  3.6792    }
  3.6793  
  3.6794 @@ -45853,7 +47041,7 @@
  3.6795    if( MEMDB ){
  3.6796      assert( pPgOld );
  3.6797      sqlite3PcacheMove(pPgOld, origPgno);
  3.6798 -    sqlite3PagerUnref(pPgOld);
  3.6799 +    sqlite3PagerUnrefNotNull(pPgOld);
  3.6800    }
  3.6801  
  3.6802    if( needSyncPgno ){
  3.6803 @@ -45882,7 +47070,7 @@
  3.6804      }
  3.6805      pPgHdr->flags |= PGHDR_NEED_SYNC;
  3.6806      sqlite3PcacheMakeDirty(pPgHdr);
  3.6807 -    sqlite3PagerUnref(pPgHdr);
  3.6808 +    sqlite3PagerUnrefNotNull(pPgHdr);
  3.6809    }
  3.6810  
  3.6811    return SQLITE_OK;
  3.6812 @@ -47598,7 +48786,7 @@
  3.6813      sqlite3OsClose(pRet->pWalFd);
  3.6814      sqlite3_free(pRet);
  3.6815    }else{
  3.6816 -    int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
  3.6817 +    int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
  3.6818      if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
  3.6819      if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
  3.6820        pRet->padToSectorBoundary = 0;
  3.6821 @@ -48388,8 +49576,8 @@
  3.6822    ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
  3.6823    ** is more of a scheduler yield than an actual delay.  But on the 10th
  3.6824    ** an subsequent retries, the delays start becoming longer and longer, 
  3.6825 -  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
  3.6826 -  ** The total delay time before giving up is less than 1 second.
  3.6827 +  ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
  3.6828 +  ** The total delay time before giving up is less than 10 seconds.
  3.6829    */
  3.6830    if( cnt>5 ){
  3.6831      int nDelay = 1;                      /* Pause time in microseconds */
  3.6832 @@ -48397,7 +49585,7 @@
  3.6833        VVA_ONLY( pWal->lockError = 1; )
  3.6834        return SQLITE_PROTOCOL;
  3.6835      }
  3.6836 -    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
  3.6837 +    if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
  3.6838      sqlite3OsSleep(pWal->pVfs, nDelay);
  3.6839    }
  3.6840  
  3.6841 @@ -48969,7 +50157,7 @@
  3.6842      iAmt -= iFirstAmt;
  3.6843      pContent = (void*)(iFirstAmt + (char*)pContent);
  3.6844      assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
  3.6845 -    rc = sqlite3OsSync(p->pFd, p->syncFlags);
  3.6846 +    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
  3.6847      if( iAmt==0 || rc ) return rc;
  3.6848    }
  3.6849    rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
  3.6850 @@ -49903,22 +51091,15 @@
  3.6851    BtShared *pBt;            /* The BtShared this cursor points to */
  3.6852    BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
  3.6853    struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
  3.6854 -#ifndef SQLITE_OMIT_INCRBLOB
  3.6855    Pgno *aOverflow;          /* Cache of overflow page locations */
  3.6856 -#endif
  3.6857 +  CellInfo info;            /* A parse of the cell we are pointing at */
  3.6858 +  i64 nKey;                 /* Size of pKey, or last integer key */
  3.6859 +  void *pKey;               /* Saved key that was cursor last known position */
  3.6860    Pgno pgnoRoot;            /* The root page of this tree */
  3.6861 -  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
  3.6862 -  CellInfo info;            /* A parse of the cell we are pointing at */
  3.6863 -  i64 nKey;        /* Size of pKey, or last integer key */
  3.6864 -  void *pKey;      /* Saved key that was cursor's last known position */
  3.6865 +  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
  3.6866    int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
  3.6867 -  u8 wrFlag;                /* True if writable */
  3.6868 -  u8 atLast;                /* Cursor pointing to the last entry */
  3.6869 -  u8 validNKey;             /* True if info.nKey is valid */
  3.6870 +  u8 curFlags;              /* zero or more BTCF_* flags defined below */
  3.6871    u8 eState;                /* One of the CURSOR_XXX constants (see below) */
  3.6872 -#ifndef SQLITE_OMIT_INCRBLOB
  3.6873 -  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
  3.6874 -#endif
  3.6875    u8 hints;                             /* As configured by CursorSetHints() */
  3.6876    i16 iPage;                            /* Index of current page in apPage */
  3.6877    u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
  3.6878 @@ -49926,6 +51107,15 @@
  3.6879  };
  3.6880  
  3.6881  /*
  3.6882 +** Legal values for BtCursor.curFlags
  3.6883 +*/
  3.6884 +#define BTCF_WriteFlag    0x01   /* True if a write cursor */
  3.6885 +#define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
  3.6886 +#define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
  3.6887 +#define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
  3.6888 +#define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
  3.6889 +
  3.6890 +/*
  3.6891  ** Potential values for BtCursor.eState.
  3.6892  **
  3.6893  ** CURSOR_INVALID:
  3.6894 @@ -50511,7 +51701,7 @@
  3.6895    ** the correct locks are held.  So do not bother - just return true.
  3.6896    ** This case does not come up very often anyhow.
  3.6897    */
  3.6898 -  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
  3.6899 +  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
  3.6900      return 1;
  3.6901    }
  3.6902  
  3.6903 @@ -50795,16 +51985,11 @@
  3.6904  }
  3.6905  #endif
  3.6906  
  3.6907 -
  3.6908 -#ifndef SQLITE_OMIT_INCRBLOB
  3.6909 -/*
  3.6910 -** Invalidate the overflow page-list cache for cursor pCur, if any.
  3.6911 -*/
  3.6912 -static void invalidateOverflowCache(BtCursor *pCur){
  3.6913 -  assert( cursorHoldsMutex(pCur) );
  3.6914 -  sqlite3_free(pCur->aOverflow);
  3.6915 -  pCur->aOverflow = 0;
  3.6916 -}
  3.6917 +/*
  3.6918 +** Invalidate the overflow cache of the cursor passed as the first argument.
  3.6919 +** on the shared btree structure pBt.
  3.6920 +*/
  3.6921 +#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
  3.6922  
  3.6923  /*
  3.6924  ** Invalidate the overflow page-list cache for all cursors opened
  3.6925 @@ -50818,6 +52003,7 @@
  3.6926    }
  3.6927  }
  3.6928  
  3.6929 +#ifndef SQLITE_OMIT_INCRBLOB
  3.6930  /*
  3.6931  ** This function is called before modifying the contents of a table
  3.6932  ** to invalidate any incrblob cursors that are open on the
  3.6933 @@ -50840,16 +52026,14 @@
  3.6934    BtShared *pBt = pBtree->pBt;
  3.6935    assert( sqlite3BtreeHoldsMutex(pBtree) );
  3.6936    for(p=pBt->pCursor; p; p=p->pNext){
  3.6937 -    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
  3.6938 +    if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){
  3.6939        p->eState = CURSOR_INVALID;
  3.6940      }
  3.6941    }
  3.6942  }
  3.6943  
  3.6944  #else
  3.6945 -  /* Stub functions when INCRBLOB is omitted */
  3.6946 -  #define invalidateOverflowCache(x)
  3.6947 -  #define invalidateAllOverflowCache(x)
  3.6948 +  /* Stub function when INCRBLOB is omitted */
  3.6949    #define invalidateIncrblobCursors(x,y,z)
  3.6950  #endif /* SQLITE_OMIT_INCRBLOB */
  3.6951  
  3.6952 @@ -51095,20 +52279,32 @@
  3.6953  ** at is deleted out from under them.
  3.6954  **
  3.6955  ** This routine returns an error code if something goes wrong.  The
  3.6956 -** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
  3.6957 +** integer *pHasMoved is set as follows:
  3.6958 +**
  3.6959 +**    0:   The cursor is unchanged
  3.6960 +**    1:   The cursor is still pointing at the same row, but the pointers
  3.6961 +**         returned by sqlite3BtreeKeyFetch() or sqlite3BtreeDataFetch()
  3.6962 +**         might now be invalid because of a balance() or other change to the
  3.6963 +**         b-tree.
  3.6964 +**    2:   The cursor is no longer pointing to the row.  The row might have
  3.6965 +**         been deleted out from under the cursor.
  3.6966  */
  3.6967  SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
  3.6968    int rc;
  3.6969  
  3.6970 +  if( pCur->eState==CURSOR_VALID ){
  3.6971 +    *pHasMoved = 0;
  3.6972 +    return SQLITE_OK;
  3.6973 +  }
  3.6974    rc = restoreCursorPosition(pCur);
  3.6975    if( rc ){
  3.6976 +    *pHasMoved = 2;
  3.6977 +    return rc;
  3.6978 +  }
  3.6979 +  if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
  3.6980 +    *pHasMoved = 2;
  3.6981 +  }else{
  3.6982      *pHasMoved = 1;
  3.6983 -    return rc;
  3.6984 -  }
  3.6985 -  if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
  3.6986 -    *pHasMoved = 1;
  3.6987 -  }else{
  3.6988 -    *pHasMoved = 0;
  3.6989    }
  3.6990    return SQLITE_OK;
  3.6991  }
  3.6992 @@ -51891,13 +53087,12 @@
  3.6993      memset(&data[hdr], 0, pBt->usableSize - hdr);
  3.6994    }
  3.6995    data[hdr] = (char)flags;
  3.6996 -  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
  3.6997 +  first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
  3.6998    memset(&data[hdr+1], 0, 4);
  3.6999    data[hdr+7] = 0;
  3.7000    put2byte(&data[hdr+5], pBt->usableSize);
  3.7001    pPage->nFree = (u16)(pBt->usableSize - first);
  3.7002    decodeFlags(pPage, flags);
  3.7003 -  pPage->hdrOffset = hdr;
  3.7004    pPage->cellOffset = first;
  3.7005    pPage->aDataEnd = &data[pBt->usableSize];
  3.7006    pPage->aCellIdx = &data[first];
  3.7007 @@ -51976,7 +53171,7 @@
  3.7008  SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
  3.7009    assert( sqlite3BtreeHoldsMutex(p) );
  3.7010    assert( ((p->pBt->nPage)&0x8000000)==0 );
  3.7011 -  return (int)btreePagecount(p->pBt);
  3.7012 +  return btreePagecount(p->pBt);
  3.7013  }
  3.7014  
  3.7015  /*
  3.7016 @@ -52001,7 +53196,7 @@
  3.7017      rc = SQLITE_CORRUPT_BKPT;
  3.7018    }else{
  3.7019      rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
  3.7020 -    if( rc==SQLITE_OK ){
  3.7021 +    if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
  3.7022        rc = btreeInitPage(*ppPage);
  3.7023        if( rc!=SQLITE_OK ){
  3.7024          releasePage(*ppPage);
  3.7025 @@ -52022,10 +53217,11 @@
  3.7026    if( pPage ){
  3.7027      assert( pPage->aData );
  3.7028      assert( pPage->pBt );
  3.7029 +    assert( pPage->pDbPage!=0 );
  3.7030      assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  3.7031      assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
  3.7032      assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  3.7033 -    sqlite3PagerUnref(pPage->pDbPage);
  3.7034 +    sqlite3PagerUnrefNotNull(pPage->pDbPage);
  3.7035    }
  3.7036  }
  3.7037  
  3.7038 @@ -52510,6 +53706,7 @@
  3.7039    return SQLITE_OK;
  3.7040  }
  3.7041  
  3.7042 +#if SQLITE_MAX_MMAP_SIZE>0
  3.7043  /*
  3.7044  ** Change the limit on the amount of the database file that may be
  3.7045  ** memory mapped.
  3.7046 @@ -52522,6 +53719,7 @@
  3.7047    sqlite3BtreeLeave(p);
  3.7048    return SQLITE_OK;
  3.7049  }
  3.7050 +#endif /* SQLITE_MAX_MMAP_SIZE>0 */
  3.7051  
  3.7052  /*
  3.7053  ** Change the way data is synced to disk in order to increase or decrease
  3.7054 @@ -52898,7 +54096,8 @@
  3.7055    BtCursor *pCur;
  3.7056    int r = 0;
  3.7057    for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
  3.7058 -    if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++; 
  3.7059 +    if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
  3.7060 +     && pCur->eState!=CURSOR_FAULT ) r++; 
  3.7061    }
  3.7062    return r;
  3.7063  }
  3.7064 @@ -53973,14 +55172,14 @@
  3.7065    pCur->pKeyInfo = pKeyInfo;
  3.7066    pCur->pBtree = p;
  3.7067    pCur->pBt = pBt;
  3.7068 -  pCur->wrFlag = (u8)wrFlag;
  3.7069 +  assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
  3.7070 +  pCur->curFlags = wrFlag;
  3.7071    pCur->pNext = pBt->pCursor;
  3.7072    if( pCur->pNext ){
  3.7073      pCur->pNext->pPrev = pCur;
  3.7074    }
  3.7075    pBt->pCursor = pCur;
  3.7076    pCur->eState = CURSOR_INVALID;
  3.7077 -  pCur->cachedRowid = 0;
  3.7078    return SQLITE_OK;
  3.7079  }
  3.7080  SQLITE_PRIVATE int sqlite3BtreeCursor(
  3.7081 @@ -54022,36 +55221,6 @@
  3.7082  }
  3.7083  
  3.7084  /*
  3.7085 -** Set the cached rowid value of every cursor in the same database file
  3.7086 -** as pCur and having the same root page number as pCur.  The value is
  3.7087 -** set to iRowid.
  3.7088 -**
  3.7089 -** Only positive rowid values are considered valid for this cache.
  3.7090 -** The cache is initialized to zero, indicating an invalid cache.
  3.7091 -** A btree will work fine with zero or negative rowids.  We just cannot
  3.7092 -** cache zero or negative rowids, which means tables that use zero or
  3.7093 -** negative rowids might run a little slower.  But in practice, zero
  3.7094 -** or negative rowids are very uncommon so this should not be a problem.
  3.7095 -*/
  3.7096 -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
  3.7097 -  BtCursor *p;
  3.7098 -  for(p=pCur->pBt->pCursor; p; p=p->pNext){
  3.7099 -    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
  3.7100 -  }
  3.7101 -  assert( pCur->cachedRowid==iRowid );
  3.7102 -}
  3.7103 -
  3.7104 -/*
  3.7105 -** Return the cached rowid for the given cursor.  A negative or zero
  3.7106 -** return value indicates that the rowid cache is invalid and should be
  3.7107 -** ignored.  If the rowid cache has never before been set, then a
  3.7108 -** zero is returned.
  3.7109 -*/
  3.7110 -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
  3.7111 -  return pCur->cachedRowid;
  3.7112 -}
  3.7113 -
  3.7114 -/*
  3.7115  ** Close a cursor.  The read lock on the database file is released
  3.7116  ** when the last cursor is closed.
  3.7117  */
  3.7118 @@ -54074,7 +55243,7 @@
  3.7119        releasePage(pCur->apPage[i]);
  3.7120      }
  3.7121      unlockBtreeIfUnused(pBt);
  3.7122 -    invalidateOverflowCache(pCur);
  3.7123 +    sqlite3DbFree(pBtree->db, pCur->aOverflow);
  3.7124      /* sqlite3_free(pCur); */
  3.7125      sqlite3BtreeLeave(pBtree);
  3.7126    }
  3.7127 @@ -54102,7 +55271,7 @@
  3.7128      int iPage = pCur->iPage;
  3.7129      memset(&info, 0, sizeof(info));
  3.7130      btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
  3.7131 -    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
  3.7132 +    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
  3.7133    }
  3.7134  #else
  3.7135    #define assertCellInfo(x)
  3.7136 @@ -54113,7 +55282,7 @@
  3.7137      if( pCur->info.nSize==0 ){
  3.7138        int iPage = pCur->iPage;
  3.7139        btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
  3.7140 -      pCur->validNKey = 1;
  3.7141 +      pCur->curFlags |= BTCF_ValidNKey;
  3.7142      }else{
  3.7143        assertCellInfo(pCur);
  3.7144      }
  3.7145 @@ -54123,8 +55292,8 @@
  3.7146  #define getCellInfo(pCur)                                                      \
  3.7147    if( pCur->info.nSize==0 ){                                                   \
  3.7148      int iPage = pCur->iPage;                                                   \
  3.7149 -    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
  3.7150 -    pCur->validNKey = 1;                                                       \
  3.7151 +    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);        \
  3.7152 +    pCur->curFlags |= BTCF_ValidNKey;                                          \
  3.7153    }else{                                                                       \
  3.7154      assertCellInfo(pCur);                                                      \
  3.7155    }
  3.7156 @@ -54295,10 +55464,12 @@
  3.7157  
  3.7158  /*
  3.7159  ** This function is used to read or overwrite payload information
  3.7160 -** for the entry that the pCur cursor is pointing to. If the eOp
  3.7161 -** parameter is 0, this is a read operation (data copied into
  3.7162 -** buffer pBuf). If it is non-zero, a write (data copied from
  3.7163 -** buffer pBuf).
  3.7164 +** for the entry that the pCur cursor is pointing to. The eOp
  3.7165 +** argument is interpreted as follows:
  3.7166 +**
  3.7167 +**   0: The operation is a read. Populate the overflow cache.
  3.7168 +**   1: The operation is a write. Populate the overflow cache.
  3.7169 +**   2: The operation is a read. Do not populate the overflow cache.
  3.7170  **
  3.7171  ** A total of "amt" bytes are read or written beginning at "offset".
  3.7172  ** Data is read to or from the buffer pBuf.
  3.7173 @@ -54306,11 +55477,11 @@
  3.7174  ** The content being read or written might appear on the main page
  3.7175  ** or be scattered out on multiple overflow pages.
  3.7176  **
  3.7177 -** If the BtCursor.isIncrblobHandle flag is set, and the current
  3.7178 -** cursor entry uses one or more overflow pages, this function
  3.7179 -** allocates space for and lazily popluates the overflow page-list 
  3.7180 -** cache array (BtCursor.aOverflow). Subsequent calls use this
  3.7181 -** cache to make seeking to the supplied offset more efficient.
  3.7182 +** If the current cursor entry uses one or more overflow pages and the
  3.7183 +** eOp argument is not 2, this function may allocate space for and lazily 
  3.7184 +** popluates the overflow page-list cache array (BtCursor.aOverflow). 
  3.7185 +** Subsequent calls use this cache to make seeking to the supplied offset 
  3.7186 +** more efficient.
  3.7187  **
  3.7188  ** Once an overflow page-list cache has been allocated, it may be
  3.7189  ** invalidated if some other cursor writes to the same table, or if
  3.7190 @@ -54334,15 +55505,22 @@
  3.7191    int iIdx = 0;
  3.7192    MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  3.7193    BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
  3.7194 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
  3.7195 +  int bEnd;                                   /* True if reading to end of data */
  3.7196 +#endif
  3.7197  
  3.7198    assert( pPage );
  3.7199    assert( pCur->eState==CURSOR_VALID );
  3.7200    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  3.7201    assert( cursorHoldsMutex(pCur) );
  3.7202 +  assert( eOp!=2 || offset==0 );      /* Always start from beginning for eOp==2 */
  3.7203  
  3.7204    getCellInfo(pCur);
  3.7205    aPayload = pCur->info.pCell + pCur->info.nHeader;
  3.7206    nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
  3.7207 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
  3.7208 +  bEnd = (offset+amt==nKey+pCur->info.nData);
  3.7209 +#endif
  3.7210  
  3.7211    if( NEVER(offset+amt > nKey+pCur->info.nData) 
  3.7212     || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  3.7213 @@ -54357,7 +55535,7 @@
  3.7214      if( a+offset>pCur->info.nLocal ){
  3.7215        a = pCur->info.nLocal - offset;
  3.7216      }
  3.7217 -    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
  3.7218 +    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
  3.7219      offset = 0;
  3.7220      pBuf += a;
  3.7221      amt -= a;
  3.7222 @@ -54371,21 +55549,30 @@
  3.7223  
  3.7224      nextPage = get4byte(&aPayload[pCur->info.nLocal]);
  3.7225  
  3.7226 -#ifndef SQLITE_OMIT_INCRBLOB
  3.7227 -    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
  3.7228 -    ** has not been allocated, allocate it now. The array is sized at
  3.7229 -    ** one entry for each overflow page in the overflow chain. The
  3.7230 -    ** page number of the first overflow page is stored in aOverflow[0],
  3.7231 -    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
  3.7232 -    ** (the cache is lazily populated).
  3.7233 -    */
  3.7234 -    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
  3.7235 +    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
  3.7236 +    ** Except, do not allocate aOverflow[] for eOp==2.
  3.7237 +    **
  3.7238 +    ** The aOverflow[] array is sized at one entry for each overflow page
  3.7239 +    ** in the overflow chain. The page number of the first overflow page is
  3.7240 +    ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
  3.7241 +    ** means "not yet known" (the cache is lazily populated).
  3.7242 +    */
  3.7243 +    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
  3.7244        int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
  3.7245 -      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
  3.7246 -      /* nOvfl is always positive.  If it were zero, fetchPayload would have
  3.7247 -      ** been used instead of this routine. */
  3.7248 -      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
  3.7249 -        rc = SQLITE_NOMEM;
  3.7250 +      if( nOvfl>pCur->nOvflAlloc ){
  3.7251 +        Pgno *aNew = (Pgno*)sqlite3DbRealloc(
  3.7252 +            pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  3.7253 +        );
  3.7254 +        if( aNew==0 ){
  3.7255 +          rc = SQLITE_NOMEM;
  3.7256 +        }else{
  3.7257 +          pCur->nOvflAlloc = nOvfl*2;
  3.7258 +          pCur->aOverflow = aNew;
  3.7259 +        }
  3.7260 +      }
  3.7261 +      if( rc==SQLITE_OK ){
  3.7262 +        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
  3.7263 +        pCur->curFlags |= BTCF_ValidOvfl;
  3.7264        }
  3.7265      }
  3.7266  
  3.7267 @@ -54393,22 +55580,19 @@
  3.7268      ** entry for the first required overflow page is valid, skip
  3.7269      ** directly to it.
  3.7270      */
  3.7271 -    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
  3.7272 +    if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
  3.7273        iIdx = (offset/ovflSize);
  3.7274        nextPage = pCur->aOverflow[iIdx];
  3.7275        offset = (offset%ovflSize);
  3.7276      }
  3.7277 -#endif
  3.7278  
  3.7279      for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
  3.7280  
  3.7281 -#ifndef SQLITE_OMIT_INCRBLOB
  3.7282        /* If required, populate the overflow page-list cache. */
  3.7283 -      if( pCur->aOverflow ){
  3.7284 +      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
  3.7285          assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
  3.7286          pCur->aOverflow[iIdx] = nextPage;
  3.7287        }
  3.7288 -#endif
  3.7289  
  3.7290        if( offset>=ovflSize ){
  3.7291          /* The only reason to read this page is to obtain the page
  3.7292 @@ -54416,13 +55600,17 @@
  3.7293          ** data is not required. So first try to lookup the overflow
  3.7294          ** page-list cache, if any, then fall back to the getOverflowPage()
  3.7295          ** function.
  3.7296 +        **
  3.7297 +        ** Note that the aOverflow[] array must be allocated because eOp!=2
  3.7298 +        ** here.  If eOp==2, then offset==0 and this branch is never taken.
  3.7299          */
  3.7300 -#ifndef SQLITE_OMIT_INCRBLOB
  3.7301 -        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
  3.7302 +        assert( eOp!=2 );
  3.7303 +        assert( pCur->curFlags & BTCF_ValidOvfl );
  3.7304 +        if( pCur->aOverflow[iIdx+1] ){
  3.7305            nextPage = pCur->aOverflow[iIdx+1];
  3.7306 -        } else 
  3.7307 -#endif
  3.7308 +        }else{
  3.7309            rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
  3.7310 +        }
  3.7311          offset -= ovflSize;
  3.7312        }else{
  3.7313          /* Need to read this page properly. It contains some of the
  3.7314 @@ -54444,13 +55632,15 @@
  3.7315          **   3) the database is file-backed, and
  3.7316          **   4) there is no open write-transaction, and
  3.7317          **   5) the database is not a WAL database,
  3.7318 +        **   6) all data from the page is being read.
  3.7319          **
  3.7320          ** then data can be read directly from the database file into the
  3.7321          ** output buffer, bypassing the page-cache altogether. This speeds
  3.7322          ** up loading large records that span many overflow pages.
  3.7323          */
  3.7324 -        if( eOp==0                                             /* (1) */
  3.7325 +        if( (eOp&0x01)==0                                      /* (1) */
  3.7326           && offset==0                                          /* (2) */
  3.7327 +         && (bEnd || a==ovflSize)                              /* (6) */
  3.7328           && pBt->inTransaction==TRANS_READ                     /* (4) */
  3.7329           && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
  3.7330           && pBt->pPage1->aData[19]==0x01                       /* (5) */
  3.7331 @@ -54467,12 +55657,12 @@
  3.7332          {
  3.7333            DbPage *pDbPage;
  3.7334            rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
  3.7335 -              (eOp==0 ? PAGER_GET_READONLY : 0)
  3.7336 +              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
  3.7337            );
  3.7338            if( rc==SQLITE_OK ){
  3.7339              aPayload = sqlite3PagerGetData(pDbPage);
  3.7340              nextPage = get4byte(aPayload);
  3.7341 -            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
  3.7342 +            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
  3.7343              sqlite3PagerUnref(pDbPage);
  3.7344              offset = 0;
  3.7345            }
  3.7346 @@ -54541,10 +55731,10 @@
  3.7347  /*
  3.7348  ** Return a pointer to payload information from the entry that the 
  3.7349  ** pCur cursor is pointing to.  The pointer is to the beginning of
  3.7350 -** the key if skipKey==0 and it points to the beginning of data if
  3.7351 -** skipKey==1.  The number of bytes of available key/data is written
  3.7352 -** into *pAmt.  If *pAmt==0, then the value returned will not be
  3.7353 -** a valid pointer.
  3.7354 +** the key if index btrees (pPage->intKey==0) and is the data for
  3.7355 +** table btrees (pPage->intKey==1). The number of bytes of available
  3.7356 +** key/data is written into *pAmt.  If *pAmt==0, then the value
  3.7357 +** returned will not be a valid pointer.
  3.7358  **
  3.7359  ** This routine is an optimization.  It is common for the entire key
  3.7360  ** and data to fit on the local page and for there to be no overflow
  3.7361 @@ -54557,41 +55747,18 @@
  3.7362  ** page of the database.  The data might change or move the next time
  3.7363  ** any btree routine is called.
  3.7364  */
  3.7365 -static const unsigned char *fetchPayload(
  3.7366 +static const void *fetchPayload(
  3.7367    BtCursor *pCur,      /* Cursor pointing to entry to read from */
  3.7368 -  u32 *pAmt,           /* Write the number of available bytes here */
  3.7369 -  int skipKey          /* read beginning at data if this is true */
  3.7370 -){
  3.7371 -  unsigned char *aPayload;
  3.7372 -  MemPage *pPage;
  3.7373 -  u32 nKey;
  3.7374 -  u32 nLocal;
  3.7375 -
  3.7376 +  u32 *pAmt            /* Write the number of available bytes here */
  3.7377 +){
  3.7378    assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  3.7379    assert( pCur->eState==CURSOR_VALID );
  3.7380 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.7381    assert( cursorHoldsMutex(pCur) );
  3.7382 -  pPage = pCur->apPage[pCur->iPage];
  3.7383 -  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  3.7384 -  if( pCur->info.nSize==0 ){
  3.7385 -    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
  3.7386 -                   &pCur->info);
  3.7387 -  }
  3.7388 -  aPayload = pCur->info.pCell;
  3.7389 -  aPayload += pCur->info.nHeader;
  3.7390 -  if( pPage->intKey ){
  3.7391 -    nKey = 0;
  3.7392 -  }else{
  3.7393 -    nKey = (int)pCur->info.nKey;
  3.7394 -  }
  3.7395 -  if( skipKey ){
  3.7396 -    aPayload += nKey;
  3.7397 -    nLocal = pCur->info.nLocal - nKey;
  3.7398 -  }else{
  3.7399 -    nLocal = pCur->info.nLocal;
  3.7400 -    assert( nLocal<=nKey );
  3.7401 -  }
  3.7402 -  *pAmt = nLocal;
  3.7403 -  return aPayload;
  3.7404 +  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  3.7405 +  assert( pCur->info.nSize>0 );
  3.7406 +  *pAmt = pCur->info.nLocal;
  3.7407 +  return (void*)(pCur->info.pCell + pCur->info.nHeader);
  3.7408  }
  3.7409  
  3.7410  
  3.7411 @@ -54610,22 +55777,10 @@
  3.7412  ** in the common case where no overflow pages are used.
  3.7413  */
  3.7414  SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
  3.7415 -  const void *p = 0;
  3.7416 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.7417 -  assert( cursorHoldsMutex(pCur) );
  3.7418 -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  3.7419 -    p = (const void*)fetchPayload(pCur, pAmt, 0);
  3.7420 -  }
  3.7421 -  return p;
  3.7422 +  return fetchPayload(pCur, pAmt);
  3.7423  }
  3.7424  SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
  3.7425 -  const void *p = 0;
  3.7426 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.7427 -  assert( cursorHoldsMutex(pCur) );
  3.7428 -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  3.7429 -    p = (const void*)fetchPayload(pCur, pAmt, 1);
  3.7430 -  }
  3.7431 -  return p;
  3.7432 +  return fetchPayload(pCur, pAmt);
  3.7433  }
  3.7434  
  3.7435  
  3.7436 @@ -54652,14 +55807,14 @@
  3.7437      return SQLITE_CORRUPT_BKPT;
  3.7438    }
  3.7439    rc = getAndInitPage(pBt, newPgno, &pNewPage,
  3.7440 -               pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  3.7441 +               (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
  3.7442    if( rc ) return rc;
  3.7443    pCur->apPage[i+1] = pNewPage;
  3.7444    pCur->aiIdx[i+1] = 0;
  3.7445    pCur->iPage++;
  3.7446  
  3.7447    pCur->info.nSize = 0;
  3.7448 -  pCur->validNKey = 0;
  3.7449 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7450    if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
  3.7451      return SQLITE_CORRUPT_BKPT;
  3.7452    }
  3.7453 @@ -54717,7 +55872,7 @@
  3.7454    releasePage(pCur->apPage[pCur->iPage]);
  3.7455    pCur->iPage--;
  3.7456    pCur->info.nSize = 0;
  3.7457 -  pCur->validNKey = 0;
  3.7458 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7459  }
  3.7460  
  3.7461  /*
  3.7462 @@ -54744,8 +55899,6 @@
  3.7463  static int moveToRoot(BtCursor *pCur){
  3.7464    MemPage *pRoot;
  3.7465    int rc = SQLITE_OK;
  3.7466 -  Btree *p = pCur->pBtree;
  3.7467 -  BtShared *pBt = p->pBt;
  3.7468  
  3.7469    assert( cursorHoldsMutex(pCur) );
  3.7470    assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
  3.7471 @@ -54760,56 +55913,51 @@
  3.7472    }
  3.7473  
  3.7474    if( pCur->iPage>=0 ){
  3.7475 -    int i;
  3.7476 -    for(i=1; i<=pCur->iPage; i++){
  3.7477 -      releasePage(pCur->apPage[i]);
  3.7478 -    }
  3.7479 -    pCur->iPage = 0;
  3.7480 +    while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
  3.7481    }else if( pCur->pgnoRoot==0 ){
  3.7482      pCur->eState = CURSOR_INVALID;
  3.7483      return SQLITE_OK;
  3.7484    }else{
  3.7485 -    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0],
  3.7486 -                        pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  3.7487 +    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
  3.7488 +                 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
  3.7489      if( rc!=SQLITE_OK ){
  3.7490        pCur->eState = CURSOR_INVALID;
  3.7491        return rc;
  3.7492      }
  3.7493      pCur->iPage = 0;
  3.7494 -
  3.7495 -    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
  3.7496 -    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  3.7497 -    ** NULL, the caller expects a table b-tree. If this is not the case,
  3.7498 -    ** return an SQLITE_CORRUPT error.  */
  3.7499 -    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
  3.7500 -    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
  3.7501 -      return SQLITE_CORRUPT_BKPT;
  3.7502 -    }
  3.7503 -  }
  3.7504 -
  3.7505 -  /* Assert that the root page is of the correct type. This must be the
  3.7506 -  ** case as the call to this function that loaded the root-page (either
  3.7507 -  ** this call or a previous invocation) would have detected corruption 
  3.7508 -  ** if the assumption were not true, and it is not possible for the flags 
  3.7509 -  ** byte to have been modified while this cursor is holding a reference
  3.7510 -  ** to the page.  */
  3.7511 +  }
  3.7512    pRoot = pCur->apPage[0];
  3.7513    assert( pRoot->pgno==pCur->pgnoRoot );
  3.7514 -  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
  3.7515 +
  3.7516 +  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
  3.7517 +  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  3.7518 +  ** NULL, the caller expects a table b-tree. If this is not the case,
  3.7519 +  ** return an SQLITE_CORRUPT error. 
  3.7520 +  **
  3.7521 +  ** Earlier versions of SQLite assumed that this test could not fail
  3.7522 +  ** if the root page was already loaded when this function was called (i.e.
  3.7523 +  ** if pCur->iPage>=0). But this is not so if the database is corrupted 
  3.7524 +  ** in such a way that page pRoot is linked into a second b-tree table 
  3.7525 +  ** (or the freelist).  */
  3.7526 +  assert( pRoot->intKey==1 || pRoot->intKey==0 );
  3.7527 +  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
  3.7528 +    return SQLITE_CORRUPT_BKPT;
  3.7529 +  }
  3.7530  
  3.7531    pCur->aiIdx[0] = 0;
  3.7532    pCur->info.nSize = 0;
  3.7533 -  pCur->atLast = 0;
  3.7534 -  pCur->validNKey = 0;
  3.7535 -
  3.7536 -  if( pRoot->nCell==0 && !pRoot->leaf ){
  3.7537 +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7538 +
  3.7539 +  if( pRoot->nCell>0 ){
  3.7540 +    pCur->eState = CURSOR_VALID;
  3.7541 +  }else if( !pRoot->leaf ){
  3.7542      Pgno subpage;
  3.7543      if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
  3.7544      subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
  3.7545      pCur->eState = CURSOR_VALID;
  3.7546      rc = moveToChild(pCur, subpage);
  3.7547    }else{
  3.7548 -    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
  3.7549 +    pCur->eState = CURSOR_INVALID;
  3.7550    }
  3.7551    return rc;
  3.7552  }
  3.7553 @@ -54861,7 +56009,7 @@
  3.7554    if( rc==SQLITE_OK ){
  3.7555      pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
  3.7556      pCur->info.nSize = 0;
  3.7557 -    pCur->validNKey = 0;
  3.7558 +    pCur->curFlags &= ~BTCF_ValidNKey;
  3.7559    }
  3.7560    return rc;
  3.7561  }
  3.7562 @@ -54900,7 +56048,7 @@
  3.7563    assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.7564  
  3.7565    /* If the cursor already points to the last entry, this is a no-op. */
  3.7566 -  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
  3.7567 +  if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
  3.7568  #ifdef SQLITE_DEBUG
  3.7569      /* This block serves to assert() that the cursor really does point 
  3.7570      ** to the last entry in the b-tree. */
  3.7571 @@ -54923,7 +56071,12 @@
  3.7572        assert( pCur->eState==CURSOR_VALID );
  3.7573        *pRes = 0;
  3.7574        rc = moveToRightmost(pCur);
  3.7575 -      pCur->atLast = rc==SQLITE_OK ?1:0;
  3.7576 +      if( rc==SQLITE_OK ){
  3.7577 +        pCur->curFlags |= BTCF_AtLast;
  3.7578 +      }else{
  3.7579 +        pCur->curFlags &= ~BTCF_AtLast;
  3.7580 +      }
  3.7581 +   
  3.7582      }
  3.7583    }
  3.7584    return rc;
  3.7585 @@ -54965,6 +56118,7 @@
  3.7586    int *pRes                /* Write search results here */
  3.7587  ){
  3.7588    int rc;
  3.7589 +  RecordCompare xRecordCompare;
  3.7590  
  3.7591    assert( cursorHoldsMutex(pCur) );
  3.7592    assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.7593 @@ -54973,19 +56127,30 @@
  3.7594  
  3.7595    /* If the cursor is already positioned at the point we are trying
  3.7596    ** to move to, then just return without doing any work */
  3.7597 -  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
  3.7598 +  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  3.7599     && pCur->apPage[0]->intKey 
  3.7600    ){
  3.7601      if( pCur->info.nKey==intKey ){
  3.7602        *pRes = 0;
  3.7603        return SQLITE_OK;
  3.7604      }
  3.7605 -    if( pCur->atLast && pCur->info.nKey<intKey ){
  3.7606 +    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
  3.7607        *pRes = -1;
  3.7608        return SQLITE_OK;
  3.7609      }
  3.7610    }
  3.7611  
  3.7612 +  if( pIdxKey ){
  3.7613 +    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
  3.7614 +    pIdxKey->isCorrupt = 0;
  3.7615 +    assert( pIdxKey->default_rc==1 
  3.7616 +         || pIdxKey->default_rc==0 
  3.7617 +         || pIdxKey->default_rc==-1
  3.7618 +    );
  3.7619 +  }else{
  3.7620 +    xRecordCompare = 0; /* All keys are integers */
  3.7621 +  }
  3.7622 +
  3.7623    rc = moveToRoot(pCur);
  3.7624    if( rc ){
  3.7625      return rc;
  3.7626 @@ -55018,7 +56183,7 @@
  3.7627      assert( biasRight==0 || biasRight==1 );
  3.7628      idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
  3.7629      pCur->aiIdx[pCur->iPage] = (u16)idx;
  3.7630 -    if( pPage->intKey ){
  3.7631 +    if( xRecordCompare==0 ){
  3.7632        for(;;){
  3.7633          i64 nCellKey;
  3.7634          pCell = findCell(pPage, idx) + pPage->childPtrSize;
  3.7635 @@ -55036,7 +56201,7 @@
  3.7636            if( lwr>upr ){ c = +1; break; }
  3.7637          }else{
  3.7638            assert( nCellKey==intKey );
  3.7639 -          pCur->validNKey = 1;
  3.7640 +          pCur->curFlags |= BTCF_ValidNKey;
  3.7641            pCur->info.nKey = nCellKey;
  3.7642            pCur->aiIdx[pCur->iPage] = (u16)idx;
  3.7643            if( !pPage->leaf ){
  3.7644 @@ -55065,22 +56230,19 @@
  3.7645          ** 2 bytes of the cell.
  3.7646          */
  3.7647          nCell = pCell[0];
  3.7648 -        if( nCell<=pPage->max1bytePayload
  3.7649 -         /* && (pCell+nCell)<pPage->aDataEnd */
  3.7650 -        ){
  3.7651 +        if( nCell<=pPage->max1bytePayload ){
  3.7652            /* This branch runs if the record-size field of the cell is a
  3.7653            ** single byte varint and the record fits entirely on the main
  3.7654            ** b-tree page.  */
  3.7655            testcase( pCell+nCell+1==pPage->aDataEnd );
  3.7656 -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  3.7657 +          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
  3.7658          }else if( !(pCell[1] & 0x80) 
  3.7659            && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  3.7660 -          /* && (pCell+nCell+2)<=pPage->aDataEnd */
  3.7661          ){
  3.7662            /* The record-size field is a 2 byte varint and the record 
  3.7663            ** fits entirely on the main b-tree page.  */
  3.7664            testcase( pCell+nCell+2==pPage->aDataEnd );
  3.7665 -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  3.7666 +          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
  3.7667          }else{
  3.7668            /* The record flows over onto one or more overflow pages. In
  3.7669            ** this case the whole cell needs to be parsed, a buffer allocated
  3.7670 @@ -55096,14 +56258,15 @@
  3.7671              goto moveto_finish;
  3.7672            }
  3.7673            pCur->aiIdx[pCur->iPage] = (u16)idx;
  3.7674 -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
  3.7675 +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
  3.7676            if( rc ){
  3.7677              sqlite3_free(pCellKey);
  3.7678              goto moveto_finish;
  3.7679            }
  3.7680 -          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
  3.7681 +          c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
  3.7682            sqlite3_free(pCellKey);
  3.7683          }
  3.7684 +        assert( pIdxKey->isCorrupt==0 || c==0 );
  3.7685          if( c<0 ){
  3.7686            lwr = idx+1;
  3.7687          }else if( c>0 ){
  3.7688 @@ -55113,6 +56276,7 @@
  3.7689            *pRes = 0;
  3.7690            rc = SQLITE_OK;
  3.7691            pCur->aiIdx[pCur->iPage] = (u16)idx;
  3.7692 +          if( pIdxKey->isCorrupt ) rc = SQLITE_CORRUPT;
  3.7693            goto moveto_finish;
  3.7694          }
  3.7695          if( lwr>upr ) break;
  3.7696 @@ -55141,7 +56305,7 @@
  3.7697    }
  3.7698  moveto_finish:
  3.7699    pCur->info.nSize = 0;
  3.7700 -  pCur->validNKey = 0;
  3.7701 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7702    return rc;
  3.7703  }
  3.7704  
  3.7705 @@ -55166,6 +56330,15 @@
  3.7706  ** successful then set *pRes=0.  If the cursor
  3.7707  ** was already pointing to the last entry in the database before
  3.7708  ** this routine was called, then set *pRes=1.
  3.7709 +**
  3.7710 +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
  3.7711 +** will be 1 if the cursor being stepped corresponds to an SQL index and
  3.7712 +** if this routine could have been skipped if that SQL index had been
  3.7713 +** a unique index.  Otherwise the caller will have set *pRes to zero.
  3.7714 +** Zero is the common case. The btree implementation is free to use the
  3.7715 +** initial *pRes value as a hint to improve performance, but the current
  3.7716 +** SQLite btree implementation does not. (Note that the comdb2 btree
  3.7717 +** implementation does use this hint, however.)
  3.7718  */
  3.7719  SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  3.7720    int rc;
  3.7721 @@ -55174,8 +56347,10 @@
  3.7722  
  3.7723    assert( cursorHoldsMutex(pCur) );
  3.7724    assert( pRes!=0 );
  3.7725 +  assert( *pRes==0 || *pRes==1 );
  3.7726    assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  3.7727    if( pCur->eState!=CURSOR_VALID ){
  3.7728 +    invalidateOverflowCache(pCur);
  3.7729      rc = restoreCursorPosition(pCur);
  3.7730      if( rc!=SQLITE_OK ){
  3.7731        *pRes = 0;
  3.7732 @@ -55209,7 +56384,7 @@
  3.7733    testcase( idx>pPage->nCell );
  3.7734  
  3.7735    pCur->info.nSize = 0;
  3.7736 -  pCur->validNKey = 0;
  3.7737 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7738    if( idx>=pPage->nCell ){
  3.7739      if( !pPage->leaf ){
  3.7740        rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
  3.7741 @@ -55252,6 +56427,15 @@
  3.7742  ** successful then set *pRes=0.  If the cursor
  3.7743  ** was already pointing to the first entry in the database before
  3.7744  ** this routine was called, then set *pRes=1.
  3.7745 +**
  3.7746 +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
  3.7747 +** will be 1 if the cursor being stepped corresponds to an SQL index and
  3.7748 +** if this routine could have been skipped if that SQL index had been
  3.7749 +** a unique index.  Otherwise the caller will have set *pRes to zero.
  3.7750 +** Zero is the common case. The btree implementation is free to use the
  3.7751 +** initial *pRes value as a hint to improve performance, but the current
  3.7752 +** SQLite btree implementation does not. (Note that the comdb2 btree
  3.7753 +** implementation does use this hint, however.)
  3.7754  */
  3.7755  SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  3.7756    int rc;
  3.7757 @@ -55259,8 +56443,9 @@
  3.7758  
  3.7759    assert( cursorHoldsMutex(pCur) );
  3.7760    assert( pRes!=0 );
  3.7761 +  assert( *pRes==0 || *pRes==1 );
  3.7762    assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  3.7763 -  pCur->atLast = 0;
  3.7764 +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl);
  3.7765    if( pCur->eState!=CURSOR_VALID ){
  3.7766      if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
  3.7767        rc = btreeRestoreCursorPosition(pCur);
  3.7768 @@ -55305,7 +56490,7 @@
  3.7769        moveToParent(pCur);
  3.7770      }
  3.7771      pCur->info.nSize = 0;
  3.7772 -    pCur->validNKey = 0;
  3.7773 +    pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  3.7774  
  3.7775      pCur->aiIdx[pCur->iPage]--;
  3.7776      pPage = pCur->apPage[pCur->iPage];
  3.7777 @@ -55638,6 +56823,7 @@
  3.7778    if( rc==SQLITE_OK ){
  3.7779      if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
  3.7780        releasePage(*ppPage);
  3.7781 +      *ppPage = 0;
  3.7782        return SQLITE_CORRUPT_BKPT;
  3.7783      }
  3.7784      (*ppPage)->isInit = 0;
  3.7785 @@ -55899,7 +57085,7 @@
  3.7786      nHeader += 4;
  3.7787    }
  3.7788    if( pPage->hasData ){
  3.7789 -    nHeader += putVarint(&pCell[nHeader], nData+nZero);
  3.7790 +    nHeader += putVarint32(&pCell[nHeader], nData+nZero);
  3.7791    }else{
  3.7792      nData = nZero = 0;
  3.7793    }
  3.7794 @@ -56027,7 +57213,6 @@
  3.7795    u32 pc;         /* Offset to cell content of cell being deleted */
  3.7796    u8 *data;       /* pPage->aData */
  3.7797    u8 *ptr;        /* Used to move bytes around within data[] */
  3.7798 -  u8 *endPtr;     /* End of loop */
  3.7799    int rc;         /* The return code */
  3.7800    int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  3.7801  
  3.7802 @@ -56052,13 +57237,8 @@
  3.7803      *pRC = rc;
  3.7804      return;
  3.7805    }
  3.7806 -  endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
  3.7807 -  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  3.7808 -  while( ptr<endPtr ){
  3.7809 -    *(u16*)ptr = *(u16*)&ptr[2];
  3.7810 -    ptr += 2;
  3.7811 -  }
  3.7812    pPage->nCell--;
  3.7813 +  memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
  3.7814    put2byte(&data[hdr+3], pPage->nCell);
  3.7815    pPage->nFree += 2;
  3.7816  }
  3.7817 @@ -56095,15 +57275,13 @@
  3.7818    int ins;          /* Index in data[] where new cell pointer is inserted */
  3.7819    int cellOffset;   /* Address of first cell pointer in data[] */
  3.7820    u8 *data;         /* The content of the whole page */
  3.7821 -  u8 *ptr;          /* Used for moving information around in data[] */
  3.7822 -  u8 *endPtr;       /* End of the loop */
  3.7823 -
  3.7824    int nSkip = (iChild ? 4 : 0);
  3.7825  
  3.7826    if( *pRC ) return;
  3.7827  
  3.7828    assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  3.7829 -  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
  3.7830 +  assert( MX_CELL(pPage->pBt)<=10921 );
  3.7831 +  assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  3.7832    assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  3.7833    assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  3.7834    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  3.7835 @@ -56148,13 +57326,7 @@
  3.7836      if( iChild ){
  3.7837        put4byte(&data[idx], iChild);
  3.7838      }
  3.7839 -    ptr = &data[end];
  3.7840 -    endPtr = &data[ins];
  3.7841 -    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  3.7842 -    while( ptr>endPtr ){
  3.7843 -      *(u16*)ptr = *(u16*)&ptr[-2];
  3.7844 -      ptr -= 2;
  3.7845 -    }
  3.7846 +    memmove(&data[ins+2], &data[ins], end-ins);
  3.7847      put2byte(&data[ins], idx);
  3.7848      put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
  3.7849  #ifndef SQLITE_OMIT_AUTOVACUUM
  3.7850 @@ -57344,7 +58516,7 @@
  3.7851    }
  3.7852  
  3.7853    assert( cursorHoldsMutex(pCur) );
  3.7854 -  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
  3.7855 +  assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE
  3.7856                && (pBt->btsFlags & BTS_READ_ONLY)==0 );
  3.7857    assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  3.7858  
  3.7859 @@ -57369,11 +58541,17 @@
  3.7860    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
  3.7861    if( rc ) return rc;
  3.7862  
  3.7863 -  /* If this is an insert into a table b-tree, invalidate any incrblob 
  3.7864 -  ** cursors open on the row being replaced (assuming this is a replace
  3.7865 -  ** operation - if it is not, the following is a no-op).  */
  3.7866    if( pCur->pKeyInfo==0 ){
  3.7867 +    /* If this is an insert into a table b-tree, invalidate any incrblob 
  3.7868 +    ** cursors open on the row being replaced */
  3.7869      invalidateIncrblobCursors(p, nKey, 0);
  3.7870 +
  3.7871 +    /* If the cursor is currently on the last row and we are appending a
  3.7872 +    ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
  3.7873 +    ** call */
  3.7874 +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){
  3.7875 +      loc = -1;
  3.7876 +    }
  3.7877    }
  3.7878  
  3.7879    if( !loc ){
  3.7880 @@ -57424,7 +58602,7 @@
  3.7881  
  3.7882    /* If no error has occurred and pPage has an overflow cell, call balance() 
  3.7883    ** to redistribute the cells within the tree. Since balance() may move
  3.7884 -  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
  3.7885 +  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
  3.7886    ** variables.
  3.7887    **
  3.7888    ** Previous versions of SQLite called moveToRoot() to move the cursor
  3.7889 @@ -57443,8 +58621,8 @@
  3.7890    ** row without seeking the cursor. This can be a big performance boost.
  3.7891    */
  3.7892    pCur->info.nSize = 0;
  3.7893 -  pCur->validNKey = 0;
  3.7894    if( rc==SQLITE_OK && pPage->nOverflow ){
  3.7895 +    pCur->curFlags &= ~(BTCF_ValidNKey);
  3.7896      rc = balance(pCur);
  3.7897  
  3.7898      /* Must make sure nOverflow is reset to zero even if the balance()
  3.7899 @@ -57476,7 +58654,7 @@
  3.7900    assert( cursorHoldsMutex(pCur) );
  3.7901    assert( pBt->inTransaction==TRANS_WRITE );
  3.7902    assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  3.7903 -  assert( pCur->wrFlag );
  3.7904 +  assert( pCur->curFlags & BTCF_WriteFlag );
  3.7905    assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  3.7906    assert( !hasReadConflicts(p, pCur->pgnoRoot) );
  3.7907  
  3.7908 @@ -57499,7 +58677,7 @@
  3.7909    ** sub-tree headed by the child page of the cell being deleted. This makes
  3.7910    ** balancing the tree following the delete operation easier.  */
  3.7911    if( !pPage->leaf ){
  3.7912 -    int notUsed;
  3.7913 +    int notUsed = 0;
  3.7914      rc = sqlite3BtreePrevious(pCur, &notUsed);
  3.7915      if( rc ) return rc;
  3.7916    }
  3.7917 @@ -57752,6 +58930,7 @@
  3.7918    int rc;
  3.7919    unsigned char *pCell;
  3.7920    int i;
  3.7921 +  int hdr;
  3.7922  
  3.7923    assert( sqlite3_mutex_held(pBt->mutex) );
  3.7924    if( pgno>btreePagecount(pBt) ){
  3.7925 @@ -57760,6 +58939,7 @@
  3.7926  
  3.7927    rc = getAndInitPage(pBt, pgno, &pPage, 0);
  3.7928    if( rc ) return rc;
  3.7929 +  hdr = pPage->hdrOffset;
  3.7930    for(i=0; i<pPage->nCell; i++){
  3.7931      pCell = findCell(pPage, i);
  3.7932      if( !pPage->leaf ){
  3.7933 @@ -57770,7 +58950,7 @@
  3.7934      if( rc ) goto cleardatabasepage_out;
  3.7935    }
  3.7936    if( !pPage->leaf ){
  3.7937 -    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
  3.7938 +    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
  3.7939      if( rc ) goto cleardatabasepage_out;
  3.7940    }else if( pnChange ){
  3.7941      assert( pPage->intKey );
  3.7942 @@ -57779,7 +58959,7 @@
  3.7943    if( freePageFlag ){
  3.7944      freePage(pPage, &rc);
  3.7945    }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
  3.7946 -    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
  3.7947 +    zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
  3.7948    }
  3.7949  
  3.7950  cleardatabasepage_out:
  3.7951 @@ -57820,6 +59000,15 @@
  3.7952  }
  3.7953  
  3.7954  /*
  3.7955 +** Delete all information from the single table that pCur is open on.
  3.7956 +**
  3.7957 +** This routine only work for pCur on an ephemeral table.
  3.7958 +*/
  3.7959 +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
  3.7960 +  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
  3.7961 +}
  3.7962 +
  3.7963 +/*
  3.7964  ** Erase all information in a table and add the root of the table to
  3.7965  ** the freelist.  Except, the root of the principle table (the one on
  3.7966  ** page 1) is never added to the freelist.
  3.7967 @@ -58116,7 +59305,7 @@
  3.7968      sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
  3.7969    }
  3.7970    if( zMsg1 ){
  3.7971 -    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
  3.7972 +    sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
  3.7973    }
  3.7974    sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
  3.7975    va_end(ap);
  3.7976 @@ -58778,7 +59967,7 @@
  3.7977    int rc;
  3.7978    assert( cursorHoldsMutex(pCsr) );
  3.7979    assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
  3.7980 -  assert( pCsr->isIncrblobHandle );
  3.7981 +  assert( pCsr->curFlags & BTCF_Incrblob );
  3.7982  
  3.7983    rc = restoreCursorPosition(pCsr);
  3.7984    if( rc!=SQLITE_OK ){
  3.7985 @@ -58807,7 +59996,7 @@
  3.7986    **   (d) there are no conflicting read-locks, and
  3.7987    **   (e) the cursor points at a valid row of an intKey table.
  3.7988    */
  3.7989 -  if( !pCsr->wrFlag ){
  3.7990 +  if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
  3.7991      return SQLITE_READONLY;
  3.7992    }
  3.7993    assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
  3.7994 @@ -58820,20 +60009,10 @@
  3.7995  }
  3.7996  
  3.7997  /* 
  3.7998 -** Set a flag on this cursor to cache the locations of pages from the 
  3.7999 -** overflow list for the current row. This is used by cursors opened
  3.8000 -** for incremental blob IO only.
  3.8001 -**
  3.8002 -** This function sets a flag only. The actual page location cache
  3.8003 -** (stored in BtCursor.aOverflow[]) is allocated and used by function
  3.8004 -** accessPayload() (the worker function for sqlite3BtreeData() and
  3.8005 -** sqlite3BtreePutData()).
  3.8006 -*/
  3.8007 -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
  3.8008 -  assert( cursorHoldsMutex(pCur) );
  3.8009 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  3.8010 -  invalidateOverflowCache(pCur);
  3.8011 -  pCur->isIncrblobHandle = 1;
  3.8012 +** Mark this cursor as an incremental blob cursor.
  3.8013 +*/
  3.8014 +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
  3.8015 +  pCur->curFlags |= BTCF_Incrblob;
  3.8016  }
  3.8017  #endif
  3.8018  
  3.8019 @@ -58882,6 +60061,13 @@
  3.8020    pCsr->hints = mask;
  3.8021  }
  3.8022  
  3.8023 +/*
  3.8024 +** Return true if the given Btree is read-only.
  3.8025 +*/
  3.8026 +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
  3.8027 +  return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
  3.8028 +}
  3.8029 +
  3.8030  /************** End of btree.c ***********************************************/
  3.8031  /************** Begin file backup.c ******************************************/
  3.8032  /*
  3.8033 @@ -59410,7 +60596,7 @@
  3.8034  
  3.8035            /* Sync the database file to disk. */
  3.8036            if( rc==SQLITE_OK ){
  3.8037 -            rc = sqlite3PagerSync(pDestPager);
  3.8038 +            rc = sqlite3PagerSync(pDestPager, 0);
  3.8039            }
  3.8040          }else{
  3.8041            sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
  3.8042 @@ -59485,10 +60671,10 @@
  3.8043  
  3.8044    /* Set the error code of the destination database handle. */
  3.8045    rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
  3.8046 -  sqlite3Error(p->pDestDb, rc, 0);
  3.8047 -
  3.8048 -  /* Exit the mutexes and free the backup context structure. */
  3.8049    if( p->pDestDb ){
  3.8050 +    sqlite3Error(p->pDestDb, rc, 0);
  3.8051 +
  3.8052 +    /* Exit the mutexes and free the backup context structure. */
  3.8053      sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
  3.8054    }
  3.8055    sqlite3BtreeLeave(p->pSrc);
  3.8056 @@ -59651,6 +60837,42 @@
  3.8057  ** name sqlite_value
  3.8058  */
  3.8059  
  3.8060 +#ifdef SQLITE_DEBUG
  3.8061 +/*
  3.8062 +** Check invariants on a Mem object.
  3.8063 +**
  3.8064 +** This routine is intended for use inside of assert() statements, like
  3.8065 +** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
  3.8066 +*/
  3.8067 +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
  3.8068 +  /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
  3.8069 +  ** function for Mem.z 
  3.8070 +  */
  3.8071 +  assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
  3.8072 +  assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
  3.8073 +
  3.8074 +  /* If p holds a string or blob, the Mem.z must point to exactly
  3.8075 +  ** one of the following:
  3.8076 +  **
  3.8077 +  **   (1) Memory in Mem.zMalloc and managed by the Mem object
  3.8078 +  **   (2) Memory to be freed using Mem.xDel
  3.8079 +  **   (3) An ephermal string or blob
  3.8080 +  **   (4) A static string or blob
  3.8081 +  */
  3.8082 +  if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
  3.8083 +    assert( 
  3.8084 +      ((p->z==p->zMalloc)? 1 : 0) +
  3.8085 +      ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
  3.8086 +      ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
  3.8087 +      ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
  3.8088 +    );
  3.8089 +  }
  3.8090 +
  3.8091 +  return 1;
  3.8092 +}
  3.8093 +#endif
  3.8094 +
  3.8095 +
  3.8096  /*
  3.8097  ** If pMem is an object with a valid string representation, this routine
  3.8098  ** ensures the internal encoding for the string representation is
  3.8099 @@ -59692,57 +60914,51 @@
  3.8100  
  3.8101  /*
  3.8102  ** Make sure pMem->z points to a writable alloca