IOSAD-103 merge in sync IOSAD-103
authorDirk Zimmermann <dz@pep.security>
Fri, 11 Jan 2019 06:00:08 +0100
branchIOSAD-103
changeset 3215724e09e831f1
parent 3185 a5c137f87a9c
parent 3214 79dacdb24a4b
child 3219 96ff8b710a0b
IOSAD-103 merge in sync
     1.1 --- a/default.conf	Wed Dec 12 09:04:03 2018 +0100
     1.2 +++ b/default.conf	Fri Jan 11 06:00:08 2019 +0100
     1.3 @@ -204,7 +204,7 @@
     1.4  # gpgconf is not available for old version of GPG, for example GPG 2.0.30. Override this variable, if you compile the engine for such an old version.
     1.5  GPG_CMD:=$(shell gpgconf --list-components | awk -F: '/^gpg:/ { print $$3; exit 0; }')
     1.6  
     1.7 -# Selects OpenPGP implementation. must be either `GPG` or `NETPGP`
     1.8 +# Selects OpenPGP implementation. must be either `GPG` or `NETPGP` or `SEQUOIA`
     1.9  OPENPGP=GPG
    1.10  
    1.11  # Path of libGPGME binary
    1.12 @@ -236,6 +236,11 @@
    1.13  NETPGP_INC=
    1.14  #NETPGP_INC=-I$(PREFIX)/include
    1.15  
    1.16 +SEQUOIA_CFLAGS=$(shell pkg-config --cflags-only-other sequoia)
    1.17 +SEQUOIA_LDFLAGS=$(shell pkg-config --libs-only-L --libs-only-other sequoia)
    1.18 +SEQUOIA_LIB=$(shell pkg-config --libs-only-l sequoia)
    1.19 +SEQUOIA_INC=$(shell pkg-config --cflags-only-I sequoia)
    1.20 +
    1.21  
    1.22  ######### OpenPGP #########
    1.23  # CppUnit library search flag
     2.1 --- a/src/Makefile	Wed Dec 12 09:04:03 2018 +0100
     2.2 +++ b/src/Makefile	Fri Jan 11 06:00:08 2019 +0100
     2.3 @@ -35,15 +35,20 @@
     2.4  endif
     2.5  
     2.6  ifeq ($(OPENPGP),GPG)
     2.7 -    NO_SOURCE+= pgp_netpgp.c
     2.8 +    NO_SOURCE+= pgp_netpgp.c pgp_sequoia.c
     2.9      CFLAGS+= -DUSE_GPG $(GPGME_INC) -DLIBGPGME=\"$(LIBGPGME)\"
    2.10      LDFLAGS+= $(GPGME_LIB)
    2.11      # No extra LDLIBS are needed here, because GPGME is dynamically loaded
    2.12  else ifeq ($(OPENPGP),NETPGP)
    2.13 -    NO_SOURCE+= pgp_gpg.c
    2.14 +    NO_SOURCE+= pgp_gpg.c pgp_sequoia.c
    2.15      CFLAGS+= -DUSE_NETPGP $(NETGPG_INC)
    2.16      LDFLAGS+= $(NETGPG_LIB)
    2.17      LDLIBS+= -lnetpgp -lcurl
    2.18 +else ifeq ($(OPENPGP),SEQUOIA)
    2.19 +    NO_SOURCE+= pgp_gpg.c pgp_netpgp.c
    2.20 +    CFLAGS+= -DUSE_SEQUOIA $(SEQUOIA_CFLAGS) $(SEQUOIA_INC)
    2.21 +    LDFLAGS+= $(SEQUOIA_LDFLAGS)
    2.22 +    LDLIBS+= $(SEQUOIA_LIB)
    2.23  else
    2.24      $(error Unknown OpenPGP library: $(OPENPGP))
    2.25  endif
     3.1 --- a/src/cryptotech.c	Wed Dec 12 09:04:03 2018 +0100
     3.2 +++ b/src/cryptotech.c	Fri Jan 11 06:00:08 2019 +0100
     3.3 @@ -8,6 +8,10 @@
     3.4  #else
     3.5  #ifdef USE_NETPGP
     3.6  #include "pgp_netpgp.h"
     3.7 +#else
     3.8 +#ifdef USE_SEQUOIA
     3.9 +#include "pgp_sequoia.h"
    3.10 +#endif
    3.11  #endif
    3.12  #endif
    3.13  // 
     4.1 --- a/src/keymanagement.c	Wed Dec 12 09:04:03 2018 +0100
     4.2 +++ b/src/keymanagement.c	Fri Jan 11 06:00:08 2019 +0100
     4.3 @@ -1140,9 +1140,13 @@
     4.4          identity->comm_type = PEP_ct_unknown;
     4.5      }
     4.6      
     4.7 -    status = set_identity(session, identity);
     4.8 -    if (status == PEP_STATUS_OK)
     4.9 -        status = set_as_pEp_user(session, identity);
    4.10 +    // We want to set an identity in the DB even if a key isn't found, but we have to preserve the status if
    4.11 +    // it's NOT ok
    4.12 +    PEP_STATUS set_id_status = set_identity(session, identity);
    4.13 +    if (set_id_status == PEP_STATUS_OK)
    4.14 +        set_id_status = set_as_pEp_user(session, identity);
    4.15 +
    4.16 +    status = (status == PEP_STATUS_OK ? set_id_status : status);
    4.17  
    4.18  pEp_free:    
    4.19      free(default_own_id);
     5.1 --- a/src/openpgp_compat.h	Wed Dec 12 09:04:03 2018 +0100
     5.2 +++ b/src/openpgp_compat.h	Fri Jan 11 06:00:08 2019 +0100
     5.3 @@ -24,6 +24,10 @@
     5.4  #else
     5.5  #ifdef USE_NETPGP
     5.6  #include "pgp_netpgp.h"
     5.7 +#else
     5.8 +#ifdef USE_SEQUOIA
     5.9 +#include "pgp_sequoia.h"
    5.10 +#endif
    5.11  #endif
    5.12  #endif    
    5.13      
     6.1 --- a/src/pEpEngine.h	Wed Dec 12 09:04:03 2018 +0100
     6.2 +++ b/src/pEpEngine.h	Fri Jan 11 06:00:08 2019 +0100
     6.3 @@ -162,6 +162,100 @@
     6.4  
     6.5  typedef int (*inject_sync_event_t)(SYNC_EVENT ev, void *management);
     6.6  
     6.7 +static inline const char *pep_status_to_string(PEP_STATUS status) {
     6.8 +    switch (status) {
     6.9 +    case PEP_STATUS_OK: return "PEP_STATUS_OK";
    6.10 +
    6.11 +    case PEP_INIT_CANNOT_LOAD_GPGME: return "PEP_INIT_CANNOT_LOAD_GPGME";
    6.12 +    case PEP_INIT_GPGME_INIT_FAILED: return "PEP_INIT_GPGME_INIT_FAILED";
    6.13 +    case PEP_INIT_NO_GPG_HOME: return "PEP_INIT_NO_GPG_HOME";
    6.14 +    case PEP_INIT_NETPGP_INIT_FAILED: return "PEP_INIT_NETPGP_INIT_FAILED";
    6.15 +    case PEP_INIT_CANNOT_DETERMINE_GPG_VERSION: return "PEP_INIT_CANNOT_DETERMINE_GPG_VERSION";
    6.16 +    case PEP_INIT_UNSUPPORTED_GPG_VERSION: return "PEP_INIT_UNSUPPORTED_GPG_VERSION";
    6.17 +    case PEP_INIT_CANNOT_CONFIG_GPG_AGENT: return "PEP_INIT_CANNOT_CONFIG_GPG_AGENT";
    6.18 +    case PEP_INIT_SQLITE3_WITHOUT_MUTEX: return "PEP_INIT_SQLITE3_WITHOUT_MUTEX";
    6.19 +    case PEP_INIT_CANNOT_OPEN_DB: return "PEP_INIT_CANNOT_OPEN_DB";
    6.20 +    case PEP_INIT_CANNOT_OPEN_SYSTEM_DB: return "PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
    6.21 +    case PEP_UNKNOWN_DB_ERROR: return "PEP_UNKNOWN_DB_ERROR";
    6.22 +    case PEP_KEY_NOT_FOUND: return "PEP_KEY_NOT_FOUND";
    6.23 +    case PEP_KEY_HAS_AMBIG_NAME: return "PEP_KEY_HAS_AMBIG_NAME";
    6.24 +    case PEP_GET_KEY_FAILED: return "PEP_GET_KEY_FAILED";
    6.25 +    case PEP_CANNOT_EXPORT_KEY: return "PEP_CANNOT_EXPORT_KEY";
    6.26 +    case PEP_CANNOT_EDIT_KEY: return "PEP_CANNOT_EDIT_KEY";
    6.27 +    case PEP_KEY_UNSUITABLE: return "PEP_KEY_UNSUITABLE";
    6.28 +    case PEP_MALFORMED_KEY_RESET_MSG: return "PEP_MALFORMED_KEY_RESET_MSG";
    6.29 +    case PEP_KEY_NOT_RESET: return "PEP_KEY_NOT_RESET";
    6.30 +
    6.31 +    case PEP_CANNOT_FIND_IDENTITY: return "PEP_CANNOT_FIND_IDENTITY";
    6.32 +    case PEP_CANNOT_SET_PERSON: return "PEP_CANNOT_SET_PERSON";
    6.33 +    case PEP_CANNOT_SET_PGP_KEYPAIR: return "PEP_CANNOT_SET_PGP_KEYPAIR";
    6.34 +    case PEP_CANNOT_SET_IDENTITY: return "PEP_CANNOT_SET_IDENTITY";
    6.35 +    case PEP_CANNOT_SET_TRUST: return "PEP_CANNOT_SET_TRUST";
    6.36 +    case PEP_KEY_BLACKLISTED: return "PEP_KEY_BLACKLISTED";
    6.37 +    case PEP_CANNOT_FIND_PERSON: return "PEP_CANNOT_FIND_PERSON";
    6.38 +
    6.39 +    case PEP_CANNOT_FIND_ALIAS: return "PEP_CANNOT_FIND_ALIAS";
    6.40 +    case PEP_CANNOT_SET_ALIAS: return "PEP_CANNOT_SET_ALIAS";
    6.41 +
    6.42 +    case PEP_UNENCRYPTED: return "PEP_UNENCRYPTED";
    6.43 +    case PEP_VERIFIED: return "PEP_VERIFIED";
    6.44 +    case PEP_DECRYPTED: return "PEP_DECRYPTED";
    6.45 +    case PEP_DECRYPTED_AND_VERIFIED: return "PEP_DECRYPTED_AND_VERIFIED";
    6.46 +    case PEP_DECRYPT_WRONG_FORMAT: return "PEP_DECRYPT_WRONG_FORMAT";
    6.47 +    case PEP_DECRYPT_NO_KEY: return "PEP_DECRYPT_NO_KEY";
    6.48 +    case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH: return "PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
    6.49 +    case PEP_VERIFY_NO_KEY: return "PEP_VERIFY_NO_KEY";
    6.50 +    case PEP_VERIFIED_AND_TRUSTED: return "PEP_VERIFIED_AND_TRUSTED";
    6.51 +    case PEP_CANNOT_REENCRYPT: return "PEP_CANNOT_REENCRYPT";
    6.52 +    case PEP_CANNOT_DECRYPT_UNKNOWN: return "PEP_CANNOT_DECRYPT_UNKNOWN";
    6.53 +
    6.54 +    case PEP_TRUSTWORD_NOT_FOUND: return "PEP_TRUSTWORD_NOT_FOUND";
    6.55 +    case PEP_TRUSTWORDS_FPR_WRONG_LENGTH: return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
    6.56 +    case PEP_TRUSTWORDS_DUPLICATE_FPR: return "PEP_TRUSTWORDS_DUPLICATE_FPR";
    6.57 +
    6.58 +    case PEP_CANNOT_CREATE_KEY: return "PEP_CANNOT_CREATE_KEY";
    6.59 +    case PEP_CANNOT_SEND_KEY: return "PEP_CANNOT_SEND_KEY";
    6.60 +
    6.61 +    case PEP_PHRASE_NOT_FOUND: return "PEP_PHRASE_NOT_FOUND";
    6.62 +
    6.63 +    case PEP_SEND_FUNCTION_NOT_REGISTERED: return "PEP_SEND_FUNCTION_NOT_REGISTERED";
    6.64 +    case PEP_CONTRAINTS_VIOLATED: return "PEP_CONTRAINTS_VIOLATED";
    6.65 +    case PEP_CANNOT_ENCODE: return "PEP_CANNOT_ENCODE";
    6.66 +
    6.67 +    case PEP_SYNC_NO_NOTIFY_CALLBACK: return "PEP_SYNC_NO_NOTIFY_CALLBACK";
    6.68 +    case PEP_SYNC_ILLEGAL_MESSAGE: return "PEP_SYNC_ILLEGAL_MESSAGE";
    6.69 +    case PEP_SYNC_NO_INJECT_CALLBACK: return "PEP_SYNC_NO_INJECT_CALLBACK";
    6.70 +    case PEP_SYNC_NO_CHANNEL: return "PEP_SYNC_NO_CHANNEL";
    6.71 +    case PEP_SYNC_CANNOT_ENCRYPT: return "PEP_SYNC_CANNOT_ENCRYPT";
    6.72 +    case PEP_SYNC_NO_MESSAGE_SEND_CALLBACK: return "PEP_SYNC_NO_MESSAGE_SEND_CALLBACK";
    6.73 +
    6.74 +    case PEP_CANNOT_INCREASE_SEQUENCE: return "PEP_CANNOT_INCREASE_SEQUENCE";
    6.75 +
    6.76 +    case PEP_STATEMACHINE_ERROR: return "PEP_STATEMACHINE_ERROR";
    6.77 +    case PEP_NO_TRUST: return "PEP_NO_TRUST";
    6.78 +    case PEP_STATEMACHINE_INVALID_STATE: return "PEP_STATEMACHINE_INVALID_STATE";
    6.79 +    case PEP_STATEMACHINE_INVALID_EVENT: return "PEP_STATEMACHINE_INVALID_EVENT";
    6.80 +    case PEP_STATEMACHINE_INVALID_CONDITION: return "PEP_STATEMACHINE_INVALID_CONDITION";
    6.81 +    case PEP_STATEMACHINE_INVALID_ACTION: return "PEP_STATEMACHINE_INVALID_ACTION";
    6.82 +    case PEP_STATEMACHINE_INHIBITED_EVENT: return "PEP_STATEMACHINE_INHIBITED_EVENT";
    6.83 +    case PEP_STATEMACHINE_CANNOT_SEND: return "PEP_STATEMACHINE_CANNOT_SEND";
    6.84 +
    6.85 +    case PEP_COMMIT_FAILED: return "PEP_COMMIT_FAILED";
    6.86 +    case PEP_MESSAGE_CONSUME: return "PEP_MESSAGE_CONSUME";
    6.87 +    case PEP_MESSAGE_IGNORE: return "PEP_MESSAGE_IGNORE";
    6.88 +
    6.89 +    case PEP_RECORD_NOT_FOUND: return "PEP_RECORD_NOT_FOUND";
    6.90 +    case PEP_CANNOT_CREATE_TEMP_FILE: return "PEP_CANNOT_CREATE_TEMP_FILE";
    6.91 +    case PEP_ILLEGAL_VALUE: return "PEP_ILLEGAL_VALUE";
    6.92 +    case PEP_BUFFER_TOO_SMALL: return "PEP_BUFFER_TOO_SMALL";
    6.93 +    case PEP_OUT_OF_MEMORY: return "PEP_OUT_OF_MEMORY";
    6.94 +    case PEP_UNKNOWN_ERROR: return "PEP_UNKNOWN_ERROR";
    6.95 +
    6.96 +    case PEP_VERSION_MISMATCH: return "PEP_VERSION_MISMATCH";
    6.97 +
    6.98 +    default: return "unknown status code";
    6.99 +    }
   6.100 +}
   6.101  
   6.102  // INIT_STATUS init() - initialize pEpEngine for a thread
   6.103  //
   6.104 @@ -535,6 +629,47 @@
   6.105      PEP_ct_pEp = 0xff
   6.106  } PEP_comm_type;
   6.107  
   6.108 +static inline const char *pep_comm_type_to_string(PEP_comm_type ct) {
   6.109 +    switch (ct) {
   6.110 +    case PEP_ct_unknown: return "unknown";
   6.111 +    case PEP_ct_no_encryption: return "no_encryption";
   6.112 +    case PEP_ct_no_encrypted_channel: return "no_encrypted_channel";
   6.113 +    case PEP_ct_key_not_found: return "key_not_found";
   6.114 +    case PEP_ct_key_expired: return "key_expired";
   6.115 +    case PEP_ct_key_revoked: return "key_revoked";
   6.116 +    case PEP_ct_key_b0rken: return "key_b0rken";
   6.117 +    case PEP_ct_my_key_not_included: return "my_key_not_included";
   6.118 +    case PEP_ct_security_by_obscurity: return "security_by_obscurity";
   6.119 +    case PEP_ct_b0rken_crypto: return "b0rken_crypto";
   6.120 +    case PEP_ct_key_too_short: return "key_too_short";
   6.121 +    case PEP_ct_compromised: return "compromised";
   6.122 +    case PEP_ct_mistrusted: return "mistrusted";
   6.123 +    case PEP_ct_unconfirmed_encryption: return "unconfirmed_encryption";
   6.124 +    case PEP_ct_OpenPGP_weak_unconfirmed: return "OpenPGP_weak_unconfirmed";
   6.125 +    case PEP_ct_to_be_checked: return "to_be_checked";
   6.126 +    case PEP_ct_SMIME_unconfirmed: return "SMIME_unconfirmed";
   6.127 +    case PEP_ct_CMS_unconfirmed: return "CMS_unconfirmed";
   6.128 +    case PEP_ct_strong_but_unconfirmed: return "strong_but_unconfirmed";
   6.129 +    case PEP_ct_OpenPGP_unconfirmed: return "OpenPGP_unconfirmed";
   6.130 +    case PEP_ct_OTR_unconfirmed: return "OTR_unconfirmed";
   6.131 +    case PEP_ct_unconfirmed_enc_anon: return "unconfirmed_enc_anon";
   6.132 +    case PEP_ct_pEp_unconfirmed: return "pEp_unconfirmed";
   6.133 +    case PEP_ct_confirmed: return "confirmed";
   6.134 +    case PEP_ct_confirmed_encryption: return "confirmed_encryption";
   6.135 +    case PEP_ct_OpenPGP_weak: return "OpenPGP_weak";
   6.136 +    case PEP_ct_to_be_checked_confirmed: return "to_be_checked_confirmed";
   6.137 +    case PEP_ct_SMIME: return "SMIME";
   6.138 +    case PEP_ct_CMS: return "CMS";
   6.139 +    case PEP_ct_strong_encryption: return "strong_encryption";
   6.140 +    case PEP_ct_OpenPGP: return "OpenPGP";
   6.141 +    case PEP_ct_OTR: return "OTR";
   6.142 +    case PEP_ct_confirmed_enc_anon: return "confirmed_enc_anon";
   6.143 +    case PEP_ct_pEp: return "pEp";
   6.144 +    default: return "invalid comm type";
   6.145 +    }
   6.146 +}
   6.147 +
   6.148 +
   6.149  typedef enum _identity_flags {
   6.150      // the first octet flags are app defined settings
   6.151      PEP_idf_not_for_sync = 0x0001,   // don't use this identity for sync
     7.1 --- a/src/pEp_internal.h	Wed Dec 12 09:04:03 2018 +0100
     7.2 +++ b/src/pEp_internal.h	Fri Jan 11 06:00:08 2019 +0100
     7.3 @@ -91,16 +91,20 @@
     7.4  #include "pEpEngine.h"
     7.5  
     7.6  // If not specified, build for GPG
     7.7 +#ifndef USE_SEQUOIA
     7.8  #ifndef USE_NETPGP
     7.9  #ifndef USE_GPG
    7.10  #define USE_GPG
    7.11  #endif
    7.12  #endif
    7.13 +#endif
    7.14  
    7.15  #ifdef USE_GPG
    7.16  #include "pgp_gpg_internal.h"
    7.17  #elif defined(USE_NETPGP)
    7.18  #include "pgp_netpgp_internal.h"
    7.19 +#elif defined(USE_SEQUOIA)
    7.20 +#include "pgp_sequoia_internal.h"
    7.21  #endif
    7.22  
    7.23  #include "keymanagement.h"
    7.24 @@ -123,6 +127,25 @@
    7.25      gpgme_ctx_t ctx;
    7.26  #elif defined(USE_NETPGP)
    7.27      pEpNetPGPSession ctx;
    7.28 +#elif defined(USE_SEQUOIA)
    7.29 +    sq_context_t ctx;
    7.30 +    sqlite3 *key_db;
    7.31 +    struct {
    7.32 +        sqlite3_stmt *begin_transaction;
    7.33 +        sqlite3_stmt *commit_transaction;
    7.34 +        sqlite3_stmt *rollback_transaction;
    7.35 +        sqlite3_stmt *tpk_find;
    7.36 +        sqlite3_stmt *tsk_find;
    7.37 +        sqlite3_stmt *tpk_find_by_keyid;
    7.38 +        sqlite3_stmt *tsk_find_by_keyid;
    7.39 +        sqlite3_stmt *tpk_find_by_email;
    7.40 +        sqlite3_stmt *tsk_find_by_email;
    7.41 +        sqlite3_stmt *tpk_all;
    7.42 +        sqlite3_stmt *tsk_all;
    7.43 +        sqlite3_stmt *tpk_save_insert_primary;
    7.44 +        sqlite3_stmt *tpk_save_insert_subkeys;
    7.45 +        sqlite3_stmt *tpk_save_insert_userids;
    7.46 +    } sq_sql;
    7.47  #endif
    7.48  
    7.49      PEP_cryptotech_t *cryptotech;
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/pgp_sequoia.c	Fri Jan 11 06:00:08 2019 +0100
     8.3 @@ -0,0 +1,2116 @@
     8.4 +// This file is under GNU General Public License 3.0
     8.5 +// see LICENSE.txt
     8.6 +
     8.7 +#define _GNU_SOURCE 1
     8.8 +
     8.9 +#include "platform.h"
    8.10 +#include "pEp_internal.h"
    8.11 +#include "pgp_gpg.h"
    8.12 +
    8.13 +#include <limits.h>
    8.14 +#include <sys/stat.h>
    8.15 +#include <sys/types.h>
    8.16 +#include <error.h>
    8.17 +
    8.18 +#include <sqlite3.h>
    8.19 +
    8.20 +#include "wrappers.h"
    8.21 +
    8.22 +#define TRACE 0
    8.23 +#ifndef TRACING
    8.24 +#  ifndef NDEBUG
    8.25 +#    define TRACING 0
    8.26 +#  else
    8.27 +#    define TRACING 1
    8.28 +#  endif
    8.29 +#endif
    8.30 +
    8.31 +// enable tracing if in debugging mode
    8.32 +#if TRACING
    8.33 +#  define _T(...) do {                          \
    8.34 +        fprintf(stderr, ##__VA_ARGS__);         \
    8.35 +    } while (0)
    8.36 +#else
    8.37 +#  define _T(...) do { } while (0)
    8.38 +#endif
    8.39 +
    8.40 +// Show the start of a tracepoint (i.e., don't print a newline).
    8.41 +#define TC(...) do {       \
    8.42 +    _T("%s: ", __func__);  \
    8.43 +    _T(__VA_ARGS__);       \
    8.44 +} while (0)
    8.45 +
    8.46 +// Show a trace point.
    8.47 +#  define T(...) do {  \
    8.48 +    TC(__VA_ARGS__); \
    8.49 +    _T("\n");          \
    8.50 +} while(0)
    8.51 +
    8.52 +// Verbosely displays errors.
    8.53 +#  define DUMP_ERR(__de_session, __de_status, ...) do {             \
    8.54 +    TC(__VA_ARGS__);                                                \
    8.55 +    _T(": ");                                                       \
    8.56 +    if ((__de_session->ctx)) {                                      \
    8.57 +        sq_error_t __de_err                                         \
    8.58 +            = sq_context_last_error((__de_session->ctx));           \
    8.59 +        if (__de_err)                                               \
    8.60 +            _T("Sequoia: %s => ", sq_error_string(__de_err));       \
    8.61 +        sq_error_free(__de_err);                                    \
    8.62 +    }                                                               \
    8.63 +    _T("%s\n", pep_status_to_string(__de_status));                  \
    8.64 +} while(0)
    8.65 +
    8.66 +// If __ec_status is an error, then disable the error, set 'status' to
    8.67 +// it, and jump to 'out'.
    8.68 +#define ERROR_OUT(__e_session, __ec_status, ...) do {               \
    8.69 +    PEP_STATUS ___ec_status = (__ec_status);                        \
    8.70 +    if ((___ec_status) != PEP_STATUS_OK) {                          \
    8.71 +        DUMP_ERR((__e_session), (___ec_status), ##__VA_ARGS__);     \
    8.72 +        status = (___ec_status);                                    \
    8.73 +        goto out;                                                   \
    8.74 +    }                                                               \
    8.75 +} while(0)
    8.76 +
    8.77 +PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    8.78 +{
    8.79 +    PEP_STATUS status = PEP_STATUS_OK;
    8.80 +
    8.81 +    sq_error_t err;
    8.82 +    session->ctx = sq_context_new("foundation.pep", &err);
    8.83 +    if (session->ctx == NULL)
    8.84 +        ERROR_OUT(session, PEP_INIT_GPGME_INIT_FAILED,
    8.85 +                  "initializing sequoia context");
    8.86 +
    8.87 +    // Create the home directory.
    8.88 +    char *home_env = getenv("HOME");
    8.89 +    if (!home_env)
    8.90 +        ERROR_OUT(session, PEP_INIT_GPGME_INIT_FAILED, "HOME unset");
    8.91 +
    8.92 +    // Create the DB and initialize it.
    8.93 +    char *path = NULL;
    8.94 +    asprintf(&path, "%s/.pEp_keys.db", home_env);
    8.95 +    if (!path)
    8.96 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
    8.97 +
    8.98 +    int sqlite_result;
    8.99 +    sqlite_result = sqlite3_open_v2(path,
   8.100 +                                    &session->key_db,
   8.101 +                                    SQLITE_OPEN_READWRITE
   8.102 +                                    | SQLITE_OPEN_CREATE
   8.103 +                                    | SQLITE_OPEN_FULLMUTEX
   8.104 +                                    | SQLITE_OPEN_PRIVATECACHE,
   8.105 +                                    NULL);
   8.106 +    free(path);
   8.107 +    if (sqlite_result != SQLITE_OK)
   8.108 +        ERROR_OUT(session, PEP_INIT_CANNOT_OPEN_DB,
   8.109 +                  "opening keys DB: %s",
   8.110 +                  sqlite3_errmsg(session->key_db));
   8.111 +
   8.112 +    sqlite_result = sqlite3_exec(session->key_db,
   8.113 +                                 "PRAGMA secure_delete=true;\n"
   8.114 +                                 "PRAGMA foreign_keys=true;\n"
   8.115 +                                 "PRAGMA locking_mode=NORMAL;\n"
   8.116 +                                 "PRAGMA journal_mode=WAL;\n",
   8.117 +                                 NULL, NULL, NULL);
   8.118 +    if (sqlite_result != SQLITE_OK)
   8.119 +        ERROR_OUT(session, PEP_INIT_CANNOT_OPEN_DB,
   8.120 +                  "setting pragmas: %s", sqlite3_errmsg(session->key_db));
   8.121 +
   8.122 +    sqlite3_busy_timeout(session->key_db, BUSY_WAIT_TIME);
   8.123 +
   8.124 +    sqlite_result = sqlite3_exec(session->key_db,
   8.125 +                                 "CREATE TABLE IF NOT EXISTS keys (\n"
   8.126 +                                 "   primary_key TEXT UNIQUE PRIMARY KEY,\n"
   8.127 +                                 "   secret BOOLEAN,\n"
   8.128 +                                 "   tpk BLOB\n"
   8.129 +                                 ");\n"
   8.130 +                                 "CREATE INDEX IF NOT EXISTS keys_index\n"
   8.131 +                                 "  ON keys (primary_key, secret)\n",
   8.132 +                                 NULL, NULL, NULL);
   8.133 +    if (sqlite_result != SQLITE_OK)
   8.134 +        ERROR_OUT(session, PEP_INIT_CANNOT_OPEN_DB,
   8.135 +                  "creating keys table: %s",
   8.136 +                  sqlite3_errmsg(session->key_db));
   8.137 +
   8.138 +    sqlite_result = sqlite3_exec(session->key_db,
   8.139 +                                 "CREATE TABLE IF NOT EXISTS subkeys (\n"
   8.140 +                                 "   subkey TEXT NOT NULL,\n"
   8.141 +                                 "   primary_key TEXT NOT NULL,\n"
   8.142 +                                 "   UNIQUE(subkey, primary_key),\n"
   8.143 +                                 "   FOREIGN KEY (primary_key)\n"
   8.144 +                                 "       REFERENCES keys(primary_key)\n"
   8.145 +                                 "     ON DELETE CASCADE\n"
   8.146 +                                 ");\n"
   8.147 +                                 "CREATE INDEX IF NOT EXISTS subkeys_index\n"
   8.148 +                                 "  ON subkeys (subkey, primary_key)\n",
   8.149 +                                 NULL, NULL, NULL);
   8.150 +    if (sqlite_result != SQLITE_OK)
   8.151 +        ERROR_OUT(session, PEP_INIT_CANNOT_OPEN_DB,
   8.152 +                  "creating subkeys table: %s",
   8.153 +                  sqlite3_errmsg(session->key_db));
   8.154 +
   8.155 +    sqlite_result = sqlite3_exec(session->key_db,
   8.156 +                                 "CREATE TABLE IF NOT EXISTS userids (\n"
   8.157 +                                 "   userid TEXT NOT NULL,\n"
   8.158 +                                 "   primary_key TEXT NOT NULL,\n"
   8.159 +                                 "   UNIQUE(userid, primary_key),\n"
   8.160 +                                 "   FOREIGN KEY (primary_key)\n"
   8.161 +                                 "       REFERENCES keys(primary_key)\n"
   8.162 +                                 "     ON DELETE CASCADE\n"
   8.163 +                                 ");\n"
   8.164 +                                 "CREATE INDEX IF NOT EXISTS userids_index\n"
   8.165 +                                 "  ON userids (userid, primary_key)\n",
   8.166 +                                 NULL, NULL, NULL);
   8.167 +    if (sqlite_result != SQLITE_OK)
   8.168 +        ERROR_OUT(session, PEP_INIT_CANNOT_OPEN_DB,
   8.169 +                  "creating userids table: %s",
   8.170 +                  sqlite3_errmsg(session->key_db));
   8.171 +
   8.172 +    sqlite_result
   8.173 +        = sqlite3_prepare_v2(session->key_db, "begin transaction",
   8.174 +                             -1, &session->sq_sql.begin_transaction, NULL);
   8.175 +    assert(sqlite_result == SQLITE_OK);
   8.176 +
   8.177 +    sqlite_result
   8.178 +        = sqlite3_prepare_v2(session->key_db, "commit transaction",
   8.179 +                             -1, &session->sq_sql.commit_transaction, NULL);
   8.180 +    assert(sqlite_result == SQLITE_OK);
   8.181 +
   8.182 +    sqlite_result
   8.183 +        = sqlite3_prepare_v2(session->key_db, "rollback transaction",
   8.184 +                             -1, &session->sq_sql.rollback_transaction, NULL);
   8.185 +    assert(sqlite_result == SQLITE_OK);
   8.186 +
   8.187 +    sqlite_result
   8.188 +        = sqlite3_prepare_v2(session->key_db,
   8.189 +                             "SELECT tpk, secret FROM keys"
   8.190 +                             " WHERE primary_key == ?",
   8.191 +                             -1, &session->sq_sql.tpk_find, NULL);
   8.192 +    assert(sqlite_result == SQLITE_OK);
   8.193 +
   8.194 +    sqlite_result
   8.195 +        = sqlite3_prepare_v2(session->key_db,
   8.196 +                             "SELECT tpk, secret FROM keys"
   8.197 +                             " WHERE primary_key == ? and secret == 1",
   8.198 +                             -1, &session->sq_sql.tsk_find, NULL);
   8.199 +    assert(sqlite_result == SQLITE_OK);
   8.200 +
   8.201 +    sqlite_result
   8.202 +        = sqlite3_prepare_v2(session->key_db,
   8.203 +                             "SELECT tpk, secret FROM subkeys"
   8.204 +                             " LEFT JOIN keys"
   8.205 +                             "  ON subkeys.primary_key == keys.primary_key"
   8.206 +                             " WHERE subkey == ?",
   8.207 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
   8.208 +    assert(sqlite_result == SQLITE_OK);
   8.209 +
   8.210 +    sqlite_result
   8.211 +        = sqlite3_prepare_v2(session->key_db,
   8.212 +                             "SELECT tpk, secret FROM subkeys"
   8.213 +                             " LEFT JOIN keys"
   8.214 +                             "  ON subkeys.primary_key == keys.primary_key"
   8.215 +                             " WHERE subkey == ?",
   8.216 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
   8.217 +    assert(sqlite_result == SQLITE_OK);
   8.218 +
   8.219 +    sqlite_result
   8.220 +        = sqlite3_prepare_v2(session->key_db,
   8.221 +                             "SELECT tpk, secret FROM subkeys"
   8.222 +                             " LEFT JOIN keys"
   8.223 +                             "  ON subkeys.primary_key == keys.primary_key"
   8.224 +                             " WHERE subkey == ? and keys.secret == 1",
   8.225 +                             -1, &session->sq_sql.tsk_find_by_keyid, NULL);
   8.226 +    assert(sqlite_result == SQLITE_OK);
   8.227 +
   8.228 +    sqlite_result
   8.229 +        = sqlite3_prepare_v2(session->key_db,
   8.230 +                             "SELECT tpk, secret FROM userids"
   8.231 +                             " LEFT JOIN keys"
   8.232 +                             "  ON userids.primary_key == keys.primary_key"
   8.233 +                             " WHERE userid == ?",
   8.234 +                             -1, &session->sq_sql.tpk_find_by_email, NULL);
   8.235 +    assert(sqlite_result == SQLITE_OK);
   8.236 +
   8.237 +    sqlite_result
   8.238 +        = sqlite3_prepare_v2(session->key_db,
   8.239 +                             "SELECT tpk, secret FROM userids"
   8.240 +                             " LEFT JOIN keys"
   8.241 +                             "  ON userids.primary_key == keys.primary_key"
   8.242 +                             " WHERE userid == ? and keys.secret == 1",
   8.243 +                             -1, &session->sq_sql.tsk_find_by_email, NULL);
   8.244 +    assert(sqlite_result == SQLITE_OK);
   8.245 +
   8.246 +    sqlite_result
   8.247 +        = sqlite3_prepare_v2(session->key_db,
   8.248 +                             "select tpk, secret from keys",
   8.249 +                             -1, &session->sq_sql.tpk_all, NULL);
   8.250 +    assert(sqlite_result == SQLITE_OK);
   8.251 +
   8.252 +    sqlite_result
   8.253 +        = sqlite3_prepare_v2(session->key_db,
   8.254 +                             "select tpk, secret from keys where secret = 1",
   8.255 +                             -1, &session->sq_sql.tsk_all, NULL);
   8.256 +    assert(sqlite_result == SQLITE_OK);
   8.257 +
   8.258 +    sqlite_result
   8.259 +        = sqlite3_prepare_v2(session->key_db,
   8.260 +                             "INSERT OR REPLACE INTO keys"
   8.261 +                             "   (primary_key, secret, tpk)"
   8.262 +                             " VALUES (?, ?, ?)",
   8.263 +                             -1, &session->sq_sql.tpk_save_insert_primary, NULL);
   8.264 +    assert(sqlite_result == SQLITE_OK);
   8.265 +
   8.266 +    sqlite_result
   8.267 +        = sqlite3_prepare_v2(session->key_db,
   8.268 +                             "INSERT OR REPLACE INTO subkeys"
   8.269 +                             "   (subkey, primary_key)"
   8.270 +                             " VALUES (?, ?)",
   8.271 +                             -1, &session->sq_sql.tpk_save_insert_subkeys, NULL);
   8.272 +    assert(sqlite_result == SQLITE_OK);
   8.273 +
   8.274 +    sqlite_result
   8.275 +        = sqlite3_prepare_v2(session->key_db,
   8.276 +                             "INSERT OR REPLACE INTO userids"
   8.277 +                             "   (userid, primary_key)"
   8.278 +                             " VALUES (?, ?)",
   8.279 +                             -1, &session->sq_sql.tpk_save_insert_userids, NULL);
   8.280 +    assert(sqlite_result == SQLITE_OK);
   8.281 +
   8.282 + out:
   8.283 +    if (status != PEP_STATUS_OK)
   8.284 +        pgp_release(session, in_first);
   8.285 +    return status;
   8.286 +}
   8.287 +
   8.288 +void pgp_release(PEP_SESSION session, bool out_last)
   8.289 +{
   8.290 +    sqlite3_stmt **stmts = (sqlite3_stmt **) &session->sq_sql;
   8.291 +    for (int i = 0; i < sizeof(session->sq_sql) / sizeof(*stmts); i ++)
   8.292 +        if (stmts[i]) {
   8.293 +            sqlite3_finalize(stmts[i]);
   8.294 +            stmts[i] = NULL;
   8.295 +        }
   8.296 +
   8.297 +    if (session->key_db) {
   8.298 +        int result = sqlite3_close_v2(session->key_db);
   8.299 +        if (result != 0)
   8.300 +            DUMP_ERR(session, PEP_UNKNOWN_ERROR,
   8.301 +                     "Closing key DB: sqlite3_close_v2: %s",
   8.302 +                     sqlite3_errstr(result));
   8.303 +        session->key_db = NULL;
   8.304 +    }
   8.305 +
   8.306 +    if (session->ctx) {
   8.307 +        sq_context_free(session->ctx);
   8.308 +        session->ctx = NULL;
   8.309 +    }
   8.310 +}
   8.311 +
   8.312 +// Ensures that a fingerprint is in canonical form.  A canonical
   8.313 +// fingerprint doesn't contain any white space.
   8.314 +//
   8.315 +// This function does *not* consume fpr.
   8.316 +static char *sq_fingerprint_canonicalize(const char *) __attribute__((nonnull));
   8.317 +static char *sq_fingerprint_canonicalize(const char *fpr)
   8.318 +{
   8.319 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
   8.320 +    char *fpr_canonicalized = sq_fingerprint_to_hex(sq_fpr);
   8.321 +    sq_fingerprint_free(sq_fpr);
   8.322 +
   8.323 +    return fpr_canonicalized;
   8.324 +}
   8.325 +
   8.326 +// Splits an OpenPGP user id into its name and email components.  A
   8.327 +// user id looks like:
   8.328 +//
   8.329 +//   Name (comment) <email>
   8.330 +//
   8.331 +// This function takes ownership of user_id!!!
   8.332 +//
   8.333 +// namep and emailp may be NULL if they are not required.
   8.334 +static void user_id_split(char *, char **, char **) __attribute__((nonnull(1)));
   8.335 +static void user_id_split(char *user_id, char **namep, char **emailp)
   8.336 +{
   8.337 +    if (namep)
   8.338 +        *namep = NULL;
   8.339 +    if (emailp)
   8.340 +        *emailp = NULL;
   8.341 +
   8.342 +    char *email = strchr(user_id, '<');
   8.343 +    if (email) {
   8.344 +        // NUL terminate the string here so that user_id now points at
   8.345 +        // most to: "Name (comment)"
   8.346 +        *email = 0;
   8.347 +
   8.348 +        if (emailp && email[1]) {
   8.349 +            email = email + 1;
   8.350 +            char *end = strchr(email, '>');
   8.351 +            if (end) {
   8.352 +                *end = 0;
   8.353 +                *emailp = strdup(email);
   8.354 +            }
   8.355 +        }
   8.356 +    }
   8.357 +
   8.358 +    if (!namep)
   8.359 +        return;
   8.360 +
   8.361 +    char *comment = strchr(user_id, '(');
   8.362 +    if (comment)
   8.363 +        *comment = 0;
   8.364 +
   8.365 +    // Kill any trailing white space.
   8.366 +    for (size_t l = strlen(user_id); l > 0 && user_id[l - 1] == ' '; l --)
   8.367 +        user_id[l - 1] = 0;
   8.368 +
   8.369 +    // Kill any leading whitespace.
   8.370 +    char *start = user_id;
   8.371 +    while (*start == ' ')
   8.372 +        start ++;
   8.373 +    if (start[0])
   8.374 +        *namep = strdup(start);
   8.375 +
   8.376 +    free(user_id);
   8.377 +}
   8.378 +
   8.379 +// step statement and load the tpk and secret.
   8.380 +static PEP_STATUS key_load(PEP_SESSION, sqlite3_stmt *, sq_tpk_t *, int *)
   8.381 +    __attribute__((nonnull(1, 2)));
   8.382 +static PEP_STATUS key_load(PEP_SESSION session, sqlite3_stmt *stmt,
   8.383 +                           sq_tpk_t *tpkp, int *secretp)
   8.384 +{
   8.385 +    PEP_STATUS status = PEP_STATUS_OK;
   8.386 +    int sqlite_result = sqlite3_step(stmt);
   8.387 +    switch (sqlite_result) {
   8.388 +    case SQLITE_ROW:
   8.389 +        if (tpkp) {
   8.390 +            int data_len = sqlite3_column_bytes(stmt, 0);
   8.391 +            const void *data = sqlite3_column_blob(stmt, 0);
   8.392 +
   8.393 +            *tpkp = sq_tpk_from_bytes(session->ctx, data, data_len);
   8.394 +            if (!*tpkp)
   8.395 +                ERROR_OUT(session, PEP_GET_KEY_FAILED, "parsing TPK");
   8.396 +        }
   8.397 +
   8.398 +        if (secretp)
   8.399 +            *secretp = sqlite3_column_int(stmt, 1);
   8.400 +
   8.401 +        break;
   8.402 +    case SQLITE_DONE:
   8.403 +        // Got nothing.
   8.404 +        status = PEP_KEY_NOT_FOUND;
   8.405 +        break;
   8.406 +    default:
   8.407 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.408 +                  "stepping: %s", sqlite3_errmsg(session->key_db));
   8.409 +    }
   8.410 +
   8.411 + out:
   8.412 +    T(" -> %s", pep_status_to_string(status));
   8.413 +    return status;
   8.414 +}
   8.415 +
   8.416 +// step statement until exhausted and load the tpks.
   8.417 +static PEP_STATUS key_loadn(PEP_SESSION, sqlite3_stmt *, sq_tpk_t **, int *)
   8.418 +    __attribute__((nonnull));
   8.419 +static PEP_STATUS key_loadn(PEP_SESSION session, sqlite3_stmt *stmt,
   8.420 +                            sq_tpk_t **tpksp, int *tpks_countp)
   8.421 +{
   8.422 +    PEP_STATUS status = PEP_STATUS_OK;
   8.423 +    int tpks_count = 0;
   8.424 +    int tpks_capacity = 8;
   8.425 +    sq_tpk_t *tpks = calloc(tpks_capacity, sizeof(sq_tpk_t));
   8.426 +    if (!tpks)
   8.427 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
   8.428 +
   8.429 +    for (;;) {
   8.430 +        sq_tpk_t tpk = NULL;
   8.431 +        status = key_load(session, stmt, &tpk, NULL);
   8.432 +        if (status == PEP_KEY_NOT_FOUND) {
   8.433 +            status = PEP_STATUS_OK;
   8.434 +            break;
   8.435 +        }
   8.436 +        ERROR_OUT(session, status, "loading TPK");
   8.437 +
   8.438 +        if (tpks_count == tpks_capacity) {
   8.439 +            tpks_capacity *= 2;
   8.440 +            tpks = realloc(tpks, sizeof(tpks[0]) * tpks_capacity);
   8.441 +            if (!tpks)
   8.442 +                ERROR_OUT(session, PEP_OUT_OF_MEMORY, "tpks");
   8.443 +        }
   8.444 +        tpks[tpks_count ++] = tpk;
   8.445 +    }
   8.446 +
   8.447 + out:
   8.448 +    if (status != PEP_STATUS_OK) {
   8.449 +        for (int i = 0; i < tpks_count; i ++)
   8.450 +            sq_tpk_free(tpks[i]);
   8.451 +        free(tpks);
   8.452 +    } else {
   8.453 +        *tpksp = tpks;
   8.454 +        *tpks_countp = tpks_count;
   8.455 +    }
   8.456 +
   8.457 +    T(" -> %s (%d tpks)", pep_status_to_string(status), *tpks_countp);
   8.458 +    return status;
   8.459 +}
   8.460 +
   8.461 +// Returns the TPK identified by the provided fingerprint.
   8.462 +//
   8.463 +// This function only matches on the primary key!
   8.464 +static PEP_STATUS tpk_find(PEP_SESSION, sq_fingerprint_t, int, sq_tpk_t *, int *)
   8.465 +    __attribute__((nonnull(1, 2)));
   8.466 +static PEP_STATUS tpk_find(PEP_SESSION session,
   8.467 +                           sq_fingerprint_t fpr, int private_only,
   8.468 +                           sq_tpk_t *tpk, int *secret)
   8.469 +{
   8.470 +    PEP_STATUS status = PEP_STATUS_OK;
   8.471 +    char *fpr_str = sq_fingerprint_to_hex(fpr);
   8.472 +
   8.473 +    T("(%s, %d)", fpr_str, private_only);
   8.474 +
   8.475 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_find : session->sq_sql.tpk_find;
   8.476 +    sqlite3_bind_text(stmt, 1, fpr_str, -1, SQLITE_STATIC);
   8.477 +
   8.478 +    status = key_load(session, stmt, tpk, secret);
   8.479 +    ERROR_OUT(session, status, "Looking up %s", fpr_str);
   8.480 +
   8.481 + out:
   8.482 +    sqlite3_reset(stmt);
   8.483 +    T("(%s, %d) -> %s", fpr_str, private_only, pep_status_to_string(status));
   8.484 +    free(fpr_str);
   8.485 +    return status;
   8.486 +}
   8.487 +
   8.488 +// Returns the TPK identified by the provided keyid.
   8.489 +//
   8.490 +// This function matches on both primary keys and subkeys!
   8.491 +//
   8.492 +// Note: There can be multiple TPKs for a given keyid.  This can
   8.493 +// occur, because an encryption subkey can be bound to multiple TPKs.
   8.494 +// Also, it is possible to collide key ids.  If there are multiple key
   8.495 +// ids for a given key, this just returns one of them.
   8.496 +//
   8.497 +// If private_only is set, this will only consider TPKs with some
   8.498 +// secret key material.
   8.499 +static PEP_STATUS tpk_find_by_keyid_hex(PEP_SESSION, const char *, int, sq_tpk_t *, int *)
   8.500 +  __attribute__((nonnull(1, 2)));
   8.501 +static PEP_STATUS tpk_find_by_keyid_hex(
   8.502 +        PEP_SESSION session, const char *keyid_hex, int private_only,
   8.503 +        sq_tpk_t *tpkp, int *secretp)
   8.504 +{
   8.505 +    PEP_STATUS status = PEP_STATUS_OK;
   8.506 +    T("(%s, %d)", keyid_hex, private_only);
   8.507 +
   8.508 +    sqlite3_stmt *stmt
   8.509 +        = private_only ? session->sq_sql.tsk_find_by_keyid : session->sq_sql.tpk_find_by_keyid;
   8.510 +    sqlite3_bind_text(stmt, 1, keyid_hex, -1, SQLITE_STATIC);
   8.511 +
   8.512 +    status = key_load(session, stmt, tpkp, secretp);
   8.513 +    ERROR_OUT(session, status, "Looking up %s", keyid_hex);
   8.514 +
   8.515 + out:
   8.516 +    sqlite3_reset(stmt);
   8.517 +    T("(%s, %d) -> %s", keyid_hex, private_only, pep_status_to_string(status));
   8.518 +    return status;
   8.519 +}
   8.520 +
   8.521 +// See tpk_find_by_keyid_hex.
   8.522 +PEP_STATUS tpk_find_by_keyid(PEP_SESSION, sq_keyid_t, int, sq_tpk_t *, int *)
   8.523 +    __attribute__((nonnull(1, 2)));
   8.524 +PEP_STATUS tpk_find_by_keyid(PEP_SESSION session,
   8.525 +                             sq_keyid_t keyid, int private_only,
   8.526 +                             sq_tpk_t *tpkp, int *secretp)
   8.527 +{
   8.528 +    char *keyid_hex = sq_keyid_to_hex(keyid);
   8.529 +    if (! keyid_hex)
   8.530 +        return PEP_OUT_OF_MEMORY;
   8.531 +    PEP_STATUS status
   8.532 +        = tpk_find_by_keyid_hex(session, keyid_hex, private_only, tpkp, secretp);
   8.533 +    free(keyid_hex);
   8.534 +    return status;
   8.535 +}
   8.536 +
   8.537 +// See tpk_find_by_keyid_hex.
   8.538 +static PEP_STATUS tpk_find_by_fpr(PEP_SESSION, sq_fingerprint_t, int,
   8.539 +                                  sq_tpk_t *, int *)
   8.540 +    __attribute__((nonnull(1, 2)));
   8.541 +static PEP_STATUS tpk_find_by_fpr(
   8.542 +    PEP_SESSION session, sq_fingerprint_t fpr, int private_only,
   8.543 +    sq_tpk_t *tpkp, int *secretp)
   8.544 +{
   8.545 +    sq_keyid_t keyid = sq_fingerprint_to_keyid(fpr);
   8.546 +    if (! keyid)
   8.547 +        return PEP_OUT_OF_MEMORY;
   8.548 +    PEP_STATUS status
   8.549 +        = tpk_find_by_keyid(session, keyid, private_only, tpkp, secretp);
   8.550 +    sq_keyid_free(keyid);
   8.551 +    return status;
   8.552 +}
   8.553 +
   8.554 +// See tpk_find_by_keyid_hex.
   8.555 +static PEP_STATUS tpk_find_by_fpr_hex(PEP_SESSION, const char *, int, sq_tpk_t *, int *secret)
   8.556 +    __attribute__((nonnull(1, 2)));
   8.557 +static PEP_STATUS tpk_find_by_fpr_hex(
   8.558 +    PEP_SESSION session, const char *fpr, int private_only,
   8.559 +    sq_tpk_t *tpkp, int *secretp)
   8.560 +{
   8.561 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
   8.562 +    if (! sq_fpr)
   8.563 +        return PEP_OUT_OF_MEMORY;
   8.564 +    PEP_STATUS status
   8.565 +        = tpk_find_by_fpr(session, sq_fpr, private_only, tpkp, secretp);
   8.566 +    sq_fingerprint_free(sq_fpr);
   8.567 +    return status;
   8.568 +}
   8.569 +
   8.570 +// Returns all known TPKs.
   8.571 +static PEP_STATUS tpk_all(PEP_SESSION, int, sq_tpk_t **, int *) __attribute__((nonnull));
   8.572 +static PEP_STATUS tpk_all(PEP_SESSION session, int private_only,
   8.573 +                          sq_tpk_t **tpksp, int *tpks_countp) {
   8.574 +    PEP_STATUS status = PEP_STATUS_OK;
   8.575 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_all : session->sq_sql.tpk_all;
   8.576 +    status = key_loadn(session, stmt, tpksp, tpks_countp);
   8.577 +    ERROR_OUT(session, status, "loading TPKs");
   8.578 + out:
   8.579 +    sqlite3_reset(stmt);
   8.580 +    return status;
   8.581 +}
   8.582 +
   8.583 +// Returns keys that have a user id that matches the specified pattern.
   8.584 +//
   8.585 +// The keys returned must be freed using sq_tpk_free.
   8.586 +static PEP_STATUS tpk_find_by_email(PEP_SESSION, const char *, int, sq_tpk_t **, int *)
   8.587 +    __attribute__((nonnull));
   8.588 +static PEP_STATUS tpk_find_by_email(PEP_SESSION session,
   8.589 +                                    const char *pattern, int private_only,
   8.590 +                                    sq_tpk_t **tpksp, int *countp)
   8.591 +{
   8.592 +    PEP_STATUS status = PEP_STATUS_OK;
   8.593 +    T("(%s)", pattern);
   8.594 +
   8.595 +    sqlite3_stmt *stmt
   8.596 +        = private_only ? session->sq_sql.tsk_find_by_email : session->sq_sql.tpk_find_by_email;
   8.597 +    sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_STATIC);
   8.598 +
   8.599 +    status = key_loadn(session, stmt, tpksp, countp);
   8.600 +    ERROR_OUT(session, status, "Searching for '%s'", pattern);
   8.601 +
   8.602 + out:
   8.603 +    sqlite3_reset(stmt);
   8.604 +    T("(%s) -> %s (%d results)", pattern, pep_status_to_string(status), *countp);
   8.605 +    return status;
   8.606 +}
   8.607 +
   8.608 +
   8.609 +// Saves the specified TPK.
   8.610 +//
   8.611 +// This function takes ownership of TPK.
   8.612 +static PEP_STATUS tpk_save(PEP_SESSION, sq_tpk_t, identity_list **)
   8.613 +    __attribute__((nonnull(1, 2)));
   8.614 +static PEP_STATUS tpk_save(PEP_SESSION session, sq_tpk_t tpk,
   8.615 +                           identity_list **private_idents)
   8.616 +{
   8.617 +    PEP_STATUS status = PEP_STATUS_OK;
   8.618 +    sq_fingerprint_t sq_fpr = NULL;
   8.619 +    char *fpr = NULL;
   8.620 +    void *tsk_buffer = NULL;
   8.621 +    size_t tsk_buffer_len = 0;
   8.622 +    int tried_commit = 0;
   8.623 +    sq_tpk_key_iter_t key_iter = NULL;
   8.624 +    sq_user_id_binding_iter_t user_id_iter = NULL;
   8.625 +
   8.626 +    sq_fpr = sq_tpk_fingerprint(tpk);
   8.627 +    fpr = sq_fingerprint_to_hex(sq_fpr);
   8.628 +    T("(%s, private_idents: %s)", fpr, private_idents ? "yes" : "no");
   8.629 +
   8.630 +    // Merge any existing data into TPK.
   8.631 +    sq_tpk_t current = NULL;
   8.632 +    status = tpk_find(session, sq_fpr, false, &current, NULL);
   8.633 +    if (status == PEP_KEY_NOT_FOUND)
   8.634 +        status = PEP_STATUS_OK;
   8.635 +    else
   8.636 +        ERROR_OUT(session, status, "Looking up %s", fpr);
   8.637 +    if (current)
   8.638 +        tpk = sq_tpk_merge(session->ctx, tpk, current);
   8.639 +
   8.640 +    int is_tsk = sq_tpk_is_tsk(tpk);
   8.641 +
   8.642 +    // Serialize it.
   8.643 +    sq_writer_t writer = sq_writer_alloc(&tsk_buffer, &tsk_buffer_len);
   8.644 +    if (! writer)
   8.645 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
   8.646 +
   8.647 +    sq_status_t sq_status;
   8.648 +    sq_tsk_t tsk = sq_tpk_into_tsk(tpk);
   8.649 +    sq_status = sq_tsk_serialize(session->ctx, tsk, writer);
   8.650 +    tpk = sq_tsk_into_tpk(tsk);
   8.651 +    //sq_writer_free(writer);
   8.652 +    if (sq_status != 0)
   8.653 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Serializing TPK");
   8.654 +
   8.655 +
   8.656 +    // Insert the TSK into the DB.
   8.657 +    sqlite3_stmt *stmt = session->sq_sql.begin_transaction;
   8.658 +    int sqlite_result = sqlite3_step(stmt);
   8.659 +    sqlite3_reset(stmt);
   8.660 +    if (sqlite_result != SQLITE_DONE)
   8.661 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.662 +                  "begin transaction failed: %s",
   8.663 +                  sqlite3_errmsg(session->key_db));
   8.664 +
   8.665 +    stmt = session->sq_sql.tpk_save_insert_primary;
   8.666 +    sqlite3_bind_text(stmt, 1, fpr, -1, SQLITE_STATIC);
   8.667 +    sqlite3_bind_int(stmt, 2, is_tsk);
   8.668 +    sqlite3_bind_blob(stmt, 3, tsk_buffer, tsk_buffer_len, SQLITE_STATIC);
   8.669 +
   8.670 +    sqlite_result = sqlite3_step(stmt);
   8.671 +    sqlite3_reset(stmt);
   8.672 +    if (sqlite_result != SQLITE_DONE)
   8.673 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.674 +                  "Saving TPK: %s", sqlite3_errmsg(session->key_db));
   8.675 +
   8.676 +    // Insert the "subkeys" (the primary key and the subkeys).
   8.677 +    stmt = session->sq_sql.tpk_save_insert_subkeys;
   8.678 +    key_iter = sq_tpk_key_iter(tpk);
   8.679 +    sq_p_key_t key;
   8.680 +    while ((key = sq_tpk_key_iter_next(key_iter, NULL, NULL))) {
   8.681 +        sq_keyid_t keyid = sq_p_key_keyid(key);
   8.682 +        char *keyid_hex = sq_keyid_to_hex(keyid);
   8.683 +        sqlite3_bind_text(stmt, 1, keyid_hex, -1, SQLITE_STATIC);
   8.684 +        sqlite3_bind_text(stmt, 2, fpr, -1, SQLITE_STATIC);
   8.685 +
   8.686 +        sqlite_result = sqlite3_step(stmt);
   8.687 +        sqlite3_reset(stmt);
   8.688 +        free(keyid_hex);
   8.689 +        sq_keyid_free(keyid);
   8.690 +        if (sqlite_result != SQLITE_DONE) {
   8.691 +            sq_tpk_key_iter_free(key_iter);
   8.692 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.693 +                      "Updating subkeys: %s", sqlite3_errmsg(session->key_db));
   8.694 +        }
   8.695 +    }
   8.696 +    sq_tpk_key_iter_free(key_iter);
   8.697 +    key_iter = NULL;
   8.698 +
   8.699 +    // Insert the "userids".
   8.700 +    stmt = session->sq_sql.tpk_save_insert_userids;
   8.701 +    user_id_iter = sq_tpk_user_id_binding_iter(tpk);
   8.702 +    sq_user_id_binding_t binding;
   8.703 +    int first = 1;
   8.704 +    while ((binding = sq_user_id_binding_iter_next(user_id_iter))) {
   8.705 +        char *user_id = sq_user_id_binding_user_id(binding);
   8.706 +        if (!user_id || !*user_id)
   8.707 +            continue;
   8.708 +
   8.709 +        // Ignore bindings with a self-revocation certificate, but no
   8.710 +        // self-signature.
   8.711 +        if (!sq_user_id_binding_selfsig(binding)) {
   8.712 +            free(user_id);
   8.713 +            continue;
   8.714 +        }
   8.715 +
   8.716 +        char *name, *email;
   8.717 +        user_id_split(user_id, &name, &email); /* user_id is comsumed.  */
   8.718 +        // XXX: Correctly clean up name and email on error...
   8.719 +
   8.720 +        if (email) {
   8.721 +            T("  userid: %s", email);
   8.722 +
   8.723 +            sqlite3_bind_text(stmt, 1, email, -1, SQLITE_STATIC);
   8.724 +            sqlite3_bind_text(stmt, 2, fpr, -1, SQLITE_STATIC);
   8.725 +
   8.726 +            sqlite_result = sqlite3_step(stmt);
   8.727 +            sqlite3_reset(stmt);
   8.728 +
   8.729 +            if (sqlite_result != SQLITE_DONE) {
   8.730 +                sq_user_id_binding_iter_free(user_id_iter);
   8.731 +                free(name);
   8.732 +                ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.733 +                          "Updating userids: %s", sqlite3_errmsg(session->key_db));
   8.734 +            }
   8.735 +        }
   8.736 +
   8.737 +        if (first && private_idents && is_tsk) {
   8.738 +            first = 0;
   8.739 +
   8.740 +            // Create an identity for the primary user id.
   8.741 +            pEp_identity *ident = new_identity(email, fpr, NULL, name);
   8.742 +            if (ident == NULL)
   8.743 +                ERROR_OUT(session, PEP_OUT_OF_MEMORY, "new_identity");
   8.744 +
   8.745 +            *private_idents = identity_list_add(*private_idents, ident);
   8.746 +            if (*private_idents == NULL)
   8.747 +                ERROR_OUT(session, PEP_OUT_OF_MEMORY, "identity_list_add");
   8.748 +        }
   8.749 +        free(email);
   8.750 +        free(name);
   8.751 +
   8.752 +    }
   8.753 +    sq_user_id_binding_iter_free(user_id_iter);
   8.754 +    user_id_iter = NULL;
   8.755 +
   8.756 + out:
   8.757 +    // Prevent ERROR_OUT from causing an infinite loop.
   8.758 +    if (! tried_commit) {
   8.759 +        tried_commit = 1;
   8.760 +        stmt = status == PEP_STATUS_OK
   8.761 +            ? session->sq_sql.commit_transaction
   8.762 +            : session->sq_sql.rollback_transaction;
   8.763 +        int sqlite_result = sqlite3_step(stmt);
   8.764 +        sqlite3_reset(stmt);
   8.765 +        if (sqlite_result != SQLITE_DONE)
   8.766 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR,
   8.767 +                      status == PEP_STATUS_OK
   8.768 +                      ? "commit failed: %s" : "rollback failed: %s",
   8.769 +                      sqlite3_errmsg(session->key_db));
   8.770 +    }
   8.771 +
   8.772 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
   8.773 +
   8.774 +    if (user_id_iter)
   8.775 +        sq_user_id_binding_iter_free(user_id_iter);
   8.776 +    if (key_iter)
   8.777 +        sq_tpk_key_iter_free(key_iter);
   8.778 +    if (stmt)
   8.779 +      sqlite3_reset(stmt);
   8.780 +    free(tsk_buffer);
   8.781 +    if (tpk)
   8.782 +        sq_tpk_free(tpk);
   8.783 +    free(fpr);
   8.784 +    sq_fingerprint_free(sq_fpr);
   8.785 +
   8.786 +    return status;
   8.787 +}
   8.788 +
   8.789 +struct decrypt_cookie {
   8.790 +    PEP_SESSION session;
   8.791 +    int get_secret_keys_called;
   8.792 +    stringlist_t *recipient_keylist;
   8.793 +    stringlist_t *signer_keylist;
   8.794 +    int good_checksums;
   8.795 +    int missing_keys;
   8.796 +    int bad_checksums;
   8.797 +    int decrypted;
   8.798 +};
   8.799 +
   8.800 +static sq_status_t
   8.801 +get_public_keys_cb(void *cookie_raw,
   8.802 +                   sq_keyid_t *keyids, size_t keyids_len,
   8.803 +                   sq_tpk_t **tpks, size_t *tpk_len,
   8.804 +                   void (**our_free)(void *))
   8.805 +{
   8.806 +    struct decrypt_cookie *cookie = cookie_raw;
   8.807 +    PEP_SESSION session = cookie->session;
   8.808 +
   8.809 +    *tpks = calloc(keyids_len, sizeof(*tpks));
   8.810 +    if (!*tpks)
   8.811 +        return SQ_STATUS_UNKNOWN_ERROR;
   8.812 +    *our_free = free;
   8.813 +
   8.814 +    int i, j;
   8.815 +    j = 0;
   8.816 +    for (i = 0; i < keyids_len; i ++) {
   8.817 +        sq_tpk_t tpk = NULL;
   8.818 +        sq_status_t status
   8.819 +            = tpk_find_by_keyid(session, keyids[i], false, &tpk, NULL);
   8.820 +        if (status == SQ_STATUS_SUCCESS)
   8.821 +            (*tpks)[j ++] = tpk;
   8.822 +    }
   8.823 +    *tpk_len = j;
   8.824 +    return SQ_STATUS_SUCCESS;
   8.825 +}
   8.826 +
   8.827 +static sq_status_t
   8.828 +get_secret_keys_cb(void *cookie_opaque,
   8.829 +                   sq_pkesk_t *pkesks, size_t pkesk_count,
   8.830 +                   sq_skesk_t *skesks, size_t skesk_count,
   8.831 +                   sq_secret_t *secret)
   8.832 +{
   8.833 +    struct decrypt_cookie *cookie = cookie_opaque;
   8.834 +    PEP_SESSION session = cookie->session;
   8.835 +    sq_tpk_t *tsks = NULL;
   8.836 +    int tsks_count = 0;
   8.837 +    int wildcards = 0;
   8.838 +
   8.839 +    if (cookie->get_secret_keys_called)
   8.840 +        // Prevent iterations, which isn't needed since we don't
   8.841 +        // support SKESKs.
   8.842 +        return SQ_STATUS_UNKNOWN_ERROR;
   8.843 +    cookie->get_secret_keys_called = 1;
   8.844 +
   8.845 +    T("%zd PKESKs", pkesk_count);
   8.846 +
   8.847 +    for (int i = 0; i < pkesk_count; i ++) {
   8.848 +        sq_pkesk_t pkesk = pkesks[i];
   8.849 +        sq_keyid_t keyid = sq_pkesk_recipient(pkesk); /* Reference. */
   8.850 +        char *keyid_str = sq_keyid_to_hex(keyid);
   8.851 +        sq_tpk_key_iter_t key_iter = NULL;
   8.852 +
   8.853 +        T("Considering PKESK for %s", keyid_str);
   8.854 +
   8.855 +        if (strcmp(keyid_str, "0000000000000000") == 0) {
   8.856 +            // Initially ignore wildcards.
   8.857 +            wildcards = 1;
   8.858 +            goto eol;
   8.859 +        }
   8.860 +
   8.861 +        // Collect the recipients.  Note: we must return the primary
   8.862 +        // key's fingerprint.
   8.863 +        sq_tpk_t tpk = NULL;
   8.864 +        int is_tsk = 0;
   8.865 +        if (tpk_find_by_keyid(session, keyid, false, &tpk, &is_tsk) != PEP_STATUS_OK)
   8.866 +            goto eol;
   8.867 +
   8.868 +        sq_fingerprint_t fp = sq_tpk_fingerprint(tpk);
   8.869 +        char *fp_string = sq_fingerprint_to_hex(fp);
   8.870 +        stringlist_add_unique(cookie->recipient_keylist, fp_string);
   8.871 +        free(fp_string);
   8.872 +        sq_fingerprint_free(fp);
   8.873 +
   8.874 +        if (cookie->decrypted)
   8.875 +            goto eol;
   8.876 +
   8.877 +        // See if we have the secret key.
   8.878 +        assert(is_tsk == sq_tpk_is_tsk(tpk));
   8.879 +        if (! is_tsk)
   8.880 +            goto eol;
   8.881 +
   8.882 +        key_iter = sq_tpk_key_iter(tpk);
   8.883 +        sq_p_key_t key;
   8.884 +        while ((key = sq_tpk_key_iter_next(key_iter, NULL, NULL))) {
   8.885 +            sq_keyid_t this_keyid = sq_p_key_keyid(key);
   8.886 +            char *this_keyid_hex = sq_keyid_to_hex(this_keyid);
   8.887 +            sq_keyid_free(this_keyid);
   8.888 +
   8.889 +            int match = strcmp(keyid_str, this_keyid_hex) == 0;
   8.890 +            free(this_keyid_hex);
   8.891 +            if (match)
   8.892 +                break;
   8.893 +        }
   8.894 +
   8.895 +        if (key == NULL) {
   8.896 +            assert(!"Inconsistent DB: key doesn't contain a subkey with keyid!");
   8.897 +            goto eol;
   8.898 +        }
   8.899 +
   8.900 +        uint8_t algo;
   8.901 +        uint8_t session_key[1024];
   8.902 +        size_t session_key_len = sizeof(session_key);
   8.903 +        if (sq_pkesk_decrypt(cookie->session->ctx,
   8.904 +                             pkesk, key, &algo,
   8.905 +                             session_key, &session_key_len) != 0) {
   8.906 +            DUMP_ERR(session, PEP_UNKNOWN_ERROR, "sq_pkesk_decrypt");
   8.907 +            goto eol;
   8.908 +        }
   8.909 +
   8.910 +        T("Decrypted PKESK for %s", keyid_str);
   8.911 +
   8.912 +        *secret = sq_secret_cached(algo, session_key, session_key_len);
   8.913 +        cookie->decrypted = 1;
   8.914 +
   8.915 +    eol:
   8.916 +        free(keyid_str);
   8.917 +        if (key_iter)
   8.918 +            sq_tpk_key_iter_free(key_iter);
   8.919 +        if (tpk)
   8.920 +            sq_tpk_free(tpk);
   8.921 +    }
   8.922 +
   8.923 +    // Consider wildcard recipients.
   8.924 +    if (wildcards) for (int i = 0; i < pkesk_count && !cookie->decrypted; i ++) {
   8.925 +        sq_pkesk_t pkesk = pkesks[i];
   8.926 +        sq_keyid_t keyid = sq_pkesk_recipient(pkesk); /* Reference. */
   8.927 +        char *keyid_str = sq_keyid_to_hex(keyid);
   8.928 +        sq_tpk_key_iter_t key_iter = NULL;
   8.929 +
   8.930 +        if (strcmp(keyid_str, "0000000000000000") != 0)
   8.931 +            goto eol2;
   8.932 +
   8.933 +        if (!tsks) {
   8.934 +            if (tpk_all(session, true, &tsks, &tsks_count) != PEP_STATUS_OK) {
   8.935 +                DUMP_ERR(session, PEP_UNKNOWN_ERROR, "Getting all tsks");
   8.936 +            }
   8.937 +        }
   8.938 +
   8.939 +        for (int j = 0; j < tsks_count; j ++) {
   8.940 +            sq_tpk_t tsk = tsks[j];
   8.941 +
   8.942 +            key_iter = sq_tpk_key_iter(tsk);
   8.943 +            sq_p_key_t key;
   8.944 +            sq_signature_t selfsig;
   8.945 +            while ((key = sq_tpk_key_iter_next(key_iter, &selfsig, NULL))) {
   8.946 +                if (! (sq_signature_can_encrypt_at_rest(selfsig)
   8.947 +                       || sq_signature_can_encrypt_for_transport(selfsig)))
   8.948 +                    continue;
   8.949 +
   8.950 +                // Note: for decryption to appear to succeed, we must
   8.951 +                // get a valid algorithm (8 of 256 values) and a
   8.952 +                // 16-bit checksum must match.  Thus, we have about a
   8.953 +                // 1 in 2**21 chance of having a false positive here.
   8.954 +                uint8_t algo;
   8.955 +                uint8_t session_key[1024];
   8.956 +                size_t session_key_len = sizeof(session_key);
   8.957 +                if (sq_pkesk_decrypt(cookie->session->ctx, pkesk, key,
   8.958 +                                     &algo, session_key, &session_key_len))
   8.959 +                    continue;
   8.960 +
   8.961 +                // Add it to the recipient list.
   8.962 +                sq_fingerprint_t fp = sq_tpk_fingerprint(tsk);
   8.963 +                char *fp_string = sq_fingerprint_to_hex(fp);
   8.964 +                T("wildcard recipient appears to be %s", fp_string);
   8.965 +                stringlist_add_unique(cookie->recipient_keylist, fp_string);
   8.966 +                free(fp_string);
   8.967 +                sq_fingerprint_free(fp);
   8.968 +
   8.969 +                *secret = sq_secret_cached(algo, session_key, session_key_len);
   8.970 +                cookie->decrypted = 1;
   8.971 +            }
   8.972 +
   8.973 +            sq_tpk_key_iter_free(key_iter);
   8.974 +            key_iter = NULL;
   8.975 +        }
   8.976 +    eol2:
   8.977 +        free(keyid_str);
   8.978 +        if (key_iter)
   8.979 +            sq_tpk_key_iter_free(key_iter);
   8.980 +    }
   8.981 +
   8.982 +    if (tsks) {
   8.983 +        for (int i = 0; i < tsks_count; i ++)
   8.984 +            sq_tpk_free(tsks[i]);
   8.985 +        free(tsks);
   8.986 +    }
   8.987 +
   8.988 +    return cookie->decrypted ? SQ_STATUS_SUCCESS : SQ_STATUS_UNKNOWN_ERROR;
   8.989 +}
   8.990 +
   8.991 +static sq_status_t
   8.992 +check_signatures_cb(void *cookie_opaque,
   8.993 +                   sq_verification_results_t results, size_t levels)
   8.994 +{
   8.995 +    struct decrypt_cookie *cookie = cookie_opaque;
   8.996 +    PEP_SESSION session = cookie->session;
   8.997 +
   8.998 +    int level;
   8.999 +    for (level = 0; level < levels; level ++) {
  8.1000 +        sq_verification_result_t *vrs;
  8.1001 +        size_t vr_count;
  8.1002 +        sq_verification_results_at_level(results, level, &vrs, &vr_count);
  8.1003 +
  8.1004 +        int i;
  8.1005 +        for (i = 0; i < vr_count; i ++) {
  8.1006 +            sq_tpk_t tpk = NULL;
  8.1007 +            sq_verification_result_code_t code
  8.1008 +                = sq_verification_result_code(vrs[i]);
  8.1009 +
  8.1010 +            if (code == SQ_VERIFICATION_RESULT_CODE_BAD_CHECKSUM) {
  8.1011 +                cookie->bad_checksums ++;
  8.1012 +                continue;
  8.1013 +            }
  8.1014 +            if (code == SQ_VERIFICATION_RESULT_CODE_MISSING_KEY) {
  8.1015 +                // No key, nothing we can do.
  8.1016 +                cookie->missing_keys ++;
  8.1017 +                continue;
  8.1018 +            }
  8.1019 +
  8.1020 +            // We need to add the fingerprint of the primary key to
  8.1021 +            // cookie->signer_keylist.
  8.1022 +            sq_signature_t sig = sq_verification_result_signature(vrs[i]);
  8.1023 +
  8.1024 +            // First try looking up by the TPK using the
  8.1025 +            // IssuerFingerprint subpacket.
  8.1026 +            sq_fingerprint_t issuer_fp = sq_signature_issuer_fingerprint(sig);
  8.1027 +            if (issuer_fp) {
  8.1028 +                sq_keyid_t issuer = sq_fingerprint_to_keyid(issuer_fp);
  8.1029 +                if (tpk_find_by_keyid(session, issuer, false, &tpk, NULL) != PEP_STATUS_OK)
  8.1030 +                    ; // Soft error.  Ignore.
  8.1031 +                sq_keyid_free(issuer);
  8.1032 +                sq_fingerprint_free(issuer_fp);
  8.1033 +            }
  8.1034 +
  8.1035 +            // If that is not available, try using the Issuer subpacket.
  8.1036 +            if (!tpk) {
  8.1037 +                sq_keyid_t issuer = sq_signature_issuer(sig);
  8.1038 +                if (issuer) {
  8.1039 +                    if (tpk_find_by_keyid(session, issuer, false, &tpk, NULL) != PEP_STATUS_OK)
  8.1040 +                        ; // Soft error.  Ignore.
  8.1041 +                }
  8.1042 +                sq_keyid_free(issuer);
  8.1043 +            }
  8.1044 +
  8.1045 +            if (tpk) {
  8.1046 +                // Ok, we have a TPK.
  8.1047 +                sq_fingerprint_t fp = sq_tpk_fingerprint(tpk);
  8.1048 +                char *fp_str = sq_fingerprint_to_hex(fp);
  8.1049 +                stringlist_add_unique(cookie->signer_keylist, fp_str);
  8.1050 +
  8.1051 +                // XXX: Check that the TPK and the key used to make
  8.1052 +                // the signature and the signature itself are alive
  8.1053 +                // and not revoked.  Revoked =>
  8.1054 +                // PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH; Expired key
  8.1055 +                // or sig => PEP_DECRYPTED.
  8.1056 +                cookie->good_checksums ++;
  8.1057 +
  8.1058 +                free(fp_str);
  8.1059 +                sq_fingerprint_free(fp);
  8.1060 +                sq_tpk_free(tpk);
  8.1061 +            } else {
  8.1062 +                // If we get
  8.1063 +                // SQ_VERIFICATION_RESULT_CODE_GOOD_CHECKSUM, then the
  8.1064 +                // TPK should be available.  But, another process
  8.1065 +                // could have deleted the key from the store in the
  8.1066 +                // mean time, so be tolerant.
  8.1067 +                cookie->missing_keys ++;
  8.1068 +            }
  8.1069 +        }
  8.1070 +    }
  8.1071 +
  8.1072 +    return SQ_STATUS_SUCCESS;
  8.1073 +}
  8.1074 +
  8.1075 +PEP_STATUS pgp_decrypt_and_verify(
  8.1076 +    PEP_SESSION session, const char *ctext, size_t csize,
  8.1077 +    const char *dsigtext, size_t dsigsize,
  8.1078 +    char **ptext, size_t *psize, stringlist_t **keylist,
  8.1079 +    char** filename_ptr)
  8.1080 +{
  8.1081 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1082 +    struct decrypt_cookie cookie = { session, 0, NULL, NULL, 0, 0, 0, };
  8.1083 +    sq_reader_t reader = NULL;
  8.1084 +    sq_writer_t writer = NULL;
  8.1085 +    *ptext = NULL;
  8.1086 +    *psize = 0;
  8.1087 +
  8.1088 +    // XXX: We don't yet handle detached signatures over encrypted
  8.1089 +    // messages.
  8.1090 +    assert(!dsigtext);
  8.1091 +
  8.1092 +    cookie.recipient_keylist = new_stringlist(NULL);
  8.1093 +    if (!cookie.recipient_keylist)
  8.1094 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "recipient_keylist");
  8.1095 +
  8.1096 +    cookie.signer_keylist = new_stringlist(NULL);
  8.1097 +    if (!cookie.signer_keylist)
  8.1098 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "signer_keylist");
  8.1099 +
  8.1100 +    reader = sq_reader_from_bytes((const uint8_t *) ctext, csize);
  8.1101 +    if (! reader)
  8.1102 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "Creating reader");
  8.1103 +
  8.1104 +    writer = sq_writer_alloc((void **) ptext, psize);
  8.1105 +    if (! writer)
  8.1106 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Creating writer");
  8.1107 +
  8.1108 +    sq_status_t sq_status = sq_decrypt(session->ctx, reader, writer,
  8.1109 +                                       get_public_keys_cb, get_secret_keys_cb,
  8.1110 +                                       check_signatures_cb, &cookie);
  8.1111 +    if (sq_status)
  8.1112 +        ERROR_OUT(session, PEP_DECRYPT_NO_KEY, "sq_decrypt");
  8.1113 +
  8.1114 +    if (! cookie.decrypted)
  8.1115 +        ERROR_OUT(session, PEP_DECRYPT_NO_KEY, "Decryption failed");
  8.1116 +
  8.1117 +    // Add a terminating NUL for naive users
  8.1118 +    void *t = realloc(*ptext, *psize + 1);
  8.1119 +    if (! t)
  8.1120 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1121 +    *ptext = t;
  8.1122 +    (*ptext)[*psize] = 0;
  8.1123 +
  8.1124 +    if (! cookie.signer_keylist) {
  8.1125 +        cookie.signer_keylist = new_stringlist("");
  8.1126 +        if (! cookie.signer_keylist)
  8.1127 +            ERROR_OUT(session, PEP_OUT_OF_MEMORY, "cookie.signer_keylist");
  8.1128 +    }
  8.1129 +    if (!cookie.signer_keylist->value)
  8.1130 +        stringlist_add(cookie.signer_keylist, "");
  8.1131 +
  8.1132 +    *keylist = cookie.signer_keylist;
  8.1133 +    stringlist_append(*keylist, cookie.recipient_keylist);
  8.1134 +
  8.1135 + out:
  8.1136 +    if (status == PEP_STATUS_OK) {
  8.1137 +        if (cookie.bad_checksums) {
  8.1138 +            // If there are any bad signatures, fail.
  8.1139 +            status = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
  8.1140 +        } else if (cookie.good_checksums) {
  8.1141 +            // If there is at least one signature that we can verify,
  8.1142 +            // succeed.
  8.1143 +            status = PEP_DECRYPTED_AND_VERIFIED;
  8.1144 +        } else {
  8.1145 +            // We couldn't verify any signatures (possibly because we
  8.1146 +            // don't have the keys).
  8.1147 +            status = PEP_DECRYPTED;
  8.1148 +        }
  8.1149 +    } else {
  8.1150 +        free_stringlist(cookie.recipient_keylist);
  8.1151 +        free_stringlist(cookie.signer_keylist);
  8.1152 +        free(*ptext);
  8.1153 +    }
  8.1154 +
  8.1155 +    if (reader)
  8.1156 +        sq_reader_free(reader);
  8.1157 +    if (writer)
  8.1158 +        sq_writer_free(writer);
  8.1159 +
  8.1160 +    T("-> %s", pep_status_to_string(status));
  8.1161 +    return status;
  8.1162 +}
  8.1163 +
  8.1164 +PEP_STATUS pgp_verify_text(
  8.1165 +    PEP_SESSION session, const char *text, size_t size,
  8.1166 +    const char *signature, size_t sig_size, stringlist_t **keylist)
  8.1167 +{
  8.1168 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1169 +    struct decrypt_cookie cookie = { session, 0, NULL, NULL, 0, 0, 0, };
  8.1170 +    sq_reader_t reader = NULL;
  8.1171 +    sq_reader_t dsig_reader = NULL;
  8.1172 +
  8.1173 +    if (size == 0 || sig_size == 0)
  8.1174 +        return PEP_DECRYPT_WRONG_FORMAT;
  8.1175 +
  8.1176 +    cookie.recipient_keylist = new_stringlist(NULL);
  8.1177 +    if (!cookie.recipient_keylist)
  8.1178 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1179 +
  8.1180 +    cookie.signer_keylist = new_stringlist(NULL);
  8.1181 +    if (!cookie.signer_keylist)
  8.1182 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1183 +
  8.1184 +    reader = sq_reader_from_bytes((const uint8_t *) text, size);
  8.1185 +    if (! reader)
  8.1186 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "Creating reader");
  8.1187 +
  8.1188 +    dsig_reader = NULL;
  8.1189 +    if (signature) {
  8.1190 +        dsig_reader = sq_reader_from_bytes((uint8_t *) signature, sig_size);
  8.1191 +        if (! dsig_reader)
  8.1192 +            ERROR_OUT(session, PEP_OUT_OF_MEMORY, "Creating signature reader");
  8.1193 +    }
  8.1194 +
  8.1195 +    if (sq_verify(session->ctx, reader, dsig_reader, /* output */ NULL,
  8.1196 +                  get_public_keys_cb, check_signatures_cb, &cookie))
  8.1197 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "sq_verify");
  8.1198 +
  8.1199 +    if (! cookie.signer_keylist) {
  8.1200 +        cookie.signer_keylist = new_stringlist("");
  8.1201 +        if (! cookie.signer_keylist)
  8.1202 +            ERROR_OUT(session, PEP_OUT_OF_MEMORY, "cookie.signer_keylist");
  8.1203 +    }
  8.1204 +    if (!cookie.signer_keylist->value)
  8.1205 +        stringlist_add(cookie.signer_keylist, "");
  8.1206 +
  8.1207 +    *keylist = cookie.signer_keylist;
  8.1208 +    stringlist_append(*keylist, cookie.recipient_keylist);
  8.1209 +
  8.1210 + out:
  8.1211 +    if (status == PEP_STATUS_OK) {
  8.1212 +        if (cookie.bad_checksums) {
  8.1213 +            // If there are any bad signatures, fail.
  8.1214 +            status = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
  8.1215 +        } else if (cookie.good_checksums) {
  8.1216 +            // If there is at least one signature that we can verify,
  8.1217 +            // succeed.
  8.1218 +            status = PEP_VERIFIED;
  8.1219 +        } else {
  8.1220 +            // We couldn't verify any signatures (possibly because we
  8.1221 +            // don't have the keys).
  8.1222 +            status = PEP_UNENCRYPTED;
  8.1223 +        }
  8.1224 +    } else {
  8.1225 +        free_stringlist(cookie.recipient_keylist);
  8.1226 +        free_stringlist(cookie.signer_keylist);
  8.1227 +    }
  8.1228 +
  8.1229 +    if (reader)
  8.1230 +        sq_reader_free(reader);
  8.1231 +    if (dsig_reader)
  8.1232 +        sq_reader_free(dsig_reader);
  8.1233 +
  8.1234 +    T("-> %s", pep_status_to_string(status));
  8.1235 +    return status;
  8.1236 +}
  8.1237 +
  8.1238 +
  8.1239 +PEP_STATUS pgp_sign_only(
  8.1240 +    PEP_SESSION session, const char* fpr, const char *ptext,
  8.1241 +    size_t psize, char **stext, size_t *ssize)
  8.1242 +{
  8.1243 +    assert(session);
  8.1244 +    assert(fpr && fpr[0]);
  8.1245 +    assert(ptext);
  8.1246 +    assert(psize);
  8.1247 +    assert(stext);
  8.1248 +    assert(ssize);
  8.1249 +
  8.1250 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1251 +    sq_tpk_t signer = NULL;
  8.1252 +    sq_writer_stack_t ws = NULL;
  8.1253 +
  8.1254 +    status = tpk_find_by_fpr_hex(session, fpr, true, &signer, NULL);
  8.1255 +    ERROR_OUT(session, status, "Looking up key '%s'", fpr);
  8.1256 +
  8.1257 +    sq_writer_t writer = sq_writer_alloc((void **) stext, ssize);
  8.1258 +    writer = sq_armor_writer_new(session->ctx, writer,
  8.1259 +                                 SQ_ARMOR_KIND_MESSAGE, NULL, 0);
  8.1260 +    if (!writer)
  8.1261 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up armor writer");
  8.1262 +
  8.1263 +    ws = sq_writer_stack_message(writer);
  8.1264 +
  8.1265 +    ws = sq_signer_new_detached(session->ctx, ws, &signer, 1);
  8.1266 +    if (!ws)
  8.1267 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up signer");
  8.1268 +
  8.1269 +    sq_status_t write_status =
  8.1270 +        sq_writer_stack_write_all (session->ctx, ws,
  8.1271 +                                   (uint8_t *) ptext, psize);
  8.1272 +    if (write_status != 0)
  8.1273 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Encrypting message");
  8.1274 +
  8.1275 +    // Add a terminating NUL for naive users
  8.1276 +    void *t = realloc(*stext, *ssize + 1);
  8.1277 +    if (! t)
  8.1278 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1279 +    *stext = t;
  8.1280 +    (*stext)[*ssize] = 0;
  8.1281 +
  8.1282 + out:
  8.1283 +    if (ws) {
  8.1284 +        sq_status_t sq_status = sq_writer_stack_finalize (session->ctx, ws);
  8.1285 +        ws = NULL;
  8.1286 +        if (sq_status != 0)
  8.1287 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Flushing writer");
  8.1288 +    }
  8.1289 +
  8.1290 +    if (signer)
  8.1291 +        sq_tpk_free(signer);
  8.1292 +
  8.1293 +    T("(%s)-> %s", fpr, pep_status_to_string(status));
  8.1294 +    return status;
  8.1295 +}
  8.1296 +
  8.1297 +static PEP_STATUS pgp_encrypt_sign_optional(
  8.1298 +    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  8.1299 +    size_t psize, char **ctext, size_t *csize, bool sign)
  8.1300 +{
  8.1301 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1302 +    int keys_count = 0;
  8.1303 +    sq_tpk_t *keys = NULL;
  8.1304 +    sq_tpk_t signer = NULL;
  8.1305 +    sq_writer_stack_t ws = NULL;
  8.1306 +
  8.1307 +    assert(session);
  8.1308 +    assert(keylist);
  8.1309 +    assert(ptext);
  8.1310 +    assert(psize);
  8.1311 +    assert(ctext);
  8.1312 +    assert(csize);
  8.1313 +
  8.1314 +    *ctext = NULL;
  8.1315 +    *csize = 0;
  8.1316 +
  8.1317 +    keys = calloc(stringlist_length(keylist), sizeof(*keys));
  8.1318 +    if (keys == NULL)
  8.1319 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1320 +
  8.1321 +    // Get the keys for the recipients.
  8.1322 +    const stringlist_t *_keylist;
  8.1323 +    for (_keylist = keylist; _keylist != NULL; _keylist = _keylist->next) {
  8.1324 +        assert(_keylist->value);
  8.1325 +        sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(_keylist->value);
  8.1326 +        status = tpk_find_by_fpr(session, sq_fpr, false, &keys[keys_count ++], NULL);
  8.1327 +        sq_fingerprint_free(sq_fpr);
  8.1328 +        ERROR_OUT(session, status, "Looking up key for recipient '%s'", _keylist->value);
  8.1329 +    }
  8.1330 +
  8.1331 +    if (sign) {
  8.1332 +        // The first key in the keylist is the signer.
  8.1333 +        status = tpk_find_by_fpr_hex(session, keylist->value, true, &signer, NULL);
  8.1334 +        ERROR_OUT(session, status, "Looking up key for signing '%s'", keylist->value);
  8.1335 +    }
  8.1336 +
  8.1337 +    sq_writer_t writer = sq_writer_alloc((void **) ctext, csize);
  8.1338 +    writer = sq_armor_writer_new(session->ctx, writer,
  8.1339 +                                 SQ_ARMOR_KIND_MESSAGE, NULL, 0);
  8.1340 +    if (!writer)
  8.1341 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up armor writer");
  8.1342 +
  8.1343 +    ws = sq_writer_stack_message(writer);
  8.1344 +    ws = sq_encryptor_new (session->ctx, ws,
  8.1345 +                           NULL, 0, keys, keys_count,
  8.1346 +                           SQ_ENCRYPTION_MODE_FOR_TRANSPORT);
  8.1347 +    if (!ws) {
  8.1348 +        sq_writer_free(writer);
  8.1349 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up encryptor");
  8.1350 +    }
  8.1351 +
  8.1352 +    if (sign) {
  8.1353 +        ws = sq_signer_new(session->ctx, ws, &signer, 1);
  8.1354 +        if (!ws)
  8.1355 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up signer");
  8.1356 +    }
  8.1357 +
  8.1358 +    ws = sq_literal_writer_new (session->ctx, ws);
  8.1359 +    if (!ws)
  8.1360 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Setting up literal writer");
  8.1361 +
  8.1362 +    sq_status_t write_status =
  8.1363 +        sq_writer_stack_write_all (session->ctx, ws,
  8.1364 +                                   (uint8_t *) ptext, psize);
  8.1365 +    if (write_status != 0)
  8.1366 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Encrypting message");
  8.1367 +
  8.1368 +    // Add a terminating NUL for naive users
  8.1369 +    void *t = realloc(*ctext, *csize + 1);
  8.1370 +    if (! t)
  8.1371 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "out of memory");
  8.1372 +    *ctext = t;
  8.1373 +    (*ctext)[*csize] = 0;
  8.1374 +
  8.1375 + out:
  8.1376 +    if (ws) {
  8.1377 +        sq_status_t sq_status = sq_writer_stack_finalize (session->ctx, ws);
  8.1378 +        ws = NULL;
  8.1379 +        if (sq_status != 0)
  8.1380 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Flushing writer");
  8.1381 +    }
  8.1382 +
  8.1383 +    if (signer)
  8.1384 +        sq_tpk_free(signer);
  8.1385 +    for (int i = 0; i < keys_count; i ++)
  8.1386 +        sq_tpk_free(keys[i]);
  8.1387 +    free(keys);
  8.1388 +
  8.1389 +    T("-> %s", pep_status_to_string(status));
  8.1390 +    return status;
  8.1391 +}
  8.1392 +
  8.1393 +PEP_STATUS pgp_encrypt_only(
  8.1394 +    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  8.1395 +    size_t psize, char **ctext, size_t *csize)
  8.1396 +{
  8.1397 +    return pgp_encrypt_sign_optional(session, keylist, ptext,
  8.1398 +        psize, ctext, csize, false);
  8.1399 +}
  8.1400 +
  8.1401 +PEP_STATUS pgp_encrypt_and_sign(
  8.1402 +    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  8.1403 +    size_t psize, char **ctext, size_t *csize)
  8.1404 +{
  8.1405 +    return pgp_encrypt_sign_optional(session, keylist, ptext,
  8.1406 +        psize, ctext, csize, true);
  8.1407 +}
  8.1408 +
  8.1409 +
  8.1410 +PEP_STATUS pgp_generate_keypair(PEP_SESSION session, pEp_identity *identity)
  8.1411 +{
  8.1412 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1413 +    char *userid = NULL;
  8.1414 +    sq_tpk_t tpk = NULL;
  8.1415 +    sq_fingerprint_t sq_fpr = NULL;
  8.1416 +    char *fpr = NULL;
  8.1417 +
  8.1418 +    assert(session);
  8.1419 +    assert(identity);
  8.1420 +    assert(identity->address);
  8.1421 +    assert(identity->fpr == NULL || identity->fpr[0] == 0);
  8.1422 +    assert(identity->username);
  8.1423 +
  8.1424 +    asprintf(&userid, "%s <%s>", identity->username, identity->address);
  8.1425 +    if (! userid)
  8.1426 +        ERROR_OUT(session, PEP_OUT_OF_MEMORY, "asprintf");
  8.1427 +
  8.1428 +    T("(%s)", userid);
  8.1429 +
  8.1430 +    // Generate a key.
  8.1431 +    sq_tsk_t tsk;
  8.1432 +    sq_signature_t rev;
  8.1433 +    if (sq_tsk_new(session->ctx, userid, &tsk, &rev) != 0)
  8.1434 +        ERROR_OUT(session, PEP_CANNOT_CREATE_KEY, "Generating a key pair");
  8.1435 +
  8.1436 +    // XXX: We should return this.
  8.1437 +    // sq_signature_free(rev);
  8.1438 +
  8.1439 +    tpk = sq_tsk_into_tpk(tsk);
  8.1440 +
  8.1441 +    // Get the fingerprint.
  8.1442 +    sq_fpr = sq_tpk_fingerprint(tpk);
  8.1443 +    fpr = sq_fingerprint_to_hex(sq_fpr);
  8.1444 +
  8.1445 +    status = tpk_save(session, tpk, NULL);
  8.1446 +    tpk = NULL;
  8.1447 +    if (status != 0)
  8.1448 +        ERROR_OUT(session, PEP_CANNOT_CREATE_KEY, "saving TSK");
  8.1449 +
  8.1450 +    free(identity->fpr);
  8.1451 +    identity->fpr = fpr;
  8.1452 +    fpr = NULL;
  8.1453 +
  8.1454 + out:
  8.1455 +    if (sq_fpr)
  8.1456 +        sq_fingerprint_free(sq_fpr);
  8.1457 +    free(fpr);
  8.1458 +    if (tpk)
  8.1459 +        sq_tpk_free(tpk);
  8.1460 +    free(userid);
  8.1461 +
  8.1462 +    T("-> %s", pep_status_to_string(status));
  8.1463 +    return status;
  8.1464 +}
  8.1465 +
  8.1466 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr_raw)
  8.1467 +{
  8.1468 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1469 +    char *fpr = sq_fingerprint_canonicalize(fpr_raw);
  8.1470 +
  8.1471 +    T("(%s)", fpr);
  8.1472 +
  8.1473 +    // XXX: Can also be used for deleting public keys!!!
  8.1474 +    assert(!"implement me");
  8.1475 +
  8.1476 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.1477 +
  8.1478 +    free(fpr);
  8.1479 +    return status;
  8.1480 +}
  8.1481 +
  8.1482 +// XXX: This needs to handle not only TPKs, but also keyrings and
  8.1483 +// revocation certificates.  Right now, we only import a single TPK
  8.1484 +// and ignore everything else.
  8.1485 +PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
  8.1486 +                              size_t size, identity_list **private_idents)
  8.1487 +{
  8.1488 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1489 +
  8.1490 +    if (private_idents)
  8.1491 +        *private_idents = NULL;
  8.1492 +
  8.1493 +    T("parsing %zd bytes", size);
  8.1494 +
  8.1495 +    sq_packet_parser_result_t ppr
  8.1496 +        = sq_packet_parser_from_bytes(session->ctx, (uint8_t *) key_data, size);
  8.1497 +    if (! ppr)
  8.1498 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "Creating packet parser");
  8.1499 +
  8.1500 +    sq_tag_t tag = sq_packet_parser_result_tag(ppr);
  8.1501 +    switch (tag) {
  8.1502 +    case SQ_TAG_SIGNATURE:
  8.1503 +        // XXX: Implement me.
  8.1504 +        assert(!"Have possible revocation certificate!");
  8.1505 +        break;
  8.1506 +
  8.1507 +    case SQ_TAG_PUBLIC_KEY:
  8.1508 +    case SQ_TAG_SECRET_KEY: {
  8.1509 +        sq_tpk_t tpk = sq_tpk_from_packet_parser(session->ctx, ppr);
  8.1510 +        if (! tpk)
  8.1511 +            ERROR_OUT(session, PEP_UNKNOWN_ERROR, "parsing key data");
  8.1512 +
  8.1513 +        // If private_idents is not NULL and there is any private key
  8.1514 +        // material, it will be saved.
  8.1515 +        status = tpk_save(session, tpk, private_idents);
  8.1516 +        ERROR_OUT(session, status, "saving TPK");
  8.1517 +
  8.1518 +        break;
  8.1519 +    }
  8.1520 +    default:
  8.1521 +        ERROR_OUT(session, PEP_STATUS_OK,
  8.1522 +                  "Can't import %s", sq_tag_to_string(tag));
  8.1523 +        break;
  8.1524 +    }
  8.1525 +
  8.1526 + out:
  8.1527 +    T("-> %s", pep_status_to_string(status));
  8.1528 +    return status;
  8.1529 +}
  8.1530 +
  8.1531 +PEP_STATUS pgp_export_keydata(
  8.1532 +        PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
  8.1533 +        bool secret)
  8.1534 +{
  8.1535 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1536 +    sq_tpk_t secret_key = NULL;
  8.1537 +    sq_tpk_t tpk = NULL;
  8.1538 +
  8.1539 +    assert(session);
  8.1540 +    assert(fpr);
  8.1541 +    assert(key_data);
  8.1542 +    assert(*key_data == NULL);
  8.1543 +    assert(size);
  8.1544 +
  8.1545 +    *size = 0;
  8.1546 +
  8.1547 +    T("(%s, %s)", fpr, secret ? "secret" : "public");
  8.1548 +
  8.1549 +    if (secret) {
  8.1550 +        status = tpk_find_by_fpr_hex(session, fpr, true, &secret_key, NULL);
  8.1551 +        if (status == PEP_KEY_NOT_FOUND)
  8.1552 +            status = PEP_STATUS_OK;
  8.1553 +        ERROR_OUT(session, status, "Looking up TSK for %s", fpr);
  8.1554 +    }
  8.1555 +
  8.1556 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.1557 +    status = tpk_find_by_fpr(session, sq_fpr, false, &tpk, NULL);
  8.1558 +    sq_fingerprint_free(sq_fpr);
  8.1559 +    ERROR_OUT(session, status, "Looking up TPK for %s", fpr);
  8.1560 +
  8.1561 +    if (secret_key) {
  8.1562 +        tpk = sq_tpk_merge(session->ctx, tpk, secret_key);
  8.1563 +        // sq_tpk_merge can return NULL if the primary keys don't
  8.1564 +        // match.  But, we looked up the tpk by the secret key's
  8.1565 +        // fingerprint so this should not be possible.
  8.1566 +        assert(tpk);
  8.1567 +        secret_key = NULL;
  8.1568 +    }
  8.1569 +
  8.1570 +    sq_writer_t memory_writer = sq_writer_alloc((void **) key_data, size);
  8.1571 +    if (! memory_writer)
  8.1572 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "creating memory writer");
  8.1573 +    sq_writer_t armor_writer = sq_armor_writer_new(session->ctx,
  8.1574 +                                                   memory_writer,
  8.1575 +                                                   SQ_ARMOR_KIND_PUBLICKEY,
  8.1576 +                                                   NULL, 0);
  8.1577 +    if (! armor_writer) {
  8.1578 +        sq_writer_free(memory_writer);
  8.1579 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "creating armored writer");
  8.1580 +    }
  8.1581 +
  8.1582 +    if (secret) {
  8.1583 +        sq_tsk_t tsk = sq_tpk_into_tsk(tpk);
  8.1584 +        sq_tsk_serialize(session->ctx, tsk, armor_writer);
  8.1585 +        tpk = sq_tsk_into_tpk(tsk);
  8.1586 +    } else {
  8.1587 +        sq_tpk_serialize(session->ctx, tpk, armor_writer);
  8.1588 +    }
  8.1589 +
  8.1590 + out:
  8.1591 +    if (tpk)
  8.1592 +        sq_tpk_free(tpk);
  8.1593 +
  8.1594 +    if (armor_writer)
  8.1595 +        sq_writer_free(armor_writer);
  8.1596 +
  8.1597 +    if (secret_key)
  8.1598 +        sq_tpk_free(secret_key);
  8.1599 +
  8.1600 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.1601 +    return status;
  8.1602 +}
  8.1603 +
  8.1604 +char* _undot_address(const char* address) {
  8.1605 +    if (!address)
  8.1606 +        return NULL;
  8.1607 +
  8.1608 +    int addr_len = strlen(address);
  8.1609 +    const char* at = strstr(address, "@");
  8.1610 +
  8.1611 +    if (!at)
  8.1612 +        at = address + addr_len;
  8.1613 +
  8.1614 +    char* retval = calloc(1, addr_len + 1);
  8.1615 +
  8.1616 +    const char* addr_curr = address;
  8.1617 +    char* retval_curr = retval;
  8.1618 +
  8.1619 +    while (addr_curr < at) {
  8.1620 +        if (*addr_curr == '.') {
  8.1621 +            addr_curr++;
  8.1622 +            continue;
  8.1623 +        }
  8.1624 +        *retval_curr = *addr_curr;
  8.1625 +        retval_curr++;
  8.1626 +        addr_curr++;
  8.1627 +    }
  8.1628 +    if (*addr_curr == '@')
  8.1629 +        strcat(retval_curr, addr_curr);
  8.1630 +
  8.1631 +    return retval;
  8.1632 +}
  8.1633 +
  8.1634 +static stringpair_list_t *add_key(PEP_SESSION session,
  8.1635 +                                  stringpair_list_t *keyinfo_list,
  8.1636 +                                  stringlist_t* keylist,
  8.1637 +                                  sq_tpk_t tpk, sq_fingerprint_t fpr) {
  8.1638 +    bool revoked = false;
  8.1639 +    // Don't add revoked keys to the keyinfo_list.
  8.1640 +    if (keyinfo_list) {
  8.1641 +        sq_revocation_status_t rs = sq_tpk_revocation_status(tpk);
  8.1642 +        sq_revocation_status_variant_t rsv = sq_revocation_status_variant(rs);
  8.1643 +        sq_revocation_status_free(rs);
  8.1644 +        if (rsv == SQ_REVOCATION_STATUS_REVOKED)
  8.1645 +            revoked = true;
  8.1646 +    }
  8.1647 +
  8.1648 +    if (revoked && ! keylist)
  8.1649 +        return keyinfo_list;
  8.1650 +
  8.1651 +    int dealloc_fpr = 0;
  8.1652 +    if (!fpr) {
  8.1653 +        dealloc_fpr = 1;
  8.1654 +        fpr = sq_tpk_fingerprint(tpk);
  8.1655 +    }
  8.1656 +    char *fpr_str = sq_fingerprint_to_hex(fpr);
  8.1657 +
  8.1658 +    if (!revoked && keyinfo_list) {
  8.1659 +        char *user_id = sq_tpk_primary_user_id(tpk);
  8.1660 +        if (user_id)
  8.1661 +            keyinfo_list = stringpair_list_add(keyinfo_list,
  8.1662 +                                               new_stringpair(fpr_str, user_id));
  8.1663 +        free(user_id);
  8.1664 +    }
  8.1665 +
  8.1666 +    if (keylist)
  8.1667 +        keylist = stringlist_add(keylist, fpr_str);
  8.1668 +
  8.1669 +    free(fpr_str);
  8.1670 +    if (dealloc_fpr)
  8.1671 +        sq_fingerprint_free(fpr);
  8.1672 +
  8.1673 +    return keyinfo_list;
  8.1674 +}
  8.1675 +
  8.1676 +static PEP_STATUS list_keys(PEP_SESSION session,
  8.1677 +                            const char* pattern, int private_only,
  8.1678 +                            stringpair_list_t** keyinfo_list, stringlist_t** keylist)
  8.1679 +{
  8.1680 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1681 +    sq_tpk_t tpk = NULL;
  8.1682 +    sq_fingerprint_t fpr = NULL;
  8.1683 +
  8.1684 +    T("('%s', private: %d)", pattern, private_only);
  8.1685 +
  8.1686 +    stringpair_list_t* _keyinfo_list = NULL;
  8.1687 +    if (keyinfo_list) {
  8.1688 +        _keyinfo_list = new_stringpair_list(NULL);
  8.1689 +        if (!_keyinfo_list)
  8.1690 +            ERROR_OUT(session, PEP_OUT_OF_MEMORY, "new_stringpair_list");
  8.1691 +    }
  8.1692 +    stringlist_t* _keylist = NULL;
  8.1693 +    if (keylist) {
  8.1694 +        _keylist = new_stringlist(NULL);
  8.1695 +        if (!_keylist)
  8.1696 +            ERROR_OUT(session, PEP_OUT_OF_MEMORY, "new_string_list");
  8.1697 +    }
  8.1698 +
  8.1699 +    // Trim any leading space.  This also makes it easier to recognize
  8.1700 +    // a string that is only whitespace.
  8.1701 +    while (*pattern == ' ')
  8.1702 +        pattern ++;
  8.1703 +
  8.1704 +    if (strchr(pattern, '@')) {
  8.1705 +        // Looks like a mailbox.
  8.1706 +        sq_tpk_t *tpks = NULL;
  8.1707 +        int count = 0;
  8.1708 +        status = tpk_find_by_email(session, pattern, private_only, &tpks, &count);
  8.1709 +        ERROR_OUT(session, status, "Looking up '%s'", pattern);
  8.1710 +        for (int i = 0; i < count; i ++) {
  8.1711 +            add_key(session, _keyinfo_list, _keylist, tpks[i], NULL);
  8.1712 +            sq_tpk_free(tpks[i]);
  8.1713 +        }
  8.1714 +        free(tpks);
  8.1715 +
  8.1716 +        if (count == 0) {
  8.1717 +            // If match failed, check to see if we've got a dotted
  8.1718 +            // address in the pattern.  If so, try again without dots.
  8.1719 +            const char* dotpos = strstr(pattern, ".");
  8.1720 +            const char* atpos = strstr(pattern, "@");
  8.1721 +            if (dotpos && atpos && (dotpos < atpos)) {
  8.1722 +                char* undotted = _undot_address(pattern);
  8.1723 +                if (undotted) {
  8.1724 +                    PEP_STATUS status = list_keys(session, undotted, private_only,
  8.1725 +                                                  keyinfo_list, keylist);
  8.1726 +                    free(undotted);
  8.1727 +                    return status;
  8.1728 +                }
  8.1729 +            }
  8.1730 +        }
  8.1731 +    } else if (// Only hex characters and spaces
  8.1732 +               pattern[strspn(pattern, "0123456789aAbBcCdDeEfF ")] == 0
  8.1733 +               // And a fair amount of them.
  8.1734 +               && strlen(pattern) >= 16) {
  8.1735 +        // Fingerprint.
  8.1736 +        fpr = sq_fingerprint_from_hex(pattern);
  8.1737 +        status = tpk_find_by_fpr(session, fpr, false, &tpk, NULL);
  8.1738 +        ERROR_OUT(session, status, "Looking up key");
  8.1739 +        add_key(session, _keyinfo_list, _keylist, tpk, fpr);
  8.1740 +    } else if (pattern[0] == 0) {
  8.1741 +        // Empty string.
  8.1742 +
  8.1743 +        sq_tpk_t *tpks = NULL;
  8.1744 +        int count = 0;
  8.1745 +        status = tpk_all(session, private_only, &tpks, &count);
  8.1746 +        ERROR_OUT(session, status, "Looking up '%s'", pattern);
  8.1747 +        for (int i = 0; i < count; i ++) {
  8.1748 +            add_key(session, _keyinfo_list, _keylist, tpks[i], NULL);
  8.1749 +            sq_tpk_free(tpks[i]);
  8.1750 +        }
  8.1751 +        free(tpks);
  8.1752 +    } else {
  8.1753 +        T("unsupported pattern '%s'", pattern);
  8.1754 +    }
  8.1755 +
  8.1756 + out:
  8.1757 +    if (tpk)
  8.1758 +        sq_tpk_free(tpk);
  8.1759 +    if (fpr)
  8.1760 +        sq_fingerprint_free(fpr);
  8.1761 +
  8.1762 +    if (status == PEP_KEY_NOT_FOUND)
  8.1763 +        status = PEP_STATUS_OK;
  8.1764 +
  8.1765 +    if (status != PEP_STATUS_OK || (_keyinfo_list && !_keyinfo_list->value)) {
  8.1766 +        free_stringpair_list(_keyinfo_list);
  8.1767 +        _keyinfo_list = NULL;
  8.1768 +    }
  8.1769 +    if (keyinfo_list)
  8.1770 +        *keyinfo_list = _keyinfo_list;
  8.1771 +
  8.1772 +    if (status != PEP_STATUS_OK || (_keylist && !_keylist->value)) {
  8.1773 +        free_stringlist(_keylist);
  8.1774 +        _keylist = NULL;
  8.1775 +    }
  8.1776 +    if (keylist)
  8.1777 +        *keylist = _keylist;
  8.1778 +
  8.1779 +    int len = -1;
  8.1780 +    if (keylist)
  8.1781 +        len = stringlist_length(*keylist);
  8.1782 +    else if (keyinfo_list)
  8.1783 +        len = stringpair_list_length(*keyinfo_list);
  8.1784 +    T("(%s) -> %s (%d keys)", pattern, pep_status_to_string(status), len);
  8.1785 +    return status;
  8.1786 +}
  8.1787 +
  8.1788 +// pattern could be empty, an fpr, or a mailbox.
  8.1789 +//
  8.1790 +// keyinfo_list is a list of <fpr, openpgp userid> tuples for the
  8.1791 +// matching keys.
  8.1792 +//
  8.1793 +// This function filters out revoked key, but not expired keys.
  8.1794 +PEP_STATUS pgp_list_keyinfo(PEP_SESSION session,
  8.1795 +                            const char* pattern,
  8.1796 +                            stringpair_list_t** keyinfo_list)
  8.1797 +{
  8.1798 +    return list_keys(session, pattern, false, keyinfo_list, NULL);
  8.1799 +}
  8.1800 +
  8.1801 +PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
  8.1802 +{
  8.1803 +    assert(!"pgp_recv_key not implemented");
  8.1804 +    return PEP_UNKNOWN_ERROR;
  8.1805 +}
  8.1806 +
  8.1807 +// Unlike pgp_list_keyinfo, this function returns revoked keys.
  8.1808 +PEP_STATUS pgp_find_keys(
  8.1809 +    PEP_SESSION session, const char *pattern, stringlist_t **keylist)
  8.1810 +{
  8.1811 +    return list_keys(session, pattern, false, NULL, keylist);
  8.1812 +}
  8.1813 +
  8.1814 +// Unlike pgp_list_keyinfo, this function returns revoked keys.
  8.1815 +PEP_STATUS pgp_find_private_keys(
  8.1816 +    PEP_SESSION session, const char *pattern, stringlist_t **keylist)
  8.1817 +{
  8.1818 +    return list_keys(session, pattern, true, NULL, keylist);
  8.1819 +}
  8.1820 +
  8.1821 +PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
  8.1822 +{
  8.1823 +    assert(!"pgp_send_key not implemented");
  8.1824 +    return PEP_UNKNOWN_ERROR;
  8.1825 +}
  8.1826 +
  8.1827 +PEP_STATUS pgp_get_key_rating(
  8.1828 +    PEP_SESSION session, const char *fpr, PEP_comm_type *comm_type)
  8.1829 +{
  8.1830 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1831 +    sq_tpk_t tpk = NULL;
  8.1832 +
  8.1833 +    assert(session);
  8.1834 +    assert(fpr);
  8.1835 +    assert(comm_type);
  8.1836 +
  8.1837 +    *comm_type = PEP_ct_unknown;
  8.1838 +
  8.1839 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.1840 +    status = tpk_find_by_fpr(session, sq_fpr, false, &tpk, NULL);
  8.1841 +    sq_fingerprint_free(sq_fpr);
  8.1842 +    ERROR_OUT(session, status, "Looking up key: %s", fpr);
  8.1843 +
  8.1844 +    *comm_type = PEP_ct_OpenPGP_unconfirmed;
  8.1845 +
  8.1846 +    if (sq_tpk_expired(tpk)) {
  8.1847 +        *comm_type = PEP_ct_key_expired;
  8.1848 +        goto out;
  8.1849 +    }
  8.1850 +
  8.1851 +    sq_revocation_status_t rs = sq_tpk_revocation_status(tpk);
  8.1852 +    sq_revocation_status_variant_t rsv = sq_revocation_status_variant(rs);
  8.1853 +    sq_revocation_status_free(rs);
  8.1854 +    if (rsv == SQ_REVOCATION_STATUS_REVOKED) {
  8.1855 +        *comm_type = PEP_ct_key_revoked;
  8.1856 +        goto out;
  8.1857 +    }
  8.1858 +
  8.1859 +    PEP_comm_type best_enc = PEP_ct_no_encryption, best_sign = PEP_ct_no_encryption;
  8.1860 +    sq_tpk_key_iter_t key_iter = sq_tpk_key_iter(tpk);
  8.1861 +    sq_p_key_t key;
  8.1862 +    sq_signature_t sig;
  8.1863 +    sq_revocation_status_t rev;
  8.1864 +    while ((key = sq_tpk_key_iter_next(key_iter, &sig, &rev))) {
  8.1865 +        if (! sig)
  8.1866 +            continue;
  8.1867 +
  8.1868 +        if (sq_revocation_status_variant(rev) == SQ_REVOCATION_STATUS_REVOKED)
  8.1869 +            continue;
  8.1870 +
  8.1871 +        if (! sq_p_key_alive(key, sig))
  8.1872 +            continue;
  8.1873 +
  8.1874 +        PEP_comm_type curr = PEP_ct_no_encryption;
  8.1875 +
  8.1876 +        int can_enc = sq_signature_can_encrypt_for_transport(sig)
  8.1877 +            || sq_signature_can_encrypt_at_rest(sig);
  8.1878 +        int can_sign = sq_signature_can_sign(sig);
  8.1879 +
  8.1880 +        sq_public_key_algo_t pk_algo = sq_p_key_public_key_algo(key);
  8.1881 +        if (pk_algo == SQ_PUBLIC_KEY_ALGO_RSA_ENCRYPT_SIGN
  8.1882 +            || pk_algo == SQ_PUBLIC_KEY_ALGO_RSA_ENCRYPT
  8.1883 +            || pk_algo == SQ_PUBLIC_KEY_ALGO_RSA_SIGN) {
  8.1884 +            int bits = sq_p_key_public_key_bits(key);
  8.1885 +            if (bits < 1024)
  8.1886 +                curr = PEP_ct_key_too_short;
  8.1887 +            else if (bits == 1024)
  8.1888 +                curr = PEP_ct_OpenPGP_weak_unconfirmed;
  8.1889 +            else
  8.1890 +                curr = PEP_ct_OpenPGP_unconfirmed;
  8.1891 +        } else {
  8.1892 +            curr = PEP_ct_OpenPGP_unconfirmed;
  8.1893 +        }
  8.1894 +
  8.1895 +        if (can_enc)
  8.1896 +            best_enc = _MAX(best_enc, curr);
  8.1897 +
  8.1898 +        if (can_sign)
  8.1899 +            best_sign = _MAX(best_sign, curr);
  8.1900 +    }
  8.1901 +    sq_tpk_key_iter_free(key_iter);
  8.1902 +
  8.1903 +    if (best_enc == PEP_ct_no_encryption || best_sign == PEP_ct_no_encryption) {
  8.1904 +        *comm_type = PEP_ct_key_b0rken;
  8.1905 +        goto out;
  8.1906 +    } else {
  8.1907 +        *comm_type = _MIN(best_enc, best_sign);
  8.1908 +    }
  8.1909 +
  8.1910 + out:
  8.1911 +    if (tpk)
  8.1912 +        sq_tpk_free(tpk);
  8.1913 +
  8.1914 +    T("(%s) -> %s", fpr, pep_comm_type_to_string(*comm_type));
  8.1915 +    return status;
  8.1916 +}
  8.1917 +
  8.1918 +
  8.1919 +PEP_STATUS pgp_renew_key(
  8.1920 +    PEP_SESSION session, const char *fpr, const timestamp *ts)
  8.1921 +{
  8.1922 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1923 +    sq_tpk_t tpk = NULL;
  8.1924 +    time_t t = mktime((struct tm *) ts);
  8.1925 +
  8.1926 +    T("(%s)", fpr);
  8.1927 +
  8.1928 +    status = tpk_find_by_fpr_hex(session, fpr, true, &tpk, NULL);
  8.1929 +    ERROR_OUT(session, status, "Looking up '%s'", fpr);
  8.1930 +
  8.1931 +    uint32_t creation_time = sq_p_key_creation_time(sq_tpk_primary(tpk));
  8.1932 +    if (creation_time > t)
  8.1933 +        // The creation time is after the expiration time!
  8.1934 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR,
  8.1935 +                  "creation time can't be after expiration time");
  8.1936 +
  8.1937 +    uint32_t delta = t - creation_time;
  8.1938 +    tpk = sq_tpk_set_expiry(session->ctx, tpk, delta);
  8.1939 +    if (! tpk)
  8.1940 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "setting expiration");
  8.1941 +
  8.1942 +    status = tpk_save(session, tpk, NULL);
  8.1943 +    tpk = NULL;
  8.1944 +    ERROR_OUT(session, status, "Saving %s", fpr);
  8.1945 +
  8.1946 + out:
  8.1947 +    if (tpk)
  8.1948 +        sq_tpk_free(tpk);
  8.1949 +
  8.1950 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.1951 +    return status;
  8.1952 +}
  8.1953 +
  8.1954 +PEP_STATUS pgp_revoke_key(
  8.1955 +    PEP_SESSION session, const char *fpr, const char *reason)
  8.1956 +{
  8.1957 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1958 +    sq_tpk_t tpk = NULL;
  8.1959 +
  8.1960 +    T("(%s)", fpr);
  8.1961 +
  8.1962 +    status = tpk_find_by_fpr_hex(session, fpr, true, &tpk, NULL);
  8.1963 +    ERROR_OUT(session, status, "Looking up %s", fpr);
  8.1964 +
  8.1965 +    tpk = sq_tpk_revoke_in_place(session->ctx, tpk,
  8.1966 +                                 SQ_REASON_FOR_REVOCATION_UNSPECIFIED,
  8.1967 +                                 reason);
  8.1968 +    if (! tpk)
  8.1969 +        ERROR_OUT(session, PEP_UNKNOWN_ERROR, "setting expiration");
  8.1970 +
  8.1971 +    assert(sq_revocation_status_variant(sq_tpk_revocation_status(tpk))
  8.1972 +           == SQ_REVOCATION_STATUS_REVOKED);
  8.1973 +
  8.1974 +    status = tpk_save(session, tpk, NULL);
  8.1975 +    tpk = NULL;
  8.1976 +    ERROR_OUT(session, status, "Saving %s", fpr);
  8.1977 +
  8.1978 + out:
  8.1979 +    if (tpk)
  8.1980 +        sq_tpk_free(tpk);
  8.1981 +
  8.1982 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.1983 +    return status;
  8.1984 +}
  8.1985 +
  8.1986 +PEP_STATUS pgp_key_expired(PEP_SESSION session, const char *fpr,
  8.1987 +                           const time_t when, bool *expired)
  8.1988 +{
  8.1989 +    PEP_STATUS status = PEP_STATUS_OK;
  8.1990 +    sq_tpk_t tpk = NULL;
  8.1991 +    T("(%s)", fpr);
  8.1992 +
  8.1993 +    assert(session);
  8.1994 +    assert(fpr);
  8.1995 +    assert(expired);
  8.1996 +
  8.1997 +    *expired = false;
  8.1998 +
  8.1999 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.2000 +    status = tpk_find_by_fpr(session, sq_fpr, false, &tpk, NULL);
  8.2001 +    sq_fingerprint_free(sq_fpr);
  8.2002 +    ERROR_OUT(session, status, "Looking up %s", fpr);
  8.2003 +
  8.2004 +    // Is the TPK live?
  8.2005 +    *expired = !sq_tpk_alive_at(tpk, when);
  8.2006 +    if (*expired)
  8.2007 +        goto out;
  8.2008 +
  8.2009 +    // Are there at least one certification subkey, one signing subkey
  8.2010 +    // and one encryption subkey that are live?
  8.2011 +    int can_certify = 0, can_encrypt = 0, can_sign = 0;
  8.2012 +
  8.2013 +    sq_tpk_key_iter_t key_iter = sq_tpk_key_iter(tpk);
  8.2014 +    sq_p_key_t key;
  8.2015 +    sq_signature_t sig;
  8.2016 +    sq_revocation_status_t rev;
  8.2017 +    while ((key = sq_tpk_key_iter_next(key_iter, &sig, &rev))) {
  8.2018 +        if (! sig)
  8.2019 +            continue;
  8.2020 +
  8.2021 +        if (sq_revocation_status_variant(rev) == SQ_REVOCATION_STATUS_REVOKED)
  8.2022 +            continue;
  8.2023 +
  8.2024 +        if (!sq_p_key_alive_at(key, sig, when))
  8.2025 +            continue;
  8.2026 +
  8.2027 +        if (sq_signature_can_encrypt_for_transport(sig)
  8.2028 +            || sq_signature_can_encrypt_at_rest(sig))
  8.2029 +            can_encrypt = 1;
  8.2030 +        if (sq_signature_can_sign(sig))
  8.2031 +            can_sign = 1;
  8.2032 +        if (sq_signature_can_certify(sig))
  8.2033 +            can_certify = 1;
  8.2034 +
  8.2035 +        if (can_encrypt && can_sign && can_certify)
  8.2036 +            break;
  8.2037 +    }
  8.2038 +    sq_tpk_key_iter_free(key_iter);
  8.2039 +
  8.2040 +    *expired = !(can_encrypt && can_sign && can_certify);
  8.2041 +
  8.2042 + out:
  8.2043 +    if (tpk)
  8.2044 +        sq_tpk_free(tpk);
  8.2045 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.2046 +    return status;
  8.2047 +}
  8.2048 +
  8.2049 +PEP_STATUS pgp_key_revoked(PEP_SESSION session, const char *fpr, bool *revoked)
  8.2050 +{
  8.2051 +    PEP_STATUS status = PEP_STATUS_OK;
  8.2052 +    sq_tpk_t tpk;
  8.2053 +
  8.2054 +    T("(%s)", fpr);
  8.2055 +
  8.2056 +    assert(session);
  8.2057 +    assert(fpr);
  8.2058 +    assert(revoked);
  8.2059 +
  8.2060 +    *revoked = false;
  8.2061 +
  8.2062 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.2063 +    status = tpk_find_by_fpr(session, sq_fpr, false, &tpk, NULL);
  8.2064 +    sq_fingerprint_free(sq_fpr);
  8.2065 +    ERROR_OUT(session, status, "Looking up %s", fpr);
  8.2066 +
  8.2067 +    sq_revocation_status_t rs = sq_tpk_revocation_status(tpk);
  8.2068 +    *revoked = sq_revocation_status_variant(rs) == SQ_REVOCATION_STATUS_REVOKED;
  8.2069 +    sq_revocation_status_free (rs);
  8.2070 +    sq_tpk_free(tpk);
  8.2071 +
  8.2072 + out:
  8.2073 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.2074 +    return status;
  8.2075 +}
  8.2076 +
  8.2077 +PEP_STATUS pgp_key_created(PEP_SESSION session, const char *fpr, time_t *created)
  8.2078 +{
  8.2079 +    PEP_STATUS status = PEP_STATUS_OK;
  8.2080 +    sq_tpk_t tpk = NULL;
  8.2081 +    T("(%s)", fpr);
  8.2082 +
  8.2083 +    *created = 0;
  8.2084 +
  8.2085 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.2086 +    status = tpk_find_by_fpr(session, sq_fpr, false, &tpk, NULL);
  8.2087 +    sq_fingerprint_free(sq_fpr);
  8.2088 +    ERROR_OUT(session, status, "Looking up %s", fpr);
  8.2089 +
  8.2090 +    sq_p_key_t k = sq_tpk_primary(tpk);
  8.2091 +    *created = sq_p_key_creation_time(k);
  8.2092 +    sq_tpk_free(tpk);
  8.2093 +
  8.2094 + out:
  8.2095 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.2096 +    return status;
  8.2097 +}
  8.2098 +
  8.2099 +PEP_STATUS pgp_binary(const char **path)
  8.2100 +{
  8.2101 +    return PEP_STATUS_OK;
  8.2102 +}
  8.2103 +
  8.2104 +PEP_STATUS pgp_contains_priv_key(PEP_SESSION session, const char *fpr,
  8.2105 +                                 bool *has_private)
  8.2106 +{
  8.2107 +    T("(%s)", fpr);
  8.2108 +    sq_fingerprint_t sq_fpr = sq_fingerprint_from_hex(fpr);
  8.2109 +    PEP_STATUS status = tpk_find_by_fpr(session, sq_fpr, true, NULL, NULL);
  8.2110 +    sq_fingerprint_free(sq_fpr);
  8.2111 +    if (status == PEP_STATUS_OK) {
  8.2112 +        *has_private = 1;
  8.2113 +    } else if (status == PEP_KEY_NOT_FOUND) {
  8.2114 +        *has_private = 0;
  8.2115 +        status = PEP_STATUS_OK;
  8.2116 +    }
  8.2117 +    T("(%s) -> %s", fpr, pep_status_to_string(status));
  8.2118 +    return status;
  8.2119 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/pgp_sequoia.h	Fri Jan 11 06:00:08 2019 +0100
     9.3 @@ -0,0 +1,115 @@
     9.4 +// This file is under GNU General Public License 3.0
     9.5 +// see LICENSE.txt
     9.6 +
     9.7 +#pragma once
     9.8 +
     9.9 +#include "pEpEngine.h"
    9.10 +
    9.11 +PEP_STATUS pgp_init(PEP_SESSION session, bool in_first);
    9.12 +void pgp_release(PEP_SESSION session, bool out_last);
    9.13 +
    9.14 +PEP_STATUS pgp_decrypt_and_verify(
    9.15 +        PEP_SESSION session, const char *ctext, size_t csize,
    9.16 +        const char *dsigtext, size_t dsigsize,
    9.17 +        char **ptext, size_t *psize, stringlist_t **keylist,
    9.18 +        char** filename_ptr
    9.19 +    );
    9.20 +
    9.21 +PEP_STATUS pgp_encrypt_and_sign(
    9.22 +        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
    9.23 +        size_t psize, char **ctext, size_t *csize
    9.24 +    );
    9.25 +
    9.26 +PEP_STATUS pgp_sign_only(
    9.27 +        PEP_SESSION session, const char* fpr, const char *ptext,
    9.28 +        size_t psize, char **stext, size_t *ssize
    9.29 +    );
    9.30 +
    9.31 +PEP_STATUS pgp_encrypt_only(
    9.32 +        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
    9.33 +        size_t psize, char **ctext, size_t *csize
    9.34 +    );
    9.35 +
    9.36 +
    9.37 +PEP_STATUS pgp_verify_text(
    9.38 +        PEP_SESSION session, const char *text, size_t size,
    9.39 +        const char *signature, size_t sig_size, stringlist_t **keylist
    9.40 +    );
    9.41 +
    9.42 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr);
    9.43 +
    9.44 +PEP_STATUS pgp_export_keydata(
    9.45 +        PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
    9.46 +        bool secret
    9.47 +    );
    9.48 +
    9.49 +PEP_STATUS pgp_find_keys(
    9.50 +        PEP_SESSION session, const char *pattern, stringlist_t **keylist
    9.51 +    );
    9.52 +
    9.53 +PEP_STATUS pgp_list_keyinfo(
    9.54 +        PEP_SESSION session, const char* pattern, stringpair_list_t** keyinfo_list
    9.55 +    );
    9.56 +
    9.57 +PEP_STATUS pgp_generate_keypair(
    9.58 +        PEP_SESSION session, pEp_identity *identity
    9.59 +    );
    9.60 +
    9.61 +PEP_STATUS pgp_get_key_rating(
    9.62 +        PEP_SESSION session,
    9.63 +        const char *fpr,
    9.64 +        PEP_comm_type *comm_type
    9.65 +    );
    9.66 +
    9.67 +PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
    9.68 +                              size_t size, identity_list **private_idents);
    9.69 +
    9.70 +PEP_STATUS pgp_import_private_keydata(PEP_SESSION session, const char *key_data,
    9.71 +                                      size_t size, identity_list **private_idents);
    9.72 +
    9.73 +PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern);
    9.74 +PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern);
    9.75 +
    9.76 +PEP_STATUS pgp_renew_key(
    9.77 +        PEP_SESSION session,
    9.78 +        const char *fpr,
    9.79 +        const timestamp *ts
    9.80 +    );
    9.81 +
    9.82 +PEP_STATUS pgp_revoke_key(
    9.83 +        PEP_SESSION session,
    9.84 +        const char *fpr,
    9.85 +        const char *reason
    9.86 +    );
    9.87 +
    9.88 +PEP_STATUS pgp_key_expired(
    9.89 +        PEP_SESSION session,
    9.90 +        const char *fpr,
    9.91 +        const time_t when,
    9.92 +        bool *expired
    9.93 +    );
    9.94 +
    9.95 +PEP_STATUS pgp_key_revoked(
    9.96 +        PEP_SESSION session,
    9.97 +        const char *fpr,
    9.98 +        bool *revoked
    9.99 +    );
   9.100 +
   9.101 +PEP_STATUS pgp_key_created(
   9.102 +        PEP_SESSION session,
   9.103 +        const char *fpr,
   9.104 +        time_t *created
   9.105 +    );
   9.106 +
   9.107 +PEP_STATUS pgp_contains_priv_key(
   9.108 +        PEP_SESSION session, 
   9.109 +        const char *fpr,
   9.110 +        bool *has_private);
   9.111 +
   9.112 +PEP_STATUS pgp_find_private_keys(
   9.113 +    PEP_SESSION session, const char *pattern, stringlist_t **keylist
   9.114 +);
   9.115 +
   9.116 +PEP_STATUS pgp_binary(const char **path);
   9.117 +
   9.118 +#define PGP_BINARY_PATH pgp_binary
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/pgp_sequoia_internal.h	Fri Jan 11 06:00:08 2019 +0100
    10.3 @@ -0,0 +1,6 @@
    10.4 +// This file is under GNU General Public License 3.0
    10.5 +// see LICENSE.txt
    10.6 +
    10.7 +#pragma once
    10.8 +
    10.9 +#include <sequoia.h>
    11.1 --- a/src/sqlite3.c	Wed Dec 12 09:04:03 2018 +0100
    11.2 +++ b/src/sqlite3.c	Fri Jan 11 06:00:08 2019 +0100
    11.3 @@ -1,6 +1,6 @@
    11.4  /******************************************************************************
    11.5  ** This file is an amalgamation of many separate C source files from SQLite
    11.6 -** version 3.19.3.  By combining all the individual C code files into this
    11.7 +** version 3.26.0.  By combining all the individual C code files into this
    11.8  ** single large file, the entire code can be compiled as a single translation
    11.9  ** unit.  This allows many compilers to do optimizations that would not be
   11.10  ** possible if the files were compiled separately.  Performance improvements
   11.11 @@ -22,6 +22,776 @@
   11.12  #ifndef SQLITE_PRIVATE
   11.13  # define SQLITE_PRIVATE static
   11.14  #endif
   11.15 +/************** Begin file ctime.c *******************************************/
   11.16 +/*
   11.17 +** 2010 February 23
   11.18 +**
   11.19 +** The author disclaims copyright to this source code.  In place of
   11.20 +** a legal notice, here is a blessing:
   11.21 +**
   11.22 +**    May you do good and not evil.
   11.23 +**    May you find forgiveness for yourself and forgive others.
   11.24 +**    May you share freely, never taking more than you give.
   11.25 +**
   11.26 +*************************************************************************
   11.27 +**
   11.28 +** This file implements routines used to report what compile-time options
   11.29 +** SQLite was built with.
   11.30 +*/
   11.31 +
   11.32 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   11.33 +
   11.34 +/*
   11.35 +** Include the configuration header output by 'configure' if we're using the
   11.36 +** autoconf-based build
   11.37 +*/
   11.38 +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
   11.39 +#include "config.h"
   11.40 +#define SQLITECONFIG_H 1
   11.41 +#endif
   11.42 +
   11.43 +/* These macros are provided to "stringify" the value of the define
   11.44 +** for those options in which the value is meaningful. */
   11.45 +#define CTIMEOPT_VAL_(opt) #opt
   11.46 +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
   11.47 +
   11.48 +/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
   11.49 +** option requires a separate macro because legal values contain a single
   11.50 +** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
   11.51 +#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
   11.52 +#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
   11.53 +
   11.54 +/*
   11.55 +** An array of names of all compile-time options.  This array should 
   11.56 +** be sorted A-Z.
   11.57 +**
   11.58 +** This array looks large, but in a typical installation actually uses
   11.59 +** only a handful of compile-time options, so most times this array is usually
   11.60 +** rather short and uses little memory space.
   11.61 +*/
   11.62 +static const char * const sqlite3azCompileOpt[] = {
   11.63 +
   11.64 +/* 
   11.65 +** BEGIN CODE GENERATED BY tool/mkctime.tcl 
   11.66 +*/
   11.67 +#if SQLITE_32BIT_ROWID
   11.68 +  "32BIT_ROWID",
   11.69 +#endif
   11.70 +#if SQLITE_4_BYTE_ALIGNED_MALLOC
   11.71 +  "4_BYTE_ALIGNED_MALLOC",
   11.72 +#endif
   11.73 +#if SQLITE_64BIT_STATS
   11.74 +  "64BIT_STATS",
   11.75 +#endif
   11.76 +#if SQLITE_ALLOW_COVERING_INDEX_SCAN
   11.77 +  "ALLOW_COVERING_INDEX_SCAN",
   11.78 +#endif
   11.79 +#if SQLITE_ALLOW_URI_AUTHORITY
   11.80 +  "ALLOW_URI_AUTHORITY",
   11.81 +#endif
   11.82 +#ifdef SQLITE_BITMASK_TYPE
   11.83 +  "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE),
   11.84 +#endif
   11.85 +#if SQLITE_BUG_COMPATIBLE_20160819
   11.86 +  "BUG_COMPATIBLE_20160819",
   11.87 +#endif
   11.88 +#if SQLITE_CASE_SENSITIVE_LIKE
   11.89 +  "CASE_SENSITIVE_LIKE",
   11.90 +#endif
   11.91 +#if SQLITE_CHECK_PAGES
   11.92 +  "CHECK_PAGES",
   11.93 +#endif
   11.94 +#if defined(__clang__) && defined(__clang_major__)
   11.95 +  "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
   11.96 +                    CTIMEOPT_VAL(__clang_minor__) "."
   11.97 +                    CTIMEOPT_VAL(__clang_patchlevel__),
   11.98 +#elif defined(_MSC_VER)
   11.99 +  "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
  11.100 +#elif defined(__GNUC__) && defined(__VERSION__)
  11.101 +  "COMPILER=gcc-" __VERSION__,
  11.102 +#endif
  11.103 +#if SQLITE_COVERAGE_TEST
  11.104 +  "COVERAGE_TEST",
  11.105 +#endif
  11.106 +#if SQLITE_DEBUG
  11.107 +  "DEBUG",
  11.108 +#endif
  11.109 +#if SQLITE_DEFAULT_AUTOMATIC_INDEX
  11.110 +  "DEFAULT_AUTOMATIC_INDEX",
  11.111 +#endif
  11.112 +#if SQLITE_DEFAULT_AUTOVACUUM
  11.113 +  "DEFAULT_AUTOVACUUM",
  11.114 +#endif
  11.115 +#ifdef SQLITE_DEFAULT_CACHE_SIZE
  11.116 +  "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
  11.117 +#endif
  11.118 +#if SQLITE_DEFAULT_CKPTFULLFSYNC
  11.119 +  "DEFAULT_CKPTFULLFSYNC",
  11.120 +#endif
  11.121 +#ifdef SQLITE_DEFAULT_FILE_FORMAT
  11.122 +  "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
  11.123 +#endif
  11.124 +#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
  11.125 +  "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
  11.126 +#endif
  11.127 +#if SQLITE_DEFAULT_FOREIGN_KEYS
  11.128 +  "DEFAULT_FOREIGN_KEYS",
  11.129 +#endif
  11.130 +#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  11.131 +  "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
  11.132 +#endif
  11.133 +#ifdef SQLITE_DEFAULT_LOCKING_MODE
  11.134 +  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
  11.135 +#endif
  11.136 +#ifdef SQLITE_DEFAULT_LOOKASIDE
  11.137 +  "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
  11.138 +#endif
  11.139 +#if SQLITE_DEFAULT_MEMSTATUS
  11.140 +  "DEFAULT_MEMSTATUS",
  11.141 +#endif
  11.142 +#ifdef SQLITE_DEFAULT_MMAP_SIZE
  11.143 +  "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
  11.144 +#endif
  11.145 +#ifdef SQLITE_DEFAULT_PAGE_SIZE
  11.146 +  "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
  11.147 +#endif
  11.148 +#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
  11.149 +  "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
  11.150 +#endif
  11.151 +#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
  11.152 +  "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
  11.153 +#endif
  11.154 +#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  11.155 +  "DEFAULT_RECURSIVE_TRIGGERS",
  11.156 +#endif
  11.157 +#ifdef SQLITE_DEFAULT_ROWEST
  11.158 +  "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
  11.159 +#endif
  11.160 +#ifdef SQLITE_DEFAULT_SECTOR_SIZE
  11.161 +  "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
  11.162 +#endif
  11.163 +#ifdef SQLITE_DEFAULT_SYNCHRONOUS
  11.164 +  "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
  11.165 +#endif
  11.166 +#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
  11.167 +  "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
  11.168 +#endif
  11.169 +#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
  11.170 +  "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
  11.171 +#endif
  11.172 +#ifdef SQLITE_DEFAULT_WORKER_THREADS
  11.173 +  "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
  11.174 +#endif
  11.175 +#if SQLITE_DIRECT_OVERFLOW_READ
  11.176 +  "DIRECT_OVERFLOW_READ",
  11.177 +#endif
  11.178 +#if SQLITE_DISABLE_DIRSYNC
  11.179 +  "DISABLE_DIRSYNC",
  11.180 +#endif
  11.181 +#if SQLITE_DISABLE_FTS3_UNICODE
  11.182 +  "DISABLE_FTS3_UNICODE",
  11.183 +#endif
  11.184 +#if SQLITE_DISABLE_FTS4_DEFERRED
  11.185 +  "DISABLE_FTS4_DEFERRED",
  11.186 +#endif
  11.187 +#if SQLITE_DISABLE_INTRINSIC
  11.188 +  "DISABLE_INTRINSIC",
  11.189 +#endif
  11.190 +#if SQLITE_DISABLE_LFS
  11.191 +  "DISABLE_LFS",
  11.192 +#endif
  11.193 +#if SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
  11.194 +  "DISABLE_PAGECACHE_OVERFLOW_STATS",
  11.195 +#endif
  11.196 +#if SQLITE_DISABLE_SKIPAHEAD_DISTINCT
  11.197 +  "DISABLE_SKIPAHEAD_DISTINCT",
  11.198 +#endif
  11.199 +#ifdef SQLITE_ENABLE_8_3_NAMES
  11.200 +  "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
  11.201 +#endif
  11.202 +#if SQLITE_ENABLE_API_ARMOR
  11.203 +  "ENABLE_API_ARMOR",
  11.204 +#endif
  11.205 +#if SQLITE_ENABLE_ATOMIC_WRITE
  11.206 +  "ENABLE_ATOMIC_WRITE",
  11.207 +#endif
  11.208 +#if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
  11.209 +  "ENABLE_BATCH_ATOMIC_WRITE",
  11.210 +#endif
  11.211 +#if SQLITE_ENABLE_CEROD
  11.212 +  "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
  11.213 +#endif
  11.214 +#if SQLITE_ENABLE_COLUMN_METADATA
  11.215 +  "ENABLE_COLUMN_METADATA",
  11.216 +#endif
  11.217 +#if SQLITE_ENABLE_COLUMN_USED_MASK
  11.218 +  "ENABLE_COLUMN_USED_MASK",
  11.219 +#endif
  11.220 +#if SQLITE_ENABLE_COSTMULT
  11.221 +  "ENABLE_COSTMULT",
  11.222 +#endif
  11.223 +#if SQLITE_ENABLE_CURSOR_HINTS
  11.224 +  "ENABLE_CURSOR_HINTS",
  11.225 +#endif
  11.226 +#if SQLITE_ENABLE_DBSTAT_VTAB
  11.227 +  "ENABLE_DBSTAT_VTAB",
  11.228 +#endif
  11.229 +#if SQLITE_ENABLE_EXPENSIVE_ASSERT
  11.230 +  "ENABLE_EXPENSIVE_ASSERT",
  11.231 +#endif
  11.232 +#if SQLITE_ENABLE_FTS1
  11.233 +  "ENABLE_FTS1",
  11.234 +#endif
  11.235 +#if SQLITE_ENABLE_FTS2
  11.236 +  "ENABLE_FTS2",
  11.237 +#endif
  11.238 +#if SQLITE_ENABLE_FTS3
  11.239 +  "ENABLE_FTS3",
  11.240 +#endif
  11.241 +#if SQLITE_ENABLE_FTS3_PARENTHESIS
  11.242 +  "ENABLE_FTS3_PARENTHESIS",
  11.243 +#endif
  11.244 +#if SQLITE_ENABLE_FTS3_TOKENIZER
  11.245 +  "ENABLE_FTS3_TOKENIZER",
  11.246 +#endif
  11.247 +#if SQLITE_ENABLE_FTS4
  11.248 +  "ENABLE_FTS4",
  11.249 +#endif
  11.250 +#if SQLITE_ENABLE_FTS5
  11.251 +  "ENABLE_FTS5",
  11.252 +#endif
  11.253 +#if SQLITE_ENABLE_GEOPOLY
  11.254 +  "ENABLE_GEOPOLY",
  11.255 +#endif
  11.256 +#if SQLITE_ENABLE_HIDDEN_COLUMNS
  11.257 +  "ENABLE_HIDDEN_COLUMNS",
  11.258 +#endif
  11.259 +#if SQLITE_ENABLE_ICU
  11.260 +  "ENABLE_ICU",
  11.261 +#endif
  11.262 +#if SQLITE_ENABLE_IOTRACE
  11.263 +  "ENABLE_IOTRACE",
  11.264 +#endif
  11.265 +#if SQLITE_ENABLE_JSON1
  11.266 +  "ENABLE_JSON1",
  11.267 +#endif
  11.268 +#if SQLITE_ENABLE_LOAD_EXTENSION
  11.269 +  "ENABLE_LOAD_EXTENSION",
  11.270 +#endif
  11.271 +#ifdef SQLITE_ENABLE_LOCKING_STYLE
  11.272 +  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
  11.273 +#endif
  11.274 +#if SQLITE_ENABLE_MEMORY_MANAGEMENT
  11.275 +  "ENABLE_MEMORY_MANAGEMENT",
  11.276 +#endif
  11.277 +#if SQLITE_ENABLE_MEMSYS3
  11.278 +  "ENABLE_MEMSYS3",
  11.279 +#endif
  11.280 +#if SQLITE_ENABLE_MEMSYS5
  11.281 +  "ENABLE_MEMSYS5",
  11.282 +#endif
  11.283 +#if SQLITE_ENABLE_MULTIPLEX
  11.284 +  "ENABLE_MULTIPLEX",
  11.285 +#endif
  11.286 +#if SQLITE_ENABLE_NORMALIZE
  11.287 +  "ENABLE_NORMALIZE",
  11.288 +#endif
  11.289 +#if SQLITE_ENABLE_NULL_TRIM
  11.290 +  "ENABLE_NULL_TRIM",
  11.291 +#endif
  11.292 +#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
  11.293 +  "ENABLE_OVERSIZE_CELL_CHECK",
  11.294 +#endif
  11.295 +#if SQLITE_ENABLE_PREUPDATE_HOOK
  11.296 +  "ENABLE_PREUPDATE_HOOK",
  11.297 +#endif
  11.298 +#if SQLITE_ENABLE_QPSG
  11.299 +  "ENABLE_QPSG",
  11.300 +#endif
  11.301 +#if SQLITE_ENABLE_RBU
  11.302 +  "ENABLE_RBU",
  11.303 +#endif
  11.304 +#if SQLITE_ENABLE_RTREE
  11.305 +  "ENABLE_RTREE",
  11.306 +#endif
  11.307 +#if SQLITE_ENABLE_SELECTTRACE
  11.308 +  "ENABLE_SELECTTRACE",
  11.309 +#endif
  11.310 +#if SQLITE_ENABLE_SESSION
  11.311 +  "ENABLE_SESSION",
  11.312 +#endif
  11.313 +#if SQLITE_ENABLE_SNAPSHOT
  11.314 +  "ENABLE_SNAPSHOT",
  11.315 +#endif
  11.316 +#if SQLITE_ENABLE_SORTER_REFERENCES
  11.317 +  "ENABLE_SORTER_REFERENCES",
  11.318 +#endif
  11.319 +#if SQLITE_ENABLE_SQLLOG
  11.320 +  "ENABLE_SQLLOG",
  11.321 +#endif
  11.322 +#if defined(SQLITE_ENABLE_STAT4)
  11.323 +  "ENABLE_STAT4",
  11.324 +#elif defined(SQLITE_ENABLE_STAT3)
  11.325 +  "ENABLE_STAT3",
  11.326 +#endif
  11.327 +#if SQLITE_ENABLE_STMTVTAB
  11.328 +  "ENABLE_STMTVTAB",
  11.329 +#endif
  11.330 +#if SQLITE_ENABLE_STMT_SCANSTATUS
  11.331 +  "ENABLE_STMT_SCANSTATUS",
  11.332 +#endif
  11.333 +#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
  11.334 +  "ENABLE_UNKNOWN_SQL_FUNCTION",
  11.335 +#endif
  11.336 +#if SQLITE_ENABLE_UNLOCK_NOTIFY
  11.337 +  "ENABLE_UNLOCK_NOTIFY",
  11.338 +#endif
  11.339 +#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  11.340 +  "ENABLE_UPDATE_DELETE_LIMIT",
  11.341 +#endif
  11.342 +#if SQLITE_ENABLE_URI_00_ERROR
  11.343 +  "ENABLE_URI_00_ERROR",
  11.344 +#endif
  11.345 +#if SQLITE_ENABLE_VFSTRACE
  11.346 +  "ENABLE_VFSTRACE",
  11.347 +#endif
  11.348 +#if SQLITE_ENABLE_WHERETRACE
  11.349 +  "ENABLE_WHERETRACE",
  11.350 +#endif
  11.351 +#if SQLITE_ENABLE_ZIPVFS
  11.352 +  "ENABLE_ZIPVFS",
  11.353 +#endif
  11.354 +#if SQLITE_EXPLAIN_ESTIMATED_ROWS
  11.355 +  "EXPLAIN_ESTIMATED_ROWS",
  11.356 +#endif
  11.357 +#if SQLITE_EXTRA_IFNULLROW
  11.358 +  "EXTRA_IFNULLROW",
  11.359 +#endif
  11.360 +#ifdef SQLITE_EXTRA_INIT
  11.361 +  "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
  11.362 +#endif
  11.363 +#ifdef SQLITE_EXTRA_SHUTDOWN
  11.364 +  "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
  11.365 +#endif
  11.366 +#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
  11.367 +  "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
  11.368 +#endif
  11.369 +#if SQLITE_FTS5_ENABLE_TEST_MI
  11.370 +  "FTS5_ENABLE_TEST_MI",
  11.371 +#endif
  11.372 +#if SQLITE_FTS5_NO_WITHOUT_ROWID
  11.373 +  "FTS5_NO_WITHOUT_ROWID",
  11.374 +#endif
  11.375 +#if SQLITE_HAS_CODEC
  11.376 +  "HAS_CODEC",
  11.377 +#endif
  11.378 +#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
  11.379 +  "HAVE_ISNAN",
  11.380 +#endif
  11.381 +#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  11.382 +  "HOMEGROWN_RECURSIVE_MUTEX",
  11.383 +#endif
  11.384 +#if SQLITE_IGNORE_AFP_LOCK_ERRORS
  11.385 +  "IGNORE_AFP_LOCK_ERRORS",
  11.386 +#endif
  11.387 +#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  11.388 +  "IGNORE_FLOCK_LOCK_ERRORS",
  11.389 +#endif
  11.390 +#if SQLITE_INLINE_MEMCPY
  11.391 +  "INLINE_MEMCPY",
  11.392 +#endif
  11.393 +#if SQLITE_INT64_TYPE
  11.394 +  "INT64_TYPE",
  11.395 +#endif
  11.396 +#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
  11.397 +  "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
  11.398 +#endif
  11.399 +#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
  11.400 +  "LIKE_DOESNT_MATCH_BLOBS",
  11.401 +#endif
  11.402 +#if SQLITE_LOCK_TRACE
  11.403 +  "LOCK_TRACE",
  11.404 +#endif
  11.405 +#if SQLITE_LOG_CACHE_SPILL
  11.406 +  "LOG_CACHE_SPILL",
  11.407 +#endif
  11.408 +#ifdef SQLITE_MALLOC_SOFT_LIMIT
  11.409 +  "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
  11.410 +#endif
  11.411 +#ifdef SQLITE_MAX_ATTACHED
  11.412 +  "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
  11.413 +#endif
  11.414 +#ifdef SQLITE_MAX_COLUMN
  11.415 +  "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
  11.416 +#endif
  11.417 +#ifdef SQLITE_MAX_COMPOUND_SELECT
  11.418 +  "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
  11.419 +#endif
  11.420 +#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
  11.421 +  "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
  11.422 +#endif
  11.423 +#ifdef SQLITE_MAX_EXPR_DEPTH
  11.424 +  "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
  11.425 +#endif
  11.426 +#ifdef SQLITE_MAX_FUNCTION_ARG
  11.427 +  "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
  11.428 +#endif
  11.429 +#ifdef SQLITE_MAX_LENGTH
  11.430 +  "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
  11.431 +#endif
  11.432 +#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
  11.433 +  "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
  11.434 +#endif
  11.435 +#ifdef SQLITE_MAX_MEMORY
  11.436 +  "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
  11.437 +#endif
  11.438 +#ifdef SQLITE_MAX_MMAP_SIZE
  11.439 +  "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
  11.440 +#endif
  11.441 +#ifdef SQLITE_MAX_MMAP_SIZE_
  11.442 +  "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
  11.443 +#endif
  11.444 +#ifdef SQLITE_MAX_PAGE_COUNT
  11.445 +  "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
  11.446 +#endif
  11.447 +#ifdef SQLITE_MAX_PAGE_SIZE
  11.448 +  "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
  11.449 +#endif
  11.450 +#ifdef SQLITE_MAX_SCHEMA_RETRY
  11.451 +  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
  11.452 +#endif
  11.453 +#ifdef SQLITE_MAX_SQL_LENGTH
  11.454 +  "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
  11.455 +#endif
  11.456 +#ifdef SQLITE_MAX_TRIGGER_DEPTH
  11.457 +  "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
  11.458 +#endif
  11.459 +#ifdef SQLITE_MAX_VARIABLE_NUMBER
  11.460 +  "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
  11.461 +#endif
  11.462 +#ifdef SQLITE_MAX_VDBE_OP
  11.463 +  "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
  11.464 +#endif
  11.465 +#ifdef SQLITE_MAX_WORKER_THREADS
  11.466 +  "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
  11.467 +#endif
  11.468 +#if SQLITE_MEMDEBUG
  11.469 +  "MEMDEBUG",
  11.470 +#endif
  11.471 +#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  11.472 +  "MIXED_ENDIAN_64BIT_FLOAT",
  11.473 +#endif
  11.474 +#if SQLITE_MMAP_READWRITE
  11.475 +  "MMAP_READWRITE",
  11.476 +#endif
  11.477 +#if SQLITE_MUTEX_NOOP
  11.478 +  "MUTEX_NOOP",
  11.479 +#endif
  11.480 +#if SQLITE_MUTEX_NREF
  11.481 +  "MUTEX_NREF",
  11.482 +#endif
  11.483 +#if SQLITE_MUTEX_OMIT
  11.484 +  "MUTEX_OMIT",
  11.485 +#endif
  11.486 +#if SQLITE_MUTEX_PTHREADS
  11.487 +  "MUTEX_PTHREADS",
  11.488 +#endif
  11.489 +#if SQLITE_MUTEX_W32
  11.490 +  "MUTEX_W32",
  11.491 +#endif
  11.492 +#if SQLITE_NEED_ERR_NAME
  11.493 +  "NEED_ERR_NAME",
  11.494 +#endif
  11.495 +#if SQLITE_NOINLINE
  11.496 +  "NOINLINE",
  11.497 +#endif
  11.498 +#if SQLITE_NO_SYNC
  11.499 +  "NO_SYNC",
  11.500 +#endif
  11.501 +#if SQLITE_OMIT_ALTERTABLE
  11.502 +  "OMIT_ALTERTABLE",
  11.503 +#endif
  11.504 +#if SQLITE_OMIT_ANALYZE
  11.505 +  "OMIT_ANALYZE",
  11.506 +#endif
  11.507 +#if SQLITE_OMIT_ATTACH
  11.508 +  "OMIT_ATTACH",
  11.509 +#endif
  11.510 +#if SQLITE_OMIT_AUTHORIZATION
  11.511 +  "OMIT_AUTHORIZATION",
  11.512 +#endif
  11.513 +#if SQLITE_OMIT_AUTOINCREMENT
  11.514 +  "OMIT_AUTOINCREMENT",
  11.515 +#endif
  11.516 +#if SQLITE_OMIT_AUTOINIT
  11.517 +  "OMIT_AUTOINIT",
  11.518 +#endif
  11.519 +#if SQLITE_OMIT_AUTOMATIC_INDEX
  11.520 +  "OMIT_AUTOMATIC_INDEX",
  11.521 +#endif
  11.522 +#if SQLITE_OMIT_AUTORESET
  11.523 +  "OMIT_AUTORESET",
  11.524 +#endif
  11.525 +#if SQLITE_OMIT_AUTOVACUUM
  11.526 +  "OMIT_AUTOVACUUM",
  11.527 +#endif
  11.528 +#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
  11.529 +  "OMIT_BETWEEN_OPTIMIZATION",
  11.530 +#endif
  11.531 +#if SQLITE_OMIT_BLOB_LITERAL
  11.532 +  "OMIT_BLOB_LITERAL",
  11.533 +#endif
  11.534 +#if SQLITE_OMIT_BTREECOUNT
  11.535 +  "OMIT_BTREECOUNT",
  11.536 +#endif
  11.537 +#if SQLITE_OMIT_CAST
  11.538 +  "OMIT_CAST",
  11.539 +#endif
  11.540 +#if SQLITE_OMIT_CHECK
  11.541 +  "OMIT_CHECK",
  11.542 +#endif
  11.543 +#if SQLITE_OMIT_COMPLETE
  11.544 +  "OMIT_COMPLETE",
  11.545 +#endif
  11.546 +#if SQLITE_OMIT_COMPOUND_SELECT
  11.547 +  "OMIT_COMPOUND_SELECT",
  11.548 +#endif
  11.549 +#if SQLITE_OMIT_CONFLICT_CLAUSE
  11.550 +  "OMIT_CONFLICT_CLAUSE",
  11.551 +#endif
  11.552 +#if SQLITE_OMIT_CTE
  11.553 +  "OMIT_CTE",
  11.554 +#endif
  11.555 +#if SQLITE_OMIT_DATETIME_FUNCS
  11.556 +  "OMIT_DATETIME_FUNCS",
  11.557 +#endif
  11.558 +#if SQLITE_OMIT_DECLTYPE
  11.559 +  "OMIT_DECLTYPE",
  11.560 +#endif
  11.561 +#if SQLITE_OMIT_DEPRECATED
  11.562 +  "OMIT_DEPRECATED",
  11.563 +#endif
  11.564 +#if SQLITE_OMIT_DISKIO
  11.565 +  "OMIT_DISKIO",
  11.566 +#endif
  11.567 +#if SQLITE_OMIT_EXPLAIN
  11.568 +  "OMIT_EXPLAIN",
  11.569 +#endif
  11.570 +#if SQLITE_OMIT_FLAG_PRAGMAS
  11.571 +  "OMIT_FLAG_PRAGMAS",
  11.572 +#endif
  11.573 +#if SQLITE_OMIT_FLOATING_POINT
  11.574 +  "OMIT_FLOATING_POINT",
  11.575 +#endif
  11.576 +#if SQLITE_OMIT_FOREIGN_KEY
  11.577 +  "OMIT_FOREIGN_KEY",
  11.578 +#endif
  11.579 +#if SQLITE_OMIT_GET_TABLE
  11.580 +  "OMIT_GET_TABLE",
  11.581 +#endif
  11.582 +#if SQLITE_OMIT_HEX_INTEGER
  11.583 +  "OMIT_HEX_INTEGER",
  11.584 +#endif
  11.585 +#if SQLITE_OMIT_INCRBLOB
  11.586 +  "OMIT_INCRBLOB",
  11.587 +#endif
  11.588 +#if SQLITE_OMIT_INTEGRITY_CHECK
  11.589 +  "OMIT_INTEGRITY_CHECK",
  11.590 +#endif
  11.591 +#if SQLITE_OMIT_LIKE_OPTIMIZATION
  11.592 +  "OMIT_LIKE_OPTIMIZATION",
  11.593 +#endif
  11.594 +#if SQLITE_OMIT_LOAD_EXTENSION
  11.595 +  "OMIT_LOAD_EXTENSION",
  11.596 +#endif
  11.597 +#if SQLITE_OMIT_LOCALTIME
  11.598 +  "OMIT_LOCALTIME",
  11.599 +#endif
  11.600 +#if SQLITE_OMIT_LOOKASIDE
  11.601 +  "OMIT_LOOKASIDE",
  11.602 +#endif
  11.603 +#if SQLITE_OMIT_MEMORYDB
  11.604 +  "OMIT_MEMORYDB",
  11.605 +#endif
  11.606 +#if SQLITE_OMIT_OR_OPTIMIZATION
  11.607 +  "OMIT_OR_OPTIMIZATION",
  11.608 +#endif
  11.609 +#if SQLITE_OMIT_PAGER_PRAGMAS
  11.610 +  "OMIT_PAGER_PRAGMAS",
  11.611 +#endif
  11.612 +#if SQLITE_OMIT_PARSER_TRACE
  11.613 +  "OMIT_PARSER_TRACE",
  11.614 +#endif
  11.615 +#if SQLITE_OMIT_POPEN
  11.616 +  "OMIT_POPEN",
  11.617 +#endif
  11.618 +#if SQLITE_OMIT_PRAGMA
  11.619 +  "OMIT_PRAGMA",
  11.620 +#endif
  11.621 +#if SQLITE_OMIT_PROGRESS_CALLBACK
  11.622 +  "OMIT_PROGRESS_CALLBACK",
  11.623 +#endif
  11.624 +#if SQLITE_OMIT_QUICKBALANCE
  11.625 +  "OMIT_QUICKBALANCE",
  11.626 +#endif
  11.627 +#if SQLITE_OMIT_REINDEX
  11.628 +  "OMIT_REINDEX",
  11.629 +#endif
  11.630 +#if SQLITE_OMIT_SCHEMA_PRAGMAS
  11.631 +  "OMIT_SCHEMA_PRAGMAS",
  11.632 +#endif
  11.633 +#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
  11.634 +  "OMIT_SCHEMA_VERSION_PRAGMAS",
  11.635 +#endif
  11.636 +#if SQLITE_OMIT_SHARED_CACHE
  11.637 +  "OMIT_SHARED_CACHE",
  11.638 +#endif
  11.639 +#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
  11.640 +  "OMIT_SHUTDOWN_DIRECTORIES",
  11.641 +#endif
  11.642 +#if SQLITE_OMIT_SUBQUERY
  11.643 +  "OMIT_SUBQUERY",
  11.644 +#endif
  11.645 +#if SQLITE_OMIT_TCL_VARIABLE
  11.646 +  "OMIT_TCL_VARIABLE",
  11.647 +#endif
  11.648 +#if SQLITE_OMIT_TEMPDB
  11.649 +  "OMIT_TEMPDB",
  11.650 +#endif
  11.651 +#if SQLITE_OMIT_TEST_CONTROL
  11.652 +  "OMIT_TEST_CONTROL",
  11.653 +#endif
  11.654 +#if SQLITE_OMIT_TRACE
  11.655 +  "OMIT_TRACE",
  11.656 +#endif
  11.657 +#if SQLITE_OMIT_TRIGGER
  11.658 +  "OMIT_TRIGGER",
  11.659 +#endif
  11.660 +#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
  11.661 +  "OMIT_TRUNCATE_OPTIMIZATION",
  11.662 +#endif
  11.663 +#if SQLITE_OMIT_UTF16
  11.664 +  "OMIT_UTF16",
  11.665 +#endif
  11.666 +#if SQLITE_OMIT_VACUUM
  11.667 +  "OMIT_VACUUM",
  11.668 +#endif
  11.669 +#if SQLITE_OMIT_VIEW
  11.670 +  "OMIT_VIEW",
  11.671 +#endif
  11.672 +#if SQLITE_OMIT_VIRTUALTABLE
  11.673 +  "OMIT_VIRTUALTABLE",
  11.674 +#endif
  11.675 +#if SQLITE_OMIT_WAL
  11.676 +  "OMIT_WAL",
  11.677 +#endif
  11.678 +#if SQLITE_OMIT_WSD
  11.679 +  "OMIT_WSD",
  11.680 +#endif
  11.681 +#if SQLITE_OMIT_XFER_OPT
  11.682 +  "OMIT_XFER_OPT",
  11.683 +#endif
  11.684 +#if SQLITE_PCACHE_SEPARATE_HEADER
  11.685 +  "PCACHE_SEPARATE_HEADER",
  11.686 +#endif
  11.687 +#if SQLITE_PERFORMANCE_TRACE
  11.688 +  "PERFORMANCE_TRACE",
  11.689 +#endif
  11.690 +#if SQLITE_POWERSAFE_OVERWRITE
  11.691 +  "POWERSAFE_OVERWRITE",
  11.692 +#endif
  11.693 +#if SQLITE_PREFER_PROXY_LOCKING
  11.694 +  "PREFER_PROXY_LOCKING",
  11.695 +#endif
  11.696 +#if SQLITE_PROXY_DEBUG
  11.697 +  "PROXY_DEBUG",
  11.698 +#endif
  11.699 +#if SQLITE_REVERSE_UNORDERED_SELECTS
  11.700 +  "REVERSE_UNORDERED_SELECTS",
  11.701 +#endif
  11.702 +#if SQLITE_RTREE_INT_ONLY
  11.703 +  "RTREE_INT_ONLY",
  11.704 +#endif
  11.705 +#if SQLITE_SECURE_DELETE
  11.706 +  "SECURE_DELETE",
  11.707 +#endif
  11.708 +#if SQLITE_SMALL_STACK
  11.709 +  "SMALL_STACK",
  11.710 +#endif
  11.711 +#ifdef SQLITE_SORTER_PMASZ
  11.712 +  "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
  11.713 +#endif
  11.714 +#if SQLITE_SOUNDEX
  11.715 +  "SOUNDEX",
  11.716 +#endif
  11.717 +#ifdef SQLITE_STAT4_SAMPLES
  11.718 +  "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
  11.719 +#endif
  11.720 +#ifdef SQLITE_STMTJRNL_SPILL
  11.721 +  "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
  11.722 +#endif
  11.723 +#if SQLITE_SUBSTR_COMPATIBILITY
  11.724 +  "SUBSTR_COMPATIBILITY",
  11.725 +#endif
  11.726 +#if SQLITE_SYSTEM_MALLOC
  11.727 +  "SYSTEM_MALLOC",
  11.728 +#endif
  11.729 +#if SQLITE_TCL
  11.730 +  "TCL",
  11.731 +#endif
  11.732 +#ifdef SQLITE_TEMP_STORE
  11.733 +  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
  11.734 +#endif
  11.735 +#if SQLITE_TEST
  11.736 +  "TEST",
  11.737 +#endif
  11.738 +#if defined(SQLITE_THREADSAFE)
  11.739 +  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
  11.740 +#elif defined(THREADSAFE)
  11.741 +  "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
  11.742 +#else
  11.743 +  "THREADSAFE=1",
  11.744 +#endif
  11.745 +#if SQLITE_UNLINK_AFTER_CLOSE
  11.746 +  "UNLINK_AFTER_CLOSE",
  11.747 +#endif
  11.748 +#if SQLITE_UNTESTABLE
  11.749 +  "UNTESTABLE",
  11.750 +#endif
  11.751 +#if SQLITE_USER_AUTHENTICATION
  11.752 +  "USER_AUTHENTICATION",
  11.753 +#endif
  11.754 +#if SQLITE_USE_ALLOCA
  11.755 +  "USE_ALLOCA",
  11.756 +#endif
  11.757 +#if SQLITE_USE_FCNTL_TRACE
  11.758 +  "USE_FCNTL_TRACE",
  11.759 +#endif
  11.760 +#if SQLITE_USE_URI
  11.761 +  "USE_URI",
  11.762 +#endif
  11.763 +#if SQLITE_VDBE_COVERAGE
  11.764 +  "VDBE_COVERAGE",
  11.765 +#endif
  11.766 +#if SQLITE_WIN32_MALLOC
  11.767 +  "WIN32_MALLOC",
  11.768 +#endif
  11.769 +#if SQLITE_ZERO_MALLOC
  11.770 +  "ZERO_MALLOC",
  11.771 +#endif
  11.772 +/* 
  11.773 +** END CODE GENERATED BY tool/mkctime.tcl 
  11.774 +*/
  11.775 +};
  11.776 +
  11.777 +SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
  11.778 +  *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
  11.779 +  return (const char**)sqlite3azCompileOpt;
  11.780 +}
  11.781 +
  11.782 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  11.783 +
  11.784 +/************** End of ctime.c ***********************************************/
  11.785  /************** Begin file sqliteInt.h ***************************************/
  11.786  /*
  11.787  ** 2001 September 15
  11.788 @@ -76,14 +846,6 @@
  11.789  #endif
  11.790  
  11.791  /*
  11.792 -** Make sure that rand_s() is available on Windows systems with MSVC 2005
  11.793 -** or higher.
  11.794 -*/
  11.795 -#if defined(_MSC_VER) && _MSC_VER>=1400
  11.796 -#  define _CRT_RAND_S
  11.797 -#endif
  11.798 -
  11.799 -/*
  11.800  ** Include the header file used to customize the compiler options for MSVC.
  11.801  ** This should be done first so that it can successfully prevent spurious
  11.802  ** compiler warnings due to subsequent content in this file and other files
  11.803 @@ -276,7 +1038,7 @@
  11.804  /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
  11.805  /************** Begin file sqlite3.h *****************************************/
  11.806  /*
  11.807 -** 2001 September 15
  11.808 +** 2001-09-15
  11.809  **
  11.810  ** The author disclaims copyright to this source code.  In place of
  11.811  ** a legal notice, here is a blessing:
  11.812 @@ -392,15 +1154,17 @@
  11.813  ** a string which identifies a particular check-in of SQLite
  11.814  ** within its configuration management system.  ^The SQLITE_SOURCE_ID
  11.815  ** string contains the date and time of the check-in (UTC) and a SHA1
  11.816 -** or SHA3-256 hash of the entire source tree.
  11.817 +** or SHA3-256 hash of the entire source tree.  If the source code has
  11.818 +** been edited in any way since it was last checked in, then the last
  11.819 +** four hexadecimal digits of the hash may be modified.
  11.820  **
  11.821  ** See also: [sqlite3_libversion()],
  11.822  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  11.823  ** [sqlite_version()] and [sqlite_source_id()].
  11.824  */
  11.825 -#define SQLITE_VERSION        "3.19.3"
  11.826 -#define SQLITE_VERSION_NUMBER 3019003
  11.827 -#define SQLITE_SOURCE_ID      "2017-06-08 14:26:16 0ee482a1e0eae22e08edc8978c9733a96603d4509645f348ebf55b579e89636b"
  11.828 +#define SQLITE_VERSION        "3.26.0"
  11.829 +#define SQLITE_VERSION_NUMBER 3026000
  11.830 +#define SQLITE_SOURCE_ID      "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9"
  11.831  
  11.832  /*
  11.833  ** CAPI3REF: Run-Time Library Version Numbers
  11.834 @@ -416,7 +1180,7 @@
  11.835  **
  11.836  ** <blockquote><pre>
  11.837  ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
  11.838 -** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
  11.839 +** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
  11.840  ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
  11.841  ** </pre></blockquote>)^
  11.842  **
  11.843 @@ -426,9 +1190,11 @@
  11.844  ** function is provided for use in DLLs since DLL users usually do not have
  11.845  ** direct access to string constants within the DLL.  ^The
  11.846  ** sqlite3_libversion_number() function returns an integer equal to
  11.847 -** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
  11.848 +** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns 
  11.849  ** a pointer to a string constant whose value is the same as the 
  11.850 -** [SQLITE_SOURCE_ID] C preprocessor macro.
  11.851 +** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
  11.852 +** using an edited copy of [the amalgamation], then the last four characters
  11.853 +** of the hash might be different from [SQLITE_SOURCE_ID].)^
  11.854  **
  11.855  ** See also: [sqlite_version()] and [sqlite_source_id()].
  11.856  */
  11.857 @@ -694,7 +1460,7 @@
  11.858  */
  11.859  #define SQLITE_OK           0   /* Successful result */
  11.860  /* beginning-of-error-codes */
  11.861 -#define SQLITE_ERROR        1   /* SQL error or missing database */
  11.862 +#define SQLITE_ERROR        1   /* Generic error */
  11.863  #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
  11.864  #define SQLITE_PERM         3   /* Access permission denied */
  11.865  #define SQLITE_ABORT        4   /* Callback routine requested an abort */
  11.866 @@ -709,7 +1475,7 @@
  11.867  #define SQLITE_FULL        13   /* Insertion failed because database is full */
  11.868  #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
  11.869  #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
  11.870 -#define SQLITE_EMPTY       16   /* Database is empty */
  11.871 +#define SQLITE_EMPTY       16   /* Internal use only */
  11.872  #define SQLITE_SCHEMA      17   /* The database schema changed */
  11.873  #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
  11.874  #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
  11.875 @@ -717,7 +1483,7 @@
  11.876  #define SQLITE_MISUSE      21   /* Library used incorrectly */
  11.877  #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
  11.878  #define SQLITE_AUTH        23   /* Authorization denied */
  11.879 -#define SQLITE_FORMAT      24   /* Auxiliary database format error */
  11.880 +#define SQLITE_FORMAT      24   /* Not used */
  11.881  #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
  11.882  #define SQLITE_NOTADB      26   /* File opened that is not a database file */
  11.883  #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
  11.884 @@ -743,6 +1509,9 @@
  11.885  ** the most recent error can be obtained using
  11.886  ** [sqlite3_extended_errcode()].
  11.887  */
  11.888 +#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
  11.889 +#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
  11.890 +#define SQLITE_ERROR_SNAPSHOT          (SQLITE_ERROR | (3<<8))
  11.891  #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
  11.892  #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
  11.893  #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
  11.894 @@ -771,18 +1540,26 @@
  11.895  #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
  11.896  #define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
  11.897  #define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
  11.898 +#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
  11.899 +#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
  11.900 +#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
  11.901  #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  11.902 +#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
  11.903  #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  11.904  #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
  11.905  #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  11.906  #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  11.907  #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  11.908  #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
  11.909 +#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
  11.910  #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  11.911 +#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
  11.912  #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  11.913  #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  11.914  #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
  11.915  #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
  11.916 +#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
  11.917 +#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
  11.918  #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
  11.919  #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
  11.920  #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
  11.921 @@ -857,6 +1634,11 @@
  11.922  ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
  11.923  ** read-only media and cannot be changed even by processes with
  11.924  ** elevated privileges.
  11.925 +**
  11.926 +** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
  11.927 +** filesystem supports doing multiple write operations atomically when those
  11.928 +** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
  11.929 +** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
  11.930  */
  11.931  #define SQLITE_IOCAP_ATOMIC                 0x00000001
  11.932  #define SQLITE_IOCAP_ATOMIC512              0x00000002
  11.933 @@ -872,6 +1654,7 @@
  11.934  #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
  11.935  #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
  11.936  #define SQLITE_IOCAP_IMMUTABLE              0x00002000
  11.937 +#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
  11.938  
  11.939  /*
  11.940  ** CAPI3REF: File Locking Levels
  11.941 @@ -1006,6 +1789,7 @@
  11.942  ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
  11.943  ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
  11.944  ** <li> [SQLITE_IOCAP_IMMUTABLE]
  11.945 +** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
  11.946  ** </ul>
  11.947  **
  11.948  ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  11.949 @@ -1143,7 +1927,8 @@
  11.950  ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
  11.951  ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
  11.952  ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
  11.953 -** write ahead log and shared memory files used for transaction control
  11.954 +** write ahead log ([WAL file]) and shared memory
  11.955 +** files used for transaction control
  11.956  ** are automatically deleted when the latest connection to the database
  11.957  ** closes.  Setting persistent WAL mode causes those files to persist after
  11.958  ** close.  Persisting the files is useful when other processes that do not
  11.959 @@ -1289,6 +2074,66 @@
  11.960  ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
  11.961  ** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
  11.962  ** this opcode.  
  11.963 +**
  11.964 +** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
  11.965 +** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
  11.966 +** the file descriptor is placed in "batch write mode", which
  11.967 +** means all subsequent write operations will be deferred and done
  11.968 +** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
  11.969 +** that do not support batch atomic writes will return SQLITE_NOTFOUND.
  11.970 +** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
  11.971 +** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
  11.972 +** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
  11.973 +** no VFS interface calls on the same [sqlite3_file] file descriptor
  11.974 +** except for calls to the xWrite method and the xFileControl method
  11.975 +** with [SQLITE_FCNTL_SIZE_HINT].
  11.976 +**
  11.977 +** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
  11.978 +** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
  11.979 +** operations since the previous successful call to 
  11.980 +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
  11.981 +** This file control returns [SQLITE_OK] if and only if the writes were
  11.982 +** all performed successfully and have been committed to persistent storage.
  11.983 +** ^Regardless of whether or not it is successful, this file control takes
  11.984 +** the file descriptor out of batch write mode so that all subsequent
  11.985 +** write operations are independent.
  11.986 +** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
  11.987 +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
  11.988 +**
  11.989 +** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
  11.990 +** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  11.991 +** operations since the previous successful call to 
  11.992 +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  11.993 +** ^This file control takes the file descriptor out of batch write mode
  11.994 +** so that all subsequent write operations are independent.
  11.995 +** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  11.996 +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
  11.997 +**
  11.998 +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
  11.999 +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
 11.1000 +** a file lock using the xLock or xShmLock methods of the VFS to wait
 11.1001 +** for up to M milliseconds before failing, where M is the single 
 11.1002 +** unsigned integer parameter.
 11.1003 +**
 11.1004 +** <li>[[SQLITE_FCNTL_DATA_VERSION]]
 11.1005 +** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
 11.1006 +** a database file.  The argument is a pointer to a 32-bit unsigned integer.
 11.1007 +** The "data version" for the pager is written into the pointer.  The
 11.1008 +** "data version" changes whenever any change occurs to the corresponding
 11.1009 +** database file, either through SQL statements on the same database
 11.1010 +** connection or through transactions committed by separate database
 11.1011 +** connections possibly in other processes. The [sqlite3_total_changes()]
 11.1012 +** interface can be used to find if any database on the connection has changed,
 11.1013 +** but that interface responds to changes on TEMP as well as MAIN and does
 11.1014 +** not provide a mechanism to detect changes to MAIN only.  Also, the
 11.1015 +** [sqlite3_total_changes()] interface responds to internal changes only and
 11.1016 +** omits changes made by other database connections.  The
 11.1017 +** [PRAGMA data_version] command provide a mechanism to detect changes to
 11.1018 +** a single attached database that occur due to other database connections,
 11.1019 +** but omits changes implemented by the database connection on which it is
 11.1020 +** called.  This file control is the only mechanism to detect changes that
 11.1021 +** happen either internally or externally and that are associated with
 11.1022 +** a particular attached database.
 11.1023  ** </ul>
 11.1024  */
 11.1025  #define SQLITE_FCNTL_LOCKSTATE               1
 11.1026 @@ -1320,6 +2165,11 @@
 11.1027  #define SQLITE_FCNTL_JOURNAL_POINTER        28
 11.1028  #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
 11.1029  #define SQLITE_FCNTL_PDB                    30
 11.1030 +#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
 11.1031 +#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
 11.1032 +#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
 11.1033 +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
 11.1034 +#define SQLITE_FCNTL_DATA_VERSION           35
 11.1035  
 11.1036  /* deprecated names */
 11.1037  #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
 11.1038 @@ -1357,12 +2207,18 @@
 11.1039  ** in the name of the object stands for "virtual file system".  See
 11.1040  ** the [VFS | VFS documentation] for further information.
 11.1041  **
 11.1042 -** The value of the iVersion field is initially 1 but may be larger in
 11.1043 -** future versions of SQLite.  Additional fields may be appended to this
 11.1044 -** object when the iVersion value is increased.  Note that the structure
 11.1045 -** of the sqlite3_vfs object changes in the transaction between
 11.1046 -** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
 11.1047 -** modified.
 11.1048 +** The VFS interface is sometimes extended by adding new methods onto
 11.1049 +** the end.  Each time such an extension occurs, the iVersion field
 11.1050 +** is incremented.  The iVersion value started out as 1 in
 11.1051 +** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
 11.1052 +** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
 11.1053 +** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
 11.1054 +** may be appended to the sqlite3_vfs object and the iVersion value
 11.1055 +** may increase again in future versions of SQLite.
 11.1056 +** Note that the structure
 11.1057 +** of the sqlite3_vfs object changes in the transition from
 11.1058 +** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
 11.1059 +** and yet the iVersion field was not modified.
 11.1060  **
 11.1061  ** The szOsFile field is the size of the subclassed [sqlite3_file]
 11.1062  ** structure used by this VFS.  mxPathname is the maximum length of
 11.1063 @@ -1890,6 +2746,16 @@
 11.1064  ** routines with a wrapper that simulations memory allocation failure or
 11.1065  ** tracks memory usage, for example. </dd>
 11.1066  **
 11.1067 +** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
 11.1068 +** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
 11.1069 +** type int, interpreted as a boolean, which if true provides a hint to
 11.1070 +** SQLite that it should avoid large memory allocations if possible.
 11.1071 +** SQLite will run faster if it is free to make large memory allocations,
 11.1072 +** but some application might prefer to run slower in exchange for
 11.1073 +** guarantees about memory fragmentation that are possible if large
 11.1074 +** allocations are avoided.  This hint is normally off.
 11.1075 +** </dd>
 11.1076 +**
 11.1077  ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
 11.1078  ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
 11.1079  ** interpreted as a boolean, which enables or disables the collection of
 11.1080 @@ -1907,25 +2773,7 @@
 11.1081  ** </dd>
 11.1082  **
 11.1083  ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
 11.1084 -** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
 11.1085 -** that SQLite can use for scratch memory.  ^(There are three arguments
 11.1086 -** to SQLITE_CONFIG_SCRATCH:  A pointer an 8-byte
 11.1087 -** aligned memory buffer from which the scratch allocations will be
 11.1088 -** drawn, the size of each scratch allocation (sz),
 11.1089 -** and the maximum number of scratch allocations (N).)^
 11.1090 -** The first argument must be a pointer to an 8-byte aligned buffer
 11.1091 -** of at least sz*N bytes of memory.
 11.1092 -** ^SQLite will not use more than one scratch buffers per thread.
 11.1093 -** ^SQLite will never request a scratch buffer that is more than 6
 11.1094 -** times the database page size.
 11.1095 -** ^If SQLite needs needs additional
 11.1096 -** scratch memory beyond what is provided by this configuration option, then 
 11.1097 -** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
 11.1098 -** ^When the application provides any amount of scratch memory using
 11.1099 -** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
 11.1100 -** [sqlite3_malloc|heap allocations].
 11.1101 -** This can help [Robson proof|prevent memory allocation failures] due to heap
 11.1102 -** fragmentation in low-memory embedded systems.
 11.1103 +** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
 11.1104  ** </dd>
 11.1105  **
 11.1106  ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
 11.1107 @@ -1961,8 +2809,7 @@
 11.1108  ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
 11.1109  ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 
 11.1110  ** that SQLite will use for all of its dynamic memory allocation needs
 11.1111 -** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
 11.1112 -** [SQLITE_CONFIG_PAGECACHE].
 11.1113 +** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
 11.1114  ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
 11.1115  ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
 11.1116  ** [SQLITE_ERROR] if invoked otherwise.
 11.1117 @@ -2148,6 +2995,22 @@
 11.1118  ** I/O required to support statement rollback.
 11.1119  ** The default value for this setting is controlled by the
 11.1120  ** [SQLITE_STMTJRNL_SPILL] compile-time option.
 11.1121 +**
 11.1122 +** [[SQLITE_CONFIG_SORTERREF_SIZE]]
 11.1123 +** <dt>SQLITE_CONFIG_SORTERREF_SIZE
 11.1124 +** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
 11.1125 +** of type (int) - the new value of the sorter-reference size threshold.
 11.1126 +** Usually, when SQLite uses an external sort to order records according
 11.1127 +** to an ORDER BY clause, all fields required by the caller are present in the
 11.1128 +** sorted records. However, if SQLite determines based on the declared type
 11.1129 +** of a table column that its values are likely to be very large - larger
 11.1130 +** than the configured sorter-reference size threshold - then a reference
 11.1131 +** is stored in each sorted record and the required column values loaded
 11.1132 +** from the database as records are returned in sorted order. The default
 11.1133 +** value for this option is to never use this optimization. Specifying a 
 11.1134 +** negative value for this option restores the default behaviour.
 11.1135 +** This option is only available if SQLite is compiled with the
 11.1136 +** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
 11.1137  ** </dl>
 11.1138  */
 11.1139  #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
 11.1140 @@ -2155,7 +3018,7 @@
 11.1141  #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
 11.1142  #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
 11.1143  #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
 11.1144 -#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
 11.1145 +#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
 11.1146  #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
 11.1147  #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
 11.1148  #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
 11.1149 @@ -2176,6 +3039,8 @@
 11.1150  #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
 11.1151  #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
 11.1152  #define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
 11.1153 +#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
 11.1154 +#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
 11.1155  
 11.1156  /*
 11.1157  ** CAPI3REF: Database Connection Configuration Options
 11.1158 @@ -2191,6 +3056,7 @@
 11.1159  ** is invoked.
 11.1160  **
 11.1161  ** <dl>
 11.1162 +** [[SQLITE_DBCONFIG_LOOKASIDE]]
 11.1163  ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
 11.1164  ** <dd> ^This option takes three additional arguments that determine the 
 11.1165  ** [lookaside memory allocator] configuration for the [database connection].
 11.1166 @@ -2213,6 +3079,7 @@
 11.1167  ** memory is in use leaves the configuration unchanged and returns 
 11.1168  ** [SQLITE_BUSY].)^</dd>
 11.1169  **
 11.1170 +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
 11.1171  ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
 11.1172  ** <dd> ^This option is used to enable or disable the enforcement of
 11.1173  ** [foreign key constraints].  There should be two additional arguments.
 11.1174 @@ -2223,6 +3090,7 @@
 11.1175  ** following this call.  The second parameter may be a NULL pointer, in
 11.1176  ** which case the FK enforcement setting is not reported back. </dd>
 11.1177  **
 11.1178 +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
 11.1179  ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
 11.1180  ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
 11.1181  ** There should be two additional arguments.
 11.1182 @@ -2233,6 +3101,7 @@
 11.1183  ** following this call.  The second parameter may be a NULL pointer, in
 11.1184  ** which case the trigger setting is not reported back. </dd>
 11.1185  **
 11.1186 +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
 11.1187  ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
 11.1188  ** <dd> ^This option is used to enable or disable the two-argument
 11.1189  ** version of the [fts3_tokenizer()] function which is part of the
 11.1190 @@ -2246,6 +3115,7 @@
 11.1191  ** following this call.  The second parameter may be a NULL pointer, in
 11.1192  ** which case the new setting is not reported back. </dd>
 11.1193  **
 11.1194 +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
 11.1195  ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
 11.1196  ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
 11.1197  ** interface independently of the [load_extension()] SQL function.
 11.1198 @@ -2263,7 +3133,7 @@
 11.1199  ** be a NULL pointer, in which case the new setting is not reported back.
 11.1200  ** </dd>
 11.1201  **
 11.1202 -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
 11.1203 +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
 11.1204  ** <dd> ^This option is used to change the name of the "main" database
 11.1205  ** schema.  ^The sole argument is a pointer to a constant UTF8 string
 11.1206  ** which will become the new schema name in place of "main".  ^SQLite
 11.1207 @@ -2272,18 +3142,80 @@
 11.1208  ** until after the database connection closes.
 11.1209  ** </dd>
 11.1210  **
 11.1211 +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
 11.1212  ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
 11.1213  ** <dd> Usually, when a database in wal mode is closed or detached from a 
 11.1214  ** database handle, SQLite checks if this will mean that there are now no 
 11.1215  ** connections at all to the database. If so, it performs a checkpoint 
 11.1216  ** operation before closing the connection. This option may be used to
 11.1217  ** override this behaviour. The first parameter passed to this operation
 11.1218 -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
 11.1219 -** default) to enable them. The second parameter is a pointer to an integer
 11.1220 +** is an integer - positive to disable checkpoints-on-close, or zero (the
 11.1221 +** default) to enable them, and negative to leave the setting unchanged.
 11.1222 +** The second parameter is a pointer to an integer
 11.1223  ** into which is written 0 or 1 to indicate whether checkpoints-on-close
 11.1224  ** have been disabled - 0 if they are not disabled, 1 if they are.
 11.1225  ** </dd>
 11.1226  **
 11.1227 +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
 11.1228 +** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
 11.1229 +** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
 11.1230 +** a single SQL query statement will always use the same algorithm regardless
 11.1231 +** of values of [bound parameters].)^ The QPSG disables some query optimizations
 11.1232 +** that look at the values of bound parameters, which can make some queries
 11.1233 +** slower.  But the QPSG has the advantage of more predictable behavior.  With
 11.1234 +** the QPSG active, SQLite will always use the same query plan in the field as
 11.1235 +** was used during testing in the lab.
 11.1236 +** The first argument to this setting is an integer which is 0 to disable 
 11.1237 +** the QPSG, positive to enable QPSG, or negative to leave the setting
 11.1238 +** unchanged. The second parameter is a pointer to an integer into which
 11.1239 +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
 11.1240 +** following this call.
 11.1241 +** </dd>
 11.1242 +**
 11.1243 +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
 11.1244 +** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
 11.1245 +** include output for any operations performed by trigger programs. This
 11.1246 +** option is used to set or clear (the default) a flag that governs this
 11.1247 +** behavior. The first parameter passed to this operation is an integer -
 11.1248 +** positive to enable output for trigger programs, or zero to disable it,
 11.1249 +** or negative to leave the setting unchanged.
 11.1250 +** The second parameter is a pointer to an integer into which is written 
 11.1251 +** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
 11.1252 +** it is not disabled, 1 if it is.  
 11.1253 +** </dd>
 11.1254 +**
 11.1255 +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
 11.1256 +** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
 11.1257 +** [VACUUM] in order to reset a database back to an empty database
 11.1258 +** with no schema and no content. The following process works even for
 11.1259 +** a badly corrupted database file:
 11.1260 +** <ol>
 11.1261 +** <li> If the database connection is newly opened, make sure it has read the
 11.1262 +**      database schema by preparing then discarding some query against the
 11.1263 +**      database, or calling sqlite3_table_column_metadata(), ignoring any
 11.1264 +**      errors.  This step is only necessary if the application desires to keep
 11.1265 +**      the database in WAL mode after the reset if it was in WAL mode before
 11.1266 +**      the reset.  
 11.1267 +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
 11.1268 +** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
 11.1269 +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
 11.1270 +** </ol>
 11.1271 +** Because resetting a database is destructive and irreversible, the
 11.1272 +** process requires the use of this obscure API and multiple steps to help
 11.1273 +** ensure that it does not happen by accident.
 11.1274 +**
 11.1275 +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
 11.1276 +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
 11.1277 +** "defensive" flag for a database connection.  When the defensive
 11.1278 +** flag is enabled, language features that allow ordinary SQL to 
 11.1279 +** deliberately corrupt the database file are disabled.  The disabled
 11.1280 +** features include but are not limited to the following:
 11.1281 +** <ul>
 11.1282 +** <li> The [PRAGMA writable_schema=ON] statement.
 11.1283 +** <li> Writes to the [sqlite_dbpage] virtual table.
 11.1284 +** <li> Direct writes to [shadow tables].
 11.1285 +** </ul>
 11.1286 +** </dd>
 11.1287  ** </dl>
 11.1288  */
 11.1289  #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
 11.1290 @@ -2293,7 +3225,11 @@
 11.1291  #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
 11.1292  #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
 11.1293  #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
 11.1294 -
 11.1295 +#define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
 11.1296 +#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
 11.1297 +#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
 11.1298 +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
 11.1299 +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
 11.1300  
 11.1301  /*
 11.1302  ** CAPI3REF: Enable Or Disable Extended Result Codes
 11.1303 @@ -2421,12 +3357,17 @@
 11.1304  ** program, the value returned reflects the number of rows modified by the 
 11.1305  ** previous INSERT, UPDATE or DELETE statement within the same trigger.
 11.1306  **
 11.1307 -** See also the [sqlite3_total_changes()] interface, the
 11.1308 -** [count_changes pragma], and the [changes() SQL function].
 11.1309 -**
 11.1310  ** If a separate thread makes changes on the same database connection
 11.1311  ** while [sqlite3_changes()] is running then the value returned
 11.1312  ** is unpredictable and not meaningful.
 11.1313 +**
 11.1314 +** See also:
 11.1315 +** <ul>
 11.1316 +** <li> the [sqlite3_total_changes()] interface
 11.1317 +** <li> the [count_changes pragma]
 11.1318 +** <li> the [changes() SQL function]
 11.1319 +** <li> the [data_version pragma]
 11.1320 +** </ul>
 11.1321  */
 11.1322  SQLITE_API int sqlite3_changes(sqlite3*);
 11.1323  
 11.1324 @@ -2444,13 +3385,26 @@
 11.1325  ** count, but those made as part of REPLACE constraint resolution are
 11.1326  ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 
 11.1327  ** are not counted.
 11.1328 +**
 11.1329 +** This the [sqlite3_total_changes(D)] interface only reports the number
 11.1330 +** of rows that changed due to SQL statement run against database
 11.1331 +** connection D.  Any changes by other database connections are ignored.
 11.1332 +** To detect changes against a database file from other database
 11.1333 +** connections use the [PRAGMA data_version] command or the
 11.1334 +** [SQLITE_FCNTL_DATA_VERSION] [file control].
 11.1335  ** 
 11.1336 -** See also the [sqlite3_changes()] interface, the
 11.1337 -** [count_changes pragma], and the [total_changes() SQL function].
 11.1338 -**
 11.1339  ** If a separate thread makes changes on the same database connection
 11.1340  ** while [sqlite3_total_changes()] is running then the value
 11.1341  ** returned is unpredictable and not meaningful.
 11.1342 +**
 11.1343 +** See also:
 11.1344 +** <ul>
 11.1345 +** <li> the [sqlite3_changes()] interface
 11.1346 +** <li> the [count_changes pragma]
 11.1347 +** <li> the [changes() SQL function]
 11.1348 +** <li> the [data_version pragma]
 11.1349 +** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
 11.1350 +** </ul>
 11.1351  */
 11.1352  SQLITE_API int sqlite3_total_changes(sqlite3*);
 11.1353  
 11.1354 @@ -2699,16 +3653,16 @@
 11.1355  **
 11.1356  ** These routines are work-alikes of the "printf()" family of functions
 11.1357  ** from the standard C library.
 11.1358 -** These routines understand most of the common K&R formatting options,
 11.1359 -** plus some additional non-standard formats, detailed below.
 11.1360 -** Note that some of the more obscure formatting options from recent
 11.1361 -** C-library standards are omitted from this implementation.
 11.1362 +** These routines understand most of the common formatting options from
 11.1363 +** the standard library printf() 
 11.1364 +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
 11.1365 +** See the [built-in printf()] documentation for details.
 11.1366  **
 11.1367  ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
 11.1368 -** results into memory obtained from [sqlite3_malloc()].
 11.1369 +** results into memory obtained from [sqlite3_malloc64()].
 11.1370  ** The strings returned by these two routines should be
 11.1371  ** released by [sqlite3_free()].  ^Both routines return a
 11.1372 -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
 11.1373 +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
 11.1374  ** memory to hold the resulting string.
 11.1375  **
 11.1376  ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
 11.1377 @@ -2732,71 +3686,7 @@
 11.1378  **
 11.1379  ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
 11.1380  **
 11.1381 -** These routines all implement some additional formatting
 11.1382 -** options that are useful for constructing SQL statements.
 11.1383 -** All of the usual printf() formatting options apply.  In addition, there
 11.1384 -** is are "%q", "%Q", "%w" and "%z" options.
 11.1385 -**
 11.1386 -** ^(The %q option works like %s in that it substitutes a nul-terminated
 11.1387 -** string from the argument list.  But %q also doubles every '\'' character.
 11.1388 -** %q is designed for use inside a string literal.)^  By doubling each '\''
 11.1389 -** character it escapes that character and allows it to be inserted into
 11.1390 -** the string.
 11.1391 -**
 11.1392 -** For example, assume the string variable zText contains text as follows:
 11.1393 -**
 11.1394 -** <blockquote><pre>
 11.1395 -**  char *zText = "It's a happy day!";
 11.1396 -** </pre></blockquote>
 11.1397 -**
 11.1398 -** One can use this text in an SQL statement as follows:
 11.1399 -**
 11.1400 -** <blockquote><pre>
 11.1401 -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
 11.1402 -**  sqlite3_exec(db, zSQL, 0, 0, 0);
 11.1403 -**  sqlite3_free(zSQL);
 11.1404 -** </pre></blockquote>
 11.1405 -**
 11.1406 -** Because the %q format string is used, the '\'' character in zText
 11.1407 -** is escaped and the SQL generated is as follows:
 11.1408 -**
 11.1409 -** <blockquote><pre>
 11.1410 -**  INSERT INTO table1 VALUES('It''s a happy day!')
 11.1411 -** </pre></blockquote>
 11.1412 -**
 11.1413 -** This is correct.  Had we used %s instead of %q, the generated SQL
 11.1414 -** would have looked like this:
 11.1415 -**
 11.1416 -** <blockquote><pre>
 11.1417 -**  INSERT INTO table1 VALUES('It's a happy day!');
 11.1418 -** </pre></blockquote>
 11.1419 -**
 11.1420 -** This second example is an SQL syntax error.  As a general rule you should
 11.1421 -** always use %q instead of %s when inserting text into a string literal.
 11.1422 -**
 11.1423 -** ^(The %Q option works like %q except it also adds single quotes around
 11.1424 -** the outside of the total string.  Additionally, if the parameter in the
 11.1425 -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
 11.1426 -** single quotes).)^  So, for example, one could say:
 11.1427 -**
 11.1428 -** <blockquote><pre>
 11.1429 -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
 11.1430 -**  sqlite3_exec(db, zSQL, 0, 0, 0);
 11.1431 -**  sqlite3_free(zSQL);
 11.1432 -** </pre></blockquote>
 11.1433 -**
 11.1434 -** The code above will render a correct SQL statement in the zSQL
 11.1435 -** variable even if the zText variable is a NULL pointer.
 11.1436 -**
 11.1437 -** ^(The "%w" formatting option is like "%q" except that it expects to
 11.1438 -** be contained within double-quotes instead of single quotes, and it
 11.1439 -** escapes the double-quote character instead of the single-quote
 11.1440 -** character.)^  The "%w" formatting option is intended for safely inserting
 11.1441 -** table and column names into a constructed SQL statement.
 11.1442 -**
 11.1443 -** ^(The "%z" formatting option works like "%s" but with the
 11.1444 -** addition that after the string has been read and copied into
 11.1445 -** the result, [sqlite3_free()] is called on the input string.)^
 11.1446 +** See also:  [built-in printf()], [printf() SQL function]
 11.1447  */
 11.1448  SQLITE_API char *sqlite3_mprintf(const char*,...);
 11.1449  SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
 11.1450 @@ -2956,7 +3846,8 @@
 11.1451  ** [database connection], supplied in the first argument.
 11.1452  ** ^The authorizer callback is invoked as SQL statements are being compiled
 11.1453  ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
 11.1454 -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
 11.1455 +** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
 11.1456 +** and [sqlite3_prepare16_v3()].  ^At various
 11.1457  ** points during the compilation process, as logic is being created
 11.1458  ** to perform various actions, the authorizer callback is invoked to
 11.1459  ** see if those actions are allowed.  ^The authorizer callback should
 11.1460 @@ -3153,8 +4044,8 @@
 11.1461  ** KEYWORDS: SQLITE_TRACE
 11.1462  **
 11.1463  ** These constants identify classes of events that can be monitored
 11.1464 -** using the [sqlite3_trace_v2()] tracing logic.  The third argument
 11.1465 -** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
 11.1466 +** using the [sqlite3_trace_v2()] tracing logic.  The M argument
 11.1467 +** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
 11.1468  ** the following constants.  ^The first argument to the trace callback
 11.1469  ** is one of the following constants.
 11.1470  **
 11.1471 @@ -3363,10 +4254,10 @@
 11.1472  ** ^If [URI filename] interpretation is enabled, and the filename argument
 11.1473  ** begins with "file:", then the filename is interpreted as a URI. ^URI
 11.1474  ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
 11.1475 -** set in the fourth argument to sqlite3_open_v2(), or if it has
 11.1476 +** set in the third argument to sqlite3_open_v2(), or if it has
 11.1477  ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
 11.1478  ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
 11.1479 -** As of SQLite version 3.7.7, URI filename interpretation is turned off
 11.1480 +** URI filename interpretation is turned off
 11.1481  ** by default, but future releases of SQLite might enable URI filename
 11.1482  ** interpretation by default.  See "[URI filenames]" for additional
 11.1483  ** information.
 11.1484 @@ -3569,13 +4460,24 @@
 11.1485  ** [database connection] D failed, then the sqlite3_errcode(D) interface
 11.1486  ** returns the numeric [result code] or [extended result code] for that
 11.1487  ** API call.
 11.1488 -** If the most recent API call was successful,
 11.1489 -** then the return value from sqlite3_errcode() is undefined.
 11.1490  ** ^The sqlite3_extended_errcode()
 11.1491  ** interface is the same except that it always returns the 
 11.1492  ** [extended result code] even when extended result codes are
 11.1493  ** disabled.
 11.1494  **
 11.1495 +** The values returned by sqlite3_errcode() and/or
 11.1496 +** sqlite3_extended_errcode() might change with each API call.
 11.1497 +** Except, there are some interfaces that are guaranteed to never
 11.1498 +** change the value of the error code.  The error-code preserving
 11.1499 +** interfaces are:
 11.1500 +**
 11.1501 +** <ul>
 11.1502 +** <li> sqlite3_errcode()
 11.1503 +** <li> sqlite3_extended_errcode()
 11.1504 +** <li> sqlite3_errmsg()
 11.1505 +** <li> sqlite3_errmsg16()
 11.1506 +** </ul>
 11.1507 +**
 11.1508  ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
 11.1509  ** text that describes the error, as either UTF-8 or UTF-16 respectively.
 11.1510  ** ^(Memory to hold the error message string is managed internally.
 11.1511 @@ -3745,6 +4647,39 @@
 11.1512  #define SQLITE_LIMIT_TRIGGER_DEPTH            10
 11.1513  #define SQLITE_LIMIT_WORKER_THREADS           11
 11.1514  
 11.1515 +/*
 11.1516 +** CAPI3REF: Prepare Flags
 11.1517 +**
 11.1518 +** These constants define various flags that can be passed into
 11.1519 +** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
 11.1520 +** [sqlite3_prepare16_v3()] interfaces.
 11.1521 +**
 11.1522 +** New flags may be added in future releases of SQLite.
 11.1523 +**
 11.1524 +** <dl>
 11.1525 +** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
 11.1526 +** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
 11.1527 +** that the prepared statement will be retained for a long time and
 11.1528 +** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
 11.1529 +** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
 11.1530 +** be used just once or at most a few times and then destroyed using
 11.1531 +** [sqlite3_finalize()] relatively soon. The current implementation acts
 11.1532 +** on this hint by avoiding the use of [lookaside memory] so as not to
 11.1533 +** deplete the limited store of lookaside memory. Future versions of
 11.1534 +** SQLite may act on this hint differently.
 11.1535 +**
 11.1536 +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
 11.1537 +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
 11.1538 +** representation of the SQL statement should be calculated and then
 11.1539 +** associated with the prepared statement, which can be obtained via
 11.1540 +** the [sqlite3_normalized_sql()] interface.)^  The semantics used to
 11.1541 +** normalize a SQL statement are unspecified and subject to change.
 11.1542 +** At a minimum, literal values will be replaced with suitable
 11.1543 +** placeholders.
 11.1544 +** </dl>
 11.1545 +*/
 11.1546 +#define SQLITE_PREPARE_PERSISTENT              0x01
 11.1547 +#define SQLITE_PREPARE_NORMALIZE               0x02
 11.1548  
 11.1549  /*
 11.1550  ** CAPI3REF: Compiling An SQL Statement
 11.1551 @@ -3752,17 +4687,29 @@
 11.1552  ** METHOD: sqlite3
 11.1553  ** CONSTRUCTOR: sqlite3_stmt
 11.1554  **
 11.1555 -** To execute an SQL query, it must first be compiled into a byte-code
 11.1556 -** program using one of these routines.
 11.1557 +** To execute an SQL statement, it must first be compiled into a byte-code
 11.1558 +** program using one of these routines.  Or, in other words, these routines
 11.1559 +** are constructors for the [prepared statement] object.
 11.1560 +**
 11.1561 +** The preferred routine to use is [sqlite3_prepare_v2()].  The
 11.1562 +** [sqlite3_prepare()] interface is legacy and should be avoided.
 11.1563 +** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
 11.1564 +** for special purposes.
 11.1565 +**
 11.1566 +** The use of the UTF-8 interfaces is preferred, as SQLite currently
 11.1567 +** does all parsing using UTF-8.  The UTF-16 interfaces are provided
 11.1568 +** as a convenience.  The UTF-16 interfaces work by converting the
 11.1569 +** input text into UTF-8, then invoking the corresponding UTF-8 interface.
 11.1570  **
 11.1571  ** The first argument, "db", is a [database connection] obtained from a
 11.1572  ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
 11.1573  ** [sqlite3_open16()].  The database connection must not have been closed.
 11.1574  **
 11.1575  ** The second argument, "zSql", is the statement to be compiled, encoded
 11.1576 -** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
 11.1577 -** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
 11.1578 -** use UTF-16.
 11.1579 +** as either UTF-8 or UTF-16.  The sqlite3_prepare(), sqlite3_prepare_v2(),
 11.1580 +** and sqlite3_prepare_v3()
 11.1581 +** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
 11.1582 +** and sqlite3_prepare16_v3() use UTF-16.
 11.1583  **
 11.1584  ** ^If the nByte argument is negative, then zSql is read up to the
 11.1585  ** first zero terminator. ^If nByte is positive, then it is the
 11.1586 @@ -3789,10 +4736,11 @@
 11.1587  ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
 11.1588  ** otherwise an [error code] is returned.
 11.1589  **
 11.1590 -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
 11.1591 -** recommended for all new programs. The two older interfaces are retained
 11.1592 -** for backwards compatibility, but their use is discouraged.
 11.1593 -** ^In the "v2" interfaces, the prepared statement
 11.1594 +** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
 11.1595 +** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
 11.1596 +** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
 11.1597 +** are retained for backwards compatibility, but their use is discouraged.
 11.1598 +** ^In the "vX" interfaces, the prepared statement
 11.1599  ** that is returned (the [sqlite3_stmt] object) contains a copy of the
 11.1600  ** original SQL text. This causes the [sqlite3_step()] interface to
 11.1601  ** behave differently in three ways:
 11.1602 @@ -3826,6 +4774,12 @@
 11.1603  ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
 11.1604  ** </li>
 11.1605  ** </ol>
 11.1606 +**
 11.1607 +** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
 11.1608 +** the extra prepFlags parameter, which is a bit array consisting of zero or
 11.1609 +** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
 11.1610 +** sqlite3_prepare_v2() interface works exactly the same as
 11.1611 +** sqlite3_prepare_v3() with a zero prepFlags parameter.
 11.1612  */
 11.1613  SQLITE_API int sqlite3_prepare(
 11.1614    sqlite3 *db,            /* Database handle */
 11.1615 @@ -3841,6 +4795,14 @@
 11.1616    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
 11.1617    const char **pzTail     /* OUT: Pointer to unused portion of zSql */
 11.1618  );
 11.1619 +SQLITE_API int sqlite3_prepare_v3(
 11.1620 +  sqlite3 *db,            /* Database handle */
 11.1621 +  const char *zSql,       /* SQL statement, UTF-8 encoded */
 11.1622 +  int nByte,              /* Maximum length of zSql in bytes. */
 11.1623 +  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
 11.1624 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
 11.1625 +  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
 11.1626 +);
 11.1627  SQLITE_API int sqlite3_prepare16(
 11.1628    sqlite3 *db,            /* Database handle */
 11.1629    const void *zSql,       /* SQL statement, UTF-16 encoded */
 11.1630 @@ -3855,6 +4817,14 @@
 11.1631    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
 11.1632    const void **pzTail     /* OUT: Pointer to unused portion of zSql */
 11.1633  );
 11.1634 +SQLITE_API int sqlite3_prepare16_v3(
 11.1635 +  sqlite3 *db,            /* Database handle */
 11.1636 +  const void *zSql,       /* SQL statement, UTF-16 encoded */
 11.1637 +  int nByte,              /* Maximum length of zSql in bytes. */
 11.1638 +  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
 11.1639 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
 11.1640 +  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
 11.1641 +);
 11.1642  
 11.1643  /*
 11.1644  ** CAPI3REF: Retrieving Statement SQL
 11.1645 @@ -3862,10 +4832,16 @@
 11.1646  **
 11.1647  ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
 11.1648  ** SQL text used to create [prepared statement] P if P was
 11.1649 -** created by either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
 11.1650 +** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
 11.1651 +** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
 11.1652  ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
 11.1653  ** string containing the SQL text of prepared statement P with
 11.1654  ** [bound parameters] expanded.
 11.1655 +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
 11.1656 +** string containing the normalized SQL text of prepared statement P.  The
 11.1657 +** semantics used to normalize a SQL statement are unspecified and subject
 11.1658 +** to change.  At a minimum, literal values will be replaced with suitable
 11.1659 +** placeholders.
 11.1660  **
 11.1661  ** ^(For example, if a prepared statement is created using the SQL
 11.1662  ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
 11.1663 @@ -3881,14 +4857,16 @@
 11.1664  ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
 11.1665  ** option causes sqlite3_expanded_sql() to always return NULL.
 11.1666  **
 11.1667 -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
 11.1668 -** automatically freed when the prepared statement is finalized.
 11.1669 +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
 11.1670 +** are managed by SQLite and are automatically freed when the prepared
 11.1671 +** statement is finalized.
 11.1672  ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
 11.1673  ** is obtained from [sqlite3_malloc()] and must be free by the application
 11.1674  ** by passing it to [sqlite3_free()].
 11.1675  */
 11.1676  SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
 11.1677  SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
 11.1678 +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
 11.1679  
 11.1680  /*
 11.1681  ** CAPI3REF: Determine If An SQL Statement Writes The Database
 11.1682 @@ -3981,8 +4959,9 @@
 11.1683  ** implementation of [application-defined SQL functions] are protected.
 11.1684  ** ^The sqlite3_value object returned by
 11.1685  ** [sqlite3_column_value()] is unprotected.
 11.1686 -** Unprotected sqlite3_value objects may only be used with
 11.1687 -** [sqlite3_result_value()] and [sqlite3_bind_value()].
 11.1688 +** Unprotected sqlite3_value objects may only be used as arguments
 11.1689 +** to [sqlite3_result_value()], [sqlite3_bind_value()], and
 11.1690 +** [sqlite3_value_dup()].
 11.1691  ** The [sqlite3_value_blob | sqlite3_value_type()] family of
 11.1692  ** interfaces require protected sqlite3_value objects.
 11.1693  */
 11.1694 @@ -4088,6 +5067,15 @@
 11.1695  ** [sqlite3_blob_open | incremental BLOB I/O] routines.
 11.1696  ** ^A negative value for the zeroblob results in a zero-length BLOB.
 11.1697  **
 11.1698 +** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
 11.1699 +** [prepared statement] S to have an SQL value of NULL, but to also be
 11.1700 +** associated with the pointer P of type T.  ^D is either a NULL pointer or
 11.1701 +** a pointer to a destructor function for P. ^SQLite will invoke the
 11.1702 +** destructor D with a single argument of P when it is finished using
 11.1703 +** P.  The T parameter should be a static string, preferably a string
 11.1704 +** literal. The sqlite3_bind_pointer() routine is part of the
 11.1705 +** [pointer passing interface] added for SQLite 3.20.0.
 11.1706 +**
 11.1707  ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
 11.1708  ** for the [prepared statement] or with a prepared statement for which
 11.1709  ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
 11.1710 @@ -4121,6 +5109,7 @@
 11.1711  SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
 11.1712                           void(*)(void*), unsigned char encoding);
 11.1713  SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
 11.1714 +SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
 11.1715  SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
 11.1716  SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
 11.1717  
 11.1718 @@ -4164,8 +5153,8 @@
 11.1719  ** ^If the value N is out of range or if the N-th parameter is
 11.1720  ** nameless, then NULL is returned.  ^The returned string is
 11.1721  ** always in UTF-8 encoding even if the named parameter was
 11.1722 -** originally specified as UTF-16 in [sqlite3_prepare16()] or
 11.1723 -** [sqlite3_prepare16_v2()].
 11.1724 +** originally specified as UTF-16 in [sqlite3_prepare16()],
 11.1725 +** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
 11.1726  **
 11.1727  ** See also: [sqlite3_bind_blob|sqlite3_bind()],
 11.1728  ** [sqlite3_bind_parameter_count()], and
 11.1729 @@ -4182,7 +5171,8 @@
 11.1730  ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
 11.1731  ** is returned if no matching parameter is found.  ^The parameter
 11.1732  ** name must be given in UTF-8 even if the original statement
 11.1733 -** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
 11.1734 +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
 11.1735 +** [sqlite3_prepare16_v3()].
 11.1736  **
 11.1737  ** See also: [sqlite3_bind_blob|sqlite3_bind()],
 11.1738  ** [sqlite3_bind_parameter_count()], and
 11.1739 @@ -4336,16 +5326,18 @@
 11.1740  ** CAPI3REF: Evaluate An SQL Statement
 11.1741  ** METHOD: sqlite3_stmt
 11.1742  **
 11.1743 -** After a [prepared statement] has been prepared using either
 11.1744 -** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
 11.1745 +** After a [prepared statement] has been prepared using any of
 11.1746 +** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
 11.1747 +** or [sqlite3_prepare16_v3()] or one of the legacy
 11.1748  ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
 11.1749  ** must be called one or more times to evaluate the statement.
 11.1750  **
 11.1751  ** The details of the behavior of the sqlite3_step() interface depend
 11.1752 -** on whether the statement was prepared using the newer "v2" interface
 11.1753 -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
 11.1754 -** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
 11.1755 -** new "v2" interface is recommended for new applications but the legacy
 11.1756 +** on whether the statement was prepared using the newer "vX" interfaces
 11.1757 +** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
 11.1758 +** [sqlite3_prepare16_v2()] or the older legacy
 11.1759 +** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
 11.1760 +** new "vX" interface is recommended for new applications but the legacy
 11.1761  ** interface will continue to be supported.
 11.1762  **
 11.1763  ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
 11.1764 @@ -4406,10 +5398,11 @@
 11.1765  ** specific [error codes] that better describes the error.
 11.1766  ** We admit that this is a goofy design.  The problem has been fixed
 11.1767  ** with the "v2" interface.  If you prepare all of your SQL statements
 11.1768 -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
 11.1769 +** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
 11.1770 +** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
 11.1771  ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
 11.1772  ** then the more specific [error codes] are returned directly
 11.1773 -** by sqlite3_step().  The use of the "v2" interface is recommended.
 11.1774 +** by sqlite3_step().  The use of the "vX" interfaces is recommended.
 11.1775  */
 11.1776  SQLITE_API int sqlite3_step(sqlite3_stmt*);
 11.1777  
 11.1778 @@ -4471,6 +5464,28 @@
 11.1779  ** KEYWORDS: {column access functions}
 11.1780  ** METHOD: sqlite3_stmt
 11.1781  **
 11.1782 +** <b>Summary:</b>
 11.1783 +** <blockquote><table border=0 cellpadding=0 cellspacing=0>
 11.1784 +** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
 11.1785 +** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
 11.1786 +** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
 11.1787 +** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
 11.1788 +** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
 11.1789 +** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
 11.1790 +** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an 
 11.1791 +** [sqlite3_value|unprotected sqlite3_value] object.
 11.1792 +** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
 11.1793 +** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
 11.1794 +** or a UTF-8 TEXT result in bytes
 11.1795 +** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
 11.1796 +** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
 11.1797 +** TEXT in bytes
 11.1798 +** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
 11.1799 +** datatype of the result
 11.1800 +** </table></blockquote>
 11.1801 +**
 11.1802 +** <b>Details:</b>
 11.1803 +**
 11.1804  ** ^These routines return information about a single column of the current
 11.1805  ** result row of a query.  ^In every case the first argument is a pointer
 11.1806  ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
 11.1807 @@ -4492,16 +5507,29 @@
 11.1808  ** are called from a different thread while any of these routines
 11.1809  ** are pending, then the results are undefined.
 11.1810  **
 11.1811 +** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
 11.1812 +** each return the value of a result column in a specific data format.  If
 11.1813 +** the result column is not initially in the requested format (for example,
 11.1814 +** if the query returns an integer but the sqlite3_column_text() interface
 11.1815 +** is used to extract the value) then an automatic type conversion is performed.
 11.1816 +**
 11.1817  ** ^The sqlite3_column_type() routine returns the
 11.1818  ** [SQLITE_INTEGER | datatype code] for the initial data type
 11.1819  ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
 11.1820 -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
 11.1821 -** returned by sqlite3_column_type() is only meaningful if no type
 11.1822 -** conversions have occurred as described below.  After a type conversion,
 11.1823 -** the value returned by sqlite3_column_type() is undefined.  Future
 11.1824 +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
 11.1825 +** The return value of sqlite3_column_type() can be used to decide which
 11.1826 +** of the first six interface should be used to extract the column value.
 11.1827 +** The value returned by sqlite3_column_type() is only meaningful if no
 11.1828 +** automatic type conversions have occurred for the value in question.  
 11.1829 +** After a type conversion, the result of calling sqlite3_column_type()
 11.1830 +** is undefined, though harmless.  Future
 11.1831  ** versions of SQLite may change the behavior of sqlite3_column_type()
 11.1832  ** following a type conversion.
 11.1833  **
 11.1834 +** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
 11.1835 +** or sqlite3_column_bytes16() interfaces can be used to determine the size
 11.1836 +** of that BLOB or string.
 11.1837 +**
 11.1838  ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
 11.1839  ** routine returns the number of bytes in that BLOB or string.
 11.1840  ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
 11.1841 @@ -4538,9 +5566,13 @@
 11.1842  ** [sqlite3_column_value()] is used in any other way, including calls
 11.1843  ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
 11.1844  ** or [sqlite3_value_bytes()], the behavior is not threadsafe.
 11.1845 -**
 11.1846 -** These routines attempt to convert the value where appropriate.  ^For
 11.1847 -** example, if the internal representation is FLOAT and a text result
 11.1848 +** Hence, the sqlite3_column_value() interface
 11.1849 +** is normally only useful within the implementation of 
 11.1850 +** [application-defined SQL functions] or [virtual tables], not within
 11.1851 +** top-level application code.
 11.1852 +**
 11.1853 +** The these routines may attempt to convert the datatype of the result.
 11.1854 +** ^For example, if the internal representation is FLOAT and a text result
 11.1855  ** is requested, [sqlite3_snprintf()] is used internally to perform the
 11.1856  ** conversion automatically.  ^(The following table details the conversions
 11.1857  ** that are applied:
 11.1858 @@ -4612,26 +5644,40 @@
 11.1859  ** ^The pointers returned are valid until a type conversion occurs as
 11.1860  ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
 11.1861  ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
 11.1862 -** and BLOBs is freed automatically.  Do <em>not</em> pass the pointers returned
 11.1863 +** and BLOBs is freed automatically.  Do not pass the pointers returned
 11.1864  ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
 11.1865  ** [sqlite3_free()].
 11.1866  **
 11.1867 -** ^(If a memory allocation error occurs during the evaluation of any
 11.1868 -** of these routines, a default value is returned.  The default value
 11.1869 -** is either the integer 0, the floating point number 0.0, or a NULL
 11.1870 -** pointer.  Subsequent calls to [sqlite3_errcode()] will return
 11.1871 -** [SQLITE_NOMEM].)^
 11.1872 +** As long as the input parameters are correct, these routines will only
 11.1873 +** fail if an out-of-memory error occurs during a format conversion.
 11.1874 +** Only the following subset of interfaces are subject to out-of-memory
 11.1875 +** errors:
 11.1876 +**
 11.1877 +** <ul>
 11.1878 +** <li> sqlite3_column_blob()
 11.1879 +** <li> sqlite3_column_text()
 11.1880 +** <li> sqlite3_column_text16()
 11.1881 +** <li> sqlite3_column_bytes()
 11.1882 +** <li> sqlite3_column_bytes16()
 11.1883 +** </ul>
 11.1884 +**
 11.1885 +** If an out-of-memory error occurs, then the return value from these
 11.1886 +** routines is the same as if the column had contained an SQL NULL value.
 11.1887 +** Valid SQL NULL returns can be distinguished from out-of-memory errors
 11.1888 +** by invoking the [sqlite3_errcode()] immediately after the suspect
 11.1889 +** return value is obtained and before any
 11.1890 +** other SQLite interface is called on the same [database connection].
 11.1891  */
 11.1892  SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
 11.1893 -SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
 11.1894 -SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
 11.1895  SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
 11.1896  SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
 11.1897  SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
 11.1898  SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
 11.1899  SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
 11.1900 +SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
 11.1901 +SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
 11.1902 +SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
 11.1903  SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
 11.1904 -SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
 11.1905  
 11.1906  /*
 11.1907  ** CAPI3REF: Destroy A Prepared Statement Object
 11.1908 @@ -4697,11 +5743,13 @@
 11.1909  **
 11.1910  ** ^These functions (collectively known as "function creation routines")
 11.1911  ** are used to add SQL functions or aggregates or to redefine the behavior
 11.1912 -** of existing SQL functions or aggregates.  The only differences between
 11.1913 -** these routines are the text encoding expected for
 11.1914 -** the second parameter (the name of the function being created)
 11.1915 -** and the presence or absence of a destructor callback for
 11.1916 -** the application data pointer.
 11.1917 +** of existing SQL functions or aggregates. The only differences between
 11.1918 +** the three "sqlite3_create_function*" routines are the text encoding 
 11.1919 +** expected for the second parameter (the name of the function being 
 11.1920 +** created) and the presence or absence of a destructor callback for
 11.1921 +** the application data pointer. Function sqlite3_create_window_function()
 11.1922 +** is similar, but allows the user to supply the extra callback functions
 11.1923 +** needed by [aggregate window functions].
 11.1924  **
 11.1925  ** ^The first parameter is the [database connection] to which the SQL
 11.1926  ** function is to be added.  ^If an application uses more than one database
 11.1927 @@ -4747,7 +5795,8 @@
 11.1928  ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
 11.1929  ** function can gain access to this pointer using [sqlite3_user_data()].)^
 11.1930  **
 11.1931 -** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
 11.1932 +** ^The sixth, seventh and eighth parameters passed to the three
 11.1933 +** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
 11.1934  ** pointers to C-language functions that implement the SQL function or
 11.1935  ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
 11.1936  ** callback only; NULL pointers must be passed as the xStep and xFinal
 11.1937 @@ -4756,15 +5805,24 @@
 11.1938  ** SQL function or aggregate, pass NULL pointers for all three function
 11.1939  ** callbacks.
 11.1940  **
 11.1941 -** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
 11.1942 -** then it is destructor for the application data pointer. 
 11.1943 -** The destructor is invoked when the function is deleted, either by being
 11.1944 -** overloaded or when the database connection closes.)^
 11.1945 -** ^The destructor is also invoked if the call to
 11.1946 -** sqlite3_create_function_v2() fails.
 11.1947 -** ^When the destructor callback of the tenth parameter is invoked, it
 11.1948 -** is passed a single argument which is a copy of the application data 
 11.1949 -** pointer which was the fifth parameter to sqlite3_create_function_v2().
 11.1950 +** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue 
 11.1951 +** and xInverse) passed to sqlite3_create_window_function are pointers to
 11.1952 +** C-language callbacks that implement the new function. xStep and xFinal
 11.1953 +** must both be non-NULL. xValue and xInverse may either both be NULL, in
 11.1954 +** which case a regular aggregate function is created, or must both be 
 11.1955 +** non-NULL, in which case the new function may be used as either an aggregate
 11.1956 +** or aggregate window function. More details regarding the implementation
 11.1957 +** of aggregate window functions are 
 11.1958 +** [user-defined window functions|available here].
 11.1959 +**
 11.1960 +** ^(If the final parameter to sqlite3_create_function_v2() or
 11.1961 +** sqlite3_create_window_function() is not NULL, then it is destructor for
 11.1962 +** the application data pointer. The destructor is invoked when the function 
 11.1963 +** is deleted, either by being overloaded or when the database connection 
 11.1964 +** closes.)^ ^The destructor is also invoked if the call to 
 11.1965 +** sqlite3_create_function_v2() fails.  ^When the destructor callback is
 11.1966 +** invoked, it is passed a single argument which is a copy of the application
 11.1967 +** data pointer which was the fifth parameter to sqlite3_create_function_v2().
 11.1968  **
 11.1969  ** ^It is permitted to register multiple implementations of the same
 11.1970  ** functions with the same name but with either differing numbers of
 11.1971 @@ -4817,6 +5875,18 @@
 11.1972    void (*xFinal)(sqlite3_context*),
 11.1973    void(*xDestroy)(void*)
 11.1974  );
 11.1975 +SQLITE_API int sqlite3_create_window_function(
 11.1976 +  sqlite3 *db,
 11.1977 +  const char *zFunctionName,
 11.1978 +  int nArg,
 11.1979 +  int eTextRep,
 11.1980 +  void *pApp,
 11.1981 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 11.1982 +  void (*xFinal)(sqlite3_context*),
 11.1983 +  void (*xValue)(sqlite3_context*),
 11.1984 +  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
 11.1985 +  void(*xDestroy)(void*)
 11.1986 +);
 11.1987  
 11.1988  /*
 11.1989  ** CAPI3REF: Text Encodings
 11.1990 @@ -4865,21 +5935,43 @@
 11.1991  ** CAPI3REF: Obtaining SQL Values
 11.1992  ** METHOD: sqlite3_value
 11.1993  **
 11.1994 -** The C-language implementation of SQL functions and aggregates uses
 11.1995 -** this set of interface routines to access the parameter values on
 11.1996 -** the function or aggregate.  
 11.1997 -**
 11.1998 -** The xFunc (for scalar functions) or xStep (for aggregates) parameters
 11.1999 -** to [sqlite3_create_function()] and [sqlite3_create_function16()]
 11.2000 -** define callbacks that implement the SQL functions and aggregates.
 11.2001 -** The 3rd parameter to these callbacks is an array of pointers to
 11.2002 -** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
 11.2003 -** each parameter to the SQL function.  These routines are used to
 11.2004 -** extract values from the [sqlite3_value] objects.
 11.2005 +** <b>Summary:</b>
 11.2006 +** <blockquote><table border=0 cellpadding=0 cellspacing=0>
 11.2007 +** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
 11.2008 +** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
 11.2009 +** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
 11.2010 +** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
 11.2011 +** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
 11.2012 +** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
 11.2013 +** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
 11.2014 +** the native byteorder
 11.2015 +** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
 11.2016 +** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
 11.2017 +** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
 11.2018 +** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
 11.2019 +** or a UTF-8 TEXT in bytes
 11.2020 +** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
 11.2021 +** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
 11.2022 +** TEXT in bytes
 11.2023 +** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
 11.2024 +** datatype of the value
 11.2025 +** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
 11.2026 +** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
 11.2027 +** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
 11.2028 +** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
 11.2029 +** against a virtual table.
 11.2030 +** </table></blockquote>
 11.2031 +**
 11.2032 +** <b>Details:</b>
 11.2033 +**
 11.2034 +** These routines extract type, size, and content information from
 11.2035 +** [protected sqlite3_value] objects.  Protected sqlite3_value objects
 11.2036 +** are used to pass parameter information into implementation of
 11.2037 +** [application-defined SQL functions] and [virtual tables].
 11.2038  **
 11.2039  ** These routines work only with [protected sqlite3_value] objects.
 11.2040  ** Any attempt to use these routines on an [unprotected sqlite3_value]
 11.2041 -** object results in undefined behavior.
 11.2042 +** is not threadsafe.
 11.2043  **
 11.2044  ** ^These routines work just like the corresponding [column access functions]
 11.2045  ** except that these routines take a single [protected sqlite3_value] object
 11.2046 @@ -4890,6 +5982,24 @@
 11.2047  ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
 11.2048  ** extract UTF-16 strings as big-endian and little-endian respectively.
 11.2049  **
 11.2050 +** ^If [sqlite3_value] object V was initialized 
 11.2051 +** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
 11.2052 +** and if X and Y are strings that compare equal according to strcmp(X,Y),
 11.2053 +** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
 11.2054 +** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() 
 11.2055 +** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
 11.2056 +**
 11.2057 +** ^(The sqlite3_value_type(V) interface returns the
 11.2058 +** [SQLITE_INTEGER | datatype code] for the initial datatype of the
 11.2059 +** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
 11.2060 +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
 11.2061 +** Other interfaces might change the datatype for an sqlite3_value object.
 11.2062 +** For example, if the datatype is initially SQLITE_INTEGER and
 11.2063 +** sqlite3_value_text(V) is called to extract a text value for that
 11.2064 +** integer, then subsequent calls to sqlite3_value_type(V) might return
 11.2065 +** SQLITE_TEXT.  Whether or not a persistent internal datatype conversion
 11.2066 +** occurs is undefined and may change from one release of SQLite to the next.
 11.2067 +**
 11.2068  ** ^(The sqlite3_value_numeric_type() interface attempts to apply
 11.2069  ** numeric affinity to the value.  This means that an attempt is
 11.2070  ** made to convert the value to an integer or floating point.  If
 11.2071 @@ -4898,6 +6008,19 @@
 11.2072  ** then the conversion is performed.  Otherwise no conversion occurs.
 11.2073  ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
 11.2074  **
 11.2075 +** ^Within the [xUpdate] method of a [virtual table], the
 11.2076 +** sqlite3_value_nochange(X) interface returns true if and only if
 11.2077 +** the column corresponding to X is unchanged by the UPDATE operation
 11.2078 +** that the xUpdate method call was invoked to implement and if
 11.2079 +** and the prior [xColumn] method call that was invoked to extracted
 11.2080 +** the value for that column returned without setting a result (probably
 11.2081 +** because it queried [sqlite3_vtab_nochange()] and found that the column
 11.2082 +** was unchanging).  ^Within an [xUpdate] method, any value for which
 11.2083 +** sqlite3_value_nochange(X) is true will in all other respects appear
 11.2084 +** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
 11.2085 +** than within an [xUpdate] method call for an UPDATE statement, then
 11.2086 +** the return value is arbitrary and meaningless.
 11.2087 +**
 11.2088  ** Please pay particular attention to the fact that the pointer returned
 11.2089  ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
 11.2090  ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
 11.2091 @@ -4906,19 +6029,43 @@
 11.2092  **
 11.2093  ** These routines must be called from the same thread as
 11.2094  ** the SQL function that supplied the [sqlite3_value*] parameters.
 11.2095 +**
 11.2096 +** As long as the input parameter is correct, these routines can only
 11.2097 +** fail if an out-of-memory error occurs during a format conversion.
 11.2098 +** Only the following subset of interfaces are subject to out-of-memory
 11.2099 +** errors:
 11.2100 +**
 11.2101 +** <ul>
 11.2102 +** <li> sqlite3_value_blob()
 11.2103 +** <li> sqlite3_value_text()
 11.2104 +** <li> sqlite3_value_text16()
 11.2105 +** <li> sqlite3_value_text16le()
 11.2106 +** <li> sqlite3_value_text16be()
 11.2107 +** <li> sqlite3_value_bytes()
 11.2108 +** <li> sqlite3_value_bytes16()
 11.2109 +** </ul>
 11.2110 +**
 11.2111 +** If an out-of-memory error occurs, then the return value from these
 11.2112 +** routines is the same as if the column had contained an SQL NULL value.
 11.2113 +** Valid SQL NULL returns can be distinguished from out-of-memory errors
 11.2114 +** by invoking the [sqlite3_errcode()] immediately after the suspect
 11.2115 +** return value is obtained and before any
 11.2116 +** other SQLite interface is called on the same [database connection].
 11.2117  */
 11.2118  SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
 11.2119 -SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
 11.2120 -SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
 11.2121  SQLITE_API double sqlite3_value_double(sqlite3_value*);
 11.2122  SQLITE_API int sqlite3_value_int(sqlite3_value*);
 11.2123  SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
 11.2124 +SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
 11.2125  SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
 11.2126  SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
 11.2127  SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
 11.2128  SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
 11.2129 +SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
 11.2130 +SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
 11.2131  SQLITE_API int sqlite3_value_type(sqlite3_value*);
 11.2132  SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
 11.2133 +SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
 11.2134  
 11.2135  /*
 11.2136  ** CAPI3REF: Finding The Subtype Of SQL Values
 11.2137 @@ -4929,10 +6076,6 @@
 11.2138  ** information can be used to pass a limited amount of context from
 11.2139  ** one SQL function to another.  Use the [sqlite3_result_subtype()]
 11.2140  ** routine to set the subtype for the return value of an SQL function.
 11.2141 -**
 11.2142 -** SQLite makes no use of subtype itself.  It merely passes the subtype
 11.2143 -** from the result of one [application-defined SQL function] into the
 11.2144 -** input of another.
 11.2145  */
 11.2146  SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
 11.2147  
 11.2148 @@ -5202,7 +6345,7 @@
 11.2149  ** when it has finished using that result.
 11.2150  ** ^If the 4th parameter to the sqlite3_result_text* interfaces
 11.2151  ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
 11.2152 -** then SQLite makes a copy of the result into space obtained from
 11.2153 +** then SQLite makes a copy of the result into space obtained
 11.2154  ** from [sqlite3_malloc()] before it returns.
 11.2155  **
 11.2156  ** ^The sqlite3_result_value() interface sets the result of
 11.2157 @@ -5215,6 +6358,17 @@
 11.2158  ** [unprotected sqlite3_value] object is required, so either
 11.2159  ** kind of [sqlite3_value] object can be used with this interface.
 11.2160  **
 11.2161 +** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
 11.2162 +** SQL NULL value, just like [sqlite3_result_null(C)], except that it
 11.2163 +** also associates the host-language pointer P or type T with that 
 11.2164 +** NULL value such that the pointer can be retrieved within an
 11.2165 +** [application-defined SQL function] using [sqlite3_value_pointer()].
 11.2166 +** ^If the D parameter is not NULL, then it is a pointer to a destructor
 11.2167 +** for the P parameter.  ^SQLite invokes D with P as its only argument
 11.2168 +** when SQLite is finished with P.  The T parameter should be a static
 11.2169 +** string and preferably a string literal. The sqlite3_result_pointer()
 11.2170 +** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
 11.2171 +**
 11.2172  ** If these routines are called from within the different thread
 11.2173  ** than the one containing the application-defined function that received
 11.2174  ** the [sqlite3_context] pointer, the results are undefined.
 11.2175 @@ -5238,6 +6392,7 @@
 11.2176  SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
 11.2177  SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
 11.2178  SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
 11.2179 +SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
 11.2180  SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
 11.2181  SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
 11.2182  
 11.2183 @@ -5567,6 +6722,41 @@
 11.2184  SQLITE_API char *sqlite3_data_directory;
 11.2185  
 11.2186  /*
 11.2187 +** CAPI3REF: Win32 Specific Interface
 11.2188 +**
 11.2189 +** These interfaces are available only on Windows.  The
 11.2190 +** [sqlite3_win32_set_directory] interface is used to set the value associated
 11.2191 +** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
 11.2192 +** zValue, depending on the value of the type parameter.  The zValue parameter
 11.2193 +** should be NULL to cause the previous value to be freed via [sqlite3_free];
 11.2194 +** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
 11.2195 +** prior to being used.  The [sqlite3_win32_set_directory] interface returns
 11.2196 +** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
 11.2197 +** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
 11.2198 +** [sqlite3_data_directory] variable is intended to act as a replacement for
 11.2199 +** the current directory on the sub-platforms of Win32 where that concept is
 11.2200 +** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
 11.2201 +** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
 11.2202 +** sqlite3_win32_set_directory interface except the string parameter must be
 11.2203 +** UTF-8 or UTF-16, respectively.
 11.2204 +*/
 11.2205 +SQLITE_API int sqlite3_win32_set_directory(
 11.2206 +  unsigned long type, /* Identifier for directory being set or reset */
 11.2207 +  void *zValue        /* New value for directory being set or reset */
 11.2208 +);
 11.2209 +SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
 11.2210 +SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
 11.2211 +
 11.2212 +/*
 11.2213 +** CAPI3REF: Win32 Directory Types
 11.2214 +**
 11.2215 +** These macros are only available on Windows.  They define the allowed values
 11.2216 +** for the type argument to the [sqlite3_win32_set_directory] interface.
 11.2217 +*/
 11.2218 +#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
 11.2219 +#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
 11.2220 +
 11.2221 +/*
 11.2222  ** CAPI3REF: Test For Auto-Commit Mode
 11.2223  ** KEYWORDS: {autocommit mode}
 11.2224  ** METHOD: sqlite3
 11.2225 @@ -5897,7 +7087,9 @@
 11.2226  ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
 11.2227  ** NULL pointer, then this routine simply checks for the existence of the
 11.2228  ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
 11.2229 -** does not.
 11.2230 +** does not.  If the table name parameter T in a call to
 11.2231 +** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
 11.2232 +** undefined behavior.
 11.2233  **
 11.2234  ** ^The column is identified by the second, third and fourth parameters to
 11.2235  ** this function. ^(The second parameter is either the name of the database
 11.2236 @@ -6164,6 +7356,9 @@
 11.2237    int (*xSavepoint)(sqlite3_vtab *pVTab, int);
 11.2238    int (*xRelease)(sqlite3_vtab *pVTab, int);
 11.2239    int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
 11.2240 +  /* The methods above are in versions 1 and 2 of the sqlite_module object.
 11.2241 +  ** Those below are for version 3 and greater. */
 11.2242 +  int (*xShadowName)(const char*);
 11.2243  };
 11.2244  
 11.2245  /*
 11.2246 @@ -6296,6 +7491,10 @@
 11.2247  
 11.2248  /*
 11.2249  ** CAPI3REF: Virtual Table Scan Flags
 11.2250 +**
 11.2251 +** Virtual table implementations are allowed to set the 
 11.2252 +** [sqlite3_index_info].idxFlags field to some combination of
 11.2253 +** these bits.
 11.2254  */
 11.2255  #define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
 11.2256  
 11.2257 @@ -6307,15 +7506,21 @@
 11.2258  ** an operator that is part of a constraint term in the wHERE clause of
 11.2259  ** a query that uses a [virtual table].
 11.2260  */
 11.2261 -#define SQLITE_INDEX_CONSTRAINT_EQ      2
 11.2262 -#define SQLITE_INDEX_CONSTRAINT_GT      4
 11.2263 -#define SQLITE_INDEX_CONSTRAINT_LE      8
 11.2264 -#define SQLITE_INDEX_CONSTRAINT_LT     16
 11.2265 -#define SQLITE_INDEX_CONSTRAINT_GE     32
 11.2266 -#define SQLITE_INDEX_CONSTRAINT_MATCH  64
 11.2267 -#define SQLITE_INDEX_CONSTRAINT_LIKE   65
 11.2268 -#define SQLITE_INDEX_CONSTRAINT_GLOB   66
 11.2269 -#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
 11.2270 +#define SQLITE_INDEX_CONSTRAINT_EQ         2
 11.2271 +#define SQLITE_INDEX_CONSTRAINT_GT         4
 11.2272 +#define SQLITE_INDEX_CONSTRAINT_LE         8
 11.2273 +#define SQLITE_INDEX_CONSTRAINT_LT        16
 11.2274 +#define SQLITE_INDEX_CONSTRAINT_GE        32
 11.2275 +#define SQLITE_INDEX_CONSTRAINT_MATCH     64
 11.2276 +#define SQLITE_INDEX_CONSTRAINT_LIKE      65
 11.2277 +#define SQLITE_INDEX_CONSTRAINT_GLOB      66
 11.2278 +#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
 11.2279 +#define SQLITE_INDEX_CONSTRAINT_NE        68
 11.2280 +#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
 11.2281 +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
 11.2282 +#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
 11.2283 +#define SQLITE_INDEX_CONSTRAINT_IS        72
 11.2284 +#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
 11.2285  
 11.2286  /*
 11.2287  ** CAPI3REF: Register A Virtual Table Implementation
 11.2288 @@ -6992,6 +8197,7 @@
 11.2289  /*
 11.2290  ** CAPI3REF: Low-Level Control Of Database Files
 11.2291  ** METHOD: sqlite3
 11.2292 +** KEYWORDS: {file control}
 11.2293  **
 11.2294  ** ^The [sqlite3_file_control()] interface makes a direct call to the
 11.2295  ** xFileControl method for the [sqlite3_io_methods] object associated
 11.2296 @@ -7006,11 +8212,18 @@
 11.2297  ** the xFileControl method.  ^The return value of the xFileControl
 11.2298  ** method becomes the return value of this routine.
 11.2299  **
 11.2300 -** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
 11.2301 +** A few opcodes for [sqlite3_file_control()] are handled directly
 11.2302 +** by the SQLite core and never invoke the 
 11.2303 +** sqlite3_io_methods.xFileControl method.
 11.2304 +** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
 11.2305  ** a pointer to the underlying [sqlite3_file] object to be written into
 11.2306 -** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
 11.2307 -** case is a short-circuit path which does not actually invoke the
 11.2308 -** underlying sqlite3_io_methods.xFileControl method.
 11.2309 +** the space pointed to by the 4th parameter.  The
 11.2310 +** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
 11.2311 +** the [sqlite3_file] object associated with the journal file instead of
 11.2312 +** the main database.  The [SQLITE_FCNTL_VFS_POINTER] opcode returns
 11.2313 +** a pointer to the underlying [sqlite3_vfs] object for the file.
 11.2314 +** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
 11.2315 +** from the pager.
 11.2316  **
 11.2317  ** ^If the second parameter (zDbName) does not match the name of any
 11.2318  ** open database file, then SQLITE_ERROR is returned.  ^This error
 11.2319 @@ -7020,7 +8233,7 @@
 11.2320  ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
 11.2321  ** xFileControl method.
 11.2322  **
 11.2323 -** See also: [SQLITE_FCNTL_LOCKSTATE]
 11.2324 +** See also: [file control opcodes]
 11.2325  */
 11.2326  SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
 11.2327  
 11.2328 @@ -7066,8 +8279,9 @@
 11.2329  #define SQLITE_TESTCTRL_ALWAYS                  13
 11.2330  #define SQLITE_TESTCTRL_RESERVE                 14
 11.2331  #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
 11.2332 -#define SQLITE_TESTCTRL_ISKEYWORD               16
 11.2333 -#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
 11.2334 +#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
 11.2335 +#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
 11.2336 +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
 11.2337  #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
 11.2338  #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
 11.2339  #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
 11.2340 @@ -7077,7 +8291,191 @@
 11.2341  #define SQLITE_TESTCTRL_ISINIT                  23
 11.2342  #define SQLITE_TESTCTRL_SORTER_MMAP             24
 11.2343  #define SQLITE_TESTCTRL_IMPOSTER                25
 11.2344 -#define SQLITE_TESTCTRL_LAST                    25
 11.2345 +#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
 11.2346 +#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
 11.2347 +
 11.2348 +/*
 11.2349 +** CAPI3REF: SQL Keyword Checking
 11.2350 +**
 11.2351 +** These routines provide access to the set of SQL language keywords 
 11.2352 +** recognized by SQLite.  Applications can uses these routines to determine
 11.2353 +** whether or not a specific identifier needs to be escaped (for example,
 11.2354 +** by enclosing in double-quotes) so as not to confuse the parser.
 11.2355 +**
 11.2356 +** The sqlite3_keyword_count() interface returns the number of distinct
 11.2357 +** keywords understood by SQLite.
 11.2358 +**
 11.2359 +** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
 11.2360 +** makes *Z point to that keyword expressed as UTF8 and writes the number
 11.2361 +** of bytes in the keyword into *L.  The string that *Z points to is not
 11.2362 +** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
 11.2363 +** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
 11.2364 +** or L are NULL or invalid pointers then calls to
 11.2365 +** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
 11.2366 +**
 11.2367 +** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
 11.2368 +** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
 11.2369 +** if it is and zero if not.
 11.2370 +**
 11.2371 +** The parser used by SQLite is forgiving.  It is often possible to use
 11.2372 +** a keyword as an identifier as long as such use does not result in a
 11.2373 +** parsing ambiguity.  For example, the statement
 11.2374 +** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
 11.2375 +** creates a new table named "BEGIN" with three columns named
 11.2376 +** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
 11.2377 +** using keywords as identifiers.  Common techniques used to avoid keyword
 11.2378 +** name collisions include:
 11.2379 +** <ul>
 11.2380 +** <li> Put all identifier names inside double-quotes.  This is the official
 11.2381 +**      SQL way to escape identifier names.
 11.2382 +** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
 11.2383 +**      but it is what SQL Server does and so lots of programmers use this
 11.2384 +**      technique.
 11.2385 +** <li> Begin every identifier with the letter "Z" as no SQL keywords start
 11.2386 +**      with "Z".
 11.2387 +** <li> Include a digit somewhere in every identifier name.
 11.2388 +** </ul>
 11.2389 +**
 11.2390 +** Note that the number of keywords understood by SQLite can depend on
 11.2391 +** compile-time options.  For example, "VACUUM" is not a keyword if
 11.2392 +** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
 11.2393 +** new keywords may be added to future releases of SQLite.
 11.2394 +*/
 11.2395 +SQLITE_API int sqlite3_keyword_count(void);
 11.2396 +SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
 11.2397 +SQLITE_API int sqlite3_keyword_check(const char*,int);
 11.2398 +
 11.2399 +/*
 11.2400 +** CAPI3REF: Dynamic String Object
 11.2401 +** KEYWORDS: {dynamic string}
 11.2402 +**
 11.2403 +** An instance of the sqlite3_str object contains a dynamically-sized
 11.2404 +** string under construction.
 11.2405 +**
 11.2406 +** The lifecycle of an sqlite3_str object is as follows:
 11.2407 +** <ol>
 11.2408 +** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
 11.2409 +** <li> ^Text is appended to the sqlite3_str object using various
 11.2410 +** methods, such as [sqlite3_str_appendf()].
 11.2411 +** <li> ^The sqlite3_str object is destroyed and the string it created
 11.2412 +** is returned using the [sqlite3_str_finish()] interface.
 11.2413 +** </ol>
 11.2414 +*/
 11.2415 +typedef struct sqlite3_str sqlite3_str;
 11.2416 +
 11.2417 +/*
 11.2418 +** CAPI3REF: Create A New Dynamic String Object
 11.2419 +** CONSTRUCTOR: sqlite3_str
 11.2420 +**
 11.2421 +** ^The [sqlite3_str_new(D)] interface allocates and initializes
 11.2422 +** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
 11.2423 +** [sqlite3_str_new()] must be freed by a subsequent call to 
 11.2424 +** [sqlite3_str_finish(X)].
 11.2425 +**
 11.2426 +** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
 11.2427 +** valid [sqlite3_str] object, though in the event of an out-of-memory
 11.2428 +** error the returned object might be a special singleton that will
 11.2429 +** silently reject new text, always return SQLITE_NOMEM from 
 11.2430 +** [sqlite3_str_errcode()], always return 0 for 
 11.2431 +** [sqlite3_str_length()], and always return NULL from
 11.2432 +** [sqlite3_str_finish(X)].  It is always safe to use the value
 11.2433 +** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
 11.2434 +** to any of the other [sqlite3_str] methods.
 11.2435 +**
 11.2436 +** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
 11.2437 +** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
 11.2438 +** length of the string contained in the [sqlite3_str] object will be
 11.2439 +** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
 11.2440 +** of [SQLITE_MAX_LENGTH].
 11.2441 +*/
 11.2442 +SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
 11.2443 +
 11.2444 +/*
 11.2445 +** CAPI3REF: Finalize A Dynamic String
 11.2446 +** DESTRUCTOR: sqlite3_str
 11.2447 +**
 11.2448 +** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
 11.2449 +** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
 11.2450 +** that contains the constructed string.  The calling application should
 11.2451 +** pass the returned value to [sqlite3_free()] to avoid a memory leak.
 11.2452 +** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
 11.2453 +** errors were encountered during construction of the string.  ^The
 11.2454 +** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
 11.2455 +** string in [sqlite3_str] object X is zero bytes long.
 11.2456 +*/
 11.2457 +SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
 11.2458 +
 11.2459 +/*
 11.2460 +** CAPI3REF: Add Content To A Dynamic String
 11.2461 +** METHOD: sqlite3_str
 11.2462 +**
 11.2463 +** These interfaces add content to an sqlite3_str object previously obtained
 11.2464 +** from [sqlite3_str_new()].
 11.2465 +**
 11.2466 +** ^The [sqlite3_str_appendf(X,F,...)] and 
 11.2467 +** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
 11.2468 +** functionality of SQLite to append formatted text onto the end of 
 11.2469 +** [sqlite3_str] object X.
 11.2470 +**
 11.2471 +** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
 11.2472 +** onto the end of the [sqlite3_str] object X.  N must be non-negative.
 11.2473 +** S must contain at least N non-zero bytes of content.  To append a
 11.2474 +** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
 11.2475 +** method instead.
 11.2476 +**
 11.2477 +** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
 11.2478 +** zero-terminated string S onto the end of [sqlite3_str] object X.
 11.2479 +**
 11.2480 +** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
 11.2481 +** single-byte character C onto the end of [sqlite3_str] object X.
 11.2482 +** ^This method can be used, for example, to add whitespace indentation.
 11.2483 +**
 11.2484 +** ^The [sqlite3_str_reset(X)] method resets the string under construction
 11.2485 +** inside [sqlite3_str] object X back to zero bytes in length.  
 11.2486 +**
 11.2487 +** These methods do not return a result code.  ^If an error occurs, that fact
 11.2488 +** is recorded in the [sqlite3_str] object and can be recovered by a
 11.2489 +** subsequent call to [sqlite3_str_errcode(X)].
 11.2490 +*/
 11.2491 +SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
 11.2492 +SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
 11.2493 +SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
 11.2494 +SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
 11.2495 +SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
 11.2496 +SQLITE_API void sqlite3_str_reset(sqlite3_str*);
 11.2497 +
 11.2498 +/*
 11.2499 +** CAPI3REF: Status Of A Dynamic String
 11.2500 +** METHOD: sqlite3_str
 11.2501 +**
 11.2502 +** These interfaces return the current status of an [sqlite3_str] object.
 11.2503 +**
 11.2504 +** ^If any prior errors have occurred while constructing the dynamic string
 11.2505 +** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
 11.2506 +** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
 11.2507 +** [SQLITE_NOMEM] following any out-of-memory error, or
 11.2508 +** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
 11.2509 +** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
 11.2510 +**
 11.2511 +** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
 11.2512 +** of the dynamic string under construction in [sqlite3_str] object X.
 11.2513 +** ^The length returned by [sqlite3_str_length(X)] does not include the
 11.2514 +** zero-termination byte.
 11.2515 +**
 11.2516 +** ^The [sqlite3_str_value(X)] method returns a pointer to the current
 11.2517 +** content of the dynamic string under construction in X.  The value
 11.2518 +** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
 11.2519 +** and might be freed or altered by any subsequent method on the same
 11.2520 +** [sqlite3_str] object.  Applications must not used the pointer returned
 11.2521 +** [sqlite3_str_value(X)] after any subsequent method call on the same
 11.2522 +** object.  ^Applications may change the content of the string returned
 11.2523 +** by [sqlite3_str_value(X)] as long as they do not write into any bytes
 11.2524 +** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
 11.2525 +** write any byte after any subsequent sqlite3_str method call.
 11.2526 +*/
 11.2527 +SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
 11.2528 +SQLITE_API int sqlite3_str_length(sqlite3_str*);
 11.2529 +SQLITE_API char *sqlite3_str_value(sqlite3_str*);
 11.2530  
 11.2531  /*
 11.2532  ** CAPI3REF: SQLite Runtime Status
 11.2533 @@ -7126,8 +8524,7 @@
 11.2534  ** <dd>This parameter is the current amount of memory checked out
 11.2535  ** using [sqlite3_malloc()], either directly or indirectly.  The
 11.2536  ** figure includes calls made to [sqlite3_malloc()] by the application
 11.2537 -** and internal memory usage by the SQLite library.  Scratch memory
 11.2538 -** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
 11.2539 +** and internal memory usage by the SQLite library.  Auxiliary page-cache
 11.2540  ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
 11.2541  ** this parameter.  The amount returned is the sum of the allocation
 11.2542  ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
 11.2543 @@ -7165,29 +8562,14 @@
 11.2544  ** *pHighwater parameter to [sqlite3_status()] is of interest.  
 11.2545  ** The value written into the *pCurrent parameter is undefined.</dd>)^
 11.2546  **
 11.2547 -** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
 11.2548 -** <dd>This parameter returns the number of allocations used out of the
 11.2549 -** [scratch memory allocator] configured using
 11.2550 -** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
 11.2551 -** in bytes.  Since a single thread may only have one scratch allocation
 11.2552 -** outstanding at time, this parameter also reports the number of threads
 11.2553 -** using scratch memory at the same time.</dd>)^
 11.2554 +** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
 11.2555 +** <dd>No longer used.</dd>
 11.2556  **
 11.2557  ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
 11.2558 -** <dd>This parameter returns the number of bytes of scratch memory
 11.2559 -** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
 11.2560 -** buffer and where forced to overflow to [sqlite3_malloc()].  The values
 11.2561 -** returned include overflows because the requested allocation was too
 11.2562 -** larger (that is, because the requested allocation was larger than the
 11.2563 -** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
 11.2564 -** slots were available.
 11.2565 -** </dd>)^
 11.2566 -**
 11.2567 -** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
 11.2568 -** <dd>This parameter records the largest memory allocation request
 11.2569 -** handed to [scratch memory allocator].  Only the value returned in the
 11.2570 -** *pHighwater parameter to [sqlite3_status()] is of interest.  
 11.2571 -** The value written into the *pCurrent parameter is undefined.</dd>)^
 11.2572 +** <dd>No longer used.</dd>
 11.2573 +**
 11.2574 +** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
 11.2575 +** <dd>No longer used.</dd>
 11.2576  **
 11.2577  ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
 11.2578  ** <dd>The *pHighwater parameter records the deepest parser stack. 
 11.2579 @@ -7200,12 +8582,12 @@
 11.2580  #define SQLITE_STATUS_MEMORY_USED          0
 11.2581  #define SQLITE_STATUS_PAGECACHE_USED       1
 11.2582  #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
 11.2583 -#define SQLITE_STATUS_SCRATCH_USED         3
 11.2584 -#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
 11.2585 +#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
 11.2586 +#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
 11.2587  #define SQLITE_STATUS_MALLOC_SIZE          5
 11.2588  #define SQLITE_STATUS_PARSER_STACK         6
 11.2589  #define SQLITE_STATUS_PAGECACHE_SIZE       7
 11.2590 -#define SQLITE_STATUS_SCRATCH_SIZE         8
 11.2591 +#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
 11.2592  #define SQLITE_STATUS_MALLOC_COUNT         9
 11.2593  
 11.2594  /*
 11.2595 @@ -7328,6 +8710,15 @@
 11.2596  ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
 11.2597  ** </dd>
 11.2598  **
 11.2599 +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
 11.2600 +** <dd>This parameter returns the number of dirty cache entries that have
 11.2601 +** been written to disk in the middle of a transaction due to the page
 11.2602 +** cache overflowing. Transactions are more efficient if they are written
 11.2603 +** to disk all at once. When pages spill mid-transaction, that introduces
 11.2604 +** additional overhead. This parameter can be used help identify
 11.2605 +** inefficiencies that can be resolve by increasing the cache size.
 11.2606 +** </dd>
 11.2607 +**
 11.2608  ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
 11.2609  ** <dd>This parameter returns zero for the current value if and only if
 11.2610  ** all foreign key constraints (deferred or immediate) have been
 11.2611 @@ -7347,7 +8738,8 @@
 11.2612  #define SQLITE_DBSTATUS_CACHE_WRITE          9
 11.2613  #define SQLITE_DBSTATUS_DEFERRED_FKS        10
 11.2614  #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
 11.2615 -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
 11.2616 +#define SQLITE_DBSTATUS_CACHE_SPILL         12
 11.2617 +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
 11.2618  
 11.2619  
 11.2620  /*
 11.2621 @@ -7410,6 +8802,24 @@
 11.2622  ** used as a proxy for the total work done by the prepared statement.
 11.2623  ** If the number of virtual machine operations exceeds 2147483647
 11.2624  ** then the value returned by this statement status code is undefined.
 11.2625 +**
 11.2626 +** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
 11.2627 +** <dd>^This is the number of times that the prepare statement has been
 11.2628 +** automatically regenerated due to schema changes or change to 
 11.2629 +** [bound parameters] that might affect the query plan.
 11.2630 +**
 11.2631 +** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
 11.2632 +** <dd>^This is the number of times that the prepared statement has
 11.2633 +** been run.  A single "run" for the purposes of this counter is one
 11.2634 +** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
 11.2635 +** The counter is incremented on the first [sqlite3_step()] call of each
 11.2636 +** cycle.
 11.2637 +**
 11.2638 +** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
 11.2639 +** <dd>^This is the approximate number of bytes of heap memory
 11.2640 +** used to store the prepared statement.  ^This value is not actually
 11.2641 +** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
 11.2642 +** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
 11.2643  ** </dd>
 11.2644  ** </dl>
 11.2645  */
 11.2646 @@ -7417,6 +8827,9 @@
 11.2647  #define SQLITE_STMTSTATUS_SORT              2
 11.2648  #define SQLITE_STMTSTATUS_AUTOINDEX         3
 11.2649  #define SQLITE_STMTSTATUS_VM_STEP           4
 11.2650 +#define SQLITE_STMTSTATUS_REPREPARE         5
 11.2651 +#define SQLITE_STMTSTATUS_RUN               6
 11.2652 +#define SQLITE_STMTSTATUS_MEMUSED           99
 11.2653  
 11.2654  /*
 11.2655  ** CAPI3REF: Custom Page Cache Object
 11.2656 @@ -8281,6 +9694,7 @@
 11.2657  ** can use to customize and optimize their behavior.
 11.2658  **
 11.2659  ** <dl>
 11.2660 +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
 11.2661  ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
 11.2662  ** <dd>Calls of the form
 11.2663  ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
 11.2664 @@ -8327,6 +9741,40 @@
 11.2665  SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
 11.2666  
 11.2667  /*
 11.2668 +** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
 11.2669 +**
 11.2670 +** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
 11.2671 +** method of a [virtual table], then it returns true if and only if the
 11.2672 +** column is being fetched as part of an UPDATE operation during which the
 11.2673 +** column value will not change.  Applications might use this to substitute
 11.2674 +** a return value that is less expensive to compute and that the corresponding
 11.2675 +** [xUpdate] method understands as a "no-change" value.
 11.2676 +**
 11.2677 +** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
 11.2678 +** the column is not changed by the UPDATE statement, then the xColumn
 11.2679 +** method can optionally return without setting a result, without calling
 11.2680 +** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
 11.2681 +** In that case, [sqlite3_value_nochange(X)] will return true for the
 11.2682 +** same column in the [xUpdate] method.
 11.2683 +*/
 11.2684 +SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
 11.2685 +
 11.2686 +/*
 11.2687 +** CAPI3REF: Determine The Collation For a Virtual Table Constraint
 11.2688 +**
 11.2689 +** This function may only be called from within a call to the [xBestIndex]
 11.2690 +** method of a [virtual table]. 
 11.2691 +**
 11.2692 +** The first argument must be the sqlite3_index_info object that is the
 11.2693 +** first parameter to the xBestIndex() method. The second argument must be
 11.2694 +** an index into the aConstraint[] array belonging to the sqlite3_index_info
 11.2695 +** structure passed to xBestIndex. This function returns a pointer to a buffer 
 11.2696 +** containing the name of the collation sequence for the corresponding
 11.2697 +** constraint.
 11.2698 +*/
 11.2699 +SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
 11.2700 +
 11.2701 +/*
 11.2702  ** CAPI3REF: Conflict resolution modes
 11.2703  ** KEYWORDS: {conflict resolution mode}
 11.2704  **
 11.2705 @@ -8596,7 +10044,6 @@
 11.2706  /*
 11.2707  ** CAPI3REF: Database Snapshot
 11.2708  ** KEYWORDS: {snapshot} {sqlite3_snapshot}
 11.2709 -** EXPERIMENTAL
 11.2710  **
 11.2711  ** An instance of the snapshot object records the state of a [WAL mode]
 11.2712  ** database for some specific point in history.
 11.2713 @@ -8613,11 +10060,6 @@
 11.2714  ** version of the database file so that it is possible to later open a new read
 11.2715  ** transaction that sees that historical version of the database rather than
 11.2716  ** the most recent version.
 11.2717 -**
 11.2718 -** The constructor for this object is [sqlite3_snapshot_get()].  The
 11.2719 -** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer
 11.2720 -** to an historical snapshot (if possible).  The destructor for 
 11.2721 -** sqlite3_snapshot objects is [sqlite3_snapshot_free()].
 11.2722  */
 11.2723  typedef struct sqlite3_snapshot {
 11.2724    unsigned char hidden[48];
 11.2725 @@ -8625,7 +10067,7 @@
 11.2726  
 11.2727  /*
 11.2728  ** CAPI3REF: Record A Database Snapshot
 11.2729 -** EXPERIMENTAL
 11.2730 +** CONSTRUCTOR: sqlite3_snapshot
 11.2731  **
 11.2732  ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
 11.2733  ** new [sqlite3_snapshot] object that records the current state of
 11.2734 @@ -8641,7 +10083,7 @@
 11.2735  ** in this case. 
 11.2736  **
 11.2737  ** <ul>
 11.2738 -**   <li> The database handle must be in [autocommit mode].
 11.2739 +**   <li> The database handle must not be in [autocommit mode].
 11.2740  **
 11.2741  **   <li> Schema S of [database connection] D must be a [WAL mode] database.
 11.2742  **
 11.2743 @@ -8664,7 +10106,7 @@
 11.2744  ** to avoid a memory leak.
 11.2745  **
 11.2746  ** The [sqlite3_snapshot_get()] interface is only available when the
 11.2747 -** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
 11.2748 +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
 11.2749  */
 11.2750  SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
 11.2751    sqlite3 *db,
 11.2752 @@ -8674,24 +10116,35 @@
 11.2753  
 11.2754  /*
 11.2755  ** CAPI3REF: Start a read transaction on an historical snapshot
 11.2756 -** EXPERIMENTAL
 11.2757 -**
 11.2758 -** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a
 11.2759 -** read transaction for schema S of
 11.2760 -** [database connection] D such that the read transaction
 11.2761 -** refers to historical [snapshot] P, rather than the most
 11.2762 -** recent change to the database.
 11.2763 -** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success
 11.2764 -** or an appropriate [error code] if it fails.
 11.2765 -**
 11.2766 -** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
 11.2767 -** the first operation following the [BEGIN] that takes the schema S
 11.2768 -** out of [autocommit mode].
 11.2769 -** ^In other words, schema S must not currently be in
 11.2770 -** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the
 11.2771 -** database connection D must be out of [autocommit mode].
 11.2772 -** ^A [snapshot] will fail to open if it has been overwritten by a
 11.2773 -** [checkpoint].
 11.2774 +** METHOD: sqlite3_snapshot
 11.2775 +**
 11.2776 +** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read 
 11.2777 +** transaction or upgrades an existing one for schema S of 
 11.2778 +** [database connection] D such that the read transaction refers to 
 11.2779 +** historical [snapshot] P, rather than the most recent change to the 
 11.2780 +** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK 
 11.2781 +** on success or an appropriate [error code] if it fails.
 11.2782 +**
 11.2783 +** ^In order to succeed, the database connection must not be in 
 11.2784 +** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
 11.2785 +** is already a read transaction open on schema S, then the database handle
 11.2786 +** must have no active statements (SELECT statements that have been passed
 11.2787 +** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). 
 11.2788 +** SQLITE_ERROR is returned if either of these conditions is violated, or
 11.2789 +** if schema S does not exist, or if the snapshot object is invalid.
 11.2790 +**
 11.2791 +** ^A call to sqlite3_snapshot_open() will fail to open if the specified
 11.2792 +** snapshot has been overwritten by a [checkpoint]. In this case 
 11.2793 +** SQLITE_ERROR_SNAPSHOT is returned.
 11.2794 +**
 11.2795 +** If there is already a read transaction open when this function is 
 11.2796 +** invoked, then the same read transaction remains open (on the same
 11.2797 +** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
 11.2798 +** is returned. If another error code - for example SQLITE_PROTOCOL or an
 11.2799 +** SQLITE_IOERR error code - is returned, then the final state of the
 11.2800 +** read transaction is undefined. If SQLITE_OK is returned, then the 
 11.2801 +** read transaction is now open on database snapshot P.
 11.2802 +**
 11.2803  ** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
 11.2804  ** database connection D does not know that the database file for
 11.2805  ** schema S is in [WAL mode].  A database connection might not know
 11.2806 @@ -8702,7 +10155,7 @@
 11.2807  ** database connection in order to make it ready to use snapshots.)
 11.2808  **
 11.2809  ** The [sqlite3_snapshot_open()] interface is only available when the
 11.2810 -** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
 11.2811 +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
 11.2812  */
 11.2813  SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
 11.2814    sqlite3 *db,
 11.2815 @@ -8712,20 +10165,20 @@
 11.2816  
 11.2817  /*
 11.2818  ** CAPI3REF: Destroy a snapshot
 11.2819 -** EXPERIMENTAL
 11.2820 +** DESTRUCTOR: sqlite3_snapshot
 11.2821  **
 11.2822  ** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
 11.2823  ** The application must eventually free every [sqlite3_snapshot] object
 11.2824  ** using this routine to avoid a memory leak.
 11.2825  **
 11.2826  ** The [sqlite3_snapshot_free()] interface is only available when the
 11.2827 -** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
 11.2828 +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
 11.2829  */
 11.2830  SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
 11.2831  
 11.2832  /*
 11.2833  ** CAPI3REF: Compare the ages of two snapshot handles.
 11.2834 -** EXPERIMENTAL
 11.2835 +** METHOD: sqlite3_snapshot
 11.2836  **
 11.2837  ** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
 11.2838  ** of two valid snapshot handles. 
 11.2839 @@ -8744,6 +10197,9 @@
 11.2840  ** Otherwise, this API returns a negative value if P1 refers to an older
 11.2841  ** snapshot than P2, zero if the two handles refer to the same database
 11.2842  ** snapshot, and a positive value if P1 is a newer snapshot than P2.
 11.2843 +**
 11.2844 +** This interface is only available if SQLite is compiled with the
 11.2845 +** [SQLITE_ENABLE_SNAPSHOT] option.
 11.2846  */
 11.2847  SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
 11.2848    sqlite3_snapshot *p1,
 11.2849 @@ -8752,27 +10208,152 @@
 11.2850  
 11.2851  /*
 11.2852  ** CAPI3REF: Recover snapshots from a wal file
 11.2853 -** EXPERIMENTAL
 11.2854 -**
 11.2855 -** If all connections disconnect from a database file but do not perform
 11.2856 -** a checkpoint, the existing wal file is opened along with the database
 11.2857 -** file the next time the database is opened. At this point it is only
 11.2858 -** possible to successfully call sqlite3_snapshot_open() to open the most
 11.2859 -** recent snapshot of the database (the one at the head of the wal file),
 11.2860 -** even though the wal file may contain other valid snapshots for which
 11.2861 -** clients have sqlite3_snapshot handles.
 11.2862 -**
 11.2863 -** This function attempts to scan the wal file associated with database zDb
 11.2864 +** METHOD: sqlite3_snapshot
 11.2865 +**
 11.2866 +** If a [WAL file] remains on disk after all database connections close
 11.2867 +** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
 11.2868 +** or because the last process to have the database opened exited without
 11.2869 +** calling [sqlite3_close()]) and a new connection is subsequently opened
 11.2870 +** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
 11.2871 +** will only be able to open the last transaction added to the WAL file
 11.2872 +** even though the WAL file contains other valid transactions.
 11.2873 +**
 11.2874 +** This function attempts to scan the WAL file associated with database zDb
 11.2875  ** of database handle db and make all valid snapshots available to
 11.2876  ** sqlite3_snapshot_open(). It is an error if there is already a read
 11.2877 -** transaction open on the database, or if the database is not a wal mode
 11.2878 +** transaction open on the database, or if the database is not a WAL mode
 11.2879  ** database.
 11.2880  **
 11.2881  ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
 11.2882 +**
 11.2883 +** This interface is only available if SQLite is compiled with the
 11.2884 +** [SQLITE_ENABLE_SNAPSHOT] option.
 11.2885  */
 11.2886  SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
 11.2887  
 11.2888  /*
 11.2889 +** CAPI3REF: Serialize a database
 11.2890 +**
 11.2891 +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
 11.2892 +** that is a serialization of the S database on [database connection] D.
 11.2893 +** If P is not a NULL pointer, then the size of the database in bytes
 11.2894 +** is written into *P.
 11.2895 +**
 11.2896 +** For an ordinary on-disk database file, the serialization is just a
 11.2897 +** copy of the disk file.  For an in-memory database or a "TEMP" database,
 11.2898 +** the serialization is the same sequence of bytes which would be written
 11.2899 +** to disk if that database where backed up to disk.
 11.2900 +**
 11.2901 +** The usual case is that sqlite3_serialize() copies the serialization of
 11.2902 +** the database into memory obtained from [sqlite3_malloc64()] and returns
 11.2903 +** a pointer to that memory.  The caller is responsible for freeing the
 11.2904 +** returned value to avoid a memory leak.  However, if the F argument
 11.2905 +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
 11.2906 +** are made, and the sqlite3_serialize() function will return a pointer
 11.2907 +** to the contiguous memory representation of the database that SQLite
 11.2908 +** is currently using for that database, or NULL if the no such contiguous
 11.2909 +** memory representation of the database exists.  A contiguous memory
 11.2910 +** representation of the database will usually only exist if there has
 11.2911 +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
 11.2912 +** values of D and S.
 11.2913 +** The size of the database is written into *P even if the 
 11.2914 +** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
 11.2915 +** of the database exists.
 11.2916 +**
 11.2917 +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
 11.2918 +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
 11.2919 +** allocation error occurs.
 11.2920 +**
 11.2921 +** This interface is only available if SQLite is compiled with the
 11.2922 +** [SQLITE_ENABLE_DESERIALIZE] option.
 11.2923 +*/
 11.2924 +SQLITE_API unsigned char *sqlite3_serialize(
 11.2925 +  sqlite3 *db,           /* The database connection */
 11.2926 +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
 11.2927 +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
 11.2928 +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
 11.2929 +);
 11.2930 +
 11.2931 +/*
 11.2932 +** CAPI3REF: Flags for sqlite3_serialize
 11.2933 +**
 11.2934 +** Zero or more of the following constants can be OR-ed together for
 11.2935 +** the F argument to [sqlite3_serialize(D,S,P,F)].
 11.2936 +**
 11.2937 +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
 11.2938 +** a pointer to contiguous in-memory database that it is currently using,
 11.2939 +** without making a copy of the database.  If SQLite is not currently using
 11.2940 +** a contiguous in-memory database, then this option causes
 11.2941 +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
 11.2942 +** using a contiguous in-memory database if it has been initialized by a
 11.2943 +** prior call to [sqlite3_deserialize()].
 11.2944 +*/
 11.2945 +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
 11.2946 +
 11.2947 +/*
 11.2948 +** CAPI3REF: Deserialize a database
 11.2949 +**
 11.2950 +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
 11.2951 +** [database connection] D to disconnect from database S and then
 11.2952 +** reopen S as an in-memory database based on the serialization contained
 11.2953 +** in P.  The serialized database P is N bytes in size.  M is the size of
 11.2954 +** the buffer P, which might be larger than N.  If M is larger than N, and
 11.2955 +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
 11.2956 +** permitted to add content to the in-memory database as long as the total
 11.2957 +** size does not exceed M bytes.
 11.2958 +**
 11.2959 +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
 11.2960 +** invoke sqlite3_free() on the serialization buffer when the database
 11.2961 +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
 11.2962 +** SQLite will try to increase the buffer size using sqlite3_realloc64()
 11.2963 +** if writes on the database cause it to grow larger than M bytes.
 11.2964 +**
 11.2965 +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
 11.2966 +** database is currently in a read transaction or is involved in a backup
 11.2967 +** operation.
 11.2968 +**
 11.2969 +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
 11.2970 +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
 11.2971 +** [sqlite3_free()] is invoked on argument P prior to returning.
 11.2972 +**
 11.2973 +** This interface is only available if SQLite is compiled with the
 11.2974 +** [SQLITE_ENABLE_DESERIALIZE] option.
 11.2975 +*/
 11.2976 +SQLITE_API int sqlite3_deserialize(
 11.2977 +  sqlite3 *db,            /* The database connection */
 11.2978 +  const char *zSchema,    /* Which DB to reopen with the deserialization */
 11.2979 +  unsigned char *pData,   /* The serialized database content */
 11.2980 +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
 11.2981 +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
 11.2982 +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
 11.2983 +);
 11.2984 +
 11.2985 +/*
 11.2986 +** CAPI3REF: Flags for sqlite3_deserialize()
 11.2987 +**
 11.2988 +** The following are allowed values for 6th argument (the F argument) to
 11.2989 +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
 11.2990 +**
 11.2991 +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
 11.2992 +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
 11.2993 +** and that SQLite should take ownership of this memory and automatically
 11.2994 +** free it when it has finished using it.  Without this flag, the caller
 11.2995 +** is responsible for freeing any dynamically allocated memory.
 11.2996 +**
 11.2997 +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
 11.2998 +** grow the size of the database using calls to [sqlite3_realloc64()].  This
 11.2999 +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
 11.3000 +** Without this flag, the deserialized database cannot increase in size beyond
 11.3001 +** the number of bytes specified by the M parameter.
 11.3002 +**
 11.3003 +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
 11.3004 +** should be treated as read-only.
 11.3005 +*/
 11.3006 +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
 11.3007 +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
 11.3008 +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
 11.3009 +
 11.3010 +/*
 11.3011  ** Undo the hack that converts floating point types to integer for
 11.3012  ** builds on processors without floating point support.
 11.3013  */
 11.3014 @@ -8883,7 +10464,7 @@
 11.3015    sqlite3_int64 iRowid;             /* Rowid for current entry */
 11.3016    sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
 11.3017    int eParentWithin;                /* Visibility of parent node */
 11.3018 -  int eWithin;                      /* OUT: Visiblity */
 11.3019 +  int eWithin;                      /* OUT: Visibility */
 11.3020    sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
 11.3021    /* The following fields are only available in 3.8.11 and later */
 11.3022    sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
 11.3023 @@ -8919,16 +10500,23 @@
 11.3024  
 11.3025  /*
 11.3026  ** CAPI3REF: Session Object Handle
 11.3027 +**
 11.3028 +** An instance of this object is a [session] that can be used to
 11.3029 +** record changes to a database.
 11.3030  */
 11.3031  typedef struct sqlite3_session sqlite3_session;
 11.3032  
 11.3033  /*
 11.3034  ** CAPI3REF: Changeset Iterator Handle
 11.3035 +**
 11.3036 +** An instance of this object acts as a cursor for iterating
 11.3037 +** over the elements of a [changeset] or [patchset].
 11.3038  */
 11.3039  typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
 11.3040  
 11.3041  /*
 11.3042  ** CAPI3REF: Create A New Session Object
 11.3043 +** CONSTRUCTOR: sqlite3_session
 11.3044  **
 11.3045  ** Create a new session object attached to database handle db. If successful,
 11.3046  ** a pointer to the new object is written to *ppSession and SQLITE_OK is
 11.3047 @@ -8965,6 +10553,7 @@
 11.3048  
 11.3049  /*
 11.3050  ** CAPI3REF: Delete A Session Object
 11.3051 +** DESTRUCTOR: sqlite3_session
 11.3052  **
 11.3053  ** Delete a session object previously allocated using 
 11.3054  ** [sqlite3session_create()]. Once a session object has been deleted, the
 11.3055 @@ -8980,6 +10569,7 @@
 11.3056  
 11.3057  /*
 11.3058  ** CAPI3REF: Enable Or Disable A Session Object
 11.3059 +** METHOD: sqlite3_session
 11.3060  **
 11.3061  ** Enable or disable the recording of changes by a session object. When
 11.3062  ** enabled, a session object records changes made to the database. When
 11.3063 @@ -8999,6 +10589,7 @@
 11.3064  
 11.3065  /*
 11.3066  ** CAPI3REF: Set Or Clear the Indirect Change Flag
 11.3067 +** METHOD: sqlite3_session
 11.3068  **
 11.3069  ** Each change recorded by a session object is marked as either direct or
 11.3070  ** indirect. A change is marked as indirect if either:
 11.3071 @@ -9028,6 +10619,7 @@
 11.3072  
 11.3073  /*
 11.3074  ** CAPI3REF: Attach A Table To A Session Object
 11.3075 +** METHOD: sqlite3_session
 11.3076  **
 11.3077  ** If argument zTab is not NULL, then it is the name of a table to attach
 11.3078  ** to the session object passed as the first argument. All subsequent changes 
 11.3079 @@ -9053,6 +10645,35 @@
 11.3080  **
 11.3081  ** SQLITE_OK is returned if the call completes without error. Or, if an error 
 11.3082  ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
 11.3083 +**
 11.3084 +** <h3>Special sqlite_stat1 Handling</h3>
 11.3085 +**
 11.3086 +** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
 11.3087 +** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
 11.3088 +**  <pre>
 11.3089 +**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
 11.3090 +**  </pre>
 11.3091 +**
 11.3092 +** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
 11.3093 +** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
 11.3094 +** are recorded for rows for which (idx IS NULL) is true. However, for such
 11.3095 +** rows a zero-length blob (SQL value X'') is stored in the changeset or
 11.3096 +** patchset instead of a NULL value. This allows such changesets to be
 11.3097 +** manipulated by legacy implementations of sqlite3changeset_invert(),
 11.3098 +** concat() and similar.
 11.3099 +**
 11.3100 +** The sqlite3changeset_apply() function automatically converts the 
 11.3101 +** zero-length blob back to a NULL value when updating the sqlite_stat1
 11.3102 +** table. However, if the application calls sqlite3changeset_new(),
 11.3103 +** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
 11.3104 +** iterator directly (including on a changeset iterator passed to a
 11.3105 +** conflict-handler callback) then the X'' value is returned. The application
 11.3106 +** must translate X'' to NULL itself if required.
 11.3107 +**
 11.3108 +** Legacy (older than 3.22.0) versions of the sessions module cannot capture
 11.3109 +** changes made to the sqlite_stat1 table. Legacy versions of the
 11.3110 +** sqlite3changeset_apply() function silently ignore any modifications to the
 11.3111 +** sqlite_stat1 table that are part of a changeset or patchset.
 11.3112  */
 11.3113  SQLITE_API int sqlite3session_attach(
 11.3114    sqlite3_session *pSession,      /* Session object */
 11.3115 @@ -9061,6 +10682,7 @@
 11.3116  
 11.3117  /*
 11.3118  ** CAPI3REF: Set a table filter on a Session Object.
 11.3119 +** METHOD: sqlite3_session
 11.3120  **
 11.3121  ** The second argument (xFilter) is the "filter callback". For changes to rows 
 11.3122  ** in tables that are not attached to the Session object, the filter is called
 11.3123 @@ -9079,6 +10701,7 @@
 11.3124  
 11.3125  /*
 11.3126  ** CAPI3REF: Generate A Changeset From A Session Object
 11.3127 +** METHOD: sqlite3_session
 11.3128  **
 11.3129  ** Obtain a changeset containing changes to the tables attached to the 
 11.3130  ** session object passed as the first argument. If successful, 
 11.3131 @@ -9188,7 +10811,8 @@
 11.3132  );
 11.3133  
 11.3134  /*
 11.3135 -** CAPI3REF: Load The Difference Between Tables Into A Session 
 11.3136 +** CAPI3REF: Load The Difference Between Tables Into A Session
 11.3137 +** METHOD: sqlite3_session
 11.3138  **
 11.3139  ** If it is not already attached to the session object passed as the first
 11.3140  ** argument, this function attaches table zTbl in the same manner as the
 11.3141 @@ -9253,6 +10877,7 @@
 11.3142  
 11.3143  /*
 11.3144  ** CAPI3REF: Generate A Patchset From A Session Object
 11.3145 +** METHOD: sqlite3_session
 11.3146  **
 11.3147  ** The differences between a patchset and a changeset are that:
 11.3148  **
 11.3149 @@ -9281,8 +10906,8 @@
 11.3150  */
 11.3151  SQLITE_API int sqlite3session_patchset(
 11.3152    sqlite3_session *pSession,      /* Session object */
 11.3153 -  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
 11.3154 -  void **ppPatchset               /* OUT: Buffer containing changeset */
 11.3155 +  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
 11.3156 +  void **ppPatchset               /* OUT: Buffer containing patchset */
 11.3157  );
 11.3158  
 11.3159  /*
 11.3160 @@ -9304,6 +10929,7 @@
 11.3161  
 11.3162  /*
 11.3163  ** CAPI3REF: Create An Iterator To Traverse A Changeset 
 11.3164 +** CONSTRUCTOR: sqlite3_changeset_iter
 11.3165  **
 11.3166  ** Create an iterator used to iterate through the contents of a changeset.
 11.3167  ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
 11.3168 @@ -9334,16 +10960,43 @@
 11.3169  ** consecutively. There is no chance that the iterator will visit a change 
 11.3170  ** the applies to table X, then one for table Y, and then later on visit 
 11.3171  ** another change for table X.
 11.3172 +**
 11.3173 +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
 11.3174 +** may be modified by passing a combination of
 11.3175 +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
 11.3176 +**
 11.3177 +** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
 11.3178 +** and therefore subject to change.
 11.3179  */
 11.3180  SQLITE_API int sqlite3changeset_start(
 11.3181    sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
 11.3182    int nChangeset,                 /* Size of changeset blob in bytes */
 11.3183    void *pChangeset                /* Pointer to blob containing changeset */
 11.3184  );
 11.3185 +SQLITE_API int sqlite3changeset_start_v2(
 11.3186 +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
 11.3187 +  int nChangeset,                 /* Size of changeset blob in bytes */
 11.3188 +  void *pChangeset,               /* Pointer to blob containing changeset */
 11.3189 +  int flags                       /* SESSION_CHANGESETSTART_* flags */
 11.3190 +);
 11.3191 +
 11.3192 +/*
 11.3193 +** CAPI3REF: Flags for sqlite3changeset_start_v2
 11.3194 +**
 11.3195 +** The following flags may passed via the 4th parameter to
 11.3196 +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
 11.3197 +**
 11.3198 +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
 11.3199 +**   Invert the changeset while iterating through it. This is equivalent to
 11.3200 +**   inverting a changeset using sqlite3changeset_invert() before applying it.
 11.3201 +**   It is an error to specify this flag with a patchset.
 11.3202 +*/
 11.3203 +#define SQLITE_CHANGESETSTART_INVERT        0x0002
 11.3204  
 11.3205  
 11.3206  /*
 11.3207  ** CAPI3REF: Advance A Changeset Iterator
 11.3208 +** METHOD: sqlite3_changeset_iter
 11.3209  **
 11.3210  ** This function may only be used with iterators created by function
 11.3211  ** [sqlite3changeset_start()]. If it is called on an iterator passed to
 11.3212 @@ -9368,6 +11021,7 @@
 11.3213  
 11.3214  /*
 11.3215  ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
 11.3216 +** METHOD: sqlite3_changeset_iter
 11.3217  **
 11.3218  ** The pIter argument passed to this function may either be an iterator
 11.3219  ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 11.3220 @@ -9402,6 +11056,7 @@
 11.3221  
 11.3222  /*
 11.3223  ** CAPI3REF: Obtain The Primary Key Definition Of A Table
 11.3224 +** METHOD: sqlite3_changeset_iter
 11.3225  **
 11.3226  ** For each modified table, a changeset includes the following:
 11.3227  **
 11.3228 @@ -9433,6 +11088,7 @@
 11.3229  
 11.3230  /*
 11.3231  ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
 11.3232 +** METHOD: sqlite3_changeset_iter
 11.3233  **
 11.3234  ** The pIter argument passed to this function may either be an iterator
 11.3235  ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 11.3236 @@ -9463,6 +11119,7 @@
 11.3237  
 11.3238  /*
 11.3239  ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
 11.3240 +** METHOD: sqlite3_changeset_iter
 11.3241  **
 11.3242  ** The pIter argument passed to this function may either be an iterator
 11.3243  ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 11.3244 @@ -9496,6 +11153,7 @@
 11.3245  
 11.3246  /*
 11.3247  ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
 11.3248 +** METHOD: sqlite3_changeset_iter
 11.3249  **
 11.3250  ** This function should only be used with iterator objects passed to a
 11.3251  ** conflict-handler callback by [sqlite3changeset_apply()] with either
 11.3252 @@ -9523,6 +11181,7 @@
 11.3253  
 11.3254  /*
 11.3255  ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
 11.3256 +** METHOD: sqlite3_changeset_iter
 11.3257  **
 11.3258  ** This function may only be called with an iterator passed to an
 11.3259  ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
 11.3260 @@ -9539,6 +11198,7 @@
 11.3261  
 11.3262  /*
 11.3263  ** CAPI3REF: Finalize A Changeset Iterator
 11.3264 +** METHOD: sqlite3_changeset_iter
 11.3265  **
 11.3266  ** This function is used to finalize an iterator allocated with
 11.3267  ** [sqlite3changeset_start()].
 11.3268 @@ -9555,6 +11215,7 @@
 11.3269  ** to that error is returned by this function. Otherwise, SQLITE_OK is
 11.3270  ** returned. This is to allow the following pattern (pseudo-code):
 11.3271  **
 11.3272 +** <pre>
 11.3273  **   sqlite3changeset_start();
 11.3274  **   while( SQLITE_ROW==sqlite3changeset_next() ){
 11.3275  **     // Do something with change.
 11.3276 @@ -9563,6 +11224,7 @@
 11.3277  **   if( rc!=SQLITE_OK ){
 11.3278  **     // An error has occurred 
 11.3279  **   }
 11.3280 +** </pre>
 11.3281  */
 11.3282  SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
 11.3283  
 11.3284 @@ -9610,6 +11272,7 @@
 11.3285  ** sqlite3_changegroup object. Calling it produces similar results as the
 11.3286  ** following code fragment:
 11.3287  **
 11.3288 +** <pre>
 11.3289  **   sqlite3_changegroup *pGrp;
 11.3290  **   rc = sqlite3_changegroup_new(&pGrp);
 11.3291  **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
 11.3292 @@ -9620,6 +11283,7 @@
 11.3293  **     *ppOut = 0;
 11.3294  **     *pnOut = 0;
 11.3295  **   }
 11.3296 +** </pre>
 11.3297  **
 11.3298  ** Refer to the sqlite3_changegroup documentation below for details.
 11.3299  */
 11.3300 @@ -9635,11 +11299,15 @@
 11.3301  
 11.3302  /*
 11.3303  ** CAPI3REF: Changegroup Handle
 11.3304 +**
 11.3305 +** A changegroup is an object used to combine two or more 
 11.3306 +** [changesets] or [patchsets]
 11.3307  */
 11.3308  typedef struct sqlite3_changegroup sqlite3_changegroup;
 11.3309  
 11.3310  /*
 11.3311  ** CAPI3REF: Create A New Changegroup Object
 11.3312 +** CONSTRUCTOR: sqlite3_changegroup
 11.3313  **
 11.3314  ** An sqlite3_changegroup object is used to combine two or more changesets
 11.3315  ** (or patchsets) into a single changeset (or patchset). A single changegroup
 11.3316 @@ -9677,6 +11345,7 @@
 11.3317  
 11.3318  /*
 11.3319  ** CAPI3REF: Add A Changeset To A Changegroup
 11.3320 +** METHOD: sqlite3_changegroup
 11.3321  **
 11.3322  ** Add all changes within the changeset (or patchset) in buffer pData (size
 11.3323  ** nData bytes) to the changegroup. 
 11.3324 @@ -9754,6 +11423,7 @@
 11.3325  
 11.3326  /*
 11.3327  ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
 11.3328 +** METHOD: sqlite3_changegroup
 11.3329  **
 11.3330  ** Obtain a buffer containing a changeset (or patchset) representing the
 11.3331  ** current contents of the changegroup. If the inputs to the changegroup
 11.3332 @@ -9784,25 +11454,25 @@
 11.3333  
 11.3334  /*
 11.3335  ** CAPI3REF: Delete A Changegroup Object
 11.3336 +** DESTRUCTOR: sqlite3_changegroup
 11.3337  */
 11.3338  SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 11.3339  
 11.3340  /*
 11.3341  ** CAPI3REF: Apply A Changeset To A Database
 11.3342  **
 11.3343 -** Apply a changeset to a database. This function attempts to update the
 11.3344 -** "main" database attached to handle db with the changes found in the
 11.3345 -** changeset passed via the second and third arguments.
 11.3346 -**
 11.3347 -** The fourth argument (xFilter) passed to this function is the "filter
 11.3348 +** Apply a changeset or patchset to a database. These functions attempt to
 11.3349 +** update the "main" database attached to handle db with the changes found in
 11.3350 +** the changeset passed via the second and third arguments. 
 11.3351 +**
 11.3352 +** The fourth argument (xFilter) passed to these functions is the "filter
 11.3353  ** callback". If it is not NULL, then for each table affected by at least one
 11.3354  ** change in the changeset, the filter callback is invoked with
 11.3355  ** the table name as the second argument, and a copy of the context pointer
 11.3356 -** passed as the sixth argument to this function as the first. If the "filter
 11.3357 -** callback" returns zero, then no attempt is made to apply any changes to 
 11.3358 -** the table. Otherwise, if the return value is non-zero or the xFilter
 11.3359 -** argument to this function is NULL, all changes related to the table are
 11.3360 -** attempted.
 11.3361 +** passed as the sixth argument as the first. If the "filter callback"
 11.3362 +** returns zero, then no attempt is made to apply any changes to the table.
 11.3363 +** Otherwise, if the return value is non-zero or the xFilter argument to
 11.3364 +** is NULL, all changes related to the table are attempted.
 11.3365  **
 11.3366  ** For each table that is not excluded by the filter callback, this function 
 11.3367  ** tests that the target database contains a compatible table. A table is 
 11.3368 @@ -9847,7 +11517,7 @@
 11.3369  **
 11.3370  ** <dl>
 11.3371  ** <dt>DELETE Changes<dd>
 11.3372 -**   For each DELETE change, this function checks if the target database 
 11.3373 +**   For each DELETE change, the function checks if the target database 
 11.3374  **   contains a row with the same primary key value (or values) as the 
 11.3375  **   original row values stored in the changeset. If it does, and the values 
 11.3376  **   stored in all non-primary key columns also match the values stored in 
 11.3377 @@ -9892,7 +11562,7 @@
 11.3378  **   [SQLITE_CHANGESET_REPLACE].
 11.3379  **
 11.3380  ** <dt>UPDATE Changes<dd>
 11.3381 -**   For each UPDATE change, this function checks if the target database 
 11.3382 +**   For each UPDATE change, the function checks if the target database 
 11.3383  **   contains a row with the same primary key value (or values) as the 
 11.3384  **   original row values stored in the changeset. If it does, and the values 
 11.3385  **   stored in all modified non-primary key columns also match the values
 11.3386 @@ -9923,11 +11593,28 @@
 11.3387  ** This can be used to further customize the applications conflict
 11.3388  ** resolution strategy.
 11.3389  **
 11.3390 -** All changes made by this function are enclosed in a savepoint transaction.
 11.3391 +** All changes made by these functions are enclosed in a savepoint transaction.
 11.3392  ** If any other error (aside from a constraint failure when attempting to
 11.3393  ** write to the target database) occurs, then the savepoint transaction is
 11.3394  ** rolled back, restoring the target database to its original state, and an 
 11.3395  ** SQLite error code returned.
 11.3396 +**
 11.3397 +** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
 11.3398 +** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
 11.3399 +** may set (*ppRebase) to point to a "rebase" that may be used with the 
 11.3400 +** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
 11.3401 +** is set to the size of the buffer in bytes. It is the responsibility of the
 11.3402 +** caller to eventually free any such buffer using sqlite3_free(). The buffer
 11.3403 +** is only allocated and populated if one or more conflicts were encountered
 11.3404 +** while applying the patchset. See comments surrounding the sqlite3_rebaser
 11.3405 +** APIs for further details.
 11.3406 +**
 11.3407 +** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
 11.3408 +** may be modified by passing a combination of
 11.3409 +** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
 11.3410 +**
 11.3411 +** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
 11.3412 +** and therefore subject to change.
 11.3413  */
 11.3414  SQLITE_API int sqlite3changeset_apply(
 11.3415    sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11.3416 @@ -9944,6 +11631,47 @@
 11.3417    ),
 11.3418    void *pCtx                      /* First argument passed to xConflict */
 11.3419  );
 11.3420 +SQLITE_API int sqlite3changeset_apply_v2(
 11.3421 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11.3422 +  int nChangeset,                 /* Size of changeset in bytes */
 11.3423 +  void *pChangeset,               /* Changeset blob */
 11.3424 +  int(*xFilter)(
 11.3425 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
 11.3426 +    const char *zTab              /* Table name */
 11.3427 +  ),
 11.3428 +  int(*xConflict)(
 11.3429 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
 11.3430 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11.3431 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11.3432 +  ),
 11.3433 +  void *pCtx,                     /* First argument passed to xConflict */
 11.3434 +  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 11.3435 +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 11.3436 +);
 11.3437 +
 11.3438 +/*
 11.3439 +** CAPI3REF: Flags for sqlite3changeset_apply_v2
 11.3440 +**
 11.3441 +** The following flags may passed via the 9th parameter to
 11.3442 +** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
 11.3443 +**
 11.3444 +** <dl>
 11.3445 +** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
 11.3446 +**   Usually, the sessions module encloses all operations performed by
 11.3447 +**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 11.3448 +**   SAVEPOINT is committed if the changeset or patchset is successfully
 11.3449 +**   applied, or rolled back if an error occurs. Specifying this flag
 11.3450 +**   causes the sessions module to omit this savepoint. In this case, if the
 11.3451 +**   caller has an open transaction or savepoint when apply_v2() is called, 
 11.3452 +**   it may revert the partially applied changeset by rolling it back.
 11.3453 +**
 11.3454 +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
 11.3455 +**   Invert the changeset before applying it. This is equivalent to inverting
 11.3456 +**   a changeset using sqlite3changeset_invert() before applying it. It is
 11.3457 +**   an error to specify this flag with a patchset.
 11.3458 +*/
 11.3459 +#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
 11.3460 +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 11.3461  
 11.3462  /* 
 11.3463  ** CAPI3REF: Constants Passed To The Conflict Handler
 11.3464 @@ -10041,6 +11769,161 @@
 11.3465  #define SQLITE_CHANGESET_REPLACE    1
 11.3466  #define SQLITE_CHANGESET_ABORT      2
 11.3467  
 11.3468 +/* 
 11.3469 +** CAPI3REF: Rebasing changesets
 11.3470 +** EXPERIMENTAL
 11.3471 +**
 11.3472 +** Suppose there is a site hosting a database in state S0. And that
 11.3473 +** modifications are made that move that database to state S1 and a
 11.3474 +** changeset recorded (the "local" changeset). Then, a changeset based
 11.3475 +** on S0 is received from another site (the "remote" changeset) and 
 11.3476 +** applied to the database. The database is then in state 
 11.3477 +** (S1+"remote"), where the exact state depends on any conflict
 11.3478 +** resolution decisions (OMIT or REPLACE) made while applying "remote".
 11.3479 +** Rebasing a changeset is to update it to take those conflict 
 11.3480 +** resolution decisions into account, so that the same conflicts
 11.3481 +** do not have to be resolved elsewhere in the network. 
 11.3482 +**
 11.3483 +** For example, if both the local and remote changesets contain an
 11.3484 +** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
 11.3485 +**
 11.3486 +**   local:  INSERT INTO t1 VALUES(1, 'v1');
 11.3487 +**   remote: INSERT INTO t1 VALUES(1, 'v2');
 11.3488 +**
 11.3489 +** and the conflict resolution is REPLACE, then the INSERT change is
 11.3490 +** removed from the local changeset (it was overridden). Or, if the
 11.3491 +** conflict resolution was "OMIT", then the local changeset is modified
 11.3492 +** to instead contain:
 11.3493 +**
 11.3494 +**           UPDATE t1 SET b = 'v2' WHERE a=1;
 11.3495 +**
 11.3496 +** Changes within the local changeset are rebased as follows:
 11.3497 +**
 11.3498 +** <dl>
 11.3499 +** <dt>Local INSERT<dd>
 11.3500 +**   This may only conflict with a remote INSERT. If the conflict 
 11.3501 +**   resolution was OMIT, then add an UPDATE change to the rebased
 11.3502 +**   changeset. Or, if the conflict resolution was REPLACE, add
 11.3503 +**   nothing to the rebased changeset.
 11.3504 +**
 11.3505 +** <dt>Local DELETE<dd>
 11.3506 +**   This may conflict with a remote UPDATE or DELETE. In both cases the
 11.3507 +**   only possible resolution is OMIT. If the remote operation was a
 11.3508 +**   DELETE, then add no change to the rebased changeset. If the remote
 11.3509 +**   operation was an UPDATE, then the old.* fields of change are updated
 11.3510 +**   to reflect the new.* values in the UPDATE.
 11.3511 +**
 11.3512 +** <dt>Local UPDATE<dd>
 11.3513 +**   This may conflict with a remote UPDATE or DELETE. If it conflicts
 11.3514 +**   with a DELETE, and the conflict resolution was OMIT, then the update
 11.3515 +**   is changed into an INSERT. Any undefined values in the new.* record
 11.3516 +**   from the update change are filled in using the old.* values from
 11.3517 +**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
 11.3518 +**   the UPDATE change is simply omitted from the rebased changeset.
 11.3519 +**
 11.3520 +**   If conflict is with a remote UPDATE and the resolution is OMIT, then
 11.3521 +**   the old.* values are rebased using the new.* values in the remote
 11.3522 +**   change. Or, if the resolution is REPLACE, then the change is copied
 11.3523 +**   into the rebased changeset with updates to columns also updated by
 11.3524 +**   the conflicting remote UPDATE removed. If this means no columns would 
 11.3525 +**   be updated, the change is omitted.
 11.3526 +** </dl>
 11.3527 +**
 11.3528 +** A local change may be rebased against multiple remote changes 
 11.3529 +** simultaneously. If a single key is modified by multiple remote 
 11.3530 +** changesets, they are combined as follows before the local changeset
 11.3531 +** is rebased:
 11.3532 +**
 11.3533 +** <ul>
 11.3534 +**    <li> If there has been one or more REPLACE resolutions on a
 11.3535 +**         key, it is rebased according to a REPLACE.
 11.3536 +**
 11.3537 +**    <li> If there have been no REPLACE resolutions on a key, then
 11.3538 +**         the local changeset is rebased according to the most recent
 11.3539 +**         of the OMIT resolutions.
 11.3540 +** </ul>
 11.3541 +**
 11.3542 +** Note that conflict resolutions from multiple remote changesets are 
 11.3543 +** combined on a per-field basis, not per-row. This means that in the 
 11.3544 +** case of multiple remote UPDATE operations, some fields of a single 
 11.3545 +** local change may be rebased for REPLACE while others are rebased for 
 11.3546 +** OMIT.
 11.3547 +**
 11.3548 +** In order to rebase a local changeset, the remote changeset must first
 11.3549 +** be applied to the local database using sqlite3changeset_apply_v2() and
 11.3550 +** the buffer of rebase information captured. Then:
 11.3551 +**
 11.3552 +** <ol>
 11.3553 +**   <li> An sqlite3_rebaser object is created by calling 
 11.3554 +**        sqlite3rebaser_create().
 11.3555 +**   <li> The new object is configured with the rebase buffer obtained from
 11.3556 +**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
 11.3557 +**        If the local changeset is to be rebased against multiple remote
 11.3558 +**        changesets, then sqlite3rebaser_configure() should be called
 11.3559 +**        multiple times, in the same order that the multiple
 11.3560 +**        sqlite3changeset_apply_v2() calls were made.
 11.3561 +**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
 11.3562 +**   <li> The sqlite3_rebaser object is deleted by calling
 11.3563 +**        sqlite3rebaser_delete().
 11.3564 +** </ol>
 11.3565 +*/
 11.3566 +typedef struct sqlite3_rebaser sqlite3_rebaser;
 11.3567 +
 11.3568 +/*
 11.3569 +** CAPI3REF: Create a changeset rebaser object.
 11.3570 +** EXPERIMENTAL
 11.3571 +**
 11.3572 +** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
 11.3573 +** point to the new object and return SQLITE_OK. Otherwise, if an error
 11.3574 +** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
 11.3575 +** to NULL. 
 11.3576 +*/
 11.3577 +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
 11.3578 +
 11.3579 +/*
 11.3580 +** CAPI3REF: Configure a changeset rebaser object.
 11.3581 +** EXPERIMENTAL
 11.3582 +**
 11.3583 +** Configure the changeset rebaser object to rebase changesets according
 11.3584 +** to the conflict resolutions described by buffer pRebase (size nRebase
 11.3585 +** bytes), which must have been obtained from a previous call to
 11.3586 +** sqlite3changeset_apply_v2().
 11.3587 +*/
 11.3588 +SQLITE_API int sqlite3rebaser_configure(
 11.3589 +  sqlite3_rebaser*, 
 11.3590 +  int nRebase, const void *pRebase
 11.3591 +); 
 11.3592 +
 11.3593 +/*
 11.3594 +** CAPI3REF: Rebase a changeset
 11.3595 +** EXPERIMENTAL
 11.3596 +**
 11.3597 +** Argument pIn must point to a buffer containing a changeset nIn bytes
 11.3598 +** in size. This function allocates and populates a buffer with a copy
 11.3599 +** of the changeset rebased rebased according to the configuration of the
 11.3600 +** rebaser object passed as the first argument. If successful, (*ppOut)
 11.3601 +** is set to point to the new buffer containing the rebased changset and 
 11.3602 +** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
 11.3603 +** responsibility of the caller to eventually free the new buffer using
 11.3604 +** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
 11.3605 +** are set to zero and an SQLite error code returned.
 11.3606 +*/
 11.3607 +SQLITE_API int sqlite3rebaser_rebase(
 11.3608 +  sqlite3_rebaser*,
 11.3609 +  int nIn, const void *pIn, 
 11.3610 +  int *pnOut, void **ppOut 
 11.3611 +);
 11.3612 +
 11.3613 +/*
 11.3614 +** CAPI3REF: Delete a changeset rebaser object.
 11.3615 +** EXPERIMENTAL
 11.3616 +**
 11.3617 +** Delete the changeset rebaser object and all associated resources. There
 11.3618 +** should be one call to this function for each successful invocation
 11.3619 +** of sqlite3rebaser_create().
 11.3620 +*/
 11.3621 +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
 11.3622 +
 11.3623  /*
 11.3624  ** CAPI3REF: Streaming Versions of API functions.
 11.3625  **
 11.3626 @@ -10049,12 +11932,13 @@
 11.3627  **
 11.3628  ** <table border=1 style="margin-left:8ex;margin-right:8ex">
 11.3629  **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
 11.3630 -**   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 
 11.3631 -**   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 
 11.3632 -**   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 
 11.3633 -**   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 
 11.3634 -**   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 
 11.3635 -**   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 
 11.3636 +**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
 11.3637 +**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 
 11.3638 +**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
 11.3639 +**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
 11.3640 +**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
 11.3641 +**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
 11.3642 +**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
 11.3643  ** </table>
 11.3644  **
 11.3645  ** Non-streaming functions that accept changesets (or patchsets) as input
 11.3646 @@ -10145,6 +12029,23 @@
 11.3647    ),
 11.3648    void *pCtx                      /* First argument passed to xConflict */
 11.3649  );
 11.3650 +SQLITE_API int sqlite3changeset_apply_v2_strm(
 11.3651 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11.3652 +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
 11.3653 +  void *pIn,                                          /* First arg for xInput */
 11.3654 +  int(*xFilter)(
 11.3655 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
 11.3656 +    const char *zTab              /* Table name */
 11.3657 +  ),
 11.3658 +  int(*xConflict)(
 11.3659 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
 11.3660 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11.3661 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11.3662 +  ),
 11.3663 +  void *pCtx,                     /* First argument passed to xConflict */
 11.3664 +  void **ppRebase, int *pnRebase,
 11.3665 +  int flags
 11.3666 +);
 11.3667  SQLITE_API int sqlite3changeset_concat_strm(
 11.3668    int (*xInputA)(void *pIn, void *pData, int *pnData),
 11.3669    void *pInA,
 11.3670 @@ -10164,6 +12065,12 @@
 11.3671    int (*xInput)(void *pIn, void *pData, int *pnData),
 11.3672    void *pIn
 11.3673  );
 11.3674 +SQLITE_API int sqlite3changeset_start_v2_strm(
 11.3675 +  sqlite3_changeset_iter **pp,
 11.3676 +  int (*xInput)(void *pIn, void *pData, int *pnData),
 11.3677 +  void *pIn,
 11.3678 +  int flags
 11.3679 +);
 11.3680  SQLITE_API int sqlite3session_changeset_strm(
 11.3681    sqlite3_session *pSession,
 11.3682    int (*xOutput)(void *pOut, const void *pData, int nData),
 11.3683 @@ -10182,7 +12089,53 @@
 11.3684      int (*xOutput)(void *pOut, const void *pData, int nData), 
 11.3685      void *pOut
 11.3686  );
 11.3687 -
 11.3688 +SQLITE_API int sqlite3rebaser_rebase_strm(
 11.3689 +  sqlite3_rebaser *pRebaser,
 11.3690 +  int (*xInput)(void *pIn, void *pData, int *pnData),
 11.3691 +  void *pIn,
 11.3692 +  int (*xOutput)(void *pOut, const void *pData, int nData),
 11.3693 +  void *pOut
 11.3694 +);
 11.3695 +
 11.3696 +/*
 11.3697 +** CAPI3REF: Configure global parameters
 11.3698 +**
 11.3699 +** The sqlite3session_config() interface is used to make global configuration
 11.3700 +** changes to the sessions module in order to tune it to the specific needs 
 11.3701 +** of the application.
 11.3702 +**
 11.3703 +** The sqlite3session_config() interface is not threadsafe. If it is invoked
 11.3704 +** while any other thread is inside any other sessions method then the
 11.3705 +** results are undefined. Furthermore, if it is invoked after any sessions
 11.3706 +** related objects have been created, the results are also undefined. 
 11.3707 +**
 11.3708 +** The first argument to the sqlite3session_config() function must be one
 11.3709 +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
 11.3710 +** interpretation of the (void*) value passed as the second parameter and
 11.3711 +** the effect of calling this function depends on the value of the first
 11.3712 +** parameter.
 11.3713 +**
 11.3714 +** <dl>
 11.3715 +** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
 11.3716 +**    By default, the sessions module streaming interfaces attempt to input
 11.3717 +**    and output data in approximately 1 KiB chunks. This operand may be used
 11.3718 +**    to set and query the value of this configuration setting. The pointer
 11.3719 +**    passed as the second argument must point to a value of type (int).
 11.3720 +**    If this value is greater than 0, it is used as the new streaming data
 11.3721 +**    chunk size for both input and output. Before returning, the (int) value
 11.3722 +**    pointed to by pArg is set to the final value of the streaming interface
 11.3723 +**    chunk size.
 11.3724 +** </dl>
 11.3725 +**
 11.3726 +** This function returns SQLITE_OK if successful, or an SQLite error code
 11.3727 +** otherwise.
 11.3728 +*/
 11.3729 +SQLITE_API int sqlite3session_config(int op, void *pArg);
 11.3730 +
 11.3731 +/*
 11.3732 +** CAPI3REF: Values for sqlite3session_config().
 11.3733 +*/
 11.3734 +#define SQLITE_SESSION_CONFIG_STRMSIZE 1
 11.3735  
 11.3736  /*
 11.3737  ** Make sure we can call this stuff from C++.
 11.3738 @@ -10640,7 +12593,7 @@
 11.3739  **            This way, even if the tokenizer does not provide synonyms
 11.3740  **            when tokenizing query text (it should not - to do would be
 11.3741  **            inefficient), it doesn't matter if the user queries for 
 11.3742 -**            'first + place' or '1st + place', as there are entires in the
 11.3743 +**            'first + place' or '1st + place', as there are entries in the
 11.3744  **            FTS index corresponding to both forms of the first token.
 11.3745  **   </ol>
 11.3746  **
 11.3747 @@ -10668,7 +12621,7 @@
 11.3748  **   extra data to the FTS index or require FTS5 to query for multiple terms,
 11.3749  **   so it is efficient in terms of disk space and query speed. However, it
 11.3750  **   does not support prefix queries very well. If, as suggested above, the
 11.3751 -**   token "first" is subsituted for "1st" by the tokenizer, then the query:
 11.3752 +**   token "first" is substituted for "1st" by the tokenizer, then the query:
 11.3753  **
 11.3754  **   <codeblock>
 11.3755  **     ... MATCH '1s*'</codeblock>
 11.3756 @@ -10783,8 +12736,9 @@
 11.3757  ** Include the configuration header output by 'configure' if we're using the
 11.3758  ** autoconf-based build
 11.3759  */
 11.3760 -#ifdef _HAVE_SQLITE_CONFIG_H
 11.3761 -#include "config.h"
 11.3762 +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
 11.3763 +/* #include "config.h" */
 11.3764 +#define SQLITECONFIG_H 1
 11.3765  #endif
 11.3766  
 11.3767  /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
 11.3768 @@ -11094,6 +13048,11 @@
 11.3769  **
 11.3770  ** Older versions of SQLite used an optional THREADSAFE macro.
 11.3771  ** We support that for legacy.
 11.3772 +**
 11.3773 +** To ensure that the correct value of "THREADSAFE" is reported when querying
 11.3774 +** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
 11.3775 +** logic is partially replicated in ctime.c. If it is updated here, it should
 11.3776 +** also be updated there.
 11.3777  */
 11.3778  #if !defined(SQLITE_THREADSAFE)
 11.3779  # if defined(THREADSAFE)
 11.3780 @@ -11269,6 +13228,21 @@
 11.3781  #endif
 11.3782  
 11.3783  /*
 11.3784 +** Some conditionals are optimizations only.  In other words, if the
 11.3785 +** conditionals are replaced with a constant 1 (true) or 0 (false) then
 11.3786 +** the correct answer is still obtained, though perhaps not as quickly.
 11.3787 +**
 11.3788 +** The following macros mark these optimizations conditionals.
 11.3789 +*/
 11.3790 +#if defined(SQLITE_MUTATION_TEST)
 11.3791 +# define OK_IF_ALWAYS_TRUE(X)  (1)
 11.3792 +# define OK_IF_ALWAYS_FALSE(X) (0)
 11.3793 +#else
 11.3794 +# define OK_IF_ALWAYS_TRUE(X)  (X)
 11.3795 +# define OK_IF_ALWAYS_FALSE(X) (X)
 11.3796 +#endif
 11.3797 +
 11.3798 +/*
 11.3799  ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
 11.3800  ** defined.  We need to defend against those failures when testing with
 11.3801  ** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
 11.3802 @@ -11462,144 +13436,153 @@
 11.3803  #define TK_AS                              24
 11.3804  #define TK_WITHOUT                         25
 11.3805  #define TK_COMMA                           26
 11.3806 -#define TK_ID                              27
 11.3807 -#define TK_ABORT                           28
 11.3808 -#define TK_ACTION                          29
 11.3809 -#define TK_AFTER                           30
 11.3810 -#define TK_ANALYZE                         31
 11.3811 -#define TK_ASC                             32
 11.3812 -#define TK_ATTACH                          33
 11.3813 -#define TK_BEFORE                          34
 11.3814 -#define TK_BY                              35
 11.3815 -#define TK_CASCADE                         36
 11.3816 -#define TK_CAST                            37
 11.3817 -#define TK_COLUMNKW                        38
 11.3818 -#define TK_CONFLICT                        39
 11.3819 -#define TK_DATABASE                        40
 11.3820 -#define TK_DESC                            41
 11.3821 -#define TK_DETACH                          42
 11.3822 -#define TK_EACH                            43
 11.3823 -#define TK_FAIL                            44
 11.3824 -#define TK_FOR                             45
 11.3825 -#define TK_IGNORE                          46
 11.3826 -#define TK_INITIALLY                       47
 11.3827 -#define TK_INSTEAD                         48
 11.3828 -#define TK_LIKE_KW                         49
 11.3829 -#define TK_MATCH                           50
 11.3830 -#define TK_NO                              51
 11.3831 -#define TK_KEY                             52
 11.3832 -#define TK_OF                              53
 11.3833 -#define TK_OFFSET                          54
 11.3834 -#define TK_PRAGMA                          55
 11.3835 -#define TK_RAISE                           56
 11.3836 -#define TK_RECURSIVE                       57
 11.3837 -#define TK_REPLACE                         58
 11.3838 -#define TK_RESTRICT                        59
 11.3839 -#define TK_ROW                             60
 11.3840 -#define TK_TRIGGER                         61
 11.3841 -#define TK_VACUUM                          62
 11.3842 -#define TK_VIEW                            63
 11.3843 -#define TK_VIRTUAL                         64
 11.3844 -#define TK_WITH                            65
 11.3845 -#define TK_REINDEX                         66
 11.3846 -#define TK_RENAME                          67
 11.3847 -#define TK_CTIME_KW                        68
 11.3848 -#define TK_ANY                             69
 11.3849 -#define TK_OR                              70
 11.3850 -#define TK_AND                             71
 11.3851 -#define TK_IS                              72
 11.3852 -#define TK_BETWEEN                         73
 11.3853 -#define TK_IN                              74
 11.3854 -#define TK_ISNULL                          75
 11.3855 -#define TK_NOTNULL                         76
 11.3856 -#define TK_NE                              77
 11.3857 -#define TK_EQ                              78
 11.3858 -#define TK_GT                              79
 11.3859 -#define TK_LE                              80
 11.3860 -#define TK_LT                              81
 11.3861 -#define TK_GE                              82
 11.3862 -#define TK_ESCAPE                          83
 11.3863 -#define TK_BITAND                          84
 11.3864 -#define TK_BITOR                           85
 11.3865 -#define TK_LSHIFT                          86
 11.3866 -#define TK_RSHIFT                          87
 11.3867 -#define TK_PLUS                            88
 11.3868 -#define TK_MINUS                           89
 11.3869 -#define TK_STAR                            90
 11.3870 -#define TK_SLASH                           91
 11.3871 -#define TK_REM                             92
 11.3872 -#define TK_CONCAT                          93
 11.3873 -#define TK_COLLATE                         94
 11.3874 -#define TK_BITNOT                          95
 11.3875 -#define TK_INDEXED                         96
 11.3876 -#define TK_STRING                          97
 11.3877 -#define TK_JOIN_KW                         98
 11.3878 -#define TK_CONSTRAINT                      99
 11.3879 -#define TK_DEFAULT                        100
 11.3880 -#define TK_NULL                           101
 11.3881 -#define TK_PRIMARY                        102
 11.3882 -#define TK_UNIQUE                         103
 11.3883 -#define TK_CHECK                          104
 11.3884 -#define TK_REFERENCES                     105
 11.3885 -#define TK_AUTOINCR                       106
 11.3886 -#define TK_ON                             107
 11.3887 -#define TK_INSERT                         108
 11.3888 -#define TK_DELETE                         109
 11.3889 -#define TK_UPDATE                         110
 11.3890 -#define TK_SET                            111
 11.3891 -#define TK_DEFERRABLE                     112
 11.3892 -#define TK_FOREIGN                        113
 11.3893 -#define TK_DROP                           114
 11.3894 -#define TK_UNION                          115
 11.3895 -#define TK_ALL                            116
 11.3896 -#define TK_EXCEPT                         117
 11.3897 -#define TK_INTERSECT                      118
 11.3898 -#define TK_SELECT                         119
 11.3899 -#define TK_VALUES                         120
 11.3900 -#define TK_DISTINCT                       121
 11.3901 -#define TK_DOT                            122
 11.3902 -#define TK_FROM                           123
 11.3903 -#define TK_JOIN                           124
 11.3904 -#define TK_USING                          125
 11.3905 -#define TK_ORDER                          126
 11.3906 -#define TK_GROUP                          127
 11.3907 -#define TK_HAVING                         128
 11.3908 -#define TK_LIMIT                          129
 11.3909 -#define TK_WHERE                          130
 11.3910 -#define TK_INTO                           131
 11.3911 -#define TK_FLOAT                          132
 11.3912 -#define TK_BLOB                           133
 11.3913 -#define TK_INTEGER                        134
 11.3914 -#define TK_VARIABLE                       135
 11.3915 -#define TK_CASE                           136
 11.3916 -#define TK_WHEN                           137
 11.3917 -#define TK_THEN                           138
 11.3918 -#define TK_ELSE                           139
 11.3919 -#define TK_INDEX                          140
 11.3920 -#define TK_ALTER                          141
 11.3921 -#define TK_ADD                            142
 11.3922 -#define TK_TO_TEXT                        143
 11.3923 -#define TK_TO_BLOB                        144
 11.3924 -#define TK_TO_NUMERIC                     145
 11.3925 -#define TK_TO_INT                         146
 11.3926 -#define TK_TO_REAL                        147
 11.3927 -#define TK_ISNOT                          148
 11.3928 -#define TK_END_OF_FILE                    149
 11.3929 -#define TK_UNCLOSED_STRING                150
 11.3930 -#define TK_FUNCTION                       151
 11.3931 -#define TK_COLUMN                         152
 11.3932 -#define TK_AGG_FUNCTION                   153
 11.3933 -#define TK_AGG_COLUMN                     154
 11.3934 -#define TK_UMINUS                         155
 11.3935 -#define TK_UPLUS                          156
 11.3936 -#define TK_REGISTER                       157
 11.3937 -#define TK_VECTOR                         158
 11.3938 -#define TK_SELECT_COLUMN                  159
 11.3939 -#define TK_IF_NULL_ROW                    160
 11.3940 -#define TK_ASTERISK                       161
 11.3941 -#define TK_SPAN                           162
 11.3942 -#define TK_SPACE                          163
 11.3943 -#define TK_ILLEGAL                        164
 11.3944 +#define TK_ABORT                           27
 11.3945 +#define TK_ACTION                          28
 11.3946 +#define TK_AFTER                           29
 11.3947 +#define TK_ANALYZE                         30
 11.3948 +#define TK_ASC                             31
 11.3949 +#define TK_ATTACH                          32
 11.3950 +#define TK_BEFORE                          33
 11.3951 +#define TK_BY                              34
 11.3952 +#define TK_CASCADE                         35
 11.3953 +#define TK_CAST                            36
 11.3954 +#define TK_CONFLICT                        37
 11.3955 +#define TK_DATABASE                        38
 11.3956 +#define TK_DESC                            39
 11.3957 +#define TK_DETACH                          40
 11.3958 +#define TK_EACH                            41
 11.3959 +#define TK_FAIL                            42
 11.3960 +#define TK_OR                              43
 11.3961 +#define TK_AND                             44
 11.3962 +#define TK_IS                              45
 11.3963 +#define TK_MATCH                           46
 11.3964 +#define TK_LIKE_KW                         47
 11.3965 +#define TK_BETWEEN                         48
 11.3966 +#define TK_IN                              49
 11.3967 +#define TK_ISNULL                          50
 11.3968 +#define TK_NOTNULL                         51
 11.3969 +#define TK_NE                              52
 11.3970 +#define TK_EQ                              53
 11.3971 +#define TK_GT                              54
 11.3972 +#define TK_LE                              55
 11.3973 +#define TK_LT                              56
 11.3974 +#define TK_GE                              57
 11.3975 +#define TK_ESCAPE                          58
 11.3976 +#define TK_ID                              59
 11.3977 +#define TK_COLUMNKW                        60
 11.3978 +#define TK_DO                              61
 11.3979 +#define TK_FOR                             62
 11.3980 +#define TK_IGNORE                          63
 11.3981 +#define TK_INITIALLY                       64
 11.3982 +#define TK_INSTEAD                         65
 11.3983 +#define TK_NO                              66
 11.3984 +#define TK_KEY                             67
 11.3985 +#define TK_OF                              68
 11.3986 +#define TK_OFFSET                          69
 11.3987 +#define TK_PRAGMA                          70
 11.3988 +#define TK_RAISE                           71
 11.3989 +#define TK_RECURSIVE                       72
 11.3990 +#define TK_REPLACE                         73
 11.3991 +#define TK_RESTRICT                        74
 11.3992 +#define TK_ROW                             75
 11.3993 +#define TK_ROWS                            76
 11.3994 +#define TK_TRIGGER                         77
 11.3995 +#define TK_VACUUM                          78
 11.3996 +#define TK_VIEW                            79
 11.3997 +#define TK_VIRTUAL                         80
 11.3998 +#define TK_WITH                            81
 11.3999 +#define TK_CURRENT                         82
 11.4000 +#define TK_FOLLOWING                       83
 11.4001 +#define TK_PARTITION                       84
 11.4002 +#define TK_PRECEDING                       85
 11.4003 +#define TK_RANGE                           86
 11.4004 +#define TK_UNBOUNDED                       87
 11.4005 +#define TK_REINDEX                         88
 11.4006 +#define TK_RENAME                          89
 11.4007 +#define TK_CTIME_KW                        90
 11.4008 +#define TK_ANY                             91
 11.4009 +#define TK_BITAND                          92
 11.4010 +#define TK_BITOR                           93
 11.4011 +#define TK_LSHIFT                          94
 11.4012 +#define TK_RSHIFT                          95
 11.4013 +#define TK_PLUS                            96
 11.4014 +#define TK_MINUS                           97
 11.4015 +#define TK_STAR                            98
 11.4016 +#define TK_SLASH                           99
 11.4017 +#define TK_REM                            100
 11.4018 +#define TK_CONCAT                         101
 11.4019 +#define TK_COLLATE                        102
 11.4020 +#define TK_BITNOT                         103
 11.4021 +#define TK_ON                             104
 11.4022 +#define TK_INDEXED                        105
 11.4023 +#define TK_STRING                         106
 11.4024 +#define TK_JOIN_KW                        107
 11.4025 +#define TK_CONSTRAINT                     108
 11.4026 +#define TK_DEFAULT                        109
 11.4027 +#define TK_NULL                           110
 11.4028 +#define TK_PRIMARY                        111
 11.4029 +#define TK_UNIQUE                         112
 11.4030 +#define TK_CHECK                          113
 11.4031 +#define TK_REFERENCES                     114
 11.4032 +#define TK_AUTOINCR                       115
 11.4033 +#define TK_INSERT                         116
 11.4034 +#define TK_DELETE                         117
 11.4035 +#define TK_UPDATE                         118
 11.4036 +#define TK_SET                            119
 11.4037 +#define TK_DEFERRABLE                     120
 11.4038 +#define TK_FOREIGN                        121
 11.4039 +#define TK_DROP                           122
 11.4040 +#define TK_UNION                          123
 11.4041 +#define TK_ALL                            124
 11.4042 +#define TK_EXCEPT                         125
 11.4043 +#define TK_INTERSECT                      126
 11.4044 +#define TK_SELECT                         127
 11.4045 +#define TK_VALUES                         128
 11.4046 +#define TK_DISTINCT                       129
 11.4047 +#define TK_DOT                            130
 11.4048 +#define TK_FROM                           131
 11.4049 +#define TK_JOIN                           132
 11.4050 +#define TK_USING                          133
 11.4051 +#define TK_ORDER                          134
 11.4052 +#define TK_GROUP                          135
 11.4053 +#define TK_HAVING                         136
 11.4054 +#define TK_LIMIT                          137
 11.4055 +#define TK_WHERE                          138
 11.4056 +#define TK_INTO                           139
 11.4057 +#define TK_NOTHING                        140
 11.4058 +#define TK_FLOAT                          141
 11.4059 +#define TK_BLOB                           142
 11.4060 +#define TK_INTEGER                        143
 11.4061 +#define TK_VARIABLE                       144
 11.4062 +#define TK_CASE                           145
 11.4063 +#define TK_WHEN                           146
 11.4064 +#define TK_THEN                           147
 11.4065 +#define TK_ELSE                           148
 11.4066 +#define TK_INDEX                          149
 11.4067 +#define TK_ALTER                          150
 11.4068 +#define TK_ADD                            151
 11.4069 +#define TK_WINDOW                         152
 11.4070 +#define TK_OVER                           153
 11.4071 +#define TK_FILTER                         154
 11.4072 +#define TK_TRUEFALSE                      155
 11.4073 +#define TK_ISNOT                          156
 11.4074 +#define TK_FUNCTION                       157
 11.4075 +#define TK_COLUMN                         158
 11.4076 +#define TK_AGG_FUNCTION                   159
 11.4077 +#define TK_AGG_COLUMN                     160
 11.4078 +#define TK_UMINUS                         161
 11.4079 +#define TK_UPLUS                          162
 11.4080 +#define TK_TRUTH                          163
 11.4081 +#define TK_REGISTER                       164
 11.4082 +#define TK_VECTOR                         165
 11.4083 +#define TK_SELECT_COLUMN                  166
 11.4084 +#define TK_IF_NULL_ROW                    167
 11.4085 +#define TK_ASTERISK                       168
 11.4086 +#define TK_SPAN                           169
 11.4087 +#define TK_END_OF_FILE                    170
 11.4088 +#define TK_UNCLOSED_STRING                171
 11.4089 +#define TK_SPACE                          172
 11.4090 +#define TK_ILLEGAL                        173
 11.4091  
 11.4092  /* The token codes above must all fit in 8 bits */
 11.4093  #define TKFLG_MASK           0xff  
 11.4094 @@ -11684,7 +13667,6 @@
 11.4095  */
 11.4096  #ifndef SQLITE_TEMP_STORE
 11.4097  # define SQLITE_TEMP_STORE 1
 11.4098 -# define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
 11.4099  #endif
 11.4100  
 11.4101  /*
 11.4102 @@ -11721,6 +13703,22 @@
 11.4103  #endif
 11.4104  
 11.4105  /*
 11.4106 +** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
 11.4107 +*/
 11.4108 +#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
 11.4109 +# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
 11.4110 +#endif
 11.4111 +
 11.4112 +/*
 11.4113 +** The compile-time options SQLITE_MMAP_READWRITE and 
 11.4114 +** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
 11.4115 +** You must choose one or the other (or neither) but not both.
 11.4116 +*/
 11.4117 +#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 11.4118 +#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 11.4119 +#endif
 11.4120 +
 11.4121 +/*
 11.4122  ** GCC does not define the offsetof() macro so we'll have to do it
 11.4123  ** ourselves.
 11.4124  */
 11.4125 @@ -11858,7 +13856,8 @@
 11.4126  # if defined(__SIZEOF_POINTER__)
 11.4127  #   define SQLITE_PTRSIZE __SIZEOF_POINTER__
 11.4128  # elif defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 11.4129 -       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)
 11.4130 +       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)   ||    \
 11.4131 +      (defined(__TOS_AIX__) && !defined(__64BIT__))
 11.4132  #   define SQLITE_PTRSIZE 4
 11.4133  # else
 11.4134  #   define SQLITE_PTRSIZE 8
 11.4135 @@ -11899,7 +13898,7 @@
 11.4136  # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 11.4137       defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 11.4138       defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 11.4139 -     defined(__arm__)
 11.4140 +     defined(__arm__)  || defined(_M_ARM64)
 11.4141  #   define SQLITE_BYTEORDER    1234
 11.4142  # elif defined(sparc)    || defined(__ppc__)
 11.4143  #   define SQLITE_BYTEORDER    4321
 11.4144 @@ -11985,7 +13984,6 @@
 11.4145  # else
 11.4146  #   define SQLITE_MAX_MMAP_SIZE 0
 11.4147  # endif
 11.4148 -# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
 11.4149  #endif
 11.4150  
 11.4151  /*
 11.4152 @@ -11995,7 +13993,6 @@
 11.4153  */
 11.4154  #ifndef SQLITE_DEFAULT_MMAP_SIZE
 11.4155  # define SQLITE_DEFAULT_MMAP_SIZE 0
 11.4156 -# define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
 11.4157  #endif
 11.4158  #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
 11.4159  # undef SQLITE_DEFAULT_MMAP_SIZE
 11.4160 @@ -12020,7 +14017,7 @@
 11.4161  ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
 11.4162  ** the Select query generator tracing logic is turned on.
 11.4163  */
 11.4164 -#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE)
 11.4165 +#if defined(SQLITE_ENABLE_SELECTTRACE)
 11.4166  # define SELECTTRACE_ENABLED 1
 11.4167  #else
 11.4168  # define SELECTTRACE_ENABLED 0
 11.4169 @@ -12037,9 +14034,10 @@
 11.4170  */
 11.4171  typedef struct BusyHandler BusyHandler;
 11.4172  struct BusyHandler {
 11.4173 -  int (*xFunc)(void *,int);  /* The busy callback */
 11.4174 -  void *pArg;                /* First arg to busy callback */
 11.4175 -  int nBusy;                 /* Incremented with each busy call */
 11.4176 +  int (*xBusyHandler)(void *,int);  /* The busy callback */
 11.4177 +  void *pBusyArg;                   /* First arg to busy callback */
 11.4178 +  int nBusy;                        /* Incremented with each busy call */
 11.4179 +  u8 bExtraFileArg;                 /* Include sqlite3_file as callback arg */
 11.4180  };
 11.4181  
 11.4182  /*
 11.4183 @@ -12139,7 +14137,6 @@
 11.4184  typedef struct Schema Schema;
 11.4185  typedef struct Expr Expr;
 11.4186  typedef struct ExprList ExprList;
 11.4187 -typedef struct ExprSpan ExprSpan;
 11.4188  typedef struct FKey FKey;
 11.4189  typedef struct FuncDestructor FuncDestructor;
 11.4190  typedef struct FuncDef FuncDef;
 11.4191 @@ -12156,13 +14153,14 @@
 11.4192  typedef struct Parse Parse;
 11.4193  typedef struct PreUpdate PreUpdate;
 11.4194  typedef struct PrintfArguments PrintfArguments;
 11.4195 +typedef struct RenameToken RenameToken;
 11.4196  typedef struct RowSet RowSet;
 11.4197  typedef struct Savepoint Savepoint;
 11.4198  typedef struct Select Select;
 11.4199  typedef struct SQLiteThread SQLiteThread;
 11.4200  typedef struct SelectDest SelectDest;
 11.4201  typedef struct SrcList SrcList;
 11.4202 -typedef struct StrAccum StrAccum;
 11.4203 +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
 11.4204  typedef struct Table Table;
 11.4205  typedef struct TableLock TableLock;
 11.4206  typedef struct Token Token;
 11.4207 @@ -12171,12 +14169,40 @@
 11.4208  typedef struct TriggerPrg TriggerPrg;
 11.4209  typedef struct TriggerStep TriggerStep;
 11.4210  typedef struct UnpackedRecord UnpackedRecord;
 11.4211 +typedef struct Upsert Upsert;
 11.4212  typedef struct VTable VTable;
 11.4213  typedef struct VtabCtx VtabCtx;
 11.4214  typedef struct Walker Walker;
 11.4215  typedef struct WhereInfo WhereInfo;
 11.4216 +typedef struct Window Window;
 11.4217  typedef struct With With;
 11.4218  
 11.4219 +
 11.4220 +/*
 11.4221 +** The bitmask datatype defined below is used for various optimizations.
 11.4222 +**
 11.4223 +** Changing this from a 64-bit to a 32-bit type limits the number of
 11.4224 +** tables in a join to 32 instead of 64.  But it also reduces the size
 11.4225 +** of the library by 738 bytes on ix86.
 11.4226 +*/
 11.4227 +#ifdef SQLITE_BITMASK_TYPE
 11.4228 +  typedef SQLITE_BITMASK_TYPE Bitmask;
 11.4229 +#else
 11.4230 +  typedef u64 Bitmask;
 11.4231 +#endif
 11.4232 +
 11.4233 +/*
 11.4234 +** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
 11.4235 +*/
 11.4236 +#define BMS  ((int)(sizeof(Bitmask)*8))
 11.4237 +
 11.4238 +/*
 11.4239 +** A bit in a Bitmask
 11.4240 +*/
 11.4241 +#define MASKBIT(n)   (((Bitmask)1)<<(n))
 11.4242 +#define MASKBIT32(n) (((unsigned int)1)<<(n))
 11.4243 +#define ALLBITS      ((Bitmask)-1)
 11.4244 +
 11.4245  /* A VList object records a mapping between parameters/variables/wildcards
 11.4246  ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
 11.4247  ** variable number associated with that parameter.  See the format description
 11.4248 @@ -12272,7 +14298,7 @@
 11.4249  SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
 11.4250  SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
 11.4251  SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
 11.4252 -SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
 11.4253 +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
 11.4254  SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
 11.4255  SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
 11.4256  SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
 11.4257 @@ -12424,6 +14450,7 @@
 11.4258    struct KeyInfo*,                     /* First argument to compare function */
 11.4259    BtCursor *pCursor                    /* Space to write cursor structure */
 11.4260  );
 11.4261 +SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
 11.4262  SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
 11.4263  SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
 11.4264  SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
 11.4265 @@ -12452,13 +14479,28 @@
 11.4266  ** entry in either an index or table btree.
 11.4267  **
 11.4268  ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
 11.4269 -** an arbitrary key and no data.  These btrees have pKey,nKey set to their
 11.4270 -** key and pData,nData,nZero set to zero.
 11.4271 +** an arbitrary key and no data.  These btrees have pKey,nKey set to the
 11.4272 +** key and the pData,nData,nZero fields are uninitialized.  The aMem,nMem
 11.4273 +** fields give an array of Mem objects that are a decomposition of the key.
 11.4274 +** The nMem field might be zero, indicating that no decomposition is available.
 11.4275  **
 11.4276  ** Table btrees (used for rowid tables) contain an integer rowid used as
 11.4277  ** the key and passed in the nKey field.  The pKey field is zero.  
 11.4278  ** pData,nData hold the content of the new entry.  nZero extra zero bytes
 11.4279  ** are appended to the end of the content when constructing the entry.
 11.4280 +** The aMem,nMem fields are uninitialized for table btrees.
 11.4281 +**
 11.4282 +** Field usage summary:
 11.4283 +**
 11.4284 +**               Table BTrees                   Index Btrees
 11.4285 +**
 11.4286 +**   pKey        always NULL                    encoded key
 11.4287 +**   nKey        the ROWID                      length of pKey
 11.4288 +**   pData       data                           not used
 11.4289 +**   aMem        not used                       decomposed key value
 11.4290 +**   nMem        not used                       entries in aMem
 11.4291 +**   nData       length of pData                not used
 11.4292 +**   nZero       extra zeros after pData        not used
 11.4293  **
 11.4294  ** This object is used to pass information into sqlite3BtreeInsert().  The
 11.4295  ** same information used to be passed as five separate parameters.  But placing
 11.4296 @@ -12469,7 +14511,7 @@
 11.4297  struct BtreePayload {
 11.4298    const void *pKey;       /* Key content for indexes.  NULL for tables */
 11.4299    sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
 11.4300 -  const void *pData;      /* Data for tables.  NULL for indexes */
 11.4301 +  const void *pData;      /* Data for tables. */
 11.4302    sqlite3_value *aMem;    /* First of nMem value in the unpacked pKey */
 11.4303    u16 nMem;               /* Number of aMem[] value.  Might be zero */
 11.4304    int nData;              /* Size of pData.  0 if none. */
 11.4305 @@ -12479,11 +14521,17 @@
 11.4306  SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
 11.4307                         int flags, int seekResult);
 11.4308  SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 11.4309 +#ifndef SQLITE_OMIT_WINDOWFUNC
 11.4310 +SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
 11.4311 +#endif
 11.4312  SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 11.4313 -SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
 11.4314 +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
 11.4315  SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 11.4316 -SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
 11.4317 +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
 11.4318  SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 11.4319 +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
 11.4320 +SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
 11.4321 +#endif
 11.4322  SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
 11.4323  SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
 11.4324  SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
 11.4325 @@ -12633,7 +14681,7 @@
 11.4326  #ifdef SQLITE_ENABLE_CURSOR_HINTS
 11.4327      Expr *pExpr;           /* Used when p4type is P4_EXPR */
 11.4328  #endif
 11.4329 -    int (*xAdvance)(BtCursor *, int *);
 11.4330 +    int (*xAdvance)(BtCursor *, int);
 11.4331    } p4;
 11.4332  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 11.4333    char *zComment;          /* Comment to improve readability */
 11.4334 @@ -12643,7 +14691,8 @@
 11.4335    u64 cycles;              /* Total time spent executing this instruction */
 11.4336  #endif
 11.4337  #ifdef SQLITE_VDBE_COVERAGE
 11.4338 -  int iSrcLine;            /* Source-code line that generated this opcode */
 11.4339 +  u32 iSrcLine;            /* Source-code line that generated this opcode
 11.4340 +                           ** with flags in the upper 8 bits */
 11.4341  #endif
 11.4342  };
 11.4343  typedef struct VdbeOp VdbeOp;
 11.4344 @@ -12677,24 +14726,27 @@
 11.4345  /*
 11.4346  ** Allowed values of VdbeOp.p4type
 11.4347  */
 11.4348 -#define P4_NOTUSED    0   /* The P4 parameter is not used */
 11.4349 -#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
 11.4350 -#define P4_STATIC   (-2)  /* Pointer to a static string */
 11.4351 -#define P4_COLLSEQ  (-3)  /* P4 is a pointer to a CollSeq structure */
 11.4352 -#define P4_FUNCDEF  (-4)  /* P4 is a pointer to a FuncDef structure */
 11.4353 -#define P4_KEYINFO  (-5)  /* P4 is a pointer to a KeyInfo structure */
 11.4354 -#define P4_EXPR     (-6)  /* P4 is a pointer to an Expr tree */
 11.4355 -#define P4_MEM      (-7)  /* P4 is a pointer to a Mem*    structure */
 11.4356 -#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
 11.4357 -#define P4_VTAB     (-8) /* P4 is a pointer to an sqlite3_vtab structure */
 11.4358 -#define P4_REAL     (-9) /* P4 is a 64-bit floating point value */
 11.4359 -#define P4_INT64    (-10) /* P4 is a 64-bit signed integer */
 11.4360 -#define P4_INT32    (-11) /* P4 is a 32-bit signed integer */
 11.4361 -#define P4_INTARRAY (-12) /* P4 is a vector of 32-bit integers */
 11.4362 -#define P4_SUBPROGRAM  (-13) /* P4 is a pointer to a SubProgram structure */
 11.4363 -#define P4_ADVANCE  (-14) /* P4 is a pointer to BtreeNext() or BtreePrev() */
 11.4364 -#define P4_TABLE    (-15) /* P4 is a pointer to a Table structure */
 11.4365 -#define P4_FUNCCTX  (-16) /* P4 is a pointer to an sqlite3_context object */
 11.4366 +#define P4_NOTUSED      0   /* The P4 parameter is not used */
 11.4367 +#define P4_TRANSIENT    0   /* P4 is a pointer to a transient string */
 11.4368 +#define P4_STATIC     (-1)  /* Pointer to a static string */
 11.4369 +#define P4_COLLSEQ    (-2)  /* P4 is a pointer to a CollSeq structure */
 11.4370 +#define P4_INT32      (-3)  /* P4 is a 32-bit signed integer */
 11.4371 +#define P4_SUBPROGRAM (-4)  /* P4 is a pointer to a SubProgram structure */
 11.4372 +#define P4_ADVANCE    (-5)  /* P4 is a pointer to BtreeNext() or BtreePrev() */
 11.4373 +#define P4_TABLE      (-6)  /* P4 is a pointer to a Table structure */
 11.4374 +/* Above do not own any resources.  Must free those below */
 11.4375 +#define P4_FREE_IF_LE (-7)
 11.4376 +#define P4_DYNAMIC    (-7)  /* Pointer to memory from sqliteMalloc() */
 11.4377 +#define P4_FUNCDEF    (-8)  /* P4 is a pointer to a FuncDef structure */
 11.4378 +#define P4_KEYINFO    (-9)  /* P4 is a pointer to a KeyInfo structure */
 11.4379 +#define P4_EXPR       (-10) /* P4 is a pointer to an Expr tree */
 11.4380 +#define P4_MEM        (-11) /* P4 is a pointer to a Mem*    structure */
 11.4381 +#define P4_VTAB       (-12) /* P4 is a pointer to an sqlite3_vtab structure */
 11.4382 +#define P4_REAL       (-13) /* P4 is a 64-bit floating point value */
 11.4383 +#define P4_INT64      (-14) /* P4 is a 64-bit signed integer */
 11.4384 +#define P4_INTARRAY   (-15) /* P4 is a vector of 32-bit integers */
 11.4385 +#define P4_FUNCCTX    (-16) /* P4 is a pointer to an sqlite3_context object */
 11.4386 +#define P4_DYNBLOB    (-17) /* Pointer to memory from sqliteMalloc() */
 11.4387  
 11.4388  /* Error message codes for OP_Halt */
 11.4389  #define P5_ConstraintNotNull 1
 11.4390 @@ -12740,169 +14792,177 @@
 11.4391  #define OP_Savepoint       0
 11.4392  #define OP_AutoCommit      1
 11.4393  #define OP_Transaction     2
 11.4394 -#define OP_SorterNext      3
 11.4395 -#define OP_PrevIfOpen      4
 11.4396 -#define OP_NextIfOpen      5
 11.4397 -#define OP_Prev            6
 11.4398 -#define OP_Next            7
 11.4399 -#define OP_Checkpoint      8
 11.4400 -#define OP_JournalMode     9
 11.4401 -#define OP_Vacuum         10
 11.4402 -#define OP_VFilter        11 /* synopsis: iplan=r[P3] zplan='P4'           */
 11.4403 -#define OP_VUpdate        12 /* synopsis: data=r[P3@P2]                    */
 11.4404 -#define OP_Goto           13
 11.4405 -#define OP_Gosub          14
 11.4406 -#define OP_InitCoroutine  15
 11.4407 -#define OP_Yield          16
 11.4408 -#define OP_MustBeInt      17
 11.4409 -#define OP_Jump           18
 11.4410 +#define OP_SorterNext      3 /* jump                                       */
 11.4411 +#define OP_Prev            4 /* jump                                       */
 11.4412 +#define OP_Next            5 /* jump                                       */
 11.4413 +#define OP_Checkpoint      6
 11.4414 +#define OP_JournalMode     7
 11.4415 +#define OP_Vacuum          8
 11.4416 +#define OP_VFilter         9 /* jump, synopsis: iplan=r[P3] zplan='P4'     */
 11.4417 +#define OP_VUpdate        10 /* synopsis: data=r[P3@P2]                    */
 11.4418 +#define OP_Goto           11 /* jump                                       */
 11.4419 +#define OP_Gosub          12 /* jump                                       */
 11.4420 +#define OP_InitCoroutine  13 /* jump                                       */
 11.4421 +#define OP_Yield          14 /* jump                                       */
 11.4422 +#define OP_MustBeInt      15 /* jump                                       */
 11.4423 +#define OP_Jump           16 /* jump                                       */
 11.4424 +#define OP_Once           17 /* jump                                       */
 11.4425 +#define OP_If             18 /* jump                                       */
 11.4426  #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
 11.4427 -#define OP_Once           20
 11.4428 -#define OP_If             21
 11.4429 -#define OP_IfNot          22
 11.4430 -#define OP_IfNullRow      23 /* synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
 11.4431 -#define OP_SeekLT         24 /* synopsis: key=r[P3@P4]                     */
 11.4432 -#define OP_SeekLE         25 /* synopsis: key=r[P3@P4]                     */
 11.4433 -#define OP_SeekGE         26 /* synopsis: key=r[P3@P4]                     */
 11.4434 -#define OP_SeekGT         27 /* synopsis: key=r[P3@P4]                     */
 11.4435 -#define OP_NoConflict     28 /* synopsis: key=r[P3@P4]                     */
 11.4436 -#define OP_NotFound       29 /* synopsis: key=r[P3@P4]                     */
 11.4437 -#define OP_Found          30 /* synopsis: key=r[P3@P4]                     */
 11.4438 -#define OP_SeekRowid      31 /* synopsis: intkey=r[P3]                     */
 11.4439 -#define OP_NotExists      32 /* synopsis: intkey=r[P3]                     */
 11.4440 -#define OP_Last           33
 11.4441 -#define OP_IfSmaller      34
 11.4442 -#define OP_SorterSort     35
 11.4443 -#define OP_Sort           36
 11.4444 -#define OP_Rewind         37
 11.4445 -#define OP_IdxLE          38 /* synopsis: key=r[P3@P4]                     */
 11.4446 -#define OP_IdxGT          39 /* synopsis: key=r[P3@P4]                     */
 11.4447 -#define OP_IdxLT          40 /* synopsis: key=r[P3@P4]                     */
 11.4448 -#define OP_IdxGE          41 /* synopsis: key=r[P3@P4]                     */
 11.4449 -#define OP_RowSetRead     42 /* synopsis: r[P3]=rowset(P1)                 */
 11.4450 -#define OP_RowSetTest     43 /* synopsis: if r[P3] in rowset(P1) goto P2   */
 11.4451 -#define OP_Program        44
 11.4452 -#define OP_FkIfZero       45 /* synopsis: if fkctr[P1]==0 goto P2          */
 11.4453 -#define OP_IfPos          46 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 11.4454 -#define OP_IfNotZero      47 /* synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
 11.4455 -#define OP_DecrJumpZero   48 /* synopsis: if (--r[P1])==0 goto P2          */
 11.4456 -#define OP_IncrVacuum     49
 11.4457 -#define OP_VNext          50
 11.4458 -#define OP_Init           51 /* synopsis: Start at P2                      */
 11.4459 -#define OP_Return         52
 11.4460 -#define OP_EndCoroutine   53
 11.4461 -#define OP_HaltIfNull     54 /* synopsis: if r[P3]=null halt               */
 11.4462 -#define OP_Halt           55
 11.4463 -#define OP_Integer        56 /* synopsis: r[P2]=P1                         */
 11.4464 -#define OP_Int64          57 /* synopsis: r[P2]=P4                         */
 11.4465 -#define OP_String         58 /* synopsis: r[P2]='P4' (len=P1)              */
 11.4466 -#define OP_Null           59 /* synopsis: r[P2..P3]=NULL                   */
 11.4467 -#define OP_SoftNull       60 /* synopsis: r[P1]=NULL                       */
 11.4468 -#define OP_Blob           61 /* synopsis: r[P2]=P4 (len=P1)                */
 11.4469 -#define OP_Variable       62 /* synopsis: r[P2]=parameter(P1,P4)           */
 11.4470 -#define OP_Move           63 /* synopsis: r[P2@P3]=r[P1@P3]                */
 11.4471 -#define OP_Copy           64 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 11.4472 -#define OP_SCopy          65 /* synopsis: r[P2]=r[P1]                      */
 11.4473 -#define OP_IntCopy        66 /* synopsis: r[P2]=r[P1]                      */
 11.4474 -#define OP_ResultRow      67 /* synopsis: output=r[P1@P2]                  */
 11.4475 -#define OP_CollSeq        68
 11.4476 -#define OP_Function0      69 /* synopsis: r[P3]=func(r[P2@P5])             */
 11.4477 -#define OP_Or             70 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
 11.4478 -#define OP_And            71 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
 11.4479 -#define OP_Function       72 /* synopsis: r[P3]=func(r[P2@P5])             */
 11.4480 -#define OP_AddImm         73 /* synopsis: r[P1]=r[P1]+P2                   */
 11.4481 -#define OP_RealAffinity   74
 11.4482 -#define OP_IsNull         75 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
 11.4483 -#define OP_NotNull        76 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
 11.4484 -#define OP_Ne             77 /* same as TK_NE, synopsis: IF r[P3]!=r[P1]   */
 11.4485 -#define OP_Eq             78 /* same as TK_EQ, synopsis: IF r[P3]==r[P1]   */
 11.4486 -#define OP_Gt             79 /* same as TK_GT, synopsis: IF r[P3]>r[P1]    */
 11.4487 -#define OP_Le             80 /* same as TK_LE, synopsis: IF r[P3]<=r[P1]   */
 11.4488 -#define OP_Lt             81 /* same as TK_LT, synopsis: IF r[P3]<r[P1]    */
 11.4489 -#define OP_Ge             82 /* same as TK_GE, synopsis: IF r[P3]>=r[P1]   */
 11.4490 -#define OP_ElseNotEq      83 /* same as TK_ESCAPE                          */
 11.4491 -#define OP_BitAnd         84 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 11.4492 -#define OP_BitOr          85 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 11.4493 -#define OP_ShiftLeft      86 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 11.4494 -#define OP_ShiftRight     87 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 11.4495 -#define OP_Add            88 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 11.4496 -#define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 11.4497 -#define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 11.4498 -#define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 11.4499 -#define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 11.4500 -#define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 11.4501 -#define OP_Cast           94 /* synopsis: affinity(r[P1])                  */
 11.4502 -#define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 11.4503 -#define OP_Permutation    96
 11.4504 -#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 11.4505 -#define OP_Compare        98 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 11.4506 -#define OP_Column         99 /* synopsis: r[P3]=PX                         */
 11.4507 -#define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
 11.4508 -#define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 11.4509 -#define OP_Count         102 /* synopsis: r[P2]=count()                    */
 11.4510 -#define OP_ReadCookie    103
 11.4511 -#define OP_SetCookie     104
 11.4512 -#define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
 11.4513 -#define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
 11.4514 -#define OP_OpenWrite     107 /* synopsis: root=P2 iDb=P3                   */
 11.4515 -#define OP_OpenDup       108
 11.4516 -#define OP_OpenAutoindex 109 /* synopsis: nColumn=P2                       */
 11.4517 -#define OP_OpenEphemeral 110 /* synopsis: nColumn=P2                       */
 11.4518 -#define OP_SorterOpen    111
 11.4519 -#define OP_SequenceTest  112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 11.4520 -#define OP_OpenPseudo    113 /* synopsis: P3 columns in r[P2]              */
 11.4521 -#define OP_Close         114
 11.4522 -#define OP_ColumnsUsed   115
 11.4523 -#define OP_Sequence      116 /* synopsis: r[P2]=cursor[P1].ctr++           */
 11.4524 -#define OP_NewRowid      117 /* synopsis: r[P2]=rowid                      */
 11.4525 -#define OP_Insert        118 /* synopsis: intkey=r[P3] data=r[P2]          */
 11.4526 -#define OP_InsertInt     119 /* synopsis: intkey=P3 data=r[P2]             */
 11.4527 -#define OP_Delete        120
 11.4528 -#define OP_ResetCount    121
 11.4529 -#define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 11.4530 -#define OP_SorterData    123 /* synopsis: r[P2]=data                       */
 11.4531 -#define OP_RowData       124 /* synopsis: r[P2]=data                       */
 11.4532 -#define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
 11.4533 -#define OP_NullRow       126
 11.4534 -#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
 11.4535 -#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
 11.4536 -#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
 11.4537 -#define OP_Seek          130 /* synopsis: Move P3 to P1.rowid              */
 11.4538 -#define OP_IdxRowid      131 /* synopsis: r[P2]=rowid                      */
 11.4539 -#define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 11.4540 -#define OP_Destroy       133
 11.4541 -#define OP_Clear         134
 11.4542 -#define OP_ResetSorter   135
 11.4543 -#define OP_CreateIndex   136 /* synopsis: r[P2]=root iDb=P1                */
 11.4544 -#define OP_CreateTable   137 /* synopsis: r[P2]=root iDb=P1                */
 11.4545 -#define OP_SqlExec       138
 11.4546 -#define OP_ParseSchema   139
 11.4547 -#define OP_LoadAnalysis  140
 11.4548 -#define OP_DropTable     141
 11.4549 -#define OP_DropIndex     142
 11.4550 -#define OP_DropTrigger   143
 11.4551 -#define OP_IntegrityCk   144
 11.4552 -#define OP_RowSetAdd     145 /* synopsis: rowset(P1)=r[P2]                 */
 11.4553 -#define OP_Param         146
 11.4554 -#define OP_FkCounter     147 /* synopsis: fkctr[P1]+=P2                    */
 11.4555 -#define OP_MemMax        148 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 11.4556 -#define OP_OffsetLimit   149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 11.4557 -#define OP_AggStep0      150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 11.4558 -#define OP_AggStep       151 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 11.4559 -#define OP_AggFinal      152 /* synopsis: accum=r[P1] N=P2                 */
 11.4560 -#define OP_Expire        153
 11.4561 -#define OP_TableLock     154 /* synopsis: iDb=P1 root=P2 write=P3          */
 11.4562 -#define OP_VBegin        155
 11.4563 -#define OP_VCreate       156
 11.4564 -#define OP_VDestroy      157
 11.4565 -#define OP_VOpen         158
 11.4566 -#define OP_VColumn       159 /* synopsis: r[P3]=vcolumn(P2)                */
 11.4567 -#define OP_VRename       160
 11.4568 -#define OP_Pagecount     161
 11.4569 -#define OP_MaxPgcnt      162
 11.4570 -#define OP_CursorHint    163
 11.4571 -#define OP_Noop          164
 11.4572 -#define OP_Explain       165
 11.4573 +#define OP_IfNot          20 /* jump                                       */
 11.4574 +#define OP_IfNullRow      21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
 11.4575 +#define OP_SeekLT         22 /* jump, synopsis: key=r[P3@P4]               */
 11.4576 +#define OP_SeekLE         23 /* jump, synopsis: key=r[P3@P4]               */
 11.4577 +#define OP_SeekGE         24 /* jump, synopsis: key=r[P3@P4]               */
 11.4578 +#define OP_SeekGT         25 /* jump, synopsis: key=r[P3@P4]               */
 11.4579 +#define OP_IfNoHope       26 /* jump, synopsis: key=r[P3@P4]               */
 11.4580 +#define OP_NoConflict     27 /* jump, synopsis: key=r[P3@P4]               */
 11.4581 +#define OP_NotFound       28 /* jump, synopsis: key=r[P3@P4]               */
 11.4582 +#define OP_Found          29 /* jump, synopsis: key=r[P3@P4]               */
 11.4583 +#define OP_SeekRowid      30 /* jump, synopsis: intkey=r[P3]               */
 11.4584 +#define OP_NotExists      31 /* jump, synopsis: intkey=r[P3]               */
 11.4585 +#define OP_Last           32 /* jump                                       */
 11.4586 +#define OP_IfSmaller      33 /* jump                                       */
 11.4587 +#define OP_SorterSort     34 /* jump                                       */
 11.4588 +#define OP_Sort           35 /* jump                                       */
 11.4589 +#define OP_Rewind         36 /* jump                                       */
 11.4590 +#define OP_IdxLE          37 /* jump, synopsis: key=r[P3@P4]               */
 11.4591 +#define OP_IdxGT          38 /* jump, synopsis: key=r[P3@P4]               */
 11.4592 +#define OP_IdxLT          39 /* jump, synopsis: key=r[P3@P4]               */
 11.4593 +#define OP_IdxGE          40 /* jump, synopsis: key=r[P3@P4]               */
 11.4594 +#define OP_RowSetRead     41 /* jump, synopsis: r[P3]=rowset(P1)           */
 11.4595 +#define OP_RowSetTest     42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
 11.4596 +#define OP_Or             43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
 11.4597 +#define OP_And            44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
 11.4598 +#define OP_Program        45 /* jump                                       */
 11.4599 +#define OP_FkIfZero       46 /* jump, synopsis: if fkctr[P1]==0 goto P2    */
 11.4600 +#define OP_IfPos          47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 11.4601 +#define OP_IfNotZero      48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
 11.4602 +#define OP_DecrJumpZero   49 /* jump, synopsis: if (--r[P1])==0 goto P2    */
 11.4603 +#define OP_IsNull         50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
 11.4604 +#define OP_NotNull        51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
 11.4605 +#define OP_Ne             52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
 11.4606 +#define OP_Eq             53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
 11.4607 +#define OP_Gt             54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
 11.4608 +#define OP_Le             55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
 11.4609 +#define OP_Lt             56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
 11.4610 +#define OP_Ge             57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
 11.4611 +#define OP_ElseNotEq      58 /* jump, same as TK_ESCAPE                    */
 11.4612 +#define OP_IncrVacuum     59 /* jump                                       */
 11.4613 +#define OP_VNext          60 /* jump                                       */
 11.4614 +#define OP_Init           61 /* jump, synopsis: Start at P2                */
 11.4615 +#define OP_PureFunc0      62
 11.4616 +#define OP_Function0      63 /* synopsis: r[P3]=func(r[P2@P5])             */
 11.4617 +#define OP_PureFunc       64
 11.4618 +#define OP_Function       65 /* synopsis: r[P3]=func(r[P2@P5])             */
 11.4619 +#define OP_Return         66
 11.4620 +#define OP_EndCoroutine   67
 11.4621 +#define OP_HaltIfNull     68 /* synopsis: if r[P3]=null halt               */
 11.4622 +#define OP_Halt           69
 11.4623 +#define OP_Integer        70 /* synopsis: r[P2]=P1                         */
 11.4624 +#define OP_Int64          71 /* synopsis: r[P2]=P4                         */
 11.4625 +#define OP_String         72 /* synopsis: r[P2]='P4' (len=P1)              */
 11.4626 +#define OP_Null           73 /* synopsis: r[P2..P3]=NULL                   */
 11.4627 +#define OP_SoftNull       74 /* synopsis: r[P1]=NULL                       */
 11.4628 +#define OP_Blob           75 /* synopsis: r[P2]=P4 (len=P1)                */
 11.4629 +#define OP_Variable       76 /* synopsis: r[P2]=parameter(P1,P4)           */
 11.4630 +#define OP_Move           77 /* synopsis: r[P2@P3]=r[P1@P3]                */
 11.4631 +#define OP_Copy           78 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 11.4632 +#define OP_SCopy          79 /* synopsis: r[P2]=r[P1]                      */
 11.4633 +#define OP_IntCopy        80 /* synopsis: r[P2]=r[P1]                      */
 11.4634 +#define OP_ResultRow      81 /* synopsis: output=r[P1@P2]                  */
 11.4635 +#define OP_CollSeq        82
 11.4636 +#define OP_AddImm         83 /* synopsis: r[P1]=r[P1]+P2                   */
 11.4637 +#define OP_RealAffinity   84
 11.4638 +#define OP_Cast           85 /* synopsis: affinity(r[P1])                  */
 11.4639 +#define OP_Permutation    86
 11.4640 +#define OP_Compare        87 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 11.4641 +#define OP_IsTrue         88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 11.4642 +#define OP_Offset         89 /* synopsis: r[P3] = sqlite_offset(P1)        */
 11.4643 +#define OP_Column         90 /* synopsis: r[P3]=PX                         */
 11.4644 +#define OP_Affinity       91 /* synopsis: affinity(r[P1@P2])               */
 11.4645 +#define OP_BitAnd         92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 11.4646 +#define OP_BitOr          93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 11.4647 +#define OP_ShiftLeft      94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 11.4648 +#define OP_ShiftRight     95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 11.4649 +#define OP_Add            96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 11.4650 +#define OP_Subtract       97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 11.4651 +#define OP_Multiply       98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 11.4652 +#define OP_Divide         99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 11.4653 +#define OP_Remainder     100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 11.4654 +#define OP_Concat        101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 11.4655 +#define OP_MakeRecord    102 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 11.4656 +#define OP_BitNot        103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
 11.4657 +#define OP_Count         104 /* synopsis: r[P2]=count()                    */
 11.4658 +#define OP_ReadCookie    105
 11.4659 +#define OP_String8       106 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 11.4660 +#define OP_SetCookie     107
 11.4661 +#define OP_ReopenIdx     108 /* synopsis: root=P2 iDb=P3                   */
 11.4662 +#define OP_OpenRead      109 /* synopsis: root=P2 iDb=P3                   */
 11.4663 +#define OP_OpenWrite     110 /* synopsis: root=P2 iDb=P3                   */
 11.4664 +#define OP_OpenDup       111
 11.4665 +#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2                       */
 11.4666 +#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2                       */
 11.4667 +#define OP_SorterOpen    114
 11.4668 +#define OP_SequenceTest  115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 11.4669 +#define OP_OpenPseudo    116 /* synopsis: P3 columns in r[P2]              */
 11.4670 +#define OP_Close         117
 11.4671 +#define OP_ColumnsUsed   118
 11.4672 +#define OP_SeekHit       119 /* synopsis: seekHit=P2                       */
 11.4673 +#define OP_Sequence      120 /* synopsis: r[P2]=cursor[P1].ctr++           */
 11.4674 +#define OP_NewRowid      121 /* synopsis: r[P2]=rowid                      */
 11.4675 +#define OP_Insert        122 /* synopsis: intkey=r[P3] data=r[P2]          */
 11.4676 +#define OP_InsertInt     123 /* synopsis: intkey=P3 data=r[P2]             */
 11.4677 +#define OP_Delete        124
 11.4678 +#define OP_ResetCount    125
 11.4679 +#define OP_SorterCompare 126 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 11.4680 +#define OP_SorterData    127 /* synopsis: r[P2]=data                       */
 11.4681 +#define OP_RowData       128 /* synopsis: r[P2]=data                       */
 11.4682 +#define OP_Rowid         129 /* synopsis: r[P2]=rowid                      */
 11.4683 +#define OP_NullRow       130
 11.4684 +#define OP_SeekEnd       131
 11.4685 +#define OP_SorterInsert  132 /* synopsis: key=r[P2]                        */
 11.4686 +#define OP_IdxInsert     133 /* synopsis: key=r[P2]                        */
 11.4687 +#define OP_IdxDelete     134 /* synopsis: key=r[P2@P3]                     */
 11.4688 +#define OP_DeferredSeek  135 /* synopsis: Move P3 to P1.rowid if needed    */
 11.4689 +#define OP_IdxRowid      136 /* synopsis: r[P2]=rowid                      */
 11.4690 +#define OP_Destroy       137
 11.4691 +#define OP_Clear         138
 11.4692 +#define OP_ResetSorter   139
 11.4693 +#define OP_CreateBtree   140 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 11.4694 +#define OP_Real          141 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 11.4695 +#define OP_SqlExec       142
 11.4696 +#define OP_ParseSchema   143
 11.4697 +#define OP_LoadAnalysis  144
 11.4698 +#define OP_DropTable     145
 11.4699 +#define OP_DropIndex     146
 11.4700 +#define OP_DropTrigger   147
 11.4701 +#define OP_IntegrityCk   148
 11.4702 +#define OP_RowSetAdd     149 /* synopsis: rowset(P1)=r[P2]                 */
 11.4703 +#define OP_Param         150
 11.4704 +#define OP_FkCounter     151 /* synopsis: fkctr[P1]+=P2                    */
 11.4705 +#define OP_MemMax        152 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 11.4706 +#define OP_OffsetLimit   153 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 11.4707 +#define OP_AggInverse    154 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
 11.4708 +#define OP_AggStep       155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 11.4709 +#define OP_AggStep1      156 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 11.4710 +#define OP_AggValue      157 /* synopsis: r[P3]=value N=P2                 */
 11.4711 +#define OP_AggFinal      158 /* synopsis: accum=r[P1] N=P2                 */
 11.4712 +#define OP_Expire        159
 11.4713 +#define OP_TableLock     160 /* synopsis: iDb=P1 root=P2 write=P3          */
 11.4714 +#define OP_VBegin        161
 11.4715 +#define OP_VCreate       162
 11.4716 +#define OP_VDestroy      163
 11.4717 +#define OP_VOpen         164
 11.4718 +#define OP_VColumn       165 /* synopsis: r[P3]=vcolumn(P2)                */
 11.4719 +#define OP_VRename       166
 11.4720 +#define OP_Pagecount     167
 11.4721 +#define OP_MaxPgcnt      168
 11.4722 +#define OP_Trace         169
 11.4723 +#define OP_CursorHint    170
 11.4724 +#define OP_Noop          171
 11.4725 +#define OP_Explain       172
 11.4726 +#define OP_Abortable     173
 11.4727  
 11.4728  /* Properties such as "out2" or "jump" that are specified in
 11.4729  ** comments following the "case" for each opcode in the vdbe.c
 11.4730 @@ -12915,27 +14975,28 @@
 11.4731  #define OPFLG_OUT2        0x10  /* out2:  P2 is an output */
 11.4732  #define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
 11.4733  #define OPFLG_INITIALIZER {\
 11.4734 -/*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
 11.4735 -/*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
 11.4736 -/*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x01,\
 11.4737 +/*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
 11.4738 +/*   8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
 11.4739 +/*  16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
 11.4740  /*  24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
 11.4741 -/*  32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 11.4742 -/*  40 */ 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01, 0x03, 0x03,\
 11.4743 -/*  48 */ 0x03, 0x01, 0x01, 0x01, 0x02, 0x02, 0x08, 0x00,\
 11.4744 -/*  56 */ 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00,\
 11.4745 -/*  64 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x26, 0x26,\
 11.4746 -/*  72 */ 0x00, 0x02, 0x02, 0x03, 0x03, 0x0b, 0x0b, 0x0b,\
 11.4747 -/*  80 */ 0x0b, 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26,\
 11.4748 -/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x02, 0x12,\
 11.4749 -/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
 11.4750 -/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4751 -/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 11.4752 -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04,\
 11.4753 -/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 11.4754 -/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4755 -/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 11.4756 -/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4757 -/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,}
 11.4758 +/*  32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 11.4759 +/*  40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
 11.4760 +/*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 11.4761 +/*  56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
 11.4762 +/*  64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
 11.4763 +/*  72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
 11.4764 +/*  80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
 11.4765 +/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
 11.4766 +/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 11.4767 +/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4768 +/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4769 +/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4770 +/* 128 */ 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00,\
 11.4771 +/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 11.4772 +/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
 11.4773 +/* 152 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 11.4774 +/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 11.4775 +/* 168 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,}
 11.4776  
 11.4777  /* The sqlite3P2Values() routine is able to run faster if it knows
 11.4778  ** the value of the largest JUMP opcode.  The smaller the maximum
 11.4779 @@ -12943,12 +15004,18 @@
 11.4780  ** generated this include file strives to group all JUMP opcodes
 11.4781  ** together near the beginning of the list.
 11.4782  */
 11.4783 -#define SQLITE_MX_JUMP_OPCODE  83  /* Maximum JUMP opcode */
 11.4784 +#define SQLITE_MX_JUMP_OPCODE  61  /* Maximum JUMP opcode */
 11.4785  
 11.4786  /************** End of opcodes.h *********************************************/
 11.4787  /************** Continuing where we left off in vdbe.h ***********************/
 11.4788  
 11.4789  /*
 11.4790 +** Additional non-public SQLITE_PREPARE_* flags
 11.4791 +*/
 11.4792 +#define SQLITE_PREPARE_SAVESQL  0x80  /* Preserve SQL text */
 11.4793 +#define SQLITE_PREPARE_MASK     0x0f  /* Mask of public flags */
 11.4794 +
 11.4795 +/*
 11.4796  ** Prototypes for the VDBE interface.  See comments on the implementation
 11.4797  ** for a description of what each of these routines does.
 11.4798  */
 11.4799 @@ -12971,7 +15038,24 @@
 11.4800  # define sqlite3VdbeVerifyNoMallocRequired(A,B)
 11.4801  # define sqlite3VdbeVerifyNoResultRow(A)
 11.4802  #endif
 11.4803 -SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 11.4804 +#if defined(SQLITE_DEBUG)
 11.4805 +SQLITE_PRIVATE   void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
 11.4806 +#else
 11.4807 +# define sqlite3VdbeVerifyAbortable(A,B)
 11.4808 +#endif
 11.4809 +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
 11.4810 +#ifndef SQLITE_OMIT_EXPLAIN
 11.4811 +SQLITE_PRIVATE   void sqlite3VdbeExplain(Parse*,u8,const char*,...);
 11.4812 +SQLITE_PRIVATE   void sqlite3VdbeExplainPop(Parse*);
 11.4813 +SQLITE_PRIVATE   int sqlite3VdbeExplainParent(Parse*);
 11.4814 +# define ExplainQueryPlan(P)        sqlite3VdbeExplain P
 11.4815 +# define ExplainQueryPlanPop(P)     sqlite3VdbeExplainPop(P)
 11.4816 +# define ExplainQueryPlanParent(P)  sqlite3VdbeExplainParent(P)
 11.4817 +#else
 11.4818 +# define ExplainQueryPlan(P)
 11.4819 +# define ExplainQueryPlanPop(P)
 11.4820 +# define ExplainQueryPlanParent(P) 0
 11.4821 +#endif
 11.4822  SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 11.4823  SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 11.4824  SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 11.4825 @@ -13005,7 +15089,8 @@
 11.4826  SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
 11.4827  SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
 11.4828  SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
 11.4829 -SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
 11.4830 +SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
 11.4831 +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
 11.4832  SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
 11.4833  SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
 11.4834  SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
 11.4835 @@ -13014,6 +15099,7 @@
 11.4836  SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
 11.4837  #endif
 11.4838  SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 11.4839 +SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
 11.4840  
 11.4841  SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
 11.4842  SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
 11.4843 @@ -13027,6 +15113,8 @@
 11.4844  SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
 11.4845  #endif
 11.4846  
 11.4847 +SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
 11.4848 +
 11.4849  /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
 11.4850  ** each VDBE opcode.
 11.4851  **
 11.4852 @@ -13067,23 +15155,52 @@
 11.4853  **
 11.4854  **    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
 11.4855  **
 11.4856 +**    VdbeCoverageNeverNull(v)         // Previous three-way branch is only
 11.4857 +**                                     // taken on the first two ways.  The
 11.4858 +**                                     // NULL option is not possible
 11.4859 +**
 11.4860 +**    VdbeCoverageEqNe(v)              // Previous OP_Jump is only interested
 11.4861 +**                                     // in distingishing equal and not-equal.
 11.4862 +**
 11.4863  ** Every VDBE branch operation must be tagged with one of the macros above.
 11.4864  ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
 11.4865  ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
 11.4866  ** routine in vdbe.c, alerting the developer to the missed tag.
 11.4867 +**
 11.4868 +** During testing, the test application will invoke
 11.4869 +** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
 11.4870 +** routine that is invoked as each bytecode branch is taken.  The callback
 11.4871 +** contains the sqlite3.c source line number ov the VdbeCoverage macro and
 11.4872 +** flags to indicate whether or not the branch was taken.  The test application
 11.4873 +** is responsible for keeping track of this and reporting byte-code branches
 11.4874 +** that are never taken.
 11.4875 +**
 11.4876 +** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
 11.4877 +** vdbe.c source file for additional information.
 11.4878  */
 11.4879  #ifdef SQLITE_VDBE_COVERAGE
 11.4880  SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
 11.4881  # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
 11.4882  # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
 11.4883 -# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
 11.4884 -# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
 11.4885 +# define VdbeCoverageAlwaysTaken(v) \
 11.4886 +         sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
 11.4887 +# define VdbeCoverageNeverTaken(v) \
 11.4888 +         sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
 11.4889 +# define VdbeCoverageNeverNull(v) \
 11.4890 +         sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
 11.4891 +# define VdbeCoverageNeverNullIf(v,x) \
 11.4892 +         if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
 11.4893 +# define VdbeCoverageEqNe(v) \
 11.4894 +         sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
 11.4895  # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
 11.4896  #else
 11.4897  # define VdbeCoverage(v)
 11.4898  # define VdbeCoverageIf(v,x)
 11.4899  # define VdbeCoverageAlwaysTaken(v)
 11.4900  # define VdbeCoverageNeverTaken(v)
 11.4901 +# define VdbeCoverageNeverNull(v)
 11.4902 +# define VdbeCoverageNeverNullIf(v,x)
 11.4903 +# define VdbeCoverageEqNe(v)
 11.4904  # define VDBE_OFFSET_LINENO(x) 0
 11.4905  #endif
 11.4906  
 11.4907 @@ -13093,6 +15210,10 @@
 11.4908  # define sqlite3VdbeScanStatus(a,b,c,d,e)
 11.4909  #endif
 11.4910  
 11.4911 +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 11.4912 +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
 11.4913 +#endif
 11.4914 +
 11.4915  #endif /* SQLITE_VDBE_H */
 11.4916  
 11.4917  /************** End of vdbe.h ************************************************/
 11.4918 @@ -13227,7 +15348,7 @@
 11.4919  SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 11.4920  
 11.4921  /* Functions used to configure a Pager object. */
 11.4922 -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
 11.4923 +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
 11.4924  SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
 11.4925  #ifdef SQLITE_HAS_CODEC
 11.4926  SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
 11.4927 @@ -13252,6 +15373,7 @@
 11.4928  SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
 11.4929  SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
 11.4930  SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
 11.4931 +SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
 11.4932  
 11.4933  /* Operations on page references. */
 11.4934  SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
 11.4935 @@ -13279,16 +15401,17 @@
 11.4936  SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 11.4937  SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 11.4938  SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 11.4939 -# ifdef SQLITE_DIRECT_OVERFLOW_READ
 11.4940 -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
 11.4941 -# endif
 11.4942  # ifdef SQLITE_ENABLE_SNAPSHOT
 11.4943  SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 11.4944  SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 11.4945  SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 11.4946 +SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
 11.4947 +SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
 11.4948  # endif
 11.4949 -#else
 11.4950 -# define sqlite3PagerUseWal(x,y) 0
 11.4951 +#endif
 11.4952 +
 11.4953 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 11.4954 +SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
 11.4955  #endif
 11.4956  
 11.4957  #ifdef SQLITE_ENABLE_ZIPVFS
 11.4958 @@ -13312,6 +15435,11 @@
 11.4959  SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
 11.4960  SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
 11.4961  SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
 11.4962 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
 11.4963 +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
 11.4964 +#else
 11.4965 +# define sqlite3PagerResetLockTimeout(X)
 11.4966 +#endif
 11.4967  
 11.4968  /* Functions used to truncate the database file. */
 11.4969  SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 11.4970 @@ -13371,6 +15499,7 @@
 11.4971    sqlite3_pcache_page *pPage;    /* Pcache object page handle */
 11.4972    void *pData;                   /* Page data */
 11.4973    void *pExtra;                  /* Extra content */
 11.4974 +  PCache *pCache;                /* PRIVATE: Cache that owns this page */
 11.4975    PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
 11.4976    Pager *pPager;                 /* The pager this page is part of */
 11.4977    Pgno pgno;                     /* Page number for this page */
 11.4978 @@ -13380,14 +15509,15 @@
 11.4979    u16 flags;                     /* PGHDR flags defined below */
 11.4980  
 11.4981    /**********************************************************************
 11.4982 -  ** Elements above are public.  All that follows is private to pcache.c
 11.4983 -  ** and should not be accessed by other modules.
 11.4984 +  ** Elements above, except pCache, are public.  All that follow are 
 11.4985 +  ** private to pcache.c and should not be accessed by other modules.
 11.4986 +  ** pCache is grouped with the public elements for efficiency.
 11.4987    */
 11.4988    i16 nRef;                      /* Number of users of this page */
 11.4989 -  PCache *pCache;                /* Cache that owns this page */
 11.4990 -
 11.4991    PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
 11.4992    PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
 11.4993 +                          /* NB: pDirtyNext and pDirtyPrev are undefined if the
 11.4994 +                          ** PgHdr object is not dirty */
 11.4995  };
 11.4996  
 11.4997  /* Bit values for PgHdr.flags */
 11.4998 @@ -13526,6 +15656,10 @@
 11.4999  /* Number of dirty pages as a percentage of the configured cache size */
 11.5000  SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
 11.5001  
 11.5002 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 11.5003 +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
 11.5004 +#endif
 11.5005 +
 11.5006  #endif /* _PCACHE_H_ */
 11.5007  
 11.5008  /************** End of pcache.h **********************************************/
 11.5009 @@ -13769,10 +15903,12 @@
 11.5010  #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
 11.5011  SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
 11.5012  SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
 11.5013 +#ifndef SQLITE_OMIT_WAL
 11.5014  SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
 11.5015  SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
 11.5016  SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
 11.5017  SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
 11.5018 +#endif /* SQLITE_OMIT_WAL */
 11.5019  SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
 11.5020  SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
 11.5021  
 11.5022 @@ -13981,6 +16117,7 @@
 11.5023  #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
 11.5024  #define DB_UnresetViews    0x0002  /* Some views have defined column names */
 11.5025  #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
 11.5026 +#define DB_ResetWanted     0x0008  /* Reset the schema when nSchemaLock==0 */
 11.5027  
 11.5028  /*
 11.5029  ** The number of different kinds of things that can be limited
 11.5030 @@ -14012,9 +16149,9 @@
 11.5031    u32 bDisable;           /* Only operate the lookaside when zero */
 11.5032    u16 sz;                 /* Size of each buffer in bytes */
 11.5033    u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
 11.5034 -  int nOut;               /* Number of buffers currently checked out */
 11.5035 -  int mxOut;              /* Highwater mark for nOut */
 11.5036 -  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
 11.5037 +  u32 nSlot;              /* Number of lookaside slots allocated */
 11.5038 +  u32 anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
 11.5039 +  LookasideSlot *pInit;   /* List of buffers not previously used */
 11.5040    LookasideSlot *pFree;   /* List of available buffers */
 11.5041    void *pStart;           /* First byte of available memory space */
 11.5042    void *pEnd;             /* First byte past end of available space */
 11.5043 @@ -14028,12 +16165,14 @@
 11.5044  ** functions use a regular table table from hash.h.)
 11.5045  **
 11.5046  ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
 11.5047 -** Collisions are on the FuncDef.u.pHash chain.
 11.5048 +** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
 11.5049 +** macro to compute a hash on the function name.
 11.5050  */
 11.5051  #define SQLITE_FUNC_HASH_SZ 23
 11.5052  struct FuncDefHash {
 11.5053    FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
 11.5054  };
 11.5055 +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
 11.5056  
 11.5057  #ifdef SQLITE_USER_AUTHENTICATION
 11.5058  /*
 11.5059 @@ -14093,9 +16232,11 @@
 11.5060    sqlite3_mutex *mutex;         /* Connection mutex */
 11.5061    Db *aDb;                      /* All backends */
 11.5062    int nDb;                      /* Number of backends currently in use */
 11.5063 -  int flags;                    /* Miscellaneous flags. See below */
 11.5064 +  u32 mDbFlags;                 /* flags recording internal state */
 11.5065 +  u64 flags;                    /* flags settable by pragmas. See below */
 11.5066    i64 lastRowid;                /* ROWID of most recent insert (see above) */
 11.5067    i64 szMmap;                   /* Default mmap_size setting */
 11.5068 +  u32 nSchemaLock;              /* Do not reset the schema when non-zero */
 11.5069    unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 11.5070    int errCode;                  /* Most recent error code (SQLITE_*) */
 11.5071    int errMask;                  /* & result codes with this before returning */
 11.5072 @@ -14112,7 +16253,7 @@
 11.5073    u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 11.5074    u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 11.5075    u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
 11.5076 -  u8 skipBtreeMutex;            /* True if no shared-cache backends */
 11.5077 +  u8 noSharedCache;             /* True if no shared-cache backends */
 11.5078    u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
 11.5079    int nextPagesize;             /* Pagesize after VACUUM if >0 */
 11.5080    u32 magic;                    /* Magic number for detect library misuse */
 11.5081 @@ -14124,8 +16265,9 @@
 11.5082      int newTnum;                /* Rootpage of table being initialized */
 11.5083      u8 iDb;                     /* Which db file is being initialized */
 11.5084      u8 busy;                    /* TRUE if currently initializing */
 11.5085 -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 11.5086 -    u8 imposterTable;           /* Building an imposter table */
 11.5087 +    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
 11.5088 +    unsigned imposterTable : 1; /* Building an imposter table */
 11.5089 +    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
 11.5090    } init;
 11.5091    int nVdbeActive;              /* Number of VDBEs currently running */
 11.5092    int nVdbeRead;                /* Number of active VDBEs that read or write */
 11.5093 @@ -14178,7 +16320,7 @@
 11.5094    Hash aModule;                 /* populated by sqlite3_create_module() */
 11.5095    VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
 11.5096    VTable **aVTrans;             /* Virtual tables with open transactions */
 11.5097 -  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
 11.5098 +  VTable *pDisconnect;          /* Disconnect these in next sqlite3_prepare() */
 11.5099  #endif
 11.5100    Hash aFunc;                   /* Hash table of connection functions */
 11.5101    Hash aCollSeq;                /* All collating sequences */
 11.5102 @@ -14227,8 +16369,8 @@
 11.5103  **      SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
 11.5104  **      SQLITE_CacheSpill    == PAGER_CACHE_SPILL
 11.5105  */
 11.5106 -#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
 11.5107 -#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
 11.5108 +#define SQLITE_WriteSchema    0x00000001  /* OK to update SQLITE_MASTER */
 11.5109 +#define SQLITE_LegacyFileFmt  0x00000002  /* Create new databases in format 1 */
 11.5110  #define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
 11.5111  #define SQLITE_FullFSync      0x00000008  /* Use full fsync on the backend */
 11.5112  #define SQLITE_CkptFullFSync  0x00000010  /* Use full fsync for checkpoint */
 11.5113 @@ -14239,30 +16381,44 @@
 11.5114                                            /*   the count using a callback. */
 11.5115  #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
 11.5116                                            /*   result set is empty */
 11.5117 -#define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
 11.5118 -#define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
 11.5119 -#define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
 11.5120 -#define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
 11.5121 -#define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
 11.5122 -#define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
 11.5123 -#define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
 11.5124 -#define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
 11.5125 -#define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
 11.5126 -#define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
 11.5127 -#define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
 11.5128 -#define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
 11.5129 -#define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
 11.5130 -#define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
 11.5131 -#define SQLITE_LoadExtFunc    0x00800000  /* Enable load_extension() SQL func */
 11.5132 -#define SQLITE_EnableTrigger  0x01000000  /* True to enable triggers */
 11.5133 -#define SQLITE_DeferFKs       0x02000000  /* Defer all FK constraints */
 11.5134 -#define SQLITE_QueryOnly      0x04000000  /* Disable database changes */
 11.5135 -#define SQLITE_VdbeEQP        0x08000000  /* Debug EXPLAIN QUERY PLAN */
 11.5136 -#define SQLITE_Vacuum         0x10000000  /* Currently in a VACUUM */
 11.5137 -#define SQLITE_CellSizeCk     0x20000000  /* Check btree cell sizes on load */
 11.5138 -#define SQLITE_Fts3Tokenizer  0x40000000  /* Enable fts3_tokenizer(2) */
 11.5139 -#define SQLITE_NoCkptOnClose  0x80000000  /* No checkpoint on close()/DETACH */
 11.5140 -
 11.5141 +#define SQLITE_IgnoreChecks   0x00000200  /* Do not enforce check constraints */
 11.5142 +#define SQLITE_ReadUncommit   0x00000400  /* READ UNCOMMITTED in shared-cache */
 11.5143 +#define SQLITE_NoCkptOnClose  0x00000800  /* No checkpoint on close()/DETACH */
 11.5144 +#define SQLITE_ReverseOrder   0x00001000  /* Reverse unordered SELECTs */
 11.5145 +#define SQLITE_RecTriggers    0x00002000  /* Enable recursive triggers */
 11.5146 +#define SQLITE_ForeignKeys    0x00004000  /* Enforce foreign key constraints  */
 11.5147 +#define SQLITE_AutoIndex      0x00008000  /* Enable automatic indexes */
 11.5148 +#define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
 11.5149 +#define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
 11.5150 +#define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
 11.5151 +#define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 11.5152 +#define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 11.5153 +#define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 11.5154 +#define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 11.5155 +#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 11.5156 +#define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
 11.5157 +#define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
 11.5158 +#define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
 11.5159 +#define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
 11.5160 +#define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
 11.5161 +
 11.5162 +/* Flags used only if debugging */
 11.5163 +#define HI(X)  ((u64)(X)<<32)
 11.5164 +#ifdef SQLITE_DEBUG
 11.5165 +#define SQLITE_SqlTrace       HI(0x0001)  /* Debug print SQL as it executes */
 11.5166 +#define SQLITE_VdbeListing    HI(0x0002)  /* Debug listings of VDBE progs */
 11.5167 +#define SQLITE_VdbeTrace      HI(0x0004)  /* True to trace VDBE execution */
 11.5168 +#define SQLITE_VdbeAddopTrace HI(0x0008)  /* Trace sqlite3VdbeAddOp() calls */
 11.5169 +#define SQLITE_VdbeEQP        HI(0x0010)  /* Debug EXPLAIN QUERY PLAN */
 11.5170 +#endif
 11.5171 +
 11.5172 +/*
 11.5173 +** Allowed values for sqlite3.mDbFlags
 11.5174 +*/
 11.5175 +#define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 11.5176 +#define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 11.5177 +#define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
 11.5178 +#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
 11.5179  
 11.5180  /*
 11.5181  ** Bits of the sqlite3.dbOptFlags field that are used by the
 11.5182 @@ -14270,18 +16426,22 @@
 11.5183  ** selectively disable various optimizations.
 11.5184  */
 11.5185  #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 11.5186 -#define SQLITE_ColumnCache    0x0002   /* Column cache */
 11.5187 +                          /*  0x0002   available for reuse */
 11.5188  #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 11.5189  #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 11.5190 -/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
 11.5191 -#define SQLITE_DistinctOpt    0x0020